@hexdecimal16/nexo 0.4.11 → 0.4.13
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/core/src/config/schema.js +3 -1
- package/core/src/engine/errors.js +10 -2
- package/core/src/engine/provider-gate.js +5 -3
- package/core/src/engine/run.js +69 -6
- package/core/src/providers/claude-p.js +64 -14
- package/core/src/providers/kiro-p.js +108 -18
- package/core/src/providers/reply-text.js +110 -0
- package/core/src/settings/config-edit.js +6 -0
- package/package.json +1 -1
- package/tui/src/app.js +20 -3
- package/tui/src/components.js +73 -0
- package/web/public/lib/span-view.ts +4 -1
|
@@ -79,6 +79,8 @@ export const ConfigSchema = Type.Object({
|
|
|
79
79
|
compactAt: Type.Number({ exclusiveMinimum: 0, maximum: 1 }),
|
|
80
80
|
/** Also compact when one S2 request with the history would cost more than this share of the turn's budget; 0 turns it off. */
|
|
81
81
|
compactAtBudgetShare: Type.Number({ minimum: 0, maximum: 1 }),
|
|
82
|
+
/** kiro-p keeps a run's Kiro session open between model calls and sends only the new entries (see `SessionCache`). */
|
|
83
|
+
reuseKiroSession: Type.Boolean(),
|
|
82
84
|
}, { additionalProperties: false }),
|
|
83
85
|
/** Guards against a run that loops: a cap on tool calls and a limit on identical consecutive calls; 0 turns a guard off. */
|
|
84
86
|
loop: Type.Object({ maxToolCalls: Type.Integer({ minimum: 0 }), repeatLimit: Type.Integer({ minimum: 0 }) }, { additionalProperties: false }),
|
|
@@ -181,7 +183,7 @@ export const DEFAULT_CONFIG = {
|
|
|
181
183
|
escalationCutoff: 0.8,
|
|
182
184
|
estimateOutputTokens: 4096,
|
|
183
185
|
},
|
|
184
|
-
session: { compactAt: 0.8, compactAtBudgetShare: 0.2 },
|
|
186
|
+
session: { compactAt: 0.8, compactAtBudgetShare: 0.2, reuseKiroSession: false },
|
|
185
187
|
loop: { maxToolCalls: 200, repeatLimit: 3 },
|
|
186
188
|
spawn: { maxParallelSubagents: 4, maxDepth: 4 },
|
|
187
189
|
instructions: [],
|
|
@@ -46,11 +46,19 @@ const MAX_RETRY_ATTEMPTS = 200;
|
|
|
46
46
|
// Transient failures pi's classifier does not know: Kiro's capacity message and the kiro-p idle stall.
|
|
47
47
|
const EXTRA_TRANSIENT = /high traffic|try again|no output for \d+\s*s/i;
|
|
48
48
|
const NOT_TRANSIENT = /quota|billing|usage limit/i;
|
|
49
|
-
|
|
49
|
+
// The model answered outside the reply format: it called a tool natively, or its JSON reply could not be used.
|
|
50
|
+
const FORMAT_FAILURE = /native tool call|reply was not a JSON object/i;
|
|
51
|
+
/** Retries a streak of format failures gets: the same request usually comes back right, but a model that keeps derailing must not loop. */
|
|
52
|
+
export const FORMAT_RETRIES = 2;
|
|
53
|
+
/** Whether a failed model call ended on a reply the provider could not use, as opposed to a transport or capacity error. */
|
|
54
|
+
export function isFormatFailure(message) {
|
|
55
|
+
return message.stopReason === "error" && FORMAT_FAILURE.test(message.errorMessage ?? "");
|
|
56
|
+
}
|
|
57
|
+
/** Whether a failed model call is worth retrying: pi's transient patterns, the kiro-cli ones above and format failures. */
|
|
50
58
|
export function isRetryableProviderMessage(message) {
|
|
51
59
|
if (message.stopReason !== "error" || !message.errorMessage)
|
|
52
60
|
return false;
|
|
53
|
-
if (isRetryableAssistantError(message))
|
|
61
|
+
if (isRetryableAssistantError(message) || isFormatFailure(message))
|
|
54
62
|
return true;
|
|
55
63
|
return EXTRA_TRANSIENT.test(message.errorMessage) && !NOT_TRANSIENT.test(message.errorMessage);
|
|
56
64
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Semaphore } from "./semaphore.js";
|
|
2
2
|
/**
|
|
3
3
|
* Wraps Models.streamSimple so each provider never exceeds its configured number of concurrent requests. When the
|
|
4
|
-
* request carries no `apiKey`, the key from `resolveApiKey` is passed as the request's `apiKey` option
|
|
4
|
+
* request carries no `apiKey`, the key from `resolveApiKey` is passed as the request's `apiKey` option, and
|
|
5
|
+
* `metadataFor`'s entries are added under the request's own `metadata`.
|
|
5
6
|
*/
|
|
6
|
-
export function createGatedStreamFn(models, limits, resolveApiKey) {
|
|
7
|
+
export function createGatedStreamFn(models, limits, resolveApiKey, metadataFor) {
|
|
7
8
|
const semaphores = new Map();
|
|
8
9
|
const semaphoreFor = (provider) => {
|
|
9
10
|
const limit = limits[provider];
|
|
@@ -21,7 +22,8 @@ export function createGatedStreamFn(models, limits, resolveApiKey) {
|
|
|
21
22
|
const release = sem ? await sem.acquire(options?.signal) : () => { };
|
|
22
23
|
try {
|
|
23
24
|
const apiKey = options?.apiKey ?? resolveApiKey?.(model.provider);
|
|
24
|
-
const
|
|
25
|
+
const metadata = metadataFor?.(model.provider);
|
|
26
|
+
const stream = models.streamSimple(model, context, apiKey === undefined && !metadata ? options : { ...options, ...(apiKey === undefined ? {} : { apiKey }), ...(metadata ? { metadata: { ...metadata, ...options?.metadata } } : {}) });
|
|
25
27
|
stream.result().then(release, release);
|
|
26
28
|
return stream;
|
|
27
29
|
}
|
package/core/src/engine/run.js
CHANGED
|
@@ -4,7 +4,7 @@ import { createModelRegistry, DEFAULT_ROUTER_POLICY, ESCALATION_BLOCK_REASONS, e
|
|
|
4
4
|
import { createLoadSkillTool, formatSkillIndex, parseSkillFile } from "../../../skills/src/index.js";
|
|
5
5
|
import { createArtifactSet, enqueueLearnJob, getActiveArtifact, insertRun, isPidAlive, listPlaybookItems, listSpans, newId, recoverCrashedRuns, resolveSourcedArtifact, RunDir, sessionRunDirs, updateRun, } from "../../../store/src/index.js";
|
|
6
6
|
import { NexoError, toErrorPayload } from "../../../telemetry/src/index.js";
|
|
7
|
-
import { builtinToolDescriptions, createAskUserTool, createBuiltinTools, createChangeTracker, createPolicy, createWebSearchTool, FileLeases, SEARCH_PROVIDER_IDS, TOOL_OUTPUT_PREFIX, } from "../../../tools/src/index.js";
|
|
7
|
+
import { builtinToolDescriptions, createAskUserTool, createBuiltinTools, createChangeTracker, createPolicy, createWebSearchTool, FileLeases, isReadOnlyCommand, matchesSessionRule, SEARCH_PROVIDER_IDS, TOOL_OUTPUT_PREFIX, } from "../../../tools/src/index.js";
|
|
8
8
|
import { realpathSync, statSync } from "node:fs";
|
|
9
9
|
import { join } from "node:path";
|
|
10
10
|
import { createApprovalClassifier } from "../approval/classifier.js";
|
|
@@ -12,12 +12,13 @@ import { budgetExhaustedError, checkAdmission, recordSpend, runBudgetLimit } fro
|
|
|
12
12
|
import { budgetLine, RunBudget } from "../cost/budget.js";
|
|
13
13
|
import { estimateCallCostUsd, priceUsage } from "../cost/pricing.js";
|
|
14
14
|
import { clearOldToolResults } from "./context-trim.js";
|
|
15
|
-
import { classifyProviderError, contextOverflowError, isRetryableProviderMessage, providerRetryDelayMs, toolErrorText, withToolErrorCodes, withinRetryLimits } from "./errors.js";
|
|
15
|
+
import { classifyProviderError, contextOverflowError, FORMAT_RETRIES, isFormatFailure, isRetryableProviderMessage, providerRetryDelayMs, toolErrorText, withToolErrorCodes, withinRetryLimits, } from "./errors.js";
|
|
16
16
|
import { assistantText, contentText, estimatePromptChars, foreignToolCallsAsText, lastAssistant, recordedContent, withBudgetLine } from "./messages.js";
|
|
17
17
|
import { DEFAULT_PLANNER_PROMPT, DEFAULT_SYSTEM_PROMPT, renderSystemPrompt } from "./prompts.js";
|
|
18
18
|
import { loadProjectInstructions } from "../session/project-instructions.js";
|
|
19
19
|
import { projectScope } from "../session/project-root.js";
|
|
20
20
|
import { environmentBlock } from "./environment.js";
|
|
21
|
+
import { KIRO_P_PROVIDER, KIRO_P_REUSE_META } from "../providers/kiro-p.js";
|
|
21
22
|
import { createGatedStreamFn } from "./provider-gate.js";
|
|
22
23
|
import { Recorder } from "./recorder.js";
|
|
23
24
|
import { activePlaybook, emptyMemoryContext, playbookSection, retrieveRunMemories, skillAppendix } from "../learn/context.js";
|
|
@@ -25,6 +26,8 @@ import { continuationPrompt } from "../scoring/continuation.js";
|
|
|
25
26
|
import { planScoring, runScoringRound, runSelfCheck, summarizeSpans } from "../scoring/runner.js";
|
|
26
27
|
import { judgeFamilyWarnings, loadJudgeRubric, loadRouterPolicy } from "../scoring/sources.js";
|
|
27
28
|
const TOOL_RESULT_INLINE_LIMIT = 32_768;
|
|
29
|
+
/** Approver calls that may be started ahead of their tool call at once (one assistant message with many calls). */
|
|
30
|
+
const APPROVAL_PREFETCH_MAX = 6;
|
|
28
31
|
const TOOL_RESULT_EXCERPT_CHARS = 8_000;
|
|
29
32
|
/** The head and tail of a large tool result, with a note pointing at the saved full text. */
|
|
30
33
|
function toolOutputExcerpt(text, pointer) {
|
|
@@ -86,7 +89,7 @@ export function createEngineEnv(deps) {
|
|
|
86
89
|
sleep: deps.sleep ?? defaultSleep,
|
|
87
90
|
registry: createModelRegistry(deps.models),
|
|
88
91
|
policy: createPolicy(deps.config.tools),
|
|
89
|
-
streamFn: createGatedStreamFn(deps.models, limits, deps.resolveApiKey),
|
|
92
|
+
streamFn: createGatedStreamFn(deps.models, limits, deps.resolveApiKey, (provider) => provider === KIRO_P_PROVIDER ? { [KIRO_P_REUSE_META]: deps.config.session.reuseKiroSession } : undefined),
|
|
90
93
|
};
|
|
91
94
|
const recovered = recoverCrashedRuns(deps.db, isPidAlive, now().toISOString());
|
|
92
95
|
if (recovered.length > 0)
|
|
@@ -413,6 +416,10 @@ export async function executeRun(env, input) {
|
|
|
413
416
|
let runSpan = null;
|
|
414
417
|
let turnSpan = null;
|
|
415
418
|
let llmSpan = null;
|
|
419
|
+
/** When the open model call started, and how long its first output and first answer text took (null until seen). */
|
|
420
|
+
let llmStartedAt = 0;
|
|
421
|
+
let llmFirstChunkMs = null;
|
|
422
|
+
let llmFirstTextMs = null;
|
|
416
423
|
let agent = null;
|
|
417
424
|
let budgetExceeded = false;
|
|
418
425
|
let seededCount = 0;
|
|
@@ -740,6 +747,36 @@ export async function executeRun(env, input) {
|
|
|
740
747
|
}
|
|
741
748
|
const approvedBy = new Map();
|
|
742
749
|
let classify = null;
|
|
750
|
+
/** Tool calls whose `beforeToolCall` has begun, and approver verdicts started ahead of theirs, by tool call id. */
|
|
751
|
+
const startedCalls = new Set();
|
|
752
|
+
const earlyVerdicts = new Map();
|
|
753
|
+
/**
|
|
754
|
+
* pi authorizes the tool calls of one assistant message one after another, so a message with several calls would
|
|
755
|
+
* wait for one approver call per tool in sequence. While the first is being authorized, the approver is started
|
|
756
|
+
* for the rest that will need it; each call's own authorization then picks its verdict up. Policy, session rules
|
|
757
|
+
* and the loop guards still run per call, in order, and a verdict nobody collects is only a wasted approver call.
|
|
758
|
+
*/
|
|
759
|
+
const prefetchApprovals = (message, signal) => {
|
|
760
|
+
const classifier = classify;
|
|
761
|
+
if (!classifier || !approvals || approvals.allowAll)
|
|
762
|
+
return;
|
|
763
|
+
const rules = approvals.sessionRules();
|
|
764
|
+
for (const block of message.content) {
|
|
765
|
+
if (earlyVerdicts.size >= APPROVAL_PREFETCH_MAX)
|
|
766
|
+
return;
|
|
767
|
+
if (block.type !== "toolCall" || startedCalls.has(block.id) || earlyVerdicts.has(block.id))
|
|
768
|
+
continue;
|
|
769
|
+
const args = block.arguments;
|
|
770
|
+
if (env.policy.decide(block.name, args).action !== "ask")
|
|
771
|
+
continue;
|
|
772
|
+
if (rules.some((rule) => matchesSessionRule(rule, block.name, args)))
|
|
773
|
+
continue;
|
|
774
|
+
const command = block.name === "bash" && typeof block.arguments.command === "string" ? block.arguments.command : null;
|
|
775
|
+
if (command !== null && isReadOnlyCommand(command))
|
|
776
|
+
continue;
|
|
777
|
+
earlyVerdicts.set(block.id, { signature: `${block.name}:${stableJson(args)}`, verdict: classifier(block.name, args, block.id, signal) });
|
|
778
|
+
}
|
|
779
|
+
};
|
|
743
780
|
let lastTrimCleared = 0;
|
|
744
781
|
/** completedCalls when the tool-call cap was first passed; tool calls from a later model reply end the run. */
|
|
745
782
|
let loopCapWarnedAt = null;
|
|
@@ -778,7 +815,8 @@ export async function executeRun(env, input) {
|
|
|
778
815
|
}
|
|
779
816
|
return withBudgetLine(foreignToolCallsAsText(current, active.model), budgetLine(budget));
|
|
780
817
|
},
|
|
781
|
-
beforeToolCall: async ({ toolCall, args }, signal) => {
|
|
818
|
+
beforeToolCall: async ({ toolCall, args, assistantMessage }, signal) => {
|
|
819
|
+
startedCalls.add(toolCall.id);
|
|
782
820
|
if (budget.isExhausted()) {
|
|
783
821
|
budgetExceeded = true;
|
|
784
822
|
return { block: true, reason: "BUDGET_EXCEEDED: the run budget is exhausted", terminate: true };
|
|
@@ -804,11 +842,21 @@ export async function executeRun(env, input) {
|
|
|
804
842
|
return { block: true, reason: `LOOP_REPEAT: ${toolCall.name} was called ${repeatCount} times in a row with identical arguments (loop.repeatLimit ${repeatLimit}). Change your approach or report the blocker.` };
|
|
805
843
|
}
|
|
806
844
|
const classifier = classify;
|
|
845
|
+
prefetchApprovals(assistantMessage, signal);
|
|
807
846
|
const auth = await env.policy.authorize(deps.prompter, toolCall.name, args, signal, depth > 0 ? `subagent ${runId.slice(-6)}` : undefined, approvals
|
|
808
847
|
? {
|
|
809
848
|
sessionRules: approvals.sessionRules,
|
|
810
849
|
...(approvals.addSessionRule ? { addSessionRule: approvals.addSessionRule } : {}),
|
|
811
|
-
...(classifier
|
|
850
|
+
...(classifier
|
|
851
|
+
? {
|
|
852
|
+
classify: (name, a, s) => {
|
|
853
|
+
const early = earlyVerdicts.get(toolCall.id);
|
|
854
|
+
earlyVerdicts.delete(toolCall.id);
|
|
855
|
+
// Only for the arguments it judged: validation may have changed them since.
|
|
856
|
+
return early && early.signature === `${name}:${stableJson(a)}` ? early.verdict : classifier(name, a, toolCall.id, s);
|
|
857
|
+
},
|
|
858
|
+
}
|
|
859
|
+
: {}),
|
|
812
860
|
allowAll: approvals.allowAll,
|
|
813
861
|
}
|
|
814
862
|
: undefined);
|
|
@@ -889,6 +937,9 @@ export async function executeRun(env, input) {
|
|
|
889
937
|
if (event.message.role === "assistant") {
|
|
890
938
|
if (llmSpan)
|
|
891
939
|
recorder.finish(llmSpan, { status: "error", attrs: { reason: "superseded by a new assistant message" } });
|
|
940
|
+
llmStartedAt = now().getTime();
|
|
941
|
+
llmFirstChunkMs = null;
|
|
942
|
+
llmFirstTextMs = null;
|
|
892
943
|
llmSpan = recorder.start({
|
|
893
944
|
kind: "llm_call",
|
|
894
945
|
name: active.model.id,
|
|
@@ -901,11 +952,16 @@ export async function executeRun(env, input) {
|
|
|
901
952
|
});
|
|
902
953
|
}
|
|
903
954
|
break;
|
|
904
|
-
case "message_update":
|
|
955
|
+
case "message_update": {
|
|
956
|
+
const kind = event.assistantMessageEvent.type;
|
|
957
|
+
if (kind === "text_delta" || kind === "thinking_delta" || kind === "toolcall_delta")
|
|
958
|
+
llmFirstChunkMs ??= now().getTime() - llmStartedAt;
|
|
905
959
|
if (event.assistantMessageEvent.type === "text_delta") {
|
|
960
|
+
llmFirstTextMs ??= now().getTime() - llmStartedAt;
|
|
906
961
|
deps.onEvent?.({ type: "text_delta", runId, depth, delta: event.assistantMessageEvent.delta });
|
|
907
962
|
}
|
|
908
963
|
break;
|
|
964
|
+
}
|
|
909
965
|
case "message_end": {
|
|
910
966
|
if (event.message.role !== "assistant" || !llmSpan)
|
|
911
967
|
break;
|
|
@@ -929,6 +985,9 @@ export async function executeRun(env, input) {
|
|
|
929
985
|
stopReason: msg.stopReason,
|
|
930
986
|
errorMessage: msg.errorMessage,
|
|
931
987
|
responseModel: msg.responseModel,
|
|
988
|
+
// Time to the first streamed output (thinking included) and to the first answer text; absent when nothing streamed.
|
|
989
|
+
firstChunkMs: llmFirstChunkMs ?? undefined,
|
|
990
|
+
firstTextMs: llmFirstTextMs ?? undefined,
|
|
932
991
|
content: contentRef ? undefined : content,
|
|
933
992
|
},
|
|
934
993
|
});
|
|
@@ -993,6 +1052,7 @@ export async function executeRun(env, input) {
|
|
|
993
1052
|
// Retries are counted per streak of consecutive failures: a successful call since the last retry starts over.
|
|
994
1053
|
let attempt = 0;
|
|
995
1054
|
let waitedMs = 0;
|
|
1055
|
+
let formatFailures = 0;
|
|
996
1056
|
let succeededCalls = completedCalls;
|
|
997
1057
|
for (;;) {
|
|
998
1058
|
const last = lastAssistant(runAgent.state.messages);
|
|
@@ -1001,8 +1061,11 @@ export async function executeRun(env, input) {
|
|
|
1001
1061
|
if (completedCalls !== succeededCalls) {
|
|
1002
1062
|
attempt = 0;
|
|
1003
1063
|
waitedMs = 0;
|
|
1064
|
+
formatFailures = 0;
|
|
1004
1065
|
succeededCalls = completedCalls;
|
|
1005
1066
|
}
|
|
1067
|
+
if (isFormatFailure(last) && ++formatFailures > FORMAT_RETRIES)
|
|
1068
|
+
break;
|
|
1006
1069
|
attempt++;
|
|
1007
1070
|
const providerCfg = config.providers[active.model.provider] ?? {};
|
|
1008
1071
|
if (input.signal?.aborted || budgetExceeded)
|
|
@@ -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";
|
|
@@ -47,14 +48,10 @@ function contentText(content) {
|
|
|
47
48
|
function attr(value) {
|
|
48
49
|
return value.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
49
50
|
}
|
|
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);
|
|
51
|
+
/** The conversation's entries as tagged blocks, in order; system messages are left out. */
|
|
52
|
+
export function renderEntries(messages) {
|
|
56
53
|
const parts = [];
|
|
57
|
-
for (const m of
|
|
54
|
+
for (const m of messages) {
|
|
58
55
|
if (m.role === "user")
|
|
59
56
|
parts.push(`<user>\n${contentText(m.content)}\n</user>`);
|
|
60
57
|
else if (m.role === "assistant") {
|
|
@@ -74,7 +71,17 @@ export function renderTranscript(messages) {
|
|
|
74
71
|
parts.push(`<tool_result id="${attr(m.toolCallId)}" name="${attr(m.toolName)}"${m.isError ? ' is_error="true"' : ""}>\n${contentText(m.content)}\n</tool_result>`);
|
|
75
72
|
}
|
|
76
73
|
}
|
|
77
|
-
return
|
|
74
|
+
return parts.join("\n\n");
|
|
75
|
+
}
|
|
76
|
+
/** Closes a rendered transcript (or the new entries of one) with the request for the next turn. */
|
|
77
|
+
export const NEXT_TURN_NOTE = "Write the assistant's next turn, continuing from the last entry above.";
|
|
78
|
+
/** Renders the conversation for stdin. A lone user message goes as-is; anything longer becomes a tagged transcript. */
|
|
79
|
+
export function renderTranscript(messages) {
|
|
80
|
+
const turns = messages.filter((m) => m.role !== "system");
|
|
81
|
+
const only = turns[0];
|
|
82
|
+
if (turns.length === 1 && only?.role === "user")
|
|
83
|
+
return contentText(only.content);
|
|
84
|
+
return `${renderEntries(turns)}\n\n${NEXT_TURN_NOTE}`;
|
|
78
85
|
}
|
|
79
86
|
function plainSchema(tool) {
|
|
80
87
|
return JSON.parse(JSON.stringify(tool.parameters));
|
|
@@ -195,9 +202,7 @@ function streamClaudeP(run, model, context, options) {
|
|
|
195
202
|
let dir;
|
|
196
203
|
try {
|
|
197
204
|
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");
|
|
205
|
+
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
206
|
if (systemPrompt) {
|
|
202
207
|
const file = join(dir, "system-prompt.md");
|
|
203
208
|
await writeFile(file, systemPrompt, "utf8");
|
|
@@ -213,6 +218,18 @@ function streamClaudeP(run, model, context, options) {
|
|
|
213
218
|
let result;
|
|
214
219
|
/** Open streamed block per API content index, for the current API message. */
|
|
215
220
|
const open = new Map();
|
|
221
|
+
// A tools turn arrives as one StructuredOutput call; its `text` field is shown while the call's JSON is still
|
|
222
|
+
// being written. Only the first call streams: a second one (the CLI asking again) would repeat it.
|
|
223
|
+
const replyText = useTools ? new ReplyTextExtractor() : undefined;
|
|
224
|
+
let replyStarted = false;
|
|
225
|
+
let replyIndex;
|
|
226
|
+
let replyBlock;
|
|
227
|
+
const closeReply = () => {
|
|
228
|
+
if (replyBlock === undefined)
|
|
229
|
+
return;
|
|
230
|
+
stream.push({ type: "text_end", contentIndex: replyBlock, content: partial.content[replyBlock].text, partial: snapshot() });
|
|
231
|
+
replyBlock = undefined;
|
|
232
|
+
};
|
|
216
233
|
for await (const line of proc.lines) {
|
|
217
234
|
if (!line.trim())
|
|
218
235
|
continue;
|
|
@@ -227,9 +244,33 @@ function streamClaudeP(run, model, context, options) {
|
|
|
227
244
|
result = event;
|
|
228
245
|
continue;
|
|
229
246
|
}
|
|
230
|
-
if (
|
|
247
|
+
if (event.type !== "stream_event" || event.parent_tool_use_id || !event.event)
|
|
231
248
|
continue;
|
|
232
249
|
const e = event.event;
|
|
250
|
+
if (replyText) {
|
|
251
|
+
if (e.type === "content_block_start" && e.content_block?.type === "tool_use" && e.content_block.name === "StructuredOutput") {
|
|
252
|
+
if (replyIndex === undefined && !replyStarted)
|
|
253
|
+
replyIndex = e.index;
|
|
254
|
+
replyStarted = true;
|
|
255
|
+
}
|
|
256
|
+
else if (e.type === "content_block_delta" && e.index === replyIndex && e.delta?.type === "input_json_delta" && e.delta.partial_json) {
|
|
257
|
+
const delta = replyText.push(e.delta.partial_json);
|
|
258
|
+
if (delta) {
|
|
259
|
+
if (replyBlock === undefined) {
|
|
260
|
+
replyBlock = partial.content.length;
|
|
261
|
+
partial.content.push({ type: "text", text: "" });
|
|
262
|
+
stream.push({ type: "text_start", contentIndex: replyBlock, partial: snapshot() });
|
|
263
|
+
}
|
|
264
|
+
partial.content[replyBlock].text += delta;
|
|
265
|
+
stream.push({ type: "text_delta", contentIndex: replyBlock, delta, partial: snapshot() });
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
else if (e.type === "content_block_stop" && e.index === replyIndex) {
|
|
269
|
+
closeReply();
|
|
270
|
+
replyIndex = undefined;
|
|
271
|
+
}
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
233
274
|
if (e.type === "message_start")
|
|
234
275
|
open.clear();
|
|
235
276
|
else if (e.type === "content_block_start" && e.index !== undefined) {
|
|
@@ -278,8 +319,17 @@ function streamClaudeP(run, model, context, options) {
|
|
|
278
319
|
partial.content = partial.content.filter((b) => b.type !== "thinking" || b.thinking.length > 0);
|
|
279
320
|
let stopReason = result?.stop_reason === "max_tokens" ? "length" : "stop";
|
|
280
321
|
if (useTools) {
|
|
322
|
+
closeReply();
|
|
281
323
|
const { text, calls } = parseStructured(result?.structured_output);
|
|
282
|
-
|
|
324
|
+
// The structured output is the answer. Text that streamed is normally the same; where it is not, the block takes it.
|
|
325
|
+
const streamed = partial.content.findIndex((b) => b.type === "text");
|
|
326
|
+
if (streamed >= 0) {
|
|
327
|
+
if (text)
|
|
328
|
+
partial.content[streamed].text = text;
|
|
329
|
+
else
|
|
330
|
+
partial.content.splice(streamed, 1);
|
|
331
|
+
}
|
|
332
|
+
else if (text) {
|
|
283
333
|
const contentIndex = partial.content.length;
|
|
284
334
|
partial.content.push({ type: "text", text: "" });
|
|
285
335
|
stream.push({ type: "text_start", contentIndex, partial: snapshot() });
|
|
@@ -324,7 +374,7 @@ function streamClaudeP(run, model, context, options) {
|
|
|
324
374
|
* Runs Claude through the local Claude Code CLI (`claude -p`), authenticated by whatever login the CLI has (a Claude
|
|
325
375
|
* subscription or its own API key), so no nexo key is needed. Claude's built-in tools are disabled; when the request
|
|
326
376
|
* carries nexo tools, the reply is constrained by `--json-schema` to `{ text, tool_calls }` and returned as real tool
|
|
327
|
-
* calls
|
|
377
|
+
* calls; the reply's `text` streams as it is written.
|
|
328
378
|
*/
|
|
329
379
|
export function claudePProvider(options = {}) {
|
|
330
380
|
const run = options.run ?? spawnRunner(options.command ?? process.env[CLAUDE_P_COMMAND_ENV] ?? "claude");
|
|
@@ -7,7 +7,8 @@ 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 { renderTranscript } from "./claude-p.js";
|
|
10
|
+
import { NEXT_TURN_NOTE, renderEntries, renderTranscript } from "./claude-p.js";
|
|
11
|
+
import { ReplyTextExtractor } from "./reply-text.js";
|
|
11
12
|
/** Provider id: models are referenced as `kiro-p/<kiro model id>`, e.g. `kiro-p/claude-sonnet-5.5` or `kiro-p/auto`. */
|
|
12
13
|
export const KIRO_P_PROVIDER = "kiro-p";
|
|
13
14
|
const KIRO_P_API = "kiro-p";
|
|
@@ -15,6 +16,10 @@ const KIRO_P_API = "kiro-p";
|
|
|
15
16
|
export const KIRO_P_COMMAND_ENV = "NEXO_KIRO_P_COMMAND";
|
|
16
17
|
/** Environment variable overriding how long a turn may stay silent before it is cancelled (milliseconds). */
|
|
17
18
|
export const KIRO_P_IDLE_ENV = "NEXO_KIRO_P_IDLE_MS";
|
|
19
|
+
/** Environment variable that, set to `1` or `0`, turns session reuse (see `SessionCache`) on or off whatever the config says. */
|
|
20
|
+
export const KIRO_P_REUSE_ENV = "NEXO_KIRO_P_REUSE_SESSION";
|
|
21
|
+
/** Request `metadata` key carrying the config's `session.reuseKiroSession` for this call (a boolean). */
|
|
22
|
+
export const KIRO_P_REUSE_META = "nexoKiroReuseSession";
|
|
18
23
|
const IDLE_MS_DEFAULT = 120_000;
|
|
19
24
|
/** How long a cancelled turn gets to settle before the whole process is considered stuck and killed. */
|
|
20
25
|
const CANCEL_GRACE_MS = 5_000;
|
|
@@ -370,6 +375,10 @@ class AcpConnection {
|
|
|
370
375
|
// makes it call the actions below natively ("Tool X is not available"). Denying every capability empties the
|
|
371
376
|
// request's tool list, so the model cannot emit a tool call at all.
|
|
372
377
|
permissions: { rules: [{ capability: "all", effect: "deny" }] },
|
|
378
|
+
// Kiro's server-side `consolidate_disclosures` experiment (seen from 2026-11-13) adds a `load_resource` tool tagged
|
|
379
|
+
// `@mcp-disclosure`, which the allowlist always re-admits and no capability denies; only `excludedTools` drops it.
|
|
380
|
+
// The rest are its siblings in the deferred-tool path, excluded so the next variant cannot slip through the same way.
|
|
381
|
+
excludedTools: ["load_resource", "tool_load", "tool_call", "disclose_context", "kiro_powers"],
|
|
373
382
|
mcpServers: {},
|
|
374
383
|
includeMcpJson: false,
|
|
375
384
|
includePowers: false,
|
|
@@ -394,8 +403,51 @@ class AcpConnection {
|
|
|
394
403
|
void rm(this.dir, { recursive: true, force: true }).catch(() => { });
|
|
395
404
|
}
|
|
396
405
|
}
|
|
406
|
+
/** Why a tools turn ended without a usable reply; the engine retries it a few times (see `isFormatFailure`). */
|
|
407
|
+
export const KIRO_P_BAD_REPLY = "reply was not a JSON object in the required format";
|
|
408
|
+
/** True when a reply that did not parse was an attempt at the JSON format rather than a plain-prose answer. */
|
|
409
|
+
function looksLikeReplyAttempt(text) {
|
|
410
|
+
return /"tool_calls"\s*:/.test(text) || /^\s*(```|\{)/.test(text);
|
|
411
|
+
}
|
|
412
|
+
/** Sessions kept open at once; the oldest is forgotten first. */
|
|
413
|
+
const REUSE_MAX = 16;
|
|
414
|
+
function digestOf(messages) {
|
|
415
|
+
return createHash("sha256").update(renderEntries(messages)).digest("hex");
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Sessions left open after a turn, so the next model call of the same conversation sends only its new entries instead
|
|
419
|
+
* of the whole transcript (Kiro's time to first output grows with the prompt: about 10s at 30k characters, 55s at 1M).
|
|
420
|
+
* A session is matched by what it has seen, not by a run id: the call's conversation must start with exactly the
|
|
421
|
+
* entries the session holds. Each session is handed to one call at a time.
|
|
422
|
+
*/
|
|
423
|
+
class SessionCache {
|
|
424
|
+
items = [];
|
|
425
|
+
/** Removes and returns the session that already holds a strict prefix of `turns`, the longest such prefix first. */
|
|
426
|
+
take(key, turns) {
|
|
427
|
+
const digests = new Map();
|
|
428
|
+
let best;
|
|
429
|
+
for (const item of this.items) {
|
|
430
|
+
if (item.key !== key || item.session.conn.closed || item.count >= turns.length || (best && item.count <= best.count))
|
|
431
|
+
continue;
|
|
432
|
+
let digest = digests.get(item.count);
|
|
433
|
+
if (digest === undefined) {
|
|
434
|
+
digest = digestOf(turns.slice(0, item.count));
|
|
435
|
+
digests.set(item.count, digest);
|
|
436
|
+
}
|
|
437
|
+
if (digest === item.digest)
|
|
438
|
+
best = item;
|
|
439
|
+
}
|
|
440
|
+
this.items = this.items.filter((item) => item !== best && !item.session.conn.closed);
|
|
441
|
+
return best;
|
|
442
|
+
}
|
|
443
|
+
put(item) {
|
|
444
|
+
this.items.push(item);
|
|
445
|
+
if (this.items.length > REUSE_MAX)
|
|
446
|
+
this.items.shift();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
397
449
|
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) {
|
|
450
|
+
function streamKiroP(getConn, model, context, options, sessions) {
|
|
399
451
|
const stream = createAssistantMessageEventStream();
|
|
400
452
|
const partial = {
|
|
401
453
|
role: "assistant",
|
|
@@ -454,10 +506,14 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
454
506
|
// Still not applied: refuse to run rather than let the model see Kiro's default tools.
|
|
455
507
|
throw new Error(`stopped: agent "${agentName}" not applied (mode ${mode ?? "none"})`);
|
|
456
508
|
};
|
|
457
|
-
/**
|
|
458
|
-
|
|
509
|
+
/**
|
|
510
|
+
* One prompt on a session. Thinking and text stream into `partial` as they arrive; on a tools turn the text shown
|
|
511
|
+
* is the reply's `text` field, read out of the JSON while it is still being written (`streamText` off: none).
|
|
512
|
+
*/
|
|
513
|
+
const runOnce = async (session, text, streamText = true) => {
|
|
459
514
|
const { conn, id } = session;
|
|
460
515
|
const outcome = { text: "" };
|
|
516
|
+
const replyText = useTools ? new ReplyTextExtractor() : undefined;
|
|
461
517
|
// A native tool call means the model was offered tools after all; Kiro would answer "not available" and loop.
|
|
462
518
|
let nativeCall;
|
|
463
519
|
let stalled = false;
|
|
@@ -493,12 +549,13 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
493
549
|
return;
|
|
494
550
|
}
|
|
495
551
|
const kind = u.sessionUpdate === "agent_message_chunk" ? "text" : u.sessionUpdate === "agent_thought_chunk" ? "thinking" : undefined;
|
|
496
|
-
const
|
|
497
|
-
if (!kind || !
|
|
552
|
+
const chunk = u.content?.type === "text" ? u.content.text : undefined;
|
|
553
|
+
if (!kind || !chunk)
|
|
498
554
|
return;
|
|
499
555
|
if (kind === "text")
|
|
500
|
-
outcome.text +=
|
|
501
|
-
|
|
556
|
+
outcome.text += chunk;
|
|
557
|
+
const delta = kind === "text" && replyText ? (streamText ? replyText.push(chunk) : "") : chunk;
|
|
558
|
+
if (!delta)
|
|
502
559
|
return;
|
|
503
560
|
if (open?.kind !== kind) {
|
|
504
561
|
close();
|
|
@@ -555,11 +612,19 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
555
612
|
};
|
|
556
613
|
try {
|
|
557
614
|
stream.push({ type: "start", partial: snapshot() });
|
|
615
|
+
const turns = context.messages.filter((m) => m.role !== "system");
|
|
616
|
+
const key = `${agentName}|${model.id}|${effort ?? ""}`;
|
|
558
617
|
const prompt = renderTranscript(context.messages);
|
|
559
|
-
|
|
618
|
+
const reused = sessions?.take(key, turns);
|
|
619
|
+
let session = reused?.session ?? (await startSession());
|
|
560
620
|
if (signal?.aborted)
|
|
561
621
|
return fail("aborted", "Request was aborted");
|
|
562
|
-
let outcome = await runOnce(session, prompt);
|
|
622
|
+
let outcome = await runOnce(session, reused ? `${renderEntries(turns.slice(reused.count))}\n\n${NEXT_TURN_NOTE}` : prompt);
|
|
623
|
+
// A kept session that fails before anything streamed is dropped for a fresh one holding the whole transcript.
|
|
624
|
+
if (reused && outcome.error && !outcome.aborted && !signal?.aborted && partial.content.length === 0) {
|
|
625
|
+
session = await startSession();
|
|
626
|
+
outcome = await runOnce(session, prompt);
|
|
627
|
+
}
|
|
563
628
|
// The shared server died under this turn before anything streamed: start a fresh one and run it again, once.
|
|
564
629
|
if (outcome.lost && !signal?.aborted && partial.content.length === 0) {
|
|
565
630
|
session = await startSession();
|
|
@@ -570,21 +635,36 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
570
635
|
if (outcome.error)
|
|
571
636
|
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
572
637
|
let stopReason = outcome.stopReason === "max_tokens" ? "length" : "stop";
|
|
638
|
+
let corrected = false;
|
|
573
639
|
if (useTools) {
|
|
574
640
|
let reply = parseReply(outcome.text, tools);
|
|
575
641
|
if (!reply) {
|
|
576
|
-
|
|
577
|
-
|
|
642
|
+
corrected = true;
|
|
643
|
+
// Same session, so the model still has the transcript: only the correction is sent. Text the first
|
|
644
|
+
// attempt already streamed is not streamed again.
|
|
645
|
+
outcome = await runOnce(session, RETRY_NOTE, !partial.content.some((b) => b.type === "text"));
|
|
578
646
|
if (outcome.aborted || signal?.aborted)
|
|
579
647
|
return fail("aborted", "Request was aborted");
|
|
580
648
|
if (outcome.error)
|
|
581
649
|
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
582
650
|
reply = parseReply(outcome.text, tools);
|
|
583
651
|
}
|
|
584
|
-
// Still not the
|
|
652
|
+
// Still not the format. A botched attempt at it (JSON naming an unknown action, a truncated object) is an
|
|
653
|
+
// error the engine retries: shown as the answer it reads as a run that did nothing. Plain prose is kept.
|
|
654
|
+
if (!reply && looksLikeReplyAttempt(outcome.text))
|
|
655
|
+
return fail("error", `kiro-cli failed: ${KIRO_P_BAD_REPLY}`);
|
|
585
656
|
const { text, calls } = reply ?? { text: outcome.text, calls: [] };
|
|
586
|
-
|
|
587
|
-
|
|
657
|
+
// The parsed reply is the answer. Text that streamed is normally the same; where it is not (the model wrote
|
|
658
|
+
// the object twice and the last one won) the block takes the parsed text.
|
|
659
|
+
const streamed = partial.content.findIndex((b) => b.type === "text");
|
|
660
|
+
if (streamed < 0) {
|
|
661
|
+
if (text)
|
|
662
|
+
pushText(text);
|
|
663
|
+
}
|
|
664
|
+
else if (text)
|
|
665
|
+
partial.content[streamed].text = text;
|
|
666
|
+
else
|
|
667
|
+
partial.content.splice(streamed, 1);
|
|
588
668
|
for (const call of calls) {
|
|
589
669
|
const contentIndex = partial.content.length;
|
|
590
670
|
partial.content.push({ ...call, arguments: {} });
|
|
@@ -599,6 +679,8 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
599
679
|
pushText(outcome.text);
|
|
600
680
|
}
|
|
601
681
|
const message = { ...snapshot(), stopReason };
|
|
682
|
+
if (sessions && !corrected && stopReason !== "length")
|
|
683
|
+
sessions.put({ session, key, count: turns.length + 1, digest: digestOf([...turns, message]) });
|
|
602
684
|
stream.push({ type: "done", reason: stopReason, message });
|
|
603
685
|
stream.end(message);
|
|
604
686
|
}
|
|
@@ -616,7 +698,7 @@ function streamKiroP(getConn, model, context, options) {
|
|
|
616
698
|
* per-call CLI start-up. Each turn is a fresh session whose agent carries the system prompt (one agent file per
|
|
617
699
|
* prompt hash), no tools and a policy denying every capability, so Kiro's request carries no tool at all; a native
|
|
618
700
|
* 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)
|
|
701
|
+
* (one correction retry when it does not parse); the reply's `text` streams as it is written. A turn silent for
|
|
620
702
|
* `$NEXO_KIRO_P_IDLE_MS` (120s) is cancelled; a server that dies is replaced on the next call, and a turn it dropped
|
|
621
703
|
* is re-run once. Kiro bills in credits and reports no token counts, so usage is empty.
|
|
622
704
|
*/
|
|
@@ -637,9 +719,17 @@ export function kiroPProvider(options = {}) {
|
|
|
637
719
|
});
|
|
638
720
|
return opened;
|
|
639
721
|
};
|
|
722
|
+
const sessions = new SessionCache();
|
|
723
|
+
/** The environment variable wins, then the call's own switch (the engine passes the config's), then the provider option. */
|
|
724
|
+
const reuse = (opts) => {
|
|
725
|
+
const env = process.env[KIRO_P_REUSE_ENV];
|
|
726
|
+
const meta = opts?.metadata?.[KIRO_P_REUSE_META];
|
|
727
|
+
const on = env === "1" || env === "0" ? env === "1" : typeof meta === "boolean" ? meta : (options.reuseSessions ?? false);
|
|
728
|
+
return on ? sessions : undefined;
|
|
729
|
+
};
|
|
640
730
|
const api = {
|
|
641
|
-
stream: (model, context, opts) => streamKiroP(getConn, model, context, opts),
|
|
642
|
-
streamSimple: (model, context, opts) => streamKiroP(getConn, model, context, opts),
|
|
731
|
+
stream: (model, context, opts) => streamKiroP(getConn, model, context, opts, reuse(opts)),
|
|
732
|
+
streamSimple: (model, context, opts) => streamKiroP(getConn, model, context, opts, reuse(opts)),
|
|
643
733
|
};
|
|
644
734
|
return createProvider({
|
|
645
735
|
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
|
+
}
|
|
@@ -106,6 +106,12 @@ const SETTING_LIST = [
|
|
|
106
106
|
description: "Also compact when one S2 request with the history would cost more than this share of a turn's budget (0 to 1; 0 = off)",
|
|
107
107
|
type: { kind: "number", integer: false, min: 0, max: 1 },
|
|
108
108
|
},
|
|
109
|
+
{
|
|
110
|
+
key: "session.reuseKiroSession",
|
|
111
|
+
label: "Reuse Kiro session",
|
|
112
|
+
description: "kiro-p models: keep one Kiro session per conversation and send only new messages, instead of the whole transcript on every model call (on/off; faster on long sessions)",
|
|
113
|
+
type: { kind: "boolean" },
|
|
114
|
+
},
|
|
109
115
|
{
|
|
110
116
|
key: "optimize.reflectionTier",
|
|
111
117
|
label: "Optimizer edit model",
|
package/package.json
CHANGED
package/tui/src/app.js
CHANGED
|
@@ -7,7 +7,7 @@ import { toErrorPayload } from "../../telemetry/src/index.js";
|
|
|
7
7
|
import { describeSessionRule } from "../../tools/src/index.js";
|
|
8
8
|
import { createApprovalPrompter } from "./approvals.js";
|
|
9
9
|
import { helpText, isSkillCommand, parseSlashCommand, SLASH_COMMANDS, skillSlashCommands } from "./commands.js";
|
|
10
|
-
import { Block, DynamicLine, QueuedList, SafeContainer, SafeMarkdown, SubagentLine, Ticker, ToolLine, ViewMode } from "./components.js";
|
|
10
|
+
import { Block, DynamicLine, QueuedList, SafeContainer, SafeMarkdown, SubagentLine, SubagentPanel, Ticker, ToolLine, ViewMode } from "./components.js";
|
|
11
11
|
import { createAutocompleteProvider } from "./file-autocomplete.js";
|
|
12
12
|
import { createSessionPickerScreen } from "./session-picker.js";
|
|
13
13
|
import { SettingsScreen } from "./settings-screen.js";
|
|
@@ -102,6 +102,8 @@ class InteractiveApp {
|
|
|
102
102
|
tui;
|
|
103
103
|
chat = new Container();
|
|
104
104
|
status = new Container();
|
|
105
|
+
/** Running subagents, pinned between the chat and the loader so their animation never happens above the viewport. */
|
|
106
|
+
subagentPanel = new SubagentPanel(() => Math.floor(this.terminal.rows / 2));
|
|
105
107
|
slot = new Container();
|
|
106
108
|
editor;
|
|
107
109
|
ticker = new Ticker();
|
|
@@ -158,6 +160,7 @@ class InteractiveApp {
|
|
|
158
160
|
this.prompter = createApprovalPrompter({ show: (box) => this.showApproval(box) }, { now: this.now, redact: (text) => this.settings.redact(text) });
|
|
159
161
|
const root = new SafeContainer();
|
|
160
162
|
root.addChild(this.chat);
|
|
163
|
+
root.addChild(this.subagentPanel);
|
|
161
164
|
root.addChild(this.status);
|
|
162
165
|
root.addChild(this.queueView);
|
|
163
166
|
root.addChild(this.slot);
|
|
@@ -537,6 +540,7 @@ class InteractiveApp {
|
|
|
537
540
|
line.interrupt();
|
|
538
541
|
for (const line of this.subagents.values())
|
|
539
542
|
line.interrupt();
|
|
543
|
+
this.flushSubagents();
|
|
540
544
|
this.currentText = null;
|
|
541
545
|
}
|
|
542
546
|
if (result) {
|
|
@@ -636,7 +640,7 @@ class InteractiveApp {
|
|
|
636
640
|
}
|
|
637
641
|
const line = new SubagentLine(escapeControl(oneLine(event.instruction, 60)), event.depth, this.ticker, escapeControl(`${event.taskId ?? event.runId.slice(-6)} · ${event.model}`));
|
|
638
642
|
this.subagents.set(event.runId, line);
|
|
639
|
-
this.
|
|
643
|
+
this.subagentPanel.add(line);
|
|
640
644
|
break;
|
|
641
645
|
}
|
|
642
646
|
case "run_end": {
|
|
@@ -644,6 +648,8 @@ class InteractiveApp {
|
|
|
644
648
|
return;
|
|
645
649
|
this.subagents.get(event.runId)?.finish(event.status, event.costUsd, event.error ? escapeControl(`${event.error.code}: ${oneLine(event.error.message, 120)}`) : "");
|
|
646
650
|
this.subagents.delete(event.runId);
|
|
651
|
+
if (!this.subagentPanel.anyRunning)
|
|
652
|
+
this.flushSubagents();
|
|
647
653
|
break;
|
|
648
654
|
}
|
|
649
655
|
case "llm_end":
|
|
@@ -662,7 +668,12 @@ class InteractiveApp {
|
|
|
662
668
|
break;
|
|
663
669
|
}
|
|
664
670
|
case "approval": {
|
|
665
|
-
|
|
671
|
+
// A subagent's approvals update its own row (latest reason only) so parallel subagents don't flood the chat.
|
|
672
|
+
if (event.depth > 0) {
|
|
673
|
+
this.subagents.get(event.runId)?.approved(escapeControl(oneLine(event.reason, 160)));
|
|
674
|
+
break;
|
|
675
|
+
}
|
|
676
|
+
this.chat.addChild(new Block(`auto-allowed: ${escapeControl(oneLine(event.reason, 160))}`, style.gray, " "));
|
|
666
677
|
break;
|
|
667
678
|
}
|
|
668
679
|
case "escalation_failed": {
|
|
@@ -705,6 +716,11 @@ class InteractiveApp {
|
|
|
705
716
|
}
|
|
706
717
|
this.tui.requestRender();
|
|
707
718
|
}
|
|
719
|
+
/** Moves the finished fan-out from the live panel into the chat, where its rows stay frozen. */
|
|
720
|
+
flushSubagents() {
|
|
721
|
+
for (const line of this.subagentPanel.takeFinished())
|
|
722
|
+
this.chat.addChild(line);
|
|
723
|
+
}
|
|
708
724
|
startTicker() {
|
|
709
725
|
this.stopTicker();
|
|
710
726
|
this.loader = new Loader(this.tui, style.cyan, style.gray, "working… (esc to stop)");
|
|
@@ -947,6 +963,7 @@ class InteractiveApp {
|
|
|
947
963
|
this.liveModel = null;
|
|
948
964
|
this.tools.clear();
|
|
949
965
|
this.subagents.clear();
|
|
966
|
+
this.subagentPanel.clear();
|
|
950
967
|
this.editor.setAutocompleteProvider(createAutocompleteProvider([...SLASH_COMMANDS, ...skillSlashCommands(next.skills())], next.cwd));
|
|
951
968
|
this.chat.clear();
|
|
952
969
|
this.renderHeader(next);
|
package/tui/src/components.js
CHANGED
|
@@ -147,6 +147,7 @@ export class SubagentLine {
|
|
|
147
147
|
status = "";
|
|
148
148
|
error = "";
|
|
149
149
|
tool = "";
|
|
150
|
+
approval = "";
|
|
150
151
|
toolCalls = 0;
|
|
151
152
|
endedAt = null;
|
|
152
153
|
startedAt = Date.now();
|
|
@@ -154,6 +155,8 @@ export class SubagentLine {
|
|
|
154
155
|
ticker;
|
|
155
156
|
depth;
|
|
156
157
|
label;
|
|
158
|
+
/** One line only (no current-tool, approval or error line), for when the live panel is short on rows. */
|
|
159
|
+
compact = false;
|
|
157
160
|
constructor(summary, depth, ticker, label) {
|
|
158
161
|
this.summary = summary;
|
|
159
162
|
this.depth = depth;
|
|
@@ -167,19 +170,28 @@ export class SubagentLine {
|
|
|
167
170
|
toolEnded() {
|
|
168
171
|
this.tool = "";
|
|
169
172
|
}
|
|
173
|
+
/** Keeps only the latest auto-approval reason; it replaces the previous one instead of piling up in the chat. */
|
|
174
|
+
approved(reason) {
|
|
175
|
+
this.approval = reason;
|
|
176
|
+
}
|
|
170
177
|
finish(status, costUsd, error = "") {
|
|
171
178
|
this.state = status === "succeeded" ? "ok" : "error";
|
|
172
179
|
this.status = status;
|
|
173
180
|
this.costUsd = costUsd;
|
|
174
181
|
this.error = error;
|
|
175
182
|
this.tool = "";
|
|
183
|
+
this.approval = "";
|
|
176
184
|
this.endedAt = Date.now();
|
|
177
185
|
}
|
|
186
|
+
get running() {
|
|
187
|
+
return this.state === "running";
|
|
188
|
+
}
|
|
178
189
|
interrupt() {
|
|
179
190
|
if (this.state === "running") {
|
|
180
191
|
this.state = "error";
|
|
181
192
|
this.status = "interrupted";
|
|
182
193
|
this.tool = "";
|
|
194
|
+
this.approval = "";
|
|
183
195
|
this.endedAt = Date.now();
|
|
184
196
|
}
|
|
185
197
|
}
|
|
@@ -190,14 +202,75 @@ export class SubagentLine {
|
|
|
190
202
|
const calls = this.toolCalls > 0 ? ` · ${this.toolCalls} tool${this.toolCalls === 1 ? "" : "s"}` : "";
|
|
191
203
|
const tail = this.state === "running" ? style.gray(` ${elapsed}${calls}`) : style.gray(` ${this.status} · ${elapsed}${calls} · ${usd(this.costUsd)}`);
|
|
192
204
|
const lines = [truncateToWidth(`${indent} ↳ ${mark} ${style.bold(this.label)} ${this.summary}${tail}`, width)];
|
|
205
|
+
if (this.compact)
|
|
206
|
+
return lines;
|
|
193
207
|
if (this.state === "running" && this.tool)
|
|
194
208
|
lines.push(truncateToWidth(style.gray(`${indent} ${this.tool}`), width));
|
|
209
|
+
if (this.state === "running" && this.approval)
|
|
210
|
+
lines.push(truncateToWidth(style.gray(`${indent} auto-allowed: ${this.approval}`), width));
|
|
195
211
|
if (this.state === "error" && this.error)
|
|
196
212
|
lines.push(truncateToWidth(style.red(`${indent} ${this.error}`), width));
|
|
197
213
|
return lines;
|
|
198
214
|
}
|
|
199
215
|
invalidate() { }
|
|
200
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* The subagents of the running fan-out, pinned below the chat. Their rows animate (spinner, elapsed time), and the
|
|
219
|
+
* main-screen renderer clears scrollback and redraws everything whenever a line above the viewport changes, so live rows
|
|
220
|
+
* must never sit in the chat where new output can push them off screen. Finished rows wait here until the whole wave is
|
|
221
|
+
* done and are then moved into the chat in one go (see `takeFinished`), where they never change again. The panel never
|
|
222
|
+
* renders more than `maxRows()` lines: first finished rows collapse into one line, then running rows go one line each,
|
|
223
|
+
* then the rest are counted.
|
|
224
|
+
*/
|
|
225
|
+
export class SubagentPanel {
|
|
226
|
+
lines = [];
|
|
227
|
+
maxRows;
|
|
228
|
+
constructor(maxRows) {
|
|
229
|
+
this.maxRows = maxRows;
|
|
230
|
+
}
|
|
231
|
+
add(line) {
|
|
232
|
+
this.lines.push(line);
|
|
233
|
+
}
|
|
234
|
+
get anyRunning() {
|
|
235
|
+
return this.lines.some((line) => line.running);
|
|
236
|
+
}
|
|
237
|
+
/** Removes and returns every row, in start order, expanded for the chat. */
|
|
238
|
+
takeFinished() {
|
|
239
|
+
const taken = this.lines.splice(0);
|
|
240
|
+
for (const line of taken)
|
|
241
|
+
line.compact = false;
|
|
242
|
+
return taken;
|
|
243
|
+
}
|
|
244
|
+
clear() {
|
|
245
|
+
this.lines.length = 0;
|
|
246
|
+
}
|
|
247
|
+
render(width) {
|
|
248
|
+
if (this.lines.length === 0)
|
|
249
|
+
return [];
|
|
250
|
+
const budget = Math.max(3, this.maxRows());
|
|
251
|
+
for (const line of this.lines)
|
|
252
|
+
line.compact = false;
|
|
253
|
+
const full = [""].concat(...this.lines.map((line) => line.render(width)));
|
|
254
|
+
if (full.length <= budget)
|
|
255
|
+
return full;
|
|
256
|
+
const running = this.lines.filter((line) => line.running);
|
|
257
|
+
const done = this.lines.length - running.length;
|
|
258
|
+
const out = [""];
|
|
259
|
+
if (done > 0)
|
|
260
|
+
out.push(truncateToWidth(style.gray(` ↳ ${done} subagent${done === 1 ? "" : "s"} finished`), width));
|
|
261
|
+
for (const line of running)
|
|
262
|
+
line.compact = true;
|
|
263
|
+
const room = budget - out.length;
|
|
264
|
+
const shown = running.length <= room ? running : running.slice(0, Math.max(0, room - 1));
|
|
265
|
+
for (const line of shown)
|
|
266
|
+
out.push(...line.render(width));
|
|
267
|
+
const hidden = running.length - shown.length;
|
|
268
|
+
if (hidden > 0)
|
|
269
|
+
out.push(truncateToWidth(style.gray(` ↳ … +${hidden} more running`), width));
|
|
270
|
+
return out;
|
|
271
|
+
}
|
|
272
|
+
invalidate() { }
|
|
273
|
+
}
|
|
201
274
|
/**
|
|
202
275
|
* Single-line secret entry. Shows one bullet per character and nothing else; the value is handed to `onSubmit` and
|
|
203
276
|
* dropped. It keeps no history, undo or kill ring.
|
|
@@ -55,7 +55,7 @@ function renderContentBlocks(content: unknown): SafeHtml {
|
|
|
55
55
|
})}`;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
const SHOWN_ATTRS = new Set(["content", "args", "result", "reason", "trigger", "instruction", "errorMessage"]);
|
|
58
|
+
const SHOWN_ATTRS = new Set(["content", "args", "result", "reason", "trigger", "instruction", "errorMessage", "firstChunkMs", "firstTextMs"]);
|
|
59
59
|
|
|
60
60
|
/** Fields, messages, tool input and output, and remaining attributes of one span. */
|
|
61
61
|
export function renderSpanDetail(span: Span, attrsTruncated: boolean, nowMs: number): SafeHtml {
|
|
@@ -67,6 +67,9 @@ export function renderSpanDetail(span: Span, attrsTruncated: boolean, nowMs: num
|
|
|
67
67
|
["started", span.startedAt],
|
|
68
68
|
["duration", formatDuration(durationMs(span.startedAt, span.endedAt, nowMs)) + (span.endedAt === null ? " (running)" : "")],
|
|
69
69
|
["queued", span.queuedMs === null ? null : formatDuration(span.queuedMs)],
|
|
70
|
+
// A model call's wait for its first streamed output (thinking included) and for the first text of the answer.
|
|
71
|
+
["first output", typeof a.firstChunkMs === "number" ? formatDuration(a.firstChunkMs) : null],
|
|
72
|
+
["first text", typeof a.firstTextMs === "number" ? formatDuration(a.firstTextMs) : null],
|
|
70
73
|
["model", span.model ? `${span.provider ?? "?"}/${span.model}` : null],
|
|
71
74
|
["tier", span.tier],
|
|
72
75
|
["effort", span.effort],
|