Stream model events to activity panel

This commit is contained in:
wuyang6
2026-05-11 21:24:32 +08:00
parent e89e35c6fd
commit 413a37b32c
2 changed files with 43 additions and 3 deletions
+27
View File
@@ -32,13 +32,16 @@ type ClawChatResponse = {
type ClawRuntimeEvent = {
type?: string;
run_id?: string;
tool_call_index?: number | null;
tool_name?: string;
tool_call_id?: string;
arguments?: unknown;
arguments_delta?: string;
assistant_content?: string;
metadata?: Record<string, unknown>;
ok?: boolean;
delta?: string;
finish_reason?: string | null;
elapsed_ms?: number;
};
@@ -52,6 +55,7 @@ type StreamState = {
textStreamed: boolean;
phase: "waiting" | "queued" | "running" | "done";
runId?: string;
announcedToolCallKeys: Set<string>;
};
type ChatRequestBody = {
@@ -107,6 +111,7 @@ export async function POST(req: Request) {
textStarted: false,
textStreamed: false,
phase: "waiting",
announcedToolCallKeys: new Set<string>(),
};
writer.write({ type: "start" });
writer.write({ type: "start-step" });
@@ -491,6 +496,28 @@ function writeRuntimeEvent(
delta: `\n${phase}${elapsed}`,
});
}
if (event.type === "content_delta" && event.delta) {
writer.write({
type: "reasoning-delta",
id: "reasoning-1",
delta: event.delta,
});
}
if (event.type === "tool_call_delta") {
const key =
event.tool_call_id ??
(event.tool_call_index === null || event.tool_call_index === undefined
? undefined
: `index:${event.tool_call_index}`);
if (key && !streamState?.announcedToolCallKeys.has(key)) {
streamState?.announcedToolCallKeys.add(key);
writer.write({
type: "reasoning-delta",
id: "reasoning-1",
delta: `\n准备调用 ${event.tool_name ?? "工具"}`,
});
}
}
if (event.type === "final_text_start") {
if (!streamState?.textStarted) {
writer.write({ type: "text-start", id: "text-1" });