egaki 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +188 -0
- package/dist/cli/cache-utils.d.ts +33 -0
- package/dist/cli/cache-utils.d.ts.map +1 -0
- package/dist/cli/cache-utils.js +157 -0
- package/dist/cli/cached-generate.d.ts +105 -0
- package/dist/cli/cached-generate.d.ts.map +1 -0
- package/dist/cli/cached-generate.js +208 -0
- package/dist/cli/cached-generate.test.d.ts +11 -0
- package/dist/cli/cached-generate.test.d.ts.map +1 -0
- package/dist/cli/cached-generate.test.js +431 -0
- package/dist/cli/cartesia-provider.d.ts +16 -0
- package/dist/cli/cartesia-provider.d.ts.map +1 -0
- package/dist/cli/cartesia-provider.js +184 -0
- package/dist/cli/chatgpt-auth.d.ts.map +1 -0
- package/dist/cli/cli.d.ts +4 -0
- package/dist/cli/cli.d.ts.map +1 -0
- package/dist/cli/cli.js +1329 -0
- package/dist/cli/clone-generate.d.ts +26 -0
- package/dist/cli/clone-generate.d.ts.map +1 -0
- package/dist/cli/clone-generate.js +148 -0
- package/dist/{credentials.d.ts → cli/credentials.d.ts} +11 -2
- package/dist/cli/credentials.d.ts.map +1 -0
- package/dist/{credentials.js → cli/credentials.js} +88 -5
- package/dist/cli/elevenlabs-speech-provider.d.ts +26 -0
- package/dist/cli/elevenlabs-speech-provider.d.ts.map +1 -0
- package/dist/cli/elevenlabs-speech-provider.js +199 -0
- package/dist/cli/generate.d.ts +140 -0
- package/dist/cli/generate.d.ts.map +1 -0
- package/dist/cli/generate.js +604 -0
- package/dist/cli/generate.test.d.ts +2 -0
- package/dist/cli/generate.test.d.ts.map +1 -0
- package/dist/cli/generate.test.js +131 -0
- package/dist/cli/login.d.ts.map +1 -0
- package/dist/{login.js → cli/login.js} +45 -4
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/main.d.ts.map +1 -0
- package/dist/cli/main.js +7 -0
- package/dist/cli/model-catalog.d.ts +141 -0
- package/dist/cli/model-catalog.d.ts.map +1 -0
- package/dist/cli/model-catalog.js +2135 -0
- package/dist/{models.d.ts → cli/models.d.ts} +16 -9
- package/dist/cli/models.d.ts.map +1 -0
- package/dist/cli/models.js +313 -0
- package/dist/cli/open-browser.d.ts.map +1 -0
- package/dist/cli/openai-speech-provider.d.ts +3 -0
- package/dist/cli/openai-speech-provider.d.ts.map +1 -0
- package/dist/cli/openai-speech-provider.js +28 -0
- package/dist/cli/separation-generate.d.ts +34 -0
- package/dist/cli/separation-generate.d.ts.map +1 -0
- package/dist/cli/separation-generate.js +117 -0
- package/dist/cli/speech-catalog.d.ts +35 -0
- package/dist/cli/speech-catalog.d.ts.map +1 -0
- package/dist/cli/speech-catalog.js +195 -0
- package/dist/cli/speech-generate.d.ts +86 -0
- package/dist/cli/speech-generate.d.ts.map +1 -0
- package/dist/cli/speech-generate.js +156 -0
- package/dist/cli/speech-models.d.ts +8 -0
- package/dist/cli/speech-models.d.ts.map +1 -0
- package/dist/cli/speech-models.js +109 -0
- package/dist/cli/speech-provider.test.d.ts +2 -0
- package/dist/cli/speech-provider.test.d.ts.map +1 -0
- package/dist/cli/speech-provider.test.js +197 -0
- package/dist/cli/subscription.d.ts.map +1 -0
- package/dist/cli/transcription-catalog.d.ts +35 -0
- package/dist/cli/transcription-catalog.d.ts.map +1 -0
- package/dist/cli/transcription-catalog.js +192 -0
- package/dist/cli/transcription-generate.d.ts +62 -0
- package/dist/cli/transcription-generate.d.ts.map +1 -0
- package/dist/cli/transcription-generate.js +144 -0
- package/dist/cli/transcription-models.d.ts +9 -0
- package/dist/cli/transcription-models.d.ts.map +1 -0
- package/dist/cli/transcription-models.js +100 -0
- package/dist/cli/transcription-providers.d.ts +22 -0
- package/dist/cli/transcription-providers.d.ts.map +1 -0
- package/dist/cli/transcription-providers.js +290 -0
- package/dist/cli/video-model-catalog.d.ts +2 -0
- package/dist/cli/video-model-catalog.d.ts.map +1 -0
- package/dist/cli/video-model-catalog.js +5 -0
- package/dist/cli/xai-auth.d.ts +29 -0
- package/dist/cli/xai-auth.d.ts.map +1 -0
- package/dist/cli/xai-auth.js +325 -0
- package/dist/vite/angled-screen.d.ts +57 -0
- package/dist/vite/angled-screen.d.ts.map +1 -0
- package/dist/vite/angled-screen.js +115 -0
- package/dist/vite/app.d.ts +51 -0
- package/dist/vite/app.d.ts.map +1 -0
- package/dist/vite/app.js +150 -0
- package/dist/vite/bands-shader.d.ts +47 -0
- package/dist/vite/bands-shader.d.ts.map +1 -0
- package/dist/vite/bands-shader.js +267 -0
- package/dist/vite/code-block.d.ts +107 -0
- package/dist/vite/code-block.d.ts.map +1 -0
- package/dist/vite/code-block.js +641 -0
- package/dist/vite/components.d.ts +36 -0
- package/dist/vite/components.d.ts.map +1 -0
- package/dist/vite/components.js +113 -0
- package/dist/vite/dispersion-rings-shader.d.ts +44 -0
- package/dist/vite/dispersion-rings-shader.d.ts.map +1 -0
- package/dist/vite/dispersion-rings-shader.js +282 -0
- package/dist/vite/easing-curves.d.ts +129 -0
- package/dist/vite/easing-curves.d.ts.map +1 -0
- package/dist/vite/easing-curves.js +584 -0
- package/dist/vite/easing-curves.test.d.ts +2 -0
- package/dist/vite/easing-curves.test.d.ts.map +1 -0
- package/dist/vite/easing-curves.test.js +200 -0
- package/dist/vite/generated-media-client.d.ts +35 -0
- package/dist/vite/generated-media-client.d.ts.map +1 -0
- package/dist/vite/generated-media-client.js +76 -0
- package/dist/vite/keyframes.d.ts +129 -0
- package/dist/vite/keyframes.d.ts.map +1 -0
- package/dist/vite/keyframes.js +194 -0
- package/dist/vite/layout-editor.d.ts +45 -0
- package/dist/vite/layout-editor.d.ts.map +1 -0
- package/dist/vite/layout-editor.js +587 -0
- package/dist/vite/layout-transition.d.ts +136 -0
- package/dist/vite/layout-transition.d.ts.map +1 -0
- package/dist/vite/layout-transition.js +581 -0
- package/dist/vite/liquid-gradient-shader.d.ts +62 -0
- package/dist/vite/liquid-gradient-shader.d.ts.map +1 -0
- package/dist/vite/liquid-gradient-shader.js +413 -0
- package/dist/vite/mdx-client.d.ts +15 -0
- package/dist/vite/mdx-client.d.ts.map +1 -0
- package/dist/vite/mdx-client.js +265 -0
- package/dist/vite/mdx-parse.d.ts +102 -0
- package/dist/vite/mdx-parse.d.ts.map +1 -0
- package/dist/vite/mdx-parse.js +251 -0
- package/dist/vite/mdx-provided-components.d.ts +18 -0
- package/dist/vite/mdx-provided-components.d.ts.map +1 -0
- package/dist/vite/mdx-provided-components.js +14 -0
- package/dist/vite/mdx-video.d.ts +327 -0
- package/dist/vite/mdx-video.d.ts.map +1 -0
- package/dist/vite/mdx-video.js +596 -0
- package/dist/vite/mdx-video.test.d.ts +9 -0
- package/dist/vite/mdx-video.test.d.ts.map +1 -0
- package/dist/vite/mdx-video.test.js +1945 -0
- package/dist/vite/media-components.d.ts +45 -0
- package/dist/vite/media-components.d.ts.map +1 -0
- package/dist/vite/media-components.js +355 -0
- package/dist/vite/media-duration-store.d.ts +66 -0
- package/dist/vite/media-duration-store.d.ts.map +1 -0
- package/dist/vite/media-duration-store.js +204 -0
- package/dist/vite/player-page.d.ts +35 -0
- package/dist/vite/player-page.d.ts.map +1 -0
- package/dist/vite/player-page.js +720 -0
- package/dist/vite/render-client.d.ts +18 -0
- package/dist/vite/render-client.d.ts.map +1 -0
- package/dist/vite/render-client.js +67 -0
- package/dist/vite/sdk.d.ts +221 -0
- package/dist/vite/sdk.d.ts.map +1 -0
- package/dist/vite/sdk.js +355 -0
- package/dist/vite/server-components.d.ts +55 -0
- package/dist/vite/server-components.d.ts.map +1 -0
- package/dist/vite/server-components.js +156 -0
- package/dist/vite/server-mdx.d.ts +74 -0
- package/dist/vite/server-mdx.d.ts.map +1 -0
- package/dist/vite/server-mdx.js +259 -0
- package/dist/vite/shader-renderer.d.ts +48 -0
- package/dist/vite/shader-renderer.d.ts.map +1 -0
- package/dist/vite/shader-renderer.js +411 -0
- package/dist/vite/store-hooks.d.ts +12 -0
- package/dist/vite/store-hooks.d.ts.map +1 -0
- package/dist/vite/store-hooks.js +93 -0
- package/dist/vite/store.d.ts +75 -0
- package/dist/vite/store.d.ts.map +1 -0
- package/dist/vite/store.js +52 -0
- package/dist/vite/store.test.d.ts +6 -0
- package/dist/vite/store.test.d.ts.map +1 -0
- package/dist/vite/store.test.js +172 -0
- package/dist/vite/tweakpane-hook.d.ts +94 -0
- package/dist/vite/tweakpane-hook.d.ts.map +1 -0
- package/dist/vite/tweakpane-hook.js +450 -0
- package/dist/vite/vite-plugin.d.ts +28 -0
- package/dist/vite/vite-plugin.d.ts.map +1 -0
- package/dist/vite/vite-plugin.js +538 -0
- package/dist/vite/wave-gradient-shader.d.ts +41 -0
- package/dist/vite/wave-gradient-shader.d.ts.map +1 -0
- package/dist/vite/wave-gradient-shader.js +207 -0
- package/package.json +103 -25
- package/skills/egaki/SKILL.md +162 -1
- package/src/cli/cache-utils.ts +169 -0
- package/src/cli/cached-generate.test.ts +531 -0
- package/src/cli/cached-generate.ts +348 -0
- package/src/cli/cartesia-provider.ts +249 -0
- package/src/cli/cli.ts +1818 -0
- package/src/cli/clone-generate.ts +202 -0
- package/src/{credentials.ts → cli/credentials.ts} +90 -7
- package/src/cli/elevenlabs-speech-provider.ts +243 -0
- package/src/cli/generate.test.ts +166 -0
- package/src/cli/generate.ts +912 -0
- package/src/{login.ts → cli/login.ts} +48 -3
- package/src/cli/main.ts +8 -0
- package/src/cli/model-catalog.ts +2336 -0
- package/src/cli/models.ts +408 -0
- package/src/cli/openai-speech-provider.ts +34 -0
- package/src/cli/separation-generate.ts +187 -0
- package/src/cli/speech-catalog.ts +253 -0
- package/src/cli/speech-generate.ts +275 -0
- package/src/cli/speech-models.ts +146 -0
- package/src/cli/speech-provider.test.ts +226 -0
- package/src/cli/transcription-catalog.ts +250 -0
- package/src/cli/transcription-generate.ts +214 -0
- package/src/cli/transcription-models.ts +136 -0
- package/src/cli/transcription-providers.ts +427 -0
- package/src/cli/video-model-catalog.ts +14 -0
- package/src/cli/xai-auth.ts +396 -0
- package/src/vite/angled-screen.tsx +214 -0
- package/src/vite/app.tsx +197 -0
- package/src/vite/bands-shader.tsx +319 -0
- package/src/vite/code-block.tsx +939 -0
- package/src/vite/components.tsx +278 -0
- package/src/vite/dispersion-rings-shader.tsx +331 -0
- package/src/vite/easing-curves.test.ts +232 -0
- package/src/vite/easing-curves.ts +743 -0
- package/src/vite/generated-media-client.tsx +121 -0
- package/src/vite/keyframes.tsx +347 -0
- package/src/vite/layout-editor.tsx +709 -0
- package/src/vite/layout-transition.tsx +706 -0
- package/src/vite/liquid-gradient-shader.tsx +474 -0
- package/src/vite/mdx-client.tsx +420 -0
- package/src/vite/mdx-parse.ts +343 -0
- package/src/vite/mdx-provided-components.ts +19 -0
- package/src/vite/mdx-video.test.tsx +2219 -0
- package/src/vite/mdx-video.tsx +976 -0
- package/src/vite/media-components.tsx +473 -0
- package/src/vite/media-duration-store.ts +225 -0
- package/src/vite/motion-timing.ts +194 -0
- package/src/vite/player-page.tsx +1107 -0
- package/src/vite/render-client.ts +84 -0
- package/src/vite/sdk.ts +598 -0
- package/src/vite/server-components.tsx +241 -0
- package/src/vite/server-mdx.ts +285 -0
- package/src/vite/shader-renderer.tsx +535 -0
- package/src/vite/store-hooks.ts +117 -0
- package/src/vite/store.test.ts +202 -0
- package/src/vite/store.ts +115 -0
- package/src/vite/styles.css +21 -0
- package/src/vite/tweakpane-hook.tsx +593 -0
- package/src/vite/virtual-modules.d.ts +30 -0
- package/src/vite/vite-plugin.ts +584 -0
- package/src/vite/wave-gradient-shader.tsx +253 -0
- package/README.md +0 -266
- package/dist/antigravity-auth.d.ts +0 -15
- package/dist/antigravity-auth.d.ts.map +0 -1
- package/dist/antigravity-auth.js +0 -544
- package/dist/antigravity-image.d.ts +0 -11
- package/dist/antigravity-image.d.ts.map +0 -1
- package/dist/antigravity-image.js +0 -285
- package/dist/chatgpt-auth.d.ts.map +0 -1
- package/dist/cli.d.ts +0 -3
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.js +0 -968
- package/dist/credentials.d.ts.map +0 -1
- package/dist/gateway-model-candidates.generated.d.ts +0 -11
- package/dist/gateway-model-candidates.generated.d.ts.map +0 -1
- package/dist/gateway-model-candidates.generated.js +0 -64
- package/dist/login.d.ts.map +0 -1
- package/dist/model-catalog.d.ts +0 -42
- package/dist/model-catalog.d.ts.map +0 -1
- package/dist/model-catalog.js +0 -1001
- package/dist/models.d.ts.map +0 -1
- package/dist/models.js +0 -246
- package/dist/open-browser.d.ts.map +0 -1
- package/dist/subscription.d.ts.map +0 -1
- package/dist/video-model-catalog.d.ts +0 -52
- package/dist/video-model-catalog.d.ts.map +0 -1
- package/dist/video-model-catalog.js +0 -306
- package/src/cli.ts +0 -1378
- package/src/model-catalog.ts +0 -1077
- package/src/models.ts +0 -309
- package/src/video-model-catalog.ts +0 -363
- /package/dist/{chatgpt-auth.d.ts → cli/chatgpt-auth.d.ts} +0 -0
- /package/dist/{chatgpt-auth.js → cli/chatgpt-auth.js} +0 -0
- /package/dist/{login.d.ts → cli/login.d.ts} +0 -0
- /package/dist/{open-browser.d.ts → cli/open-browser.d.ts} +0 -0
- /package/dist/{open-browser.js → cli/open-browser.js} +0 -0
- /package/dist/{subscription.d.ts → cli/subscription.d.ts} +0 -0
- /package/dist/{subscription.js → cli/subscription.js} +0 -0
- /package/src/{chatgpt-auth.ts → cli/chatgpt-auth.ts} +0 -0
- /package/src/{open-browser.ts → cli/open-browser.ts} +0 -0
- /package/src/{subscription.ts → cli/subscription.ts} +0 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Voice cloning via Cartesia and ElevenLabs APIs.
|
|
2
|
+
// Both are simple multipart/form-data HTTP calls, no SDK needed.
|
|
3
|
+
// Returns Error | Result (errore style) instead of throwing.
|
|
4
|
+
//
|
|
5
|
+
// Cartesia: POST https://api.cartesia.ai/voices/clone
|
|
6
|
+
// Instant clone, up to 10s of audio, free.
|
|
7
|
+
// Docs: https://docs.cartesia.ai/api-reference/voices/clone
|
|
8
|
+
// Best practices: https://docs.cartesia.ai/build-with-cartesia/capability-guides/clone-voices
|
|
9
|
+
//
|
|
10
|
+
// ElevenLabs: POST https://api.elevenlabs.io/v1/voices/add
|
|
11
|
+
// Instant voice clone (IVC), 1-3 min recommended.
|
|
12
|
+
// Docs: https://elevenlabs.io/docs/api-reference/voices/ivc/create
|
|
13
|
+
import { injectCredentialsToEnv } from './credentials.js'
|
|
14
|
+
|
|
15
|
+
// ─── types ──────────────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
export type CloneProvider = 'cartesia' | 'elevenlabs'
|
|
18
|
+
|
|
19
|
+
export interface CloneVoiceOptions {
|
|
20
|
+
/** Raw audio bytes of the clip to clone from. */
|
|
21
|
+
audio: Uint8Array
|
|
22
|
+
/** Name for the cloned voice. */
|
|
23
|
+
name: string
|
|
24
|
+
/** Which provider to use. */
|
|
25
|
+
provider: CloneProvider
|
|
26
|
+
/** Cartesia only: ISO 639-1 language code. Default: 'en'. Ignored by ElevenLabs. */
|
|
27
|
+
language?: string
|
|
28
|
+
/** Optional description for the voice. */
|
|
29
|
+
description?: string
|
|
30
|
+
/** Original filename for MIME type inference. */
|
|
31
|
+
filename?: string
|
|
32
|
+
/** Cartesia: optional base voice ID to derive from. */
|
|
33
|
+
baseVoiceId?: string
|
|
34
|
+
/** ElevenLabs: apply AI noise removal to the clip. */
|
|
35
|
+
removeBackgroundNoise?: boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface CloneVoiceResult {
|
|
39
|
+
voiceId: string
|
|
40
|
+
name: string
|
|
41
|
+
provider: CloneProvider
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// ─── validation ─────────────────────────────────────────────────────────────
|
|
45
|
+
|
|
46
|
+
const CARTESIA_LANGUAGES = new Set([
|
|
47
|
+
'en', 'fr', 'de', 'es', 'pt', 'zh', 'ja', 'hi', 'it', 'ko', 'nl', 'pl',
|
|
48
|
+
'ru', 'sv', 'tr', 'tl', 'bg', 'ro', 'ar', 'cs', 'el', 'fi', 'hr', 'ms',
|
|
49
|
+
'sk', 'da', 'ta', 'uk', 'hu', 'no', 'vi', 'bn', 'th', 'he', 'ka', 'id',
|
|
50
|
+
'te', 'gu', 'kn', 'ml', 'mr', 'pa',
|
|
51
|
+
])
|
|
52
|
+
|
|
53
|
+
function validateOptions(opts: CloneVoiceOptions): Error | void {
|
|
54
|
+
if (opts.audio.length === 0) {
|
|
55
|
+
return new Error('No audio data provided. Pass an audio file or use --stdin.')
|
|
56
|
+
}
|
|
57
|
+
if (!opts.name || opts.name.trim().length === 0) {
|
|
58
|
+
return new Error('Voice name is required. Use --name "My Voice".')
|
|
59
|
+
}
|
|
60
|
+
if (opts.provider === 'cartesia' && opts.language && !CARTESIA_LANGUAGES.has(opts.language)) {
|
|
61
|
+
return new Error(
|
|
62
|
+
`Unsupported language "${opts.language}" for Cartesia. ` +
|
|
63
|
+
`Supported: ${[...CARTESIA_LANGUAGES].join(', ')}`,
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ─── MIME type helper ───────────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
function mimeTypeFromFilename(filename?: string): string {
|
|
71
|
+
if (!filename) return 'audio/mpeg'
|
|
72
|
+
const ext = filename.split('.').pop()?.toLowerCase()
|
|
73
|
+
const map: Record<string, string> = {
|
|
74
|
+
mp3: 'audio/mpeg',
|
|
75
|
+
wav: 'audio/wav',
|
|
76
|
+
flac: 'audio/flac',
|
|
77
|
+
ogg: 'audio/ogg',
|
|
78
|
+
oga: 'audio/ogg',
|
|
79
|
+
webm: 'audio/webm',
|
|
80
|
+
mpga: 'audio/mpeg',
|
|
81
|
+
m4a: 'audio/mp4',
|
|
82
|
+
}
|
|
83
|
+
return map[ext ?? ''] ?? 'audio/mpeg'
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ─── Cartesia clone ─────────────────────────────────────────────────────────
|
|
87
|
+
|
|
88
|
+
const CARTESIA_API_BASE = 'https://api.cartesia.ai'
|
|
89
|
+
const CARTESIA_API_VERSION = '2026-03-01'
|
|
90
|
+
|
|
91
|
+
async function cloneVoiceCartesia(opts: CloneVoiceOptions): Promise<Error | CloneVoiceResult> {
|
|
92
|
+
const apiKey = process.env['CARTESIA_API_KEY']
|
|
93
|
+
if (!apiKey) {
|
|
94
|
+
return new Error(
|
|
95
|
+
'CARTESIA_API_KEY not found. Run: egaki login --provider cartesia\n' +
|
|
96
|
+
'Get your key at https://play.cartesia.ai/keys',
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const language = opts.language ?? 'en'
|
|
101
|
+
const mime = mimeTypeFromFilename(opts.filename)
|
|
102
|
+
const ext = opts.filename?.split('.').pop() ?? 'mp3'
|
|
103
|
+
|
|
104
|
+
const formData = new FormData()
|
|
105
|
+
const blob = new Blob([Buffer.from(opts.audio)], { type: mime })
|
|
106
|
+
formData.append('clip', new File([blob], `clip.${ext}`, { type: mime }))
|
|
107
|
+
formData.append('name', opts.name.trim())
|
|
108
|
+
formData.append('language', language)
|
|
109
|
+
if (opts.description) formData.append('description', opts.description)
|
|
110
|
+
if (opts.baseVoiceId) formData.append('base_voice_id', opts.baseVoiceId)
|
|
111
|
+
|
|
112
|
+
const response = await fetch(`${CARTESIA_API_BASE}/voices/clone`, {
|
|
113
|
+
method: 'POST',
|
|
114
|
+
headers: {
|
|
115
|
+
'Cartesia-Version': CARTESIA_API_VERSION,
|
|
116
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
117
|
+
},
|
|
118
|
+
body: formData,
|
|
119
|
+
}).catch((cause) => new Error('Cartesia clone request failed', { cause }))
|
|
120
|
+
if (response instanceof Error) return response
|
|
121
|
+
|
|
122
|
+
if (!response.ok) {
|
|
123
|
+
const errorText = await response.text().catch(() => '')
|
|
124
|
+
return new Error(`Cartesia clone API error ${response.status}: ${errorText || response.statusText}`)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const json = await response.json().catch(
|
|
128
|
+
(cause) => new Error('Cartesia clone API returned invalid JSON', { cause }),
|
|
129
|
+
) as { id?: string; name?: string } | Error
|
|
130
|
+
if (json instanceof Error) return json
|
|
131
|
+
if (!json.id) return new Error('Cartesia clone API response did not include a voice id')
|
|
132
|
+
return {
|
|
133
|
+
voiceId: json.id,
|
|
134
|
+
name: json.name ?? opts.name.trim(),
|
|
135
|
+
provider: 'cartesia',
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ─── ElevenLabs clone ───────────────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
const ELEVENLABS_API_BASE = 'https://api.elevenlabs.io'
|
|
142
|
+
|
|
143
|
+
async function cloneVoiceElevenLabs(opts: CloneVoiceOptions): Promise<Error | CloneVoiceResult> {
|
|
144
|
+
const apiKey = process.env['ELEVENLABS_API_KEY']
|
|
145
|
+
if (!apiKey) {
|
|
146
|
+
return new Error(
|
|
147
|
+
'ELEVENLABS_API_KEY not found. Run: egaki login --provider elevenlabs\n' +
|
|
148
|
+
'Get your key at https://elevenlabs.io/app/settings/api-keys',
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const mime = mimeTypeFromFilename(opts.filename)
|
|
153
|
+
const ext = opts.filename?.split('.').pop() ?? 'mp3'
|
|
154
|
+
|
|
155
|
+
const formData = new FormData()
|
|
156
|
+
formData.append('name', opts.name.trim())
|
|
157
|
+
const blob = new Blob([Buffer.from(opts.audio)], { type: mime })
|
|
158
|
+
formData.append('files', new File([blob], `clip.${ext}`, { type: mime }))
|
|
159
|
+
if (opts.description) formData.append('description', opts.description)
|
|
160
|
+
if (opts.removeBackgroundNoise) formData.append('remove_background_noise', 'true')
|
|
161
|
+
|
|
162
|
+
const response = await fetch(`${ELEVENLABS_API_BASE}/v1/voices/add`, {
|
|
163
|
+
method: 'POST',
|
|
164
|
+
headers: {
|
|
165
|
+
'xi-api-key': apiKey,
|
|
166
|
+
},
|
|
167
|
+
body: formData,
|
|
168
|
+
}).catch((cause) => new Error('ElevenLabs clone request failed', { cause }))
|
|
169
|
+
if (response instanceof Error) return response
|
|
170
|
+
|
|
171
|
+
if (!response.ok) {
|
|
172
|
+
const errorText = await response.text().catch(() => '')
|
|
173
|
+
return new Error(`ElevenLabs clone API error ${response.status}: ${errorText || response.statusText}`)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const json = await response.json().catch(
|
|
177
|
+
(cause) => new Error('ElevenLabs clone API returned invalid JSON', { cause }),
|
|
178
|
+
) as { voice_id?: string; requires_verification?: boolean } | Error
|
|
179
|
+
if (json instanceof Error) return json
|
|
180
|
+
if (!json.voice_id) return new Error('ElevenLabs clone API response did not include a voice_id')
|
|
181
|
+
return {
|
|
182
|
+
voiceId: json.voice_id,
|
|
183
|
+
name: opts.name.trim(),
|
|
184
|
+
provider: 'elevenlabs',
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ─── main entry point ───────────────────────────────────────────────────────
|
|
189
|
+
|
|
190
|
+
export async function cloneVoiceUncached(
|
|
191
|
+
opts: CloneVoiceOptions,
|
|
192
|
+
): Promise<Error | CloneVoiceResult> {
|
|
193
|
+
injectCredentialsToEnv()
|
|
194
|
+
|
|
195
|
+
const validationError = validateOptions(opts)
|
|
196
|
+
if (validationError) return validationError
|
|
197
|
+
|
|
198
|
+
if (opts.provider === 'elevenlabs') {
|
|
199
|
+
return cloneVoiceElevenLabs(opts)
|
|
200
|
+
}
|
|
201
|
+
return cloneVoiceCartesia(opts)
|
|
202
|
+
}
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
// Reads and writes API keys from ~/.config/egaki/credentials.json.
|
|
3
3
|
// Keys are stored per-provider and injected as env vars at runtime.
|
|
4
4
|
//
|
|
5
|
-
// Most providers store a plain API key string.
|
|
6
|
-
//
|
|
5
|
+
// Most providers store a plain API key string. OAuth providers store structured
|
|
6
|
+
// token objects for their custom backend flows.
|
|
7
7
|
import fs from 'node:fs'
|
|
8
8
|
import path from 'node:path'
|
|
9
9
|
import os from 'node:os'
|
|
10
10
|
import type { ChatGptAuth } from './chatgpt-auth.js'
|
|
11
|
+
import type { XaiAuth } from './xai-auth.js'
|
|
11
12
|
|
|
12
13
|
export type ProviderInfo = {
|
|
13
14
|
envVar: string
|
|
@@ -57,6 +58,37 @@ export const PROVIDERS: Record<string, ProviderInfo> = {
|
|
|
57
58
|
label: 'fal.ai (Flux, SDXL, Recraft)',
|
|
58
59
|
hint: 'Get your key at https://fal.ai/dashboard/keys',
|
|
59
60
|
},
|
|
61
|
+
xai: {
|
|
62
|
+
envVar: 'XAI_API_KEY',
|
|
63
|
+
label: 'xAI (API key)',
|
|
64
|
+
hint: 'Get your key at https://console.x.ai/team/default/api-keys',
|
|
65
|
+
},
|
|
66
|
+
'xai-oauth': {
|
|
67
|
+
envVar: 'XAI_API_KEY',
|
|
68
|
+
label: 'xAI Grok Build (use your subscription)',
|
|
69
|
+
hint: 'Sign in with your xAI / Grok account via browser',
|
|
70
|
+
oauth: true,
|
|
71
|
+
},
|
|
72
|
+
elevenlabs: {
|
|
73
|
+
envVar: 'ELEVENLABS_API_KEY',
|
|
74
|
+
label: 'ElevenLabs (speech)',
|
|
75
|
+
hint: 'Get your key at https://elevenlabs.io/app/settings/api-keys',
|
|
76
|
+
},
|
|
77
|
+
cartesia: {
|
|
78
|
+
envVar: 'CARTESIA_API_KEY',
|
|
79
|
+
label: 'Cartesia (speech)',
|
|
80
|
+
hint: 'Get your key at https://play.cartesia.ai/keys',
|
|
81
|
+
},
|
|
82
|
+
deepgram: {
|
|
83
|
+
envVar: 'DEEPGRAM_API_KEY',
|
|
84
|
+
label: 'Deepgram (transcription)',
|
|
85
|
+
hint: 'Get your key at https://console.deepgram.com/api-keys',
|
|
86
|
+
},
|
|
87
|
+
groq: {
|
|
88
|
+
envVar: 'GROQ_API_KEY',
|
|
89
|
+
label: 'Groq (fast transcription)',
|
|
90
|
+
hint: 'Get your key at https://console.groq.com/keys',
|
|
91
|
+
},
|
|
60
92
|
}
|
|
61
93
|
|
|
62
94
|
export const EGAKI_GATEWAY_URL = 'https://egaki.org/v3/ai'
|
|
@@ -72,8 +104,8 @@ function getCredentialsPath(): string {
|
|
|
72
104
|
return path.join(getConfigDir(), 'credentials.json')
|
|
73
105
|
}
|
|
74
106
|
|
|
75
|
-
/** A credential value is
|
|
76
|
-
export type CredentialValue = string | ChatGptAuth
|
|
107
|
+
/** A credential value is a plain API key string, a ChatGPT OAuth object, or an xAI OAuth object. */
|
|
108
|
+
export type CredentialValue = string | ChatGptAuth | XaiAuth
|
|
77
109
|
export type Credentials = Record<string, CredentialValue>
|
|
78
110
|
|
|
79
111
|
export function readCredentials(): Credentials {
|
|
@@ -115,6 +147,19 @@ export function getChatGptAuth(): ChatGptAuth | undefined {
|
|
|
115
147
|
return undefined
|
|
116
148
|
}
|
|
117
149
|
|
|
150
|
+
export function saveXaiAuth(auth: XaiAuth): void {
|
|
151
|
+
const creds = readCredentials()
|
|
152
|
+
creds['xai-oauth'] = auth
|
|
153
|
+
writeCredentials(creds)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function getXaiAuth(): XaiAuth | undefined {
|
|
157
|
+
const creds = readCredentials()
|
|
158
|
+
const val = creds['xai-oauth']
|
|
159
|
+
if (!val || typeof val !== 'object') return undefined
|
|
160
|
+
return val
|
|
161
|
+
}
|
|
162
|
+
|
|
118
163
|
export function removeProviderKey(provider: string): void {
|
|
119
164
|
const creds = readCredentials()
|
|
120
165
|
delete creds[provider]
|
|
@@ -127,17 +172,26 @@ export function getProviderKey(provider: string): string | undefined {
|
|
|
127
172
|
return typeof val === 'string' ? val : undefined
|
|
128
173
|
}
|
|
129
174
|
|
|
130
|
-
// Inject all stored non-
|
|
175
|
+
// Inject all stored non-OAuth credentials as env vars so the AI SDK
|
|
131
176
|
// providers pick them up automatically. Env vars already set by the user take
|
|
132
177
|
// precedence (we don't overwrite them).
|
|
178
|
+
//
|
|
179
|
+
// Special case: if xAI OAuth tokens exist, skip injecting the stored xai API
|
|
180
|
+
// key so OAuth takes priority. Only an explicit XAI_API_KEY env var (set by
|
|
181
|
+
// the user before egaki runs) will override OAuth.
|
|
133
182
|
export function injectCredentialsToEnv(): void {
|
|
134
183
|
const creds = readCredentials()
|
|
184
|
+
const hasXaiOAuth = Boolean(getXaiAuth())
|
|
135
185
|
for (const [provider, value] of Object.entries(creds)) {
|
|
136
186
|
const info = PROVIDERS[provider]
|
|
137
187
|
if (!info) continue
|
|
138
188
|
|
|
139
|
-
// Skip
|
|
189
|
+
// Skip OAuth entries — handled separately via custom fetch
|
|
140
190
|
if (provider === 'chatgpt') continue
|
|
191
|
+
if (provider === 'xai-oauth') continue
|
|
192
|
+
|
|
193
|
+
// Skip stored xai key when OAuth tokens exist (OAuth takes priority)
|
|
194
|
+
if (provider === 'xai' && hasXaiOAuth && !process.env[info.envVar]) continue
|
|
141
195
|
|
|
142
196
|
if (typeof value === 'string' && !process.env[info.envVar]) {
|
|
143
197
|
process.env[info.envVar] = value
|
|
@@ -153,6 +207,16 @@ export function shouldUseChatGptBackend(): boolean {
|
|
|
153
207
|
return !process.env['OPENAI_API_KEY'] && Boolean(getChatGptAuth())
|
|
154
208
|
}
|
|
155
209
|
|
|
210
|
+
/**
|
|
211
|
+
* Returns true when xAI models should use the OAuth flow instead of a direct
|
|
212
|
+
* XAI_API_KEY. Priority: explicit env var > xai-oauth tokens > stored xai key.
|
|
213
|
+
* Only an explicit XAI_API_KEY (set before egaki runs) overrides OAuth.
|
|
214
|
+
*/
|
|
215
|
+
export function shouldUseXaiOAuth(): boolean {
|
|
216
|
+
if (process.env['XAI_API_KEY']) return false
|
|
217
|
+
return Boolean(getXaiAuth())
|
|
218
|
+
}
|
|
219
|
+
|
|
156
220
|
// Check if a provider has a key available (from env or stored credentials).
|
|
157
221
|
// Returns the source of the key for display purposes.
|
|
158
222
|
export function getKeyStatus(provider: string): {
|
|
@@ -164,7 +228,7 @@ export function getKeyStatus(provider: string): {
|
|
|
164
228
|
return { available: false, source: 'none' }
|
|
165
229
|
}
|
|
166
230
|
|
|
167
|
-
//
|
|
231
|
+
// OAuth providers are special cases
|
|
168
232
|
if (provider === 'chatgpt') {
|
|
169
233
|
const auth = getChatGptAuth()
|
|
170
234
|
if (auth) {
|
|
@@ -173,6 +237,25 @@ export function getKeyStatus(provider: string): {
|
|
|
173
237
|
return { available: false, source: 'none' }
|
|
174
238
|
}
|
|
175
239
|
|
|
240
|
+
if (provider === 'xai-oauth') {
|
|
241
|
+
const auth = getXaiAuth()
|
|
242
|
+
if (auth) {
|
|
243
|
+
return { available: true, source: 'oauth' }
|
|
244
|
+
}
|
|
245
|
+
return { available: false, source: 'none' }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// For xai provider, report the same priority used by generation:
|
|
249
|
+
// explicit env var > xai-oauth tokens > stored xai key.
|
|
250
|
+
if (provider === 'xai') {
|
|
251
|
+
if (process.env[info.envVar]) return { available: true, source: 'env' }
|
|
252
|
+
const oauthAuth = getXaiAuth()
|
|
253
|
+
if (oauthAuth) return { available: true, source: 'oauth' }
|
|
254
|
+
const stored = getProviderKey(provider)
|
|
255
|
+
if (stored) return { available: true, source: 'stored' }
|
|
256
|
+
return { available: false, source: 'none' }
|
|
257
|
+
}
|
|
258
|
+
|
|
176
259
|
if (process.env[info.envVar]) {
|
|
177
260
|
return { available: true, source: 'env' }
|
|
178
261
|
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// ElevenLabs speech provider for egaki.
|
|
2
|
+
// Uses the streaming endpoint /v1/text-to-speech/{voiceId}/stream/with-timestamps
|
|
3
|
+
// to get audio chunks + character timestamps in a single streamed response.
|
|
4
|
+
// Character-level alignment is grouped into word-level timestamps.
|
|
5
|
+
//
|
|
6
|
+
// API docs: https://elevenlabs.io/docs/api-reference/text-to-speech/stream-with-timestamps
|
|
7
|
+
//
|
|
8
|
+
// The streaming response is NDJSON (newline-delimited JSON), NOT SSE.
|
|
9
|
+
// Each line is a JSON object: { audio_base64, alignment?, normalized_alignment? }
|
|
10
|
+
// Alignment data (character-level timing) is included in the first chunk only.
|
|
11
|
+
// We use `normalized_alignment` (timing of what was actually spoken).
|
|
12
|
+
|
|
13
|
+
import type { SpeechProvider, SpeechProviderOptions, SpeechProviderResult } from './speech-generate.js'
|
|
14
|
+
import type { WordTimestamp } from './transcription-generate.js'
|
|
15
|
+
|
|
16
|
+
const ELEVENLABS_API_BASE = 'https://api.elevenlabs.io'
|
|
17
|
+
const DEFAULT_VOICE_ID = '21m00Tcm4TlvDq8ikWAM' // Rachel
|
|
18
|
+
|
|
19
|
+
// ─── format mapping ──────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
const FORMAT_MAP: Record<string, string> = {
|
|
22
|
+
mp3: 'mp3_44100_128',
|
|
23
|
+
mp3_32: 'mp3_44100_32',
|
|
24
|
+
mp3_64: 'mp3_44100_64',
|
|
25
|
+
mp3_96: 'mp3_44100_96',
|
|
26
|
+
mp3_128: 'mp3_44100_128',
|
|
27
|
+
mp3_192: 'mp3_44100_192',
|
|
28
|
+
pcm: 'pcm_44100',
|
|
29
|
+
pcm_16000: 'pcm_16000',
|
|
30
|
+
pcm_22050: 'pcm_22050',
|
|
31
|
+
pcm_24000: 'pcm_24000',
|
|
32
|
+
pcm_44100: 'pcm_44100',
|
|
33
|
+
ulaw: 'ulaw_8000',
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function resolveOutputFormat(format?: string): string {
|
|
37
|
+
if (!format) return 'mp3_44100_128'
|
|
38
|
+
return FORMAT_MAP[format] || format
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function formatToMediaType(format: string): string {
|
|
42
|
+
if (format.startsWith('mp3')) return 'audio/mpeg'
|
|
43
|
+
if (format.startsWith('pcm')) return 'audio/pcm'
|
|
44
|
+
if (format.startsWith('ulaw')) return 'audio/basic'
|
|
45
|
+
if (format.startsWith('opus')) return 'audio/opus'
|
|
46
|
+
if (format.startsWith('wav')) return 'audio/wav'
|
|
47
|
+
return 'audio/mpeg'
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ─── character-to-word grouping ──────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
interface CharacterAlignment {
|
|
53
|
+
characters: string[]
|
|
54
|
+
character_start_times_seconds: number[]
|
|
55
|
+
character_end_times_seconds: number[]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Group character-level timestamps into word-level timestamps.
|
|
60
|
+
* Characters are grouped by splitting on whitespace characters.
|
|
61
|
+
* Each word's start time is the start of its first character,
|
|
62
|
+
* and its end time is the end of its last character.
|
|
63
|
+
*/
|
|
64
|
+
export function groupCharactersToWords(alignment: CharacterAlignment): WordTimestamp[] {
|
|
65
|
+
const words: WordTimestamp[] = []
|
|
66
|
+
let currentWord = ''
|
|
67
|
+
let wordStart = -1
|
|
68
|
+
let wordEnd = -1
|
|
69
|
+
|
|
70
|
+
for (let i = 0; i < alignment.characters.length; i++) {
|
|
71
|
+
const char = alignment.characters[i]!
|
|
72
|
+
const charStart = alignment.character_start_times_seconds[i]!
|
|
73
|
+
const charEnd = alignment.character_end_times_seconds[i]!
|
|
74
|
+
|
|
75
|
+
if (/\s/.test(char)) {
|
|
76
|
+
if (currentWord) {
|
|
77
|
+
words.push({ word: currentWord, startSecond: wordStart, endSecond: wordEnd })
|
|
78
|
+
currentWord = ''
|
|
79
|
+
wordStart = -1
|
|
80
|
+
wordEnd = -1
|
|
81
|
+
}
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
currentWord += char
|
|
86
|
+
if (wordStart < 0) wordStart = charStart
|
|
87
|
+
wordEnd = charEnd
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (currentWord) {
|
|
91
|
+
words.push({ word: currentWord, startSecond: wordStart, endSecond: wordEnd })
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return words
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ─── NDJSON stream types ─────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
interface ElevenLabsStreamChunk {
|
|
100
|
+
audio_base64: string
|
|
101
|
+
alignment?: CharacterAlignment | null
|
|
102
|
+
normalized_alignment?: CharacterAlignment | null
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ─── NDJSON stream parser ────────────────────────────────────────────────────
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Parse an ElevenLabs NDJSON stream into audio chunks and character alignment.
|
|
109
|
+
* Each line is a JSON object with audio_base64 and optional alignment data.
|
|
110
|
+
* Alignment is typically only in the first chunk.
|
|
111
|
+
*/
|
|
112
|
+
export async function parseElevenLabsStream(
|
|
113
|
+
body: ReadableStream<Uint8Array>,
|
|
114
|
+
): Promise<{ audioChunks: Uint8Array[]; alignment?: CharacterAlignment }> {
|
|
115
|
+
const audioChunks: Uint8Array[] = []
|
|
116
|
+
let alignment: CharacterAlignment | undefined
|
|
117
|
+
|
|
118
|
+
const reader = body.pipeThrough(new TextDecoderStream() as any).getReader()
|
|
119
|
+
let buffer = ''
|
|
120
|
+
|
|
121
|
+
while (true) {
|
|
122
|
+
const { done, value } = await reader.read()
|
|
123
|
+
if (done) break
|
|
124
|
+
buffer += value
|
|
125
|
+
|
|
126
|
+
// Process complete lines
|
|
127
|
+
let newlineIdx: number
|
|
128
|
+
while ((newlineIdx = buffer.indexOf('\n')) >= 0) {
|
|
129
|
+
const line = buffer.slice(0, newlineIdx).trim()
|
|
130
|
+
buffer = buffer.slice(newlineIdx + 1)
|
|
131
|
+
if (!line) continue
|
|
132
|
+
|
|
133
|
+
let chunk: ElevenLabsStreamChunk
|
|
134
|
+
try {
|
|
135
|
+
chunk = JSON.parse(line)
|
|
136
|
+
} catch {
|
|
137
|
+
continue
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (chunk.audio_base64) {
|
|
141
|
+
audioChunks.push(new Uint8Array(Buffer.from(chunk.audio_base64, 'base64')))
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Capture alignment from first chunk that has it
|
|
145
|
+
if (!alignment) {
|
|
146
|
+
const align = chunk.normalized_alignment || chunk.alignment
|
|
147
|
+
if (align) alignment = align
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Process any remaining data in buffer (last line without trailing newline)
|
|
153
|
+
const remaining = buffer.trim()
|
|
154
|
+
if (remaining) {
|
|
155
|
+
try {
|
|
156
|
+
const chunk: ElevenLabsStreamChunk = JSON.parse(remaining)
|
|
157
|
+
if (chunk.audio_base64) {
|
|
158
|
+
audioChunks.push(new Uint8Array(Buffer.from(chunk.audio_base64, 'base64')))
|
|
159
|
+
}
|
|
160
|
+
if (!alignment) {
|
|
161
|
+
const align = chunk.normalized_alignment || chunk.alignment
|
|
162
|
+
if (align) alignment = align
|
|
163
|
+
}
|
|
164
|
+
} catch { /* ignore malformed trailing data */ }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return { audioChunks, alignment }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Concatenate multiple Uint8Array chunks into one. */
|
|
171
|
+
function concatUint8Arrays(chunks: Uint8Array[]): Uint8Array {
|
|
172
|
+
const totalLength = chunks.reduce((sum, c) => sum + c.length, 0)
|
|
173
|
+
const result = new Uint8Array(totalLength)
|
|
174
|
+
let offset = 0
|
|
175
|
+
for (const chunk of chunks) {
|
|
176
|
+
result.set(chunk, offset)
|
|
177
|
+
offset += chunk.length
|
|
178
|
+
}
|
|
179
|
+
return result
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ─── provider ────────────────────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
export function createElevenLabsSpeechProvider(): SpeechProvider {
|
|
185
|
+
return {
|
|
186
|
+
async generate(options: SpeechProviderOptions): Promise<SpeechProviderResult> {
|
|
187
|
+
const apiKey = process.env['ELEVENLABS_API_KEY']
|
|
188
|
+
if (!apiKey) {
|
|
189
|
+
throw new Error('Missing ELEVENLABS_API_KEY. Run: egaki login --provider elevenlabs --key <key>')
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const voiceId = options.voice || DEFAULT_VOICE_ID
|
|
193
|
+
const outputFormat = resolveOutputFormat(options.outputFormat)
|
|
194
|
+
|
|
195
|
+
const body: Record<string, unknown> = {
|
|
196
|
+
text: options.text,
|
|
197
|
+
model_id: options.modelId,
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (options.language) {
|
|
201
|
+
body.language_code = options.language
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (options.speed != null) {
|
|
205
|
+
body.voice_settings = { speed: options.speed }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const queryParams = new URLSearchParams({ output_format: outputFormat })
|
|
209
|
+
const url = `${ELEVENLABS_API_BASE}/v1/text-to-speech/${voiceId}/stream/with-timestamps?${queryParams}`
|
|
210
|
+
|
|
211
|
+
const response = await fetch(url, {
|
|
212
|
+
method: 'POST',
|
|
213
|
+
headers: {
|
|
214
|
+
'Content-Type': 'application/json',
|
|
215
|
+
'xi-api-key': apiKey,
|
|
216
|
+
},
|
|
217
|
+
body: JSON.stringify(body),
|
|
218
|
+
signal: options.abortSignal,
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
if (!response.ok) {
|
|
222
|
+
const errorText = await response.text().catch(() => '')
|
|
223
|
+
throw new Error(
|
|
224
|
+
`ElevenLabs TTS API error ${response.status}: ${errorText || response.statusText}`,
|
|
225
|
+
)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (!response.body) {
|
|
229
|
+
throw new Error('ElevenLabs TTS stream response has no body')
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const { audioChunks, alignment } = await parseElevenLabsStream(response.body)
|
|
233
|
+
const audio = concatUint8Arrays(audioChunks)
|
|
234
|
+
const timestamps = alignment ? groupCharactersToWords(alignment) : undefined
|
|
235
|
+
|
|
236
|
+
return {
|
|
237
|
+
audio,
|
|
238
|
+
mediaType: formatToMediaType(outputFormat),
|
|
239
|
+
timestamps,
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
}
|
|
243
|
+
}
|