@oh-my-pi/pi-catalog 18.14.2 → 18.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/dist/types/compat/auth-ids.d.ts +2 -2
- package/dist/types/compat/catalog-policy.d.ts +6 -4
- package/dist/types/compat/factory-droid.d.ts +7 -1
- package/dist/types/compat/provider-ids.d.ts +1 -1
- package/dist/types/compat/resolve.d.ts +1 -1
- package/dist/types/compat/revision.d.ts +2 -0
- package/dist/types/compat/server-side-fallback.d.ts +6 -4
- package/dist/types/compat/types.d.ts +3 -1
- package/dist/types/discovery/openai-compatible.d.ts +6 -0
- package/dist/types/models.d.ts +16 -0
- package/dist/types/pricing.d.ts +66 -6
- package/dist/types/provider-models/openai-compat.d.ts +18 -0
- package/dist/types/types.d.ts +11 -5
- package/dist/types/utils.d.ts +8 -0
- package/package.json +4 -4
- package/src/compat/auth-ids.ts +5 -0
- package/src/compat/axes.ts +5 -2
- package/src/compat/catalog-policy.ts +14 -10
- package/src/compat/collapse.ts +1 -1
- package/src/compat/factory-droid.ts +9 -1
- package/src/compat/provider-ids.ts +2 -0
- package/src/compat/resolve.ts +4 -2
- package/src/compat/revision.ts +10 -0
- package/src/compat/rules/README.md +2 -0
- package/src/compat/rules/auth/_order.kdl +1 -1
- package/src/compat/rules/auth/mistral.kdl +4 -0
- package/src/compat/rules/auth/modular.kdl +9 -0
- package/src/compat/rules/auth/scaleway.kdl +17 -0
- package/src/compat/rules/providers/factory-droid.kdl +219 -13
- package/src/compat/rules/providers/modular.kdl +35 -0
- package/src/compat/rules/providers/sambanova.kdl +2 -0
- package/src/compat/rules/providers/scaleway.kdl +179 -0
- package/src/compat/rules/runtime/behavior.kdl +13 -0
- package/src/compat/rules.json +1 -1
- package/src/compat/server-side-fallback.ts +7 -5
- package/src/compat/types.ts +3 -1
- package/src/discovery/codex.ts +2 -9
- package/src/discovery/factory-droid.ts +35 -11
- package/src/discovery/openai-compatible.ts +1 -1
- package/src/model-cache.ts +32 -58
- package/src/model-catalog-format.ts +5 -8
- package/src/model-manager.ts +27 -121
- package/src/model-tokenizer.ts +13 -12
- package/src/models.json +1 -1
- package/src/models.ts +32 -1
- package/src/pricing.ts +59 -94
- package/src/provider-models/descriptors.ts +2 -0
- package/src/provider-models/openai-compat.ts +60 -59
- package/src/types.ts +11 -4
- package/src/utils.ts +18 -0
- package/src/wire/factory-droid.ts +4 -3
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CatalogPolicyInputs } from "../types";
|
|
2
2
|
import { resolveCatalogPolicy } from "./catalog-policy";
|
|
3
|
+
import type { ResolveRoute } from "./resolve";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Ordered model ids for Anthropic's server-side refusal `fallbacks` chain,
|
|
6
|
-
* authored on the `server-side-fallback-models` catalog axis
|
|
7
|
-
*
|
|
7
|
+
* authored on the `server-side-fallback-models` catalog axis, on `route`'s
|
|
8
|
+
* upstream when it names one. Empty when no rule assigns a chain (the model,
|
|
9
|
+
* host or upstream does not support the feature).
|
|
8
10
|
*/
|
|
9
|
-
export function serverSideFallbackModels(model:
|
|
10
|
-
const value = resolveCatalogPolicy(model).serverSideFallbackModels;
|
|
11
|
+
export function serverSideFallbackModels(model: CatalogPolicyInputs, route?: ResolveRoute): readonly string[] {
|
|
12
|
+
const value = resolveCatalogPolicy(model, route).serverSideFallbackModels;
|
|
11
13
|
if (!Array.isArray(value)) return [];
|
|
12
14
|
return value.filter((entry): entry is string => typeof entry === "string");
|
|
13
15
|
}
|
package/src/compat/types.ts
CHANGED
|
@@ -753,6 +753,8 @@ export interface ResolvedAxes {
|
|
|
753
753
|
export interface RequestPolicy {
|
|
754
754
|
completionsReasoningMode?: "none" | "effort" | "opt-in" | "forced-on";
|
|
755
755
|
completionsReasoningHistory?: "omit" | "preserved" | "interleaved";
|
|
756
|
+
/** Send `allow_missing_thinking` with the effort so tool-call turns may arrive without reasoning. */
|
|
757
|
+
completionsAllowMissingThinking?: boolean;
|
|
756
758
|
anthropicThinking?: "adaptive" | "adaptive-summarized" | "budget-interleaved" | "budget-effort";
|
|
757
759
|
/** Advertise `fine-grained-tool-streaming-2025-05-14` on requests that carry tools. */
|
|
758
760
|
anthropicToolStreamingBeta?: boolean;
|
|
@@ -760,7 +762,7 @@ export interface RequestPolicy {
|
|
|
760
762
|
openaiPlatformHeader?: string;
|
|
761
763
|
responsesCacheRetention?: boolean;
|
|
762
764
|
responsesVerbosity?: "low";
|
|
763
|
-
responsesServiceTier?: "priority";
|
|
765
|
+
responsesServiceTier?: "priority" | "ultrafast";
|
|
764
766
|
responsesParallelToolCalls?: boolean;
|
|
765
767
|
responsesSafetyIdentifier?: boolean;
|
|
766
768
|
/** Default `tool_choice` to `auto` when the request carries tools and the caller picks none. */
|
package/src/discovery/codex.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { compareRevision, parseRevision } from "../compat/revision";
|
|
|
3
3
|
import { classifyModel } from "../compat/taxonomy";
|
|
4
4
|
import { getBundledModels } from "../models";
|
|
5
5
|
import type { FetchImpl, ModelSpec } from "../types";
|
|
6
|
-
import { discoveryFetch } from "../utils";
|
|
6
|
+
import { discoveryFetch, parseServiceTierIds } from "../utils";
|
|
7
7
|
import { CODEX_BASE_URL, CODEX_CLIENT_VERSION, OPENAI_HEADER_VALUES, OPENAI_HEADERS } from "../wire/codex";
|
|
8
8
|
|
|
9
9
|
const DEFAULT_MODEL_LIST_PATHS = ["/codex/models", "/models"] as const;
|
|
@@ -334,14 +334,7 @@ function parseCodexModelEntry(entry: unknown): ParsedCodexModelEntry | null {
|
|
|
334
334
|
|
|
335
335
|
// codex-rs `ModelServiceTier { id, name, description }`; only the id reaches the wire.
|
|
336
336
|
// An explicit empty array is kept: it means the model offers no optional tier.
|
|
337
|
-
|
|
338
|
-
if (Array.isArray(payload.service_tiers)) {
|
|
339
|
-
serviceTiers = [];
|
|
340
|
-
for (const tier of payload.service_tiers) {
|
|
341
|
-
const id = tier !== null && typeof tier === "object" && "id" in tier ? toNonEmptyString(tier.id) : null;
|
|
342
|
-
if (id && !serviceTiers.includes(id)) serviceTiers.push(id);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
337
|
+
const serviceTiers = parseServiceTierIds(payload.service_tiers);
|
|
345
338
|
|
|
346
339
|
return {
|
|
347
340
|
slug,
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
resolveFactoryDroidRotation,
|
|
7
7
|
} from "../compat/factory-droid";
|
|
8
8
|
import { hasModelScopedEffortLadder, resolveModelPolicy } from "../compat/resolve";
|
|
9
|
+
import { serverSideFallbackModels } from "../compat/server-side-fallback";
|
|
9
10
|
import type { RequestPolicy } from "../compat/types";
|
|
10
11
|
import { getBundledModel, isGeneratedProvider } from "../models";
|
|
11
12
|
import type { FetchImpl, ModelSpec, ThinkingConfig, ThinkingControlMode } from "../types";
|
|
@@ -166,16 +167,35 @@ export interface FactoryDroidModelDiscoveryOptions extends AccountScope {
|
|
|
166
167
|
*/
|
|
167
168
|
export function factoryDroidSeedModels(scope: AccountScope): ModelSpec<"factory-droid-agent">[] {
|
|
168
169
|
const inferenceRegion = resolveFactoryDroidInferenceRegion(scope);
|
|
169
|
-
return
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
170
|
+
return withAvailableRefusalFallbacks(
|
|
171
|
+
factoryDroidRegistry()
|
|
172
|
+
.filter(model => isModelAvailable(model, {}, null, inferenceRegion, null))
|
|
173
|
+
.map(model =>
|
|
174
|
+
buildFactoryDroidModel(model, {
|
|
175
|
+
region: scope.region,
|
|
176
|
+
inferenceRegion,
|
|
177
|
+
orgId: scope.orgId,
|
|
178
|
+
apiProviders: resolveFactoryDroidRotation(model.policy, inferenceRegion),
|
|
179
|
+
}),
|
|
180
|
+
),
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Native offers a server-side refusal fallback only when the account can run
|
|
186
|
+
* that model, so each model keeps the fallbacks its upstreams name that are
|
|
187
|
+
* themselves in the account's roster.
|
|
188
|
+
*/
|
|
189
|
+
function withAvailableRefusalFallbacks(models: ModelSpec<"factory-droid-agent">[]): ModelSpec<"factory-droid-agent">[] {
|
|
190
|
+
const available = new Set(models.map(model => model.id));
|
|
191
|
+
for (const model of models) {
|
|
192
|
+
const named = new Set<string>();
|
|
193
|
+
for (const upstream of model.factoryDroidApiProviders ?? []) {
|
|
194
|
+
for (const id of serverSideFallbackModels(model, { upstream })) named.add(id);
|
|
195
|
+
}
|
|
196
|
+
if (named.size > 0) model.factoryDroidRefusalFallbacks = [...named].filter(id => available.has(id));
|
|
197
|
+
}
|
|
198
|
+
return models;
|
|
179
199
|
}
|
|
180
200
|
|
|
181
201
|
/**
|
|
@@ -219,7 +239,7 @@ export async function fetchFactoryDroidModels(
|
|
|
219
239
|
} catch {
|
|
220
240
|
return null;
|
|
221
241
|
}
|
|
222
|
-
|
|
242
|
+
const models = factoryDroidRegistry().flatMap(model => {
|
|
223
243
|
if (!isModelAvailable(model, flags, policy, servingRegion, images)) return [];
|
|
224
244
|
const { id } = model.spec;
|
|
225
245
|
const family = model.policy.routingFamily;
|
|
@@ -236,6 +256,7 @@ export async function fetchFactoryDroidModels(
|
|
|
236
256
|
if (routed !== undefined) spec.factoryDroidRoutingSource = "configured_order";
|
|
237
257
|
return [spec];
|
|
238
258
|
});
|
|
259
|
+
return withAvailableRefusalFallbacks(models);
|
|
239
260
|
}
|
|
240
261
|
|
|
241
262
|
/**
|
|
@@ -282,6 +303,9 @@ export function buildFactoryDroidModel(
|
|
|
282
303
|
...factoryDroidRegionalLimits(policy, resolveFactoryDroidInferenceRegion(options)),
|
|
283
304
|
...(options.orgId ? { factoryDroidOrgId: options.orgId } : {}),
|
|
284
305
|
...(policy.creditRates ? { factoryDroidCredits: policy.creditRates.input } : {}),
|
|
306
|
+
...(policy.creditRates?.effectiveRates
|
|
307
|
+
? { factoryDroidCreditCards: policy.creditRates.effectiveRates.map(card => ({ ...card })) }
|
|
308
|
+
: {}),
|
|
285
309
|
...(options.apiProviders?.length ? { factoryDroidApiProviders: [...options.apiProviders] } : {}),
|
|
286
310
|
};
|
|
287
311
|
const resolved = resolveModelPolicy(model);
|
|
@@ -21,7 +21,7 @@ export const DEFAULT_OPENAI_COMPATIBLE_DISCOVERY_TIMEOUT_MS = 10_000;
|
|
|
21
21
|
* successful fast discovery requests do not leave armed timeout signals for
|
|
22
22
|
* concurrent GC to trip over later.
|
|
23
23
|
*/
|
|
24
|
-
async function withOpenAICompatibleDiscoveryTimeout<T>(
|
|
24
|
+
export async function withOpenAICompatibleDiscoveryTimeout<T>(
|
|
25
25
|
timeoutMs: number,
|
|
26
26
|
run: (signal: AbortSignal) => Promise<T>,
|
|
27
27
|
): Promise<T> {
|
package/src/model-cache.ts
CHANGED
|
@@ -4,15 +4,17 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import type { Database } from "bun:sqlite";
|
|
6
6
|
import * as path from "node:path";
|
|
7
|
+
import { type } from "@oh-my-pi/omptype";
|
|
7
8
|
import { getModelDbPath, isSqliteCorruptionError, logger, openSqliteDatabaseSync, VERSION } from "@oh-my-pi/pi-utils";
|
|
8
9
|
import RULES from "./compat/rules.json" with { type: "json" };
|
|
9
10
|
import type { Api, Model } from "./types";
|
|
10
11
|
|
|
11
12
|
// Rows persist fully materialized Models so compatible warm-cache reads do not
|
|
12
13
|
// rerun the compat cascade. Request headers are intentionally omitted:
|
|
13
|
-
// arbitrary provider-defined header names can carry credentials.
|
|
14
|
-
// invalidates
|
|
15
|
-
//
|
|
14
|
+
// arbitrary provider-defined header names can carry credentials. v15
|
|
15
|
+
// invalidates unbundled OpenAI-compatible rows that ignored provider-reported
|
|
16
|
+
// token limits. v14 invalidates Antigravity rows predating per-SKU
|
|
17
|
+
// `antigravityWireProfiles` (model_enum label, output cap, SKU thinking). v13 replaces
|
|
16
18
|
// sparse ModelSpec rows with materialized rows and records the exact app/rules/
|
|
17
19
|
// builder policy that produced them. v12 invalidated Kimi Code rows carrying the blanket
|
|
18
20
|
// maxTokens: 32000 that predate per-family output caps (k3/k3-256k -> 131072,
|
|
@@ -27,7 +29,7 @@ import type { Api, Model } from "./types";
|
|
|
27
29
|
// retired unknown-limit sentinels (222222/8888); v5 invalidated rows predating
|
|
28
30
|
// effort-tier variant collapsing (raw `-low`/`-high`/`-thinking` member ids);
|
|
29
31
|
// v4 dropped the pre-efforts ThinkingConfig shape.
|
|
30
|
-
const CACHE_SCHEMA_VERSION =
|
|
32
|
+
const CACHE_SCHEMA_VERSION = 15;
|
|
31
33
|
/** Oldest schema whose rows are guaranteed never to carry request headers. */
|
|
32
34
|
const FIRST_HEADERLESS_SCHEMA_VERSION = 11;
|
|
33
35
|
const HEADER_RESTORE_VERSION = 1;
|
|
@@ -444,61 +446,33 @@ function migrateCacheSchema(db: Database): void {
|
|
|
444
446
|
}
|
|
445
447
|
}
|
|
446
448
|
|
|
449
|
+
/**
|
|
450
|
+
* A cacheable materialized model. Also checked on the write side against
|
|
451
|
+
* in-memory models, so optional keys may be present as `undefined`.
|
|
452
|
+
*/
|
|
453
|
+
const MaterializedModel = type({
|
|
454
|
+
id: "string > 0",
|
|
455
|
+
name: "string > 0",
|
|
456
|
+
api: "string > 0",
|
|
457
|
+
provider: "string > 0",
|
|
458
|
+
// Empty is legitimate: Azure-style providers resolve the endpoint from
|
|
459
|
+
// configuration at request time, and their bundled rows carry "".
|
|
460
|
+
baseUrl: "string",
|
|
461
|
+
reasoning: "boolean",
|
|
462
|
+
input: "('text' | 'image')[] > 0",
|
|
463
|
+
"headers?": "undefined",
|
|
464
|
+
supportsComputerUseConfig: "boolean | null",
|
|
465
|
+
contextWindow: "number > 0 | null",
|
|
466
|
+
maxTokens: "number > 0 | null",
|
|
467
|
+
identity: { class: "string" },
|
|
468
|
+
"compat?": type("object")
|
|
469
|
+
.narrow(compat => !Array.isArray(compat))
|
|
470
|
+
.or("undefined"),
|
|
471
|
+
cost: { input: "number", output: "number", cacheRead: "number", cacheWrite: "number" },
|
|
472
|
+
});
|
|
473
|
+
|
|
447
474
|
function isMaterializedModel(value: unknown): value is PersistedModel<Api> {
|
|
448
|
-
|
|
449
|
-
const model = value as Partial<PersistedModel<Api>>;
|
|
450
|
-
if (
|
|
451
|
-
typeof model.id !== "string" ||
|
|
452
|
-
model.id.length === 0 ||
|
|
453
|
-
typeof model.name !== "string" ||
|
|
454
|
-
model.name.length === 0 ||
|
|
455
|
-
typeof model.api !== "string" ||
|
|
456
|
-
model.api.length === 0 ||
|
|
457
|
-
typeof model.provider !== "string" ||
|
|
458
|
-
model.provider.length === 0 ||
|
|
459
|
-
// Empty is legitimate: Azure-style providers resolve the endpoint from
|
|
460
|
-
// configuration at request time, and their bundled rows carry "".
|
|
461
|
-
typeof model.baseUrl !== "string" ||
|
|
462
|
-
typeof model.reasoning !== "boolean" ||
|
|
463
|
-
!Array.isArray(model.input) ||
|
|
464
|
-
model.input.length === 0 ||
|
|
465
|
-
model.input.some(input => input !== "text" && input !== "image") ||
|
|
466
|
-
model.headers !== undefined ||
|
|
467
|
-
!Object.hasOwn(model, "supportsComputerUseConfig") ||
|
|
468
|
-
(model.supportsComputerUseConfig !== null && typeof model.supportsComputerUseConfig !== "boolean")
|
|
469
|
-
) {
|
|
470
|
-
return false;
|
|
471
|
-
}
|
|
472
|
-
const contextWindow = model.contextWindow;
|
|
473
|
-
const maxTokens = model.maxTokens;
|
|
474
|
-
if (
|
|
475
|
-
(contextWindow !== null &&
|
|
476
|
-
(typeof contextWindow !== "number" || !Number.isFinite(contextWindow) || contextWindow <= 0)) ||
|
|
477
|
-
(maxTokens !== null && (typeof maxTokens !== "number" || !Number.isFinite(maxTokens) || maxTokens <= 0))
|
|
478
|
-
) {
|
|
479
|
-
return false;
|
|
480
|
-
}
|
|
481
|
-
if (
|
|
482
|
-
model.identity === null ||
|
|
483
|
-
typeof model.identity !== "object" ||
|
|
484
|
-
typeof model.identity.class !== "string" ||
|
|
485
|
-
(model.compat !== undefined &&
|
|
486
|
-
(model.compat === null || typeof model.compat !== "object" || Array.isArray(model.compat)))
|
|
487
|
-
) {
|
|
488
|
-
return false;
|
|
489
|
-
}
|
|
490
|
-
if (model.cost === null || typeof model.cost !== "object") return false;
|
|
491
|
-
const cost = model.cost as Partial<Model<Api>["cost"]>;
|
|
492
|
-
return (
|
|
493
|
-
typeof cost.input === "number" &&
|
|
494
|
-
Number.isFinite(cost.input) &&
|
|
495
|
-
typeof cost.output === "number" &&
|
|
496
|
-
Number.isFinite(cost.output) &&
|
|
497
|
-
typeof cost.cacheRead === "number" &&
|
|
498
|
-
Number.isFinite(cost.cacheRead) &&
|
|
499
|
-
typeof cost.cacheWrite === "number" &&
|
|
500
|
-
Number.isFinite(cost.cacheWrite)
|
|
501
|
-
);
|
|
475
|
+
return MaterializedModel.allows(value);
|
|
502
476
|
}
|
|
503
477
|
|
|
504
478
|
function parseMaterializedModels<TApi extends Api>(serialized: string): Model<TApi>[] | null {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Api, Model } from "./types";
|
|
2
|
+
import { isRecord } from "./utils";
|
|
2
3
|
|
|
3
4
|
// References are distinguished from JSON scalar values by their one-element array.
|
|
4
5
|
// -1 represents an explicitly present undefined value.
|
|
@@ -32,16 +33,12 @@ export interface ModelCatalog {
|
|
|
32
33
|
readonly compatValues: Map<string, Map<number, Map<string, Record<string, unknown>>>>;
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
function isObject(value: unknown): value is Record<string, unknown> {
|
|
36
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
36
|
// JSON serialization with sorted object keys gives both deterministic ordering
|
|
40
37
|
// and value-based interning, independent of the input objects' insertion order.
|
|
41
38
|
function canonical(value: unknown): string {
|
|
42
39
|
if (value === undefined) return "u";
|
|
43
40
|
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
44
|
-
if (
|
|
41
|
+
if (isRecord(value)) {
|
|
45
42
|
return `{${Object.keys(value)
|
|
46
43
|
.sort()
|
|
47
44
|
.map(key => `${JSON.stringify(key)}:${canonical(value[key])}`)
|
|
@@ -80,7 +77,7 @@ export function encodeModelCatalog(models: Record<string, Record<string, Model<A
|
|
|
80
77
|
let node: Node;
|
|
81
78
|
if (Array.isArray(value)) {
|
|
82
79
|
node = [1, value.map(encode)];
|
|
83
|
-
} else if (
|
|
80
|
+
} else if (isRecord(value)) {
|
|
84
81
|
const properties: EncodedValue[] = [];
|
|
85
82
|
const boolKeys: number[] = [];
|
|
86
83
|
const masks: number[] = [];
|
|
@@ -119,7 +116,7 @@ export function encodeModelCatalog(models: Record<string, Record<string, Model<A
|
|
|
119
116
|
for (const provider of Object.keys(models).sort()) {
|
|
120
117
|
for (const id of Object.keys(models[provider]).sort()) {
|
|
121
118
|
const model = models[provider][id];
|
|
122
|
-
if (typeof model.api !== "string" || !
|
|
119
|
+
if (typeof model.api !== "string" || !isRecord(model.compat)) continue;
|
|
123
120
|
let counts = compatCounts.get(model.api);
|
|
124
121
|
if (!counts) compatCounts.set(model.api, (counts = new Map()));
|
|
125
122
|
const signature = canonical(model.compat);
|
|
@@ -161,7 +158,7 @@ export function encodeModelCatalog(models: Record<string, Record<string, Model<A
|
|
|
161
158
|
const rest: Record<string, unknown> = Object.create(null);
|
|
162
159
|
const source = model as unknown as Record<string, unknown>;
|
|
163
160
|
const base = typeof model.api === "string" ? bases.get(model.api) : undefined;
|
|
164
|
-
const useBase = base !== undefined &&
|
|
161
|
+
const useBase = base !== undefined && isRecord(model.compat);
|
|
165
162
|
for (const key of Object.keys(source).sort()) {
|
|
166
163
|
if (Object.hasOwn(constants, key) || (key === "id" && source.id === id) || (key === "compat" && useBase))
|
|
167
164
|
continue;
|
package/src/model-manager.ts
CHANGED
|
@@ -1,19 +1,12 @@
|
|
|
1
|
+
import { type } from "@oh-my-pi/omptype";
|
|
1
2
|
import { logger, VERSION } from "@oh-my-pi/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 {
|
|
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;
|
|
@@ -818,18 +811,19 @@ function preferDiscoveryLimit(discoveryLimit: number | null, fallbackLimit: numb
|
|
|
818
811
|
* caller treats the source as failed (cache/static fallback) rather than as an
|
|
819
812
|
* intentionally empty catalog that an authoritative provider would prune to.
|
|
820
813
|
*/
|
|
821
|
-
function buildDiscoveredModelSpecs<TApi extends Api>(
|
|
814
|
+
function buildDiscoveredModelSpecs<TApi extends Api>(values: unknown): DiscoveredModelSet<TApi> {
|
|
822
815
|
const models: Model<TApi>[] = [];
|
|
823
816
|
const explicitKindModels = new Set<Model<TApi>>();
|
|
824
|
-
if (!Array.isArray(
|
|
817
|
+
if (!Array.isArray(values)) {
|
|
825
818
|
return { models, explicitKindModels };
|
|
826
819
|
}
|
|
827
820
|
const failures: unknown[] = [];
|
|
828
|
-
for (const
|
|
829
|
-
if (!
|
|
821
|
+
for (const value of values) {
|
|
822
|
+
if (!ModelSpecShape.allows(value)) continue;
|
|
823
|
+
const item = value as ModelSpec<TApi>;
|
|
830
824
|
let model: Model<TApi>;
|
|
831
825
|
try {
|
|
832
|
-
model = buildModel(item
|
|
826
|
+
model = buildModel(item);
|
|
833
827
|
} catch (error) {
|
|
834
828
|
failures.push(error);
|
|
835
829
|
logger.warn("model discovery skipped a model that failed to build", {
|
|
@@ -846,109 +840,21 @@ function buildDiscoveredModelSpecs<TApi extends Api>(value: unknown): Discovered
|
|
|
846
840
|
return { models, explicitKindModels };
|
|
847
841
|
}
|
|
848
842
|
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
868
|
-
if (typeof v.name !== "string" || v.name.length === 0) {
|
|
869
|
-
return false;
|
|
870
|
-
}
|
|
871
|
-
if (typeof v.api !== "string" || v.api.length === 0) {
|
|
872
|
-
return false;
|
|
873
|
-
}
|
|
874
|
-
if (typeof v.provider !== "string" || v.provider.length === 0) {
|
|
875
|
-
return false;
|
|
876
|
-
}
|
|
877
|
-
if (typeof v.baseUrl !== "string" || v.baseUrl.length === 0) {
|
|
878
|
-
return false;
|
|
879
|
-
}
|
|
880
|
-
if (typeof v.reasoning !== "boolean") {
|
|
881
|
-
return false;
|
|
882
|
-
}
|
|
883
|
-
if (!isModelInputArray(v.input)) {
|
|
884
|
-
return false;
|
|
885
|
-
}
|
|
886
|
-
if (!isModelCost(v.cost)) {
|
|
887
|
-
return false;
|
|
888
|
-
}
|
|
889
|
-
// Finite positive: NaN > 0 is false, +Infinity < Infinity is false.
|
|
890
|
-
const cw = v.contextWindow;
|
|
891
|
-
if (cw !== null && (typeof cw !== "number" || !(cw > 0 && cw < Infinity))) {
|
|
892
|
-
return false;
|
|
893
|
-
}
|
|
894
|
-
const mt = v.maxTokens;
|
|
895
|
-
if (mt !== null && (typeof mt !== "number" || !(mt > 0 && mt < Infinity))) {
|
|
896
|
-
return false;
|
|
897
|
-
}
|
|
898
|
-
return true;
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
function isModelInputArray(value: unknown): value is ("text" | "image")[] {
|
|
902
|
-
if (!Array.isArray(value) || value.length === 0) {
|
|
903
|
-
return false;
|
|
904
|
-
}
|
|
905
|
-
for (let i = 0; i < value.length; i++) {
|
|
906
|
-
const item = value[i];
|
|
907
|
-
if (item !== "text" && item !== "image") {
|
|
908
|
-
return false;
|
|
909
|
-
}
|
|
910
|
-
}
|
|
911
|
-
return true;
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
function isTokenCost(value: unknown): value is TokenCost {
|
|
915
|
-
if (!isRecord(value)) {
|
|
916
|
-
return false;
|
|
917
|
-
}
|
|
918
|
-
const c = value as {
|
|
919
|
-
input?: unknown;
|
|
920
|
-
output?: unknown;
|
|
921
|
-
cacheRead?: unknown;
|
|
922
|
-
cacheWrite?: unknown;
|
|
923
|
-
};
|
|
924
|
-
// Finite (NaN-safe): -Infinity < x < Infinity rejects NaN and both infinities.
|
|
925
|
-
// Preserves original behavior: 0 and negatives remain valid.
|
|
926
|
-
const ci = c.input;
|
|
927
|
-
if (typeof ci !== "number" || !(ci > -Infinity && ci < Infinity)) {
|
|
928
|
-
return false;
|
|
929
|
-
}
|
|
930
|
-
const co = c.output;
|
|
931
|
-
if (typeof co !== "number" || !(co > -Infinity && co < Infinity)) {
|
|
932
|
-
return false;
|
|
933
|
-
}
|
|
934
|
-
const cr = c.cacheRead;
|
|
935
|
-
if (typeof cr !== "number" || !(cr > -Infinity && cr < Infinity)) {
|
|
936
|
-
return false;
|
|
937
|
-
}
|
|
938
|
-
const cw = c.cacheWrite;
|
|
939
|
-
if (typeof cw !== "number" || !(cw > -Infinity && cw < Infinity)) {
|
|
940
|
-
return false;
|
|
941
|
-
}
|
|
942
|
-
return true;
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
function isModelCost(value: unknown): value is ModelCost {
|
|
946
|
-
if (!isTokenCost(value)) return false;
|
|
947
|
-
const cost = value as TokenCost & { longContext?: unknown; timeBased?: unknown };
|
|
948
|
-
if (cost.timeBased !== undefined && !isTimeBasedCost(cost.timeBased)) return false;
|
|
949
|
-
const longContext = cost.longContext;
|
|
950
|
-
if (longContext === undefined) return true;
|
|
951
|
-
if (!isTokenCost(longContext) || !isRecord(longContext)) return false;
|
|
952
|
-
const threshold = longContext.inputThreshold;
|
|
953
|
-
return typeof threshold === "number" && threshold > 0 && threshold < Infinity;
|
|
954
|
-
}
|
|
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
|
+
});
|
package/src/model-tokenizer.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
|
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
|
-
|
|
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
|
/**
|