@hexdecimal16/nexo 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +75 -0
- package/README.md +75 -2
- package/cli/src/bin.js +12 -0
- package/cli/src/common.js +77 -0
- package/cli/src/doctor.js +27 -0
- package/cli/src/eval-cases.js +65 -0
- package/cli/src/eval.js +67 -0
- package/cli/src/init.js +186 -0
- package/cli/src/interactive.js +171 -0
- package/cli/src/learn.js +178 -0
- package/cli/src/main.js +261 -0
- package/cli/src/optimize.js +289 -0
- package/cli/src/prompter.js +47 -0
- package/cli/src/review.js +120 -0
- package/cli/src/runtime.js +2 -0
- package/cli/src/sessions.js +34 -0
- package/cli/src/settings.js +169 -0
- package/cli/src/ui.js +112 -0
- package/core/src/approval/classifier.js +152 -0
- package/core/src/config/load.js +283 -0
- package/core/src/config/schema.js +186 -0
- package/core/src/cost/admission.js +90 -0
- package/core/src/cost/budget.js +67 -0
- package/core/src/cost/pricing.js +53 -0
- package/core/src/engine/charged-completion.js +79 -0
- package/core/src/engine/errors.js +77 -0
- package/core/src/engine/mcp.js +11 -0
- package/core/src/engine/messages.js +86 -0
- package/core/src/engine/prompts.js +32 -0
- package/core/src/engine/provider-gate.js +33 -0
- package/core/src/engine/recorder.js +61 -0
- package/core/src/engine/run.js +1115 -0
- package/core/src/engine/semaphore.js +45 -0
- package/core/src/engine/spawn-tool.js +119 -0
- package/core/src/engine/task-graph.js +153 -0
- package/core/src/index.js +32 -0
- package/core/src/learn/caller.js +171 -0
- package/core/src/learn/context.js +112 -0
- package/core/src/learn/embedder.js +216 -0
- package/core/src/learn/job.js +196 -0
- package/core/src/learn/wiki.js +116 -0
- package/core/src/learn/worker.js +141 -0
- package/core/src/optimize/adapter.js +250 -0
- package/core/src/optimize/climb.js +338 -0
- package/core/src/optimize/mine.js +95 -0
- package/core/src/optimize/optimize.js +377 -0
- package/core/src/optimize/optimizer-run.js +67 -0
- package/core/src/optimize/reflect.js +100 -0
- package/core/src/optimize/scratchpad-wiki.js +69 -0
- package/core/src/providers/claude-p.js +363 -0
- package/core/src/providers/kiro-p.js +408 -0
- package/core/src/providers/models.js +10 -0
- package/core/src/scoring/calibration.js +31 -0
- package/core/src/scoring/continuation.js +24 -0
- package/core/src/scoring/runner.js +322 -0
- package/core/src/scoring/sources.js +88 -0
- package/core/src/session/lock.js +88 -0
- package/core/src/session/session.js +647 -0
- package/core/src/session/shell.js +26 -0
- package/core/src/session/transcript.js +86 -0
- package/core/src/settings/config-edit.js +472 -0
- package/core/src/settings/credentials.js +120 -0
- package/core/src/settings/doctor.js +103 -0
- package/core/src/settings/settings.js +265 -0
- package/mcp/src/hub.js +184 -0
- package/mcp/src/index.js +1 -0
- package/memory/src/curate.js +121 -0
- package/memory/src/index.js +4 -0
- package/memory/src/prompts.js +143 -0
- package/memory/src/retrieval.js +91 -0
- package/memory/src/similarity.js +47 -0
- package/optimize/src/engine.js +206 -0
- package/optimize/src/index.js +8 -0
- package/optimize/src/merge.js +73 -0
- package/optimize/src/novelty.js +37 -0
- package/optimize/src/pareto.js +83 -0
- package/optimize/src/proposer.js +148 -0
- package/optimize/src/report.js +107 -0
- package/optimize/src/rng.js +34 -0
- package/optimize/src/target.js +34 -0
- package/package.json +47 -4
- package/router/src/effort.js +5 -0
- package/router/src/escalation.js +171 -0
- package/router/src/index.js +4 -0
- package/router/src/registry.js +21 -0
- package/router/src/resolve.js +117 -0
- package/scorers/src/deterministic.js +194 -0
- package/scorers/src/family.js +48 -0
- package/scorers/src/index.js +6 -0
- package/scorers/src/judge.js +284 -0
- package/scorers/src/regex-worker.js +3 -0
- package/scorers/src/spec.js +147 -0
- package/scorers/src/trajectory.js +22 -0
- package/scorers/src/types.js +1 -0
- package/sdk/src/index.js +3 -0
- package/sdk/src/nexo.js +94 -0
- package/sdk/src/runtime.js +39 -0
- package/skills/bundled/browser/SKILL.md +57 -0
- package/skills/bundled/wiki/SKILL.md +73 -0
- package/skills/src/frontmatter.js +25 -0
- package/skills/src/index.js +3 -0
- package/skills/src/load-skill-tool.js +41 -0
- package/skills/src/loader.js +95 -0
- package/store/src/artifacts.js +124 -0
- package/store/src/budget-ledger.js +18 -0
- package/store/src/cost-report.js +46 -0
- package/store/src/cursor.js +35 -0
- package/store/src/db.js +83 -0
- package/store/src/errors.js +9 -0
- package/store/src/eval-cases.js +255 -0
- package/store/src/feedback.js +154 -0
- package/store/src/goldens.js +61 -0
- package/store/src/ids.js +6 -0
- package/store/src/index.js +25 -0
- package/store/src/judge-calibration.js +182 -0
- package/store/src/learn-jobs.js +145 -0
- package/store/src/memories.js +261 -0
- package/store/src/migrations/001_init.sql +81 -0
- package/store/src/migrations/002_feedback.sql +9 -0
- package/store/src/migrations/003_sessions.sql +19 -0
- package/store/src/migrations/004_scoring.sql +22 -0
- package/store/src/migrations/005_judge_calibration.sql +27 -0
- package/store/src/migrations/006_learning.sql +77 -0
- package/store/src/migrations/007_optimize.sql +55 -0
- package/store/src/migrations/008_mined_cases_and_climb.sql +46 -0
- package/store/src/mine.js +51 -0
- package/store/src/optim.js +291 -0
- package/store/src/paths.js +22 -0
- package/store/src/playbooks.js +199 -0
- package/store/src/reindex.js +143 -0
- package/store/src/run-dir.js +151 -0
- package/store/src/runs.js +310 -0
- package/store/src/scores.js +92 -0
- package/store/src/sessions.js +304 -0
- package/store/src/span-tree.js +98 -0
- package/store/src/spans.js +84 -0
- package/store/src/sql.js +13 -0
- package/store/src/workspace-copy.js +80 -0
- package/telemetry/src/errors.js +32 -0
- package/telemetry/src/index.js +3 -0
- package/telemetry/src/logger.js +39 -0
- package/telemetry/src/redact.js +106 -0
- package/tools/src/builtin.js +269 -0
- package/tools/src/change-tracker.js +57 -0
- package/tools/src/confine.js +101 -0
- package/tools/src/index.js +7 -0
- package/tools/src/leases.js +57 -0
- package/tools/src/policy.js +162 -0
- package/tools/src/read-only.js +338 -0
- package/tools/src/session-rules.js +140 -0
- package/tui/src/app.js +948 -0
- package/tui/src/approvals.js +96 -0
- package/tui/src/commands.js +38 -0
- package/tui/src/components.js +248 -0
- package/tui/src/index.js +5 -0
- package/tui/src/picker.js +78 -0
- package/tui/src/session-picker.js +100 -0
- package/tui/src/settings-screen.js +346 -0
- package/tui/src/text.js +70 -0
- package/tui/src/theme.js +44 -0
- package/web/public/cost.html +25 -0
- package/web/public/fonts/GeistMono-400.woff2 +0 -0
- package/web/public/fonts/GeistMono-600.woff2 +0 -0
- package/web/public/fonts/InstrumentSans-400.woff2 +0 -0
- package/web/public/fonts/InstrumentSans-600.woff2 +0 -0
- package/web/public/index.html +39 -0
- package/web/public/judge.html +21 -0
- package/web/public/learning.html +46 -0
- package/web/public/lib/api-types.ts +616 -0
- package/web/public/lib/common-view.ts +47 -0
- package/web/public/lib/cost-view.ts +35 -0
- package/web/public/lib/files-view.ts +90 -0
- package/web/public/lib/format.ts +35 -0
- package/web/public/lib/html.ts +51 -0
- package/web/public/lib/judge-view.ts +62 -0
- package/web/public/lib/learning-view.ts +102 -0
- package/web/public/lib/optim-view.ts +148 -0
- package/web/public/lib/pager.ts +56 -0
- package/web/public/lib/poller.ts +79 -0
- package/web/public/lib/run-view.ts +61 -0
- package/web/public/lib/runs-view.ts +87 -0
- package/web/public/lib/score-view.ts +94 -0
- package/web/public/lib/sessions-view.ts +28 -0
- package/web/public/lib/span-view.ts +90 -0
- package/web/public/lib/theme.ts +60 -0
- package/web/public/lib/timeline.ts +97 -0
- package/web/public/lib/wikis-view.ts +257 -0
- package/web/public/optim.html +30 -0
- package/web/public/pages/api.ts +60 -0
- package/web/public/pages/cost.ts +36 -0
- package/web/public/pages/judge.ts +15 -0
- package/web/public/pages/learning.ts +92 -0
- package/web/public/pages/optim.ts +68 -0
- package/web/public/pages/paging.ts +18 -0
- package/web/public/pages/run.ts +221 -0
- package/web/public/pages/runs.ts +91 -0
- package/web/public/pages/sessions.ts +25 -0
- package/web/public/pages/wikis.ts +103 -0
- package/web/public/run.html +36 -0
- package/web/public/sessions.html +28 -0
- package/web/public/style.css +288 -0
- package/web/public/wikis.html +25 -0
- package/web/src/api.js +682 -0
- package/web/src/http-errors.js +60 -0
- package/web/src/index.js +5 -0
- package/web/src/server.js +292 -0
- package/web/src/static.js +86 -0
- package/web/src/strip.js +22 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { createInterface } from "node:readline";
|
|
6
|
+
import { createAssistantMessageEventStream, createProvider, getCurrentSystemPrompt, getCurrentTools, } from "@earendil-works/pi-ai";
|
|
7
|
+
import { getBuiltinModels } from "@earendil-works/pi-ai/providers/all";
|
|
8
|
+
import { renderTranscript } from "./claude-p.js";
|
|
9
|
+
/** Provider id: models are referenced as `kiro-p/<kiro model id>`, e.g. `kiro-p/claude-sonnet-5.5` or `kiro-p/auto`. */
|
|
10
|
+
export const KIRO_P_PROVIDER = "kiro-p";
|
|
11
|
+
const KIRO_P_API = "kiro-p";
|
|
12
|
+
/** Environment variable naming the Kiro CLI executable; `kiro-cli` on the PATH otherwise. */
|
|
13
|
+
export const KIRO_P_COMMAND_ENV = "NEXO_KIRO_P_COMMAND";
|
|
14
|
+
/** `kiro-cli chat --effort` values; pi's `minimal` maps to `low`. */
|
|
15
|
+
const EFFORTS = new Set(["low", "medium", "high", "xhigh", "max"]);
|
|
16
|
+
function spawnRunner(command) {
|
|
17
|
+
return ({ args, stdin, cwd, signal, onStderr }) => {
|
|
18
|
+
const child = spawn(command, args, { cwd, stdio: ["pipe", "pipe", "pipe"], ...(signal ? { signal } : {}) });
|
|
19
|
+
let stderr = "";
|
|
20
|
+
child.stderr.setEncoding("utf8");
|
|
21
|
+
child.stderr.on("data", (chunk) => {
|
|
22
|
+
if (stderr.length < 16_384)
|
|
23
|
+
stderr += chunk;
|
|
24
|
+
onStderr?.(chunk);
|
|
25
|
+
});
|
|
26
|
+
const exit = new Promise((resolve) => {
|
|
27
|
+
child.on("error", (error) => resolve({ code: null, stderr: stderr || error.message }));
|
|
28
|
+
child.on("close", (code) => resolve({ code, stderr }));
|
|
29
|
+
});
|
|
30
|
+
child.stdin.on("error", () => { });
|
|
31
|
+
child.stdin.end(stdin);
|
|
32
|
+
return { lines: createInterface({ input: child.stdout, crlfDelay: Infinity }), exit };
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** Kiro's model ids (`kiro-cli chat --list-models`) with their context windows. Kiro bills in credits, so cost stays zero. */
|
|
36
|
+
const KIRO_MODELS = [
|
|
37
|
+
["auto", 1_000_000],
|
|
38
|
+
["claude-opus-5.5", 1_000_000],
|
|
39
|
+
["claude-sonnet-5.5", 1_000_000],
|
|
40
|
+
["claude-opus-5", 1_000_000],
|
|
41
|
+
["claude-sonnet-5", 1_000_000],
|
|
42
|
+
["claude-opus-4.8", 1_000_000],
|
|
43
|
+
["claude-opus-4.7", 1_000_000],
|
|
44
|
+
["claude-opus-4.6", 1_000_000],
|
|
45
|
+
["claude-sonnet-4.6", 1_000_000],
|
|
46
|
+
["claude-opus-4.5", 200_000],
|
|
47
|
+
["claude-sonnet-4.5", 200_000],
|
|
48
|
+
["claude-haiku-4.5", 200_000],
|
|
49
|
+
["gpt-5.6-sol", 1_000_000],
|
|
50
|
+
["gpt-5.6-terra", 1_000_000],
|
|
51
|
+
["gpt-5.6-luna", 1_000_000],
|
|
52
|
+
["deepseek-3.2", 164_000],
|
|
53
|
+
["minimax-m2.5", 196_000],
|
|
54
|
+
["minimax-m2.1", 196_000],
|
|
55
|
+
["glm-5", 200_000],
|
|
56
|
+
["qwen3-coder-next", 256_000],
|
|
57
|
+
];
|
|
58
|
+
/** Kiro's models, borrowing pi-ai's Anthropic catalog entry (by `claude-x-5.5` → `claude-x-5-5`) where there is one. */
|
|
59
|
+
function kiroPModels() {
|
|
60
|
+
const anthropic = new Map(getBuiltinModels("anthropic").map((m) => [m.id, m]));
|
|
61
|
+
return KIRO_MODELS.map(([id, contextWindow]) => {
|
|
62
|
+
const base = anthropic.get(id.replace(/\./g, "-"));
|
|
63
|
+
return {
|
|
64
|
+
...(base ?? { reasoning: id.startsWith("claude-") || id === "auto", maxTokens: 32_000 }),
|
|
65
|
+
id,
|
|
66
|
+
name: `${base?.name ?? id} (kiro-cli)`,
|
|
67
|
+
api: KIRO_P_API,
|
|
68
|
+
provider: KIRO_P_PROVIDER,
|
|
69
|
+
baseUrl: "",
|
|
70
|
+
input: ["text"],
|
|
71
|
+
contextWindow,
|
|
72
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function plainSchema(tool) {
|
|
77
|
+
return JSON.parse(JSON.stringify(tool.parameters));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* System-prompt section describing the tools and the JSON reply format. Kiro has no structured-output flag, so the
|
|
81
|
+
* format is asked for here and parsed by `parseReply`.
|
|
82
|
+
*/
|
|
83
|
+
export function kiroToolsSection(tools) {
|
|
84
|
+
const defs = tools.map((t) => JSON.stringify({ name: t.name, description: t.description, parameters: plainSchema(t) })).join("\n");
|
|
85
|
+
return [
|
|
86
|
+
"# Actions",
|
|
87
|
+
"",
|
|
88
|
+
'Every reply must be exactly one JSON object and nothing else (no prose around it, no code fence): {"text": string, "tool_calls": [{"name": string, "arguments": object}]}.',
|
|
89
|
+
"`text` is what you say to the user (may be empty). `tool_calls` lists actions for the caller to run now, with arguments matching the action's parameters.",
|
|
90
|
+
"The actions below are not tools you can invoke: never call them directly, only list them in `tool_calls`. The caller runs them and sends the results back in the next turn as <tool_result> entries. When you are finished, leave `tool_calls` empty and put the final answer in `text`.",
|
|
91
|
+
"",
|
|
92
|
+
"Available actions, one JSON definition per line:",
|
|
93
|
+
defs,
|
|
94
|
+
].join("\n");
|
|
95
|
+
}
|
|
96
|
+
let callSeq = 0;
|
|
97
|
+
function toolCallId() {
|
|
98
|
+
callSeq = (callSeq + 1) % 1_000_000;
|
|
99
|
+
return `kiro_p_${Date.now().toString(36)}_${callSeq}`;
|
|
100
|
+
}
|
|
101
|
+
/** Spans of the top-level `{...}` groups in `raw`, braces inside JSON strings ignored. */
|
|
102
|
+
function objectSpans(raw) {
|
|
103
|
+
const spans = [];
|
|
104
|
+
let depth = 0;
|
|
105
|
+
let start = -1;
|
|
106
|
+
let inString = false;
|
|
107
|
+
let escaped = false;
|
|
108
|
+
for (let i = 0; i < raw.length; i++) {
|
|
109
|
+
const ch = raw[i];
|
|
110
|
+
if (inString) {
|
|
111
|
+
if (escaped)
|
|
112
|
+
escaped = false;
|
|
113
|
+
else if (ch === "\\")
|
|
114
|
+
escaped = true;
|
|
115
|
+
else if (ch === '"')
|
|
116
|
+
inString = false;
|
|
117
|
+
}
|
|
118
|
+
else if (ch === '"' && depth > 0)
|
|
119
|
+
inString = true;
|
|
120
|
+
else if (ch === "{") {
|
|
121
|
+
if (depth === 0)
|
|
122
|
+
start = i;
|
|
123
|
+
depth++;
|
|
124
|
+
}
|
|
125
|
+
else if (ch === "}" && depth > 0) {
|
|
126
|
+
depth--;
|
|
127
|
+
if (depth === 0)
|
|
128
|
+
spans.push([start, i]);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return spans;
|
|
132
|
+
}
|
|
133
|
+
function isReply(value) {
|
|
134
|
+
return typeof value === "object" && value !== null && ("text" in value || "tool_calls" in value);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Parses a tools-turn reply: the JSON object, tolerating a code fence or stray prose around it. The model sometimes
|
|
138
|
+
* writes the object, corrects itself ("no code fence") and writes it again, so the last object that parses wins.
|
|
139
|
+
* Undefined when there is none.
|
|
140
|
+
*/
|
|
141
|
+
export function parseReply(raw, tools) {
|
|
142
|
+
let obj;
|
|
143
|
+
for (const [start, end] of objectSpans(raw).reverse()) {
|
|
144
|
+
try {
|
|
145
|
+
const candidate = JSON.parse(raw.slice(start, end + 1));
|
|
146
|
+
if (isReply(candidate)) {
|
|
147
|
+
obj = candidate;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
catch {
|
|
152
|
+
// Not JSON (a brace in prose, or a fenced block the model abandoned): try the previous span.
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (!obj)
|
|
156
|
+
return undefined;
|
|
157
|
+
const known = new Set(tools.map((t) => t.name));
|
|
158
|
+
const calls = [];
|
|
159
|
+
if (Array.isArray(obj.tool_calls)) {
|
|
160
|
+
for (const c of obj.tool_calls) {
|
|
161
|
+
if (typeof c?.name !== "string" || !known.has(c.name))
|
|
162
|
+
return undefined;
|
|
163
|
+
const args = typeof c.arguments === "object" && c.arguments !== null && !Array.isArray(c.arguments) ? c.arguments : {};
|
|
164
|
+
calls.push({ type: "toolCall", id: toolCallId(), name: c.name, arguments: args });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return { text: typeof obj.text === "string" ? obj.text : "", calls };
|
|
168
|
+
}
|
|
169
|
+
function effortFlag(reasoning) {
|
|
170
|
+
if (!reasoning)
|
|
171
|
+
return undefined;
|
|
172
|
+
const effort = reasoning === "minimal" ? "low" : reasoning;
|
|
173
|
+
return EFFORTS.has(effort) ? effort : undefined;
|
|
174
|
+
}
|
|
175
|
+
/** Kiro reports credits, not tokens, so usage carries no counts and no cost. */
|
|
176
|
+
function emptyUsage() {
|
|
177
|
+
return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } };
|
|
178
|
+
}
|
|
179
|
+
const AGENT_NAME = "nexo";
|
|
180
|
+
const RETRY_NOTE = "\n\nYour previous reply was not a single valid JSON object in the required format. Reply again with only that JSON object.";
|
|
181
|
+
function streamKiroP(run, model, context, options) {
|
|
182
|
+
const stream = createAssistantMessageEventStream();
|
|
183
|
+
const partial = {
|
|
184
|
+
role: "assistant",
|
|
185
|
+
content: [],
|
|
186
|
+
api: model.api,
|
|
187
|
+
provider: model.provider,
|
|
188
|
+
model: model.id,
|
|
189
|
+
usage: emptyUsage(),
|
|
190
|
+
stopReason: "pending",
|
|
191
|
+
timestamp: Date.now(),
|
|
192
|
+
};
|
|
193
|
+
const snapshot = () => ({ ...partial, content: [...partial.content] });
|
|
194
|
+
const fail = (reason, message) => {
|
|
195
|
+
const error = { ...snapshot(), stopReason: reason, errorMessage: message };
|
|
196
|
+
stream.push({ type: "error", reason, error });
|
|
197
|
+
stream.end(error);
|
|
198
|
+
};
|
|
199
|
+
const pushText = (text) => {
|
|
200
|
+
const contentIndex = partial.content.length;
|
|
201
|
+
partial.content.push({ type: "text", text: "" });
|
|
202
|
+
stream.push({ type: "text_start", contentIndex, partial: snapshot() });
|
|
203
|
+
partial.content[contentIndex].text = text;
|
|
204
|
+
stream.push({ type: "text_delta", contentIndex, delta: text, partial: snapshot() });
|
|
205
|
+
stream.push({ type: "text_end", contentIndex, content: text, partial: snapshot() });
|
|
206
|
+
};
|
|
207
|
+
queueMicrotask(async () => {
|
|
208
|
+
const signal = options?.signal;
|
|
209
|
+
if (signal?.aborted)
|
|
210
|
+
return fail("aborted", "Request was aborted");
|
|
211
|
+
const tools = getCurrentTools(context.messages);
|
|
212
|
+
const useTools = tools.length > 0 && options?.toolChoice !== "none";
|
|
213
|
+
const systemPrompt = [getCurrentSystemPrompt(context.messages), useTools ? kiroToolsSection(tools) : ""].filter((s) => s.length > 0).join("\n\n");
|
|
214
|
+
let dir;
|
|
215
|
+
try {
|
|
216
|
+
// The agent lives in a throwaway workspace: Kiro finds it under ./.kiro/agents, and no project files are in reach.
|
|
217
|
+
dir = await mkdtemp(join(tmpdir(), "nexo-kiro-p-"));
|
|
218
|
+
await mkdir(join(dir, ".kiro", "agents"), { recursive: true });
|
|
219
|
+
const agent = {
|
|
220
|
+
name: AGENT_NAME,
|
|
221
|
+
description: "nexo model turn",
|
|
222
|
+
prompt: systemPrompt || "You are a helpful assistant.",
|
|
223
|
+
tools: [],
|
|
224
|
+
// `tools: []` alone still leaves Kiro's skill tool in the request, which keeps the model in tool-calling mode and
|
|
225
|
+
// makes it call the actions below natively ("Tool X is not available"). Denying every capability empties the
|
|
226
|
+
// request's tool list, so the model cannot emit a tool call at all.
|
|
227
|
+
permissions: { rules: [{ capability: "all", effect: "deny" }] },
|
|
228
|
+
mcpServers: {},
|
|
229
|
+
includeMcpJson: false,
|
|
230
|
+
includePowers: false,
|
|
231
|
+
};
|
|
232
|
+
await writeFile(join(dir, ".kiro", "agents", `${AGENT_NAME}.json`), JSON.stringify(agent), "utf8");
|
|
233
|
+
const args = ["chat", "--v3", "--no-interactive", "--agent", AGENT_NAME, "--trust-tools=", "--output-format", "stream-json", "--model", model.id];
|
|
234
|
+
const effort = effortFlag(options?.reasoning);
|
|
235
|
+
if (effort)
|
|
236
|
+
args.push("--effort", effort);
|
|
237
|
+
const prompt = renderTranscript(context.messages);
|
|
238
|
+
const cwd = dir;
|
|
239
|
+
stream.push({ type: "start", partial: snapshot() });
|
|
240
|
+
/** One kiro-cli run; plain turns stream their text and thinking into `partial` as it arrives. */
|
|
241
|
+
const runOnce = async (stdin) => {
|
|
242
|
+
// A missing or rejected agent makes Kiro fall back to its default agent, which has tools: stop the run.
|
|
243
|
+
const kill = new AbortController();
|
|
244
|
+
let fellBack;
|
|
245
|
+
// A native tool call means the model was offered tools after all; Kiro would answer "not available" and loop.
|
|
246
|
+
let nativeCall;
|
|
247
|
+
const onAbort = () => kill.abort();
|
|
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
|
+
}
|
|
318
|
+
}
|
|
319
|
+
close();
|
|
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`;
|
|
327
|
+
}
|
|
328
|
+
finally {
|
|
329
|
+
signal?.removeEventListener("abort", onAbort);
|
|
330
|
+
}
|
|
331
|
+
return outcome;
|
|
332
|
+
};
|
|
333
|
+
let outcome = await runOnce(prompt);
|
|
334
|
+
if (signal?.aborted)
|
|
335
|
+
return fail("aborted", "Request was aborted");
|
|
336
|
+
if (outcome.error)
|
|
337
|
+
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
338
|
+
let stopReason = outcome.stopReason === "max_tokens" ? "length" : "stop";
|
|
339
|
+
if (useTools) {
|
|
340
|
+
let reply = parseReply(outcome.finalText, tools);
|
|
341
|
+
if (!reply) {
|
|
342
|
+
outcome = await runOnce(`${prompt}${RETRY_NOTE}`);
|
|
343
|
+
if (signal?.aborted)
|
|
344
|
+
return fail("aborted", "Request was aborted");
|
|
345
|
+
if (outcome.error)
|
|
346
|
+
return fail("error", `kiro-cli failed: ${outcome.error}`);
|
|
347
|
+
reply = parseReply(outcome.finalText, tools);
|
|
348
|
+
}
|
|
349
|
+
// 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.finalText, calls: [] };
|
|
351
|
+
if (text)
|
|
352
|
+
pushText(text);
|
|
353
|
+
for (const call of calls) {
|
|
354
|
+
const contentIndex = partial.content.length;
|
|
355
|
+
partial.content.push({ ...call, arguments: {} });
|
|
356
|
+
stream.push({ type: "toolcall_start", contentIndex, partial: snapshot() });
|
|
357
|
+
partial.content[contentIndex] = call;
|
|
358
|
+
stream.push({ type: "toolcall_delta", contentIndex, delta: JSON.stringify(call.arguments), partial: snapshot() });
|
|
359
|
+
stream.push({ type: "toolcall_end", contentIndex, toolCall: call, partial: snapshot() });
|
|
360
|
+
}
|
|
361
|
+
stopReason = calls.length > 0 ? "toolUse" : "stop";
|
|
362
|
+
}
|
|
363
|
+
else if (!partial.content.some((b) => b.type === "text") && outcome.finalText) {
|
|
364
|
+
pushText(outcome.finalText);
|
|
365
|
+
}
|
|
366
|
+
const message = { ...snapshot(), stopReason };
|
|
367
|
+
stream.push({ type: "done", reason: stopReason, message });
|
|
368
|
+
stream.end(message);
|
|
369
|
+
}
|
|
370
|
+
catch (error) {
|
|
371
|
+
if (signal?.aborted)
|
|
372
|
+
return fail("aborted", "Request was aborted");
|
|
373
|
+
fail("error", `kiro-cli failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
374
|
+
}
|
|
375
|
+
finally {
|
|
376
|
+
if (dir)
|
|
377
|
+
await rm(dir, { recursive: true, force: true }).catch(() => { });
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
return stream;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Runs models through the local Kiro CLI's v3 engine (`kiro-cli chat --v3 --no-interactive`), authenticated by the
|
|
384
|
+
* CLI's own login. Each turn gets a throwaway agent carrying the system prompt, no tools and a policy denying every
|
|
385
|
+
* capability (so Kiro's request carries no tool at all), plus `--trust-tools=` so nothing Kiro might still offer can
|
|
386
|
+
* run; a native tool call stops the run. Nexo tools are described in the prompt and the reply parsed as
|
|
387
|
+
* `{ text, tool_calls }` (one retry when it does not parse), so tool turns arrive whole rather than streamed. Kiro bills
|
|
388
|
+
* in credits and reports no token counts, so usage is empty.
|
|
389
|
+
*/
|
|
390
|
+
export function kiroPProvider(options = {}) {
|
|
391
|
+
const run = options.run ?? spawnRunner(options.command ?? process.env[KIRO_P_COMMAND_ENV] ?? "kiro-cli");
|
|
392
|
+
const api = {
|
|
393
|
+
stream: (model, context, opts) => streamKiroP(run, model, context, opts),
|
|
394
|
+
streamSimple: (model, context, opts) => streamKiroP(run, model, context, opts),
|
|
395
|
+
};
|
|
396
|
+
return createProvider({
|
|
397
|
+
id: KIRO_P_PROVIDER,
|
|
398
|
+
name: "Kiro CLI (kiro-cli chat --v3)",
|
|
399
|
+
auth: {
|
|
400
|
+
apiKey: {
|
|
401
|
+
name: "Kiro CLI login",
|
|
402
|
+
resolve: async () => ({ auth: {}, source: "kiro-cli login" }),
|
|
403
|
+
},
|
|
404
|
+
},
|
|
405
|
+
models: kiroPModels(),
|
|
406
|
+
api,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { builtinModels } from "@earendil-works/pi-ai/providers/all";
|
|
2
|
+
import { claudePProvider } from "./claude-p.js";
|
|
3
|
+
import { kiroPProvider } from "./kiro-p.js";
|
|
4
|
+
/** pi-ai's built-in providers plus nexo's own (`claude-p`, `kiro-p`). */
|
|
5
|
+
export function nexoModels() {
|
|
6
|
+
const models = builtinModels();
|
|
7
|
+
models.setProvider(claudePProvider());
|
|
8
|
+
models.setProvider(kiroPProvider());
|
|
9
|
+
return models;
|
|
10
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { agreementStats, confusionMatrix, judgeHumanPairs, listDisagreements, recordCalibration, syncJudgeDisagreement, } from "../../../store/src/index.js";
|
|
2
|
+
import { NexoError } from "../../../telemetry/src/index.js";
|
|
3
|
+
import { judgeFamilyWarnings, judgeKey } from "./sources.js";
|
|
4
|
+
/**
|
|
5
|
+
* `nexo eval judge` (spec §7.4): compares the judge's verdicts with human verdicts on the runs that have both, stores
|
|
6
|
+
* accuracy and Cohen's κ, and lists the disagreements. `judgeKey` defaults to the configured `models.judge`. Throws
|
|
7
|
+
* CONFIG_INVALID when no judge is configured and none is named.
|
|
8
|
+
*/
|
|
9
|
+
export function calibrateJudge(db, config, options) {
|
|
10
|
+
const key = options.judgeKey ?? judgeKey(config);
|
|
11
|
+
if (!key) {
|
|
12
|
+
throw new NexoError({
|
|
13
|
+
code: "CONFIG_INVALID",
|
|
14
|
+
message: "No judge is configured",
|
|
15
|
+
hint: 'Set models.judge in your nexo config, for example [{"provider":"google","id":"<model-id>"}], or pass --judge <provider/id>',
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
const pairs = judgeHumanPairs(db, key);
|
|
19
|
+
// Feedback sync only sees verdicts that existed when the human answered; this covers runs judged afterwards or reindexed.
|
|
20
|
+
for (const pair of pairs)
|
|
21
|
+
syncJudgeDisagreement(db, pair.runId, options.nowIso);
|
|
22
|
+
const matrix = confusionMatrix(pairs);
|
|
23
|
+
const calibration = agreementStats(matrix).cases > 0 ? recordCalibration(db, key, options.nowIso) : null;
|
|
24
|
+
return {
|
|
25
|
+
judgeKey: key,
|
|
26
|
+
calibration,
|
|
27
|
+
matrix,
|
|
28
|
+
disagreements: listDisagreements(db, { judgeKey: key }),
|
|
29
|
+
warnings: options.judgeKey === undefined ? judgeFamilyWarnings(config) : [],
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DATA_BLOCK_NOTE, dataBlock } from "../../../scorers/src/index.js";
|
|
2
|
+
/** Longest rationale quoted per failed check in the continuation prompt. */
|
|
3
|
+
const RATIONALE_CHARS = 1500;
|
|
4
|
+
/** The message that starts an S2 continuation turn: why the answer was rejected and what to do next. */
|
|
5
|
+
export function continuationPrompt(signals, scores) {
|
|
6
|
+
const failed = scores.filter((s) => s.status === "ok" && s.passed === false);
|
|
7
|
+
const lines = [
|
|
8
|
+
"[nexo] Your previous answer did not pass the end-of-run checks, so you continue on a stronger model.",
|
|
9
|
+
"",
|
|
10
|
+
"Why:",
|
|
11
|
+
...signals.map((s) => `- ${s.trigger}: ${s.reason}`),
|
|
12
|
+
];
|
|
13
|
+
if (failed.length > 0) {
|
|
14
|
+
// Rationales quote judge text and command output, which the agent's own work can influence.
|
|
15
|
+
lines.push("", "Failed checks:", DATA_BLOCK_NOTE);
|
|
16
|
+
for (const s of failed) {
|
|
17
|
+
const value = s.value === null ? "" : ` (score ${s.value.toFixed(2)})`;
|
|
18
|
+
const rationale = s.rationale.length > RATIONALE_CHARS ? `…${s.rationale.slice(-RATIONALE_CHARS)}` : s.rationale;
|
|
19
|
+
lines.push(`- ${s.scorerName} [${s.scorerType}]${value}:`, dataBlock("check", rationale));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
lines.push("", "Fix the problems, then give your complete final answer again.");
|
|
23
|
+
return lines.join("\n");
|
|
24
|
+
}
|