@phren/agent 0.3.31 → 0.3.32

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,467 @@
1
+ /**
2
+ * Provider model discovery. This module only performs bounded GET requests;
3
+ * it never starts a completion. The on-disk cache is deliberately separate
4
+ * from credentials and contains provider metadata only.
5
+ */
6
+ import * as fs from "node:fs";
7
+ import * as os from "node:os";
8
+ import * as path from "node:path";
9
+ import { createHash } from "node:crypto";
10
+ import { codexConfiguredModel } from "./providers/codex-auth.js";
11
+ import { configuredProviders, resolveConnector } from "./provider-connectors.js";
12
+ import { getBuiltinModels, getDiscoveredModels, normalizeReasoningEffort, registerDiscoveredModels, } from "./models.js";
13
+ export const MODEL_CACHE_TTL_MS = 15 * 60 * 1000;
14
+ const MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
15
+ const MAX_ROWS = 400;
16
+ const MAX_ANTHROPIC_PAGES = 4;
17
+ const runtimeStatuses = new Map();
18
+ function record(value) {
19
+ return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
20
+ }
21
+ function string(value) {
22
+ return typeof value === "string" && value.trim() ? value.trim() : undefined;
23
+ }
24
+ function number(value) {
25
+ const parsed = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
26
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
27
+ }
28
+ function rows(value) {
29
+ return Array.isArray(value) ? value.map(record).filter((row) => !!row) : [];
30
+ }
31
+ function modelId(row) {
32
+ return string(row.id) ?? string(row.slug) ?? string(row.model) ?? string(row.name);
33
+ }
34
+ function label(row, id) {
35
+ return string(row.display_name) ?? string(row.displayName) ?? string(row.name) ?? id;
36
+ }
37
+ function supportedEfforts(value) {
38
+ const values = Array.isArray(value) ? value : [];
39
+ const seen = new Set();
40
+ for (const item of values) {
41
+ const raw = typeof item === "string" ? item : string(record(item)?.effort) ?? string(record(item)?.id) ?? string(record(item)?.name);
42
+ const lower = raw?.toLowerCase();
43
+ // ultra is a CLI orchestration level, not an API effort. A provider may
44
+ // advertise max distinctly from xhigh.
45
+ const effort = lower === "max" ? "max" : normalizeReasoningEffort(raw);
46
+ if (effort && effort !== "none")
47
+ seen.add(effort);
48
+ }
49
+ return ["minimal", "low", "medium", "high", "xhigh", "max"].filter((level) => seen.has(level));
50
+ }
51
+ function catalogEffort(raw) {
52
+ const lower = raw?.toLowerCase();
53
+ if (lower === "max")
54
+ return "max";
55
+ return normalizeReasoningEffort(raw);
56
+ }
57
+ function anthropicEfforts(row) {
58
+ const capabilities = record(row.capabilities);
59
+ const effort = record(capabilities?.effort);
60
+ if (effort?.supported !== true)
61
+ return [];
62
+ return supportedEfforts(["minimal", "low", "medium", "high", "xhigh", "max"].map((level) => {
63
+ const detail = record(effort[level]);
64
+ return detail?.supported === true ? level : undefined;
65
+ }));
66
+ }
67
+ function advertisedEfforts(row, provider) {
68
+ // These fields are only used when the provider explicitly supplies levels.
69
+ // A boolean `reasoning: true` or a supported_parameters entry is not enough.
70
+ for (const value of [
71
+ row.supported_reasoning_levels,
72
+ row.supported_reasoning_efforts,
73
+ row.reasoning_efforts,
74
+ row.effort_levels,
75
+ record(row.reasoning)?.supported_efforts,
76
+ record(row.reasoning)?.effort_levels,
77
+ record(row.capabilities)?.supported_reasoning_levels,
78
+ record(row.capabilities)?.effort_levels,
79
+ record(record(row.capabilities)?.effort)?.levels,
80
+ ]) {
81
+ const parsed = supportedEfforts(value);
82
+ if (parsed.length > 0)
83
+ return parsed;
84
+ }
85
+ const anthropic = anthropicEfforts(row);
86
+ if (anthropic.length > 0)
87
+ return anthropic;
88
+ if (provider === "openrouter") {
89
+ const parameters = Array.isArray(row.supported_parameters) ? row.supported_parameters : [];
90
+ if (parameters.some((item) => item === "reasoning_effort"))
91
+ return ["low", "medium", "high"];
92
+ }
93
+ return [];
94
+ }
95
+ function defaultEffort(row, range) {
96
+ const raw = string(row.default_reasoning_level) ?? string(row.default_reasoning_effort) ?? string(row.default_effort)
97
+ ?? string(record(row.reasoning)?.default_effort) ?? string(record(row.capabilities)?.default_effort);
98
+ const effort = catalogEffort(raw);
99
+ return effort && (effort === "none" || range.includes(effort)) ? effort : null;
100
+ }
101
+ function pricing(row) {
102
+ const source = record(row.pricing) ?? row;
103
+ const input = number(source.prompt) ?? number(source.input);
104
+ const output = number(source.completion) ?? number(source.output);
105
+ if (input === undefined || output === undefined)
106
+ return undefined;
107
+ const cache = number(source.cache_read) ?? number(source.cacheRead) ?? number(source.input_cache_read);
108
+ // Provider catalogues report dollars per token. Do not guess units from
109
+ // the magnitude: an expensive per-token price is still per-token.
110
+ return { inputPer1M: input * 1_000_000, outputPer1M: output * 1_000_000, ...(cache !== undefined ? { cacheReadPer1M: cache * 1_000_000 } : {}) };
111
+ }
112
+ function vision(row) {
113
+ const architecture = record(row.architecture);
114
+ const modalities = architecture?.input_modalities ?? row.input_modalities ?? row.inputModalities;
115
+ if (!Array.isArray(modalities)) {
116
+ const image = record(record(row.capabilities)?.image_input);
117
+ return typeof image?.supported === "boolean" ? image.supported : undefined;
118
+ }
119
+ return modalities.some((item) => item === "image");
120
+ }
121
+ function entry(provider, row) {
122
+ const id = modelId(row);
123
+ if (!id || id.length > 200)
124
+ return undefined;
125
+ let reasoningRange = advertisedEfforts(row, provider);
126
+ const capabilities = record(row.capabilities);
127
+ const thinking = record(capabilities?.thinking) ?? record(row.thinking);
128
+ const adaptive = record(record(thinking?.types)?.adaptive);
129
+ const budget = record(record(thinking?.types)?.enabled)?.supported === true;
130
+ if (provider === "anthropic" && !reasoningRange.length && budget)
131
+ reasoningRange = ["low", "medium", "high"];
132
+ const reasoningMode = adaptive?.supported === true || string(thinking?.type) === "adaptive" || string(capabilities?.reasoning_mode) === "adaptive"
133
+ ? "adaptive" : budget ? "budget" : undefined;
134
+ const contextWindow = number(row.context_window) ?? number(row.context_length) ?? number(row.max_input_tokens);
135
+ const topProvider = record(row.top_provider);
136
+ const maxOutputTokens = number(topProvider?.max_completion_tokens) ?? number(row.max_output_tokens) ?? number(row.max_completion_tokens) ?? number(row.max_tokens);
137
+ const modelPricing = pricing(row);
138
+ const modelVision = vision(row);
139
+ return {
140
+ id,
141
+ provider,
142
+ label: label(row, id),
143
+ ...(contextWindow !== undefined ? { contextWindow } : {}),
144
+ ...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
145
+ reasoningDefault: defaultEffort(row, reasoningRange),
146
+ reasoningRange,
147
+ ...(reasoningMode ? { reasoningMode } : {}),
148
+ ...(modelPricing ? { pricing: modelPricing } : {}),
149
+ ...(modelVision !== undefined ? { vision: modelVision } : {}),
150
+ };
151
+ }
152
+ function unique(entries) {
153
+ const seen = new Set();
154
+ return entries.filter((item) => {
155
+ if (!item || seen.has(item.id))
156
+ return false;
157
+ seen.add(item.id);
158
+ return true;
159
+ }).slice(0, MAX_ROWS);
160
+ }
161
+ export function parseCodexModels(raw) {
162
+ const root = record(raw);
163
+ const list = Array.isArray(raw) ? raw : root?.models;
164
+ return unique(rows(list).map((row) => {
165
+ const levels = supportedEfforts(row.supported_reasoning_levels);
166
+ const id = string(row.slug) ?? string(row.id);
167
+ if (!id)
168
+ return undefined;
169
+ return {
170
+ id,
171
+ provider: "openai-codex",
172
+ label: string(row.display_name) ?? id,
173
+ ...(number(row.context_window) !== undefined ? { contextWindow: number(row.context_window) } : {}),
174
+ reasoningDefault: defaultEffort(row, levels),
175
+ reasoningRange: levels,
176
+ ...(Array.isArray(row.input_modalities) ? { vision: row.input_modalities.some((item) => item === "image") } : {}),
177
+ };
178
+ }));
179
+ }
180
+ export function parseOpenRouterModels(raw) {
181
+ const root = record(raw);
182
+ return unique(rows(root?.data).filter((row) => {
183
+ const architecture = record(row.architecture);
184
+ const modalities = architecture?.input_modalities;
185
+ const parameters = row.supported_parameters;
186
+ const hasText = Array.isArray(modalities) && modalities.some((item) => item === "text");
187
+ const hasTools = Array.isArray(parameters) && parameters.some((item) => item === "tools" || item === "tool_choice");
188
+ return hasText && hasTools;
189
+ }).map((row) => entry("openrouter", row)));
190
+ }
191
+ function readCodexModels() {
192
+ const codexHome = process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
193
+ try {
194
+ return parseCodexModels(JSON.parse(fs.readFileSync(path.join(codexHome, "models_cache.json"), "utf8")));
195
+ }
196
+ catch {
197
+ return [];
198
+ }
199
+ }
200
+ export function parseOpenAiModels(raw, codexModels = readCodexModels()) {
201
+ const root = record(raw);
202
+ const codexById = new Map([...getBuiltinModels("openai"), ...codexModels.filter((model) => model.provider === "openai-codex")].map((model) => [model.id, model]));
203
+ return unique(rows(root?.data).filter((row) => {
204
+ const id = modelId(row)?.toLowerCase() ?? "";
205
+ if (/embedding|embed|audio|transcri|whisper|dall-e|image|moderation|realtime/.test(id))
206
+ return false;
207
+ const architecture = record(row.architecture);
208
+ const modalities = architecture?.input_modalities ?? row.input_modalities;
209
+ const outputs = architecture?.output_modalities ?? row.output_modalities;
210
+ return (!Array.isArray(modalities) || modalities.some((item) => item === "text"))
211
+ && (!Array.isArray(outputs) || outputs.some((item) => item === "text"));
212
+ }).map((row) => {
213
+ const model = entry("openai", row);
214
+ const codex = model ? codexById.get(model.id) : undefined;
215
+ return model && codex ? {
216
+ ...model,
217
+ ...(codex.contextWindow !== undefined ? { contextWindow: codex.contextWindow } : {}),
218
+ reasoningDefault: codex.reasoningDefault,
219
+ reasoningRange: codex.reasoningRange,
220
+ ...(codex.vision !== undefined ? { vision: codex.vision } : {}),
221
+ } : model;
222
+ }));
223
+ }
224
+ export function parseAnthropicModels(raw) {
225
+ const root = record(raw);
226
+ return unique(rows(root?.data).map((row) => entry("anthropic", row)));
227
+ }
228
+ export function parseDeepSeekModels(raw) {
229
+ const root = record(raw);
230
+ return unique(rows(root?.data ?? raw).map((row) => {
231
+ const model = entry("deepseek", row);
232
+ if (!model || !/^deepseek-(?:v4|flash$|pro$)/i.test(model.id) || model.reasoningRange.length > 0)
233
+ return model;
234
+ // DeepSeek V4 documents low/high/max. Older reasoner models use the
235
+ // budget-mode contract and must not be presented as invented efforts.
236
+ return { ...model, reasoningRange: ["low", "high", "max"] };
237
+ }));
238
+ }
239
+ export function parseOllamaModels(raw) {
240
+ const root = record(raw);
241
+ return unique(rows(root?.models).map((row) => {
242
+ const id = string(row.name) ?? string(row.model);
243
+ if (!id)
244
+ return undefined;
245
+ const details = record(row.details);
246
+ return {
247
+ id,
248
+ provider: "ollama",
249
+ label: id,
250
+ ...(number(row.context_length) ? { contextWindow: number(row.context_length) } : {}),
251
+ reasoningDefault: null,
252
+ reasoningRange: [],
253
+ ...(details?.families && Array.isArray(details.families) && details.families.some((family) => /clip|vision/i.test(String(family))) ? { vision: true } : {}),
254
+ };
255
+ }));
256
+ }
257
+ function cachePath() {
258
+ return path.join(os.homedir(), ".phren-agent", "model-catalog.json");
259
+ }
260
+ function readCache() {
261
+ try {
262
+ const parsed = record(JSON.parse(fs.readFileSync(cachePath(), "utf8")));
263
+ if (parsed?.version !== 1)
264
+ return { version: 1, entries: {} };
265
+ const entries = {};
266
+ for (const [key, value] of Object.entries(record(parsed.entries) ?? {})) {
267
+ const candidate = record(value);
268
+ const provider = string(candidate?.provider);
269
+ const fetchedAt = string(candidate?.fetchedAt);
270
+ const models = candidate?.models;
271
+ if (!provider || !fetchedAt || !Number.isFinite(Date.parse(fetchedAt)) || !Array.isArray(models))
272
+ continue;
273
+ if (!["openai", "openai-codex", "openrouter", "anthropic", "deepseek", "ollama", "openai-compat"].includes(provider))
274
+ continue;
275
+ const validModels = models.filter((model) => {
276
+ const row = record(model);
277
+ return !!row && string(row.id) !== undefined && row.provider === provider
278
+ && typeof row.label === "string" && Array.isArray(row.reasoningRange)
279
+ && row.reasoningRange.every(level => typeof level === "string" && normalizeReasoningEffort(level) === level)
280
+ && (row.reasoningDefault === null || (typeof row.reasoningDefault === "string" && normalizeReasoningEffort(row.reasoningDefault) === row.reasoningDefault));
281
+ });
282
+ entries[key] = { provider, cacheKey: string(candidate?.cacheKey) ?? key, fetchedAt, models: validModels.slice(0, MAX_ROWS) };
283
+ }
284
+ return { version: 1, entries };
285
+ }
286
+ catch {
287
+ return { version: 1, entries: {} };
288
+ }
289
+ }
290
+ function writeCache(cache) {
291
+ try {
292
+ const file = cachePath();
293
+ fs.mkdirSync(path.dirname(file), { recursive: true });
294
+ const temp = `${file}.${process.pid}.tmp`;
295
+ fs.writeFileSync(temp, JSON.stringify(cache, null, 2) + "\n", { mode: 0o600 });
296
+ fs.renameSync(temp, file);
297
+ }
298
+ catch { /* discovery still works for this invocation */ }
299
+ }
300
+ function writeCacheEntry(entry) {
301
+ const latest = readCache();
302
+ latest.entries[entry.cacheKey] = entry;
303
+ writeCache(latest);
304
+ }
305
+ function safeCacheKey(provider, baseUrl) {
306
+ const normalized = (baseUrl ?? "").replace(/[?#].*$/, "").replace(/^https?:\/\/[^/]+@/i, "https://");
307
+ return `${provider}-${createHash("sha256").update(normalized).digest("hex").slice(0, 16)}`;
308
+ }
309
+ function baseUrlFor(provider, override) {
310
+ return resolveConnector(provider, override).baseUrl;
311
+ }
312
+ export function credentialedProviders(env = process.env) {
313
+ return configuredProviders(env);
314
+ }
315
+ async function getJson(fetchImpl, url, headers, signal) {
316
+ const response = await fetchImpl(url, { headers, signal, redirect: "error" });
317
+ const reader = response.body?.getReader();
318
+ let text = "";
319
+ if (reader) {
320
+ const decoder = new TextDecoder();
321
+ const chunks = [];
322
+ let total = 0;
323
+ try {
324
+ while (true) {
325
+ const next = await reader.read();
326
+ if (next.done)
327
+ break;
328
+ total += next.value.byteLength;
329
+ if (total > MAX_RESPONSE_BYTES) {
330
+ await reader.cancel();
331
+ throw new Error("model catalogue response is too large");
332
+ }
333
+ chunks.push(decoder.decode(next.value, { stream: true }));
334
+ }
335
+ chunks.push(decoder.decode());
336
+ text = chunks.join("");
337
+ }
338
+ finally {
339
+ reader.releaseLock();
340
+ }
341
+ }
342
+ else {
343
+ text = await response.text();
344
+ if (new TextEncoder().encode(text).byteLength > MAX_RESPONSE_BYTES)
345
+ throw new Error("model catalogue response is too large");
346
+ }
347
+ if (!response.ok)
348
+ throw new Error(`HTTP ${response.status}`);
349
+ try {
350
+ return JSON.parse(text);
351
+ }
352
+ catch {
353
+ throw new Error("model catalogue returned invalid JSON");
354
+ }
355
+ }
356
+ function headersFor(provider, key) {
357
+ if (provider === "anthropic")
358
+ return { "x-api-key": key ?? "", "anthropic-version": "2023-06-01" };
359
+ return key ? { Authorization: `Bearer ${key}` } : {};
360
+ }
361
+ async function fetchCatalog(provider, baseUrl, key, fetchImpl) {
362
+ const controller = new AbortController();
363
+ const timer = setTimeout(() => controller.abort(), 5_000);
364
+ try {
365
+ if (provider === "openai-codex") {
366
+ const codexHome = process.env.CODEX_HOME || path.join(os.homedir(), ".codex");
367
+ try {
368
+ return parseCodexModels(JSON.parse(fs.readFileSync(path.join(codexHome, "models_cache.json"), "utf8")));
369
+ }
370
+ catch {
371
+ return [];
372
+ }
373
+ }
374
+ if (provider === "anthropic") {
375
+ const all = [];
376
+ let after;
377
+ for (let page = 0; page < MAX_ANTHROPIC_PAGES; page++) {
378
+ const query = new URLSearchParams({ limit: "100", ...(after ? { after_id: after } : {}) });
379
+ const raw = record(await getJson(fetchImpl, `${baseUrl}/v1/models?${query}`, headersFor(provider, key), controller.signal));
380
+ all.push(...rows(raw?.data));
381
+ if (raw?.has_more !== true)
382
+ break;
383
+ after = string(raw.last_id);
384
+ if (!after)
385
+ break;
386
+ }
387
+ return parseAnthropicModels({ data: all });
388
+ }
389
+ const url = provider === "ollama" ? `${baseUrl}/api/tags`
390
+ : provider === "openrouter" ? `${baseUrl}/models?supported_parameters=tools&output_modalities=text`
391
+ : `${baseUrl}/models`;
392
+ const raw = await getJson(fetchImpl, url, headersFor(provider, key), controller.signal);
393
+ if (provider === "openrouter")
394
+ return parseOpenRouterModels(raw);
395
+ if (provider === "openai" || provider === "openai-compat")
396
+ return parseOpenAiModels(raw, provider === "openai" ? readCodexModels() : []).map((item) => ({ ...item, provider }));
397
+ if (provider === "deepseek")
398
+ return parseDeepSeekModels(raw);
399
+ return parseOllamaModels(raw);
400
+ }
401
+ finally {
402
+ clearTimeout(timer);
403
+ }
404
+ }
405
+ export async function discoverProvider(provider, options = {}) {
406
+ const connector = resolveConnector(provider, options.baseUrl);
407
+ const baseUrl = connector.baseUrl;
408
+ const key = safeCacheKey(provider, baseUrl);
409
+ const now = options.now ?? Date.now();
410
+ const cache = readCache();
411
+ const cached = cache.entries[key];
412
+ const cachedModels = cached?.models ?? getDiscoveredModels(provider) ?? [];
413
+ const fetchedAt = cached?.fetchedAt;
414
+ const fresh = cached && now - Date.parse(cached.fetchedAt) < MODEL_CACHE_TTL_MS;
415
+ if (!options.force && fresh) {
416
+ registerDiscoveredModels(provider, cached.models, "cache");
417
+ runtimeStatuses.set(key, "cached");
418
+ return { provider, models: cached.models, status: "cached", fetchedAt, baseUrl };
419
+ }
420
+ if (provider !== "openai-codex" && !baseUrl) {
421
+ runtimeStatuses.set(key, cached ? "stale" : "offline");
422
+ return { provider, models: cachedModels, status: cached ? "stale" : "offline", ...(fetchedAt ? { fetchedAt } : {}), error: "provider endpoint is not configured" };
423
+ }
424
+ try {
425
+ const models = await fetchCatalog(provider, baseUrl ?? "", connector.apiKey, options.fetchImpl ?? fetch);
426
+ if (models.length === 0)
427
+ throw new Error("provider returned no model rows");
428
+ registerDiscoveredModels(provider, models, "live");
429
+ runtimeStatuses.set(key, "live");
430
+ writeCacheEntry({ provider, cacheKey: key, fetchedAt: new Date(now).toISOString(), models });
431
+ return { provider, models, status: "live", fetchedAt: new Date(now).toISOString(), baseUrl };
432
+ }
433
+ catch (error) {
434
+ const message = error instanceof Error ? error.message : String(error);
435
+ if (cached) {
436
+ registerDiscoveredModels(provider, cached.models, "cache");
437
+ runtimeStatuses.set(key, "stale");
438
+ return { provider, models: cached.models, status: "stale", fetchedAt, error: message, baseUrl };
439
+ }
440
+ runtimeStatuses.set(key, "offline");
441
+ const fallback = getBuiltinModels(provider);
442
+ registerDiscoveredModels(provider, fallback, "fallback");
443
+ return { provider, models: fallback.map(model => ({ ...model, catalogSource: "fallback" })), status: "offline", error: message, baseUrl };
444
+ }
445
+ }
446
+ export async function discoverAllProviders(options = {}) {
447
+ return Promise.all(credentialedProviders().map((provider) => discoverProvider(provider, provider === "openai-compat" || provider === "deepseek" ? options : { ...options, baseUrl: undefined })));
448
+ }
449
+ /** Refreshes the cache for the Hook/CLI JSON catalog, without completions. */
450
+ export async function refreshModelCatalogs(force = false) {
451
+ return discoverAllProviders({ force });
452
+ }
453
+ export function catalogStatusFor(provider) {
454
+ const key = safeCacheKey(provider, baseUrlFor(provider));
455
+ const runtime = runtimeStatuses.get(key);
456
+ if (runtime)
457
+ return runtime;
458
+ const cached = readCache().entries[key];
459
+ if (cached && Date.now() - Date.parse(cached.fetchedAt) < MODEL_CACHE_TTL_MS)
460
+ return "cached";
461
+ if (cached)
462
+ return "stale";
463
+ return "offline";
464
+ }
465
+ export function codexCatalogConfiguredModel() {
466
+ return codexConfiguredModel(process.env.CODEX_HOME || path.join(os.homedir(), ".codex"));
467
+ }
@@ -3,53 +3,43 @@
3
3
  * phren Hook's model picker. Only providers with credentials here are
4
4
  * listed, in the order auto-detection picks them, so the first is the
5
5
  * default. Each id is `<provider>/<model>`; the Hook passes it back as
6
- * `--provider` and `--model`. Loads no agent runtime and makes no request.
6
+ * `--provider` and `--model`. The bin refreshes the bounded catalogue cache
7
+ * before calling this formatter; it never starts a completion.
7
8
  */
8
- import { resolveApiKey } from "@phren/cli/auth/profiles";
9
- import { codexConfiguredModel, hasCodexToken } from "./providers/codex-auth.js";
10
- import { getBuiltinModels, getDefaultModel } from "./models.js";
9
+ import { resolveConnector } from "./provider-connectors.js";
10
+ import { codexConfiguredModel } from "./providers/codex-auth.js";
11
+ import { getBuiltinModels, getDefaultModel, getDiscoveredModels } from "./models.js";
12
+ import { catalogStatusFor, credentialedProviders as discoveredCredentialedProviders, refreshModelCatalogs } from "./model-discovery.js";
11
13
  const PROVIDER_NAMES = {
12
14
  "openai-codex": "ChatGPT subscription",
13
15
  openai: "OpenAI API",
14
16
  openrouter: "OpenRouter",
15
17
  anthropic: "Anthropic API",
16
18
  deepseek: "DeepSeek API",
19
+ "openai-compat": "OpenAI-compatible endpoint",
17
20
  ollama: "Ollama",
18
21
  };
19
- /** Providers with credentials on this computer, in resolveProvider's order.
20
- * openai-compat needs an endpoint and model of its own, so it is never listed. */
22
+ /** Providers with credentials/connectors on this computer, in resolveProvider's order. */
21
23
  export function credentialedProviders(env = process.env) {
22
- const providers = [];
23
- if (hasCodexToken())
24
- providers.push("openai-codex");
25
- if (resolveApiKey("openai", "OPENAI_API_KEY"))
26
- providers.push("openai");
27
- if (resolveApiKey("openrouter", "OPENROUTER_API_KEY"))
28
- providers.push("openrouter");
29
- if (resolveApiKey("anthropic", "ANTHROPIC_API_KEY"))
30
- providers.push("anthropic");
31
- if (env.DEEPSEEK_API_KEY)
32
- providers.push("deepseek");
33
- if (env.PHREN_OLLAMA_URL && env.PHREN_OLLAMA_URL !== "off")
34
- providers.push("ollama");
35
- return providers;
24
+ return discoveredCredentialedProviders(env);
36
25
  }
37
26
  export function listModels(providers = credentialedProviders()) {
38
27
  const models = [];
39
28
  for (const provider of providers) {
40
- const entries = getBuiltinModels(provider);
29
+ const entries = getDiscoveredModels(provider) ?? getBuiltinModels(provider);
41
30
  // Codex's own config can name a model the catalog doesn't have yet.
42
- const configured = provider === "openai-codex" ? codexConfiguredModel() : undefined;
31
+ const configured = provider === "openai-codex" ? codexConfiguredModel() : resolveConnector(provider).model;
43
32
  if (configured && !entries.some(entry => entry.id === configured)) {
44
- entries.unshift({ ...entries[0], id: configured, label: configured });
33
+ entries.unshift({ provider, id: configured, label: configured, reasoningDefault: null, reasoningRange: [] });
45
34
  }
46
- const preferred = configured ?? getDefaultModel(provider);
35
+ const preferred = configured ?? entries[0]?.id ?? getDefaultModel(provider);
47
36
  for (const entry of entries) {
48
37
  models.push({
49
38
  id: `${provider}/${entry.id}`,
50
39
  name: entry.label,
51
40
  provider,
52
41
  description: PROVIDER_NAMES[provider] ?? provider,
42
+ catalogStatus: entries[0]?.catalogSource === "fallback" ? "offline" : catalogStatusFor(provider),
53
43
  ...(!models.some(model => model.isDefault) && entry.id === preferred ? { isDefault: true } : {}),
54
44
  ...(entry.reasoningDefault && entry.reasoningDefault !== "none" ? { defaultReasoningEffort: entry.reasoningDefault } : {}),
55
45
  supportedReasoningEfforts: entry.reasoningRange.filter(level => level !== "none"),
@@ -61,7 +51,8 @@ export function listModels(providers = credentialedProviders()) {
61
51
  export function printModels(args) {
62
52
  const models = listModels();
63
53
  if (args.includes("--json")) {
64
- console.log(JSON.stringify({ models }));
54
+ const statuses = Object.fromEntries([...new Set(models.map((model) => model.provider))].map((provider) => [provider, catalogStatusFor(provider)]));
55
+ console.log(JSON.stringify({ models, catalogStatus: statuses }));
65
56
  return;
66
57
  }
67
58
  if (!models.length) {
@@ -71,3 +62,7 @@ export function printModels(args) {
71
62
  for (const model of models)
72
63
  console.log(`${model.isDefault ? "*" : " "} ${model.id.padEnd(48)} ${model.name} (${model.description})`);
73
64
  }
65
+ export async function refreshModelsCommand(args) {
66
+ await refreshModelCatalogs(args.includes("--refresh"));
67
+ printModels(args);
68
+ }
package/dist/models.js CHANGED
@@ -1,8 +1,28 @@
1
1
  /** Levels the pickers step through ("none" is CLI/env only). */
2
- export const REASONING_LEVELS = ["low", "medium", "high", "xhigh"];
2
+ export const REASONING_LEVELS = ["minimal", "low", "medium", "high", "xhigh", "max", "ultra"];
3
+ /** Provider-discovered rows are installed here by model-discovery.ts. */
4
+ const discoveredModels = new Map();
5
+ export function registerDiscoveredModels(provider, entries, source) {
6
+ const rows = entries.map((entry) => ({
7
+ ...entry,
8
+ provider,
9
+ catalogSource: source,
10
+ reasoningRange: [...entry.reasoningRange],
11
+ pricing: entry.pricing ? { ...entry.pricing } : undefined,
12
+ }));
13
+ discoveredModels.set(provider, rows);
14
+ }
15
+ export function getDiscoveredModels(provider) {
16
+ const rows = discoveredModels.get(provider);
17
+ return rows?.map((entry) => ({
18
+ ...entry,
19
+ reasoningRange: [...entry.reasoningRange],
20
+ pricing: entry.pricing ? { ...entry.pricing } : undefined,
21
+ }));
22
+ }
3
23
  /**
4
24
  * DeepSeek V4.1 Flash, direct or through an OpenAI-compatible relay: 1M
5
- * context, 393,216 max output, effort low/high/max (picker "xhigh" = max).
25
+ * context, 393,216 max output, effort low/high/max.
6
26
  * Off-peak rates; DeepSeek bills 2x at peak (weekdays 01-04 and 06-10 UTC).
7
27
  * Checked against https://api-docs.deepseek.com/quick_start/pricing on
8
28
  * 2026-10-01: cache hit $0.003 / miss $0.15 / output $0.60 off-peak
@@ -13,7 +33,7 @@ const DEEPSEEK_FLASH = {
13
33
  contextWindow: 1_000_000,
14
34
  maxOutputTokens: 393_216,
15
35
  reasoningDefault: null,
16
- reasoningRange: ["low", "high", "xhigh"],
36
+ reasoningRange: ["low", "high", "max"],
17
37
  pricing: { inputPer1M: 0.15, outputPer1M: 0.6, cacheReadPer1M: 0.003 },
18
38
  };
19
39
  const BUILTIN_MODELS = {
@@ -305,7 +325,7 @@ const BUILTIN_MODELS = {
305
325
  contextWindow: 1_000_000,
306
326
  maxOutputTokens: 65_536,
307
327
  reasoningDefault: null,
308
- reasoningRange: ["low", "high", "xhigh"],
328
+ reasoningRange: ["low", "high", "max"],
309
329
  pricing: { inputPer1M: 0.66, outputPer1M: 1.98, cacheReadPer1M: 0.022 },
310
330
  },
311
331
  ],
@@ -459,10 +479,10 @@ export function normalizeReasoningEffort(raw) {
459
479
  return undefined;
460
480
  const value = raw.toLowerCase();
461
481
  if (value === "max")
462
- return "xhigh";
482
+ return "max";
463
483
  if (value === "off")
464
484
  return "none";
465
- if (value === "none" || value === "low" || value === "medium" || value === "high" || value === "xhigh")
485
+ if (value === "none" || value === "minimal" || value === "low" || value === "medium" || value === "high" || value === "xhigh")
466
486
  return value;
467
487
  return undefined;
468
488
  }
@@ -474,7 +494,7 @@ export function isUnmeteredEndpoint(baseUrl) {
474
494
  return !!baseUrl && /^https?:\/\/opencode\.ai\/zen\/go(?:\/|$)/i.test(baseUrl);
475
495
  }
476
496
  export function getDefaultModel(provider) {
477
- return BUILTIN_MODELS[provider][0].id;
497
+ return discoveredModels.get(provider)?.[0]?.id ?? BUILTIN_MODELS[provider][0].id;
478
498
  }
479
499
  export function getBuiltinModels(provider) {
480
500
  return BUILTIN_MODELS[provider].map((entry) => ({
@@ -486,6 +506,9 @@ export function getBuiltinModels(provider) {
486
506
  export function getModelMetadata(provider, model) {
487
507
  const normalizedProvider = normalizeProviderId(provider);
488
508
  if (normalizedProvider) {
509
+ const discovered = discoveredModels.get(normalizedProvider)?.find((entry) => entry.id === model);
510
+ if (discovered)
511
+ return discovered;
489
512
  const direct = BUILTIN_MODELS[normalizedProvider].find((entry) => entry.id === model);
490
513
  if (direct)
491
514
  return direct;
@@ -509,7 +532,7 @@ export function getReasoningRange(provider, model) {
509
532
  }
510
533
  export function lookupMaxOutputTokens(model, provider) {
511
534
  const metadata = getModelMetadata(provider, model);
512
- if (metadata)
535
+ if (metadata?.maxOutputTokens)
513
536
  return metadata.maxOutputTokens;
514
537
  const lower = model.toLowerCase();
515
538
  for (const [prefix, limit] of LEGACY_OUTPUT_LIMITS) {
@@ -520,7 +543,7 @@ export function lookupMaxOutputTokens(model, provider) {
520
543
  }
521
544
  export function lookupContextWindow(model, provider) {
522
545
  const metadata = getModelMetadata(provider, model);
523
- if (metadata)
546
+ if (metadata?.contextWindow)
524
547
  return metadata.contextWindow;
525
548
  const lower = model.toLowerCase();
526
549
  for (const [prefix, limit] of LEGACY_CONTEXT_LIMITS) {