Skip to content

Commit 6b50186

Browse files
Claudehotlong
andauthored
Fix unused handleStreamData and connect stream data to reasoning display
- Remove unused event handler that triggered CodeQL warning - Use useChat's data property to access stream events - Process reasoning-delta, step-start, and step-finish events from data array - Fixes issue where reasoning display never appeared in UI Agent-Logs-Url: https://github.com/objectstack-ai/framework/sessions/fff45c75-5fcc-435e-a01f-9c5fba9d8326 Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 0c7a42a commit 6b50186

File tree

1 file changed

+34
-48
lines changed

1 file changed

+34
-48
lines changed

apps/studio/src/components/AiChatPanel.tsx

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export function AiChatPanel() {
457457
[baseUrl, selectedAgent],
458458
);
459459

460-
const { messages, sendMessage, setMessages, status, error, addToolApprovalResponse } = useChat({
460+
const { messages, sendMessage, setMessages, status, error, addToolApprovalResponse, data } = useChat({
461461
transport,
462462
messages: initialMessages,
463463
streamMode: 'stream-data',
@@ -473,56 +473,42 @@ export function AiChatPanel() {
473473

474474
const isStreaming = status === 'streaming' || status === 'submitted';
475475

476-
// Listen to custom stream data events
476+
// Process stream data events for reasoning and step progress
477477
useEffect(() => {
478-
if (!isStreaming) return;
479-
480-
// Create a custom event listener for stream data
481-
const handleStreamData = (event: MessageEvent) => {
482-
try {
483-
const data = JSON.parse(event.data);
484-
485-
if (data.type === 'reasoning-delta') {
486-
setThinkingState((prev) => ({
487-
...prev,
488-
reasoning: [...prev.reasoning, data.delta],
489-
}));
490-
} else if (data.type === 'step-start') {
491-
setThinkingState((prev) => {
492-
const newActiveSteps = new Map(prev.activeSteps);
493-
newActiveSteps.set(data.stepId, {
494-
stepName: data.stepName,
495-
startedAt: Date.now(),
496-
});
497-
return {
498-
...prev,
499-
activeSteps: newActiveSteps,
500-
};
501-
});
502-
} else if (data.type === 'step-finish') {
503-
setThinkingState((prev) => {
504-
const newActiveSteps = new Map(prev.activeSteps);
505-
newActiveSteps.delete(data.stepId);
506-
return {
507-
...prev,
508-
activeSteps: newActiveSteps,
509-
completedSteps: [...prev.completedSteps, data.stepName],
510-
};
478+
if (!data || data.length === 0) return;
479+
480+
// Process each data event from the stream
481+
data.forEach((event: any) => {
482+
if (event.type === 'reasoning-delta') {
483+
setThinkingState((prev) => ({
484+
...prev,
485+
reasoning: [...prev.reasoning, event.delta],
486+
}));
487+
} else if (event.type === 'step-start') {
488+
setThinkingState((prev) => {
489+
const newActiveSteps = new Map(prev.activeSteps);
490+
newActiveSteps.set(event.stepId, {
491+
stepName: event.stepName,
492+
startedAt: Date.now(),
511493
});
512-
}
513-
} catch {
514-
// Ignore parsing errors for non-JSON events
494+
return {
495+
...prev,
496+
activeSteps: newActiveSteps,
497+
};
498+
});
499+
} else if (event.type === 'step-finish') {
500+
setThinkingState((prev) => {
501+
const newActiveSteps = new Map(prev.activeSteps);
502+
newActiveSteps.delete(event.stepId);
503+
return {
504+
...prev,
505+
activeSteps: newActiveSteps,
506+
completedSteps: [...prev.completedSteps, event.stepName],
507+
};
508+
});
515509
}
516-
};
517-
518-
// Note: This is a simplified approach. In production, you'd want to
519-
// integrate more deeply with the transport layer or use a custom
520-
// transport that exposes stream events.
521-
522-
return () => {
523-
// Cleanup if needed
524-
};
525-
}, [isStreaming]);
510+
});
511+
}, [data]);
526512

527513
// Persist messages to localStorage whenever they change
528514
useEffect(() => {

0 commit comments

Comments
 (0)