import { streamFrameApplies } from "../../streamScope"; import { parseTool, parseToolCall } from "../../wsParse"; import type { FrameEvent, FramePayload, StoreState } from "./shared"; /** 工具帧处理组:tool_call.start / tool_call.end / tool_call.delta / * tool_execution.update + 遗留 tool_used。命中(含组内主动丢弃)返回 true, * 未命中返回 false 交给下一组。 */ export function handleToolEvent(ev: FrameEvent, r: FramePayload, s: StoreState): boolean { switch (ev.type) { case "tool_call.start": case "tool_call.end": { if (!streamFrameApplies(ev, r, s.activeSessionId)) return true; const tool = parseToolCall(ev.data); if (tool) s.addTool(tool); return true; } case "tool_call.delta": { // Argument fragments stream between start/end; the card is added at the // boundary so mid-stream reconnects do not drop it. return true; } case "tool_execution.update": { // Same card as tool_call.* — same guard. if (!streamFrameApplies(ev, r, s.activeSessionId)) return true; const tool = parseToolCall(ev.data); if (tool) s.addTool(tool); return true; } case "tool_used": { // Legacy tool echo — same card as tool_call.*. if (!streamFrameApplies(ev, r, s.activeSessionId)) return true; const tool = parseTool(ev.data); if (tool) s.addTool(tool); return true; } default: return false; } }