opencode-model-router 1.6.0 → 1.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/CHANGELOG.md +357 -0
- package/README.md +156 -11
- package/package.json +3 -2
- package/src/commands/output.ts +132 -26
- package/src/index.ts +165 -6
- package/src/router/agent-options.ts +17 -8
- package/src/router/catalog.ts +457 -0
- package/src/router/config.ts +58 -5
- package/src/router/sessions.ts +140 -5
- package/src/router/subagents.ts +85 -0
package/src/router/sessions.ts
CHANGED
|
@@ -164,12 +164,124 @@ function normTaskKw(kw: string): string {
|
|
|
164
164
|
return kw.toLowerCase().split("(")[0]!.split("/")[0]!.trim();
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
+
/**
|
|
168
|
+
* A dispatch is "multi-step" when its text describes a sequence rather than one
|
|
169
|
+
* lookup: an ordered/bulleted/step list item, an explicit sequencing or
|
|
170
|
+
* distributive word, or shell-style command chaining (`;`, `&&`). These are the
|
|
171
|
+
* cheapest reliable signals that the subagent is expected to make several tool
|
|
172
|
+
* calls, which is exactly the case proportional bypass must NOT exempt from
|
|
173
|
+
* enforcement.
|
|
174
|
+
*
|
|
175
|
+
* List markers accept `1.`, `1)` and `1:` because "Step 1: ... Step 2: ..." is a
|
|
176
|
+
* common phrasing; `step N` is also matched inline, since it frequently appears
|
|
177
|
+
* mid-line rather than at the start of one.
|
|
178
|
+
*/
|
|
179
|
+
const MULTI_STEP_RE =
|
|
180
|
+
/(?:^\s*(?:[-*+]|\d+[.):])\s)|\bstep\s*\d+\s*[.):]|\bthen\b|\bone at a time\b|\bone-at-a-time\b|\bsequentially\b|\bin order\b|\bin this exact order\b|\beach\b|\bafter that\b|\bfor every\b|;|&&/im;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* An enumeration of three or more named subjects ("router, guard and verify")
|
|
184
|
+
* describes breadth even when no file is named and no sequencing word appears.
|
|
185
|
+
* Two items are deliberately NOT enough: "read a.json, b.json" is already caught
|
|
186
|
+
* by the path count, and a two-item phrase is common in single-shot requests.
|
|
187
|
+
*/
|
|
188
|
+
const ENUMERATION_RE =
|
|
189
|
+
/[\w./-]+\s*,\s*[\w./-]+\s*(?:,\s*[\w./-]+|\b(?:and|or)\s+[\w./-]+)/i;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Bare distributive phrasing — "summarize every config file", "list all guard
|
|
193
|
+
* modules" — describes breadth over a whole class of targets with none of the
|
|
194
|
+
* other breadth signals: no comma enumeration, no connector, no second
|
|
195
|
+
* imperative line, no named path, and well under the length backstop. It is
|
|
196
|
+
* still a fan-out, so it must not be trivial.
|
|
197
|
+
*
|
|
198
|
+
* `each` and `for every` are handled by `MULTI_STEP_RE`; this gate covers the
|
|
199
|
+
* remaining bare `every`/`all` forms. The quantifier only counts when it scopes
|
|
200
|
+
* a plural or collective TARGET class ("all guard modules", "every config
|
|
201
|
+
* file"), which is what keeps depth phrasing over ONE file trivial: in "read
|
|
202
|
+
* every line of package.json" and "read all of src/index.ts" the quantifier is
|
|
203
|
+
* partitive, so the intervening-word class refuses to cross `of`, and the head
|
|
204
|
+
* noun may not be followed by a file extension — otherwise `package` in
|
|
205
|
+
* `package.json` reads as a collective, since `.` is a word boundary. The
|
|
206
|
+
* generic plural branch carries a stop-list because English has many singular
|
|
207
|
+
* words ending in `s` — "what does all this mean in tiers.json" must stay a
|
|
208
|
+
* single-file lookup.
|
|
209
|
+
*/
|
|
210
|
+
const DISTRIBUTIVE_RE =
|
|
211
|
+
/\b(?:every|all)\s+(?:(?:the|these|those|other|remaining)\s+)?(?:(?!of\b)[a-z][a-z-]*\s+){0,2}(?:files?|modules?|tests?|configs?|dirs?|directory|directories|components?|routes?|stores?|handlers?|guards?|helpers?|scripts?|packages?|classes?|functions?|endpoints?|entries?|(?!(?:this|its|his|hers|thus|was|has|does|less|plus|yes|gas|bus|css|js|status|focus|process|access|address|business|class|success)\b)[a-z][a-z-]{2,}s)\b(?!\.[a-z])/i;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Lines that open with an imperative verb. Two or more of them is a task list
|
|
215
|
+
* written as prose. Matched per line so the `Working directory:` / `Platform:` /
|
|
216
|
+
* `Shell:` footer described below cannot inflate the count.
|
|
217
|
+
*/
|
|
218
|
+
const IMPERATIVE_LINE_RE =
|
|
219
|
+
/^\s*(?:read|search|grep|list|find|check|report|summari[sz]e|open|inspect|show|tell|explain|analy[sz]e|compare|verify|count|locate|trace)\b/i;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* File-path-like tokens, counted to detect multi-file recon. Deliberately
|
|
223
|
+
* requires a known file EXTENSION rather than accepting any slashed path.
|
|
224
|
+
*
|
|
225
|
+
* Dispatch prompts commonly arrive with a `Working directory: <cwd>` footer.
|
|
226
|
+
* That footer is NOT emitted by this plugin — nothing in this repo writes it; it
|
|
227
|
+
* comes from the host/orchestrator's own prompt conventions. Requiring an
|
|
228
|
+
* extension is what keeps a POSIX cwd (`/home/u/proj`) from being miscounted as
|
|
229
|
+
* a target file.
|
|
230
|
+
*/
|
|
231
|
+
const PATH_TOKEN_RE =
|
|
232
|
+
/[\w./\\-]*[\w-]\.(?:ts|tsx|js|jsx|mjs|cjs|json|jsonc|md|ya?ml|toml|txt|css|scss|html|py|rs|go|rb|java|sh|ps1|sql|lock)\b/gi;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Well-known extensionless files, which `PATH_TOKEN_RE` cannot see. Matched
|
|
236
|
+
* case-SENSITIVELY against the raw text so the conventional capitalisation is
|
|
237
|
+
* required: "read Makefile and LICENSE" names two files, while "read the license
|
|
238
|
+
* field in package.json" names one. The lookahead prevents double-counting
|
|
239
|
+
* `README.md`, which `PATH_TOKEN_RE` already matches.
|
|
240
|
+
*/
|
|
241
|
+
const BARE_FILENAME_RE =
|
|
242
|
+
/\b(?:Makefile|Dockerfile|LICENSE|README|CHANGELOG|Gemfile|Rakefile|Procfile|NOTICE|CODEOWNERS)\b(?!\.\w)/g;
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* At most this many distinct target files still counts as a single-shot lookup.
|
|
246
|
+
*
|
|
247
|
+
* Zero paths is deliberately still eligible: "search the codebase for X" and
|
|
248
|
+
* "grep for the handler" name no file yet are the canonical cheap lookups, and
|
|
249
|
+
* requiring exactly one path would reclassify them. Breadth that names no file
|
|
250
|
+
* is caught by `ENUMERATION_RE` and `IMPERATIVE_LINE_RE` instead.
|
|
251
|
+
*/
|
|
252
|
+
const MAX_TRIVIAL_PATHS = 1;
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Backstop for multi-step dispatches that use none of the marker words above:
|
|
256
|
+
* a genuine single-shot lookup is short. Sized well above the real single-shot
|
|
257
|
+
* prompts ("read package.json and tell me the version") and well below a real
|
|
258
|
+
* recon brief, and measured AFTER the cwd/platform footer so that footer can
|
|
259
|
+
* never by itself push a dispatch over the line.
|
|
260
|
+
*/
|
|
261
|
+
const MAX_TRIVIAL_CHARS = 240;
|
|
262
|
+
|
|
167
263
|
/**
|
|
168
264
|
* Classify a dispatch as "trivial" AT DISPATCH TIME (m2): conservative,
|
|
169
|
-
* tier-gated.
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
265
|
+
* tier-gated. Trivial means the GA-6 proportionality case — a SINGLE-SHOT
|
|
266
|
+
* lookup — not merely "read-only". A dispatch is trivial only when ALL hold:
|
|
267
|
+
*
|
|
268
|
+
* 1. tier is `fast` (medium/heavy is never trivial), and
|
|
269
|
+
* 2. the text is non-empty, and
|
|
270
|
+
* 3. it carries NO medium/heavy taskPattern signal, and
|
|
271
|
+
* 4. it matches a fast taskPattern stem, and
|
|
272
|
+
* 5. it names at most `MAX_TRIVIAL_PATHS` distinct file paths, counting both
|
|
273
|
+
* extensioned paths and well-known extensionless files, and
|
|
274
|
+
* 6. it carries no multi-step marker (`MULTI_STEP_RE`), no 3+ item
|
|
275
|
+
* enumeration (`ENUMERATION_RE`), no distributive breadth quantifier
|
|
276
|
+
* (`DISTRIBUTIVE_RE`), and no second imperative line, and
|
|
277
|
+
* 7. it is at most `MAX_TRIVIAL_CHARS` long.
|
|
278
|
+
*
|
|
279
|
+
* Clauses 5-7 are the narrowing. Without them ANY `fast` dispatch containing a
|
|
280
|
+
* stem like "read" or "search" was trivial, which silently exempted multi-file
|
|
281
|
+
* recon from enforced-mode hard blocks — the read_budget guard could therefore
|
|
282
|
+
* never fire on a `@fast` subagent, defeating the guard it exists to apply.
|
|
283
|
+
* Real work is still NEVER trivial, so bypass still cannot disable enforcement
|
|
284
|
+
* on implementation.
|
|
173
285
|
*/
|
|
174
286
|
export function classifyTrivial(
|
|
175
287
|
dispatchText: string,
|
|
@@ -177,7 +289,8 @@ export function classifyTrivial(
|
|
|
177
289
|
cfg: RouterConfig,
|
|
178
290
|
): boolean {
|
|
179
291
|
if (tier !== "fast") return false;
|
|
180
|
-
const
|
|
292
|
+
const raw = dispatchText || "";
|
|
293
|
+
const text = raw.toLowerCase();
|
|
181
294
|
if (!text.trim()) return false;
|
|
182
295
|
const disqualifiers = [
|
|
183
296
|
...(cfg.taskPatterns?.medium ?? []),
|
|
@@ -187,6 +300,28 @@ export function classifyTrivial(
|
|
|
187
300
|
const n = normTaskKw(kw);
|
|
188
301
|
if (n.length >= 3 && text.includes(n)) return false;
|
|
189
302
|
}
|
|
303
|
+
|
|
304
|
+
// Shape gates: a single-shot lookup is short, names at most one file, and
|
|
305
|
+
// does not enumerate steps or subjects. Checked before the fast-stem match so
|
|
306
|
+
// that a multi-file recon prompt can never be rescued by containing "read".
|
|
307
|
+
if (raw.length > MAX_TRIVIAL_CHARS) return false;
|
|
308
|
+
if (MULTI_STEP_RE.test(raw)) return false;
|
|
309
|
+
if (ENUMERATION_RE.test(raw)) return false;
|
|
310
|
+
if (DISTRIBUTIVE_RE.test(raw)) return false;
|
|
311
|
+
|
|
312
|
+
const imperativeLines = raw
|
|
313
|
+
.split(/\r?\n/)
|
|
314
|
+
.filter((line) => IMPERATIVE_LINE_RE.test(line)).length;
|
|
315
|
+
if (imperativeLines > 1) return false;
|
|
316
|
+
|
|
317
|
+
const paths = new Set<string>(
|
|
318
|
+
(text.match(PATH_TOKEN_RE) ?? []).map((p) => p.trim()),
|
|
319
|
+
);
|
|
320
|
+
for (const bare of raw.match(BARE_FILENAME_RE) ?? []) {
|
|
321
|
+
paths.add(bare.toLowerCase());
|
|
322
|
+
}
|
|
323
|
+
if (paths.size > MAX_TRIVIAL_PATHS) return false;
|
|
324
|
+
|
|
190
325
|
const fast = cfg.taskPatterns?.fast ?? [];
|
|
191
326
|
for (const kw of fast) {
|
|
192
327
|
const n = normTaskKw(kw);
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Preset } from "./config";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Model override to apply to a pre-existing subagent. `variant` is always
|
|
5
|
+
* present in the result (possibly `undefined`) so callers can distinguish
|
|
6
|
+
* "this tier has no variant" from "leave whatever was there" — an inherited
|
|
7
|
+
* variant from another provider is a silent dispatch-time failure.
|
|
8
|
+
*/
|
|
9
|
+
export interface SubagentOverride {
|
|
10
|
+
model: string;
|
|
11
|
+
variant: string | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isPlainObject(v: unknown): v is Record<string, unknown> {
|
|
15
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolves `subagentTiers` (agent name → tier name) into concrete per-agent
|
|
20
|
+
* model overrides, using the models of the *active* preset. This is what lets
|
|
21
|
+
* a pre-existing agent follow `/preset` instead of pinning a model id in its
|
|
22
|
+
* own definition.
|
|
23
|
+
*
|
|
24
|
+
* Pure. Entries are skipped — never throw — when:
|
|
25
|
+
* - the named tier does not exist in the active preset (e.g. a map written
|
|
26
|
+
* for a preset that defines a tier this one doesn't),
|
|
27
|
+
* - the agent name collides with a tier name, which would clobber the
|
|
28
|
+
* plugin's own `@fast`/`@medium`/`@heavy` agents,
|
|
29
|
+
* - the agent is already registered with a `mode` other than "subagent";
|
|
30
|
+
* primary agents are the orchestrator, and pinning one to a tier model
|
|
31
|
+
* would defeat the routing design.
|
|
32
|
+
*
|
|
33
|
+
* Note this can only see agents already present in the opencode config —
|
|
34
|
+
* markdown-defined agents are loaded separately, so their `mode` is not
|
|
35
|
+
* visible here. Listing a primary agent defined in markdown is not caught.
|
|
36
|
+
*/
|
|
37
|
+
export function resolveSubagentOverrides(input: {
|
|
38
|
+
subagentTiers?: Record<string, string>;
|
|
39
|
+
tiers: Preset;
|
|
40
|
+
/** The opencode config's `agent` record, when already populated. */
|
|
41
|
+
existingAgents?: Record<string, unknown>;
|
|
42
|
+
}): Record<string, SubagentOverride> {
|
|
43
|
+
const { subagentTiers, tiers, existingAgents } = input;
|
|
44
|
+
if (!isPlainObject(subagentTiers)) return {};
|
|
45
|
+
|
|
46
|
+
const out: Record<string, SubagentOverride> = {};
|
|
47
|
+
|
|
48
|
+
for (const [agentName, tierName] of Object.entries(subagentTiers)) {
|
|
49
|
+
if (typeof tierName !== "string" || tierName === "") continue;
|
|
50
|
+
if (agentName === "") continue;
|
|
51
|
+
|
|
52
|
+
// Never rewrite the plugin's own tier agents.
|
|
53
|
+
if (Object.prototype.hasOwnProperty.call(tiers, agentName)) continue;
|
|
54
|
+
|
|
55
|
+
const tier = tiers[tierName];
|
|
56
|
+
if (!tier || typeof tier.model !== "string" || tier.model === "") continue;
|
|
57
|
+
|
|
58
|
+
const existing = existingAgents?.[agentName];
|
|
59
|
+
if (isPlainObject(existing) && typeof existing.mode === "string") {
|
|
60
|
+
if (existing.mode !== "subagent") continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
out[agentName] = { model: tier.model, variant: tier.variant };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Merges a resolved override onto an agent's existing config entry.
|
|
71
|
+
*
|
|
72
|
+
* `variant` is set or removed explicitly rather than spread, so a tier without
|
|
73
|
+
* a variant clears one left behind by a previous preset instead of inheriting
|
|
74
|
+
* it. Every other field on the entry is preserved.
|
|
75
|
+
*/
|
|
76
|
+
export function mergeSubagentOverride(
|
|
77
|
+
existing: unknown,
|
|
78
|
+
override: SubagentOverride,
|
|
79
|
+
): Record<string, unknown> {
|
|
80
|
+
const base = isPlainObject(existing) ? { ...existing } : {};
|
|
81
|
+
base.model = override.model;
|
|
82
|
+
if (override.variant) base.variant = override.variant;
|
|
83
|
+
else delete base.variant;
|
|
84
|
+
return base;
|
|
85
|
+
}
|