-
Notifications
You must be signed in to change notification settings - Fork 9.5k
fix: render assistant text bubble for messages with tool calls #3315
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
Open
xingzhiyyu
wants to merge
5
commits into
bytedance:main
Choose a base branch
from
xingzhiyyu:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+329
−6
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1321eb
fix: render assistant text bubble for messages with tool calls
8909d87
fix(frontend): show visible text for subagent and present-files messages
c8aaf66
chore: prettier format
8c51fad
fix(frontend): keep present-files grouped
b87b770
test(frontend): assert subagent content grouping
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
280 changes: 280 additions & 0 deletions
280
frontend/tests/unit/core/messages/utils-regression.test.ts
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,280 @@ | ||
| import type { AIMessage, Message } from "@langchain/langgraph-sdk"; | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import { | ||
| extractPresentFilesFromMessage, | ||
| getMessageGroups, | ||
| } from "@/core/messages/utils"; | ||
|
|
||
| describe("regression: tool-call messages must not swallow text/reasoning content", () => { | ||
| test("AI message with tool_calls + text content generates both processing and assistant bubble", () => { | ||
| const messages = [ | ||
| { | ||
| id: "human-1", | ||
| type: "human", | ||
| content: "Search for deer", | ||
| }, | ||
| { | ||
| id: "ai-1", | ||
| type: "ai", | ||
| content: "Let me search that for you", | ||
| tool_calls: [ | ||
| { id: "tc-1", name: "web_search", args: { query: "deer" } }, | ||
| ], | ||
| }, | ||
| { | ||
| id: "tool-1", | ||
| type: "tool", | ||
| name: "web_search", | ||
| tool_call_id: "tc-1", | ||
| content: "Results about deer", | ||
| }, | ||
| { | ||
| id: "ai-2", | ||
| type: "ai", | ||
| content: "Here is what I found", | ||
| }, | ||
| ] as Message[]; | ||
|
|
||
| const groups = getMessageGroups(messages); | ||
|
|
||
| expect(groups.map((g) => g.type)).toEqual([ | ||
| "human", | ||
| "assistant", | ||
| "assistant:processing", | ||
| "assistant", | ||
| ]); | ||
|
|
||
| // The assistant bubble with the tool-call message text must exist | ||
| // and contain the visible content. | ||
| const assistantGroups = groups.filter((g) => g.type === "assistant"); | ||
| expect(assistantGroups).toHaveLength(2); | ||
| expect(assistantGroups[0]!.messages).toHaveLength(1); | ||
| expect(assistantGroups[0]!.messages[0]!.content).toBe( | ||
| "Let me search that for you", | ||
| ); | ||
|
|
||
| // The processing group must contain the tool-call AI + tool result. | ||
| const processingGroup = groups.find( | ||
| (g) => g.type === "assistant:processing", | ||
| ); | ||
| expect(processingGroup).toBeDefined(); | ||
| expect(processingGroup!.messages.map((m) => m.id)).toEqual([ | ||
| "ai-1", | ||
| "tool-1", | ||
| ]); | ||
| }); | ||
|
|
||
| test("AI message with reasoning + tool_calls + text content produces all three", () => { | ||
| const messages = [ | ||
| { | ||
| id: "human-1", | ||
| type: "human", | ||
| content: "Hello", | ||
| }, | ||
| { | ||
| id: "ai-1", | ||
| type: "ai", | ||
| content: "<think>need to search</think>Let me check", | ||
| tool_calls: [{ id: "tc-1", name: "web_search", args: {} }], | ||
| }, | ||
| ] as Message[]; | ||
|
|
||
| const groups = getMessageGroups(messages); | ||
|
|
||
| // Order: human, assistant bubble (before processing), processing | ||
| expect(groups.map((g) => g.type)).toEqual([ | ||
| "human", | ||
| "assistant", | ||
| "assistant:processing", | ||
| ]); | ||
|
|
||
| const assistantGroup = groups.find((g) => g.type === "assistant"); | ||
| expect(assistantGroup).toBeDefined(); | ||
| // The visible content after stripping <think> should be "Let me check" | ||
| expect(assistantGroup!.messages[0]!.content).toContain("Let me check"); | ||
|
|
||
| // The processing group must be the last group (after the assistant bubble) | ||
| // and contain both reasoning and tool_calls. | ||
| const processingGroup = groups.at(-1); | ||
| expect(processingGroup?.type).toBe("assistant:processing"); | ||
| expect(processingGroup!.messages).toHaveLength(1); | ||
|
|
||
| const processingMessage = processingGroup!.messages[0]! as AIMessage; | ||
| expect(processingMessage.id).toBe("ai-1"); | ||
| // Both reasoning (<think>) and tool_calls must be present | ||
| expect(processingMessage.content).toContain("<think>"); | ||
| expect(processingMessage.tool_calls).toBeDefined(); | ||
| expect(processingMessage.tool_calls).toHaveLength(1); | ||
| }); | ||
|
|
||
| test("plain AI answer without tool_calls produces only an assistant bubble", () => { | ||
| const messages = [ | ||
| { id: "human-1", type: "human", content: "Hi" }, | ||
| { id: "ai-1", type: "ai", content: "Hello there" }, | ||
| ] as Message[]; | ||
|
|
||
| const groups = getMessageGroups(messages); | ||
| expect(groups.map((g) => g.type)).toEqual(["human", "assistant"]); | ||
| }); | ||
|
|
||
| test("AI message with files presented will not be displayed twice in processing and assistant bubble", () => { | ||
| const messages = [ | ||
| { id: "human-1", type: "human", content: "Hi" }, | ||
| { | ||
| id: "ai-1", | ||
| type: "ai", | ||
| content: "Here are the files you requested", | ||
| tool_calls: [ | ||
| { | ||
| id: "tc-1", | ||
| name: "present_files", | ||
| args: { | ||
| filepaths: ["/path/to/file1.txt", "test.txt"], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ] as Message[]; | ||
|
|
||
| const groups = getMessageGroups(messages); | ||
| expect(groups.map((g) => g.type)).toEqual([ | ||
| "human", | ||
| "assistant:present-files", | ||
| ]); | ||
| expect(groups[1]!.messages).toHaveLength(1); | ||
| expect(groups[1]!.messages[0]!.content).toBe( | ||
| "Here are the files you requested", | ||
| ); | ||
| expect(extractPresentFilesFromMessage(groups[1]!.messages[0]!)).toEqual([ | ||
| "/path/to/file1.txt", | ||
| "test.txt", | ||
| ]); | ||
| }); | ||
|
|
||
| test("present_files tool result attaches to the present-files group", () => { | ||
| const messages = [ | ||
| { id: "human-1", type: "human", content: "Show me the report" }, | ||
| { | ||
| id: "ai-1", | ||
| type: "ai", | ||
| content: "Here is the report.", | ||
| tool_calls: [ | ||
| { | ||
| id: "tc-1", | ||
| name: "present_files", | ||
| args: { | ||
| filepaths: ["/mnt/user-data/outputs/report.md"], | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| id: "tool-1", | ||
| type: "tool", | ||
| name: "present_files", | ||
| tool_call_id: "tc-1", | ||
| content: "Successfully presented files", | ||
| }, | ||
| ] as Message[]; | ||
|
|
||
| const groups = getMessageGroups(messages); | ||
|
|
||
| expect(groups.map((g) => g.type)).toEqual([ | ||
| "human", | ||
| "assistant:present-files", | ||
| ]); | ||
| expect(groups[1]!.messages.map((message) => message.id)).toEqual([ | ||
| "ai-1", | ||
| "tool-1", | ||
| ]); | ||
| }); | ||
|
|
||
| test("subagent AI message with content creates assistant bubble and subagent group", () => { | ||
| const messages = [ | ||
| { id: "human-1", type: "human", content: "Delegate this" }, | ||
| { | ||
| id: "ai-1", | ||
| type: "ai", | ||
| content: "Launching a subagent to help.", | ||
| tool_calls: [ | ||
| { | ||
| id: "tc-1", | ||
| name: "task", | ||
| args: { | ||
| subagent_type: "general-purpose", | ||
| description: "Inspect the issue", | ||
| prompt: "Inspect the issue", | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ] as Message[]; | ||
|
|
||
| const groups = getMessageGroups(messages); | ||
|
|
||
| expect(groups.map((g) => g.type)).toEqual([ | ||
| "human", | ||
| "assistant", | ||
| "assistant:subagent", | ||
| ]); | ||
| expect(groups[1]!.messages.map((message) => message.id)).toEqual(["ai-1"]); | ||
| expect(groups[1]!.messages[0]!.content).toBe( | ||
| "Launching a subagent to help.", | ||
| ); | ||
| expect(groups[2]!.messages.map((message) => message.id)).toEqual(["ai-1"]); | ||
| expect(groups[2]!.messages[0]!.type).toBe("ai"); | ||
| expect(groups[2]!.messages[0]!.tool_calls).toEqual([ | ||
| { | ||
| id: "tc-1", | ||
| name: "task", | ||
| args: { | ||
| subagent_type: "general-purpose", | ||
| description: "Inspect the issue", | ||
| prompt: "Inspect the issue", | ||
| }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("subagent tool result attaches to the subagent group", () => { | ||
| const messages = [ | ||
| { id: "human-1", type: "human", content: "Delegate this" }, | ||
| { | ||
| id: "ai-1", | ||
| type: "ai", | ||
| content: "Launching a subagent to help.", | ||
| tool_calls: [ | ||
| { | ||
| id: "tc-1", | ||
| name: "task", | ||
| args: { | ||
| subagent_type: "general-purpose", | ||
| description: "Inspect the issue", | ||
| prompt: "Inspect the issue", | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| id: "tool-1", | ||
| type: "tool", | ||
| name: "task", | ||
| tool_call_id: "tc-1", | ||
| content: "Done", | ||
| }, | ||
| ] as Message[]; | ||
|
|
||
| const groups = getMessageGroups(messages); | ||
|
|
||
| expect(groups.map((g) => g.type)).toEqual([ | ||
| "human", | ||
| "assistant", | ||
| "assistant:subagent", | ||
| ]); | ||
| expect(groups[2]!.messages.map((message) => message.id)).toEqual([ | ||
| "ai-1", | ||
| "tool-1", | ||
| ]); | ||
| }); | ||
| }); |
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.
@copilot Simply answer me in Chinese,can the issue be resolved by placing this branch after the "needprocessing" branch? Don‘t make any chance to the code.