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