@linxiraos/pi-catalog 1.4.7 → 1.4.10

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 (83) hide show
  1. package/CHANGELOG.md +6 -21
  2. package/dist/types/compat/auth-ids.d.ts +2 -2
  3. package/dist/types/compat/catalog-policy.d.ts +6 -4
  4. package/dist/types/compat/factory-droid.d.ts +7 -1
  5. package/dist/types/compat/provider-ids.d.ts +1 -1
  6. package/dist/types/compat/resolve.d.ts +1 -1
  7. package/dist/types/compat/revision.d.ts +2 -0
  8. package/dist/types/compat/server-side-fallback.d.ts +6 -4
  9. package/dist/types/compat/types.d.ts +3 -1
  10. package/dist/types/discovery/cursor-proto.d.ts +8 -0
  11. package/dist/types/discovery/openai-compatible.d.ts +6 -0
  12. package/dist/types/model-manager.d.ts +11 -1
  13. package/dist/types/model-thinking.d.ts +4 -0
  14. package/dist/types/models.d.ts +16 -0
  15. package/dist/types/pricing.d.ts +66 -6
  16. package/dist/types/provider-models/openai-compat.d.ts +18 -13
  17. package/dist/types/types.d.ts +14 -19
  18. package/dist/types/utils.d.ts +8 -0
  19. package/package.json +4 -4
  20. package/src/compat/auth-ids.ts +5 -0
  21. package/src/compat/axes.ts +21 -9
  22. package/src/compat/catalog-policy.d.ts +9 -0
  23. package/src/compat/catalog-policy.ts +14 -10
  24. package/src/compat/collapse.ts +1 -1
  25. package/src/compat/factory-droid.d.ts +7 -1
  26. package/src/compat/factory-droid.ts +9 -1
  27. package/src/compat/provider-ids.d.ts +1 -1
  28. package/src/compat/provider-ids.ts +2 -0
  29. package/src/compat/resolve.d.ts +1 -1
  30. package/src/compat/resolve.ts +4 -5
  31. package/src/compat/revision.d.ts +2 -0
  32. package/src/compat/revision.ts +10 -0
  33. package/src/compat/rules/README.md +3 -1
  34. package/src/compat/rules/auth/_order.kdl +1 -1
  35. package/src/compat/rules/auth/mistral.kdl +4 -0
  36. package/src/compat/rules/auth/modular.kdl +9 -0
  37. package/src/compat/rules/auth/scaleway.kdl +17 -0
  38. package/src/compat/rules/classes/anthropic.kdl +5 -0
  39. package/src/compat/rules/classes/deepseek.kdl +1 -1
  40. package/src/compat/rules/classes/gemini.kdl +1 -1
  41. package/src/compat/rules/classes/xai.kdl +1 -1
  42. package/src/compat/rules/providers/anthropic.kdl +8 -4
  43. package/src/compat/rules/providers/cursor.kdl +10 -0
  44. package/src/compat/rules/providers/deepseek.kdl +2 -2
  45. package/src/compat/rules/providers/factory-droid.kdl +219 -13
  46. package/src/compat/rules/providers/modular.kdl +35 -0
  47. package/src/compat/rules/providers/openai-codex.kdl +1 -1
  48. package/src/compat/rules/providers/sambanova.kdl +2 -0
  49. package/src/compat/rules/providers/scaleway.kdl +179 -0
  50. package/src/compat/rules/runtime/behavior.kdl +13 -0
  51. package/src/compat/rules.json +1 -1
  52. package/src/compat/server-side-fallback.d.ts +9 -0
  53. package/src/compat/server-side-fallback.ts +7 -5
  54. package/src/compat/types.d.ts +3 -1
  55. package/src/compat/types.ts +3 -1
  56. package/src/discovery/codex.ts +2 -9
  57. package/src/discovery/cursor-proto.d.ts +8 -0
  58. package/src/discovery/cursor-proto.ts +12 -1
  59. package/src/discovery/factory-droid.ts +35 -11
  60. package/src/discovery/openai-compatible.d.ts +6 -0
  61. package/src/discovery/openai-compatible.ts +1 -1
  62. package/src/model-cache.ts +32 -58
  63. package/src/model-catalog-format.ts +5 -8
  64. package/src/model-manager.d.ts +11 -1
  65. package/src/model-manager.ts +85 -143
  66. package/src/model-thinking.d.ts +4 -0
  67. package/src/model-thinking.ts +10 -0
  68. package/src/model-tokenizer.ts +13 -12
  69. package/src/models.d.ts +16 -0
  70. package/src/models.json +1 -1
  71. package/src/models.ts +32 -1
  72. package/src/pricing.d.ts +66 -6
  73. package/src/pricing.ts +59 -94
  74. package/src/provider-models/catalog-snapshot.json.zst +0 -0
  75. package/src/provider-models/catalog-snapshot.json.zst.d.ts +3 -0
  76. package/src/provider-models/descriptors.ts +2 -0
  77. package/src/provider-models/openai-compat.d.ts +18 -13
  78. package/src/provider-models/openai-compat.ts +120 -71
  79. package/src/types.d.ts +14 -19
  80. package/src/types.ts +11 -18
  81. package/src/utils.d.ts +8 -0
  82. package/src/utils.ts +18 -0
  83. package/src/wire/factory-droid.ts +4 -3
