Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

stability-pricing.ts

83 lines2.9 KB

1/**
2 * Stability prices from the pricing app bundle (issue #122). The HTML
3 * shell is empty; the published table lives in the script it loads.
4 * `1 credit = $0.01`, and a service whose price is a single credit count
5 * becomes that many cents per request. "From 0.9" is not a single amount,
6 * so that service stays unpriced. A page that prices nothing throws.
7 */
8import { compileUnitCard } from '@modelschemas/rate-card'
9import type { RateCard } from '@modelschemas/rate-card'
10​
11import { tagDocsFacts } from './fact-sources.ts'
12import { assertParsed, cachedDocs } from './model-facts.ts'
13import { fetchText, sha256Text } from './types.ts'
14import type { ModelInfo } from './types.ts'
15​
16export const STABILITY_PRICING_URL = 'https://platform.stability.ai/pricing'
17​
18const SERVICE =
19 /\{id:"([^"]+)",service:"([^"]+)",description:"[^"]*",price:"([^"]+)"/g
20const CREDIT_USD = /1 credit = \$(\d+(?:\.\d+)?)/
21const EXACT_CREDITS = /^\d+(?:\.\d+)?$/
22​
23/** Service id → USD per request. Inexact prices are absent. */
24export function parseStabilityPricing(source: string): Map<string, number> {
25 const credit = source.match(CREDIT_USD)?.[1]
26 if (credit === undefined) return new Map()
27 const usd = Number(credit)
28 const out = new Map<string, number>()
29 for (const match of source.matchAll(SERVICE)) {
30 const id = match[1]
31 const price = match[3]
32 if (!id || !price || !EXACT_CREDITS.test(price) || out.has(id)) continue
33 out.set(id, Number(price) * usd)
34 }
35 return out
37​
38async function loadStabilityPricingSource(): Promise<string> {
39 const html = await fetchText(STABILITY_PRICING_URL)
40 const src = html.match(/src="(\/assets\/[^"]+\.js)"/)?.[1]
41 if (!src) {
42 throw new Error('stability pricing: page has no app script')
43 }
44 return fetchText(new URL(src, STABILITY_PRICING_URL).href)
46​
47type PricedFacts = Pick<ModelInfo, 'pricing' | 'factSources'>
48​
49/**
50 * Card lookup by the pricing page's service id. Catalog engine ids that
51 * the page does not name (including SDXL's "From 0.9") get no card.
52 */
53export async function stabilityModelPricing(
54 kv?: KVNamespace,
55): Promise<(rawId: string) => PricedFacts> {
56 const doc = await cachedDocs(kv, STABILITY_PRICING_URL, async () => {
57 const source = await loadStabilityPricingSource()
58 const parsed = parseStabilityPricing(source)
59 assertParsed(parsed, 'stability pricing page')
60 return {
61 rates: Object.fromEntries(parsed),
62 hash: await sha256Text(source),
63 extractedAt: new Date().toISOString(),
64 }
65 })
66 return (rawId) => {
67 const usd = doc.rates[rawId]
68 if (usd === undefined) return {}
69 const pricing: RateCard | null = compileUnitCard(
70 { rates: usd },
71 {
72 url: STABILITY_PRICING_URL,
73 hash: doc.hash,
74 extractedAt: doc.extractedAt,
75 },
76 )
77 if (!pricing) return {}
78 return {
79 pricing,
80 factSources: tagDocsFacts({ pricing }, STABILITY_PRICING_URL, doc.hash),
81 }
82 }