whale-code 6.4.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.
Files changed (319) hide show
  1. package/README.md +95 -0
  2. package/bin/swag-agent.js +9 -0
  3. package/bin/swagmanager-mcp.js +321 -0
  4. package/dist/cli/app.d.ts +26 -0
  5. package/dist/cli/app.js +64 -0
  6. package/dist/cli/chat/AgentSelector.d.ts +14 -0
  7. package/dist/cli/chat/AgentSelector.js +14 -0
  8. package/dist/cli/chat/ChatApp.d.ts +9 -0
  9. package/dist/cli/chat/ChatApp.js +267 -0
  10. package/dist/cli/chat/ChatInput.d.ts +39 -0
  11. package/dist/cli/chat/ChatInput.js +509 -0
  12. package/dist/cli/chat/MarkdownText.d.ts +10 -0
  13. package/dist/cli/chat/MarkdownText.js +20 -0
  14. package/dist/cli/chat/MessageList.d.ts +37 -0
  15. package/dist/cli/chat/MessageList.js +80 -0
  16. package/dist/cli/chat/ModelSelector.d.ts +20 -0
  17. package/dist/cli/chat/ModelSelector.js +73 -0
  18. package/dist/cli/chat/RewindViewer.d.ts +26 -0
  19. package/dist/cli/chat/RewindViewer.js +185 -0
  20. package/dist/cli/chat/StoreSelector.d.ts +14 -0
  21. package/dist/cli/chat/StoreSelector.js +24 -0
  22. package/dist/cli/chat/StreamingText.d.ts +12 -0
  23. package/dist/cli/chat/StreamingText.js +12 -0
  24. package/dist/cli/chat/SubagentPanel.d.ts +45 -0
  25. package/dist/cli/chat/SubagentPanel.js +110 -0
  26. package/dist/cli/chat/TeamPanel.d.ts +21 -0
  27. package/dist/cli/chat/TeamPanel.js +42 -0
  28. package/dist/cli/chat/ToolIndicator.d.ts +25 -0
  29. package/dist/cli/chat/ToolIndicator.js +436 -0
  30. package/dist/cli/chat/hooks/useAgentLoop.d.ts +39 -0
  31. package/dist/cli/chat/hooks/useAgentLoop.js +382 -0
  32. package/dist/cli/chat/hooks/useSlashCommands.d.ts +37 -0
  33. package/dist/cli/chat/hooks/useSlashCommands.js +387 -0
  34. package/dist/cli/commands/config-cmd.d.ts +10 -0
  35. package/dist/cli/commands/config-cmd.js +99 -0
  36. package/dist/cli/commands/doctor.d.ts +14 -0
  37. package/dist/cli/commands/doctor.js +172 -0
  38. package/dist/cli/commands/init.d.ts +16 -0
  39. package/dist/cli/commands/init.js +278 -0
  40. package/dist/cli/commands/mcp.d.ts +12 -0
  41. package/dist/cli/commands/mcp.js +162 -0
  42. package/dist/cli/login/LoginApp.d.ts +7 -0
  43. package/dist/cli/login/LoginApp.js +157 -0
  44. package/dist/cli/print-mode.d.ts +31 -0
  45. package/dist/cli/print-mode.js +202 -0
  46. package/dist/cli/serve-mode.d.ts +37 -0
  47. package/dist/cli/serve-mode.js +636 -0
  48. package/dist/cli/services/agent-definitions.d.ts +25 -0
  49. package/dist/cli/services/agent-definitions.js +91 -0
  50. package/dist/cli/services/agent-events.d.ts +178 -0
  51. package/dist/cli/services/agent-events.js +175 -0
  52. package/dist/cli/services/agent-loop.d.ts +90 -0
  53. package/dist/cli/services/agent-loop.js +762 -0
  54. package/dist/cli/services/agent-worker-base.d.ts +97 -0
  55. package/dist/cli/services/agent-worker-base.js +220 -0
  56. package/dist/cli/services/auth-service.d.ts +30 -0
  57. package/dist/cli/services/auth-service.js +160 -0
  58. package/dist/cli/services/background-processes.d.ts +126 -0
  59. package/dist/cli/services/background-processes.js +318 -0
  60. package/dist/cli/services/browser-auth.d.ts +24 -0
  61. package/dist/cli/services/browser-auth.js +180 -0
  62. package/dist/cli/services/claude-md-loader.d.ts +16 -0
  63. package/dist/cli/services/claude-md-loader.js +58 -0
  64. package/dist/cli/services/config-store.d.ts +47 -0
  65. package/dist/cli/services/config-store.js +79 -0
  66. package/dist/cli/services/debug-log.d.ts +10 -0
  67. package/dist/cli/services/debug-log.js +52 -0
  68. package/dist/cli/services/error-logger.d.ts +58 -0
  69. package/dist/cli/services/error-logger.js +269 -0
  70. package/dist/cli/services/file-history.d.ts +21 -0
  71. package/dist/cli/services/file-history.js +83 -0
  72. package/dist/cli/services/format-server-response.d.ts +16 -0
  73. package/dist/cli/services/format-server-response.js +440 -0
  74. package/dist/cli/services/git-context.d.ts +11 -0
  75. package/dist/cli/services/git-context.js +66 -0
  76. package/dist/cli/services/hooks.d.ts +85 -0
  77. package/dist/cli/services/hooks.js +258 -0
  78. package/dist/cli/services/interactive-tools.d.ts +125 -0
  79. package/dist/cli/services/interactive-tools.js +260 -0
  80. package/dist/cli/services/keybinding-manager.d.ts +52 -0
  81. package/dist/cli/services/keybinding-manager.js +115 -0
  82. package/dist/cli/services/local-tools.d.ts +22 -0
  83. package/dist/cli/services/local-tools.js +697 -0
  84. package/dist/cli/services/lsp-manager.d.ts +18 -0
  85. package/dist/cli/services/lsp-manager.js +717 -0
  86. package/dist/cli/services/mcp-client.d.ts +48 -0
  87. package/dist/cli/services/mcp-client.js +157 -0
  88. package/dist/cli/services/memory-manager.d.ts +16 -0
  89. package/dist/cli/services/memory-manager.js +57 -0
  90. package/dist/cli/services/model-manager.d.ts +18 -0
  91. package/dist/cli/services/model-manager.js +71 -0
  92. package/dist/cli/services/model-router.d.ts +26 -0
  93. package/dist/cli/services/model-router.js +149 -0
  94. package/dist/cli/services/permission-modes.d.ts +13 -0
  95. package/dist/cli/services/permission-modes.js +43 -0
  96. package/dist/cli/services/rewind.d.ts +84 -0
  97. package/dist/cli/services/rewind.js +194 -0
  98. package/dist/cli/services/ripgrep.d.ts +28 -0
  99. package/dist/cli/services/ripgrep.js +138 -0
  100. package/dist/cli/services/sandbox.d.ts +29 -0
  101. package/dist/cli/services/sandbox.js +97 -0
  102. package/dist/cli/services/server-tools.d.ts +61 -0
  103. package/dist/cli/services/server-tools.js +543 -0
  104. package/dist/cli/services/session-persistence.d.ts +23 -0
  105. package/dist/cli/services/session-persistence.js +99 -0
  106. package/dist/cli/services/subagent-worker.d.ts +19 -0
  107. package/dist/cli/services/subagent-worker.js +41 -0
  108. package/dist/cli/services/subagent.d.ts +47 -0
  109. package/dist/cli/services/subagent.js +647 -0
  110. package/dist/cli/services/system-prompt.d.ts +7 -0
  111. package/dist/cli/services/system-prompt.js +198 -0
  112. package/dist/cli/services/team-lead.d.ts +73 -0
  113. package/dist/cli/services/team-lead.js +512 -0
  114. package/dist/cli/services/team-state.d.ts +77 -0
  115. package/dist/cli/services/team-state.js +398 -0
  116. package/dist/cli/services/teammate.d.ts +31 -0
  117. package/dist/cli/services/teammate.js +689 -0
  118. package/dist/cli/services/telemetry.d.ts +61 -0
  119. package/dist/cli/services/telemetry.js +209 -0
  120. package/dist/cli/services/tools/agent-tools.d.ts +14 -0
  121. package/dist/cli/services/tools/agent-tools.js +347 -0
  122. package/dist/cli/services/tools/file-ops.d.ts +15 -0
  123. package/dist/cli/services/tools/file-ops.js +487 -0
  124. package/dist/cli/services/tools/search-tools.d.ts +8 -0
  125. package/dist/cli/services/tools/search-tools.js +186 -0
  126. package/dist/cli/services/tools/shell-exec.d.ts +10 -0
  127. package/dist/cli/services/tools/shell-exec.js +168 -0
  128. package/dist/cli/services/tools/task-manager.d.ts +28 -0
  129. package/dist/cli/services/tools/task-manager.js +209 -0
  130. package/dist/cli/services/tools/web-tools.d.ts +11 -0
  131. package/dist/cli/services/tools/web-tools.js +395 -0
  132. package/dist/cli/setup/SetupApp.d.ts +9 -0
  133. package/dist/cli/setup/SetupApp.js +191 -0
  134. package/dist/cli/shared/MatrixIntro.d.ts +4 -0
  135. package/dist/cli/shared/MatrixIntro.js +83 -0
  136. package/dist/cli/shared/Theme.d.ts +74 -0
  137. package/dist/cli/shared/Theme.js +127 -0
  138. package/dist/cli/shared/WhaleBanner.d.ts +10 -0
  139. package/dist/cli/shared/WhaleBanner.js +12 -0
  140. package/dist/cli/shared/markdown.d.ts +21 -0
  141. package/dist/cli/shared/markdown.js +756 -0
  142. package/dist/cli/status/StatusApp.d.ts +4 -0
  143. package/dist/cli/status/StatusApp.js +105 -0
  144. package/dist/cli/stores/StoreApp.d.ts +7 -0
  145. package/dist/cli/stores/StoreApp.js +81 -0
  146. package/dist/index.d.ts +15 -0
  147. package/dist/index.js +538 -0
  148. package/dist/local-agent/connection.d.ts +48 -0
  149. package/dist/local-agent/connection.js +332 -0
  150. package/dist/local-agent/discovery.d.ts +18 -0
  151. package/dist/local-agent/discovery.js +146 -0
  152. package/dist/local-agent/executor.d.ts +34 -0
  153. package/dist/local-agent/executor.js +241 -0
  154. package/dist/local-agent/index.d.ts +14 -0
  155. package/dist/local-agent/index.js +198 -0
  156. package/dist/node/adapters/base.d.ts +35 -0
  157. package/dist/node/adapters/base.js +10 -0
  158. package/dist/node/adapters/discord.d.ts +29 -0
  159. package/dist/node/adapters/discord.js +299 -0
  160. package/dist/node/adapters/email.d.ts +23 -0
  161. package/dist/node/adapters/email.js +218 -0
  162. package/dist/node/adapters/imessage.d.ts +17 -0
  163. package/dist/node/adapters/imessage.js +118 -0
  164. package/dist/node/adapters/slack.d.ts +26 -0
  165. package/dist/node/adapters/slack.js +259 -0
  166. package/dist/node/adapters/sms.d.ts +23 -0
  167. package/dist/node/adapters/sms.js +161 -0
  168. package/dist/node/adapters/telegram.d.ts +17 -0
  169. package/dist/node/adapters/telegram.js +101 -0
  170. package/dist/node/adapters/webchat.d.ts +27 -0
  171. package/dist/node/adapters/webchat.js +160 -0
  172. package/dist/node/adapters/whatsapp.d.ts +28 -0
  173. package/dist/node/adapters/whatsapp.js +230 -0
  174. package/dist/node/cli.d.ts +2 -0
  175. package/dist/node/cli.js +325 -0
  176. package/dist/node/config.d.ts +17 -0
  177. package/dist/node/config.js +31 -0
  178. package/dist/node/runtime.d.ts +50 -0
  179. package/dist/node/runtime.js +351 -0
  180. package/dist/server/handlers/__test-utils__/mock-supabase.d.ts +11 -0
  181. package/dist/server/handlers/__test-utils__/mock-supabase.js +393 -0
  182. package/dist/server/handlers/analytics.d.ts +17 -0
  183. package/dist/server/handlers/analytics.js +266 -0
  184. package/dist/server/handlers/api-keys.d.ts +6 -0
  185. package/dist/server/handlers/api-keys.js +221 -0
  186. package/dist/server/handlers/billing.d.ts +33 -0
  187. package/dist/server/handlers/billing.js +272 -0
  188. package/dist/server/handlers/browser.d.ts +10 -0
  189. package/dist/server/handlers/browser.js +517 -0
  190. package/dist/server/handlers/catalog.d.ts +99 -0
  191. package/dist/server/handlers/catalog.js +976 -0
  192. package/dist/server/handlers/comms.d.ts +254 -0
  193. package/dist/server/handlers/comms.js +588 -0
  194. package/dist/server/handlers/creations.d.ts +6 -0
  195. package/dist/server/handlers/creations.js +479 -0
  196. package/dist/server/handlers/crm.d.ts +89 -0
  197. package/dist/server/handlers/crm.js +538 -0
  198. package/dist/server/handlers/discovery.d.ts +6 -0
  199. package/dist/server/handlers/discovery.js +288 -0
  200. package/dist/server/handlers/embeddings.d.ts +92 -0
  201. package/dist/server/handlers/embeddings.js +197 -0
  202. package/dist/server/handlers/enrichment.d.ts +8 -0
  203. package/dist/server/handlers/enrichment.js +768 -0
  204. package/dist/server/handlers/image-gen.d.ts +6 -0
  205. package/dist/server/handlers/image-gen.js +409 -0
  206. package/dist/server/handlers/inventory.d.ts +319 -0
  207. package/dist/server/handlers/inventory.js +447 -0
  208. package/dist/server/handlers/kali.d.ts +10 -0
  209. package/dist/server/handlers/kali.js +210 -0
  210. package/dist/server/handlers/llm-providers.d.ts +6 -0
  211. package/dist/server/handlers/llm-providers.js +673 -0
  212. package/dist/server/handlers/local-agent.d.ts +6 -0
  213. package/dist/server/handlers/local-agent.js +118 -0
  214. package/dist/server/handlers/meta-ads.d.ts +111 -0
  215. package/dist/server/handlers/meta-ads.js +2279 -0
  216. package/dist/server/handlers/nodes.d.ts +33 -0
  217. package/dist/server/handlers/nodes.js +699 -0
  218. package/dist/server/handlers/operations.d.ts +138 -0
  219. package/dist/server/handlers/operations.js +131 -0
  220. package/dist/server/handlers/platform.d.ts +23 -0
  221. package/dist/server/handlers/platform.js +227 -0
  222. package/dist/server/handlers/supply-chain.d.ts +19 -0
  223. package/dist/server/handlers/supply-chain.js +327 -0
  224. package/dist/server/handlers/transcription.d.ts +17 -0
  225. package/dist/server/handlers/transcription.js +121 -0
  226. package/dist/server/handlers/video-gen.d.ts +6 -0
  227. package/dist/server/handlers/video-gen.js +466 -0
  228. package/dist/server/handlers/voice.d.ts +8 -0
  229. package/dist/server/handlers/voice.js +1146 -0
  230. package/dist/server/handlers/workflow-steps.d.ts +86 -0
  231. package/dist/server/handlers/workflow-steps.js +2349 -0
  232. package/dist/server/handlers/workflows.d.ts +7 -0
  233. package/dist/server/handlers/workflows.js +989 -0
  234. package/dist/server/index.d.ts +1 -0
  235. package/dist/server/index.js +2427 -0
  236. package/dist/server/lib/batch-client.d.ts +80 -0
  237. package/dist/server/lib/batch-client.js +467 -0
  238. package/dist/server/lib/code-worker-pool.d.ts +31 -0
  239. package/dist/server/lib/code-worker-pool.js +224 -0
  240. package/dist/server/lib/code-worker.d.ts +1 -0
  241. package/dist/server/lib/code-worker.js +188 -0
  242. package/dist/server/lib/compaction-service.d.ts +32 -0
  243. package/dist/server/lib/compaction-service.js +162 -0
  244. package/dist/server/lib/logger.d.ts +19 -0
  245. package/dist/server/lib/logger.js +46 -0
  246. package/dist/server/lib/otel.d.ts +38 -0
  247. package/dist/server/lib/otel.js +126 -0
  248. package/dist/server/lib/pg-rate-limiter.d.ts +21 -0
  249. package/dist/server/lib/pg-rate-limiter.js +86 -0
  250. package/dist/server/lib/prompt-sanitizer.d.ts +37 -0
  251. package/dist/server/lib/prompt-sanitizer.js +177 -0
  252. package/dist/server/lib/provider-capabilities.d.ts +85 -0
  253. package/dist/server/lib/provider-capabilities.js +190 -0
  254. package/dist/server/lib/provider-failover.d.ts +74 -0
  255. package/dist/server/lib/provider-failover.js +210 -0
  256. package/dist/server/lib/rate-limiter.d.ts +39 -0
  257. package/dist/server/lib/rate-limiter.js +147 -0
  258. package/dist/server/lib/server-agent-loop.d.ts +107 -0
  259. package/dist/server/lib/server-agent-loop.js +667 -0
  260. package/dist/server/lib/server-subagent.d.ts +78 -0
  261. package/dist/server/lib/server-subagent.js +203 -0
  262. package/dist/server/lib/session-checkpoint.d.ts +51 -0
  263. package/dist/server/lib/session-checkpoint.js +145 -0
  264. package/dist/server/lib/ssrf-guard.d.ts +13 -0
  265. package/dist/server/lib/ssrf-guard.js +240 -0
  266. package/dist/server/lib/supabase-client.d.ts +7 -0
  267. package/dist/server/lib/supabase-client.js +78 -0
  268. package/dist/server/lib/template-resolver.d.ts +31 -0
  269. package/dist/server/lib/template-resolver.js +215 -0
  270. package/dist/server/lib/utils.d.ts +16 -0
  271. package/dist/server/lib/utils.js +147 -0
  272. package/dist/server/local-agent-gateway.d.ts +82 -0
  273. package/dist/server/local-agent-gateway.js +426 -0
  274. package/dist/server/providers/anthropic.d.ts +20 -0
  275. package/dist/server/providers/anthropic.js +199 -0
  276. package/dist/server/providers/bedrock.d.ts +20 -0
  277. package/dist/server/providers/bedrock.js +194 -0
  278. package/dist/server/providers/gemini.d.ts +24 -0
  279. package/dist/server/providers/gemini.js +486 -0
  280. package/dist/server/providers/openai.d.ts +24 -0
  281. package/dist/server/providers/openai.js +522 -0
  282. package/dist/server/providers/registry.d.ts +32 -0
  283. package/dist/server/providers/registry.js +58 -0
  284. package/dist/server/providers/shared.d.ts +32 -0
  285. package/dist/server/providers/shared.js +124 -0
  286. package/dist/server/providers/types.d.ts +92 -0
  287. package/dist/server/providers/types.js +12 -0
  288. package/dist/server/proxy-handlers.d.ts +6 -0
  289. package/dist/server/proxy-handlers.js +89 -0
  290. package/dist/server/tool-router.d.ts +149 -0
  291. package/dist/server/tool-router.js +803 -0
  292. package/dist/server/validation.d.ts +24 -0
  293. package/dist/server/validation.js +301 -0
  294. package/dist/server/worker.d.ts +19 -0
  295. package/dist/server/worker.js +201 -0
  296. package/dist/setup.d.ts +8 -0
  297. package/dist/setup.js +181 -0
  298. package/dist/shared/agent-core.d.ts +157 -0
  299. package/dist/shared/agent-core.js +534 -0
  300. package/dist/shared/anthropic-types.d.ts +105 -0
  301. package/dist/shared/anthropic-types.js +7 -0
  302. package/dist/shared/api-client.d.ts +90 -0
  303. package/dist/shared/api-client.js +379 -0
  304. package/dist/shared/constants.d.ts +33 -0
  305. package/dist/shared/constants.js +80 -0
  306. package/dist/shared/sse-parser.d.ts +26 -0
  307. package/dist/shared/sse-parser.js +259 -0
  308. package/dist/shared/tool-dispatch.d.ts +52 -0
  309. package/dist/shared/tool-dispatch.js +191 -0
  310. package/dist/shared/types.d.ts +72 -0
  311. package/dist/shared/types.js +7 -0
  312. package/dist/updater.d.ts +25 -0
  313. package/dist/updater.js +140 -0
  314. package/dist/webchat/widget.d.ts +0 -0
  315. package/dist/webchat/widget.js +397 -0
  316. package/package.json +95 -0
  317. package/src/cli/services/builtin-skills/commit.md +19 -0
  318. package/src/cli/services/builtin-skills/review-pr.md +21 -0
  319. package/src/cli/services/builtin-skills/review.md +18 -0
