|
| 1 | +import { |
| 2 | + type AssistantMessage, |
| 3 | + type AssistantMessageEvent, |
| 4 | + type AssistantMessageEventStream, |
| 5 | + createAssistantMessageEventStream, |
| 6 | + streamSimple, |
| 7 | +} from "@earendil-works/pi-ai"; |
| 8 | + |
| 9 | +const DSML_MARKER = "<|dsml|"; |
| 10 | +export const DEFAULT_PROXY_RESPONSE_TIMEOUT_MS = 90_000; |
| 11 | + |
| 12 | +/** Remove textual proxy protocol that must never be rendered or re-parsed. */ |
| 13 | +export function stripDsmlProtocol(text: string): string { |
| 14 | + const normalized = text.toLowerCase().replaceAll("|", "|"); |
| 15 | + const fullMarker = normalized.indexOf(DSML_MARKER); |
| 16 | + if (fullMarker >= 0) return text.slice(0, fullMarker).trimEnd(); |
| 17 | + |
| 18 | + // Streaming can split the marker across chunks. Hide a trailing partial |
| 19 | + // marker as soon as it starts instead of flashing protocol in the TUI. |
| 20 | + for (let i = normalized.lastIndexOf("<"); i >= 0; i = normalized.lastIndexOf("<", i - 1)) { |
| 21 | + const suffix = normalized.slice(i); |
| 22 | + if (DSML_MARKER.startsWith(suffix)) return text.slice(0, i).trimEnd(); |
| 23 | + } |
| 24 | + return text; |
| 25 | +} |
| 26 | + |
| 27 | +/** Normalize a proxy response before pi-agent-core can execute its tools. */ |
| 28 | +export function sanitizeAssistantMessage(message: AssistantMessage): AssistantMessage { |
| 29 | + const seenToolCalls = new Set<string>(); |
| 30 | + const content: AssistantMessage["content"] = []; |
| 31 | + for (const block of message.content) { |
| 32 | + if (block.type === "text") { |
| 33 | + const text = stripDsmlProtocol(block.text); |
| 34 | + if (text) content.push({ ...block, text }); |
| 35 | + continue; |
| 36 | + } |
| 37 | + if (block.type === "toolCall") { |
| 38 | + const fingerprint = `${block.name}:${stableJson(block.arguments)}`; |
| 39 | + if (seenToolCalls.has(fingerprint)) continue; |
| 40 | + seenToolCalls.add(fingerprint); |
| 41 | + } |
| 42 | + content.push(block); |
| 43 | + } |
| 44 | + return { ...message, content }; |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Proxy models occasionally return both native tool calls and a textual DSML |
| 49 | + * copy, and have also repeated an identical native call in one response. This |
| 50 | + * wrapper cleans every partial snapshot plus the final message so the UI and |
| 51 | + * executor see the same safe response. |
| 52 | + */ |
| 53 | +export function streamProxySafely(...args: Parameters<typeof streamSimple>): ReturnType<typeof streamSimple> { |
| 54 | + const [model, context, options] = args; |
| 55 | + const output = createAssistantMessageEventStream(); |
| 56 | + const controller = new AbortController(); |
| 57 | + const parentSignal = options?.signal; |
| 58 | + let latest = emptyAssistantMessage(model); |
| 59 | + let timedOut = false; |
| 60 | + |
| 61 | + const abortFromParent = () => controller.abort(parentSignal?.reason); |
| 62 | + if (parentSignal?.aborted) abortFromParent(); |
| 63 | + else parentSignal?.addEventListener("abort", abortFromParent, { once: true }); |
| 64 | + |
| 65 | + const timeoutMs = proxyResponseTimeoutMs(); |
| 66 | + const timer = setTimeout(() => { |
| 67 | + timedOut = true; |
| 68 | + controller.abort(); |
| 69 | + output.push({ |
| 70 | + type: "error", |
| 71 | + reason: "error", |
| 72 | + error: failedAssistantMessage( |
| 73 | + latest, |
| 74 | + "error", |
| 75 | + `Model response exceeded ${Math.round(timeoutMs / 1000)}s and was stopped. Retry with a narrower request or start a fresh session.`, |
| 76 | + ), |
| 77 | + }); |
| 78 | + }, timeoutMs); |
| 79 | + timer.unref?.(); |
| 80 | + |
| 81 | + const upstream = streamSimple(model, context, { ...options, signal: controller.signal }); |
| 82 | + void (async () => { |
| 83 | + try { |
| 84 | + for await (const event of upstream) { |
| 85 | + const sanitized = sanitizeEvent(event); |
| 86 | + latest = messageFromEvent(sanitized); |
| 87 | + output.push(sanitized); |
| 88 | + } |
| 89 | + } catch (error) { |
| 90 | + if (timedOut) return; |
| 91 | + output.push({ |
| 92 | + type: "error", |
| 93 | + reason: controller.signal.aborted ? "aborted" : "error", |
| 94 | + error: failedAssistantMessage( |
| 95 | + latest, |
| 96 | + controller.signal.aborted ? "aborted" : "error", |
| 97 | + error instanceof Error ? error.message : String(error), |
| 98 | + ), |
| 99 | + }); |
| 100 | + } |
| 101 | + })(); |
| 102 | + void output.result().finally(() => { |
| 103 | + clearTimeout(timer); |
| 104 | + parentSignal?.removeEventListener("abort", abortFromParent); |
| 105 | + }); |
| 106 | + return output; |
| 107 | +} |
| 108 | + |
| 109 | +export function sanitizeAssistantStream(upstream: AssistantMessageEventStream): AssistantMessageEventStream { |
| 110 | + const output = createAssistantMessageEventStream(); |
| 111 | + void (async () => { |
| 112 | + for await (const event of upstream) output.push(sanitizeEvent(event)); |
| 113 | + })(); |
| 114 | + return output; |
| 115 | +} |
| 116 | + |
| 117 | +function sanitizeEvent(event: AssistantMessageEvent): AssistantMessageEvent { |
| 118 | + if (event.type === "done") return { ...event, message: sanitizeAssistantMessage(event.message) }; |
| 119 | + if (event.type === "error") return { ...event, error: sanitizeAssistantMessage(event.error) }; |
| 120 | + return { ...event, partial: sanitizeAssistantMessage(event.partial) }; |
| 121 | +} |
| 122 | + |
| 123 | +export function proxyResponseTimeoutMs(value = process.env.CODEBASE_RESPONSE_TIMEOUT_MS): number { |
| 124 | + if (value === undefined || value.trim() === "") return DEFAULT_PROXY_RESPONSE_TIMEOUT_MS; |
| 125 | + const parsed = Number(value); |
| 126 | + if (!Number.isFinite(parsed)) return DEFAULT_PROXY_RESPONSE_TIMEOUT_MS; |
| 127 | + return Math.max(10_000, Math.min(600_000, Math.round(parsed))); |
| 128 | +} |
| 129 | + |
| 130 | +function messageFromEvent(event: AssistantMessageEvent): AssistantMessage { |
| 131 | + if (event.type === "done") return event.message; |
| 132 | + if (event.type === "error") return event.error; |
| 133 | + return event.partial; |
| 134 | +} |
| 135 | + |
| 136 | +function emptyAssistantMessage(model: Parameters<typeof streamSimple>[0]): AssistantMessage { |
| 137 | + return { |
| 138 | + role: "assistant", |
| 139 | + content: [], |
| 140 | + api: model.api, |
| 141 | + provider: model.provider, |
| 142 | + model: model.id, |
| 143 | + usage: { |
| 144 | + input: 0, |
| 145 | + output: 0, |
| 146 | + cacheRead: 0, |
| 147 | + cacheWrite: 0, |
| 148 | + totalTokens: 0, |
| 149 | + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, |
| 150 | + }, |
| 151 | + stopReason: "stop", |
| 152 | + timestamp: Date.now(), |
| 153 | + }; |
| 154 | +} |
| 155 | + |
| 156 | +function failedAssistantMessage( |
| 157 | + message: AssistantMessage, |
| 158 | + stopReason: "aborted" | "error", |
| 159 | + errorMessage: string, |
| 160 | +): AssistantMessage { |
| 161 | + const sanitized = sanitizeAssistantMessage(message); |
| 162 | + return { |
| 163 | + ...sanitized, |
| 164 | + // A partially streamed tool call was never executed. Do not persist or |
| 165 | + // render it as a completed action after timeout/abort. |
| 166 | + content: sanitized.content.filter((block) => block.type !== "toolCall"), |
| 167 | + stopReason, |
| 168 | + errorMessage, |
| 169 | + }; |
| 170 | +} |
| 171 | + |
| 172 | +function stableJson(value: unknown): string { |
| 173 | + if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`; |
| 174 | + if (value && typeof value === "object") { |
| 175 | + return `{${Object.entries(value as Record<string, unknown>) |
| 176 | + .sort(([a], [b]) => a.localeCompare(b)) |
| 177 | + .map(([key, child]) => `${JSON.stringify(key)}:${stableJson(child)}`) |
| 178 | + .join(",")}}`; |
| 179 | + } |
| 180 | + return JSON.stringify(value) ?? String(value); |
| 181 | +} |
0 commit comments