@linxiraos/pi-catalog 1.4.6 → 1.4.7

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.
Files changed (40) hide show
  1. package/CHANGELOG.md +24 -1
  2. package/README.md +3 -3
  3. package/dist/types/build.d.ts +12 -4
  4. package/dist/types/compat/types.d.ts +3 -1
  5. package/dist/types/discovery/codex.d.ts +4 -5
  6. package/dist/types/model-cache.d.ts +8 -0
  7. package/dist/types/model-catalog-format.d.ts +37 -0
  8. package/dist/types/models.d.ts +6 -3
  9. package/dist/types/wire/codex.d.ts +1 -1
  10. package/package.json +6 -10
  11. package/src/build.d.ts +12 -4
  12. package/src/build.ts +50 -3
  13. package/src/compat/axes.ts +6 -0
  14. package/src/compat/rules/README.md +11 -11
  15. package/src/compat/rules/auth/typesafe.kdl +2 -1
  16. package/src/compat/rules/classes/deepseek.kdl +2 -1
  17. package/src/compat/rules/classes/openai.kdl +2 -2
  18. package/src/compat/rules/providers/factory-droid.kdl +24 -0
  19. package/src/compat/rules/providers/google-vertex.kdl +3 -0
  20. package/src/compat/rules/providers/kimi-code.kdl +6 -0
  21. package/src/compat/rules/providers/mistral.kdl +8 -0
  22. package/src/compat/rules/runtime/behavior.kdl +9 -6
  23. package/src/compat/rules.json +1 -1
  24. package/src/compat/types.d.ts +3 -1
  25. package/src/compat/types.ts +3 -1
  26. package/src/discovery/codex.d.ts +4 -5
  27. package/src/discovery/codex.ts +5 -6
  28. package/src/model-cache.d.ts +8 -0
  29. package/src/model-cache.ts +51 -3
  30. package/src/model-catalog-format.d.ts +37 -0
  31. package/src/model-catalog-format.ts +299 -0
  32. package/src/models.d.ts +6 -3
  33. package/src/models.json +1 -1
  34. package/src/models.ts +16 -10
  35. package/src/provider-models/cache-provider-id.ts +23 -6
  36. package/src/provider-models/openai-compat.ts +83 -23
  37. package/src/provider-models/special.ts +5 -6
  38. package/src/wire/codex.d.ts +1 -1
  39. package/src/wire/codex.ts +1 -1
  40. package/src/models.json.d.ts +0 -9
package/src/models.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { classifyModel } from "./compat/taxonomy";
2
- import MODELS from "./models.json" with { type: "json" };
2
+ import { getCatalogProvider, getCatalogProviders, openModelCatalog } from "./model-catalog-format";
3
+ import modelsData from "./models.json" with { type: "json" };
3
4
  import { dueEffectiveRate } from "./pricing";
