@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,186 @@
|
|
|
1
|
+
import { ScorerSpecSchema } from "../../../scorers/src/index.js";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
/** Written out literally so the static type is the union of effort names; a test checks it accepts exactly `EFFORTS`. */
|
|
4
|
+
export const EffortSchema = Type.Union([
|
|
5
|
+
Type.Literal("off"),
|
|
6
|
+
Type.Literal("minimal"),
|
|
7
|
+
Type.Literal("low"),
|
|
8
|
+
Type.Literal("medium"),
|
|
9
|
+
Type.Literal("high"),
|
|
10
|
+
Type.Literal("xhigh"),
|
|
11
|
+
Type.Literal("max"),
|
|
12
|
+
]);
|
|
13
|
+
const ModelSpecSchema = Type.Object({ provider: Type.String({ minLength: 1 }), id: Type.String({ minLength: 1 }), effort: Type.Optional(EffortSchema) }, { additionalProperties: false });
|
|
14
|
+
/**
|
|
15
|
+
* An OpenAI-compatible embeddings endpoint (`POST {baseUrl}/embeddings`); `baseUrl` defaults to OpenAI's for provider
|
|
16
|
+
* `openai`. Provider `google-vertex` uses the Vertex AI API instead and takes no `baseUrl`.
|
|
17
|
+
*/
|
|
18
|
+
const EmbeddingModelSchema = Type.Object({
|
|
19
|
+
provider: Type.String({ minLength: 1 }),
|
|
20
|
+
id: Type.String({ minLength: 1 }),
|
|
21
|
+
baseUrl: Type.Optional(Type.String({ pattern: "^https?://[^\\s]+$" })),
|
|
22
|
+
dimensions: Type.Optional(Type.Integer({ minimum: 1, maximum: 8192 })),
|
|
23
|
+
}, { additionalProperties: false });
|
|
24
|
+
const ModelRefSchema = Type.Object({ ref: Type.Union([Type.Literal("s1"), Type.Literal("s2")]) }, { additionalProperties: false });
|
|
25
|
+
/** `models.approver` by role, optionally with its own effort (default low). */
|
|
26
|
+
const ApproverRefSchema = Type.Object({ ref: Type.Union([Type.Literal("s1"), Type.Literal("s2")]), effort: Type.Optional(EffortSchema) }, { additionalProperties: false });
|
|
27
|
+
const PolicyActionSchema = Type.Union([Type.Literal("allow"), Type.Literal("ask"), Type.Literal("deny")]);
|
|
28
|
+
const ToolRuleSchema = Type.Object({ tool: Type.String({ minLength: 1 }), match: Type.Optional(Type.String()), action: PolicyActionSchema }, { additionalProperties: false });
|
|
29
|
+
const NonInteractiveAskSchema = Type.Union([Type.Literal("allow"), Type.Literal("deny")]);
|
|
30
|
+
const LooseSection = Type.Object({}, { additionalProperties: true });
|
|
31
|
+
const Usd = Type.Number({ exclusiveMinimum: 0 });
|
|
32
|
+
/** `artifact:<kind>` (the active artifact of that kind, seeded with the built-in default) or `file:<path>`. */
|
|
33
|
+
const ArtifactSource = (kind) => Type.String({ pattern: `^(artifact:${kind}|file:.+)$` });
|
|
34
|
+
export const ConfigSchema = Type.Object({
|
|
35
|
+
models: Type.Object({
|
|
36
|
+
s1: ModelSpecSchema,
|
|
37
|
+
s2: ModelSpecSchema,
|
|
38
|
+
judge: Type.Optional(Type.Array(ModelSpecSchema)),
|
|
39
|
+
reflector: Type.Optional(Type.Union([ModelSpecSchema, ModelRefSchema])),
|
|
40
|
+
approver: Type.Optional(Type.Union([ModelSpecSchema, ApproverRefSchema])),
|
|
41
|
+
embedding: Type.Optional(EmbeddingModelSchema),
|
|
42
|
+
}, { additionalProperties: false }),
|
|
43
|
+
providers: Type.Record(Type.String(), Type.Object({
|
|
44
|
+
maxConcurrent: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
45
|
+
maxRetries: Type.Optional(Type.Integer({ minimum: 0, maximum: 10 })),
|
|
46
|
+
retryBaseDelayMs: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
47
|
+
rateLimitBaseDelayMs: Type.Optional(Type.Integer({ minimum: 0 })),
|
|
48
|
+
}, { additionalProperties: false })),
|
|
49
|
+
router: Type.Object({ policy: ArtifactSource("router_policy"), maxEscalations: Type.Integer({ minimum: 0 }) }, { additionalProperties: false }),
|
|
50
|
+
scoring: Type.Object({
|
|
51
|
+
enabled: Type.Boolean(),
|
|
52
|
+
scorers: Type.Array(ScorerSpecSchema),
|
|
53
|
+
rubric: ArtifactSource("judge_rubric"),
|
|
54
|
+
passThreshold: Type.Number({ minimum: 0, maximum: 1 }),
|
|
55
|
+
commandTimeoutMs: Type.Integer({ minimum: 100 }),
|
|
56
|
+
subagents: Type.Boolean(),
|
|
57
|
+
/** Set by loadConfig: "project" when ./.nexo/config.json supplied `scorers`. A value in a config file is ignored. */
|
|
58
|
+
scorersOrigin: Type.Optional(Type.Union([Type.Literal("user"), Type.Literal("project")])),
|
|
59
|
+
/**
|
|
60
|
+
* Set by loadConfig: the tool policy from the user-level layers only (defaults, ~/.nexo/config.json, CLI
|
|
61
|
+
* overrides). Project-level command scorers are checked against it, so ./.nexo/config.json cannot approve its
|
|
62
|
+
* own commands. A value in a config file is ignored.
|
|
63
|
+
*/
|
|
64
|
+
scorerTools: Type.Optional(Type.Object({ policy: Type.Record(Type.String(), PolicyActionSchema), rules: Type.Array(ToolRuleSchema), nonInteractiveAsk: NonInteractiveAskSchema }, { additionalProperties: false })),
|
|
65
|
+
}, { additionalProperties: false }),
|
|
66
|
+
budgets: Type.Object({
|
|
67
|
+
perRunUsd: Usd,
|
|
68
|
+
perSessionUsd: Usd,
|
|
69
|
+
dailyUsd: Usd,
|
|
70
|
+
monthlyUsd: Usd,
|
|
71
|
+
optimDefaultUsd: Usd,
|
|
72
|
+
escalationCutoff: Type.Number({ minimum: 0, maximum: 1 }),
|
|
73
|
+
estimateOutputTokens: Type.Integer({ minimum: 1 }),
|
|
74
|
+
}, { additionalProperties: false }),
|
|
75
|
+
session: Type.Object({
|
|
76
|
+
compactAt: Type.Number({ exclusiveMinimum: 0, maximum: 1 }),
|
|
77
|
+
/** Also compact when one S2 request with the history would cost more than this share of the turn's budget; 0 turns it off. */
|
|
78
|
+
compactAtBudgetShare: Type.Number({ minimum: 0, maximum: 1 }),
|
|
79
|
+
}, { additionalProperties: false }),
|
|
80
|
+
spawn: Type.Object({ maxParallelSubagents: Type.Integer({ minimum: 1 }), maxDepth: Type.Integer({ minimum: 0 }) }, { additionalProperties: false }),
|
|
81
|
+
skills: Type.Object({
|
|
82
|
+
dirs: Type.Array(Type.String({ minLength: 1 })),
|
|
83
|
+
pins: Type.Record(Type.String(), Type.Object({ model: Type.Optional(Type.String({ minLength: 1 })), effort: Type.Optional(EffortSchema) }, { additionalProperties: false })),
|
|
84
|
+
}, { additionalProperties: false }),
|
|
85
|
+
tools: Type.Object({
|
|
86
|
+
policy: Type.Record(Type.String(), PolicyActionSchema),
|
|
87
|
+
rules: Type.Array(ToolRuleSchema),
|
|
88
|
+
nonInteractiveAsk: NonInteractiveAskSchema,
|
|
89
|
+
autoApprove: Type.Union([Type.Literal("off"), Type.Literal("on")]),
|
|
90
|
+
bashTimeoutMs: Type.Integer({ minimum: 1000 }),
|
|
91
|
+
}, { additionalProperties: false }),
|
|
92
|
+
mcp: Type.Object({
|
|
93
|
+
/** Stdio MCP servers by name; their tools reach the agent as `mcp__<name>__<tool>`. */
|
|
94
|
+
servers: Type.Record(Type.String(), Type.Object({
|
|
95
|
+
command: Type.String({ minLength: 1 }),
|
|
96
|
+
args: Type.Optional(Type.Array(Type.String())),
|
|
97
|
+
env: Type.Optional(Type.Record(Type.String(), Type.String())),
|
|
98
|
+
timeoutMs: Type.Optional(Type.Integer({ minimum: 1000 })),
|
|
99
|
+
}, { additionalProperties: false })),
|
|
100
|
+
}, { additionalProperties: false }),
|
|
101
|
+
pricingOverrides: Type.Record(Type.String(), Type.Object({
|
|
102
|
+
input: Type.Number({ minimum: 0 }),
|
|
103
|
+
output: Type.Number({ minimum: 0 }),
|
|
104
|
+
cacheRead: Type.Optional(Type.Number({ minimum: 0 })),
|
|
105
|
+
cacheWrite: Type.Optional(Type.Number({ minimum: 0 })),
|
|
106
|
+
}, { additionalProperties: false })),
|
|
107
|
+
redaction: Type.Object({ patterns: Type.Array(Type.String({ minLength: 1 })) }, { additionalProperties: false }),
|
|
108
|
+
memory: Type.Object({
|
|
109
|
+
enabled: Type.Boolean(),
|
|
110
|
+
maxTokens: Type.Integer({ minimum: 0 }),
|
|
111
|
+
weights: Type.Object({ relevance: Type.Number({ minimum: 0 }), importance: Type.Number({ minimum: 0 }), recency: Type.Number({ minimum: 0 }) }, { additionalProperties: false }),
|
|
112
|
+
tauDays: Type.Number({ exclusiveMinimum: 0 }),
|
|
113
|
+
topK: Type.Integer({ minimum: 1, maximum: 200 }),
|
|
114
|
+
minRelevance: Type.Number({ minimum: 0, maximum: 1 }),
|
|
115
|
+
}, { additionalProperties: false }),
|
|
116
|
+
learning: Type.Object({
|
|
117
|
+
enabled: Type.Boolean(),
|
|
118
|
+
maxPlaybookItems: Type.Integer({ minimum: 1, maximum: 500 }),
|
|
119
|
+
perJobUsd: Usd,
|
|
120
|
+
staleMinutes: Type.Integer({ minimum: 1 }),
|
|
121
|
+
drainAfterRunMs: Type.Integer({ minimum: 0 }),
|
|
122
|
+
}, { additionalProperties: false }),
|
|
123
|
+
optimize: Type.Object({
|
|
124
|
+
/** Fewest eval cases an optimization starts with (spec §8.2.1). */
|
|
125
|
+
minCases: Type.Integer({ minimum: 3 }),
|
|
126
|
+
/** Iterations without a new Pareto member before the loop stops. */
|
|
127
|
+
patience: Type.Integer({ minimum: 1 }),
|
|
128
|
+
/** Lowest judge Cohen's κ that lets judge-only evidence promote a candidate. */
|
|
129
|
+
minJudgeKappa: Type.Number({ minimum: -1, maximum: 1 }),
|
|
130
|
+
/** Embedding cosine at or above which a reflected text counts as a near-duplicate of a candidate and is skipped before any rollout. */
|
|
131
|
+
noveltySimilarity: Type.Number({ exclusiveMinimum: 0, maximum: 1 }),
|
|
132
|
+
/** How strongly a parent's child count lowers its chance of being sampled again (0 turns the penalty off). */
|
|
133
|
+
offspringAlpha: Type.Number({ minimum: 0, maximum: 10 }),
|
|
134
|
+
/** Mining proposed eval cases from unreviewed runs (design §3). */
|
|
135
|
+
mine: Type.Object({
|
|
136
|
+
days: Type.Integer({ minimum: 1 }),
|
|
137
|
+
maxRuns: Type.Integer({ minimum: 10, maximum: 5000 }),
|
|
138
|
+
minCluster: Type.Integer({ minimum: 2 }),
|
|
139
|
+
maxNew: Type.Integer({ minimum: 1 }),
|
|
140
|
+
cosine: Type.Number({ exclusiveMinimum: 0, maximum: 1 }),
|
|
141
|
+
jaccard: Type.Number({ exclusiveMinimum: 0, maximum: 1 }),
|
|
142
|
+
}, { additionalProperties: false }),
|
|
143
|
+
/** `nexo optimize climb` (design §4). */
|
|
144
|
+
climb: Type.Object({ guards: Type.Integer({ minimum: 0, maximum: 10 }) }, { additionalProperties: false }),
|
|
145
|
+
}, { additionalProperties: false }),
|
|
146
|
+
telemetry: Type.Optional(LooseSection),
|
|
147
|
+
ui: Type.Optional(LooseSection),
|
|
148
|
+
}, { additionalProperties: false });
|
|
149
|
+
/** Every default from spec §9 except `models`, which the user must provide. */
|
|
150
|
+
export const DEFAULT_CONFIG = {
|
|
151
|
+
providers: {},
|
|
152
|
+
router: { policy: "artifact:router_policy", maxEscalations: 1 },
|
|
153
|
+
scoring: {
|
|
154
|
+
enabled: true,
|
|
155
|
+
scorers: [],
|
|
156
|
+
rubric: "artifact:judge_rubric",
|
|
157
|
+
passThreshold: 0.6,
|
|
158
|
+
commandTimeoutMs: 120_000,
|
|
159
|
+
subagents: false,
|
|
160
|
+
},
|
|
161
|
+
budgets: {
|
|
162
|
+
perRunUsd: 0.5,
|
|
163
|
+
perSessionUsd: 5,
|
|
164
|
+
dailyUsd: 10,
|
|
165
|
+
monthlyUsd: 150,
|
|
166
|
+
optimDefaultUsd: 5,
|
|
167
|
+
escalationCutoff: 0.8,
|
|
168
|
+
estimateOutputTokens: 4096,
|
|
169
|
+
},
|
|
170
|
+
session: { compactAt: 0.8, compactAtBudgetShare: 0.2 },
|
|
171
|
+
spawn: { maxParallelSubagents: 4, maxDepth: 4 },
|
|
172
|
+
skills: { dirs: ["$NEXO_HOME/skills", "./.nexo/skills"], pins: {} },
|
|
173
|
+
tools: {
|
|
174
|
+
policy: { read: "allow", write: "allow", edit: "allow", bash: "ask", web_fetch: "ask", load_skill: "allow", spawn: "allow" },
|
|
175
|
+
rules: [],
|
|
176
|
+
nonInteractiveAsk: "deny",
|
|
177
|
+
autoApprove: "off",
|
|
178
|
+
bashTimeoutMs: 120_000,
|
|
179
|
+
},
|
|
180
|
+
mcp: { servers: {} },
|
|
181
|
+
pricingOverrides: {},
|
|
182
|
+
redaction: { patterns: [] },
|
|
183
|
+
memory: { enabled: true, maxTokens: 1500, weights: { relevance: 1, importance: 0.5, recency: 0.3 }, tauDays: 14, topK: 20, minRelevance: 0 },
|
|
184
|
+
learning: { enabled: true, maxPlaybookItems: 50, perJobUsd: 0.15, staleMinutes: 10, drainAfterRunMs: 30_000 },
|
|
185
|
+
optimize: { minCases: 10, patience: 5, minJudgeKappa: 0.6, noveltySimilarity: 0.98, offspringAlpha: 0.5, mine: { days: 30, maxRuns: 500, minCluster: 3, maxNew: 3, cosine: 0.8, jaccard: 0.5 }, climb: { guards: 2 } },
|
|
186
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { addSpend, dayScope, getSpent, monthScope } from "../../../store/src/index.js";
|
|
2
|
+
import { NexoError } from "../../../telemetry/src/index.js";
|
|
3
|
+
export function sessionScope(sessionId) {
|
|
4
|
+
return `session:${sessionId}`;
|
|
5
|
+
}
|
|
6
|
+
export function checkAdmission(db, budgets, now) {
|
|
7
|
+
const checks = [
|
|
8
|
+
{ scope: dayScope(now), limit: budgets.dailyUsd, label: "Daily", key: "budgets.dailyUsd", wait: "tomorrow (UTC)" },
|
|
9
|
+
{ scope: monthScope(now), limit: budgets.monthlyUsd, label: "Monthly", key: "budgets.monthlyUsd", wait: "next month (UTC)" },
|
|
10
|
+
];
|
|
11
|
+
for (const c of checks) {
|
|
12
|
+
if (!Number.isFinite(c.limit)) {
|
|
13
|
+
throw new NexoError({
|
|
14
|
+
code: "CONFIG_INVALID",
|
|
15
|
+
message: `${c.label} budget limit is not a finite number: ${c.limit}`,
|
|
16
|
+
hint: `Set ${c.key} to a number in your nexo config`,
|
|
17
|
+
context: { key: c.key, value: c.limit },
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
const spent = getSpent(db, c.scope);
|
|
21
|
+
if (spent >= c.limit) {
|
|
22
|
+
throw new NexoError({
|
|
23
|
+
code: "BUDGET_EXCEEDED",
|
|
24
|
+
message: `${c.label} budget of $${c.limit.toFixed(2)} is exhausted ($${spent.toFixed(4)} spent)`,
|
|
25
|
+
hint: `Raise ${c.key} in your nexo config or wait until ${c.wait}`,
|
|
26
|
+
context: { scope: c.scope, limitUsd: c.limit, spentUsd: spent },
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** The ledger scope of an optimization (spec §5.2). */
|
|
32
|
+
export function optimScope(optimRunId) {
|
|
33
|
+
return `optim:${optimRunId}`;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Adds spend to the day, month and root-run ledger scopes, to `session:<id>` for a session turn and to `optim:<id>` for
|
|
37
|
+
* an optimization rollout.
|
|
38
|
+
*/
|
|
39
|
+
export function recordSpend(db, rootRunId, usd, now, sessionId = null, optimRunId = null) {
|
|
40
|
+
if (!Number.isFinite(usd) || usd < 0) {
|
|
41
|
+
throw new NexoError({ code: "INTERNAL", message: `recordSpend received an invalid amount: ${usd}`, context: { rootRunId, usd } });
|
|
42
|
+
}
|
|
43
|
+
const scopes = [dayScope(now), monthScope(now), `run:${rootRunId}`];
|
|
44
|
+
if (sessionId)
|
|
45
|
+
scopes.push(sessionScope(sessionId));
|
|
46
|
+
if (optimRunId)
|
|
47
|
+
scopes.push(optimScope(optimRunId));
|
|
48
|
+
addSpend(db, scopes, usd, now.toISOString());
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The budget a run may spend: the smallest of its own limit, the session's remaining budget (chat turns) and what is
|
|
52
|
+
* left of the daily and monthly caps, with the limit that set it. Admission only checks the caps when a run starts, so
|
|
53
|
+
* this keeps one run from spending past them.
|
|
54
|
+
*/
|
|
55
|
+
export function runBudgetLimit(db, budgets, now, limits) {
|
|
56
|
+
const candidates = [
|
|
57
|
+
{ usd: limits.run, by: "run" },
|
|
58
|
+
...(limits.session !== undefined ? [{ usd: limits.session, by: "session" }] : []),
|
|
59
|
+
{ usd: budgets.dailyUsd - getSpent(db, dayScope(now)), by: "daily" },
|
|
60
|
+
{ usd: budgets.monthlyUsd - getSpent(db, monthScope(now)), by: "monthly" },
|
|
61
|
+
];
|
|
62
|
+
let best = candidates[0];
|
|
63
|
+
for (const c of candidates)
|
|
64
|
+
if (c.usd < best.usd)
|
|
65
|
+
best = c;
|
|
66
|
+
return { usd: Math.max(0, best.usd), by: best.by };
|
|
67
|
+
}
|
|
68
|
+
const CAPPED_BY = {
|
|
69
|
+
session: "capped by the session's remaining budget",
|
|
70
|
+
daily: "capped by what is left of the daily budget",
|
|
71
|
+
monthly: "capped by what is left of the monthly budget",
|
|
72
|
+
};
|
|
73
|
+
/** BUDGET_EXCEEDED for a run whose budget is spent or too small for its next request, naming the limit that set it and the fix that applies (`chat`: a session turn). */
|
|
74
|
+
export function budgetExhaustedError(budget, by, chat) {
|
|
75
|
+
const capped = by === "run" ? "" : `; ${CAPPED_BY[by]}`;
|
|
76
|
+
const hint = {
|
|
77
|
+
run: chat
|
|
78
|
+
? "Each chat turn is capped by budgets.perRunUsd; raise it in /settings (/budget raises the session's cap, not a turn's)"
|
|
79
|
+
: "Pass --budget or raise budgets.perRunUsd",
|
|
80
|
+
session: "Raise the session's budget with /budget <usd>, then continue",
|
|
81
|
+
daily: "Raise budgets.dailyUsd or wait until tomorrow (UTC)",
|
|
82
|
+
monthly: "Raise budgets.monthlyUsd or wait until next month (UTC)",
|
|
83
|
+
}[by];
|
|
84
|
+
return new NexoError({
|
|
85
|
+
code: "BUDGET_EXCEEDED",
|
|
86
|
+
message: `${chat ? "Turn" : "Run"} budget of $${budget.limitUsd.toFixed(2)} is not enough to continue ($${budget.spentUsd.toFixed(4)} spent${capped})`,
|
|
87
|
+
hint,
|
|
88
|
+
context: { limitUsd: budget.limitUsd, spentUsd: budget.spentUsd, limitedBy: by },
|
|
89
|
+
});
|
|
90
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { NexoError } from "../../../telemetry/src/index.js";
|
|
2
|
+
const EPSILON = 1e-9;
|
|
3
|
+
function assertAmount(method, name, value) {
|
|
4
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
5
|
+
throw new NexoError({
|
|
6
|
+
code: "INTERNAL",
|
|
7
|
+
message: `RunBudget.${method} received an invalid ${name}: ${value}`,
|
|
8
|
+
context: { method, argument: name, value },
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** USD budget of one run. Amounts must be finite and >= 0, otherwise INTERNAL is thrown. Child runs reserve from it and settle their actual spend back into it. */
|
|
13
|
+
export class RunBudget {
|
|
14
|
+
limitUsd;
|
|
15
|
+
spent = 0;
|
|
16
|
+
reserved = 0;
|
|
17
|
+
constructor(limitUsd) {
|
|
18
|
+
if (!Number.isFinite(limitUsd) || limitUsd < 0) {
|
|
19
|
+
throw new NexoError({
|
|
20
|
+
code: "CONFIG_INVALID",
|
|
21
|
+
message: `Run budget limit must be a finite number >= 0, got ${limitUsd}`,
|
|
22
|
+
hint: "Set budgets.perRunUsd to a non-negative number in your nexo config",
|
|
23
|
+
context: { limitUsd },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
this.limitUsd = limitUsd;
|
|
27
|
+
}
|
|
28
|
+
get spentUsd() {
|
|
29
|
+
return this.spent;
|
|
30
|
+
}
|
|
31
|
+
get reservedUsd() {
|
|
32
|
+
return this.reserved;
|
|
33
|
+
}
|
|
34
|
+
remainingUsd() {
|
|
35
|
+
return Math.max(0, this.limitUsd - this.spent - this.reserved);
|
|
36
|
+
}
|
|
37
|
+
remainingFraction() {
|
|
38
|
+
return this.limitUsd <= 0 ? 0 : this.remainingUsd() / this.limitUsd;
|
|
39
|
+
}
|
|
40
|
+
canAfford(usd) {
|
|
41
|
+
return !this.isExhausted() && usd <= this.remainingUsd() + EPSILON;
|
|
42
|
+
}
|
|
43
|
+
charge(usd) {
|
|
44
|
+
assertAmount("charge", "usd", usd);
|
|
45
|
+
this.spent += usd;
|
|
46
|
+
}
|
|
47
|
+
tryReserve(usd) {
|
|
48
|
+
assertAmount("tryReserve", "usd", usd);
|
|
49
|
+
if (usd <= 0 || !this.canAfford(usd))
|
|
50
|
+
return false;
|
|
51
|
+
this.reserved += usd;
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
settleReservation(reservedUsd, actualUsd) {
|
|
55
|
+
assertAmount("settleReservation", "reservedUsd", reservedUsd);
|
|
56
|
+
// The reservation is freed before the actual amount is checked, so a bad actual never leaks reserved budget.
|
|
57
|
+
this.reserved = Math.max(0, this.reserved - reservedUsd);
|
|
58
|
+
assertAmount("settleReservation", "actualUsd", actualUsd);
|
|
59
|
+
this.spent += actualUsd;
|
|
60
|
+
}
|
|
61
|
+
isExhausted() {
|
|
62
|
+
return this.remainingUsd() <= EPSILON;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export function budgetLine(budget) {
|
|
66
|
+
return `Budget: $${budget.remainingUsd().toFixed(4)} of $${budget.limitUsd.toFixed(2)} remaining`;
|
|
67
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { calculateCost } from "@earendil-works/pi-ai";
|
|
2
|
+
export function modelKey(model) {
|
|
3
|
+
return `${model.provider}/${model.id}`;
|
|
4
|
+
}
|
|
5
|
+
function hasRates(cost) {
|
|
6
|
+
return cost.input > 0 || cost.output > 0 || cost.cacheRead > 0 || cost.cacheWrite > 0;
|
|
7
|
+
}
|
|
8
|
+
function withRates(model, rates) {
|
|
9
|
+
return { ...model, cost: { input: rates.input, output: rates.output, cacheRead: rates.cacheRead ?? 0, cacheWrite: rates.cacheWrite ?? 0 } };
|
|
10
|
+
}
|
|
11
|
+
function reportedTotal(usage) {
|
|
12
|
+
const total = usage.cost?.total;
|
|
13
|
+
return typeof total === "number" ? total : 0;
|
|
14
|
+
}
|
|
15
|
+
function computeCost(model, usage) {
|
|
16
|
+
const copy = structuredClone(usage);
|
|
17
|
+
if (typeof copy.cost !== "object" || copy.cost === null) {
|
|
18
|
+
copy.cost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 };
|
|
19
|
+
}
|
|
20
|
+
return calculateCost(model, copy).total;
|
|
21
|
+
}
|
|
22
|
+
function validCost(costUsd) {
|
|
23
|
+
return Number.isFinite(costUsd) && costUsd >= 0 ? { costUsd, costUnknown: false } : { costUsd: 0, costUnknown: true };
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Price precedence: pricingOverrides > provider-reported cost > pi model rates; otherwise flagged unknown.
|
|
27
|
+
* A computed cost that is not a finite number >= 0 (for example from a usage with missing fields) is flagged unknown with costUsd 0.
|
|
28
|
+
*/
|
|
29
|
+
export function priceUsage(model, usage, overrides) {
|
|
30
|
+
const override = overrides[modelKey(model)];
|
|
31
|
+
if (override)
|
|
32
|
+
return validCost(computeCost(withRates(model, override), usage));
|
|
33
|
+
const reported = reportedTotal(usage);
|
|
34
|
+
if (reported > 0)
|
|
35
|
+
return validCost(reported);
|
|
36
|
+
if (hasRates(model.cost))
|
|
37
|
+
return validCost(computeCost(model, usage));
|
|
38
|
+
const tokens = usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
|
|
39
|
+
return { costUsd: 0, costUnknown: tokens > 0 };
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Upper-bound USD estimate for one request: ceil(chars/4) input tokens plus min(outputTokens, model.maxTokens) output tokens.
|
|
43
|
+
* Returns 0 when no price is known or the result is not a finite number >= 0.
|
|
44
|
+
*/
|
|
45
|
+
export function estimateCallCostUsd(model, overrides, promptChars, outputTokens) {
|
|
46
|
+
const rates = overrides[modelKey(model)] ?? (hasRates(model.cost) ? model.cost : undefined);
|
|
47
|
+
if (!rates)
|
|
48
|
+
return 0;
|
|
49
|
+
const inputTokens = Math.ceil(promptChars / 4);
|
|
50
|
+
const out = model.maxTokens > 0 ? Math.min(outputTokens, model.maxTokens) : outputTokens;
|
|
51
|
+
const usd = (inputTokens * rates.input + out * rates.output) / 1_000_000;
|
|
52
|
+
return Number.isFinite(usd) && usd >= 0 ? usd : 0;
|
|
53
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { normalizeContext } from "@earendil-works/pi-ai";
|
|
2
|
+
import { NexoError, toErrorPayload } from "../../../telemetry/src/index.js";
|
|
3
|
+
import { estimateCallCostUsd } from "../cost/pricing.js";
|
|
4
|
+
import { classifyProviderError, providerRetryDelayMs } from "./errors.js";
|
|
5
|
+
import { assistantText } from "./messages.js";
|
|
6
|
+
/**
|
|
7
|
+
* One LLM completion outside the agent loop (judge, self-check, reflector, curator). Each attempt reserves its estimate
|
|
8
|
+
* from `payer.budget` (so parallel calls never spend more than the budget together), caps the reply at
|
|
9
|
+
* `request.outputTokens` (default `budgets.estimateOutputTokens`), streams through the provider gate, releases the reservation, charges the priced reply
|
|
10
|
+
* through `payer.charge` and is recorded as one span. Retryable provider errors are retried like agent turns. Throws
|
|
11
|
+
* `payer.refused(estimate)`, a classified provider error, or INTERNAL on abort.
|
|
12
|
+
*/
|
|
13
|
+
export async function runChargedCompletion(payer, request, signal) {
|
|
14
|
+
const { env, recorder, budget } = payer;
|
|
15
|
+
const { config, redact } = env.deps;
|
|
16
|
+
const { model, span } = request;
|
|
17
|
+
const providerCfg = config.providers[model.provider] ?? {};
|
|
18
|
+
const maxRetries = request.maxRetries ?? providerCfg.maxRetries ?? 3;
|
|
19
|
+
// The reply is capped at the output tokens the estimate assumes, so a verbose reply cannot cost more than was reserved.
|
|
20
|
+
const outputTokens = request.outputTokens ?? config.budgets.estimateOutputTokens;
|
|
21
|
+
const maxTokens = model.maxTokens > 0 ? Math.min(outputTokens, model.maxTokens) : outputTokens;
|
|
22
|
+
let costUsd = 0;
|
|
23
|
+
let costUnknown = false;
|
|
24
|
+
for (let attempt = 1;; attempt++) {
|
|
25
|
+
const estimate = estimateCallCostUsd(model, config.pricingOverrides, request.system.length + request.user.length, outputTokens);
|
|
26
|
+
const intoHoldback = estimate > 0 && budget.remainingUsd() - estimate + 1e-9 < (payer.holdbackUsd?.() ?? 0);
|
|
27
|
+
const reserved = !intoHoldback && (estimate > 0 ? budget.tryReserve(estimate) : budget.canAfford(0));
|
|
28
|
+
if (!reserved)
|
|
29
|
+
throw payer.refused(estimate);
|
|
30
|
+
const spanId = recorder.start({
|
|
31
|
+
kind: span.kind,
|
|
32
|
+
name: span.name,
|
|
33
|
+
parentSpanId: span.parentSpanId,
|
|
34
|
+
model: model.id,
|
|
35
|
+
provider: model.provider,
|
|
36
|
+
tier: span.tier,
|
|
37
|
+
effort: request.effort,
|
|
38
|
+
modelSource: "default",
|
|
39
|
+
attrs: span.attrs,
|
|
40
|
+
});
|
|
41
|
+
let msg;
|
|
42
|
+
try {
|
|
43
|
+
const context = normalizeContext({ systemPrompt: request.system, messages: [{ role: "user", content: request.user, timestamp: env.now().getTime() }] });
|
|
44
|
+
const stream = await env.streamFn(model, context, { reasoning: request.effort === "off" ? undefined : request.effort, maxTokens, signal, ...(request.cacheRetention ? { cacheRetention: request.cacheRetention } : {}) });
|
|
45
|
+
msg = await stream.result();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (estimate > 0)
|
|
49
|
+
budget.settleReservation(estimate, 0);
|
|
50
|
+
recorder.finish(spanId, { status: "error", attrs: { error: redact(toErrorPayload(error)) } });
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
if (estimate > 0)
|
|
54
|
+
budget.settleReservation(estimate, 0);
|
|
55
|
+
const priced = payer.charge(model, msg);
|
|
56
|
+
costUsd += priced.costUsd;
|
|
57
|
+
costUnknown ||= priced.costUnknown;
|
|
58
|
+
const text = assistantText(msg);
|
|
59
|
+
const failed = msg.stopReason === "error" || msg.stopReason === "aborted";
|
|
60
|
+
recorder.finish(spanId, {
|
|
61
|
+
status: failed ? "error" : "ok",
|
|
62
|
+
tokensIn: msg.usage.input,
|
|
63
|
+
tokensOut: msg.usage.output,
|
|
64
|
+
cacheRead: msg.usage.cacheRead,
|
|
65
|
+
cacheWrite: msg.usage.cacheWrite,
|
|
66
|
+
costUsd: priced.costUsd,
|
|
67
|
+
costUnknown: priced.costUnknown,
|
|
68
|
+
attrs: { stopReason: msg.stopReason, errorMessage: msg.errorMessage, reply: redact(text) },
|
|
69
|
+
});
|
|
70
|
+
if (msg.stopReason === "aborted" || signal.aborted)
|
|
71
|
+
throw new NexoError({ code: "INTERNAL", message: payer.abortMessage });
|
|
72
|
+
if (!failed)
|
|
73
|
+
return { text, costUsd, costUnknown };
|
|
74
|
+
const error = classifyProviderError(msg.errorMessage ?? "Provider error");
|
|
75
|
+
if (!error.retryable || attempt > maxRetries)
|
|
76
|
+
throw error;
|
|
77
|
+
await env.sleep(providerRetryDelayMs(attempt, msg.errorMessage ?? "", providerCfg), signal);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { NexoError } from "../../../telemetry/src/index.js";
|
|
2
|
+
/** Maps a provider error message (pi encodes failures as text) to a NexoError code and hint. */
|
|
3
|
+
export function classifyProviderError(message) {
|
|
4
|
+
const lower = message.toLowerCase();
|
|
5
|
+
if (/\b(401|403)\b|unauthori|invalid.*api.?key|no api key|authentication|permission denied/.test(lower)) {
|
|
6
|
+
return new NexoError({
|
|
7
|
+
code: "PROVIDER_AUTH",
|
|
8
|
+
message,
|
|
9
|
+
hint: "Set the provider's API key environment variable (for example ANTHROPIC_API_KEY or OPENAI_API_KEY) or log in with pi-ai",
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
if (/\b429\b|rate.?limit|too many requests|resource.?exhausted/.test(lower)) {
|
|
13
|
+
return new NexoError({ code: "PROVIDER_RATE_LIMIT", message, retryable: true, hint: "Lower providers.<name>.maxConcurrent or retry later" });
|
|
14
|
+
}
|
|
15
|
+
if (/timed? ?out|timeout|etimedout/.test(lower)) {
|
|
16
|
+
return new NexoError({ code: "PROVIDER_TIMEOUT", message, retryable: true, hint: "Retry the run; check network connectivity" });
|
|
17
|
+
}
|
|
18
|
+
if (/\b400\b|bad request|invalid_argument|invalid_request_error/.test(lower)) {
|
|
19
|
+
return new NexoError({
|
|
20
|
+
code: "PROVIDER_UNAVAILABLE",
|
|
21
|
+
message,
|
|
22
|
+
hint: "The provider rejected the request as invalid (HTTP 400); retrying sends the same request. The message above says which part it refused",
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return new NexoError({ code: "PROVIDER_UNAVAILABLE", message, retryable: true, hint: "The provider returned an error; retry later" });
|
|
26
|
+
}
|
|
27
|
+
export function contextOverflowError(message) {
|
|
28
|
+
return new NexoError({
|
|
29
|
+
code: "PROVIDER_CONTEXT_OVERFLOW",
|
|
30
|
+
message,
|
|
31
|
+
hint: "The conversation no longer fits the model's context window; use a model with a larger window or narrow the task",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
export function retryAfterMs(errorMessage) {
|
|
35
|
+
const match = /retry[- ]after[^0-9]{0,5}(\d+(?:\.\d+)?)/i.exec(errorMessage);
|
|
36
|
+
return match?.[1] ? Math.round(Number(match[1]) * 1000) : undefined;
|
|
37
|
+
}
|
|
38
|
+
const RATE_LIMIT_MAX_DELAY_MS = 60_000;
|
|
39
|
+
/**
|
|
40
|
+
* Backoff before retry `attempt` (1-based, counted per streak of consecutive failures) of a retryable provider error.
|
|
41
|
+
* A rate limit waits `rateLimitBaseDelayMs` (default 5 s) doubling per attempt up to a minute, ±20% so parallel callers
|
|
42
|
+
* spread out; quota windows are seconds to a minute long and providers such as Vertex send no retry-after. Other
|
|
43
|
+
* errors use `retryDelayMs`. A retry-after in the message is always waited at least.
|
|
44
|
+
*/
|
|
45
|
+
export function providerRetryDelayMs(attempt, errorMessage, cfg, random = Math.random) {
|
|
46
|
+
if (classifyProviderError(errorMessage).code !== "PROVIDER_RATE_LIMIT")
|
|
47
|
+
return retryDelayMs(attempt, cfg.retryBaseDelayMs ?? 1000, errorMessage, random);
|
|
48
|
+
const base = Math.min(RATE_LIMIT_MAX_DELAY_MS, (cfg.rateLimitBaseDelayMs ?? 5000) * 2 ** (attempt - 1));
|
|
49
|
+
return Math.max(Math.round(base * (0.8 + 0.4 * random())), retryAfterMs(errorMessage) ?? 0);
|
|
50
|
+
}
|
|
51
|
+
/** Backoff before retry `attempt` (1-based) of a retryable provider error: full jitter on an exponential base, at least the provider's retry-after. */
|
|
52
|
+
export function retryDelayMs(attempt, baseDelayMs, errorMessage, random = Math.random) {
|
|
53
|
+
return Math.max(random() * baseDelayMs * 2 ** (attempt - 1), retryAfterMs(errorMessage) ?? 0);
|
|
54
|
+
}
|
|
55
|
+
/** The text the model sees for a failed tool call: code, retryability, message and hint. */
|
|
56
|
+
export function toolErrorText(error) {
|
|
57
|
+
return `${error.code}${error.retryable ? " (retryable)" : ""}: ${error.message}${error.hint ? `\nhint: ${error.hint}` : ""}`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* pi turns an error thrown by a tool into a result holding only `error.message`, so a NexoError is rethrown with its
|
|
61
|
+
* code, retryability and hint in the message.
|
|
62
|
+
*/
|
|
63
|
+
export function withToolErrorCodes(tool) {
|
|
64
|
+
return {
|
|
65
|
+
...tool,
|
|
66
|
+
execute: async (toolCallId, params, signal, onUpdate) => {
|
|
67
|
+
try {
|
|
68
|
+
return await tool.execute(toolCallId, params, signal, onUpdate);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof NexoError)
|
|
72
|
+
throw new Error(toolErrorText(error), { cause: error });
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createMcpHub } from "../../../mcp/src/index.js";
|
|
2
|
+
/** Timeout of one MCP tool call when the server sets none: long enough to approve a browser connection. */
|
|
3
|
+
export const DEFAULT_MCP_TIMEOUT_MS = 120_000;
|
|
4
|
+
/** The hub for `config.mcp.servers`, or null when none is configured. Servers start on the first run that lists tools. */
|
|
5
|
+
export function createConfiguredMcpHub(config, options) {
|
|
6
|
+
const entries = Object.entries(config.mcp.servers);
|
|
7
|
+
if (entries.length === 0)
|
|
8
|
+
return null;
|
|
9
|
+
const servers = Object.fromEntries(entries.map(([name, s]) => [name, { command: s.command, args: s.args ?? [], env: s.env ?? {}, timeoutMs: s.timeoutMs ?? DEFAULT_MCP_TIMEOUT_MS }]));
|
|
10
|
+
return createMcpHub({ servers, cwd: options.cwd, onWarning: (message, context) => options.logger.warn(message, context) });
|
|
11
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export function assistantText(message) {
|
|
2
|
+
if (!message)
|
|
3
|
+
return "";
|
|
4
|
+
return message.content
|
|
5
|
+
.filter((block) => block.type === "text")
|
|
6
|
+
.map((block) => block.text)
|
|
7
|
+
.join("");
|
|
8
|
+
}
|
|
9
|
+
/** What the model said, for the llm_call span: text, thinking and tool calls without provider signatures. */
|
|
10
|
+
export function recordedContent(message) {
|
|
11
|
+
return message.content.map((block) => {
|
|
12
|
+
if (block.type === "text")
|
|
13
|
+
return { type: "text", text: block.text };
|
|
14
|
+
if (block.type === "thinking")
|
|
15
|
+
return block.redacted ? { type: "thinking", thinking: block.thinking, redacted: true } : { type: "thinking", thinking: block.thinking };
|
|
16
|
+
return { type: "toolCall", id: block.id, name: block.name, arguments: block.arguments };
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export function lastAssistant(messages) {
|
|
20
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
21
|
+
const m = messages[i];
|
|
22
|
+
if (m && m.role === "assistant")
|
|
23
|
+
return m;
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
export function contentText(content) {
|
|
28
|
+
if (typeof content === "string")
|
|
29
|
+
return content;
|
|
30
|
+
if (!Array.isArray(content))
|
|
31
|
+
return "";
|
|
32
|
+
return content.map((b) => (b && typeof b === "object" && "text" in b ? String(b.text) : "")).join("");
|
|
33
|
+
}
|
|
34
|
+
/** Returns a copy of `messages` whose last user/toolResult message carries the budget line; other shapes are returned unchanged. */
|
|
35
|
+
export function withBudgetLine(messages, line) {
|
|
36
|
+
const last = messages.at(-1);
|
|
37
|
+
if (!last || (last.role !== "user" && last.role !== "toolResult"))
|
|
38
|
+
return messages;
|
|
39
|
+
const content = typeof last.content === "string" ? [{ type: "text", text: last.content }] : last.content;
|
|
40
|
+
const updated = { ...last, content: [...content, { type: "text", text: `\n[nexo] ${line}` }] };
|
|
41
|
+
return [...messages.slice(0, -1), updated];
|
|
42
|
+
}
|
|
43
|
+
const MAX_CALL_ARGS_CHARS = 2000;
|
|
44
|
+
/**
|
|
45
|
+
* Gemini 3 rejects a request whose history holds a function call without that model's own thought signature, and
|
|
46
|
+
* pi-ai drops signatures made by any other model. For a Google target, tool calls made by another provider or model
|
|
47
|
+
* (earlier turns on S1, the part of a run before an escalation, a `/model` switch) are sent as text instead: each call
|
|
48
|
+
* becomes `[tool call <name> <args>]` in its assistant message and its result a user message `[tool result <name>]`.
|
|
49
|
+
* The target's own calls stay structured and signed. Other targets get `messages` unchanged.
|
|
50
|
+
*/
|
|
51
|
+
export function foreignToolCallsAsText(messages, target) {
|
|
52
|
+
if (!target.api.startsWith("google"))
|
|
53
|
+
return messages;
|
|
54
|
+
const rewritten = new Set();
|
|
55
|
+
let changed = false;
|
|
56
|
+
const out = messages.map((m) => {
|
|
57
|
+
if (m.role === "assistant") {
|
|
58
|
+
const a = m;
|
|
59
|
+
if ((a.provider === target.provider && a.model === target.id) || !a.content.some((b) => b.type === "toolCall"))
|
|
60
|
+
return m;
|
|
61
|
+
changed = true;
|
|
62
|
+
return {
|
|
63
|
+
...a,
|
|
64
|
+
content: a.content.map((b) => {
|
|
65
|
+
if (b.type !== "toolCall")
|
|
66
|
+
return b;
|
|
67
|
+
rewritten.add(b.id);
|
|
68
|
+
const args = JSON.stringify(b.arguments ?? {});
|
|
69
|
+
return { type: "text", text: `[tool call ${b.name} ${args.length > MAX_CALL_ARGS_CHARS ? `${args.slice(0, MAX_CALL_ARGS_CHARS)}…` : args}]` };
|
|
70
|
+
}),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (m.role === "toolResult" && rewritten.has(m.toolCallId)) {
|
|
74
|
+
const r = m;
|
|
75
|
+
const [first, ...rest] = r.content;
|
|
76
|
+
const head = `[tool result ${r.toolName}${r.isError ? " (error)" : ""}]`;
|
|
77
|
+
const content = first?.type === "text" ? [{ type: "text", text: `${head}\n${first.text}` }, ...rest] : [{ type: "text", text: head }, ...r.content];
|
|
78
|
+
return { role: "user", content, timestamp: r.timestamp };
|
|
79
|
+
}
|
|
80
|
+
return m;
|
|
81
|
+
});
|
|
82
|
+
return changed ? out : messages;
|
|
83
|
+
}
|
|
84
|
+
export function estimatePromptChars(messages) {
|
|
85
|
+
return JSON.stringify(messages).length;
|
|
86
|
+
}
|