|
| 1 | +import pc from "picocolors"; |
| 2 | + |
| 3 | +function safeJsonParse(text: string): unknown { |
| 4 | + try { |
| 5 | + return JSON.parse(text); |
| 6 | + } catch { |
| 7 | + return null; |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +function asRecord(value: unknown): Record<string, unknown> | null { |
| 12 | + if (typeof value !== "object" || value === null || Array.isArray(value)) return null; |
| 13 | + return value as Record<string, unknown>; |
| 14 | +} |
| 15 | + |
| 16 | +function asString(value: unknown, fallback = ""): string { |
| 17 | + return typeof value === "string" ? value : fallback; |
| 18 | +} |
| 19 | + |
| 20 | +function extractTextContent(content: string | Array<{ type: string; text?: string }>): string { |
| 21 | + if (typeof content === "string") return content; |
| 22 | + if (!Array.isArray(content)) return ""; |
| 23 | + return content |
| 24 | + .filter((c) => c.type === "text" && c.text) |
| 25 | + .map((c) => c.text!) |
| 26 | + .join(""); |
| 27 | +} |
| 28 | + |
| 29 | +export function printPiStreamEvent(raw: string, _debug: boolean): void { |
| 30 | + const line = raw.trim(); |
| 31 | + if (!line) return; |
| 32 | + |
| 33 | + const parsed = asRecord(safeJsonParse(line)); |
| 34 | + if (!parsed) { |
| 35 | + console.log(line); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const type = asString(parsed.type); |
| 40 | + |
| 41 | + if (type === "agent_start") { |
| 42 | + console.log(pc.blue("Pi agent started")); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (type === "agent_end") { |
| 47 | + console.log(pc.blue("Pi agent finished")); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (type === "turn_start") { |
| 52 | + console.log(pc.blue("Turn started")); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + if (type === "turn_end") { |
| 57 | + const message = asRecord(parsed.message); |
| 58 | + if (message) { |
| 59 | + const content = message.content as string | Array<{ type: string; text?: string }>; |
| 60 | + const text = extractTextContent(content); |
| 61 | + if (text) { |
| 62 | + console.log(pc.green(`assistant: ${text}`)); |
| 63 | + } |
| 64 | + } |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (type === "message_update") { |
| 69 | + const assistantEvent = asRecord(parsed.assistantMessageEvent); |
| 70 | + if (assistantEvent) { |
| 71 | + const msgType = asString(assistantEvent.type); |
| 72 | + if (msgType === "text_delta") { |
| 73 | + const delta = asString(assistantEvent.delta); |
| 74 | + if (delta) { |
| 75 | + console.log(pc.green(delta)); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + if (type === "tool_execution_start") { |
| 83 | + const toolName = asString(parsed.toolName); |
| 84 | + const args = parsed.args; |
| 85 | + console.log(pc.yellow(`tool_start: ${toolName}`)); |
| 86 | + if (args !== undefined) { |
| 87 | + try { |
| 88 | + console.log(pc.gray(JSON.stringify(args, null, 2))); |
| 89 | + } catch { |
| 90 | + console.log(pc.gray(String(args))); |
| 91 | + } |
| 92 | + } |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (type === "tool_execution_end") { |
| 97 | + const result = parsed.result; |
| 98 | + const isError = parsed.isError === true; |
| 99 | + const output = typeof result === "string" ? result : JSON.stringify(result); |
| 100 | + if (output) { |
| 101 | + console.log((isError ? pc.red : pc.gray)(output)); |
| 102 | + } |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + console.log(line); |
| 107 | +} |
0 commit comments