@@ -1,19 +1,12 @@
1
+ import { type } from "@linxiraos/pi-omptype";
1
2
  import { logger, VERSION } from "@linxiraos/pi-utils";
2
3
  import { buildModel } from "./build";
3
4
  import { collapseBuiltVariants } from "./compat/collapse";
4
5
  import { applyCatalogMetrics, CatalogMetricsIndex } from "./identity/metrics";
5
6
  import { readModelCache, writeModelCache } from "./model-cache";
6
7
  import { type GeneratedProvider, getBundledModels } from "./models";
7
- import { isTimeBasedCost } from "./pricing";
8
- import {
9
- type Api,
10
- type Model,
11
- type ModelCost,
12
- modelKind,
13
- type ModelSpec,
14
- type Provider,
15
- type TokenCost,
16
- } from "./types";
8
+ import { TimeBasedCostShape } from "./pricing";
9
+ import { type Api, type Model, modelKind, type ModelSpec, type Provider } from "./types";
17
10
  import { isRecord } from "./utils";
18
11
 
19
12
  const DEFAULT_CACHE_TTL_MS = 2 * 60 * 60 * 1000;
@@ -267,7 +260,6 @@ export async function resolveProviderModels<TApi extends Api = Api, TModelsDevPa
267
260
  const cache = readModelCache<TApi>(cacheProviderId, ttlMs, now, dbPath);
268
261
  const dynamicModelsAuthoritative = options.dynamicModelsAuthoritative ?? false;
269
262
  const isAuthoritativeDynamicCache = dynamicModelsAuthoritative && (cache?.payloadAuthoritative ?? false);
270
- const cacheAdditiveStaticModelIds = isAuthoritativeDynamicCache ? undefined : modelsDevAdditiveStaticModelIds;
271
263
  const restoredCache = restoreCachedModelHeaders(
272
264
  cache?.models ?? [],
273
265
  staticModels,
@@ -286,7 +278,12 @@ export async function resolveProviderModels<TApi extends Api = Api, TModelsDevPa
286
278
  staticNames: options.keepStaticNames ? new Map(staticModels.map(model => [model.id, model.name])) : undefined,
287
279
  };
288
280
  const cacheDropIds = options.dropCachedModelIdsOnStaticMismatch;
289
- const staticCatalogFingerprint = fingerprintStaticModels(staticModels, dynamicModelsAuthoritative);
281
+ // Additive mode is cache identity: a snapshot written while shared-catalog
282
+ // rows could replace bundled metadata must not match an additive resolver.
283
+ const staticCatalogFingerprint = fingerprintStaticModels(staticModels, {
284
+ authoritative: dynamicModelsAuthoritative,
285
+ additive: modelsDevAdditiveStaticModelIds !== undefined,
286
+ });
290
287
  // Endpoint-migration policy is cache identity: adding an id must invalidate
291
288
  // matching-static-catalog caches written by the prior resolver.
292
289
  const staticFingerprint =