@@ -0,0 +1,1146 @@
1
+ // server/handlers/voice.ts — ElevenLabs audio suite v2.1
2
+ //
3
+ // Actions: speak, batch, sound_effects, music_plan, music_compose,
4
+ // music_compose_detailed, speech_to_text, stem_separation,
5
+ // voice_changer, audio_isolation, voice_design, voice_design_create,
6
+ // forced_alignment, search_voices, list_voices, get_voice,
7
+ // clone_voice, usage, list_models,
8
+ // pvc_create, pvc_upload, pvc_separate_speakers, pvc_status,
9
+ // pvc_select_speaker, pvc_verify, pvc_train
10
+ //
11
+ // v2.1: music_plan (structured composition plans), auto vocal clarity styles,
12
+ // composition_plan support on music_compose, 192kbps music output
13
+ // v2: DRY helpers, credential caching, voice name resolution, batch TTS,
14
+ // retry with backoff, updated models (eleven_v3, scribe_v2)
15
+ // ============================================================================
16
+ // CONSTANTS
17
+ // ============================================================================
18
+ const ELEVENLABS_BASE = "https://api.elevenlabs.io/v1";
19
+ const DEFAULT_VOICE_ID = "21m00Tcm4TlvDq8ikWAM"; // Rachel
20
+ const DEFAULT_TTS_MODEL = "eleven_v3";
21
+ const DEFAULT_STS_MODEL = "eleven_english_sts_v2";
22
+ const DEFAULT_MUSIC_MODEL = "music_v1";
23
+ const DEFAULT_SFX_MODEL = "eleven_text_to_sound_v2";
24
+ const DEFAULT_TTV_MODEL = "eleven_ttv_v3";
25
+ const DEFAULT_STT_MODEL = "scribe_v2";
26
+ const MAX_RETRIES = 2;
27
+ const RETRY_DELAY_MS = 1000;
28
+ const VOICE_CACHE_TTL = 600_000; // 10 min
29
+ const KEY_CACHE_TTL = 300_000; // 5 min
30
+ // ============================================================================
31
+ // CREDENTIAL CACHING — 5-min TTL, avoids decrypt RPC on every call
32
+ // ============================================================================
33
+ const keyCache = new Map();
34
+ async function getElevenLabsKey(sb, storeId) {
35
+ const cached = keyCache.get(storeId);
36
+ if (cached && Date.now() - cached.fetchedAt < KEY_CACHE_TTL)
37
+ return cached.key;
38
+ const { data, error } = await sb.rpc("decrypt_secret", {
39
+ p_name: "ELEVENLABS_API_KEY",
40
+ p_store_id: storeId,
41
+ });
42
+ if (error || !data)
43
+ return null;
44
+ const key = data;
45
+ keyCache.set(storeId, { key, fetchedAt: Date.now() });
46
+ if (keyCache.size > 50) {
47
+ // Evict oldest entry by fetchedAt, not insertion order
48
+ let oldestKey;
49
+ let oldestTime = Infinity;
50
+ for (const [k, v] of keyCache) {
51
+ if (v.fetchedAt < oldestTime) {
52
+ oldestTime = v.fetchedAt;
53
+ oldestKey = k;
54
+ }
55
+ }
56
+ if (oldestKey)
57
+ keyCache.delete(oldestKey);
58
+ }
59
+ return key;
60
+ }
61
+ // ============================================================================
62
+ // VOICE NAME RESOLUTION — accept name or ID, cache voice list 10 min
63
+ // ============================================================================
64
+ const voiceListCache = new Map();
65
+ /** Popular voice presets for quick lookup without API call */
66
+ const VOICE_PRESETS = {
67
+ rachel: "21m00Tcm4TlvDq8ikWAM",
68
+ daniel: "onwK4e9ZLuTAKqWW03F9",
69
+ roger: "CwhRBWXzGAHq8TQ4Fs17",
70
+ sarah: "EXAVITQu4vr4xnSDxMaL",
71
+ adam: "pNInz6obpgDQGcFmaJgB",
72
+ charlie: "IKne3meq5aSn9XLyUdCD",
73
+ emily: "LcfcDJNUP1GQjkzn1xUU",
74
+ josh: "TxGEqnHWrfWFTfGW9XjX",
75
+ };
76
+ async function resolveVoiceId(nameOrId, apiKey, storeId) {
77
+ // Already a voice ID (UUID-like or 20+ char alphanumeric)?
78
+ if (/^[a-zA-Z0-9]{20,}$/.test(nameOrId))
79
+ return nameOrId;
80
+ // Check presets first (instant, no API call)
81
+ const preset = VOICE_PRESETS[nameOrId.toLowerCase()];
82
+ if (preset)
83
+ return preset;
84
+ // Fetch and cache voice list from ElevenLabs
85
+ let voices = voiceListCache.get(storeId);
86
+ if (!voices || Date.now() - voices.fetchedAt > VOICE_CACHE_TTL) {
87
+ try {
88
+ const resp = await fetch(`${ELEVENLABS_BASE}/voices`, {
89
+ headers: { "xi-api-key": apiKey },
90
+ });
91
+ if (resp.ok) {
92
+ const data = await resp.json();
93
+ const list = (data.voices || []).map((v) => ({ voice_id: v.voice_id, name: v.name }));
94
+ voices = { voices: list, fetchedAt: Date.now() };
95
+ voiceListCache.set(storeId, voices);
96
+ if (voiceListCache.size > 50) {
97
+ let oldestKey;
98
+ let oldestTime = Infinity;
99
+ for (const [k, v] of voiceListCache) {
100
+ if (v.fetchedAt < oldestTime) {
101
+ oldestTime = v.fetchedAt;
102
+ oldestKey = k;
103
+ }
104
+ }
105
+ if (oldestKey)
106
+ voiceListCache.delete(oldestKey);
107
+ }
108
+ }
109
+ }
110
+ catch { /* fall through to fuzzy match on stale cache */ }
111
+ }
112
+ if (voices) {
113
+ const lower = nameOrId.toLowerCase();
114
+ // Exact match
115
+ const exact = voices.voices.find(v => v.name.toLowerCase() === lower);
116
+ if (exact)
117
+ return exact.voice_id;
118
+ // Starts-with match
119
+ const starts = voices.voices.find(v => v.name.toLowerCase().startsWith(lower));
120
+ if (starts)
121
+ return starts.voice_id;
122
+ // Contains match
123
+ const contains = voices.voices.find(v => v.name.toLowerCase().includes(lower));
124
+ if (contains)
125
+ return contains.voice_id;
126
+ }
127
+ // Fall back — treat as voice ID (let ElevenLabs return 404 if invalid)
128
+ return nameOrId;
129
+ }
130
+ async function uploadAudioAndRecord(sb, storeId, audioBuffer, label, ext, mimeType, aiTags) {
131
+ const id = crypto.randomUUID();
132
+ const fileName = `${id}.${ext}`;
133
+ const storagePath = `ai-generated/${storeId.toUpperCase()}/audio/${fileName}`;
134
+ const { error: uploadErr } = await sb.storage
135
+ .from("store-media")
136
+ .upload(storagePath, audioBuffer, { contentType: mimeType, upsert: true });
137
+ if (uploadErr)
138
+ throw new Error(`Storage upload failed: ${uploadErr.message}`);
139
+ const { data: urlData } = sb.storage.from("store-media").getPublicUrl(storagePath);
140
+ const fileUrl = urlData.publicUrl;
141
+ const { data: mediaRow, error: mediaErr } = await sb
142
+ .from("store_media")
143
+ .insert({
144
+ store_id: storeId, file_name: fileName, file_path: storagePath,
145
+ file_url: fileUrl, file_size: audioBuffer.length, file_type: mimeType,
146
+ category: "ai_generated", ai_tags: aiTags, ai_description: label, source: "elevenlabs",
147
+ })
148
+ .select("id")
149
+ .single();
150
+ if (mediaErr)
151
+ console.error("[voice] store_media insert error:", mediaErr.message);
152
+ return { file_url: fileUrl, file_name: fileName, file_size: audioBuffer.length, media_id: mediaRow?.id || id };
153
+ }
154
+ // ============================================================================
155
+ // DRY HELPERS — eliminate 90% of code duplication
156
+ // ============================================================================
157
+ function formatInfo(outputFormat) {
158
+ if (outputFormat.startsWith("pcm"))
159
+ return { ext: "pcm", mime: "audio/pcm" };
160
+ if (outputFormat.startsWith("opus"))
161
+ return { ext: "opus", mime: "audio/opus" };
162
+ return { ext: "mp3", mime: "audio/mpeg" };
163
+ }
164
+ /** Fetch with retry for ElevenLabs API (handles 429/500/502/503) */
165
+ async function fetchWithRetry(url, init) {
166
+ let lastErr = null;
167
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
168
+ try {
169
+ const resp = await fetch(url, init);
170
+ if (resp.ok || (resp.status < 500 && resp.status !== 429))
171
+ return resp;
172
+ if (attempt < MAX_RETRIES) {
173
+ const delay = RETRY_DELAY_MS * Math.pow(2, attempt);
174
+ await new Promise(r => setTimeout(r, delay));
175
+ continue;
176
+ }
177
+ return resp; // final attempt, return whatever we got
178
+ }
179
+ catch (err) {
180
+ lastErr = err;
181
+ if (attempt < MAX_RETRIES) {
182
+ await new Promise(r => setTimeout(r, RETRY_DELAY_MS * Math.pow(2, attempt)));
183
+ }
184
+ }
185
+ }
186
+ throw lastErr || new Error("Fetch failed after retries");
187
+ }
188
+ /** API call → buffer → upload → standard result */
189
+ async function generateAndUpload(sb, storeId, apiKey, url, body, label, tags, ext, mime, extraFields = {}) {
190
+ try {
191
+ const resp = await fetchWithRetry(url, {
192
+ method: "POST",
193
+ headers: { "xi-api-key": apiKey, "Content-Type": "application/json", Accept: "audio/mpeg" },
194
+ body: JSON.stringify(body),
195
+ });
196
+ if (!resp.ok) {
197
+ const errText = await resp.text();
198
+ return { success: false, error: `ElevenLabs error ${resp.status}: ${errText}` };
199
+ }
200
+ const audioBuffer = Buffer.from(await resp.arrayBuffer());
201
+ const upload = await uploadAudioAndRecord(sb, storeId, audioBuffer, label, ext, mime, tags);
202
+ return {
203
+ success: true,
204
+ data: {
205
+ file_url: upload.file_url,
206
+ file_size: upload.file_size,
207
+ media_id: upload.media_id,
208
+ format: ext,
209
+ ...extraFields,
210
+ },
211
+ };
212
+ }
213
+ catch (err) {
214
+ return { success: false, error: `${label} failed: ${err}` };
215
+ }
216
+ }
217
+ /** FormData-based API call → buffer → upload → standard result */
218
+ async function processFormData(sb, storeId, apiKey, url, formData, label, tags, ext, mime, extraFields = {}) {
219
+ try {
220
+ const resp = await fetchWithRetry(url, {
221
+ method: "POST",
222
+ headers: { "xi-api-key": apiKey },
223
+ body: formData,
224
+ });
225
+ if (!resp.ok) {
226
+ const errText = await resp.text();
227
+ return { success: false, error: `ElevenLabs error ${resp.status}: ${errText}` };
228
+ }
229
+ const audioBuffer = Buffer.from(await resp.arrayBuffer());
230
+ const upload = await uploadAudioAndRecord(sb, storeId, audioBuffer, label, ext, mime, tags);
231
+ return {
232
+ success: true,
233
+ data: {
234
+ file_url: upload.file_url,
235
+ file_size: upload.file_size,
236
+ media_id: upload.media_id,
237
+ format: ext,
238
+ ...extraFields,
239
+ },
240
+ };
241
+ }
242
+ catch (err) {
243
+ return { success: false, error: `${label} failed: ${err}` };
244
+ }
245
+ }
246
+ /** Build FormData from base64 audio input */
247
+ function audioFormData(audioBase64, fieldName = "file", fileName = "audio.mp3") {
248
+ const buffer = Buffer.from(audioBase64, "base64");
249
+ const blob = new Blob([buffer], { type: "audio/mpeg" });
250
+ const formData = new FormData();
251
+ formData.append(fieldName, blob, fileName);
252
+ return formData;
253
+ }
254
+ // ============================================================================
255
+ // MAIN HANDLER
256
+ // ============================================================================
257
+ export async function handleVoice(sb, args, storeId) {
258
+ const sid = storeId;
259
+ const action = args.action;
260
+ const apiKey = await getElevenLabsKey(sb, sid);
261
+ if (!apiKey) {
262
+ return { success: false, error: "ELEVENLABS_API_KEY not configured. Add it to user_tool_secrets for this store." };
263
+ }
264
+ const outputFormat = args.output_format || "mp3_44100_128";
265
+ const { ext, mime } = formatInfo(outputFormat);
266
+ switch (action) {
267
+ // ── TEXT-TO-SPEECH ─────────────────────────────────────────────────
268
+ case "speak": {
269
+ const text = args.text;
270
+ if (!text)
271
+ return { success: false, error: "text parameter is required" };
272
+ if (text.length > 5000)
273
+ return { success: false, error: "Text too long (max 5000 characters). Split into smaller chunks or use batch action." };
274
+ // Resolve voice by name or ID
275
+ const voiceInput = (args.voice_name || args.voice_id || DEFAULT_VOICE_ID);
276
+ const voiceId = await resolveVoiceId(voiceInput, apiKey, sid);
277
+ const modelId = args.model_id || DEFAULT_TTS_MODEL;
278
+ // Smart defaults: cloned/custom voices get higher similarity for accuracy
279
+ const presetIds = new Set(Object.values(VOICE_PRESETS));
280
+ const isCloned = !presetIds.has(voiceId) && voiceId !== DEFAULT_VOICE_ID;
281
+ const defaultStability = isCloned ? 0.35 : 0.5;
282
+ const defaultSimilarity = isCloned ? 0.9 : 0.75;
283
+ const defaultStyle = isCloned ? 0.05 : 0;
284
+ return generateAndUpload(sb, sid, apiKey, `${ELEVENLABS_BASE}/text-to-speech/${voiceId}?output_format=${outputFormat}`, {
285
+ text,
286
+ model_id: modelId,
287
+ voice_settings: {
288
+ stability: args.stability ?? defaultStability,
289
+ similarity_boost: args.similarity_boost ?? defaultSimilarity,
290
+ style: args.style ?? defaultStyle,
291
+ use_speaker_boost: args.speaker_boost ?? true,
292
+ },
293
+ }, `TTS: ${text.substring(0, 80)}`, ["AI Generated", "Text-to-Speech", "ElevenLabs"], ext, mime, { voice_id: voiceId, model_id: modelId, text_length: text.length, cloned_voice: isCloned });
294
+ }
295
+ // ── BATCH TTS ──────────────────────────────────────────────────────
296
+ case "batch": {
297
+ const items = args.items;
298
+ if (!items?.length)
299
+ return { success: false, error: "items array is required (each with text, optional voice_name/voice_id)" };
300
+ if (items.length > 10)
301
+ return { success: false, error: "Maximum 10 items per batch" };
302
+ const results = await Promise.allSettled(items.map(async (item) => {
303
+ const voiceInput = item.voice_name || item.voice_id || DEFAULT_VOICE_ID;
304
+ const voiceId = await resolveVoiceId(voiceInput, apiKey, sid);
305
+ const modelId = item.model_id || DEFAULT_TTS_MODEL;
306
+ // Smart defaults: cloned/custom voices get higher similarity for accuracy
307
+ const presetIds = new Set(Object.values(VOICE_PRESETS));
308
+ const isCloned = !presetIds.has(voiceId) && voiceId !== DEFAULT_VOICE_ID;
309
+ const batchStability = isCloned ? 0.35 : 0.5;
310
+ const batchSimilarity = isCloned ? 0.9 : 0.75;
311
+ const batchStyle = isCloned ? 0.05 : 0;
312
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/text-to-speech/${voiceId}?output_format=${outputFormat}`, {
313
+ method: "POST",
314
+ headers: { "xi-api-key": apiKey, "Content-Type": "application/json", Accept: "audio/mpeg" },
315
+ body: JSON.stringify({
316
+ text: item.text,
317
+ model_id: modelId,
318
+ voice_settings: { stability: batchStability, similarity_boost: batchSimilarity, style: batchStyle, use_speaker_boost: true },
319
+ }),
320
+ });
321
+ if (!resp.ok)
322
+ throw new Error(`ElevenLabs error ${resp.status}`);
323
+ const audioBuffer = Buffer.from(await resp.arrayBuffer());
324
+ const upload = await uploadAudioAndRecord(sb, sid, audioBuffer, `TTS: ${item.text.substring(0, 80)}`, ext, mime, ["AI Generated", "Text-to-Speech", "ElevenLabs", "Batch"]);
325
+ return {
326
+ text: item.text.substring(0, 80),
327
+ voice_id: voiceId,
328
+ file_url: upload.file_url,
329
+ file_size: upload.file_size,
330
+ media_id: upload.media_id,
331
+ format: ext,
332
+ };
333
+ }));
334
+ const clips = [];
335
+ const errors = [];
336
+ results.forEach((r, i) => {
337
+ if (r.status === "fulfilled")
338
+ clips.push(r.value);
339
+ else
340
+ errors.push({ index: i, text: items[i].text.substring(0, 40), error: r.reason.message });
341
+ });
342
+ return {
343
+ success: true,
344
+ data: {
345
+ generated: clips.length,
346
+ failed: errors.length,
347
+ total: items.length,
348
+ clips,
349
+ ...(errors.length > 0 ? { errors } : {}),
350
+ },
351
+ };
352
+ }
353
+ // ── SPEECH-TO-TEXT ──────────────────────────────────────────────────
354
+ case "speech_to_text": {
355
+ const audioBase64 = args.audio_base64;
356
+ if (!audioBase64)
357
+ return { success: false, error: "audio_base64 parameter is required" };
358
+ try {
359
+ const formData = audioFormData(audioBase64);
360
+ formData.append("model_id", args.model_id || DEFAULT_STT_MODEL);
361
+ if (args.language_code)
362
+ formData.append("language_code", args.language_code);
363
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/speech-to-text`, {
364
+ method: "POST",
365
+ headers: { "xi-api-key": apiKey },
366
+ body: formData,
367
+ });
368
+ if (!resp.ok) {
369
+ const errText = await resp.text();
370
+ return { success: false, error: `ElevenLabs STT error ${resp.status}: ${errText}` };
371
+ }
372
+ return { success: true, data: await resp.json() };
373
+ }
374
+ catch (err) {
375
+ return { success: false, error: `STT failed: ${err}` };
376
+ }
377
+ }
378
+ // ── SOUND EFFECTS ──────────────────────────────────────────────────
379
+ case "sound_effects": {
380
+ const text = args.text;
381
+ if (!text)
382
+ return { success: false, error: "text parameter is required (describe the sound)" };
383
+ const body = {
384
+ text,
385
+ model_id: DEFAULT_SFX_MODEL,
386
+ prompt_influence: args.prompt_influence ?? 0.3,
387
+ loop: args.loop ?? false,
388
+ };
389
+ if (args.duration_seconds)
390
+ body.duration_seconds = args.duration_seconds;
391
+ return generateAndUpload(sb, sid, apiKey, `${ELEVENLABS_BASE}/sound-generation?output_format=${outputFormat}`, body, `SFX: ${text.substring(0, 80)}`, ["AI Generated", "Sound Effect", "ElevenLabs"], ext, mime, { prompt: text, duration_seconds: args.duration_seconds || "auto", loop: body.loop });
392
+ }
393
+ // ── MUSIC PLAN ────────────────────────────────────────────────────
394
+ // FREE endpoint that converts a prompt into a structured composition plan
395
+ // with proper sections, lyrics per line, timing, and styles.
396
+ // The plan can then be fed to music_compose for much clearer vocals.
397
+ case "music_plan": {
398
+ const prompt = args.prompt;
399
+ if (!prompt)
400
+ return { success: false, error: "prompt parameter is required (describe the song)" };
401
+ const planBody = { prompt, model_id: DEFAULT_MUSIC_MODEL };
402
+ if (args.music_length_ms)
403
+ planBody.music_length_ms = args.music_length_ms;
404
+ try {
405
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/music/plan`, {
406
+ method: "POST",
407
+ headers: { "xi-api-key": apiKey, "Content-Type": "application/json" },
408
+ body: JSON.stringify(planBody),
409
+ });
410
+ if (!resp.ok) {
411
+ const errText = await resp.text();
412
+ return { success: false, error: `ElevenLabs plan error ${resp.status}: ${errText}` };
413
+ }
414
+ const plan = await resp.json();
415
+ return {
416
+ success: true,
417
+ data: {
418
+ composition_plan: plan,
419
+ hint: "Pass this composition_plan to music_compose to generate the song. You can edit sections, lyrics, or styles before generating.",
420
+ },
421
+ };
422
+ }
423
+ catch (err) {
424
+ return { success: false, error: `Music plan failed: ${err}` };
425
+ }
426
+ }
427
+ // ── MUSIC COMPOSE ──────────────────────────────────────────────────
428
+ case "music_compose": {
429
+ const prompt = args.prompt;
430
+ const compositionPlan = args.composition_plan;
431
+ if (!prompt && !compositionPlan)
432
+ return { success: false, error: "prompt or composition_plan is required" };
433
+ const isInstrumental = args.force_instrumental ?? false;
434
+ // Use higher quality for music (192kbps vs default 128kbps)
435
+ const musicFormat = outputFormat === "mp3_44100_128" ? "mp3_44100_192" : outputFormat;
436
+ const musicFmt = formatInfo(musicFormat);
437
+ // If composition_plan provided, use it (from music_plan or manually built)
438
+ if (compositionPlan) {
439
+ // Clone to avoid mutating the caller's object
440
+ const plan = JSON.parse(JSON.stringify(compositionPlan));
441
+ // Inject vocal clarity styles when not instrumental
442
+ if (!isInstrumental) {
443
+ if (!plan.positive_global_styles) {
444
+ plan.positive_global_styles = ["clear vocals", "crisp pronunciation", "studio quality"];
445
+ }
446
+ if (!plan.negative_global_styles) {
447
+ plan.negative_global_styles = ["distorted vocals", "mumbled lyrics", "lo-fi vocals"];
448
+ }
449
+ }
450
+ const body = {
451
+ composition_plan: plan,
452
+ model_id: DEFAULT_MUSIC_MODEL,
453
+ force_instrumental: isInstrumental,
454
+ };
455
+ return generateAndUpload(sb, sid, apiKey, `${ELEVENLABS_BASE}/music?output_format=${musicFormat}`, body, `Music (plan): ${JSON.stringify(plan).substring(0, 80)}`, ["AI Generated", "Music", "ElevenLabs", "Composition Plan"], musicFmt.ext, musicFmt.mime, { method: "composition_plan", instrumental: isInstrumental });
456
+ }
457
+ // Simple prompt mode
458
+ const body = {
459
+ prompt: prompt,
460
+ model_id: DEFAULT_MUSIC_MODEL,
461
+ force_instrumental: isInstrumental,
462
+ };
463
+ if (args.music_length_ms)
464
+ body.music_length_ms = args.music_length_ms;
465
+ return generateAndUpload(sb, sid, apiKey, `${ELEVENLABS_BASE}/music?output_format=${musicFormat}`, body, `Music: ${prompt.substring(0, 80)}`, ["AI Generated", "Music", "ElevenLabs"], musicFmt.ext, musicFmt.mime, { prompt, music_length_ms: args.music_length_ms || "auto", instrumental: isInstrumental });
466
+ }
467
+ // ── MUSIC COMPOSE DETAILED ─────────────────────────────────────────
468
+ case "music_compose_detailed": {
469
+ const prompt = args.prompt;
470
+ const compositionPlan = args.composition_plan;
471
+ if (!prompt && !compositionPlan)
472
+ return { success: false, error: "prompt or composition_plan required" };
473
+ const isInstrumental = args.force_instrumental ?? false;
474
+ // Use higher quality for music (192kbps vs default 128kbps)
475
+ const musicFormat = outputFormat === "mp3_44100_128" ? "mp3_44100_192" : outputFormat;
476
+ const musicFmt = formatInfo(musicFormat);
477
+ // Clone to avoid mutating the caller's object, then inject vocal clarity styles
478
+ let planToUse = compositionPlan;
479
+ if (compositionPlan && !isInstrumental) {
480
+ const plan = JSON.parse(JSON.stringify(compositionPlan));
481
+ if (!plan.positive_global_styles) {
482
+ plan.positive_global_styles = ["clear vocals", "crisp pronunciation", "studio quality"];
483
+ }
484
+ if (!plan.negative_global_styles) {
485
+ plan.negative_global_styles = ["distorted vocals", "mumbled lyrics", "lo-fi vocals"];
486
+ }
487
+ planToUse = plan;
488
+ }
489
+ const body = {
490
+ model_id: DEFAULT_MUSIC_MODEL,
491
+ force_instrumental: isInstrumental,
492
+ with_timestamps: args.with_timestamps ?? false,
493
+ };
494
+ if (prompt)
495
+ body.prompt = prompt;
496
+ if (planToUse)
497
+ body.composition_plan = planToUse;
498
+ if (args.music_length_ms)
499
+ body.music_length_ms = args.music_length_ms;
500
+ return generateAndUpload(sb, sid, apiKey, `${ELEVENLABS_BASE}/music/detailed?output_format=${musicFormat}`, body, `Music (detailed): ${(prompt || "composition plan").substring(0, 80)}`, ["AI Generated", "Music", "ElevenLabs", "Detailed"], musicFmt.ext, musicFmt.mime, { prompt: prompt || "(composition plan)", with_timestamps: body.with_timestamps });
501
+ }
502
+ // ── STEM SEPARATION ────────────────────────────────────────────────
503
+ case "stem_separation": {
504
+ const audioBase64 = args.audio_base64;
505
+ if (!audioBase64)
506
+ return { success: false, error: "audio_base64 parameter is required" };
507
+ const stemVariation = args.stem_variation || "six_stems_v1";
508
+ const formData = audioFormData(audioBase64);
509
+ formData.append("stem_variation_id", stemVariation);
510
+ return processFormData(sb, sid, apiKey, `${ELEVENLABS_BASE}/music/stem-separation?output_format=${outputFormat}`, formData, `Stem separation (${stemVariation})`, ["AI Generated", "Stem Separation", "ElevenLabs"], "zip", "application/zip", {
511
+ stem_variation: stemVariation,
512
+ stems: stemVariation === "two_stems_v1"
513
+ ? ["vocals", "accompaniment"]
514
+ : ["vocals", "drums", "bass", "piano", "guitar", "other"],
515
+ });
516
+ }
517
+ // ── VOICE CHANGER ──────────────────────────────────────────────────
518
+ case "voice_changer": {
519
+ const audioBase64 = args.audio_base64;
520
+ if (!audioBase64)
521
+ return { success: false, error: "audio_base64 parameter is required" };
522
+ const voiceInput = (args.voice_name || args.voice_id);
523
+ if (!voiceInput)
524
+ return { success: false, error: "voice_id or voice_name parameter is required" };
525
+ const voiceId = await resolveVoiceId(voiceInput, apiKey, sid);
526
+ const modelId = args.model_id || DEFAULT_STS_MODEL;
527
+ const formData = audioFormData(audioBase64, "audio");
528
+ formData.append("model_id", modelId);
529
+ formData.append("remove_background_noise", String(args.remove_background_noise ?? false));
530
+ if (args.voice_settings)
531
+ formData.append("voice_settings", JSON.stringify(args.voice_settings));
532
+ return processFormData(sb, sid, apiKey, `${ELEVENLABS_BASE}/speech-to-speech/${voiceId}?output_format=${outputFormat}`, formData, `Voice change to ${voiceId}`, ["AI Generated", "Voice Changer", "ElevenLabs"], ext, mime, { voice_id: voiceId, model_id: modelId });
533
+ }
534
+ // ── AUDIO ISOLATION ────────────────────────────────────────────────
535
+ case "audio_isolation": {
536
+ const audioBase64 = args.audio_base64;
537
+ if (!audioBase64)
538
+ return { success: false, error: "audio_base64 parameter is required" };
539
+ const formData = audioFormData(audioBase64, "audio");
540
+ return processFormData(sb, sid, apiKey, `${ELEVENLABS_BASE}/audio-isolation`, formData, "Audio isolation — vocals isolated", ["AI Generated", "Audio Isolation", "ElevenLabs"], ext, mime, { description: "Isolated vocals with background noise removed" });
541
+ }
542
+ // ── VOICE DESIGN ───────────────────────────────────────────────────
543
+ case "voice_design": {
544
+ const voiceDescription = args.voice_description;
545
+ if (!voiceDescription)
546
+ return { success: false, error: "voice_description parameter is required" };
547
+ try {
548
+ const body = {
549
+ voice_description: voiceDescription,
550
+ text: args.preview_text || "Hello, this is a preview of the designed voice.",
551
+ auto_enhance_description: args.auto_enhance ?? true,
552
+ model_id: DEFAULT_TTV_MODEL,
553
+ };
554
+ if (args.reference_audio_base64) {
555
+ body.reference_audio = args.reference_audio_base64;
556
+ if (args.reference_weight !== undefined)
557
+ body.reference_weight = args.reference_weight;
558
+ }
559
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/text-to-voice/design?output_format=${outputFormat}`, {
560
+ method: "POST",
561
+ headers: { "xi-api-key": apiKey, "Content-Type": "application/json" },
562
+ body: JSON.stringify(body),
563
+ });
564
+ if (!resp.ok) {
565
+ const errText = await resp.text();
566
+ return { success: false, error: `ElevenLabs Voice Design error ${resp.status}: ${errText}` };
567
+ }
568
+ const data = await resp.json();
569
+ const previews = [];
570
+ for (let i = 0; i < (data.previews || []).length; i++) {
571
+ const p = data.previews[i];
572
+ const b64 = p.audio_base_64 || p.audio_base64;
573
+ if (!b64)
574
+ continue;
575
+ try {
576
+ const buf = Buffer.from(b64, "base64");
577
+ const upload = await uploadAudioAndRecord(sb, sid, buf, `Voice design preview ${i + 1}: ${voiceDescription.substring(0, 60)}`, "mp3", "audio/mpeg", ["AI Generated", "Voice Design", "Preview", "ElevenLabs"]);
578
+ previews.push({ generated_voice_id: p.generated_voice_id, file_url: upload.file_url, media_id: upload.media_id });
579
+ }
580
+ catch (uploadErr) {
581
+ console.error(`[voice] preview ${i} upload failed:`, uploadErr);
582
+ previews.push({ generated_voice_id: p.generated_voice_id, file_url: "(upload failed)", media_id: "" });
583
+ }
584
+ }
585
+ return { success: true, data: { previews, description: voiceDescription } };
586
+ }
587
+ catch (err) {
588
+ return { success: false, error: `Voice design failed: ${err}` };
589
+ }
590
+ }
591
+ // ── VOICE DESIGN CREATE ────────────────────────────────────────────
592
+ case "voice_design_create": {
593
+ const generatedVoiceId = args.generated_voice_id;
594
+ const voiceName = args.voice_name;
595
+ if (!generatedVoiceId)
596
+ return { success: false, error: "generated_voice_id is required (from voice_design)" };
597
+ if (!voiceName)
598
+ return { success: false, error: "voice_name is required" };
599
+ try {
600
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/text-to-voice/create`, {
601
+ method: "POST",
602
+ headers: { "xi-api-key": apiKey, "Content-Type": "application/json" },
603
+ body: JSON.stringify({
604
+ generated_voice_id: generatedVoiceId,
605
+ voice_name: voiceName,
606
+ voice_description: args.voice_description || "",
607
+ labels: args.labels || {},
608
+ }),
609
+ });
610
+ if (!resp.ok) {
611
+ const errText = await resp.text();
612
+ return { success: false, error: `ElevenLabs Voice Create error ${resp.status}: ${errText}` };
613
+ }
614
+ return { success: true, data: await resp.json() };
615
+ }
616
+ catch (err) {
617
+ return { success: false, error: `Voice create failed: ${err}` };
618
+ }
619
+ }
620
+ // ── CLONE VOICE ────────────────────────────────────────────────────
621
+ // Accepts single audio_base64 or array of audio_samples for multiple clips.
622
+ // file_paths support: CLI pre-processor reads files and injects audio_samples.
623
+ // ElevenLabs recommends 1-2 min clean audio minimum for instant cloning.
624
+ case "clone_voice": {
625
+ const voiceName = args.voice_name;
626
+ if (!voiceName)
627
+ return { success: false, error: "voice_name is required" };
628
+ // Accept multiple samples (array) or single audio_base64
629
+ const samples = [];
630
+ if (Array.isArray(args.audio_samples)) {
631
+ for (const s of args.audio_samples) {
632
+ if (typeof s === "string" && s.length > 100)
633
+ samples.push(s);
634
+ }
635
+ }
636
+ if (typeof args.audio_base64 === "string" && args.audio_base64.length > 100) {
637
+ samples.push(args.audio_base64);
638
+ }
639
+ if (samples.length === 0) {
640
+ return { success: false, error: "Provide file_paths (array of local audio paths) or audio_samples (array of base64 strings). file_paths is preferred — the CLI encodes automatically." };
641
+ }
642
+ try {
643
+ const formData = new FormData();
644
+ formData.append("name", voiceName);
645
+ if (args.voice_description)
646
+ formData.append("description", args.voice_description);
647
+ // Enable noise removal only if samples are long enough (>= 5s ≈ 80KB base64 at 128kbps)
648
+ // ElevenLabs requires >= 4.6s per sample when noise removal is on
649
+ const shortestSample = Math.min(...samples.map(s => s.length));
650
+ const autoNoiseRemoval = shortestSample >= 80_000; // ~5 seconds at 128kbps
651
+ const removeNoise = args.remove_background_noise ?? autoNoiseRemoval;
652
+ formData.append("remove_background_noise", String(removeNoise));
653
+ // Attach each sample as a separate file
654
+ for (let i = 0; i < samples.length; i++) {
655
+ const buffer = Buffer.from(samples[i], "base64");
656
+ const blob = new Blob([buffer], { type: "audio/mpeg" });
657
+ formData.append("files", blob, `sample-${i}.mp3`);
658
+ }
659
+ // Optional: labels for the voice
660
+ if (args.labels) {
661
+ formData.append("labels", JSON.stringify(args.labels));
662
+ }
663
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/add`, {
664
+ method: "POST",
665
+ headers: { "xi-api-key": apiKey },
666
+ body: formData,
667
+ });
668
+ if (!resp.ok) {
669
+ const errText = await resp.text();
670
+ return { success: false, error: `Voice clone error ${resp.status}: ${errText}` };
671
+ }
672
+ const data = await resp.json();
673
+ // Calculate total audio size for quality feedback
674
+ const totalBytes = samples.reduce((sum, s) => sum + Math.ceil(s.length * 3 / 4), 0);
675
+ const totalSecs = Math.round(totalBytes / 16000); // rough estimate at 128kbps
676
+ const qualityNote = totalSecs < 30
677
+ ? `Tip: only ~${totalSecs}s of audio provided. For better accuracy, use 1-2 minutes of clean, continuous speech.`
678
+ : `Good: ~${totalSecs}s of audio used.`;
679
+ return {
680
+ success: true,
681
+ data: {
682
+ voice_id: data.voice_id,
683
+ name: voiceName,
684
+ samples_used: samples.length,
685
+ noise_removed: removeNoise,
686
+ quality_note: qualityNote,
687
+ message: `Voice "${voiceName}" cloned successfully. Use this voice_id with speak action.`,
688
+ },
689
+ };
690
+ }
691
+ catch (err) {
692
+ return { success: false, error: `Voice clone failed: ${err}` };
693
+ }
694
+ }
695
+ // ══════════════════════════════════════════════════════════════════
696
+ // PROFESSIONAL VOICE CLONING (PVC) — near-perfect accuracy
697
+ // Multi-step: create → upload → separate speakers → verify → train
698
+ // Requires Creator plan or above on ElevenLabs.
699
+ // ══════════════════════════════════════════════════════════════════
700
+ // ── PVC CREATE ─────────────────────────────────────────────────────
701
+ case "pvc_create": {
702
+ const voiceName = args.voice_name;
703
+ const language = args.language || "en";
704
+ if (!voiceName)
705
+ return { success: false, error: "voice_name is required" };
706
+ try {
707
+ const body = { name: voiceName, language };
708
+ if (args.voice_description)
709
+ body.description = args.voice_description;
710
+ if (args.labels)
711
+ body.labels = args.labels;
712
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/pvc`, {
713
+ method: "POST",
714
+ headers: { "xi-api-key": apiKey, "Content-Type": "application/json" },
715
+ body: JSON.stringify(body),
716
+ });
717
+ if (!resp.ok) {
718
+ const errText = await resp.text();
719
+ return { success: false, error: `PVC create error ${resp.status}: ${errText}` };
720
+ }
721
+ const data = await resp.json();
722
+ return {
723
+ success: true,
724
+ data: {
725
+ voice_id: data.voice_id,
726
+ name: voiceName,
727
+ language,
728
+ next_step: "Upload audio samples with pvc_upload action, then use pvc_train to start training.",
729
+ },
730
+ };
731
+ }
732
+ catch (err) {
733
+ return { success: false, error: `PVC create failed: ${err}` };
734
+ }
735
+ }
736
+ // ── PVC UPLOAD ─────────────────────────────────────────────────────
737
+ // Upload audio samples to a PVC voice. Accepts audio_samples or file_paths.
738
+ case "pvc_upload": {
739
+ const voiceId = args.voice_id;
740
+ if (!voiceId)
741
+ return { success: false, error: "voice_id is required (from pvc_create)" };
742
+ const samples = [];
743
+ if (Array.isArray(args.audio_samples)) {
744
+ for (const s of args.audio_samples) {
745
+ if (typeof s === "string" && s.length > 100)
746
+ samples.push(s);
747
+ }
748
+ }
749
+ if (typeof args.audio_base64 === "string" && args.audio_base64.length > 100) {
750
+ samples.push(args.audio_base64);
751
+ }
752
+ if (samples.length === 0) {
753
+ return { success: false, error: "Provide file_paths or audio_samples. The more audio, the better the clone (30+ min recommended)." };
754
+ }
755
+ try {
756
+ const formData = new FormData();
757
+ for (let i = 0; i < samples.length; i++) {
758
+ const buffer = Buffer.from(samples[i], "base64");
759
+ const blob = new Blob([buffer], { type: "audio/mpeg" });
760
+ formData.append("files", blob, `sample-${i}.mp3`);
761
+ }
762
+ const removeNoise = args.remove_background_noise ?? false;
763
+ formData.append("remove_background_noise", String(removeNoise));
764
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/pvc/${voiceId}/samples`, {
765
+ method: "POST",
766
+ headers: { "xi-api-key": apiKey },
767
+ body: formData,
768
+ });
769
+ if (!resp.ok) {
770
+ const errText = await resp.text();
771
+ return { success: false, error: `PVC upload error ${resp.status}: ${errText}` };
772
+ }
773
+ const sampleData = await resp.json();
774
+ const sampleIds = Array.isArray(sampleData) ? sampleData.map((s) => s.sample_id) : [];
775
+ return {
776
+ success: true,
777
+ data: {
778
+ voice_id: voiceId,
779
+ samples_uploaded: samples.length,
780
+ sample_ids: sampleIds,
781
+ noise_removed: removeNoise,
782
+ next_step: "If audio has multiple speakers, use pvc_separate_speakers. Otherwise use pvc_verify to start verification, then pvc_train.",
783
+ },
784
+ };
785
+ }
786
+ catch (err) {
787
+ return { success: false, error: `PVC upload failed: ${err}` };
788
+ }
789
+ }
790
+ // ── PVC SEPARATE SPEAKERS ──────────────────────────────────────────
791
+ case "pvc_separate_speakers": {
792
+ const voiceId = args.voice_id;
793
+ const sampleId = args.sample_id;
794
+ if (!voiceId || !sampleId)
795
+ return { success: false, error: "voice_id and sample_id are required" };
796
+ try {
797
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/pvc/${voiceId}/samples/${sampleId}/separate-speakers`, { method: "POST", headers: { "xi-api-key": apiKey } });
798
+ if (!resp.ok) {
799
+ const errText = await resp.text();
800
+ return { success: false, error: `Speaker separation error ${resp.status}: ${errText}` };
801
+ }
802
+ return {
803
+ success: true,
804
+ data: {
805
+ voice_id: voiceId,
806
+ sample_id: sampleId,
807
+ status: "started",
808
+ next_step: "Use pvc_status to check separation progress. When done, use pvc_select_speaker to pick the right speaker.",
809
+ },
810
+ };
811
+ }
812
+ catch (err) {
813
+ return { success: false, error: `Speaker separation failed: ${err}` };
814
+ }
815
+ }
816
+ // ── PVC STATUS ─────────────────────────────────────────────────────
817
+ // Check speaker separation status or training status
818
+ case "pvc_status": {
819
+ const voiceId = args.voice_id;
820
+ if (!voiceId)
821
+ return { success: false, error: "voice_id is required" };
822
+ try {
823
+ // If sample_id provided, check speaker separation status
824
+ if (args.sample_id) {
825
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/pvc/${voiceId}/samples/${args.sample_id}/speakers`, { headers: { "xi-api-key": apiKey } });
826
+ if (!resp.ok) {
827
+ const errText = await resp.text();
828
+ return { success: false, error: `PVC status error ${resp.status}: ${errText}` };
829
+ }
830
+ const data = await resp.json();
831
+ return { success: true, data };
832
+ }
833
+ // Otherwise check overall voice / training status
834
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/${voiceId}`, {
835
+ headers: { "xi-api-key": apiKey },
836
+ });
837
+ if (!resp.ok) {
838
+ const errText = await resp.text();
839
+ return { success: false, error: `PVC status error ${resp.status}: ${errText}` };
840
+ }
841
+ const voice = await resp.json();
842
+ return {
843
+ success: true,
844
+ data: {
845
+ voice_id: voice.voice_id,
846
+ name: voice.name,
847
+ category: voice.category,
848
+ fine_tuning: voice.fine_tuning,
849
+ samples: voice.samples?.length || 0,
850
+ },
851
+ };
852
+ }
853
+ catch (err) {
854
+ return { success: false, error: `PVC status failed: ${err}` };
855
+ }
856
+ }
857
+ // ── PVC SELECT SPEAKER ─────────────────────────────────────────────
858
+ case "pvc_select_speaker": {
859
+ const voiceId = args.voice_id;
860
+ const sampleId = args.sample_id;
861
+ const speakerIds = args.speaker_ids;
862
+ if (!voiceId || !sampleId)
863
+ return { success: false, error: "voice_id and sample_id required" };
864
+ if (!Array.isArray(speakerIds) || speakerIds.length === 0) {
865
+ return { success: false, error: "speaker_ids array required (from pvc_status separation results)" };
866
+ }
867
+ try {
868
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/pvc/${voiceId}/samples/${sampleId}`, {
869
+ method: "POST",
870
+ headers: { "xi-api-key": apiKey, "Content-Type": "application/json" },
871
+ body: JSON.stringify({ selected_speaker_ids: speakerIds }),
872
+ });
873
+ if (!resp.ok) {
874
+ const errText = await resp.text();
875
+ return { success: false, error: `Select speaker error ${resp.status}: ${errText}` };
876
+ }
877
+ return {
878
+ success: true,
879
+ data: {
880
+ voice_id: voiceId,
881
+ sample_id: sampleId,
882
+ selected_speakers: speakerIds,
883
+ next_step: "Use pvc_verify for verification, then pvc_train to start training.",
884
+ },
885
+ };
886
+ }
887
+ catch (err) {
888
+ return { success: false, error: `Select speaker failed: ${err}` };
889
+ }
890
+ }
891
+ // ── PVC VERIFY ─────────────────────────────────────────────────────
892
+ // Request manual verification (easiest path — ElevenLabs staff reviews)
893
+ case "pvc_verify": {
894
+ const voiceId = args.voice_id;
895
+ if (!voiceId)
896
+ return { success: false, error: "voice_id is required" };
897
+ try {
898
+ const formData = new FormData();
899
+ // Optional: extra context about why you have permission to use this voice
900
+ const extraText = args.extra_text || "Voice owner has given consent for cloning.";
901
+ formData.append("extra_text", extraText);
902
+ // If verification audio provided (voice owner speaking), attach it
903
+ if (typeof args.audio_base64 === "string" && args.audio_base64.length > 100) {
904
+ const buffer = Buffer.from(args.audio_base64, "base64");
905
+ const blob = new Blob([buffer], { type: "audio/mpeg" });
906
+ formData.append("files", blob, "verification.mp3");
907
+ }
908
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/pvc/${voiceId}/verification`, {
909
+ method: "POST",
910
+ headers: { "xi-api-key": apiKey },
911
+ body: formData,
912
+ });
913
+ if (!resp.ok) {
914
+ const errText = await resp.text();
915
+ return { success: false, error: `PVC verify error ${resp.status}: ${errText}` };
916
+ }
917
+ return {
918
+ success: true,
919
+ data: {
920
+ voice_id: voiceId,
921
+ status: "verification_requested",
922
+ message: "Manual verification submitted. ElevenLabs will review. Once approved, use pvc_train to start training.",
923
+ },
924
+ };
925
+ }
926
+ catch (err) {
927
+ return { success: false, error: `PVC verify failed: ${err}` };
928
+ }
929
+ }
930
+ // ── PVC TRAIN ──────────────────────────────────────────────────────
931
+ case "pvc_train": {
932
+ const voiceId = args.voice_id;
933
+ if (!voiceId)
934
+ return { success: false, error: "voice_id is required" };
935
+ // eleven_multilingual_v2 is most stable for PVC training
936
+ const modelId = args.model_id || "eleven_multilingual_v2";
937
+ try {
938
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/pvc/${voiceId}/train`, {
939
+ method: "POST",
940
+ headers: { "xi-api-key": apiKey, "Content-Type": "application/json" },
941
+ body: JSON.stringify({ model_id: modelId }),
942
+ });
943
+ if (!resp.ok) {
944
+ const errText = await resp.text();
945
+ return { success: false, error: `PVC train error ${resp.status}: ${errText}` };
946
+ }
947
+ return {
948
+ success: true,
949
+ data: {
950
+ voice_id: voiceId,
951
+ model_id: modelId,
952
+ status: "training_started",
953
+ message: "Training started. Use pvc_status to monitor progress. Training typically takes 5-30 minutes depending on audio length.",
954
+ },
955
+ };
956
+ }
957
+ catch (err) {
958
+ return { success: false, error: `PVC train failed: ${err}` };
959
+ }
960
+ }
961
+ // ── FORCED ALIGNMENT ───────────────────────────────────────────────
962
+ case "forced_alignment": {
963
+ const audioBase64 = args.audio_base64;
964
+ const transcript = args.transcript;
965
+ if (!audioBase64)
966
+ return { success: false, error: "audio_base64 parameter is required" };
967
+ if (!transcript)
968
+ return { success: false, error: "transcript parameter is required" };
969
+ try {
970
+ const formData = audioFormData(audioBase64);
971
+ formData.append("text", transcript);
972
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/forced-alignment`, {
973
+ method: "POST",
974
+ headers: { "xi-api-key": apiKey },
975
+ body: formData,
976
+ });
977
+ if (!resp.ok) {
978
+ const errText = await resp.text();
979
+ return { success: false, error: `ElevenLabs Forced Alignment error ${resp.status}: ${errText}` };
980
+ }
981
+ return { success: true, data: await resp.json() };
982
+ }
983
+ catch (err) {
984
+ return { success: false, error: `Forced alignment failed: ${err}` };
985
+ }
986
+ }
987
+ // ── SEARCH VOICES ──────────────────────────────────────────────────
988
+ case "search_voices": {
989
+ const query = args.query || "";
990
+ try {
991
+ // Use shared voices endpoint for broader library search
992
+ const params = new URLSearchParams({ page_size: "25" });
993
+ if (query)
994
+ params.set("search", query);
995
+ if (args.gender)
996
+ params.set("gender", args.gender);
997
+ if (args.language)
998
+ params.set("language", args.language);
999
+ if (args.accent)
1000
+ params.set("accent", args.accent);
1001
+ if (args.age)
1002
+ params.set("age", args.age);
1003
+ if (args.use_case)
1004
+ params.set("use_case", args.use_case);
1005
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/shared-voices?${params}`, {
1006
+ headers: { "xi-api-key": apiKey },
1007
+ });
1008
+ if (!resp.ok) {
1009
+ const errText = await resp.text();
1010
+ return { success: false, error: `Voice search error ${resp.status}: ${errText}` };
1011
+ }
1012
+ const data = await resp.json();
1013
+ const voices = (data.voices || []).map((v) => ({
1014
+ voice_id: v.voice_id,
1015
+ name: v.name,
1016
+ category: v.category,
1017
+ gender: v.gender,
1018
+ accent: v.accent,
1019
+ age: v.age,
1020
+ language: v.language,
1021
+ use_case: v.use_case,
1022
+ description: v.description,
1023
+ preview_url: v.preview_url,
1024
+ }));
1025
+ return { success: true, data: { count: voices.length, voices } };
1026
+ }
1027
+ catch (err) {
1028
+ return { success: false, error: `Voice search failed: ${err}` };
1029
+ }
1030
+ }
1031
+ // ── LIST VOICES ────────────────────────────────────────────────────
1032
+ case "list_voices": {
1033
+ try {
1034
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices`, {
1035
+ headers: { "xi-api-key": apiKey },
1036
+ });
1037
+ if (!resp.ok) {
1038
+ const errText = await resp.text();
1039
+ return { success: false, error: `List Voices error ${resp.status}: ${errText}` };
1040
+ }
1041
+ const data = await resp.json();
1042
+ const voices = (data.voices || []).map((v) => ({
1043
+ voice_id: v.voice_id, name: v.name, category: v.category,
1044
+ labels: v.labels, description: v.description, preview_url: v.preview_url,
1045
+ }));
1046
+ return { success: true, data: { count: voices.length, voices } };
1047
+ }
1048
+ catch (err) {
1049
+ return { success: false, error: `List voices failed: ${err}` };
1050
+ }
1051
+ }
1052
+ // ── GET VOICE ──────────────────────────────────────────────────────
1053
+ case "get_voice": {
1054
+ const voiceInput = (args.voice_name || args.voice_id);
1055
+ if (!voiceInput)
1056
+ return { success: false, error: "voice_id or voice_name is required" };
1057
+ try {
1058
+ const voiceId = await resolveVoiceId(voiceInput, apiKey, sid);
1059
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/voices/${voiceId}`, {
1060
+ headers: { "xi-api-key": apiKey },
1061
+ });
1062
+ if (!resp.ok) {
1063
+ const errText = await resp.text();
1064
+ return { success: false, error: `Get Voice error ${resp.status}: ${errText}` };
1065
+ }
1066
+ const voice = await resp.json();
1067
+ return {
1068
+ success: true,
1069
+ data: {
1070
+ voice_id: voice.voice_id, name: voice.name, category: voice.category,
1071
+ labels: voice.labels, description: voice.description, preview_url: voice.preview_url,
1072
+ settings: voice.settings, fine_tuning: voice.fine_tuning,
1073
+ },
1074
+ };
1075
+ }
1076
+ catch (err) {
1077
+ return { success: false, error: `Get voice failed: ${err}` };
1078
+ }
1079
+ }
1080
+ // ── USAGE / QUOTA ──────────────────────────────────────────────────
1081
+ case "usage": {
1082
+ try {
1083
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/user/subscription`, {
1084
+ headers: { "xi-api-key": apiKey },
1085
+ });
1086
+ if (!resp.ok) {
1087
+ const errText = await resp.text();
1088
+ return { success: false, error: `Usage error ${resp.status}: ${errText}` };
1089
+ }
1090
+ const sub = await resp.json();
1091
+ return {
1092
+ success: true,
1093
+ data: {
1094
+ tier: sub.tier,
1095
+ character_count: sub.character_count,
1096
+ character_limit: sub.character_limit,
1097
+ characters_remaining: (sub.character_limit || 0) - (sub.character_count || 0),
1098
+ usage_percent: sub.character_limit
1099
+ ? Math.round(((sub.character_count || 0) / sub.character_limit) * 100)
1100
+ : 0,
1101
+ next_reset: sub.next_character_count_reset_unix
1102
+ ? new Date(sub.next_character_count_reset_unix * 1000).toISOString()
1103
+ : null,
1104
+ max_voice_add_edits: sub.max_voice_add_edits,
1105
+ voice_add_edit_counter: sub.voice_add_edit_counter,
1106
+ professional_voice_limit: sub.professional_voice_limit,
1107
+ },
1108
+ };
1109
+ }
1110
+ catch (err) {
1111
+ return { success: false, error: `Usage check failed: ${err}` };
1112
+ }
1113
+ }
1114
+ // ── LIST MODELS ────────────────────────────────────────────────────
1115
+ case "list_models": {
1116
+ try {
1117
+ const resp = await fetchWithRetry(`${ELEVENLABS_BASE}/models`, {
1118
+ headers: { "xi-api-key": apiKey },
1119
+ });
1120
+ if (!resp.ok) {
1121
+ const errText = await resp.text();
1122
+ return { success: false, error: `List Models error ${resp.status}: ${errText}` };
1123
+ }
1124
+ const models = await resp.json();
1125
+ return {
1126
+ success: true,
1127
+ data: (models || []).map((m) => ({
1128
+ model_id: m.model_id, name: m.name, description: m.description,
1129
+ can_be_finetuned: m.can_be_finetuned,
1130
+ can_do_text_to_speech: m.can_do_text_to_speech,
1131
+ can_do_voice_conversion: m.can_do_voice_conversion,
1132
+ languages: m.languages,
1133
+ })),
1134
+ };
1135
+ }
1136
+ catch (err) {
1137
+ return { success: false, error: `List models failed: ${err}` };
1138
+ }
1139
+ }
1140
+ default:
1141
+ return {
1142
+ success: false,
1143
+ error: `Unknown voice action: ${action}. Valid: speak, batch, sound_effects, music_plan, music_compose, music_compose_detailed, speech_to_text, stem_separation, voice_changer, audio_isolation, voice_design, voice_design_create, clone_voice, forced_alignment, search_voices, list_voices, get_voice, usage, list_models, pvc_create, pvc_upload, pvc_separate_speakers, pvc_status, pvc_select_speaker, pvc_verify, pvc_train`,
1144
+ };
1145
+ }
1146
+ }