@contractspec/example.voice-providers 1.57.0 → 1.59.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +43 -46
- package/.turbo/turbo-prebuild.log +1 -0
- package/CHANGELOG.md +25 -0
- package/dist/browser/connection.sample.js +55 -0
- package/dist/browser/docs/index.js +36 -0
- package/dist/browser/docs/voice-providers.docblock.js +36 -0
- package/dist/browser/example.js +33 -0
- package/dist/browser/handlers/create-provider.js +35 -0
- package/dist/browser/handlers/list-voices.js +41 -0
- package/dist/browser/handlers/synthesize.js +41 -0
- package/dist/browser/index.js +171 -0
- package/dist/browser/run.js +179 -0
- package/dist/connection.sample.d.ts +4 -8
- package/dist/connection.sample.d.ts.map +1 -1
- package/dist/connection.sample.js +54 -49
- package/dist/docs/index.d.ts +2 -1
- package/dist/docs/index.d.ts.map +1 -0
- package/dist/docs/index.js +37 -1
- package/dist/docs/voice-providers.docblock.d.ts +2 -1
- package/dist/docs/voice-providers.docblock.d.ts.map +1 -0
- package/dist/docs/voice-providers.docblock.js +35 -29
- package/dist/example.d.ts +2 -6
- package/dist/example.d.ts.map +1 -1
- package/dist/example.js +32 -44
- package/dist/handlers/create-provider.d.ts +21 -25
- package/dist/handlers/create-provider.d.ts.map +1 -1
- package/dist/handlers/create-provider.js +32 -28
- package/dist/handlers/list-voices.d.ts +3 -7
- package/dist/handlers/list-voices.d.ts.map +1 -1
- package/dist/handlers/list-voices.js +39 -7
- package/dist/handlers/synthesize.d.ts +5 -9
- package/dist/handlers/synthesize.d.ts.map +1 -1
- package/dist/handlers/synthesize.js +39 -7
- package/dist/index.d.ts +7 -6
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +172 -8
- package/dist/node/connection.sample.js +55 -0
- package/dist/node/docs/index.js +36 -0
- package/dist/node/docs/voice-providers.docblock.js +36 -0
- package/dist/node/example.js +33 -0
- package/dist/node/handlers/create-provider.js +35 -0
- package/dist/node/handlers/list-voices.js +41 -0
- package/dist/node/handlers/synthesize.js +41 -0
- package/dist/node/index.js +171 -0
- package/dist/node/run.js +179 -0
- package/dist/run.d.ts +1 -4
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +160 -98
- package/package.json +93 -32
- package/tsdown.config.js +1 -2
- package/.turbo/turbo-build$colon$bundle.log +0 -46
- package/dist/connection.sample.js.map +0 -1
- package/dist/docs/voice-providers.docblock.js.map +0 -1
- package/dist/example.js.map +0 -1
- package/dist/handlers/create-provider.js.map +0 -1
- package/dist/handlers/list-voices.js.map +0 -1
- package/dist/handlers/synthesize.js.map +0 -1
- package/dist/run.js.map +0 -1
- package/tsconfig.tsbuildinfo +0 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/handlers/create-provider.ts
|
|
2
|
+
import { FalVoiceProvider } from "@contractspec/integration.providers-impls/impls/fal-voice";
|
|
3
|
+
import { GradiumVoiceProvider } from "@contractspec/integration.providers-impls/impls/gradium-voice";
|
|
4
|
+
function createVoiceProvider(input) {
|
|
5
|
+
const { integrationKey, secrets, config } = input;
|
|
6
|
+
if (!secrets.apiKey) {
|
|
7
|
+
throw new Error("Voice provider apiKey is required.");
|
|
8
|
+
}
|
|
9
|
+
switch (integrationKey) {
|
|
10
|
+
case "ai-voice.gradium":
|
|
11
|
+
return new GradiumVoiceProvider({
|
|
12
|
+
apiKey: secrets.apiKey,
|
|
13
|
+
defaultVoiceId: config?.defaultVoiceId,
|
|
14
|
+
region: config?.region,
|
|
15
|
+
baseUrl: config?.baseUrl,
|
|
16
|
+
timeoutMs: config?.timeoutMs,
|
|
17
|
+
outputFormat: config?.outputFormat
|
|
18
|
+
});
|
|
19
|
+
case "ai-voice.fal":
|
|
20
|
+
return new FalVoiceProvider({
|
|
21
|
+
apiKey: secrets.apiKey,
|
|
22
|
+
modelId: config?.modelId,
|
|
23
|
+
defaultVoiceUrl: config?.defaultVoiceUrl,
|
|
24
|
+
defaultExaggeration: config?.defaultExaggeration,
|
|
25
|
+
defaultTemperature: config?.defaultTemperature,
|
|
26
|
+
defaultCfg: config?.defaultCfg,
|
|
27
|
+
pollIntervalMs: config?.pollIntervalMs
|
|
28
|
+
});
|
|
29
|
+
default:
|
|
30
|
+
throw new Error(`Unsupported voice provider: ${integrationKey}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/handlers/synthesize.ts
|
|
35
|
+
async function synthesizeVoice(input) {
|
|
36
|
+
const provider = createVoiceProvider(input);
|
|
37
|
+
return provider.synthesize(input.synthesis);
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
synthesizeVoice
|
|
41
|
+
};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// src/connection.sample.ts
|
|
2
|
+
var gradiumVoiceConnection = {
|
|
3
|
+
meta: {
|
|
4
|
+
id: "conn-gradium-voice-demo",
|
|
5
|
+
tenantId: "acme-inc",
|
|
6
|
+
integrationKey: "ai-voice.gradium",
|
|
7
|
+
integrationVersion: "1.0.0",
|
|
8
|
+
label: "Gradium Voice",
|
|
9
|
+
environment: "production",
|
|
10
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
11
|
+
updatedAt: "2026-01-01T00:00:00.000Z"
|
|
12
|
+
},
|
|
13
|
+
ownershipMode: "byok",
|
|
14
|
+
config: {
|
|
15
|
+
defaultVoiceId: "YTpq7expH9539ERJ",
|
|
16
|
+
region: "eu",
|
|
17
|
+
outputFormat: "wav"
|
|
18
|
+
},
|
|
19
|
+
secretProvider: "vault",
|
|
20
|
+
secretRef: "vault://integrations/acme-inc/conn-gradium-voice-demo",
|
|
21
|
+
status: "connected"
|
|
22
|
+
};
|
|
23
|
+
var falVoiceConnection = {
|
|
24
|
+
meta: {
|
|
25
|
+
id: "conn-fal-voice-demo",
|
|
26
|
+
tenantId: "acme-inc",
|
|
27
|
+
integrationKey: "ai-voice.fal",
|
|
28
|
+
integrationVersion: "1.0.0",
|
|
29
|
+
label: "Fal Voice",
|
|
30
|
+
environment: "production",
|
|
31
|
+
createdAt: "2026-01-01T00:00:00.000Z",
|
|
32
|
+
updatedAt: "2026-01-01T00:00:00.000Z"
|
|
33
|
+
},
|
|
34
|
+
ownershipMode: "byok",
|
|
35
|
+
config: {
|
|
36
|
+
modelId: "fal-ai/chatterbox/text-to-speech",
|
|
37
|
+
defaultVoiceUrl: "https://storage.googleapis.com/chatterbox-demo-samples/prompts/male_rickmorty.mp3",
|
|
38
|
+
defaultExaggeration: 0.25,
|
|
39
|
+
defaultTemperature: 0.7,
|
|
40
|
+
defaultCfg: 0.5,
|
|
41
|
+
pollIntervalMs: 1000
|
|
42
|
+
},
|
|
43
|
+
secretProvider: "vault",
|
|
44
|
+
secretRef: "vault://integrations/acme-inc/conn-fal-voice-demo",
|
|
45
|
+
status: "connected"
|
|
46
|
+
};
|
|
47
|
+
var voiceSampleConnections = [
|
|
48
|
+
gradiumVoiceConnection,
|
|
49
|
+
falVoiceConnection
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
// src/docs/voice-providers.docblock.ts
|
|
53
|
+
import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
|
|
54
|
+
var blocks = [
|
|
55
|
+
{
|
|
56
|
+
id: "docs.examples.voice-providers",
|
|
57
|
+
title: "Voice Providers (example)",
|
|
58
|
+
summary: "Multi-provider voice integration example covering Gradium and Fal text-to-speech flows.",
|
|
59
|
+
kind: "reference",
|
|
60
|
+
visibility: "public",
|
|
61
|
+
route: "/docs/examples/voice-providers",
|
|
62
|
+
tags: ["voice", "tts", "gradium", "fal", "example"],
|
|
63
|
+
body: `## What this example shows
|
|
64
|
+
` + "- Provider selection for `ai-voice.gradium` and `ai-voice.fal`.\n" + `- Listing voice catalogs and synthesizing text into audio bytes.
|
|
65
|
+
` + `- Connection metadata patterns for BYOK secret references.
|
|
66
|
+
|
|
67
|
+
` + `## Secrets and config
|
|
68
|
+
` + "- `apiKey` for each provider.\n" + "- Gradium config: `defaultVoiceId`, `region`, `outputFormat`.\n" + "- Fal config: `modelId`, `defaultVoiceUrl`, synthesis tuning fields.\n\n" + `## Guardrails
|
|
69
|
+
` + `- Keep API keys in secret providers only.
|
|
70
|
+
` + `- Prefer declarative provider config over hardcoded runtime options.
|
|
71
|
+
` + "- Keep synthesis side effects explicit for deterministic workflows."
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
id: "docs.examples.voice-providers.usage",
|
|
75
|
+
title: "Voice Providers - Usage",
|
|
76
|
+
summary: "How to wire provider factory and synthesis helpers in runtime code.",
|
|
77
|
+
kind: "usage",
|
|
78
|
+
visibility: "public",
|
|
79
|
+
route: "/docs/examples/voice-providers/usage",
|
|
80
|
+
tags: ["voice", "usage"],
|
|
81
|
+
body: `## Usage
|
|
82
|
+
` + "- Call `createVoiceProvider` with integration key, secrets, and config.\n" + "- Use `listVoices` to expose voice choices in admin/config screens.\n" + "- Use `synthesizeVoice` for message generation or workflow steps.\n\n" + `## Notes
|
|
83
|
+
` + `- Fal uses an audio URL output; this example downloads bytes for a canonical result shape.
|
|
84
|
+
` + "- Gradium maps provider output formats into ContractSpec voice result conventions."
|
|
85
|
+
}
|
|
86
|
+
];
|
|
87
|
+
registerDocBlocks(blocks);
|
|
88
|
+
// src/example.ts
|
|
89
|
+
import { defineExample } from "@contractspec/lib.contracts";
|
|
90
|
+
var example = defineExample({
|
|
91
|
+
meta: {
|
|
92
|
+
key: "voice-providers",
|
|
93
|
+
version: "1.0.0",
|
|
94
|
+
title: "Voice Providers (Gradium and Fal)",
|
|
95
|
+
description: "Multi-provider voice integration example for Gradium and Fal text-to-speech adapters.",
|
|
96
|
+
kind: "integration",
|
|
97
|
+
visibility: "public",
|
|
98
|
+
stability: "experimental",
|
|
99
|
+
owners: ["@platform.integrations"],
|
|
100
|
+
tags: ["voice", "tts", "gradium", "fal", "integrations"]
|
|
101
|
+
},
|
|
102
|
+
docs: {
|
|
103
|
+
rootDocId: "docs.examples.voice-providers",
|
|
104
|
+
usageDocId: "docs.examples.voice-providers.usage"
|
|
105
|
+
},
|
|
106
|
+
entrypoints: {
|
|
107
|
+
packageName: "@contractspec/example.voice-providers",
|
|
108
|
+
docs: "./docs"
|
|
109
|
+
},
|
|
110
|
+
surfaces: {
|
|
111
|
+
templates: true,
|
|
112
|
+
sandbox: { enabled: true, modes: ["markdown", "specs"] },
|
|
113
|
+
studio: { enabled: true, installable: true },
|
|
114
|
+
mcp: { enabled: true }
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
var example_default = example;
|
|
118
|
+
|
|
119
|
+
// src/handlers/create-provider.ts
|
|
120
|
+
import { FalVoiceProvider } from "@contractspec/integration.providers-impls/impls/fal-voice";
|
|
121
|
+
import { GradiumVoiceProvider } from "@contractspec/integration.providers-impls/impls/gradium-voice";
|
|
122
|
+
function createVoiceProvider(input) {
|
|
123
|
+
const { integrationKey, secrets, config } = input;
|
|
124
|
+
if (!secrets.apiKey) {
|
|
125
|
+
throw new Error("Voice provider apiKey is required.");
|
|
126
|
+
}
|
|
127
|
+
switch (integrationKey) {
|
|
128
|
+
case "ai-voice.gradium":
|
|
129
|
+
return new GradiumVoiceProvider({
|
|
130
|
+
apiKey: secrets.apiKey,
|
|
131
|
+
defaultVoiceId: config?.defaultVoiceId,
|
|
132
|
+
region: config?.region,
|
|
133
|
+
baseUrl: config?.baseUrl,
|
|
134
|
+
timeoutMs: config?.timeoutMs,
|
|
135
|
+
outputFormat: config?.outputFormat
|
|
136
|
+
});
|
|
137
|
+
case "ai-voice.fal":
|
|
138
|
+
return new FalVoiceProvider({
|
|
139
|
+
apiKey: secrets.apiKey,
|
|
140
|
+
modelId: config?.modelId,
|
|
141
|
+
defaultVoiceUrl: config?.defaultVoiceUrl,
|
|
142
|
+
defaultExaggeration: config?.defaultExaggeration,
|
|
143
|
+
defaultTemperature: config?.defaultTemperature,
|
|
144
|
+
defaultCfg: config?.defaultCfg,
|
|
145
|
+
pollIntervalMs: config?.pollIntervalMs
|
|
146
|
+
});
|
|
147
|
+
default:
|
|
148
|
+
throw new Error(`Unsupported voice provider: ${integrationKey}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/handlers/list-voices.ts
|
|
153
|
+
async function listVoices(input) {
|
|
154
|
+
const provider = createVoiceProvider(input);
|
|
155
|
+
return provider.listVoices();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/handlers/synthesize.ts
|
|
159
|
+
async function synthesizeVoice(input) {
|
|
160
|
+
const provider = createVoiceProvider(input);
|
|
161
|
+
return provider.synthesize(input.synthesis);
|
|
162
|
+
}
|
|
163
|
+
export {
|
|
164
|
+
voiceSampleConnections,
|
|
165
|
+
synthesizeVoice,
|
|
166
|
+
listVoices,
|
|
167
|
+
gradiumVoiceConnection,
|
|
168
|
+
falVoiceConnection,
|
|
169
|
+
example_default as example,
|
|
170
|
+
createVoiceProvider
|
|
171
|
+
};
|
package/dist/node/run.js
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// src/handlers/create-provider.ts
|
|
2
|
+
import { FalVoiceProvider } from "@contractspec/integration.providers-impls/impls/fal-voice";
|
|
3
|
+
import { GradiumVoiceProvider } from "@contractspec/integration.providers-impls/impls/gradium-voice";
|
|
4
|
+
function createVoiceProvider(input) {
|
|
5
|
+
const { integrationKey, secrets, config } = input;
|
|
6
|
+
if (!secrets.apiKey) {
|
|
7
|
+
throw new Error("Voice provider apiKey is required.");
|
|
8
|
+
}
|
|
9
|
+
switch (integrationKey) {
|
|
10
|
+
case "ai-voice.gradium":
|
|
11
|
+
return new GradiumVoiceProvider({
|
|
12
|
+
apiKey: secrets.apiKey,
|
|
13
|
+
defaultVoiceId: config?.defaultVoiceId,
|
|
14
|
+
region: config?.region,
|
|
15
|
+
baseUrl: config?.baseUrl,
|
|
16
|
+
timeoutMs: config?.timeoutMs,
|
|
17
|
+
outputFormat: config?.outputFormat
|
|
18
|
+
});
|
|
19
|
+
case "ai-voice.fal":
|
|
20
|
+
return new FalVoiceProvider({
|
|
21
|
+
apiKey: secrets.apiKey,
|
|
22
|
+
modelId: config?.modelId,
|
|
23
|
+
defaultVoiceUrl: config?.defaultVoiceUrl,
|
|
24
|
+
defaultExaggeration: config?.defaultExaggeration,
|
|
25
|
+
defaultTemperature: config?.defaultTemperature,
|
|
26
|
+
defaultCfg: config?.defaultCfg,
|
|
27
|
+
pollIntervalMs: config?.pollIntervalMs
|
|
28
|
+
});
|
|
29
|
+
default:
|
|
30
|
+
throw new Error(`Unsupported voice provider: ${integrationKey}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/handlers/list-voices.ts
|
|
35
|
+
async function listVoices(input) {
|
|
36
|
+
const provider = createVoiceProvider(input);
|
|
37
|
+
return provider.listVoices();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/handlers/synthesize.ts
|
|
41
|
+
async function synthesizeVoice(input) {
|
|
42
|
+
const provider = createVoiceProvider(input);
|
|
43
|
+
return provider.synthesize(input.synthesis);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/run.ts
|
|
47
|
+
async function runVoiceProvidersExampleFromEnv() {
|
|
48
|
+
const integrationKey = resolveIntegrationKey();
|
|
49
|
+
const mode = resolveMode();
|
|
50
|
+
const dryRun = process.env.CONTRACTSPEC_VOICE_DRY_RUN === "true";
|
|
51
|
+
const config = resolveConfig(integrationKey);
|
|
52
|
+
const text = process.env.CONTRACTSPEC_VOICE_TEXT ?? "Hello from ContractSpec voice providers example.";
|
|
53
|
+
const voiceId = process.env.CONTRACTSPEC_VOICE_ID;
|
|
54
|
+
if (dryRun) {
|
|
55
|
+
return {
|
|
56
|
+
integrationKey,
|
|
57
|
+
mode,
|
|
58
|
+
dryRun,
|
|
59
|
+
text,
|
|
60
|
+
voiceId,
|
|
61
|
+
config
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
const input = {
|
|
65
|
+
integrationKey,
|
|
66
|
+
secrets: {
|
|
67
|
+
apiKey: resolveApiKey(integrationKey)
|
|
68
|
+
},
|
|
69
|
+
config
|
|
70
|
+
};
|
|
71
|
+
const output = {
|
|
72
|
+
integrationKey,
|
|
73
|
+
mode,
|
|
74
|
+
dryRun
|
|
75
|
+
};
|
|
76
|
+
if (mode === "list" || mode === "both") {
|
|
77
|
+
const voices = await listVoices(input);
|
|
78
|
+
output.voices = voices;
|
|
79
|
+
}
|
|
80
|
+
if (mode === "synthesize" || mode === "both") {
|
|
81
|
+
const result = await synthesizeVoice({
|
|
82
|
+
...input,
|
|
83
|
+
synthesis: {
|
|
84
|
+
text,
|
|
85
|
+
voiceId
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
output.synthesis = {
|
|
89
|
+
format: result.format,
|
|
90
|
+
sampleRateHz: result.sampleRateHz,
|
|
91
|
+
bytes: result.audio.length,
|
|
92
|
+
url: result.url
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
return output;
|
|
96
|
+
}
|
|
97
|
+
function resolveMode() {
|
|
98
|
+
const raw = (process.env.CONTRACTSPEC_VOICE_MODE ?? "both").toLowerCase();
|
|
99
|
+
if (raw === "list" || raw === "synthesize" || raw === "both") {
|
|
100
|
+
return raw;
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`Unsupported CONTRACTSPEC_VOICE_MODE: ${raw}. Use list, synthesize, or both.`);
|
|
103
|
+
}
|
|
104
|
+
function resolveIntegrationKey() {
|
|
105
|
+
const raw = (process.env.CONTRACTSPEC_VOICE_PROVIDER ?? "gradium").toLowerCase();
|
|
106
|
+
if (raw === "gradium")
|
|
107
|
+
return "ai-voice.gradium";
|
|
108
|
+
if (raw === "fal")
|
|
109
|
+
return "ai-voice.fal";
|
|
110
|
+
throw new Error(`Unsupported CONTRACTSPEC_VOICE_PROVIDER: ${raw}. Use gradium or fal.`);
|
|
111
|
+
}
|
|
112
|
+
function resolveApiKey(integrationKey) {
|
|
113
|
+
const shared = process.env.CONTRACTSPEC_VOICE_API_KEY;
|
|
114
|
+
if (shared)
|
|
115
|
+
return shared;
|
|
116
|
+
const specific = integrationKey === "ai-voice.gradium" ? process.env.GRADIUM_API_KEY : process.env.FAL_KEY;
|
|
117
|
+
if (!specific) {
|
|
118
|
+
const envName = integrationKey === "ai-voice.gradium" ? "GRADIUM_API_KEY" : "FAL_KEY";
|
|
119
|
+
throw new Error(`Missing API key. Set CONTRACTSPEC_VOICE_API_KEY or ${envName}.`);
|
|
120
|
+
}
|
|
121
|
+
return specific;
|
|
122
|
+
}
|
|
123
|
+
function resolveConfig(integrationKey) {
|
|
124
|
+
if (integrationKey === "ai-voice.gradium") {
|
|
125
|
+
const config = {
|
|
126
|
+
defaultVoiceId: process.env.GRADIUM_DEFAULT_VOICE_ID,
|
|
127
|
+
region: process.env.GRADIUM_REGION === "eu" || process.env.GRADIUM_REGION === "us" ? process.env.GRADIUM_REGION : undefined,
|
|
128
|
+
baseUrl: process.env.GRADIUM_BASE_URL,
|
|
129
|
+
timeoutMs: parseOptionalInt(process.env.GRADIUM_TIMEOUT_MS),
|
|
130
|
+
outputFormat: parseGradiumOutputFormat(process.env.GRADIUM_OUTPUT_FORMAT)
|
|
131
|
+
};
|
|
132
|
+
return config;
|
|
133
|
+
}
|
|
134
|
+
return {
|
|
135
|
+
modelId: process.env.FAL_MODEL_ID,
|
|
136
|
+
defaultVoiceUrl: process.env.FAL_DEFAULT_VOICE_URL,
|
|
137
|
+
defaultExaggeration: parseOptionalNumber(process.env.FAL_DEFAULT_EXAGGERATION),
|
|
138
|
+
defaultTemperature: parseOptionalNumber(process.env.FAL_DEFAULT_TEMPERATURE),
|
|
139
|
+
defaultCfg: parseOptionalNumber(process.env.FAL_DEFAULT_CFG),
|
|
140
|
+
pollIntervalMs: parseOptionalInt(process.env.FAL_POLL_INTERVAL_MS)
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function parseOptionalInt(value) {
|
|
144
|
+
if (!value)
|
|
145
|
+
return;
|
|
146
|
+
const parsed = Number.parseInt(value, 10);
|
|
147
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
148
|
+
}
|
|
149
|
+
function parseOptionalNumber(value) {
|
|
150
|
+
if (!value)
|
|
151
|
+
return;
|
|
152
|
+
const parsed = Number.parseFloat(value);
|
|
153
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
154
|
+
}
|
|
155
|
+
function parseGradiumOutputFormat(value) {
|
|
156
|
+
if (!value)
|
|
157
|
+
return;
|
|
158
|
+
switch (value) {
|
|
159
|
+
case "wav":
|
|
160
|
+
case "pcm":
|
|
161
|
+
case "opus":
|
|
162
|
+
case "ulaw_8000":
|
|
163
|
+
case "alaw_8000":
|
|
164
|
+
case "pcm_16000":
|
|
165
|
+
case "pcm_24000":
|
|
166
|
+
return value;
|
|
167
|
+
default:
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
runVoiceProvidersExampleFromEnv().then((result) => {
|
|
172
|
+
console.log(JSON.stringify(result, null, 2));
|
|
173
|
+
}).catch((error) => {
|
|
174
|
+
console.error(error);
|
|
175
|
+
process.exitCode = 1;
|
|
176
|
+
});
|
|
177
|
+
export {
|
|
178
|
+
runVoiceProvidersExampleFromEnv
|
|
179
|
+
};
|
package/dist/run.d.ts
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
declare function runVoiceProvidersExampleFromEnv(): Promise<Record<string, unknown>>;
|
|
3
|
-
//#endregion
|
|
4
|
-
export { runVoiceProvidersExampleFromEnv };
|
|
1
|
+
export declare function runVoiceProvidersExampleFromEnv(): Promise<Record<string, unknown>>;
|
|
5
2
|
//# sourceMappingURL=run.d.ts.map
|
package/dist/run.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.ts","
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAUA,wBAAsB,+BAA+B,qCAyDpD"}
|
package/dist/run.js
CHANGED
|
@@ -1,118 +1,180 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/handlers/create-provider.ts
|
|
3
|
+
import { FalVoiceProvider } from "@contractspec/integration.providers-impls/impls/fal-voice";
|
|
4
|
+
import { GradiumVoiceProvider } from "@contractspec/integration.providers-impls/impls/gradium-voice";
|
|
5
|
+
function createVoiceProvider(input) {
|
|
6
|
+
const { integrationKey, secrets, config } = input;
|
|
7
|
+
if (!secrets.apiKey) {
|
|
8
|
+
throw new Error("Voice provider apiKey is required.");
|
|
9
|
+
}
|
|
10
|
+
switch (integrationKey) {
|
|
11
|
+
case "ai-voice.gradium":
|
|
12
|
+
return new GradiumVoiceProvider({
|
|
13
|
+
apiKey: secrets.apiKey,
|
|
14
|
+
defaultVoiceId: config?.defaultVoiceId,
|
|
15
|
+
region: config?.region,
|
|
16
|
+
baseUrl: config?.baseUrl,
|
|
17
|
+
timeoutMs: config?.timeoutMs,
|
|
18
|
+
outputFormat: config?.outputFormat
|
|
19
|
+
});
|
|
20
|
+
case "ai-voice.fal":
|
|
21
|
+
return new FalVoiceProvider({
|
|
22
|
+
apiKey: secrets.apiKey,
|
|
23
|
+
modelId: config?.modelId,
|
|
24
|
+
defaultVoiceUrl: config?.defaultVoiceUrl,
|
|
25
|
+
defaultExaggeration: config?.defaultExaggeration,
|
|
26
|
+
defaultTemperature: config?.defaultTemperature,
|
|
27
|
+
defaultCfg: config?.defaultCfg,
|
|
28
|
+
pollIntervalMs: config?.pollIntervalMs
|
|
29
|
+
});
|
|
30
|
+
default:
|
|
31
|
+
throw new Error(`Unsupported voice provider: ${integrationKey}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/handlers/list-voices.ts
|
|
36
|
+
async function listVoices(input) {
|
|
37
|
+
const provider = createVoiceProvider(input);
|
|
38
|
+
return provider.listVoices();
|
|
39
|
+
}
|
|
3
40
|
|
|
4
|
-
|
|
41
|
+
// src/handlers/synthesize.ts
|
|
42
|
+
async function synthesizeVoice(input) {
|
|
43
|
+
const provider = createVoiceProvider(input);
|
|
44
|
+
return provider.synthesize(input.synthesis);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/run.ts
|
|
5
48
|
async function runVoiceProvidersExampleFromEnv() {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
const integrationKey = resolveIntegrationKey();
|
|
50
|
+
const mode = resolveMode();
|
|
51
|
+
const dryRun = process.env.CONTRACTSPEC_VOICE_DRY_RUN === "true";
|
|
52
|
+
const config = resolveConfig(integrationKey);
|
|
53
|
+
const text = process.env.CONTRACTSPEC_VOICE_TEXT ?? "Hello from ContractSpec voice providers example.";
|
|
54
|
+
const voiceId = process.env.CONTRACTSPEC_VOICE_ID;
|
|
55
|
+
if (dryRun) {
|
|
56
|
+
return {
|
|
57
|
+
integrationKey,
|
|
58
|
+
mode,
|
|
59
|
+
dryRun,
|
|
60
|
+
text,
|
|
61
|
+
voiceId,
|
|
62
|
+
config
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const input = {
|
|
66
|
+
integrationKey,
|
|
67
|
+
secrets: {
|
|
68
|
+
apiKey: resolveApiKey(integrationKey)
|
|
69
|
+
},
|
|
70
|
+
config
|
|
71
|
+
};
|
|
72
|
+
const output = {
|
|
73
|
+
integrationKey,
|
|
74
|
+
mode,
|
|
75
|
+
dryRun
|
|
76
|
+
};
|
|
77
|
+
if (mode === "list" || mode === "both") {
|
|
78
|
+
const voices = await listVoices(input);
|
|
79
|
+
output.voices = voices;
|
|
80
|
+
}
|
|
81
|
+
if (mode === "synthesize" || mode === "both") {
|
|
82
|
+
const result = await synthesizeVoice({
|
|
83
|
+
...input,
|
|
84
|
+
synthesis: {
|
|
85
|
+
text,
|
|
86
|
+
voiceId
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
output.synthesis = {
|
|
90
|
+
format: result.format,
|
|
91
|
+
sampleRateHz: result.sampleRateHz,
|
|
92
|
+
bytes: result.audio.length,
|
|
93
|
+
url: result.url
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return output;
|
|
47
97
|
}
|
|
48
98
|
function resolveMode() {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
99
|
+
const raw = (process.env.CONTRACTSPEC_VOICE_MODE ?? "both").toLowerCase();
|
|
100
|
+
if (raw === "list" || raw === "synthesize" || raw === "both") {
|
|
101
|
+
return raw;
|
|
102
|
+
}
|
|
103
|
+
throw new Error(`Unsupported CONTRACTSPEC_VOICE_MODE: ${raw}. Use list, synthesize, or both.`);
|
|
52
104
|
}
|
|
53
105
|
function resolveIntegrationKey() {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
106
|
+
const raw = (process.env.CONTRACTSPEC_VOICE_PROVIDER ?? "gradium").toLowerCase();
|
|
107
|
+
if (raw === "gradium")
|
|
108
|
+
return "ai-voice.gradium";
|
|
109
|
+
if (raw === "fal")
|
|
110
|
+
return "ai-voice.fal";
|
|
111
|
+
throw new Error(`Unsupported CONTRACTSPEC_VOICE_PROVIDER: ${raw}. Use gradium or fal.`);
|
|
58
112
|
}
|
|
59
113
|
function resolveApiKey(integrationKey) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
114
|
+
const shared = process.env.CONTRACTSPEC_VOICE_API_KEY;
|
|
115
|
+
if (shared)
|
|
116
|
+
return shared;
|
|
117
|
+
const specific = integrationKey === "ai-voice.gradium" ? process.env.GRADIUM_API_KEY : process.env.FAL_KEY;
|
|
118
|
+
if (!specific) {
|
|
119
|
+
const envName = integrationKey === "ai-voice.gradium" ? "GRADIUM_API_KEY" : "FAL_KEY";
|
|
120
|
+
throw new Error(`Missing API key. Set CONTRACTSPEC_VOICE_API_KEY or ${envName}.`);
|
|
121
|
+
}
|
|
122
|
+
return specific;
|
|
68
123
|
}
|
|
69
124
|
function resolveConfig(integrationKey) {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
125
|
+
if (integrationKey === "ai-voice.gradium") {
|
|
126
|
+
const config = {
|
|
127
|
+
defaultVoiceId: process.env.GRADIUM_DEFAULT_VOICE_ID,
|
|
128
|
+
region: process.env.GRADIUM_REGION === "eu" || process.env.GRADIUM_REGION === "us" ? process.env.GRADIUM_REGION : undefined,
|
|
129
|
+
baseUrl: process.env.GRADIUM_BASE_URL,
|
|
130
|
+
timeoutMs: parseOptionalInt(process.env.GRADIUM_TIMEOUT_MS),
|
|
131
|
+
outputFormat: parseGradiumOutputFormat(process.env.GRADIUM_OUTPUT_FORMAT)
|
|
132
|
+
};
|
|
133
|
+
return config;
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
modelId: process.env.FAL_MODEL_ID,
|
|
137
|
+
defaultVoiceUrl: process.env.FAL_DEFAULT_VOICE_URL,
|
|
138
|
+
defaultExaggeration: parseOptionalNumber(process.env.FAL_DEFAULT_EXAGGERATION),
|
|
139
|
+
defaultTemperature: parseOptionalNumber(process.env.FAL_DEFAULT_TEMPERATURE),
|
|
140
|
+
defaultCfg: parseOptionalNumber(process.env.FAL_DEFAULT_CFG),
|
|
141
|
+
pollIntervalMs: parseOptionalInt(process.env.FAL_POLL_INTERVAL_MS)
|
|
142
|
+
};
|
|
85
143
|
}
|
|
86
144
|
function parseOptionalInt(value) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
145
|
+
if (!value)
|
|
146
|
+
return;
|
|
147
|
+
const parsed = Number.parseInt(value, 10);
|
|
148
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
90
149
|
}
|
|
91
150
|
function parseOptionalNumber(value) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
151
|
+
if (!value)
|
|
152
|
+
return;
|
|
153
|
+
const parsed = Number.parseFloat(value);
|
|
154
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
95
155
|
}
|
|
96
156
|
function parseGradiumOutputFormat(value) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
157
|
+
if (!value)
|
|
158
|
+
return;
|
|
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;
|
|
170
|
+
}
|
|
108
171
|
}
|
|
109
172
|
runVoiceProvidersExampleFromEnv().then((result) => {
|
|
110
|
-
|
|
173
|
+
console.log(JSON.stringify(result, null, 2));
|
|
111
174
|
}).catch((error) => {
|
|
112
|
-
|
|
113
|
-
|
|
175
|
+
console.error(error);
|
|
176
|
+
process.exitCode = 1;
|
|
114
177
|
});
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
//# sourceMappingURL=run.js.map
|
|
178
|
+
export {
|
|
179
|
+
runVoiceProvidersExampleFromEnv
|
|
180
|
+
};
|