dsh-model-router 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  A small plugin for the [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) that stops treating every model call the same. It splits your session into two roles:
4
4
 
5
- - **The planner** — your main agent — always runs on `deepseek-v4-pro`. That's where the thinking happens: understanding what you want, designing the approach, reviewing results, writing the final answer.
6
- - **The executors** — every subagent it delegates to — always run on `deepseek-v4-flash`. That's where the work happens: writing code, running commands, iterating on builds.
5
+ - **The planner** — your main agent — defaults to `deepseek-flash` (V4.1 Flash, native multimodal). That's where the thinking happens: understanding what you want, designing the approach, reviewing results, writing the final answer.
6
+ - **The executors** — every subagent it delegates to — default to `deepseek-flash` as well.
7
7
 
8
- The idea is simple: pro is the better thinker, flash is fast and cheap at grinding through implementation. You get the careful planning of the big model without paying pro prices for every single tool call.
8
+ Both roles share the same model until V4.1-Pro launches (V4.1 Flash already beats V4-Pro on performance, cost, and speed, so DeepSeek is retiring `deepseek-v4-pro` / `deepseek-v4-flash` / `deepseek-v4-flash-vision-exp` — all three now route to V4.1 Flash server-side). The role split stays in the config, so flipping the planner back to Pro later is a one-line change. You keep the careful plan/delegate/review rhythm without paying Pro prices for every tool call.
9
9
 
10
10
  ## Install
11
11
 
@@ -29,7 +29,7 @@ Once it's in, restart the profile. You should see the row under `model-router` i
29
29
 
30
30
  Three small surfaces, one rule:
31
31
 
32
- 1. **Request routing** — every model request gets stamped with a role. Root agents get `deepseek-v4-pro`; delegation children (`subagent`, `subagent_fork`, workflow workers, ralph rounds) get `deepseek-v4-flash`. The rewrite sits at the outermost layer of the request pipeline, so it wins — even over the harness's own default model (which is `deepseek-v4-flash` out of the box) and over whatever model you pick in the UI for the session. That's intentional: it's the "enforce" knob.
32
+ 1. **Request routing** — every model request gets stamped with a role. Root agents get the planner route; delegation children (`subagent`, `subagent_fork`, workflow workers, ralph rounds) get the executor route. Both default to `deepseek-flash` (V4.1 Flash) until V4.1-Pro launches, so today the stamp unifies while the role split stays configurable. The rewrite sits at the outermost layer of the request pipeline, so it wins — even over the harness's own default model and over whatever model you pick in the UI for the session. That's intentional: it's the "enforce" knob.
33
33
  2. **A prompt section** — a short note that renders before the agent's persona, telling the planner: you're the thinker, delegate the implementation. Without this, the model tends to just do everything itself.
34
34
  3. **A skill** — the `pro-flash-routing` skill shows up in the session's skill catalog and spells out the working rhythm: plan, delegate, review, report. Same convention, but loadable on demand when the agent wants details.
35
35
 
@@ -50,7 +50,7 @@ The router always stamps `provider` + `model`. `reasoningEffort` and `maxTokens`
50
50
 
51
51
  Routing is on by default. Two ways to switch it off:
52
52
 
53
- - **GUI (Settings → Plugins → dsh-model-router):** the plugin registers a live
53
+ - **GUI (Settings → Plugins → Model router):** the plugin registers a live
54
54
  settings section; flip `enabled` off. It applies immediately (no restart),
55
55
  persists in `settings.yaml` under `model-router:`, and unregisters the prompt
56
56
  section and the skill too. Flip it back on and everything returns. The same
@@ -68,15 +68,18 @@ Since v0.5.0 the package ships a browser half, and the harness serves it
68
68
  automatically — no extra config. After installing (or updating to) v0.5.0+
69
69
  and restarting the profile, **Settings → Plugins** shows a **Model router**
70
70
  card with a live **Enabled** switch, an "Overridden" badge and **Reset to
71
- default** button once you've changed it, and a read-only view of the current
72
- planner/executor routes and mode. Flipping the switch applies immediately (no
71
+ default** button once you've changed it, a read-only view of the current
72
+ planner/executor routes and mode, and a Vision section (v0.6.0+) with its own
73
+ live **Vision** switch, **Reset vision to default** button, and read-only
74
+ vision-model line. Flipping the switch applies immediately (no
73
75
  restart) and persists in `settings.yaml` under `model-router:` — the same
74
76
  mechanism the GUI toggle described above uses.
75
77
 
76
78
  One harness-wide caveat (it applies to **all** settings pages — Models,
77
79
  Plugins, everything — not to this plugin specifically): the harness serves
78
80
  settings pages only to **loopback** browsers (`localhost` / `127.x`). A remote
79
- browser sees "settings are unavailable" instead of the card. Fallbacks that
81
+ browser may not get the settings page at all; where the card does render it
82
+ shows a read-only note. Fallbacks that
80
83
  work everywhere:
81
84
 
82
85
  - **Patch row** — set `enabled: false` in the profile's `cordis.patch.yml`
@@ -93,21 +96,32 @@ and off by default** (`vision.enabled: false`). Two ways to turn it on:
93
96
  no restart, persists in `settings.yaml`), or
94
97
  - **Patch row / settings:** set `vision.enabled: true` on the plugin's config.
95
98
 
96
- When enabled, **any request whose messages carry an image is stamped with the
97
- vision model** — `deepseek-v4-flash-vision-exp` from `deepseek-official` by
98
- default **from every role**: the root (planner) agent and all delegated
99
+ When enabled, **any request sent while the session log carries an image is stamped with the
100
+ vision model** — `deepseek-flash` from `deepseek-official` by
101
+ default (V4.1 Flash is natively multimodal, so this matches the role routes
102
+ unless you pin something else) — **from every role**: the root (planner) agent and all delegated
99
103
  subagents. Everything else keeps the pro/flash role routing untouched. The
100
104
  vision branch is checked first, so a subagent reading an image still lands on
101
105
  the vision model, not on flash. Optional `vision.reasoningEffort` /
102
106
  `vision.maxTokens` pins work exactly like the per-role ones. Image detection
