-
Notifications
You must be signed in to change notification settings - Fork 52.6k
fix(desktop): preserve pre-tool-call text parts on message completion #64492
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
Closed
isukennedy
wants to merge
2
commits into
NousResearch:main
from
isukennedy:fix/desktop-preserve-pre-tool-text
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
128 changes: 128 additions & 0 deletions
128
apps/desktop/src/app/session/hooks/use-message-stream/pre-tool-text.test.tsx
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 |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { QueryClient } from '@tanstack/react-query' | ||
| import { act, cleanup, render, waitFor } from '@testing-library/react' | ||
| import { type MutableRefObject, useEffect, useRef } from 'react' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import type { ClientSessionState } from '@/app/types' | ||
| import { createClientSessionState } from '@/lib/chat-runtime' | ||
| import type { RpcEvent } from '@/types/hermes' | ||
|
|
||
| import { useMessageStream } from './index' | ||
|
|
||
| const SID = 'session-1' | ||
|
|
||
| let handleEvent: ((event: RpcEvent) => void) | null = null | ||
| let sessionStateRef: MutableRefObject<Map<string, ClientSessionState>> | null = null | ||
|
|
||
| function Harness() { | ||
| const activeSessionIdRef = useRef<string | null>(SID) | ||
| const sessionStateByRuntimeIdRef = useRef(new Map<string, ClientSessionState>()) | ||
| const queryClientRef = useRef(new QueryClient()) | ||
|
|
||
| // Expose for tests | ||
| sessionStateRef = sessionStateByRuntimeIdRef | ||
|
|
||
| const stream = useMessageStream({ | ||
| activeSessionIdRef, | ||
| hydrateFromStoredSession: vi.fn(async () => undefined), | ||
| queryClient: queryClientRef.current, | ||
| refreshHermesConfig: vi.fn(async () => undefined), | ||
| refreshSessions: vi.fn(async () => undefined), | ||
| sessionStateByRuntimeIdRef, | ||
| updateSessionState: (sessionId, updater) => { | ||
| const current = sessionStateByRuntimeIdRef.current.get(sessionId) ?? createClientSessionState() | ||
| const next = updater(current) | ||
| sessionStateByRuntimeIdRef.current.set(sessionId, next) | ||
|
|
||
| return next | ||
| } | ||
| }) | ||
|
|
||
| useEffect(() => { | ||
| handleEvent = stream.handleGatewayEvent | ||
| }, [stream.handleGatewayEvent]) | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| async function mountStream() { | ||
| render(<Harness />) | ||
| await waitFor(() => expect(handleEvent).not.toBeNull()) | ||
| } | ||
|
|
||
| function getState() { | ||
| return sessionStateRef!.current.get(SID)! | ||
| } | ||
|
|
||
| describe('useMessageStream pre-tool-call text preservation', () => { | ||
| beforeEach(() => { | ||
| handleEvent = null | ||
| sessionStateRef = null | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| cleanup() | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it('preserves text streamed before tool calls when the turn completes', async () => { | ||
| await mountStream() | ||
|
|
||
| // Simulate: message.start -> text delta (pre-tool narration) -> tool.start -> tool.complete -> text delta (post-tool) -> message.complete | ||
| act(() => handleEvent!({ payload: {}, session_id: SID, type: 'message.start' })) | ||
|
|
||
| // Pre-tool-call narration | ||
| act(() => handleEvent!({ payload: { text: 'Here is what I found: ' }, session_id: SID, type: 'message.delta' })) | ||
| act(() => handleEvent!({ payload: { text: 'let me check the files.' }, session_id: SID, type: 'message.delta' })) | ||
|
|
||
| // Tool call | ||
| act(() => | ||
| handleEvent!({ | ||
| payload: { name: 'search_files', tool_id: 'tool-1', args: { pattern: 'test' } }, | ||
| session_id: SID, | ||
| type: 'tool.start' | ||
| }) | ||
| ) | ||
|
|
||
| act(() => | ||
| handleEvent!({ | ||
| payload: { name: 'search_files', tool_id: 'tool-1', result: { matches: [] } }, | ||
| session_id: SID, | ||
| type: 'tool.complete' | ||
| }) | ||
| ) | ||
|
|
||
| // Post-tool-call narration | ||
| act(() => handleEvent!({ payload: { text: ' The results show X.' }, session_id: SID, type: 'message.delta' })) | ||
|
|
||
| // Completion — final_response only contains the last API call's text | ||
| act(() => | ||
| handleEvent!({ | ||
| payload: { text: 'The results show X.' }, | ||
| session_id: SID, | ||
| type: 'message.complete' | ||
| }) | ||
| ) | ||
|
|
||
| const state = getState() | ||
| const assistantMsg = state.messages.find(m => m.role === 'assistant') | ||
| expect(assistantMsg).toBeDefined() | ||
|
|
||
| const parts = assistantMsg!.parts | ||
|
|
||
| // Should have: text (pre-tool), tool-call, text (post-tool) | ||
| const textParts = parts.filter(p => p.type === 'text') | ||
| const toolParts = parts.filter(p => p.type === 'tool-call') | ||
|
|
||
| // Pre-tool-call narration must survive | ||
| expect(textParts.length).toBeGreaterThanOrEqual(2) | ||
| const allText = textParts.map(p => p.text).join('') | ||
| expect(allText).toContain('Here is what I found:') | ||
| expect(allText).toContain('let me check the files.') | ||
| expect(allText).toContain('The results show X.') | ||
|
|
||
| // Tool row must survive | ||
| expect(toolParts.length).toBe(1) | ||
| expect(toolParts[0].toolName).toBe('search_files') | ||
| }) | ||
| }) |
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.
Please add a hook-level regression test for pre-tool
message.deltatext followed by a tool event andmessage.complete; this boundary is the behavior the change must preserve.