@contractspec/example.voice-providers 1.57.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 (49) hide show
  1. package/.turbo/turbo-build$colon$bundle.log +46 -0
  2. package/.turbo/turbo-build.log +47 -0
  3. package/CHANGELOG.md +27 -0
  4. package/LICENSE +21 -0
  5. package/README.md +14 -0
  6. package/dist/connection.sample.d.ts +9 -0
  7. package/dist/connection.sample.d.ts.map +1 -0
  8. package/dist/connection.sample.js +51 -0
  9. package/dist/connection.sample.js.map +1 -0
  10. package/dist/docs/index.d.ts +1 -0
  11. package/dist/docs/index.js +1 -0
  12. package/dist/docs/voice-providers.docblock.d.ts +1 -0
  13. package/dist/docs/voice-providers.docblock.js +31 -0
  14. package/dist/docs/voice-providers.docblock.js.map +1 -0
  15. package/dist/example.d.ts +7 -0
  16. package/dist/example.d.ts.map +1 -0
  17. package/dist/example.js +46 -0
  18. package/dist/example.js.map +1 -0
  19. package/dist/handlers/create-provider.d.ts +29 -0
  20. package/dist/handlers/create-provider.d.ts.map +1 -0
  21. package/dist/handlers/create-provider.js +32 -0
  22. package/dist/handlers/create-provider.js.map +1 -0
  23. package/dist/handlers/list-voices.d.ts +8 -0
  24. package/dist/handlers/list-voices.d.ts.map +1 -0
  25. package/dist/handlers/list-voices.js +10 -0
  26. package/dist/handlers/list-voices.js.map +1 -0
  27. package/dist/handlers/synthesize.d.ts +11 -0
  28. package/dist/handlers/synthesize.d.ts.map +1 -0
  29. package/dist/handlers/synthesize.js +10 -0
  30. package/dist/handlers/synthesize.js.map +1 -0
  31. package/dist/index.d.ts +6 -0
  32. package/dist/index.js +8 -0
  33. package/dist/run.d.ts +5 -0
  34. package/dist/run.d.ts.map +1 -0
  35. package/dist/run.js +118 -0
  36. package/dist/run.js.map +1 -0
  37. package/package.json +65 -0
  38. package/src/connection.sample.ts +54 -0
  39. package/src/docs/index.ts +1 -0
  40. package/src/docs/voice-providers.docblock.ts +48 -0
  41. package/src/example.ts +32 -0
  42. package/src/handlers/create-provider.ts +70 -0
  43. package/src/handlers/list-voices.ts +13 -0
  44. package/src/handlers/synthesize.ts +20 -0
  45. package/src/index.ts +6 -0
  46. package/src/run.ts +180 -0
  47. package/tsconfig.json +9 -0
  48. package/tsconfig.tsbuildinfo +1 -0
  49. package/tsdown.config.js +6 -0
