@mastra/voice-sarvam 0.1.0-alpha.1
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/LICENSE +44 -0
- package/README.md +105 -0
- package/dist/_tsup-dts-rollup.d.cts +51 -0
- package/dist/_tsup-dts-rollup.d.ts +51 -0
- package/dist/index.cjs +125 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +123 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
**Acceptance**
|
|
4
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
5
|
+
|
|
6
|
+
**Copyright License**
|
|
7
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below
|
|
8
|
+
|
|
9
|
+
**Limitations**
|
|
10
|
+
You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
|
|
11
|
+
|
|
12
|
+
You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
|
|
13
|
+
|
|
14
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
|
|
15
|
+
|
|
16
|
+
**Patents**
|
|
17
|
+
The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
18
|
+
|
|
19
|
+
**Notices**
|
|
20
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
|
|
21
|
+
|
|
22
|
+
If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
|
|
23
|
+
|
|
24
|
+
**No Other Rights**
|
|
25
|
+
These terms do not imply any licenses other than those expressly granted in these terms.
|
|
26
|
+
|
|
27
|
+
**Termination**
|
|
28
|
+
If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
|
|
29
|
+
|
|
30
|
+
**No Liability**
|
|
31
|
+
As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
|
|
32
|
+
|
|
33
|
+
**Definitions**
|
|
34
|
+
The _licensor_ is the entity offering these terms, and the _software_ is the software the licensor makes available under these terms, including any portion of it.
|
|
35
|
+
|
|
36
|
+
_you_ refers to the individual or entity agreeing to these terms.
|
|
37
|
+
|
|
38
|
+
_your company_ is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. _control_ means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
39
|
+
|
|
40
|
+
_your licenses_ are all the licenses granted to you for the software under these terms.
|
|
41
|
+
|
|
42
|
+
_use_ means anything you do with the software requiring one of your licenses.
|
|
43
|
+
|
|
44
|
+
_trademark_ means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# @mastra/voice-sarvam
|
|
2
|
+
|
|
3
|
+
Sarvam Voice integration for Mastra, providing Text-to-Speech (TTS) capabilities using Sarvam's voice technology.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mastra/voice-sarvam
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Configuration
|
|
12
|
+
|
|
13
|
+
The module requires the following environment variables:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
SARVAM_API_KEY=your_api_key
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { SarvamVoice } from '@mastra/voice-sarvam';
|
|
23
|
+
|
|
24
|
+
const voice = new CompositeVoice({
|
|
25
|
+
speakProvider: new SarvamVoice({
|
|
26
|
+
speechModel: { apiKey: 'YOUR-API-KEY' },
|
|
27
|
+
speaker: 'meera',
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
// Create an agent with voice capabilities
|
|
32
|
+
export const agent = new Agent({
|
|
33
|
+
name: 'Agent',
|
|
34
|
+
instructions: `You are a helpful assistant with voice capabilities.`,
|
|
35
|
+
model: google('gemini-1.5-pro-latest'),
|
|
36
|
+
voice: voice,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// List available speakers
|
|
40
|
+
const speakers = await voice.getSpeakers();
|
|
41
|
+
|
|
42
|
+
// Generate speech and save to file
|
|
43
|
+
const audio = await agent.speak("Hello, I'm your AI assistant!");
|
|
44
|
+
const filePath = path.join(process.cwd(), 'agent.mp3');
|
|
45
|
+
const writer = createWriteStream(filePath);
|
|
46
|
+
|
|
47
|
+
audio.pipe(writer);
|
|
48
|
+
|
|
49
|
+
await new Promise<void>((resolve, reject) => {
|
|
50
|
+
writer.on('finish', () => resolve());
|
|
51
|
+
writer.on('error', reject);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Generate speech from a text stream
|
|
55
|
+
const textStream = getTextStream(); // Your text stream source
|
|
56
|
+
const audioStream = await voice.speak(textStream);
|
|
57
|
+
|
|
58
|
+
// The stream can be piped to a destination
|
|
59
|
+
const streamFilePath = path.join(process.cwd(), 'stream.mp3');
|
|
60
|
+
const streamWriter = createWriteStream(streamFilePath);
|
|
61
|
+
|
|
62
|
+
audioStream.pipe(streamWriter);
|
|
63
|
+
|
|
64
|
+
console.log(`Speech saved to ${filePath} and ${streamFilePath}`);
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Features
|
|
68
|
+
|
|
69
|
+
- High-quality Text-to-Speech synthesis
|
|
70
|
+
- Support for 10+ Indian languages
|
|
71
|
+
- Choice of 10+ diverse speakers
|
|
72
|
+
- Advanced voice customization options
|
|
73
|
+
|
|
74
|
+
## Available Voices
|
|
75
|
+
|
|
76
|
+
### Speakers
|
|
77
|
+
|
|
78
|
+
- `meera` (default)
|
|
79
|
+
- `pavithra`
|
|
80
|
+
- `maitreyi`
|
|
81
|
+
- `arvind`
|
|
82
|
+
- `amol`
|
|
83
|
+
- `amartya`
|
|
84
|
+
- `diya`
|
|
85
|
+
- `neel`
|
|
86
|
+
- `misha`
|
|
87
|
+
- `vian`
|
|
88
|
+
- `arjun`
|
|
89
|
+
- `maya`
|
|
90
|
+
|
|
91
|
+
### Languages
|
|
92
|
+
|
|
93
|
+
| Language | Code |
|
|
94
|
+
| --------- | ----- |
|
|
95
|
+
| English | en-IN |
|
|
96
|
+
| Hindi | hi-IN |
|
|
97
|
+
| Bengali | bn-IN |
|
|
98
|
+
| Kannada | kn-IN |
|
|
99
|
+
| Malayalam | ml-IN |
|
|
100
|
+
| Marathi | mr-IN |
|
|
101
|
+
| Odia | od-IN |
|
|
102
|
+
| Punjabi | pa-IN |
|
|
103
|
+
| Tamil | ta-IN |
|
|
104
|
+
| Telugu | te-IN |
|
|
105
|
+
| Gujarati | gu-IN |
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { MastraVoice } from '@mastra/core/voice';
|
|
2
|
+
|
|
3
|
+
export declare const SARVAM_LANGUAGES: readonly ["hi-IN", "bn-IN", "kn-IN", "ml-IN", "mr-IN", "od-IN", "pa-IN", "ta-IN", "te-IN", "en-IN", "gu-IN"];
|
|
4
|
+
|
|
5
|
+
export declare const SARVAM_MODELS: readonly ["bulbul:v1"];
|
|
6
|
+
|
|
7
|
+
export declare const SARVAM_VOICES: readonly ["meera", "pavithra", "maitreyi", "arvind", "amol", "amartya", "diya", "neel", "misha", "vian", "arjun", "maya"];
|
|
8
|
+
|
|
9
|
+
export declare type SarvamLanguage = (typeof SARVAM_LANGUAGES)[number];
|
|
10
|
+
|
|
11
|
+
export declare type SarvamModel = (typeof SARVAM_MODELS)[number];
|
|
12
|
+
|
|
13
|
+
export declare class SarvamVoice extends MastraVoice {
|
|
14
|
+
private apiKey?;
|
|
15
|
+
private model;
|
|
16
|
+
private language;
|
|
17
|
+
private properties;
|
|
18
|
+
protected speaker: SarvamVoiceId;
|
|
19
|
+
private baseUrl;
|
|
20
|
+
constructor({ speechModel, speaker, }?: {
|
|
21
|
+
speechModel?: SarvamVoiceConfig;
|
|
22
|
+
speaker?: SarvamVoiceId;
|
|
23
|
+
});
|
|
24
|
+
private makeRequest;
|
|
25
|
+
private streamToString;
|
|
26
|
+
speak(input: string | NodeJS.ReadableStream, options?: {
|
|
27
|
+
speaker?: SarvamVoiceId;
|
|
28
|
+
}): Promise<NodeJS.ReadableStream>;
|
|
29
|
+
getSpeakers(): Promise<{
|
|
30
|
+
voiceId: "meera" | "pavithra" | "maitreyi" | "arvind" | "amol" | "amartya" | "diya" | "neel" | "misha" | "vian" | "arjun" | "maya";
|
|
31
|
+
}[]>;
|
|
32
|
+
listen(_input: NodeJS.ReadableStream, _options?: Record<string, unknown>): Promise<string | NodeJS.ReadableStream>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare interface SarvamVoiceConfig {
|
|
36
|
+
apiKey?: string;
|
|
37
|
+
model?: SarvamModel;
|
|
38
|
+
language?: SarvamLanguage;
|
|
39
|
+
properties?: {
|
|
40
|
+
pitch?: number;
|
|
41
|
+
pace?: number;
|
|
42
|
+
loudness?: number;
|
|
43
|
+
speech_sample_rate?: 8000 | 16000 | 22050;
|
|
44
|
+
enable_preprocessing?: boolean;
|
|
45
|
+
eng_interpolation_wt?: number;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export declare type SarvamVoiceId = (typeof SARVAM_VOICES)[number];
|
|
50
|
+
|
|
51
|
+
export { }
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { MastraVoice } from '@mastra/core/voice';
|
|
2
|
+
|
|
3
|
+
export declare const SARVAM_LANGUAGES: readonly ["hi-IN", "bn-IN", "kn-IN", "ml-IN", "mr-IN", "od-IN", "pa-IN", "ta-IN", "te-IN", "en-IN", "gu-IN"];
|
|
4
|
+
|
|
5
|
+
export declare const SARVAM_MODELS: readonly ["bulbul:v1"];
|
|
6
|
+
|
|
7
|
+
export declare const SARVAM_VOICES: readonly ["meera", "pavithra", "maitreyi", "arvind", "amol", "amartya", "diya", "neel", "misha", "vian", "arjun", "maya"];
|
|
8
|
+
|
|
9
|
+
export declare type SarvamLanguage = (typeof SARVAM_LANGUAGES)[number];
|
|
10
|
+
|
|
11
|
+
export declare type SarvamModel = (typeof SARVAM_MODELS)[number];
|
|
12
|
+
|
|
13
|
+
export declare class SarvamVoice extends MastraVoice {
|
|
14
|
+
private apiKey?;
|
|
15
|
+
private model;
|
|
16
|
+
private language;
|
|
17
|
+
private properties;
|
|
18
|
+
protected speaker: SarvamVoiceId;
|
|
19
|
+
private baseUrl;
|
|
20
|
+
constructor({ speechModel, speaker, }?: {
|
|
21
|
+
speechModel?: SarvamVoiceConfig;
|
|
22
|
+
speaker?: SarvamVoiceId;
|
|
23
|
+
});
|
|
24
|
+
private makeRequest;
|
|
25
|
+
private streamToString;
|
|
26
|
+
speak(input: string | NodeJS.ReadableStream, options?: {
|
|
27
|
+
speaker?: SarvamVoiceId;
|
|
28
|
+
}): Promise<NodeJS.ReadableStream>;
|
|
29
|
+
getSpeakers(): Promise<{
|
|
30
|
+
voiceId: "meera" | "pavithra" | "maitreyi" | "arvind" | "amol" | "amartya" | "diya" | "neel" | "misha" | "vian" | "arjun" | "maya";
|
|
31
|
+
}[]>;
|
|
32
|
+
listen(_input: NodeJS.ReadableStream, _options?: Record<string, unknown>): Promise<string | NodeJS.ReadableStream>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare interface SarvamVoiceConfig {
|
|
36
|
+
apiKey?: string;
|
|
37
|
+
model?: SarvamModel;
|
|
38
|
+
language?: SarvamLanguage;
|
|
39
|
+
properties?: {
|
|
40
|
+
pitch?: number;
|
|
41
|
+
pace?: number;
|
|
42
|
+
loudness?: number;
|
|
43
|
+
speech_sample_rate?: 8000 | 16000 | 22050;
|
|
44
|
+
enable_preprocessing?: boolean;
|
|
45
|
+
eng_interpolation_wt?: number;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export declare type SarvamVoiceId = (typeof SARVAM_VOICES)[number];
|
|
50
|
+
|
|
51
|
+
export { }
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var stream = require('stream');
|
|
4
|
+
var voice = require('@mastra/core/voice');
|
|
5
|
+
|
|
6
|
+
// src/index.ts
|
|
7
|
+
|
|
8
|
+
// src/voices.ts
|
|
9
|
+
var SARVAM_VOICES = [
|
|
10
|
+
"meera",
|
|
11
|
+
"pavithra",
|
|
12
|
+
"maitreyi",
|
|
13
|
+
"arvind",
|
|
14
|
+
"amol",
|
|
15
|
+
"amartya",
|
|
16
|
+
"diya",
|
|
17
|
+
"neel",
|
|
18
|
+
"misha",
|
|
19
|
+
"vian",
|
|
20
|
+
"arjun",
|
|
21
|
+
"maya"
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
// src/index.ts
|
|
25
|
+
var SarvamVoice = class extends voice.MastraVoice {
|
|
26
|
+
apiKey;
|
|
27
|
+
model = "bulbul:v1";
|
|
28
|
+
language = "en-IN";
|
|
29
|
+
properties = {};
|
|
30
|
+
speaker = "meera";
|
|
31
|
+
baseUrl = "https://api.sarvam.ai";
|
|
32
|
+
constructor({
|
|
33
|
+
speechModel,
|
|
34
|
+
speaker
|
|
35
|
+
} = {}) {
|
|
36
|
+
const defaultSpeechModel = {
|
|
37
|
+
model: "bulbul:v1",
|
|
38
|
+
apiKey: process.env.SARVAM_API_KEY,
|
|
39
|
+
language: "en-IN"
|
|
40
|
+
};
|
|
41
|
+
super({
|
|
42
|
+
speechModel: {
|
|
43
|
+
name: speechModel?.model ?? defaultSpeechModel.model,
|
|
44
|
+
apiKey: speechModel?.apiKey ?? defaultSpeechModel.apiKey
|
|
45
|
+
},
|
|
46
|
+
speaker
|
|
47
|
+
});
|
|
48
|
+
this.apiKey = speechModel?.apiKey || defaultSpeechModel.apiKey;
|
|
49
|
+
if (!this.apiKey) {
|
|
50
|
+
throw new Error("SARVAM_API_KEY must be set");
|
|
51
|
+
}
|
|
52
|
+
this.model = speechModel?.model || defaultSpeechModel.model;
|
|
53
|
+
this.language = speechModel?.language || defaultSpeechModel.language;
|
|
54
|
+
this.properties = speechModel?.properties || {};
|
|
55
|
+
this.speaker = speaker || "meera";
|
|
56
|
+
}
|
|
57
|
+
async makeRequest(endpoint, payload) {
|
|
58
|
+
const headers = new Headers({
|
|
59
|
+
"api-subscription-key": this.apiKey,
|
|
60
|
+
"Content-Type": "application/json"
|
|
61
|
+
});
|
|
62
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers,
|
|
65
|
+
body: JSON.stringify(payload)
|
|
66
|
+
});
|
|
67
|
+
if (!response.ok) {
|
|
68
|
+
let errorMessage;
|
|
69
|
+
try {
|
|
70
|
+
const error = await response.json();
|
|
71
|
+
errorMessage = error.message || response.statusText;
|
|
72
|
+
} catch {
|
|
73
|
+
errorMessage = response.statusText;
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`Sarvam AI API Error: ${errorMessage}`);
|
|
76
|
+
}
|
|
77
|
+
return response;
|
|
78
|
+
}
|
|
79
|
+
async streamToString(stream) {
|
|
80
|
+
const chunks = [];
|
|
81
|
+
for await (const chunk of stream) {
|
|
82
|
+
if (typeof chunk === "string") {
|
|
83
|
+
chunks.push(Buffer.from(chunk));
|
|
84
|
+
} else {
|
|
85
|
+
chunks.push(chunk);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return Buffer.concat(chunks).toString("utf-8");
|
|
89
|
+
}
|
|
90
|
+
async speak(input, options) {
|
|
91
|
+
const text = typeof input === "string" ? input : await this.streamToString(input);
|
|
92
|
+
return this.traced(async () => {
|
|
93
|
+
const payload = {
|
|
94
|
+
inputs: [text],
|
|
95
|
+
target_language_code: this.language,
|
|
96
|
+
speaker: options?.speaker || this.speaker,
|
|
97
|
+
model: this.model,
|
|
98
|
+
...this.properties
|
|
99
|
+
};
|
|
100
|
+
const response = await this.makeRequest("/text-to-speech", payload);
|
|
101
|
+
const { audios } = await response.json();
|
|
102
|
+
if (!audios || !audios.length) {
|
|
103
|
+
throw new Error("No audio received from Sarvam AI");
|
|
104
|
+
}
|
|
105
|
+
const audioBuffer = Buffer.from(audios[0], "base64");
|
|
106
|
+
const stream$1 = new stream.PassThrough();
|
|
107
|
+
stream$1.write(audioBuffer);
|
|
108
|
+
stream$1.end();
|
|
109
|
+
return stream$1;
|
|
110
|
+
}, "voice.sarvam.speak")();
|
|
111
|
+
}
|
|
112
|
+
async getSpeakers() {
|
|
113
|
+
return this.traced(async () => {
|
|
114
|
+
return SARVAM_VOICES.map((voice) => ({
|
|
115
|
+
voiceId: voice
|
|
116
|
+
}));
|
|
117
|
+
}, "voice.deepgram.getSpeakers")();
|
|
118
|
+
}
|
|
119
|
+
//Todo: Implement the listen method
|
|
120
|
+
async listen(_input, _options) {
|
|
121
|
+
throw new Error("Listening method coming soon.");
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
exports.SarvamVoice = SarvamVoice;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SarvamVoice } from './_tsup-dts-rollup.cjs';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SarvamVoice } from './_tsup-dts-rollup.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { PassThrough } from 'stream';
|
|
2
|
+
import { MastraVoice } from '@mastra/core/voice';
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
|
|
6
|
+
// src/voices.ts
|
|
7
|
+
var SARVAM_VOICES = [
|
|
8
|
+
"meera",
|
|
9
|
+
"pavithra",
|
|
10
|
+
"maitreyi",
|
|
11
|
+
"arvind",
|
|
12
|
+
"amol",
|
|
13
|
+
"amartya",
|
|
14
|
+
"diya",
|
|
15
|
+
"neel",
|
|
16
|
+
"misha",
|
|
17
|
+
"vian",
|
|
18
|
+
"arjun",
|
|
19
|
+
"maya"
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
// src/index.ts
|
|
23
|
+
var SarvamVoice = class extends MastraVoice {
|
|
24
|
+
apiKey;
|
|
25
|
+
model = "bulbul:v1";
|
|
26
|
+
language = "en-IN";
|
|
27
|
+
properties = {};
|
|
28
|
+
speaker = "meera";
|
|
29
|
+
baseUrl = "https://api.sarvam.ai";
|
|
30
|
+
constructor({
|
|
31
|
+
speechModel,
|
|
32
|
+
speaker
|
|
33
|
+
} = {}) {
|
|
34
|
+
const defaultSpeechModel = {
|
|
35
|
+
model: "bulbul:v1",
|
|
36
|
+
apiKey: process.env.SARVAM_API_KEY,
|
|
37
|
+
language: "en-IN"
|
|
38
|
+
};
|
|
39
|
+
super({
|
|
40
|
+
speechModel: {
|
|
41
|
+
name: speechModel?.model ?? defaultSpeechModel.model,
|
|
42
|
+
apiKey: speechModel?.apiKey ?? defaultSpeechModel.apiKey
|
|
43
|
+
},
|
|
44
|
+
speaker
|
|
45
|
+
});
|
|
46
|
+
this.apiKey = speechModel?.apiKey || defaultSpeechModel.apiKey;
|
|
47
|
+
if (!this.apiKey) {
|
|
48
|
+
throw new Error("SARVAM_API_KEY must be set");
|
|
49
|
+
}
|
|
50
|
+
this.model = speechModel?.model || defaultSpeechModel.model;
|
|
51
|
+
this.language = speechModel?.language || defaultSpeechModel.language;
|
|
52
|
+
this.properties = speechModel?.properties || {};
|
|
53
|
+
this.speaker = speaker || "meera";
|
|
54
|
+
}
|
|
55
|
+
async makeRequest(endpoint, payload) {
|
|
56
|
+
const headers = new Headers({
|
|
57
|
+
"api-subscription-key": this.apiKey,
|
|
58
|
+
"Content-Type": "application/json"
|
|
59
|
+
});
|
|
60
|
+
const response = await fetch(`${this.baseUrl}${endpoint}`, {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers,
|
|
63
|
+
body: JSON.stringify(payload)
|
|
64
|
+
});
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
let errorMessage;
|
|
67
|
+
try {
|
|
68
|
+
const error = await response.json();
|
|
69
|
+
errorMessage = error.message || response.statusText;
|
|
70
|
+
} catch {
|
|
71
|
+
errorMessage = response.statusText;
|
|
72
|
+
}
|
|
73
|
+
throw new Error(`Sarvam AI API Error: ${errorMessage}`);
|
|
74
|
+
}
|
|
75
|
+
return response;
|
|
76
|
+
}
|
|
77
|
+
async streamToString(stream) {
|
|
78
|
+
const chunks = [];
|
|
79
|
+
for await (const chunk of stream) {
|
|
80
|
+
if (typeof chunk === "string") {
|
|
81
|
+
chunks.push(Buffer.from(chunk));
|
|
82
|
+
} else {
|
|
83
|
+
chunks.push(chunk);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return Buffer.concat(chunks).toString("utf-8");
|
|
87
|
+
}
|
|
88
|
+
async speak(input, options) {
|
|
89
|
+
const text = typeof input === "string" ? input : await this.streamToString(input);
|
|
90
|
+
return this.traced(async () => {
|
|
91
|
+
const payload = {
|
|
92
|
+
inputs: [text],
|
|
93
|
+
target_language_code: this.language,
|
|
94
|
+
speaker: options?.speaker || this.speaker,
|
|
95
|
+
model: this.model,
|
|
96
|
+
...this.properties
|
|
97
|
+
};
|
|
98
|
+
const response = await this.makeRequest("/text-to-speech", payload);
|
|
99
|
+
const { audios } = await response.json();
|
|
100
|
+
if (!audios || !audios.length) {
|
|
101
|
+
throw new Error("No audio received from Sarvam AI");
|
|
102
|
+
}
|
|
103
|
+
const audioBuffer = Buffer.from(audios[0], "base64");
|
|
104
|
+
const stream = new PassThrough();
|
|
105
|
+
stream.write(audioBuffer);
|
|
106
|
+
stream.end();
|
|
107
|
+
return stream;
|
|
108
|
+
}, "voice.sarvam.speak")();
|
|
109
|
+
}
|
|
110
|
+
async getSpeakers() {
|
|
111
|
+
return this.traced(async () => {
|
|
112
|
+
return SARVAM_VOICES.map((voice) => ({
|
|
113
|
+
voiceId: voice
|
|
114
|
+
}));
|
|
115
|
+
}, "voice.deepgram.getSpeakers")();
|
|
116
|
+
}
|
|
117
|
+
//Todo: Implement the listen method
|
|
118
|
+
async listen(_input, _options) {
|
|
119
|
+
throw new Error("Listening method coming soon.");
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export { SarvamVoice };
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mastra/voice-sarvam",
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
|
+
"description": "Mastra Sarvam AI voice integration",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.cts",
|
|
19
|
+
"default": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"./package.json": "./package.json"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"zod": "^3.24.2",
|
|
26
|
+
"@mastra/core": "^0.6.1-alpha.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@microsoft/api-extractor": "^7.52.1",
|
|
30
|
+
"@types/node": "^22.13.10",
|
|
31
|
+
"eslint": "^9.22.0",
|
|
32
|
+
"tsup": "^8.4.0",
|
|
33
|
+
"typescript": "^5.8.2",
|
|
34
|
+
"vitest": "^2.1.9",
|
|
35
|
+
"@internal/lint": "0.0.1"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
|
|
39
|
+
"build:watch": "pnpm build --watch",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"test:watch": "vitest watch",
|
|
42
|
+
"lint": "eslint ."
|
|
43
|
+
}
|
|
44
|
+
}
|