103
- reads the session log: any user message or tool result carrying an image keeps
104
- the session on the vision model while that image is in context.
107
+ reads the session event log (`user/message`, `assistant/message`, and
108
+ `tool/result`, including images nested in `tool-result` blocks): once an image
109
+ appears anywhere in the log, subsequent requests stay on the vision model for
110
+ the rest of the session (sticky — the image stays in request context until
111
+ compaction or pruning drops it). Detection shapes are verified against real
112
+ session logs: `user/message` carries the image at `data.content`, while
113
+ `assistant/message` and `tool/result` carry it at `data.message.content`
114
+ (tool results nest it inside a `tool-result` block). Possible follow-up:
115
+ optional `vision.historyLimit` to bound the scan to the trailing N events
116
+ (unset keeps today's sticky entire-log scan).
105
117
 
106
118
  The plugin ships the support in its own `cordis.patch.yml`:
107
119
 
108
- - a **catalog entry** for the vision model on the `llm-deepseek` row (with
109
- `inputModalities: [text, image]`, plus raised `contextWindow`/`maxTokens`
110
- restated for the pro/flash rows), and
120
+ - a **catalog entry** for `deepseek-flash` on the `llm-deepseek` row (with
121
+ `inputModalities: [text, image]`, `contextWindow: 1000000`, `maxTokens:
122
+ 384000`), plus the retired `deepseek-v4-flash` / `deepseek-v4-pro` /
123
+ `deepseek-v4-flash-vision-exp` ids kept as compat aliases through the
124
+ transition, and
111
125
  - **raised `attachment-local` image admission limits** so normal screenshots
112
126
  (~8K, 15MB) attach without being rejected (`maxImageDimension: 8192`,
113
127
  `maxImagePixels: 100000000`, `maxImageBytes: 15728640`).
@@ -127,14 +141,14 @@ All configuration lives on the plugin row. Patch it in the profile's `cordis.pat
127
141
  - patch:
128
142
  - id: model-router
129
143
  config:
130
- planner: # root-agent route
144
+ planner: # root-agent route (deepseek-flash until V4.1-Pro lands)
131
145
  provider: deepseek-official
132
- model: deepseek-v4-pro
146
+ model: deepseek-flash
133
147
  reasoningEffort: high # off | low | high | max (omit to inherit)
134
148
  maxTokens: 8192 # output cap (omit to inherit)
135
149
  executor: # subagent route
136
150
  provider: deepseek-official
137
- model: deepseek-v4-flash
151
+ model: deepseek-flash
138
152
  reasoningEffort: high
139
153
  escalateOnError: true # after a failed step…
140
154
  escalateTo: max # …bump effort for the next request
@@ -148,15 +162,16 @@ All configuration lives on the plugin row. Patch it in the profile's `cordis.pat
148
162
 
149
163
  **Error-driven escalation** (`escalateOnError`): when a route's agent hits a failed tool step, the *next* request bumps to `escalateTo` and wears off after `recoverySteps` clean steps. It's deterministic and stateless — the router folds the session log per request, so only prior steps are considered (a failure can't escalate the very request that caused it). It's a per-route knob: enable it on the executor to make flash think harder after a flubbed execution step, without touching the baseline.
150
164
 
151
- The defaults are exactly the table at the top of this page. To switch the router off for a session, disable the row (`disabled: true`) or remove the plugin — `dsh plugin --profile web remove dsh-model-router`.
165
+ The defaults are exactly the list at the top of this page. To switch the router off for a session, disable the row (`disabled: true`) or remove the plugin — `dsh plugin --profile web remove dsh-model-router`.
152
166
 
153
- ## Reduce pro token usage
167
+ ## Reduce token usage
154
168
 
155
- The planner is the expensive model, so most of the savings come from shrinking its spend:
169
+ With both roles on `deepseek-flash`, the bill is already far below the old
170
+ pro-based setup — most of the remaining savings come from shrinking spend:
156
171
 
157
- - **Lower `reasoningEffort`.** The harness default runs pro at `max`, which produces a lot of reasoning tokens. `high` (or `low`) on the planner route keeps most of the quality at a fraction of the cost.
172
+ - **Lower `reasoningEffort`.** The harness default runs at `max`, which produces a lot of reasoning tokens. `high` (or `low`) on a route keeps most of the quality at a fraction of the cost.
158
173
  - **Cap output** with `maxTokens` on the planner route so a verbose turn can't balloon.
159
- - **Reserve pro for planning** with `mode: plan` — trivial Q&A and execution-style turns stop hitting pro at all.
174
+ - **Reserve the planner route for planning** with `mode: plan` — trivial Q&A and execution-style turns stop hitting the planner route at all (matters again once V4.1-Pro lands and the routes split).
160
175
  - **Keep the planner's context lean.** Input tokens dominate after reasoning. Delegate aggressively and trust the subagent's report; don't re-read big files or full transcripts on the planner. Use targeted reads and let auto-compaction (`/compact`) trim history.
161
176
  - **Tune the host pruner.** The tool-result pruner truncates oversized results before they reach the model (default ~8 KB); lowering `tool-result-pruner` → `thresholdChars` trims more planner input. That's harness config, not this plugin's row.
162
177
  - **Exploit DeepSeek's context cache.** Repeated prefixes are served from cache at a big discount, so keep the system prompt and conversation prefix stable between turns.
@@ -165,14 +180,30 @@ The first three are one-line changes on this plugin's row; the last three are di
165
180
 
166
181
  ## Does it work?
167
182
 
168
- I verified it against a real session log. Run a task that makes the agent plan and delegate, then check which models actually made the requests:
183
+ Verify against a real session log. Run a task that makes the agent plan and delegate, then check which models actually made the requests:
169
184
 
170
185
  ```bash
171
186
  zstd -d -c "$DSH_HOME"/sessions/<workspace>/<session>/session.jsonl.zstd \
172
- | grep -o '"model":"deepseek-v4-[a-z]*"' | sort | uniq -c
187
+ | grep '"type":"assistant/message"' \
188
+ | grep -o '"model":"deepseek-[a-z-]*"' | sort | uniq -c
173
189
  ```
174
190
 
175
- Planner messages come back as `deepseek-v4-pro`; subagent messages as `deepseek-v4-flash`. In my test: 9 pro requests in the planner's session, 6 flash in the subagent's.
191
+ Two details matter in that command: filtering to `assistant/message` counts only real model responses
192
+ (the raw log also records `request/header`, session-title, and web-search calls, which would inflate
193
+ the numbers), and the `[a-z-]*` pattern keeps hyphenated model names
194
+ (`deepseek-flash`, legacy `deepseek-v4-flash-vision-exp`) intact — a plain
195
+ `[a-z]*` silently truncates them.
196
+
197
+ Since v0.7.0 both roles log `deepseek-flash` (unified until V4.1-Pro lands and
198
+ the planner route points at it). For reference, the v4-era split re-verified
199
+ against production logs: a root session with delegations showed 170 pro / 182
200
+ flash responses, and every child session (`delegationDepth >= 1`) showed flash
201
+ only; with vision routing enabled, an image-heavy session logged 508
202
+ `deepseek-v4-flash-vision-exp` responses.
203
+
204
+ One operational note: the routing rewrite is loaded at harness boot. After updating the plugin (e.g.
205
+ 0.6.3 → 0.7.0), restart the profile — a session that keeps running across the update can keep behaving
206
+ per the old code until the process reloads.
176
207
 
177
208
  ## Development
178
209
 
@@ -187,6 +218,19 @@ npm run build
187
218
 
188
219
  The `prepare` script builds `lib/` automatically, which is what makes the git install work without shipping build artifacts in the repo. The `dsh.bundle` field in `package.json` is what tells `dsh plugin` how to compose the plugin into a profile.
189
220
 
221
+ ## Releasing
222
+
223
+ Pushing a tag alone does not make a release. Every version needs all four:
224
+
225
+ ```bash
226
+ npm run typecheck && npm test && npm run build # green first
227
+ git tag vX.Y.Z && git push origin main && git push origin vX.Y.Z
228
+ gh release create vX.Y.Z --title "vX.Y.Z" --notes "<CHANGELOG entry>"
229
+ npm publish --access public # needs login + 2FA (--otp) or a publish token
230
+ ```
231
+
232
+ Keep `vX.Y.Z` flagged as Latest (`gh release edit vX.Y.Z --latest`) when backfilling older ones. Note: `scripts/publish.sh` is a one-shot bootstrap for a brand-new repo, not the per-release path.
233
+
190
234
  ## License
191
235
 
192
236
  MIT
package/cordis.patch.yml CHANGED
@@ -9,16 +9,17 @@
9
9
  - id: model-router
10
10
  name: 'dsh-model-router'
11
11
  config:
12
+ # Both roles default to deepseek-flash (V4.1 Flash, native multimodal).
12
13
  # The session's root agent: planning, design, review, synthesis.
13
14
  planner:
14
15
  provider: deepseek-official
15
- model: deepseek-v4-pro
16
+ model: deepseek-flash
16
17
  # reasoningEffort: high # optional: off | low | high | max (omit to inherit)
17
18
  # maxTokens: 8192 # optional output-token cap (omit to inherit)
18
19
  # Every delegated subagent: code writing and execution.
19
20
  executor:
20
21
  provider: deepseek-official
21
- model: deepseek-v4-flash
22
+ model: deepseek-flash
22
23
  # reasoningEffort: high
23
24
  # maxTokens: 16384
24
25
  # escalateOnError: true # v1: after a failed step, escalate effort
@@ -29,31 +30,39 @@
29
30
  # falls back to the executor route to reserve pro for planning.
30
31
  mode: strict
31
32
  # enabled: false # off switch — GUI: Settings → Plugins → model-router (live, no restart); patch row needs a reboot
32
- # vision.enabled: true # route image-bearing requests to deepseek-v4-flash-vision-exp
33
+ # vision.enabled: true # native on deepseek-flash; only enable to pin a distinct vision route
33
34
  # GUI: Settings → Plugins → "Model router" card (v0.5.0+, loopback browser only)
34
35
  # Publish the always-on routing convention prompt section.
35
36
  promptSection: true
36
37
  # Register the `pro-flash-routing` skill in the session catalog.
37
38
  skill: true
38
- # Vision support: the catalog must list the vision model with image input, and
39
- # image admission limits are raised so normal screenshots (~8K, 15MB) attach.
39
+ # Vision support: deepseek-flash (V4.1 Flash) is natively multimodal, so the
40
+ # catalog lists it with image input. Legacy v4 names are kept as compat
41
+ # aliases (retired server-side, routed to V4.1 Flash): they let existing
42
+ # configs keep working through the transition. Max output is 384K per the
43
+ # current Models & Pricing page (was 256K on the v4 preview docs).
40
44
  # Direct id-targeted entries (same shape as a profile cordis.patch.yml): they
41
45
  # replace the targeted row's whole config.
42
46
  - id: llm-deepseek
43
47
  config:
44
48
  models:
49
+ - id: deepseek-flash
50
+ name: DeepSeek-V4.1-Flash
51
+ contextWindow: 1000000
52
+ maxTokens: 384000
53
+ inputModalities: [text, image]
45
54
  - id: deepseek-v4-flash
46
55
  name: DeepSeek-V4-Flash
47
56
  contextWindow: 1000000
48
- maxTokens: 256000
57
+ maxTokens: 384000
49
58
  - id: deepseek-v4-pro
50
59
  name: DeepSeek-V4-Pro
51
60
  contextWindow: 1000000
52
- maxTokens: 256000
61
+ maxTokens: 384000
53
62
  - id: deepseek-v4-flash-vision-exp
54
63
  name: DeepSeek-V4-Flash-Vision-Exp
55
64
  contextWindow: 1000000
56
- maxTokens: 256000
65
+ maxTokens: 384000
57
66
  inputModalities: [text, image]
58
67
  - id: attachment-local
59
68
  config:
package/lib/client.js CHANGED
@@ -36,7 +36,7 @@ var inject = ["slots", "locale", "settingsScope"];
36
36
  var en = {
37
37
  nav: "Model router",
38
38
  title: "Model router",
39
- description: "Role-based model routing: planning runs on deepseek-v4-pro, delegated executor subagents on deepseek-v4-flash.",
39
+ description: "Role-based model routing: both roles default to deepseek-flash (V4.1 Flash, native multimodal).",
40
40
  enabled: "Enabled",
41
41
  enabledHint: "Off stops rewriting requests and unregisters the prompt section and skill; on restores them.",
42
42
  on: "On",
@@ -50,7 +50,7 @@ var en = {
50
50
  modeStrict: "strict \u2014 the root agent is always the planner",
51
51
  modePlan: "plan \u2014 the root agent is pro only while plan mode is active",
52
52
  visionTitle: "Vision routing",
53
- visionDesc: "Opt-in (default off): when enabled, any request containing an image routes to the vision model, from every role; other requests keep the pro/flash routing.",
53
+ visionDesc: "Opt-in (default off, native on deepseek-flash): when enabled, any request containing an image routes to the vision model, from every role; other requests keep the role routing.",
54
54
  visionSwitch: "Vision",
55
55
  visionReset: "Reset vision to default",
56
56
  visionRoute: "Vision model"
@@ -58,7 +58,7 @@ var en = {
58
58
  var zh = {
59
59
  nav: "\u6A21\u578B\u8DEF\u7531",
60
60
  title: "\u6A21\u578B\u8DEF\u7531",
61
- description: "\u6309\u89D2\u8272\u5206\u914D\u6A21\u578B\uFF1A\u89C4\u5212\u5728 deepseek-v4-pro \u4E0A\u8FD0\u884C\uFF0C\u59D4\u6D3E\u7684\u6267\u884C\u5B50\u4EE3\u7406\u5728 deepseek-v4-flash \u4E0A\u8FD0\u884C\u3002",
61
+ description: "\u6309\u89D2\u8272\u5206\u914D\u6A21\u578B\uFF1A\u4E24\u4E2A\u89D2\u8272\u9ED8\u8BA4\u5747\u4E3A deepseek-flash\uFF08V4.1 Flash\uFF0C\u539F\u751F\u591A\u6A21\u6001\uFF09\u3002",
62
62
  enabled: "\u542F\u7528",
63
63
  enabledHint: "\u5173\u95ED\u540E\u505C\u6B62\u6539\u5199\u8BF7\u6C42\uFF0C\u5E76\u6CE8\u9500\u63D0\u793A\u533A\u5757\u4E0E\u6280\u80FD\uFF1B\u91CD\u65B0\u6253\u5F00\u5373\u6062\u590D\u3002",
64
64
  on: "\u5F00",
@@ -72,7 +72,7 @@ var zh = {
72
72
  modeStrict: "strict \u2014 \u6839 agent \u59CB\u7EC8\u4E3A\u89C4\u5212\u8005",
73
73
  modePlan: "plan \u2014 \u6839 agent \u4EC5\u5728\u8BA1\u5212\u6A21\u5F0F\u6FC0\u6D3B\u65F6\u4F7F\u7528 pro",
74
74
  visionTitle: "\u89C6\u89C9\u8DEF\u7531",
75
- visionDesc: "\u53EF\u9009\uFF08\u9ED8\u8BA4\u5173\u95ED\uFF09\uFF1A\u542F\u7528\u540E\uFF0C\u4EFB\u4F55\u5305\u542B\u56FE\u7247\u7684\u8BF7\u6C42\u90FD\u4F1A\u8DEF\u7531\u5230\u89C6\u89C9\u6A21\u578B\uFF0C\u9002\u7528\u4E8E\u6240\u6709\u89D2\u8272\uFF1B\u5176\u4ED6\u8BF7\u6C42\u4FDD\u6301 pro/flash \u8DEF\u7531\u3002",
75
+ visionDesc: "\u53EF\u9009\uFF08\u9ED8\u8BA4\u5173\u95ED\uFF0Cdeepseek-flash \u539F\u751F\u652F\u6301\uFF09\uFF1A\u542F\u7528\u540E\uFF0C\u4EFB\u4F55\u5305\u542B\u56FE\u7247\u7684\u8BF7\u6C42\u90FD\u4F1A\u8DEF\u7531\u5230\u89C6\u89C9\u6A21\u578B\uFF0C\u9002\u7528\u4E8E\u6240\u6709\u89D2\u8272\uFF1B\u5176\u4ED6\u8BF7\u6C42\u4FDD\u6301\u89D2\u8272\u8DEF\u7531\u3002",
76
76
  visionSwitch: "\u89C6\u89C9",
77
77
  visionReset: "\u6062\u590D\u89C6\u89C9\u9ED8\u8BA4\u503C",
78
78
  visionRoute: "\u89C6\u89C9\u6A21\u578B"
package/lib/index.d.ts CHANGED
@@ -130,16 +130,58 @@ declare function effortFor(route: ModelRoute, events: readonly unknown[] | undef
130
130
  * an `image` block, directly or nested inside a `tool-result` block (tool
131
131
  * results may embed the images they produced). String content never does.
132
132
  *
133
+ * @deprecated Superseded by {@link sessionHasImage} (v0.6.2): the
134
+ * `agent/request` waterfall payload never carries `messages`, so production
135
+ * routing reads the session event log instead. Kept for backward compat.
133
136
  * @param messages - the request's `messages` array (any runtime shape).
134
137
  * @returns true when at least one message contains image content.
135
138
  */
136
139
  declare function hasImageContent(messages: unknown): boolean;
140
+ /**
141
+ * Whether the agent's session log carries any image content.
142
+ *
143
+ * The `agent/request` waterfall payload never includes `messages`, so vision
144
+ * detection reads the session event log instead. Shapes mirror the harness
145
+ * `SessionEventMap` (`@deepseek-ai/dsh-session`):
146
+ * - `user/message` event's data IS the `UserMessage` (content at `data.content`),
147
+ * - `assistant/message` content lives at `data.message.content` (`AssistantMessage`),
148
+ * - `tool/result` content lives at `data.message.content` (`ToolResultMessage`,
149
+ * a single-element `[ToolResultBlock]` whose nested `content` may hold images).
150
+ * Blocks may also nest images inside `tool-result` blocks. Extraction tries
151
+ * `data.message.content` first, then `data.content`, so minor harness shape
152
+ * drift still detects rather than silently missing.
153
+ *
154
+ * Sticky by design: one image anywhere in the log routes vision for the rest
155
+ * of the session, because the image stays in request context until compaction
156
+ * or pruning drops it. The scan is O(N) per request from the tail (early exit
157
+ * on hit); text-only sessions scan the full log, which is fine at session scale.
158
+ *
159
+ * @param events - the agent's session event log (or `undefined`).
160
+ * @returns true when any user, assistant, or tool-result message carries image content.
161
+ */
162
+ declare function sessionHasImage(events: readonly unknown[] | undefined): boolean;
163
+ /**
164
+ * Whether a request should take the vision route.
165
+ *
166
+ * Pure, testable wiring helper for the `agent/request` listener in
167
+ * `src/index.ts`: vision wins only when explicitly enabled AND the session
168
+ * log carries an image. Extracted so the gating logic is covered without
169
+ * spinning up Cordis.
170
+ *
171
+ * @param events - the agent's session event log.
172
+ * @param vision - the resolved vision route (only `enabled` is read).
173
+ * @returns true when the request must be stamped with the vision model.
174
+ */
175
+ declare function shouldUseVision(events: readonly unknown[] | undefined, vision: Pick<VisionRoute, "enabled"> | undefined): boolean;
137
176
 
138
177
  /** Plugin row id; the bundle patch inserts it under this id. */
139
178
  declare const name = "model-router";
140
179
  /**
141
- * Opt-in vision route. Defaults are OFF on `deepseek-v4-flash-vision-exp` from
142
- * `deepseek-official`; `reasoningEffort` and `maxTokens` are optional pins.
180
+ * Opt-in vision route. Defaults to `deepseek-flash` (V4.1 Flash, native
181
+ * multimodal) from `deepseek-official`; `reasoningEffort` and `maxTokens`
182
+ * are optional pins. Native multimodal makes the vision branch a no-op when
183
+ * the role routes already point at the same model — kept for explicitness
184
+ * and for a future V4.1-Pro split.
143
185
  */
144
186
  declare const VisionRouteSchema: z<Schemastery.ObjectS<{
145
187
  enabled: z<boolean, boolean>;
@@ -265,7 +307,7 @@ declare const SETTINGS_NS: _deepseek_ai_dsh_settings.SettingsNamespace;
265
307
  declare const SKILL_NAME = "pro-flash-routing";
266
308
  declare const SKILL_DESCRIPTION = "Route planning and code execution across models: plan on the pro planner agent, delegate implementation to flash executor subagents.";
267
309
  declare const SKILL_WHEN_TO_USE = "Use when a task combines planning and implementation: before writing code, after a plan is approved, when delegating execution work, or when the user asks about the pro/flash routing convention.";
268
- declare const SKILL_CONTENT = "# Pro planner / Flash executor routing\n\nThis session routes models by role:\n\n- **Planner (this agent)** \u2014 `deepseek-v4-pro`. Planning, design decisions, reviewing delegated output, and user-facing synthesis happen here.\n- **Executors (every subagent)** \u2014 `deepseek-v4-flash`. Implementation work happens there: writing code, running commands, builds, and tests. The harness forces the model automatically; you do not select it.\n\n## Working rhythm\n\n1. **Plan here.** Explore, decide the approach, and (when plan mode is on) submit the plan with `exit_plan_mode`. The plan stays on this agent.\n2. **Delegate the execution.** Once a plan is approved, hand each self-contained chunk of implementation to a subagent with a complete prompt: exact files to touch, the change to make, and how to verify. Subagents are automatically routed to `deepseek-v4-flash`, so keep them execution-focused: give them the decision, not the decision to make.\n3. **Review here.** Read the subagent's result on this agent, verify it yourself (tests, diffs, logs), and iterate with follow-up messages to the same subagent when available.\n4. **Report here.** Summaries, plans, and answers to the user come from this agent.\n\n## Keep this agent's context lean\n\nInput tokens are the expensive part of the planner. Don't re-read large files or full transcripts on this agent \u2014 trust the subagent's final report. Prefer targeted reads (offset/limit) over whole files. When the context grows, compact rather than re-sending everything.\n\n## Delegation guidelines\n\n- Start independent delegations together in one assistant message and continue useful work while they run (background mode by default).\n- Prefer `subagent` for self-contained work and `workflow` when many independent pieces need fan-out; their workers run on flash as well.\n- Do not delegate design: subagents execute decisions already made.\n- If a subagent's task grows into design work, pull it back to this agent and re-delegate the narrowed execution.\n\n## Verification\n\n- Executor output was produced by `deepseek-v4-flash`; planner output by `deepseek-v4-pro`. If you need to confirm, check the session log's model metadata.\n- If routing ever looks wrong, the `model-router` plugin row in the profile composition is the single place that owns it.";
310
+ declare const SKILL_CONTENT = "# Pro planner / Flash executor routing\n\nThis session routes models by role (both default to deepseek-flash, V4.1 Flash with native multimodal; vision needs no separate model):\n\n- **Planner (this agent)** \u2014 `deepseek-flash`. Planning, design decisions, reviewing delegated output, and user-facing synthesis happen here.\n- **Executors (every subagent)** \u2014 `deepseek-flash`. Implementation work happens there: writing code, running commands, builds, and tests. The harness forces the model automatically; you do not select it.\n\n## Working rhythm\n\n1. **Plan here.** Explore, decide the approach, and (when plan mode is on) submit the plan with `exit_plan_mode`. The plan stays on this agent.\n2. **Delegate the execution.** Once a plan is approved, hand each self-contained chunk of implementation to a subagent with a complete prompt: exact files to touch, the change to make, and how to verify. Subagents are automatically routed to `deepseek-flash`, so keep them execution-focused: give them the decision, not the decision to make.\n3. **Review here.** Read the subagent's result on this agent, verify it yourself (tests, diffs, logs), and iterate with follow-up messages to the same subagent when available.\n4. **Report here.** Summaries, plans, and answers to the user come from this agent.\n\n## Keep this agent's context lean\n\nInput tokens are the expensive part of the planner. Don't re-read large files or full transcripts on this agent \u2014 trust the subagent's final report. Prefer targeted reads (offset/limit) over whole files. When the context grows, compact rather than re-sending everything.\n\n## Delegation guidelines\n\n- Start independent delegations together in one assistant message and continue useful work while they run (background mode by default).\n- Prefer `subagent` for self-contained work and `workflow` when many independent pieces need fan-out; their workers run on flash as well.\n- Do not delegate design: subagents execute decisions already made.\n- If a subagent's task grows into design work, pull it back to this agent and re-delegate the narrowed execution.\n\n## Verification\n\n- Both roles produce `deepseek-flash` (unified since V4.1; split again when V4.1-Pro lands). If you need to confirm, check the session log's model metadata.\n- If routing ever looks wrong, the `model-router` plugin row in the profile composition is the single place that owns it.";
269
311
  /** The plugin row id the bundle patch must insert. */
270
312
  declare const ROW_ID = "model-router";
271
313
  /** Minimal structural view of the live agent object the router reads. */
@@ -334,4 +376,4 @@ declare class ModelRouter extends Service {
334
376
  private render;
335
377
  }
336
378
 
337
- export { type AgentRole, Config, type ModelRoute, ModelRouter, ROW_ID, type ReasoningEffort, type RouterConfig, type RoutingMode, SETTINGS_NS, SKILL_CONTENT, SKILL_DESCRIPTION, SKILL_NAME, SKILL_WHEN_TO_USE, type VisionRoute, VisionRouteSchema, ModelRouter as default, effortFor, hasImageContent, name, recentStepsHadError, roleFor, routeFor };
379
+ export { type AgentRole, Config, type ModelRoute, ModelRouter, ROW_ID, type ReasoningEffort, type RouterConfig, type RoutingMode, SETTINGS_NS, SKILL_CONTENT, SKILL_DESCRIPTION, SKILL_NAME, SKILL_WHEN_TO_USE, type VisionRoute, VisionRouteSchema, ModelRouter as default, effortFor, hasImageContent, name, recentStepsHadError, roleFor, routeFor, sessionHasImage, shouldUseVision };
package/lib/index.js CHANGED
@@ -57,12 +57,17 @@ function sessionHasImage(events) {
57
57
  if (!Array.isArray(events)) return false;
58
58
  for (let i = events.length - 1; i >= 0; i -= 1) {
59
59
  const event = events[i];
60
- if (event?.type !== "user/message" && event?.type !== "assistant/message") continue;
60
+ if (event?.type !== "user/message" && event?.type !== "assistant/message" && event?.type !== "tool/result")
61
+ continue;
61
62
  const content = event.data?.message?.content ?? event.data?.content;
62
63
  if (Array.isArray(content) && blocksContainImage(content)) return true;
63
64
  }
64
65
  return false;
65
66
  }
67
+ function shouldUseVision(events, vision) {
68
+ if (vision?.enabled !== true) return false;
69
+ return sessionHasImage(events);
70
+ }
66
71
  function blocksContainImage(blocks) {
67
72
  for (const block of blocks) {
68
73
  const candidate = block;
@@ -86,18 +91,18 @@ var ModelRouteSchema = z.object({
86
91
  var VisionRouteSchema = z.object({
87
92
  enabled: z.boolean().default(false),
88
93
  provider: z.string().min(1).default("deepseek-official"),
89
- model: z.string().min(1).default("deepseek-v4-flash-vision-exp"),
94
+ model: z.string().min(1).default("deepseek-flash"),
90
95
  reasoningEffort: z.union(["off", "low", "high", "max"]),
91
96
  maxTokens: z.number().min(1)
92
97
  });
93
98
  var Config = z.object({
94
99
  planner: ModelRouteSchema.default({
95
100
  provider: "deepseek-official",
96
- model: "deepseek-v4-pro"
101
+ model: "deepseek-flash"
97
102
  }),
98
103
  executor: ModelRouteSchema.default({
99
104
  provider: "deepseek-official",
100
- model: "deepseek-v4-flash"
105
+ model: "deepseek-flash"
101
106
  }),
102
107
  mode: z.union(["strict", "plan"]).default("strict"),
103
108
  enabled: z.boolean().default(true),
@@ -125,15 +130,15 @@ var SKILL_DESCRIPTION = "Route planning and code execution across models: plan o
125
130
  var SKILL_WHEN_TO_USE = `Use when a task combines planning and implementation: before writing code, after a plan is approved, when delegating execution work, or when the user asks about the pro/flash routing convention.`;
126
131
  var SKILL_CONTENT = `# Pro planner / Flash executor routing
127
132
 
128
- This session routes models by role:
133
+ This session routes models by role (both default to deepseek-flash, V4.1 Flash with native multimodal; vision needs no separate model):
129
134
 
130
- - **Planner (this agent)** \u2014 \`deepseek-v4-pro\`. Planning, design decisions, reviewing delegated output, and user-facing synthesis happen here.
131
- - **Executors (every subagent)** \u2014 \`deepseek-v4-flash\`. Implementation work happens there: writing code, running commands, builds, and tests. The harness forces the model automatically; you do not select it.
135
+ - **Planner (this agent)** \u2014 \`deepseek-flash\`. Planning, design decisions, reviewing delegated output, and user-facing synthesis happen here.
136
+ - **Executors (every subagent)** \u2014 \`deepseek-flash\`. Implementation work happens there: writing code, running commands, builds, and tests. The harness forces the model automatically; you do not select it.
132
137
 
133
138
  ## Working rhythm
134
139
 
135
140
  1. **Plan here.** Explore, decide the approach, and (when plan mode is on) submit the plan with \`exit_plan_mode\`. The plan stays on this agent.
136
- 2. **Delegate the execution.** Once a plan is approved, hand each self-contained chunk of implementation to a subagent with a complete prompt: exact files to touch, the change to make, and how to verify. Subagents are automatically routed to \`deepseek-v4-flash\`, so keep them execution-focused: give them the decision, not the decision to make.
141
+ 2. **Delegate the execution.** Once a plan is approved, hand each self-contained chunk of implementation to a subagent with a complete prompt: exact files to touch, the change to make, and how to verify. Subagents are automatically routed to \`deepseek-flash\`, so keep them execution-focused: give them the decision, not the decision to make.
137
142
  3. **Review here.** Read the subagent's result on this agent, verify it yourself (tests, diffs, logs), and iterate with follow-up messages to the same subagent when available.
138
143
  4. **Report here.** Summaries, plans, and answers to the user come from this agent.
139
144
 
@@ -150,7 +155,7 @@ Input tokens are the expensive part of the planner. Don't re-read large files or
150
155
 
151
156
  ## Verification
152
157
 
153
- - Executor output was produced by \`deepseek-v4-flash\`; planner output by \`deepseek-v4-pro\`. If you need to confirm, check the session log's model metadata.
158
+ - Both roles produce \`deepseek-flash\` (unified since V4.1; split again when V4.1-Pro lands). If you need to confirm, check the session log's model metadata.
154
159
  - If routing ever looks wrong, the \`model-router\` plugin row in the profile composition is the single place that owns it.`;
155
160
  var ROW_ID = "model-router";
156
161
  function isPlanModeActive(agent) {
@@ -185,7 +190,7 @@ var ModelRouter = class extends Service {
185
190
  const resolved = await next();
186
191
  const cfg = this.source();
187
192
  if (!cfg.enabled) return resolved;
188
- if (cfg.vision.enabled && sessionHasImage(agent.session?.events)) {
193
+ if (shouldUseVision(agent.session?.events, cfg.vision)) {
189
194
  const stamped2 = {
190
195
  ...resolved,
191
196
  provider: cfg.vision.provider,
@@ -281,6 +286,8 @@ export {
281
286
  name,
282
287
  recentStepsHadError,
283
288
  roleFor,
284
- routeFor
289
+ routeFor,
290
+ sessionHasImage,
291
+ shouldUseVision
285
292
  };
286
293
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/policy.ts"],"sourcesContent":["/**\n * dsh-model-router: role-based model routing for the DeepSeek Harness.\n *\n * The planner (the session's root agent) runs on `deepseek-v4-pro`; delegated\n * executor subagents run on `deepseek-v4-flash`. Enforcement is a per-agent\n * `agent/request` rewrite registered when the agent is created, so it applies\n * in every mode (web / headless / tui) and every agent preset, including\n * subagents the delegation tools create.\n *\n * Each role route may also pin `reasoningEffort` and `maxTokens`; when set,\n * they override the session's selection for that role. A `mode` switch lets a\n * deployment reserve the planner route for actual planning.\n *\n * The plugin also publishes:\n * - a system-prompt section stating the planner/executor convention, and\n * - the `pro-flash-routing` skill teaching the agent to plan itself and\n * delegate code execution to flash subagents.\n *\n * @module dsh-model-router\n */\nimport { Context, Service } from \"@deepseek-ai/cordis\";\nimport z from \"@deepseek-ai/schemastery\";\nimport { foldPlanMode } from \"@deepseek-ai/dsh-plan-mode\";\nimport { installSettingsSection, settingsNamespace } from \"@deepseek-ai/dsh-settings\";\nimport { effortFor, hasImageContent, recentStepsHadError, roleFor, routeFor, sessionHasImage, type RouterConfig } from \"./policy.js\";\n\n/** Plugin row id; the bundle patch inserts it under this id. */\nconst name = \"model-router\";\n\n/** One provider/model pair, with defaults and optional effort/token caps. */\nconst ModelRouteSchema = z.object({\n provider: z.string().min(1),\n model: z.string().min(1),\n reasoningEffort: z.union([\"off\", \"low\", \"high\", \"max\"]),\n maxTokens: z.number().min(1),\n escalateOnError: z.boolean(),\n escalateTo: z.union([\"off\", \"low\", \"high\", \"max\"]),\n recoverySteps: z.number().min(1),\n});\n\n/**\n * Opt-in vision route. Defaults are OFF on `deepseek-v4-flash-vision-exp` from\n * `deepseek-official`; `reasoningEffort` and `maxTokens` are optional pins.\n */\nconst VisionRouteSchema = z.object({\n enabled: z.boolean().default(false),\n provider: z.string().min(1).default(\"deepseek-official\"),\n model: z.string().min(1).default(\"deepseek-v4-flash-vision-exp\"),\n reasoningEffort: z.union([\"off\", \"low\", \"high\", \"max\"]),\n maxTokens: z.number().min(1),\n});\n\n/** The plugin's public config, validated at row load. */\nconst Config = z.object({\n planner: ModelRouteSchema.default({\n provider: \"deepseek-official\",\n model: \"deepseek-v4-pro\",\n } as never),\n executor: ModelRouteSchema.default({\n provider: \"deepseek-official\",\n model: \"deepseek-v4-flash\",\n } as never),\n mode: z.union([\"strict\", \"plan\"]).default(\"strict\"),\n enabled: z.boolean().default(true),\n promptSection: z.boolean().default(true),\n skill: z.boolean().default(true),\n vision: VisionRouteSchema.default({} as never),\n});\n\n/** Settings namespace the live on/off toggle lives under (settings.yaml). */\nconst SETTINGS_NS = settingsNamespace(\"model-router\");\n\n/**\n * Resolve raw row config into the internal shape, failing loud on garbage.\n * Schemastery schemas are callable: invoking validates and applies defaults.\n * @param raw - the row's config object.\n * @returns the validated RouterConfig.\n */\nfunction resolveConfig(raw: unknown): RouterConfig {\n const parsed = Config(raw ?? {});\n return {\n planner: parsed.planner,\n executor: parsed.executor,\n mode: parsed.mode,\n enabled: parsed.enabled,\n promptSection: parsed.promptSection,\n skill: parsed.skill,\n vision: parsed.vision,\n };\n}\n\n/**\n * Always-on guidance section. Negative order renders before the persona, so\n * the convention is established before the agent's identity line.\n */\nconst SECTION_ORDER = -50;\n\nconst SECTION_TEXT = `Model routing is role-based. Planning runs on {PLANNER_MODEL}; implementation runs on {EXECUTOR_MODEL}. You are the root agent: plan, design, review subagent output, and write the final answer here. Delegate implementation — writing code, running commands, builds, tests — to subagents with complete, self-contained prompts, preferring background delegation for independent work. Keep plans and replies concise. Do not hand-write large amounts of code or run long executions here; delegate instead.{VISION_LINE}`;\n\nconst SKILL_NAME = \"pro-flash-routing\";\n\nconst SKILL_DESCRIPTION =\n \"Route planning and code execution across models: plan on the pro planner agent, delegate implementation to flash executor subagents.\";\n\nconst SKILL_WHEN_TO_USE = `Use when a task combines planning and implementation: before writing code, after a plan is approved, when delegating execution work, or when the user asks about the pro/flash routing convention.`;\n\nconst SKILL_CONTENT = `# Pro planner / Flash executor routing\n\nThis session routes models by role:\n\n- **Planner (this agent)** — \\`deepseek-v4-pro\\`. Planning, design decisions, reviewing delegated output, and user-facing synthesis happen here.\n- **Executors (every subagent)** — \\`deepseek-v4-flash\\`. Implementation work happens there: writing code, running commands, builds, and tests. The harness forces the model automatically; you do not select it.\n\n## Working rhythm\n\n1. **Plan here.** Explore, decide the approach, and (when plan mode is on) submit the plan with \\`exit_plan_mode\\`. The plan stays on this agent.\n2. **Delegate the execution.** Once a plan is approved, hand each self-contained chunk of implementation to a subagent with a complete prompt: exact files to touch, the change to make, and how to verify. Subagents are automatically routed to \\`deepseek-v4-flash\\`, so keep them execution-focused: give them the decision, not the decision to make.\n3. **Review here.** Read the subagent's result on this agent, verify it yourself (tests, diffs, logs), and iterate with follow-up messages to the same subagent when available.\n4. **Report here.** Summaries, plans, and answers to the user come from this agent.\n\n## Keep this agent's context lean\n\nInput tokens are the expensive part of the planner. Don't re-read large files or full transcripts on this agent — trust the subagent's final report. Prefer targeted reads (offset/limit) over whole files. When the context grows, compact rather than re-sending everything.\n\n## Delegation guidelines\n\n- Start independent delegations together in one assistant message and continue useful work while they run (background mode by default).\n- Prefer \\`subagent\\` for self-contained work and \\`workflow\\` when many independent pieces need fan-out; their workers run on flash as well.\n- Do not delegate design: subagents execute decisions already made.\n- If a subagent's task grows into design work, pull it back to this agent and re-delegate the narrowed execution.\n\n## Verification\n\n- Executor output was produced by \\`deepseek-v4-flash\\`; planner output by \\`deepseek-v4-pro\\`. If you need to confirm, check the session log's model metadata.\n- If routing ever looks wrong, the \\`model-router\\` plugin row in the profile composition is the single place that owns it.`;\n\n/** The plugin row id the bundle patch must insert. */\nconst ROW_ID = \"model-router\";\n\n/** Minimal structural view of the live agent object the router reads. */\ninterface AgentLike {\n ctx: AgentScopedContext;\n options?: { subagentDepth?: number };\n session?: { header?: { origin?: string }; events?: unknown[] };\n}\n\n/** The agent-scoped context's waterfall surface the router uses. */\ninterface AgentScopedContext {\n on(\n event: \"agent/request\",\n listener: (\n payload: Record<string, unknown>,\n next: () => Promise<Record<string, unknown>>,\n ) => Promise<Record<string, unknown>>,\n options?: { prepend?: boolean },\n ): () => void;\n}\n\n/** Host-plane surface the router consumes (events, prompt registry, skills). */\ninterface HarnessContext {\n on(\n event: \"agent/created\",\n listener: (payload: { agent: AgentLike }) => void,\n ): () => void;\n on(event: \"agent/disposed\", listener: (agent: unknown) => void): () => void;\n systemPrompt: {\n section(section: { name: string; order: number; text: string }): () => void;\n };\n skills: {\n register(skill: {\n name: string;\n description: string;\n whenToUse?: string;\n content: string;\n source: string;\n }): () => void;\n };\n}\n\n/** Fold plan-mode state for an agent without trusting the agent's exact shape. */\nfunction isPlanModeActive(agent: AgentLike): boolean {\n const events = agent.session?.events;\n if (!Array.isArray(events)) return false;\n try {\n return foldPlanMode(events as Parameters<typeof foldPlanMode>[0]);\n } catch {\n return false;\n }\n}\n\n/**\n * Cordis service: per-agent request routing plus the convention surface.\n */\nclass ModelRouter extends Service {\n static inject = [\"skills\", \"systemPrompt\"];\n\n config: RouterConfig;\n\n /** Currently authoritative config; swapped by the settings section when one is mounted. */\n source: () => RouterConfig;\n\n /** Host-plane surface the router consumes (events, prompt registry, skills). */\n harness: HarnessContext;\n\n /** Disposer of the currently registered prompt section, if any. */\n promptDispose?: () => void;\n\n /** Disposer of the currently registered skill, if any. */\n skillDispose?: () => void;\n\n constructor(ctx: Context, rawConfig: unknown = {}) {\n super(ctx, \"modelRouter\");\n this.config = resolveConfig(rawConfig);\n this.source = () => this.config;\n this.harness = ctx as unknown as HarnessContext;\n\n // Every agent that gets created — root sessions, delegation children,\n // workflow workers, ralph rounds — passes through here.\n this.harness.on(\"agent/created\", ({ agent }) => {\n // `prepend` puts this listener OUTERMOST in the `agent/request`\n // waterfall: the harness's model-selection listener runs inside it, so\n // this rewrite is applied LAST and wins over the session's selected\n // model (which dsh-base defaults to deepseek-v4-flash and the user\n // settings or UI can change).\n const dispose = agent.ctx.on(\n \"agent/request\",\n async (payload, next) => {\n const resolved = await next();\n const cfg = this.source();\n if (!cfg.enabled) return resolved;\n // Vision branch FIRST: any request whose session log carries image\n // content is stamped with the vision model, regardless of role. The\n // `agent/request` waterfall payload never includes `messages`, so\n // detection reads the session event log. The harness's own\n // model-selection listener has already run (`await next()`), so\n // this rewrite wins over the session model.\n if (\n cfg.vision.enabled &&\n sessionHasImage(agent.session?.events)\n ) {\n const stamped: Record<string, unknown> = {\n ...resolved,\n provider: cfg.vision.provider,\n model: cfg.vision.model,\n };\n if (cfg.vision.maxTokens !== undefined) stamped.maxTokens = cfg.vision.maxTokens;\n const effort = effortFor(cfg.vision, agent.session?.events);\n if (effort !== undefined) stamped.reasoningEffort = effort;\n return stamped;\n }\n const route = routeFor(agent, cfg, isPlanModeActive(agent));\n if (route === undefined) return resolved;\n const stamped: Record<string, unknown> = {\n ...resolved,\n provider: route.provider,\n model: route.model,\n };\n if (route.maxTokens !== undefined) stamped.maxTokens = route.maxTokens;\n const effort = effortFor(route, agent.session?.events);\n if (effort !== undefined) stamped.reasoningEffort = effort;\n return stamped;\n },\n { prepend: true },\n );\n this.harness.on(\"agent/disposed\", (disposed) => {\n if (disposed === agent) dispose();\n });\n });\n\n // Register the convention surface from the composition config, then let\n // the settings section (when mounted) take over as the live source.\n this.render(this.config);\n installSettingsSection(\n ctx,\n SETTINGS_NS,\n Config,\n this.config as unknown as ReturnType<typeof Config>,\n {\n setSource: (current) => {\n this.source = current;\n },\n onChange: () => this.render(this.source()),\n },\n );\n }\n\n /**\n * Register the convention surface (prompt section + skill) for `cfg`,\n * replacing whatever is currently registered. Called on construction and\n * after every committed settings change; while `enabled` is false, nothing\n * stays registered.\n */\n private render(cfg: RouterConfig): void {\n if (this.promptDispose) {\n this.promptDispose();\n this.promptDispose = undefined;\n }\n if (this.skillDispose) {\n this.skillDispose();\n this.skillDispose = undefined;\n }\n if (!cfg.enabled) return;\n if (cfg.promptSection) {\n this.promptDispose = this.harness.systemPrompt.section({\n name: ROW_ID,\n order: SECTION_ORDER,\n text: SECTION_TEXT.replaceAll(\"{PLANNER_MODEL}\", cfg.planner.model)\n .replaceAll(\"{EXECUTOR_MODEL}\", cfg.executor.model)\n .replace(\n \"{VISION_LINE}\",\n cfg.vision.enabled\n ? ` Image-bearing requests route to ${cfg.vision.model}.`\n : \"\",\n ),\n });\n }\n if (cfg.skill) {\n this.skillDispose = this.harness.skills.register({\n name: SKILL_NAME,\n description: SKILL_DESCRIPTION,\n whenToUse: SKILL_WHEN_TO_USE,\n content: SKILL_CONTENT,\n source: \"runtime\",\n });\n }\n }\n}\n\nexport {\n Config,\n ModelRouter,\n ModelRouter as default,\n SETTINGS_NS,\n VisionRouteSchema,\n name,\n ROW_ID,\n SKILL_CONTENT,\n SKILL_DESCRIPTION,\n SKILL_NAME,\n SKILL_WHEN_TO_USE,\n effortFor,\n hasImageContent,\n recentStepsHadError,\n roleFor,\n routeFor,\n};\nexport type {\n AgentRole,\n ModelRoute,\n ReasoningEffort,\n RoutingMode,\n RouterConfig,\n VisionRoute,\n} from \"./policy.js\";\n","/**\n * Pure routing policy for dsh-model-router: which model each agent role gets.\n * Kept free of Cordis imports so the policy is trivially unit-testable.\n * @module dsh-model-router/policy\n */\n\n/** Reasoning-effort levels a route may pin (mirrors the harness vocabulary). */\nexport type ReasoningEffort = \"off\" | \"low\" | \"high\" | \"max\";\n\n/**\n * How the router treats the root agent.\n * - `strict`: the root agent is always the planner (pro).\n * - `plan`: the root agent is pro only while plan mode is active; otherwise it\n * falls back to the executor route, reserving pro for real planning.\n */\nexport type RoutingMode = \"strict\" | \"plan\";\n\n/** One route: a provider/model pair stamped onto an agent request. */\nexport interface ModelRoute {\n provider: string;\n model: string;\n /**\n * Optional reasoning-effort override. When omitted, the request inherits the\n * session's own selection; when set, the router pins it for that role.\n */\n reasoningEffort?: ReasoningEffort;\n /** Optional output-token cap for the role; omitted means inherit. */\n maxTokens?: number;\n /**\n * Error-driven escalation (v1): when true, a failed execution step bumps the\n * next request's effort to `escalateTo`, wearing off after `recoverySteps`\n * clean steps. Deterministic, stateless — the session log is folded per\n * request, so only *prior* steps are ever considered.\n */\n escalateOnError?: boolean;\n /** Effort used for the request after a failed step. */\n escalateTo?: ReasoningEffort;\n /** Clean steps before escalation wears off. Defaults to 2. */\n recoverySteps?: number;\n}\n\n/** The two roles the router distinguishes. */\nexport type AgentRole = \"planner\" | \"executor\";\n\n/**\n * Opt-in vision route: when enabled, any request whose messages carry image\n * content is stamped with the vision model, from every role (root agent and\n * subagents alike). Everything else keeps the pro/flash role routing.\n */\nexport interface VisionRoute {\n /** Master switch. Defaults to false — vision routing is opt-in. */\n enabled: boolean;\n /** Provider the vision model is served from. */\n provider: string;\n /** The vision model to stamp onto image-bearing requests. */\n model: string;\n /** Optional reasoning-effort pin; omitted means inherit the session's. */\n reasoningEffort?: ReasoningEffort;\n /** Optional output-token cap; omitted means inherit the session's. */\n maxTokens?: number;\n}\n\n/** Resolved router configuration: one route per role plus routing mode. */\nexport interface RouterConfig {\n planner: ModelRoute;\n executor: ModelRoute;\n mode: RoutingMode;\n /**\n * Live off-switch. Defaults to true; settable from Settings → Plugins →\n * model-router (applies immediately) or from the patch row (next boot).\n * When false, the router stops rewriting requests and unregisters the\n * prompt section and the skill.\n */\n enabled: boolean;\n promptSection: boolean;\n skill: boolean;\n /** Opt-in vision routing for image-bearing requests (v0.6.0+). */\n vision: VisionRoute;\n}\n\n/** Default recovery window: an error escalates for the next two completed steps. */\nexport const DEFAULT_RECOVERY_STEPS = 2;\n\n/**\n * Classify an agent as planner or executor.\n *\n * The main (root) agent of a session is the planner. Every agent created as a\n * delegation child — `subagent`, `subagent_fork`, workflow workers, ralph\n * rounds — is an executor. The harness stamps two durable facts on children:\n * `options.subagentDepth` (>= 1) and the session header `origin: \"subagent\"`.\n *\n * @param agent - the live agent (any subset of the runtime shape).\n * @returns the role the agent should be routed as.\n */\nexport function roleFor(agent: unknown): AgentRole {\n const options = (agent as { options?: unknown })?.options;\n const depth = (options as { subagentDepth?: unknown })?.subagentDepth;\n if (typeof depth === \"number\" && depth >= 1) return \"executor\";\n const session = (agent as { session?: unknown })?.session;\n const origin = (session as { header?: unknown })?.header\n ? ((session as { header: { origin?: unknown } }).header.origin)\n : undefined;\n if (origin === \"subagent\") return \"executor\";\n return \"planner\";\n}\n\n/**\n * Resolve the route for one agent.\n * @param agent - the live agent.\n * @param config - the resolved router configuration.\n * @param planModeActive - whether plan mode is currently folded active for the\n * agent's session; consulted only in `plan` routing mode.\n * @returns the model route to stamp, or `undefined` to leave the request alone.\n */\nexport function routeFor(\n agent: unknown,\n config: RouterConfig,\n planModeActive = false,\n): ModelRoute | undefined {\n const role = roleFor(agent);\n if (role === \"executor\") return config.executor;\n // Root agent. In `plan` mode, reserve the planner route for actual planning;\n // otherwise the root falls back to the executor route.\n if (config.mode === \"plan\" && !planModeActive) return config.executor;\n return config.planner;\n}\n\n/**\n * Whether any of the last `recoverySteps` completed steps carried a failed\n * tool result. A failure is a `tool/result` event whose data carries an\n * `error` field (the harness records tool failures there).\n *\n * Steps are deduplicated by `turn:step`, and only *completed* steps count —\n * events are scanned from the tail, so the current in-flight request is never\n * considered.\n *\n * @param events - the agent's session event log (or `undefined`).\n * @param recoverySteps - how many completed steps back to scan.\n * @returns true when a failed step is within the window.\n */\nexport function recentStepsHadError(\n events: readonly unknown[] | undefined,\n recoverySteps: number = DEFAULT_RECOVERY_STEPS,\n): boolean {\n if (!Array.isArray(events) || recoverySteps <= 0) return false;\n const seen = new Set<string>();\n let steps = 0;\n for (let i = events.length - 1; i >= 0; i -= 1) {\n const event = events[i] as\n | { type?: string; data?: { turn?: number; step?: number; error?: unknown } }\n | undefined;\n if (event?.type !== \"tool/result\" || event.data === undefined) continue;\n const key = `${event.data.turn}:${event.data.step}`;\n if (!seen.has(key)) {\n // A new step beyond the recovery window ends the scan; events for steps\n // already inside the window are still checked below.\n if (steps >= recoverySteps) break;\n seen.add(key);\n steps += 1;\n }\n if (event.data.error !== undefined && event.data.error !== null) return true;\n }\n return false;\n}\n\n/**\n * Resolve the reasoning effort to stamp for one request.\n *\n * Baseline is the route's `reasoningEffort`; when `escalateOnError` is enabled\n * and a recent step failed, the effort bumps to `escalateTo` (falling back to\n * the baseline when `escalateTo` is unset). Returns `undefined` to leave the\n * request's effort alone (inherit the session selection).\n *\n * @param route - the resolved route for the agent.\n * @param events - the agent's session event log.\n * @returns the effort to stamp, or `undefined` to inherit.\n */\nexport function effortFor(\n route: ModelRoute,\n events: readonly unknown[] | undefined,\n): ReasoningEffort | undefined {\n if (route.escalateOnError === true && recentStepsHadError(events, route.recoverySteps)) {\n return route.escalateTo ?? route.reasoningEffort;\n }\n return route.reasoningEffort;\n}\n\n/**\n * Whether any message in a request payload carries image content.\n *\n * A message carries an image when its `content` is a block array containing\n * an `image` block, directly or nested inside a `tool-result` block (tool\n * results may embed the images they produced). String content never does.\n *\n * @param messages - the request's `messages` array (any runtime shape).\n * @returns true when at least one message contains image content.\n */\nexport function hasImageContent(messages: unknown): boolean {\n if (!Array.isArray(messages)) return false;\n for (const message of messages) {\n const content = (message as { content?: unknown })?.content;\n if (typeof content === \"string\") continue;\n if (Array.isArray(content) && blocksContainImage(content)) return true;\n }\n return false;\n}\n\n/**\n * Whether the agent's session log carries any image content.\n *\n * The `agent/request` waterfall payload never includes `messages`, so vision\n * detection reads the session event log instead: a `user/message` event's data\n * IS the message (content at `data.content`), an `assistant/message` event's\n * content lives at `data.message.content`, and both may nest images inside\n * `tool-result` blocks. Once an image is in the conversation it stays in the\n * request context, so one image anywhere in the log routes vision.\n *\n * @param events - the agent's session event log (or `undefined`).\n * @returns true when any user or assistant message carries image content.\n */\nexport function sessionHasImage(events: readonly unknown[] | undefined): boolean {\n if (!Array.isArray(events)) return false;\n for (let i = events.length - 1; i >= 0; i -= 1) {\n const event = events[i] as\n | { type?: string; data?: { content?: unknown; message?: { content?: unknown } } }\n | undefined;\n if (event?.type !== \"user/message\" && event?.type !== \"assistant/message\") continue;\n const content = event.data?.message?.content ?? event.data?.content;\n if (Array.isArray(content) && blocksContainImage(content)) return true;\n }\n return false;\n}\n\n/** Whether any block (or nested tool-result content) is an image block. */\nfunction blocksContainImage(blocks: readonly unknown[]): boolean {\n for (const block of blocks) {\n const candidate = block as { type?: unknown; content?: unknown };\n if (candidate?.type === \"image\") return true;\n if (candidate?.type === \"tool-result\" && Array.isArray(candidate.content) && blocksContainImage(candidate.content)) return true;\n }\n return false;\n}\n"],"mappings":";AAoBA,SAAkB,eAAe;AACjC,OAAO,OAAO;AACd,SAAS,oBAAoB;AAC7B,SAAS,wBAAwB,yBAAyB;;;AC0DnD,IAAM,yBAAyB;AAa/B,SAAS,QAAQ,OAA2B;AACjD,QAAM,UAAW,OAAiC;AAClD,QAAM,QAAS,SAAyC;AACxD,MAAI,OAAO,UAAU,YAAY,SAAS,EAAG,QAAO;AACpD,QAAM,UAAW,OAAiC;AAClD,QAAM,SAAU,SAAkC,SAC5C,QAA6C,OAAO,SACtD;AACJ,MAAI,WAAW,WAAY,QAAO;AAClC,SAAO;AACT;AAUO,SAAS,SACd,OACA,QACA,iBAAiB,OACO;AACxB,QAAM,OAAO,QAAQ,KAAK;AAC1B,MAAI,SAAS,WAAY,QAAO,OAAO;AAGvC,MAAI,OAAO,SAAS,UAAU,CAAC,eAAgB,QAAO,OAAO;AAC7D,SAAO,OAAO;A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AAAA,QACA,EAAE,SAAS,KAAK;AAAA,MAClB;AACA,WAAK,QAAQ,GAAG,kBAAkB,CAAC,aAAa;AAC9C,YAAI,aAAa,MAAO,SAAQ;AAAA,MAClC,CAAC;AAAA,IACH,CAAC;AAID,SAAK,OAAO,KAAK,MAAM;AACvB;AAAA,MACE;AAAA,MACA;AAAA,MACA;AAAA,MACA,KAAK;AAAA,MACL;AAAA,QACE,WAAW,CAAC,YAAY;AACtB,eAAK,SAAS;AAAA,QAChB;AAAA,QACA,UAAU,MAAM,KAAK,OAAO,KAAK,OAAO,CAAC;AAAA,MAC3C;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,OAAO,KAAyB;AACtC,QAAI,KAAK,eAAe;AACtB,WAAK,cAAc;AACnB,WAAK,gBAAgB;AAAA,IACvB;AACA,QAAI,KAAK,cAAc;AACrB,WAAK,aAAa;AAClB,WAAK,eAAe;AAAA,IACtB;AACA,QAAI,CAAC,IAAI,QAAS;AAClB,QAAI,IAAI,eAAe;AACrB,WAAK,gBAAgB,KAAK,QAAQ,aAAa,QAAQ;AAAA,QACrD,MAAM;AAAA,QACN,OAAO;AAAA,QACP,MAAM,aAAa,WAAW,mBAAmB,IAAI,QAAQ,KAAK,EAC/D,WAAW,oBAAoB,IAAI,SAAS,KAAK,EACjD;AAAA,UACC;AAAA,UACA,IAAI,OAAO,UACP,oCAAoC,IAAI,OAAO,KAAK,MACpD;AAAA,QACN;AAAA,MACJ,CAAC;AAAA,IACH;AACA,QAAI,IAAI,OAAO;AACb,WAAK,eAAe,KAAK,QAAQ,OAAO,SAAS;AAAA,QAC/C,MAAM;AAAA,QACN,aAAa;AAAA,QACb,WAAW;AAAA,QACX,SAAS;AAAA,QACT,QAAQ;AAAA,MACV,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["stamped","effort"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/policy.ts"],"sourcesContent":["/**\r\n * dsh-model-router: role-based model routing for the DeepSeek Harness.\r\n *\r\n * Both roles default to `deepseek-flash` (V4.1 Flash, native multimodal):\r\n * the planner (the session's root agent) and delegated executor subagents\r\n * share the same model until V4.1-Pro launches, at which point the planner\r\n * route can be flipped back with a one-line config change. Enforcement is a\r\n * per-agent `agent/request` rewrite registered when the agent is created, so\r\n * it applies in every mode (web / headless / tui) and every agent preset,\r\n * including subagents the delegation tools create.\r\n *\r\n * Each role route may also pin `reasoningEffort` and `maxTokens`; when set,\r\n * they override the session's selection for that role. A `mode` switch lets a\r\n * deployment reserve the planner route for actual planning.\r\n *\r\n * The plugin also publishes:\r\n * - a system-prompt section stating the planner/executor convention, and\r\n * - the `pro-flash-routing` skill teaching the agent to plan itself and\r\n * delegate code execution to flash subagents.\r\n *\r\n * @module dsh-model-router\r\n */\r\nimport { Context, Service } from \"@deepseek-ai/cordis\";\r\nimport z from \"@deepseek-ai/schemastery\";\r\nimport { foldPlanMode } from \"@deepseek-ai/dsh-plan-mode\";\r\nimport { installSettingsSection, settingsNamespace } from \"@deepseek-ai/dsh-settings\";\r\nimport { effortFor, hasImageContent, recentStepsHadError, roleFor, routeFor, sessionHasImage, shouldUseVision, type RouterConfig } from \"./policy.js\";\r\n\r\n/** Plugin row id; the bundle patch inserts it under this id. */\r\nconst name = \"model-router\";\r\n\r\n/** One provider/model pair, with defaults and optional effort/token caps. */\r\nconst ModelRouteSchema = z.object({\r\n provider: z.string().min(1),\r\n model: z.string().min(1),\r\n reasoningEffort: z.union([\"off\", \"low\", \"high\", \"max\"]),\r\n maxTokens: z.number().min(1),\r\n escalateOnError: z.boolean(),\r\n escalateTo: z.union([\"off\", \"low\", \"high\", \"max\"]),\r\n recoverySteps: z.number().min(1),\r\n});\r\n\r\n/**\r\n * Opt-in vision route. Defaults to `deepseek-flash` (V4.1 Flash, native\r\n * multimodal) from `deepseek-official`; `reasoningEffort` and `maxTokens`\r\n * are optional pins. Native multimodal makes the vision branch a no-op when\r\n * the role routes already point at the same model — kept for explicitness\r\n * and for a future V4.1-Pro split.\r\n */\r\nconst VisionRouteSchema = z.object({\r\n enabled: z.boolean().default(false),\r\n provider: z.string().min(1).default(\"deepseek-official\"),\r\n model: z.string().min(1).default(\"deepseek-flash\"),\r\n reasoningEffort: z.union([\"off\", \"low\", \"high\", \"max\"]),\r\n maxTokens: z.number().min(1),\r\n});\r\n\r\n/** The plugin's public config, validated at row load. */\r\nconst Config = z.object({\r\n planner: ModelRouteSchema.default({\r\n provider: \"deepseek-official\",\r\n model: \"deepseek-flash\",\r\n } as never),\r\n executor: ModelRouteSchema.default({\r\n provider: \"deepseek-official\",\r\n model: \"deepseek-flash\",\r\n } as never),\r\n mode: z.union([\"strict\", \"plan\"]).default(\"strict\"),\r\n enabled: z.boolean().default(true),\r\n promptSection: z.boolean().default(true),\r\n skill: z.boolean().default(true),\r\n vision: VisionRouteSchema.default({} as never),\r\n});\r\n\r\n/** Settings namespace the live on/off toggle lives under (settings.yaml). */\r\nconst SETTINGS_NS = settingsNamespace(\"model-router\");\r\n\r\n/**\r\n * Resolve raw row config into the internal shape, failing loud on garbage.\r\n * Schemastery schemas are callable: invoking validates and applies defaults.\r\n * @param raw - the row's config object.\r\n * @returns the validated RouterConfig.\r\n */\r\nfunction resolveConfig(raw: unknown): RouterConfig {\r\n const parsed = Config(raw ?? {});\r\n return {\r\n planner: parsed.planner,\r\n executor: parsed.executor,\r\n mode: parsed.mode,\r\n enabled: parsed.enabled,\r\n promptSection: parsed.promptSection,\r\n skill: parsed.skill,\r\n vision: parsed.vision,\r\n };\r\n}\r\n\r\n/**\r\n * Always-on guidance section. Negative order renders before the persona, so\r\n * the convention is established before the agent's identity line.\r\n */\r\nconst SECTION_ORDER = -50;\r\n\r\nconst SECTION_TEXT = `Model routing is role-based. Planning runs on {PLANNER_MODEL}; implementation runs on {EXECUTOR_MODEL}. You are the root agent: plan, design, review subagent output, and write the final answer here. Delegate implementation — writing code, running commands, builds, tests — to subagents with complete, self-contained prompts, preferring background delegation for independent work. Keep plans and replies concise. Do not hand-write large amounts of code or run long executions here; delegate instead.{VISION_LINE}`;\r\n\r\nconst SKILL_NAME = \"pro-flash-routing\";\r\n\r\nconst SKILL_DESCRIPTION =\r\n \"Route planning and code execution across models: plan on the pro planner agent, delegate implementation to flash executor subagents.\";\r\n\r\nconst SKILL_WHEN_TO_USE = `Use when a task combines planning and implementation: before writing code, after a plan is approved, when delegating execution work, or when the user asks about the pro/flash routing convention.`;\r\n\r\nconst SKILL_CONTENT = `# Pro planner / Flash executor routing\r\n\r\nThis session routes models by role (both default to deepseek-flash, V4.1 Flash with native multimodal; vision needs no separate model):\r\n\r\n- **Planner (this agent)** — \\`deepseek-flash\\`. Planning, design decisions, reviewing delegated output, and user-facing synthesis happen here.\r\n- **Executors (every subagent)** — \\`deepseek-flash\\`. Implementation work happens there: writing code, running commands, builds, and tests. The harness forces the model automatically; you do not select it.\r\n\r\n## Working rhythm\r\n\r\n1. **Plan here.** Explore, decide the approach, and (when plan mode is on) submit the plan with \\`exit_plan_mode\\`. The plan stays on this agent.\r\n2. **Delegate the execution.** Once a plan is approved, hand each self-contained chunk of implementation to a subagent with a complete prompt: exact files to touch, the change to make, and how to verify. Subagents are automatically routed to \\`deepseek-flash\\`, so keep them execution-focused: give them the decision, not the decision to make.\r\n3. **Review here.** Read the subagent's result on this agent, verify it yourself (tests, diffs, logs), and iterate with follow-up messages to the same subagent when available.\r\n4. **Report here.** Summaries, plans, and answers to the user come from this agent.\r\n\r\n## Keep this agent's context lean\r\n\r\nInput tokens are the expensive part of the planner. Don't re-read large files or full transcripts on this agent — trust the subagent's final report. Prefer targeted reads (offset/limit) over whole files. When the context grows, compact rather than re-sending everything.\r\n\r\n## Delegation guidelines\r\n\r\n- Start independent delegations together in one assistant message and continue useful work while they run (background mode by default).\r\n- Prefer \\`subagent\\` for self-contained work and \\`workflow\\` when many independent pieces need fan-out; their workers run on flash as well.\r\n- Do not delegate design: subagents execute decisions already made.\r\n- If a subagent's task grows into design work, pull it back to this agent and re-delegate the narrowed execution.\r\n\r\n## Verification\r\n\r\n- Both roles produce \\`deepseek-flash\\` (unified since V4.1; split again when V4.1-Pro lands). If you need to confirm, check the session log's model metadata.\r\n- If routing ever looks wrong, the \\`model-router\\` plugin row in the profile composition is the single place that owns it.`;\r\n\r\n/** The plugin row id the bundle patch must insert. */\r\nconst ROW_ID = \"model-router\";\r\n\r\n/** Minimal structural view of the live agent object the router reads. */\r\ninterface AgentLike {\r\n ctx: AgentScopedContext;\r\n options?: { subagentDepth?: number };\r\n session?: { header?: { origin?: string }; events?: unknown[] };\r\n}\r\n\r\n/** The agent-scoped context's waterfall surface the router uses. */\r\ninterface AgentScopedContext {\r\n on(\r\n event: \"agent/request\",\r\n listener: (\r\n payload: Record<string, unknown>,\r\n next: () => Promise<Record<string, unknown>>,\r\n ) => Promise<Record<string, unknown>>,\r\n options?: { prepend?: boolean },\r\n ): () => void;\r\n}\r\n\r\n/** Host-plane surface the router consumes (events, prompt registry, skills). */\r\ninterface HarnessContext {\r\n on(\r\n event: \"agent/created\",\r\n listener: (payload: { agent: AgentLike }) => void,\r\n ): () => void;\r\n on(event: \"agent/disposed\", listener: (agent: unknown) => void): () => void;\r\n systemPrompt: {\r\n section(section: { name: string; order: number; text: string }): () => void;\r\n };\r\n skills: {\r\n register(skill: {\r\n name: string;\r\n description: string;\r\n whenToUse?: string;\r\n content: string;\r\n source: string;\r\n }): () => void;\r\n };\r\n}\r\n\r\n/** Fold plan-mode state for an agent without trusting the agent's exact shape. */\r\nfunction isPlanModeActive(agent: AgentLike): boolean {\r\n const events = agent.session?.events;\r\n if (!Array.isArray(events)) return false;\r\n try {\r\n return foldPlanMode(events as Parameters<typeof foldPlanMode>[0]);\r\n } catch {\r\n return false;\r\n }\r\n}\r\n\r\n/**\r\n * Cordis service: per-agent request routing plus the convention surface.\r\n */\r\nclass ModelRouter extends Service {\r\n static inject = [\"skills\", \"systemPrompt\"];\r\n\r\n config: RouterConfig;\r\n\r\n /** Currently authoritative config; swapped by the settings section when one is mounted. */\r\n source: () => RouterConfig;\r\n\r\n /** Host-plane surface the router consumes (events, prompt registry, skills). */\r\n harness: HarnessContext;\r\n\r\n /** Disposer of the currently registered prompt section, if any. */\r\n promptDispose?: () => void;\r\n\r\n /** Disposer of the currently registered skill, if any. */\r\n skillDispose?: () => void;\r\n\r\n constructor(ctx: Context, rawConfig: unknown = {}) {\r\n super(ctx, \"modelRouter\");\r\n this.config = resolveConfig(rawConfig);\r\n this.source = () => this.config;\r\n this.harness = ctx as unknown as HarnessContext;\r\n\r\n // Every agent that gets created — root sessions, delegation children,\r\n // workflow workers, ralph rounds — passes through here.\r\n this.harness.on(\"agent/created\", ({ agent }) => {\r\n // `prepend` puts this listener OUTERMOST in the `agent/request`\r\n // waterfall: the harness's model-selection listener runs inside it, so\r\n // this rewrite is applied LAST and wins over the session's selected\r\n // model (which dsh-base defaulted to deepseek-v4-flash, now deepseek-flash, and the user\r\n // settings or UI can change).\r\n const dispose = agent.ctx.on(\r\n \"agent/request\",\r\n async (payload, next) => {\r\n const resolved = await next();\r\n const cfg = this.source();\r\n if (!cfg.enabled) return resolved;\r\n // Vision branch FIRST: any request whose session log carries image\r\n // content is stamped with the vision model, regardless of role. The\r\n // `agent/request` waterfall payload never includes `messages`, so\r\n // detection reads the session event log (`user/message`,\r\n // `assistant/message`, `tool/result`). The harness's own\r\n // model-selection listener has already run (`await next()`), so\r\n // this rewrite wins over the session model.\r\n if (shouldUseVision(agent.session?.events, cfg.vision)) {\r\n const stamped: Record<string, unknown> = {\r\n ...resolved,\r\n provider: cfg.vision.provider,\r\n model: cfg.vision.model,\r\n };\r\n if (cfg.vision.maxTokens !== undefined) stamped.maxTokens = cfg.vision.maxTokens;\r\n const effort = effortFor(cfg.vision, agent.session?.events);\r\n if (effort !== undefined) stamped.reasoningEffort = effort;\r\n return stamped;\r\n }\r\n const route = routeFor(agent, cfg, isPlanModeActive(agent));\r\n if (route === undefined) return resolved;\r\n const stamped: Record<string, unknown> = {\r\n ...resolved,\r\n provider: route.provider,\r\n model: route.model,\r\n };\r\n if (route.maxTokens !== undefined) stamped.maxTokens = route.maxTokens;\r\n const effort = effortFor(route, agent.session?.events);\r\n if (effort !== undefined) stamped.reasoningEffort = effort;\r\n return stamped;\r\n },\r\n { prepend: true },\r\n );\r\n this.harness.on(\"agent/disposed\", (disposed) => {\r\n if (disposed === agent) dispose();\r\n });\r\n });\r\n\r\n // Register the convention surface from the composition config, then let\r\n // the settings section (when mounted) take over as the live source.\r\n this.render(this.config);\r\n installSettingsSection(\r\n ctx,\r\n SETTINGS_NS,\r\n Config,\r\n this.config as unknown as ReturnType<typeof Config>,\r\n {\r\n setSource: (current) => {\r\n this.source = current;\r\n },\r\n onChange: () => this.render(this.source()),\r\n },\r\n );\r\n }\r\n\r\n /**\r\n * Register the convention surface (prompt section + skill) for `cfg`,\r\n * replacing whatever is currently registered. Called on construction and\r\n * after every committed settings change; while `enabled` is false, nothing\r\n * stays registered.\r\n */\r\n private render(cfg: RouterConfig): void {\r\n if (this.promptDispose) {\r\n this.promptDispose();\r\n this.promptDispose = undefined;\r\n }\r\n if (this.skillDispose) {\r\n this.skillDispose();\r\n this.skillDispose = undefined;\r\n }\r\n if (!cfg.enabled) return;\r\n if (cfg.promptSection) {\r\n this.promptDispose = this.harness.systemPrompt.section({\r\n name: ROW_ID,\r\n order: SECTION_ORDER,\r\n text: SECTION_TEXT.replaceAll(\"{PLANNER_MODEL}\", cfg.planner.model)\r\n .replaceAll(\"{EXECUTOR_MODEL}\", cfg.executor.model)\r\n .replace(\r\n \"{VISION_LINE}\",\r\n cfg.vision.enabled\r\n ? ` Image-bearing requests route to ${cfg.vision.model}.`\r\n : \"\",\r\n ),\r\n });\r\n }\r\n if (cfg.skill) {\r\n this.skillDispose = this.harness.skills.register({\r\n name: SKILL_NAME,\r\n description: SKILL_DESCRIPTION,\r\n whenToUse: SKILL_WHEN_TO_USE,\r\n content: SKILL_CONTENT,\r\n source: \"runtime\",\r\n });\r\n }\r\n }\r\n}\r\n\r\nexport {\r\n Config,\r\n ModelRouter,\r\n ModelRouter as default,\r\n SETTINGS_NS,\r\n VisionRouteSchema,\r\n name,\r\n ROW_ID,\r\n SKILL_CONTENT,\r\n SKILL_DESCRIPTION,\r\n SKILL_NAME,\r\n SKILL_WHEN_TO_USE,\r\n effortFor,\r\n hasImageContent,\r\n recentStepsHadError,\r\n roleFor,\r\n routeFor,\r\n sessionHasImage,\r\n shouldUseVision,\r\n};\r\nexport type {\r\n AgentRole,\r\n ModelRoute,\r\n ReasoningEffort,\r\n RoutingMode,\r\n RouterConfig,\r\n VisionRoute,\r\n} from \"./policy.js\";\r\n","/**\n * Pure routing policy for dsh-model-router: which model each agent role gets.\n * Kept free of Cordis imports so the policy is trivially unit-testable.\n * @module dsh-model-router/policy\n */\n\n/** Reasoning-effort levels a route may pin (mirrors the harness vocabulary). */\nexport type ReasoningEffort = \"off\" | \"low\" | \"high\" | \"max\";\n\n/**\n * How the router treats the root agent.\n * - `strict`: the root agent is always the planner (pro).\n * - `plan`: the root agent is pro only while plan mode is active; otherwise it\n * falls back to the executor route, reserving pro for real planning.\n */\nexport type RoutingMode = \"strict\" | \"plan\";\n\n/** One route: a provider/model pair stamped onto an agent request. */\nexport interface ModelRoute {\n provider: string;\n model: string;\n /**\n * Optional reasoning-effort override. When omitted, the request inherits the\n * session's own selection; when set, the router pins it for that role.\n */\n reasoningEffort?: ReasoningEffort;\n /** Optional output-token cap for the role; omitted means inherit. */\n maxTokens?: number;\n /**\n * Error-driven escalation (v1): when true, a failed execution step bumps the\n * next request's effort to `escalateTo`, wearing off after `recoverySteps`\n * clean steps. Deterministic, stateless — the session log is folded per\n * request, so only *prior* steps are ever considered.\n */\n escalateOnError?: boolean;\n /** Effort used for the request after a failed step. */\n escalateTo?: ReasoningEffort;\n /** Clean steps before escalation wears off. Defaults to 2. */\n recoverySteps?: number;\n}\n\n/** The two roles the router distinguishes. */\nexport type AgentRole = \"planner\" | \"executor\";\n\n/**\n * Opt-in vision route: when enabled, any request whose messages carry image\n * content is stamped with the vision model, from every role (root agent and\n * subagents alike). Everything else keeps the pro/flash role routing.\n */\nexport interface VisionRoute {\n /** Master switch. Defaults to false — vision routing is opt-in. */\n enabled: boolean;\n /** Provider the vision model is served from. */\n provider: string;\n /** The vision model to stamp onto image-bearing requests. */\n model: string;\n /** Optional reasoning-effort pin; omitted means inherit the session's. */\n reasoningEffort?: ReasoningEffort;\n /** Optional output-token cap; omitted means inherit the session's. */\n maxTokens?: number;\n}\n\n/** Resolved router configuration: one route per role plus routing mode. */\nexport interface RouterConfig {\n planner: ModelRoute;\n executor: ModelRoute;\n mode: RoutingMode;\n /**\n * Live off-switch. Defaults to true; settable from Settings → Plugins →\n * model-router (applies immediately) or from the patch row (next boot).\n * When false, the router stops rewriting requests and unregisters the\n * prompt section and the skill.\n */\n enabled: boolean;\n promptSection: boolean;\n skill: boolean;\n /** Opt-in vision routing for image-bearing requests (v0.6.0+). */\n vision: VisionRoute;\n}\n\n/** Default recovery window: an error escalates for the next two completed steps. */\nexport const DEFAULT_RECOVERY_STEPS = 2;\n\n/**\n * Classify an agent as planner or executor.\n *\n * The main (root) agent of a session is the planner. Every agent created as a\n * delegation child — `subagent`, `subagent_fork`, workflow workers, ralph\n * rounds — is an executor. The harness stamps two durable facts on children:\n * `options.subagentDepth` (>= 1) and the session header `origin: \"subagent\"`.\n *\n * @param agent - the live agent (any subset of the runtime shape).\n * @returns the role the agent should be routed as.\n */\nexport function roleFor(agent: unknown): AgentRole {\n const options = (agent as { options?: unknown })?.options;\n const depth = (options as { subagentDepth?: unknown })?.subagentDepth;\n if (typeof depth === \"number\" && depth >= 1) return \"executor\";\n const session = (agent as { session?: unknown })?.session;\n const origin = (session as { header?: unknown })?.header\n ? ((session as { header: { origin?: unknown } }).header.origin)\n : undefined;\n if (origin === \"subagent\") return \"executor\";\n return \"planner\";\n}\n\n/**\n * Resolve the route for one agent.\n * @param agent - the live agent.\n * @param config - the resolved router configuration.\n * @param planModeActive - whether plan mode is currently folded active for the\n * agent's session; consulted only in `plan` routing mode.\n * @returns the model route to stamp, or `undefined` to leave the request alone.\n */\nexport function routeFor(\n agent: unknown,\n config: RouterConfig,\n planModeActive = false,\n): ModelRoute | undefined {\n const role = roleFor(agent);\n if (role === \"executor\") return config.executor;\n // Root agent. In `plan` mode, reserve the planner route for actual planning;\n // otherwise the root falls back to the executor route.\n if (config.mode === \"plan\" && !planModeActive) return config.executor;\n return config.planner;\n}\n\n/**\n * Whether any of the last `recoverySteps` completed steps carried a failed\n * tool result. A failure is a `tool/result` event whose data carries an\n * `error` field (the harness records tool failures there).\n *\n * Steps are deduplicated by `turn:step`, and only *completed* steps count —\n * events are scanned from the tail, so the current in-flight request is never\n * considered.\n *\n * @param events - the agent's session event log (or `undefined`).\n * @param recoverySteps - how many completed steps back to scan.\n * @returns true when a failed step is within the window.\n */\nexport function recentStepsHadError(\n events: readonly unknown[] | undefined,\n recoverySteps: number = DEFAULT_RECOVERY_STEPS,\n): boolean {\n if (!Array.isArray(events) || recoverySteps <= 0) return false;\n const seen = new Set<string>();\n let steps = 0;\n for (let i = events.length - 1; i >= 0; i -= 1) {\n const event = events[i] as\n | { type?: string; data?: { turn?: number; step?: number; error?: unknown } }\n | undefined;\n if (event?.type !== \"tool/result\" || event.data === undefined) continue;\n const key = `${event.data.turn}:${event.data.step}`;\n if (!seen.has(key)) {\n // A new step beyond the recovery window ends the scan; events for steps\n // already inside the window are still checked below.\n if (steps >= recoverySteps) break;\n seen.add(key);\n steps += 1;\n }\n if (event.data.error !== undefined && event.data.error !== null) return true;\n }\n return false;\n}\n\n/**\n * Resolve the reasoning effort to stamp for one request.\n *\n * Baseline is the route's `reasoningEffort`; when `escalateOnError` is enabled\n * and a recent step failed, the effort bumps to `escalateTo` (falling back to\n * the baseline when `escalateTo` is unset). Returns `undefined` to leave the\n * request's effort alone (inherit the session selection).\n *\n * @param route - the resolved route for the agent.\n * @param events - the agent's session event log.\n * @returns the effort to stamp, or `undefined` to inherit.\n */\nexport function effortFor(\n route: ModelRoute,\n events: readonly unknown[] | undefined,\n): ReasoningEffort | undefined {\n if (route.escalateOnError === true && recentStepsHadError(events, route.recoverySteps)) {\n return route.escalateTo ?? route.reasoningEffort;\n }\n return route.reasoningEffort;\n}\n\n/**\n * Whether any message in a request payload carries image content.\n *\n * A message carries an image when its `content` is a block array containing\n * an `image` block, directly or nested inside a `tool-result` block (tool\n * results may embed the images they produced). String content never does.\n *\n * @deprecated Superseded by {@link sessionHasImage} (v0.6.2): the\n * `agent/request` waterfall payload never carries `messages`, so production\n * routing reads the session event log instead. Kept for backward compat.\n * @param messages - the request's `messages` array (any runtime shape).\n * @returns true when at least one message contains image content.\n */\nexport function hasImageContent(messages: unknown): boolean {\n if (!Array.isArray(messages)) return false;\n for (const message of messages) {\n const content = (message as { content?: unknown })?.content;\n if (typeof content === \"string\") continue;\n if (Array.isArray(content) && blocksContainImage(content)) return true;\n }\n return false;\n}\n\n/**\n * Whether the agent's session log carries any image content.\n *\n * The `agent/request` waterfall payload never includes `messages`, so vision\n * detection reads the session event log instead. Shapes mirror the harness\n * `SessionEventMap` (`@deepseek-ai/dsh-session`):\n * - `user/message` event's data IS the `UserMessage` (content at `data.content`),\n * - `assistant/message` content lives at `data.message.content` (`AssistantMessage`),\n * - `tool/result` content lives at `data.message.content` (`ToolResultMessage`,\n * a single-element `[ToolResultBlock]` whose nested `content` may hold images).\n * Blocks may also nest images inside `tool-result` blocks. Extraction tries\n * `data.message.content` first, then `data.content`, so minor harness shape\n * drift still detects rather than silently missing.\n *\n * Sticky by design: one image anywhere in the log routes vision for the rest\n * of the session, because the image stays in request context until compaction\n * or pruning drops it. The scan is O(N) per request from the tail (early exit\n * on hit); text-only sessions scan the full log, which is fine at session scale.\n *\n * @param events - the agent's session event log (or `undefined`).\n * @returns true when any user, assistant, or tool-result message carries image content.\n */\nexport function sessionHasImage(events: readonly unknown[] | undefined): boolean {\n if (!Array.isArray(events)) return false;\n for (let i = events.length - 1; i >= 0; i -= 1) {\n const event = events[i] as\n | { type?: string; data?: { content?: unknown; message?: { content?: unknown } } }\n | undefined;\n if (\n event?.type !== \"user/message\" &&\n event?.type !== \"assistant/message\" &&\n event?.type !== \"tool/result\"\n )\n continue;\n // Robust extraction: prefer the wrapped message shape, fall back to direct.\n const content = event.data?.message?.content ?? event.data?.content;\n if (Array.isArray(content) && blocksContainImage(content)) return true;\n }\n return false;\n}\n\n/**\n * Whether a request should take the vision route.\n *\n * Pure, testable wiring helper for the `agent/request` listener in\n * `src/index.ts`: vision wins only when explicitly enabled AND the session\n * log carries an image. Extracted so the gating logic is covered without\n * spinning up Cordis.\n *\n * @param events - the agent's session event log.\n * @param vision - the resolved vision route (only `enabled` is read).\n * @returns true when the request must be stamped with the vision model.\n */\nexport function shouldUseVision(\n events: readonly unknown[] | undefined,\n vision: Pick<VisionRoute, \"enabled\"> | undefined,\n): boolean {\n if (vision?.enabled !== true) return false;\n return sessionHasImage(events);\n}\n\n/**\n * Whether any block (or nested tool-result content) is an image block.\n * Exact `type` matching is intentional: it mirrors the harness\n * `ContentBlockMap` vocabulary (`@deepseek-ai/dsh-llm`: `text`, `reasoning`,\n * `image`, `tool-call`, `tool-result`). Provider wire variants (`image_url`,\n * `input_image`, …) are normalized by adapters before reaching the log.\n */\nfunction blocksContainImage(blocks: readonly unknown[]): boolean {\n for (const block of blocks) {\n const candidate = block as { type?: unknown; content?: unknown };\n if (candidate?.type === \"image\") return true;\n if (candidate?.type === \"tool-result\" && Array.isArray(candidate.content) && blocksContainImage(candidate.content)) return true;\n }\n return false;\n}\n"],"mappings":";AAsBA,SAAkB,eAAe;AACjC,OAAO,OAAO;AACd,SAAS,oBAAoB;AAC7B,SAAS,wBAAwB,yBAAyB;;;ACwDnD,IAAM,yBAAyB;AAa/B,SAAS,QAAQ,OAA2B;AACjD,QAAM,UAAW,OAAiC;AAClD,QAAM,QAAS,SAAyC;AACxD,MAAI,OAAO,UAAU,YAAY,SAAS,EAAG,QAAO;AACpD,QAAM,UAAW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K,EACjD;AAAA,UACC;AAAA,UACA,IAAI,OAAO,UACP,oCAAoC,IAAI,OAAO,KAAK,MACpD;AAAA,QACN;AAAA,MACJ,CAAC;AAAA,IACH;AACA,QAAI,IAAI,OAAO;AACb,WAAK,eAAe,KAAK,QAAQ,OAAO,SAAS;AAAA,QAC/C,MAAM;AAAA,QACN,aAAa;AAAA,QACb,WAAW;AAAA,QACX,SAAS;AAAA,QACT,QAAQ;AAAA,MACV,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["stamped","effort"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-model-router",
3
- "description": "DeepSeek Harness plugin: role-based model routing — the planner agent runs on deepseek-v4-pro, delegated executor subagents run on deepseek-v4-flash.",
4
- "version": "0.6.2",
3
+ "description": "DeepSeek Harness plugin: role-based model routing — both roles default to deepseek-flash (V4.1 Flash, native multimodal) until V4.1-Pro launches.",
4
+ "version": "0.7.0",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -6,18 +6,22 @@ whenToUse: Use when a task combines planning and implementation: before writing
6
6
 
7
7
  # Pro planner / Flash executor routing
8
8
 
9
- This session routes models by role:
9
+ This session routes models by role (both default to deepseek-flash, V4.1 Flash with native multimodal; vision needs no separate model):
10
10
 
11
- - **Planner (the root agent)** — `deepseek-v4-pro`. Planning, design decisions, reviewing delegated output, and user-facing synthesis happen here.
12
- - **Executors (every subagent)** — `deepseek-v4-flash`. Implementation work happens there: writing code, running commands, builds, and tests. The harness forces the model automatically; you do not select it.
11
+ - **Planner (this agent)** — `deepseek-flash`. Planning, design decisions, reviewing delegated output, and user-facing synthesis happen here.
12
+ - **Executors (every subagent)** — `deepseek-flash`. Implementation work happens there: writing code, running commands, builds, and tests. The harness forces the model automatically; you do not select it.
13
13
 
14
14
  ## Working rhythm
15
15
 
16
16
  1. **Plan here.** Explore, decide the approach, and (when plan mode is on) submit the plan with `exit_plan_mode`. The plan stays on this agent.
17
- 2. **Delegate the execution.** Once a plan is approved, hand each self-contained chunk of implementation to a subagent with a complete prompt: exact files to touch, the change to make, and how to verify. Subagents are automatically routed to `deepseek-v4-flash`, so keep them execution-focused: give them the decision, not the decision to make.
17
+ 2. **Delegate the execution.** Once a plan is approved, hand each self-contained chunk of implementation to a subagent with a complete prompt: exact files to touch, the change to make, and how to verify. Subagents are automatically routed to `deepseek-flash`, so keep them execution-focused: give them the decision, not the decision to make.
18
18
  3. **Review here.** Read the subagent's result on this agent, verify it yourself (tests, diffs, logs), and iterate with follow-up messages to the same subagent when available.
19
19
  4. **Report here.** Summaries, plans, and answers to the user come from this agent.
20
20
 
21
+ ## Keep this agent's context lean
22
+
23
+ Input tokens are the expensive part of the planner. Don't re-read large files or full transcripts on this agent — trust the subagent's final report. Prefer targeted reads (offset/limit) over whole files. When the context grows, compact rather than re-sending everything.
24
+
21
25
  ## Delegation guidelines
22
26
 
23
27
  - Start independent delegations together in one assistant message and continue useful work while they run (background mode by default).
@@ -27,5 +31,5 @@ This session routes models by role:
27
31
 
28
32
  ## Verification
29
33
 
30
- - Executor output was produced by `deepseek-v4-flash`; planner output by `deepseek-v4-pro`. If you need to confirm, check the session log's model metadata.
34
+ - Both roles produce `deepseek-flash` output (unified since V4.1; split again when V4.1-Pro lands). If you need to confirm, check the session log's model metadata.
31
35
  - If routing ever looks wrong, the `model-router` plugin row in the profile composition is the single place that owns it.