opencode-tier-router 0.0.0-stage → 0.1.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,195 @@
1
+ import { For, Show } from "solid-js";
2
+ import type { Ui } from "@opencode/plugin/gui";
3
+ import type { TaskKind } from "../types";
4
+ import type { Api } from "./api";
5
+
6
+ const KNOWN_CLASSIFIERS = ["opencode-go/jev-1.14", "opencode-go/jev-1.13", "opencode/jev-1.13-free"];
7
+ const KINDS: TaskKind[] = ["plan", "implement", "debug", "refactor", "review", "explain", "chat"];
8
+
9
+ const numberFrom = (text: string): number | undefined => {
10
+ const trimmed = text.trim();
11
+ if (!trimmed) return undefined;
12
+ const value = Number(trimmed);
13
+ return Number.isFinite(value) && value >= 0 ? value : undefined;
14
+ };
15
+
16
+ export function Advanced(props: { api: Api; ui: Ui; t: (key: string) => string }) {
17
+ const config = () => props.api.config();
18
+ const tierNames = () => (config()?.tiers ?? []).map((tier) => tier.name);
19
+
20
+ return (
21
+ <>
22
+ <props.ui.SettingsSection
23
+ title={props.t("section.classifier")}
24
+ footer={
25
+ <Show when={props.api.probe()}>
26
+ <span>
27
+ {props.api.probe()!.ok
28
+ ? `${props.t("classifier.probe.ok")} ${props.api.probe()!.latencyMs} ms`
29
+ : `${props.t("classifier.probe.fail")} ${props.api.probe()!.error ?? ""}`}
30
+ </span>
31
+ </Show>
32
+ }
33
+ >
34
+ <props.ui.SettingsRow id="classifier-model" title={props.t("classifier.model")} description={props.t("classifier.model.detail")}>
35
+ <div style={{ display: "flex", gap: "6px", "align-items": "center", "flex-wrap": "wrap" }}>
36
+ <props.ui.Select
37
+ label={props.t("classifier.model")}
38
+ value={KNOWN_CLASSIFIERS.includes(config()?.classifier?.model ?? "") ? config()!.classifier.model : "custom"}
39
+ options={[
40
+ ...KNOWN_CLASSIFIERS.map((model) => ({ value: model, label: model })),
41
+ { value: "custom", label: props.t("classifier.custom") },
42
+ ]}
43
+ onChange={(value) =>
44
+ props.api.patchNested("classifier", {
45
+ model: value === "custom" ? config()!.classifier.model : value,
46
+ })
47
+ }
48
+ />
49
+ <props.ui.TextInput
50
+ label={props.t("classifier.model")}
51
+ value={config()?.classifier?.model ?? ""}
52
+ onInput={(text) => props.api.patchNested("classifier", { model: text })}
53
+ />
54
+ <props.ui.Button size="small" onClick={() => void props.api.testClassifier()}>
55
+ {props.t("classifier.test")}
56
+ </props.ui.Button>
57
+ </div>
58
+ </props.ui.SettingsRow>
59
+ <props.ui.SettingsRow id="classifier-timeout" title={props.t("classifier.timeout")}>
60
+ <props.ui.TextInput
61
+ label={props.t("classifier.timeout")}
62
+ value={config()?.classifier?.timeoutMs?.toString() ?? ""}
63
+ onInput={(text) => props.api.patchNested("classifier", { timeoutMs: numberFrom(text) ?? 900 })}
64
+ />
65
+ </props.ui.SettingsRow>
66
+ <props.ui.SettingsRow id="classifier-confidence" title={props.t("classifier.confidence")}>
67
+ <props.ui.TextInput
68
+ label={props.t("classifier.confidence")}
69
+ value={config()?.classifier?.confidenceThreshold?.toString() ?? ""}
70
+ onInput={(text) => props.api.patchNested("classifier", { confidenceThreshold: numberFrom(text) ?? 0.34 })}
71
+ />
72
+ </props.ui.SettingsRow>
73
+ <props.ui.SettingsRow id="classifier-minchars" title={props.t("classifier.minchars")}>
74
+ <props.ui.TextInput
75
+ label={props.t("classifier.minchars")}
76
+ value={config()?.classifier?.minPromptChars?.toString() ?? ""}
77
+ onInput={(text) => props.api.patchNested("classifier", { minPromptChars: numberFrom(text) ?? 12 })}
78
+ />
79
+ </props.ui.SettingsRow>
80
+ </props.ui.SettingsSection>
81
+
82
+ <props.ui.SettingsSection title={props.t("section.cache")}>
83
+ <props.ui.SettingsRow id="cache-tokens" title={props.t("cache.tokens")}>
84
+ <props.ui.TextInput
85
+ label={props.t("cache.tokens")}
86
+ value={config()?.cache?.minTokens?.toString() ?? ""}
87
+ onInput={(text) => props.api.patchNested("cache", { minTokens: numberFrom(text) ?? 20_000 })}
88
+ />
89
+ </props.ui.SettingsRow>
90
+ <props.ui.SettingsRow id="cache-delta" title={props.t("cache.delta")}>
91
+ <props.ui.TextInput
92
+ label={props.t("cache.delta")}
93
+ value={config()?.cache?.minTierDelta?.toString() ?? ""}
94
+ onInput={(text) => props.api.patchNested("cache", { minTierDelta: numberFrom(text) ?? 1 })}
95
+ />
96
+ </props.ui.SettingsRow>
97
+ <props.ui.SettingsRow id="cache-penalty" title={props.t("cache.penalty")}>
98
+ <props.ui.TextInput
99
+ label={props.t("cache.penalty")}
100
+ value={config()?.cache?.maxPenaltyUsd?.toString() ?? ""}
101
+ onInput={(text) => props.api.patchNested("cache", { maxPenaltyUsd: numberFrom(text) ?? 0.05 })}
102
+ />
103
+ </props.ui.SettingsRow>
104
+ <props.ui.SettingsRow id="downgrade" title={props.t("cache.downgrade")}>
105
+ <props.ui.TextInput
106
+ label={props.t("cache.downgrade")}
107
+ value={config()?.downgradeDelta?.toString() ?? ""}
108
+ onInput={(text) => props.api.patch({ downgradeDelta: numberFrom(text) ?? 1 })}
109
+ />
110
+ </props.ui.SettingsRow>
111
+ <props.ui.SettingsRow id="fallback" title={props.t("cache.fallback")}>
112
+ <props.ui.Select
113
+ label={props.t("cache.fallback")}
114
+ value={config()?.fallbackTier ?? tierNames()[0]}
115
+ options={tierNames().map((name) => ({ value: name, label: name }))}
116
+ onChange={(value) => props.api.patch({ fallbackTier: value })}
117
+ />
118
+ </props.ui.SettingsRow>
119
+ </props.ui.SettingsSection>
120
+
121
+ <props.ui.SettingsSection title={props.t("section.kinds")}>
122
+ <For each={KINDS}>
123
+ {(kind) => (
124
+ <props.ui.SettingsRow id={`kind-${kind}`} title={kind}>
125
+ <props.ui.Select
126
+ label={kind}
127
+ value={config()?.kindMinimumTier?.[kind] ?? "none"}
128
+ options={[
129
+ { value: "none", label: props.t("tiers.none") },
130
+ ...tierNames().map((name) => ({ value: name, label: name })),
131
+ ]}
132
+ onChange={(value) => {
133
+ const next = { ...(config()?.kindMinimumTier ?? {}) };
134
+ if (value === "none") delete next[kind];
135
+ else next[kind] = value;
136
+ props.api.patch({ kindMinimumTier: next });
137
+ }}
138
+ />
139
+ </props.ui.SettingsRow>
140
+ )}
141
+ </For>
142
+ </props.ui.SettingsSection>
143
+
144
+ <props.ui.SettingsSection title={props.t("section.cascade")} footer={<span>{props.t("cascade.detail")}</span>}>
145
+ <props.ui.SettingsRow id="cascade-mode" title={props.t("cascade.mode")}>
146
+ <props.ui.Select
147
+ label={props.t("cascade.mode")}
148
+ value={config()?.cascade?.mode ?? "off"}
149
+ options={[
150
+ { value: "off", label: props.t("mode.off") },
151
+ { value: "shadow", label: props.t("mode.shadow") },
152
+ { value: "auto", label: props.t("mode.auto") },
153
+ ]}
154
+ onChange={(value) => props.api.patchNested("cascade", { mode: value })}
155
+ />
156
+ </props.ui.SettingsRow>
157
+ <props.ui.SettingsRow id="cascade-target" title={props.t("cascade.target")}>
158
+ <props.ui.Select
159
+ label={props.t("cascade.target")}
160
+ value={config()?.cascade?.targetTier ?? tierNames()[0]}
161
+ options={tierNames().map((name) => ({ value: name, label: name }))}
162
+ onChange={(value) => props.api.patchNested("cascade", { targetTier: value })}
163
+ />
164
+ </props.ui.SettingsRow>
165
+ <props.ui.SettingsRow id="cascade-threshold" title={props.t("cascade.threshold")}>
166
+ <props.ui.TextInput
167
+ label={props.t("cascade.threshold")}
168
+ value={config()?.cascade?.gateThreshold?.toString() ?? ""}
169
+ onInput={(text) => props.api.patchNested("cascade", { gateThreshold: numberFrom(text) ?? 0.5 })}
170
+ />
171
+ </props.ui.SettingsRow>
172
+ <props.ui.SettingsRow id="cascade-gate-timeout" title={props.t("cascade.gateTimeout")}>
173
+ <props.ui.TextInput
174
+ label={props.t("cascade.gateTimeout")}
175
+ value={config()?.cascade?.gateTimeoutMs?.toString() ?? ""}
176
+ onInput={(text) => props.api.patchNested("cascade", { gateTimeoutMs: numberFrom(text) ?? 1_500 })}
177
+ />
178
+ </props.ui.SettingsRow>
179
+ <props.ui.SettingsRow id="cascade-fetch-timeout" title={props.t("cascade.fetchTimeout")}>
180
+ <props.ui.TextInput
181
+ label={props.t("cascade.fetchTimeout")}
182
+ value={config()?.cascade?.fetchTimeoutMs?.toString() ?? ""}
183
+ onInput={(text) => props.api.patchNested("cascade", { fetchTimeoutMs: numberFrom(text) ?? 30_000 })}
184
+ />
185
+ </props.ui.SettingsRow>
186
+ </props.ui.SettingsSection>
187
+
188
+ <props.ui.SettingsSection title={props.t("section.advanced")} footer={<span>{props.t("advanced.providers.detail")}</span>}>
189
+ <props.ui.SettingsRow id="providers" title={props.t("advanced.providers")}>
190
+ <span>{(config()?.providers ?? []).join(", ")}</span>
191
+ </props.ui.SettingsRow>
192
+ </props.ui.SettingsSection>
193
+ </>
194
+ );
195
+ }
package/src/gui/api.ts ADDED
@@ -0,0 +1,168 @@
1
+ import { createSignal } from "solid-js";
2
+ import type { Context } from "@opencode/plugin/gui";
3
+ import { TierRouterRpc } from "../rpc";
4
+ import type { RouterConfig, Spend } from "../types";
5
+
6
+ export interface ModelOption {
7
+ model: string;
8
+ variants: string[];
9
+ inputPrice: number;
10
+ outputPrice: number;
11
+ cacheReadPrice: number;
12
+ context: number;
13
+ maxOutput: number;
14
+ }
15
+
16
+ export interface LastDecision {
17
+ at?: string;
18
+ sessionID?: string;
19
+ kind?: string;
20
+ source?: string;
21
+ tier?: string;
22
+ model?: string;
23
+ variant?: string | null;
24
+ confidence?: number;
25
+ }
26
+
27
+ export interface DailySpend {
28
+ day: string;
29
+ usd: number;
30
+ }
31
+
32
+ export interface CascadeStats {
33
+ mode: string;
34
+ last: {
35
+ at?: string;
36
+ gateProbability?: number;
37
+ accepted?: boolean;
38
+ wouldEscalate?: boolean;
39
+ escalated?: boolean;
40
+ draftTier?: string;
41
+ targetTier?: string;
42
+ gateMs?: number;
43
+ } | null;
44
+ recentCount: number;
45
+ wouldEscalateCount: number;
46
+ rate: number | null;
47
+ }
48
+
49
+ export interface StatusPayload {
50
+ mode: string;
51
+ routingActive: boolean;
52
+ lastDecision: LastDecision | null;
53
+ spend: Spend & { daily?: DailySpend[] };
54
+ lastDecisions: LastDecision[];
55
+ cascadeStats?: CascadeStats;
56
+ fileErrors: string[];
57
+ optionsIgnored: boolean;
58
+ }
59
+
60
+ /**
61
+ * Signal store behind the settings page. All writes go through the plugin's
62
+ * RPC, so the server stays the only writer of the config file.
63
+ */
64
+ export function createApi(ctx: Context) {
65
+ const [config, setConfig] = createSignal<RouterConfig | null>(null);
66
+ const [models, setModels] = createSignal<ModelOption[]>([]);
67
+ const [status, setStatus] = createSignal<StatusPayload | null>(null);
68
+ const [error, setError] = createSignal<string | null>(null);
69
+ const [dirty, setDirty] = createSignal(false);
70
+ const [busy, setBusy] = createSignal(false);
71
+ const [probe, setProbe] = createSignal<{ ok: boolean; latencyMs?: number; error?: string } | null>(null);
72
+
73
+ const rpc = () => ctx.client.rpc(TierRouterRpc) as any;
74
+
75
+ const refreshStatus = async (): Promise<void> => {
76
+ setStatus((await rpc().status({})) as StatusPayload);
77
+ };
78
+
79
+ /** Reloads everything, but never discards an unsaved draft. */
80
+ const refresh = async (): Promise<void> => {
81
+ const [loaded, state] = await Promise.all([rpc().getConfig({}), rpc().status({})]);
82
+ if (!dirty()) setConfig(loaded.config as RouterConfig);
83
+ setModels((loaded.models ?? []) as ModelOption[]);
84
+ setStatus(state as StatusPayload);
85
+ setError(loaded.errors?.[0] ?? null);
86
+ };
87
+
88
+ const patch = (next: Partial<RouterConfig>): void => {
89
+ const current = config();
90
+ if (!current) return;
91
+ setConfig({ ...current, ...next });
92
+ setDirty(true);
93
+ };
94
+
95
+ const patchNested = (key: "classifier" | "budget" | "cache" | "cascade", next: Record<string, unknown>): void => {
96
+ const current = config();
97
+ if (!current) return;
98
+ patch({ [key]: { ...(current[key] as object), ...next } } as Partial<RouterConfig>);
99
+ };
100
+
101
+ const save = async (): Promise<boolean> => {
102
+ const current = config();
103
+ if (!current) return false;
104
+ setBusy(true);
105
+ try {
106
+ const result = await rpc().saveConfig({ config: current });
107
+ if (!result.ok) {
108
+ setError(result.errors?.[0] ?? "save refused");
109
+ return false;
110
+ }
111
+ setError(null);
112
+ await refresh();
113
+ return true;
114
+ } catch (cause) {
115
+ setError(String((cause as Error)?.message ?? cause));
116
+ return false;
117
+ } finally {
118
+ setBusy(false);
119
+ }
120
+ };
121
+
122
+ const resetDefaults = async (): Promise<void> => {
123
+ setBusy(true);
124
+ try {
125
+ // The server owns the defaults: drop the file, keep the options empty, reload.
126
+ const result = await rpc().resetConfig({});
127
+ if (result.ok) {
128
+ await refresh();
129
+ setError(null);
130
+ } else {
131
+ setError(result.errors?.[0] ?? "reset refused");
132
+ }
133
+ } catch (cause) {
134
+ setError(String((cause as Error)?.message ?? cause));
135
+ } finally {
136
+ setBusy(false);
137
+ }
138
+ };
139
+
140
+ const testClassifier = async (): Promise<void> => {
141
+ setProbe({ ok: true, latencyMs: undefined });
142
+ try {
143
+ const result = await rpc().probeClassifier({});
144
+ setProbe(result);
145
+ } catch (cause) {
146
+ setProbe({ ok: false, error: String((cause as Error)?.message ?? cause) });
147
+ }
148
+ };
149
+
150
+ return {
151
+ config,
152
+ models,
153
+ status,
154
+ error,
155
+ dirty,
156
+ busy,
157
+ probe,
158
+ refresh,
159
+ refreshStatus,
160
+ patch,
161
+ patchNested,
162
+ save,
163
+ resetDefaults,
164
+ testClassifier,
165
+ };
166
+ }
167
+
168
+ export type Api = ReturnType<typeof createApi>;
@@ -0,0 +1,78 @@
1
+ import { Show } from "solid-js";
2
+ import type { Ui } from "@opencode/plugin/gui";
3
+ import type { Api } from "./api";
4
+ import type { FeedbackMode } from "../types";
5
+
6
+ const numberFrom = (text: string): number | undefined => {
7
+ const trimmed = text.trim();
8
+ if (!trimmed) return undefined;
9
+ const value = Number(trimmed);
10
+ return Number.isFinite(value) && value >= 0 ? value : undefined;
11
+ };
12
+
13
+ export function ModeBudget(props: { api: Api; ui: Ui; t: (key: string) => string }) {
14
+ const config = () => props.api.config();
15
+
16
+ return (
17
+ <>
18
+ <props.ui.SettingsSection title={props.t("section.mode")}>
19
+ <props.ui.SettingsRow id="mode" title={props.t("mode.title")} description={props.t("mode.detail")}>
20
+ <props.ui.Select
21
+ label={props.t("mode.title")}
22
+ value={config()?.mode ?? "auto"}
23
+ options={[
24
+ { value: "auto", label: props.t("mode.auto") },
25
+ { value: "shadow", label: props.t("mode.shadow") },
26
+ { value: "off", label: props.t("mode.off") },
27
+ ]}
28
+ onChange={(value) => props.api.patch({ mode: value as "auto" | "shadow" | "off" })}
29
+ />
30
+ </props.ui.SettingsRow>
31
+ <props.ui.SettingsRow id="feedback" title={props.t("feedback.title")}>
32
+ <props.ui.Select
33
+ label={props.t("feedback.title")}
34
+ value={config()?.feedback ?? "information"}
35
+ options={[
36
+ { value: "information", label: props.t("feedback.information") },
37
+ { value: "confirmation", label: props.t("feedback.confirmation") },
38
+ ]}
39
+ onChange={(value) => props.api.patch({ feedback: value as FeedbackMode })}
40
+ />
41
+ </props.ui.SettingsRow>
42
+ </props.ui.SettingsSection>
43
+
44
+ <props.ui.SettingsSection title={props.t("section.budget")} footer={<span>{props.t("budget.detail")}</span>}>
45
+ <props.ui.SettingsRow id="budget-day" title={props.t("budget.day")}>
46
+ <props.ui.TextInput
47
+ label={props.t("budget.day")}
48
+ placeholder={props.t("budget.none")}
49
+ value={config()?.budget?.dailyUsd?.toString() ?? ""}
50
+ onInput={(text) => props.api.patchNested("budget", { dailyUsd: numberFrom(text) })}
51
+ />
52
+ </props.ui.SettingsRow>
53
+ <props.ui.SettingsRow id="budget-month" title={props.t("budget.month")}>
54
+ <props.ui.TextInput
55
+ label={props.t("budget.month")}
56
+ placeholder={props.t("budget.none")}
57
+ value={config()?.budget?.monthlyUsd?.toString() ?? ""}
58
+ onInput={(text) => props.api.patchNested("budget", { monthlyUsd: numberFrom(text) })}
59
+ />
60
+ </props.ui.SettingsRow>
61
+ <props.ui.SettingsRow id="budget-soft" title={props.t("budget.soft")}>
62
+ <props.ui.TextInput
63
+ label={props.t("budget.soft")}
64
+ value={config()?.budget?.softRatio?.toString() ?? ""}
65
+ onInput={(text) => props.api.patchNested("budget", { softRatio: numberFrom(text) ?? 0.7 })}
66
+ />
67
+ </props.ui.SettingsRow>
68
+ <props.ui.SettingsRow id="budget-hard" title={props.t("budget.hard")}>
69
+ <props.ui.TextInput
70
+ label={props.t("budget.hard")}
71
+ value={config()?.budget?.hardRatio?.toString() ?? ""}
72
+ onInput={(text) => props.api.patchNested("budget", { hardRatio: numberFrom(text) ?? 0.9 })}
73
+ />
74
+ </props.ui.SettingsRow>
75
+ </props.ui.SettingsSection>
76
+ </>
77
+ );
78
+ }
@@ -0,0 +1,91 @@
1
+ import { For, Show } from "solid-js";
2
+ import type { Api } from "./api";
3
+ import type { Ui } from "@opencode/plugin/gui";
4
+
5
+ const money = (value: number | undefined): string =>
6
+ `$${(value ?? 0).toFixed(value !== undefined && value < 1 ? 4 : 2)}`;
7
+
8
+ /** Read-only headline: routing state, last decision, spend. */
9
+ export function StatusBlock(props: { api: Api; ui: Ui; t: (key: string) => string }) {
10
+ const status = () => props.api.status();
11
+ const daily = () => status()?.spend.daily ?? [];
12
+ const peak = () => Math.max(0.000001, ...daily().map((day) => day.usd));
13
+
14
+ return (
15
+ <props.ui.SettingsSection title={props.t("section.state")}>
16
+ <props.ui.SettingsRow id="state-routing" title={props.t("state.routing")}>
17
+ <Show when={status()} fallback={<span>—</span>}>
18
+ <span>
19
+ {status()!.routingActive ? props.t("state.active") : props.t("state.inactive")} · {status()!.mode}
20
+ </span>
21
+ </Show>
22
+ </props.ui.SettingsRow>
23
+ <props.ui.SettingsRow id="state-last" title={props.t("state.last")} description={props.t("state.last.detail")}>
24
+ <Show when={status()?.lastDecision} fallback={<span>—</span>}>
25
+ <span>
26
+ {status()!.lastDecision!.tier} · {status()!.lastDecision!.model}
27
+ {status()!.lastDecision!.variant ? `#${status()!.lastDecision!.variant}` : ""}
28
+ </span>
29
+ </Show>
30
+ </props.ui.SettingsRow>
31
+ <props.ui.SettingsRow id="state-spend" title={props.t("state.spend")}>
32
+ <span>
33
+ {props.t("state.today")} {money(status()?.spend.todayUsd)} · {props.t("state.month")}{" "}
34
+ {money(status()?.spend.monthUsd)}
35
+ </span>
36
+ </props.ui.SettingsRow>
37
+ <Show when={status()?.cascadeStats}>
38
+ <props.ui.SettingsRow id="state-cascade" title={props.t("state.cascade")}>
39
+ <div style={{ display: "flex", "flex-direction": "column", gap: "2px" }}>
40
+ <Show when={status()!.cascadeStats!.last} fallback={<span>—</span>}>
41
+ <span style={{ "font-variant-numeric": "tabular-nums" }}>
42
+ p={status()!.cascadeStats!.last!.gateProbability ?? "—"} ·{" "}
43
+ {status()!.cascadeStats!.last!.accepted
44
+ ? props.t("state.cascade.accepted")
45
+ : status()!.cascadeStats!.last!.escalated
46
+ ? props.t("state.cascade.escalated")
47
+ : props.t("state.cascade.rejected")}{" "}
48
+ · {status()!.cascadeStats!.last!.draftTier}→{status()!.cascadeStats!.last!.targetTier} ·{" "}
49
+ {status()!.cascadeStats!.last!.gateMs ?? "—"}ms
50
+ </span>
51
+ </Show>
52
+ <span style={{ opacity: 0.7 }}>
53
+ {props.t("state.cascade.rate")} :{" "}
54
+ {status()!.cascadeStats!.rate === null ? "—" : `${Math.round(status()!.cascadeStats!.rate! * 100)}%`}{" "}
55
+ ({status()!.cascadeStats!.recentCount})
56
+ </span>
57
+ </div>
58
+ </props.ui.SettingsRow>
59
+ </Show>
60
+ <props.ui.SettingsRow id="state-days" title={props.t("state.days")}>
61
+ <div style={{ display: "flex", gap: "4px", "align-items": "flex-end", height: "36px" }}>
62
+ <For each={daily()}>
63
+ {(day) => (
64
+ <div
65
+ title={`${day.day}: ${money(day.usd)}`}
66
+ style={{
67
+ width: "14px",
68
+ height: `${Math.max(2, (day.usd / peak()) * 34)}px`,
69
+ background: "currentColor",
70
+ opacity: day.usd > 0 ? 0.8 : 0.15,
71
+ "border-radius": "2px",
72
+ }}
73
+ />
74
+ )}
75
+ </For>
76
+ </div>
77
+ </props.ui.SettingsRow>
78
+ <props.ui.SettingsRow id="state-decisions" title={props.t("state.decisions")}>
79
+ <div style={{ display: "flex", "flex-direction": "column", gap: "2px", "max-height": "180px", overflow: "auto" }}>
80
+ <For each={(status()?.lastDecisions ?? []).slice(0, 10)}>
81
+ {(entry) => (
82
+ <span style={{ "font-variant-numeric": "tabular-nums" }}>
83
+ {entry.at?.slice(11, 19)} · {entry.tier} · {entry.model} · {entry.source}
84
+ </span>
85
+ )}
86
+ </For>
87
+ </div>
88
+ </props.ui.SettingsRow>
89
+ </props.ui.SettingsSection>
90
+ );
91
+ }
@@ -0,0 +1,126 @@
1
+ import { For, Show } from "solid-js";
2
+ import type { Ui } from "@opencode/plugin/gui";
3
+ import type { Tier } from "../types";
4
+ import type { Api } from "./api";
5
+
6
+ const move = (tiers: Tier[], from: number, to: number): Tier[] => {
7
+ if (to < 0 || to >= tiers.length) return tiers;
8
+ const copy = [...tiers];
9
+ const [item] = copy.splice(from, 1);
10
+ copy.splice(to, 0, item);
11
+ return copy;
12
+ };
13
+
14
+ export function tierProblems(
15
+ tiers: Tier[],
16
+ variantsOf: (model: string) => string[],
17
+ t: (key: string) => string,
18
+ ): string[] {
19
+ const errors: string[] = [];
20
+ if (tiers.length < 2) errors.push(t("tiers.error.count"));
21
+ const names = new Set<string>();
22
+ for (const tier of tiers) {
23
+ if (names.has(tier.name)) errors.push(`${t("tiers.error.duplicate")}: ${tier.name}`);
24
+ names.add(tier.name);
25
+ if (!tier.model) errors.push(`${t("tiers.error.model")}: ${tier.name || "?"}`);
26
+ const variants = variantsOf(tier.model);
27
+ if (tier.variant !== null && variants.length > 0 && !variants.includes(tier.variant)) {
28
+ errors.push(`${t("tiers.error.variant")}: ${tier.name}`);
29
+ }
30
+ }
31
+ return errors;
32
+ }
33
+
34
+ export function TiersEditor(props: { api: Api; ui: Ui; t: (key: string) => string }) {
35
+ const tiers = () => props.api.config()?.tiers ?? [];
36
+ const models = () => props.api.models();
37
+
38
+ const variantsOf = (model: string): string[] =>
39
+ models().find((option) => option.model === model)?.variants ?? [];
40
+
41
+ const setTiers = (next: Tier[]): void => props.api.patch({ tiers: next });
42
+
43
+ const update = (index: number, patch: Partial<Tier>): void => {
44
+ setTiers(tiers().map((tier, i) => (i === index ? { ...tier, ...patch } : tier)));
45
+ };
46
+
47
+ const problems = () => tierProblems(tiers(), variantsOf, props.t);
48
+
49
+ return (
50
+ <props.ui.SettingsSection
51
+ title={props.t("section.tiers")}
52
+ footer={
53
+ <div style={{ display: "flex", "flex-direction": "column", gap: "4px" }}>
54
+ <Show when={problems().length > 0}>
55
+ <span style={{ color: "var(--color-danger, #d14343)" }}>{problems().join(" · ")}</span>
56
+ </Show>
57
+ <props.ui.Button
58
+ size="small"
59
+ onClick={() => setTiers([...tiers(), { name: `tier-${tiers().length + 1}`, model: models()[0]?.model ?? "", variant: null }])}
60
+ >
61
+ {props.t("tiers.add")}
62
+ </props.ui.Button>
63
+ </div>
64
+ }
65
+ >
66
+ <For each={tiers()}>
67
+ {(tier, index) => (
68
+ <props.ui.SettingsRow
69
+ id={`tier-${index()}`}
70
+ title={
71
+ <span>
72
+ {index() + 1}. {tier.name || "?"}
73
+ </span>
74
+ }
75
+ description={`${tier.model}${tier.variant ? `#${tier.variant}` : ""}`}
76
+ >
77
+ <div style={{ display: "flex", gap: "6px", "align-items": "center", "flex-wrap": "wrap" }}>
78
+ <props.ui.TextInput
79
+ label={props.t("tiers.name")}
80
+ value={tier.name}
81
+ onInput={(text) => update(index(), { name: text })}
82
+ />
83
+ <props.ui.Select
84
+ label={props.t("tiers.model")}
85
+ value={tier.model}
86
+ options={models().map((option) => ({ value: option.model, label: option.model }))}
87
+ onChange={(value) => update(index(), { model: value, variant: variantsOf(value)[0] ?? null })}
88
+ />
89
+ <props.ui.Select
90
+ label={props.t("tiers.variant")}
91
+ value={tier.variant ?? "none"}
92
+ options={
93
+ variantsOf(tier.model).length === 0
94
+ ? [{ value: "none", label: props.t("tiers.none") }]
95
+ : variantsOf(tier.model).map((variant) => ({ value: variant, label: variant }))
96
+ }
97
+ onChange={(value) => update(index(), { variant: value === "none" ? null : value })}
98
+ />
99
+ <props.ui.Button
100
+ size="small"
101
+ variant="ghost"
102
+ disabled={index() === 0}
103
+ onClick={() => setTiers(move(tiers(), index(), index() - 1))}
104
+ >
105
+ ↑
106
+ </props.ui.Button>
107
+ <props.ui.Button
108
+ size="small"
109
+ variant="ghost"
110
+ disabled={index() === tiers().length - 1}
111
+ onClick={() => setTiers(move(tiers(), index(), index() + 1))}
112
+ >
113
+ ↓
114
+ </props.ui.Button>
115
+ <props.ui.IconButton
116
+ icon="trash"
117
+ label={props.t("tiers.remove")}
118
+ onClick={() => setTiers(tiers().filter((_, i) => i !== index()))}
119
+ />
120
+ </div>
121
+ </props.ui.SettingsRow>
122
+ )}
123
+ </For>
124
+ </props.ui.SettingsSection>
125
+ );
126
+ }