@agentsky/sky 3.48.2 → 3.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-W24YSGH4.js → chunk-2YUZZ5VG.js} +7 -856
- package/dist/{chunk-ZRV3UUW5.js → chunk-6LRSRXQI.js} +1 -1
- package/dist/{chunk-PGX3AGCZ.js → chunk-7JFTEJ7B.js} +1 -1
- package/dist/chunk-BKNZQZZD.js +857 -0
- package/dist/{chunk-RKMMLSGJ.js → chunk-CK7N635F.js} +1 -1
- package/dist/{chunk-NAIYHS6Q.js → chunk-HFWHVU72.js} +2 -1
- package/dist/{chunk-A45NGV2Q.js → chunk-HGL7TLNA.js} +2 -2
- package/dist/{chunk-ZF2P6PKM.js → chunk-HNUY5PBQ.js} +2 -2
- package/dist/{chunk-OJ5ERABF.js → chunk-IO2N34S2.js} +1 -1
- package/dist/{chunk-UJS4JIE2.js → chunk-JMGVHVOS.js} +10 -10
- package/dist/{chunk-CV2ZRIF7.js → chunk-QTE5OMZY.js} +4 -4
- package/dist/{chunk-FVIPGI3E.js → chunk-RVI7ZWR2.js} +5 -5
- package/dist/chunk-TGU7T4O7.js +1363 -0
- package/dist/{chunk-NJPIE762.js → chunk-UYBKORXA.js} +43 -24
- package/dist/chunk-YO2XUCCF.js +30 -0
- package/dist/{chunk-6D36IT7T.js → chunk-ZOXE2XNH.js} +2 -2
- package/dist/{compute-AERPKXJ3.js → compute-4MMGFJGE.js} +2 -2
- package/dist/{compute-report-CTT4WBSQ.js → compute-report-LIMMRQY3.js} +15 -13
- package/dist/grok-bot-export-7LSTXWJW.js +57 -0
- package/dist/index.js +599 -1770
- package/dist/{local-skills-E56TR7O6.js → local-skills-5LJST7EP.js} +3 -3
- package/dist/{local-skills-run-TST44QSA.js → local-skills-run-6SZJQSVF.js} +9 -9
- package/dist/{mcp-register-HDOVT4QT.js → mcp-register-XY4MR3ZZ.js} +3 -3
- package/dist/{mcp-serve-2LBAEAJX.js → mcp-serve-QROKKUJV.js} +6 -6
- package/dist/{trace-S2XZC75N.js → trace-SJZ25T35.js} +4 -3
- package/dist/{trace-exclusions-JEGMUPWQ.js → trace-exclusions-IC6OA2ND.js} +3 -2
- package/dist/trace-handoff-CD5LOS7B.js +116 -0
- package/dist/{trace-library-HN5TU5EJ.js → trace-library-V23TEXG4.js} +14 -8
- package/dist/trace-privacy-DU3TL5EO.js +11 -0
- package/package.json +1 -1
- package/vendor/trace-collect/SOURCE.json +2 -1
- package/vendor/trace-collect/sources/_pii.mjs +56 -0
- package/vendor/trace-collect/sources/grok-bot.mjs +180 -0
- package/vendor/trace-collect/sources/index.mjs +4 -2
- package/vendor/trace-collect/sources/muse-agent.mjs +95 -0
|
@@ -0,0 +1,1363 @@
|
|
|
1
|
+
import {
|
|
2
|
+
unsafeNumberSpans
|
|
3
|
+
} from "./chunk-BKNZQZZD.js";
|
|
4
|
+
|
|
5
|
+
// ../../packages/conversation/src/write.ts
|
|
6
|
+
import { randomBytes, randomUUID } from "crypto";
|
|
7
|
+
|
|
8
|
+
// ../../packages/conversation/src/conversation.ts
|
|
9
|
+
var ENGINE_LABEL = { claude_code: "Claude Code", codex: "Codex" };
|
|
10
|
+
var OTHER_LABELS = { hermes: "Hermes", openclaw: "OpenClaw", pi: "Pi", cursor: "Cursor", grok: "Grok", gemini: "Gemini" };
|
|
11
|
+
function engineLabel(engine) {
|
|
12
|
+
return ENGINE_LABEL[engine] ?? OTHER_LABELS[engine] ?? engine;
|
|
13
|
+
}
|
|
14
|
+
function isEngine(value) {
|
|
15
|
+
return value === "claude_code" || value === "codex";
|
|
16
|
+
}
|
|
17
|
+
function messageText(message) {
|
|
18
|
+
return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
19
|
+
}
|
|
20
|
+
function resultText(part) {
|
|
21
|
+
const output = part.result.output;
|
|
22
|
+
if (typeof output === "string") return output;
|
|
23
|
+
return output === void 0 ? JSON.stringify(part.result) : JSON.stringify(output);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// ../../packages/conversation/src/fit.ts
|
|
27
|
+
var CONTEXT_BUDGET_CHARS = { claude_code: 48e4, codex: 64e4 };
|
|
28
|
+
var PORTABLE_BUDGET_CHARS = 2e5;
|
|
29
|
+
var MAX_TOOL_OUTPUT_CHARS = 6e4;
|
|
30
|
+
var IMAGE_CHARS = 6e3;
|
|
31
|
+
var SUMMARY_SHARE = 0.15;
|
|
32
|
+
var LINE_CHARS = 400;
|
|
33
|
+
function size(message) {
|
|
34
|
+
let total = 0;
|
|
35
|
+
for (const part of message.parts) {
|
|
36
|
+
if (part.type === "text") total += part.text.length;
|
|
37
|
+
else if (part.type === "tool_call") total += JSON.stringify(part.args).length + part.tool_name.length;
|
|
38
|
+
else if (part.type === "tool_result") total += Math.min(resultText(part).length, MAX_TOOL_OUTPUT_CHARS);
|
|
39
|
+
else if (part.type === "image" || part.type === "file") total += IMAGE_CHARS;
|
|
40
|
+
}
|
|
41
|
+
return total;
|
|
42
|
+
}
|
|
43
|
+
function clip(text, max) {
|
|
44
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
45
|
+
return flat.length > max ? `${flat.slice(0, max - 1)}\u2026` : flat;
|
|
46
|
+
}
|
|
47
|
+
function fromBothEnds(entries, maxChars) {
|
|
48
|
+
const order = entries.map((_, index) => index % 2 ? entries.length - 1 - (index >> 1) : index >> 1);
|
|
49
|
+
const kept = /* @__PURE__ */ new Set();
|
|
50
|
+
let length = 0;
|
|
51
|
+
for (const index of order) {
|
|
52
|
+
if (length + entries[index].length + 1 > maxChars) break;
|
|
53
|
+
kept.add(index);
|
|
54
|
+
length += entries[index].length + 1;
|
|
55
|
+
}
|
|
56
|
+
const out = [];
|
|
57
|
+
entries.forEach((entry, index) => {
|
|
58
|
+
if (kept.has(index)) out.push(entry);
|
|
59
|
+
else if (kept.has(index - 1) || index === 0 && !kept.has(0)) out.push(`(${entries.length - kept.size} entries left out here)`);
|
|
60
|
+
});
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
var listAskedAndAnswered = (older, { maxChars }) => {
|
|
64
|
+
const tools = /* @__PURE__ */ new Map();
|
|
65
|
+
const all = [];
|
|
66
|
+
const asked = [];
|
|
67
|
+
for (const message of older) {
|
|
68
|
+
for (const part of message.parts) if (part.type === "tool_call") tools.set(part.tool_name, (tools.get(part.tool_name) ?? 0) + 1);
|
|
69
|
+
const text = messageText(message);
|
|
70
|
+
if (!text.trim()) continue;
|
|
71
|
+
const entry = `${message.role === "user" ? "Asked" : "Answered"}: ${clip(text, LINE_CHARS)}`;
|
|
72
|
+
all.push(entry);
|
|
73
|
+
if (message.role === "user") asked.push(entry);
|
|
74
|
+
}
|
|
75
|
+
const used = [...tools].map(([name, count]) => `${name} \xD7${count}`).join(", ");
|
|
76
|
+
const tail = used ? `
|
|
77
|
+
Tools used in that part: ${used}.` : "";
|
|
78
|
+
const room = maxChars - tail.length;
|
|
79
|
+
const fits = (entries) => entries.reduce((sum, entry) => sum + entry.length + 1, 0) <= room;
|
|
80
|
+
const body = fits(all) ? all : fits(asked) ? asked : fromBothEnds(asked, room - 40);
|
|
81
|
+
return body.join("\n") + tail;
|
|
82
|
+
};
|
|
83
|
+
function startsTurn(message) {
|
|
84
|
+
return message.role === "user" && message.parts.some((part) => part.type === "text" || part.type === "image");
|
|
85
|
+
}
|
|
86
|
+
function fitConversation(conversation, to, opts = {}) {
|
|
87
|
+
const budget = opts.budgetChars ?? (to ? CONTEXT_BUDGET_CHARS[to] : PORTABLE_BUDGET_CHARS);
|
|
88
|
+
const sizes = conversation.messages.map(size);
|
|
89
|
+
const total = sizes.reduce((sum, value) => sum + value, 0);
|
|
90
|
+
if (total <= budget) return { messages: conversation.messages, summarised: 0 };
|
|
91
|
+
const room = budget * (1 - SUMMARY_SHARE);
|
|
92
|
+
let start = conversation.messages.length;
|
|
93
|
+
let kept = 0;
|
|
94
|
+
for (let index = conversation.messages.length - 1; index >= 0; index -= 1) {
|
|
95
|
+
if (kept + sizes[index] > room) break;
|
|
96
|
+
kept += sizes[index];
|
|
97
|
+
start = index;
|
|
98
|
+
}
|
|
99
|
+
while (start < conversation.messages.length && !startsTurn(conversation.messages[start])) start += 1;
|
|
100
|
+
if (start >= conversation.messages.length) {
|
|
101
|
+
start = conversation.messages.length - 1;
|
|
102
|
+
while (start > 0 && !startsTurn(conversation.messages[start])) start -= 1;
|
|
103
|
+
}
|
|
104
|
+
const older = conversation.messages.slice(0, start);
|
|
105
|
+
const maxChars = Math.floor(budget * SUMMARY_SHARE);
|
|
106
|
+
const summary = (opts.summarize ?? listAskedAndAnswered)(older, { from: conversation.engine, to, maxChars }).slice(0, maxChars);
|
|
107
|
+
const first = older[0]?.created_at ?? "";
|
|
108
|
+
const note = {
|
|
109
|
+
id: "summary",
|
|
110
|
+
role: "user",
|
|
111
|
+
created_at: first,
|
|
112
|
+
parts: [{ type: "text", index: 0, text: `[This conversation was continued from ${engineLabel(conversation.engine)}. Its oldest ${older.length} messages were too long to carry and are summarised below; everything after this note is the original conversation, word for word.]
|
|
113
|
+
|
|
114
|
+
${summary}` }],
|
|
115
|
+
metadata: { summary_of: older.length }
|
|
116
|
+
};
|
|
117
|
+
return { messages: [note, ...conversation.messages.slice(start)], summarised: older.length };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ../../packages/conversation/src/tools.ts
|
|
121
|
+
var CLAUDE_CODE_TOOLS = /* @__PURE__ */ new Set([
|
|
122
|
+
"Bash",
|
|
123
|
+
"Read",
|
|
124
|
+
"Write",
|
|
125
|
+
"Edit",
|
|
126
|
+
"MultiEdit",
|
|
127
|
+
"Glob",
|
|
128
|
+
"Grep",
|
|
129
|
+
"LS",
|
|
130
|
+
"WebFetch",
|
|
131
|
+
"WebSearch",
|
|
132
|
+
"Task",
|
|
133
|
+
"Agent",
|
|
134
|
+
"TodoWrite",
|
|
135
|
+
"NotebookEdit",
|
|
136
|
+
"NotebookRead",
|
|
137
|
+
"BashOutput",
|
|
138
|
+
"KillShell",
|
|
139
|
+
"ExitPlanMode",
|
|
140
|
+
"Skill",
|
|
141
|
+
"SlashCommand"
|
|
142
|
+
]);
|
|
143
|
+
var CODEX_FUNCTIONS = /* @__PURE__ */ new Set(["shell", "shell_command", "exec_command", "write_stdin", "update_plan", "view_image", "web_search"]);
|
|
144
|
+
function str(value) {
|
|
145
|
+
return typeof value === "string" ? value : "";
|
|
146
|
+
}
|
|
147
|
+
function quote(value) {
|
|
148
|
+
return /^[A-Za-z0-9_@%+=:,./-]+$/.test(value) ? value : `'${value.replace(/'/g, `'\\''`)}'`;
|
|
149
|
+
}
|
|
150
|
+
function absolute(path, cwd) {
|
|
151
|
+
if (path.startsWith("/") || !cwd) return path;
|
|
152
|
+
return `${cwd.replace(/\/+$/, "")}/${path.replace(/^\.\//, "")}`;
|
|
153
|
+
}
|
|
154
|
+
function relativeTo(path, cwd) {
|
|
155
|
+
if (!cwd) return path;
|
|
156
|
+
const root = `${cwd.replace(/\/+$/, "")}/`;
|
|
157
|
+
return path.startsWith(root) ? path.slice(root.length) : path;
|
|
158
|
+
}
|
|
159
|
+
function lines(text) {
|
|
160
|
+
const split = text.split("\n");
|
|
161
|
+
if (split.at(-1) === "") split.pop();
|
|
162
|
+
return split;
|
|
163
|
+
}
|
|
164
|
+
function codexCommand(call) {
|
|
165
|
+
const args = call.args;
|
|
166
|
+
const workdir = str(args.workdir) || str(args.working_directory);
|
|
167
|
+
if (call.tool_name === "exec_command") return str(args.cmd) ? { command: str(args.cmd), workdir } : null;
|
|
168
|
+
if (call.tool_name === "shell_command") return str(args.command) ? { command: str(args.command), workdir } : null;
|
|
169
|
+
if (call.tool_name !== "shell") return null;
|
|
170
|
+
const argv = Array.isArray(args.command) ? args.command.filter((part) => typeof part === "string") : [];
|
|
171
|
+
if (!argv.length) return str(args.command) ? { command: str(args.command), workdir } : null;
|
|
172
|
+
if (argv.length === 3 && /(^|\/)(ba|z)?sh$/.test(argv[0]) && (argv[1] === "-lc" || argv[1] === "-c")) return { command: argv[2], workdir };
|
|
173
|
+
if (argv[0] === "apply_patch") return null;
|
|
174
|
+
return { command: argv.map(quote).join(" "), workdir };
|
|
175
|
+
}
|
|
176
|
+
function codexPatch(call) {
|
|
177
|
+
if (call.tool_name === "apply_patch") return str(call.args.input) || str(call.args.patch) || null;
|
|
178
|
+
const argv = Array.isArray(call.args.command) ? call.args.command : [];
|
|
179
|
+
return call.tool_name === "shell" && argv[0] === "apply_patch" && typeof argv[1] === "string" ? argv[1] : null;
|
|
180
|
+
}
|
|
181
|
+
function parsePatch(patch) {
|
|
182
|
+
const all = lines(patch.replace(/\r\n/g, "\n"));
|
|
183
|
+
if (all[0]?.trim() !== "*** Begin Patch" || all.at(-1)?.trim() !== "*** End Patch") return null;
|
|
184
|
+
const files = [];
|
|
185
|
+
for (const line of all.slice(1, -1)) {
|
|
186
|
+
const header = /^\*\*\* (Add|Update|Delete) File: (.+)$/.exec(line);
|
|
187
|
+
if (header) {
|
|
188
|
+
files.push({ action: header[1].toLowerCase(), path: header[2].trim(), moveTo: null, body: [] });
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const current = files.at(-1);
|
|
192
|
+
if (!current) return null;
|
|
193
|
+
const move = /^\*\*\* Move to: (.+)$/.exec(line);
|
|
194
|
+
if (move) current.moveTo = move[1].trim();
|
|
195
|
+
else current.body.push(line);
|
|
196
|
+
}
|
|
197
|
+
return files;
|
|
198
|
+
}
|
|
199
|
+
function patchToClaudeCode(patch, cwd) {
|
|
200
|
+
const files = parsePatch(patch);
|
|
201
|
+
if (!files || files.length !== 1) return null;
|
|
202
|
+
const [file] = files;
|
|
203
|
+
if (file.moveTo) return null;
|
|
204
|
+
if (file.action === "add") {
|
|
205
|
+
if (file.body.some((line) => !line.startsWith("+"))) return null;
|
|
206
|
+
return { name: "Write", input: { file_path: absolute(file.path, cwd), content: file.body.map((line) => `${line.slice(1)}
|
|
207
|
+
`).join("") } };
|
|
208
|
+
}
|
|
209
|
+
if (file.action !== "update") return null;
|
|
210
|
+
const body = file.body.filter((line) => line !== "*** End of File");
|
|
211
|
+
if (body.filter((line) => line.startsWith("@@")).length > 1) return null;
|
|
212
|
+
const old = [];
|
|
213
|
+
const next = [];
|
|
214
|
+
for (const line of body.filter((line2) => !line2.startsWith("@@"))) {
|
|
215
|
+
if (line.startsWith("-")) old.push(line.slice(1));
|
|
216
|
+
else if (line.startsWith("+")) next.push(line.slice(1));
|
|
217
|
+
else if (line.startsWith(" ") || line === "") {
|
|
218
|
+
old.push(line.slice(1));
|
|
219
|
+
next.push(line.slice(1));
|
|
220
|
+
} else return null;
|
|
221
|
+
}
|
|
222
|
+
if (!old.length) return null;
|
|
223
|
+
return { name: "Edit", input: { file_path: absolute(file.path, cwd), old_string: old.join("\n"), new_string: next.join("\n") } };
|
|
224
|
+
}
|
|
225
|
+
function toClaudeCode(call, cwd) {
|
|
226
|
+
if (CLAUDE_CODE_TOOLS.has(call.tool_name)) return { name: call.tool_name, input: call.args };
|
|
227
|
+
const patch = codexPatch(call);
|
|
228
|
+
if (patch) return patchToClaudeCode(patch, cwd);
|
|
229
|
+
const shell = codexCommand(call);
|
|
230
|
+
if (!shell) return null;
|
|
231
|
+
const inPlace = !shell.workdir || shell.workdir === cwd;
|
|
232
|
+
return { name: "Bash", input: { command: inPlace ? shell.command : `cd ${quote(shell.workdir)} && ${shell.command}` } };
|
|
233
|
+
}
|
|
234
|
+
function shellCall(command, cwd) {
|
|
235
|
+
return { kind: "function", name: "shell", arguments: { command: ["bash", "-lc", command], ...cwd ? { workdir: cwd } : {} } };
|
|
236
|
+
}
|
|
237
|
+
function patchCall(body) {
|
|
238
|
+
return { kind: "custom", name: "apply_patch", input: ["*** Begin Patch", ...body, "*** End Patch"].join("\n") };
|
|
239
|
+
}
|
|
240
|
+
function toCodex(call, cwd) {
|
|
241
|
+
const args = call.args;
|
|
242
|
+
if (call.tool_name === "apply_patch") return str(args.input) ? { kind: "custom", name: "apply_patch", input: str(args.input) } : null;
|
|
243
|
+
if (CODEX_FUNCTIONS.has(call.tool_name)) return { kind: "function", name: call.tool_name, arguments: args };
|
|
244
|
+
switch (call.tool_name) {
|
|
245
|
+
case "Bash":
|
|
246
|
+
return str(args.command) ? shellCall(str(args.command), cwd) : null;
|
|
247
|
+
case "Read": {
|
|
248
|
+
const path = str(args.file_path);
|
|
249
|
+
if (!path) return null;
|
|
250
|
+
const offset = typeof args.offset === "number" ? args.offset : null;
|
|
251
|
+
const limit = typeof args.limit === "number" ? args.limit : null;
|
|
252
|
+
if (offset === null && limit === null) return shellCall(`cat -n ${quote(path)}`, cwd);
|
|
253
|
+
const first = offset ?? 1;
|
|
254
|
+
return shellCall(`cat -n ${quote(path)} | sed -n '${first},${limit === null ? "$" : first + limit - 1}p'`, cwd);
|
|
255
|
+
}
|
|
256
|
+
case "Write": {
|
|
257
|
+
const path = str(args.file_path);
|
|
258
|
+
if (!path || typeof args.content !== "string") return null;
|
|
259
|
+
return patchCall([`*** Add File: ${relativeTo(path, cwd)}`, ...lines(args.content).map((line) => `+${line}`)]);
|
|
260
|
+
}
|
|
261
|
+
case "Edit": {
|
|
262
|
+
const path = str(args.file_path);
|
|
263
|
+
if (!path || typeof args.old_string !== "string" || typeof args.new_string !== "string" || args.replace_all === true) return null;
|
|
264
|
+
return patchCall([`*** Update File: ${relativeTo(path, cwd)}`, "@@", ...lines(args.old_string).map((line) => `-${line}`), ...lines(args.new_string).map((line) => `+${line}`)]);
|
|
265
|
+
}
|
|
266
|
+
default:
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// ../../packages/conversation/src/write.ts
|
|
272
|
+
function cut(text) {
|
|
273
|
+
if (text.length <= MAX_TOOL_OUTPUT_CHARS) return text;
|
|
274
|
+
const half = Math.floor(MAX_TOOL_OUTPUT_CHARS / 2);
|
|
275
|
+
return `${text.slice(0, half)}
|
|
276
|
+
[\u2026 ${text.length - 2 * half} characters left out \u2026]
|
|
277
|
+
${text.slice(-half)}`;
|
|
278
|
+
}
|
|
279
|
+
function callAsText(from, call, result) {
|
|
280
|
+
const outcome = result ? `${result.status === "error" ? "It failed" : "It returned"}:
|
|
281
|
+
${cut(resultText(result))}` : "No result was recorded.";
|
|
282
|
+
return `[In ${engineLabel(from)} I called ${call.tool_name} with ${JSON.stringify(call.args)}. ${outcome}]`;
|
|
283
|
+
}
|
|
284
|
+
function decoded(part) {
|
|
285
|
+
if (!part.data || !/^text\/|json/.test(part.media_type)) return `[Attached file: ${part.name}]`;
|
|
286
|
+
return `[Attached file: ${part.name}]
|
|
287
|
+
${Buffer.from(part.data, "base64").toString("utf8")}`;
|
|
288
|
+
}
|
|
289
|
+
function imageData(part) {
|
|
290
|
+
if (part.data) return { mediaType: part.media_type, data: part.data };
|
|
291
|
+
const match = /^data:([^;,]+);base64,(.*)$/s.exec(part.uri ?? "");
|
|
292
|
+
return match ? { mediaType: match[1], data: match[2] } : null;
|
|
293
|
+
}
|
|
294
|
+
function plan(conversation, to, opts, map) {
|
|
295
|
+
const { messages, summarised } = fitConversation(conversation, to, { budgetChars: opts.budgetChars, summarize: opts.summarize });
|
|
296
|
+
const report = { messages: { user: 0, agent: 0 }, toolCalls: { mapped: 0, asText: 0 }, reasoningLeftOut: 0, summarised };
|
|
297
|
+
const planned = messages.map((message) => {
|
|
298
|
+
const answered = new Set(message.parts.filter((part) => part.type === "tool_result").map((part) => part.call_id));
|
|
299
|
+
const mapped = /* @__PURE__ */ new Map();
|
|
300
|
+
for (const part of message.parts) {
|
|
301
|
+
if (part.type === "reasoning") report.reasoningLeftOut += 1;
|
|
302
|
+
if (part.type !== "tool_call") continue;
|
|
303
|
+
if (answered.has(part.call_id) && map(part)) {
|
|
304
|
+
mapped.set(part.call_id, true);
|
|
305
|
+
report.toolCalls.mapped += 1;
|
|
306
|
+
} else report.toolCalls.asText += 1;
|
|
307
|
+
}
|
|
308
|
+
return { message, mapped };
|
|
309
|
+
});
|
|
310
|
+
return { planned, report };
|
|
311
|
+
}
|
|
312
|
+
function resultsOf(message) {
|
|
313
|
+
return new Map(message.parts.filter((part) => part.type === "tool_result").map((part) => [part.call_id, part]));
|
|
314
|
+
}
|
|
315
|
+
function iso(at, fallback) {
|
|
316
|
+
const parsed = Date.parse(at);
|
|
317
|
+
return Number.isFinite(parsed) ? new Date(parsed).toISOString() : fallback.toISOString();
|
|
318
|
+
}
|
|
319
|
+
function claudeProjectFolder(cwd) {
|
|
320
|
+
return cwd.replace(/[^a-zA-Z0-9]/g, "-");
|
|
321
|
+
}
|
|
322
|
+
function writeClaudeCode(conversation, opts = {}) {
|
|
323
|
+
const cwd = opts.cwd ?? conversation.cwd ?? "/";
|
|
324
|
+
const now = opts.now ?? /* @__PURE__ */ new Date();
|
|
325
|
+
const nativeId = opts.nativeId ?? randomUUID();
|
|
326
|
+
const calls = /* @__PURE__ */ new Map();
|
|
327
|
+
const { planned, report } = plan(conversation, "claude_code", opts, (call) => {
|
|
328
|
+
const native = toClaudeCode(call, cwd);
|
|
329
|
+
if (native) calls.set(call.call_id, native);
|
|
330
|
+
return native;
|
|
331
|
+
});
|
|
332
|
+
const lines2 = [];
|
|
333
|
+
let parent = null;
|
|
334
|
+
const push = (type, message, at) => {
|
|
335
|
+
const uuid = randomUUID();
|
|
336
|
+
lines2.push(JSON.stringify({
|
|
337
|
+
parentUuid: parent,
|
|
338
|
+
isSidechain: false,
|
|
339
|
+
userType: "external",
|
|
340
|
+
cwd,
|
|
341
|
+
sessionId: nativeId,
|
|
342
|
+
version: opts.version ?? "2.1.0",
|
|
343
|
+
type,
|
|
344
|
+
message,
|
|
345
|
+
uuid,
|
|
346
|
+
timestamp: iso(at, now)
|
|
347
|
+
}));
|
|
348
|
+
parent = uuid;
|
|
349
|
+
};
|
|
350
|
+
for (const { message, mapped } of planned) {
|
|
351
|
+
if (message.role === "user" || message.role === "system") {
|
|
352
|
+
const content2 = [];
|
|
353
|
+
for (const part of message.parts) {
|
|
354
|
+
if (part.type === "text") content2.push({ type: "text", text: part.text });
|
|
355
|
+
else if (part.type === "image") {
|
|
356
|
+
const image = imageData(part);
|
|
357
|
+
content2.push(image ? { type: "image", source: { type: "base64", media_type: image.mediaType, data: image.data } } : { type: "text", text: "[image]" });
|
|
358
|
+
} else if (part.type === "file") content2.push({ type: "text", text: decoded(part) });
|
|
359
|
+
}
|
|
360
|
+
if (!content2.length) continue;
|
|
361
|
+
report.messages.user += 1;
|
|
362
|
+
const only = content2.length === 1 && content2[0].type === "text" ? content2[0].text : null;
|
|
363
|
+
push("user", { role: "user", content: only ?? content2 }, message.created_at);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const results = resultsOf(message);
|
|
367
|
+
const content = [];
|
|
368
|
+
const answers = [];
|
|
369
|
+
for (const part of message.parts) {
|
|
370
|
+
if (part.type === "text") content.push({ type: "text", text: part.text });
|
|
371
|
+
else if (part.type === "image") content.push({ type: "text", text: "[image]" });
|
|
372
|
+
else if (part.type === "tool_call") {
|
|
373
|
+
const result = results.get(part.call_id);
|
|
374
|
+
const native = calls.get(part.call_id);
|
|
375
|
+
if (mapped.has(part.call_id) && native && result) {
|
|
376
|
+
content.push({ type: "tool_use", id: part.call_id, name: native.name, input: native.input });
|
|
377
|
+
answers.push({ type: "tool_result", tool_use_id: part.call_id, content: cut(resultText(result)), ...result.status === "error" ? { is_error: true } : {} });
|
|
378
|
+
} else content.push({ type: "text", text: callAsText(conversation.engine, part, result) });
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (!content.length) continue;
|
|
382
|
+
report.messages.agent += 1;
|
|
383
|
+
push("assistant", {
|
|
384
|
+
id: `msg_${randomBytes(12).toString("hex")}`,
|
|
385
|
+
type: "message",
|
|
386
|
+
role: "assistant",
|
|
387
|
+
model: conversation.model ?? "unknown",
|
|
388
|
+
content,
|
|
389
|
+
stop_reason: answers.length ? "tool_use" : "end_turn",
|
|
390
|
+
stop_sequence: null,
|
|
391
|
+
usage: { input_tokens: 0, output_tokens: 0 }
|
|
392
|
+
}, message.created_at);
|
|
393
|
+
if (answers.length) push("user", { role: "user", content: answers }, message.created_at);
|
|
394
|
+
}
|
|
395
|
+
return {
|
|
396
|
+
engine: "claude_code",
|
|
397
|
+
nativeId,
|
|
398
|
+
cwd,
|
|
399
|
+
path: `projects/${claudeProjectFolder(cwd)}/${nativeId}.jsonl`,
|
|
400
|
+
content: lines2.map((line) => `${line}
|
|
401
|
+
`).join(""),
|
|
402
|
+
resume: `claude --resume ${nativeId}`,
|
|
403
|
+
report
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
function rolloutStamp(at) {
|
|
407
|
+
return at.toISOString().slice(0, 19).replace(/:/g, "-");
|
|
408
|
+
}
|
|
409
|
+
function writeCodex(conversation, opts = {}) {
|
|
410
|
+
const cwd = opts.cwd ?? conversation.cwd ?? "/";
|
|
411
|
+
const now = opts.now ?? /* @__PURE__ */ new Date();
|
|
412
|
+
const nativeId = opts.nativeId ?? randomUUID();
|
|
413
|
+
const calls = /* @__PURE__ */ new Map();
|
|
414
|
+
const { planned, report } = plan(conversation, "codex", opts, (call) => {
|
|
415
|
+
const native = toCodex(call, cwd);
|
|
416
|
+
if (native) calls.set(call.call_id, native);
|
|
417
|
+
return native;
|
|
418
|
+
});
|
|
419
|
+
const lines2 = [];
|
|
420
|
+
const push = (type, payload, at) => lines2.push(JSON.stringify({ timestamp: iso(at, now), type, payload }));
|
|
421
|
+
push("session_meta", {
|
|
422
|
+
id: nativeId,
|
|
423
|
+
timestamp: now.toISOString(),
|
|
424
|
+
cwd,
|
|
425
|
+
originator: "codex_cli_rs",
|
|
426
|
+
cli_version: opts.version ?? "0.61.0",
|
|
427
|
+
instructions: null,
|
|
428
|
+
source: "cli",
|
|
429
|
+
model_provider: "openai"
|
|
430
|
+
}, now.toISOString());
|
|
431
|
+
for (const { message, mapped } of planned) {
|
|
432
|
+
if (message.role === "user" || message.role === "system") {
|
|
433
|
+
const content = [];
|
|
434
|
+
for (const part of message.parts) {
|
|
435
|
+
if (part.type === "text") content.push({ type: "input_text", text: part.text });
|
|
436
|
+
else if (part.type === "image") {
|
|
437
|
+
const image = imageData(part);
|
|
438
|
+
content.push(image ? { type: "input_image", image_url: `data:${image.mediaType};base64,${image.data}` } : { type: "input_text", text: "[image]" });
|
|
439
|
+
} else if (part.type === "file") content.push({ type: "input_text", text: decoded(part) });
|
|
440
|
+
}
|
|
441
|
+
if (!content.length) continue;
|
|
442
|
+
report.messages.user += 1;
|
|
443
|
+
push("response_item", { type: "message", role: "user", content }, message.created_at);
|
|
444
|
+
const text = content.filter((block) => block.type === "input_text").map((block) => block.text).join("\n");
|
|
445
|
+
push("event_msg", { type: "user_message", message: text, images: [] }, message.created_at);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
const results = resultsOf(message);
|
|
449
|
+
let wrote = false;
|
|
450
|
+
let pending = "";
|
|
451
|
+
const say = () => {
|
|
452
|
+
if (!pending) return;
|
|
453
|
+
push("response_item", { type: "message", role: "assistant", content: [{ type: "output_text", text: pending }] }, message.created_at);
|
|
454
|
+
push("event_msg", { type: "agent_message", message: pending }, message.created_at);
|
|
455
|
+
pending = "";
|
|
456
|
+
wrote = true;
|
|
457
|
+
};
|
|
458
|
+
for (const part of message.parts) {
|
|
459
|
+
if (part.type === "text") pending = pending ? `${pending}
|
|
460
|
+
|
|
461
|
+
${part.text}` : part.text;
|
|
462
|
+
else if (part.type === "image") pending = pending ? `${pending}
|
|
463
|
+
|
|
464
|
+
[image]` : "[image]";
|
|
465
|
+
else if (part.type === "tool_call") {
|
|
466
|
+
const result = results.get(part.call_id);
|
|
467
|
+
const native = calls.get(part.call_id);
|
|
468
|
+
if (!(mapped.has(part.call_id) && native && result)) {
|
|
469
|
+
const text = callAsText(conversation.engine, part, result);
|
|
470
|
+
pending = pending ? `${pending}
|
|
471
|
+
|
|
472
|
+
${text}` : text;
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
say();
|
|
476
|
+
push("response_item", native.kind === "custom" ? { type: "custom_tool_call", status: "completed", call_id: part.call_id, name: native.name, input: native.input } : { type: "function_call", name: native.name, arguments: JSON.stringify(native.arguments), call_id: part.call_id }, message.created_at);
|
|
477
|
+
wrote = true;
|
|
478
|
+
} else if (part.type === "tool_result" && mapped.has(part.call_id)) {
|
|
479
|
+
say();
|
|
480
|
+
const native = calls.get(part.call_id);
|
|
481
|
+
const output = cut(resultText(part));
|
|
482
|
+
push(
|
|
483
|
+
"response_item",
|
|
484
|
+
native.kind === "custom" ? { type: "custom_tool_call_output", call_id: part.call_id, output } : { type: "function_call_output", call_id: part.call_id, output: JSON.stringify({ output, metadata: { exit_code: part.status === "error" ? 1 : 0, duration_seconds: 0 } }) },
|
|
485
|
+
message.created_at
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
say();
|
|
490
|
+
if (wrote) report.messages.agent += 1;
|
|
491
|
+
}
|
|
492
|
+
const day = now.toISOString().slice(0, 10).split("-").join("/");
|
|
493
|
+
return {
|
|
494
|
+
engine: "codex",
|
|
495
|
+
nativeId,
|
|
496
|
+
cwd,
|
|
497
|
+
path: `sessions/${day}/rollout-${rolloutStamp(now)}-${nativeId}.jsonl`,
|
|
498
|
+
content: lines2.map((line) => `${line}
|
|
499
|
+
`).join(""),
|
|
500
|
+
resume: `codex resume ${nativeId}`,
|
|
501
|
+
report
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
function writeConversation(to, conversation, opts = {}) {
|
|
505
|
+
return to === "codex" ? writeCodex(conversation, opts) : writeClaudeCode(conversation, opts);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// ../../packages/spec/src/native/triage.ts
|
|
509
|
+
var KEPT_ATTACHMENT_KINDS = /* @__PURE__ */ new Set([
|
|
510
|
+
"queued_command",
|
|
511
|
+
"edited_text_file"
|
|
512
|
+
]);
|
|
513
|
+
var DROPPED_ATTACHMENT_KINDS = /* @__PURE__ */ new Set([
|
|
514
|
+
"total_tokens_reminder",
|
|
515
|
+
"batching_reminder_sent",
|
|
516
|
+
"hook_success",
|
|
517
|
+
"hook_non_blocking_error",
|
|
518
|
+
"hook_additional_context",
|
|
519
|
+
"environment",
|
|
520
|
+
"deferred_tools_delta",
|
|
521
|
+
"compact_file_reference"
|
|
522
|
+
]);
|
|
523
|
+
var DROPPED_LINE_TYPES = /* @__PURE__ */ new Set([
|
|
524
|
+
"bridge-session",
|
|
525
|
+
"queue-operation",
|
|
526
|
+
"last-prompt",
|
|
527
|
+
"custom-title",
|
|
528
|
+
"atis-latch",
|
|
529
|
+
"pr-link",
|
|
530
|
+
"frame-link",
|
|
531
|
+
"mode",
|
|
532
|
+
"file-history-snapshot",
|
|
533
|
+
"file-history-delta",
|
|
534
|
+
"ai-title",
|
|
535
|
+
"agent-setting",
|
|
536
|
+
"permission-mode",
|
|
537
|
+
"cost-state",
|
|
538
|
+
"relocated",
|
|
539
|
+
"artifact-autoreact-ledger",
|
|
540
|
+
"artifact-comment-monitor"
|
|
541
|
+
]);
|
|
542
|
+
function isInjectedTurn(line) {
|
|
543
|
+
return line.isMeta === true || line.isSynthetic === true || line.isCompactSummary === true;
|
|
544
|
+
}
|
|
545
|
+
function isImageCompanion(blocks) {
|
|
546
|
+
if (blocks.length === 0) return false;
|
|
547
|
+
return blocks.every((block) => {
|
|
548
|
+
if (typeof block !== "object" || block === null) return false;
|
|
549
|
+
const b = block;
|
|
550
|
+
return b.type === "text" && typeof b.text === "string" && /^\s*\[Image:\s*source:/.test(b.text);
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
function triage(line) {
|
|
554
|
+
const type = typeof line.type === "string" ? line.type : null;
|
|
555
|
+
if (type === null) return { keep: false, reason: "no line type" };
|
|
556
|
+
if (type === "attachment") {
|
|
557
|
+
const payload = typeof line.attachment === "object" && line.attachment !== null ? line.attachment : line;
|
|
558
|
+
const kind = typeof payload.type === "string" ? payload.type : "";
|
|
559
|
+
if (KEPT_ATTACHMENT_KINDS.has(kind)) return { keep: true, reason: `attachment:${kind}` };
|
|
560
|
+
if (DROPPED_ATTACHMENT_KINDS.has(kind)) return { keep: false, reason: `bookkeeping:${kind}` };
|
|
561
|
+
return { keep: true, reason: `attachment:unclassified:${kind || "unnamed"}` };
|
|
562
|
+
}
|
|
563
|
+
if (DROPPED_LINE_TYPES.has(type)) return { keep: false, reason: `bookkeeping:${type}` };
|
|
564
|
+
if (type === "user" || type === "assistant") return { keep: true, reason: type };
|
|
565
|
+
if (type === "system") return { keep: false, reason: "harness system line" };
|
|
566
|
+
return { keep: true, reason: `unclassified:${type}` };
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// ../../packages/spec/src/native/claude-code.ts
|
|
570
|
+
var INTERRUPT_MARKERS = [
|
|
571
|
+
"[Request interrupted by user",
|
|
572
|
+
"Tool use was rejected"
|
|
573
|
+
];
|
|
574
|
+
function textOf(value) {
|
|
575
|
+
return typeof value === "string" ? value : "";
|
|
576
|
+
}
|
|
577
|
+
function basename(path) {
|
|
578
|
+
const parts = path.split(/[\\/]/);
|
|
579
|
+
return parts[parts.length - 1] ?? path;
|
|
580
|
+
}
|
|
581
|
+
function guessMediaType(name) {
|
|
582
|
+
const ext = name.slice(name.lastIndexOf(".") + 1).toLowerCase();
|
|
583
|
+
const map = {
|
|
584
|
+
png: "image/png",
|
|
585
|
+
jpg: "image/jpeg",
|
|
586
|
+
jpeg: "image/jpeg",
|
|
587
|
+
gif: "image/gif",
|
|
588
|
+
webp: "image/webp",
|
|
589
|
+
pdf: "application/pdf",
|
|
590
|
+
json: "application/json",
|
|
591
|
+
csv: "text/csv",
|
|
592
|
+
md: "text/markdown",
|
|
593
|
+
html: "text/html",
|
|
594
|
+
css: "text/css",
|
|
595
|
+
js: "text/javascript",
|
|
596
|
+
ts: "text/typescript",
|
|
597
|
+
tsx: "text/typescript",
|
|
598
|
+
py: "text/x-python"
|
|
599
|
+
};
|
|
600
|
+
return map[ext] ?? "text/plain";
|
|
601
|
+
}
|
|
602
|
+
function looksInterrupted(blocks) {
|
|
603
|
+
return blocks.some((block) => {
|
|
604
|
+
if (typeof block !== "object" || block === null) return false;
|
|
605
|
+
const text = textOf(block.text);
|
|
606
|
+
return INTERRUPT_MARKERS.some((marker) => text.includes(marker));
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
function imageBlockToPart(block, index) {
|
|
610
|
+
const source = typeof block.source === "object" && block.source !== null ? block.source : {};
|
|
611
|
+
const data = textOf(source.data);
|
|
612
|
+
const uri = textOf(source.url) || textOf(block.url);
|
|
613
|
+
if (!data && !uri) return null;
|
|
614
|
+
const embedded = uri && isEmbeddedImage(uri) ? uri : "";
|
|
615
|
+
if (!data && !embedded) return { type: "text", index, text: imageLinkText(uri) };
|
|
616
|
+
return {
|
|
617
|
+
type: "image",
|
|
618
|
+
index,
|
|
619
|
+
media_type: textOf(source.media_type) || imageMediaType(embedded || uri || `data:${textOf(source.type)};`),
|
|
620
|
+
...embedded ? { uri: embedded } : {},
|
|
621
|
+
...data ? { data } : {}
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
function documentBlockToPart(block, index) {
|
|
625
|
+
const source = typeof block.source === "object" && block.source !== null ? block.source : {};
|
|
626
|
+
const mediaType = textOf(source.media_type) || "application/pdf";
|
|
627
|
+
const name = textOf(block.title) || `document.${mediaType === "application/pdf" ? "pdf" : mediaType === "text/plain" ? "txt" : "bin"}`;
|
|
628
|
+
if (source.type === "base64" && textOf(source.data)) {
|
|
629
|
+
const data = textOf(source.data);
|
|
630
|
+
return { type: "file", index, name, media_type: mediaType, data, size_bytes: Math.floor(data.replace(/=+$/, "").length * 3 / 4) };
|
|
631
|
+
}
|
|
632
|
+
if (source.type === "text" && textOf(source.data)) {
|
|
633
|
+
const bytes = new TextEncoder().encode(textOf(source.data));
|
|
634
|
+
let binary = "";
|
|
635
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
636
|
+
return { type: "file", index, name, media_type: "text/plain", data: btoa(binary), size_bytes: bytes.length };
|
|
637
|
+
}
|
|
638
|
+
const url = textOf(source.url);
|
|
639
|
+
return url ? { type: "text", index, text: remoteLinkText("document", url) } : null;
|
|
640
|
+
}
|
|
641
|
+
function isEmbeddedImage(url) {
|
|
642
|
+
return /^data:/i.test(url);
|
|
643
|
+
}
|
|
644
|
+
function imageLinkText(url) {
|
|
645
|
+
return remoteLinkText("image", url);
|
|
646
|
+
}
|
|
647
|
+
function remoteLinkText(kind, url) {
|
|
648
|
+
return `[${kind} not carried, linked at ${url}]`;
|
|
649
|
+
}
|
|
650
|
+
function imageMediaType(url) {
|
|
651
|
+
const data = /^data:([a-zA-Z0-9.+-]+\/[a-zA-Z0-9.+-]+)[;,]/.exec(url);
|
|
652
|
+
if (data) return data[1].toLowerCase();
|
|
653
|
+
const ext = /\.([a-zA-Z0-9]+)(?:[?#]|$)/.exec(url)?.[1]?.toLowerCase();
|
|
654
|
+
const byExtension = {
|
|
655
|
+
png: "image/png",
|
|
656
|
+
jpg: "image/jpeg",
|
|
657
|
+
jpeg: "image/jpeg",
|
|
658
|
+
gif: "image/gif",
|
|
659
|
+
webp: "image/webp",
|
|
660
|
+
svg: "image/svg+xml",
|
|
661
|
+
bmp: "image/bmp",
|
|
662
|
+
avif: "image/avif"
|
|
663
|
+
};
|
|
664
|
+
return ext && byExtension[ext] || "application/octet-stream";
|
|
665
|
+
}
|
|
666
|
+
function assistantParts(blocks) {
|
|
667
|
+
const parts = [];
|
|
668
|
+
const calls = [];
|
|
669
|
+
blocks.forEach((raw, index) => {
|
|
670
|
+
if (typeof raw !== "object" || raw === null) return;
|
|
671
|
+
const block = raw;
|
|
672
|
+
switch (block.type) {
|
|
673
|
+
case "text": {
|
|
674
|
+
const text = textOf(block.text);
|
|
675
|
+
if (text) parts.push({ type: "text", index, text });
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
case "thinking": {
|
|
679
|
+
const text = textOf(block.thinking);
|
|
680
|
+
if (text) parts.push({ type: "reasoning", index, text, redacted: false });
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
case "image": {
|
|
684
|
+
const part = imageBlockToPart(block, index);
|
|
685
|
+
if (part) parts.push(part);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
case "tool_use": {
|
|
689
|
+
calls.push({
|
|
690
|
+
type: "agent.tool_use",
|
|
691
|
+
part: {
|
|
692
|
+
type: "tool_call",
|
|
693
|
+
index,
|
|
694
|
+
call_id: textOf(block.id),
|
|
695
|
+
tool_name: textOf(block.name),
|
|
696
|
+
args: block.input ?? {}
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
default:
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
return { parts, calls };
|
|
706
|
+
}
|
|
707
|
+
function toolResults(blocks) {
|
|
708
|
+
const out = [];
|
|
709
|
+
blocks.forEach((raw, index) => {
|
|
710
|
+
if (typeof raw !== "object" || raw === null) return;
|
|
711
|
+
const block = raw;
|
|
712
|
+
if (block.type !== "tool_result") return;
|
|
713
|
+
const content = block.content;
|
|
714
|
+
const output = typeof content === "string" ? content : content === void 0 ? "" : JSON.stringify(content);
|
|
715
|
+
out.push({
|
|
716
|
+
type: "agent.tool_result",
|
|
717
|
+
part: {
|
|
718
|
+
type: "tool_result",
|
|
719
|
+
index,
|
|
720
|
+
call_id: textOf(block.tool_use_id),
|
|
721
|
+
tool_name: "",
|
|
722
|
+
status: block.is_error === true ? "error" : "ok",
|
|
723
|
+
result: { output }
|
|
724
|
+
}
|
|
725
|
+
});
|
|
726
|
+
});
|
|
727
|
+
return out;
|
|
728
|
+
}
|
|
729
|
+
var RENDERED_BLOCKS = /* @__PURE__ */ new Set(["text", "thinking", "image", "document", "tool_use", "tool_result"]);
|
|
730
|
+
function unsupportedBlock(content) {
|
|
731
|
+
if (!Array.isArray(content)) return false;
|
|
732
|
+
return content.some((raw) => {
|
|
733
|
+
if (typeof raw !== "object" || raw === null) return false;
|
|
734
|
+
const type = raw.type;
|
|
735
|
+
return typeof type === "string" && !RENDERED_BLOCKS.has(type);
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
function preservedAsJson(label, filename, payload, reason) {
|
|
739
|
+
const text = JSON.stringify(payload);
|
|
740
|
+
const bytes = new TextEncoder().encode(text);
|
|
741
|
+
let binary = "";
|
|
742
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
743
|
+
return {
|
|
744
|
+
events: [{ type: "agent.message", parts: [
|
|
745
|
+
{ type: "text", index: 0, text: label },
|
|
746
|
+
{ type: "file", index: 1, name: filename, media_type: "application/json", data: btoa(binary), size_bytes: bytes.length }
|
|
747
|
+
] }],
|
|
748
|
+
notes: [{ kind: "unsupported", reason }]
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
function decodeLine(line) {
|
|
752
|
+
const verdict = triage(line);
|
|
753
|
+
if (!verdict.keep) return { events: [], notes: [{ kind: "dropped", reason: verdict.reason }] };
|
|
754
|
+
const type = line.type;
|
|
755
|
+
if (type === "attachment") {
|
|
756
|
+
const payload = typeof line.attachment === "object" && line.attachment !== null ? line.attachment : line;
|
|
757
|
+
if (payload.type === "queued_command") {
|
|
758
|
+
const text = textOf(payload.prompt);
|
|
759
|
+
if (!text) return { events: [], notes: [] };
|
|
760
|
+
return {
|
|
761
|
+
events: [{ type: "user.message", parts: [{ type: "text", index: 0, text }] }],
|
|
762
|
+
notes: []
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
if (payload.type === "file" || payload.type === "edited_text_file") {
|
|
766
|
+
const name = textOf(payload.displayPath) || basename(textOf(payload.filename)) || "attachment.txt";
|
|
767
|
+
const content2 = payload.content;
|
|
768
|
+
const nested = content2 && typeof content2 === "object" ? content2 : {};
|
|
769
|
+
const file = nested.file && typeof nested.file === "object" ? nested.file : {};
|
|
770
|
+
const text = typeof content2 === "string" ? content2 : textOf(file.content) || textOf(nested.content) || textOf(payload.snippet);
|
|
771
|
+
const bytes = new TextEncoder().encode(text);
|
|
772
|
+
let binary = "";
|
|
773
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
774
|
+
if (!name) return { events: [], notes: [] };
|
|
775
|
+
return {
|
|
776
|
+
events: [
|
|
777
|
+
{
|
|
778
|
+
type: "user.message",
|
|
779
|
+
parts: [{ type: "file", index: 0, name, media_type: guessMediaType(name), ...text ? { data: btoa(binary), size_bytes: bytes.length } : {} }]
|
|
780
|
+
}
|
|
781
|
+
],
|
|
782
|
+
notes: []
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
return preservedAsJson(
|
|
786
|
+
`Source attachment: ${textOf(payload.type) || "unnamed"}`,
|
|
787
|
+
"source-attachment.json",
|
|
788
|
+
payload,
|
|
789
|
+
`attachment:${textOf(payload.type) || "unnamed"} preserved as JSON`
|
|
790
|
+
);
|
|
791
|
+
}
|
|
792
|
+
const messageContent = typeof line.message === "object" && line.message !== null ? line.message.content : null;
|
|
793
|
+
if (unsupportedBlock(messageContent)) {
|
|
794
|
+
return preservedAsJson(
|
|
795
|
+
`Source record: ${type || "untyped"} (it carries a block this reader does not render)`,
|
|
796
|
+
"source-record.json",
|
|
797
|
+
line,
|
|
798
|
+
`unsupported-block:${type || "untyped"} preserved as JSON`
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
if (verdict.reason.startsWith("unclassified:")) {
|
|
802
|
+
return preservedAsJson(
|
|
803
|
+
`Source record: ${type || "untyped"}`,
|
|
804
|
+
"source-record.json",
|
|
805
|
+
line,
|
|
806
|
+
`${verdict.reason} preserved as JSON`
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
const message = typeof line.message === "object" && line.message !== null ? line.message : null;
|
|
810
|
+
if (!message) return { events: [], notes: [] };
|
|
811
|
+
const content = message.content;
|
|
812
|
+
if (type === "user") {
|
|
813
|
+
if (typeof content === "string") {
|
|
814
|
+
if (isInjectedTurn(line) || !content.trim()) return { events: [], notes: [] };
|
|
815
|
+
return {
|
|
816
|
+
events: [{ type: "user.message", parts: [{ type: "text", index: 0, text: content }] }],
|
|
817
|
+
notes: []
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
if (!Array.isArray(content)) return { events: [], notes: [] };
|
|
821
|
+
if (looksInterrupted(content)) {
|
|
822
|
+
return {
|
|
823
|
+
events: [
|
|
824
|
+
{ type: "agent.status", part: { type: "status", index: 0, level: "idle", text: "Conversation interrupted" } }
|
|
825
|
+
],
|
|
826
|
+
notes: [{ kind: "interrupted" }]
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
const results = toolResults(content);
|
|
830
|
+
if (isInjectedTurn(line)) {
|
|
831
|
+
if (results.length > 0) return { events: results, notes: [] };
|
|
832
|
+
if (isImageCompanion(content)) {
|
|
833
|
+
const parts2 = [];
|
|
834
|
+
let images = 0;
|
|
835
|
+
content.forEach((raw, index) => {
|
|
836
|
+
if (typeof raw !== "object" || raw === null) return;
|
|
837
|
+
const b = raw;
|
|
838
|
+
if (b.type === "image") {
|
|
839
|
+
const part = imageBlockToPart(b, index);
|
|
840
|
+
if (part) {
|
|
841
|
+
parts2.push(part);
|
|
842
|
+
images += 1;
|
|
843
|
+
}
|
|
844
|
+
} else if (typeof b.text === "string" && b.text) {
|
|
845
|
+
parts2.push({ type: "text", index, text: b.text });
|
|
846
|
+
}
|
|
847
|
+
});
|
|
848
|
+
if (parts2.length === 0) return { events: [], notes: [{ kind: "dropped", reason: "injected turn" }] };
|
|
849
|
+
return {
|
|
850
|
+
events: [{ type: "user.message", parts: parts2 }],
|
|
851
|
+
notes: [{ kind: "image-companion", count: images }]
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
return { events: [], notes: [{ kind: "dropped", reason: "injected turn" }] };
|
|
855
|
+
}
|
|
856
|
+
const parts = [];
|
|
857
|
+
content.forEach((raw, index) => {
|
|
858
|
+
if (typeof raw !== "object" || raw === null) return;
|
|
859
|
+
const b = raw;
|
|
860
|
+
if (b.type === "text") {
|
|
861
|
+
const text = textOf(b.text);
|
|
862
|
+
if (text.trim()) parts.push({ type: "text", index, text });
|
|
863
|
+
} else if (b.type === "image") {
|
|
864
|
+
const part = imageBlockToPart(b, index);
|
|
865
|
+
if (part) parts.push(part);
|
|
866
|
+
} else if (b.type === "document") {
|
|
867
|
+
const part = documentBlockToPart(b, index);
|
|
868
|
+
if (part) parts.push(part);
|
|
869
|
+
}
|
|
870
|
+
});
|
|
871
|
+
const events = [...results];
|
|
872
|
+
if (parts.length > 0) events.push({ type: "user.message", parts });
|
|
873
|
+
return { events, notes: [] };
|
|
874
|
+
}
|
|
875
|
+
if (type === "assistant" && Array.isArray(content)) {
|
|
876
|
+
const { parts, calls } = assistantParts(content);
|
|
877
|
+
const events = [];
|
|
878
|
+
if (parts.length > 0) events.push({ type: "agent.message", parts });
|
|
879
|
+
events.push(...calls);
|
|
880
|
+
return {
|
|
881
|
+
events,
|
|
882
|
+
notes: [],
|
|
883
|
+
spec: {
|
|
884
|
+
llm: textOf(message.model) || void 0,
|
|
885
|
+
reasoningEffort: textOf(line.effort) || void 0
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
return { events: [], notes: [] };
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
// ../../packages/spec/src/native/codex.ts
|
|
893
|
+
var EMPTY = { events: [], spec: {} };
|
|
894
|
+
function str2(value) {
|
|
895
|
+
return typeof value === "string" ? value : "";
|
|
896
|
+
}
|
|
897
|
+
function record(value) {
|
|
898
|
+
return typeof value === "object" && value !== null ? value : null;
|
|
899
|
+
}
|
|
900
|
+
function messageText2(content) {
|
|
901
|
+
if (typeof content === "string") return content;
|
|
902
|
+
if (!Array.isArray(content)) return "";
|
|
903
|
+
return content.map((block) => {
|
|
904
|
+
const b = record(block);
|
|
905
|
+
if (!b) return "";
|
|
906
|
+
return str2(b.text) || str2(b.output_text);
|
|
907
|
+
}).join("");
|
|
908
|
+
}
|
|
909
|
+
function imageMediaType2(url) {
|
|
910
|
+
const data = /^data:([a-zA-Z0-9.+-]+\/[a-zA-Z0-9.+-]+)[;,]/.exec(url);
|
|
911
|
+
if (data) return data[1].toLowerCase();
|
|
912
|
+
const ext = /\.([a-zA-Z0-9]+)(?:[?#]|$)/.exec(url)?.[1]?.toLowerCase();
|
|
913
|
+
const byExtension = {
|
|
914
|
+
png: "image/png",
|
|
915
|
+
jpg: "image/jpeg",
|
|
916
|
+
jpeg: "image/jpeg",
|
|
917
|
+
gif: "image/gif",
|
|
918
|
+
webp: "image/webp",
|
|
919
|
+
svg: "image/svg+xml",
|
|
920
|
+
bmp: "image/bmp",
|
|
921
|
+
avif: "image/avif"
|
|
922
|
+
};
|
|
923
|
+
return ext && byExtension[ext] || "application/octet-stream";
|
|
924
|
+
}
|
|
925
|
+
function messageParts(content) {
|
|
926
|
+
if (typeof content === "string") {
|
|
927
|
+
return content ? { parts: [{ type: "text", index: 0, text: content }], text: content } : { parts: [], text: "" };
|
|
928
|
+
}
|
|
929
|
+
if (!Array.isArray(content)) return { parts: [], text: "" };
|
|
930
|
+
const parts = [];
|
|
931
|
+
let text = "";
|
|
932
|
+
for (const raw of content) {
|
|
933
|
+
const b = record(raw);
|
|
934
|
+
if (!b) continue;
|
|
935
|
+
if (b.type === "input_image" || b.type === "image" || b.type === "image_url") {
|
|
936
|
+
const url = str2(b.image_url) || str2(record(b.image_url)?.url) || str2(b.url);
|
|
937
|
+
const declared = str2(b.media_type) || str2(b.mime_type);
|
|
938
|
+
if (url && isEmbeddedImage(url)) parts.push({ type: "image", index: parts.length, media_type: declared || imageMediaType2(url), uri: url });
|
|
939
|
+
else if (url) parts.push({ type: "text", index: parts.length, text: imageLinkText(url) });
|
|
940
|
+
continue;
|
|
941
|
+
}
|
|
942
|
+
const chunk = str2(b.text) || str2(b.output_text);
|
|
943
|
+
if (!chunk) continue;
|
|
944
|
+
text += chunk;
|
|
945
|
+
const last = parts[parts.length - 1];
|
|
946
|
+
if (last && last.type === "text") last.text += chunk;
|
|
947
|
+
else parts.push({ type: "text", index: parts.length, text: chunk });
|
|
948
|
+
}
|
|
949
|
+
return { parts, text };
|
|
950
|
+
}
|
|
951
|
+
function reasoningText(payload) {
|
|
952
|
+
const direct = str2(payload.text);
|
|
953
|
+
if (direct) return direct;
|
|
954
|
+
const summary = payload.summary;
|
|
955
|
+
if (typeof summary === "string") return summary;
|
|
956
|
+
if (Array.isArray(summary)) {
|
|
957
|
+
return summary.map((entry) => typeof entry === "string" ? entry : str2(record(entry)?.text)).filter(Boolean).join("\n");
|
|
958
|
+
}
|
|
959
|
+
return "";
|
|
960
|
+
}
|
|
961
|
+
function callArgs(payload) {
|
|
962
|
+
const raw = payload.arguments;
|
|
963
|
+
if (typeof raw === "string" && raw.trim()) {
|
|
964
|
+
if (unsafeNumberSpans(raw).length > 0) return { arguments: raw };
|
|
965
|
+
try {
|
|
966
|
+
const parsed = JSON.parse(raw);
|
|
967
|
+
if (record(parsed)) return parsed;
|
|
968
|
+
return { arguments: parsed };
|
|
969
|
+
} catch {
|
|
970
|
+
return { arguments: raw };
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
return record(payload.action) ?? record(payload.input) ?? {};
|
|
974
|
+
}
|
|
975
|
+
var INJECTED_USER_OPENERS = [
|
|
976
|
+
"# AGENTS.md instructions",
|
|
977
|
+
"<INSTRUCTIONS>",
|
|
978
|
+
"<codex_internal_context",
|
|
979
|
+
"<in-app-browser-context",
|
|
980
|
+
"<user_instructions>",
|
|
981
|
+
"<environment_context>",
|
|
982
|
+
"[Context: repo="
|
|
983
|
+
];
|
|
984
|
+
function looksInjected(text) {
|
|
985
|
+
const head = text.trimStart();
|
|
986
|
+
return INJECTED_USER_OPENERS.some((marker) => head.startsWith(marker));
|
|
987
|
+
}
|
|
988
|
+
function toolName(payload) {
|
|
989
|
+
const named = str2(payload.name);
|
|
990
|
+
if (named) return named;
|
|
991
|
+
if (payload.type === "local_shell_call") return "shell";
|
|
992
|
+
return str2(payload.type) || "tool";
|
|
993
|
+
}
|
|
994
|
+
function callIdOf(payload) {
|
|
995
|
+
return str2(payload.call_id);
|
|
996
|
+
}
|
|
997
|
+
function callOrOwnId(payload) {
|
|
998
|
+
return str2(payload.call_id) || str2(payload.id);
|
|
999
|
+
}
|
|
1000
|
+
function decodeCodexLine(line) {
|
|
1001
|
+
const type = str2(line.type);
|
|
1002
|
+
const payload = record(line.payload);
|
|
1003
|
+
if (type === "session_meta") {
|
|
1004
|
+
if (!payload) return EMPTY;
|
|
1005
|
+
return {
|
|
1006
|
+
events: [],
|
|
1007
|
+
spec: {}
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
if (type === "turn_context") {
|
|
1011
|
+
if (!payload) return EMPTY;
|
|
1012
|
+
return {
|
|
1013
|
+
events: [],
|
|
1014
|
+
spec: {
|
|
1015
|
+
llm: str2(payload.model) || void 0,
|
|
1016
|
+
reasoningEffort: str2(payload.effort) || str2(payload.reasoning_effort) || void 0
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
}
|
|
1020
|
+
if (type === "compacted") return { events: [], spec: {} };
|
|
1021
|
+
if (type !== "event_msg" && type !== "response_item") return EMPTY;
|
|
1022
|
+
if (!payload) return EMPTY;
|
|
1023
|
+
const kind = str2(payload.type);
|
|
1024
|
+
if (kind === "message") {
|
|
1025
|
+
const role = str2(payload.role);
|
|
1026
|
+
if (role === "developer" || role === "system") return EMPTY;
|
|
1027
|
+
const { parts, text } = messageParts(payload.content);
|
|
1028
|
+
if (parts.length === 0) return EMPTY;
|
|
1029
|
+
if (role === "user" && text.trim() && looksInjected(text)) return EMPTY;
|
|
1030
|
+
if (role === "user") return { events: [{ type: "user.message", parts }], spec: {} };
|
|
1031
|
+
if (role === "assistant") return { events: [{ type: "agent.message", parts }], spec: {} };
|
|
1032
|
+
return EMPTY;
|
|
1033
|
+
}
|
|
1034
|
+
if (kind === "agent_message" || kind === "agent_message_delta") {
|
|
1035
|
+
const text = messageText2(payload.content) || str2(payload.delta) || str2(payload.text);
|
|
1036
|
+
if (!text) return EMPTY;
|
|
1037
|
+
const from = str2(payload.author) || "agent";
|
|
1038
|
+
const to = str2(payload.recipient);
|
|
1039
|
+
return { events: [{ type: "agent.message", parts: [{ type: "text", index: 0, text: `[${from}${to ? ` \u2192 ${to}` : ""}]
|
|
1040
|
+
${text}` }] }], spec: {} };
|
|
1041
|
+
}
|
|
1042
|
+
if (kind === "reasoning") {
|
|
1043
|
+
const text = reasoningText(payload);
|
|
1044
|
+
if (!text.trim()) return EMPTY;
|
|
1045
|
+
return {
|
|
1046
|
+
events: [
|
|
1047
|
+
{ type: "agent.reasoning", part: { type: "reasoning", index: 0, text, redacted: false } }
|
|
1048
|
+
],
|
|
1049
|
+
spec: {}
|
|
1050
|
+
};
|
|
1051
|
+
}
|
|
1052
|
+
if (kind === "function_call" || kind === "local_shell_call" || kind === "custom_tool_call") {
|
|
1053
|
+
const id = callOrOwnId(payload);
|
|
1054
|
+
if (!id) return EMPTY;
|
|
1055
|
+
return {
|
|
1056
|
+
events: [
|
|
1057
|
+
{
|
|
1058
|
+
type: "agent.tool_use",
|
|
1059
|
+
part: {
|
|
1060
|
+
type: "tool_call",
|
|
1061
|
+
index: 0,
|
|
1062
|
+
call_id: id,
|
|
1063
|
+
tool_name: toolName(payload),
|
|
1064
|
+
args: callArgs(payload)
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
],
|
|
1068
|
+
spec: {}
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
if (kind === "function_call_output" || kind === "custom_tool_call_output") {
|
|
1072
|
+
const id = callIdOf(payload);
|
|
1073
|
+
if (!id) return {
|
|
1074
|
+
events: [{ type: "agent.message", parts: [{
|
|
1075
|
+
type: "text",
|
|
1076
|
+
index: 0,
|
|
1077
|
+
text: `[Imported tool output: ${str2(payload.name) || "unknown tool"}; source has no call ID]
|
|
1078
|
+
${typeof payload.output === "string" ? payload.output : JSON.stringify(payload.output ?? "")}`
|
|
1079
|
+
}] }],
|
|
1080
|
+
spec: {},
|
|
1081
|
+
unattachedResult: true
|
|
1082
|
+
};
|
|
1083
|
+
const output = payload.output;
|
|
1084
|
+
const inner = record(output);
|
|
1085
|
+
const text = typeof output === "string" ? output : inner && typeof inner.output === "string" ? inner.output : JSON.stringify(output ?? "");
|
|
1086
|
+
const failed = inner && inner.success === false || record(inner?.metadata)?.exit_code !== void 0 && record(inner?.metadata)?.exit_code !== 0;
|
|
1087
|
+
return {
|
|
1088
|
+
events: [
|
|
1089
|
+
{
|
|
1090
|
+
type: "agent.tool_result",
|
|
1091
|
+
part: {
|
|
1092
|
+
type: "tool_result",
|
|
1093
|
+
index: 0,
|
|
1094
|
+
call_id: id,
|
|
1095
|
+
tool_name: "",
|
|
1096
|
+
status: failed ? "error" : "ok",
|
|
1097
|
+
result: { output: text }
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
],
|
|
1101
|
+
spec: {}
|
|
1102
|
+
};
|
|
1103
|
+
}
|
|
1104
|
+
if (kind === "turn_aborted") {
|
|
1105
|
+
return {
|
|
1106
|
+
events: [
|
|
1107
|
+
{
|
|
1108
|
+
type: "agent.status",
|
|
1109
|
+
part: { type: "status", index: 0, level: "idle", text: "Turn interrupted" }
|
|
1110
|
+
}
|
|
1111
|
+
],
|
|
1112
|
+
spec: {}
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
if (type === "response_item" && kind) {
|
|
1116
|
+
const text = JSON.stringify(line);
|
|
1117
|
+
const bytes = new TextEncoder().encode(text);
|
|
1118
|
+
let binary = "";
|
|
1119
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
1120
|
+
return {
|
|
1121
|
+
events: [{ type: "agent.message", parts: [
|
|
1122
|
+
{ type: "text", index: 0, text: `Source record: ${kind} (this reader does not render it)` },
|
|
1123
|
+
{ type: "file", index: 1, name: "source-record.json", media_type: "application/json", data: btoa(binary), size_bytes: bytes.length }
|
|
1124
|
+
] }],
|
|
1125
|
+
spec: {}
|
|
1126
|
+
};
|
|
1127
|
+
}
|
|
1128
|
+
return EMPTY;
|
|
1129
|
+
}
|
|
1130
|
+
|
|
1131
|
+
// ../../packages/spec/src/native/index.ts
|
|
1132
|
+
function parseNative(engine, records) {
|
|
1133
|
+
const parse = { events: [], notes: [], spec: {}, unattachedResults: 0 };
|
|
1134
|
+
const absorb = (spec) => {
|
|
1135
|
+
for (const [key, value] of Object.entries(spec ?? {})) {
|
|
1136
|
+
if (value !== void 0) parse.spec[key] = value;
|
|
1137
|
+
}
|
|
1138
|
+
};
|
|
1139
|
+
for (const record3 of records) {
|
|
1140
|
+
if (engine === "codex") {
|
|
1141
|
+
const decoded2 = decodeCodexLine(record3);
|
|
1142
|
+
parse.events.push(...decoded2.events);
|
|
1143
|
+
if (decoded2.unattachedResult) parse.unattachedResults += 1;
|
|
1144
|
+
absorb(decoded2.spec);
|
|
1145
|
+
} else {
|
|
1146
|
+
const decoded2 = decodeLine(record3);
|
|
1147
|
+
parse.events.push(...decoded2.events);
|
|
1148
|
+
parse.notes.push(...decoded2.notes);
|
|
1149
|
+
absorb(decoded2.spec);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
return parse;
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
// ../../packages/conversation/src/platform-text.ts
|
|
1156
|
+
var INPUT_SCOPE = /\n\n<agentsky-input-scope>[^\n]*<\/agentsky-input-scope>(?=$|\n\n<attachments>\n)/;
|
|
1157
|
+
var SYSTEM_SECTION = /^<system-reminder>\n([\s\S]*?)\n<\/system-reminder>(?:\n\n|$)/;
|
|
1158
|
+
var APP_CONTEXT = /\s*<app_context>[\s\S]*?(?:<\/app_context>|$)/g;
|
|
1159
|
+
var HAS_APP_CONTEXT = /<app_context>/;
|
|
1160
|
+
var HARNESS_TURN = /^\s*(?:<(task-notification|local-command-caveat|local-command-stdout|local-command-stderr|command-name|command-message|command-args)>[\s\S]*?<\/\1>\s*)+$/;
|
|
1161
|
+
var RECOVERY_NOTICE_CLOSING = "Preserve completed work; do not repeat completed actions.";
|
|
1162
|
+
var RECOVERY_NOTICE_OPENING = /^(?:The subscription usage limit that stopped the previous request has reset\.|.{0,240}?Continue the interrupted task from its current state\.)/s;
|
|
1163
|
+
var EARLIER = /<earlier_conversation>\n([\s\S]*?)\n?<\/earlier_conversation>/;
|
|
1164
|
+
function earlierTurns(system) {
|
|
1165
|
+
const block = EARLIER.exec(system)?.[1];
|
|
1166
|
+
if (!block) return [];
|
|
1167
|
+
const turns = [];
|
|
1168
|
+
for (const line of block.split("\n")) {
|
|
1169
|
+
try {
|
|
1170
|
+
const entry = JSON.parse(line);
|
|
1171
|
+
if (typeof entry.text !== "string" || !entry.text.trim()) continue;
|
|
1172
|
+
turns.push({ role: entry.from === "Agent" ? "agent" : "user", text: entry.text, at: typeof entry.at === "string" ? entry.at : "" });
|
|
1173
|
+
} catch {
|
|
1174
|
+
continue;
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
return turns;
|
|
1178
|
+
}
|
|
1179
|
+
function isRecoveryNotice(text) {
|
|
1180
|
+
const trimmed = text.trim();
|
|
1181
|
+
return trimmed.endsWith(RECOVERY_NOTICE_CLOSING) && RECOVERY_NOTICE_OPENING.test(trimmed);
|
|
1182
|
+
}
|
|
1183
|
+
function personText(raw) {
|
|
1184
|
+
let text = raw.replace(INPUT_SCOPE, "");
|
|
1185
|
+
const earlier = [];
|
|
1186
|
+
for (let section = SYSTEM_SECTION.exec(text); section; section = SYSTEM_SECTION.exec(text)) {
|
|
1187
|
+
earlier.push(...earlierTurns(section[1] ?? ""));
|
|
1188
|
+
text = text.slice(section[0].length);
|
|
1189
|
+
}
|
|
1190
|
+
text = text.replace(INPUT_SCOPE, "");
|
|
1191
|
+
if (HAS_APP_CONTEXT.test(text)) text = text.replace(APP_CONTEXT, "").trim();
|
|
1192
|
+
if (!text.trim() || HARNESS_TURN.test(text) || isRecoveryNotice(text)) text = "";
|
|
1193
|
+
return { text, earlier };
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
// ../../packages/conversation/src/read.ts
|
|
1197
|
+
var THINKING_BLOCKS = /* @__PURE__ */ new Set(["thinking", "redacted_thinking"]);
|
|
1198
|
+
var PRESERVED_RECORDS = /* @__PURE__ */ new Set(["source-record.json", "source-attachment.json"]);
|
|
1199
|
+
function record2(value) {
|
|
1200
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
1201
|
+
}
|
|
1202
|
+
function str3(value) {
|
|
1203
|
+
return typeof value === "string" ? value : "";
|
|
1204
|
+
}
|
|
1205
|
+
function jsonLines(text) {
|
|
1206
|
+
const lines2 = [];
|
|
1207
|
+
for (const line of text.split("\n")) {
|
|
1208
|
+
if (!line.trim()) continue;
|
|
1209
|
+
try {
|
|
1210
|
+
const parsed = record2(JSON.parse(line));
|
|
1211
|
+
if (parsed) lines2.push(parsed);
|
|
1212
|
+
} catch {
|
|
1213
|
+
continue;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
return lines2;
|
|
1217
|
+
}
|
|
1218
|
+
function reasoningOf(block, index) {
|
|
1219
|
+
return block.type === "thinking" ? { type: "reasoning", index, text: str3(block.thinking), redacted: false } : { type: "reasoning", index, text: "", redacted: true };
|
|
1220
|
+
}
|
|
1221
|
+
function withoutThinking(line) {
|
|
1222
|
+
const message = record2(line.message);
|
|
1223
|
+
if (line.type !== "assistant" || !message || !Array.isArray(message.content)) return { line, reasoning: [] };
|
|
1224
|
+
const reasoning = [];
|
|
1225
|
+
const content = message.content.filter((raw, index) => {
|
|
1226
|
+
const block = record2(raw);
|
|
1227
|
+
if (!block || !THINKING_BLOCKS.has(str3(block.type))) return true;
|
|
1228
|
+
reasoning.push(reasoningOf(block, index));
|
|
1229
|
+
return false;
|
|
1230
|
+
});
|
|
1231
|
+
return { line: { ...line, message: { ...message, content } }, reasoning };
|
|
1232
|
+
}
|
|
1233
|
+
var Fold = class {
|
|
1234
|
+
messages = [];
|
|
1235
|
+
calls = /* @__PURE__ */ new Map();
|
|
1236
|
+
engine;
|
|
1237
|
+
constructor(engine) {
|
|
1238
|
+
this.engine = engine;
|
|
1239
|
+
}
|
|
1240
|
+
open(role, at) {
|
|
1241
|
+
const message = { id: `m${this.messages.length}`, role, created_at: at, parts: [], metadata: { engine: this.engine } };
|
|
1242
|
+
this.messages.push(message);
|
|
1243
|
+
return message;
|
|
1244
|
+
}
|
|
1245
|
+
step(at) {
|
|
1246
|
+
const last = this.messages.at(-1);
|
|
1247
|
+
if (last && last.role === "agent" && !last.parts.some((part) => part.type === "tool_result")) return last;
|
|
1248
|
+
return this.open("agent", at);
|
|
1249
|
+
}
|
|
1250
|
+
add(message, part) {
|
|
1251
|
+
message.parts.push({ ...part, index: message.parts.length });
|
|
1252
|
+
}
|
|
1253
|
+
reasoning(parts, at) {
|
|
1254
|
+
if (!parts.length) return;
|
|
1255
|
+
const message = this.step(at);
|
|
1256
|
+
for (const part of parts) this.add(message, part);
|
|
1257
|
+
}
|
|
1258
|
+
event(event, at) {
|
|
1259
|
+
switch (event.type) {
|
|
1260
|
+
case "user.message": {
|
|
1261
|
+
const parts = [];
|
|
1262
|
+
for (const part of event.parts) {
|
|
1263
|
+
if (part.type === "file" && PRESERVED_RECORDS.has(part.name)) continue;
|
|
1264
|
+
if (part.type !== "text") {
|
|
1265
|
+
parts.push(part);
|
|
1266
|
+
continue;
|
|
1267
|
+
}
|
|
1268
|
+
const { text, earlier } = personText(part.text);
|
|
1269
|
+
if (!this.messages.length) for (const turn of earlier) this.add(this.open(turn.role, turn.at || at), { type: "text", index: 0, text: turn.text });
|
|
1270
|
+
if (text) parts.push({ ...part, text });
|
|
1271
|
+
}
|
|
1272
|
+
if (!parts.length) return;
|
|
1273
|
+
const message = this.open("user", at);
|
|
1274
|
+
for (const part of parts) this.add(message, part);
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
case "agent.message": {
|
|
1278
|
+
if (event.parts.some((part) => part.type === "file" && PRESERVED_RECORDS.has(part.name))) return;
|
|
1279
|
+
const message = this.step(at);
|
|
1280
|
+
for (const part of event.parts) this.add(message, part);
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
case "agent.reasoning":
|
|
1284
|
+
this.add(this.step(at), event.part);
|
|
1285
|
+
return;
|
|
1286
|
+
case "agent.tool_use": {
|
|
1287
|
+
const message = this.step(at);
|
|
1288
|
+
this.add(message, event.part);
|
|
1289
|
+
this.calls.set(event.part.call_id, { message, call: message.parts.at(-1) });
|
|
1290
|
+
return;
|
|
1291
|
+
}
|
|
1292
|
+
case "agent.tool_result": {
|
|
1293
|
+
const owner = this.calls.get(event.part.call_id);
|
|
1294
|
+
if (!owner) return;
|
|
1295
|
+
this.add(owner.message, { ...event.part, tool_name: owner.call.tool_name });
|
|
1296
|
+
return;
|
|
1297
|
+
}
|
|
1298
|
+
default:
|
|
1299
|
+
return;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
};
|
|
1303
|
+
function claudeCode(lines2) {
|
|
1304
|
+
const fold = new Fold("claude_code");
|
|
1305
|
+
let nativeId = null;
|
|
1306
|
+
let cwd = null;
|
|
1307
|
+
let model = null;
|
|
1308
|
+
for (const raw of lines2) {
|
|
1309
|
+
if (raw.isSidechain === true) continue;
|
|
1310
|
+
nativeId ??= str3(raw.sessionId) || null;
|
|
1311
|
+
cwd ??= str3(raw.cwd) || null;
|
|
1312
|
+
const at = str3(raw.timestamp);
|
|
1313
|
+
const { line, reasoning } = withoutThinking(raw);
|
|
1314
|
+
const parse = parseNative("claude_code", [line]);
|
|
1315
|
+
if (parse.spec.llm && parse.spec.llm !== "<synthetic>") model = parse.spec.llm;
|
|
1316
|
+
fold.reasoning(reasoning, at);
|
|
1317
|
+
for (const event of parse.events) fold.event(event, at);
|
|
1318
|
+
}
|
|
1319
|
+
return { engine: "claude_code", nativeId, cwd, model, messages: fold.messages };
|
|
1320
|
+
}
|
|
1321
|
+
function codex(lines2) {
|
|
1322
|
+
const fold = new Fold("codex");
|
|
1323
|
+
let nativeId = null;
|
|
1324
|
+
let cwd = null;
|
|
1325
|
+
let model = null;
|
|
1326
|
+
let rollout = null;
|
|
1327
|
+
for (const line of lines2) {
|
|
1328
|
+
const meta = line.type === "session_meta" ? record2(line.payload) : null;
|
|
1329
|
+
if (meta) {
|
|
1330
|
+
rollout ??= str3(meta.id) || null;
|
|
1331
|
+
if (str3(meta.id) !== rollout) continue;
|
|
1332
|
+
nativeId ??= rollout;
|
|
1333
|
+
cwd ??= str3(meta.cwd) || null;
|
|
1334
|
+
continue;
|
|
1335
|
+
}
|
|
1336
|
+
if (record2(line.payload)?.author && record2(line.payload)?.author !== "/root") continue;
|
|
1337
|
+
const at = str3(line.timestamp);
|
|
1338
|
+
const parse = parseNative("codex", [line]);
|
|
1339
|
+
if (parse.spec.llm) model = parse.spec.llm;
|
|
1340
|
+
const item = record2(line.payload);
|
|
1341
|
+
for (const event of parse.events) {
|
|
1342
|
+
if (event.type === "agent.tool_use" && item?.type === "custom_tool_call" && typeof item.input === "string") {
|
|
1343
|
+
fold.event({ ...event, part: { ...event.part, args: { input: item.input } } }, at);
|
|
1344
|
+
} else fold.event(event, at);
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
return { engine: "codex", nativeId, cwd, model, messages: fold.messages };
|
|
1348
|
+
}
|
|
1349
|
+
function readConversation(engine, text) {
|
|
1350
|
+
const lines2 = jsonLines(text);
|
|
1351
|
+
return engine === "codex" ? codex(lines2) : claudeCode(lines2);
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
// ../../packages/conversation/src/index.ts
|
|
1355
|
+
function convertSession(from, text, to, opts = {}) {
|
|
1356
|
+
return writeConversation(to, readConversation(from, text), opts);
|
|
1357
|
+
}
|
|
1358
|
+
|
|
1359
|
+
export {
|
|
1360
|
+
isEngine,
|
|
1361
|
+
writeConversation,
|
|
1362
|
+
convertSession
|
|
1363
|
+
};
|