|
| 1 | +import { openSync, readSync, closeSync, existsSync } from "node:fs"; |
| 2 | +import { randomUUID } from "node:crypto"; |
| 3 | + |
| 4 | +export function isRecord(v: unknown): v is Record<string, unknown> { |
| 5 | + return typeof v === "object" && v !== null && !Array.isArray(v); |
| 6 | +} |
| 7 | + |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | +// JSONL record types (Claude CLI streaming output) |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | + |
| 12 | +type TextBlock = { type: "text"; text: string }; |
| 13 | +type ToolUseBlock = { type: "tool_use"; name: string; input?: Record<string, unknown> }; |
| 14 | +type ThinkingBlock = { type: "thinking" }; |
| 15 | +type ToolResultBlock = { type: "tool_result"; is_error?: boolean; content?: string | unknown[] }; |
| 16 | + |
| 17 | +type ContentBlock = TextBlock | ToolUseBlock | ThinkingBlock | ToolResultBlock; |
| 18 | + |
| 19 | +type AssistantRecord = { |
| 20 | + type: "assistant"; |
| 21 | + message: { content: ContentBlock[] }; |
| 22 | +}; |
| 23 | + |
| 24 | +type UserRecord = { |
| 25 | + type: "user"; |
| 26 | + message: { content: ContentBlock[] }; |
| 27 | +}; |
| 28 | + |
| 29 | +type ContentBlockDeltaRecord = { |
| 30 | + type: "content_block_delta"; |
| 31 | + delta: { type: "text_delta"; text: string }; |
| 32 | +}; |
| 33 | + |
| 34 | +type ResultRecord = { |
| 35 | + type: "result"; |
| 36 | + subtype?: "success" | "error"; |
| 37 | + is_error?: boolean; |
| 38 | + result?: string; |
| 39 | + error?: string; |
| 40 | +}; |
| 41 | + |
| 42 | +export type JsonlRecord = AssistantRecord | UserRecord | ContentBlockDeltaRecord | ResultRecord; |
| 43 | + |
| 44 | +function truncate(s: string, n: number): string { |
| 45 | + return s.length > n ? s.slice(0, n) + "..." : s; |
| 46 | +} |
| 47 | + |
| 48 | +function redactSensitive(input: string): string { |
| 49 | + return input |
| 50 | + .replace(/AKIA[A-Z0-9]{16}/g, "[REDACTED]") |
| 51 | + .replace(/sk-ant-[A-Za-z0-9\-_]+/g, "[REDACTED]") |
| 52 | + .replace(/sk-[A-Za-z0-9]{32,}/g, "[REDACTED]") |
| 53 | + .replace(/Bearer [A-Za-z0-9._\-]+/g, "Bearer [REDACTED]") |
| 54 | + .replace(/-----BEGIN [A-Z ]+ KEY-----/g, "[REDACTED]"); |
| 55 | +} |
| 56 | + |
| 57 | +function summarizeToolInput(name: string, input: Record<string, unknown>): string { |
| 58 | + const filePath = input.file_path ?? input.path; |
| 59 | + if (typeof filePath === "string") return `Tool: ${name}(${truncate(filePath, 80)})`; |
| 60 | + if (typeof input.command === "string") return `Tool: ${name}(${truncate(input.command, 80)})`; |
| 61 | + if (typeof input.pattern === "string") return `Tool: ${name}(${truncate(input.pattern, 80)})`; |
| 62 | + return `Tool: ${name}`; |
| 63 | +} |
| 64 | + |
| 65 | +function summarizeToolResult(block: ToolResultBlock): string { |
| 66 | + if (block.is_error === true) return "Tool error"; |
| 67 | + const content = block.content; |
| 68 | + if (typeof content === "string" && content.length > 0) return `Tool result: ${truncate(content, 120)}`; |
| 69 | + if (Array.isArray(content)) { |
| 70 | + for (const part of content) { |
| 71 | + if (isRecord(part) && part.type === "text" && typeof part.text === "string") { |
| 72 | + return `Tool result: ${truncate(part.text, 120)}`; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return "Tool result"; |
| 77 | +} |
| 78 | + |
| 79 | +/** Accepts a parsed JSONL record (untrusted) and returns a display summary, or null to skip. */ |
| 80 | +export function summarizeJsonlRecord(record: Record<string, unknown>): string | null { |
| 81 | + const typed = record as JsonlRecord; |
| 82 | + |
| 83 | + switch (typed.type) { |
| 84 | + case "assistant": |
| 85 | + case "user": { |
| 86 | + const message = isRecord(typed.message) ? typed.message : null; |
| 87 | + if (!message) return null; |
| 88 | + const content = Array.isArray(message.content) ? (message.content as ContentBlock[]) : []; |
| 89 | + for (const block of content) { |
| 90 | + if (!isRecord(block)) continue; |
| 91 | + switch (block.type) { |
| 92 | + case "tool_use": { |
| 93 | + const b = block as ToolUseBlock; |
| 94 | + const input = isRecord(b.input) ? b.input : {}; |
| 95 | + return redactSensitive(summarizeToolInput(String(b.name ?? "unknown"), input)); |
| 96 | + } |
| 97 | + case "text": |
| 98 | + return redactSensitive(truncate(String((block as TextBlock).text ?? ""), 200)); |
| 99 | + case "thinking": |
| 100 | + return redactSensitive("Thinking..."); |
| 101 | + case "tool_result": |
| 102 | + return redactSensitive(summarizeToolResult(block as ToolResultBlock)); |
| 103 | + } |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + case "content_block_delta": { |
| 108 | + const delta = isRecord(typed.delta) ? typed.delta : null; |
| 109 | + if (delta && (delta as ContentBlockDeltaRecord["delta"]).type === "text_delta") { |
| 110 | + return redactSensitive(truncate(String((delta as ContentBlockDeltaRecord["delta"]).text ?? ""), 200)); |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | + case "result": { |
| 115 | + const r = typed as ResultRecord; |
| 116 | + if (r.subtype === "success") { |
| 117 | + return redactSensitive("Turn complete"); |
| 118 | + } |
| 119 | + if (r.subtype === "error" || r.is_error === true) { |
| 120 | + return redactSensitive( |
| 121 | + `Error: ${truncate(String(r.result ?? r.error ?? ""), 200)}` |
| 122 | + ); |
| 123 | + } |
| 124 | + return null; |
| 125 | + } |
| 126 | + default: |
| 127 | + return null; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// --------------------------------------------------------------------------- |
| 132 | +// API communication |
| 133 | +// --------------------------------------------------------------------------- |
| 134 | + |
| 135 | +async function postLoopEvent( |
| 136 | + apiBaseUrl: string, |
| 137 | + loopId: string, |
| 138 | + token: string, |
| 139 | + event: { type: string; data: { chunk: string } } |
| 140 | +): Promise<void> { |
| 141 | + try { |
| 142 | + await fetch(`${apiBaseUrl}/loops/${loopId}/events`, { |
| 143 | + method: "POST", |
| 144 | + headers: { |
| 145 | + "Authorization": `Bearer ${token}`, |
| 146 | + "Content-Type": "application/json", |
| 147 | + "x-loop-event-nonce": randomUUID(), |
| 148 | + }, |
| 149 | + body: JSON.stringify({ |
| 150 | + type: event.type, |
| 151 | + data: { chunk: event.data.chunk }, |
| 152 | + timestamp: new Date().toISOString(), |
| 153 | + }), |
| 154 | + }); |
| 155 | + } catch (err) { |
| 156 | + console.error("[output-tailer] Failed to post loop event:", err); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// --------------------------------------------------------------------------- |
| 161 | +// Output tailer |
| 162 | +// --------------------------------------------------------------------------- |
| 163 | + |
| 164 | +export function startOutputTailer( |
| 165 | + jsonlPath: string, |
| 166 | + apiBaseUrl: string, |
| 167 | + loopId: string, |
| 168 | + token: string, |
| 169 | + initialByteOffset: number |
| 170 | +): { stop: () => void; flush: () => Promise<void> } { |
| 171 | + let stopped = false; |
| 172 | + let byteOffset = initialByteOffset; |
| 173 | + let pendingRemainder = Buffer.alloc(0); |
| 174 | + let lastSentAt: number | null = null; |
| 175 | + |
| 176 | + async function pollOnce(): Promise<void> { |
| 177 | + if (stopped) return; |
| 178 | + if (!existsSync(jsonlPath)) return; |
| 179 | + let fd: number | null = null; |
| 180 | + try { |
| 181 | + fd = openSync(jsonlPath, "r"); |
| 182 | + const chunkSize = 65536; |
| 183 | + const chunk = Buffer.alloc(chunkSize); |
| 184 | + let bytesRead: number; |
| 185 | + while ((bytesRead = readSync(fd, chunk, 0, chunkSize, byteOffset)) > 0) { |
| 186 | + byteOffset += bytesRead; |
| 187 | + pendingRemainder = Buffer.concat([pendingRemainder, chunk.subarray(0, bytesRead)]); |
| 188 | + } |
| 189 | + } catch { |
| 190 | + return; |
| 191 | + } finally { |
| 192 | + if (fd !== null) closeSync(fd); |
| 193 | + } |
| 194 | + |
| 195 | + const newlineIndex = pendingRemainder.lastIndexOf(10); // 0x0a = newline |
| 196 | + if (newlineIndex === -1) return; |
| 197 | + const completeLines = pendingRemainder.subarray(0, newlineIndex).toString("utf8"); |
| 198 | + pendingRemainder = pendingRemainder.subarray(newlineIndex + 1); |
| 199 | + |
| 200 | + let lastDisplay: string | null = null; |
| 201 | + for (const line of completeLines.split("\n")) { |
| 202 | + const trimmed = line.trim(); |
| 203 | + if (!trimmed) continue; |
| 204 | + let parsed: unknown; |
| 205 | + try { |
| 206 | + parsed = JSON.parse(trimmed); |
| 207 | + } catch { |
| 208 | + continue; |
| 209 | + } |
| 210 | + if (!isRecord(parsed)) continue; |
| 211 | + const display = summarizeJsonlRecord(parsed); |
| 212 | + if (!display) continue; |
| 213 | + lastDisplay = display; |
| 214 | + } |
| 215 | + |
| 216 | + if (lastDisplay !== null) { |
| 217 | + const now = Date.now(); |
| 218 | + if (lastSentAt === null || now - lastSentAt >= 5000) { |
| 219 | + lastSentAt = now; |
| 220 | + await postLoopEvent(apiBaseUrl, loopId, token, { type: "output", data: { chunk: lastDisplay } }); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + const intervalId = setInterval(() => { pollOnce().catch(() => {}); }, 2000); |
| 226 | + |
| 227 | + return { |
| 228 | + stop: () => { stopped = true; clearInterval(intervalId); }, |
| 229 | + flush: async () => { |
| 230 | + clearInterval(intervalId); |
| 231 | + await pollOnce(); |
| 232 | + stopped = true; |
| 233 | + }, |
| 234 | + }; |
| 235 | +} |
0 commit comments