4
5
  import type {
5
6
  Api,
@@ -21,20 +22,21 @@ import type {
21
22
  *
22
23
  * For runtime-aware resolution, use `createModelManager()` / `resolveProviderModels()`.
23
24
  */
25
+ const catalog = openModelCatalog(modelsData);
26
+ const bundledProviders: readonly KnownProvider[] = Object.freeze(getCatalogProviders(catalog) as KnownProvider[]);
27
+ const bundledProviderSet = new Set<string>(bundledProviders);
24
28
  const modelRegistry = new Map<string, Map<string, Model<Api>>>();
25
29
 
26
30
  /** Return one provider's bundled models, materialized by the generator. */
27
31
  function getProviderModels(provider: string): Map<string, Model<Api>> | undefined {
28
32
  const cachedModels = modelRegistry.get(provider);
29
33
  if (cachedModels !== undefined) return cachedModels;
30
- if (!Object.hasOwn(MODELS, provider)) return undefined;
34
+ const rawModels = getCatalogProvider(catalog, provider);
35
+ if (!rawModels) return undefined;
31
36
 
32
37
  const providerModels = new Map<string, Model<Api>>();
33
- const rawModels = MODELS[provider as keyof typeof MODELS];
34
38
  for (const id in rawModels) {
35
- // models.json rows are complete Models emitted by generate-models.ts;
36
- // consuming them verbatim keeps startup allocation-free.
37
- const row = rawModels[id as keyof typeof rawModels] as unknown as Model<Api>;
39
+ const row = rawModels[id];
38
40
  // Rows baked before the compat engine (and stale cache snapshots) lack
39
41
  // `identity`; classify lazily so consumers can rely on the field.
40
42
  row.identity ??= classifyModel(provider, id, { lenient: true });
@@ -44,22 +46,26 @@ function getProviderModels(provider: string): Map<string, Model<Api>> | undefine
44
46
  return providerModels;
45
47
  }
46
48
 
47
- export type GeneratedProvider = keyof typeof MODELS;
49
+ /** Provider identifier accepted by the bundled model registry. */
50
+ export type GeneratedProvider = string;
48
51
 
49
52
  /** Whether `provider` has bundled rows in models.json (e.g. a provider id authored in KDL). */
50
53
  export function isGeneratedProvider(provider: string): provider is GeneratedProvider {
51
- return Object.hasOwn(MODELS, provider);
54
+ return bundledProviderSet.has(provider);
52
55
  }
53
56
 
57
+ /** Look up a bundled model; nested catalog values are shared and must not be mutated. */
54
58
  export function getBundledModel<TApi extends Api = Api>(provider: GeneratedProvider, modelId: string): Model<TApi> {
55
59
  const providerModels = getProviderModels(provider);
56
60
  return providerModels?.get(modelId) as Model<TApi>;
57
61
  }
58
62
 
59
- export function getBundledProviders(): KnownProvider[] {
60
- return Object.keys(MODELS) as KnownProvider[];
63
+ /** List providers with bundled rows; the array is shared and frozen. */
64
+ export function getBundledProviders(): readonly KnownProvider[] {
65
+ return bundledProviders;
61
66
  }
62
67
 
68
+ /** List a provider's bundled models; nested catalog values are shared and must not be mutated. */
63
69
  export function getBundledModels(provider: GeneratedProvider): Model<Api>[] {
64
70
  const models = getProviderModels(provider);
65
71
  return models ? (Array.from(models.values()) as Model<Api>[]) : [];
@@ -26,6 +26,9 @@ const CREDENTIAL_SCOPED_MODEL_CACHE_PROVIDERS: Readonly<Record<string, true>> =
26
26
  // than from the synchronous, credential-less startup read.
27
27
  "singularityapi-dev": true,
28
28
  "singularityapi-tech": true,
29
+ // Kimi Code's roster is plan-gated and discovery is authoritative, so a
30
+ // shared namespace would serve one plan's ids to another plan's credential.
31
+ "kimi-code": true,
29
32
  };
30
33
 
31
34
  /** Whether a provider's model-cache namespace requires its resolved credential. */
@@ -37,6 +40,8 @@ export function getDefaultModelDiscoveryBaseUrl(providerId: string): string | un
37
40
  switch (providerId) {
38
41
  case "charm-hyper":
39
42
  return CHARM_HYPER_API_BASE_URL;
43
+ case "kimi-code":
44
+ return "https://api.kimi.com/coding/v1";
40
45
  case "meta":
41
46
  case "muse-code":
42
47
  return "https://api.meta.ai/v1";
@@ -71,11 +76,11 @@ export function resolveOllamaModelCacheProviderId(providerId: string, baseUrl?:
71
76
  }
72
77
 
73
78
  /**
74
- * The `sub` claim of a Cursor access token. Cursor access tokens are JWTs
75
- * that `refreshCursorToken` rotates (new `exp`/`iat`) for the same account,
79
+ * The `sub` claim of a JWT access token. Cursor and Kimi Code access tokens
80
+ * are JWTs that refresh rotates (new `exp`/`iat`/`jti`) for the same account,
76
81
  * so the subject is the stable account scope. Non-JWT keys return undefined.
77
82
  */
78
- function cursorCredentialSubject(apiKey: string): string | undefined {
83
+ function jwtCredentialSubject(apiKey: string): string | undefined {
79
84
  const parts = apiKey.split(".");
80
85
  if (parts.length !== 3 || !parts[1]) return undefined;
81
86
  try {
@@ -112,7 +117,7 @@ export function resolveModelCacheProviderId(providerId: string, options: ModelCa
112
117
  // mark the account default `isProviderDefault`, which startup now honors.
113
118
  const baseUrl = (options.baseUrl ?? CURSOR_DEFAULT_BASE_URL).replace(/\/+$/, "");
114
119
  const apiKey = options.apiKey ?? "";
115
- const scope = `${cursorCredentialSubject(apiKey) ?? apiKey}\u0000${baseUrl}`;
120
+ const scope = `${jwtCredentialSubject(apiKey) ?? apiKey}\u0000${baseUrl}`;
116
121
  return `cursor:rich-models-v5:${Bun.hash(scope).toString(36)}`;
117
122
  }
118
123
  case "charm-hyper": {
@@ -132,6 +137,17 @@ export function resolveModelCacheProviderId(providerId: string, options: ModelCa
132
137
  // `/v1`-suffixed one; both must land on one namespace.
133
138
  return `charm-hyper:models-v1:${Bun.hash(normalizeCharmHyperBaseUrl(options.baseUrl)).toString(36)}`;
134
139
  }
140
+ case "kimi-code": {
141
+ // The `/coding/v1/models` roster depends on the subscription plan behind
142
+ // the credential, and discovery is authoritative: a namespace without the
143
+ // credential would serve the previous plan's roster for the full TTL,
144
+ // including ids the current plan cannot call. OAuth access tokens rotate on
145
+ // refresh, so the scope is the token's account subject, not the token.
146
+ const baseUrl = options.baseUrl ?? getDefaultModelDiscoveryBaseUrl(providerId)!;
147
+ const apiKey = options.apiKey ?? "";
148
+ const scope = `${jwtCredentialSubject(apiKey) ?? apiKey}\u0000${baseUrl}`;
149
+ return `kimi-code:models-v1:${Bun.hash(scope).toString(36)}`;
150
+ }
135
151
  case "muse-code": {
136
152
  const baseUrl = options.baseUrl ?? getDefaultModelDiscoveryBaseUrl(providerId)!;
137
153
  const scope = `${options.apiKey ?? ""}\u0000${baseUrl}`;
@@ -160,7 +176,8 @@ export function resolveModelCacheProviderId(providerId: string, options: ModelCa
160
176
  }
161
177
  case "litellm": {
162
178
  const baseUrl = options.baseUrl ?? getDefaultModelDiscoveryBaseUrl(providerId)!;
163
- // rich-v13 moves `mode: decisions` groups off chat onto the Decisions runner.
179
+ // rich-v14 fetches every /v2/model/info page rather than caching only
180
+ // its first 50 models. rich-v13 moves `mode: decisions` groups off chat.
164
181
  // rich-v12 invalidates namespaced ids that missed bare catalog references.
165
182
  // rich-v11 excluded ClinePass gateway metadata (issue #10932). rich-v10
166
183
  // filtered known non-conversational LiteLLM modes, unioned compat across
@@ -168,7 +185,7 @@ export function resolveModelCacheProviderId(providerId: string, options: ModelCa
168
185
  // declaration into it; earlier versions invalidated rows whose
169
186
  // `compatConfig` retained a colliding bundled model's provider-specific
170
187
  // transport (e.g. Fireworks `wireModelIdMode`) (issue #9938).
171
- return `litellm:rich-v13:${Bun.hash(baseUrl).toString(36)}`;
188
+ return `litellm:rich-v14:${Bun.hash(baseUrl).toString(36)}`;
172
189
  }
173
190
  case "gmi-cloud":
174
191
  case "siliconflow":
@@ -4222,9 +4222,11 @@ export function kimiCodeModelManagerOptions(
4222
4222
  config?: KimiCodeModelManagerConfig,
4223
4223
  ): ModelManagerOptions<"openai-completions"> {
4224
4224
  const apiKey = config?.apiKey;
4225
- const baseUrl = config?.baseUrl ?? "https://api.kimi.com/coding/v1";
4225
+ const baseUrl = config?.baseUrl ?? getDefaultModelDiscoveryBaseUrl("kimi-code")!;
4226
4226
  return {
4227
4227
  providerId: "kimi-code",
4228
+ cacheProviderId: resolveModelCacheProviderId("kimi-code", { apiKey, baseUrl }),
4229
+ dynamicModelsAuthoritative: true,
4228
4230
  ...(apiKey && {
4229
4231
  fetchDynamicModels: () =>
4230
4232
  fetchOpenAICompatibleModels({
@@ -5679,7 +5681,7 @@ type LiteLLMRichEndpointModel<TApi extends Api> = {
5679
5681
  };
5680
5682
  type LiteLLMRichEndpointFailure = {
5681
5683
  endpoint: string;
5682
- reason: "http-status" | "invalid-json" | "network-error";
5684
+ reason: "http-status" | "invalid-json" | "invalid-pagination" | "network-error";
5683
5685
  status?: number;
5684
5686
  error?: unknown;
5685
5687
  };
@@ -5692,6 +5694,8 @@ type LiteLLMRichEndpointResult<TApi extends Api> =
5692
5694
  | { failure: LiteLLMRichEndpointFailure };
5693
5695
 
5694
5696
  const LITELLM_RICH_ENDPOINTS = ["/model_group/info", "/v2/model/info", "/model/info", "/v1/model/info"] as const;
5697
+ // Bound paginated management responses before trusting their advertised page count.
5698
+ const LITELLM_MAX_RICH_PAGES = 100;
5695
5699
  export const OPENAI_COMPAT_DISCOVERY_DEFAULT_CONTEXT_WINDOW = 128_000;
5696
5700
  export const OPENAI_COMPAT_DISCOVERY_DEFAULT_MAX_TOKENS = 32_768;
5697
5701
  const UNKNOWN_PROXY_COST = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 } as const;
@@ -6171,29 +6175,83 @@ async function fetchLiteLLMRichEndpoint<TApi extends Api>(
6171
6175
  if (options.apiKey) {
6172
6176
  requestHeaders.Authorization = `Bearer ${options.apiKey}`;
6173
6177
  }
6174
- let response: Response;
6175
- try {
6176
- response = await fetchImpl(`${managementBaseUrl}${endpoint}`, {
6177
- method: "GET",
6178
- headers: requestHeaders,
6179
- signal,
6180
- });
6181
- } catch (error) {
6182
- return { failure: { endpoint, reason: "network-error", error } };
6183
- }
6184
- if (!response.ok) {
6185
- return response.status === 404 ? null : { failure: { endpoint, reason: "http-status", status: response.status } };
6186
- }
6187
- let payload: unknown;
6188
- try {
6189
- payload = await response.json();
6190
- } catch (error) {
6191
- return { failure: { endpoint, reason: "invalid-json", status: response.status, error } };
6178
+ const fetchPage = async (
6179
+ page?: number,
6180
+ ): Promise<{ payload: unknown; status: number } | { failure: LiteLLMRichEndpointFailure } | null> => {
6181
+ let response: Response;
6182
+ try {
6183
+ response = await fetchImpl(`${managementBaseUrl}${endpoint}${page === undefined ? "" : `?page=${page}`}`, {
6184
+ method: "GET",
6185
+ headers: requestHeaders,
6186
+ signal,
6187
+ });
6188
+ } catch (error) {
6189
+ return { failure: { endpoint, reason: "network-error", error } };
6190
+ }
6191
+ if (!response.ok) {
6192
+ return response.status === 404 && page === undefined
6193
+ ? null
6194
+ : { failure: { endpoint, reason: "http-status", status: response.status } };
6195
+ }
6196
+ try {
6197
+ return { payload: await response.json(), status: response.status };
6198
+ } catch (error) {
6199
+ return { failure: { endpoint, reason: "invalid-json", status: response.status, error } };
6200
+ }
6201
+ };
6202
+ const firstPage = await fetchPage();
6203
+ if (!firstPage || "failure" in firstPage) {
6204
+ return firstPage;
6192
6205
  }
6193
- const entries = extractLiteLLMRichEntries(payload);
6206
+ const entries = extractLiteLLMRichEntries(firstPage.payload);
6194
6207
  if (!entries || entries.length === 0) {
6195
6208
  return null;
6196
6209
  }
6210
+ // /v2/model/info returns {current_page, total_pages, data}; older
6211
+ // endpoints and unpaginated /v2 proxies must keep their existing behavior.
6212
+ if (
6213
+ endpoint === "/v2/model/info" &&
6214
+ isRecord(firstPage.payload) &&
6215
+ ("current_page" in firstPage.payload || "total_pages" in firstPage.payload)
6216
+ ) {
6217
+ const { current_page: currentPage, total_pages: totalPages, total_count: totalCount } = firstPage.payload;
6218
+ const invalidPage = (status: number): LiteLLMRichEndpointResult<TApi> => ({
6219
+ failure: { endpoint, reason: "invalid-pagination", status },
6220
+ });
6221
+ if (
6222
+ currentPage !== 1 ||
6223
+ typeof totalPages !== "number" ||
6224
+ !Number.isSafeInteger(totalPages) ||
6225
+ totalPages < 1 ||
6226
+ totalPages > LITELLM_MAX_RICH_PAGES ||
6227
+ (totalCount !== undefined &&
6228
+ (typeof totalCount !== "number" || !Number.isSafeInteger(totalCount) || totalCount < entries.length))
6229
+ ) {
6230
+ return invalidPage(firstPage.status);
6231
+ }
6232
+ const remainingPages = await Promise.all(
6233
+ Array.from({ length: totalPages - 1 }, (_, index) => fetchPage(index + 2)),
6234
+ );
6235
+ for (const [index, nextPage] of remainingPages.entries()) {
6236
+ if (!nextPage || "failure" in nextPage) {
6237
+ return nextPage ?? invalidPage(firstPage.status);
6238
+ }
6239
+ const nextEntries = extractLiteLLMRichEntries(nextPage.payload);
6240
+ if (
6241
+ !isRecord(nextPage.payload) ||
6242
+ nextPage.payload.current_page !== index + 2 ||
6243
+ nextPage.payload.total_pages !== totalPages ||
6244
+ (totalCount !== undefined && nextPage.payload.total_count !== totalCount) ||
6245
+ !nextEntries?.length
6246
+ ) {
6247
+ return invalidPage(nextPage.status);
6248
+ }
6249
+ entries.push(...nextEntries);
6250
+ }
6251
+ if (totalCount !== undefined && entries.length !== totalCount) {
6252
+ return invalidPage(firstPage.status);
6253
+ }
6254
+ }
6197
6255
  const deduped = new Map<string, LiteLLMRichEndpointModel<TApi>>();
6198
6256
  const excludedModelIds = new Set<string>();
6199
6257
  for (const entry of entries) {
@@ -6350,8 +6408,10 @@ export function litellmModelManagerOptions(config?: LiteLLMModelManagerConfig):
6350
6408
  const baseUrl = config?.baseUrl ?? getDefaultModelDiscoveryBaseUrl("litellm")!;
6351
6409
  return {
6352
6410
  providerId: "litellm",
6353
- // rich-v13 routes `mode: decisions` groups to the Decisions runner as judge
6354
- // rows. rich-v12 invalidates namespaced proxy ids that missed bare catalog
6411
+ // rich-v14 fetches all /v2/model/info pages so cached proxy rosters
6412
+ // include models beyond page one. rich-v13 routes `mode: decisions`
6413
+ // groups to the Decisions runner as judge rows.
6414
+ // rich-v12 invalidates namespaced proxy ids that missed bare catalog
6355
6415
  // references. rich-v11 excluded ClinePass gateway metadata (issue #10932).
6356
6416
  // rich-v10 filtered known non-conversational LiteLLM modes, keyed the
6357
6417
  // deployment's `supports_vision` declaration into cached compat, and
@@ -94,11 +94,10 @@ export function openaiCodexModelManagerOptions(
94
94
  * {@link unionAccountCatalogs}.
95
95
  *
96
96
  * Returns `null` when any account's fetch failed transiently, so a partial list
97
- * cannot replace the previous or bundled authoritative catalog. An account
98
- * whose credential the backend rejected outright (401/403 — revoked or
99
- * deauthorized) contributes nothing and is skipped instead: it would otherwise
100
- * veto every sibling account's models until the user removes it. If no account
101
- * produced a catalog, discovery aborts the same way.
97
+ * cannot replace the previous or bundled authoritative catalog. A definitive
98
+ * account denial (401/403 auth or 402 payment) contributes nothing and is skipped
99
+ * instead: it would otherwise veto every sibling account's models until the
100
+ * account recovers. If no account produced a catalog, discovery aborts.
102
101
  */
103
102
  function unionCodexModels(
104
103
  results: readonly { accountId: string | undefined; result: CodexModelDiscoveryResult | null }[],
@@ -107,7 +106,7 @@ function unionCodexModels(
107
106
  for (const { accountId, result } of results) {
108
107
  if (!result) return null;
109
108
  if (result.rejectedStatus !== undefined) {
110
- logger.warn("Codex model discovery skipped an account whose credential was rejected", {
109
+ logger.warn("Codex model discovery skipped an account denied by the backend", {
111
110
  accountId,
112
111
  status: result.rejectedStatus,
113
112
  });
@@ -49,7 +49,7 @@ export declare const OPENAI_HEADER_VALUES: {
49
49
  readonly BETA_RESPONSES: "responses=experimental";
50
50
  readonly BETA_RESPONSES_WEBSOCKETS_V2: "responses_websockets=2026-02-06";
51
51
  readonly REMOTE_COMPACTION_V2: "remote_compaction_v2";
52
- readonly ORIGINATOR_CODEX: "zeta";
52
+ readonly ORIGINATOR_CODEX: "omp";
53
53
  };
54
54
  export declare const URL_PATHS: {
55
55
  readonly RESPONSES: "/responses";
package/src/wire/codex.ts CHANGED
@@ -68,7 +68,7 @@ export const OPENAI_HEADER_VALUES = {
68
68
  BETA_RESPONSES: "responses=experimental",
69
69
  BETA_RESPONSES_WEBSOCKETS_V2: "responses_websockets=2026-02-06",
70
70
  REMOTE_COMPACTION_V2: "remote_compaction_v2",
71
- ORIGINATOR_CODEX: "zeta",
71
+ ORIGINATOR_CODEX: "omp",
72
72
  } as const;
73
73
 
74
74
  export const URL_PATHS = {
@@ -1,9 +0,0 @@
1
- import type { Api } from "./types";
2
-
3
- /** Typed declaration for the generated models.json — only the `api` field matters for type inference. */
4
- declare const models: {
5
- [provider: string]: {
6
- [modelId: string]: { readonly api: Api; [key: string]: unknown };
7
- };
8
- };
9
- export default models;