@@ -294,6 +291,13 @@ export async function resolveProviderModels<TApi extends Api = Api, TModelsDevPa
294
291
  ? `${staticCatalogFingerprint}:drop:${Bun.hash(cacheDropIds.join("\0")).toString(36)}`
295
292
  : staticCatalogFingerprint;
296
293
  const cacheFingerprintMatches = cache?.staticFingerprint === staticFingerprint && staticFingerprint.length > 0;
294
+ // A matching cache was written against this static catalog under the same
295
+ // additive policy, so its same-id rows are live discovery merged over the
296
+ // bundled rows, never shared-catalog overrides (#17481). Strip them only on
297
+ // a mismatch: a static-catalog upgrade or a cache written before additive
298
+ // semantics were enabled.
299
+ const cacheAdditiveStaticModelIds =
300
+ isAuthoritativeDynamicCache || cacheFingerprintMatches ? undefined : modelsDevAdditiveStaticModelIds;
297
301
  const cacheNeedsModelMigration =
298
302
  !cacheFingerprintMatches &&
299
303
  cacheDropIds !== undefined &&
@@ -315,8 +319,8 @@ export async function resolveProviderModels<TApi extends Api = Api, TModelsDevPa
315
319
  // Cold-start fast path: when a fresh, authoritative cache exists, the network
316
320
  // fetch is skipped, AND the static catalog slice is byte-identical to what
317
321
  // was merged in last time, the cache row IS the authoritative merge result.
