diff --git a/packages/adapters/openclaw-gateway/src/ui/parse-stdout.test.ts b/packages/adapters/openclaw-gateway/src/ui/parse-stdout.test.ts new file mode 100644 index 00000000000..cbea411c3b9 --- /dev/null +++ b/packages/adapters/openclaw-gateway/src/ui/parse-stdout.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, it } from "vitest"; +import { parseOpenClawGatewayStdoutLine } from "./parse-stdout.js"; + +const ts = "2026-10-21T18:44:08.160Z"; + +function event(stream: string, data: Record) { + return `[openclaw-gateway:event] run=run-1 stream=${stream} data=${JSON.stringify(data)}`; +} + +describe("parseOpenClawGatewayStdoutLine", () => { + it("keeps assistant deltas", () => { + expect(parseOpenClawGatewayStdoutLine(event("assistant", { text: "Hel", delta: "lo" }), ts)).toEqual([ + { kind: "assistant", ts, text: "lo", delta: true }, + ]); + }); + + it("maps thinking deltas to thinking entries", () => { + expect(parseOpenClawGatewayStdoutLine(event("thinking", { text: "Check the", delta: " versions" }), ts)).toEqual([ + { kind: "thinking", ts, text: " versions", delta: true }, + ]); + }); + + it("maps a full thinking text when there is no delta", () => { + expect(parseOpenClawGatewayStdoutLine(event("thinking", { text: "Plan first." }), ts)).toEqual([ + { kind: "thinking", ts, text: "Plan first." }, + ]); + }); + + it("maps a tool item start to a tool call", () => { + const line = event("item", { + itemId: "tool:call-1", + toolCallId: "call-1", + name: "read", + kind: "tool", + phase: "start", + title: "Read from skills/cve-patching/SKILL.md", + status: "running", + }); + expect(parseOpenClawGatewayStdoutLine(line, ts)).toEqual([ + { kind: "tool_call", ts, name: "read", toolUseId: "call-1", input: { title: "Read from skills/cve-patching/SKILL.md" } }, + ]); + }); + + it("maps a tool item end to a tool result, failed ones as errors", () => { + const ok = event("item", { toolCallId: "call-1", name: "read", kind: "tool", phase: "end", title: "Read SKILL.md", status: "completed" }); + const failed = event("item", { + toolCallId: "call-2", + name: "tool_search", + kind: "tool", + phase: "end", + title: "Tool Search", + status: "failed", + error: "set limit on each batch query", + }); + expect(parseOpenClawGatewayStdoutLine(ok, ts)).toEqual([ + { kind: "tool_result", ts, toolUseId: "call-1", toolName: "read", content: "Read SKILL.md", isError: false }, + ]); + expect(parseOpenClawGatewayStdoutLine(failed, ts)).toEqual([ + { kind: "tool_result", ts, toolUseId: "call-2", toolName: "tool_search", content: "set limit on each batch query", isError: true }, + ]); + }); + + it("still ignores other item kinds and streams", () => { + expect(parseOpenClawGatewayStdoutLine(event("item", { kind: "message", phase: "start" }), ts)).toEqual([]); + expect(parseOpenClawGatewayStdoutLine(event("usage", { input: 10 }), ts)).toEqual([]); + }); +}); diff --git a/packages/adapters/openclaw-gateway/src/ui/parse-stdout.ts b/packages/adapters/openclaw-gateway/src/ui/parse-stdout.ts index c8cb48ae4fa..91a1979939e 100644 --- a/packages/adapters/openclaw-gateway/src/ui/parse-stdout.ts +++ b/packages/adapters/openclaw-gateway/src/ui/parse-stdout.ts @@ -38,6 +38,41 @@ function parseAgentEventLine(line: string, ts: string): TranscriptEntry[] { return []; } + if (stream === "thinking") { + const delta = asString(data?.delta); + if (delta.length > 0) { + return [{ kind: "thinking", ts, text: delta, delta: true }]; + } + + const text = asString(data?.text); + if (text.length > 0) { + return [{ kind: "thinking", ts, text }]; + } + return []; + } + + if (stream === "item" && asString(data?.kind) === "tool") { + const toolUseId = asString(data?.toolCallId) || asString(data?.itemId); + const name = asString(data?.name) || "tool"; + const title = asString(data?.title) || asString(data?.meta); + const phase = asString(data?.phase).toLowerCase(); + if (phase === "start") { + return [{ kind: "tool_call", ts, name, toolUseId: toolUseId || undefined, input: title ? { title } : {} }]; + } + if (phase === "end") { + const status = asString(data?.status).toLowerCase(); + return [{ + kind: "tool_result", + ts, + toolUseId: toolUseId || name, + toolName: name, + content: asString(data?.error) || title || status, + isError: status !== "completed", + }]; + } + return []; + } + if (stream === "error") { const message = asString(data?.error) || asString(data?.message); return message ? [{ kind: "stderr", ts, text: message }] : [];