Skip to content

refactor(frontend): optimize network queries and improve code readability#919

Open
zhiyunyao wants to merge 2 commits intobytedance:mainfrom
zhiyunyao:refactor/frontend
Open

refactor(frontend): optimize network queries and improve code readability#919
zhiyunyao wants to merge 2 commits intobytedance:mainfrom
zhiyunyao:refactor/frontend

Conversation

@zhiyunyao
Copy link
Contributor

PR Summary

This pull request aims to improve frontend performance, as outlined in #909 and #887 (note: fully resolving #887 requires backend efforts, which are outside the scope of this PR). The main improvements focus on optimizing network queries to reduce unnecessary data fetching.

1. Optimize Network Queries

  • useThreadStream: Added useStream with fetchStateHistory: { limit: 1 } to limit the amount of fetched data.
  • useThreads: Added select fields to minimize redundant network requests and disabled refetchOnWindowFocus to prevent unnecessary re-fetching when the window regains focus.
  • useModels: Disabled refetchOnWindowFocus to avoid unnecessary re-fetching when the window regains focus.

2. Improve Code Readability

  • ChatPage and titleOfThread: Refined thread title logic for improved clarity and maintainability.
  • loadModels: Refactored code for better readability and structure.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR focuses on improving frontend responsiveness by reducing unnecessary network fetching in thread/model hooks and simplifying thread title handling in the chat UI, aligning with the performance concerns raised in #909 and partially addressing perceived latency from #887.

Changes:

  • Limit streamed thread state history fetching and reduce query refetches on window focus.
  • Add field selection to the threads search query to reduce response payload.
  • Simplify thread title derivation and related page title logic; refactor loadModels fetch flow.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
frontend/src/core/threads/utils.ts Simplifies titleOfThread with nullish coalescing.
frontend/src/core/threads/hooks.ts Adjusts stream history fetching; optimizes threads search query and disables refetch-on-focus.
frontend/src/core/models/hooks.ts Disables refetch-on-focus for the models query.
frontend/src/core/models/api.ts Cleans up loadModels async flow (single await) but changes typing approach.
frontend/src/app/workspace/chats/[thread_id]/page.tsx Refactors title/message selection logic and document title handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…lity

- useThreadStream: Add useStream with fetchStateHistory: {limit: 1}
- useThreads: Add select fields and disable refetchOnWindowFocus
- useModels: Disable refetchOnWindowFocus
- ChatPage and titleOfThread: Improve thread title logic
- loadModels: Refactor code for better readability
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return thread.values.title;
}
return "Untitled";
return thread?.values?.title ?? "Untitled";
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

titleOfThread takes a non-optional AgentThread, but the implementation uses thread?.…. This optional chaining can mask programming errors (passing undefined) and makes the contract unclear. Either remove the thread? and rely on values?.title, or widen the parameter type to explicitly accept null | undefined if that’s intended.

Suggested change
return thread?.values?.title ?? "Untitled";
return thread.values?.title ?? "Untitled";
Copilot uses AI. Check for mistakes.
threadId: isNewThread ? undefined : threadId,
reconnectOnMount: true,
fetchStateHistory: true,
fetchStateHistory: {limit: 1},
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting: fetchStateHistory: {limit: 1} is inconsistent with the surrounding code style (spaces inside braces, and after :). Please run the formatter or update it to match the file’s existing formatting.

Suggested change
fetchStateHistory: {limit: 1},
fetchStateHistory: { limit: 1 },
Copilot uses AI. Check for mistakes.
Comment on lines +195 to +197
select: [
"thread_id", "updated_at", "values"
],
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting: the select array is split across multiple lines but keeps all elements on one line, which doesn’t match the formatting used elsewhere in this file (e.g., streamMode: ["values", ...]). Please reformat (either a single-line array or one element per line) to keep consistent style and avoid noisy diffs.

Suggested change
select: [
"thread_id", "updated_at", "values"
],
select: ["thread_id", "updated_at", "values"],
Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants