Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/10758.yml
Original file line number Diff line number Diff line change
@@ -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))
10 changes: 8 additions & 2 deletions src/plugins/chat/public/components/chat_window.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ function ChatWindowContent({
const [input, setInput] = useState('');
const [isStreaming, setIsStreaming] = useState(false);
const [currentRunId, setCurrentRunId] = useState<string | null>(null);

const timelineRef = React.useRef<Message[]>(timeline);

React.useEffect(() => {
timelineRef.current = timeline;
}, [timeline]);

// Create the event handler using useMemo
const eventHandler = useMemo(
Expand All @@ -69,9 +75,9 @@ function ChatWindowContent({
chatService,
setTimeline,
setIsStreaming,
() => timeline
() => timelineRef.current
),
[service, chatService, timeline] // Only recreate if services change
[service, chatService]
);

// Register actions
Expand Down
17 changes: 5 additions & 12 deletions src/plugins/chat/public/services/chat_event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -494,13 +484,16 @@ export class ChatEventHandler {
*/
private async sendToolResultToAssistant(toolCallId: string, result: any): Promise<void> {
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);

Expand Down
Loading