tuxevil-rotator 4.0.0 → 4.2.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 +62 -0
- package/README.md +8 -1
- package/package.json +1 -1
- package/src/audio-transcription.ts +44 -4
- package/src/compat.ts +196 -222
- package/src/config-defaults.ts +5 -0
- package/src/config-overrides.ts +16 -0
- package/src/dashboard-live.ts +10 -8
- package/src/dashboard-types.ts +94 -1
- package/src/dashboard.ts +194 -1
- package/src/effort-levels.ts +43 -0
- package/src/effort-routing.ts +304 -0
- package/src/index.ts +2 -5
- package/src/model-catalog-revision.ts +20 -0
- package/src/model-catalog.ts +304 -0
- package/src/providers/adapter.ts +18 -0
- package/src/providers/google-antigravity/catalog.ts +190 -1
- package/src/providers/google-antigravity/forward.ts +2 -0
- package/src/providers/google-antigravity/index.ts +11 -0
- package/src/providers/google-antigravity/translators.ts +67 -14
- package/src/providers/ollama/index.ts +11 -1
- package/src/providers/ollama/quota-poll-store.ts +22 -1
- package/src/providers/ollama/quota.ts +95 -23
- package/src/providers/openai-codex/catalog.ts +9 -4
- package/src/providers/openai-codex/index.ts +18 -2
- package/src/providers/opencode-zen/index.ts +11 -2
- package/src/proxy.ts +143 -28
- package/src/rotator.ts +296 -21
- package/src/settings-repository.ts +36 -5
- package/src/types.ts +72 -26
- package/src/validators.ts +37 -1
- package/src/web/app.tsx +6 -1
- package/src/web/components/icons.tsx +29 -0
- package/src/web/components/overlays.tsx +13 -1
- package/src/web/components/ui.tsx +6 -1
- package/src/web/lib/accounts.ts +1 -0
- package/src/web/lib/attention.ts +12 -0
- package/src/web/lib/effort.ts +407 -0
- package/src/web/lib/models.ts +284 -0
- package/src/web/lib/pools.ts +53 -9
- package/src/web/lib/status.ts +13 -3
- package/src/web/nav.ts +1 -0
- package/src/web/pages/AccountDetail.tsx +16 -2
- package/src/web/pages/EffortRouting.tsx +1130 -0
- package/src/web/pages/Models.tsx +888 -0
- package/src/web/pages/Overview.tsx +31 -15
- package/src/web/pages/Usage.tsx +3 -3
- package/src/web/state/api.ts +31 -0
- package/src/web/state/model-catalog.ts +103 -0
- package/src/web/state/router.ts +150 -19
- package/src/web/state/store.ts +6 -6
- package/src/web/styles/app.css +854 -0
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
// Effort routing on the Models page: the editor's draft and its checks, how a
|
|
2
|
+
// route reads in the panel, and where each kind of request ends up. Pure, so
|
|
3
|
+
// tests can drive it without a browser.
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
CatalogEffortRoute,
|
|
7
|
+
CatalogEffortSuggestion,
|
|
8
|
+
CatalogModelEntry,
|
|
9
|
+
ModelCatalogResponse,
|
|
10
|
+
} from "../../dashboard-types.js";
|
|
11
|
+
import {
|
|
12
|
+
CLAUDE_THINKING_BUDGET_EFFORTS,
|
|
13
|
+
CLAUDE_THINKING_DISABLED_EFFORT,
|
|
14
|
+
EFFORT_LEVELS,
|
|
15
|
+
orderEffortLevels,
|
|
16
|
+
reservedEffortAlias,
|
|
17
|
+
} from "../../effort-levels.js";
|
|
18
|
+
import type { Tone } from "./status.js";
|
|
19
|
+
|
|
20
|
+
export { EFFORT_LEVELS };
|
|
21
|
+
|
|
22
|
+
const ANTIGRAVITY = "google-antigravity";
|
|
23
|
+
const fold = (value: string) => value.trim().toLowerCase();
|
|
24
|
+
|
|
25
|
+
/** "a", "a and b", "a, b and c". */
|
|
26
|
+
export function joinWords(items: string[]): string {
|
|
27
|
+
if (items.length <= 2) return items.join(" and ");
|
|
28
|
+
return `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ── Panel ───────────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
export function routeTargetsByLevel(route: Pick<CatalogEffortRoute, "targets">): Record<string, string> {
|
|
34
|
+
return Object.fromEntries(route.targets.map((target) => [target.effort, target.model]));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Pill, tone and one-line explanation for a route's state. */
|
|
38
|
+
export function effortRouteStatus(
|
|
39
|
+
route: CatalogEffortRoute,
|
|
40
|
+
providerNames: Record<string, string> = {},
|
|
41
|
+
): { label: string; tone: Tone; detail: string } {
|
|
42
|
+
switch (route.status) {
|
|
43
|
+
case "provider-off":
|
|
44
|
+
return { label: "Off", tone: "muted", detail: "Google Antigravity is off" };
|
|
45
|
+
case "off":
|
|
46
|
+
return { label: "Off", tone: "muted", detail: "Requests get a 404" };
|
|
47
|
+
case "hidden": {
|
|
48
|
+
if (route.shadowedBy) {
|
|
49
|
+
return { label: "Hidden", tone: "bad", detail: `${providerNames[route.shadowedBy] ?? route.shadowedBy} serves this ID instead` };
|
|
50
|
+
}
|
|
51
|
+
const fallback = route.targets.find((target) => target.effort === route.defaultEffort);
|
|
52
|
+
return {
|
|
53
|
+
label: "Hidden",
|
|
54
|
+
tone: "bad",
|
|
55
|
+
detail: fallback && !fallback.known ? "Default model isn't listed" : "Default model is off",
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
case "partial": {
|
|
59
|
+
const off = route.targets.filter((target) => !target.served).map((target) => target.effort);
|
|
60
|
+
return {
|
|
61
|
+
label: off.length === 1 ? `${off[0]} is off` : `${off.length} levels off`,
|
|
62
|
+
tone: "warn",
|
|
63
|
+
detail: `${joinWords(off)} requests get a 404`,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
default:
|
|
67
|
+
return { label: "Live", tone: "ok", detail: "Listed in /v1/models" };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Requests in the last hour, counted under the variants that served them. */
|
|
72
|
+
export function effortRouteRequests(route: Pick<CatalogEffortRoute, "targets">, requestsByModel: Record<string, number>): number {
|
|
73
|
+
const counts = new Map<string, number>();
|
|
74
|
+
for (const [model, count] of Object.entries(requestsByModel)) counts.set(fold(model), (counts.get(fold(model)) ?? 0) + count);
|
|
75
|
+
const models = new Set(route.targets.map((target) => fold(target.model)));
|
|
76
|
+
return [...models].reduce((sum, model) => sum + (counts.get(model) ?? 0), 0);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ── Editor draft ────────────────────────────────────────────────────────
|
|
80
|
+
|
|
81
|
+
export interface EffortDraftRow {
|
|
82
|
+
/** Stable key: "level:<name>" for standard levels, "custom:<n>" for added ones. */
|
|
83
|
+
id: string;
|
|
84
|
+
level: string;
|
|
85
|
+
/** Model id, or "" while the level is not used. */
|
|
86
|
+
model: string;
|
|
87
|
+
custom: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface EffortDraft {
|
|
91
|
+
/** The saved alias being edited, or null while creating one. */
|
|
92
|
+
original: string | null;
|
|
93
|
+
alias: string;
|
|
94
|
+
/** Every standard level, then custom levels in the order added. */
|
|
95
|
+
rows: EffortDraftRow[];
|
|
96
|
+
/** Row whose level requests without a usable effort get. */
|
|
97
|
+
defaultRowId: string | null;
|
|
98
|
+
nextCustomId: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const isMapped = (row: EffortDraftRow) => Boolean(row.model) && Boolean(row.level.trim());
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Prefer medium, then the mapped level closest to `nearIndex` (medium's place
|
|
105
|
+
* by default), taking the higher one on a tie.
|
|
106
|
+
*/
|
|
107
|
+
export function pickDefaultRow(rows: EffortDraftRow[], nearIndex = EFFORT_LEVELS.indexOf("medium")): EffortDraftRow | null {
|
|
108
|
+
const mapped = rows.filter(isMapped);
|
|
109
|
+
if (mapped.length === 0) return null;
|
|
110
|
+
const medium = mapped.find((row) => fold(row.level) === "medium");
|
|
111
|
+
if (medium) return medium;
|
|
112
|
+
return mapped.reduce((best, row) => {
|
|
113
|
+
const distance = Math.abs(rows.indexOf(row) - nearIndex);
|
|
114
|
+
const bestDistance = Math.abs(rows.indexOf(best) - nearIndex);
|
|
115
|
+
return distance < bestDistance || (distance === bestDistance && rows.indexOf(row) > rows.indexOf(best)) ? row : best;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A draft for a saved route, a suggested family, or (with no source) a blank route. */
|
|
120
|
+
export function createEffortDraft(
|
|
121
|
+
source: { alias: string; defaultEffort?: string; targets: Array<{ effort: string; model: string }> } | null,
|
|
122
|
+
original: string | null = null,
|
|
123
|
+
): EffortDraft {
|
|
124
|
+
const rows: EffortDraftRow[] = EFFORT_LEVELS.map((level) => ({ id: `level:${level}`, level, model: "", custom: false }));
|
|
125
|
+
let nextCustomId = 1;
|
|
126
|
+
for (const target of source?.targets ?? []) {
|
|
127
|
+
const level = fold(target.effort);
|
|
128
|
+
let row = rows.find((entry) => !entry.custom && entry.level === level);
|
|
129
|
+
if (!row) {
|
|
130
|
+
row = { id: `custom:${nextCustomId++}`, level, model: "", custom: true };
|
|
131
|
+
rows.push(row);
|
|
132
|
+
}
|
|
133
|
+
row.model = target.model;
|
|
134
|
+
}
|
|
135
|
+
const preferred = rows.find((row) => isMapped(row) && row.level === fold(source?.defaultEffort ?? ""));
|
|
136
|
+
return {
|
|
137
|
+
original,
|
|
138
|
+
alias: source?.alias ?? "",
|
|
139
|
+
rows,
|
|
140
|
+
defaultRowId: (preferred ?? pickDefaultRow(rows))?.id ?? null,
|
|
141
|
+
nextCustomId,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function draftTargets(draft: EffortDraft): Record<string, string> {
|
|
146
|
+
const mapped = draft.rows.filter(isMapped);
|
|
147
|
+
const byLevel = new Map(mapped.map((row) => [fold(row.level), row.model]));
|
|
148
|
+
return Object.fromEntries(orderEffortLevels(byLevel.keys()).map((level) => [level, byLevel.get(level)!]));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function draftDefaultRow(draft: EffortDraft): EffortDraftRow | null {
|
|
152
|
+
const row = draft.rows.find((entry) => entry.id === draft.defaultRowId);
|
|
153
|
+
return row && isMapped(row) ? row : null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Request body for POST/PUT /api/catalog/effort-routes. */
|
|
157
|
+
export function draftBody(draft: EffortDraft): { alias: string; defaultEffort: string; targets: Record<string, string> } {
|
|
158
|
+
return {
|
|
159
|
+
alias: draft.alias.trim(),
|
|
160
|
+
defaultEffort: fold(draftDefaultRow(draft)?.level ?? ""),
|
|
161
|
+
targets: draftTargets(draft),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Unfinished custom levels count too, so Save stays available to explain them. */
|
|
166
|
+
export function isDraftDirty(draft: EffortDraft, saved: EffortDraft | null): boolean {
|
|
167
|
+
if (!saved) return true;
|
|
168
|
+
const snapshot = (entry: EffortDraft) =>
|
|
169
|
+
JSON.stringify([draftBody(entry), entry.rows.filter((row) => row.custom).map((row) => [row.level, row.model])]);
|
|
170
|
+
return snapshot(draft) !== snapshot(saved);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export interface DraftChange {
|
|
174
|
+
draft: EffortDraft;
|
|
175
|
+
/** Level the default moved to, to announce it. */
|
|
176
|
+
movedDefaultTo: string | null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Keeps the default on a mapped level after rows change. */
|
|
180
|
+
function settleDefault(draft: EffortDraft, nearIndex?: number): DraftChange {
|
|
181
|
+
if (draftDefaultRow(draft)) return { draft, movedDefaultTo: null };
|
|
182
|
+
const hadDefault = draft.defaultRowId !== null;
|
|
183
|
+
const next = pickDefaultRow(draft.rows, nearIndex);
|
|
184
|
+
return {
|
|
185
|
+
draft: { ...draft, defaultRowId: next?.id ?? null },
|
|
186
|
+
movedDefaultTo: hadDefault && next ? fold(next.level) : null,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function updateRow(draft: EffortDraft, rowId: string, patch: Partial<EffortDraftRow>): EffortDraft {
|
|
191
|
+
return { ...draft, rows: draft.rows.map((row) => (row.id === rowId ? { ...row, ...patch } : row)) };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function setDraftAlias(draft: EffortDraft, alias: string): EffortDraft {
|
|
195
|
+
return { ...draft, alias };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export function setRowModel(draft: EffortDraft, rowId: string, model: string): DraftChange {
|
|
199
|
+
const index = draft.rows.findIndex((row) => row.id === rowId);
|
|
200
|
+
return settleDefault(updateRow(draft, rowId, { model }), index);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export function setRowLevel(draft: EffortDraft, rowId: string, level: string): DraftChange {
|
|
204
|
+
const index = draft.rows.findIndex((row) => row.id === rowId);
|
|
205
|
+
return settleDefault(updateRow(draft, rowId, { level }), index);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function setDefaultRow(draft: EffortDraft, rowId: string): EffortDraft {
|
|
209
|
+
return { ...draft, defaultRowId: rowId };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function addCustomRow(draft: EffortDraft): { draft: EffortDraft; rowId: string } {
|
|
213
|
+
const rowId = `custom:${draft.nextCustomId}`;
|
|
214
|
+
return {
|
|
215
|
+
rowId,
|
|
216
|
+
draft: {
|
|
217
|
+
...draft,
|
|
218
|
+
nextCustomId: draft.nextCustomId + 1,
|
|
219
|
+
rows: [...draft.rows, { id: rowId, level: "", model: "", custom: true }],
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export function removeRow(draft: EffortDraft, rowId: string): DraftChange {
|
|
225
|
+
const index = draft.rows.findIndex((row) => row.id === rowId);
|
|
226
|
+
return settleDefault({ ...draft, rows: draft.rows.filter((row) => row.id !== rowId) }, index);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** The suggested family whose base ID the alias names, while some of its variants are unused. */
|
|
230
|
+
export function familyForAlias(draft: EffortDraft, suggestions: CatalogEffortSuggestion[]): CatalogEffortSuggestion | null {
|
|
231
|
+
const alias = fold(draft.alias);
|
|
232
|
+
if (!alias) return null;
|
|
233
|
+
const family = suggestions.find((entry) => fold(entry.alias) === alias);
|
|
234
|
+
if (!family) return null;
|
|
235
|
+
const current = draftTargets(draft);
|
|
236
|
+
return family.targets.every((target) => current[target.effort] === target.model) ? null : family;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** Puts each of the family's variants on its level. */
|
|
240
|
+
export function fillFamily(draft: EffortDraft, family: CatalogEffortSuggestion): EffortDraft {
|
|
241
|
+
let next = draft;
|
|
242
|
+
for (const target of family.targets) {
|
|
243
|
+
const row = next.rows.find((entry) => !entry.custom && entry.level === target.effort)
|
|
244
|
+
?? next.rows.find((entry) => fold(entry.level) === target.effort);
|
|
245
|
+
if (row) next = updateRow(next, row.id, { model: target.model });
|
|
246
|
+
else {
|
|
247
|
+
const added = addCustomRow(next);
|
|
248
|
+
next = updateRow(added.draft, added.rowId, { level: target.effort, model: target.model });
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return settleDefault(next).draft;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Antigravity models a level can use: the alias's own variants first. */
|
|
255
|
+
export function effortModelOptions(
|
|
256
|
+
catalog: Pick<ModelCatalogResponse, "models">,
|
|
257
|
+
alias: string,
|
|
258
|
+
): { variants: CatalogModelEntry[]; others: CatalogModelEntry[] } {
|
|
259
|
+
const base = fold(alias);
|
|
260
|
+
const models = catalog.models.filter((entry) => entry.providerId === ANTIGRAVITY);
|
|
261
|
+
const variants = base ? models.filter((entry) => fold(entry.id).startsWith(`${base}-`)) : [];
|
|
262
|
+
return { variants, others: models.filter((entry) => !variants.includes(entry)) };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// ── Checks ──────────────────────────────────────────────────────────────
|
|
266
|
+
|
|
267
|
+
export interface DraftIssue {
|
|
268
|
+
message: string;
|
|
269
|
+
/** Shown while typing; otherwise only after leaving the field or trying to save. */
|
|
270
|
+
live: boolean;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export interface DraftErrors {
|
|
274
|
+
alias: DraftIssue | null;
|
|
275
|
+
rows: Record<string, DraftIssue>;
|
|
276
|
+
form: string | null;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function validateEffortDraft(
|
|
280
|
+
draft: EffortDraft,
|
|
281
|
+
catalog: Pick<ModelCatalogResponse, "models" | "effortRoutes">,
|
|
282
|
+
providerNames: Record<string, string> = {},
|
|
283
|
+
): DraftErrors {
|
|
284
|
+
const errors: DraftErrors = { alias: null, rows: {}, form: null };
|
|
285
|
+
const alias = draft.alias.trim();
|
|
286
|
+
const keepsName = draft.original !== null && fold(draft.original) === fold(alias);
|
|
287
|
+
const reserved = reservedEffortAlias(alias);
|
|
288
|
+
if (!alias) errors.alias = { message: "Enter the model ID clients will call.", live: false };
|
|
289
|
+
else if (/\s/.test(alias)) errors.alias = { message: "Model IDs can't contain spaces.", live: true };
|
|
290
|
+
else if (reserved) errors.alias = { message: reserved, live: true };
|
|
291
|
+
else if (catalog.effortRoutes.some((route) => fold(route.alias) === fold(alias) && route.alias !== draft.original)) {
|
|
292
|
+
errors.alias = { message: `Another effort model already uses ${alias}.`, live: true };
|
|
293
|
+
} else if (!keepsName) {
|
|
294
|
+
const taken = catalog.models.find((entry) => fold(entry.id) === fold(alias));
|
|
295
|
+
if (taken) {
|
|
296
|
+
errors.alias = {
|
|
297
|
+
message: `${taken.id} is already a model from ${providerNames[taken.providerId] ?? taken.providerId}. Pick an ID that isn't in use.`,
|
|
298
|
+
live: true,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const seen = new Set<string>();
|
|
304
|
+
for (const row of draft.rows) {
|
|
305
|
+
const level = fold(row.level);
|
|
306
|
+
if (row.custom) {
|
|
307
|
+
if (!level) {
|
|
308
|
+
errors.rows[row.id] = { message: "Name this level, or remove it.", live: false };
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (/\s/.test(level)) {
|
|
312
|
+
errors.rows[row.id] = { message: "Level names can't contain spaces.", live: true };
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
if (seen.has(level)) {
|
|
316
|
+
errors.rows[row.id] = { message: `There's already a ${level} level.`, live: true };
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
if (!row.model) errors.rows[row.id] = { message: "Choose a model for this level, or remove it.", live: false };
|
|
320
|
+
}
|
|
321
|
+
seen.add(level);
|
|
322
|
+
}
|
|
323
|
+
if (!draft.rows.some(isMapped)) errors.form = "Choose a model for at least one effort level.";
|
|
324
|
+
return errors;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Turns a refused save's field errors into draft errors, so they show beside their fields. */
|
|
328
|
+
export function draftErrorsFromServer(draft: EffortDraft, fieldErrors: Record<string, string>): DraftErrors {
|
|
329
|
+
const errors: DraftErrors = { alias: null, rows: {}, form: null };
|
|
330
|
+
const form: string[] = [];
|
|
331
|
+
for (const [field, message] of Object.entries(fieldErrors)) {
|
|
332
|
+
if (field === "alias") {
|
|
333
|
+
errors.alias = { message, live: true };
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
const level = field.startsWith("targets.") ? field.slice("targets.".length) : null;
|
|
337
|
+
const row = level === null ? undefined : draft.rows.find((entry) => fold(entry.level) === level);
|
|
338
|
+
if (row) errors.rows[row.id] = { message, live: true };
|
|
339
|
+
else form.push(message);
|
|
340
|
+
}
|
|
341
|
+
errors.form = form.length > 0 ? form.join(" ") : null;
|
|
342
|
+
return errors;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function hasDraftErrors(errors: DraftErrors): boolean {
|
|
346
|
+
return Boolean(errors.alias || errors.form || Object.keys(errors.rows).length > 0);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ── Preview ─────────────────────────────────────────────────────────────
|
|
350
|
+
|
|
351
|
+
export interface ResolvedLevel {
|
|
352
|
+
level: string;
|
|
353
|
+
model: string;
|
|
354
|
+
/** False when requests for it would get a 404. */
|
|
355
|
+
served: boolean;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export interface EffortPreview {
|
|
359
|
+
levels: ResolvedLevel[];
|
|
360
|
+
/** Where requests without a mapped effort go, or null with no default yet. */
|
|
361
|
+
fallback: ResolvedLevel | null;
|
|
362
|
+
/** A standard level with no model, to show what "unmapped" means. */
|
|
363
|
+
unmappedExample: string | null;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export function previewEffortDraft(draft: EffortDraft, catalog: Pick<ModelCatalogResponse, "models">): EffortPreview {
|
|
367
|
+
const served = (model: string) =>
|
|
368
|
+
catalog.models.some((entry) => entry.providerId === ANTIGRAVITY && fold(entry.id) === fold(model) && entry.offReason === null);
|
|
369
|
+
const levels = Object.entries(draftTargets(draft)).map(([level, model]) => ({ level, model, served: served(model) }));
|
|
370
|
+
const fallbackRow = draftDefaultRow(draft);
|
|
371
|
+
return {
|
|
372
|
+
levels,
|
|
373
|
+
fallback: fallbackRow ? { level: fold(fallbackRow.level), model: fallbackRow.model, served: served(fallbackRow.model) } : null,
|
|
374
|
+
unmappedExample: EFFORT_LEVELS.find((level) => !levels.some((entry) => entry.level === level)) ?? null,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export type RequestFormat = "chat" | "responses" | "messages";
|
|
379
|
+
|
|
380
|
+
/** An example request that asks for `level`. */
|
|
381
|
+
export function exampleRequest(format: RequestFormat, alias: string, level: string): string {
|
|
382
|
+
const model = alias.trim() || "your-model-id";
|
|
383
|
+
if (format === "responses") {
|
|
384
|
+
return `POST /v1/responses\n{\n "model": "${model}",\n "reasoning": { "effort": "${level}" },\n "input": "Hi"\n}`;
|
|
385
|
+
}
|
|
386
|
+
if (format === "messages") {
|
|
387
|
+
return `POST /v1/messages\n{\n "model": "${model}",\n "max_tokens": 4096,\n "output_config": { "effort": "${level}" },\n "messages": [{ "role": "user", "content": "Hi" }]\n}`;
|
|
388
|
+
}
|
|
389
|
+
return `POST /v1/chat/completions\n{\n "model": "${model}",\n "reasoning_effort": "${level}",\n "messages": [{ "role": "user", "content": "Hi" }]\n}`;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** How Anthropic Messages requests without `output_config.effort` pick a level. */
|
|
393
|
+
export function claudeThinkingRules(): Array<{ when: string; level: string }> {
|
|
394
|
+
const rules: Array<{ when: string; level: string }> = [];
|
|
395
|
+
let lower = 0;
|
|
396
|
+
for (const bucket of CLAUDE_THINKING_BUDGET_EFFORTS) {
|
|
397
|
+
const when = Number.isFinite(bucket.below)
|
|
398
|
+
? lower === 0
|
|
399
|
+
? `budget_tokens under ${bucket.below.toLocaleString("en-US")}`
|
|
400
|
+
: `budget_tokens ${lower.toLocaleString("en-US")} to ${(bucket.below - 1).toLocaleString("en-US")}`
|
|
401
|
+
: `budget_tokens ${lower.toLocaleString("en-US")} or more`;
|
|
402
|
+
rules.push({ when, level: bucket.effort });
|
|
403
|
+
lower = bucket.below;
|
|
404
|
+
}
|
|
405
|
+
rules.push({ when: "thinking turned off", level: CLAUDE_THINKING_DISABLED_EFFORT });
|
|
406
|
+
return rules;
|
|
407
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
// Models page: one row per model id across providers, and a summary per
|
|
2
|
+
// provider built from the catalog API and the live overview.
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
CatalogModelEntry,
|
|
6
|
+
CatalogProvider,
|
|
7
|
+
DashboardAccount,
|
|
8
|
+
DashboardOverview,
|
|
9
|
+
ModelCatalogResponse,
|
|
10
|
+
} from "../../dashboard-types.js";
|
|
11
|
+
import type { ModelQuota } from "../../types.js";
|
|
12
|
+
import { buildPools } from "./pools.js";
|
|
13
|
+
import { canServe, providerIds, quotaProvider } from "./status.js";
|
|
14
|
+
|
|
15
|
+
/** Two-letter marks and hues for provider monograms. */
|
|
16
|
+
export const PROVIDER_MARKS: Record<string, { mark: string; hue: number }> = {
|
|
17
|
+
"google-antigravity": { mark: "AG", hue: 215 },
|
|
18
|
+
"openai-codex": { mark: "CX", hue: 165 },
|
|
19
|
+
ollama: { mark: "OL", hue: 28 },
|
|
20
|
+
"opencode-zen": { mark: "ZN", hue: 280 },
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function providerMark(id: string): { mark: string; hue: number } {
|
|
24
|
+
return PROVIDER_MARKS[id] ?? { mark: id.slice(0, 2).toUpperCase(), hue: 250 };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Config key for one provider's copy of a model. */
|
|
28
|
+
export function offeringKey(entry: Pick<CatalogModelEntry, "providerId" | "id">): string {
|
|
29
|
+
return `${entry.providerId}/${entry.id}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function isServed(entry: CatalogModelEntry): boolean {
|
|
33
|
+
return entry.offReason === null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ModelRow {
|
|
37
|
+
/** Lower-cased id: one row per id across providers. */
|
|
38
|
+
key: string;
|
|
39
|
+
id: string;
|
|
40
|
+
/** Every provider that lists the id, in provider order. */
|
|
41
|
+
offerings: CatalogModelEntry[];
|
|
42
|
+
/** Served by at least one provider. */
|
|
43
|
+
on: boolean;
|
|
44
|
+
/** Listed by more than one provider. */
|
|
45
|
+
shared: boolean;
|
|
46
|
+
/** Waiting for review on at least one provider. */
|
|
47
|
+
waiting: boolean;
|
|
48
|
+
/** Requests in the last hour. */
|
|
49
|
+
requests: number;
|
|
50
|
+
/** The effort-routed model ID that sends one of its levels to this model. */
|
|
51
|
+
effortRoute: { alias: string; effort: string } | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function buildModelRows(
|
|
55
|
+
catalog: ModelCatalogResponse,
|
|
56
|
+
requestsByModel: Record<string, number>,
|
|
57
|
+
): ModelRow[] {
|
|
58
|
+
const requests = new Map<string, number>();
|
|
59
|
+
for (const [model, count] of Object.entries(requestsByModel)) {
|
|
60
|
+
const key = model.toLowerCase();
|
|
61
|
+
requests.set(key, (requests.get(key) ?? 0) + count);
|
|
62
|
+
}
|
|
63
|
+
const rows = new Map<string, ModelRow>();
|
|
64
|
+
for (const entry of catalog.models) {
|
|
65
|
+
const key = entry.id.toLowerCase();
|
|
66
|
+
const row = rows.get(key) ?? {
|
|
67
|
+
key,
|
|
68
|
+
id: entry.id,
|
|
69
|
+
offerings: [],
|
|
70
|
+
on: false,
|
|
71
|
+
shared: false,
|
|
72
|
+
waiting: false,
|
|
73
|
+
requests: requests.get(key) ?? 0,
|
|
74
|
+
effortRoute: null,
|
|
75
|
+
};
|
|
76
|
+
row.offerings.push(entry);
|
|
77
|
+
row.effortRoute ??= entry.effortRoute ?? null;
|
|
78
|
+
row.on ||= isServed(entry);
|
|
79
|
+
row.waiting ||= entry.modelSwitch === "review";
|
|
80
|
+
row.shared = row.offerings.length > 1;
|
|
81
|
+
rows.set(key, row);
|
|
82
|
+
}
|
|
83
|
+
return [...rows.values()];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type ModelFilter = "all" | "on" | "off" | "shared" | "waiting" | "effort";
|
|
87
|
+
|
|
88
|
+
export const MODEL_FILTERS: Array<{ id: ModelFilter; label: string; match: (row: ModelRow) => boolean }> = [
|
|
89
|
+
{ id: "all", label: "All", match: () => true },
|
|
90
|
+
{ id: "on", label: "On", match: (row) => row.on },
|
|
91
|
+
{ id: "off", label: "Off", match: (row) => !row.on },
|
|
92
|
+
{ id: "shared", label: "Shared", match: (row) => row.shared },
|
|
93
|
+
{ id: "waiting", label: "Waiting for review", match: (row) => row.waiting },
|
|
94
|
+
{ id: "effort", label: "Effort routing", match: (row) => row.effortRoute !== null },
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
export function isModelFilter(value: unknown): value is ModelFilter {
|
|
98
|
+
return MODEL_FILTERS.some((filter) => filter.id === value);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export type ModelSort = "requests" | "name";
|
|
102
|
+
|
|
103
|
+
export function filterModelRows(
|
|
104
|
+
rows: ModelRow[],
|
|
105
|
+
options: { filter: ModelFilter; provider: string; query: string },
|
|
106
|
+
): ModelRow[] {
|
|
107
|
+
const match = MODEL_FILTERS.find((filter) => filter.id === options.filter)?.match ?? (() => true);
|
|
108
|
+
const query = options.query.trim().toLowerCase();
|
|
109
|
+
return rows.filter(
|
|
110
|
+
(row) =>
|
|
111
|
+
match(row) &&
|
|
112
|
+
(!options.provider || row.offerings.some((entry) => entry.providerId === options.provider)) &&
|
|
113
|
+
(!query || row.offerings.some((entry) => entry.id.toLowerCase().includes(query))),
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Busiest first, then by name; models that are off sink below the rest. */
|
|
118
|
+
export function sortModelRows(rows: ModelRow[], sort: ModelSort): ModelRow[] {
|
|
119
|
+
return [...rows].sort((a, b) => {
|
|
120
|
+
if (a.on !== b.on) return a.on ? -1 : 1;
|
|
121
|
+
if (sort === "requests" && a.requests !== b.requests) return b.requests - a.requests;
|
|
122
|
+
return a.id.localeCompare(b.id);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export type ProviderState = "off" | "not-connected" | "needs-sign-in" | "unavailable" | "healthy";
|
|
127
|
+
|
|
128
|
+
export interface ProviderQuota {
|
|
129
|
+
key: string;
|
|
130
|
+
name: string;
|
|
131
|
+
/** Tier-weighted remaining quota across in-service accounts, 0–100. */
|
|
132
|
+
percent: number;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface ProviderSummary {
|
|
136
|
+
provider: CatalogProvider;
|
|
137
|
+
/** 0-based position in the routing order. */
|
|
138
|
+
rank: number;
|
|
139
|
+
/** Accounts holding a credential for this provider. */
|
|
140
|
+
accounts: DashboardAccount[];
|
|
141
|
+
/** Accounts that can serve this provider now. */
|
|
142
|
+
canServe: number;
|
|
143
|
+
/** Accounts whose credential for this provider was rejected. */
|
|
144
|
+
rejected: DashboardAccount[];
|
|
145
|
+
models: number;
|
|
146
|
+
modelsOn: number;
|
|
147
|
+
quotas: ProviderQuota[];
|
|
148
|
+
state: ProviderState;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function hasProvider(account: DashboardAccount, providerId: string): boolean {
|
|
152
|
+
return providerIds(account).includes(providerId);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function isTurnedOffForAccount(account: DashboardAccount, providerId: string): boolean {
|
|
156
|
+
return account.disabledProviders?.includes(providerId) ?? false;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Account can take this provider's traffic now. */
|
|
160
|
+
export function canServeProvider(
|
|
161
|
+
account: DashboardAccount,
|
|
162
|
+
providerId: string,
|
|
163
|
+
now: number,
|
|
164
|
+
routing?: {
|
|
165
|
+
quotaPools: readonly string[];
|
|
166
|
+
diagnostics: DashboardOverview["routingDiagnostics"];
|
|
167
|
+
},
|
|
168
|
+
): boolean {
|
|
169
|
+
if (
|
|
170
|
+
!canServe(account) ||
|
|
171
|
+
account.invalidProviders?.[providerId] ||
|
|
172
|
+
(account.providerCooldowns?.[providerId] ?? 0) > now ||
|
|
173
|
+
isTurnedOffForAccount(account, providerId)
|
|
174
|
+
) return false;
|
|
175
|
+
|
|
176
|
+
const quotas = providerQuotaRows(account, providerId);
|
|
177
|
+
const pools = new Set(routing?.quotaPools.length ? routing.quotaPools : quotas.map((quota) => quota.modelKey));
|
|
178
|
+
if (pools.size === 0) return true;
|
|
179
|
+
return [...pools].some((key) => {
|
|
180
|
+
const diagnostics = routing?.diagnostics[key]?.accounts ?? [];
|
|
181
|
+
const identified = account.id ? diagnostics.filter((entry) => entry.id === account.id) : [];
|
|
182
|
+
const byEmail = diagnostics.filter((entry) => entry.email === account.email);
|
|
183
|
+
// A legacy producer may omit identity. Email is usable only if unique
|
|
184
|
+
// and must never replace a different identity supplied by the server.
|
|
185
|
+
const diagnostic = identified.length === 1
|
|
186
|
+
? identified[0]
|
|
187
|
+
: identified.length === 0 && byEmail.length === 1 && (!account.id || !byEmail[0].id)
|
|
188
|
+
? byEmail[0]
|
|
189
|
+
: null;
|
|
190
|
+
if (diagnostic) return diagnostic.rejectedReason === null;
|
|
191
|
+
if ((account.cooldownsByModel[key] ?? 0) > now) return false;
|
|
192
|
+
// Missing and negative quota mean unknown, which routing permits.
|
|
193
|
+
return quotas.find((quota) => quota.modelKey === key)?.percentRemaining !== 0;
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export function providerQuotaRows(account: DashboardAccount, providerId: string): ModelQuota[] {
|
|
198
|
+
return (account.quota ?? []).filter((quota) => quotaProvider(quota) === providerId);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function summarizeProviders(
|
|
202
|
+
catalog: ModelCatalogResponse,
|
|
203
|
+
overview: DashboardOverview,
|
|
204
|
+
now: number,
|
|
205
|
+
): ProviderSummary[] {
|
|
206
|
+
const pools = buildPools(overview, now);
|
|
207
|
+
return catalog.providers.map((provider, rank) => {
|
|
208
|
+
const accounts = overview.accounts.filter((account) => hasProvider(account, provider.id));
|
|
209
|
+
const rejected = accounts.filter((account) => Boolean(account.invalidProviders?.[provider.id]));
|
|
210
|
+
const models = catalog.models.filter((entry) => entry.providerId === provider.id);
|
|
211
|
+
const routing = { quotaPools: models.map((entry) => entry.quotaPool), diagnostics: overview.routingDiagnostics };
|
|
212
|
+
const servable = accounts.filter((account) => canServeProvider(account, provider.id, now, routing)).length;
|
|
213
|
+
const state: ProviderState = !provider.enabled
|
|
214
|
+
? "off"
|
|
215
|
+
: accounts.length === 0
|
|
216
|
+
? "not-connected"
|
|
217
|
+
: rejected.length > 0
|
|
218
|
+
? "needs-sign-in"
|
|
219
|
+
: servable === 0
|
|
220
|
+
? "unavailable"
|
|
221
|
+
: "healthy";
|
|
222
|
+
return {
|
|
223
|
+
provider,
|
|
224
|
+
rank,
|
|
225
|
+
accounts,
|
|
226
|
+
canServe: servable,
|
|
227
|
+
rejected,
|
|
228
|
+
models: models.length,
|
|
229
|
+
modelsOn: models.filter(isServed).length,
|
|
230
|
+
quotas: pools
|
|
231
|
+
.filter((pool) => pool.provider === provider.id && pool.members.length > 0)
|
|
232
|
+
.map((pool) => ({ key: pool.key, name: pool.name, percent: pool.capacity })),
|
|
233
|
+
state,
|
|
234
|
+
};
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Move one provider up or down the routing order. */
|
|
239
|
+
export function moveProvider(order: string[], id: string, offset: number): string[] {
|
|
240
|
+
const from = order.indexOf(id);
|
|
241
|
+
const to = from + offset;
|
|
242
|
+
if (from < 0 || to < 0 || to >= order.length) return order;
|
|
243
|
+
const next = [...order];
|
|
244
|
+
next.splice(from, 1);
|
|
245
|
+
next.splice(to, 0, id);
|
|
246
|
+
return next;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** Drop `id` just before or after `targetId` (drag and drop). */
|
|
250
|
+
export function placeProvider(order: string[], id: string, targetId: string, after: boolean): string[] {
|
|
251
|
+
if (id === targetId) return order;
|
|
252
|
+
const next = order.filter((entry) => entry !== id);
|
|
253
|
+
const at = next.indexOf(targetId);
|
|
254
|
+
if (at < 0 || !order.includes(id)) return order;
|
|
255
|
+
next.splice(after ? at + 1 : at, 0, id);
|
|
256
|
+
return next;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Context windows as people say them: 128K, 1M, 1.05M. */
|
|
260
|
+
export function formatContextWindow(tokens: number | null): string {
|
|
261
|
+
if (tokens === null || !Number.isFinite(tokens) || tokens <= 0) return "—";
|
|
262
|
+
if (tokens >= 1_000_000) {
|
|
263
|
+
const millions = tokens % 1_048_576 === 0 ? tokens / 1_048_576 : tokens / 1_000_000;
|
|
264
|
+
return `${Number(millions.toFixed(2))}M`;
|
|
265
|
+
}
|
|
266
|
+
if (tokens % 1000 === 0) return `${tokens / 1000}K`;
|
|
267
|
+
if (tokens % 1024 === 0) return `${tokens / 1024}K`;
|
|
268
|
+
return `${Math.round(tokens / 1000)}K`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const POOL_NAMES: Record<string, string> = {
|
|
272
|
+
claude: "Claude",
|
|
273
|
+
gemini: "Gemini",
|
|
274
|
+
monthly: "Monthly",
|
|
275
|
+
session: "Session",
|
|
276
|
+
weekly: "Weekly",
|
|
277
|
+
"openai-codex": "Codex",
|
|
278
|
+
"openai-codex-spark": "Codex Spark",
|
|
279
|
+
"opencode-zen": "Zen",
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
export function quotaPoolLabel(key: string): string {
|
|
283
|
+
return POOL_NAMES[key] ?? key;
|
|
284
|
+
}
|