@mastra/voice-google 0.14.1-alpha.0 → 0.14.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.
@@ -1,290 +0,0 @@
1
- > Discover all available pages from the documentation index: https://mastra.ai/llms.txt
2
-
3
- # Google
4
-
5
- The Google Voice implementation in Mastra provides both text-to-speech (TTS) and speech-to-text (STT) capabilities using Google Cloud services. It supports multiple voices, languages, advanced audio configuration options, and both standard API key authentication and Vertex AI mode for enterprise deployments.
6
-
7
- ## Usage example
8
-
9
- ```typescript
10
- import { GoogleVoice } from '@mastra/voice-google'
11
-
12
- // Initialize with default configuration (uses GOOGLE_API_KEY environment variable)
13
- const voice = new GoogleVoice()
14
-
15
- // Text-to-Speech (plain text)
16
- const audioStream = await voice.speak('Hello, world!', {
17
- languageCode: 'en-US',
18
- audioConfig: {
19
- audioEncoding: 'LINEAR16',
20
- },
21
- })
22
-
23
- // Text-to-Speech with SSML
24
- const ssmlStream = await voice.speak('ignored', {
25
- input: {
26
- ssml: '<speak>Take <say-as interpret-as="unit">5 mg</say-as> daily.</speak>',
27
- },
28
- })
29
-
30
- // Text-to-Speech with Gemini-TTS model
31
- const geminiStream = await voice.speak('Hello from Gemini TTS!', {
32
- voice: { name: 'Kore', modelName: 'gemini-2.5-flash-preview-tts' },
33
- input: { prompt: 'Warm, calm tone.' },
34
- })
35
-
36
- // Speech-to-Text
37
- const transcript = await voice.listen(audioStream, {
38
- config: {
39
- encoding: 'LINEAR16',
40
- languageCode: 'en-US',
41
- },
42
- })
43
-
44
- // Get available voices for a specific language
45
- const voices = await voice.getSpeakers({ languageCode: 'en-US' })
46
- ```
47
-
48
- ## Constructor parameters
49
-
50
- **speechModel** (`GoogleModelConfig`): Configuration for text-to-speech functionality (Default: `{ apiKey: process.env.GOOGLE_API_KEY }`)
51
-
52
- **speechModel.apiKey** (`string`): Google Cloud API key. Falls back to GOOGLE\_API\_KEY environment variable. Not used when vertexAI is true.
53
-
54
- **speechModel.keyFilename** (`string`): Path to service account JSON key file. Falls back to GOOGLE\_APPLICATION\_CREDENTIALS environment variable.
55
-
56
- **speechModel.credentials** (`object`): In-memory service account credentials object with client\_email and private\_key properties.
57
-
58
- **listeningModel** (`GoogleModelConfig`): Configuration for speech-to-text functionality (Default: `{ apiKey: process.env.GOOGLE_API_KEY }`)
59
-
60
- **listeningModel.apiKey** (`string`): Google Cloud API key. Falls back to GOOGLE\_API\_KEY environment variable. Not used when vertexAI is true.
61
-
62
- **listeningModel.keyFilename** (`string`): Path to service account JSON key file. Falls back to GOOGLE\_APPLICATION\_CREDENTIALS environment variable.
63
-
64
- **listeningModel.credentials** (`object`): In-memory service account credentials object with client\_email and private\_key properties.
65
-
66
- **speaker** (`string`): Default voice ID to use for text-to-speech (Default: `'en-US-Casual-K'`)
67
-
68
- **vertexAI** (`boolean`): Enable Vertex AI mode for enterprise deployments. Uses project-based authentication instead of API keys. Requires 'project' to be set. (Default: `false`)
69
-
70
- **project** (`string`): Google Cloud project ID (required when vertexAI is true). Falls back to GOOGLE\_CLOUD\_PROJECT environment variable.
71
-
72
- **location** (`string`): Google Cloud region for Vertex AI. Falls back to GOOGLE\_CLOUD\_LOCATION environment variable. (Default: `'us-central1'`)
73
-
74
- ## Methods
75
-
76
- ### `speak()`
77
-
78
- Converts text to speech using Google Cloud Text-to-Speech service.
79
-
80
- **input** (`string | NodeJS.ReadableStream`): Text to convert to speech. If a stream is provided, it will be converted to text first.
81
-
82
- **options** (`object`): Speech synthesis options
83
-
84
- **options.speaker** (`string`): Voice ID to use for this request.
85
-
86
- **options.languageCode** (`string`): Language code for the voice (e.g., 'en-US'). Defaults to the language code derived from the speaker ID, or 'en-US'.
87
-
88
- **options.input** (`ISynthesizeSpeechRequest['input']`): Rich input object passed through to the Google Cloud TTS API. Supports ssml, markup, prompt (Gemini-TTS style steering), customPronunciations, and multiSpeakerMarkup. When provided without text, ssml, markup, or multiSpeakerMarkup, the positional input argument is used as the text field automatically.
89
-
90
- **options.voice** (`ISynthesizeSpeechRequest['voice']`): Voice configuration merged on top of defaults (name and languageCode). Supports modelName (e.g., 'gemini-2.5-flash-preview-tts') and multiSpeakerVoiceConfig.
91
-
92
- **options.audioConfig** (`ISynthesizeSpeechRequest['audioConfig']`): Audio configuration options from Google Cloud Text-to-Speech API.
93
-
94
- Returns: `Promise<NodeJS.ReadableStream>`
95
-
96
- ### `listen()`
97
-
98
- Converts speech to text using Google Cloud Speech-to-Text service. Supports both v1 (default) and v2 APIs. The v2 API adds support for AAC-in-MP4 audio (iOS Safari) via auto-decoding.
99
-
100
- #### v1 (default)
101
-
102
- **audioStream** (`NodeJS.ReadableStream`): Audio stream to transcribe
103
-
104
- **options** (`GoogleListenOptionsV1`): v1 recognition options
105
-
106
- **options.config** (`IRecognitionConfig`): v1 recognition configuration from Google Cloud Speech-to-Text API
107
-
108
- #### v2
109
-
110
- Pass `v2: true` to use the Cloud Speech-to-Text v2 API, which supports additional audio formats like AAC-in-MP4 (iOS Safari).
111
-
112
- The v2 `recognize` call is IAM-authorized and does not accept API-key-only authentication. Configure service account credentials on the `listeningModel` (or set `GOOGLE_APPLICATION_CREDENTIALS`) and set `GOOGLE_CLOUD_PROJECT` so the recognizer path can be resolved, even when `vertexAI` is not enabled.
113
-
114
- ```typescript
115
- import { GoogleVoice } from '@mastra/voice-google'
116
-
117
- // v2 listen() requires service account credentials, not just GOOGLE_API_KEY.
118
- // Set GOOGLE_CLOUD_PROJECT so the recognizer path can be resolved.
119
- const voice = new GoogleVoice({
120
- listeningModel: { keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS },
121
- })
122
-
123
- const transcript = await voice.listen(iosSafariAacStream, {
124
- v2: true,
125
- config: {
126
- autoDecodingConfig: {},
127
- },
128
- })
129
- ```
130
-
131
- > **Note:** `listen({ v2: true })` fails with `PERMISSION_DENIED` on `speech.recognizers.recognize` when only `GOOGLE_API_KEY` is set. An API-key request carries no OAuth identity, so granting `roles/speech.client` to a user account does not help — the role must be granted to the service account presented in the request. This applies regardless of the `vertexAI` setting; `speak()` and v1 `listen()` still work with an API key alone.
132
-
133
- **audioStream** (`NodeJS.ReadableStream`): Audio stream to transcribe
134
-
135
- **options** (`GoogleListenOptionsV2`): v2 recognition options
136
-
137
- **options.v2** (`true`): Enables the v2 API path
138
-
139
- **options.config** (`v2.IRecognitionConfig`): v2 recognition configuration. Defaults to auto-decoding with languageCodes: \['en-US'] and model: 'long'. Set autoDecodingConfig: {} to auto-detect the audio format, or use explicitDecodingConfig to specify an encoding like MP4\_AAC, M4A\_AAC, or MOV\_AAC.
140
-
141
- **options.recognizer** (`string`): v2 recognizer resource path. Defaults to projects/{project}/locations/global/recognizers/\_ where {project} is resolved from the constructor project option, GOOGLE\_CLOUD\_PROJECT, or the client's default project.
142
-
143
- Returns: `Promise<string>`
144
-
145
- ### `getSpeakers()`
146
-
147
- Returns an array of available voice options, where each node contains:
148
-
149
- **voiceId** (`string`): Unique identifier for the voice
150
-
151
- **languageCodes** (`string[]`): List of language codes supported by this voice
152
-
153
- ### `isUsingVertexAI()`
154
-
155
- Checks if Vertex AI mode is enabled.
156
-
157
- Returns: `boolean` - `true` if using Vertex AI, `false` otherwise
158
-
159
- ### `getProject()`
160
-
161
- Gets the configured Google Cloud project ID.
162
-
163
- Returns: `string | undefined` - The project ID or `undefined` if not set
164
-
165
- ### `getLocation()`
166
-
167
- Gets the configured Google Cloud location/region.
168
-
169
- Returns: `string` - The location (default: `'us-central1'`)
170
-
171
- ## Authentication
172
-
173
- The Google Voice provider supports two authentication methods:
174
-
175
- ### Standard Mode (API Key)
176
-
177
- Uses a Google Cloud API key for authentication. Covers `speak()` and v1 `listen()`. It does not cover `listen({ v2: true })`, which is IAM-authorized and requires service account credentials (see [v2](#v2)).
178
-
179
- ```typescript
180
- // Using environment variable (GOOGLE_API_KEY)
181
- const voice = new GoogleVoice()
182
-
183
- // Using explicit API key
184
- const voice = new GoogleVoice({
185
- speechModel: { apiKey: 'your-api-key' },
186
- listeningModel: { apiKey: 'your-api-key' },
187
- speaker: 'en-US-Casual-K',
188
- })
189
- ```
190
-
191
- ### Vertex AI Mode (Service Account)
192
-
193
- Uses Google Cloud project-based authentication with service accounts. Recommended for production and enterprise deployments.
194
-
195
- **Benefits:**
196
-
197
- - Better security (no API keys in code)
198
- - IAM-based access control
199
- - Project-level billing and quotas
200
- - Audit logging
201
- - Enterprise features
202
-
203
- **Configuration Options:**
204
-
205
- ```typescript
206
- // Using Application Default Credentials (ADC)
207
- // Set GOOGLE_APPLICATION_CREDENTIALS and GOOGLE_CLOUD_PROJECT env vars
208
- const voice = new GoogleVoice({
209
- vertexAI: true,
210
- project: 'your-gcp-project',
211
- location: 'us-central1', // Optional, defaults to 'us-central1'
212
- })
213
-
214
- // Using service account key file
215
- const voice = new GoogleVoice({
216
- vertexAI: true,
217
- project: 'your-gcp-project',
218
- speechModel: {
219
- keyFilename: '/path/to/service-account.json',
220
- },
221
- listeningModel: {
222
- keyFilename: '/path/to/service-account.json',
223
- },
224
- })
225
-
226
- // Using in-memory credentials
227
- const voice = new GoogleVoice({
228
- vertexAI: true,
229
- project: 'your-gcp-project',
230
- speechModel: {
231
- credentials: {
232
- client_email: 'service-account@project.iam.gserviceaccount.com',
233
- private_key: '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----',
234
- },
235
- },
236
- })
237
- ```
238
-
239
- #### Required Permissions
240
-
241
- #### IAM Roles
242
-
243
- For Text-to-Speech:
244
-
245
- - `roles/texttospeech.admin` - Text-to-Speech Admin (full access)
246
- - `roles/texttospeech.editor` - Text-to-Speech Editor (create and manage)
247
- - `roles/texttospeech.viewer` - Text-to-Speech Viewer (read-only)
248
-
249
- For Speech-to-Text:
250
-
251
- - `roles/speech.client` - Speech-to-Text Client
252
-
253
- Grant `roles/speech.client` to the service account whose credentials the request presents (via `keyFilename`, `credentials`, or `GOOGLE_APPLICATION_CREDENTIALS`). This role is required for `listen({ v2: true })` specifically, not only for Vertex AI mode. Granting it to a user account has no effect on API-key-only requests, which carry no identity to authorize.
254
-
255
- #### OAuth Scopes
256
-
257
- For synchronous Text-to-Speech synthesis:
258
-
259
- - `https://www.googleapis.com/auth/cloud-platform` - Full access to Google Cloud Platform services
260
-
261
- For long-audio Text-to-Speech operations:
262
-
263
- - `locations.longAudioSynthesize` - Create long-audio synthesis operations
264
- - `operations.get` - Get operation status
265
- - `operations.list` - List operations
266
-
267
- ## Important notes
268
-
269
- 1. **Authentication**: Either a Google Cloud API key (standard mode) or service account credentials (Vertex AI mode) is required.
270
-
271
- 2. **Environment Variables**:
272
-
273
- - `GOOGLE_API_KEY` - API key for standard mode
274
- - `GOOGLE_CLOUD_PROJECT` - Project ID for Vertex AI mode
275
- - `GOOGLE_CLOUD_LOCATION` - Location for Vertex AI mode (defaults to 'us-central1')
276
- - `GOOGLE_APPLICATION_CREDENTIALS` - Path to service account key file
277
-
278
- 3. The default voice is set to `'en-US-Casual-K'`.
279
-
280
- 4. Both text-to-speech and speech-to-text services use LINEAR16 as the default audio encoding.
281
-
282
- 5. The `speak()` method supports advanced audio configuration through the Google Cloud Text-to-Speech API.
283
-
284
- 6. The `listen()` method supports various recognition configurations through the Google Cloud Speech-to-Text API.
285
-
286
- 7. `listen({ v2: true })` requires service account credentials and `GOOGLE_CLOUD_PROJECT`; it fails with `PERMISSION_DENIED` when only `GOOGLE_API_KEY` is set. `speak()` and v1 `listen()` work with an API key alone.
287
-
288
- 8. Available voices can be filtered by language code using the `getSpeakers()` method.
289
-
290
- 9. Vertex AI mode provides enterprise features including IAM control, audit logs, and project-level billing.