-
Notifications
You must be signed in to change notification settings - Fork 106
chore(web): delete needsNewBubbleRef latch, derive from message tail #31904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,20 @@ export function finalizeRunningToolCalls( | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Whether the next streaming chunk should extend the tail bubble or start | ||
| * a fresh one. Derived directly from the message array — the boundary | ||
| * events that previously latched this decision (idle, message_complete, | ||
| * generation_handoff, generation_cancelled, dequeued, conversation switch) | ||
| * all leave the tail in a state where `isStreaming` is either `false` or | ||
| * the tail is no longer an assistant row, so the derivation answers | ||
| * correctly without any shared flag. | ||
| */ | ||
| export function tailIsStreamingAssistant(prev: DisplayMessage[]): boolean { | ||
| const last = prev[prev.length - 1]; | ||
| return !!last && last.role === "assistant" && !!last.isStreaming; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // assistant_text_delta | ||
| // --------------------------------------------------------------------------- | ||
|
|
@@ -58,15 +72,28 @@ export function createStreamingBubble( | |
| ]; | ||
| } | ||
|
|
||
| /** Append text to the last streaming assistant bubble. */ | ||
| /** | ||
| * Append text to the streaming assistant tail bubble, creating one if the | ||
| * tail isn't a streaming assistant row. | ||
| * | ||
| * The "should I open a new bubble" question is derived from the message | ||
| * array itself — when the previous turn finalized (via `finalizeOnIdle`, | ||
| * `finalizeMessageComplete`, `stopStreaming`, conversation switch, or a | ||
| * user message append), the tail's `isStreaming` flag is already false | ||
| * (or the tail is no longer an assistant row), so this updater branches | ||
| * to `createStreamingBubble` without needing a shared latch. | ||
| */ | ||
| export function appendTextDelta( | ||
| prev: DisplayMessage[], | ||
| text: string, | ||
| messageId?: string, | ||
| stableId?: string, | ||
| ): DisplayMessage[] { | ||
| const last = prev[prev.length - 1]; | ||
| if (!last || last.role !== "assistant" || !last.isStreaming) return prev; | ||
| if (!tailIsStreamingAssistant(prev)) { | ||
| return createStreamingBubble(prev, text, messageId, stableId); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In our conversation focused on simplifying client state, let's add an item to our list for removing
|
||
| } | ||
|
|
||
| const last = prev[prev.length - 1]!; | ||
| const segments = [...(last.textSegments ?? [])]; | ||
| const order = [...(last.contentOrder ?? [])]; | ||
| const lastOrderEntry = order[order.length - 1]; | ||
|
|
@@ -102,14 +129,28 @@ export function appendTextDelta( | |
| // assistant_activity_state (idle) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /** Finalize running tool calls on ALL streaming assistant messages (idle signal). */ | ||
| /** | ||
| * Finalize all streaming assistant messages when the daemon signals turn | ||
| * idle. Sets `isStreaming: false` on each streaming assistant row and | ||
| * marks any running tool calls as completed. | ||
| * | ||
| * Flipping `isStreaming` here is what lets `appendTextDelta` / | ||
| * `upsertToolCall` derive "next chunk should open a new bubble" from the | ||
| * message array alone — without this, the previous turn's tail would | ||
| * still look like a streaming assistant when the next turn's first chunk | ||
| * arrives, and the next chunk would erroneously extend it. | ||
| */ | ||
| export function finalizeOnIdle(prev: DisplayMessage[]): DisplayMessage[] { | ||
| let changed = false; | ||
| const updated = prev.map((m) => { | ||
| if (m.role !== "assistant" || !m.isStreaming) return m; | ||
| if (!m.toolCalls?.some((tc) => tc.status === "running")) return m; | ||
| changed = true; | ||
| return { ...m, toolCalls: finalizeRunningToolCalls(m.toolCalls)! }; | ||
| const finalized = finalizeRunningToolCalls(m.toolCalls); | ||
| return { | ||
| ...m, | ||
| isStreaming: false, | ||
| ...(finalized ? { toolCalls: finalized } : {}), | ||
| }; | ||
| }); | ||
| return changed ? updated : prev; | ||
| } | ||
|
|
@@ -389,21 +430,23 @@ export function completeSurface( | |
| // tool_use_start | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| /** Insert or update a tool call on an assistant message. */ | ||
| /** | ||
| * Insert or update a tool call on the streaming assistant tail bubble, | ||
| * creating a new bubble if the tail isn't a streaming assistant row. | ||
| * | ||
| * The bubble-creation decision is derived from `prev` itself — no shared | ||
| * latch passes through. Same finalization invariant as `appendTextDelta`: | ||
| * boundary events leave the tail with `isStreaming: false` (or non- | ||
| * assistant), so this updater opens a fresh bubble correctly. | ||
| */ | ||
| export function upsertToolCall( | ||
| prev: DisplayMessage[], | ||
| toolCall: ChatMessageToolCall, | ||
| shouldCreateNewBubble: boolean, | ||
| stableId?: string, | ||
| ): DisplayMessage[] { | ||
| const lastIdx = prev.length - 1; | ||
| const last = prev[lastIdx]; | ||
|
|
||
| if ( | ||
| !shouldCreateNewBubble && | ||
| last?.role === "assistant" && | ||
| last.isStreaming | ||
| ) { | ||
| if (tailIsStreamingAssistant(prev)) { | ||
| const lastIdx = prev.length - 1; | ||
| const last = prev[lastIdx]!; | ||
| const existingIdx = | ||
| last.toolCalls?.findIndex((tc) => tc.id === toolCall.id) ?? -1; | ||
| if (existingIdx !== -1) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,6 @@ export interface UseConversationSwitchParams { | |
| // Refs owned by the parent that the reset clears or refreshes. | ||
| draftConversationIdResolutionRef: MutableRefObject<boolean>; | ||
| previousConversationIdRef: MutableRefObject<string | null>; | ||
| needsNewBubbleRef: MutableRefObject<boolean>; | ||
| streamingMessageIdsRef: MutableRefObject<Set<string>>; | ||
| pendingQueuedStableIdsRef: MutableRefObject<string[]>; | ||
| requestIdToStableIdRef: MutableRefObject<Map<string, string>>; | ||
|
|
@@ -87,7 +86,6 @@ export function useConversationSwitch({ | |
| activeConversationId, | ||
| draftConversationIdResolutionRef, | ||
| previousConversationIdRef, | ||
| needsNewBubbleRef, | ||
| streamingMessageIdsRef, | ||
| pendingQueuedStableIdsRef, | ||
| requestIdToStableIdRef, | ||
|
|
@@ -143,7 +141,8 @@ export function useConversationSwitch({ | |
| // Reset all per-conversation state so nothing leaks between threads. | ||
| useTurnStore.getState().resetTurn(); | ||
| setIsLoadingHistory(true); | ||
| needsNewBubbleRef.current = true; | ||
| // `setMessages([])` makes the tail derivation return "create new bubble" | ||
| // for any subsequent stream event — no separate latch needed. | ||
|
Comment on lines
+144
to
+145
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete this comment |
||
| setMessages([]); | ||
| streamingMessageIdsRef.current.clear(); | ||
| pendingQueuedStableIdsRef.current = []; | ||
|
|
@@ -187,7 +186,6 @@ export function useConversationSwitch({ | |
| previousConversationIdRef, | ||
| contextWindowUsageByConversationRef, | ||
| dismissedSurfaceIdsRef, | ||
| needsNewBubbleRef, | ||
| streamingMessageIdsRef, | ||
| pendingQueuedStableIdsRef, | ||
| requestIdToStableIdRef, | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our conversation focused on simplifying client state, let's add an item to our list for removing
isStreamingfrom messages.Messages don't have streaming state. they are just the packets of data. the only thing that has streaming state is the assistant itself on any given conversation