@hexdecimal16/nexo 0.4.12 → 0.4.14
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 +3 -0
- package/cli/src/common.js +1 -1
- package/cli/src/main.js +17 -2
- package/core/src/config/schema.js +3 -1
- package/core/src/engine/context-trim.js +39 -0
- package/core/src/engine/errors.js +10 -2
- package/core/src/engine/messages.js +14 -1
- package/core/src/engine/provider-gate.js +5 -3
- package/core/src/engine/run.js +105 -14
- package/core/src/providers/claude-p.js +129 -23
- package/core/src/providers/codex-p.js +16 -3
- package/core/src/providers/kiro-p.js +115 -21
- package/core/src/providers/reply-text.js +110 -0
- package/core/src/session/mentions.js +32 -8
- package/core/src/session/session.js +17 -13
- package/core/src/settings/config-edit.js +6 -0
- package/package.json +1 -1
- package/sdk/src/index.js +2 -0
- package/store/src/run-dir.js +10 -0
- package/tools/src/builtin.js +39 -8
- package/tools/src/images.js +174 -0
- package/tools/src/index.js +1 -0
- package/tui/src/app.js +71 -5
- package/tui/src/commands.js +1 -1
- package/tui/src/components.js +73 -0
- package/tui/src/image-attachments.js +142 -0
- package/web/public/lib/span-view.ts +21 -1
- package/web/public/pages/run.ts +8 -1
- package/web/public/run.html +1 -0
- package/web/public/style.css +2 -0
|
@@ -4,6 +4,7 @@ import { join } from "node:path";
|
|
|
4
4
|
import { calculateCost, createAssistantMessageEventStream, createProvider, getCurrentSystemPrompt, getCurrentTools, } from "@earendil-works/pi-ai";
|
|
5
5
|
import { getBuiltinModels } from "@earendil-works/pi-ai/providers/all";
|
|
6
6
|
import { spawnGroup } from "./process-runner.js";
|
|
7
|
+
import { ReplyTextExtractor } from "./reply-text.js";
|
|
7
8
|
/** Provider id: models are referenced as `claude-p/<model>`, e.g. `claude-p/opus` or `claude-p/claude-sonnet-5-5`. */
|
|
8
9
|
export const CLAUDE_P_PROVIDER = "claude-p";
|
|
9
10
|
const CLAUDE_P_API = "claude-p";
|
|
@@ -26,7 +27,7 @@ function claudePModels() {
|
|
|
26
27
|
api: CLAUDE_P_API,
|
|
27
28
|
provider: CLAUDE_P_PROVIDER,
|
|
28
29
|
baseUrl: "",
|
|
29
|
-
input: ["text"],
|
|
30
|
+
input: ["text", "image"],
|
|
30
31
|
});
|
|
31
32
|
const out = anthropic.map((m) => rehome(m, m.id, `${m.name} (claude -p)`));
|
|
32
33
|
for (const alias of ALIASES) {
|
|
@@ -39,24 +40,42 @@ function claudePModels() {
|
|
|
39
40
|
}
|
|
40
41
|
return out;
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
+
/** Appends text to `parts`, joining it to a trailing text part. */
|
|
44
|
+
export function pushText(parts, text) {
|
|
45
|
+
if (!text)
|
|
46
|
+
return;
|
|
47
|
+
const last = parts.at(-1);
|
|
48
|
+
if (last?.type === "text")
|
|
49
|
+
last.text += text;
|
|
50
|
+
else
|
|
51
|
+
parts.push({ type: "text", text });
|
|
52
|
+
}
|
|
53
|
+
function pushContent(parts, content) {
|
|
43
54
|
if (typeof content === "string")
|
|
44
|
-
return content;
|
|
45
|
-
|
|
55
|
+
return pushText(parts, content);
|
|
56
|
+
content.forEach((b, i) => {
|
|
57
|
+
if (i > 0)
|
|
58
|
+
pushText(parts, "\n");
|
|
59
|
+
if (b.type === "text")
|
|
60
|
+
pushText(parts, b.text ?? "");
|
|
61
|
+
else if (b.type === "image" && b.data && b.mimeType)
|
|
62
|
+
parts.push({ type: "image", data: b.data, mimeType: b.mimeType });
|
|
63
|
+
});
|
|
46
64
|
}
|
|
47
65
|
function attr(value) {
|
|
48
66
|
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
49
67
|
}
|
|
50
|
-
/**
|
|
51
|
-
export function
|
|
52
|
-
const turns = messages.filter((m) => m.role !== "system");
|
|
53
|
-
const only = turns[0];
|
|
54
|
-
if (turns.length === 1 && only?.role === "user")
|
|
55
|
-
return contentText(only.content);
|
|
68
|
+
/** The conversation's entries as tagged blocks, in order, with images left in place; system messages are left out. */
|
|
69
|
+
export function renderEntryParts(messages) {
|
|
56
70
|
const parts = [];
|
|
57
|
-
|
|
58
|
-
if (
|
|
59
|
-
parts
|
|
71
|
+
messages.forEach((m, i) => {
|
|
72
|
+
if (i > 0)
|
|
73
|
+
pushText(parts, "\n\n");
|
|
74
|
+
if (m.role === "user") {
|
|
75
|
+
pushText(parts, "<user>\n");
|
|
76
|
+
pushContent(parts, m.content);
|
|
77
|
+
pushText(parts, "\n</user>");
|
|
78
|
+
}
|
|
60
79
|
else if (m.role === "assistant") {
|
|
61
80
|
const body = m.content
|
|
62
81
|
.map((b) => {
|
|
@@ -68,13 +87,51 @@ export function renderTranscript(messages) {
|
|
|
68
87
|
})
|
|
69
88
|
.filter((s) => s.length > 0)
|
|
70
89
|
.join("\n");
|
|
71
|
-
parts
|
|
90
|
+
pushText(parts, `<assistant>\n${body}\n</assistant>`);
|
|
72
91
|
}
|
|
73
92
|
else if (m.role === "toolResult") {
|
|
74
|
-
parts
|
|
93
|
+
pushText(parts, `<tool_result id="${attr(m.toolCallId)}" name="${attr(m.toolName)}"${m.isError ? ' is_error="true"' : ""}>\n`);
|
|
94
|
+
pushContent(parts, m.content);
|
|
95
|
+
pushText(parts, "\n</tool_result>");
|
|
75
96
|
}
|
|
97
|
+
});
|
|
98
|
+
return parts;
|
|
99
|
+
}
|
|
100
|
+
/** Text of `parts` with each image replaced by `marker(n)` (n counts from 1). */
|
|
101
|
+
export function partsText(parts, marker = () => "[image]") {
|
|
102
|
+
let n = 0;
|
|
103
|
+
return parts.map((p) => (p.type === "text" ? p.text : marker(++n))).join("");
|
|
104
|
+
}
|
|
105
|
+
/** The conversation's entries as tagged blocks, in order, as text: images become `[image]`. Used where only text goes (hashing). */
|
|
106
|
+
export function renderEntries(messages) {
|
|
107
|
+
return partsText(renderEntryParts(messages));
|
|
108
|
+
}
|
|
109
|
+
/** Closes a rendered transcript (or the new entries of one) with the request for the next turn. */
|
|
110
|
+
export const NEXT_TURN_NOTE = "Write the assistant's next turn, continuing from the last entry above.";
|
|
111
|
+
/** Renders the conversation as parts. A lone user message goes as-is; anything longer becomes a tagged transcript. */
|
|
112
|
+
export function renderTranscriptParts(messages) {
|
|
113
|
+
const turns = messages.filter((m) => m.role !== "system");
|
|
114
|
+
const only = turns[0];
|
|
115
|
+
if (turns.length === 1 && only?.role === "user") {
|
|
116
|
+
const parts = [];
|
|
117
|
+
pushContent(parts, only.content);
|
|
118
|
+
return parts;
|
|
76
119
|
}
|
|
77
|
-
|
|
120
|
+
const parts = renderEntryParts(turns);
|
|
121
|
+
pushText(parts, `\n\n${NEXT_TURN_NOTE}`);
|
|
122
|
+
return parts;
|
|
123
|
+
}
|
|
124
|
+
/** Renders the conversation for stdin as text, images as `[image]`. */
|
|
125
|
+
export function renderTranscript(messages) {
|
|
126
|
+
return partsText(renderTranscriptParts(messages));
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The stdin line for `claude -p --input-format stream-json`: one user message whose content blocks are the parts in
|
|
130
|
+
* order, images as base64 sources.
|
|
131
|
+
*/
|
|
132
|
+
export function streamJsonInput(parts) {
|
|
133
|
+
const content = parts.map((p) => (p.type === "text" ? { type: "text", text: p.text } : { type: "image", source: { type: "base64", media_type: p.mimeType, data: p.data } }));
|
|
134
|
+
return `${JSON.stringify({ type: "user", message: { role: "user", content } })}\n`;
|
|
78
135
|
}
|
|
79
136
|
function plainSchema(tool) {
|
|
80
137
|
return JSON.parse(JSON.stringify(tool.parameters));
|
|
@@ -195,9 +252,7 @@ function streamClaudeP(run, model, context, options) {
|
|
|
195
252
|
let dir;
|
|
196
253
|
try {
|
|
197
254
|
dir = await mkdtemp(join(tmpdir(), "nexo-claude-p-"));
|
|
198
|
-
const args = ["-p", "--output-format", "stream-json", "--verbose", "--model", model.id, "--tools", "", "--no-session-persistence", "--setting-sources", "", "--strict-mcp-config", "--disable-slash-commands"];
|
|
199
|
-
if (!useTools)
|
|
200
|
-
args.push("--include-partial-messages");
|
|
255
|
+
const args = ["-p", "--output-format", "stream-json", "--verbose", "--include-partial-messages", "--model", model.id, "--tools", "", "--no-session-persistence", "--setting-sources", "", "--strict-mcp-config", "--disable-slash-commands"];
|
|
201
256
|
if (systemPrompt) {
|
|
202
257
|
const file = join(dir, "system-prompt.md");
|
|
203
258
|
await writeFile(file, systemPrompt, "utf8");
|
|
@@ -208,11 +263,29 @@ function streamClaudeP(run, model, context, options) {
|
|
|
208
263
|
args.push("--effort", effort);
|
|
209
264
|
if (useTools)
|
|
210
265
|
args.push("--json-schema", JSON.stringify(replySchema(tools)));
|
|
211
|
-
|
|
266
|
+
// Images go as content blocks of a stream-json user message; a text-only prompt stays plain stdin.
|
|
267
|
+
const parts = renderTranscriptParts(context.messages);
|
|
268
|
+
const withImages = parts.some((p) => p.type === "image");
|
|
269
|
+
if (withImages)
|
|
270
|
+
args.push("--input-format", "stream-json");
|
|
271
|
+
const stdin = withImages ? streamJsonInput(parts) : partsText(parts);
|
|
272
|
+
const proc = run({ args, stdin, cwd: dir, ...(signal ? { signal } : {}) });
|
|
212
273
|
stream.push({ type: "start", partial: snapshot() });
|
|
213
274
|
let result;
|
|
214
275
|
/** Open streamed block per API content index, for the current API message. */
|
|
215
276
|
const open = new Map();
|
|
277
|
+
// A tools turn arrives as one StructuredOutput call; its `text` field is shown while the call's JSON is still
|
|
278
|
+
// being written. Only the first call streams: a second one (the CLI asking again) would repeat it.
|
|
279
|
+
const replyText = useTools ? new ReplyTextExtractor() : undefined;
|
|
280
|
+
let replyStarted = false;
|
|
281
|
+
let replyIndex;
|
|
282
|
+
let replyBlock;
|
|
283
|
+
const closeReply = () => {
|
|
284
|
+
if (replyBlock === undefined)
|
|
285
|
+
return;
|
|
286
|
+
stream.push({ type: "text_end", contentIndex: replyBlock, content: partial.content[replyBlock].text, partial: snapshot() });
|
|
287
|
+
replyBlock = undefined;
|
|
288
|
+
};
|
|
216
289
|
for await (const line of proc.lines) {
|
|
217
290
|
if (!line.trim())
|
|
218
291
|
continue;
|
|
@@ -227,9 +300,33 @@ function streamClaudeP(run, model, context, options) {
|
|
|
227
300
|
result = event;
|
|
228
301
|
continue;
|
|
229
302
|
}
|
|
230
|
-
if (
|
|
303
|
+
if (event.type !== "stream_event" || event.parent_tool_use_id || !event.event)
|
|
231
304
|
continue;
|
|
232
305
|
const e = event.event;
|
|
306
|
+
if (replyText) {
|
|
307
|
+
if (e.type === "content_block_start" && e.content_block?.type === "tool_use" && e.content_block.name === "StructuredOutput") {
|
|
308
|
+
if (replyIndex === undefined && !replyStarted)
|
|
309
|
+
replyIndex = e.index;
|
|
310
|
+
replyStarted = true;
|
|
311
|
+
}
|
|
312
|
+
else if (e.type === "content_block_delta" && e.index === replyIndex && e.delta?.type === "input_json_delta" && e.delta.partial_json) {
|
|
313
|
+
const delta = replyText.push(e.delta.partial_json);
|
|
314
|
+
if (delta) {
|
|
315
|
+
if (replyBlock === undefined) {
|
|
316
|
+
replyBlock = partial.content.length;
|
|
317
|
+
partial.content.push({ type: "text", text: "" });
|
|
318
|
+
stream.push({ type: "text_start", contentIndex: replyBlock, partial: snapshot() });
|
|
319
|
+
}
|
|
320
|
+
partial.content[replyBlock].text += delta;
|
|
321
|
+
stream.push({ type: "text_delta", contentIndex: replyBlock, delta, partial: snapshot() });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else if (e.type === "content_block_stop" && e.index === replyIndex) {
|
|
325
|
+
closeReply();
|
|
326
|
+
replyIndex = undefined;
|
|
327
|
+
}
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
233
330
|
if (e.type === "message_start")
|
|
234
331
|
open.clear();
|
|
235
332
|
else if (e.type === "content_block_start" && e.index !== undefined) {
|
|
@@ -278,8 +375,17 @@ function streamClaudeP(run, model, context, options) {
|
|
|
278
375
|
partial.content = partial.content.filter((b) => b.type !== "thinking" || b.thinking.length > 0);
|
|
279
376
|
let stopReason = result?.stop_reason === "max_tokens" ? "length" : "stop";
|
|
280
377
|
if (useTools) {
|
|
378
|
+
closeReply();
|
|
281
379
|
const { text, calls } = parseStructured(result?.structured_output);
|
|
282
|
-
|
|
380
|
+
// The structured output is the answer. Text that streamed is normally the same; where it is not, the block takes it.
|
|
381
|
+
const streamed = partial.content.findIndex((b) => b.type === "text");
|
|
382
|
+
if (streamed >= 0) {
|
|
383
|
+
if (text)
|
|
384
|
+
partial.content[streamed].text = text;
|
|
385
|
+
else
|
|
386
|
+
partial.content.splice(streamed, 1);
|
|
387
|
+
}
|
|
388
|
+
else if (text) {
|
|
283
389
|
const contentIndex = partial.content.length;
|
|
284
390
|
partial.content.push({ type: "text", text: "" });
|
|
285
391
|
stream.push({ type: "text_start", contentIndex, partial: snapshot() });
|
|
@@ -324,7 +430,7 @@ function streamClaudeP(run, model, context, options) {
|
|
|
324
430
|
* Runs Claude through the local Claude Code CLI (`claude -p`), authenticated by whatever login the CLI has (a Claude
|
|
325
431
|
* subscription or its own API key), so no nexo key is needed. Claude's built-in tools are disabled; when the request
|
|
326
432
|
* carries nexo tools, the reply is constrained by `--json-schema` to `{ text, tool_calls }` and returned as real tool
|
|
327
|
-
* calls
|
|
433
|
+
* calls; the reply's `text` streams as it is written.
|
|
328
434
|
*/
|
|
329
435
|
export function claudePProvider(options = {}) {
|
|
330
436
|
const run = options.run ?? spawnRunner(options.command ?? process.env[CLAUDE_P_COMMAND_ENV] ?? "claude");
|
|
@@ -3,7 +3,7 @@ import { tmpdir } from "node:os";
|
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { calculateCost, createAssistantMessageEventStream, createProvider, getCurrentSystemPrompt, getCurrentTools, } from "@earendil-works/pi-ai";
|
|
5
5
|
import { getBuiltinModels } from "@earendil-works/pi-ai/providers/all";
|
|
6
|
-
import {
|
|
6
|
+
import { partsText, renderTranscriptParts } from "./claude-p.js";
|
|
7
7
|
import { spawnGroup } from "./process-runner.js";
|
|
8
8
|
/** Provider id: models are referenced as `codex-p/<model>`, e.g. `codex-p/gpt-6-astra`. */
|
|
9
9
|
export const CODEX_P_PROVIDER = "codex-p";
|
|
@@ -54,9 +54,11 @@ function codexPModels() {
|
|
|
54
54
|
api: CODEX_P_API,
|
|
55
55
|
provider: CODEX_P_PROVIDER,
|
|
56
56
|
baseUrl: "",
|
|
57
|
-
input: ["text"],
|
|
57
|
+
input: ["text", "image"],
|
|
58
58
|
}));
|
|
59
59
|
}
|
|
60
|
+
/** File extension for an attached image's type. */
|
|
61
|
+
const IMAGE_EXTENSION = { "image/png": "png", "image/jpeg": "jpg", "image/gif": "gif", "image/webp": "webp" };
|
|
60
62
|
function plainSchema(tool) {
|
|
61
63
|
return JSON.parse(JSON.stringify(tool.parameters));
|
|
62
64
|
}
|
|
@@ -267,8 +269,19 @@ function streamCodexP(run, getCatalog, model, context, options) {
|
|
|
267
269
|
await writeFile(file, JSON.stringify(codexReplySchema(tools)), "utf8");
|
|
268
270
|
args.push("--output-schema", file);
|
|
269
271
|
}
|
|
272
|
+
// Images are attached as files with --image (in the order they appear); the transcript marks where each one was.
|
|
273
|
+
const parts = renderTranscriptParts(context.messages);
|
|
274
|
+
let imageCount = 0;
|
|
275
|
+
for (const part of parts) {
|
|
276
|
+
if (part.type !== "image")
|
|
277
|
+
continue;
|
|
278
|
+
const file = join(dir, `image-${++imageCount}.${IMAGE_EXTENSION[part.mimeType] ?? "png"}`);
|
|
279
|
+
await writeFile(file, Buffer.from(part.data, "base64"));
|
|
280
|
+
args.push(`--image=${file}`);
|
|
281
|
+
}
|
|
270
282
|
args.push("-");
|
|
271
|
-
const
|
|
283
|
+
const prompt = partsText(parts, (n) => `[attached image ${n} of ${imageCount}]`);
|
|
284
|
+
const proc = run({ args, stdin: prompt, cwd: dir, ...(signal ? { signal } : {}) });
|
|
272
285
|
stream.push({ type: "start", partial: snapshot() });
|
|
273
286
|
let rawUsage;
|
|
274
287
|
let completed = false;
|
|
@@ -7,7 +7,14 @@ import { join } from "node:path";
|
|
|
7
7
|
import { createInterface } from "node:readline";
|
|
8
8
|
import { createAssistantMessageEventStream, createProvider, getCurrentSystemPrompt, getCurrentTools, } from "@earendil-works/pi-ai";
|
|
9
9
|
import { getBuiltinModels } from "@earendil-works/pi-ai/providers/all";
|
|
10
|
-
import {
|
|
10
|
+
import { NEXT_TURN_NOTE, pushText, renderEntries, renderEntryParts, renderTranscriptParts } from "./claude-p.js";
|
|
11
|
+
import { ReplyTextExtractor } from "./reply-text.js";
|
|
12
|
+
/** Entries a kept session has not seen yet, closed with the request for the next turn. */
|
|
13
|
+
function newEntries(turns) {
|
|
14
|
+
const parts = renderEntryParts(turns);
|
|
15
|
+
pushText(parts, `\n\n${NEXT_TURN_NOTE}`);
|
|
16
|
+
return parts;
|
|
17
|
+
}
|
|
11
18
|
/** Provider id: models are referenced as `kiro-p/<kiro model id>`, e.g. `kiro-p/claude-sonnet-5.5` or `kiro-p/auto`. */
|
|
12
19
|
export const KIRO_P_PROVIDER = "kiro-p";
|
|
13
20
|
const KIRO_P_API = "kiro-p";
|
|
@@ -15,6 +22,10 @@ const KIRO_P_API = "kiro-p";
|
|
|
15
22
|
export const KIRO_P_COMMAND_ENV = "NEXO_KIRO_P_COMMAND";
|
|
16
23
|
/** Environment variable overriding how long a turn may stay silent before it is cancelled (milliseconds). */
|
|
17
24
|
export const KIRO_P_IDLE_ENV = "NEXO_KIRO_P_IDLE_MS";
|
|
25
|
+
/** Environment variable that, set to `1` or `0`, turns session reuse (see `SessionCache`) on or off whatever the config says. */
|
|
26
|
+
export const KIRO_P_REUSE_ENV = "NEXO_KIRO_P_REUSE_SESSION";
|
|
27
|
+
/** Request `metadata` key carrying the config's `session.reuseKiroSession` for this call (a boolean). */
|
|
28
|
+
export const KIRO_P_REUSE_META = "nexoKiroReuseSession";
|
|
18
29
|
const IDLE_MS_DEFAULT = 120_000;
|
|
19
30
|
/** How long a cancelled turn gets to settle before the whole process is considered stuck and killed. */
|
|
20
31
|
const CANCEL_GRACE_MS = 5_000;
|
|
@@ -130,7 +141,7 @@ function kiroPModels() {
|
|
|
130
141
|
api: KIRO_P_API,
|
|
131
142
|
provider: KIRO_P_PROVIDER,
|
|
132
143
|
baseUrl: "",
|
|
133
|
-
input: ["text"],
|
|
144
|
+
input: ["text", "image"],
|
|
134
145
|
contextWindow,
|
|
135
146
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
136
147
|
};
|
|
@@ -398,8 +409,51 @@ class AcpConnection {
|
|
|
398
409
|
void rm(this.dir, { recursive: true, force: true }).catch(() => { });
|
|
399
410
|
}
|
|
400
411
|
}
|
|
412
|
+
/** Why a tools turn ended without a usable reply; the engine retries it a few times (see `isFormatFailure`). */
|
|
413
|
+
export const KIRO_P_BAD_REPLY = "reply was not a JSON object in the required format";
|
|
414
|
+
/** True when a reply that did not parse was an attempt at the JSON format rather than a plain-prose answer. */
|
|
415
|
+
function looksLikeReplyAttempt(text) {
|
|
416
|
+
return /"tool_calls"\s*:/.test(text) || /^\s*(```|\{)/.test(text);
|
|
417
|
+
}
|
|
418
|
+
/** Sessions kept open at once; the oldest is forgotten first. */
|
|
419
|
+
const REUSE_MAX = 16;
|
|
420
|
+
function digestOf(messages) {
|
|
421
|
+
return createHash("sha256").update(renderEntries(messages)).digest("hex");
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Sessions left open after a turn, so the next model call of the same conversation sends only its new entries instead
|
|
425
|
+
* of the whole transcript (Kiro's time to first output grows with the prompt: about 10s at 30k characters, 55s at 1M).
|
|
426
|
+
* A session is matched by what it has seen, not by a run id: the call's conversation must start with exactly the
|
|
427
|
+
* entries the session holds. Each session is handed to one call at a time.
|
|
428
|
+
*/
|
|
429
|
+
class SessionCache {
|
|
430
|
+
items = [];
|
|
431
|
+
/** Removes and returns the session that already holds a strict prefix of `turns`, the longest such prefix first. */
|
|
432
|
+
take(key, turns) {
|
|
433
|
+
const digests = new Map();
|
|
434
|
+
let best;
|
|
435
|
+
for (const item of this.items) {
|
|
436
|
+
if (item.key !== key || item.session.conn.closed || item.count >= turns.length || (best && item.count <= best.count))
|
|
437
|
+
continue;
|
|
438
|
+
let digest = digests.get(item.count);
|
|
439
|
+
if (digest === undefined) {
|
|
440
|
+
digest = digestOf(turns.slice(0, item.count));
|
|
441
|
+
digests.set(item.count, digest);
|
|
442
|
+
}
|
|
443
|
+
if (digest === item.digest)
|
|
444
|
+
best = item;
|
|
445
|
+
}
|
|
446
|
+
this.items = this.items.filter((item) => item !== best && !item.session.conn.closed);
|
|
447
|
+
return best;
|
|
448
|
+
}
|
|
449
|
+
put(item) {
|
|
450
|
+
this.items.push(item);
|
|
451
|
+
if (this.items.length > REUSE_MAX)
|
|
452
|
+
this.items.shift();
|
|
453
|
+
}
|
|
454
|
+
}
|
|
401
455
|
const RETRY_NOTE = "Your previous reply was not a single valid JSON object in the required format. Reply again with only that JSON object.";
|
|
402
|
-
function streamKiroP(getConn, model, context, options) {
|
|
456
|
+
function streamKiroP(getConn, model, context, options, sessions) {
|
|
403
457
|
const stream = createAssistantMessageEventStream();
|
|
404
458
|
const partial = {
|
|
405
459
|
role: "assistant",
|
|
@@ -458,10 +512,16 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
458
512
|
// Still not applied: refuse to run rather than let the model see Kiro's default tools.
|
|
459
513
|
throw new Error(`stopped: agent "${agentName}" not applied (mode ${mode ?? "none"})`);
|
|
460
514
|
};
|
|
461
|
-
/**
|
|
462
|
-
|
|
515
|
+
/**
|
|
516
|
+
* One prompt on a session. Thinking and text stream into `partial` as they arrive; on a tools turn the text shown
|
|
517
|
+
* is the reply's `text` field, read out of the JSON while it is still being written (`streamText` off: none).
|
|
518
|
+
*/
|
|
519
|
+
const runOnce = async (session, input, streamText = true) => {
|
|
520
|
+
// ACP content blocks: text, and images as base64 (Kiro advertises promptCapabilities.image).
|
|
521
|
+
const prompt = typeof input === "string" ? [{ type: "text", text: input }] : input.map((p) => (p.type === "text" ? { type: "text", text: p.text } : { type: "image", data: p.data, mimeType: p.mimeType }));
|
|
463
522
|
const { conn, id } = session;
|
|
464
523
|
const outcome = { text: "" };
|
|
524
|
+
const replyText = useTools ? new ReplyTextExtractor() : undefined;
|
|
465
525
|
// A native tool call means the model was offered tools after all; Kiro would answer "not available" and loop.
|
|
466
526
|
let nativeCall;
|
|
467
527
|
let stalled = false;
|
|
@@ -497,12 +557,13 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
497
557
|
return;
|
|
498
558
|
}
|
|
499
559
|
const kind = u.sessionUpdate === "agent_message_chunk" ? "text" : u.sessionUpdate === "agent_thought_chunk" ? "thinking" : undefined;
|
|
500
|
-
const
|
|
501
|
-
if (!kind || !
|
|
560
|
+
const chunk = u.content?.type === "text" ? u.content.text : undefined;
|
|
561
|
+
if (!kind || !chunk)
|
|
502
562
|
return;
|
|
503
563
|
if (kind === "text")
|
|
504
|
-
outcome.text +=
|
|
505
|
-
|
|
564
|
+
outcome.text += chunk;
|
|
565
|
+
const delta = kind === "text" && replyText ? (streamText ? replyText.push(chunk) : "") : chunk;
|
|
566
|
+
if (!delta)
|
|
506
567
|
return;
|
|
507
568
|
if (open?.kind !== kind) {
|
|
508
569
|
close();
|
|
@@ -529,7 +590,7 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
529
590
|
arm();
|
|
530
591
|
try {
|
|
531
592
|
const settled = await Promise.race([
|
|
532
|
-
conn.request("session/prompt", { sessionId: id, prompt
|
|
593
|
+
conn.request("session/prompt", { sessionId: id, prompt }).then((r) => ({ r: r }), (e) => ({ e })),
|
|
533
594
|
aborted,
|
|
534
595
|
]);
|
|
535
596
|
close();
|
|
@@ -559,11 +620,19 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
559
620
|
};
|
|
560
621
|
try {
|
|
561
622
|
stream.push({ type: "start", partial: snapshot() });
|
|
562
|
-
const
|
|
563
|
-
|
|
623
|
+
const turns = context.messages.filter((m) => m.role !== "system");
|
|
624
|
+
const key = `${agentName}|${model.id}|${effort ?? ""}`;
|
|
625
|
+
const prompt = renderTranscriptParts(context.messages);
|
|
626
|
+
const reused = sessions?.take(key, turns);
|
|
627
|
+
let session = reused?.session ?? (await startSession());
|
|
564
628
|
if (signal?.aborted)
|
|
565
629
|
return fail("aborted", "Request was aborted");
|
|
566
|
-
let outcome = await runOnce(session, prompt);
|
|
630
|
+
let outcome = await runOnce(session, reused ? newEntries(turns.slice(reused.count)) : prompt);
|
|
631
|
+
// A kept session that fails before anything streamed is dropped for a fresh one holding the whole transcript.
|
|
632
|
+
if (reused && outcome.error && !outcome.aborted && !signal?.aborted && partial.content.length === 0) {
|
|
633
|
+
session = await startSession();
|
|
634
|
+
outcome = await runOnce(session, prompt);
|
|
635
|
+
}
|
|
567
636
|
// The shared server died under this turn before anything streamed: start a fresh one and run it again, once.
|
|
568
637
|
if (outcome.lost && !signal?.aborted && partial.content.length === 0) {
|
|
569
638
|
session = await startSession();
|
|
@@ -574,21 +643,36 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
574
643
|
if (outcome.error)
|
|
575
644
|
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
576
645
|
let stopReason = outcome.stopReason === "max_tokens" ? "length" : "stop";
|
|
646
|
+
let corrected = false;
|
|
577
647
|
if (useTools) {
|
|
578
648
|
let reply = parseReply(outcome.text, tools);
|
|
579
649
|
if (!reply) {
|
|
580
|
-
|
|
581
|
-
|
|
650
|
+
corrected = true;
|
|
651
|
+
// Same session, so the model still has the transcript: only the correction is sent. Text the first
|
|
652
|
+
// attempt already streamed is not streamed again.
|
|
653
|
+
outcome = await runOnce(session, RETRY_NOTE, !partial.content.some((b) => b.type === "text"));
|
|
582
654
|
if (outcome.aborted || signal?.aborted)
|
|
583
655
|
return fail("aborted", "Request was aborted");
|
|
584
656
|
if (outcome.error)
|
|
585
657
|
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
586
658
|
reply = parseReply(outcome.text, tools);
|
|
587
659
|
}
|
|
588
|
-
// Still not the
|
|
660
|
+
// Still not the format. A botched attempt at it (JSON naming an unknown action, a truncated object) is an
|
|
661
|
+
// error the engine retries: shown as the answer it reads as a run that did nothing. Plain prose is kept.
|
|
662
|
+
if (!reply && looksLikeReplyAttempt(outcome.text))
|
|
663
|
+
return fail("error", `kiro-cli failed: ${KIRO_P_BAD_REPLY}`);
|
|
589
664
|
const { text, calls } = reply ?? { text: outcome.text, calls: [] };
|
|
590
|
-
|
|
591
|
-
|
|
665
|
+
// The parsed reply is the answer. Text that streamed is normally the same; where it is not (the model wrote
|
|
666
|
+
// the object twice and the last one won) the block takes the parsed text.
|
|
667
|
+
const streamed = partial.content.findIndex((b) => b.type === "text");
|
|
668
|
+
if (streamed < 0) {
|
|
669
|
+
if (text)
|
|
670
|
+
pushText(text);
|
|
671
|
+
}
|
|
672
|
+
else if (text)
|
|
673
|
+
partial.content[streamed].text = text;
|
|
674
|
+
else
|
|
675
|
+
partial.content.splice(streamed, 1);
|
|
592
676
|
for (const call of calls) {
|
|
593
677
|
const contentIndex = partial.content.length;
|
|
594
678
|
partial.content.push({ ...call, arguments: {} });
|
|
@@ -603,6 +687,8 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
603
687
|
pushText(outcome.text);
|
|
604
688
|
}
|
|
605
689
|
const message = { ...snapshot(), stopReason };
|
|
690
|
+
if (sessions && !corrected && stopReason !== "length")
|
|
691
|
+
sessions.put({ session, key, count: turns.length + 1, digest: digestOf([...turns, message]) });
|
|
606
692
|
stream.push({ type: "done", reason: stopReason, message });
|
|
607
693
|
stream.end(message);
|
|
608
694
|
}
|
|
@@ -620,7 +706,7 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
620
706
|
* per-call CLI start-up. Each turn is a fresh session whose agent carries the system prompt (one agent file per
|
|
621
707
|
* prompt hash), no tools and a policy denying every capability, so Kiro's request carries no tool at all; a native
|
|
622
708
|
* tool call cancels the turn. Nexo tools are described in the prompt and the reply parsed as `{ text, tool_calls }`
|
|
623
|
-
* (one correction retry when it does not parse)
|
|
709
|
+
* (one correction retry when it does not parse); the reply's `text` streams as it is written. A turn silent for
|
|
624
710
|
* `$NEXO_KIRO_P_IDLE_MS` (120s) is cancelled; a server that dies is replaced on the next call, and a turn it dropped
|
|
625
711
|
* is re-run once. Kiro bills in credits and reports no token counts, so usage is empty.
|
|
626
712
|
*/
|
|
@@ -641,9 +727,17 @@ export function kiroPProvider(options = {}) {
|
|
|
641
727
|
});
|
|
642
728
|
return opened;
|
|
643
729
|
};
|
|
730
|
+
const sessions = new SessionCache();
|
|
731
|
+
/** The environment variable wins, then the call's own switch (the engine passes the config's), then the provider option. */
|
|
732
|
+
const reuse = (opts) => {
|
|
733
|
+
const env = process.env[KIRO_P_REUSE_ENV];
|
|
734
|
+
const meta = opts?.metadata?.[KIRO_P_REUSE_META];
|
|
735
|
+
const on = env === "1" || env === "0" ? env === "1" : typeof meta === "boolean" ? meta : (options.reuseSessions ?? false);
|
|
736
|
+
return on ? sessions : undefined;
|
|
737
|
+
};
|
|
644
738
|
const api = {
|
|
645
|
-
stream: (model, context, opts) => streamKiroP(getConn, model, context, opts),
|
|
646
|
-
streamSimple: (model, context, opts) => streamKiroP(getConn, model, context, opts),
|
|
739
|
+
stream: (model, context, opts) => streamKiroP(getConn, model, context, opts, reuse(opts)),
|
|
740
|
+
streamSimple: (model, context, opts) => streamKiroP(getConn, model, context, opts, reuse(opts)),
|
|
647
741
|
};
|
|
648
742
|
return createProvider({
|
|
649
743
|
id: KIRO_P_PROVIDER,
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const SIMPLE_ESCAPES = { n: "\n", t: "\t", r: "\r", b: "\b", f: "\f", '"': '"', "\\": "\\", "/": "/" };
|
|
2
|
+
/** Longest key kept while scanning; anything longer cannot be `text`. */
|
|
3
|
+
const KEY_CHARS = 16;
|
|
4
|
+
/**
|
|
5
|
+
* Reads a `{ "text": string, "tool_calls": [...] }` reply as it streams and returns the decoded characters of the
|
|
6
|
+
* top-level `text` value as they arrive, so a tools turn can show what the model is saying before the reply is whole.
|
|
7
|
+
* Text before the object (prose, a code fence) is skipped, as is an object that closes without a `text` string. Only
|
|
8
|
+
* the first `text` value is read: a reply the model writes twice streams once.
|
|
9
|
+
*/
|
|
10
|
+
export class ReplyTextExtractor {
|
|
11
|
+
mode = "seek";
|
|
12
|
+
depth = 0;
|
|
13
|
+
/** Inside `string`: the previous character was a backslash. */
|
|
14
|
+
escaped = false;
|
|
15
|
+
/** The depth-1 string being read, and the last one that closed (a key when a colon follows). */
|
|
16
|
+
current = "";
|
|
17
|
+
lastString = null;
|
|
18
|
+
/** A `"text":` key was just read; the next value is the one to stream if it is a string. */
|
|
19
|
+
wantText = false;
|
|
20
|
+
/** Inside `text`: a partial escape sequence carried over from the previous chunk (`\` or `\u12`). */
|
|
21
|
+
escape = "";
|
|
22
|
+
/** True once the `text` value has closed. */
|
|
23
|
+
get done() {
|
|
24
|
+
return this.mode === "done";
|
|
25
|
+
}
|
|
26
|
+
/** Feeds the next piece of the reply and returns the characters of `text` it completed (often empty). */
|
|
27
|
+
push(chunk) {
|
|
28
|
+
let out = "";
|
|
29
|
+
for (const ch of chunk) {
|
|
30
|
+
switch (this.mode) {
|
|
31
|
+
case "done":
|
|
32
|
+
return out;
|
|
33
|
+
case "seek":
|
|
34
|
+
if (ch === "{") {
|
|
35
|
+
this.mode = "object";
|
|
36
|
+
this.depth = 1;
|
|
37
|
+
this.lastString = null;
|
|
38
|
+
this.wantText = false;
|
|
39
|
+
}
|
|
40
|
+
break;
|
|
41
|
+
case "object":
|
|
42
|
+
if (ch === '"') {
|
|
43
|
+
if (this.wantText && this.depth === 1) {
|
|
44
|
+
this.mode = "text";
|
|
45
|
+
this.escape = "";
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.mode = "string";
|
|
49
|
+
this.escaped = false;
|
|
50
|
+
this.current = "";
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
else if (ch === "{" || ch === "[") {
|
|
54
|
+
this.depth++;
|
|
55
|
+
this.wantText = false;
|
|
56
|
+
}
|
|
57
|
+
else if (ch === "}" || ch === "]") {
|
|
58
|
+
this.depth--;
|
|
59
|
+
if (this.depth === 0)
|
|
60
|
+
this.mode = "seek";
|
|
61
|
+
}
|
|
62
|
+
else if (ch === ":") {
|
|
63
|
+
if (this.depth === 1 && this.lastString === "text")
|
|
64
|
+
this.wantText = true;
|
|
65
|
+
this.lastString = null;
|
|
66
|
+
}
|
|
67
|
+
else if (!/\s/.test(ch)) {
|
|
68
|
+
// A number, literal or comma: whatever follows is not the `text` string.
|
|
69
|
+
this.wantText = false;
|
|
70
|
+
this.lastString = null;
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
case "string":
|
|
74
|
+
if (this.escaped)
|
|
75
|
+
this.escaped = false;
|
|
76
|
+
else if (ch === "\\")
|
|
77
|
+
this.escaped = true;
|
|
78
|
+
else if (ch === '"') {
|
|
79
|
+
this.mode = "object";
|
|
80
|
+
this.lastString = this.depth === 1 ? this.current : null;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
if (this.depth === 1 && this.current.length <= KEY_CHARS)
|
|
84
|
+
this.current += ch;
|
|
85
|
+
break;
|
|
86
|
+
case "text":
|
|
87
|
+
if (this.escape) {
|
|
88
|
+
this.escape += ch;
|
|
89
|
+
if (this.escape[1] === "u") {
|
|
90
|
+
if (this.escape.length < 6)
|
|
91
|
+
break;
|
|
92
|
+
const code = Number.parseInt(this.escape.slice(2), 16);
|
|
93
|
+
out += Number.isNaN(code) ? "" : String.fromCharCode(code);
|
|
94
|
+
}
|
|
95
|
+
else
|
|
96
|
+
out += SIMPLE_ESCAPES[ch] ?? ch;
|
|
97
|
+
this.escape = "";
|
|
98
|
+
}
|
|
99
|
+
else if (ch === "\\")
|
|
100
|
+
this.escape = ch;
|
|
101
|
+
else if (ch === '"')
|
|
102
|
+
this.mode = "done";
|
|
103
|
+
else
|
|
104
|
+
out += ch;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
}
|