Open History to see the last commit
1/**
2 * One FAL rate-card extract through the REAL `AI` binding and AI Gateway —
3 * the route `scripts/compare-fal-extract-models.ts` stands in for, which
4 * #551 and #556 shipped without exercising (#552). Prints the Pricing
5 * section, then the card or the rejection. Billed to Unified Billing.
6 *
7 * CLOUDFLARE_ACCOUNT_ID=… pnpm run fal-extract-smoke -- [fal model id]
8 *
9 * Node, not bun: wrangler's remote bindings cannot run under bun
10 * (oven-sh/bun#39247). Needs `wrangler login`, and `FAL_KEY` (env or
11 * Doppler dev) for the endpoint's request properties.
12 */
13import { mkdtemp, writeFile } from 'node:fs/promises'
14import { tmpdir } from 'node:os'
15import { join } from 'node:path'
16import { getPlatformProxy } from 'wrangler'
17
18import {
19 extractRateCard,
20 gatewayChat,
21 pricingSection,
22 pricingSectionHash,
23} from '../src/server/ingest/extract-fal-pricing.ts'
24import { falLlmsTxtUrl } from '../src/server/providers/fal.ts'
25import { requestPropertiesOf } from './fal-models.ts'
26
27const rawId = process.argv[2] ?? 'heygen/heygen-voice'
28
29// Only the AI binding: the repo config would also boot D1, KV and DOs.
30const configPath = join(
31 await mkdtemp(join(tmpdir(), 'fal-extract-smoke-')),
32 'wrangler.json',
33)
34await writeFile(
35 configPath,
36 JSON.stringify({
37 name: 'fal-extract-smoke',
38 compatibility_date: '2025-09-02',
39 ai: { binding: 'AI', remote: true },
40 }),
41)
42const { env, dispose } = await getPlatformProxy<{ AI: Ai }>({ configPath })
43try {
44 const sourceUrl = falLlmsTxtUrl(rawId)
45 const section = pricingSection(await (await fetch(sourceUrl)).text())
46 console.log(section)
47 const requestProperties = await requestPropertiesOf(rawId)
48 console.log(`request properties: ${[...requestProperties].sort().join(', ')}`)
49 const started = performance.now()
50 const result = await extractRateCard({
51 pricingText: section,
52 requestProperties,
53 sourceUrl,
54 sourceHash: await pricingSectionHash(section),
55 now: Math.floor(Date.now() / 1000),
56 chat: gatewayChat(env.AI, 'modelschemas'),
57 })
58 console.log(`${Math.round(performance.now() - started)}ms`)
59 console.log(JSON.stringify(result, null, 1))
60} finally {
61 await dispose()
62}