@dadado/agent-kit-cli 6.23.0 → 6.25.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/README.md +1 -0
- package/dashboard/dashboard-data.mjs +126 -5
- package/dashboard/dashboard.html +45 -4
- package/dashboard/lib/live-refresh.mjs +1 -0
- package/dist/{add-RKZQX6ZK.js → add-OYLOMCM6.js} +14 -6
- package/dist/chunk-2F6NMQOA.js +519 -0
- package/dist/chunk-2QYWMSI5.js +193 -0
- package/dist/{chunk-B3VR5YBQ.js → chunk-2TW7ENWC.js} +12 -11
- package/dist/{chunk-2IP4OO6R.js → chunk-2XLI7XEY.js} +1 -1
- package/dist/chunk-6M6UKECW.js +176 -0
- package/dist/{chunk-UJCATSYO.js → chunk-7Y74KO7U.js} +45 -16
- package/dist/{chunk-DZCLMIAS.js → chunk-AQLBEEPD.js} +236 -2
- package/dist/{chunk-R4DUO5DA.js → chunk-AXQMVQ6O.js} +61 -237
- package/dist/{chunk-7TYA3GMI.js → chunk-C7SV7UP5.js} +7 -4
- package/dist/{chunk-F7YAAKYP.js → chunk-CIV42JGT.js} +7 -5
- package/dist/{chunk-CQHD6UKB.js → chunk-CXE6ASSX.js} +1 -0
- package/dist/{chunk-6BEFGKCU.js → chunk-EO5NGAYV.js} +1 -1
- package/dist/{chunk-MJJBIOSY.js → chunk-G5MQRP6M.js} +31 -13
- package/dist/chunk-GMG7ZH4E.js +357 -0
- package/dist/{chunk-5EBT3V7G.js → chunk-GQSSXDDK.js} +1 -1
- package/dist/{chunk-5LNHRL5E.js → chunk-H7RAFRRZ.js} +41 -245
- package/dist/{chunk-IESSMKOV.js → chunk-HSIHZWFF.js} +4 -4
- package/dist/{chunk-W3ZJVBNY.js → chunk-IFBIO7VT.js} +449 -418
- package/dist/{chunk-V2H57TFD.js → chunk-JG3CNGQM.js} +78 -170
- package/dist/{chunk-KDJ4ML7I.js → chunk-JGG574RK.js} +3 -3
- package/dist/{chunk-SGTTC2JP.js → chunk-KWYF7QNH.js} +1 -1
- package/dist/{chunk-CVZNSRXV.js → chunk-M4UL422I.js} +80 -15
- package/dist/{chunk-AWQ6KRFV.js → chunk-NJ2SUSWX.js} +1 -1
- package/dist/{chunk-DRZFWIBN.js → chunk-PIMGV3E5.js} +1 -1
- package/dist/{chunk-QNISTPPZ.js → chunk-QACMV46U.js} +3 -3
- package/dist/{chunk-5PSV76CQ.js → chunk-QLTGHJB3.js} +25 -6
- package/dist/{chunk-OYFCBYMP.js → chunk-QX55T4UX.js} +13 -1
- package/dist/chunk-SAQ3EE5R.js +138 -0
- package/dist/{chunk-C4LCOOL4.js → chunk-TOSAAQ5A.js} +1 -1
- package/dist/chunk-WKFE3RUC.js +433 -0
- package/dist/{chunk-BGL5DRK2.js → chunk-XGZOU4DS.js} +1 -1
- package/dist/{commands-SAQEEE6V.js → commands-UW2C2ZN4.js} +4 -4
- package/dist/{conflicts-5JOP4GX2.js → conflicts-RING6AHV.js} +6 -6
- package/dist/{contribute-BQKRT5GN.js → contribute-LSY75JZH.js} +5 -4
- package/dist/{countdown-25E5C7AR.js → countdown-SZ2P4XHB.js} +267 -23
- package/dist/{diff-6JSXBVSX.js → diff-NZVBVQSK.js} +13 -5
- package/dist/{doctor-I3MTHS7F.js → doctor-TJSXBVGV.js} +103 -17
- package/dist/{ground-control-IDCXN5FK.js → ground-control-ZJXPUIQN.js} +3 -3
- package/dist/{guard-RJZEEKSL.js → guard-ZO4Z3LLB.js} +47 -9
- package/dist/{handoff-5AGE2D3T.js → handoff-2OZSWGTE.js} +2 -0
- package/dist/{hook-WNA42CXE.js → hook-3VPKNSOO.js} +12 -10
- package/dist/index.js +27 -26
- package/dist/{init-FZ2UFPO7.js → init-UW2BJBO2.js} +21 -15
- package/dist/{install-CTLT3TSI.js → install-RQSI4CHG.js} +18 -15
- package/dist/installs-7TR4H4QH.js +222 -0
- package/dist/{mission-control-CRMCRZUU.js → mission-control-VSU6NT43.js} +1 -1
- package/dist/{queue-JW4VR33H.js → queue-JHYILLMX.js} +9 -9
- package/dist/{run-X2WOONTB.js → run-CQF5RM5X.js} +21 -21
- package/dist/{run-plan-4MOLLIES.js → run-plan-BEOCOJCF.js} +13 -13
- package/dist/{scan-K3T5RQLC.js → scan-7D3RMO34.js} +2 -1
- package/dist/{session-start-MV7A4263.js → session-start-QV7CEFRX.js} +16 -10
- package/dist/{sessions-UPFDWDMB.js → sessions-AWAAO3DU.js} +1 -1
- package/dist/{setup-global-VNADNTQ2.js → setup-global-ST4TT427.js} +8 -7
- package/dist/{status-SXA3OK5E.js → status-HUBPBGUU.js} +13 -10
- package/dist/update-DD5UZPGG.js +31 -0
- package/dist/update-all-YYUTXHGR.js +695 -0
- package/package.json +1 -1
- package/dist/chunk-4MR4QXMF.js +0 -52
- package/dist/chunk-HAFRAXGV.js +0 -17
- package/dist/update-RMAWXMCV.js +0 -304
|
@@ -27,348 +27,12 @@ import {
|
|
|
27
27
|
withKoloristColor
|
|
28
28
|
} from "./chunk-QTON2GQX.js";
|
|
29
29
|
|
|
30
|
-
// src/plan-loop/backends.ts
|
|
31
|
-
import { execFileSync as execFileSync2, spawn } from "child_process";
|
|
32
|
-
import { createWriteStream as createWriteStream2 } from "fs";
|
|
33
|
-
import { access as access2 } from "fs/promises";
|
|
34
|
-
import path2 from "path";
|
|
35
|
-
import { StringDecoder as StringDecoder3 } from "string_decoder";
|
|
36
|
-
|
|
37
|
-
// src/plan-loop/codex.ts
|
|
38
|
-
import { execFileSync } from "child_process";
|
|
39
|
-
import { access } from "fs/promises";
|
|
40
|
-
import path from "path";
|
|
41
|
-
|
|
42
|
-
// src/plan-loop/vendor-cli-pins.ts
|
|
43
|
-
var VENDOR_CLI_PINS = {
|
|
44
|
-
claude: {
|
|
45
|
-
bin: "claude",
|
|
46
|
-
min: "2.1.259",
|
|
47
|
-
tested: "2.1.292",
|
|
48
|
-
testedOn: "2026-10-08 `claude --version` (Linux)",
|
|
49
|
-
versionRe: /(\d+\.\d+\.\d+)/
|
|
50
|
-
},
|
|
51
|
-
"cursor-agent": {
|
|
52
|
-
bin: "cursor-agent",
|
|
53
|
-
min: null,
|
|
54
|
-
tested: "2026.10.01",
|
|
55
|
-
testedOn: "2026-10-08 `cursor-agent --version` = 2026.10.01-e373342 (Linux)",
|
|
56
|
-
versionRe: /(\d{4}\.\d{2}\.\d{2})(?:-[0-9a-f]+)?/
|
|
57
|
-
},
|
|
58
|
-
codex: {
|
|
59
|
-
bin: "codex",
|
|
60
|
-
min: null,
|
|
61
|
-
tested: "0.160.1",
|
|
62
|
-
testedOn: "2026-10-08 `codex --version` = codex-cli 0.160.1 (Linux)",
|
|
63
|
-
versionRe: /(\d+\.\d+\.\d+)/
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
function parseVendorVersion(id, raw) {
|
|
67
|
-
if (!raw) return null;
|
|
68
|
-
return VENDOR_CLI_PINS[id].versionRe.exec(raw)?.[1] ?? null;
|
|
69
|
-
}
|
|
70
|
-
function compareVendorVersions(a, b) {
|
|
71
|
-
const pa = a.split(/\D+/).filter(Boolean).map(Number);
|
|
72
|
-
const pb = b.split(/\D+/).filter(Boolean).map(Number);
|
|
73
|
-
for (let i = 0; i < Math.max(pa.length, pb.length); i += 1) {
|
|
74
|
-
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
75
|
-
if (d !== 0) return d;
|
|
76
|
-
}
|
|
77
|
-
return 0;
|
|
78
|
-
}
|
|
79
|
-
function vendorVersionStatus(id, raw) {
|
|
80
|
-
const pin = VENDOR_CLI_PINS[id];
|
|
81
|
-
const version = parseVendorVersion(id, raw);
|
|
82
|
-
if (!version) return { status: "unknown", version: null };
|
|
83
|
-
if (pin.min && compareVendorVersions(version, pin.min) < 0) {
|
|
84
|
-
return {
|
|
85
|
-
status: "too-old",
|
|
86
|
-
version,
|
|
87
|
-
message: `${pin.bin} ${version} is older than ${pin.min}, the oldest version the headless runner supports.`
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
if (compareVendorVersions(version, pin.tested) > 0) {
|
|
91
|
-
return {
|
|
92
|
-
status: "newer-than-tested",
|
|
93
|
-
version,
|
|
94
|
-
message: `${pin.bin} ${version} is newer than ${pin.tested}, the last version the kit's headless contract was checked against; continuing (a changed output format is named here first).`
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
return { status: "supported", version };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// src/plan-loop/codex.ts
|
|
101
|
-
var CODEX_CHATGPT_LOGIN_ENV = "AGENT_KIT_CODEX_CHATGPT_LOGIN";
|
|
102
|
-
var CODEX_MAX_RESUMES = 50;
|
|
103
|
-
function codexExecArgs(opts) {
|
|
104
|
-
const args = [
|
|
105
|
-
"exec",
|
|
106
|
-
"--json",
|
|
107
|
-
"--color",
|
|
108
|
-
"never",
|
|
109
|
-
"--cd",
|
|
110
|
-
opts.workspace,
|
|
111
|
-
"--dangerously-bypass-approvals-and-sandbox"
|
|
112
|
-
];
|
|
113
|
-
if (opts.model) args.push("--model", opts.model);
|
|
114
|
-
args.push("-");
|
|
115
|
-
return args;
|
|
116
|
-
}
|
|
117
|
-
function codexResumeArgs(opts) {
|
|
118
|
-
const args = ["exec", "resume", "--json", "--dangerously-bypass-approvals-and-sandbox"];
|
|
119
|
-
if (opts.model) args.push("--model", opts.model);
|
|
120
|
-
args.push(opts.threadId, "-");
|
|
121
|
-
return args;
|
|
122
|
-
}
|
|
123
|
-
function resolveCodexAuth(env) {
|
|
124
|
-
if (env[CODEX_CHATGPT_LOGIN_ENV] === "1") return { mode: "chatgpt-login", env };
|
|
125
|
-
const key = env.CODEX_API_KEY || env.OPENAI_API_KEY;
|
|
126
|
-
if (!key) {
|
|
127
|
-
return {
|
|
128
|
-
mode: "refused",
|
|
129
|
-
message: `codex backend: API-key mode needs OPENAI_API_KEY (or CODEX_API_KEY) in the environment. Using a ChatGPT sign-in is off by default until OpenAI clears it for third-party apps; set ${CODEX_CHATGPT_LOGIN_ENV}=1 to run on your own codex login.`
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
return { mode: "api-key", env: { ...env, CODEX_API_KEY: key } };
|
|
133
|
-
}
|
|
134
|
-
function codexSlashPointer(name) {
|
|
135
|
-
return [
|
|
136
|
-
`Read \`.cursor/commands/${name}.md\` now and follow that contract exactly \u2014 it is the source of truth for /${name}; this paragraph is only a thin adapter for the Codex CLI.`,
|
|
137
|
-
"",
|
|
138
|
-
"Adapter rules (Codex CLI, headless):",
|
|
139
|
-
"- Ask questions is unavailable here: print one line `HITL_GATE: <ask-id> | <label 1> | <label 2> | ...` immediately followed by the same labels as one numbered list, one list per message, then end your turn and WAIT for the answer (ask-id and labels: `.cursor/skills/core/hitl-gates/SKILL.md`). The answer arrives as your next prompt, `HITL_REPLY: <ask-id> | operator reply <n> | <label>`; cite that line as the Ask provenance.",
|
|
140
|
-
"- Skip or cancel means stop.",
|
|
141
|
-
"- Never `/git-prod` without an explicit operator yes.",
|
|
142
|
-
"- Do not clone Cursor hooks or invent behavior beyond the SoT file."
|
|
143
|
-
].join("\n");
|
|
144
|
-
}
|
|
145
|
-
async function codexPrompt(workspace, prompt) {
|
|
146
|
-
const m = /^\/([A-Za-z0-9][A-Za-z0-9_-]*)/.exec(prompt.trimStart());
|
|
147
|
-
if (!m?.[1]) return { ok: true, prompt };
|
|
148
|
-
const rel = path.join(".cursor", "commands", `${m[1]}.md`);
|
|
149
|
-
try {
|
|
150
|
-
await access(path.join(workspace, rel));
|
|
151
|
-
} catch {
|
|
152
|
-
return { ok: false, missing: rel };
|
|
153
|
-
}
|
|
154
|
-
return { ok: true, prompt: `${codexSlashPointer(m[1])}
|
|
155
|
-
|
|
156
|
-
${prompt}` };
|
|
157
|
-
}
|
|
158
|
-
var isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
159
|
-
var line = (event) => JSON.stringify(event);
|
|
160
|
-
function createCodexTranslator(onThread) {
|
|
161
|
-
let lastMessage = "";
|
|
162
|
-
let threadId = "";
|
|
163
|
-
return (raw) => {
|
|
164
|
-
const text = raw.trim();
|
|
165
|
-
if (!text.startsWith("{")) return [];
|
|
166
|
-
let event;
|
|
167
|
-
try {
|
|
168
|
-
event = JSON.parse(text);
|
|
169
|
-
} catch {
|
|
170
|
-
return [];
|
|
171
|
-
}
|
|
172
|
-
if (!isRecord(event)) return [];
|
|
173
|
-
const item = isRecord(event.item) ? event.item : null;
|
|
174
|
-
const itemType = item ? item.type ?? item.item_type : void 0;
|
|
175
|
-
const isMessage = itemType === "agent_message" || itemType === "assistant_message";
|
|
176
|
-
switch (event.type) {
|
|
177
|
-
case "thread.started":
|
|
178
|
-
if (typeof event.thread_id === "string") {
|
|
179
|
-
threadId = event.thread_id;
|
|
180
|
-
onThread(threadId);
|
|
181
|
-
}
|
|
182
|
-
return [line({ type: "system", subtype: "init", session_id: threadId, model: "codex" })];
|
|
183
|
-
case "item.started":
|
|
184
|
-
if (itemType === "command_execution" && typeof item?.command === "string") {
|
|
185
|
-
return [
|
|
186
|
-
line({
|
|
187
|
-
type: "assistant",
|
|
188
|
-
message: {
|
|
189
|
-
content: [{ type: "tool_use", name: "shell", input: { command: item.command } }]
|
|
190
|
-
}
|
|
191
|
-
})
|
|
192
|
-
];
|
|
193
|
-
}
|
|
194
|
-
return [];
|
|
195
|
-
case "item.completed":
|
|
196
|
-
if (isMessage && typeof item?.text === "string") {
|
|
197
|
-
lastMessage = item.text;
|
|
198
|
-
return [
|
|
199
|
-
line({ type: "assistant", message: { content: [{ type: "text", text: item.text }] } })
|
|
200
|
-
];
|
|
201
|
-
}
|
|
202
|
-
if (item && itemType === "command_execution") {
|
|
203
|
-
const exit = typeof item.exit_code === "number" ? item.exit_code : null;
|
|
204
|
-
return [
|
|
205
|
-
line({
|
|
206
|
-
type: "user",
|
|
207
|
-
message: {
|
|
208
|
-
content: [
|
|
209
|
-
{
|
|
210
|
-
type: "tool_result",
|
|
211
|
-
content: typeof item.aggregated_output === "string" ? item.aggregated_output : "",
|
|
212
|
-
is_error: exit !== null && exit !== 0
|
|
213
|
-
}
|
|
214
|
-
]
|
|
215
|
-
}
|
|
216
|
-
})
|
|
217
|
-
];
|
|
218
|
-
}
|
|
219
|
-
return [];
|
|
220
|
-
case "turn.completed": {
|
|
221
|
-
const result = lastMessage;
|
|
222
|
-
lastMessage = "";
|
|
223
|
-
return [
|
|
224
|
-
line({
|
|
225
|
-
type: "result",
|
|
226
|
-
subtype: "success",
|
|
227
|
-
is_error: false,
|
|
228
|
-
result,
|
|
229
|
-
session_id: threadId,
|
|
230
|
-
...isRecord(event.usage) ? { usage: event.usage } : {}
|
|
231
|
-
})
|
|
232
|
-
];
|
|
233
|
-
}
|
|
234
|
-
case "turn.failed": {
|
|
235
|
-
const message = isRecord(event.error) && typeof event.error.message === "string" ? event.error.message : "codex turn failed";
|
|
236
|
-
lastMessage = "";
|
|
237
|
-
return [
|
|
238
|
-
line({
|
|
239
|
-
type: "result",
|
|
240
|
-
subtype: "error_during_execution",
|
|
241
|
-
is_error: true,
|
|
242
|
-
result: message,
|
|
243
|
-
errors: [message],
|
|
244
|
-
session_id: threadId
|
|
245
|
-
})
|
|
246
|
-
];
|
|
247
|
-
}
|
|
248
|
-
default:
|
|
249
|
-
return [];
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
var codexVersionCache = /* @__PURE__ */ new Map();
|
|
254
|
-
function readCodexVersion(bin) {
|
|
255
|
-
if (!codexVersionCache.has(bin)) {
|
|
256
|
-
let out = null;
|
|
257
|
-
try {
|
|
258
|
-
out = execFileSync(bin, ["--version"], {
|
|
259
|
-
encoding: "utf8",
|
|
260
|
-
stdio: ["ignore", "pipe", "ignore"],
|
|
261
|
-
timeout: 1e4
|
|
262
|
-
});
|
|
263
|
-
} catch {
|
|
264
|
-
out = null;
|
|
265
|
-
}
|
|
266
|
-
codexVersionCache.set(bin, out);
|
|
267
|
-
}
|
|
268
|
-
return codexVersionCache.get(bin) ?? null;
|
|
269
|
-
}
|
|
270
|
-
function mergeRecords(records) {
|
|
271
|
-
const merged = emptyHitlRecord();
|
|
272
|
-
for (const r of records) {
|
|
273
|
-
merged.replies.push(...r.replies);
|
|
274
|
-
merged.fallbackDetections += r.fallbackDetections;
|
|
275
|
-
if (r.stop) merged.stop = r.stop;
|
|
276
|
-
}
|
|
277
|
-
return merged;
|
|
278
|
-
}
|
|
279
|
-
var codexBackend = {
|
|
280
|
-
id: "codex",
|
|
281
|
-
async resolve() {
|
|
282
|
-
return whichBinary("codex");
|
|
283
|
-
},
|
|
284
|
-
async run(opts) {
|
|
285
|
-
const log = opts.log ?? ((text) => console.log(text));
|
|
286
|
-
const auth = resolveCodexAuth(mergeChildEnv(opts.env));
|
|
287
|
-
if (auth.mode === "refused") throw new Error(auth.message);
|
|
288
|
-
const env = auth.env;
|
|
289
|
-
const redact = claudeRedactions(env);
|
|
290
|
-
const pin = vendorVersionStatus("codex", (opts.versionFn ?? readCodexVersion)("codex"));
|
|
291
|
-
if (pin.status === "newer-than-tested" || pin.status === "too-old") log(pin.message);
|
|
292
|
-
const prepared = await codexPrompt(opts.workspace, opts.prompt);
|
|
293
|
-
if (!prepared.ok) {
|
|
294
|
-
throw new Error(
|
|
295
|
-
`codex backend: ${prepared.missing} is missing in the workspace, so the tick's leading slash has no contract to point codex at. Install the kit (\`agent-kit install\`) and retry.`
|
|
296
|
-
);
|
|
297
|
-
}
|
|
298
|
-
log(
|
|
299
|
-
`codex tick: codex exec --json (no approvals, resume relay, ${auth.mode === "api-key" ? "API key" : "ChatGPT sign-in, opt-in"}).`
|
|
300
|
-
);
|
|
301
|
-
let threadId = null;
|
|
302
|
-
const translate = createCodexTranslator((id) => {
|
|
303
|
-
threadId = id;
|
|
304
|
-
});
|
|
305
|
-
const hitl = resolveHitlRunOptions(opts.hitl, { kind: "resume", backend: "codex" });
|
|
306
|
-
const common = {
|
|
307
|
-
cwd: opts.workspace,
|
|
308
|
-
env,
|
|
309
|
-
redact,
|
|
310
|
-
spawnFn: opts.spawnFn,
|
|
311
|
-
hitl,
|
|
312
|
-
render: opts.render,
|
|
313
|
-
onSpawn: opts.onSpawn,
|
|
314
|
-
backendId: "codex",
|
|
315
|
-
translate
|
|
316
|
-
};
|
|
317
|
-
const records = [];
|
|
318
|
-
const usage = new UsageTally();
|
|
319
|
-
let result;
|
|
320
|
-
try {
|
|
321
|
-
result = await spawnLogged(
|
|
322
|
-
"codex",
|
|
323
|
-
codexExecArgs({ workspace: opts.workspace, model: opts.model }),
|
|
324
|
-
opts.logPath,
|
|
325
|
-
{ ...common, stdinText: prepared.prompt }
|
|
326
|
-
);
|
|
327
|
-
if (result.hitl) records.push(result.hitl);
|
|
328
|
-
if (result.usage) usage.merge(result.usage);
|
|
329
|
-
for (let i = 0; result.resumeWith && i < CODEX_MAX_RESUMES; i += 1) {
|
|
330
|
-
const reply = result.resumeWith;
|
|
331
|
-
const thread = threadId;
|
|
332
|
-
if (!thread) {
|
|
333
|
-
const record = emptyHitlRecord();
|
|
334
|
-
record.stop = {
|
|
335
|
-
...unansweredStop("none", "child exited"),
|
|
336
|
-
message: "stopped: codex reported no thread id, so the reply could not be delivered"
|
|
337
|
-
};
|
|
338
|
-
records.push(record);
|
|
339
|
-
break;
|
|
340
|
-
}
|
|
341
|
-
result = await spawnLogged(
|
|
342
|
-
"codex",
|
|
343
|
-
codexResumeArgs({ threadId: thread, model: opts.model }),
|
|
344
|
-
opts.logPath,
|
|
345
|
-
{ ...common, stdinText: reply, appendLog: true }
|
|
346
|
-
);
|
|
347
|
-
if (result.hitl) records.push(result.hitl);
|
|
348
|
-
if (result.usage) usage.merge(result.usage);
|
|
349
|
-
}
|
|
350
|
-
} catch (err) {
|
|
351
|
-
throw new Error(`codex backend failed to start: ${redactSecrets(String(err), redact)}`);
|
|
352
|
-
}
|
|
353
|
-
return {
|
|
354
|
-
exitCode: result.exitCode,
|
|
355
|
-
hitl: mergeRecords(records),
|
|
356
|
-
usage,
|
|
357
|
-
...result.lastResult ? { lastResult: result.lastResult } : {}
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
// src/plan-loop/cursor-acp.ts
|
|
363
|
-
import { createWriteStream } from "fs";
|
|
364
|
-
import { StringDecoder as StringDecoder2 } from "string_decoder";
|
|
365
|
-
|
|
366
30
|
// src/plan-loop/stream-render.ts
|
|
367
31
|
import { StringDecoder } from "string_decoder";
|
|
368
32
|
import { trueColor } from "kolorist";
|
|
369
33
|
|
|
370
34
|
// src/plan-loop/stream-events.ts
|
|
371
|
-
function
|
|
35
|
+
function isRecord(value) {
|
|
372
36
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
373
37
|
}
|
|
374
38
|
function asString(value) {
|
|
@@ -393,7 +57,7 @@ var SUMMARY_KEYS = [
|
|
|
393
57
|
];
|
|
394
58
|
function summarizeToolInput(input) {
|
|
395
59
|
if (typeof input === "string") return oneLine(input);
|
|
396
|
-
if (!
|
|
60
|
+
if (!isRecord(input)) return input === void 0 ? "" : oneLine(JSON.stringify(input));
|
|
397
61
|
for (const key of SUMMARY_KEYS) {
|
|
398
62
|
const value = input[key];
|
|
399
63
|
if (typeof value === "string" && value.trim()) return oneLine(value);
|
|
@@ -406,14 +70,14 @@ function firstLineOfContent(content) {
|
|
|
406
70
|
if (typeof content === "string") return firstNonEmptyLine(content);
|
|
407
71
|
if (Array.isArray(content)) {
|
|
408
72
|
for (const part of content) {
|
|
409
|
-
if (
|
|
73
|
+
if (isRecord(part) && typeof part.text === "string") {
|
|
410
74
|
const line3 = firstNonEmptyLine(part.text);
|
|
411
75
|
if (line3) return line3;
|
|
412
76
|
}
|
|
413
77
|
}
|
|
414
78
|
return "";
|
|
415
79
|
}
|
|
416
|
-
if (
|
|
80
|
+
if (isRecord(content)) return oneLine(JSON.stringify(content));
|
|
417
81
|
return "";
|
|
418
82
|
}
|
|
419
83
|
function firstNonEmptyLine(text) {
|
|
@@ -425,12 +89,12 @@ function firstNonEmptyLine(text) {
|
|
|
425
89
|
}
|
|
426
90
|
function parseAssistant(event) {
|
|
427
91
|
const message = event.message;
|
|
428
|
-
const content =
|
|
92
|
+
const content = isRecord(message) ? message.content : void 0;
|
|
429
93
|
if (typeof content === "string") return content ? [{ kind: "text", text: content }] : [];
|
|
430
94
|
if (!Array.isArray(content)) return [{ kind: "silent" }];
|
|
431
95
|
const out = [];
|
|
432
96
|
for (const part of content) {
|
|
433
|
-
if (!
|
|
97
|
+
if (!isRecord(part)) continue;
|
|
434
98
|
const type = asString(part.type);
|
|
435
99
|
if (type === "text" && typeof part.text === "string") {
|
|
436
100
|
if (part.text) out.push({ kind: "text", text: part.text });
|
|
@@ -447,7 +111,7 @@ function parseAssistant(event) {
|
|
|
447
111
|
var HITL_REPLY_PREFIX = "HITL_REPLY:";
|
|
448
112
|
function parseUser(event) {
|
|
449
113
|
const message = event.message;
|
|
450
|
-
const content =
|
|
114
|
+
const content = isRecord(message) ? message.content : void 0;
|
|
451
115
|
if (typeof content === "string") {
|
|
452
116
|
if (event.isReplay === true && content.startsWith(HITL_REPLY_PREFIX)) {
|
|
453
117
|
return [{ kind: "hitl_reply", line: oneLine(content) }];
|
|
@@ -458,7 +122,7 @@ function parseUser(event) {
|
|
|
458
122
|
const out = [];
|
|
459
123
|
let promptChars = 0;
|
|
460
124
|
for (const part of content) {
|
|
461
|
-
if (!
|
|
125
|
+
if (!isRecord(part)) continue;
|
|
462
126
|
const type = asString(part.type);
|
|
463
127
|
if (type === "tool_result") {
|
|
464
128
|
out.push({
|
|
@@ -507,16 +171,16 @@ function percent(value) {
|
|
|
507
171
|
return n === void 0 ? null : `${Math.round(n * 100)}%`;
|
|
508
172
|
}
|
|
509
173
|
function parseRateLimit(event) {
|
|
510
|
-
const info =
|
|
174
|
+
const info = isRecord(event.rate_limit_info) ? event.rate_limit_info : event;
|
|
511
175
|
const status = asString(info.status) ?? "unknown";
|
|
512
176
|
const type = asString(info.rateLimitType);
|
|
513
177
|
const parts = [];
|
|
514
178
|
if (type) parts.push(type);
|
|
515
179
|
parts.push(status);
|
|
516
|
-
const windows =
|
|
180
|
+
const windows = isRecord(info.unifiedWindows) ? info.unifiedWindows : null;
|
|
517
181
|
if (windows) {
|
|
518
182
|
for (const [name, window] of Object.entries(windows)) {
|
|
519
|
-
if (!
|
|
183
|
+
if (!isRecord(window)) continue;
|
|
520
184
|
const used = percent(window.utilization);
|
|
521
185
|
if (used) parts.push(`${name} ${used}`);
|
|
522
186
|
}
|
|
@@ -524,16 +188,16 @@ function parseRateLimit(event) {
|
|
|
524
188
|
return { kind: "rate_limit", line: parts.join(" \xB7 "), severe: status !== "allowed" };
|
|
525
189
|
}
|
|
526
190
|
function parseCursorToolCall(event) {
|
|
527
|
-
const call =
|
|
528
|
-
const entry = call ? Object.entries(call).find(([, v]) =>
|
|
191
|
+
const call = isRecord(event.tool_call) ? event.tool_call : null;
|
|
192
|
+
const entry = call ? Object.entries(call).find(([, v]) => isRecord(v)) : void 0;
|
|
529
193
|
const name = entry ? entry[0].replace(/ToolCall$/, "") : "tool";
|
|
530
|
-
const body = entry &&
|
|
194
|
+
const body = entry && isRecord(entry[1]) ? entry[1] : {};
|
|
531
195
|
const subtype = asString(event.subtype) ?? "";
|
|
532
196
|
if (subtype === "started") {
|
|
533
197
|
return { kind: "tool_use", name, summary: summarizeToolInput(body.args) };
|
|
534
198
|
}
|
|
535
199
|
if (subtype === "completed") {
|
|
536
|
-
const result =
|
|
200
|
+
const result = isRecord(body.result) ? body.result : null;
|
|
537
201
|
const failed = result !== null && ("error" in result || "failure" in result || "rejected" in result);
|
|
538
202
|
let line3 = "";
|
|
539
203
|
if (result) {
|
|
@@ -555,7 +219,7 @@ function parseStreamLine(line3) {
|
|
|
555
219
|
} catch {
|
|
556
220
|
return [{ kind: "unparsed" }];
|
|
557
221
|
}
|
|
558
|
-
if (!
|
|
222
|
+
if (!isRecord(event)) return [{ kind: "unparsed" }];
|
|
559
223
|
switch (asString(event.type)) {
|
|
560
224
|
case "assistant":
|
|
561
225
|
return parseAssistant(event);
|
|
@@ -602,6 +266,33 @@ function formatDuration(ms) {
|
|
|
602
266
|
function formatCost(usd) {
|
|
603
267
|
return `$${usd.toFixed(usd < 0.1 ? 4 : 2)}`;
|
|
604
268
|
}
|
|
269
|
+
var RESULT_LINE_HEAD = `${SPACE_MARKS.star} result `;
|
|
270
|
+
var RESULT_COST_SESSION_TOTAL = "session total";
|
|
271
|
+
function formatResultLine(event, opts = {}) {
|
|
272
|
+
const parts = [`${RESULT_LINE_HEAD}${event.status}`];
|
|
273
|
+
if (event.costUsd !== void 0) {
|
|
274
|
+
const cost = formatCost(event.costUsd);
|
|
275
|
+
parts.push(opts.sessionTotal ? `${cost} ${RESULT_COST_SESSION_TOTAL}` : cost);
|
|
276
|
+
}
|
|
277
|
+
if (event.durationMs !== void 0) parts.push(formatDuration(event.durationMs));
|
|
278
|
+
else if (event.apiMs !== void 0) parts.push(`${formatDuration(event.apiMs)} api`);
|
|
279
|
+
if (event.turns !== void 0) {
|
|
280
|
+
parts.push(`${event.turns} turn${event.turns === 1 ? "" : "s"}`);
|
|
281
|
+
}
|
|
282
|
+
return parts.join(` ${SPACE_MARKS.tick} `);
|
|
283
|
+
}
|
|
284
|
+
var ESC = String.fromCharCode(27);
|
|
285
|
+
var ANSI_RE = new RegExp(`${ESC}\\[[0-9;]*m`, "g");
|
|
286
|
+
var RESULT_LINE_RE = new RegExp(
|
|
287
|
+
`^${RESULT_LINE_HEAD}(\\S+)(?: ${SPACE_MARKS.tick} \\$(\\d+(?:\\.\\d+)?)( ${RESULT_COST_SESSION_TOTAL})?)?(?: ${SPACE_MARKS.tick} .*)?$`
|
|
288
|
+
);
|
|
289
|
+
function parseResultLine(line3) {
|
|
290
|
+
const m = RESULT_LINE_RE.exec(line3.replace(ANSI_RE, "").replace(/\r$/, ""));
|
|
291
|
+
if (!m) return null;
|
|
292
|
+
const cost = m[2] === void 0 ? null : Number(m[2]);
|
|
293
|
+
const costUsd = cost !== null && Number.isFinite(cost) ? cost : null;
|
|
294
|
+
return { status: m[1] ?? "", costUsd, sessionTotal: costUsd !== null && m[3] !== void 0 };
|
|
295
|
+
}
|
|
605
296
|
function renderStreamEvent(event, opts) {
|
|
606
297
|
const width = Math.max(MIN_COLUMNS, opts.columns);
|
|
607
298
|
const color = opts.color;
|
|
@@ -620,16 +311,12 @@ function renderStreamEvent(event, opts) {
|
|
|
620
311
|
const text = line3 ? `${head} ${SPACE_MARKS.tick} ${line3}` : head;
|
|
621
312
|
return paint(event.ok ? LABEL_MUTED : STATUS_ERR, text, color);
|
|
622
313
|
}
|
|
623
|
-
case "result":
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
parts.push(`${event.turns} turn${event.turns === 1 ? "" : "s"}`);
|
|
630
|
-
}
|
|
631
|
-
return paint(event.ok ? STATUS_OK : STATUS_ERR, parts.join(` ${SPACE_MARKS.tick} `), color);
|
|
632
|
-
}
|
|
314
|
+
case "result":
|
|
315
|
+
return paint(
|
|
316
|
+
event.ok ? STATUS_OK : STATUS_ERR,
|
|
317
|
+
formatResultLine(event, { sessionTotal: opts.sessionTotal === true }),
|
|
318
|
+
color
|
|
319
|
+
);
|
|
633
320
|
case "hook": {
|
|
634
321
|
const detail = event.detail ? ` (${event.detail})` : "";
|
|
635
322
|
return paint(
|
|
@@ -656,78 +343,420 @@ function renderStreamEvent(event, opts) {
|
|
|
656
343
|
return null;
|
|
657
344
|
}
|
|
658
345
|
}
|
|
659
|
-
var StreamRenderer = class {
|
|
660
|
-
pending = "";
|
|
661
|
-
dropped = false;
|
|
662
|
-
textOpen = false;
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
346
|
+
var StreamRenderer = class {
|
|
347
|
+
pending = "";
|
|
348
|
+
dropped = false;
|
|
349
|
+
textOpen = false;
|
|
350
|
+
/** A costed result was already rendered: later costs are the session's running total. */
|
|
351
|
+
costSeen = false;
|
|
352
|
+
decoder = new StringDecoder("utf8");
|
|
353
|
+
write;
|
|
354
|
+
color;
|
|
355
|
+
columns;
|
|
356
|
+
constructor(opts = {}) {
|
|
357
|
+
this.write = opts.write ?? ((text) => process.stdout.write(text));
|
|
358
|
+
this.color = shouldUseWelcomeColor({
|
|
359
|
+
stdoutIsTTY: opts.isTTY ?? Boolean(process.stdout.isTTY),
|
|
360
|
+
env: opts.env
|
|
361
|
+
});
|
|
362
|
+
this.columns = opts.columns ?? process.stdout.columns ?? 80;
|
|
363
|
+
}
|
|
364
|
+
feed(chunk) {
|
|
365
|
+
try {
|
|
366
|
+
const text = typeof chunk === "string" ? chunk : this.decoder.write(chunk);
|
|
367
|
+
if (!text) return;
|
|
368
|
+
const combined = this.pending + text;
|
|
369
|
+
const lines = combined.split("\n");
|
|
370
|
+
this.pending = lines.pop() ?? "";
|
|
371
|
+
for (const line3 of lines) {
|
|
372
|
+
if (this.dropped) {
|
|
373
|
+
this.dropped = false;
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
this.renderLine(line3);
|
|
377
|
+
}
|
|
378
|
+
if (this.pending.length > PENDING_MAX_CHARS) {
|
|
379
|
+
this.pending = "";
|
|
380
|
+
this.dropped = true;
|
|
381
|
+
}
|
|
382
|
+
} catch {
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
end() {
|
|
386
|
+
try {
|
|
387
|
+
const rest = this.pending + this.decoder.end();
|
|
388
|
+
this.pending = "";
|
|
389
|
+
if (rest && !this.dropped) this.renderLine(rest);
|
|
390
|
+
this.dropped = false;
|
|
391
|
+
this.closeText();
|
|
392
|
+
} catch {
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
renderLine(line3) {
|
|
396
|
+
for (const event of parseStreamLine(line3)) {
|
|
397
|
+
const costed = event.kind === "result" && event.costUsd !== void 0;
|
|
398
|
+
const rendered = renderStreamEvent(event, {
|
|
399
|
+
color: this.color,
|
|
400
|
+
columns: this.columns,
|
|
401
|
+
sessionTotal: costed && this.costSeen
|
|
402
|
+
});
|
|
403
|
+
if (costed) this.costSeen = true;
|
|
404
|
+
if (rendered === null) continue;
|
|
405
|
+
if (event.kind === "text") {
|
|
406
|
+
this.write(rendered);
|
|
407
|
+
this.textOpen = !rendered.endsWith("\n");
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
this.closeText();
|
|
411
|
+
this.write(`${rendered}
|
|
412
|
+
`);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
closeText() {
|
|
416
|
+
if (!this.textOpen) return;
|
|
417
|
+
this.textOpen = false;
|
|
418
|
+
this.write("\n");
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
function createStreamRenderer(opts = {}) {
|
|
422
|
+
return new StreamRenderer(opts);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// src/plan-loop/backends.ts
|
|
426
|
+
import { execFileSync as execFileSync2, spawn } from "child_process";
|
|
427
|
+
import { createWriteStream as createWriteStream2 } from "fs";
|
|
428
|
+
import { access as access2 } from "fs/promises";
|
|
429
|
+
import path2 from "path";
|
|
430
|
+
import { StringDecoder as StringDecoder3 } from "string_decoder";
|
|
431
|
+
|
|
432
|
+
// src/plan-loop/codex.ts
|
|
433
|
+
import { execFileSync } from "child_process";
|
|
434
|
+
import { access } from "fs/promises";
|
|
435
|
+
import path from "path";
|
|
436
|
+
|
|
437
|
+
// src/plan-loop/vendor-cli-pins.ts
|
|
438
|
+
var VENDOR_CLI_PINS = {
|
|
439
|
+
claude: {
|
|
440
|
+
bin: "claude",
|
|
441
|
+
min: "2.1.259",
|
|
442
|
+
tested: "2.1.292",
|
|
443
|
+
testedOn: "2026-10-08 `claude --version` (Linux)",
|
|
444
|
+
versionRe: /(\d+\.\d+\.\d+)/
|
|
445
|
+
},
|
|
446
|
+
"cursor-agent": {
|
|
447
|
+
bin: "cursor-agent",
|
|
448
|
+
min: null,
|
|
449
|
+
tested: "2026.10.01",
|
|
450
|
+
testedOn: "2026-10-08 `cursor-agent --version` = 2026.10.01-e373342 (Linux)",
|
|
451
|
+
versionRe: /(\d{4}\.\d{2}\.\d{2})(?:-[0-9a-f]+)?/
|
|
452
|
+
},
|
|
453
|
+
codex: {
|
|
454
|
+
bin: "codex",
|
|
455
|
+
min: null,
|
|
456
|
+
tested: "0.160.1",
|
|
457
|
+
testedOn: "2026-10-08 `codex --version` = codex-cli 0.160.1 (Linux)",
|
|
458
|
+
versionRe: /(\d+\.\d+\.\d+)/
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
function parseVendorVersion(id, raw) {
|
|
462
|
+
if (!raw) return null;
|
|
463
|
+
return VENDOR_CLI_PINS[id].versionRe.exec(raw)?.[1] ?? null;
|
|
464
|
+
}
|
|
465
|
+
function compareVendorVersions(a, b) {
|
|
466
|
+
const pa = a.split(/\D+/).filter(Boolean).map(Number);
|
|
467
|
+
const pb = b.split(/\D+/).filter(Boolean).map(Number);
|
|
468
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i += 1) {
|
|
469
|
+
const d = (pa[i] ?? 0) - (pb[i] ?? 0);
|
|
470
|
+
if (d !== 0) return d;
|
|
471
|
+
}
|
|
472
|
+
return 0;
|
|
473
|
+
}
|
|
474
|
+
function vendorVersionStatus(id, raw) {
|
|
475
|
+
const pin = VENDOR_CLI_PINS[id];
|
|
476
|
+
const version = parseVendorVersion(id, raw);
|
|
477
|
+
if (!version) return { status: "unknown", version: null };
|
|
478
|
+
if (pin.min && compareVendorVersions(version, pin.min) < 0) {
|
|
479
|
+
return {
|
|
480
|
+
status: "too-old",
|
|
481
|
+
version,
|
|
482
|
+
message: `${pin.bin} ${version} is older than ${pin.min}, the oldest version the headless runner supports.`
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
if (compareVendorVersions(version, pin.tested) > 0) {
|
|
486
|
+
return {
|
|
487
|
+
status: "newer-than-tested",
|
|
488
|
+
version,
|
|
489
|
+
message: `${pin.bin} ${version} is newer than ${pin.tested}, the last version the kit's headless contract was checked against; continuing (a changed output format is named here first).`
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
return { status: "supported", version };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// src/plan-loop/codex.ts
|
|
496
|
+
var CODEX_CHATGPT_LOGIN_ENV = "AGENT_KIT_CODEX_CHATGPT_LOGIN";
|
|
497
|
+
var CODEX_MAX_RESUMES = 50;
|
|
498
|
+
function codexExecArgs(opts) {
|
|
499
|
+
const args = [
|
|
500
|
+
"exec",
|
|
501
|
+
"--json",
|
|
502
|
+
"--color",
|
|
503
|
+
"never",
|
|
504
|
+
"--cd",
|
|
505
|
+
opts.workspace,
|
|
506
|
+
"--dangerously-bypass-approvals-and-sandbox"
|
|
507
|
+
];
|
|
508
|
+
if (opts.model) args.push("--model", opts.model);
|
|
509
|
+
args.push("-");
|
|
510
|
+
return args;
|
|
511
|
+
}
|
|
512
|
+
function codexResumeArgs(opts) {
|
|
513
|
+
const args = ["exec", "resume", "--json", "--dangerously-bypass-approvals-and-sandbox"];
|
|
514
|
+
if (opts.model) args.push("--model", opts.model);
|
|
515
|
+
args.push(opts.threadId, "-");
|
|
516
|
+
return args;
|
|
517
|
+
}
|
|
518
|
+
function resolveCodexAuth(env) {
|
|
519
|
+
if (env[CODEX_CHATGPT_LOGIN_ENV] === "1") return { mode: "chatgpt-login", env };
|
|
520
|
+
const key = env.CODEX_API_KEY || env.OPENAI_API_KEY;
|
|
521
|
+
if (!key) {
|
|
522
|
+
return {
|
|
523
|
+
mode: "refused",
|
|
524
|
+
message: `codex backend: API-key mode needs OPENAI_API_KEY (or CODEX_API_KEY) in the environment. Using a ChatGPT sign-in is off by default until OpenAI clears it for third-party apps; set ${CODEX_CHATGPT_LOGIN_ENV}=1 to run on your own codex login.`
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
return { mode: "api-key", env: { ...env, CODEX_API_KEY: key } };
|
|
528
|
+
}
|
|
529
|
+
function codexSlashPointer(name) {
|
|
530
|
+
return [
|
|
531
|
+
`Read \`.cursor/commands/${name}.md\` now and follow that contract exactly \u2014 it is the source of truth for /${name}; this paragraph is only a thin adapter for the Codex CLI.`,
|
|
532
|
+
"",
|
|
533
|
+
"Adapter rules (Codex CLI, headless):",
|
|
534
|
+
"- Ask questions is unavailable here: print one line `HITL_GATE: <ask-id> | <label 1> | <label 2> | ...` immediately followed by the same labels as one numbered list, one list per message, then end your turn and WAIT for the answer (ask-id and labels: `.cursor/skills/core/hitl-gates/SKILL.md`). The answer arrives as your next prompt, `HITL_REPLY: <ask-id> | operator reply <n> | <label>`; cite that line as the Ask provenance.",
|
|
535
|
+
"- Skip or cancel means stop.",
|
|
536
|
+
"- Never `/git-prod` without an explicit operator yes.",
|
|
537
|
+
"- Do not clone Cursor hooks or invent behavior beyond the SoT file."
|
|
538
|
+
].join("\n");
|
|
539
|
+
}
|
|
540
|
+
async function codexPrompt(workspace, prompt) {
|
|
541
|
+
const m = /^\/([A-Za-z0-9][A-Za-z0-9_-]*)/.exec(prompt.trimStart());
|
|
542
|
+
if (!m?.[1]) return { ok: true, prompt };
|
|
543
|
+
const rel = path.join(".cursor", "commands", `${m[1]}.md`);
|
|
544
|
+
try {
|
|
545
|
+
await access(path.join(workspace, rel));
|
|
546
|
+
} catch {
|
|
547
|
+
return { ok: false, missing: rel };
|
|
674
548
|
}
|
|
675
|
-
|
|
549
|
+
return { ok: true, prompt: `${codexSlashPointer(m[1])}
|
|
550
|
+
|
|
551
|
+
${prompt}` };
|
|
552
|
+
}
|
|
553
|
+
var isRecord2 = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
554
|
+
var line = (event) => JSON.stringify(event);
|
|
555
|
+
function createCodexTranslator(onThread) {
|
|
556
|
+
let lastMessage = "";
|
|
557
|
+
let threadId = "";
|
|
558
|
+
return (raw) => {
|
|
559
|
+
const text = raw.trim();
|
|
560
|
+
if (!text.startsWith("{")) return [];
|
|
561
|
+
let event;
|
|
676
562
|
try {
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
563
|
+
event = JSON.parse(text);
|
|
564
|
+
} catch {
|
|
565
|
+
return [];
|
|
566
|
+
}
|
|
567
|
+
if (!isRecord2(event)) return [];
|
|
568
|
+
const item = isRecord2(event.item) ? event.item : null;
|
|
569
|
+
const itemType = item ? item.type ?? item.item_type : void 0;
|
|
570
|
+
const isMessage = itemType === "agent_message" || itemType === "assistant_message";
|
|
571
|
+
switch (event.type) {
|
|
572
|
+
case "thread.started":
|
|
573
|
+
if (typeof event.thread_id === "string") {
|
|
574
|
+
threadId = event.thread_id;
|
|
575
|
+
onThread(threadId);
|
|
686
576
|
}
|
|
687
|
-
|
|
577
|
+
return [line({ type: "system", subtype: "init", session_id: threadId, model: "codex" })];
|
|
578
|
+
case "item.started":
|
|
579
|
+
if (itemType === "command_execution" && typeof item?.command === "string") {
|
|
580
|
+
return [
|
|
581
|
+
line({
|
|
582
|
+
type: "assistant",
|
|
583
|
+
message: {
|
|
584
|
+
content: [{ type: "tool_use", name: "shell", input: { command: item.command } }]
|
|
585
|
+
}
|
|
586
|
+
})
|
|
587
|
+
];
|
|
588
|
+
}
|
|
589
|
+
return [];
|
|
590
|
+
case "item.completed":
|
|
591
|
+
if (isMessage && typeof item?.text === "string") {
|
|
592
|
+
lastMessage = item.text;
|
|
593
|
+
return [
|
|
594
|
+
line({ type: "assistant", message: { content: [{ type: "text", text: item.text }] } })
|
|
595
|
+
];
|
|
596
|
+
}
|
|
597
|
+
if (item && itemType === "command_execution") {
|
|
598
|
+
const exit = typeof item.exit_code === "number" ? item.exit_code : null;
|
|
599
|
+
return [
|
|
600
|
+
line({
|
|
601
|
+
type: "user",
|
|
602
|
+
message: {
|
|
603
|
+
content: [
|
|
604
|
+
{
|
|
605
|
+
type: "tool_result",
|
|
606
|
+
content: typeof item.aggregated_output === "string" ? item.aggregated_output : "",
|
|
607
|
+
is_error: exit !== null && exit !== 0
|
|
608
|
+
}
|
|
609
|
+
]
|
|
610
|
+
}
|
|
611
|
+
})
|
|
612
|
+
];
|
|
613
|
+
}
|
|
614
|
+
return [];
|
|
615
|
+
case "turn.completed": {
|
|
616
|
+
const result = lastMessage;
|
|
617
|
+
lastMessage = "";
|
|
618
|
+
return [
|
|
619
|
+
line({
|
|
620
|
+
type: "result",
|
|
621
|
+
subtype: "success",
|
|
622
|
+
is_error: false,
|
|
623
|
+
result,
|
|
624
|
+
session_id: threadId,
|
|
625
|
+
...isRecord2(event.usage) ? { usage: event.usage } : {}
|
|
626
|
+
})
|
|
627
|
+
];
|
|
688
628
|
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
629
|
+
case "turn.failed": {
|
|
630
|
+
const message = isRecord2(event.error) && typeof event.error.message === "string" ? event.error.message : "codex turn failed";
|
|
631
|
+
lastMessage = "";
|
|
632
|
+
return [
|
|
633
|
+
line({
|
|
634
|
+
type: "result",
|
|
635
|
+
subtype: "error_during_execution",
|
|
636
|
+
is_error: true,
|
|
637
|
+
result: message,
|
|
638
|
+
errors: [message],
|
|
639
|
+
session_id: threadId
|
|
640
|
+
})
|
|
641
|
+
];
|
|
692
642
|
}
|
|
693
|
-
|
|
643
|
+
default:
|
|
644
|
+
return [];
|
|
694
645
|
}
|
|
695
|
-
}
|
|
696
|
-
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
var codexVersionCache = /* @__PURE__ */ new Map();
|
|
649
|
+
function readCodexVersion(bin) {
|
|
650
|
+
if (!codexVersionCache.has(bin)) {
|
|
651
|
+
let out = null;
|
|
697
652
|
try {
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
653
|
+
out = execFileSync(bin, ["--version"], {
|
|
654
|
+
encoding: "utf8",
|
|
655
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
656
|
+
timeout: 1e4
|
|
657
|
+
});
|
|
703
658
|
} catch {
|
|
659
|
+
out = null;
|
|
704
660
|
}
|
|
661
|
+
codexVersionCache.set(bin, out);
|
|
705
662
|
}
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
663
|
+
return codexVersionCache.get(bin) ?? null;
|
|
664
|
+
}
|
|
665
|
+
function mergeRecords(records) {
|
|
666
|
+
const merged = emptyHitlRecord();
|
|
667
|
+
for (const r of records) {
|
|
668
|
+
merged.replies.push(...r.replies);
|
|
669
|
+
merged.fallbackDetections += r.fallbackDetections;
|
|
670
|
+
if (r.stop) merged.stop = r.stop;
|
|
671
|
+
}
|
|
672
|
+
return merged;
|
|
673
|
+
}
|
|
674
|
+
var codexBackend = {
|
|
675
|
+
id: "codex",
|
|
676
|
+
async resolve() {
|
|
677
|
+
return whichBinary("codex");
|
|
678
|
+
},
|
|
679
|
+
async run(opts) {
|
|
680
|
+
const log = opts.log ?? ((text) => console.log(text));
|
|
681
|
+
const auth = resolveCodexAuth(mergeChildEnv(opts.env));
|
|
682
|
+
if (auth.mode === "refused") throw new Error(auth.message);
|
|
683
|
+
const env = auth.env;
|
|
684
|
+
const redact = claudeRedactions(env);
|
|
685
|
+
const pin = vendorVersionStatus("codex", (opts.versionFn ?? readCodexVersion)("codex"));
|
|
686
|
+
if (pin.status === "newer-than-tested" || pin.status === "too-old") log(pin.message);
|
|
687
|
+
const prepared = await codexPrompt(opts.workspace, opts.prompt);
|
|
688
|
+
if (!prepared.ok) {
|
|
689
|
+
throw new Error(
|
|
690
|
+
`codex backend: ${prepared.missing} is missing in the workspace, so the tick's leading slash has no contract to point codex at. Install the kit (\`agent-kit install\`) and retry.`
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
log(
|
|
694
|
+
`codex tick: codex exec --json (no approvals, resume relay, ${auth.mode === "api-key" ? "API key" : "ChatGPT sign-in, opt-in"}).`
|
|
695
|
+
);
|
|
696
|
+
let threadId = null;
|
|
697
|
+
const translate = createCodexTranslator((id) => {
|
|
698
|
+
threadId = id;
|
|
699
|
+
});
|
|
700
|
+
const hitl = resolveHitlRunOptions(opts.hitl, { kind: "resume", backend: "codex" });
|
|
701
|
+
const common = {
|
|
702
|
+
cwd: opts.workspace,
|
|
703
|
+
env,
|
|
704
|
+
redact,
|
|
705
|
+
spawnFn: opts.spawnFn,
|
|
706
|
+
hitl,
|
|
707
|
+
render: opts.render,
|
|
708
|
+
onSpawn: opts.onSpawn,
|
|
709
|
+
backendId: "codex",
|
|
710
|
+
translate
|
|
711
|
+
};
|
|
712
|
+
const records = [];
|
|
713
|
+
const usage = new UsageTally();
|
|
714
|
+
let result;
|
|
715
|
+
try {
|
|
716
|
+
result = await spawnLogged(
|
|
717
|
+
"codex",
|
|
718
|
+
codexExecArgs({ workspace: opts.workspace, model: opts.model }),
|
|
719
|
+
opts.logPath,
|
|
720
|
+
{ ...common, stdinText: prepared.prompt }
|
|
721
|
+
);
|
|
722
|
+
if (result.hitl) records.push(result.hitl);
|
|
723
|
+
if (result.usage) usage.merge(result.usage);
|
|
724
|
+
for (let i = 0; result.resumeWith && i < CODEX_MAX_RESUMES; i += 1) {
|
|
725
|
+
const reply = result.resumeWith;
|
|
726
|
+
const thread = threadId;
|
|
727
|
+
if (!thread) {
|
|
728
|
+
const record = emptyHitlRecord();
|
|
729
|
+
record.stop = {
|
|
730
|
+
...unansweredStop("none", "child exited"),
|
|
731
|
+
message: "stopped: codex reported no thread id, so the reply could not be delivered"
|
|
732
|
+
};
|
|
733
|
+
records.push(record);
|
|
734
|
+
break;
|
|
735
|
+
}
|
|
736
|
+
result = await spawnLogged(
|
|
737
|
+
"codex",
|
|
738
|
+
codexResumeArgs({ threadId: thread, model: opts.model }),
|
|
739
|
+
opts.logPath,
|
|
740
|
+
{ ...common, stdinText: reply, appendLog: true }
|
|
741
|
+
);
|
|
742
|
+
if (result.hitl) records.push(result.hitl);
|
|
743
|
+
if (result.usage) usage.merge(result.usage);
|
|
714
744
|
}
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
`);
|
|
745
|
+
} catch (err) {
|
|
746
|
+
throw new Error(`codex backend failed to start: ${redactSecrets(String(err), redact)}`);
|
|
718
747
|
}
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
748
|
+
return {
|
|
749
|
+
exitCode: result.exitCode,
|
|
750
|
+
hitl: mergeRecords(records),
|
|
751
|
+
usage,
|
|
752
|
+
...result.lastResult ? { lastResult: result.lastResult } : {}
|
|
753
|
+
};
|
|
724
754
|
}
|
|
725
755
|
};
|
|
726
|
-
function createStreamRenderer(opts = {}) {
|
|
727
|
-
return new StreamRenderer(opts);
|
|
728
|
-
}
|
|
729
756
|
|
|
730
757
|
// src/plan-loop/cursor-acp.ts
|
|
758
|
+
import { createWriteStream } from "fs";
|
|
759
|
+
import { StringDecoder as StringDecoder2 } from "string_decoder";
|
|
731
760
|
var ACP_PROTOCOL_VERSION = 1;
|
|
732
761
|
var CURSOR_ASK_QUESTION_ID = "cursor-ask-question";
|
|
733
762
|
var ACP_EXIT_GRACE_MS = 1e3;
|
|
@@ -1846,12 +1875,14 @@ function getBackend(id) {
|
|
|
1846
1875
|
export {
|
|
1847
1876
|
codexBackend,
|
|
1848
1877
|
parseStreamLine,
|
|
1878
|
+
parseResultLine,
|
|
1849
1879
|
renderStreamEvent,
|
|
1850
1880
|
createStreamRenderer,
|
|
1851
1881
|
cursorAcpBackend,
|
|
1852
1882
|
resolveHitlRunOptions,
|
|
1853
1883
|
redactSecrets,
|
|
1854
1884
|
RedactingStreamBuffer,
|
|
1885
|
+
CLAUDE_NESTED_SESSION_KEYS,
|
|
1855
1886
|
claudeChildEnv,
|
|
1856
1887
|
CLAUDE_MAX_TURNS_ENV,
|
|
1857
1888
|
CLAUDE_MAX_BUDGET_USD_ENV,
|