From aa61ac4a6a19290aebf1160f2312271069bf24b8 Mon Sep 17 00:00:00 2001 From: Jiaping Zeng Date: Fri, 17 Oct 2025 22:56:05 -0700 Subject: [PATCH 1/2] fix tool result messages not containing latest user question Signed-off-by: Jiaping Zeng --- .../chat/public/components/chat_window.tsx | 10 ++++++++-- .../chat/public/services/chat_event_handler.ts | 17 +++++------------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/plugins/chat/public/components/chat_window.tsx b/src/plugins/chat/public/components/chat_window.tsx index 8a27c2c191ad..24590eefb0c5 100644 --- a/src/plugins/chat/public/components/chat_window.tsx +++ b/src/plugins/chat/public/components/chat_window.tsx @@ -60,6 +60,12 @@ function ChatWindowContent({ const [input, setInput] = useState(''); const [isStreaming, setIsStreaming] = useState(false); const [currentRunId, setCurrentRunId] = useState(null); + + const timelineRef = React.useRef(timeline); + + React.useEffect(() => { + timelineRef.current = timeline; + }, [timeline]); // Create the event handler using useMemo const eventHandler = useMemo( @@ -69,9 +75,9 @@ function ChatWindowContent({ chatService, setTimeline, setIsStreaming, - () => timeline + () => timelineRef.current ), - [service, chatService, timeline] // Only recreate if services change + [service, chatService] ); // Register actions diff --git a/src/plugins/chat/public/services/chat_event_handler.ts b/src/plugins/chat/public/services/chat_event_handler.ts index 78413928a169..391539c408a9 100644 --- a/src/plugins/chat/public/services/chat_event_handler.ts +++ b/src/plugins/chat/public/services/chat_event_handler.ts @@ -300,16 +300,6 @@ export class ChatEventHandler { result: result.data, }); - // Add tool result message to timeline - const toolMessage: ToolMessage = { - id: `tool-result-${toolCallId}`, - role: 'tool', - content: typeof result.data === 'string' ? result.data : JSON.stringify(result.data), - toolCallId, - }; - - this.onTimelineUpdate((prev) => [...prev, toolMessage]); - // Send tool result back to assistant if chatService is available if (this.chatService && (this.chatService as any).sendToolResult) { await this.sendToolResultToAssistant(toolCallId, result.data); @@ -494,13 +484,16 @@ export class ChatEventHandler { */ private async sendToolResultToAssistant(toolCallId: string, result: any): Promise { try { - const messages = this.getTimeline(); // Now pass timeline directly - no transformation needed - const { observable } = await (this.chatService as any).sendToolResult( + const messages = this.getTimeline(); + + const { observable, toolMessage } = await (this.chatService as any).sendToolResult( toolCallId, result, messages ); + this.onTimelineUpdate((prev) => [...prev, toolMessage]); + // Set streaming state and subscribe to the response stream this.onStreamingStateChange(true); From e19cffd6c98c5dba0d48395628169d94641aed8e Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Tue, 21 Oct 2025 18:49:25 +0000 Subject: [PATCH 2/2] Changeset file for PR #10758 created/updated --- changelogs/fragments/10758.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/10758.yml diff --git a/changelogs/fragments/10758.yml b/changelogs/fragments/10758.yml new file mode 100644 index 000000000000..5cc910fff6f0 --- /dev/null +++ b/changelogs/fragments/10758.yml @@ -0,0 +1,2 @@ +fix: +- [BUG][Chat] fix tool result messages not containing latest user question ([#10758](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/10758)) \ No newline at end of file