@lks347/dsh-oauth 1.0.1 → 1.0.2

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/src/models.js CHANGED
@@ -7,14 +7,19 @@ import { qwenEndpoint } from "./qwen.js";
7
7
  import { OAuthRequestError } from "./request-errors.js";
8
8
  import { builtinModels } from '@earendil-works/pi-ai/providers/all';
9
9
  import { getProviderDefinition } from './catalog.js';
10
+ import { readCodexCatalogClientVersion } from './local-imports.js';
10
11
 
11
12
  const JSON_HEADERS = { accept: "application/json" };
12
- const CODEX_CLIENT_VERSION = "0.156.1";
13
+ const CODEX_CLIENT_VERSION = "0.171.0";
14
+ const CODEX_VERSION_FORMAT = /^(?:0|[1-9]\d{0,3})\.(?:0|[1-9]\d{0,3})\.(?:0|[1-9]\d{0,3})$/;
13
15
 
14
16
  /** Codex model catalog requires this query; a missing client_version is HTTP 400. */
15
- export function codexModelsUrl(clientVersion = CODEX_CLIENT_VERSION) {
16
- const url = new URL("https://chatgpt.com/backend-api/codex/models");
17
- url.searchParams.set("client_version", clientVersion);
17
+ export function codexModelsUrl(clientVersion) {
18
+ const url = new URL(getProviderDefinition("openai-codex").modelsURL);
19
+ const version = typeof clientVersion === "string" && CODEX_VERSION_FORMAT.test(clientVersion) &&
20
+ clientVersion.localeCompare(CODEX_CLIENT_VERSION, "en", { numeric: true }) > 0
21
+ ? clientVersion : CODEX_CLIENT_VERSION;
22
+ url.searchParams.set("client_version", version);
18
23
  return url.toString();
19
24
  }
20
25
 
@@ -153,15 +158,65 @@ export async function fetchAccountModels(provider, payload) {
153
158
  if (definition?.catalogProvider && !definition.modelsURL) {
154
159
  const registry = builtinModels({ authContext: { env: async () => undefined, fileExists: async () => false } });
155
160
  const installed = await registry.getProvider(definition.catalogProvider)?.getModels();
156
- if (!installed?.length) throw new Error('The installed provider catalog is unavailable.');
157
- const source = new Map(installed.map(model => [model.id, model]));
158
- return parseModelList(installed, provider).map(model => {
161
+ const source = new Map((installed ?? []).map(model => [model.id, model]));
162
+ const fallback = parseModelList(installed, provider).map(model => {
159
163
  const compat = modelCompatFrom(source.get(model.id)?.compat);
160
164
  return { ...model, ...(compat ? { compat } : {}), source: 'installed-catalog', entitlement: 'unknown' };
161
165
  });
166
+ if (provider === 'aliyun-token-plan' || provider === 'zai') {
167
+ try {
168
+ const catalog = await fetchModelPage(provider, `${definition.baseURL}/models`, { ...JSON_HEADERS, authorization: `Bearer ${access}` });
169
+ const models = parseModelList(catalog, provider);
170
+ if (!models.length) throw new Error(`${provider} returned no supported models.`);
171
+ const available = new Map(fallback.map(model => [model.id, model]));
172
+ return models.map(model => ({ ...available.get(model.id), ...model, source: 'account', entitlement: provider === 'aliyun-token-plan' ? 'subscription' : 'unknown' }));
173
+ } catch (error) {
174
+ // Older regional endpoints can omit listing entirely.
175
+ if (!(error instanceof OAuthRequestError) || ![404, 405, 501].includes(error.status)) throw error;
176
+ }
177
+ }
178
+ if (provider === 'minimax') {
179
+ const url = `https://${definition.region === 'cn' ? 'api.minimax.cn' : 'api.minimax.io'}/v1/token_plan/remains`;
180
+ try {
181
+ const quota = await fetchModelPage(provider, url, { ...JSON_HEADERS, authorization: `Bearer ${access}` });
182
+ const models = new Map(fallback.map(model => [model.id.toLowerCase(), model]));
183
+ for (const row of Array.isArray(quota?.model_remains) ? quota.model_remains : []) {
184
+ const id = row?.model_name;
185
+ if (typeof id !== 'string' || !/^MiniMax-M\d+(?:\.\d+)*(?:-[a-z0-9]+)*$/i.test(id)) continue;
186
+ const key = id.toLowerCase();
187
+ models.set(key, { ...(models.get(key) ?? { id, name: id, role: 'chat' }), source: 'account-quota', entitlement: 'subscription' });
188
+ }
189
+ if (models.size) return [...models.values()];
190
+ } catch (error) {
191
+ if (!(error instanceof OAuthRequestError) || ![404, 405, 501].includes(error.status)) throw error;
192
+ }
193
+ }
194
+ if (!fallback.length) throw new Error('The installed provider catalog is unavailable.');
195
+ return fallback;
162
196
  }
163
- const request = modelListRequest(provider, access, payload);
197
+ const codexClientVersion = provider === "openai-codex" ? await readCodexCatalogClientVersion() : undefined;
198
+ const request = modelListRequest(provider, access, payload, codexClientVersion);
164
199
  let body = await fetchModelPage(provider, request.url, request.headers);
200
+ if (provider === "openrouter") {
201
+ const rows = Array.isArray(body?.data) ? [...body.data] : [];
202
+ const total = body?.total_count;
203
+ const visited = new Set([request.url]);
204
+ while (body?.links?.next) {
205
+ const next = new URL(body.links.next, request.url);
206
+ const original = new URL(request.url);
207
+ if (next.origin !== original.origin || next.pathname !== original.pathname || next.username || next.password || next.hash || visited.has(next.href) || visited.size >= 50) {
208
+ throw new Error("OpenRouter 模型目录分页链接无效或超出限制");
209
+ }
210
+ visited.add(next.href);
211
+ body = await fetchModelPage(provider, next.href, request.headers);
212
+ if (!Array.isArray(body?.data)) throw new Error("OpenRouter 模型目录分页响应无效");
213
+ rows.push(...body.data);
214
+ }
215
+ if (Number.isInteger(total) && rows.length < total) {
216
+ throw new Error("OpenRouter 未返回完整的模型目录,请稍后重试");
217
+ }
218
+ body = { data: rows };
219
+ }
165
220
  // Anthropic pages its catalog: follow has_more / last_id until the list ends.
166
221
  if (provider === "anthropic") {
167
222
  const rows = Array.isArray(body?.data) ? [...body.data] : [];
@@ -211,8 +266,12 @@ async function withGrokMedia(chatModels, access) {
211
266
  } catch {
212
267
  /* Preserve only media the account's successful catalog response listed. */
213
268
  }
269
+ const unique = new Map();
270
+ for (const model of [...preferredMedia(listed), ...listed]) {
271
+ if (!unique.has(model.id)) unique.set(model.id, model);
272
+ }
214
273
  const seen = new Set(chats.map((model) => model.id));
215
- return [...chats, ...preferredMedia(listed).filter((model) => !seen.has(model.id))];
274
+ return [...chats, ...[...unique.values()].filter((model) => !seen.has(model.id))];
216
275
  }
217
276
 
218
277
  /** Copilot's picker and policy flags describe account availability, including its Individual-only fallback. */
@@ -220,7 +279,8 @@ function copilotAccountCatalog(body, requestUrl) {
220
279
  const rows = Array.isArray(body?.data) ? body.data.filter((item) =>
221
280
  item && typeof item === "object" && item.capabilities?.type === "chat" &&
222
281
  item.capabilities?.supports?.tool_calls !== false) : [];
223
- const picker = rows.filter((item) => item.model_picker_enabled === true && item.policy?.state !== "disabled");
282
+ const picker = rows.filter((item) => item.policy?.state !== "disabled" &&
283
+ (item.model_picker_enabled === true || item.model_picker_enabled == null && item.policy?.state === "enabled"));
224
284
  const individual = requestUrl === "https://api.individual.githubcopilot.com/models";
225
285
  return { data: picker.length > 0 || !individual ? picker : rows.filter((item) => item.policy?.state === "enabled") };
226
286
  }
@@ -455,8 +515,9 @@ function thinkingLevel(value) {
455
515
  * @param {string} provider
456
516
  * @param {string} access
457
517
  * @param {Record<string, unknown>} payload
518
+ * @param {string} [codexClientVersion] Version observed in the local Codex CLI cache.
458
519
  */
459
- export function modelListRequest(provider, access, payload = {}) {
520
+ export function modelListRequest(provider, access, payload = {}, codexClientVersion) {
460
521
  const authHeaders = new Headers({ ...JSON_HEADERS, authorization: `Bearer ${access}` });
461
522
  // Existing OAuth resolvers may attach protocol-specific auth headers. New
462
523
  // key connections use only the chosen credential and fixed catalog headers.
@@ -469,9 +530,12 @@ export function modelListRequest(provider, access, payload = {}) {
469
530
  const headers = { ...bearer };
470
531
  const accountId = chatgptAccountId(access) ?? (typeof payload.accountId === "string" ? payload.accountId : undefined);
471
532
  if (accountId) headers["chatgpt-account-id"] = accountId;
472
- return { url: codexModelsUrl(), headers };
533
+ return { url: codexModelsUrl(codexClientVersion), headers };
473
534
  }
474
535
  if (provider === "anthropic") {
536
+ if (payload.type === "api_key") {
537
+ return { url: getProviderDefinition(provider).modelsURL, headers: { ...JSON_HEADERS, "anthropic-version": "2023-06-01", "x-api-key": access } };
538
+ }
475
539
  return {
476
540
  url: "https://api.anthropic.com/v1/models?limit=1000",
477
541
  headers: {
@@ -485,7 +549,7 @@ export function modelListRequest(provider, access, payload = {}) {
485
549
  return { url: "https://openrouter.ai/api/v1/models/user", headers: bearer };
486
550
  }
487
551
  if (provider === "xai") {
488
- return { url: modelsUrl(payload.apiBase, "https://cli-chat-proxy.grok.com/v1"), headers: bearer };
552
+ return { url: modelsUrl(payload.apiBase, "https://cli-chat-proxy.grok.com/v1"), headers: { "x-xai-token-auth": "xai-grok-cli", ...bearer } };
489
553
  }
490
554
  if (provider === "kimi-coding") {
491
555
  if (payload.region === 'cn' || payload.region === 'global') {
@@ -1,4 +1,7 @@
1
1
  import assert from "node:assert/strict";
2
+ import { mkdtemp, rm, writeFile } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
2
5
  import test from "node:test";
3
6
  import { codexModelsUrl, fetchAccountModels, modelListRequest, parseModelList, preferredMedia } from "./models.js";
4
7
  import { OAuthRequestError, withGrantRecovery } from "./request-errors.js";
@@ -6,12 +9,49 @@ import { OAuthRequestError, withGrantRecovery } from "./request-errors.js";
6
9
  test("codex model list URL includes client_version", () => {
7
10
  const url = new URL(codexModelsUrl());
8
11
  assert.equal(url.origin + url.pathname, "https://chatgpt.com/backend-api/codex/models");
9
- assert.equal(url.searchParams.get("client_version"), "0.156.1");
12
+ assert.equal(url.searchParams.get("client_version"), "0.171.0");
13
+ for (const invalid of ["0.99.99", "0.171.0-test", "0.200.0&limit=1", { version: "0.200.0" }]) {
14
+ assert.equal(new URL(codexModelsUrl(invalid)).searchParams.get("client_version"), "0.171.0");
15
+ }
16
+ });
17
+
18
+ test("Codex model pull uses a newer local client version and the connected account's live catalog", async (t) => {
19
+ const home = await mkdtemp(join(tmpdir(), "dsh-codex-catalog-test-"));
20
+ const previousHome = process.env.CODEX_HOME;
21
+ process.env.CODEX_HOME = home;
22
+ t.after(async () => {
23
+ if (previousHome === undefined) delete process.env.CODEX_HOME;
24
+ else process.env.CODEX_HOME = previousHome;
25
+ await rm(home, { recursive: true, force: true });
26
+ });
27
+ const cachePath = join(home, "models_cache.json");
28
+ await writeFile(cachePath, JSON.stringify({ client_version: "0.200.0", models: [{ slug: "another-accounts-model" }] }));
29
+ const requests = [];
30
+ t.mock.method(globalThis, "fetch", async (url, options) => {
31
+ const request = new URL(url);
32
+ assert.equal(request.origin + request.pathname, "https://chatgpt.com/backend-api/codex/models");
33
+ assert.equal(options.headers.authorization, "Bearer fixture-access");
34
+ assert.equal(options.headers["chatgpt-account-id"], "fixture-account");
35
+ requests.push(request.searchParams.get("client_version"));
36
+ return Response.json({ models: [
37
+ { slug: "new-chat", visibility: "list" },
38
+ { slug: "existing-chat", visibility: "list" },
39
+ { slug: "internal-model", visibility: "hide" },
40
+ ] });
41
+ });
42
+ const grant = { access: "fixture-access", accountId: "fixture-account" };
43
+ const models = await fetchAccountModels("openai-codex", grant);
44
+ assert.deepEqual(models.map(model => model.id), ["new-chat", "existing-chat"]);
45
+ await writeFile(cachePath, "{broken cache");
46
+ await fetchAccountModels("openai-codex", grant);
47
+ await rm(cachePath);
48
+ await fetchAccountModels("openai-codex", grant);
49
+ assert.deepEqual(requests, ["0.200.0", "0.171.0", "0.171.0"]);
10
50
  });
11
51
 
12
52
  test("each OAuth model list sends the fields that provider requires", () => {
13
53
  const codex = modelListRequest("openai-codex", "header.payload.sig");
14
- assert.equal(new URL(codex.url).searchParams.get("client_version"), "0.156.1");
54
+ assert.equal(new URL(codex.url).searchParams.get("client_version"), "0.171.0");
15
55
 
16
56
  const claude = modelListRequest("anthropic", "token");
17
57
  assert.equal(claude.headers["anthropic-version"], "2023-06-01");
@@ -413,3 +453,51 @@ test("an authentication rejection on a later Anthropic page retries the read-onl
413
453
  ]);
414
454
  assert.equal(resolveCalls, 2);
415
455
  });
456
+
457
+ test("OpenRouter follows same-account pagination and refuses partial or redirected catalog links", async t => {
458
+ let next = "?limit=1&offset=1";
459
+ const requests = [];
460
+ t.mock.method(globalThis, "fetch", async (url, options) => {
461
+ const request = new URL(url);
462
+ assert.equal(request.origin, "https://openrouter.ai");
463
+ assert.equal(request.pathname, "/api/v1/models/user");
464
+ assert.equal(options.headers.authorization, "Bearer fixture");
465
+ requests.push(request.searchParams.get("offset"));
466
+ return request.searchParams.has("offset")
467
+ ? Response.json({ data: [{ id: "router/second" }] })
468
+ : Response.json({ data: [{ id: "router/first" }], total_count: 2, links: { next } });
469
+ });
470
+ const get = () => fetchAccountModels("openrouter", { access: "fixture" });
471
+ assert.deepEqual((await get()).map(model => model.id), ["router/first", "router/second"]);
472
+ assert.deepEqual(requests, [null, "1"]);
473
+ next = "https://elsewhere.example/api/v1/models/user";
474
+ await assert.rejects(get(), /分页链接无效/);
475
+ next = null;
476
+ await assert.rejects(get(), /未返回完整/);
477
+ });
478
+
479
+ test("Copilot retains explicitly policy-enabled chat entries when their picker flag is absent", async t => {
480
+ t.mock.method(globalThis, "fetch", async () => Response.json({ data: [
481
+ { id: "listed", model_picker_enabled: true, capabilities: { type: "chat" } },
482
+ { id: "policy-only", policy: { state: "enabled" }, capabilities: { type: "chat" } },
483
+ { id: "explicitly-hidden", model_picker_enabled: false, policy: { state: "enabled" }, capabilities: { type: "chat" } },
484
+ { id: "disabled", model_picker_enabled: true, policy: { state: "disabled" }, capabilities: { type: "chat" } },
485
+ ] }));
486
+ const models = await fetchAccountModels("github-copilot", { access: "fixture", apiBase: "https://api.business.githubcopilot.com" });
487
+ assert.deepEqual(models.map(model => model.id), ["listed", "policy-only"]);
488
+ });
489
+
490
+ test("Grok catalog retains media variants while keeping the preferred defaults first", async t => {
491
+ t.mock.method(globalThis, "fetch", async (url) => Response.json({ data: new URL(url).hostname === "api.x.ai"
492
+ ? [{ id: "grok-imagine-image-2.0" }, { id: "grok-imagine-video-1.5" }]
493
+ : [{ id: "grok-chat" }, { id: "grok-imagine-image" }, { id: "grok-imagine-video" }, { id: "grok-imagine-image-2.0" }] }));
494
+ const models = await fetchAccountModels("xai", { access: "fixture" });
495
+ assert.deepEqual(models.map(model => model.id), ["grok-chat", "grok-imagine-image-2.0", "grok-imagine-video-1.5", "grok-imagine-image", "grok-imagine-video"]);
496
+ });
497
+
498
+ test("Claude Console model requests use x-api-key instead of subscription authorization", () => {
499
+ const request = modelListRequest("anthropic", "fixture", { type: "api_key" });
500
+ assert.equal(request.headers["x-api-key"], "fixture");
501
+ assert.equal(request.headers.authorization, undefined);
502
+ assert.equal(request.headers["anthropic-beta"], undefined);
503
+ });
@@ -87,8 +87,11 @@ test('DeepSeek documented effort metadata maps only advertised reasoning levels'
87
87
  assert.equal(row.maxTokens, 384000);
88
88
  });
89
89
 
90
- test('installed MiniMax, ZAI and Alibaba model imports perform no remote request', async t => {
91
- t.mock.method(globalThis, 'fetch', async () => { throw new Error('Installed catalog must not fetch or infer.'); });
90
+ test('installed MiniMax, ZAI and Alibaba models remain available when live discovery is unsupported', async t => {
91
+ t.mock.method(globalThis, 'fetch', async (url) => {
92
+ assert.match(new URL(url).pathname, /\/(?:models|token_plan\/remains)$/);
93
+ return new Response('', { status: 404 });
94
+ });
92
95
  const registry = builtinModels({ authContext: { env: async () => undefined, fileExists: async () => false } });
93
96
  for (const provider of ['minimax', 'zai', 'aliyun-token-plan']) for (const region of ['cn', 'global']) {
94
97
  const definition = getProviderDefinition(provider, { region });
@@ -137,3 +140,21 @@ test('gateway catalog is bound to configured instance and unconfigured Google do
137
140
  assert.throws(() => modelListRequest('new-api', 'key', { baseURL: 'http://127.0.0.1/v1' }), /目录接口/);
138
141
  assert.throws(() => modelListRequest('google-antigravity', 'key'), /目录接口/);
139
142
  });
143
+
144
+ test('Alibaba and MiniMax can discover models absent from the installed registry', async t => {
145
+ t.mock.method(globalThis, 'fetch', async (url, options) => {
146
+ assert.equal(options.headers.authorization, 'Bearer fixture');
147
+ const target = new URL(url);
148
+ if (target.hostname === 'token-plan.cn-beijing.maas.aliyuncs.com') {
149
+ assert.equal(target.pathname, '/compatible-mode/v1/models');
150
+ return Response.json({ data: [{ id: 'new-bailian-chat' }] });
151
+ }
152
+ assert.equal(target.origin + target.pathname, 'https://api.minimax.cn/v1/token_plan/remains');
153
+ return Response.json({ model_remains: [{ model_name: 'MiniMax-M9' }, { model_name: 'All Models' }] });
154
+ });
155
+ const ali = await fetchAccountModels('aliyun-token-plan', { region: 'cn', access: 'fixture' });
156
+ assert.deepEqual(ali.map(model => [model.id, model.source]), [['new-bailian-chat', 'account']]);
157
+ const minimax = await fetchAccountModels('minimax', { region: 'cn', access: 'fixture' });
158
+ assert.equal(minimax.find(model => model.id === 'MiniMax-M9').source, 'account-quota');
159
+ assert.equal(minimax.some(model => model.id === 'All Models'), false);
160
+ });