opencode-model-router 1.4.0 → 1.6.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 +134 -12
- package/package.json +1 -1
- package/src/commands/output.ts +236 -0
- package/src/guard/enforce.ts +14 -1
- package/src/guard/guards.ts +26 -0
- package/src/guard/store.ts +46 -3
- package/src/index.ts +272 -321
- package/src/router/agent-options.ts +118 -0
- package/src/router/config.ts +555 -46
- package/src/router/idle-sweep.ts +35 -0
- package/src/router/jsonc.ts +92 -0
- package/src/router/prompts.ts +72 -0
- package/src/router/protocol.ts +1 -1
- package/src/router/sessions.ts +159 -22
- package/src/telemetry/trajectory.ts +48 -1
- package/src/verify/checker.ts +47 -1
- package/src/verify/deterministic.ts +13 -5
- package/src/verify/dispatch.ts +27 -2
- package/src/verify/gate.ts +25 -1
- package/src/verify/paths.ts +29 -0
- package/src/verify/timeout.ts +93 -0
- package/src/verify/wiring.ts +267 -0
- package/tiers.json +79 -6
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registration-path agent options.
|
|
3
|
+
*
|
|
4
|
+
* `src/index.ts` turns each tier of the active preset into an opencode agent
|
|
5
|
+
* definition; this module owns the `options` bag on that definition. It is the
|
|
6
|
+
* only place that knows how a tier's provider-agnostic `effort` maps onto the
|
|
7
|
+
* provider-specific knob (`effort` for Anthropic, `reasoning_effort` for
|
|
8
|
+
* OpenAI) and what to do when a tier asks for something the provider cannot do.
|
|
9
|
+
*
|
|
10
|
+
* The display-path builder in `src/commands/output.ts` renders what a tier is
|
|
11
|
+
* configured with; this one decides what is actually sent. They are kept apart
|
|
12
|
+
* on purpose: `/tiers` should not warn, downgrade or drop keys.
|
|
13
|
+
*/
|
|
14
|
+
import type { TierConfig } from "./config";
|
|
15
|
+
import { EFFORT_LEVELS } from "./config";
|
|
16
|
+
import { isClaudeModel } from "./protocol";
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Warn-once
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
// Agent registration runs on every `config` hook, so an unqualified
|
|
23
|
+
// console.warn would repeat the same line for the life of the session. Keys are
|
|
24
|
+
// deliberately tight (tier name + offending value where the value matters), so
|
|
25
|
+
// two different mistakes are still two different warnings.
|
|
26
|
+
const warnedAgentOptionsEffort = new Set<string>();
|
|
27
|
+
|
|
28
|
+
export function warnAgentOptionsEffortOnce(key: string, message: string): void {
|
|
29
|
+
if (warnedAgentOptionsEffort.has(key)) return;
|
|
30
|
+
warnedAgentOptionsEffort.add(key);
|
|
31
|
+
console.warn(`[model-router] ${message}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function resetAgentOptionsEffortWarnings(): void {
|
|
35
|
+
warnedAgentOptionsEffort.clear();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isEffortLevel(value: unknown): value is (typeof EFFORT_LEVELS)[number] {
|
|
39
|
+
return typeof value === "string" && EFFORT_LEVELS.some((level) => level === value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isOpenAIModel(model: string): boolean {
|
|
43
|
+
const s = model.toLowerCase();
|
|
44
|
+
return s.startsWith("openai/") || /\bgpt-/.test(s) || /(^|[/\-_])o[134]([/\-_]|$)/.test(s);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Provider-specific agent options for a tier, as registered with opencode.
|
|
49
|
+
*
|
|
50
|
+
* Precedence, highest first: explicit `thinking` (Anthropic) or explicit
|
|
51
|
+
* `reasoning.effort` (OpenAI), then the provider-agnostic `effort`. A key is
|
|
52
|
+
* only ever present when something asked for it — an unset `effort` leaves no
|
|
53
|
+
* trace in the returned object.
|
|
54
|
+
*/
|
|
55
|
+
export function buildAgentOptions(tier: TierConfig, tierName = tier.model): Record<string, unknown> {
|
|
56
|
+
const opts: Record<string, unknown> = {};
|
|
57
|
+
|
|
58
|
+
// Anthropic thinking config
|
|
59
|
+
if (tier.thinking?.budgetTokens != null) {
|
|
60
|
+
if (tier.thinking.budgetTokens) {
|
|
61
|
+
opts.budget_tokens = tier.thinking.budgetTokens;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// OpenAI reasoning config
|
|
66
|
+
if (tier.reasoning) {
|
|
67
|
+
if (tier.reasoning.effort) {
|
|
68
|
+
opts.reasoning_effort = tier.reasoning.effort;
|
|
69
|
+
}
|
|
70
|
+
if (tier.reasoning.summary) {
|
|
71
|
+
opts.reasoning_summary = tier.reasoning.summary;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const effort: unknown = tier.effort;
|
|
76
|
+
if (effort !== undefined) {
|
|
77
|
+
if (!isEffortLevel(effort)) {
|
|
78
|
+
// Loading tiers.json rejects this, but an override layer or a
|
|
79
|
+
// programmatic caller can still hand us junk. Ignore it rather than
|
|
80
|
+
// failing registration for the whole preset.
|
|
81
|
+
warnAgentOptionsEffortOnce(
|
|
82
|
+
`invalid:${tierName}:${String(effort)}`,
|
|
83
|
+
`tier ${tierName}: invalid effort '${String(effort)}' ignored`,
|
|
84
|
+
);
|
|
85
|
+
} else if (isClaudeModel(tier.model)) {
|
|
86
|
+
if (tier.thinking?.budgetTokens != null) {
|
|
87
|
+
warnAgentOptionsEffortOnce(
|
|
88
|
+
`anthropic-conflict:${tierName}`,
|
|
89
|
+
`tier ${tierName}: both thinking and effort set; explicit thinking wins`,
|
|
90
|
+
);
|
|
91
|
+
} else {
|
|
92
|
+
opts.effort = effort;
|
|
93
|
+
}
|
|
94
|
+
} else if (isOpenAIModel(tier.model)) {
|
|
95
|
+
if (tier.reasoning?.effort) {
|
|
96
|
+
warnAgentOptionsEffortOnce(
|
|
97
|
+
`openai-conflict:${tierName}`,
|
|
98
|
+
`tier ${tierName}: both reasoning.effort and effort set; explicit reasoning wins`,
|
|
99
|
+
);
|
|
100
|
+
} else if (effort === "xhigh" || effort === "max") {
|
|
101
|
+
warnAgentOptionsEffortOnce(
|
|
102
|
+
`openai-downgrade:${tierName}:${effort}`,
|
|
103
|
+
`tier ${tierName}: downgrading effort '${effort}' to 'high' because OpenAI reasoning_effort only supports low, medium, or high`,
|
|
104
|
+
);
|
|
105
|
+
opts.reasoning_effort = "high";
|
|
106
|
+
} else {
|
|
107
|
+
opts.reasoning_effort = effort;
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
warnAgentOptionsEffortOnce(
|
|
111
|
+
`unknown-provider:${tierName}`,
|
|
112
|
+
`tier ${tierName}: effort ignored for unknown provider family '${tier.model}'`,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return opts;
|
|
118
|
+
}
|