@picassohq/cli 0.0.0-stage → 1.0.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/agents/browser_agent.yaml +53 -0
- package/agents/code_reviewer.yaml +49 -0
- package/agents/codebase_investigator.yaml +70 -0
- package/agents/design_auditor.yaml +50 -0
- package/agents/devex_auditor.yaml +50 -0
- package/agents/doc_writer.yaml +54 -0
- package/agents/engineering_architect.yaml +51 -0
- package/agents/generalist.yaml +55 -0
- package/agents/migration_planner.yaml +51 -0
- package/agents/product_reviewer.yaml +48 -0
- package/agents/qa_operator.yaml +52 -0
- package/agents/release_engineer.yaml +50 -0
- package/agents/security_auditor.yaml +62 -0
- package/agents/test_writer.yaml +52 -0
- package/assets/first-light-lockup.png +0 -0
- package/assets/first-light-tagline.png +0 -0
- package/assets/house/README.md +26 -0
- package/assets/house/auraone-banner.png +0 -0
- package/assets/house/auraone-logo.png +0 -0
- package/assets/house/kiro-prism-banner.png +0 -0
- package/assets/house/kiro-prism-logo.png +0 -0
- package/assets/house/picasso-banner.png +0 -0
- package/assets/house/picasso-logo.png +0 -0
- package/assets/wordmark-app.png +0 -0
- package/assets/wordmark-tight.png +0 -0
- package/assets/wordmark.png +0 -0
- package/design/styles.css +1042 -0
- package/dist/build-info.json +37 -0
- package/dist/index.js +3028 -0
- package/goals/cli-hardening.yaml +40 -0
- package/goals/repo-maintenance.yaml +40 -0
- package/package.json +49 -4
- package/runtime/dsh/picasso-ecosystem.community.amendments.yml +24 -0
- package/runtime/dsh/picasso-ecosystem.community.json +185 -0
- package/runtime/dsh/picasso-ecosystem.community.patch.yml +40 -0
- package/runtime/dsh/picasso-ecosystem.cordis.patch.yml +143 -0
- package/runtime/dsh/picasso-isolation.patch.yml +25 -0
- package/runtime/dsh/picasso-providers.patch.yml +373 -0
- package/runtime/dsh/picasso-runtime.cordis.yml +738 -0
- package/runtime/dsh/picasso-sdk.patch.yml +204 -0
- package/runtime/engine-manifest.json +214 -0
- package/runtime/package.json +6 -0
- package/runtime/packages/picasso-cache-probe/cordis.patch.yml +5 -0
- package/runtime/packages/picasso-cache-probe/index.js +477 -0
- package/runtime/packages/picasso-cache-probe/package.json +38 -0
- package/runtime/packages/picasso-commands/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-commands/fresh-review.js +573 -0
- package/runtime/packages/picasso-commands/index.js +497 -0
- package/runtime/packages/picasso-commands/package.json +28 -0
- package/runtime/packages/picasso-instructions/conflicts.js +115 -0
- package/runtime/packages/picasso-instructions/formats.js +145 -0
- package/runtime/packages/picasso-instructions/frontmatter.js +175 -0
- package/runtime/packages/picasso-instructions/glob.js +114 -0
- package/runtime/packages/picasso-instructions/globals.js +90 -0
- package/runtime/packages/picasso-instructions/imports.js +123 -0
- package/runtime/packages/picasso-instructions/index.js +326 -0
- package/runtime/packages/picasso-instructions/modes.js +64 -0
- package/runtime/packages/picasso-instructions/package.json +30 -0
- package/runtime/packages/picasso-instructions/parse.js +134 -0
- package/runtime/packages/picasso-instructions/report.js +161 -0
- package/runtime/packages/picasso-instructions/resolve.js +939 -0
- package/runtime/packages/picasso-instructions/scan.js +181 -0
- package/runtime/packages/picasso-instructions/settings.js +150 -0
- package/runtime/packages/picasso-instructions/skills.js +345 -0
- package/runtime/packages/picasso-lean-context/cordis.patch.yml +13 -0
- package/runtime/packages/picasso-lean-context/index.js +6049 -0
- package/runtime/packages/picasso-lean-context/package.json +26 -0
- package/runtime/packages/picasso-lean-context/timeline.js +417 -0
- package/runtime/packages/picasso-librarian/classify.js +146 -0
- package/runtime/packages/picasso-librarian/cordis.patch.yml +10 -0
- package/runtime/packages/picasso-librarian/index.js +878 -0
- package/runtime/packages/picasso-librarian/notes.js +159 -0
- package/runtime/packages/picasso-librarian/package.json +34 -0
- package/runtime/packages/picasso-librarian/state.js +478 -0
- package/runtime/packages/picasso-memory/index.js +1184 -0
- package/runtime/packages/picasso-memory/package.json +21 -0
- package/runtime/packages/picasso-model-router/connections.js +895 -0
- package/runtime/packages/picasso-model-router/cordis.patch.yml +17 -0
- package/runtime/packages/picasso-model-router/index.js +1081 -0
- package/runtime/packages/picasso-model-router/package.json +30 -0
- package/runtime/packages/picasso-options/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-options/index.js +263 -0
- package/runtime/packages/picasso-options/package.json +28 -0
- package/runtime/packages/picasso-progress-reports/cordis.patch.yml +10 -0
- package/runtime/packages/picasso-progress-reports/index.js +267 -0
- package/runtime/packages/picasso-progress-reports/package.json +30 -0
- package/runtime/packages/picasso-progress-reports/report.js +235 -0
- package/runtime/packages/picasso-sdk-server/aside.js +128 -0
- package/runtime/packages/picasso-sdk-server/connections.js +38 -0
- package/runtime/packages/picasso-sdk-server/context-timeline.js +27 -0
- package/runtime/packages/picasso-sdk-server/egress-gate.js +520 -0
- package/runtime/packages/picasso-sdk-server/failure-policy.js +246 -0
- package/runtime/packages/picasso-sdk-server/file-input.js +169 -0
- package/runtime/packages/picasso-sdk-server/goal.js +131 -0
- package/runtime/packages/picasso-sdk-server/ignore-file.js +458 -0
- package/runtime/packages/picasso-sdk-server/ignore-plugin.js +12 -0
- package/runtime/packages/picasso-sdk-server/ignore-sandbox.js +122 -0
- package/runtime/packages/picasso-sdk-server/ignore-snapshot.js +131 -0
- package/runtime/packages/picasso-sdk-server/image-input.js +44 -0
- package/runtime/packages/picasso-sdk-server/index.js +886 -0
- package/runtime/packages/picasso-sdk-server/instructions.js +34 -0
- package/runtime/packages/picasso-sdk-server/jobs.js +113 -0
- package/runtime/packages/picasso-sdk-server/mcp-gate.js +51 -0
- package/runtime/packages/picasso-sdk-server/model-selection.js +162 -0
- package/runtime/packages/picasso-sdk-server/package.json +50 -0
- package/runtime/packages/picasso-sdk-server/picasso-records.js +50 -0
- package/runtime/packages/picasso-sdk-server/provider-reload.js +84 -0
- package/runtime/packages/picasso-sdk-server/queue.js +322 -0
- package/runtime/packages/picasso-sdk-server/requester.js +43 -0
- package/runtime/packages/picasso-sdk-server/rewind.js +108 -0
- package/runtime/packages/picasso-sdk-server/schedule.js +187 -0
- package/runtime/packages/picasso-sdk-server/session-grants.js +35 -0
- package/runtime/packages/picasso-sdk-server/session-state.js +118 -0
- package/runtime/packages/picasso-sdk-server/shell-paths.js +869 -0
- package/runtime/packages/picasso-sdk-server/stream-deltas.js +211 -0
- package/runtime/packages/picasso-sdk-server/team-tasks.js +451 -0
- package/runtime/packages/picasso-sdk-server/tool-output.js +128 -0
- package/runtime/packages/picasso-sdk-server/tools-list.js +51 -0
- package/runtime/packages/picasso-sdk-server/wave2-hooks.js +612 -0
- package/runtime/packages/picasso-sdk-server/workspace-archive.js +170 -0
- package/runtime/packages/picasso-sdk-server/workspace-restore.js +180 -0
- package/runtime/packages/picasso-web-search/backends.js +350 -0
- package/runtime/packages/picasso-web-search/config.js +121 -0
- package/runtime/packages/picasso-web-search/cordis.patch.yml +7 -0
- package/runtime/packages/picasso-web-search/failure.js +111 -0
- package/runtime/packages/picasso-web-search/format.js +35 -0
- package/runtime/packages/picasso-web-search/index.js +66 -0
- package/runtime/packages/picasso-web-search/package.json +38 -0
- package/runtime/packages/picasso-web-search/router.js +155 -0
- package/runtime/packages/picasso-x-reader/circle.js +324 -0
- package/runtime/packages/picasso-x-reader/command.js +169 -0
- package/runtime/packages/picasso-x-reader/cordis.patch.yml +8 -0
- package/runtime/packages/picasso-x-reader/index.js +170 -0
- package/runtime/packages/picasso-x-reader/package.json +39 -0
- package/runtime/packages/picasso-x-reader/read.js +370 -0
- package/runtime/packages/picasso-x-reader/rewrite.js +38 -0
- package/runtime/packages/picasso-x-reader/settings.js +131 -0
- package/runtime/packages/picasso-x-reader/urls.js +154 -0
- package/runtime/packages/picasso-x-reader/xmd.js +512 -0
- package/runtime/plugins/picasso-auth-bridge/index.js +380 -0
- package/runtime/plugins/picasso-auth-bridge/package.json +28 -0
- package/runtime/plugins/picasso-auto-approve/index.js +1003 -0
- package/runtime/plugins/picasso-auto-approve/package.json +28 -0
- package/runtime/plugins/picasso-browser/index.js +424 -0
- package/runtime/plugins/picasso-browser/package.json +28 -0
- package/runtime/plugins/picasso-checkpoints/evidence.js +210 -0
- package/runtime/plugins/picasso-checkpoints/index.js +885 -0
- package/runtime/plugins/picasso-checkpoints/package.json +29 -0
- package/runtime/plugins/picasso-commands/fresh-review.js +573 -0
- package/runtime/plugins/picasso-commands/index.js +497 -0
- package/runtime/plugins/picasso-commands/package.json +28 -0
- package/runtime/plugins/picasso-instructions/conflicts.js +115 -0
- package/runtime/plugins/picasso-instructions/formats.js +145 -0
- package/runtime/plugins/picasso-instructions/frontmatter.js +175 -0
- package/runtime/plugins/picasso-instructions/glob.js +114 -0
- package/runtime/plugins/picasso-instructions/globals.js +90 -0
- package/runtime/plugins/picasso-instructions/imports.js +123 -0
- package/runtime/plugins/picasso-instructions/index.js +326 -0
- package/runtime/plugins/picasso-instructions/modes.js +64 -0
- package/runtime/plugins/picasso-instructions/package.json +30 -0
- package/runtime/plugins/picasso-instructions/parse.js +134 -0
- package/runtime/plugins/picasso-instructions/report.js +161 -0
- package/runtime/plugins/picasso-instructions/resolve.js +939 -0
- package/runtime/plugins/picasso-instructions/scan.js +181 -0
- package/runtime/plugins/picasso-instructions/settings.js +150 -0
- package/runtime/plugins/picasso-instructions/skills.js +345 -0
- package/runtime/plugins/picasso-lean-context/index.js +6049 -0
- package/runtime/plugins/picasso-lean-context/package.json +26 -0
- package/runtime/plugins/picasso-lean-context/timeline.js +417 -0
- package/runtime/plugins/picasso-librarian/classify.js +146 -0
- package/runtime/plugins/picasso-librarian/index.js +878 -0
- package/runtime/plugins/picasso-librarian/notes.js +159 -0
- package/runtime/plugins/picasso-librarian/package.json +34 -0
- package/runtime/plugins/picasso-librarian/state.js +478 -0
- package/runtime/plugins/picasso-lifecycle-hooks/index.js +186 -0
- package/runtime/plugins/picasso-lifecycle-hooks/package.json +25 -0
- package/runtime/plugins/picasso-mail/inbox.js +207 -0
- package/runtime/plugins/picasso-mail/index.js +293 -0
- package/runtime/plugins/picasso-mail/package.json +22 -0
- package/runtime/plugins/picasso-memory/index.js +1184 -0
- package/runtime/plugins/picasso-memory/package.json +21 -0
- package/runtime/plugins/picasso-model-router/connections.js +895 -0
- package/runtime/plugins/picasso-model-router/index.js +1081 -0
- package/runtime/plugins/picasso-model-router/package.json +30 -0
- package/runtime/plugins/picasso-options/index.js +263 -0
- package/runtime/plugins/picasso-options/package.json +28 -0
- package/runtime/plugins/picasso-progress-reports/index.js +267 -0
- package/runtime/plugins/picasso-progress-reports/package.json +30 -0
- package/runtime/plugins/picasso-progress-reports/report.js +235 -0
- package/runtime/plugins/picasso-provider-models/index.js +436 -0
- package/runtime/plugins/picasso-provider-models/package.json +28 -0
- package/runtime/plugins/picasso-repo-index/index.js +1850 -0
- package/runtime/plugins/picasso-repo-index/package.json +28 -0
- package/runtime/plugins/picasso-schedule-pause/index.js +344 -0
- package/runtime/plugins/picasso-schedule-pause/package.json +28 -0
- package/runtime/plugins/picasso-session-lifecycle/index.js +796 -0
- package/runtime/plugins/picasso-session-lifecycle/package.json +28 -0
- package/runtime/plugins/picasso-trackers/index.js +895 -0
- package/runtime/plugins/picasso-trackers/package.json +28 -0
- package/runtime/plugins/picasso-web-search/backends.js +350 -0
- package/runtime/plugins/picasso-web-search/config.js +121 -0
- package/runtime/plugins/picasso-web-search/failure.js +111 -0
- package/runtime/plugins/picasso-web-search/format.js +35 -0
- package/runtime/plugins/picasso-web-search/index.js +66 -0
- package/runtime/plugins/picasso-web-search/package.json +38 -0
- package/runtime/plugins/picasso-web-search/router.js +155 -0
- package/runtime/plugins/picasso-x-reader/circle.js +324 -0
- package/runtime/plugins/picasso-x-reader/command.js +169 -0
- package/runtime/plugins/picasso-x-reader/index.js +170 -0
- package/runtime/plugins/picasso-x-reader/package.json +39 -0
- package/runtime/plugins/picasso-x-reader/read.js +370 -0
- package/runtime/plugins/picasso-x-reader/rewrite.js +38 -0
- package/runtime/plugins/picasso-x-reader/settings.js +131 -0
- package/runtime/plugins/picasso-x-reader/urls.js +154 -0
- package/runtime/plugins/picasso-x-reader/xmd.js +512 -0
- package/runtime/relay/attachment-text.mjs +737 -0
- package/runtime/relay/companion/companion.css +126 -0
- package/runtime/relay/companion/companion.js +648 -0
- package/runtime/relay/companion/icon.svg +5 -0
- package/runtime/relay/companion/index.html +25 -0
- package/runtime/relay/companion/manifest.json +13 -0
- package/runtime/relay/companion/sw.js +41 -0
- package/runtime/relay/main.mjs +37252 -0
- package/runtime/relay/model-listing.mjs +614 -0
- package/runtime/relay/versions-routes.mjs +1198 -0
- package/runtime/verify-plugins.mjs +205 -0
- package/runtime-lock/package-lock.json +10779 -0
- package/runtime-lock/package.json +7 -0
- package/scripts/install-runtime.mjs +400 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1081 @@
|
|
|
1
|
+
// @picasso/model-router — B12 automatic per-request model routing.
|
|
2
|
+
//
|
|
3
|
+
// Replaces "one default model for everything" with a deterministic policy: when `mode: "route"`, every
|
|
4
|
+
// top-level (non-subagent) session is routed per prompt — a rule ladder classifies each admitted
|
|
5
|
+
// `user/message` and the matching tier's `{provider, model, reasoningEffort?}` is installed for the next
|
|
6
|
+
// request. An explicit pick (create-time `model`, `session/selectModel`, a composer override) pins the
|
|
7
|
+
// session and the router stays quiet for it afterwards.
|
|
8
|
+
//
|
|
9
|
+
// TRAP (verified in dsh-api-session-controller `SessionCommandController.selectModel`): the public
|
|
10
|
+
// `session/selectModel` Remote first installs the selection via `agents.selectForNextRequest` and then
|
|
11
|
+
// fires `agentDefaultModel.saveSelection` — routing through it would silently rewrite the user's persisted
|
|
12
|
+
// default model on every pick. This plugin therefore calls the lower-level, non-persisting seam itself:
|
|
13
|
+
//
|
|
14
|
+
// ctx.get('sessionController').agents.selectForNextRequest(agent, selection)
|
|
15
|
+
//
|
|
16
|
+
// which appends the same durable `model/selection` event and updates `selection.current`, and never
|
|
17
|
+
// touches `agentDefaultModel`. The plugin fails closed when that private seam is absent.
|
|
18
|
+
//
|
|
19
|
+
// Policy lives in the `picasso-model-router` settings namespace; every field is `.volatile()` so the
|
|
20
|
+
// relay's `PUT /routing/policy` (a `settings/mutate` per top-level key) applies live and survives restart.
|
|
21
|
+
// The child-agent half of the policy is dsh-owned (`subagent-model-selection-settings` → the recorded
|
|
22
|
+
// `subagent/model-selection-policy` projection), written by the relay — this plugin never writes it.
|
|
23
|
+
//
|
|
24
|
+
// Session semantics ("Auto"): `session/create` drops unknown request keys, so the relay's `routing:'auto'`
|
|
25
|
+
// flag cannot reach the session. The plugin therefore treats every top-level session as Auto-eligible
|
|
26
|
+
// while `mode === 'route'`: sessions without an explicit pick are classified per prompt, and any explicit
|
|
27
|
+
// pick (observed as a `model/selection` the router did not write) pins the session. On resume, a recorded
|
|
28
|
+
// selection outside the ladder is treated as a user pin; one inside the ladder is treated as router-owned
|
|
29
|
+
// state so routed threads keep routing after a restart (a recorded explicit pick of a ladder route is the
|
|
30
|
+
// documented edge — it re-classifies on the next prompt).
|
|
31
|
+
//
|
|
32
|
+
// Ordering: `user/message` is classified when admitted and its pick is applied immediately when the Agent
|
|
33
|
+
// is live; the `agent/pre-step` waterfall is the deterministic backstop — it flushes any pending selection
|
|
34
|
+
// before prompt assembly reads `selection.current`, so a race between the event and step-1 assembly can
|
|
35
|
+
// never leak the unrouted model into a request.
|
|
36
|
+
//
|
|
37
|
+
// Events read: `session/created`, `session/event` (`user/message`, `model/selection`, `llm/retry`,
|
|
38
|
+
// `turn/start`, `turn/end`, `tool/call`, `tool-workflow/run-start`), `agent/pre-step`, `session/disposed`.
|
|
39
|
+
//
|
|
40
|
+
// Remote surface (`modelRouter/*`): `policy`, `status`, `pin` — read/diagnostic seams the relay or tests
|
|
41
|
+
// drive; routing itself needs no calls.
|
|
42
|
+
|
|
43
|
+
import { createRequire } from 'node:module';
|
|
44
|
+
import { delimiter, join } from 'node:path';
|
|
45
|
+
import { readFileSync, realpathSync, statSync } from 'node:fs';
|
|
46
|
+
import { homedir } from 'node:os';
|
|
47
|
+
|
|
48
|
+
import {
|
|
49
|
+
ConnectionFallback,
|
|
50
|
+
RECORD_TYPE as CONNECTION_RECORD_TYPE,
|
|
51
|
+
connectionsFile,
|
|
52
|
+
llmProbe,
|
|
53
|
+
routeIdentity,
|
|
54
|
+
normalizeSettings as normalizeConnectionSettings,
|
|
55
|
+
sessionIdOf,
|
|
56
|
+
settingsReader as connectionsSettingsReader,
|
|
57
|
+
writeSettings as writeConnectionSettings,
|
|
58
|
+
} from './connections.js';
|
|
59
|
+
|
|
60
|
+
export * from './connections.js';
|
|
61
|
+
|
|
62
|
+
export const name = 'picasso-model-router';
|
|
63
|
+
export const dshTarget = '0.2.2-rc.3';
|
|
64
|
+
/** The Remote service key / settings namespace this plugin answers to. */
|
|
65
|
+
export const SERVICE_KEY = 'modelRouter';
|
|
66
|
+
export const SETTINGS_NS = 'picasso-model-router';
|
|
67
|
+
const REMOTE_METHOD_DESCRIPTOR = '@deepseek-ai/dsh-typert-protocol/remote-methods';
|
|
68
|
+
|
|
69
|
+
// ── Config schema (settings/mutate on `picasso-model-router`) ─────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* `@deepseek-ai/schemastery` from the dsh install that hosts this plugin — the same trick
|
|
73
|
+
* @picasso/trackers and @picasso/auth-bridge use: the Host's own argv[1] (the launched dsh bin) anchors
|
|
74
|
+
* `createRequire`, with `dsh` on PATH as the runner-up. `undefined` outside a dsh Host — the plugin then
|
|
75
|
+
* runs schema-less (config passes through raw and `settings/mutate` declines).
|
|
76
|
+
*/
|
|
77
|
+
function loadSchemastery() {
|
|
78
|
+
const mod = loadFromDsh('@deepseek-ai/schemastery', (found) => typeof (found?.default ?? found)?.object === 'function');
|
|
79
|
+
return mod === undefined ? undefined : mod.default ?? mod;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** One package from the dsh install hosting this plugin (argv[1] anchor, then `dsh` on PATH), or undefined. */
|
|
83
|
+
function loadFromDsh(specifier, accept) {
|
|
84
|
+
const anchors = [];
|
|
85
|
+
if (typeof process.argv?.[1] === 'string' && process.argv[1]) anchors.push(process.argv[1]);
|
|
86
|
+
for (const dir of (process.env.PATH ?? '').split(delimiter).filter(Boolean)) anchors.push(join(dir, 'dsh'));
|
|
87
|
+
for (const anchor of anchors) {
|
|
88
|
+
try {
|
|
89
|
+
const require = createRequire(realpathSync(anchor));
|
|
90
|
+
const mod = require(require.resolve(specifier));
|
|
91
|
+
if (accept(mod)) return mod;
|
|
92
|
+
} catch {
|
|
93
|
+
// not a dsh install (or an ESM-only entry) — try the next anchor
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function buildConfig(z) {
|
|
100
|
+
/** One ladder tier: a catalog route ({provider, model}) with an optional reasoning effort. */
|
|
101
|
+
const tier = z.object({
|
|
102
|
+
/** Tier id rules and presetDefaults name. */
|
|
103
|
+
id: z.string().min(1).required(),
|
|
104
|
+
provider: z.string().min(1).required(),
|
|
105
|
+
model: z.string().min(1).required(),
|
|
106
|
+
/** Reasoning effort id; dropped (not fatal) when the model doesn't accept it. */
|
|
107
|
+
reasoningEffort: z.string().min(1),
|
|
108
|
+
});
|
|
109
|
+
/** A deterministic classifier rule; first matching rule wins. A rule with no match fields matches all. */
|
|
110
|
+
const rule = z.object({
|
|
111
|
+
/** The ladder tier this rule routes to. */
|
|
112
|
+
tier: z.string().min(1).required(),
|
|
113
|
+
/** Total text length bounds (characters). */
|
|
114
|
+
minChars: z.natural(),
|
|
115
|
+
maxChars: z.natural(),
|
|
116
|
+
/** Content-block types (besides text) the message must carry — `image`, `file`, … */
|
|
117
|
+
attachments: z.array(z.string().min(1)),
|
|
118
|
+
/** true → only a message admitted while a turn is still running (steer/follow-up). */
|
|
119
|
+
steer: z.boolean(),
|
|
120
|
+
/** true → only while the current turn has seen a tool call or tool-workflow run. */
|
|
121
|
+
toolWorkflow: z.boolean(),
|
|
122
|
+
/** `source.kind` values the admitted message may carry (e.g. `user`); absent matches any. */
|
|
123
|
+
source: z.array(z.string().min(1)),
|
|
124
|
+
/** ECMAScript regex matched against the message text; invalid patterns make the rule inert. */
|
|
125
|
+
pattern: z.string().min(1),
|
|
126
|
+
});
|
|
127
|
+
return z.object({
|
|
128
|
+
/** `off` is byte-identical to today: no listeners act, no selection is ever written. */
|
|
129
|
+
mode: z.union([z.const('off'), z.const('route')]).volatile().default('off'),
|
|
130
|
+
/** Ordered tiers, cheap → strong; escalation walks the order, capped at the last. */
|
|
131
|
+
ladder: z.array(tier).volatile().default([]),
|
|
132
|
+
/** Classifier rules evaluated in order; the first match picks the tier, else the ladder's first tier. */
|
|
133
|
+
rules: z.array(rule).volatile().default([]),
|
|
134
|
+
/** Agent-preset id (`persona-*`, `ptc`, …) → ladder tier applied at create when no model was named. */
|
|
135
|
+
presetDefaults: z.dict(z.string().min(1)).volatile().default({}),
|
|
136
|
+
/** After `maxRetries` is exhausted, escalate exactly one tier per failure class per turn. */
|
|
137
|
+
escalateOnLlmRetry: z.boolean().volatile().default(true),
|
|
138
|
+
/** JSON file whose policy fields override the ones above, read live (the relay saves it when settings/mutate is refused). */
|
|
139
|
+
policyFile: z.string().min(1),
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const schemastery = loadSchemastery();
|
|
144
|
+
/**
|
|
145
|
+
* The settings schema for `picasso-model-router`: `settings/describe` renders it, `settings/mutate`
|
|
146
|
+
* writes each field live (all volatile). `undefined` when no bundled schemastery resolves.
|
|
147
|
+
*/
|
|
148
|
+
export let Config = schemastery === undefined ? undefined : buildConfig(schemastery);
|
|
149
|
+
|
|
150
|
+
/** A volatile config ref (`{get,set,[write]}`) unwraps to its snapshot; plain values pass through. */
|
|
151
|
+
/**
|
|
152
|
+
* A `.volatile()` field's live value. The pinned engine (0.2.2) hands each one over as a cosmokit ref: `{get}` plus a
|
|
153
|
+
* `Symbol(cosmokit.volatile.write)` writer, with no `set`; older test cells carry `{get, set}`.
|
|
154
|
+
*/
|
|
155
|
+
function unref(value) {
|
|
156
|
+
if (!isRecord(value) || typeof value.get !== 'function') return value;
|
|
157
|
+
const writer = Object.getOwnPropertySymbols(value).some((symbol) => /volatile/u.test(symbol.description ?? ''));
|
|
158
|
+
return writer || typeof value.set === 'function' ? value.get() : value;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isRecord(value) {
|
|
162
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ── Policy ──────────────────────────────────────────────────────────────────────────────────────────
|
|
166
|
+
|
|
167
|
+
export class ModelRouterError extends Error {
|
|
168
|
+
constructor(code, message, details = {}) {
|
|
169
|
+
super(message);
|
|
170
|
+
this.name = 'RemoteError';
|
|
171
|
+
this.code = `modelRouter/${code}`;
|
|
172
|
+
this.details = details;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function cleanTier(raw) {
|
|
177
|
+
if (!isRecord(raw)) return undefined;
|
|
178
|
+
const provider = typeof raw.provider === 'string' ? raw.provider : '';
|
|
179
|
+
const model = typeof raw.model === 'string' ? raw.model : '';
|
|
180
|
+
const id = typeof raw.id === 'string' && raw.id ? raw.id : `${provider}/${model}`;
|
|
181
|
+
if (!provider || !model) return undefined;
|
|
182
|
+
const effort = typeof raw.reasoningEffort === 'string' && raw.reasoningEffort ? raw.reasoningEffort : undefined;
|
|
183
|
+
return { id, provider, model, ...(effort === undefined ? {} : { reasoningEffort: effort }) };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function cleanRule(raw) {
|
|
187
|
+
if (!isRecord(raw) || typeof raw.tier !== 'string' || !raw.tier) return undefined;
|
|
188
|
+
const out = { tier: raw.tier };
|
|
189
|
+
if (Number.isSafeInteger(raw.minChars)) out.minChars = raw.minChars;
|
|
190
|
+
if (Number.isSafeInteger(raw.maxChars)) out.maxChars = raw.maxChars;
|
|
191
|
+
if (Array.isArray(raw.attachments)) {
|
|
192
|
+
const kinds = raw.attachments.filter((kind) => typeof kind === 'string' && kind);
|
|
193
|
+
if (kinds.length > 0) out.attachments = kinds;
|
|
194
|
+
}
|
|
195
|
+
if (raw.steer === true) out.steer = true;
|
|
196
|
+
if (raw.toolWorkflow === true) out.toolWorkflow = true;
|
|
197
|
+
if (Array.isArray(raw.source)) {
|
|
198
|
+
const kinds = raw.source.filter((kind) => typeof kind === 'string' && kind);
|
|
199
|
+
if (kinds.length > 0) out.source = kinds;
|
|
200
|
+
}
|
|
201
|
+
if (typeof raw.pattern === 'string' && raw.pattern) {
|
|
202
|
+
try {
|
|
203
|
+
out.pattern = new RegExp(raw.pattern);
|
|
204
|
+
} catch {
|
|
205
|
+
// an un-compilable pattern makes the rule inert, not the policy
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** The policy's normalized runtime view; built fresh per read so live (volatile) writes apply at once. */
|
|
213
|
+
export function readPolicy(config) {
|
|
214
|
+
const mode = unref(config?.mode) === 'route' ? 'route' : 'off';
|
|
215
|
+
const ladder = (Array.isArray(unref(config?.ladder)) ? unref(config?.ladder) : []).map(cleanTier).filter((tier) => tier !== undefined);
|
|
216
|
+
const rules = (Array.isArray(unref(config?.rules)) ? unref(config?.rules) : []).map(cleanRule).filter((rule) => rule !== undefined);
|
|
217
|
+
const defaults = {};
|
|
218
|
+
const rawDefaults = unref(config?.presetDefaults);
|
|
219
|
+
if (isRecord(rawDefaults)) {
|
|
220
|
+
for (const [preset, tier] of Object.entries(rawDefaults)) {
|
|
221
|
+
if (preset && typeof tier === 'string' && tier) defaults[preset] = tier;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
mode,
|
|
226
|
+
ladder,
|
|
227
|
+
rules,
|
|
228
|
+
presetDefaults: defaults,
|
|
229
|
+
escalateOnLlmRetry: unref(config?.escalateOnLlmRetry) !== false,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const POLICY_KEYS = ['mode', 'ladder', 'rules', 'presetDefaults', 'escalateOnLlmRetry'];
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* `config` with the policy fields of `config.policyFile` laid over it. The file is re-read only when it changes (inode +
|
|
237
|
+
* size + mtime; the relay replaces it by rename); a missing or unreadable file leaves `config` as it is.
|
|
238
|
+
*/
|
|
239
|
+
export function withPolicyFile(config, cache = {}) {
|
|
240
|
+
const file = unref(config?.policyFile);
|
|
241
|
+
if (typeof file !== 'string' || file === '') return config;
|
|
242
|
+
let stamp;
|
|
243
|
+
try {
|
|
244
|
+
const stat = statSync(file);
|
|
245
|
+
stamp = `${stat.ino}:${stat.size}:${stat.mtimeMs}`;
|
|
246
|
+
} catch {
|
|
247
|
+
return config;
|
|
248
|
+
}
|
|
249
|
+
if (cache.file !== file || cache.stamp !== stamp) {
|
|
250
|
+
let saved = {};
|
|
251
|
+
try {
|
|
252
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
253
|
+
if (isRecord(parsed)) saved = Object.fromEntries(POLICY_KEYS.filter((key) => key in parsed).map((key) => [key, parsed[key]]));
|
|
254
|
+
} catch {
|
|
255
|
+
saved = {};
|
|
256
|
+
}
|
|
257
|
+
Object.assign(cache, { file, stamp, saved });
|
|
258
|
+
}
|
|
259
|
+
return Object.keys(cache.saved).length === 0 ? config : { ...Object.fromEntries(POLICY_KEYS.map((key) => [key, config?.[key]])), ...cache.saved };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export function selectionEqual(a, b) {
|
|
263
|
+
return a !== undefined && b !== undefined
|
|
264
|
+
&& a.provider === b.provider
|
|
265
|
+
&& a.model === b.model
|
|
266
|
+
&& a.reasoningEffort === b.reasoningEffort;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Text + attachment kinds of a durable `user/message`'s data. */
|
|
270
|
+
export function messageShape(data) {
|
|
271
|
+
const content = Array.isArray(data?.content) ? data.content : [];
|
|
272
|
+
let text = '';
|
|
273
|
+
const attachments = new Set();
|
|
274
|
+
for (const block of content) {
|
|
275
|
+
if (!isRecord(block)) continue;
|
|
276
|
+
if (block.type === 'text' && typeof block.text === 'string') text += block.text;
|
|
277
|
+
else if (typeof block.type === 'string' && block.type) attachments.add(block.type);
|
|
278
|
+
}
|
|
279
|
+
return { text, attachments, sourceKind: typeof data?.source?.kind === 'string' ? data.source.kind : undefined };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** First matching rule wins; a rule with no match fields is a catch-all (matches every message). */
|
|
283
|
+
export function ruleMatches(rule, shape, session) {
|
|
284
|
+
if (rule.minChars !== undefined && shape.text.length < rule.minChars) return false;
|
|
285
|
+
if (rule.maxChars !== undefined && shape.text.length > rule.maxChars) return false;
|
|
286
|
+
if (rule.attachments !== undefined && !rule.attachments.some((kind) => shape.attachments.has(kind))) return false;
|
|
287
|
+
if (rule.steer === true && !session.running) return false;
|
|
288
|
+
if (rule.toolWorkflow === true && !session.toolWorkflow) return false;
|
|
289
|
+
if (rule.source !== undefined && !rule.source.includes(shape.sourceKind ?? '')) return false;
|
|
290
|
+
if (rule.pattern !== undefined && !rule.pattern.test(shape.text)) return false;
|
|
291
|
+
return true;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** The tier a message takes: the first matching rule's tier when it exists in the ladder, else ladder[0]. */
|
|
295
|
+
export function classify(policy, shape, session) {
|
|
296
|
+
if (policy.ladder.length === 0) return undefined;
|
|
297
|
+
for (const rule of policy.rules) {
|
|
298
|
+
if (!ruleMatches(rule, shape, session)) continue;
|
|
299
|
+
const tier = policy.ladder.find((entry) => entry.id === rule.tier);
|
|
300
|
+
if (tier !== undefined) return tier;
|
|
301
|
+
}
|
|
302
|
+
return policy.ladder[0];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Failure class for one-tier-per-class-per-turn dedup: context-length vs rate-limit vs other. */
|
|
306
|
+
export function failureClass(error) {
|
|
307
|
+
const text = typeof error === 'string' ? error : isRecord(error) ? `${error.message ?? ''} ${error.code ?? ''} ${error.status ?? ''}` : String(error ?? '');
|
|
308
|
+
if (/context.?length|too.?long|token.?limit|context.?window/i.test(text)) return 'context';
|
|
309
|
+
if (/\b429\b|rate.?limit|throttl|\b5\d\d\b|overloaded|unavailable/i.test(text)) return 'rate';
|
|
310
|
+
return 'other';
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// ── Router ──────────────────────────────────────────────────────────────────────────────────────────
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The routing core. `deps` are resolved per call so tests can inject fakes and the plugin fails closed
|
|
317
|
+
* when dsh doesn't compose a piece (no `sessionController.agents.selectForNextRequest`, no `llm`).
|
|
318
|
+
*/
|
|
319
|
+
export class ModelRouter {
|
|
320
|
+
/**
|
|
321
|
+
* @param {object} options
|
|
322
|
+
* @param {() => object} options.policy - the normalized policy view (re-read per event for live edits).
|
|
323
|
+
* @param {(sessionId: string, tier: object) => Promise<object|undefined>} options.install - validate +
|
|
324
|
+
* install the tier's selection for the session's next request; resolves to the installed selection,
|
|
325
|
+
* or `undefined` when the agent isn't live yet (the pick stays pending for `agent/pre-step`).
|
|
326
|
+
* @param {(line: string) => void} [options.log] - warn sink.
|
|
327
|
+
*/
|
|
328
|
+
constructor({ policy, install, log }) {
|
|
329
|
+
this.policy = policy;
|
|
330
|
+
this.install = install;
|
|
331
|
+
this.log = typeof log === 'function' ? log : () => undefined;
|
|
332
|
+
/** sessionId → {pinned, pending, lastRouted, lastRoutedTier, running, toolWorkflow, turn, escalated}. */
|
|
333
|
+
this.sessions = new Map();
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
state(sessionId) {
|
|
337
|
+
let found = this.sessions.get(sessionId);
|
|
338
|
+
if (found === undefined) {
|
|
339
|
+
found = { pinned: false, pending: undefined, lastRouted: undefined, lastRoutedTier: undefined, running: false, toolWorkflow: false, turn: undefined, escalated: new Set(), classified: new Set(), installing: 0 };
|
|
340
|
+
this.sessions.set(sessionId, found);
|
|
341
|
+
}
|
|
342
|
+
return found;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* `session/created`: per-preset defaults queue a tier for the first request; a recorded off-ladder
|
|
347
|
+
* selection (resume) marks the session pinned. Subagent sessions are never touched — dsh's
|
|
348
|
+
* `subagent-model-selection-settings` + recorded `subagent/model-selection-policy` own their routes.
|
|
349
|
+
*/
|
|
350
|
+
onCreated(session, { recordedSelection, agentPreset } = {}) {
|
|
351
|
+
if (this.policy().mode !== 'route') return;
|
|
352
|
+
const id = session?.header?.id ?? session?.id;
|
|
353
|
+
if (typeof id !== 'string' || !id) return;
|
|
354
|
+
// Subagent sessions are never touched — dsh's `subagent-model-selection-settings` + the recorded
|
|
355
|
+
// `subagent/model-selection-policy` projection own child routes (child's own policy wins over parent's).
|
|
356
|
+
if (session?.header?.origin === 'subagent') return;
|
|
357
|
+
const state = this.state(id);
|
|
358
|
+
if (recordedSelection !== undefined && this.policy().ladder.every((tier) => !selectionEqual(tier, recordedSelection))) {
|
|
359
|
+
// The session recorded a route the ladder doesn't offer: a user's explicit pick survived restart.
|
|
360
|
+
state.pinned = true;
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const preset = typeof agentPreset === 'string' ? agentPreset : session?.header?.agentPreset;
|
|
364
|
+
const tierId = typeof preset === 'string' ? this.policy().presetDefaults[preset] : undefined;
|
|
365
|
+
const tier = this.policy().ladder.find((entry) => entry.id === tierId);
|
|
366
|
+
if (tier !== undefined) {
|
|
367
|
+
state.pending = { tier, reason: 'preset' };
|
|
368
|
+
void this.applyPending(id);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* `session/event`: classify admitted prompts, pin on foreign selections, escalate on exhausted retries,
|
|
374
|
+
* and track the turn shape the classifier's steer/toolWorkflow rules read.
|
|
375
|
+
*/
|
|
376
|
+
onEvent(session, event) {
|
|
377
|
+
const id = session?.header?.id ?? session?.id;
|
|
378
|
+
if (typeof id !== 'string' || !id || !isRecord(event)) return;
|
|
379
|
+
const state = this.state(id);
|
|
380
|
+
const turn = event?.data?.turn;
|
|
381
|
+
switch (event.type) {
|
|
382
|
+
case 'turn/start':
|
|
383
|
+
state.running = true;
|
|
384
|
+
state.toolWorkflow = false;
|
|
385
|
+
state.turn = typeof turn === 'object' && turn !== null ? turn.id : turn;
|
|
386
|
+
state.escalated.clear();
|
|
387
|
+
state.classified.clear();
|
|
388
|
+
return;
|
|
389
|
+
case 'turn/end':
|
|
390
|
+
state.running = false;
|
|
391
|
+
return;
|
|
392
|
+
case 'tool/call':
|
|
393
|
+
case 'tool-workflow/run-start':
|
|
394
|
+
state.toolWorkflow = true;
|
|
395
|
+
return;
|
|
396
|
+
default:
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
if (event.type === 'model/selection') {
|
|
400
|
+
const data = event.data;
|
|
401
|
+
if (selectionEqual(data, state.lastRouted)) return; // our own write
|
|
402
|
+
if (state.installing > 0) {
|
|
403
|
+
// Written by this router's own install (the engine appends it synchronously, before the install returns).
|
|
404
|
+
state.lastRouted = { provider: data?.provider, model: data?.model, ...(data?.reasoningEffort === undefined ? {} : { reasoningEffort: data.reasoningEffort }) };
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
// An explicit pick (`session/selectModel`, a create-time model, a composer override) pins the
|
|
408
|
+
// session: the router never re-routes it and drops any queued pick. Tracked even while `off`, so a
|
|
409
|
+
// live policy flip to `route` never re-routes a session the user already picked for.
|
|
410
|
+
state.pinned = true;
|
|
411
|
+
state.pending = undefined;
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
if (this.policy().mode !== 'route' || session?.header?.origin === 'subagent') return;
|
|
415
|
+
if (state.pinned) return;
|
|
416
|
+
if (event.type === 'user/message') {
|
|
417
|
+
// Already classified from the step's claimed input at its pre-step (the step that runs it is routed).
|
|
418
|
+
if (typeof event.data?.id === 'string' && state.classified.has(event.data.id)) return;
|
|
419
|
+
const tier = classify(this.policy(), messageShape(event.data), state);
|
|
420
|
+
if (tier !== undefined) {
|
|
421
|
+
state.pending = { tier, reason: 'classify' };
|
|
422
|
+
void this.applyPending(id);
|
|
423
|
+
}
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (event.type === 'llm/retry') {
|
|
427
|
+
const data = event.data;
|
|
428
|
+
const retry = typeof data?.retry === 'number' ? data.retry : 0;
|
|
429
|
+
const maxRetries = typeof data?.maxRetries === 'number' ? data.maxRetries : 0;
|
|
430
|
+
if (!this.policy().escalateOnLlmRetry || retry < maxRetries) return;
|
|
431
|
+
const kind = failureClass(data?.error);
|
|
432
|
+
if (state.escalated.has(kind)) return; // one tier per failure class per turn
|
|
433
|
+
state.escalated.add(kind);
|
|
434
|
+
const tier = this.nextTier(state, data?.provider);
|
|
435
|
+
if (tier !== undefined) {
|
|
436
|
+
state.pending = { tier, reason: 'escalate' };
|
|
437
|
+
void this.applyPending(id);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** The ladder tier above the session's current one; `provider` (from llm/retry) locates it when the
|
|
443
|
+
* router hasn't routed this session yet. Off-ladder sessions escalate onto the ladder's bottom tier;
|
|
444
|
+
* the ladder's top is the cap (returns undefined there). */
|
|
445
|
+
nextTier(state, provider) {
|
|
446
|
+
const ladder = this.policy().ladder;
|
|
447
|
+
if (ladder.length === 0) return undefined;
|
|
448
|
+
let index = state.lastRoutedTier === undefined ? -1 : ladder.findIndex((tier) => tier.id === state.lastRoutedTier);
|
|
449
|
+
if (index < 0 && typeof provider === 'string') index = ladder.findIndex((tier) => tier.provider === provider);
|
|
450
|
+
const next = ladder[index + 1];
|
|
451
|
+
return index >= ladder.length - 1 ? undefined : next;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Install `state.pending`'s tier for the next request; a no-op while pinned or when nothing is queued.
|
|
456
|
+
* The pick is claimed before the async install so concurrent flushes (event + pre-step) can't install
|
|
457
|
+
* it twice; an agent that isn't live yet requeues it (unless a newer pick landed meanwhile).
|
|
458
|
+
*/
|
|
459
|
+
async applyPending(sessionId, options = {}) {
|
|
460
|
+
const state = this.sessions.get(sessionId);
|
|
461
|
+
const pending = state?.pending;
|
|
462
|
+
if (state === undefined || pending === undefined || state.pinned) return;
|
|
463
|
+
state.pending = undefined; // claim
|
|
464
|
+
let resolved;
|
|
465
|
+
state.installing += 1;
|
|
466
|
+
try {
|
|
467
|
+
resolved = await this.install(sessionId, pending.tier, options);
|
|
468
|
+
} catch (error) {
|
|
469
|
+
this.log(`${name}: routing ${sessionId} → ${pending.tier.id} failed (${error instanceof Error ? error.message : String(error)}); leaving the session's current model.`);
|
|
470
|
+
return;
|
|
471
|
+
} finally {
|
|
472
|
+
state.installing -= 1;
|
|
473
|
+
}
|
|
474
|
+
if (resolved === undefined) {
|
|
475
|
+
if (state.pending === undefined && !state.pinned) state.pending = pending; // agent not live yet — pre-step flushes it
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
state.lastRouted = resolved;
|
|
479
|
+
state.lastRoutedTier = pending.tier.id;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* `agent/pre-step` waterfall: the deterministic backstop. Installs any pending pick BEFORE the rest of
|
|
484
|
+
* the step chain (prompt assembly reads `selection.current` inside `next()`), then always proceeds.
|
|
485
|
+
*/
|
|
486
|
+
async preStep({ agent, messages }, next) {
|
|
487
|
+
const id = agent?.id ?? agent?.session?.header?.id;
|
|
488
|
+
try {
|
|
489
|
+
if (typeof id === 'string' && this.policy().mode === 'route' && agent?.session?.header?.origin !== 'subagent') {
|
|
490
|
+
const state = this.state(id);
|
|
491
|
+
// The engine fixes this step's route before its pre-step and appends the prompt's `user/message` only after
|
|
492
|
+
// it, so the person's message the step claimed is classified here, and the pick applies to this very step.
|
|
493
|
+
const prompt = Array.isArray(messages) ? messages.filter((message) => isRecord(message) && message.role === 'user' && message.source?.kind === 'user').at(-1) : undefined;
|
|
494
|
+
if (prompt !== undefined && !state.pinned && !(typeof prompt.id === 'string' && state.classified.has(prompt.id))) {
|
|
495
|
+
if (typeof prompt.id === 'string') state.classified.add(prompt.id);
|
|
496
|
+
const tier = classify(this.policy(), messageShape(prompt), state);
|
|
497
|
+
if (tier !== undefined) state.pending = { tier, reason: 'classify' };
|
|
498
|
+
}
|
|
499
|
+
await this.applyPending(id, { immediate: true });
|
|
500
|
+
}
|
|
501
|
+
} catch (error) {
|
|
502
|
+
this.log(`${name}: pre-step routing for ${String(id)} skipped (${error instanceof Error ? error.message : String(error)}).`);
|
|
503
|
+
}
|
|
504
|
+
return next();
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** `modelRouter/status {sessionId}` — what the router knows about a thread, for chips/diagnostics. */
|
|
508
|
+
status(sessionId) {
|
|
509
|
+
if (typeof sessionId !== 'string' || !sessionId) throw new ModelRouterError('bad-request', 'status needs a sessionId.', {});
|
|
510
|
+
const state = this.sessions.get(sessionId);
|
|
511
|
+
return {
|
|
512
|
+
sessionId,
|
|
513
|
+
mode: this.policy().mode,
|
|
514
|
+
session: state === undefined ? 'unrouted' : state.pinned ? 'pinned' : 'routed',
|
|
515
|
+
...(state?.lastRouted === undefined ? {} : { lastRouted: { ...state.lastRouted } }),
|
|
516
|
+
...(state?.lastRoutedTier === undefined ? {} : { tier: state.lastRoutedTier }),
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/** `modelRouter/pin {sessionId}` — mark a session router-quiet without a durable selection write. */
|
|
521
|
+
pin(sessionId) {
|
|
522
|
+
if (typeof sessionId !== 'string' || !sessionId) throw new ModelRouterError('bad-request', 'pin needs a sessionId.', {});
|
|
523
|
+
const state = this.state(sessionId);
|
|
524
|
+
state.pinned = true;
|
|
525
|
+
state.pending = undefined;
|
|
526
|
+
return { sessionId, pinned: true };
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/** The Remote-facing half: `modelRouter/*` methods plus the live policy view. */
|
|
531
|
+
export class ModelRouterService {
|
|
532
|
+
constructor(router, read, connections) {
|
|
533
|
+
this.router = router;
|
|
534
|
+
this.read = read;
|
|
535
|
+
/** `{fallback: ConnectionFallback, read, write}` — the ordered model connections. */
|
|
536
|
+
this.connectionsApi = connections;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/** `modelRouter/connections` — `{policy, connections: [{id, label, kind, models, status, reason?, since?}]}` in order. */
|
|
540
|
+
async connections() {
|
|
541
|
+
if (this.connectionsApi === undefined) throw new ModelRouterError('seam-unavailable', 'model connections are unavailable.', {});
|
|
542
|
+
return this.withEfforts(await this.connectionsApi.fallback.view());
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* The view plus the reasoning levels kept in the same file, shared with the CLI: `efforts` (a default per model),
|
|
547
|
+
* `alwaysThinks` (models that refuse thinking off) and `defaultEffort` (the default for every model, when set).
|
|
548
|
+
*/
|
|
549
|
+
withEfforts(view) {
|
|
550
|
+
const current = this.connectionsApi.read();
|
|
551
|
+
return {
|
|
552
|
+
...view,
|
|
553
|
+
efforts: { ...(current.efforts ?? {}) },
|
|
554
|
+
alwaysThinks: [...(current.alwaysThinks ?? [])],
|
|
555
|
+
...(current.defaultEffort === undefined ? {} : { defaultEffort: current.defaultEffort }),
|
|
556
|
+
modelPickScope: current.modelPickScope === 'session' ? 'session' : 'default',
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* `modelRouter/setConnections {policy?, order?, equivalents?}` — persist and answer the new view. Called two ways:
|
|
562
|
+
* the Host gateway (Mac relay, `POST /remote/modelRouter/setConnections {args:{policy, order}}`) spreads the args
|
|
563
|
+
* object positionally by these parameter names; the SDK server (TUI) passes the one object. The parameters must
|
|
564
|
+
* stay plain identifiers (no defaults or destructuring): the gateway reads them from the source and refuses
|
|
565
|
+
* anything else (`gateway/signature-invalid`).
|
|
566
|
+
*/
|
|
567
|
+
async setConnections(policy, order, equivalents, efforts, defaultEffort, alwaysThinks, modelPickScope) {
|
|
568
|
+
if (this.connectionsApi === undefined) throw new ModelRouterError('seam-unavailable', 'model connections are unavailable.', {});
|
|
569
|
+
const params = remoteParams(policy, { policy, order, equivalents, efforts, defaultEffort, alwaysThinks, modelPickScope });
|
|
570
|
+
if (!isRecord(params)) {
|
|
571
|
+
throw new ModelRouterError('bad-request', 'setConnections takes {policy?, order?, equivalents?, efforts?, defaultEffort?, alwaysThinks?, modelPickScope?}.', {});
|
|
572
|
+
}
|
|
573
|
+
// `modelPickScope`: what the next model pick applies to (`default`: this chat and new sessions; `session`: this chat).
|
|
574
|
+
if (params.modelPickScope !== undefined && params.modelPickScope !== 'default' && params.modelPickScope !== 'session') {
|
|
575
|
+
throw new ModelRouterError('bad-request', 'modelPickScope must be "default" or "session".', {});
|
|
576
|
+
}
|
|
577
|
+
// `defaultEffort`: the level every model runs at unless this chat or the model's own default says otherwise (each
|
|
578
|
+
// request clamps it to the levels the model offers). `null` clears it; never off.
|
|
579
|
+
if (params.defaultEffort !== undefined && params.defaultEffort !== null && !isThinkingLevel(params.defaultEffort)) {
|
|
580
|
+
throw new ModelRouterError('bad-request', 'defaultEffort must be a reasoning level (never off) or null.', {});
|
|
581
|
+
}
|
|
582
|
+
// `alwaysThinks` adds models a provider refused to run with thinking off (a 400 the Mac or the CLI saw); never removes.
|
|
583
|
+
if (params.alwaysThinks !== undefined) {
|
|
584
|
+
if (!Array.isArray(params.alwaysThinks) || !params.alwaysThinks.every((ref) => typeof ref === 'string' && ref.includes('/'))) {
|
|
585
|
+
throw new ModelRouterError('bad-request', 'alwaysThinks must be a list of "<route>/<model>".', {});
|
|
586
|
+
}
|
|
587
|
+
params.alwaysThinks = [...(this.connectionsApi.read().alwaysThinks ?? []), ...params.alwaysThinks];
|
|
588
|
+
}
|
|
589
|
+
// `efforts` is a patch: `{"<route>/<model>": "<level>"}` sets a model's default level, `null` clears it.
|
|
590
|
+
if (params.efforts !== undefined) {
|
|
591
|
+
if (!isRecord(params.efforts)) throw new ModelRouterError('bad-request', 'efforts must map "<route>/<model>" to a level or null.', {});
|
|
592
|
+
const merged = { ...(this.connectionsApi.read().efforts ?? {}) };
|
|
593
|
+
for (const [ref, level] of Object.entries(params.efforts)) {
|
|
594
|
+
if (level === null) delete merged[ref];
|
|
595
|
+
else if (typeof level === 'string' && !/^(?:off|none|disabled)$/u.test(level)) merged[ref] = level;
|
|
596
|
+
else throw new ModelRouterError('bad-request', 'a default level must be a level the model offers, never off.', { ref });
|
|
597
|
+
}
|
|
598
|
+
params.efforts = merged;
|
|
599
|
+
}
|
|
600
|
+
if (params.policy !== undefined && !['ask', 'ask-managed', 'auto'].includes(params.policy)) {
|
|
601
|
+
throw new ModelRouterError('bad-request', 'policy must be ask, ask-managed or auto.', { policy: params.policy });
|
|
602
|
+
}
|
|
603
|
+
if (params.order !== undefined && (!Array.isArray(params.order) || !params.order.every((id) => typeof id === 'string' && id))) {
|
|
604
|
+
throw new ModelRouterError('bad-request', 'order must be a list of connection ids.', {});
|
|
605
|
+
}
|
|
606
|
+
const current = this.connectionsApi.read();
|
|
607
|
+
const next = { ...current, ...params };
|
|
608
|
+
if (next.defaultEffort === null) delete next.defaultEffort;
|
|
609
|
+
this.connectionsApi.write(normalizeConnectionSettings(next));
|
|
610
|
+
return this.withEfforts(await this.connectionsApi.fallback.view());
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** `modelRouter/checkAccess {id, model?}` — one tiny probe request: `{id, model, ok, reason?, kind?, code?, status?, latencyMs}`. */
|
|
614
|
+
async checkAccess(id, model) {
|
|
615
|
+
if (this.connectionsApi === undefined) throw new ModelRouterError('seam-unavailable', 'model connections are unavailable.', {});
|
|
616
|
+
try {
|
|
617
|
+
return await this.connectionsApi.fallback.checkAccess(remoteParams(id, { id, model }));
|
|
618
|
+
} catch (error) {
|
|
619
|
+
if (error instanceof TypeError) throw new ModelRouterError('bad-request', error.message, {});
|
|
620
|
+
throw error;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/** `modelRouter/policy` — the live, normalized policy (what `settings/describe` renders, resolved). */
|
|
625
|
+
policy() {
|
|
626
|
+
return this.read();
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/** `modelRouter/status {sessionId}`; also takes the bare id or the `{sessionId}` object. */
|
|
630
|
+
status(sessionId) {
|
|
631
|
+
return this.router.status(remoteSessionId(sessionId));
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/** `modelRouter/pin {sessionId}`; also takes the bare id or the `{sessionId}` object. */
|
|
635
|
+
pin(sessionId) {
|
|
636
|
+
return this.router.pin(remoteSessionId(sessionId));
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* One Remote method, two call shapes: the gateway's positional spread (`first` is the first named field) or a single
|
|
642
|
+
* params object (`first` is that object and nothing follows). `named` holds the positional fields; absent ones drop.
|
|
643
|
+
*/
|
|
644
|
+
/** A reasoning level word that turns thinking on (the same shape the CLI accepts; off words are never a default). */
|
|
645
|
+
function isThinkingLevel(level) {
|
|
646
|
+
return typeof level === 'string' && /^[a-z][a-z0-9-]{0,15}$/u.test(level) && !/^(?:off|none|disabled|minimal-off)$/u.test(level);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function remoteParams(first, named) {
|
|
650
|
+
const rest = Object.entries(named).slice(1);
|
|
651
|
+
if (isRecord(first) && rest.every(([, value]) => value === undefined)) return first;
|
|
652
|
+
if (first === undefined && rest.every(([, value]) => value === undefined)) return {};
|
|
653
|
+
return Object.fromEntries(Object.entries(named).filter(([, value]) => value !== undefined));
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** `'id'` or `{sessionId: 'id'}` → `'id'`; anything else passes through for the router's own validation. */
|
|
657
|
+
function remoteSessionId(value) {
|
|
658
|
+
return isRecord(value) && Object.hasOwn(value, 'sessionId') ? value.sessionId : value;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const REMOTE_METHODS = ['policy', 'status', 'pin', 'connections', 'setConnections', 'checkAccess'];
|
|
662
|
+
|
|
663
|
+
Object.defineProperty(ModelRouterService.prototype, REMOTE_METHOD_DESCRIPTOR, {
|
|
664
|
+
enumerable: false,
|
|
665
|
+
value: Object.freeze({ version: 1, methods: Object.freeze(REMOTE_METHODS.map((method) => Object.freeze({ method, invocation: Object.freeze({ kind: 'direct' }) }))) }),
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
/** Cordis entry. Provides `modelRouter`, bound for the Typert gateway (`typertRemote`). */
|
|
669
|
+
/** The live `llm-pi-ai` providers dict from the loader's entries (patch overlays included), or undefined. */
|
|
670
|
+
function llmPiAiProviders(ctx) {
|
|
671
|
+
let loader;
|
|
672
|
+
try {
|
|
673
|
+
loader = ctx.root?.loader ?? ctx.loader;
|
|
674
|
+
} catch {
|
|
675
|
+
return undefined;
|
|
676
|
+
}
|
|
677
|
+
if (loader === undefined || loader === null || typeof loader.entries !== 'function') return undefined;
|
|
678
|
+
try {
|
|
679
|
+
for (const entry of loader.entries()) {
|
|
680
|
+
const options = entry?.options;
|
|
681
|
+
if (options?.id === 'llm-pi-ai' || options?.name === '@deepseek-ai/dsh-llm-pi-ai') {
|
|
682
|
+
if (options.disabled === true) continue;
|
|
683
|
+
return options.config?.providers;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
} catch {
|
|
687
|
+
return undefined;
|
|
688
|
+
}
|
|
689
|
+
return undefined;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** `refs:` of `$DSH_HOME/.credentials.yaml` as name → value (flat ` NAME: value` lines); empty when unreadable. */
|
|
693
|
+
function storedCredentialRefs(home = process.env.DSH_HOME || join(homedir(), '.dsh')) {
|
|
694
|
+
let text;
|
|
695
|
+
try {
|
|
696
|
+
text = readFileSync(join(home, '.credentials.yaml'), 'utf8');
|
|
697
|
+
} catch {
|
|
698
|
+
return {};
|
|
699
|
+
}
|
|
700
|
+
const refs = {};
|
|
701
|
+
let inRefs = false;
|
|
702
|
+
for (const line of text.split(/\r?\n/u)) {
|
|
703
|
+
if (/^refs:\s*$/u.test(line)) { inRefs = true; continue; }
|
|
704
|
+
if (/^\S/u.test(line)) { inRefs = false; continue; }
|
|
705
|
+
if (!inRefs) continue;
|
|
706
|
+
const match = /^ {2}([A-Za-z0-9_]+):\s*(.*?)\s*$/u.exec(line);
|
|
707
|
+
if (match) refs[match[1]] = match[2].replace(/^(['"])(.*)\1$/u, '$2');
|
|
708
|
+
}
|
|
709
|
+
return refs;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
export function apply(ctx, config = {}, deps = {}) {
|
|
713
|
+
const policyFileCache = {};
|
|
714
|
+
const log = deps.log ?? (typeof ctx.logger?.warn === 'function' ? (line) => ctx.logger.warn(line) : (line) => console.warn(line));
|
|
715
|
+
const policyView = () => readPolicy(withPolicyFile(config, policyFileCache));
|
|
716
|
+
const get = deps.get ?? ((key) => ctx.get?.(key));
|
|
717
|
+
|
|
718
|
+
/**
|
|
719
|
+
* The non-persisting selection seam — `ApiSessionAgentController.selectForNextRequest`. Reachable
|
|
720
|
+
* through `sessionController.agents` (the Remote service's controller member); `ctx.get('agents')`
|
|
721
|
+
* is only the live registry and carries no selection API. Missing → install fails closed.
|
|
722
|
+
*/
|
|
723
|
+
const controller = () => {
|
|
724
|
+
const found = get('sessionController')?.agents;
|
|
725
|
+
return isRecord(found) && typeof found.selectForNextRequest === 'function' ? found : undefined;
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
const resolveAgent = deps.resolveAgent ?? (async (sessionId) => {
|
|
729
|
+
const agents = hostService('sessionController')?.agents;
|
|
730
|
+
if (isRecord(agents) && typeof agents.resolveAgent === 'function') {
|
|
731
|
+
const found = await agents.resolveAgent(sessionId);
|
|
732
|
+
return isRecord(found) && isRecord(found.agent) ? found.agent : undefined;
|
|
733
|
+
}
|
|
734
|
+
// No session controller (the CLI/TUI SDK profile): the live agent registry.
|
|
735
|
+
const live = hostService('agents')?.get?.(sessionId);
|
|
736
|
+
return isRecord(live) ? live : undefined;
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
/** A service by key: `ctx.get` first; a direct `ctx.<key>` read throws on a Context that did not inject it. */
|
|
740
|
+
const hostService = (key) => {
|
|
741
|
+
try {
|
|
742
|
+
const found = get(key);
|
|
743
|
+
if (found !== undefined && found !== null) return found;
|
|
744
|
+
} catch {
|
|
745
|
+
// not provided through get — try the property
|
|
746
|
+
}
|
|
747
|
+
try {
|
|
748
|
+
return ctx[key];
|
|
749
|
+
} catch {
|
|
750
|
+
return undefined; // "cannot get property … without inject"
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
const llmRegistry = () => (typeof deps.llm === 'function' ? deps.llm() : deps.llm) ?? hostService('llm');
|
|
754
|
+
|
|
755
|
+
/** The catalog check `requireModel` performs (`modelAvailable`): provider listed + model advertised. */
|
|
756
|
+
const modelAvailable = deps.modelAvailable ?? (async ({ provider, model }) => {
|
|
757
|
+
const registry = llmRegistry();
|
|
758
|
+
if (registry === undefined || registry === null) return false;
|
|
759
|
+
if (!registry.listProviders().some((entry) => entry.id === provider)) return false;
|
|
760
|
+
const models = await registry.listModels(provider);
|
|
761
|
+
return models.some((entry) => entry.id === model);
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
/**
|
|
765
|
+
* AO-007.07: a server-side route-policy/allowance probe, consulted after the catalog check. When
|
|
766
|
+
* managed inference is active the dsh llm registry (or `deps.routeAllowed` in tests) answers
|
|
767
|
+
* whether `{provider, model}` sits inside the approved route/budget; a surface that exists and
|
|
768
|
+
* refuses (or errors) FAILS CLOSED — a tier switch or retry-escalation never installs on a
|
|
769
|
+
* client-side pick alone. No allowance surface → the catalog check remains the authority.
|
|
770
|
+
*/
|
|
771
|
+
const routeAllowed = deps.routeAllowed ?? (async ({ provider, model }) => {
|
|
772
|
+
const registry = llmRegistry();
|
|
773
|
+
if (registry === undefined || registry === null) return process.env.PICASSO_MANAGED_MODE !== '1';
|
|
774
|
+
const check = registry.routeAllowed ?? registry.allowance ?? registry.allowsRoute ?? registry.checkRoute;
|
|
775
|
+
// Managed inference without a server allowance surface cannot vouch for any client-side tier pick: fail closed.
|
|
776
|
+
// BYO and local sessions have no server route policy, so the catalog check stays their authority.
|
|
777
|
+
if (typeof check !== 'function') return process.env.PICASSO_MANAGED_MODE !== '1';
|
|
778
|
+
try {
|
|
779
|
+
const verdict = await check.call(registry, { provider, model });
|
|
780
|
+
if (isRecord(verdict)) return verdict.allowed !== false;
|
|
781
|
+
return verdict !== false;
|
|
782
|
+
} catch {
|
|
783
|
+
return false; // an allowance surface that cannot answer denies
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* The checks every install runs before a route is selected: catalog → server route allowance →
|
|
789
|
+
* `resolveCallConfig` (an effort the model refuses is dropped, not fatal). Returns the normalized selection.
|
|
790
|
+
*/
|
|
791
|
+
const validatedSelection = async (sessionId, tier) => {
|
|
792
|
+
const selection = { provider: tier.provider, model: tier.model };
|
|
793
|
+
if (!(await modelAvailable(selection))) {
|
|
794
|
+
throw new ModelRouterError('model-unavailable', `route "${tier.provider}/${tier.model}" is not in the session model catalog.`, { sessionId, tier: tier.id });
|
|
795
|
+
}
|
|
796
|
+
// AO-007.07: managed inference's server-side allowance vetoes the tier — a rate-limit retry
|
|
797
|
+
// can never escalate outside the approved route or budget this way.
|
|
798
|
+
if (!(await routeAllowed(selection))) {
|
|
799
|
+
throw new ModelRouterError('route-denied', `route "${tier.provider}/${tier.model}" is outside the server route policy or allowance.`, { sessionId, tier: tier.id });
|
|
800
|
+
}
|
|
801
|
+
const registry = llmRegistry();
|
|
802
|
+
if (registry === undefined || registry === null || typeof registry.resolveCallConfig !== 'function') {
|
|
803
|
+
throw new ModelRouterError('seam-unavailable', 'model routing is unavailable: the llm service is not mounted.', { sessionId });
|
|
804
|
+
}
|
|
805
|
+
let resolved;
|
|
806
|
+
try {
|
|
807
|
+
resolved = await registry.resolveCallConfig({
|
|
808
|
+
provider: tier.provider,
|
|
809
|
+
model: tier.model,
|
|
810
|
+
...(tier.reasoningEffort === undefined ? {} : { reasoningEffort: tier.reasoningEffort }),
|
|
811
|
+
});
|
|
812
|
+
} catch (error) {
|
|
813
|
+
if (tier.reasoningEffort === undefined) throw error;
|
|
814
|
+
// An effort the model doesn't accept is dropped, never a prompt failure.
|
|
815
|
+
resolved = await registry.resolveCallConfig({ provider: tier.provider, model: tier.model });
|
|
816
|
+
log(`${name}: tier "${tier.id}" effort "${tier.reasoningEffort}" rejected (${error instanceof Error ? error.message : String(error)}); routed without it.`);
|
|
817
|
+
}
|
|
818
|
+
return {
|
|
819
|
+
provider: resolved.provider,
|
|
820
|
+
model: resolved.model,
|
|
821
|
+
...(resolved.reasoningEffort === undefined ? {} : { reasoningEffort: resolved.reasoningEffort }),
|
|
822
|
+
};
|
|
823
|
+
};
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* Validate + install one tier for the next request, mirroring `selectModel` minus `saveSelection`:
|
|
827
|
+
* catalog check → server route allowance → `resolveCallConfig` (effort dropped on refusal, not
|
|
828
|
+
* fatal) → `selectForNextRequest`. Returns the installed selection, or undefined when the agent
|
|
829
|
+
* isn't live yet.
|
|
830
|
+
*/
|
|
831
|
+
const install = deps.install ?? (async (sessionId, tier, options = {}) => {
|
|
832
|
+
const agents = controller();
|
|
833
|
+
if (agents === undefined) {
|
|
834
|
+
// The pinned engine (0.2.2): no `selectForNextRequest`; the agent's own non-persisting `model.select` records
|
|
835
|
+
// the same durable `model/selection` (it never saves a default), validated first like every install.
|
|
836
|
+
const reachable = typeof hostService('sessionController')?.agents?.resolveAgent === 'function' || typeof hostService('agents')?.get === 'function';
|
|
837
|
+
if (deps.resolveAgent === undefined && !reachable) {
|
|
838
|
+
throw new ModelRouterError('seam-unavailable', 'model routing is unavailable: no session controller or agent registry is mounted.', { sessionId });
|
|
839
|
+
}
|
|
840
|
+
const agent = await resolveAgent(sessionId);
|
|
841
|
+
if (agent === undefined) return undefined;
|
|
842
|
+
if (typeof agent.model?.select !== 'function') {
|
|
843
|
+
throw new ModelRouterError('seam-unavailable', 'model routing is unavailable: no model selection seam is mounted.', { sessionId });
|
|
844
|
+
}
|
|
845
|
+
const run = async () => {
|
|
846
|
+
const selected = await validatedSelection(sessionId, tier);
|
|
847
|
+
agent.model.select(selected);
|
|
848
|
+
return selected;
|
|
849
|
+
};
|
|
850
|
+
const serial = hostService('sessionController')?.agents;
|
|
851
|
+
const selected = typeof serial?.serializeImageAdmission === 'function' ? await serial.serializeImageAdmission(agent, run) : await run();
|
|
852
|
+
if (options?.immediate === true) retargetStep(agent, selected);
|
|
853
|
+
return selected;
|
|
854
|
+
}
|
|
855
|
+
const agent = await resolveAgent(sessionId);
|
|
856
|
+
if (agent === undefined) return undefined;
|
|
857
|
+
const body = async () => {
|
|
858
|
+
const selected = await validatedSelection(sessionId, tier);
|
|
859
|
+
agents.selectForNextRequest(agent, selected);
|
|
860
|
+
return selected;
|
|
861
|
+
};
|
|
862
|
+
// dsh serializes model selection through the same admission chain `session/selectModel` uses; run
|
|
863
|
+
// inside it so a routed pick can never interleave a `model/selection` append with admission work.
|
|
864
|
+
return typeof agents.serializeImageAdmission === 'function' ? agents.serializeImageAdmission(agent, body) : body();
|
|
865
|
+
});
|
|
866
|
+
|
|
867
|
+
const router = deps.router ?? new ModelRouter({ policy: policyView, install, log });
|
|
868
|
+
|
|
869
|
+
// ── Ordered model connections with automatic fallback (connections.js) ──────────────────────────
|
|
870
|
+
const connectionsPath = typeof config?.connectionsFile === 'string' && config.connectionsFile ? config.connectionsFile : connectionsFile();
|
|
871
|
+
const readConnections = deps.connectionsSettings ?? connectionsSettingsReader(connectionsPath);
|
|
872
|
+
const writeConnections = deps.writeConnectionsSettings ?? ((value) => writeConnectionSettings(value, connectionsPath));
|
|
873
|
+
/** session id → the selection a connection switch is installing (so its `model/selection` is not a user pick). */
|
|
874
|
+
const connectionInstalls = new Map();
|
|
875
|
+
/** agent → the route object its current step was prepared with (`agent/pre-step` `modelSelection`). */
|
|
876
|
+
const stepSelections = new WeakMap();
|
|
877
|
+
/** session id → the last request route seen (`request/context`), the current-route backstop. */
|
|
878
|
+
const lastRequestRoute = new Map();
|
|
879
|
+
const appendRecord = deps.appendPluginRecord ?? (() => {
|
|
880
|
+
const mod = loadFromDsh('@deepseek-ai/dsh-session', (found) => typeof found?.appendPluginRecord === 'function');
|
|
881
|
+
return mod?.appendPluginRecord;
|
|
882
|
+
})();
|
|
883
|
+
|
|
884
|
+
/**
|
|
885
|
+
* Install a connection's route for the agent's next request without persisting a default: the router's own
|
|
886
|
+
* `selectForNextRequest` path on the Mac, the SDK server's live selection (`session/model/set`) on the CLI.
|
|
887
|
+
*/
|
|
888
|
+
const installConnection = deps.installConnection ?? (async (agent, selection, { immediate = false } = {}) => {
|
|
889
|
+
const sessionId = sessionIdOf(agent);
|
|
890
|
+
connectionInstalls.set(sessionId, selection);
|
|
891
|
+
try {
|
|
892
|
+
const agents = controller();
|
|
893
|
+
if (agents !== undefined) {
|
|
894
|
+
const state = router.state(sessionId);
|
|
895
|
+
const previous = state.lastRouted;
|
|
896
|
+
state.lastRouted = selection; // the router must not read the switch as a user pin
|
|
897
|
+
try {
|
|
898
|
+
const installed = await install(sessionId, { id: `connection:${selection.provider}`, ...selection });
|
|
899
|
+
state.lastRouted = installed ?? previous;
|
|
900
|
+
// dsh's installModelSelection applies `assembled` (snapshotted at prompt assembly) to each request; a
|
|
901
|
+
// retried step does not re-assemble, so a switch the retry must use is applied there too.
|
|
902
|
+
if (immediate && installed !== undefined && typeof agents.selectionFor === 'function') {
|
|
903
|
+
try {
|
|
904
|
+
const live = agents.selectionFor(agent);
|
|
905
|
+
if (live !== undefined && live !== null) live.assembled = installed;
|
|
906
|
+
} catch (error) {
|
|
907
|
+
log(`${name}: the switch applies from the next step (${error instanceof Error ? error.message : String(error)}).`);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
return installed;
|
|
911
|
+
} catch (error) {
|
|
912
|
+
state.lastRouted = previous;
|
|
913
|
+
throw error;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
let installed;
|
|
917
|
+
const live = hostService('picassoModelSelection');
|
|
918
|
+
if (live !== undefined && live !== null && typeof live.set === 'function') {
|
|
919
|
+
// The CLI/TUI SDK server's live selection (`session/model/set`).
|
|
920
|
+
const reply = await live.set({ sessionId, ...selection }, { immediate });
|
|
921
|
+
installed = reply?.selected ?? reply;
|
|
922
|
+
} else if (typeof agent?.model?.select === 'function') {
|
|
923
|
+
// The engine's own durable selection (`agent.model.select`, what `session/selectModel` calls): the Mac relay
|
|
924
|
+
// on 0.2.2, whose session controller has no `selectForNextRequest`.
|
|
925
|
+
const run = async () => {
|
|
926
|
+
const selected = await validatedSelection(sessionId, { id: `connection:${selection.provider}`, ...selection });
|
|
927
|
+
agent.model.select(selected);
|
|
928
|
+
return selected;
|
|
929
|
+
};
|
|
930
|
+
const serial = hostService('sessionController')?.agents;
|
|
931
|
+
installed = typeof serial?.serializeImageAdmission === 'function' ? await serial.serializeImageAdmission(agent, run) : await run();
|
|
932
|
+
} else {
|
|
933
|
+
throw new ModelRouterError('seam-unavailable', 'switching connections is unavailable: no model selection seam is mounted.', { sessionId });
|
|
934
|
+
}
|
|
935
|
+
if (immediate && installed !== undefined) retargetStep(agent, installed);
|
|
936
|
+
return installed;
|
|
937
|
+
} finally {
|
|
938
|
+
connectionInstalls.delete(sessionId);
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
/**
|
|
943
|
+
* The engine fixes a step's route before it runs (`agent/pre-step` carries that `modelSelection`, and a retried
|
|
944
|
+
* request reuses it), so a selection made from `agent/request-error` alone reaches only the next step. A switch the
|
|
945
|
+
* retry must use also retargets the running step's own selection object, captured at its pre-step.
|
|
946
|
+
*/
|
|
947
|
+
const retargetStep = (agent, selected) => {
|
|
948
|
+
const step = stepSelections.get(agent);
|
|
949
|
+
if (step === undefined || step === null || typeof step !== 'object') return;
|
|
950
|
+
try {
|
|
951
|
+
step.provider = selected.provider;
|
|
952
|
+
step.model = selected.model;
|
|
953
|
+
if (selected.reasoningEffort === undefined) delete step.reasoningEffort;
|
|
954
|
+
else step.reasoningEffort = selected.reasoningEffort;
|
|
955
|
+
} catch (error) {
|
|
956
|
+
log(`${name}: the switch applies from the next step (${error instanceof Error ? error.message : String(error)}).`);
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
|
|
960
|
+
const currentRoute = (agent) => {
|
|
961
|
+
const config = agent?.session?.requestHeader?.()?.config;
|
|
962
|
+
if (typeof config?.provider === 'string' && typeof config.model === 'string') return config;
|
|
963
|
+
return lastRequestRoute.get(sessionIdOf(agent));
|
|
964
|
+
};
|
|
965
|
+
|
|
966
|
+
const askUser = deps.askUser ?? (async (agent, question, signal) => {
|
|
967
|
+
const questions = hostService('userQuestions');
|
|
968
|
+
if (questions === undefined || typeof questions.ask !== 'function') return undefined;
|
|
969
|
+
try {
|
|
970
|
+
const answer = await questions.ask({ agent, questions: [question], ...(signal === undefined ? {} : { signal }) });
|
|
971
|
+
const item = (Array.isArray(answer?.answers) ? answer.answers : []).find((entry) => entry?.id === question.id);
|
|
972
|
+
return Array.isArray(item?.selected) ? item.selected[0] : undefined;
|
|
973
|
+
} catch (error) {
|
|
974
|
+
log(`${name}: the connection prompt could not be shown (${error instanceof Error ? error.message : String(error)}).`);
|
|
975
|
+
return undefined;
|
|
976
|
+
}
|
|
977
|
+
});
|
|
978
|
+
|
|
979
|
+
/**
|
|
980
|
+
* Endpoint + key fingerprint of one route, read from the live llm-pi-ai config (every patch layer applied) and the
|
|
981
|
+
* engine home's stored credential refs. Routes that share it (a `prism` and a `kiro-prism` on one endpoint and key)
|
|
982
|
+
* are listed as one connection.
|
|
983
|
+
*/
|
|
984
|
+
const routeIdentityOf = deps.routeIdentity ?? ((id) => {
|
|
985
|
+
const providers = llmPiAiProviders(ctx);
|
|
986
|
+
const profile = isRecord(providers) ? providers[id] : undefined;
|
|
987
|
+
if (!isRecord(profile)) return undefined;
|
|
988
|
+
return routeIdentity(profile, (ref) => process.env[ref] || storedCredentialRefs()[ref]);
|
|
989
|
+
});
|
|
990
|
+
|
|
991
|
+
const fallback = deps.connectionFallback ?? new ConnectionFallback({
|
|
992
|
+
settings: readConnections,
|
|
993
|
+
identity: routeIdentityOf,
|
|
994
|
+
providers: async () => {
|
|
995
|
+
const registry = llmRegistry();
|
|
996
|
+
return registry === undefined || registry === null || typeof registry.listProviders !== 'function' ? [] : registry.listProviders();
|
|
997
|
+
},
|
|
998
|
+
models: async (id) => {
|
|
999
|
+
const registry = llmRegistry();
|
|
1000
|
+
if (registry === undefined || registry === null || typeof registry.listModels !== 'function') return [];
|
|
1001
|
+
const models = await registry.listModels(id);
|
|
1002
|
+
return (Array.isArray(models) ? models : []).map((entry) => entry?.id).filter((entry) => typeof entry === 'string' && entry);
|
|
1003
|
+
},
|
|
1004
|
+
install: installConnection,
|
|
1005
|
+
current: currentRoute,
|
|
1006
|
+
probe: deps.probe ?? llmProbe(llmRegistry),
|
|
1007
|
+
ask: askUser,
|
|
1008
|
+
note: (agent, event) => {
|
|
1009
|
+
if (typeof appendRecord !== 'function') return;
|
|
1010
|
+
try {
|
|
1011
|
+
appendRecord(agent.session, CONNECTION_RECORD_TYPE, { event });
|
|
1012
|
+
} catch (error) {
|
|
1013
|
+
log(`${name}: connection note not recorded (${error instanceof Error ? error.message : String(error)}).`);
|
|
1014
|
+
}
|
|
1015
|
+
},
|
|
1016
|
+
log,
|
|
1017
|
+
});
|
|
1018
|
+
|
|
1019
|
+
const service = new ModelRouterService(router, policyView, { fallback, read: readConnections, write: writeConnections });
|
|
1020
|
+
service.typertRemote = Object.freeze({ service, serviceKey: SERVICE_KEY, namespace: SERVICE_KEY });
|
|
1021
|
+
ctx.provide(SERVICE_KEY, service);
|
|
1022
|
+
|
|
1023
|
+
const observeConnections = (session, event) => {
|
|
1024
|
+
const id = session?.header?.id ?? session?.id;
|
|
1025
|
+
if (typeof id !== 'string' || !isRecord(event)) return;
|
|
1026
|
+
if (event.type === 'request/context' && typeof event.data?.provider === 'string' && typeof event.data?.model === 'string') {
|
|
1027
|
+
lastRequestRoute.set(id, { provider: event.data.provider, model: event.data.model });
|
|
1028
|
+
} else if (event.type === 'model/selection') {
|
|
1029
|
+
const pending = connectionInstalls.get(id);
|
|
1030
|
+
const ours = pending !== undefined && pending.provider === event.data?.provider;
|
|
1031
|
+
// A pick the switch didn't make is the user's: their route wins and the fallback bookkeeping ends.
|
|
1032
|
+
if (!ours) fallback.forget(id);
|
|
1033
|
+
} else if (event.type === 'assistant/message') {
|
|
1034
|
+
const route = lastRequestRoute.get(id);
|
|
1035
|
+
if (route !== undefined) fallback.recovered(route.provider);
|
|
1036
|
+
}
|
|
1037
|
+
};
|
|
1038
|
+
|
|
1039
|
+
const disposers = [];
|
|
1040
|
+
disposers.push(
|
|
1041
|
+
ctx.on('session/created', (session) => {
|
|
1042
|
+
let recorded;
|
|
1043
|
+
let agentPreset;
|
|
1044
|
+
try {
|
|
1045
|
+
const projections = ctx.sessionProjections ?? get('sessionProjections');
|
|
1046
|
+
const projection = projections?.stateOf?.(session, 'modelSelection');
|
|
1047
|
+
recorded = projection?.pending ?? projection?.lastUsed ?? undefined;
|
|
1048
|
+
const preset = projections?.stateOf?.(session, 'agentPreset');
|
|
1049
|
+
agentPreset = typeof preset === 'string' ? preset : undefined;
|
|
1050
|
+
} catch {
|
|
1051
|
+
recorded = undefined;
|
|
1052
|
+
}
|
|
1053
|
+
router.onCreated(session, { recordedSelection: recorded, agentPreset });
|
|
1054
|
+
}),
|
|
1055
|
+
ctx.on('session/event', (session, event) => {
|
|
1056
|
+
router.onEvent(session, event);
|
|
1057
|
+
observeConnections(session, event);
|
|
1058
|
+
}),
|
|
1059
|
+
ctx.on('agent/pre-step', async (args, next) => {
|
|
1060
|
+
if (args?.agent !== undefined && args.modelSelection !== null && typeof args.modelSelection === 'object') stepSelections.set(args.agent, args.modelSelection);
|
|
1061
|
+
try {
|
|
1062
|
+
await fallback.maybeRestore(args?.agent);
|
|
1063
|
+
} catch (error) {
|
|
1064
|
+
log(`${name}: moving back to the earlier connection skipped (${error instanceof Error ? error.message : String(error)}).`);
|
|
1065
|
+
}
|
|
1066
|
+
return router.preStep(args, next);
|
|
1067
|
+
}),
|
|
1068
|
+
ctx.on('agent/request-error', (payload, next) => fallback.onRequestError(payload, next)),
|
|
1069
|
+
ctx.on('session/disposed', (session) => {
|
|
1070
|
+
const id = session?.header?.id ?? session?.id;
|
|
1071
|
+
if (typeof id === 'string') {
|
|
1072
|
+
router.sessions.delete(id);
|
|
1073
|
+
fallback.forget(id);
|
|
1074
|
+
lastRequestRoute.delete(id);
|
|
1075
|
+
}
|
|
1076
|
+
}),
|
|
1077
|
+
);
|
|
1078
|
+
return () => {
|
|
1079
|
+
for (const dispose of disposers) dispose?.();
|
|
1080
|
+
};
|
|
1081
|
+
}
|