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

sync-elevenlabs-models.ts

131 lines4.0 KB

1/**
2 * Inserts new ElevenLabs model ids into
3 * `packages/ai-elevenlabs/src/model-meta.ts`.
4 *
5 * Text-to-speech: https://modelschemas.com/v1/models?provider=elevenlabs
6 * Music: request schema `v1/music` (compose). That enum is the SDK
7 * `MusicModelId` set.
8 * Sound effects: `v1/sound-generation`.
9 * Voice design: `v1/text-to-voice/design`.
10 *
11 * Usage:
12 * pnpm tsx scripts/sync-elevenlabs-models.ts
13 *
14 * Runs as part of `pnpm generate:models` (the daily Sync Model Metadata
15 * workflow). Only adds missing ids. Transcription stays hand-maintained:
16 * its speech-to-text schema has no `model_id` enum. Music and voice design
17 * are pinned to SDK unions, so an id the SDK does not know yet fails the build.
18 */
19​
20import { readFile, writeFile } from 'node:fs/promises'
21import { dirname, resolve } from 'node:path'
22import { fileURLToPath } from 'node:url'
23import {
24 insertMissingModels,
25 modelIdsFromRequestSchema,
26 ttsModelIdsFromCatalog,
27} from './model-sync/elevenlabs'
28import type { ElevenLabsCatalogModel } from './model-sync/elevenlabs'
29​
30const CATALOG_URL = 'https://modelschemas.com/v1/models?provider=elevenlabs'
31const SCHEMA_ROOT = 'https://modelschemas.com/v1/schemas/elevenlabs/audio'
32const __dirname = dirname(fileURLToPath(import.meta.url))
33const META_FILE = resolve(
34 __dirname,
35 '../packages/ai-elevenlabs/src/model-meta.ts',
37​
38interface CatalogResponse {
39 count?: number
40 models?: Array<ElevenLabsCatalogModel>
42​
43function schemaUrl(endpointId: string): string {
44 return `${SCHEMA_ROOT}/${endpointId}?kind=input`
46​
47async function readJson(url: string): Promise<unknown> {
48 const response = await fetch(url)
49 if (!response.ok) {
50 throw new Error(
51 `modelschemas ${response.status} ${response.statusText} for ${url}`,
52 )
53 }
54 return response.json()
56​
57function unwrapSchema(payload: unknown, url: string): unknown {
58 if (
59 typeof payload !== 'object' ||
60 payload === null ||
61 !('schema' in payload)
62 ) {
63 throw new Error(`modelschemas response has no schema for ${url}`)
64 }
65 return payload.schema
67​
68async function main() {
69 const musicUrl = schemaUrl('v1/music')
70 const sfxUrl = schemaUrl('v1/sound-generation')
71 const voiceUrl = schemaUrl('v1/text-to-voice/design')
72 const [catalogPayload, musicPayload, sfxPayload, voicePayload] =
73 await Promise.all([
74 readJson(CATALOG_URL),
75 readJson(musicUrl),
76 readJson(sfxUrl),
77 readJson(voiceUrl),
78 ])
79​
80 const body = catalogPayload as CatalogResponse
81 const models = body.models
82 if (!Array.isArray(models) || models.length === 0) {
83 throw new Error('modelschemas returned no ElevenLabs models')
84 }
85 if (typeof body.count === 'number' && body.count !== models.length) {
86 throw new Error(
87 `modelschemas page is incomplete: count ${body.count}, got ${models.length}`,
88 )
89 }
90​
91 const groups: Array<[string, string, readonly string[]]> = [
92 ['ELEVENLABS_TTS_MODELS', 'text-to-speech', ttsModelIdsFromCatalog(models)],
93 [
94 'ELEVENLABS_AUDIO_MODELS',
95 'audio',
96 [
97 ...modelIdsFromRequestSchema(unwrapSchema(musicPayload, musicUrl)),
98 ...modelIdsFromRequestSchema(unwrapSchema(sfxPayload, sfxUrl)),
99 ],
100 ],
101 [
102 'ELEVENLABS_VOICE_MODELS',
103 'voice',
104 modelIdsFromRequestSchema(unwrapSchema(voicePayload, voiceUrl)),
105 ],
106 ]
107 if (groups.some(([, , ids]) => ids.length === 0)) {
108 throw new Error('modelschemas returned an empty ElevenLabs model list')
109 }
110​
111 const source = await readFile(META_FILE, 'utf8')
112 let next = source
113 const additions: Array<string> = []
114 for (const [exportName, label, ids] of groups) {
115 const added = ids.filter((id) => !source.includes(`'${id}'`))
116 if (added.length > 0) additions.push(`${label}: ${added.join(', ')}`)
117 next = insertMissingModels(next, exportName, ids)
118 }
119 if (next === source) {
120 console.log('ElevenLabs models already match modelschemas')
121 return
122 }
123​
124 await writeFile(META_FILE, next)
125 console.log(`Added ElevenLabs models (${additions.join('; ')})`)
127​
128main().catch((error) => {
129 console.error(error)
130 process.exit(1)
131})