Spaces:
Paused
Paused
File size: 7,594 Bytes
b152fd5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | import pc from "picocolors";
function asRecord(value: unknown): Record<string, unknown> | null {
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
return value as Record<string, unknown>;
}
function asString(value: unknown, fallback = ""): string {
return typeof value === "string" ? value : fallback;
}
function asNumber(value: unknown, fallback = 0): number {
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
}
function errorText(value: unknown): string {
if (typeof value === "string") return value;
const rec = asRecord(value);
if (!rec) return "";
const msg =
(typeof rec.message === "string" && rec.message) ||
(typeof rec.error === "string" && rec.error) ||
(typeof rec.code === "string" && rec.code) ||
"";
if (msg) return msg;
try {
return JSON.stringify(rec);
} catch {
return "";
}
}
function printItemStarted(item: Record<string, unknown>): boolean {
const itemType = asString(item.type);
if (itemType === "command_execution") {
const command = asString(item.command);
console.log(pc.yellow("tool_call: command_execution"));
if (command) console.log(pc.gray(command));
return true;
}
if (itemType === "tool_use") {
const name = asString(item.name, "unknown");
console.log(pc.yellow(`tool_call: ${name}`));
if (item.input !== undefined) {
try {
console.log(pc.gray(JSON.stringify(item.input, null, 2)));
} catch {
console.log(pc.gray(String(item.input)));
}
}
return true;
}
return false;
}
function printItemCompleted(item: Record<string, unknown>): boolean {
const itemType = asString(item.type);
if (itemType === "agent_message") {
const text = asString(item.text);
if (text) console.log(pc.green(`assistant: ${text}`));
return true;
}
if (itemType === "reasoning") {
const text = asString(item.text);
if (text) console.log(pc.gray(`thinking: ${text}`));
return true;
}
if (itemType === "tool_use") {
const name = asString(item.name, "unknown");
console.log(pc.yellow(`tool_call: ${name}`));
if (item.input !== undefined) {
try {
console.log(pc.gray(JSON.stringify(item.input, null, 2)));
} catch {
console.log(pc.gray(String(item.input)));
}
}
return true;
}
if (itemType === "command_execution") {
const command = asString(item.command);
const status = asString(item.status);
const exitCode = typeof item.exit_code === "number" && Number.isFinite(item.exit_code) ? item.exit_code : null;
const output = asString(item.aggregated_output).replace(/\s+$/, "");
const isError =
(exitCode !== null && exitCode !== 0) ||
status === "failed" ||
status === "errored" ||
status === "error" ||
status === "cancelled";
const summaryParts = [
"tool_result: command_execution",
command ? `command="${command}"` : "",
status ? `status=${status}` : "",
exitCode !== null ? `exit_code=${exitCode}` : "",
].filter(Boolean);
console.log((isError ? pc.red : pc.cyan)(summaryParts.join(" ")));
if (output) console.log((isError ? pc.red : pc.gray)(output));
return true;
}
if (itemType === "file_change") {
const changes = Array.isArray(item.changes) ? item.changes : [];
const entries = changes
.map((changeRaw) => asRecord(changeRaw))
.filter((change): change is Record<string, unknown> => Boolean(change))
.map((change) => {
const kind = asString(change.kind, "update");
const path = asString(change.path, "unknown");
return `${kind} ${path}`;
});
const preview = entries.length > 0 ? entries.slice(0, 6).join(", ") : "none";
const more = entries.length > 6 ? ` (+${entries.length - 6} more)` : "";
console.log(pc.cyan(`file_change: ${preview}${more}`));
return true;
}
if (itemType === "error") {
const message = errorText(item.message ?? item.error ?? item);
if (message) console.log(pc.red(`error: ${message}`));
return true;
}
if (itemType === "tool_result") {
const isError = item.is_error === true || asString(item.status) === "error";
const text = asString(item.content) || asString(item.result) || asString(item.output);
console.log((isError ? pc.red : pc.cyan)(`tool_result${isError ? " (error)" : ""}`));
if (text) console.log((isError ? pc.red : pc.gray)(text));
return true;
}
return false;
}
export function printCodexStreamEvent(raw: string, _debug: boolean): void {
const line = raw.trim();
if (!line) return;
let parsed: Record<string, unknown> | null = null;
try {
parsed = JSON.parse(line) as Record<string, unknown>;
} catch {
console.log(line);
return;
}
const type = asString(parsed.type);
if (type === "thread.started") {
const threadId = asString(parsed.thread_id);
const model = asString(parsed.model);
const details = [threadId ? `session: ${threadId}` : "", model ? `model: ${model}` : ""].filter(Boolean).join(", ");
console.log(pc.blue(`Codex thread started${details ? ` (${details})` : ""}`));
return;
}
if (type === "turn.started") {
console.log(pc.blue("turn started"));
return;
}
if (type === "item.started" || type === "item.completed") {
const item = asRecord(parsed.item);
if (item) {
const handled =
type === "item.started"
? printItemStarted(item)
: printItemCompleted(item);
if (!handled) {
const itemType = asString(item.type, "unknown");
const id = asString(item.id);
const status = asString(item.status);
const meta = [id ? `id=${id}` : "", status ? `status=${status}` : ""].filter(Boolean).join(" ");
console.log(pc.gray(`${type}: ${itemType}${meta ? ` (${meta})` : ""}`));
}
} else {
console.log(pc.gray(type));
}
return;
}
if (type === "turn.completed") {
const usage = asRecord(parsed.usage);
const input = asNumber(usage?.input_tokens);
const output = asNumber(usage?.output_tokens);
const cached = asNumber(usage?.cached_input_tokens, asNumber(usage?.cache_read_input_tokens));
const cost = asNumber(parsed.total_cost_usd);
const isError = parsed.is_error === true;
const subtype = asString(parsed.subtype);
const errors = Array.isArray(parsed.errors) ? parsed.errors.map(errorText).filter(Boolean) : [];
console.log(
pc.blue(`tokens: in=${input} out=${output} cached=${cached} cost=$${cost.toFixed(6)}`),
);
if (subtype || isError || errors.length > 0) {
console.log(
pc.red(`result: subtype=${subtype || "unknown"} is_error=${isError ? "true" : "false"}`),
);
if (errors.length > 0) console.log(pc.red(`errors: ${errors.join(" | ")}`));
}
return;
}
if (type === "turn.failed") {
const usage = asRecord(parsed.usage);
const input = asNumber(usage?.input_tokens);
const output = asNumber(usage?.output_tokens);
const cached = asNumber(usage?.cached_input_tokens, asNumber(usage?.cache_read_input_tokens));
const message = errorText(parsed.error ?? parsed.message);
console.log(pc.red(`turn failed${message ? `: ${message}` : ""}`));
console.log(pc.blue(`tokens: in=${input} out=${output} cached=${cached}`));
return;
}
if (type === "error") {
const message = errorText(parsed.message ?? parsed.error ?? parsed);
if (message) console.log(pc.red(`error: ${message}`));
return;
}
console.log(line);
}
|