318
- // Additive caches still need same-id rows stripped because an older binary
319
- // may have written the snapshot before additive semantics were enabled.
322
+ // A mismatched additive cache never reaches here; its same-id rows are
323
+ // stripped below.
320
324
  if (
321
325
  !shouldFetchFromNetwork &&
322
326
  cache?.fresh &&
@@ -324,18 +328,9 @@ export async function resolveProviderModels<TApi extends Api = Api, TModelsDevPa
324
328
  cacheFingerprintMatches &&
325
329
  !cacheHasUnresolvedHeaders
326
330
  ) {
327
- const cacheContribution = cacheAdditiveStaticModelIds
328
- ? restoredCache.models.filter(model => !cacheAdditiveStaticModelIds.has(model.id))
329
- : restoredCache.models;
330
- const cachedModels = cacheAdditiveStaticModelIds
331
- ? mergeCatalogMetrics(
332
- mergeDynamicModels(staticModels, cacheContribution, mergeAuthority),
333
- restoredCache.models,
334
- )
335
- : restoredCache.models;
336
- const source: ModelResolutionSource = cacheContribution.length > 0 ? "cache" : "bundled";
331
+ const source: ModelResolutionSource = restoredCache.models.length > 0 ? "cache" : "bundled";
337
332
  return {
338
- models: collapseBuiltVariants(cachedModels),
333
+ models: collapseBuiltVariants(restoredCache.models),
339
334
  stale: false,
340
335
  source,
341
336
  ...(source === "cache" ? { updatedAt: cache.updatedAt } : {}),
@@ -382,7 +377,10 @@ export async function resolveProviderModels<TApi extends Api = Api, TModelsDevPa
382
377
  dynamicModels.length > 0 &&
383
378
  (dynamicModelsAuthoritative || !hasModelsDevFetcher || modelsDevFetchSucceeded)
384
379
  : modelsDevFetchSucceeded;
385
- const mergedWithCache = mergeDynamicModels(staticModels, cacheModels, mergeAuthority);
380
+ const mergedWithCache = mergeCachedModels(staticModels, cacheModels, {
381
+ authority: mergeAuthority,
382
+ matchesStatic: cacheFingerprintMatches,
383
+ });
386
384
  const mergedWithModelsDev = mergeDynamicModels(
387
385
  mergedWithCache,
388
386
  modelsDevModels,
@@ -455,7 +453,10 @@ export async function resolveProviderModels<TApi extends Api = Api, TModelsDevPa
455
453
  ? preparedLatestCacheModels.filter(model => !cacheAdditiveStaticModelIds.has(model.id))
456
454
  : preparedLatestCacheModels;
457
455
  const mergedFallbackModels = mergeDynamicModels(
458
- mergeDynamicModels(staticModels, latestCacheModels, mergeAuthority),
456
+ mergeCachedModels(staticModels, latestCacheModels, {
457
+ authority: mergeAuthority,
458
+ matchesStatic: cacheFingerprintMatches,
459
+ }),
459
460
  modelsDevModels,
460
461
  mergeAuthority,
461
462
  );
@@ -631,6 +632,24 @@ function mergeDynamicModels<TApi extends Api>(
631
632
  return Array.from(merged.values());
632
633
  }
633
634
 
635
+ /**
636
+ * Lays cached rows over the static catalog. A snapshot written against this
637
+ * static catalog (`matchesStatic`) is already that catalog merged with live
638
+ * discovery, so its rows replace same-id static rows verbatim; re-merging would
639
+ * only rebuild them and churn the persisted payload. A snapshot from another
640
+ * static catalog re-merges so upgraded bundled metadata applies.
641
+ */
642
+ function mergeCachedModels<TApi extends Api>(
643
+ staticModels: readonly Model<TApi>[],
644
+ cacheModels: readonly Model<TApi>[],
645
+ options: { authority: DynamicMergeAuthority; matchesStatic: boolean },
646
+ ): Model<TApi>[] {
647
+ if (!options.matchesStatic) return mergeDynamicModels(staticModels, cacheModels, options.authority);
648
+ const merged = new Map<string, Model<TApi>>(staticModels.map(model => [model.id, model]));
649
+ for (const cachedModel of cacheModels) merged.set(cachedModel.id, cachedModel);
650
+ return Array.from(merged.values());
651
+ }
652
+
634
653
  function retainModelIds<TApi extends Api>(
635
654
  models: readonly Model<TApi>[],
636
655
  retainedModels: readonly Model<TApi>[],
@@ -642,6 +661,17 @@ function retainModelIds<TApi extends Api>(
642
661
 
643
662
  const MODEL_CACHE_FINGERPRINT_VERSION = `merge-v5:${VERSION}`;
644
663
 
664
+ /**
665
+ * Merge policy that is part of a cache's identity in
666
+ * {@link fingerprintStaticModels}: `authoritative` when dynamic discovery owns
667
+ * the catalog, `additive` when the shared catalog may only add ids. The flags
668
+ * compose; snapshots written under different policies never match.
669
+ */
670
+ export interface StaticFingerprintPolicy {
671
+ authoritative?: boolean;
672
+ additive?: boolean;
673
+ }
674
+
645
675
  /**
646
676
  * Return the versioned content identity for a static provider slice. Callers
647
677
  * may mutate or freeze their arrays, so every call observes current content
@@ -649,12 +679,11 @@ const MODEL_CACHE_FINGERPRINT_VERSION = `merge-v5:${VERSION}`;
649
679
  */
650
680
  export function fingerprintStaticModels<TApi extends Api>(
651
681
  models: readonly (ModelSpec<TApi> | Model<TApi>)[],
652
- dynamicModelsAuthoritative = false,
682
+ policy: StaticFingerprintPolicy = {},
653
683
  ): string {
654
- if (models.length === 0) return `${MODEL_CACHE_FINGERPRINT_VERSION}:empty`;
655
- if (dynamicModelsAuthoritative)
656
- return `${MODEL_CACHE_FINGERPRINT_VERSION}:authoritative:${fingerprintStaticModels(models)}`;
657
- return `${MODEL_CACHE_FINGERPRINT_VERSION}:${Bun.hash(JSON.stringify(models)).toString(36)}`;
684
+ const policyKey = `${policy.authoritative ? ":authoritative" : ""}${policy.additive ? ":additive" : ""}`;
685
+ if (models.length === 0) return `${MODEL_CACHE_FINGERPRINT_VERSION}${policyKey}:empty`;
686
+ return `${MODEL_CACHE_FINGERPRINT_VERSION}${policyKey}:${Bun.hash(JSON.stringify(models)).toString(36)}`;
658
687
  }
659
688
 
660
689
  function mergeDynamicModel<TApi extends Api>(
@@ -782,18 +811,19 @@ function preferDiscoveryLimit(discoveryLimit: number | null, fallbackLimit: numb
782
811
  * caller treats the source as failed (cache/static fallback) rather than as an
783
812
  * intentionally empty catalog that an authoritative provider would prune to.
784
813
  */
785
- function buildDiscoveredModelSpecs<TApi extends Api>(value: unknown): DiscoveredModelSet<TApi> {
814
+ function buildDiscoveredModelSpecs<TApi extends Api>(values: unknown): DiscoveredModelSet<TApi> {
786
815
  const models: Model<TApi>[] = [];
787
816
  const explicitKindModels = new Set<Model<TApi>>();
788
- if (!Array.isArray(value)) {
817
+ if (!Array.isArray(values)) {
789
818
  return { models, explicitKindModels };
790
819
  }
791
820
  const failures: unknown[] = [];
792
- for (const item of value) {
793
- if (!isModelLike(item)) continue;
821
+ for (const value of values) {
822
+ if (!ModelSpecShape.allows(value)) continue;
823
+ const item = value as ModelSpec<TApi>;
794
824
  let model: Model<TApi>;
795
825
  try {
796
- model = buildModel(item as ModelSpec<TApi>);
826
+ model = buildModel(item);
797
827
  } catch (error) {
798
828
  failures.push(error);
799
829
  logger.warn("model discovery skipped a model that failed to build", {
@@ -810,109 +840,21 @@ function buildDiscoveredModelSpecs<TApi extends Api>(value: unknown): Discovered
810
840
  return { models, explicitKindModels };
811
841
  }
812
842
 
813
- function isModelLike(value: unknown): value is ModelSpec<Api> {
814
- if (!isRecord(value)) {
815
- return false;
816
- }
817
- const v = value as {
818
- id?: unknown;
819
- name?: unknown;
820
- api?: unknown;
821
- provider?: unknown;
822
- baseUrl?: unknown;
823
- reasoning?: unknown;
824
- input?: unknown;
825
- cost?: unknown;
826
- contextWindow?: unknown;
827
- maxTokens?: unknown;
828
- };
829
- if (typeof v.id !== "string" || v.id.length === 0) {
830
- return false;
831
- }
832
- if (typeof v.name !== "string" || v.name.length === 0) {
833
- return false;
834
- }
835
- if (typeof v.api !== "string" || v.api.length === 0) {
836
- return false;
837
- }
838
- if (typeof v.provider !== "string" || v.provider.length === 0) {
839
- return false;
840
- }
841
- if (typeof v.baseUrl !== "string" || v.baseUrl.length === 0) {
842
- return false;
843
- }
844
- if (typeof v.reasoning !== "boolean") {
845
- return false;
846
- }
847
- if (!isModelInputArray(v.input)) {
848
- return false;
849
- }
850
- if (!isModelCost(v.cost)) {
851
- return false;
852
- }
853
- // Finite positive: NaN > 0 is false, +Infinity < Infinity is false.
854
- const cw = v.contextWindow;
855
- if (cw !== null && (typeof cw !== "number" || !(cw > 0 && cw < Infinity))) {
856
- return false;
857
- }
858
- const mt = v.maxTokens;
859
- if (mt !== null && (typeof mt !== "number" || !(mt > 0 && mt < Infinity))) {
860
- return false;
861
- }
862
- return true;
863
- }
864
-
865
- function isModelInputArray(value: unknown): value is ("text" | "image")[] {
866
- if (!Array.isArray(value) || value.length === 0) {
867
- return false;
868
- }
869
- for (let i = 0; i < value.length; i++) {
870
- const item = value[i];
871
- if (item !== "text" && item !== "image") {
872
- return false;
873
- }
874
- }
875
- return true;
876
- }
877
-
878
- function isTokenCost(value: unknown): value is TokenCost {
879
- if (!isRecord(value)) {
880
- return false;
881
- }
882
- const c = value as {
883
- input?: unknown;
884
- output?: unknown;
885
- cacheRead?: unknown;
886
- cacheWrite?: unknown;
887
- };
888
- // Finite (NaN-safe): -Infinity < x < Infinity rejects NaN and both infinities.
889
- // Preserves original behavior: 0 and negatives remain valid.
890
- const ci = c.input;
891
- if (typeof ci !== "number" || !(ci > -Infinity && ci < Infinity)) {
892
- return false;
893
- }
894
- const co = c.output;
895
- if (typeof co !== "number" || !(co > -Infinity && co < Infinity)) {
896
- return false;
897
- }
898
- const cr = c.cacheRead;
899
- if (typeof cr !== "number" || !(cr > -Infinity && cr < Infinity)) {
900
- return false;
901
- }
902
- const cw = c.cacheWrite;
903
- if (typeof cw !== "number" || !(cw > -Infinity && cw < Infinity)) {
904
- return false;
905
- }
906
- return true;
907
- }
908
-
909
- function isModelCost(value: unknown): value is ModelCost {
910
- if (!isTokenCost(value)) return false;
911
- const cost = value as TokenCost & { longContext?: unknown; timeBased?: unknown };
912
- if (cost.timeBased !== undefined && !isTimeBasedCost(cost.timeBased)) return false;
913
- const longContext = cost.longContext;
914
- if (longContext === undefined) return true;
915
- if (!isTokenCost(longContext) || !isRecord(longContext)) return false;
916
- const threshold = longContext.inputThreshold;
917
- return typeof threshold === "number" && threshold > 0 && threshold < Infinity;
918
- }
843
+ /** Finite rates; 0 and negatives stay valid. */
844
+ const FiniteTokenCost = type({ input: "number", output: "number", cacheRead: "number", cacheWrite: "number" });
845
+ /** Discovery output is built in memory, so optional fields may be present as `undefined`. */
846
+ const ModelSpecShape = type({
847
+ id: "string > 0",
848
+ name: "string > 0",
849
+ api: "string > 0",
850
+ provider: "string > 0",
851
+ baseUrl: "string > 0",
852
+ reasoning: "boolean",
853
+ input: "('text' | 'image')[] > 0",
854
+ cost: FiniteTokenCost.and({
855
+ "timeBased?": TimeBasedCostShape.or("undefined"),
856
+ "longContext?": FiniteTokenCost.and({ inputThreshold: "number > 0" }).or("undefined"),
857
+ }),
858
+ contextWindow: "number > 0 | null",
859
+ maxTokens: "number > 0 | null",
860
+ });
@@ -32,6 +32,10 @@ export declare function withoutUnverifiedThinking<T extends {
32
32
  /**
33
33
  * Clamps a requested thinking level against explicit model metadata.
34
34
  *
35
+ * An unserved level first follows `thinking.effortMap` when the map sends it to
36
+ * a served tier that reaches the wire unchanged (a gateway answering `medium`
37
+ * as `high`); otherwise it climbs down to the nearest lower tier.
38
+ *
35
39
  * Non-reasoning models always resolve to `undefined`.
36
40
  */
37
41
  export declare function clampThinkingLevelForModel<TApi extends Api>(model: ApiModel<TApi> | undefined, requested: Effort | undefined): Effort | undefined;
@@ -42,6 +42,10 @@ export function withoutUnverifiedThinking<T extends { reasoning: boolean; thinki
42
42
  /**
43
43
  * Clamps a requested thinking level against explicit model metadata.
44
44
  *
45
+ * An unserved level first follows `thinking.effortMap` when the map sends it to
46
+ * a served tier that reaches the wire unchanged (a gateway answering `medium`
47
+ * as `high`); otherwise it climbs down to the nearest lower tier.
48
+ *
45
49
  * Non-reasoning models always resolve to `undefined`.
46
50
  */
47
51
  export function clampThinkingLevelForModel<TApi extends Api>(
@@ -60,6 +64,12 @@ export function clampThinkingLevelForModel<TApi extends Api>(
60
64
  return requested;
61
65
  }
62
66
 
67
+ const effortMap = model.thinking?.effortMap;
68
+ const aliased = effortMap?.[requested] as Effort | undefined;
69
+ if (aliased !== undefined && levels.includes(aliased) && (effortMap![aliased] ?? aliased) === aliased) {
70
+ return aliased;
71
+ }
72
+
63
73
  const requestedIndex = THINKING_EFFORTS.indexOf(requested);
64
74
  if (requestedIndex === -1) {
65
75
  return undefined;
@@ -1,4 +1,4 @@
1
- import { compareRevision, parseRevision } from "./compat/revision";
1
+ import { revisionAtLeast } from "./compat/revision";
2
2
  import { classifyModel } from "./compat/taxonomy";
3
3
  import type { ModelIdentity } from "./compat/types";
4
4
  import { bareModelId } from "./identity/id";
@@ -8,15 +8,6 @@ import type { ModelTokenizer } from "./types";
8
8
  const MAX_TOKENIZER_CACHE_ENTRIES = 2_048;
9
9
  const modelTokenizerCache = new Map<string, ModelTokenizer | null>();
10
10
 
11
- function revisionAtLeast(revision: string | undefined, floor: string): boolean {
12
- if (revision === undefined) return false;
13
- const parsedRevision = parseRevision(revision);
14
- const parsedFloor = parseRevision(floor);
15
- return (
16
- parsedRevision !== undefined && parsedFloor !== undefined && compareRevision(parsedRevision, parsedFloor) >= 0
17
- );
18
- }
19
-
20
11
  function claudeTokenizer(identity: ModelIdentity): ModelTokenizer | undefined {
21
12
  if (identity.class !== "anthropic") return undefined;
22
13
  if (identity.family === "opus") {
@@ -49,9 +40,19 @@ function glmTokenizer(identity: ModelIdentity): ModelTokenizer | undefined {
49
40
  return identity.class === "glm" && revisionAtLeast(identity.revision, "5") ? "glm5" : undefined;
50
41
  }
51
42
 
52
- // GPT-5 and later bill text in o200k_base; older generations (cl100k GPT-4, unparsed gpt-4o) keep the estimate.
43
+ // GPT-5 and later bill text in o200k_base, GPT-3.5 and GPT-4 (Turbo) in cl100k_base. GPT-4.1, GPT-4.5, gpt-4o and the
44
+ // o-series are o200k too, which is the default for a model without a tokenizer.
53
45
  function openAITokenizer(identity: ModelIdentity): ModelTokenizer | undefined {
54
- return identity.class === "openai" && revisionAtLeast(identity.revision, "5") ? "o200k" : undefined;
46
+ if (identity.class !== "openai") return undefined;
47
+ if (revisionAtLeast(identity.revision, "5")) return "o200k";
48
+ if (
49
+ identity.family === "gpt" &&
50
+ revisionAtLeast(identity.revision, "3.5") &&
51
+ !revisionAtLeast(identity.revision, "4.1")
52
+ ) {
53
+ return "cl100k";
54
+ }
55
+ return undefined;
55
56
  }
56
57
 
57
58
  /**
package/src/models.d.ts CHANGED
@@ -11,6 +11,22 @@ export declare function getBundledProviders(): readonly KnownProvider[];
11
11
  export declare function getBundledModels(provider: GeneratedProvider): Model<Api>[];
12
12
  /** Resolve display semantics without mistaking an absent rate card for a free model. */
13
13
  export declare function getModelPricingStatus(model: Pick<Model<Api>, "cost" | "pricingStatus">): ModelPricingStatus;
14
+ /** Factory Droid credit rate in force at a time, and the next dated change to it. */
15
+ export interface FactoryDroidCreditRate {
16
+ rate: number;
17
+ /** The earliest later `credit-rates` card that changes `rate`: the rate it sets and when (Unix ms). */
18
+ next?: {
19
+ rate: number;
20
+ at: number;
21
+ };
22
+ }
23
+ /**
24
+ * Factory Droid Standard Credits rate at `timestamp` (Unix ms, default now):
25
+ * the `input` of the `credit-rates` card due then (the latest at or before
26
+ * it, as for cost cards), else the base rate. Undefined for models without a
27
+ * credit rate.
28
+ */
29
+ export declare function getFactoryDroidCreditRate(model: Pick<Model, "factoryDroidCredits" | "factoryDroidCreditCards">, timestamp?: number): FactoryDroidCreditRate | undefined;
14
30
  /**
15
31
  * The base token card in force at `timestamp` (Unix ms, default now): the
16
32
  * latest due `timeBased` effective rate, else the row's own legs. Price