package/dist/run.js ADDED
@@ -0,0 +1,118 @@
1
+ import { listVoices } from "./handlers/list-voices.js";
2
+ import { synthesizeVoice } from "./handlers/synthesize.js";
3
+
4
+ //#region src/run.ts
5
+ async function runVoiceProvidersExampleFromEnv() {
6
+ const integrationKey = resolveIntegrationKey();
7
+ const mode = resolveMode();
8
+ const dryRun = process.env.CONTRACTSPEC_VOICE_DRY_RUN === "true";
9
+ const config = resolveConfig(integrationKey);
10
+ const text = process.env.CONTRACTSPEC_VOICE_TEXT ?? "Hello from ContractSpec voice providers example.";
11
+ const voiceId = process.env.CONTRACTSPEC_VOICE_ID;
12
+ if (dryRun) return {
13
+ integrationKey,
14
+ mode,
15
+ dryRun,
16
+ text,
17
+ voiceId,
18
+ config
19
+ };
20
+ const input = {
21
+ integrationKey,
22
+ secrets: { apiKey: resolveApiKey(integrationKey) },
23
+ config
24
+ };
25
+ const output = {
26
+ integrationKey,
27
+ mode,
28
+ dryRun
29
+ };
30
+ if (mode === "list" || mode === "both") output.voices = await listVoices(input);
31
+ if (mode === "synthesize" || mode === "both") {
32
+ const result = await synthesizeVoice({
33
+ ...input,
34
+ synthesis: {
35
+ text,
36
+ voiceId
37
+ }
38
+ });
39
+ output.synthesis = {
40
+ format: result.format,
41
+ sampleRateHz: result.sampleRateHz,
42
+ bytes: result.audio.length,
43
+ url: result.url
44
+ };
45
+ }
46
+ return output;
47
+ }
48
+ function resolveMode() {
49
+ const raw = (process.env.CONTRACTSPEC_VOICE_MODE ?? "both").toLowerCase();
50
+ if (raw === "list" || raw === "synthesize" || raw === "both") return raw;
51
+ throw new Error(`Unsupported CONTRACTSPEC_VOICE_MODE: ${raw}. Use list, synthesize, or both.`);
52
+ }
53
+ function resolveIntegrationKey() {
54
+ const raw = (process.env.CONTRACTSPEC_VOICE_PROVIDER ?? "gradium").toLowerCase();
55
+ if (raw === "gradium") return "ai-voice.gradium";
56
+ if (raw === "fal") return "ai-voice.fal";
57
+ throw new Error(`Unsupported CONTRACTSPEC_VOICE_PROVIDER: ${raw}. Use gradium or fal.`);
58
+ }
59
+ function resolveApiKey(integrationKey) {
60
+ const shared = process.env.CONTRACTSPEC_VOICE_API_KEY;
61
+ if (shared) return shared;
62
+ const specific = integrationKey === "ai-voice.gradium" ? process.env.GRADIUM_API_KEY : process.env.FAL_KEY;
63
+ if (!specific) {
64
+ const envName = integrationKey === "ai-voice.gradium" ? "GRADIUM_API_KEY" : "FAL_KEY";
65
+ throw new Error(`Missing API key. Set CONTRACTSPEC_VOICE_API_KEY or ${envName}.`);
66
+ }
67
+ return specific;
68
+ }
69
+ function resolveConfig(integrationKey) {
70
+ if (integrationKey === "ai-voice.gradium") return {
71
+ defaultVoiceId: process.env.GRADIUM_DEFAULT_VOICE_ID,
72
+ region: process.env.GRADIUM_REGION === "eu" || process.env.GRADIUM_REGION === "us" ? process.env.GRADIUM_REGION : void 0,
73
+ baseUrl: process.env.GRADIUM_BASE_URL,
74
+ timeoutMs: parseOptionalInt(process.env.GRADIUM_TIMEOUT_MS),
75
+ outputFormat: parseGradiumOutputFormat(process.env.GRADIUM_OUTPUT_FORMAT)
76
+ };
77
+ return {
78
+ modelId: process.env.FAL_MODEL_ID,
79
+ defaultVoiceUrl: process.env.FAL_DEFAULT_VOICE_URL,
80
+ defaultExaggeration: parseOptionalNumber(process.env.FAL_DEFAULT_EXAGGERATION),
81
+ defaultTemperature: parseOptionalNumber(process.env.FAL_DEFAULT_TEMPERATURE),
82
+ defaultCfg: parseOptionalNumber(process.env.FAL_DEFAULT_CFG),
83
+ pollIntervalMs: parseOptionalInt(process.env.FAL_POLL_INTERVAL_MS)
84
+ };
85
+ }
86
+ function parseOptionalInt(value) {
87
+ if (!value) return void 0;
88
+ const parsed = Number.parseInt(value, 10);
89
+ return Number.isFinite(parsed) ? parsed : void 0;
90
+ }
91
+ function parseOptionalNumber(value) {
92
+ if (!value) return void 0;
93
+ const parsed = Number.parseFloat(value);
94
+ return Number.isFinite(parsed) ? parsed : void 0;
95
+ }
96
+ function parseGradiumOutputFormat(value) {
97
+ if (!value) return void 0;
98
+ switch (value) {
99
+ case "wav":
100
+ case "pcm":
101
+ case "opus":
102
+ case "ulaw_8000":
103
+ case "alaw_8000":
104
+ case "pcm_16000":
105
+ case "pcm_24000": return value;
106
+ default: return;
107
+ }
108
+ }
109
+ runVoiceProvidersExampleFromEnv().then((result) => {
110
+ console.log(JSON.stringify(result, null, 2));
111
+ }).catch((error) => {
112
+ console.error(error);
113
+ process.exitCode = 1;
114
+ });
115
+
116
+ //#endregion
117
+ export { runVoiceProvidersExampleFromEnv };
118
+ //# sourceMappingURL=run.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run.js","names":[],"sources":["../src/run.ts"],"sourcesContent":["import { listVoices } from './handlers/list-voices';\nimport { synthesizeVoice } from './handlers/synthesize';\nimport type {\n VoiceIntegrationKey,\n VoiceProviderConfig,\n VoiceProviderFactoryInput,\n} from './handlers/create-provider';\n\ntype VoiceMode = 'list' | 'synthesize' | 'both';\n\nexport async function runVoiceProvidersExampleFromEnv() {\n const integrationKey = resolveIntegrationKey();\n const mode = resolveMode();\n const dryRun = process.env.CONTRACTSPEC_VOICE_DRY_RUN === 'true';\n const config = resolveConfig(integrationKey);\n const text =\n process.env.CONTRACTSPEC_VOICE_TEXT ??\n 'Hello from ContractSpec voice providers example.';\n const voiceId = process.env.CONTRACTSPEC_VOICE_ID;\n\n if (dryRun) {\n return {\n integrationKey,\n mode,\n dryRun,\n text,\n voiceId,\n config,\n };\n }\n\n const input: VoiceProviderFactoryInput = {\n integrationKey,\n secrets: {\n apiKey: resolveApiKey(integrationKey),\n },\n config,\n };\n\n const output: Record<string, unknown> = {\n integrationKey,\n mode,\n dryRun,\n };\n\n if (mode === 'list' || mode === 'both') {\n const voices = await listVoices(input);\n output.voices = voices;\n }\n\n if (mode === 'synthesize' || mode === 'both') {\n const result = await synthesizeVoice({\n ...input,\n synthesis: {\n text,\n voiceId,\n },\n });\n output.synthesis = {\n format: result.format,\n sampleRateHz: result.sampleRateHz,\n bytes: result.audio.length,\n url: result.url,\n };\n }\n\n return output;\n}\n\nfunction resolveMode(): VoiceMode {\n const raw = (process.env.CONTRACTSPEC_VOICE_MODE ?? 'both').toLowerCase();\n if (raw === 'list' || raw === 'synthesize' || raw === 'both') {\n return raw;\n }\n throw new Error(\n `Unsupported CONTRACTSPEC_VOICE_MODE: ${raw}. Use list, synthesize, or both.`\n );\n}\n\nfunction resolveIntegrationKey(): VoiceIntegrationKey {\n const raw = (\n process.env.CONTRACTSPEC_VOICE_PROVIDER ?? 'gradium'\n ).toLowerCase();\n if (raw === 'gradium') return 'ai-voice.gradium';\n if (raw === 'fal') return 'ai-voice.fal';\n throw new Error(\n `Unsupported CONTRACTSPEC_VOICE_PROVIDER: ${raw}. Use gradium or fal.`\n );\n}\n\nfunction resolveApiKey(integrationKey: VoiceIntegrationKey): string {\n const shared = process.env.CONTRACTSPEC_VOICE_API_KEY;\n if (shared) return shared;\n\n const specific =\n integrationKey === 'ai-voice.gradium'\n ? process.env.GRADIUM_API_KEY\n : process.env.FAL_KEY;\n\n if (!specific) {\n const envName =\n integrationKey === 'ai-voice.gradium' ? 'GRADIUM_API_KEY' : 'FAL_KEY';\n throw new Error(\n `Missing API key. Set CONTRACTSPEC_VOICE_API_KEY or ${envName}.`\n );\n }\n\n return specific;\n}\n\nfunction resolveConfig(\n integrationKey: VoiceIntegrationKey\n): VoiceProviderConfig {\n if (integrationKey === 'ai-voice.gradium') {\n const config: VoiceProviderConfig = {\n defaultVoiceId: process.env.GRADIUM_DEFAULT_VOICE_ID,\n region:\n process.env.GRADIUM_REGION === 'eu' ||\n process.env.GRADIUM_REGION === 'us'\n ? process.env.GRADIUM_REGION\n : undefined,\n baseUrl: process.env.GRADIUM_BASE_URL,\n timeoutMs: parseOptionalInt(process.env.GRADIUM_TIMEOUT_MS),\n outputFormat: parseGradiumOutputFormat(process.env.GRADIUM_OUTPUT_FORMAT),\n };\n return config;\n }\n\n return {\n modelId: process.env.FAL_MODEL_ID,\n defaultVoiceUrl: process.env.FAL_DEFAULT_VOICE_URL,\n defaultExaggeration: parseOptionalNumber(\n process.env.FAL_DEFAULT_EXAGGERATION\n ),\n defaultTemperature: parseOptionalNumber(\n process.env.FAL_DEFAULT_TEMPERATURE\n ),\n defaultCfg: parseOptionalNumber(process.env.FAL_DEFAULT_CFG),\n pollIntervalMs: parseOptionalInt(process.env.FAL_POLL_INTERVAL_MS),\n };\n}\n\nfunction parseOptionalInt(value: string | undefined): number | undefined {\n if (!value) return undefined;\n const parsed = Number.parseInt(value, 10);\n return Number.isFinite(parsed) ? parsed : undefined;\n}\n\nfunction parseOptionalNumber(value: string | undefined): number | undefined {\n if (!value) return undefined;\n const parsed = Number.parseFloat(value);\n return Number.isFinite(parsed) ? parsed : undefined;\n}\n\nfunction parseGradiumOutputFormat(\n value: string | undefined\n): VoiceProviderConfig['outputFormat'] {\n if (!value) return undefined;\n switch (value) {\n case 'wav':\n case 'pcm':\n case 'opus':\n case 'ulaw_8000':\n case 'alaw_8000':\n case 'pcm_16000':\n case 'pcm_24000':\n return value;\n default:\n return undefined;\n }\n}\n\nrunVoiceProvidersExampleFromEnv()\n .then((result) => {\n console.log(JSON.stringify(result, null, 2));\n })\n .catch((error) => {\n console.error(error);\n process.exitCode = 1;\n });\n"],"mappings":";;;;AAUA,eAAsB,kCAAkC;CACtD,MAAM,iBAAiB,uBAAuB;CAC9C,MAAM,OAAO,aAAa;CAC1B,MAAM,SAAS,QAAQ,IAAI,+BAA+B;CAC1D,MAAM,SAAS,cAAc,eAAe;CAC5C,MAAM,OACJ,QAAQ,IAAI,2BACZ;CACF,MAAM,UAAU,QAAQ,IAAI;AAE5B,KAAI,OACF,QAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACD;CAGH,MAAM,QAAmC;EACvC;EACA,SAAS,EACP,QAAQ,cAAc,eAAe,EACtC;EACD;EACD;CAED,MAAM,SAAkC;EACtC;EACA;EACA;EACD;AAED,KAAI,SAAS,UAAU,SAAS,OAE9B,QAAO,SADQ,MAAM,WAAW,MAAM;AAIxC,KAAI,SAAS,gBAAgB,SAAS,QAAQ;EAC5C,MAAM,SAAS,MAAM,gBAAgB;GACnC,GAAG;GACH,WAAW;IACT;IACA;IACD;GACF,CAAC;AACF,SAAO,YAAY;GACjB,QAAQ,OAAO;GACf,cAAc,OAAO;GACrB,OAAO,OAAO,MAAM;GACpB,KAAK,OAAO;GACb;;AAGH,QAAO;;AAGT,SAAS,cAAyB;CAChC,MAAM,OAAO,QAAQ,IAAI,2BAA2B,QAAQ,aAAa;AACzE,KAAI,QAAQ,UAAU,QAAQ,gBAAgB,QAAQ,OACpD,QAAO;AAET,OAAM,IAAI,MACR,wCAAwC,IAAI,kCAC7C;;AAGH,SAAS,wBAA6C;CACpD,MAAM,OACJ,QAAQ,IAAI,+BAA+B,WAC3C,aAAa;AACf,KAAI,QAAQ,UAAW,QAAO;AAC9B,KAAI,QAAQ,MAAO,QAAO;AAC1B,OAAM,IAAI,MACR,4CAA4C,IAAI,uBACjD;;AAGH,SAAS,cAAc,gBAA6C;CAClE,MAAM,SAAS,QAAQ,IAAI;AAC3B,KAAI,OAAQ,QAAO;CAEnB,MAAM,WACJ,mBAAmB,qBACf,QAAQ,IAAI,kBACZ,QAAQ,IAAI;AAElB,KAAI,CAAC,UAAU;EACb,MAAM,UACJ,mBAAmB,qBAAqB,oBAAoB;AAC9D,QAAM,IAAI,MACR,sDAAsD,QAAQ,GAC/D;;AAGH,QAAO;;AAGT,SAAS,cACP,gBACqB;AACrB,KAAI,mBAAmB,mBAYrB,QAXoC;EAClC,gBAAgB,QAAQ,IAAI;EAC5B,QACE,QAAQ,IAAI,mBAAmB,QAC/B,QAAQ,IAAI,mBAAmB,OAC3B,QAAQ,IAAI,iBACZ;EACN,SAAS,QAAQ,IAAI;EACrB,WAAW,iBAAiB,QAAQ,IAAI,mBAAmB;EAC3D,cAAc,yBAAyB,QAAQ,IAAI,sBAAsB;EAC1E;AAIH,QAAO;EACL,SAAS,QAAQ,IAAI;EACrB,iBAAiB,QAAQ,IAAI;EAC7B,qBAAqB,oBACnB,QAAQ,IAAI,yBACb;EACD,oBAAoB,oBAClB,QAAQ,IAAI,wBACb;EACD,YAAY,oBAAoB,QAAQ,IAAI,gBAAgB;EAC5D,gBAAgB,iBAAiB,QAAQ,IAAI,qBAAqB;EACnE;;AAGH,SAAS,iBAAiB,OAA+C;AACvE,KAAI,CAAC,MAAO,QAAO;CACnB,MAAM,SAAS,OAAO,SAAS,OAAO,GAAG;AACzC,QAAO,OAAO,SAAS,OAAO,GAAG,SAAS;;AAG5C,SAAS,oBAAoB,OAA+C;AAC1E,KAAI,CAAC,MAAO,QAAO;CACnB,MAAM,SAAS,OAAO,WAAW,MAAM;AACvC,QAAO,OAAO,SAAS,OAAO,GAAG,SAAS;;AAG5C,SAAS,yBACP,OACqC;AACrC,KAAI,CAAC,MAAO,QAAO;AACnB,SAAQ,OAAR;EACE,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK,YACH,QAAO;EACT,QACE;;;AAIN,iCAAiC,CAC9B,MAAM,WAAW;AAChB,SAAQ,IAAI,KAAK,UAAU,QAAQ,MAAM,EAAE,CAAC;EAC5C,CACD,OAAO,UAAU;AAChB,SAAQ,MAAM,MAAM;AACpB,SAAQ,WAAW;EACnB"}
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@contractspec/example.voice-providers",
3
+ "version": "1.57.0",
4
+ "description": "Voice provider example: Gradium and Fal text-to-speech integration patterns.",
5
+ "type": "module",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": "./dist/index.js",
9
+ "./connection.sample": "./dist/connection.sample.js",
10
+ "./docs": "./dist/docs/index.js",
11
+ "./docs/voice-providers.docblock": "./dist/docs/voice-providers.docblock.js",
12
+ "./example": "./dist/example.js",
13
+ "./handlers/create-provider": "./dist/handlers/create-provider.js",
14
+ "./handlers/list-voices": "./dist/handlers/list-voices.js",
15
+ "./handlers/synthesize": "./dist/handlers/synthesize.js",
16
+ "./run": "./dist/run.js",
17
+ "./*": "./*"
18
+ },
19
+ "scripts": {
20
+ "publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
21
+ "publish:pkg:canary": "bun publish:pkg --tag canary",
22
+ "build": "bun build:types && bun build:bundle",
23
+ "build:bundle": "tsdown",
24
+ "build:types": "tsc --noEmit",
25
+ "dev": "bun build:bundle --watch",
26
+ "clean": "rimraf dist .turbo",
27
+ "lint": "bun lint:fix",
28
+ "lint:fix": "eslint src --fix",
29
+ "lint:check": "eslint src",
30
+ "test": "bun test"
31
+ },
32
+ "dependencies": {
33
+ "@contractspec/integration.providers-impls": "1.57.0",
34
+ "@contractspec/lib.contracts": "1.57.0"
35
+ },
36
+ "devDependencies": {
37
+ "@contractspec/tool.tsdown": "1.57.0",
38
+ "@contractspec/tool.typescript": "1.57.0",
39
+ "tsdown": "^0.20.3",
40
+ "typescript": "^5.9.3"
41
+ },
42
+ "publishConfig": {
43
+ "access": "public",
44
+ "exports": {
45
+ ".": "./dist/index.js",
46
+ "./docs": "./dist/docs/index.js",
47
+ "./docs/voice-providers.docblock": "./dist/docs/voice-providers.docblock.js",
48
+ "./example": "./dist/example.js",
49
+ "./connection.sample": "./dist/connection.sample.js",
50
+ "./handlers/create-provider": "./dist/handlers/create-provider.js",
51
+ "./handlers/list-voices": "./dist/handlers/list-voices.js",
52
+ "./handlers/synthesize": "./dist/handlers/synthesize.js",
53
+ "./run": "./dist/run.js",
54
+ "./*": "./*"
55
+ },
56
+ "registry": "https://registry.npmjs.org/"
57
+ },
58
+ "license": "MIT",
59
+ "repository": {
60
+ "type": "git",
61
+ "url": "https://github.com/lssm-tech/contractspec.git",
62
+ "directory": "packages/examples/voice-providers"
63
+ },
64
+ "homepage": "https://contractspec.io"
65
+ }
@@ -0,0 +1,54 @@
1
+ import type { IntegrationConnection } from '@contractspec/lib.contracts/integrations/connection';
2
+
3
+ export const gradiumVoiceConnection: IntegrationConnection = {
4
+ meta: {
5
+ id: 'conn-gradium-voice-demo',
6
+ tenantId: 'acme-inc',
7
+ integrationKey: 'ai-voice.gradium',
8
+ integrationVersion: '1.0.0',
9
+ label: 'Gradium Voice',
10
+ environment: 'production',
11
+ createdAt: '2026-01-01T00:00:00.000Z',
12
+ updatedAt: '2026-01-01T00:00:00.000Z',
13
+ },
14
+ ownershipMode: 'byok',
15
+ config: {
16
+ defaultVoiceId: 'YTpq7expH9539ERJ',
17
+ region: 'eu',
18
+ outputFormat: 'wav',
19
+ },
20
+ secretProvider: 'vault',
21
+ secretRef: 'vault://integrations/acme-inc/conn-gradium-voice-demo',
22
+ status: 'connected',
23
+ };
24
+
25
+ export const falVoiceConnection: IntegrationConnection = {
26
+ meta: {
27
+ id: 'conn-fal-voice-demo',
28
+ tenantId: 'acme-inc',
29
+ integrationKey: 'ai-voice.fal',
30
+ integrationVersion: '1.0.0',
31
+ label: 'Fal Voice',
32
+ environment: 'production',
33
+ createdAt: '2026-01-01T00:00:00.000Z',
34
+ updatedAt: '2026-01-01T00:00:00.000Z',
35
+ },
36
+ ownershipMode: 'byok',
37
+ config: {
38
+ modelId: 'fal-ai/chatterbox/text-to-speech',
39
+ defaultVoiceUrl:
40
+ 'https://storage.googleapis.com/chatterbox-demo-samples/prompts/male_rickmorty.mp3',
41
+ defaultExaggeration: 0.25,
42
+ defaultTemperature: 0.7,
43
+ defaultCfg: 0.5,
44
+ pollIntervalMs: 1000,
45
+ },
46
+ secretProvider: 'vault',
47
+ secretRef: 'vault://integrations/acme-inc/conn-fal-voice-demo',
48
+ status: 'connected',
49
+ };
50
+
51
+ export const voiceSampleConnections: IntegrationConnection[] = [
52
+ gradiumVoiceConnection,
53
+ falVoiceConnection,
54
+ ];
@@ -0,0 +1 @@
1
+ import './voice-providers.docblock';
@@ -0,0 +1,48 @@
1
+ import type { DocBlock } from '@contractspec/lib.contracts/docs';
2
+ import { registerDocBlocks } from '@contractspec/lib.contracts/docs';
3
+
4
+ const blocks: DocBlock[] = [
5
+ {
6
+ id: 'docs.examples.voice-providers',
7
+ title: 'Voice Providers (example)',
8
+ summary:
9
+ 'Multi-provider voice integration example covering Gradium and Fal text-to-speech flows.',
10
+ kind: 'reference',
11
+ visibility: 'public',
12
+ route: '/docs/examples/voice-providers',
13
+ tags: ['voice', 'tts', 'gradium', 'fal', 'example'],
14
+ body:
15
+ '## What this example shows\n' +
16
+ '- Provider selection for `ai-voice.gradium` and `ai-voice.fal`.\n' +
17
+ '- Listing voice catalogs and synthesizing text into audio bytes.\n' +
18
+ '- Connection metadata patterns for BYOK secret references.\n\n' +
19
+ '## Secrets and config\n' +
20
+ '- `apiKey` for each provider.\n' +
21
+ '- Gradium config: `defaultVoiceId`, `region`, `outputFormat`.\n' +
22
+ '- Fal config: `modelId`, `defaultVoiceUrl`, synthesis tuning fields.\n\n' +
23
+ '## Guardrails\n' +
24
+ '- Keep API keys in secret providers only.\n' +
25
+ '- Prefer declarative provider config over hardcoded runtime options.\n' +
26
+ '- Keep synthesis side effects explicit for deterministic workflows.',
27
+ },
28
+ {
29
+ id: 'docs.examples.voice-providers.usage',
30
+ title: 'Voice Providers - Usage',
31
+ summary:
32
+ 'How to wire provider factory and synthesis helpers in runtime code.',
33
+ kind: 'usage',
34
+ visibility: 'public',
35
+ route: '/docs/examples/voice-providers/usage',
36
+ tags: ['voice', 'usage'],
37
+ body:
38
+ '## Usage\n' +
39
+ '- Call `createVoiceProvider` with integration key, secrets, and config.\n' +
40
+ '- Use `listVoices` to expose voice choices in admin/config screens.\n' +
41
+ '- Use `synthesizeVoice` for message generation or workflow steps.\n\n' +
42
+ '## Notes\n' +
43
+ '- Fal uses an audio URL output; this example downloads bytes for a canonical result shape.\n' +
44
+ '- Gradium maps provider output formats into ContractSpec voice result conventions.',
45
+ },
46
+ ];
47
+
48
+ registerDocBlocks(blocks);
package/src/example.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { defineExample } from '@contractspec/lib.contracts';
2
+
3
+ const example = defineExample({
4
+ meta: {
5
+ key: 'voice-providers',
6
+ version: '1.0.0',
7
+ title: 'Voice Providers (Gradium and Fal)',
8
+ description:
9
+ 'Multi-provider voice integration example for Gradium and Fal text-to-speech adapters.',
10
+ kind: 'integration',
11
+ visibility: 'public',
12
+ stability: 'experimental',
13
+ owners: ['@platform.integrations'],
14
+ tags: ['voice', 'tts', 'gradium', 'fal', 'integrations'],
15
+ },
16
+ docs: {
17
+ rootDocId: 'docs.examples.voice-providers',
18
+ usageDocId: 'docs.examples.voice-providers.usage',
19
+ },
20
+ entrypoints: {
21
+ packageName: '@contractspec/example.voice-providers',
22
+ docs: './docs',
23
+ },
24
+ surfaces: {
25
+ templates: true,
26
+ sandbox: { enabled: true, modes: ['markdown', 'specs'] },
27
+ studio: { enabled: true, installable: true },
28
+ mcp: { enabled: true },
29
+ },
30
+ });
31
+
32
+ export default example;
@@ -0,0 +1,70 @@
1
+ import { FalVoiceProvider } from '@contractspec/integration.providers-impls/impls/fal-voice';
2
+ import { GradiumVoiceProvider } from '@contractspec/integration.providers-impls/impls/gradium-voice';
3
+ import type { VoiceProvider } from '@contractspec/lib.contracts/integrations/providers/voice';
4
+
5
+ export type VoiceIntegrationKey = 'ai-voice.gradium' | 'ai-voice.fal';
6
+
7
+ export interface VoiceProviderSecrets {
8
+ apiKey: string;
9
+ }
10
+
11
+ export interface VoiceProviderConfig {
12
+ defaultVoiceId?: string;
13
+ region?: 'eu' | 'us';
14
+ baseUrl?: string;
15
+ timeoutMs?: number;
16
+ outputFormat?:
17
+ | 'wav'
18
+ | 'pcm'
19
+ | 'opus'
20
+ | 'ulaw_8000'
21
+ | 'alaw_8000'
22
+ | 'pcm_16000'
23
+ | 'pcm_24000';
24
+ modelId?: string;
25
+ defaultVoiceUrl?: string;
26
+ defaultExaggeration?: number;
27
+ defaultTemperature?: number;
28
+ defaultCfg?: number;
29
+ pollIntervalMs?: number;
30
+ }
31
+
32
+ export interface VoiceProviderFactoryInput {
33
+ integrationKey: VoiceIntegrationKey;
34
+ secrets: VoiceProviderSecrets;
35
+ config?: VoiceProviderConfig;
36
+ }
37
+
38
+ export function createVoiceProvider(
39
+ input: VoiceProviderFactoryInput
40
+ ): VoiceProvider {
41
+ const { integrationKey, secrets, config } = input;
42
+
43
+ if (!secrets.apiKey) {
44
+ throw new Error('Voice provider apiKey is required.');
45
+ }
46
+
47
+ switch (integrationKey) {
48
+ case 'ai-voice.gradium':
49
+ return new GradiumVoiceProvider({
50
+ apiKey: secrets.apiKey,
51
+ defaultVoiceId: config?.defaultVoiceId,
52
+ region: config?.region,
53
+ baseUrl: config?.baseUrl,
54
+ timeoutMs: config?.timeoutMs,
55
+ outputFormat: config?.outputFormat,
56
+ });
57
+ case 'ai-voice.fal':
58
+ return new FalVoiceProvider({
59
+ apiKey: secrets.apiKey,
60
+ modelId: config?.modelId,
61
+ defaultVoiceUrl: config?.defaultVoiceUrl,
62
+ defaultExaggeration: config?.defaultExaggeration,
63
+ defaultTemperature: config?.defaultTemperature,
64
+ defaultCfg: config?.defaultCfg,
65
+ pollIntervalMs: config?.pollIntervalMs,
66
+ });
67
+ default:
68
+ throw new Error(`Unsupported voice provider: ${integrationKey}`);
69
+ }
70
+ }
@@ -0,0 +1,13 @@
1
+ import type { Voice } from '@contractspec/lib.contracts/integrations/providers/voice';
2
+
3
+ import {
4
+ createVoiceProvider,
5
+ type VoiceProviderFactoryInput,
6
+ } from './create-provider';
7
+
8
+ export async function listVoices(
9
+ input: VoiceProviderFactoryInput
10
+ ): Promise<Voice[]> {
11
+ const provider = createVoiceProvider(input);
12
+ return provider.listVoices();
13
+ }
@@ -0,0 +1,20 @@
1
+ import type {
2
+ VoiceSynthesisInput,
3
+ VoiceSynthesisResult,
4
+ } from '@contractspec/lib.contracts/integrations/providers/voice';
5
+
6
+ import {
7
+ createVoiceProvider,
8
+ type VoiceProviderFactoryInput,
9
+ } from './create-provider';
10
+
11
+ export interface SynthesizeVoiceInput extends VoiceProviderFactoryInput {
12
+ synthesis: VoiceSynthesisInput;
13
+ }
14
+
15
+ export async function synthesizeVoice(
16
+ input: SynthesizeVoiceInput
17
+ ): Promise<VoiceSynthesisResult> {
18
+ const provider = createVoiceProvider(input);
19
+ return provider.synthesize(input.synthesis);
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from './handlers/create-provider';
2
+ export * from './handlers/list-voices';
3
+ export * from './handlers/synthesize';
4
+ export * from './connection.sample';
5
+ export { default as example } from './example';
6
+ import './docs';
package/src/run.ts ADDED
@@ -0,0 +1,180 @@
1
+ import { listVoices } from './handlers/list-voices';
2
+ import { synthesizeVoice } from './handlers/synthesize';
3
+ import type {
4
+ VoiceIntegrationKey,
5
+ VoiceProviderConfig,
6
+ VoiceProviderFactoryInput,
7
+ } from './handlers/create-provider';
8
+
9
+ type VoiceMode = 'list' | 'synthesize' | 'both';
10
+
11
+ export async function runVoiceProvidersExampleFromEnv() {
12
+ const integrationKey = resolveIntegrationKey();
13
+ const mode = resolveMode();
14
+ const dryRun = process.env.CONTRACTSPEC_VOICE_DRY_RUN === 'true';
15
+ const config = resolveConfig(integrationKey);
16
+ const text =
17
+ process.env.CONTRACTSPEC_VOICE_TEXT ??
18
+ 'Hello from ContractSpec voice providers example.';
19
+ const voiceId = process.env.CONTRACTSPEC_VOICE_ID;
20
+
21
+ if (dryRun) {
22
+ return {
23
+ integrationKey,
24
+ mode,
25
+ dryRun,
26
+ text,
27
+ voiceId,
28
+ config,
29
+ };
30
+ }
31
+
32
+ const input: VoiceProviderFactoryInput = {
33
+ integrationKey,
34
+ secrets: {
35
+ apiKey: resolveApiKey(integrationKey),
36
+ },
37
+ config,
38
+ };
39
+
40
+ const output: Record<string, unknown> = {
41
+ integrationKey,
42
+ mode,
43
+ dryRun,
44
+ };
45
+
46
+ if (mode === 'list' || mode === 'both') {
47
+ const voices = await listVoices(input);
48
+ output.voices = voices;
49
+ }
50
+
51
+ if (mode === 'synthesize' || mode === 'both') {
52
+ const result = await synthesizeVoice({
53
+ ...input,
54
+ synthesis: {
55
+ text,
56
+ voiceId,
57
+ },
58
+ });
59
+ output.synthesis = {
60
+ format: result.format,
61
+ sampleRateHz: result.sampleRateHz,
62
+ bytes: result.audio.length,
63
+ url: result.url,
64
+ };
65
+ }
66
+
67
+ return output;
68
+ }
69
+
70
+ function resolveMode(): VoiceMode {
71
+ const raw = (process.env.CONTRACTSPEC_VOICE_MODE ?? 'both').toLowerCase();
72
+ if (raw === 'list' || raw === 'synthesize' || raw === 'both') {
73
+ return raw;
74
+ }
75
+ throw new Error(
76
+ `Unsupported CONTRACTSPEC_VOICE_MODE: ${raw}. Use list, synthesize, or both.`
77
+ );
78
+ }
79
+
80
+ function resolveIntegrationKey(): VoiceIntegrationKey {
81
+ const raw = (
82
+ process.env.CONTRACTSPEC_VOICE_PROVIDER ?? 'gradium'
83
+ ).toLowerCase();
84
+ if (raw === 'gradium') return 'ai-voice.gradium';
85
+ if (raw === 'fal') return 'ai-voice.fal';
86
+ throw new Error(
87
+ `Unsupported CONTRACTSPEC_VOICE_PROVIDER: ${raw}. Use gradium or fal.`
88
+ );
89
+ }
90
+
91
+ function resolveApiKey(integrationKey: VoiceIntegrationKey): string {
92
+ const shared = process.env.CONTRACTSPEC_VOICE_API_KEY;
93
+ if (shared) return shared;
94
+
95
+ const specific =
96
+ integrationKey === 'ai-voice.gradium'
97
+ ? process.env.GRADIUM_API_KEY
98
+ : process.env.FAL_KEY;
99
+
100
+ if (!specific) {
101
+ const envName =
102
+ integrationKey === 'ai-voice.gradium' ? 'GRADIUM_API_KEY' : 'FAL_KEY';
103
+ throw new Error(
104
+ `Missing API key. Set CONTRACTSPEC_VOICE_API_KEY or ${envName}.`
105
+ );
106
+ }
107
+
108
+ return specific;
109
+ }
110
+
111
+ function resolveConfig(
112
+ integrationKey: VoiceIntegrationKey
113
+ ): VoiceProviderConfig {
114
+ if (integrationKey === 'ai-voice.gradium') {
115
+ const config: VoiceProviderConfig = {
116
+ defaultVoiceId: process.env.GRADIUM_DEFAULT_VOICE_ID,
117
+ region:
118
+ process.env.GRADIUM_REGION === 'eu' ||
119
+ process.env.GRADIUM_REGION === 'us'
120
+ ? process.env.GRADIUM_REGION
121
+ : undefined,
122
+ baseUrl: process.env.GRADIUM_BASE_URL,
123
+ timeoutMs: parseOptionalInt(process.env.GRADIUM_TIMEOUT_MS),
124
+ outputFormat: parseGradiumOutputFormat(process.env.GRADIUM_OUTPUT_FORMAT),
125
+ };
126
+ return config;
127
+ }
128
+
129
+ return {
130
+ modelId: process.env.FAL_MODEL_ID,
131
+ defaultVoiceUrl: process.env.FAL_DEFAULT_VOICE_URL,
132
+ defaultExaggeration: parseOptionalNumber(
133
+ process.env.FAL_DEFAULT_EXAGGERATION
134
+ ),
135
+ defaultTemperature: parseOptionalNumber(
136
+ process.env.FAL_DEFAULT_TEMPERATURE
137
+ ),
138
+ defaultCfg: parseOptionalNumber(process.env.FAL_DEFAULT_CFG),
139
+ pollIntervalMs: parseOptionalInt(process.env.FAL_POLL_INTERVAL_MS),
140
+ };
141
+ }
142
+
143
+ function parseOptionalInt(value: string | undefined): number | undefined {
144
+ if (!value) return undefined;
145
+ const parsed = Number.parseInt(value, 10);
146
+ return Number.isFinite(parsed) ? parsed : undefined;
147
+ }
148
+
149
+ function parseOptionalNumber(value: string | undefined): number | undefined {
150
+ if (!value) return undefined;
151
+ const parsed = Number.parseFloat(value);
152
+ return Number.isFinite(parsed) ? parsed : undefined;
153
+ }
154
+
155
+ function parseGradiumOutputFormat(
156
+ value: string | undefined
157
+ ): VoiceProviderConfig['outputFormat'] {
158
+ if (!value) return undefined;
159
+ switch (value) {
160
+ case 'wav':
161
+ case 'pcm':
162
+ case 'opus':
163
+ case 'ulaw_8000':
164
+ case 'alaw_8000':
165
+ case 'pcm_16000':
166
+ case 'pcm_24000':
167
+ return value;
168
+ default:
169
+ return undefined;
170
+ }
171
+ }
172
+
173
+ runVoiceProvidersExampleFromEnv()
174
+ .then((result) => {
175
+ console.log(JSON.stringify(result, null, 2));
176
+ })
177
+ .catch((error) => {
178
+ console.error(error);
179
+ process.exitCode = 1;
180
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@contractspec/tool.typescript/react-library.json",
3
+ "include": ["src"],
4
+ "exclude": ["node_modules", "dist"],
5
+ "compilerOptions": {
6
+ "rootDir": "src",
7
+ "outDir": "dist"
8
+ }
9
+ }