@hexdecimal16/nexo 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +75 -0
- package/README.md +75 -2
- package/cli/src/bin.js +12 -0
- package/cli/src/common.js +78 -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 +266 -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/cli/src/version.js +19 -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 +965 -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,86 @@
|
|
|
1
|
+
/** Turns kept verbatim when older turns are compacted. */
|
|
2
|
+
export const KEEP_VERBATIM_TURNS = 2;
|
|
3
|
+
/** Longest session title, in characters. */
|
|
4
|
+
export const TITLE_CHARS = 60;
|
|
5
|
+
/** Per-message cap on the text sent to the compaction call; tool results get the smaller cap. */
|
|
6
|
+
const SUMMARY_MESSAGE_CHARS = 8_000;
|
|
7
|
+
const SUMMARY_TOOL_RESULT_CHARS = 2_000;
|
|
8
|
+
export const COMPACTION_SYSTEM_PROMPT = `You compress the earlier part of a conversation between a user and nexo, an agent that works with tools in the user's workspace.
|
|
9
|
+
Write a concise summary that the agent will read instead of those turns. Keep:
|
|
10
|
+
- the user's goals, preferences and constraints;
|
|
11
|
+
- decisions made and their reasons;
|
|
12
|
+
- files created, changed or read, with their paths;
|
|
13
|
+
- important tool results and facts learned;
|
|
14
|
+
- open questions and the current state of the work.
|
|
15
|
+
Use short bullets. No preamble.`;
|
|
16
|
+
/** The user-role message that stands in for compacted turns. */
|
|
17
|
+
export function summaryMessage(summary, timestamp) {
|
|
18
|
+
return { role: "user", content: `[nexo] Summary of the earlier conversation (older turns were compacted):\n\n${summary}`, timestamp };
|
|
19
|
+
}
|
|
20
|
+
export function latestCompaction(lines) {
|
|
21
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
22
|
+
const line = lines[i];
|
|
23
|
+
if (line?.type === "compaction")
|
|
24
|
+
return line;
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
/** Message lines still in the active conversation: those after the latest compaction's replaced turns. */
|
|
29
|
+
export function activeMessageLines(lines) {
|
|
30
|
+
const through = latestCompaction(lines)?.replacedThroughTurn ?? 0;
|
|
31
|
+
return lines.filter((l) => l.type === "message" && l.turnIndex > through);
|
|
32
|
+
}
|
|
33
|
+
/** The history the next turn starts from: the latest compaction summary (if any) and every active message. */
|
|
34
|
+
export function buildHistory(lines, timestamp) {
|
|
35
|
+
const marker = latestCompaction(lines);
|
|
36
|
+
const messages = activeMessageLines(lines).map((l) => l.message);
|
|
37
|
+
return marker?.summary ? [summaryMessage(marker.summary, timestamp), ...messages] : messages;
|
|
38
|
+
}
|
|
39
|
+
/** First non-empty line of the first user message, at most TITLE_CHARS characters. */
|
|
40
|
+
export function titleFrom(instruction) {
|
|
41
|
+
const line = instruction.split("\n").map((l) => l.trim()).find((l) => l.length > 0) ?? "";
|
|
42
|
+
return line.length > TITLE_CHARS ? line.slice(0, TITLE_CHARS) : line;
|
|
43
|
+
}
|
|
44
|
+
function cap(text, max) {
|
|
45
|
+
return text.length > max ? `${text.slice(0, max)} […${text.length - max} characters cut]` : text;
|
|
46
|
+
}
|
|
47
|
+
function blockText(content) {
|
|
48
|
+
if (typeof content === "string")
|
|
49
|
+
return content;
|
|
50
|
+
return content.map((b) => (b.type === "text" ? b.text : b.type === "image" ? "[image]" : "")).join("");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Plain-text rendering of turns for the compaction call, at most `maxChars` long: when the turns do not fit, the oldest
|
|
54
|
+
* lines are dropped first (the previous summary is kept, cut to half of `maxChars` if needed).
|
|
55
|
+
*/
|
|
56
|
+
export function renderForSummary(previousSummary, messages, maxChars = Number.POSITIVE_INFINITY) {
|
|
57
|
+
const head = previousSummary ? `Summary of the conversation before these turns:\n${cap(previousSummary, Math.floor(maxChars / 2))}\n\n` : "";
|
|
58
|
+
const lines = [];
|
|
59
|
+
for (const m of messages) {
|
|
60
|
+
if (m.role === "user")
|
|
61
|
+
lines.push(`[user] ${cap(blockText(m.content), SUMMARY_MESSAGE_CHARS)}`);
|
|
62
|
+
else if (m.role === "toolResult")
|
|
63
|
+
lines.push(`[tool result ${m.toolName}${m.isError ? " (error)" : ""}] ${cap(blockText(m.content), SUMMARY_TOOL_RESULT_CHARS)}`);
|
|
64
|
+
else if (m.role === "assistant") {
|
|
65
|
+
for (const block of m.content) {
|
|
66
|
+
if (block.type === "text" && block.text)
|
|
67
|
+
lines.push(`[assistant] ${cap(block.text, SUMMARY_MESSAGE_CHARS)}`);
|
|
68
|
+
if (block.type === "toolCall")
|
|
69
|
+
lines.push(`[assistant calls ${block.name}] ${cap(JSON.stringify(block.arguments), SUMMARY_TOOL_RESULT_CHARS)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const dropped = "[earlier lines omitted]\n";
|
|
74
|
+
let label = "Turns to summarize:\n";
|
|
75
|
+
let size = head.length + label.length + lines.reduce((sum, l) => sum + l.length + 1, 0);
|
|
76
|
+
let start = 0;
|
|
77
|
+
while (size > maxChars && start < lines.length) {
|
|
78
|
+
size -= (lines[start]?.length ?? 0) + 1;
|
|
79
|
+
start++;
|
|
80
|
+
if (start === 1) {
|
|
81
|
+
label += dropped;
|
|
82
|
+
size += dropped.length;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return `${head}${label}${lines.slice(start).join("\n")}`.slice(0, Number.isFinite(maxChars) ? maxChars : undefined);
|
|
86
|
+
}
|
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { EFFORTS } from "../../../router/src/index.js";
|
|
4
|
+
import { NexoError } from "../../../telemetry/src/index.js";
|
|
5
|
+
import { configErrors, deepMerge, projectConfigPath, readConfigFile } from "../config/load.js";
|
|
6
|
+
import { DEFAULT_CONFIG } from "../config/schema.js";
|
|
7
|
+
/** Values of `boolean` and `scorerToggle` settings as shown and picked. */
|
|
8
|
+
export const ON_OFF = ["on", "off"];
|
|
9
|
+
export const PROJECT_OVERRIDE_NOTICE = "overridden by project config";
|
|
10
|
+
const POLICY_VALUES = ["allow", "ask", "deny"];
|
|
11
|
+
const USD = { kind: "number", integer: false, exclusiveMin: 0 };
|
|
12
|
+
const POLICY_DESCRIPTIONS = {
|
|
13
|
+
read: "read files in the workspace",
|
|
14
|
+
write: "create or overwrite files in the workspace",
|
|
15
|
+
edit: "edit files in the workspace",
|
|
16
|
+
bash: "run shell commands",
|
|
17
|
+
web_fetch: "fetch web pages",
|
|
18
|
+
load_skill: "load a skill's instructions",
|
|
19
|
+
spawn: "start subagents",
|
|
20
|
+
};
|
|
21
|
+
/** Every setting the settings screen and `nexo settings` can edit, in display order. */
|
|
22
|
+
export const EDITABLE_SETTINGS = [
|
|
23
|
+
{ key: "models.s1", label: "Model s1", description: "Default model for most work (provider/model-id)", type: { kind: "model" } },
|
|
24
|
+
{ key: "models.s2", label: "Model s2", description: "Stronger model the router escalates to (provider/model-id)", type: { kind: "model" } },
|
|
25
|
+
{ key: "models.s1.effort", label: "Effort s1", description: "Default reasoning effort for s1", type: { kind: "enum", values: EFFORTS } },
|
|
26
|
+
{ key: "models.s2.effort", label: "Effort s2", description: "Default reasoning effort for s2", type: { kind: "enum", values: EFFORTS } },
|
|
27
|
+
{ key: "budgets.perRunUsd", label: "Budget per run", description: "Spending cap for one run or turn, in USD", type: USD },
|
|
28
|
+
{ key: "budgets.perSessionUsd", label: "Budget per session", description: "Spending cap for one interactive session, in USD", type: USD },
|
|
29
|
+
{ key: "budgets.dailyUsd", label: "Daily budget", description: "Spending cap per UTC day, in USD", type: USD },
|
|
30
|
+
{ key: "budgets.monthlyUsd", label: "Monthly budget", description: "Spending cap per UTC month, in USD", type: USD },
|
|
31
|
+
{
|
|
32
|
+
key: "budgets.optimDefaultUsd",
|
|
33
|
+
label: "Optimization budget",
|
|
34
|
+
description: "Default spending cap for one `nexo optimize` run, in USD (--budget-usd overrides)",
|
|
35
|
+
type: USD,
|
|
36
|
+
},
|
|
37
|
+
...Object.keys(DEFAULT_CONFIG.tools.policy).map((tool) => ({
|
|
38
|
+
key: `tools.policy.${tool}`,
|
|
39
|
+
label: `Policy ${tool}`,
|
|
40
|
+
description: `Whether the agent may ${POLICY_DESCRIPTIONS[tool] ?? `use the ${tool} tool`}: allow, ask first, or deny`,
|
|
41
|
+
type: { kind: "enum", values: POLICY_VALUES },
|
|
42
|
+
})),
|
|
43
|
+
{
|
|
44
|
+
key: "tools.nonInteractiveAsk",
|
|
45
|
+
label: "Ask without a terminal",
|
|
46
|
+
description: "What an `ask` policy does when nobody can answer (nexo run without a TTY); with auto mode on, the approver may still allow some `ask` tools first",
|
|
47
|
+
type: { kind: "enum", values: ["allow", "deny"] },
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
key: "tools.autoApprove",
|
|
51
|
+
label: "Auto mode",
|
|
52
|
+
description: "An approver model allows in-scope actions that would otherwise ask (on/off); /auto overrides it per session. Also applies to `nexo run`, where it can allow `ask` tools without a human even when nonInteractiveAsk is deny",
|
|
53
|
+
type: { kind: "enum", values: ["off", "on"] },
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
key: "session.compactAt",
|
|
57
|
+
label: "Compact at",
|
|
58
|
+
description: "Share of the context window at which a session compacts older turns (0 to 1)",
|
|
59
|
+
type: { kind: "number", integer: false, exclusiveMin: 0, max: 1 },
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
key: "session.compactAtBudgetShare",
|
|
63
|
+
label: "Compact at budget share",
|
|
64
|
+
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)",
|
|
65
|
+
type: { kind: "number", integer: false, min: 0, max: 1 },
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
key: "spawn.maxParallelSubagents",
|
|
69
|
+
label: "Parallel subagents",
|
|
70
|
+
description: "Most subagents one run may have working at the same time",
|
|
71
|
+
type: { kind: "number", integer: true, min: 1 },
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
key: "models.judge",
|
|
75
|
+
label: "Judge models",
|
|
76
|
+
description: "Models that judge run quality, as provider/model-id; several joined with + form a panel (a/x+b/y)",
|
|
77
|
+
type: { kind: "models" },
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
key: "scoring.judge",
|
|
81
|
+
label: "LLM judge scoring",
|
|
82
|
+
description: "on adds an llm_judge scorer to your user config, off removes it; on needs models.judge set first",
|
|
83
|
+
type: { kind: "scorerToggle" },
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
key: "models.reflector",
|
|
87
|
+
label: "Model reflector",
|
|
88
|
+
description: "Model that writes lessons after a run: s1, s2 or provider/model-id",
|
|
89
|
+
type: { kind: "modelOrRef" },
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
key: "models.approver",
|
|
93
|
+
label: "Model approver",
|
|
94
|
+
description: "Model that decides approvals in auto mode: s1, s2 or provider/model-id (effort defaults to low)",
|
|
95
|
+
type: { kind: "modelOrRef" },
|
|
96
|
+
},
|
|
97
|
+
{ key: "models.embedding", label: "Model embedding", description: "Embedding model for memory, eval mining and optimization novelty checks (provider/model-id); its price goes in pricingOverrides in config.json", type: { kind: "model" } },
|
|
98
|
+
{ key: "learning.enabled", label: "Learning", description: "Learn lessons from judged runs (on/off)", type: { kind: "boolean" } },
|
|
99
|
+
{ key: "learning.perJobUsd", label: "Learning budget per job", description: "Spending cap for one learn job, in USD", type: USD },
|
|
100
|
+
{ key: "memory.enabled", label: "Memory", description: "Retrieve stored memories into the prompt (on/off)", type: { kind: "boolean" } },
|
|
101
|
+
{
|
|
102
|
+
key: "memory.maxTokens",
|
|
103
|
+
label: "Memory tokens",
|
|
104
|
+
description: "Most tokens of retrieved memories added to a prompt",
|
|
105
|
+
type: { kind: "number", integer: true, min: 0 },
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
key: "optimize.minCases",
|
|
109
|
+
label: "Optimization min cases",
|
|
110
|
+
description: "Fewest eval cases an optimization needs",
|
|
111
|
+
type: { kind: "number", integer: true, min: 3 },
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
key: "optimize.patience",
|
|
115
|
+
label: "Optimization patience",
|
|
116
|
+
description: "Iterations without a new Pareto member before an optimization stops",
|
|
117
|
+
type: { kind: "number", integer: true, min: 1 },
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
key: "optimize.minJudgeKappa",
|
|
121
|
+
label: "Promotion min judge κ",
|
|
122
|
+
description: "Lowest judge–human agreement (Cohen's κ) that lets judge-only evidence promote a candidate",
|
|
123
|
+
type: { kind: "number", integer: false, min: -1, max: 1 },
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
key: "optimize.noveltySimilarity",
|
|
127
|
+
label: "Optimization novelty limit",
|
|
128
|
+
description: "Embedding similarity (0.5 to 1) at or above which a reflected text is skipped as a near-duplicate of a candidate",
|
|
129
|
+
type: { kind: "number", integer: false, min: 0.5, max: 1 },
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
key: "optimize.offspringAlpha",
|
|
133
|
+
label: "Optimization offspring penalty",
|
|
134
|
+
description: "How strongly a parent's child count lowers its chance of being picked again (0 turns it off)",
|
|
135
|
+
type: { kind: "number", integer: false, min: 0, max: 10 },
|
|
136
|
+
},
|
|
137
|
+
];
|
|
138
|
+
function isPlainObject(value) {
|
|
139
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
140
|
+
}
|
|
141
|
+
function invalidRequest(message, hint, context) {
|
|
142
|
+
return new NexoError({ code: "INVALID_REQUEST", message, ...(hint ? { hint } : {}), ...(context ? { context } : {}) });
|
|
143
|
+
}
|
|
144
|
+
export function findSetting(key) {
|
|
145
|
+
const def = EDITABLE_SETTINGS.find((s) => s.key === key);
|
|
146
|
+
if (!def) {
|
|
147
|
+
throw invalidRequest(`Unknown setting "${key}"`, `Editable settings: ${EDITABLE_SETTINGS.map((s) => s.key).join(", ")}`, { key });
|
|
148
|
+
}
|
|
149
|
+
return def;
|
|
150
|
+
}
|
|
151
|
+
function segments(key) {
|
|
152
|
+
return key.split(".");
|
|
153
|
+
}
|
|
154
|
+
function getPath(obj, path) {
|
|
155
|
+
let cur = obj;
|
|
156
|
+
for (const seg of path) {
|
|
157
|
+
if (!isPlainObject(cur))
|
|
158
|
+
return undefined;
|
|
159
|
+
cur = cur[seg];
|
|
160
|
+
}
|
|
161
|
+
return cur;
|
|
162
|
+
}
|
|
163
|
+
function setPath(obj, path, value) {
|
|
164
|
+
let cur = obj;
|
|
165
|
+
for (const seg of path.slice(0, -1)) {
|
|
166
|
+
if (!isPlainObject(cur[seg]))
|
|
167
|
+
cur[seg] = {};
|
|
168
|
+
cur = cur[seg];
|
|
169
|
+
}
|
|
170
|
+
cur[path[path.length - 1]] = value;
|
|
171
|
+
}
|
|
172
|
+
/** Deletes the leaf and then every parent object left empty. */
|
|
173
|
+
function deletePath(obj, path) {
|
|
174
|
+
const parents = [obj];
|
|
175
|
+
for (const seg of path.slice(0, -1)) {
|
|
176
|
+
const next = parents[parents.length - 1]?.[seg];
|
|
177
|
+
if (!isPlainObject(next))
|
|
178
|
+
return;
|
|
179
|
+
parents.push(next);
|
|
180
|
+
}
|
|
181
|
+
delete parents[parents.length - 1]?.[path[path.length - 1]];
|
|
182
|
+
for (let i = parents.length - 1; i > 0; i--) {
|
|
183
|
+
if (Object.keys(parents[i]).length > 0)
|
|
184
|
+
break;
|
|
185
|
+
delete parents[i - 1]?.[path[i - 1]];
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/** Config paths a setting is made of: a model is its provider and id (its effort is a separate setting). */
|
|
189
|
+
function leafPaths(def) {
|
|
190
|
+
const base = segments(def.key);
|
|
191
|
+
return def.type.kind === "model" ? [[...base, "provider"], [...base, "id"]] : [base];
|
|
192
|
+
}
|
|
193
|
+
const SCORERS_PATH = ["scoring", "scorers"];
|
|
194
|
+
const JUDGE_PATH = ["models", "judge"];
|
|
195
|
+
function scorersOf(layer) {
|
|
196
|
+
const raw = getPath(layer, SCORERS_PATH);
|
|
197
|
+
return Array.isArray(raw) ? raw.filter(isPlainObject) : [];
|
|
198
|
+
}
|
|
199
|
+
function hasJudgeScorer(layer) {
|
|
200
|
+
return scorersOf(layer).some((s) => s.kind === "llm_judge");
|
|
201
|
+
}
|
|
202
|
+
function hasJudgeModels(layer) {
|
|
203
|
+
const raw = getPath(layer, JUDGE_PATH);
|
|
204
|
+
return Array.isArray(raw) && raw.length > 0;
|
|
205
|
+
}
|
|
206
|
+
function definedIn(layer, def) {
|
|
207
|
+
if (def.type.kind === "scorerToggle")
|
|
208
|
+
return hasJudgeScorer(layer);
|
|
209
|
+
return leafPaths(def).some((p) => getPath(layer, p) !== undefined);
|
|
210
|
+
}
|
|
211
|
+
function formatModel(raw) {
|
|
212
|
+
return isPlainObject(raw) && typeof raw.provider === "string" && typeof raw.id === "string" ? `${raw.provider}/${raw.id}` : null;
|
|
213
|
+
}
|
|
214
|
+
function displayValue(def, layer) {
|
|
215
|
+
if (def.type.kind === "scorerToggle")
|
|
216
|
+
return hasJudgeScorer(layer) ? "on" : "off";
|
|
217
|
+
const raw = getPath(layer, segments(def.key));
|
|
218
|
+
if (def.type.kind === "models") {
|
|
219
|
+
if (!Array.isArray(raw))
|
|
220
|
+
return null;
|
|
221
|
+
const parts = raw.map(formatModel);
|
|
222
|
+
return parts.length > 0 && parts.every((p) => p !== null) ? parts.join("+") : null;
|
|
223
|
+
}
|
|
224
|
+
if (def.type.kind === "modelOrRef") {
|
|
225
|
+
if (isPlainObject(raw) && typeof raw.ref === "string")
|
|
226
|
+
return raw.ref;
|
|
227
|
+
return formatModel(raw);
|
|
228
|
+
}
|
|
229
|
+
if (def.type.kind === "boolean")
|
|
230
|
+
return typeof raw === "boolean" ? (raw ? "on" : "off") : null;
|
|
231
|
+
if (def.type.kind === "model") {
|
|
232
|
+
if (!isPlainObject(raw))
|
|
233
|
+
return null;
|
|
234
|
+
return typeof raw.provider === "string" && typeof raw.id === "string" ? `${raw.provider}/${raw.id}` : null;
|
|
235
|
+
}
|
|
236
|
+
return typeof raw === "string" || typeof raw === "number" ? raw : null;
|
|
237
|
+
}
|
|
238
|
+
function readLayer(file) {
|
|
239
|
+
if (!existsSync(file))
|
|
240
|
+
return {};
|
|
241
|
+
const data = readConfigFile(file);
|
|
242
|
+
if (!isPlainObject(data)) {
|
|
243
|
+
throw new NexoError({
|
|
244
|
+
code: "CONFIG_INVALID",
|
|
245
|
+
message: `Config file ${file} must contain a JSON object`,
|
|
246
|
+
hint: "Fix the file so its top level is an object",
|
|
247
|
+
context: { file },
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
return data;
|
|
251
|
+
}
|
|
252
|
+
/** Reads the user and project config files (CONFIG_INVALID when one is not a JSON object). */
|
|
253
|
+
export function readConfigLayers(userFile, cwd) {
|
|
254
|
+
const projectFile = projectConfigPath(cwd);
|
|
255
|
+
return { user: readLayer(userFile), project: readLayer(projectFile), userFile, projectFile };
|
|
256
|
+
}
|
|
257
|
+
/** Defaults, user and project merged without validation. */
|
|
258
|
+
export function mergedLayers(layers) {
|
|
259
|
+
return deepMerge(DEFAULT_CONFIG, layers.user, layers.project);
|
|
260
|
+
}
|
|
261
|
+
function errorsUnder(config, pointer) {
|
|
262
|
+
return configErrors(config).filter((e) => e.path === pointer || e.path.startsWith(`${pointer}/`));
|
|
263
|
+
}
|
|
264
|
+
function overrideNotice(def, layers) {
|
|
265
|
+
const errors = errorsUnder(mergedLayers(layers), `/${segments(def.key).join("/")}`);
|
|
266
|
+
if (errors.length === 0)
|
|
267
|
+
return PROJECT_OVERRIDE_NOTICE;
|
|
268
|
+
return `${PROJECT_OVERRIDE_NOTICE}; ${layers.projectFile} has an invalid value: ${errors.map((e) => `${e.path} ${e.message}`).join("; ")}`;
|
|
269
|
+
}
|
|
270
|
+
/** Settings whose absent value still means something: the effective value and its source are `default`. */
|
|
271
|
+
const IMPLICIT_DEFAULTS = { "models.reflector": "s1", "models.approver": "s1" };
|
|
272
|
+
export function settingView(def, layers) {
|
|
273
|
+
// Project scorers replace the user's list, so any `scoring.scorers` in the project config overrides the toggle.
|
|
274
|
+
const overriddenByProject = def.type.kind === "scorerToggle" ? Array.isArray(getPath(layers.project, SCORERS_PATH)) : definedIn(layers.project, def);
|
|
275
|
+
const implicit = IMPLICIT_DEFAULTS[def.key];
|
|
276
|
+
const source = overriddenByProject
|
|
277
|
+
? "project"
|
|
278
|
+
: definedIn(layers.user, def)
|
|
279
|
+
? "user"
|
|
280
|
+
: definedIn(DEFAULT_CONFIG, def) || def.type.kind === "scorerToggle" || implicit !== undefined
|
|
281
|
+
? "default"
|
|
282
|
+
: "unset";
|
|
283
|
+
return {
|
|
284
|
+
...def,
|
|
285
|
+
value: displayValue(def, mergedLayers(layers)) ?? implicit ?? null,
|
|
286
|
+
source,
|
|
287
|
+
userValue: definedIn(layers.user, def) ? displayValue(def, layers.user) : null,
|
|
288
|
+
overriddenByProject,
|
|
289
|
+
notice: overriddenByProject ? overrideNotice(def, layers) : null,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
/** `s1`/`s2` as a role ref, anything else as `provider/id` (or a `{provider,id}` / `{ref}` object). */
|
|
293
|
+
function parseModelOrRef(key, value) {
|
|
294
|
+
if (value === "s1" || value === "s2")
|
|
295
|
+
return { ref: value };
|
|
296
|
+
if (isPlainObject(value) && (value.ref === "s1" || value.ref === "s2") && Object.keys(value).length === 1)
|
|
297
|
+
return { ref: value.ref };
|
|
298
|
+
try {
|
|
299
|
+
return parseModel(key, value);
|
|
300
|
+
}
|
|
301
|
+
catch {
|
|
302
|
+
throw invalidRequest(`Invalid value for ${key}: expected "s1", "s2" or "provider/model-id"`, 'Type s1, s2 or "provider/model-id"', { key });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/** Carries a stored `effort` over when an existing entry has the same provider and id. */
|
|
306
|
+
function keepEffort(model, existing) {
|
|
307
|
+
const old = existing.find((e) => isPlainObject(e) && "provider" in model && e.provider === model.provider && e.id === model.id);
|
|
308
|
+
return isPlainObject(old) && old.effort !== undefined ? { ...model, effort: old.effort } : model;
|
|
309
|
+
}
|
|
310
|
+
/** `provider/id` entries joined with `+` (or an array of them); at least one, none empty. */
|
|
311
|
+
function parseModels(key, value) {
|
|
312
|
+
const parts = typeof value === "string" ? value.split("+").map((p) => p.trim()) : Array.isArray(value) ? value : [];
|
|
313
|
+
if (parts.length === 0) {
|
|
314
|
+
throw invalidRequest(`Invalid value for ${key}: expected one or more "provider/model-id" joined with +`, 'For example "google/gemini-x" or "google/a+openai/b"', { key });
|
|
315
|
+
}
|
|
316
|
+
try {
|
|
317
|
+
return parts.map((p) => parseModel(key, p));
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
throw invalidRequest(`Invalid value for ${key}: expected one or more "provider/model-id" joined with +`, 'For example "google/gemini-x" or "google/a+openai/b"', { key });
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
/** `on`/`true` and `off`/`false` (any case) or a JSON boolean. */
|
|
324
|
+
function parseOnOff(key, value) {
|
|
325
|
+
if (typeof value === "boolean")
|
|
326
|
+
return value;
|
|
327
|
+
const text = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
328
|
+
if (text === "on" || text === "true")
|
|
329
|
+
return true;
|
|
330
|
+
if (text === "off" || text === "false")
|
|
331
|
+
return false;
|
|
332
|
+
const pointer = `/${segments(key).join("/")}`;
|
|
333
|
+
throw invalidRequest(`Invalid value for ${key}: ${pointer} must be on or off`, "Use on, off, true or false", { key, errors: [{ path: pointer, message: "must be on or off" }] });
|
|
334
|
+
}
|
|
335
|
+
function parseModel(key, value) {
|
|
336
|
+
if (typeof value === "string") {
|
|
337
|
+
const slash = value.indexOf("/");
|
|
338
|
+
if (slash > 0 && slash < value.length - 1)
|
|
339
|
+
return { provider: value.slice(0, slash), id: value.slice(slash + 1) };
|
|
340
|
+
}
|
|
341
|
+
else if (isPlainObject(value) && typeof value.provider === "string" && typeof value.id === "string" && value.provider && value.id) {
|
|
342
|
+
return { provider: value.provider, id: value.id };
|
|
343
|
+
}
|
|
344
|
+
throw invalidRequest(`Invalid value for ${key}: expected "provider/model-id"`, 'Pick a model from the list or type "provider/model-id"', { key });
|
|
345
|
+
}
|
|
346
|
+
/** Plain decimal numbers only: no hex, binary, exponent, separators or Infinity. */
|
|
347
|
+
const DECIMAL = /^-?(\d+(\.\d+)?|\.\d+)$/;
|
|
348
|
+
/** Converts text typed on the command line into the value type of a setting. */
|
|
349
|
+
export function parseSettingInput(def, raw) {
|
|
350
|
+
const text = raw.trim();
|
|
351
|
+
if (def.type.kind !== "number")
|
|
352
|
+
return text;
|
|
353
|
+
const value = DECIMAL.test(text) ? Number(text) : Number.NaN;
|
|
354
|
+
if (!Number.isFinite(value)) {
|
|
355
|
+
throw invalidRequest(`Invalid value for ${def.key}: "${text}" is not a number`, undefined, { key: def.key, errors: [{ path: `/${segments(def.key).join("/")}`, message: "must be a number" }] });
|
|
356
|
+
}
|
|
357
|
+
return value;
|
|
358
|
+
}
|
|
359
|
+
function writeJsonAtomic(file, data) {
|
|
360
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
361
|
+
try {
|
|
362
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
363
|
+
writeFileSync(tmp, `${JSON.stringify(data, null, 2)}\n`, { flag: "wx" });
|
|
364
|
+
renameSync(tmp, file);
|
|
365
|
+
}
|
|
366
|
+
catch (error) {
|
|
367
|
+
rmSync(tmp, { force: true });
|
|
368
|
+
throw new NexoError({
|
|
369
|
+
code: "STORE_IO",
|
|
370
|
+
message: `Cannot write config file ${file}: ${error.code ?? "unknown error"}`,
|
|
371
|
+
hint: `Check that ${dirname(file)} is writable by you`,
|
|
372
|
+
context: { file },
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
function parseJudgeToggle(key, value) {
|
|
377
|
+
if (value === "on" || value === "off")
|
|
378
|
+
return value === "on";
|
|
379
|
+
const pointer = `/${segments(key).join("/")}`;
|
|
380
|
+
throw invalidRequest(`Invalid value for ${key}: ${pointer} must be one of ${ON_OFF.join(", ")}`, undefined, { key, errors: [{ path: pointer, message: `must be one of ${ON_OFF.join(", ")}` }] });
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* `scoring.judge` edits the user config's `scoring.scorers`: on adds `{ kind: "llm_judge", name: "quality" }` unless an
|
|
384
|
+
* llm_judge scorer is there, off removes every llm_judge scorer; other scorers stay. A config with an llm_judge scorer
|
|
385
|
+
* and no `models.judge` fails to load, so on needs a judge model in the effective config.
|
|
386
|
+
*/
|
|
387
|
+
function writeJudgeToggle(def, layers, userFile, on) {
|
|
388
|
+
const user = structuredClone(layers.user);
|
|
389
|
+
const present = hasJudgeScorer(user);
|
|
390
|
+
if (on === present)
|
|
391
|
+
return settingView(def, layers);
|
|
392
|
+
if (on) {
|
|
393
|
+
if (!hasJudgeModels(user)) {
|
|
394
|
+
throw invalidRequest(`Cannot turn ${def.key} on: no judge model is set in your user config`, "Set models.judge first (for example google/<model-id>); an llm_judge scorer without one makes the config invalid", { key: def.key });
|
|
395
|
+
}
|
|
396
|
+
setPath(user, SCORERS_PATH, [...scorersOf(user), { kind: "llm_judge", name: "quality" }]);
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
const kept = scorersOf(user).filter((s) => s.kind !== "llm_judge");
|
|
400
|
+
if (kept.length > 0)
|
|
401
|
+
setPath(user, SCORERS_PATH, kept);
|
|
402
|
+
else
|
|
403
|
+
deletePath(user, SCORERS_PATH);
|
|
404
|
+
}
|
|
405
|
+
const errors = errorsUnder(deepMerge(DEFAULT_CONFIG, user), "/scoring/scorers");
|
|
406
|
+
if (errors.length > 0)
|
|
407
|
+
throw invalidRequest(`Invalid value for ${def.key}: ${errors.map((e) => `${e.path} ${e.message}`).join("; ")}`, undefined, { key: def.key, errors });
|
|
408
|
+
writeJsonAtomic(userFile, user);
|
|
409
|
+
return settingView(def, { ...layers, user });
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Sets one key in the user config. The candidate user config is validated against the config schema on top of the
|
|
413
|
+
* defaults; schema errors at or below the key's path reject the change (INVALID_REQUEST naming each path) and the file
|
|
414
|
+
* is left untouched. Errors elsewhere, such as a model not chosen yet on a first run, do not block it. The project
|
|
415
|
+
* config is not part of the check: when it holds an invalid value for the key, the returned view's notice names it.
|
|
416
|
+
* Every other key of the user config is kept.
|
|
417
|
+
*/
|
|
418
|
+
export function writeSetting(userFile, cwd, key, value) {
|
|
419
|
+
const def = findSetting(key);
|
|
420
|
+
const layers = readConfigLayers(userFile, cwd);
|
|
421
|
+
const user = structuredClone(layers.user);
|
|
422
|
+
if (def.type.kind === "scorerToggle")
|
|
423
|
+
return writeJudgeToggle(def, layers, userFile, parseJudgeToggle(key, value));
|
|
424
|
+
const path = segments(key);
|
|
425
|
+
const pointer = `/${path.join("/")}`;
|
|
426
|
+
if (def.type.kind === "models") {
|
|
427
|
+
const existing = getPath(user, path);
|
|
428
|
+
setPath(user, path, parseModels(key, value).map((m) => keepEffort(m, Array.isArray(existing) ? existing : [])));
|
|
429
|
+
}
|
|
430
|
+
else if (def.type.kind === "modelOrRef") {
|
|
431
|
+
const next = parseModelOrRef(key, value);
|
|
432
|
+
setPath(user, path, keepEffort(next, [getPath(user, path)]));
|
|
433
|
+
}
|
|
434
|
+
else if (def.type.kind === "boolean") {
|
|
435
|
+
setPath(user, path, parseOnOff(key, value));
|
|
436
|
+
}
|
|
437
|
+
else if (def.type.kind === "model") {
|
|
438
|
+
const model = parseModel(key, value);
|
|
439
|
+
const existing = getPath(user, path);
|
|
440
|
+
setPath(user, path, { ...(isPlainObject(existing) ? existing : {}), ...model });
|
|
441
|
+
}
|
|
442
|
+
else {
|
|
443
|
+
if (def.type.kind === "enum" && !(typeof value === "string" && def.type.values.includes(value))) {
|
|
444
|
+
const message = `must be one of ${def.type.values.join(", ")}`;
|
|
445
|
+
throw invalidRequest(`Invalid value for ${key}: ${pointer} ${message}`, undefined, { key, errors: [{ path: pointer, message }] });
|
|
446
|
+
}
|
|
447
|
+
setPath(user, path, value);
|
|
448
|
+
}
|
|
449
|
+
const errors = errorsUnder(deepMerge(DEFAULT_CONFIG, user), pointer);
|
|
450
|
+
if (errors.length > 0) {
|
|
451
|
+
throw invalidRequest(`Invalid value for ${key}: ${errors.map((e) => `${e.path} ${e.message}`).join("; ")}`, undefined, { key, errors });
|
|
452
|
+
}
|
|
453
|
+
writeJsonAtomic(userFile, user);
|
|
454
|
+
return settingView(def, { ...layers, user });
|
|
455
|
+
}
|
|
456
|
+
/** Removes one key from the user config so the project value or the default applies again. */
|
|
457
|
+
export function resetSetting(userFile, cwd, key) {
|
|
458
|
+
const def = findSetting(key);
|
|
459
|
+
const layers = readConfigLayers(userFile, cwd);
|
|
460
|
+
if (def.type.kind === "scorerToggle")
|
|
461
|
+
return writeJudgeToggle(def, layers, userFile, false);
|
|
462
|
+
if (!definedIn(layers.user, def))
|
|
463
|
+
return settingView(def, layers);
|
|
464
|
+
const user = structuredClone(layers.user);
|
|
465
|
+
for (const p of leafPaths(def))
|
|
466
|
+
deletePath(user, p);
|
|
467
|
+
if (def.key === "models.judge" && hasJudgeScorer(user) && !hasJudgeModels(user)) {
|
|
468
|
+
throw invalidRequest("Cannot reset models.judge while an llm_judge scorer is configured", "Turn scoring.judge off first (or remove the llm_judge scorer)", { key: def.key });
|
|
469
|
+
}
|
|
470
|
+
writeJsonAtomic(userFile, user);
|
|
471
|
+
return settingView(def, { ...layers, user });
|
|
472
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { chmodSync, lstatSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join } from "node:path";
|
|
3
|
+
import { isPidAlive } from "../../../store/src/index.js";
|
|
4
|
+
import { NexoError } from "../../../telemetry/src/index.js";
|
|
5
|
+
const OWNER_ONLY = 0o600;
|
|
6
|
+
function isPlainObject(value) {
|
|
7
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
function invalid(file, message, hint) {
|
|
10
|
+
return new NexoError({ code: "CONFIG_INVALID", message: `${message}: ${file}`, hint, context: { file } });
|
|
11
|
+
}
|
|
12
|
+
function lstatOrNull(file) {
|
|
13
|
+
try {
|
|
14
|
+
return lstatSync(file);
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (error.code === "ENOENT")
|
|
18
|
+
return null;
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** A symlinked credentials file is refused: the atomic rename would replace the link and leave the old key in its target. */
|
|
23
|
+
function refuseSymlink(file, stat) {
|
|
24
|
+
if (stat?.isSymbolicLink()) {
|
|
25
|
+
throw invalid(file, "Credentials file is a symbolic link", `Replace the symbolic link with a regular file (mode 600) at ${file}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Reads `credentials.json`. A missing file is empty. Refuses (CONFIG_INVALID) a symbolic link, a file that group or
|
|
30
|
+
* others can access, and one that is not a JSON object of provider entries. `apiKey` values are returned trimmed.
|
|
31
|
+
* Error messages never quote the file contents.
|
|
32
|
+
*/
|
|
33
|
+
export function readCredentials(file) {
|
|
34
|
+
let text;
|
|
35
|
+
try {
|
|
36
|
+
const stat = lstatOrNull(file);
|
|
37
|
+
if (!stat)
|
|
38
|
+
return {};
|
|
39
|
+
refuseSymlink(file, stat);
|
|
40
|
+
if (process.platform !== "win32") {
|
|
41
|
+
const mode = stat.mode & 0o777;
|
|
42
|
+
if ((mode & 0o077) !== 0) {
|
|
43
|
+
throw invalid(file, `Credentials file is accessible by group or others (mode ${mode.toString(8).padStart(4, "0")})`, `Run: chmod 600 ${file}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
text = readFileSync(file, "utf8");
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error instanceof NexoError)
|
|
50
|
+
throw error;
|
|
51
|
+
throw new NexoError({
|
|
52
|
+
code: "STORE_IO",
|
|
53
|
+
message: `Cannot read credentials file ${file}: ${error.code ?? "unknown error"}`,
|
|
54
|
+
hint: "Check that the file exists and is readable by you",
|
|
55
|
+
context: { file },
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
let data;
|
|
59
|
+
try {
|
|
60
|
+
data = JSON.parse(text);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
throw invalid(file, "Credentials file is not valid JSON", `Fix the JSON syntax or remove the file (format: {"<provider>": {"apiKey": "..."}})`);
|
|
64
|
+
}
|
|
65
|
+
if (!isPlainObject(data))
|
|
66
|
+
throw invalid(file, "Credentials file must be a JSON object", `Use the format {"<provider>": {"apiKey": "..."}}`);
|
|
67
|
+
const out = {};
|
|
68
|
+
for (const [provider, entry] of Object.entries(data)) {
|
|
69
|
+
const apiKey = isPlainObject(entry) && typeof entry.apiKey === "string" ? entry.apiKey.trim() : null;
|
|
70
|
+
if (!isPlainObject(entry) || (entry.apiKey !== undefined && !apiKey)) {
|
|
71
|
+
throw invalid(file, `Credentials entry "${provider}" must be an object with a non-empty string apiKey`, `Fix or remove the "${provider}" entry`);
|
|
72
|
+
}
|
|
73
|
+
out[provider] = apiKey ? { ...entry, apiKey } : { ...entry };
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
/** Deletes `<file>.<pid>.<time>.tmp` files left by processes that are no longer running (they may hold keys). */
|
|
78
|
+
function removeStaleTemps(file) {
|
|
79
|
+
const prefix = `${basename(file)}.`;
|
|
80
|
+
let names;
|
|
81
|
+
try {
|
|
82
|
+
names = readdirSync(dirname(file));
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
for (const name of names) {
|
|
88
|
+
const match = name.startsWith(prefix) ? /^(\d+)\.\d+\.tmp$/.exec(name.slice(prefix.length)) : null;
|
|
89
|
+
if (!match)
|
|
90
|
+
continue;
|
|
91
|
+
const pid = Number(match[1]);
|
|
92
|
+
if (pid === process.pid || !isPidAlive(pid))
|
|
93
|
+
rmSync(join(dirname(file), name), { force: true });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Writes `credentials.json` atomically (temp file with mode 0600, then rename) and re-applies mode 0600. Refuses a
|
|
98
|
+
* symbolic link (CONFIG_INVALID) and first removes temp files left by crashed writers.
|
|
99
|
+
*/
|
|
100
|
+
export function writeCredentials(file, data) {
|
|
101
|
+
refuseSymlink(file, lstatOrNull(file));
|
|
102
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
103
|
+
try {
|
|
104
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
105
|
+
removeStaleTemps(file);
|
|
106
|
+
writeFileSync(tmp, `${JSON.stringify(data, null, 2)}\n`, { mode: OWNER_ONLY, flag: "wx" });
|
|
107
|
+
chmodSync(tmp, OWNER_ONLY);
|
|
108
|
+
renameSync(tmp, file);
|
|
109
|
+
chmodSync(file, OWNER_ONLY);
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
rmSync(tmp, { force: true });
|
|
113
|
+
throw new NexoError({
|
|
114
|
+
code: "STORE_IO",
|
|
115
|
+
message: `Cannot write credentials file ${file}: ${error.code ?? "unknown error"}`,
|
|
116
|
+
hint: `Check that ${dirname(file)} is writable by you`,
|
|
117
|
+
context: { file },
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|