@hexdecimal16/nexo 0.2.2 → 0.3.1
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/cli/src/companion.js +23 -0
- package/cli/src/ui.js +2 -0
- package/core/src/engine/run.js +2 -1
- package/core/src/engine/spawn-tool.js +29 -3
- package/core/src/providers/claude-p.js +2 -18
- package/core/src/providers/kiro-p.js +395 -147
- package/core/src/providers/process-runner.js +91 -0
- package/package.json +1 -1
- package/sdk/src/index.js +1 -0
- package/sdk/src/run-tree.js +81 -0
- package/tools/src/builtin.js +32 -5
- package/tools/src/html-text.js +68 -0
- package/tui/src/app.js +8 -4
- package/tui/src/components.js +31 -5
- package/web/public/cost.html +1 -0
- package/web/public/index.html +1 -0
- package/web/public/judge.html +1 -0
- package/web/public/learning.html +1 -0
- package/web/public/lib/companion-mood.ts +82 -0
- package/web/public/lib/companion-pages.ts +31 -0
- package/web/public/optim.html +1 -0
- package/web/public/pages/companion.ts +264 -0
- package/web/public/run.html +1 -0
- package/web/public/sessions.html +1 -0
- package/web/public/style.css +61 -0
- package/web/public/wikis.html +1 -0
- package/web/src/companion.js +149 -0
- package/web/src/server.js +9 -0
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { rmSync } from "node:fs";
|
|
2
4
|
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
5
|
import { tmpdir } from "node:os";
|
|
4
6
|
import { join } from "node:path";
|
|
@@ -11,25 +13,86 @@ export const KIRO_P_PROVIDER = "kiro-p";
|
|
|
11
13
|
const KIRO_P_API = "kiro-p";
|
|
12
14
|
/** Environment variable naming the Kiro CLI executable; `kiro-cli` on the PATH otherwise. */
|
|
13
15
|
export const KIRO_P_COMMAND_ENV = "NEXO_KIRO_P_COMMAND";
|
|
14
|
-
/**
|
|
16
|
+
/** Environment variable overriding how long a turn may stay silent before it is cancelled (milliseconds). */
|
|
17
|
+
export const KIRO_P_IDLE_ENV = "NEXO_KIRO_P_IDLE_MS";
|
|
18
|
+
const IDLE_MS_DEFAULT = 120_000;
|
|
19
|
+
/** How long a cancelled turn gets to settle before the whole process is considered stuck and killed. */
|
|
20
|
+
const CANCEL_GRACE_MS = 5_000;
|
|
21
|
+
/** `effortLevel` values; pi's `minimal` maps to `low`. */
|
|
15
22
|
const EFFORTS = new Set(["low", "medium", "high", "xhigh", "max"]);
|
|
23
|
+
const isWindows = process.platform === "win32";
|
|
24
|
+
const liveGroups = new Set();
|
|
25
|
+
const liveDirs = new Set();
|
|
26
|
+
let exitHandlerRegistered = false;
|
|
27
|
+
function registerExitHandler() {
|
|
28
|
+
if (exitHandlerRegistered)
|
|
29
|
+
return;
|
|
30
|
+
exitHandlerRegistered = true;
|
|
31
|
+
process.on("exit", () => {
|
|
32
|
+
for (const pgid of liveGroups) {
|
|
33
|
+
try {
|
|
34
|
+
process.kill(-pgid, "SIGKILL");
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// the process group already exited
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (const dir of liveDirs)
|
|
41
|
+
rmSync(dir, { recursive: true, force: true });
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function killGroup(child) {
|
|
45
|
+
try {
|
|
46
|
+
if (!isWindows && child.pid !== undefined)
|
|
47
|
+
process.kill(-child.pid, "SIGKILL");
|
|
48
|
+
else
|
|
49
|
+
child.kill("SIGKILL");
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// the process group already exited
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Spawns the server in its own process group; the pipes are unref'd so an idle server never keeps the host alive. */
|
|
16
56
|
function spawnRunner(command) {
|
|
17
|
-
return ({ args,
|
|
18
|
-
|
|
57
|
+
return ({ args, cwd }) => {
|
|
58
|
+
registerExitHandler();
|
|
59
|
+
const child = spawn(command, args, { cwd, stdio: ["pipe", "pipe", "pipe"], detached: !isWindows });
|
|
60
|
+
const pgid = child.pid;
|
|
61
|
+
if (pgid !== undefined && !isWindows)
|
|
62
|
+
liveGroups.add(pgid);
|
|
19
63
|
let stderr = "";
|
|
20
64
|
child.stderr.setEncoding("utf8");
|
|
21
65
|
child.stderr.on("data", (chunk) => {
|
|
22
66
|
if (stderr.length < 16_384)
|
|
23
67
|
stderr += chunk;
|
|
24
|
-
onStderr?.(chunk);
|
|
25
68
|
});
|
|
69
|
+
child.stdin.on("error", () => { });
|
|
26
70
|
const exit = new Promise((resolve) => {
|
|
27
|
-
child.on("error", (error) =>
|
|
28
|
-
|
|
71
|
+
child.on("error", (error) => {
|
|
72
|
+
if (pgid !== undefined)
|
|
73
|
+
liveGroups.delete(pgid);
|
|
74
|
+
resolve({ code: null, stderr: stderr || error.message });
|
|
75
|
+
});
|
|
76
|
+
child.on("close", (code) => {
|
|
77
|
+
if (pgid !== undefined)
|
|
78
|
+
liveGroups.delete(pgid);
|
|
79
|
+
resolve({ code, stderr });
|
|
80
|
+
});
|
|
29
81
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
82
|
+
return {
|
|
83
|
+
write: (line) => {
|
|
84
|
+
child.stdin.write(`${line}\n`);
|
|
85
|
+
},
|
|
86
|
+
lines: createInterface({ input: child.stdout, crlfDelay: Infinity }),
|
|
87
|
+
exit,
|
|
88
|
+
kill: () => killGroup(child),
|
|
89
|
+
hold: (on) => {
|
|
90
|
+
const method = on ? "ref" : "unref";
|
|
91
|
+
child[method]();
|
|
92
|
+
for (const s of [child.stdin, child.stdout, child.stderr])
|
|
93
|
+
s[method]?.();
|
|
94
|
+
},
|
|
95
|
+
};
|
|
33
96
|
};
|
|
34
97
|
}
|
|
35
98
|
/** Kiro's model ids (`kiro-cli chat --list-models`) with their context windows. Kiro bills in credits, so cost stays zero. */
|
|
@@ -176,9 +239,163 @@ function effortFlag(reasoning) {
|
|
|
176
239
|
function emptyUsage() {
|
|
177
240
|
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
|
|
178
241
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
242
|
+
/**
|
|
243
|
+
* One long-lived `kiro-cli acp --agent-engine=v3` server. Requests are matched to responses by id, session updates are
|
|
244
|
+
* routed by session id, and every pending request fails at once when the process goes away.
|
|
245
|
+
*/
|
|
246
|
+
class AcpConnection {
|
|
247
|
+
closed = false;
|
|
248
|
+
dir;
|
|
249
|
+
nextId = 0;
|
|
250
|
+
pending = new Map();
|
|
251
|
+
listeners = new Map();
|
|
252
|
+
agents = new Map();
|
|
253
|
+
proc;
|
|
254
|
+
onClose;
|
|
255
|
+
constructor(proc, dir, onClose) {
|
|
256
|
+
this.proc = proc;
|
|
257
|
+
this.dir = dir;
|
|
258
|
+
this.onClose = onClose;
|
|
259
|
+
}
|
|
260
|
+
/** Starts the server and completes the `initialize` handshake. */
|
|
261
|
+
static async open(run, onClose) {
|
|
262
|
+
registerExitHandler();
|
|
263
|
+
// The agents live in a throwaway workspace: Kiro finds them under ./.kiro/agents, and no project files are in reach.
|
|
264
|
+
const dir = await mkdtemp(join(tmpdir(), "nexo-kiro-p-"));
|
|
265
|
+
liveDirs.add(dir);
|
|
266
|
+
await mkdir(join(dir, ".kiro", "agents"), { recursive: true });
|
|
267
|
+
const proc = run({ args: ["acp", "--agent-engine=v3"], cwd: dir });
|
|
268
|
+
const conn = new AcpConnection(proc, dir, onClose);
|
|
269
|
+
void conn.pump();
|
|
270
|
+
void proc.exit.then((exit) => conn.shutdown(new Error(exit.stderr.trim().split("\n").at(-1) || `kiro-cli exited with code ${exit.code ?? "null"}`)));
|
|
271
|
+
try {
|
|
272
|
+
await conn.request("initialize", { protocolVersion: 1, clientCapabilities: {}, clientInfo: { name: "nexo", version: "0" } });
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
conn.shutdown(error instanceof Error ? error : new Error(String(error)));
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
return conn;
|
|
279
|
+
}
|
|
280
|
+
async pump() {
|
|
281
|
+
try {
|
|
282
|
+
for await (const line of this.proc.lines)
|
|
283
|
+
this.handle(line);
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
// the stream was torn down with the process
|
|
287
|
+
}
|
|
288
|
+
this.shutdown(new Error("kiro-cli closed its output"));
|
|
289
|
+
}
|
|
290
|
+
handle(line) {
|
|
291
|
+
if (!line.trim())
|
|
292
|
+
return;
|
|
293
|
+
let m;
|
|
294
|
+
try {
|
|
295
|
+
m = JSON.parse(line);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (m.id !== undefined && m.method) {
|
|
301
|
+
// Agent-to-client request: refuse permissions (the agents deny everything anyway) and error on the rest, so the agent never waits.
|
|
302
|
+
if (m.method === "session/request_permission") {
|
|
303
|
+
const options = (m.params?.options ?? []);
|
|
304
|
+
const pick = options.find((o) => o.kind === "reject_once") ?? options.find((o) => o.kind === "reject_always");
|
|
305
|
+
this.send({ id: m.id, result: { outcome: pick ? { outcome: "selected", optionId: pick.optionId } : { outcome: "cancelled" } } });
|
|
306
|
+
}
|
|
307
|
+
else
|
|
308
|
+
this.send({ id: m.id, error: { code: -32601, message: "Method not found" } });
|
|
309
|
+
}
|
|
310
|
+
else if (m.id !== undefined) {
|
|
311
|
+
const p = this.pending.get(Number(m.id));
|
|
312
|
+
if (!p)
|
|
313
|
+
return;
|
|
314
|
+
this.settle(Number(m.id));
|
|
315
|
+
if (m.error)
|
|
316
|
+
p.reject(new Error(m.error.message ?? `error ${m.error.code ?? ""}`));
|
|
317
|
+
else
|
|
318
|
+
p.resolve(m.result);
|
|
319
|
+
}
|
|
320
|
+
else if (m.method === "session/update") {
|
|
321
|
+
const sessionId = m.params?.sessionId;
|
|
322
|
+
if (typeof sessionId === "string")
|
|
323
|
+
this.listeners.get(sessionId)?.(m.params?.update);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
send(message) {
|
|
327
|
+
try {
|
|
328
|
+
this.proc.write(JSON.stringify({ jsonrpc: "2.0", ...message }));
|
|
329
|
+
}
|
|
330
|
+
catch {
|
|
331
|
+
// a dead process is reported through exit
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
settle(id) {
|
|
335
|
+
this.pending.delete(id);
|
|
336
|
+
// Idle server: let the host exit (the exit handler kills the server); a request in flight keeps the host alive.
|
|
337
|
+
if (this.pending.size === 0 && !this.closed)
|
|
338
|
+
this.proc.hold?.(false);
|
|
339
|
+
}
|
|
340
|
+
request(method, params) {
|
|
341
|
+
if (this.closed)
|
|
342
|
+
return Promise.reject(new Error("kiro-cli is not running"));
|
|
343
|
+
const id = ++this.nextId;
|
|
344
|
+
return new Promise((resolve, reject) => {
|
|
345
|
+
if (this.pending.size === 0)
|
|
346
|
+
this.proc.hold?.(true);
|
|
347
|
+
this.pending.set(id, { resolve, reject });
|
|
348
|
+
this.send({ id, method, params });
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
notify(method, params) {
|
|
352
|
+
this.send({ method, params });
|
|
353
|
+
}
|
|
354
|
+
listen(sessionId, fn) {
|
|
355
|
+
this.listeners.set(sessionId, fn);
|
|
356
|
+
}
|
|
357
|
+
unlisten(sessionId) {
|
|
358
|
+
this.listeners.delete(sessionId);
|
|
359
|
+
}
|
|
360
|
+
/** Writes the agent file for `name` once; later sessions select it with `modeId`. */
|
|
361
|
+
ensureAgent(name, prompt) {
|
|
362
|
+
let written = this.agents.get(name);
|
|
363
|
+
if (!written) {
|
|
364
|
+
const agent = {
|
|
365
|
+
name,
|
|
366
|
+
description: "nexo model turn",
|
|
367
|
+
prompt,
|
|
368
|
+
tools: [],
|
|
369
|
+
// `tools: []` alone still leaves Kiro's skill tool in the request, which keeps the model in tool-calling mode and
|
|
370
|
+
// makes it call the actions below natively ("Tool X is not available"). Denying every capability empties the
|
|
371
|
+
// request's tool list, so the model cannot emit a tool call at all.
|
|
372
|
+
permissions: { rules: [{ capability: "all", effect: "deny" }] },
|
|
373
|
+
mcpServers: {},
|
|
374
|
+
includeMcpJson: false,
|
|
375
|
+
includePowers: false,
|
|
376
|
+
};
|
|
377
|
+
written = writeFile(join(this.dir, ".kiro", "agents", `${name}.json`), JSON.stringify(agent), "utf8");
|
|
378
|
+
this.agents.set(name, written);
|
|
379
|
+
}
|
|
380
|
+
return written;
|
|
381
|
+
}
|
|
382
|
+
/** Fails everything in flight, stops the process and removes the workspace. Safe to call more than once. */
|
|
383
|
+
shutdown(reason) {
|
|
384
|
+
if (this.closed)
|
|
385
|
+
return;
|
|
386
|
+
this.closed = true;
|
|
387
|
+
this.onClose();
|
|
388
|
+
for (const p of this.pending.values())
|
|
389
|
+
p.reject(reason);
|
|
390
|
+
this.pending.clear();
|
|
391
|
+
this.listeners.clear();
|
|
392
|
+
this.proc.kill();
|
|
393
|
+
liveDirs.delete(this.dir);
|
|
394
|
+
void rm(this.dir, { recursive: true, force: true }).catch(() => { });
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const RETRY_NOTE = "Your previous reply was not a single valid JSON object in the required format. Reply again with only that JSON object.";
|
|
398
|
+
function streamKiroP(getConn, model, context, options) {
|
|
182
399
|
const stream = createAssistantMessageEventStream();
|
|
183
400
|
const partial = {
|
|
184
401
|
role: "assistant",
|
|
@@ -211,143 +428,161 @@ function streamKiroP(run, model, context, options) {
|
|
|
211
428
|
const tools = getCurrentTools(context.messages);
|
|
212
429
|
const useTools = tools.length > 0 && options?.toolChoice !== "none";
|
|
213
430
|
const systemPrompt = [getCurrentSystemPrompt(context.messages), useTools ? kiroToolsSection(tools) : ""].filter((s) => s.length > 0).join("\n\n");
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
431
|
+
const agentPrompt = systemPrompt || "You are a helpful assistant.";
|
|
432
|
+
const agentName = `nexo-${createHash("sha256").update(agentPrompt).digest("hex").slice(0, 12)}`;
|
|
433
|
+
const effort = effortFlag(options?.reasoning);
|
|
434
|
+
const idleMs = Number(process.env[KIRO_P_IDLE_ENV]) || IDLE_MS_DEFAULT;
|
|
435
|
+
/** A fresh session on the shared server, running the agent that carries this turn's system prompt. */
|
|
436
|
+
const startSession = async () => {
|
|
437
|
+
const conn = await getConn();
|
|
438
|
+
await conn.ensureAgent(agentName, agentPrompt);
|
|
439
|
+
// Kiro notices a new agent file a moment after it is written, and a session created sooner silently runs its
|
|
440
|
+
// default agent (which has tools). Retry a few times until the requested agent is the session's mode.
|
|
441
|
+
let mode;
|
|
442
|
+
for (let attempt = 0; attempt < 5; attempt++) {
|
|
443
|
+
if (attempt > 0)
|
|
444
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
445
|
+
const res = (await conn.request("session/new", {
|
|
446
|
+
cwd: conn.dir,
|
|
447
|
+
mcpServers: [],
|
|
448
|
+
_meta: { kiro: { modeId: agentName, modelId: model.id, ...(effort ? { effortLevel: effort } : {}) } },
|
|
449
|
+
}));
|
|
450
|
+
mode = res.modes?.currentModeId;
|
|
451
|
+
if (res.sessionId && mode === agentName)
|
|
452
|
+
return { conn, id: res.sessionId };
|
|
453
|
+
}
|
|
454
|
+
// Still not applied: refuse to run rather than let the model see Kiro's default tools.
|
|
455
|
+
throw new Error(`stopped: agent "${agentName}" not applied (mode ${mode ?? "none"})`);
|
|
456
|
+
};
|
|
457
|
+
/** One prompt on a session; plain turns stream their text and thinking into `partial` as it arrives. */
|
|
458
|
+
const runOnce = async (session, text) => {
|
|
459
|
+
const { conn, id } = session;
|
|
460
|
+
const outcome = { text: "" };
|
|
461
|
+
// A native tool call means the model was offered tools after all; Kiro would answer "not available" and loop.
|
|
462
|
+
let nativeCall;
|
|
463
|
+
let stalled = false;
|
|
464
|
+
let open;
|
|
465
|
+
let idle;
|
|
466
|
+
let killTimer;
|
|
467
|
+
const cancel = () => conn.notify("session/cancel", { sessionId: id });
|
|
468
|
+
const arm = () => {
|
|
469
|
+
clearTimeout(idle);
|
|
470
|
+
idle = setTimeout(() => {
|
|
471
|
+
stalled = true;
|
|
472
|
+
cancel();
|
|
473
|
+
killTimer = setTimeout(() => conn.shutdown(new Error("kiro-cli did not answer the cancel")), CANCEL_GRACE_MS);
|
|
474
|
+
}, idleMs);
|
|
231
475
|
};
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
signal?.addEventListener("abort", onAbort, { once: true });
|
|
249
|
-
const proc = run({
|
|
250
|
-
args,
|
|
251
|
-
stdin,
|
|
252
|
-
cwd,
|
|
253
|
-
signal: kill.signal,
|
|
254
|
-
onStderr: (chunk) => {
|
|
255
|
-
const m = /\[warn\] (agent "[^"]*"[^\n]*)/.exec(chunk);
|
|
256
|
-
if (m && !fellBack) {
|
|
257
|
-
fellBack = m[1];
|
|
258
|
-
kill.abort();
|
|
259
|
-
}
|
|
260
|
-
},
|
|
261
|
-
});
|
|
262
|
-
const outcome = { finalText: "" };
|
|
263
|
-
let open;
|
|
264
|
-
const close = () => {
|
|
265
|
-
if (!open)
|
|
266
|
-
return;
|
|
267
|
-
const block = partial.content[open.index];
|
|
268
|
-
if (block?.type === "text")
|
|
269
|
-
stream.push({ type: "text_end", contentIndex: open.index, content: block.text, partial: snapshot() });
|
|
270
|
-
else if (block?.type === "thinking")
|
|
271
|
-
stream.push({ type: "thinking_end", contentIndex: open.index, content: block.thinking, partial: snapshot() });
|
|
272
|
-
open = undefined;
|
|
273
|
-
};
|
|
274
|
-
try {
|
|
275
|
-
for await (const line of proc.lines) {
|
|
276
|
-
if (!line.trim())
|
|
277
|
-
continue;
|
|
278
|
-
let event;
|
|
279
|
-
try {
|
|
280
|
-
event = JSON.parse(line);
|
|
281
|
-
}
|
|
282
|
-
catch {
|
|
283
|
-
continue;
|
|
284
|
-
}
|
|
285
|
-
const data = event.data ?? {};
|
|
286
|
-
if (event.type === "runFinished") {
|
|
287
|
-
outcome.finalText = data.finalText ?? "";
|
|
288
|
-
outcome.stopReason = data.stopReason;
|
|
289
|
-
if (data.status !== "success")
|
|
290
|
-
outcome.error = `run ${data.status ?? "failed"}${data.stopReason ? ` (${data.stopReason})` : ""}`;
|
|
291
|
-
}
|
|
292
|
-
else if (event.type === "runError")
|
|
293
|
-
outcome.error = data.message ?? "run failed";
|
|
294
|
-
else if (event.type === "sessionUpdate" && data.update?.sessionUpdate === "tool_call") {
|
|
295
|
-
if (!nativeCall) {
|
|
296
|
-
nativeCall = data.update.title ?? "tool";
|
|
297
|
-
kill.abort();
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
else if (event.type === "sessionUpdate" && !useTools) {
|
|
301
|
-
const kind = data.update?.sessionUpdate === "agent_message_chunk" ? "text" : data.update?.sessionUpdate === "agent_thought_chunk" ? "thinking" : undefined;
|
|
302
|
-
const delta = data.update?.content?.type === "text" ? data.update.content.text : undefined;
|
|
303
|
-
if (!kind || !delta)
|
|
304
|
-
continue;
|
|
305
|
-
if (open?.kind !== kind) {
|
|
306
|
-
close();
|
|
307
|
-
open = { kind, index: partial.content.length };
|
|
308
|
-
partial.content.push(kind === "text" ? { type: "text", text: "" } : { type: "thinking", thinking: "" });
|
|
309
|
-
stream.push({ type: kind === "text" ? "text_start" : "thinking_start", contentIndex: open.index, partial: snapshot() });
|
|
310
|
-
}
|
|
311
|
-
const block = partial.content[open.index];
|
|
312
|
-
if (block.type === "text")
|
|
313
|
-
block.text += delta;
|
|
314
|
-
else if (block.type === "thinking")
|
|
315
|
-
block.thinking += delta;
|
|
316
|
-
stream.push({ type: kind === "text" ? "text_delta" : "thinking_delta", contentIndex: open.index, delta, partial: snapshot() });
|
|
317
|
-
}
|
|
476
|
+
const close = () => {
|
|
477
|
+
if (!open)
|
|
478
|
+
return;
|
|
479
|
+
const block = partial.content[open.index];
|
|
480
|
+
if (block?.type === "text")
|
|
481
|
+
stream.push({ type: "text_end", contentIndex: open.index, content: block.text, partial: snapshot() });
|
|
482
|
+
else if (block?.type === "thinking")
|
|
483
|
+
stream.push({ type: "thinking_end", contentIndex: open.index, content: block.thinking, partial: snapshot() });
|
|
484
|
+
open = undefined;
|
|
485
|
+
};
|
|
486
|
+
conn.listen(id, (u) => {
|
|
487
|
+
arm();
|
|
488
|
+
if (u.sessionUpdate === "tool_call") {
|
|
489
|
+
if (!nativeCall) {
|
|
490
|
+
nativeCall = u.title ?? "tool";
|
|
491
|
+
cancel();
|
|
318
492
|
}
|
|
319
|
-
|
|
320
|
-
const exit = await proc.exit;
|
|
321
|
-
if (fellBack)
|
|
322
|
-
outcome.error = `stopped: ${fellBack}`;
|
|
323
|
-
else if (nativeCall)
|
|
324
|
-
outcome.error = `stopped: native tool call "${nativeCall}" (the nexo agent offers no tools)`;
|
|
325
|
-
else if (!outcome.error && outcome.stopReason === undefined)
|
|
326
|
-
outcome.error = exit.stderr.trim().split("\n").at(-1) || `exited with code ${exit.code ?? "null"} without a result`;
|
|
493
|
+
return;
|
|
327
494
|
}
|
|
328
|
-
|
|
329
|
-
|
|
495
|
+
const kind = u.sessionUpdate === "agent_message_chunk" ? "text" : u.sessionUpdate === "agent_thought_chunk" ? "thinking" : undefined;
|
|
496
|
+
const delta = u.content?.type === "text" ? u.content.text : undefined;
|
|
497
|
+
if (!kind || !delta)
|
|
498
|
+
return;
|
|
499
|
+
if (kind === "text")
|
|
500
|
+
outcome.text += delta;
|
|
501
|
+
if (useTools)
|
|
502
|
+
return;
|
|
503
|
+
if (open?.kind !== kind) {
|
|
504
|
+
close();
|
|
505
|
+
open = { kind, index: partial.content.length };
|
|
506
|
+
partial.content.push(kind === "text" ? { type: "text", text: "" } : { type: "thinking", thinking: "" });
|
|
507
|
+
stream.push({ type: kind === "text" ? "text_start" : "thinking_start", contentIndex: open.index, partial: snapshot() });
|
|
330
508
|
}
|
|
331
|
-
|
|
509
|
+
const block = partial.content[open.index];
|
|
510
|
+
if (block.type === "text")
|
|
511
|
+
block.text += delta;
|
|
512
|
+
else if (block.type === "thinking")
|
|
513
|
+
block.thinking += delta;
|
|
514
|
+
stream.push({ type: kind === "text" ? "text_delta" : "thinking_delta", contentIndex: open.index, delta, partial: snapshot() });
|
|
515
|
+
});
|
|
516
|
+
let abort;
|
|
517
|
+
const aborted = new Promise((resolve) => {
|
|
518
|
+
abort = () => resolve("aborted");
|
|
519
|
+
});
|
|
520
|
+
const onAbort = () => {
|
|
521
|
+
cancel();
|
|
522
|
+
abort();
|
|
332
523
|
};
|
|
333
|
-
|
|
524
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
525
|
+
arm();
|
|
526
|
+
try {
|
|
527
|
+
const settled = await Promise.race([
|
|
528
|
+
conn.request("session/prompt", { sessionId: id, prompt: [{ type: "text", text }] }).then((r) => ({ r: r }), (e) => ({ e })),
|
|
529
|
+
aborted,
|
|
530
|
+
]);
|
|
531
|
+
close();
|
|
532
|
+
if (settled === "aborted")
|
|
533
|
+
outcome.aborted = true;
|
|
534
|
+
else if (stalled)
|
|
535
|
+
outcome.error = `no output for ${Math.round(idleMs / 1000)}s`;
|
|
536
|
+
else if (nativeCall)
|
|
537
|
+
outcome.error = `stopped: native tool call "${nativeCall}" (the nexo agent offers no tools)`;
|
|
538
|
+
else if ("e" in settled) {
|
|
539
|
+
outcome.error = settled.e.message;
|
|
540
|
+
outcome.lost = conn.closed;
|
|
541
|
+
}
|
|
542
|
+
else {
|
|
543
|
+
outcome.stopReason = settled.r?.stopReason;
|
|
544
|
+
if (outcome.stopReason === "cancelled")
|
|
545
|
+
outcome.error = "run cancelled";
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
finally {
|
|
549
|
+
clearTimeout(idle);
|
|
550
|
+
clearTimeout(killTimer);
|
|
551
|
+
conn.unlisten(id);
|
|
552
|
+
signal?.removeEventListener("abort", onAbort);
|
|
553
|
+
}
|
|
554
|
+
return outcome;
|
|
555
|
+
};
|
|
556
|
+
try {
|
|
557
|
+
stream.push({ type: "start", partial: snapshot() });
|
|
558
|
+
const prompt = renderTranscript(context.messages);
|
|
559
|
+
let session = await startSession();
|
|
334
560
|
if (signal?.aborted)
|
|
335
561
|
return fail("aborted", "Request was aborted");
|
|
562
|
+
let outcome = await runOnce(session, prompt);
|
|
563
|
+
// The shared server died under this turn before anything streamed: start a fresh one and run it again, once.
|
|
564
|
+
if (outcome.lost && !signal?.aborted && partial.content.length === 0) {
|
|
565
|
+
session = await startSession();
|
|
566
|
+
outcome = await runOnce(session, prompt);
|
|
567
|
+
}
|
|
568
|
+
if (outcome.aborted || signal?.aborted)
|
|
569
|
+
return fail("aborted", "Request was aborted");
|
|
336
570
|
if (outcome.error)
|
|
337
571
|
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
338
572
|
let stopReason = outcome.stopReason === "max_tokens" ? "length" : "stop";
|
|
339
573
|
if (useTools) {
|
|
340
|
-
let reply = parseReply(outcome.
|
|
574
|
+
let reply = parseReply(outcome.text, tools);
|
|
341
575
|
if (!reply) {
|
|
342
|
-
|
|
343
|
-
|
|
576
|
+
// Same session, so the model still has the transcript: only the correction is sent.
|
|
577
|
+
outcome = await runOnce(session, RETRY_NOTE);
|
|
578
|
+
if (outcome.aborted || signal?.aborted)
|
|
344
579
|
return fail("aborted", "Request was aborted");
|
|
345
580
|
if (outcome.error)
|
|
346
581
|
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
347
|
-
reply = parseReply(outcome.
|
|
582
|
+
reply = parseReply(outcome.text, tools);
|
|
348
583
|
}
|
|
349
584
|
// Still not the JSON format: keep what the model said as a plain answer rather than dropping it.
|
|
350
|
-
const { text, calls } = reply ?? { text: outcome.
|
|
585
|
+
const { text, calls } = reply ?? { text: outcome.text, calls: [] };
|
|
351
586
|
if (text)
|
|
352
587
|
pushText(text);
|
|
353
588
|
for (const call of calls) {
|
|
@@ -360,8 +595,8 @@ function streamKiroP(run, model, context, options) {
|
|
|
360
595
|
}
|
|
361
596
|
stopReason = calls.length > 0 ? "toolUse" : "stop";
|
|
362
597
|
}
|
|
363
|
-
else if (!partial.content.some((b) => b.type === "text") && outcome.
|
|
364
|
-
pushText(outcome.
|
|
598
|
+
else if (!partial.content.some((b) => b.type === "text") && outcome.text) {
|
|
599
|
+
pushText(outcome.text);
|
|
365
600
|
}
|
|
366
601
|
const message = { ...snapshot(), stopReason };
|
|
367
602
|
stream.push({ type: "done", reason: stopReason, message });
|
|
@@ -372,30 +607,43 @@ function streamKiroP(run, model, context, options) {
|
|
|
372
607
|
return fail("aborted", "Request was aborted");
|
|
373
608
|
fail("error", `kiro-cli failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
374
609
|
}
|
|
375
|
-
finally {
|
|
376
|
-
if (dir)
|
|
377
|
-
await rm(dir, { recursive: true, force: true }).catch(() => { });
|
|
378
|
-
}
|
|
379
610
|
});
|
|
380
611
|
return stream;
|
|
381
612
|
}
|
|
382
613
|
/**
|
|
383
|
-
* Runs models through
|
|
384
|
-
* CLI's own login.
|
|
385
|
-
*
|
|
386
|
-
*
|
|
387
|
-
*
|
|
388
|
-
*
|
|
614
|
+
* Runs models through one long-lived Kiro CLI ACP server (`kiro-cli acp --agent-engine=v3`), authenticated by the
|
|
615
|
+
* CLI's own login. The server starts on the first call and is shared by every call after it, which removes the
|
|
616
|
+
* per-call CLI start-up. Each turn is a fresh session whose agent carries the system prompt (one agent file per
|
|
617
|
+
* prompt hash), no tools and a policy denying every capability, so Kiro's request carries no tool at all; a native
|
|
618
|
+
* tool call cancels the turn. Nexo tools are described in the prompt and the reply parsed as `{ text, tool_calls }`
|
|
619
|
+
* (one correction retry when it does not parse), so tool turns arrive whole rather than streamed. A turn silent for
|
|
620
|
+
* `$NEXO_KIRO_P_IDLE_MS` (120s) is cancelled; a server that dies is replaced on the next call, and a turn it dropped
|
|
621
|
+
* is re-run once. Kiro bills in credits and reports no token counts, so usage is empty.
|
|
389
622
|
*/
|
|
390
623
|
export function kiroPProvider(options = {}) {
|
|
391
624
|
const run = options.run ?? spawnRunner(options.command ?? process.env[KIRO_P_COMMAND_ENV] ?? "kiro-cli");
|
|
625
|
+
let current;
|
|
626
|
+
const getConn = () => {
|
|
627
|
+
if (current)
|
|
628
|
+
return current;
|
|
629
|
+
const opened = AcpConnection.open(run, () => {
|
|
630
|
+
if (current === opened)
|
|
631
|
+
current = undefined;
|
|
632
|
+
});
|
|
633
|
+
current = opened;
|
|
634
|
+
opened.catch(() => {
|
|
635
|
+
if (current === opened)
|
|
636
|
+
current = undefined;
|
|
637
|
+
});
|
|
638
|
+
return opened;
|
|
639
|
+
};
|
|
392
640
|
const api = {
|
|
393
|
-
stream: (model, context, opts) => streamKiroP(
|
|
394
|
-
streamSimple: (model, context, opts) => streamKiroP(
|
|
641
|
+
stream: (model, context, opts) => streamKiroP(getConn, model, context, opts),
|
|
642
|
+
streamSimple: (model, context, opts) => streamKiroP(getConn, model, context, opts),
|
|
395
643
|
};
|
|
396
644
|
return createProvider({
|
|
397
645
|
id: KIRO_P_PROVIDER,
|
|
398
|
-
name: "Kiro CLI (kiro-cli
|
|
646
|
+
name: "Kiro CLI (kiro-cli acp)",
|
|
399
647
|
auth: {
|
|
400
648
|
apiKey: {
|
|
401
649
|
name: "Kiro CLI login",
|