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.
@@ -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
+ }