@mastra/voice-playai 0.1.3-alpha.9 → 0.1.3

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/dist/index.cjs CHANGED
@@ -163,7 +163,11 @@ var PlayAIVoice = class extends voice.MastraVoice {
163
163
  async streamToString(stream) {
164
164
  const chunks = [];
165
165
  for await (const chunk of stream) {
166
- chunks.push(Buffer.from(chunk));
166
+ if (typeof chunk === "string") {
167
+ chunks.push(Buffer.from(chunk));
168
+ } else {
169
+ chunks.push(chunk);
170
+ }
167
171
  }
168
172
  return Buffer.concat(chunks).toString("utf-8");
169
173
  }
package/dist/index.js CHANGED
@@ -161,7 +161,11 @@ var PlayAIVoice = class extends MastraVoice {
161
161
  async streamToString(stream) {
162
162
  const chunks = [];
163
163
  for await (const chunk of stream) {
164
- chunks.push(Buffer.from(chunk));
164
+ if (typeof chunk === "string") {
165
+ chunks.push(Buffer.from(chunk));
166
+ } else {
167
+ chunks.push(chunk);
168
+ }
165
169
  }
166
170
  return Buffer.concat(chunks).toString("utf-8");
167
171
  }
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@mastra/voice-playai",
3
- "version": "0.1.3-alpha.9",
3
+ "version": "0.1.3",
4
4
  "description": "Mastra PlayAI voice integration",
5
5
  "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
6
9
  "main": "dist/index.js",
7
10
  "types": "dist/index.d.ts",
8
11
  "exports": {
@@ -19,20 +22,20 @@
19
22
  "./package.json": "./package.json"
20
23
  },
21
24
  "dependencies": {
22
- "zod": "^3.24.1",
23
- "@mastra/core": "^0.5.0-alpha.9"
25
+ "zod": "^3.24.2",
26
+ "@mastra/core": "^0.5.0"
24
27
  },
25
28
  "devDependencies": {
26
- "eslint": "^9.20.1",
27
- "@microsoft/api-extractor": "^7.49.2",
28
- "@types/node": "^22.13.1",
29
- "tsup": "^8.0.1",
30
- "typescript": "^5.7.3",
31
- "vitest": "^2.1.8",
32
- "@internal/lint": "0.0.0"
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"
33
36
  },
34
37
  "scripts": {
35
- "build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake",
38
+ "build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
36
39
  "build:watch": "pnpm build --watch",
37
40
  "test": "vitest run",
38
41
  "lint": "eslint ."
@@ -1,23 +0,0 @@
1
-
2
- > @mastra/voice-playai@0.1.3-alpha.9 build /home/runner/work/mastra/mastra/voice/playai
3
- > tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake
4
-
5
- CLI Building entry: src/index.ts
6
- CLI Using tsconfig: tsconfig.json
7
- CLI tsup v8.3.6
8
- TSC Build start
9
- TSC ⚡️ Build success in 5673ms
10
- DTS Build start
11
- CLI Target: es2022
12
- Analysis will use the bundled TypeScript version 5.7.3
13
- Writing package typings: /home/runner/work/mastra/mastra/voice/playai/dist/_tsup-dts-rollup.d.ts
14
- Analysis will use the bundled TypeScript version 5.7.3
15
- Writing package typings: /home/runner/work/mastra/mastra/voice/playai/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 9456ms
17
- CLI Cleaning output folder
18
- ESM Build start
19
- CJS Build start
20
- ESM dist/index.js 5.97 KB
21
- ESM ⚡️ Build success in 466ms
22
- CJS dist/index.cjs 6.03 KB
23
- CJS ⚡️ Build success in 466ms
package/CHANGELOG.md DELETED
@@ -1,243 +0,0 @@
1
- # @mastra/voice-playai
2
-
3
- ## 0.1.3-alpha.9
4
-
5
- ### Patch Changes
6
-
7
- - Updated dependencies [e9fbac5]
8
- - Updated dependencies [1e8bcbc]
9
- - Updated dependencies [aeb5e36]
10
- - Updated dependencies [f2301de]
11
- - @mastra/core@0.5.0-alpha.9
12
-
13
- ## 0.1.3-alpha.8
14
-
15
- ### Patch Changes
16
-
17
- - Updated dependencies [506f1d5]
18
- - @mastra/core@0.5.0-alpha.8
19
-
20
- ## 0.1.3-alpha.7
21
-
22
- ### Patch Changes
23
-
24
- - Updated dependencies [ee667a2]
25
- - @mastra/core@0.5.0-alpha.7
26
-
27
- ## 0.1.3-alpha.6
28
-
29
- ### Patch Changes
30
-
31
- - Updated dependencies [f6678e4]
32
- - @mastra/core@0.5.0-alpha.6
33
-
34
- ## 0.1.3-alpha.5
35
-
36
- ### Patch Changes
37
-
38
- - Updated dependencies [22643eb]
39
- - Updated dependencies [6feb23f]
40
- - Updated dependencies [f2d6727]
41
- - Updated dependencies [301e4ee]
42
- - Updated dependencies [dfbe4e9]
43
- - Updated dependencies [9e81f35]
44
- - Updated dependencies [caefaa2]
45
- - Updated dependencies [c151ae6]
46
- - Updated dependencies [52e0418]
47
- - Updated dependencies [03236ec]
48
- - Updated dependencies [3764e71]
49
- - Updated dependencies [df982db]
50
- - Updated dependencies [0461849]
51
- - Updated dependencies [2259379]
52
- - Updated dependencies [358f069]
53
- - @mastra/core@0.5.0-alpha.5
54
-
55
- ## 0.1.3-alpha.4
56
-
57
- ### Patch Changes
58
-
59
- - Updated dependencies [d79aedf]
60
- - @mastra/core@0.5.0-alpha.4
61
-
62
- ## 0.1.3-alpha.3
63
-
64
- ### Patch Changes
65
-
66
- - Updated dependencies [3d0e290]
67
- - @mastra/core@0.5.0-alpha.3
68
-
69
- ## 0.1.3-alpha.2
70
-
71
- ### Patch Changes
72
-
73
- - Updated dependencies [02ffb7b]
74
- - @mastra/core@0.5.0-alpha.2
75
-
76
- ## 0.1.3-alpha.1
77
-
78
- ### Patch Changes
79
-
80
- - Updated dependencies [dab255b]
81
- - @mastra/core@0.5.0-alpha.1
82
-
83
- ## 0.1.3-alpha.0
84
-
85
- ### Patch Changes
86
-
87
- - Updated dependencies [59df7b6]
88
- - Updated dependencies [29f3a82]
89
- - Updated dependencies [59df7b6]
90
- - Updated dependencies [c139344]
91
- - @mastra/core@0.5.0-alpha.0
92
-
93
- ## 0.1.2
94
-
95
- ### Patch Changes
96
-
97
- - Updated dependencies [1da20e7]
98
- - @mastra/core@0.4.4
99
-
100
- ## 0.1.2-alpha.0
101
-
102
- ### Patch Changes
103
-
104
- - Updated dependencies [1da20e7]
105
- - @mastra/core@0.4.4-alpha.0
106
-
107
- ## 0.1.1
108
-
109
- ### Patch Changes
110
-
111
- - bb4f447: Add support for commonjs
112
- - Updated dependencies [0d185b1]
113
- - Updated dependencies [ed55f1d]
114
- - Updated dependencies [06aa827]
115
- - Updated dependencies [0fd78ac]
116
- - Updated dependencies [2512a93]
117
- - Updated dependencies [e62de74]
118
- - Updated dependencies [0d25b75]
119
- - Updated dependencies [fd14a3f]
120
- - Updated dependencies [8d13b14]
121
- - Updated dependencies [3f369a2]
122
- - Updated dependencies [3ee4831]
123
- - Updated dependencies [4d4e1e1]
124
- - Updated dependencies [bb4f447]
125
- - Updated dependencies [108793c]
126
- - Updated dependencies [5f28f44]
127
- - Updated dependencies [dabecf4]
128
- - @mastra/core@0.4.3
129
-
130
- ## 0.1.1-alpha.4
131
-
132
- ### Patch Changes
133
-
134
- - Updated dependencies [dabecf4]
135
- - @mastra/core@0.4.3-alpha.4
136
-
137
- ## 0.1.1-alpha.3
138
-
139
- ### Patch Changes
140
-
141
- - bb4f447: Add support for commonjs
142
- - Updated dependencies [0fd78ac]
143
- - Updated dependencies [0d25b75]
144
- - Updated dependencies [fd14a3f]
145
- - Updated dependencies [3f369a2]
146
- - Updated dependencies [4d4e1e1]
147
- - Updated dependencies [bb4f447]
148
- - @mastra/core@0.4.3-alpha.3
149
-
150
- ## 0.1.1-alpha.2
151
-
152
- ### Patch Changes
153
-
154
- - Updated dependencies [2512a93]
155
- - Updated dependencies [e62de74]
156
- - @mastra/core@0.4.3-alpha.2
157
-
158
- ## 0.1.1-alpha.1
159
-
160
- ### Patch Changes
161
-
162
- - Updated dependencies [0d185b1]
163
- - Updated dependencies [ed55f1d]
164
- - Updated dependencies [8d13b14]
165
- - Updated dependencies [3ee4831]
166
- - Updated dependencies [108793c]
167
- - Updated dependencies [5f28f44]
168
- - @mastra/core@0.4.3-alpha.1
169
-
170
- ## 0.1.1-alpha.0
171
-
172
- ### Patch Changes
173
-
174
- - Updated dependencies [06aa827]
175
- - @mastra/core@0.4.3-alpha.0
176
-
177
- ## 0.1.0
178
-
179
- ### Patch Changes
180
-
181
- - aa72ec5: Fix ts in playai
182
- - 0503a32: fix ts error
183
- - 41d0166: deprecate @mastra/speech-playai for @mastra/voice-playai
184
- - Updated dependencies [7fceae1]
185
- - Updated dependencies [8d94c3e]
186
- - Updated dependencies [99dcdb5]
187
- - Updated dependencies [6cb63e0]
188
- - Updated dependencies [f626fbb]
189
- - Updated dependencies [e752340]
190
- - Updated dependencies [eb91535]
191
- - @mastra/core@0.4.2
192
-
193
- ## 0.1.0-alpha.4
194
-
195
- ### Patch Changes
196
-
197
- - Updated dependencies [8d94c3e]
198
- - Updated dependencies [99dcdb5]
199
- - Updated dependencies [e752340]
200
- - Updated dependencies [eb91535]
201
- - @mastra/core@0.4.2-alpha.2
202
-
203
- ## 0.1.0-alpha.3
204
-
205
- ### Patch Changes
206
-
207
- - Updated dependencies [6cb63e0]
208
- - @mastra/core@0.4.2-alpha.1
209
-
210
- ## 0.1.0-alpha.2
211
-
212
- ### Patch Changes
213
-
214
- - 41d0166: deprecate @mastra/speech-playai for @mastra/voice-playai
215
- - Updated dependencies [7fceae1]
216
- - Updated dependencies [f626fbb]
217
- - @mastra/core@0.4.2-alpha.0
218
-
219
- ## 0.1.0 (2024-XX-XX)
220
-
221
- This package replaces the deprecated @mastra/speech-playai package. All functionality has been migrated to this new package with a more consistent naming scheme.
222
-
223
- ### Changes from @mastra/speech-playai
224
-
225
- - Package renamed from @mastra/speech-playai to @mastra/voice-playai
226
- - API changes:
227
- - `PlayAITTS` class renamed to `PlayAIVoice`
228
- - `generate()` and `stream()` methods combined into `speak()`
229
- - `voices()` method renamed to `getSpeakers()`
230
- - Constructor configuration simplified
231
- - Added support for text stream input
232
- - Default speaker configuration added
233
- - All core functionality remains the same
234
- - Import paths should be updated from '@mastra/speech-playai' to '@mastra/voice-playai'
235
-
236
- ### Key Features Preserved
237
-
238
- - High-quality voice synthesis
239
- - Multiple voice options with rich metadata
240
- - Streaming support
241
- - Natural and expressive speech output
242
-
243
- For a complete history of changes prior to the rename, please see the changelog of the original package.
package/eslint.config.js DELETED
@@ -1,6 +0,0 @@
1
- import { createConfig } from '@internal/lint/eslint';
2
-
3
- const config = await createConfig();
4
-
5
- /** @type {import("eslint").Linter.Config[]} */
6
- export default [...config];
package/src/index.test.ts DELETED
@@ -1,143 +0,0 @@
1
- import { createWriteStream, mkdirSync } from 'fs';
2
- import { writeFile } from 'fs/promises';
3
- import path from 'path';
4
- import { Readable } from 'stream';
5
- import { describe, it, expect, beforeEach } from 'vitest';
6
-
7
- import { PlayAIVoice, PLAYAI_VOICES } from './index.js';
8
-
9
- describe('PlayAI Voice Integration Tests', () => {
10
- const voice = new PlayAIVoice({
11
- speechModel: {
12
- name: 'PlayDialog',
13
- apiKey: process.env.PLAYAI_API_KEY!,
14
- userId: process.env.PLAYAI_USER_ID!,
15
- },
16
- });
17
- const outputDir = path.join(process.cwd(), 'test-outputs');
18
- let voiceId: string;
19
-
20
- beforeEach(async () => {
21
- // Create output directory if it doesn't exist
22
- try {
23
- mkdirSync(outputDir, { recursive: true });
24
- } catch {
25
- // Ignore if directory already exists
26
- }
27
-
28
- const speakers = await voice.getSpeakers();
29
- voiceId = speakers.find(
30
- v => v.voiceId === 's3://voice-cloning-zero-shot/1591b954-8760-41a9-bc58-9176a68c5726/original/manifest.json',
31
- )!.voiceId;
32
- expect(voiceId).toBeDefined();
33
- });
34
-
35
- it('should verify available speakers', async () => {
36
- const speakers = await voice.getSpeakers();
37
- expect(speakers.length).toBeGreaterThan(0);
38
- expect(speakers[0]).toHaveProperty('voiceId');
39
- expect(speakers[0].voiceId).toBe(PLAYAI_VOICES[0].id);
40
- });
41
-
42
- it('should allow immediate playback while streaming', async () => {
43
- // Create a longer text to ensure we get multiple chunks
44
- const longText = 'This is a longer text that will be streamed. '.repeat(5);
45
-
46
- const audioStream = await voice.speak(longText, {
47
- speaker: voiceId,
48
- });
49
-
50
- // Create a write stream to simulate real-time playback
51
- const outputPath = path.join(outputDir, 'playai-streaming-output.mp3');
52
- const writeStream = createWriteStream(outputPath);
53
-
54
- let firstChunkTime: number | null = null;
55
- let lastChunkTime: number | null = null;
56
- let totalChunks = 0;
57
-
58
- for await (const chunk of audioStream) {
59
- if (!firstChunkTime) {
60
- firstChunkTime = Date.now();
61
- }
62
- lastChunkTime = Date.now();
63
- totalChunks++;
64
- writeStream.write(chunk);
65
- }
66
-
67
- writeStream.end();
68
- expect(firstChunkTime).toBeDefined();
69
- expect(lastChunkTime).toBeDefined();
70
- expect(lastChunkTime! - firstChunkTime!).toBeGreaterThan(100); // Should take some time to receive all chunks
71
- console.log(`Total streaming time: ${lastChunkTime! - firstChunkTime!}ms for ${totalChunks} chunks`);
72
- }, 30000);
73
-
74
- it('should test speak method', async () => {
75
- const audioStream = await voice.speak('Hello from PlayAI!', {
76
- speaker: voiceId,
77
- });
78
-
79
- const chunks: Buffer[] = [];
80
- for await (const chunk of audioStream) {
81
- chunks.push(Buffer.from(chunk));
82
- }
83
- const audioBuffer = Buffer.concat(chunks);
84
-
85
- await writeFile(path.join(outputDir, 'playai-generate-output.mp3'), audioBuffer);
86
- expect(audioBuffer.length).toBeGreaterThan(0);
87
- }, 30000);
88
-
89
- it('should handle stream input in speak method', async () => {
90
- // Create a readable stream from text
91
- const textStream = Readable.from(['Hello', ' from', ' stream', ' input!']);
92
-
93
- const audioStream = await voice.speak(textStream, {
94
- speaker: voiceId,
95
- });
96
-
97
- const chunks: Buffer[] = [];
98
- for await (const chunk of audioStream) {
99
- chunks.push(Buffer.from(chunk));
100
- }
101
- const audioBuffer = Buffer.concat(chunks);
102
-
103
- await writeFile(path.join(outputDir, 'playai-stream-input-output.mp3'), audioBuffer);
104
- expect(audioBuffer.length).toBeGreaterThan(0);
105
- }, 30000);
106
-
107
- it('should handle errors gracefully', async () => {
108
- // Test with invalid voice ID
109
- await expect(
110
- voice.speak('Hello', {
111
- speaker: 'invalid-voice-id',
112
- }),
113
- ).rejects.toThrow();
114
-
115
- // Test with empty text
116
- await expect(
117
- voice.speak('', {
118
- speaker: voiceId,
119
- }),
120
- ).rejects.toThrow();
121
- });
122
-
123
- it('should work with default configuration', async () => {
124
- // Create instance with no args
125
- const defaultVoice = new PlayAIVoice();
126
-
127
- // Should use default model name and get API key and userId from env
128
- const audioStream = await defaultVoice.speak('Testing default configuration');
129
-
130
- const chunks: Buffer[] = [];
131
- for await (const chunk of audioStream) {
132
- chunks.push(Buffer.from(chunk));
133
- }
134
- const audioBuffer = Buffer.concat(chunks);
135
-
136
- await writeFile(path.join(outputDir, 'playai-default-config-output.mp3'), audioBuffer);
137
- expect(audioBuffer.length).toBeGreaterThan(0);
138
-
139
- // Verify default speaker was used
140
- const speakers = await defaultVoice.getSpeakers();
141
- expect(speakers.length).toBeGreaterThan(0);
142
- }, 30000);
143
- });
package/src/index.ts DELETED
@@ -1,255 +0,0 @@
1
- import { PassThrough } from 'stream';
2
-
3
- import { MastraVoice } from '@mastra/core/voice';
4
-
5
- interface PlayAIVoiceInfo {
6
- name: string;
7
- accent: string;
8
- gender: 'M' | 'F';
9
- age: 'Young' | 'Middle' | 'Old';
10
- style: 'Conversational' | 'Narrative';
11
- id: string;
12
- }
13
-
14
- export const PLAYAI_VOICES: PlayAIVoiceInfo[] = [
15
- {
16
- name: 'Angelo',
17
- accent: 'US',
18
- gender: 'M',
19
- age: 'Young',
20
- style: 'Conversational',
21
- id: 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json',
22
- },
23
- {
24
- name: 'Arsenio',
25
- accent: 'US African American',
26
- gender: 'M',
27
- age: 'Middle',
28
- style: 'Conversational',
29
- id: 's3://voice-cloning-zero-shot/65977f5e-a22a-4b36-861b-ecede19bdd65/original/manifest.json',
30
- },
31
- {
32
- name: 'Cillian',
33
- accent: 'Irish',
34
- gender: 'M',
35
- age: 'Middle',
36
- style: 'Conversational',
37
- id: 's3://voice-cloning-zero-shot/1591b954-8760-41a9-bc58-9176a68c5726/original/manifest.json',
38
- },
39
- {
40
- name: 'Timo',
41
- accent: 'US',
42
- gender: 'M',
43
- age: 'Middle',
44
- style: 'Conversational',
45
- id: 's3://voice-cloning-zero-shot/677a4ae3-252f-476e-85ce-eeed68e85951/original/manifest.json',
46
- },
47
- {
48
- name: 'Dexter',
49
- accent: 'US',
50
- gender: 'M',
51
- age: 'Middle',
52
- style: 'Conversational',
53
- id: 's3://voice-cloning-zero-shot/b27bc13e-996f-4841-b584-4d35801aea98/original/manifest.json',
54
- },
55
- {
56
- name: 'Miles',
57
- accent: 'US African American',
58
- gender: 'M',
59
- age: 'Young',
60
- style: 'Conversational',
61
- id: 's3://voice-cloning-zero-shot/29dd9a52-bd32-4a6e-bff1-bbb98dcc286a/original/manifest.json',
62
- },
63
- {
64
- name: 'Briggs',
65
- accent: 'US Southern (Oklahoma)',
66
- gender: 'M',
67
- age: 'Old',
68
- style: 'Conversational',
69
- id: 's3://voice-cloning-zero-shot/71cdb799-1e03-41c6-8a05-f7cd55134b0b/original/manifest.json',
70
- },
71
- {
72
- name: 'Deedee',
73
- accent: 'US African American',
74
- gender: 'F',
75
- age: 'Middle',
76
- style: 'Conversational',
77
- id: 's3://voice-cloning-zero-shot/e040bd1b-f190-4bdb-83f0-75ef85b18f84/original/manifest.json',
78
- },
79
- {
80
- name: 'Nia',
81
- accent: 'US',
82
- gender: 'F',
83
- age: 'Young',
84
- style: 'Conversational',
85
- id: 's3://voice-cloning-zero-shot/831bd330-85c6-4333-b2b4-10c476ea3491/original/manifest.json',
86
- },
87
- {
88
- name: 'Inara',
89
- accent: 'US African American',
90
- gender: 'F',
91
- age: 'Middle',
92
- style: 'Conversational',
93
- id: 's3://voice-cloning-zero-shot/adb83b67-8d75-48ff-ad4d-a0840d231ef1/original/manifest.json',
94
- },
95
- {
96
- name: 'Constanza',
97
- accent: 'US Latin American',
98
- gender: 'F',
99
- age: 'Young',
100
- style: 'Conversational',
101
- id: 's3://voice-cloning-zero-shot/b0aca4d7-1738-4848-a80b-307ac44a7298/original/manifest.json',
102
- },
103
- {
104
- name: 'Gideon',
105
- accent: 'British',
106
- gender: 'M',
107
- age: 'Old',
108
- style: 'Narrative',
109
- id: 's3://voice-cloning-zero-shot/5a3a1168-7793-4b2c-8f90-aff2b5232131/original/manifest.json',
110
- },
111
- {
112
- name: 'Casper',
113
- accent: 'US',
114
- gender: 'M',
115
- age: 'Middle',
116
- style: 'Narrative',
117
- id: 's3://voice-cloning-zero-shot/1bbc6986-fadf-4bd8-98aa-b86fed0476e9/original/manifest.json',
118
- },
119
- {
120
- name: 'Mitch',
121
- accent: 'Australian',
122
- gender: 'M',
123
- age: 'Middle',
124
- style: 'Narrative',
125
- id: 's3://voice-cloning-zero-shot/c14e50f2-c5e3-47d1-8c45-fa4b67803d19/original/manifest.json',
126
- },
127
- {
128
- name: 'Ava',
129
- accent: 'Australian',
130
- gender: 'F',
131
- age: 'Middle',
132
- style: 'Narrative',
133
- id: 's3://voice-cloning-zero-shot/50381567-ff7b-46d2-bfdc-a9584a85e08d/original/manifest.json',
134
- },
135
- ];
136
-
137
- interface PlayAIConfig {
138
- name?: 'PlayDialog' | 'Play3.0-mini';
139
- apiKey?: string;
140
- userId?: string;
141
- }
142
-
143
- export class PlayAIVoice extends MastraVoice {
144
- private baseUrl = 'https://api.play.ai/api/v1';
145
- private userId: string;
146
-
147
- constructor({ speechModel, speaker }: { speechModel?: PlayAIConfig; speaker?: string } = {}) {
148
- super({
149
- speechModel: {
150
- name: speechModel?.name ?? 'PlayDialog',
151
- apiKey: speechModel?.apiKey ?? process.env.PLAYAI_API_KEY,
152
- },
153
- speaker: speaker ?? PLAYAI_VOICES[0]?.id,
154
- });
155
- const userId = speechModel?.userId ?? process.env.PLAYAI_USER_ID;
156
- if (!userId) {
157
- throw new Error('userId is required');
158
- }
159
-
160
- this.userId = userId;
161
- }
162
-
163
- private async makeRequest(endpoint: string, payload?: any, method: 'GET' | 'POST' = 'POST') {
164
- const headers = new Headers({
165
- Authorization: `Bearer ${this.speechModel?.apiKey}`,
166
- 'Content-Type': 'application/json',
167
- 'X-USER-ID': this.userId,
168
- });
169
-
170
- const response = await fetch(`${this.baseUrl}${endpoint}`, {
171
- method,
172
- headers,
173
- body: payload ? JSON.stringify(payload) : undefined,
174
- });
175
-
176
- if (!response.ok) {
177
- const error = (await response.json()) as { message: string };
178
-
179
- throw new Error(`PlayAI API Error: ${error.message || response.statusText}`);
180
- }
181
-
182
- return response;
183
- }
184
-
185
- private async streamToString(stream: NodeJS.ReadableStream): Promise<string> {
186
- const chunks: Buffer[] = [];
187
- for await (const chunk of stream) {
188
- chunks.push(Buffer.from(chunk));
189
- }
190
- return Buffer.concat(chunks).toString('utf-8');
191
- }
192
-
193
- async speak(input: string | NodeJS.ReadableStream, options?: { speaker?: string }): Promise<NodeJS.ReadableStream> {
194
- const text = typeof input === 'string' ? input : await this.streamToString(input);
195
-
196
- return this.traced(async () => {
197
- const payload = {
198
- text,
199
- voice: options?.speaker || this.speaker,
200
- model: this.speechModel?.name,
201
- };
202
-
203
- const response = await this.makeRequest('/tts/stream', payload);
204
- if (!response.body) {
205
- throw new Error('No response body received');
206
- }
207
-
208
- // Create a PassThrough stream for the audio
209
- const stream = new PassThrough();
210
-
211
- // Process the stream
212
- const reader = response.body.getReader();
213
- void (async () => {
214
- try {
215
- while (true) {
216
- const { done, value } = await reader.read();
217
- if (done) {
218
- stream.end();
219
- break;
220
- }
221
- stream.write(value);
222
- }
223
- } catch (error) {
224
- stream.destroy(error as Error);
225
- }
226
- })();
227
-
228
- return stream;
229
- }, 'voice.playai.speak')();
230
- }
231
-
232
- async listen(
233
- _input: NodeJS.ReadableStream,
234
- _options?: Record<string, unknown>,
235
- ): Promise<string | NodeJS.ReadableStream> {
236
- throw new Error('PlayAI does not support speech recognition');
237
- }
238
-
239
- async getSpeakers() {
240
- return this.traced(
241
- () =>
242
- Promise.resolve(
243
- PLAYAI_VOICES.map(voice => ({
244
- voiceId: voice.id,
245
- name: voice.name,
246
- accent: voice.accent,
247
- gender: voice.gender,
248
- age: voice.age,
249
- style: voice.style,
250
- })),
251
- ),
252
- 'voice.playai.voices',
253
- )();
254
- }
255
- }
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.node.json",
3
- "include": ["src/**/*"],
4
- "exclude": ["node_modules", "**/*.test.ts"]
5
- }
package/vitest.config.ts DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- globals: true,
6
- include: ['src/**/*.test.ts'],
7
- },
8
- });