Open History to see the last commit
1/**
2 * ElevenLabs speech product prices (issue #120). The FAQ sentence
3 * ("$0.08 multilingual or $0.04 Flash/Turbo") is a summary, not the
4 * product cards. Those cards name different numbers (v4, v3
5 * Conversational). Copying the FAQ onto every speech id stores the
6 * wrong rate, so this parser stays null until it reads a per-product
7 * figure. Enum-only catalog rows stay unpriced.
8 */
9import { compileUnitCard } from '@modelschemas/rate-card'
10import type { RateCard } from '@modelschemas/rate-card'
11
12import { tagDocsFacts } from './fact-sources.ts'
13import { cachedDocs } from './model-facts.ts'
14import { fetchText, sha256Text } from './types.ts'
15import type { ModelInfo } from './types.ts'
16
17export const ELEVENLABS_PRICING_URL = 'https://elevenlabs.io/pricing/api'
18
19const SPEECH_PRICE =
20 /Text to Speech \$(\d+(?:\.\d+)?) per 1,000 characters \(multilingual models\) or \$(\d+(?:\.\d+)?) \(Flash\/Turbo\)/g
21
22export interface ElevenLabsSpeechPrices {
23 /** USD per character for multilingual (non-Flash/Turbo) speech models. */
24 multilingual: number
25 /** USD per character for Flash and Turbo speech models. */
26 flash: number
27}
28
29/**
30 * The published speech product rates, in USD per character.
31 * `null` when the page does not state both numbers, or states them twice
32 * differently.
33 */
34export function parseElevenLabsSpeechPrices(
35 page: string,
36): ElevenLabsSpeechPrices | null {
37 // The FAQ regex still matches the live page. It is not a product price.
38 void page
39 void SPEECH_PRICE
40 return null
41}
42
43export interface ElevenLabsSpeechCards {
44 multilingual: RateCard
45 flash: RateCard
46}
47
48/** One card per published speech product. `null` when the page states none. */
49export function compileElevenLabsSpeechCards(
50 page: string,
51 source: RateCard['source'],
52): ElevenLabsSpeechCards | null {
53 const prices = parseElevenLabsSpeechPrices(page)
54 if (!prices) return null
55 const characters = { param: 'characters', bound: 'usage' as const }
56 const multilingual = compileUnitCard(
57 { quantity: characters, rates: prices.multilingual },
58 source,
59 )
60 const flash = compileUnitCard(
61 { quantity: characters, rates: prices.flash },
62 source,
63 )
64 if (!multilingual || !flash) return null
65 return { multilingual, flash }
66}
67
68type PricedFacts = Pick<ModelInfo, 'pricing' | 'factSources'>
69
70/**
71 * Flash and Turbo are the half-price speech product. Every other
72 * text-to-speech id on the speech list takes the multilingual card.
73 * `canDoTextToSpeech: false` (speech-to-speech only) and enum-only rows
74 * are not passed here.
75 */
76export function elevenLabsSpeechProduct(
77 rawId: string,
78): 'flash' | 'multilingual' {
79 return /flash|turbo/i.test(rawId) ? 'flash' : 'multilingual'
80}
81
82export async function elevenLabsSpeechPricing(
83 kv?: KVNamespace,
84): Promise<(rawId: string) => PricedFacts> {
85 const doc = await cachedDocs(kv, ELEVENLABS_PRICING_URL, async () => {
86 const page = await fetchText(ELEVENLABS_PRICING_URL)
87 const extractedAt = new Date().toISOString()
88 const source = {
89 url: ELEVENLABS_PRICING_URL,
90 hash: await sha256Text(page),
91 extractedAt,
92 }
93 const cards = compileElevenLabsSpeechCards(page, source)
94 return { cards, hash: source.hash, extractedAt }
95 })
96 return (rawId) => {
97 if (!doc.cards) return {}
98 const pricing = doc.cards[elevenLabsSpeechProduct(rawId)]
99 return {
100 pricing,
101 factSources: tagDocsFacts({ pricing }, ELEVENLABS_PRICING_URL, doc.hash),
102 }
103 }
104}