Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 28 additions & 46 deletions src/cost/pricing-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,72 +75,54 @@ function parseModelPricing(value: unknown): ModelPricing | null {
};
}

function collectModelPricing(value: unknown, models: Record<string, ModelPricing>): void {
/**
* Recurses through an untyped models.dev JSON tree and yields every node that
* resolves to a model id, alongside that node. Each `parseModelsDev*` walker
* below shares this single traversal and only differs in which field it
* extracts from the yielded node.
*/
function* walkModelNodes(value: unknown): Generator<[id: string, node: Record<string, unknown>]> {
if (Array.isArray(value)) {
for (const item of value) yield* walkModelNodes(item);
return;
}
if (!isRecord(value)) return;

const id = typeof value.id === "string" ? value.id : typeof value.model === "string" ? value.model : null;
const pricing = parseModelPricing(value);
if (id !== null && pricing !== null) {
models[id] = pricing;
}
if (id !== null) yield [id, value];

for (const child of Object.values(value)) {
if (isRecord(child)) {
collectModelPricing(child, models);
continue;
}
if (Array.isArray(child)) {
for (const item of child) collectModelPricing(item, models);
}
yield* walkModelNodes(child);
}
}

export function parseModelsDevPricing(payload: unknown): Record<string, ModelPricing> {
const models: Record<string, ModelPricing> = {};
collectModelPricing(payload, models);
return models;
}

function collectModelReasoning(value: unknown, reasoning: Record<string, boolean>): void {
if (!isRecord(value)) {
if (Array.isArray(value)) for (const item of value) collectModelReasoning(item, reasoning);
return;
}
const id = typeof value.id === "string" ? value.id : typeof value.model === "string" ? value.model : null;
if (id !== null && typeof value.reasoning === "boolean") {
reasoning[id] = value.reasoning;
}
for (const child of Object.values(value)) {
if (isRecord(child) || Array.isArray(child)) collectModelReasoning(child, reasoning);
for (const [id, node] of walkModelNodes(payload)) {
const pricing = parseModelPricing(node);
if (pricing !== null) models[id] = pricing;
}
return models;
}

export function parseModelsDevReasoning(payload: unknown): Record<string, boolean> {
const reasoning: Record<string, boolean> = {};
collectModelReasoning(payload, reasoning);
return reasoning;
}

function collectModelContextWindows(value: unknown, windows: Record<string, number>): void {
if (!isRecord(value)) {
if (Array.isArray(value)) for (const item of value) collectModelContextWindows(item, windows);
return;
}
const id = typeof value.id === "string" ? value.id : typeof value.model === "string" ? value.model : null;
// models.dev nests the window under `limit.context`.
const limit = isRecord(value.limit) ? value.limit : undefined;
const context = limit !== undefined && typeof limit.context === "number" ? limit.context : undefined;
if (id !== null && context !== undefined && Number.isFinite(context) && context > 0) {
windows[id] = context;
}
for (const child of Object.values(value)) {
if (isRecord(child) || Array.isArray(child)) collectModelContextWindows(child, windows);
for (const [id, node] of walkModelNodes(payload)) {
if (typeof node.reasoning === "boolean") reasoning[id] = node.reasoning;
}
return reasoning;
}

export function parseModelsDevContextWindows(payload: unknown): Record<string, number> {
const windows: Record<string, number> = {};
collectModelContextWindows(payload, windows);
for (const [id, node] of walkModelNodes(payload)) {
// models.dev nests the window under `limit.context`.
const limit = isRecord(node.limit) ? node.limit : undefined;
const context = limit !== undefined && typeof limit.context === "number" ? limit.context : undefined;
if (context !== undefined && Number.isFinite(context) && context > 0) {
windows[id] = context;
}
}
return windows;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/unit/pricing-fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ test("parseModelsDevContextWindows reads limit.context per model", () => {
expect(windows["openai/no-limit"]).toBeUndefined();
});

test("parseModelsDevPricing walks a top-level array payload the same way the other collectors do", () => {
// A nesting level where the root itself is an array, rather than an object
// whose values are arrays. parseModelsDevReasoning and
// parseModelsDevContextWindows already handle this; pricing must match.
const models = parseModelsDevPricing([
{ id: "m1", input_cost_per_million: 1, output_cost_per_million: 2 },
]);
expect(models["m1"]).toBeDefined();
});

function response(body: unknown, ok = true, status = 200): Response {
return new Response(JSON.stringify(body), { status: ok ? status : status });
}
Expand Down
Loading