2026-03-05 15:24:20 +01:00
|
|
|
import { asNumber, asString, parseJson, parseObject } from "@paperclipai/adapter-utils/server-utils";
|
2026-03-04 16:48:54 -06:00
|
|
|
|
2026-03-05 15:24:20 +01:00
|
|
|
function errorText(value: unknown): string {
|
2026-03-04 16:48:54 -06:00
|
|
|
if (typeof value === "string") return value;
|
|
|
|
|
const rec = parseObject(value);
|
2026-03-05 15:24:20 +01:00
|
|
|
const message = asString(rec.message, "").trim();
|
2026-03-04 16:48:54 -06:00
|
|
|
if (message) return message;
|
2026-03-05 15:24:20 +01:00
|
|
|
const data = parseObject(rec.data);
|
|
|
|
|
const nestedMessage = asString(data.message, "").trim();
|
|
|
|
|
if (nestedMessage) return nestedMessage;
|
|
|
|
|
const name = asString(rec.name, "").trim();
|
|
|
|
|
if (name) return name;
|
2026-03-06 15:23:55 +00:00
|
|
|
const code = asString(rec.code, "").trim();
|
|
|
|
|
if (code) return code;
|
2026-03-04 16:48:54 -06:00
|
|
|
try {
|
|
|
|
|
return JSON.stringify(rec);
|
|
|
|
|
} catch {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function parseOpenCodeJsonl(stdout: string) {
|
|
|
|
|
let sessionId: string | null = null;
|
|
|
|
|
const messages: string[] = [];
|
2026-03-05 15:24:20 +01:00
|
|
|
const errors: string[] = [];
|
2026-03-04 16:48:54 -06:00
|
|
|
const usage = {
|
|
|
|
|
inputTokens: 0,
|
|
|
|
|
cachedInputTokens: 0,
|
|
|
|
|
outputTokens: 0,
|
2026-03-05 15:24:20 +01:00
|
|
|
costUsd: 0,
|
2026-03-04 16:48:54 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const rawLine of stdout.split(/\r?\n/)) {
|
|
|
|
|
const line = rawLine.trim();
|
|
|
|
|
if (!line) continue;
|
|
|
|
|
|
|
|
|
|
const event = parseJson(line);
|
|
|
|
|
if (!event) continue;
|
|
|
|
|
|
2026-03-05 15:24:20 +01:00
|
|
|
const currentSessionId = asString(event.sessionID, "").trim();
|
|
|
|
|
if (currentSessionId) sessionId = currentSessionId;
|
2026-03-04 16:48:54 -06:00
|
|
|
|
|
|
|
|
const type = asString(event.type, "");
|
|
|
|
|
|
|
|
|
|
if (type === "text") {
|
|
|
|
|
const part = parseObject(event.part);
|
|
|
|
|
const text = asString(part.text, "").trim();
|
|
|
|
|
if (text) messages.push(text);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === "step_finish") {
|
|
|
|
|
const part = parseObject(event.part);
|
|
|
|
|
const tokens = parseObject(part.tokens);
|
|
|
|
|
const cache = parseObject(tokens.cache);
|
|
|
|
|
usage.inputTokens += asNumber(tokens.input, 0);
|
|
|
|
|
usage.cachedInputTokens += asNumber(cache.read, 0);
|
2026-03-05 15:24:20 +01:00
|
|
|
usage.outputTokens += asNumber(tokens.output, 0) + asNumber(tokens.reasoning, 0);
|
|
|
|
|
usage.costUsd += asNumber(part.cost, 0);
|
2026-03-04 16:48:54 -06:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-05 15:24:20 +01:00
|
|
|
if (type === "tool_use") {
|
2026-03-04 16:48:54 -06:00
|
|
|
const part = parseObject(event.part);
|
2026-03-05 15:24:20 +01:00
|
|
|
const state = parseObject(part.state);
|
|
|
|
|
if (asString(state.status, "") === "error") {
|
|
|
|
|
const text = asString(state.error, "").trim();
|
|
|
|
|
if (text) errors.push(text);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === "error") {
|
|
|
|
|
const text = errorText(event.error ?? event.message).trim();
|
|
|
|
|
if (text) errors.push(text);
|
|
|
|
|
continue;
|
2026-03-04 16:48:54 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
sessionId,
|
|
|
|
|
summary: messages.join("\n\n").trim(),
|
|
|
|
|
usage,
|
2026-03-05 15:24:20 +01:00
|
|
|
errorMessage: errors.length > 0 ? errors.join("\n") : null,
|
2026-03-04 16:48:54 -06:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isOpenCodeUnknownSessionError(stdout: string, stderr: string): boolean {
|
|
|
|
|
const haystack = `${stdout}\n${stderr}`
|
|
|
|
|
.split(/\r?\n/)
|
|
|
|
|
.map((line) => line.trim())
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
.join("\n");
|
|
|
|
|
|
2026-03-06 15:23:55 +00:00
|
|
|
return /unknown\s+session|session\s+.*\s+not\s+found|resource\s+not\s+found:.*[\\/]session[\\/].*\.json|notfounderror|no session/i.test(
|
2026-03-04 16:48:54 -06:00
|
|
|
haystack,
|
|
|
|
|
);
|
|
|
|
|
}
|