Open History to see the last commit
1/**
2 * Fireworks serverless prices (issue #109). The headline table's Standard
3 * cell is input / cached input / output, USD per 1M tokens. Priority is a
4 * separate serving mode and is not the default card. "(US)" rows share a
5 * slug with the base row at a different rate, so they are skipped rather
6 * than merged. Size-band tables name no API id and are ignored. Fast rows
7 * bind to `accounts/fireworks/routers/<slug>-fast`.
8 */
9import { assertParsed, markdownTableRows } from './model-facts.ts'
10import type { cachedDocs } from './model-facts.ts'
11import { fetchText, sha256Text } from './types.ts'
12
13export const FIREWORKS_PRICING_URL =
14 'https://docs.fireworks.ai/serverless/pricing.md'
15
16export interface FireworksRates {
17 input: number
18 cacheRead: number
19 output: number
20}
21
22function standardRates(cell: string): FireworksRates | null {
23 const text = cell.replace(/\\/g, '').replace(/\s+/g, '')
24 const match = text.match(
25 /^\$(\d+(?:\.\d+)?)\/\$(\d+(?:\.\d+)?)\/\$(\d+(?:\.\d+)?)$/,
26 )
27 if (!match?.[1] || !match[2] || !match[3]) return null
28 return {
29 input: Number(match[1]),
30 cacheRead: Number(match[2]),
31 output: Number(match[3]),
32 }
33}
34
35/** API id → Standard USD per million tokens. */
36export function parseFireworksPricing(
37 markdown: string,
38): Map<string, FireworksRates> {
39 const out = new Map<string, FireworksRates>()
40 const dropped = new Set<string>()
41 for (const cells of markdownTableRows(markdown.replace(/^ +\|/gm, '|'))) {
42 const label = cells[0] ?? ''
43 const rates = standardRates(cells[1] ?? '')
44 const slug = label.match(/models\/fireworks\/([A-Za-z0-9._-]+)/)?.[1]
45 if (!slug || !rates) continue
46 const name = (label.match(/\[([^\]]+)\]/)?.[1] ?? label).replace(/\\/g, '')
47 if (/\(US\)/i.test(name)) continue
48 const id = /fast/i.test(name)
49 ? `accounts/fireworks/routers/${slug}-fast`
50 : `accounts/fireworks/models/${slug}`
51 if (out.has(id) || dropped.has(id)) {
52 out.delete(id)
53 dropped.add(id)
54 continue
55 }
56 out.set(id, rates)
57 }
58 return out
59}
60
61export interface FireworksPricingDoc {
62 rates: Record<string, FireworksRates>
63 hash: string
64 extractedAt: string
65}
66
67/** Headline Standard cells. The adapter merges these with the serverless API. */
68export function loadFireworksPricingDoc(
69 kv: KVNamespace | undefined,
70 cached: typeof cachedDocs,
71): Promise<FireworksPricingDoc> {
72 return cached(kv, FIREWORKS_PRICING_URL, async () => {
73 const markdown = await fetchText(FIREWORKS_PRICING_URL)
74 const parsed = parseFireworksPricing(markdown)
75 assertParsed(parsed, 'fireworks pricing page')
76 return {
77 rates: Object.fromEntries(parsed),
78 hash: await sha256Text(markdown),
79 extractedAt: new Date().toISOString(),
80 }
81 })
82}