@mastra/voice-google 0.0.0-afterToolExecute-20250414225911

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.md ADDED
@@ -0,0 +1,46 @@
1
+ # Elastic License 2.0 (ELv2)
2
+
3
+ Copyright (c) 2025 Mastra AI, Inc.
4
+
5
+ **Acceptance**
6
+ By using the software, you agree to all of the terms and conditions below.
7
+
8
+ **Copyright License**
9
+ 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
10
+
11
+ **Limitations**
12
+ 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.
13
+
14
+ 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.
15
+
16
+ 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.
17
+
18
+ **Patents**
19
+ 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.
20
+
21
+ **Notices**
22
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
23
+
24
+ If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
25
+
26
+ **No Other Rights**
27
+ These terms do not imply any licenses other than those expressly granted in these terms.
28
+
29
+ **Termination**
30
+ 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.
31
+
32
+ **No Liability**
33
+ 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.
34
+
35
+ **Definitions**
36
+ 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.
37
+
38
+ _you_ refers to the individual or entity agreeing to these terms.
39
+
40
+ _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.
41
+
42
+ _your licenses_ are all the licenses granted to you for the software under these terms.
43
+
44
+ _use_ means anything you do with the software requiring one of your licenses.
45
+
46
+ _trademark_ means trademarks, service marks, and similar rights.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # @mastra/voice-google
2
+
3
+ Google Cloud Voice integration for Mastra, providing both Text-to-Speech (TTS) and Speech-to-Text capabilities.
4
+
5
+ > Note: This package replaces the deprecated @mastra/speech-google package, combining both speech synthesis and recognition capabilities.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @mastra/voice-google
11
+ ```
12
+
13
+ ## Configuration
14
+
15
+ The module requires the following environment variable:
16
+
17
+ ```bash
18
+ GOOGLE_API_KEY=your_api_key
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```typescript
24
+ import { GoogleVoice } from '@mastra/voice-google';
25
+
26
+ // Initialize with configuration
27
+ const voice = new GoogleVoice({
28
+ speechModel: {
29
+ apiKey: 'your-api-key', // Optional, can use GOOGLE_API_KEY env var
30
+ },
31
+ listeningModel: {
32
+ apiKey: 'your-api-key', // Optional, can use GOOGLE_API_KEY env var
33
+ },
34
+ speaker: 'en-US-Standard-F', // Default voice
35
+ });
36
+
37
+ // List available voices
38
+ const voices = await voice.getSpeakers();
39
+
40
+ // Generate speech
41
+ const audioStream = await voice.speak('Hello from Mastra!', {
42
+ speaker: 'en-US-Standard-F',
43
+ languageCode: 'en-US',
44
+ });
45
+
46
+ // Transcribe speech
47
+ const text = await voice.listen(audioStream);
48
+ ```
49
+
50
+ ## Features
51
+
52
+ - Neural Text-to-Speech synthesis
53
+ - Speech-to-Text recognition
54
+ - Multiple voice options across different languages
55
+ - Streaming support for both speech and transcription
56
+ - High-quality audio processing
57
+ - Natural-sounding voice synthesis
58
+
59
+ ## Voice Options
60
+
61
+ View the complete list using the `getSpeakers()` method or [Google Cloud's documentation](https://cloud.google.com/text-to-speech/docs/voices).
@@ -0,0 +1,73 @@
1
+ import type { google } from '@google-cloud/text-to-speech/build/protos/protos';
2
+ import type { google as google_2 } from '@google-cloud/speech/build/protos/protos';
3
+ import { MastraVoice } from '@mastra/core/voice';
4
+
5
+ /**
6
+ * Configuration for Google Cloud Voice models
7
+ * @interface GoogleModelConfig
8
+ * @property {string} [apiKey] - Optional Google Cloud API key. If not provided, will use GOOGLE_API_KEY environment variable
9
+ */
10
+ export declare interface GoogleModelConfig {
11
+ apiKey?: string;
12
+ }
13
+
14
+ /**
15
+ * GoogleVoice class provides Text-to-Speech and Speech-to-Text capabilities using Google Cloud services
16
+ * @class GoogleVoice
17
+ * @extends MastraVoice
18
+ */
19
+ export declare class GoogleVoice extends MastraVoice {
20
+ private ttsClient;
21
+ private speechClient;
22
+ /**
23
+ * Creates an instance of GoogleVoice
24
+ * @param {Object} config - Configuration options
25
+ * @param {GoogleModelConfig} [config.speechModel] - Configuration for speech synthesis
26
+ * @param {GoogleModelConfig} [config.listeningModel] - Configuration for speech recognition
27
+ * @param {string} [config.speaker] - Default voice ID to use for speech synthesis
28
+ * @throws {Error} If no API key is provided via config or environment variable
29
+ */
30
+ constructor({ listeningModel, speechModel, speaker, }?: {
31
+ listeningModel?: GoogleModelConfig;
32
+ speechModel?: GoogleModelConfig;
33
+ speaker?: string;
34
+ });
35
+ /**
36
+ * Gets a list of available voices
37
+ * @returns {Promise<Array<{voiceId: string, languageCodes: string[]}>>} List of available voices and their supported languages. Default language is en-US.
38
+ */
39
+ getSpeakers({ languageCode }?: {
40
+ languageCode?: string;
41
+ }): Promise<{
42
+ voiceId: string;
43
+ languageCodes: string[];
44
+ }[]>;
45
+ private streamToString;
46
+ /**
47
+ * Converts text to speech
48
+ * @param {string | NodeJS.ReadableStream} input - Text or stream to convert to speech
49
+ * @param {Object} [options] - Speech synthesis options
50
+ * @param {string} [options.speaker] - Voice ID to use
51
+ * @param {string} [options.languageCode] - Language code for the voice
52
+ * @param {TextToSpeechTypes.cloud.texttospeech.v1.ISynthesizeSpeechRequest['audioConfig']} [options.audioConfig] - Audio configuration options
53
+ * @returns {Promise<NodeJS.ReadableStream>} Stream of synthesized audio. Default encoding is LINEAR16.
54
+ */
55
+ speak(input: string | NodeJS.ReadableStream, options?: {
56
+ speaker?: string;
57
+ languageCode?: string;
58
+ audioConfig?: google.cloud.texttospeech.v1.ISynthesizeSpeechRequest['audioConfig'];
59
+ }): Promise<NodeJS.ReadableStream>;
60
+ /**
61
+ * Converts speech to text
62
+ * @param {NodeJS.ReadableStream} audioStream - Audio stream to transcribe. Default encoding is LINEAR16.
63
+ * @param {Object} [options] - Recognition options
64
+ * @param {SpeechTypes.cloud.speech.v1.IRecognitionConfig} [options.config] - Recognition configuration
65
+ * @returns {Promise<string>} Transcribed text
66
+ */
67
+ listen(audioStream: NodeJS.ReadableStream, options?: {
68
+ stream?: boolean;
69
+ config?: google_2.cloud.speech.v1.IRecognitionConfig;
70
+ }): Promise<string>;
71
+ }
72
+
73
+ export { }
@@ -0,0 +1,73 @@
1
+ import type { google } from '@google-cloud/text-to-speech/build/protos/protos';
2
+ import type { google as google_2 } from '@google-cloud/speech/build/protos/protos';
3
+ import { MastraVoice } from '@mastra/core/voice';
4
+
5
+ /**
6
+ * Configuration for Google Cloud Voice models
7
+ * @interface GoogleModelConfig
8
+ * @property {string} [apiKey] - Optional Google Cloud API key. If not provided, will use GOOGLE_API_KEY environment variable
9
+ */
10
+ export declare interface GoogleModelConfig {
11
+ apiKey?: string;
12
+ }
13
+
14
+ /**
15
+ * GoogleVoice class provides Text-to-Speech and Speech-to-Text capabilities using Google Cloud services
16
+ * @class GoogleVoice
17
+ * @extends MastraVoice
18
+ */
19
+ export declare class GoogleVoice extends MastraVoice {
20
+ private ttsClient;
21
+ private speechClient;
22
+ /**
23
+ * Creates an instance of GoogleVoice
24
+ * @param {Object} config - Configuration options
25
+ * @param {GoogleModelConfig} [config.speechModel] - Configuration for speech synthesis
26
+ * @param {GoogleModelConfig} [config.listeningModel] - Configuration for speech recognition
27
+ * @param {string} [config.speaker] - Default voice ID to use for speech synthesis
28
+ * @throws {Error} If no API key is provided via config or environment variable
29
+ */
30
+ constructor({ listeningModel, speechModel, speaker, }?: {
31
+ listeningModel?: GoogleModelConfig;
32
+ speechModel?: GoogleModelConfig;
33
+ speaker?: string;
34
+ });
35
+ /**
36
+ * Gets a list of available voices
37
+ * @returns {Promise<Array<{voiceId: string, languageCodes: string[]}>>} List of available voices and their supported languages. Default language is en-US.
38
+ */
39
+ getSpeakers({ languageCode }?: {
40
+ languageCode?: string;
41
+ }): Promise<{
42
+ voiceId: string;
43
+ languageCodes: string[];
44
+ }[]>;
45
+ private streamToString;
46
+ /**
47
+ * Converts text to speech
48
+ * @param {string | NodeJS.ReadableStream} input - Text or stream to convert to speech
49
+ * @param {Object} [options] - Speech synthesis options
50
+ * @param {string} [options.speaker] - Voice ID to use
51
+ * @param {string} [options.languageCode] - Language code for the voice
52
+ * @param {TextToSpeechTypes.cloud.texttospeech.v1.ISynthesizeSpeechRequest['audioConfig']} [options.audioConfig] - Audio configuration options
53
+ * @returns {Promise<NodeJS.ReadableStream>} Stream of synthesized audio. Default encoding is LINEAR16.
54
+ */
55
+ speak(input: string | NodeJS.ReadableStream, options?: {
56
+ speaker?: string;
57
+ languageCode?: string;
58
+ audioConfig?: google.cloud.texttospeech.v1.ISynthesizeSpeechRequest['audioConfig'];
59
+ }): Promise<NodeJS.ReadableStream>;
60
+ /**
61
+ * Converts speech to text
62
+ * @param {NodeJS.ReadableStream} audioStream - Audio stream to transcribe. Default encoding is LINEAR16.
63
+ * @param {Object} [options] - Recognition options
64
+ * @param {SpeechTypes.cloud.speech.v1.IRecognitionConfig} [options.config] - Recognition configuration
65
+ * @returns {Promise<string>} Transcribed text
66
+ */
67
+ listen(audioStream: NodeJS.ReadableStream, options?: {
68
+ stream?: boolean;
69
+ config?: google_2.cloud.speech.v1.IRecognitionConfig;
70
+ }): Promise<string>;
71
+ }
72
+
73
+ export { }
package/dist/index.cjs ADDED
@@ -0,0 +1,156 @@
1
+ 'use strict';
2
+
3
+ var stream = require('stream');
4
+ var speech = require('@google-cloud/speech');
5
+ var textToSpeech = require('@google-cloud/text-to-speech');
6
+ var voice = require('@mastra/core/voice');
7
+
8
+ // src/index.ts
9
+ var DEFAULT_VOICE = "en-US-Casual-K";
10
+ var GoogleVoice = class extends voice.MastraVoice {
11
+ ttsClient;
12
+ speechClient;
13
+ /**
14
+ * Creates an instance of GoogleVoice
15
+ * @param {Object} config - Configuration options
16
+ * @param {GoogleModelConfig} [config.speechModel] - Configuration for speech synthesis
17
+ * @param {GoogleModelConfig} [config.listeningModel] - Configuration for speech recognition
18
+ * @param {string} [config.speaker] - Default voice ID to use for speech synthesis
19
+ * @throws {Error} If no API key is provided via config or environment variable
20
+ */
21
+ constructor({
22
+ listeningModel,
23
+ speechModel,
24
+ speaker
25
+ } = {}) {
26
+ const defaultApiKey = process.env.GOOGLE_API_KEY;
27
+ const defaultSpeaker = DEFAULT_VOICE;
28
+ super({
29
+ speechModel: {
30
+ name: "",
31
+ apiKey: speechModel?.apiKey ?? defaultApiKey
32
+ },
33
+ listeningModel: {
34
+ name: "",
35
+ apiKey: listeningModel?.apiKey ?? defaultApiKey
36
+ },
37
+ speaker: speaker ?? defaultSpeaker
38
+ });
39
+ const apiKey = defaultApiKey || speechModel?.apiKey || listeningModel?.apiKey;
40
+ if (!apiKey) {
41
+ throw new Error(
42
+ "Google API key is not set, set GOOGLE_API_KEY environment variable or pass apiKey to constructor"
43
+ );
44
+ }
45
+ this.ttsClient = new textToSpeech.TextToSpeechClient({
46
+ apiKey: this.speechModel?.apiKey || defaultApiKey
47
+ });
48
+ this.speechClient = new speech.SpeechClient({
49
+ apiKey: this.listeningModel?.apiKey || defaultApiKey
50
+ });
51
+ }
52
+ /**
53
+ * Gets a list of available voices
54
+ * @returns {Promise<Array<{voiceId: string, languageCodes: string[]}>>} List of available voices and their supported languages. Default language is en-US.
55
+ */
56
+ async getSpeakers({ languageCode = "en-US" } = {}) {
57
+ return this.traced(async () => {
58
+ const [response] = await this.ttsClient.listVoices({ languageCode });
59
+ return (response?.voices || []).filter((voice) => voice.name && voice.languageCodes).map((voice) => ({
60
+ voiceId: voice.name,
61
+ languageCodes: voice.languageCodes
62
+ }));
63
+ }, "voice.google.getSpeakers")();
64
+ }
65
+ async streamToString(stream) {
66
+ const chunks = [];
67
+ for await (const chunk of stream) {
68
+ if (typeof chunk === "string") {
69
+ chunks.push(Buffer.from(chunk));
70
+ } else {
71
+ chunks.push(chunk);
72
+ }
73
+ }
74
+ return Buffer.concat(chunks).toString("utf-8");
75
+ }
76
+ /**
77
+ * Converts text to speech
78
+ * @param {string | NodeJS.ReadableStream} input - Text or stream to convert to speech
79
+ * @param {Object} [options] - Speech synthesis options
80
+ * @param {string} [options.speaker] - Voice ID to use
81
+ * @param {string} [options.languageCode] - Language code for the voice
82
+ * @param {TextToSpeechTypes.cloud.texttospeech.v1.ISynthesizeSpeechRequest['audioConfig']} [options.audioConfig] - Audio configuration options
83
+ * @returns {Promise<NodeJS.ReadableStream>} Stream of synthesized audio. Default encoding is LINEAR16.
84
+ */
85
+ async speak(input, options) {
86
+ return this.traced(async () => {
87
+ const text = typeof input === "string" ? input : await this.streamToString(input);
88
+ const request = {
89
+ input: { text },
90
+ voice: {
91
+ name: options?.speaker || this.speaker,
92
+ languageCode: options?.languageCode || options?.speaker?.split("-").slice(0, 2).join("-") || "en-US"
93
+ },
94
+ audioConfig: options?.audioConfig || { audioEncoding: "LINEAR16" }
95
+ };
96
+ const [response] = await this.ttsClient.synthesizeSpeech(request);
97
+ if (!response.audioContent) {
98
+ throw new Error("No audio content returned.");
99
+ }
100
+ if (typeof response.audioContent === "string") {
101
+ throw new Error("Audio content is a string.");
102
+ }
103
+ const stream$1 = new stream.PassThrough();
104
+ stream$1.end(Buffer.from(response.audioContent));
105
+ return stream$1;
106
+ }, "voice.google.speak")();
107
+ }
108
+ /**
109
+ * Converts speech to text
110
+ * @param {NodeJS.ReadableStream} audioStream - Audio stream to transcribe. Default encoding is LINEAR16.
111
+ * @param {Object} [options] - Recognition options
112
+ * @param {SpeechTypes.cloud.speech.v1.IRecognitionConfig} [options.config] - Recognition configuration
113
+ * @returns {Promise<string>} Transcribed text
114
+ */
115
+ async listen(audioStream, options) {
116
+ return this.traced(async () => {
117
+ const chunks = [];
118
+ for await (const chunk of audioStream) {
119
+ if (typeof chunk === "string") {
120
+ chunks.push(Buffer.from(chunk));
121
+ } else {
122
+ chunks.push(chunk);
123
+ }
124
+ }
125
+ const buffer = Buffer.concat(chunks);
126
+ let request = {
127
+ config: {
128
+ encoding: "LINEAR16",
129
+ languageCode: "en-US",
130
+ ...options?.config
131
+ },
132
+ audio: {
133
+ content: buffer.toString("base64")
134
+ }
135
+ };
136
+ console.log(`BEFORE REQUEST`);
137
+ const [response] = await this.speechClient.recognize(request);
138
+ console.log(`AFTER REQUEST`);
139
+ if (!response.results || response.results.length === 0) {
140
+ throw new Error("No transcription results returned");
141
+ }
142
+ const transcription = response.results.map((result) => {
143
+ if (!result.alternatives || result.alternatives.length === 0) {
144
+ return "";
145
+ }
146
+ return result.alternatives[0].transcript || "";
147
+ }).filter((text) => text.length > 0).join(" ");
148
+ if (!transcription) {
149
+ throw new Error("No valid transcription found in results");
150
+ }
151
+ return transcription;
152
+ }, "voice.google.listen")();
153
+ }
154
+ };
155
+
156
+ exports.GoogleVoice = GoogleVoice;
@@ -0,0 +1,2 @@
1
+ export { GoogleModelConfig } from './_tsup-dts-rollup.cjs';
2
+ export { GoogleVoice } from './_tsup-dts-rollup.cjs';
@@ -0,0 +1,2 @@
1
+ export { GoogleModelConfig } from './_tsup-dts-rollup.js';
2
+ export { GoogleVoice } from './_tsup-dts-rollup.js';
package/dist/index.js ADDED
@@ -0,0 +1,154 @@
1
+ import { PassThrough } from 'stream';
2
+ import { SpeechClient } from '@google-cloud/speech';
3
+ import { TextToSpeechClient } from '@google-cloud/text-to-speech';
4
+ import { MastraVoice } from '@mastra/core/voice';
5
+
6
+ // src/index.ts
7
+ var DEFAULT_VOICE = "en-US-Casual-K";
8
+ var GoogleVoice = class extends MastraVoice {
9
+ ttsClient;
10
+ speechClient;
11
+ /**
12
+ * Creates an instance of GoogleVoice
13
+ * @param {Object} config - Configuration options
14
+ * @param {GoogleModelConfig} [config.speechModel] - Configuration for speech synthesis
15
+ * @param {GoogleModelConfig} [config.listeningModel] - Configuration for speech recognition
16
+ * @param {string} [config.speaker] - Default voice ID to use for speech synthesis
17
+ * @throws {Error} If no API key is provided via config or environment variable
18
+ */
19
+ constructor({
20
+ listeningModel,
21
+ speechModel,
22
+ speaker
23
+ } = {}) {
24
+ const defaultApiKey = process.env.GOOGLE_API_KEY;
25
+ const defaultSpeaker = DEFAULT_VOICE;
26
+ super({
27
+ speechModel: {
28
+ name: "",
29
+ apiKey: speechModel?.apiKey ?? defaultApiKey
30
+ },
31
+ listeningModel: {
32
+ name: "",
33
+ apiKey: listeningModel?.apiKey ?? defaultApiKey
34
+ },
35
+ speaker: speaker ?? defaultSpeaker
36
+ });
37
+ const apiKey = defaultApiKey || speechModel?.apiKey || listeningModel?.apiKey;
38
+ if (!apiKey) {
39
+ throw new Error(
40
+ "Google API key is not set, set GOOGLE_API_KEY environment variable or pass apiKey to constructor"
41
+ );
42
+ }
43
+ this.ttsClient = new TextToSpeechClient({
44
+ apiKey: this.speechModel?.apiKey || defaultApiKey
45
+ });
46
+ this.speechClient = new SpeechClient({
47
+ apiKey: this.listeningModel?.apiKey || defaultApiKey
48
+ });
49
+ }
50
+ /**
51
+ * Gets a list of available voices
52
+ * @returns {Promise<Array<{voiceId: string, languageCodes: string[]}>>} List of available voices and their supported languages. Default language is en-US.
53
+ */
54
+ async getSpeakers({ languageCode = "en-US" } = {}) {
55
+ return this.traced(async () => {
56
+ const [response] = await this.ttsClient.listVoices({ languageCode });
57
+ return (response?.voices || []).filter((voice) => voice.name && voice.languageCodes).map((voice) => ({
58
+ voiceId: voice.name,
59
+ languageCodes: voice.languageCodes
60
+ }));
61
+ }, "voice.google.getSpeakers")();
62
+ }
63
+ async streamToString(stream) {
64
+ const chunks = [];
65
+ for await (const chunk of stream) {
66
+ if (typeof chunk === "string") {
67
+ chunks.push(Buffer.from(chunk));
68
+ } else {
69
+ chunks.push(chunk);
70
+ }
71
+ }
72
+ return Buffer.concat(chunks).toString("utf-8");
73
+ }
74
+ /**
75
+ * Converts text to speech
76
+ * @param {string | NodeJS.ReadableStream} input - Text or stream to convert to speech
77
+ * @param {Object} [options] - Speech synthesis options
78
+ * @param {string} [options.speaker] - Voice ID to use
79
+ * @param {string} [options.languageCode] - Language code for the voice
80
+ * @param {TextToSpeechTypes.cloud.texttospeech.v1.ISynthesizeSpeechRequest['audioConfig']} [options.audioConfig] - Audio configuration options
81
+ * @returns {Promise<NodeJS.ReadableStream>} Stream of synthesized audio. Default encoding is LINEAR16.
82
+ */
83
+ async speak(input, options) {
84
+ return this.traced(async () => {
85
+ const text = typeof input === "string" ? input : await this.streamToString(input);
86
+ const request = {
87
+ input: { text },
88
+ voice: {
89
+ name: options?.speaker || this.speaker,
90
+ languageCode: options?.languageCode || options?.speaker?.split("-").slice(0, 2).join("-") || "en-US"
91
+ },
92
+ audioConfig: options?.audioConfig || { audioEncoding: "LINEAR16" }
93
+ };
94
+ const [response] = await this.ttsClient.synthesizeSpeech(request);
95
+ if (!response.audioContent) {
96
+ throw new Error("No audio content returned.");
97
+ }
98
+ if (typeof response.audioContent === "string") {
99
+ throw new Error("Audio content is a string.");
100
+ }
101
+ const stream = new PassThrough();
102
+ stream.end(Buffer.from(response.audioContent));
103
+ return stream;
104
+ }, "voice.google.speak")();
105
+ }
106
+ /**
107
+ * Converts speech to text
108
+ * @param {NodeJS.ReadableStream} audioStream - Audio stream to transcribe. Default encoding is LINEAR16.
109
+ * @param {Object} [options] - Recognition options
110
+ * @param {SpeechTypes.cloud.speech.v1.IRecognitionConfig} [options.config] - Recognition configuration
111
+ * @returns {Promise<string>} Transcribed text
112
+ */
113
+ async listen(audioStream, options) {
114
+ return this.traced(async () => {
115
+ const chunks = [];
116
+ for await (const chunk of audioStream) {
117
+ if (typeof chunk === "string") {
118
+ chunks.push(Buffer.from(chunk));
119
+ } else {
120
+ chunks.push(chunk);
121
+ }
122
+ }
123
+ const buffer = Buffer.concat(chunks);
124
+ let request = {
125
+ config: {
126
+ encoding: "LINEAR16",
127
+ languageCode: "en-US",
128
+ ...options?.config
129
+ },
130
+ audio: {
131
+ content: buffer.toString("base64")
132
+ }
133
+ };
134
+ console.log(`BEFORE REQUEST`);
135
+ const [response] = await this.speechClient.recognize(request);
136
+ console.log(`AFTER REQUEST`);
137
+ if (!response.results || response.results.length === 0) {
138
+ throw new Error("No transcription results returned");
139
+ }
140
+ const transcription = response.results.map((result) => {
141
+ if (!result.alternatives || result.alternatives.length === 0) {
142
+ return "";
143
+ }
144
+ return result.alternatives[0].transcript || "";
145
+ }).filter((text) => text.length > 0).join(" ");
146
+ if (!transcription) {
147
+ throw new Error("No valid transcription found in results");
148
+ }
149
+ return transcription;
150
+ }, "voice.google.listen")();
151
+ }
152
+ };
153
+
154
+ export { GoogleVoice };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@mastra/voice-google",
3
+ "version": "0.0.0-afterToolExecute-20250414225911",
4
+ "description": "Mastra Google 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
+ "license": "Elastic-2.0",
25
+ "dependencies": {
26
+ "@google-cloud/speech": "^6.7.1",
27
+ "@google-cloud/text-to-speech": "^6.0.1",
28
+ "zod": "^3.24.2",
29
+ "@mastra/core": "0.0.0-afterToolExecute-20250414225911"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^20.17.27",
33
+ "eslint": "^9.23.0",
34
+ "tsup": "^8.4.0",
35
+ "typescript": "^5.8.2",
36
+ "vitest": "^2.1.9",
37
+ "@internal/lint": "0.0.0-afterToolExecute-20250414225911"
38
+ },
39
+ "scripts": {
40
+ "build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
41
+ "build:watch": "pnpm build --watch",
42
+ "test": "vitest run",
43
+ "lint": "eslint ."
44
+ }
45
+ }