| 63 | // 工具结果不走 messages 流,而是以 method=tools 的 stream_event 事件返回(tool-started/tool-finished)。 |
| 64 | // 取出 tool-finished 的 output(一条 ToolMessage 字典),交给 msgChunks 与 AI 消息按 tool_call_id 关联。 |
| 65 | const toolFinishedMessage = (chunk) => { |
| 66 | const streamEvent = chunk?.event |
| 67 | if (!streamEvent || streamEvent.method !== 'tools') return null |
| 68 | |
| 69 | const data = streamEvent.data |
| 70 | if (!data || data.event !== 'tool-finished') return null |
| 71 | |
| 72 | const output = data.output |
| 73 | if (!output || typeof output !== 'object') return null |
| 74 | |
| 75 | const id = output.id || output.tool_call_id || data.tool_call_id |
| 76 | if (!id) return null |
| 77 | return { ...output, type: 'tool', id } |
| 78 | } |
| 79 | |
| 80 | export function useAgentStreamHandler({ |
| 81 | getThreadState, |