@mastra/voice-openai-realtime 0.0.0-working-memory-per-user-20250620163010 → 0.0.0-zod-v4-compat-part-2-20250820135355
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/CHANGELOG.md +161 -7
- package/LICENSE.md +11 -42
- package/dist/index.cjs +7 -1
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +325 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -0
- package/dist/utils.d.ts +18 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +13 -13
- package/src/index.test.ts +22 -28
- package/src/index.ts +5 -1
- package/tsconfig.build.json +9 -0
- package/tsconfig.json +1 -1
- package/tsup.config.ts +22 -0
- package/dist/_tsup-dts-rollup.d.cts +0 -348
- package/dist/_tsup-dts-rollup.d.ts +0 -348
- package/dist/index.d.cts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,15 +1,169 @@
|
|
|
1
1
|
# @mastra/voice-openai-realtime
|
|
2
2
|
|
|
3
|
-
## 0.0.0-
|
|
3
|
+
## 0.0.0-zod-v4-compat-part-2-20250820135355
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
- Updated dependencies [
|
|
8
|
-
- Updated dependencies [
|
|
9
|
-
- Updated dependencies [
|
|
10
|
-
- Updated dependencies [
|
|
11
|
-
-
|
|
12
|
-
|
|
7
|
+
- Updated dependencies [94f4812]
|
|
8
|
+
- Updated dependencies [e202b82]
|
|
9
|
+
- Updated dependencies [e00f6a0]
|
|
10
|
+
- Updated dependencies [35c5798]
|
|
11
|
+
- @mastra/core@0.0.0-zod-v4-compat-part-2-20250820135355
|
|
12
|
+
|
|
13
|
+
## 0.10.6
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 047ffee: dependencies updates:
|
|
18
|
+
- Updated dependency [`ws@^8.18.3` ↗︎](https://www.npmjs.com/package/ws/v/8.18.3) (from `^8.18.2`, in `dependencies`)
|
|
19
|
+
- 3fd871d: dependencies updates:
|
|
20
|
+
- Updated dependency [`openai-realtime-api@^1.0.8` ↗︎](https://www.npmjs.com/package/openai-realtime-api/v/1.0.8) (from `^1.0.7`, in `dependencies`)
|
|
21
|
+
- f42c4c2: update peer deps for packages to latest core range
|
|
22
|
+
- Updated dependencies [510e2c8]
|
|
23
|
+
- Updated dependencies [2f72fb2]
|
|
24
|
+
- Updated dependencies [27cc97a]
|
|
25
|
+
- Updated dependencies [3f89307]
|
|
26
|
+
- Updated dependencies [9eda7d4]
|
|
27
|
+
- Updated dependencies [9d49408]
|
|
28
|
+
- Updated dependencies [41daa63]
|
|
29
|
+
- Updated dependencies [ad0a58b]
|
|
30
|
+
- Updated dependencies [254a36b]
|
|
31
|
+
- Updated dependencies [2ecf658]
|
|
32
|
+
- Updated dependencies [7a7754f]
|
|
33
|
+
- Updated dependencies [fc92d80]
|
|
34
|
+
- Updated dependencies [e0f73c6]
|
|
35
|
+
- Updated dependencies [0b89602]
|
|
36
|
+
- Updated dependencies [4d37822]
|
|
37
|
+
- Updated dependencies [23a6a7c]
|
|
38
|
+
- Updated dependencies [cda801d]
|
|
39
|
+
- Updated dependencies [a77c823]
|
|
40
|
+
- Updated dependencies [ff9c125]
|
|
41
|
+
- Updated dependencies [09bca64]
|
|
42
|
+
- Updated dependencies [b8efbb9]
|
|
43
|
+
- Updated dependencies [71466e7]
|
|
44
|
+
- Updated dependencies [0c99fbe]
|
|
45
|
+
- @mastra/core@0.12.0
|
|
46
|
+
|
|
47
|
+
## 0.10.6-alpha.1
|
|
48
|
+
|
|
49
|
+
### Patch Changes
|
|
50
|
+
|
|
51
|
+
- f42c4c2: update peer deps for packages to latest core range
|
|
52
|
+
- @mastra/core@0.12.0-alpha.5
|
|
53
|
+
|
|
54
|
+
## 0.10.6-alpha.0
|
|
55
|
+
|
|
56
|
+
### Patch Changes
|
|
57
|
+
|
|
58
|
+
- 047ffee: dependencies updates:
|
|
59
|
+
- Updated dependency [`ws@^8.18.3` ↗︎](https://www.npmjs.com/package/ws/v/8.18.3) (from `^8.18.2`, in `dependencies`)
|
|
60
|
+
- 3fd871d: dependencies updates:
|
|
61
|
+
- Updated dependency [`openai-realtime-api@^1.0.8` ↗︎](https://www.npmjs.com/package/openai-realtime-api/v/1.0.8) (from `^1.0.7`, in `dependencies`)
|
|
62
|
+
- Updated dependencies [510e2c8]
|
|
63
|
+
- Updated dependencies [2f72fb2]
|
|
64
|
+
- Updated dependencies [3f89307]
|
|
65
|
+
- Updated dependencies [9eda7d4]
|
|
66
|
+
- Updated dependencies [9d49408]
|
|
67
|
+
- Updated dependencies [2ecf658]
|
|
68
|
+
- Updated dependencies [7a7754f]
|
|
69
|
+
- Updated dependencies [fc92d80]
|
|
70
|
+
- Updated dependencies [23a6a7c]
|
|
71
|
+
- Updated dependencies [09bca64]
|
|
72
|
+
- @mastra/core@0.12.0-alpha.0
|
|
73
|
+
|
|
74
|
+
## 0.10.5
|
|
75
|
+
|
|
76
|
+
### Patch Changes
|
|
77
|
+
|
|
78
|
+
- ce088f5: Update all peerdeps to latest core
|
|
79
|
+
- @mastra/core@0.11.1
|
|
80
|
+
|
|
81
|
+
## 0.10.4
|
|
82
|
+
|
|
83
|
+
### Patch Changes
|
|
84
|
+
|
|
85
|
+
- 8c07105: bugfix: OpenAIRealtimeVoice cannot handle error from openai
|
|
86
|
+
|
|
87
|
+
issue: https://github.com/mastra-ai/mastra/issues/5648
|
|
88
|
+
|
|
89
|
+
- Updated dependencies [0b56518]
|
|
90
|
+
- Updated dependencies [db5cc15]
|
|
91
|
+
- Updated dependencies [2ba5b76]
|
|
92
|
+
- Updated dependencies [5237998]
|
|
93
|
+
- Updated dependencies [c3a30de]
|
|
94
|
+
- Updated dependencies [37c1acd]
|
|
95
|
+
- Updated dependencies [1aa60b1]
|
|
96
|
+
- Updated dependencies [89ec9d4]
|
|
97
|
+
- Updated dependencies [cf3a184]
|
|
98
|
+
- Updated dependencies [d6bfd60]
|
|
99
|
+
- Updated dependencies [626b0f4]
|
|
100
|
+
- Updated dependencies [c22a91f]
|
|
101
|
+
- Updated dependencies [f7403ab]
|
|
102
|
+
- Updated dependencies [6c89d7f]
|
|
103
|
+
- @mastra/core@0.10.15
|
|
104
|
+
|
|
105
|
+
## 0.10.4-alpha.0
|
|
106
|
+
|
|
107
|
+
### Patch Changes
|
|
108
|
+
|
|
109
|
+
- 8c07105: bugfix: OpenAIRealtimeVoice cannot handle error from openai
|
|
110
|
+
|
|
111
|
+
issue: https://github.com/mastra-ai/mastra/issues/5648
|
|
112
|
+
|
|
113
|
+
- Updated dependencies [db5cc15]
|
|
114
|
+
- Updated dependencies [5237998]
|
|
115
|
+
- Updated dependencies [37c1acd]
|
|
116
|
+
- Updated dependencies [1aa60b1]
|
|
117
|
+
- Updated dependencies [89ec9d4]
|
|
118
|
+
- Updated dependencies [626b0f4]
|
|
119
|
+
- Updated dependencies [c22a91f]
|
|
120
|
+
- Updated dependencies [f7403ab]
|
|
121
|
+
- Updated dependencies [6c89d7f]
|
|
122
|
+
- @mastra/core@0.10.15-alpha.0
|
|
123
|
+
|
|
124
|
+
## 0.10.3
|
|
125
|
+
|
|
126
|
+
### Patch Changes
|
|
127
|
+
|
|
128
|
+
- e724b23: Added support for Int16Array
|
|
129
|
+
- Updated dependencies [9dda1ac]
|
|
130
|
+
- Updated dependencies [c984582]
|
|
131
|
+
- Updated dependencies [7e801dd]
|
|
132
|
+
- Updated dependencies [a606c75]
|
|
133
|
+
- Updated dependencies [7aa70a4]
|
|
134
|
+
- Updated dependencies [764f86a]
|
|
135
|
+
- Updated dependencies [1760a1c]
|
|
136
|
+
- Updated dependencies [038e5ae]
|
|
137
|
+
- Updated dependencies [7dda16a]
|
|
138
|
+
- Updated dependencies [5ebfcdd]
|
|
139
|
+
- Updated dependencies [b2d0c91]
|
|
140
|
+
- Updated dependencies [4e809ad]
|
|
141
|
+
- Updated dependencies [57929df]
|
|
142
|
+
- Updated dependencies [b7852ed]
|
|
143
|
+
- Updated dependencies [6320a61]
|
|
144
|
+
- @mastra/core@0.10.9
|
|
145
|
+
|
|
146
|
+
## 0.10.3-alpha.0
|
|
147
|
+
|
|
148
|
+
### Patch Changes
|
|
149
|
+
|
|
150
|
+
- e724b23: Added support for Int16Array
|
|
151
|
+
- Updated dependencies [9dda1ac]
|
|
152
|
+
- Updated dependencies [c984582]
|
|
153
|
+
- Updated dependencies [7e801dd]
|
|
154
|
+
- Updated dependencies [a606c75]
|
|
155
|
+
- Updated dependencies [7aa70a4]
|
|
156
|
+
- Updated dependencies [764f86a]
|
|
157
|
+
- Updated dependencies [1760a1c]
|
|
158
|
+
- Updated dependencies [038e5ae]
|
|
159
|
+
- Updated dependencies [7dda16a]
|
|
160
|
+
- Updated dependencies [5ebfcdd]
|
|
161
|
+
- Updated dependencies [b2d0c91]
|
|
162
|
+
- Updated dependencies [4e809ad]
|
|
163
|
+
- Updated dependencies [57929df]
|
|
164
|
+
- Updated dependencies [b7852ed]
|
|
165
|
+
- Updated dependencies [6320a61]
|
|
166
|
+
- @mastra/core@0.10.9-alpha.0
|
|
13
167
|
|
|
14
168
|
## 0.10.2
|
|
15
169
|
|
package/LICENSE.md
CHANGED
|
@@ -1,46 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Apache License 2.0
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2025
|
|
3
|
+
Copyright (c) 2025 Kepler Software, Inc.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
7
8
|
|
|
8
|
-
|
|
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
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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.
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
package/dist/index.cjs
CHANGED
|
@@ -381,7 +381,8 @@ var OpenAIRealtimeVoice = class extends voice.MastraVoice {
|
|
|
381
381
|
});
|
|
382
382
|
} else if (audioData instanceof Int16Array) {
|
|
383
383
|
try {
|
|
384
|
-
this.
|
|
384
|
+
const base64Audio = this.int16ArrayToBase64(audioData);
|
|
385
|
+
this.sendEvent("input_audio_buffer.append", { audio: base64Audio, event_id: eventId });
|
|
385
386
|
} catch (err) {
|
|
386
387
|
this.emit("error", err);
|
|
387
388
|
}
|
|
@@ -540,6 +541,9 @@ var OpenAIRealtimeVoice = class extends voice.MastraVoice {
|
|
|
540
541
|
this.emit("response.done", ev);
|
|
541
542
|
speakerStreams.delete(ev.response.id);
|
|
542
543
|
});
|
|
544
|
+
this.client.on("error", async (ev) => {
|
|
545
|
+
this.emit("error", ev);
|
|
546
|
+
});
|
|
543
547
|
}
|
|
544
548
|
async handleFunctionCalls(ev) {
|
|
545
549
|
for (const output of ev.response?.output ?? []) {
|
|
@@ -627,3 +631,5 @@ var OpenAIRealtimeVoice = class extends voice.MastraVoice {
|
|
|
627
631
|
};
|
|
628
632
|
|
|
629
633
|
exports.OpenAIRealtimeVoice = OpenAIRealtimeVoice;
|
|
634
|
+
//# sourceMappingURL=index.cjs.map
|
|
635
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/index.ts"],"names":["zodToJsonSchema","Readable","MastraVoice","EventEmitter","buffer","WebSocket","ev","PassThrough"],"mappings":";;;;;;;;;AAeO,IAAM,cAAA,GAAiB,CAAC,KAAA,KAAmB;AAChD,EAAA,MAAM,cAAgF,EAAC;AACvF,EAAA,KAAA,MAAW,CAAC,MAAM,IAAI,CAAA,IAAK,OAAO,OAAA,CAAQ,KAAA,IAAS,EAAE,CAAA,EAAG;AACtD,IAAA,IAAI,UAAA;AAEJ,IAAA,IAAI,aAAA,IAAiB,IAAA,IAAQ,IAAA,CAAK,WAAA,EAAa;AAC7C,MAAA,IAAI,WAAA,CAAY,IAAA,CAAK,WAAW,CAAA,EAAG;AACjC,QAAA,UAAA,GAAaA,+BAAA,CAAgB,KAAK,WAAW,CAAA;AAC7C,QAAA,OAAO,UAAA,CAAW,OAAA;AAAA,MACpB,CAAA,MAAO;AACL,QAAA,UAAA,GAAa,IAAA,CAAK,WAAA;AAAA,MACpB;AAAA,IACF,CAAA,MAAA,IAAW,gBAAgB,IAAA,EAAM;AAC/B,MAAA,IAAI,WAAA,CAAY,IAAA,CAAK,UAAU,CAAA,EAAG;AAChC,QAAA,UAAA,GAAaA,+BAAA,CAAgB,KAAK,UAAU,CAAA;AAC5C,QAAA,OAAO,UAAA,CAAW,OAAA;AAAA,MACpB,CAAA,MAAO;AACL,QAAA,UAAA,GAAa,IAAA,CAAK,UAAA;AAAA,MACpB;AAAA,IACF,CAAA,MAAO;AACL,MAAA,OAAA,CAAQ,IAAA,CAAK,CAAA,KAAA,EAAQ,IAAI,CAAA,iDAAA,CAAmD,CAAA;AAC5E,MAAA;AAAA,IACF;AACA,IAAA,MAAM,UAAA,GAA6B;AAAA,MACjC,IAAA,EAAM,UAAA;AAAA,MACN,IAAA;AAAA,MACA,WAAA,EAAa,IAAA,CAAK,WAAA,IAAe,CAAA,MAAA,EAAS,IAAI,CAAA,CAAA;AAAA,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AACA,IAAA,OAAO,KAAK,MAAM,CAAA;AAAA,EACpB;AAAA,EAEQ,SAAA,CAAU,MAAc,IAAA,EAAW;AACzC,IAAA,IAAI,CAAC,KAAK,EAAA,IAAM,IAAA,CAAK,GAAG,UAAA,KAAe,IAAA,CAAK,GAAG,IAAA,EAAM;AACnD,MAAA,IAAA,CAAK,MAAM,IAAA,CAAK,EAAE,IAAA,EAAY,GAAG,MAAM,CAAA;AAAA,IACzC,CAAA,MAAO;AACL,MAAA,IAAA,CAAK,EAAA,EAAI,IAAA;AAAA,QACP,KAAK,SAAA,CAAU;AAAA,UACb,IAAA;AAAA,UACA,GAAG;AAAA,SACJ;AAAA,OACH;AAAA,IACF;AAAA,EACF;AACF","file":"index.cjs","sourcesContent":["import { Readable } from 'stream';\nimport type { ToolsInput } from '@mastra/core/agent';\nimport { zodToJsonSchema } from 'zod-to-json-schema';\n\nexport type OpenAIExecuteFunction = (args: any) => Promise<any>;\ntype ToolDefinition = {\n type: 'function';\n name: string;\n description: string;\n parameters: {\n [key: string]: any;\n };\n};\n\ntype TTools = ToolsInput;\nexport const transformTools = (tools?: TTools) => {\n const openaiTools: { openaiTool: ToolDefinition; execute: OpenAIExecuteFunction }[] = [];\n for (const [name, tool] of Object.entries(tools || {})) {\n let parameters: { [key: string]: any };\n\n if ('inputSchema' in tool && tool.inputSchema) {\n if (isZodObject(tool.inputSchema)) {\n parameters = zodToJsonSchema(tool.inputSchema);\n delete parameters.$schema;\n } else {\n parameters = tool.inputSchema;\n }\n } else if ('parameters' in tool) {\n if (isZodObject(tool.parameters)) {\n parameters = zodToJsonSchema(tool.parameters);\n delete parameters.$schema;\n } else {\n parameters = tool.parameters;\n }\n } else {\n console.warn(`Tool ${name} has neither inputSchema nor parameters, skipping`);\n continue;\n }\n const openaiTool: ToolDefinition = {\n type: 'function',\n name,\n description: tool.description || `Tool: ${name}`,\n parameters,\n };\n\n if (tool.execute) {\n // Create an adapter function that works with both ToolAction and VercelTool execute functions\n const executeAdapter = async (args: any) => {\n try {\n if (!tool.execute) {\n throw new Error(`Tool ${name} has no execute function`);\n }\n\n // For ToolAction, the first argument is a context object with the args in a 'context' property\n if ('inputSchema' in tool) {\n return await tool.execute({ context: args });\n }\n // For VercelTool, pass args directly\n else {\n // Create a minimal ToolExecutionOptions object with required properties\n const options = {\n toolCallId: 'unknown',\n messages: [],\n };\n return await tool.execute(args, options);\n }\n } catch (error) {\n console.error(`Error executing tool ${name}:`, error);\n throw error;\n }\n };\n openaiTools.push({ openaiTool, execute: executeAdapter });\n } else {\n console.warn(`Tool ${name} has no execute function, skipping`);\n }\n }\n return openaiTools;\n};\n\nexport const isReadableStream = (obj: unknown) => {\n return (\n obj &&\n obj instanceof Readable &&\n typeof obj.read === 'function' &&\n typeof obj.pipe === 'function' &&\n obj.readable === true\n );\n};\n\nfunction isZodObject(schema: unknown) {\n return (\n !!schema &&\n typeof schema === 'object' &&\n '_def' in schema &&\n schema._def &&\n typeof schema._def === 'object' &&\n 'typeName' in schema._def &&\n schema._def.typeName === 'ZodObject'\n );\n}\n","import { EventEmitter } from 'events';\nimport { PassThrough } from 'stream';\nimport type { ToolsInput } from '@mastra/core/agent';\nimport type { RuntimeContext } from '@mastra/core/runtime-context';\nimport { MastraVoice } from '@mastra/core/voice';\nimport type { Realtime, RealtimeServerEvents } from 'openai-realtime-api';\nimport { WebSocket } from 'ws';\nimport { isReadableStream, transformTools } from './utils';\n\n/**\n * Event callback function type\n */\ntype EventCallback = (...args: any[]) => void;\n\ntype StreamWithId = PassThrough & { id: string };\n\n/**\n * Map of event types to their callback arrays\n */\ntype EventMap = {\n transcribing: [{ text: string }];\n writing: [{ text: string }];\n speaking: [{ audio: string }];\n speaker: [StreamWithId];\n error: [Error];\n} & {\n [key: string]: EventCallback[];\n};\n\n/** Default voice for text-to-speech. 'alloy' provides a neutral, balanced voice suitable for most use cases */\nconst DEFAULT_VOICE: Realtime.Voice = 'alloy';\n\nconst DEFAULT_TRANSCRIBER: Realtime.AudioTranscriptionModel = 'whisper-1';\n\nconst DEFAULT_URL = 'wss://api.openai.com/v1/realtime';\n\n/**\n * Default model for real-time voice interactions.\n * This model is optimized for low-latency responses while maintaining high quality output.\n */\nconst DEFAULT_MODEL = 'gpt-4o-mini-realtime-preview-2024-12-17';\n\n// /**\n// * Default Voice Activity Detection (VAD) configuration.\n// * These settings control how the system detects speech segments.\n// *\n// * @property {string} type - Uses server-side VAD for better accuracy\n// * @property {number} threshold - Speech detection sensitivity (0.5 = balanced)\n// * @property {number} prefix_padding_ms - Includes 1 second of audio before speech\n// * @property {number} silence_duration_ms - Waits 1 second of silence before ending turn\n// */\n// const DEFAULT_VAD_CONFIG = {\n// type: 'server_vad',\n// threshold: 0.5,\n// prefix_padding_ms: 1000,\n// silence_duration_ms: 1000,\n// } as Realtime.TurnDetection;\n\ntype TTools = ToolsInput;\n\n/**\n * Available voice options for text-to-speech.\n * Each voice has unique characteristics suitable for different use cases:\n * - alloy: Neutral and balanced\n * - echo: Warm and natural\n * - shimmer: Clear and expressive\n * - And more...\n */\nconst VOICES = ['alloy', 'ash', 'ballad', 'coral', 'echo', 'sage', 'shimmer', 'verse'];\n\ntype RealtimeClientServerEventMap = {\n [K in RealtimeServerEvents.EventType]: [RealtimeServerEvents.EventMap[K]];\n} & {\n ['conversation.item.input_audio_transcription.delta']: [{ delta: string; response_id: string }];\n ['conversation.item.input_audio_transcription.done']: [{ response_id: string }];\n};\n\n/**\n * OpenAIRealtimeVoice provides real-time voice interaction capabilities using OpenAI's\n * WebSocket-based API. It supports:\n * - Real-time text-to-speech\n * - Speech-to-text (transcription)\n * - Voice activity detection\n * - Multiple voice options\n * - Event-based audio streaming\n *\n * The class manages WebSocket connections, audio streaming, and event handling\n * for seamless voice interactions.\n *\n * @extends MastraVoice\n *\n * @example\n * ```typescript\n * const voice = new OpenAIRealtimeVoice({\n * apiKey: process.env.OPENAI_API_KEY,\n * model: 'gpt-4o-mini-realtime'\n * });\n *\n * await voice.open();\n * voice.on('speaking', (audioData) => {\n * // Handle audio data\n * });\n *\n * await voice.speak('Hello, how can I help you today?');\n * ```\n */\nexport class OpenAIRealtimeVoice extends MastraVoice {\n private ws?: WebSocket;\n private state: 'close' | 'open';\n private client: EventEmitter<RealtimeClientServerEventMap>;\n private events: EventMap;\n private instructions?: string;\n private tools?: TTools;\n private debug: boolean;\n private queue: unknown[] = [];\n private transcriber: Realtime.AudioTranscriptionModel;\n private runtimeContext?: RuntimeContext;\n /**\n * Creates a new instance of OpenAIRealtimeVoice.\n *\n * @param options - Configuration options for the voice instance\n * @param options.url - The base URL for the OpenAI Realtime API\n * @param options.model - The model ID to use (defaults to GPT-4 Mini Realtime)\n * @param options.apiKey - OpenAI API key. Falls back to process.env.OPENAI_API_KEY\n * @param options.speaker - Voice ID to use (defaults to 'alloy')\n * @param options.debug - Enable debug mode\n *\n * @example\n * ```typescript\n * const voice = new OpenAIRealtimeVoice({\n * apiKey: 'your-api-key',\n * model: 'gpt-4o-mini-realtime',\n * speaker: 'alloy'\n * });\n * ```\n */\n constructor(\n private options: {\n model?: string;\n url?: string;\n apiKey?: string;\n speaker?: Realtime.Voice;\n transcriber?: Realtime.AudioTranscriptionModel;\n debug?: boolean;\n } = {},\n ) {\n super();\n\n this.client = new EventEmitter();\n this.state = 'close';\n this.events = {} as EventMap;\n this.speaker = options.speaker || DEFAULT_VOICE;\n this.transcriber = options.transcriber || DEFAULT_TRANSCRIBER;\n this.debug = options.debug || false;\n }\n\n /**\n * Returns a list of available voice speakers.\n *\n * @returns Promise resolving to an array of voice objects, each containing at least a voiceId\n *\n * @example\n * ```typescript\n * const speakers = await voice.getSpeakers();\n * // speakers = [{ voiceId: 'alloy' }, { voiceId: 'echo' }, ...]\n * ```\n */\n getSpeakers(): Promise<Array<{ voiceId: string; [key: string]: any }>> {\n return Promise.resolve(VOICES.map(v => ({ voiceId: v })));\n }\n\n /**\n * Disconnects from the OpenAI realtime session and cleans up resources.\n * Should be called when you're done with the voice instance.\n *\n * @example\n * ```typescript\n * voice.close(); // Disconnects and cleans up\n * ```\n */\n close() {\n if (!this.ws) return;\n this.ws.close();\n this.state = 'close';\n }\n\n /**\n * Equips the voice instance with a set of instructions.\n * Instructions allow the model to perform additional actions during conversations.\n *\n * @param instructions - Optional instructions to addInstructions\n * @returns Transformed instructions ready for use with the model\n *\n * @example\n * ```typescript\n * voice.addInstructions('You are a helpful assistant.');\n * ```\n */\n addInstructions(instructions?: string) {\n this.instructions = instructions;\n }\n\n /**\n * Equips the voice instance with a set of tools.\n * Tools allow the model to perform additional actions during conversations.\n *\n * @param tools - Optional tools configuration to addTools\n * @returns Transformed tools configuration ready for use with the model\n *\n * @example\n * ```typescript\n * const tools = {\n * search: async (query: string) => { ... },\n * calculate: (expression: string) => { ... }\n * };\n * voice.addTools(tools);\n * ```\n */\n addTools(tools?: TTools) {\n this.tools = tools || {};\n }\n\n /**\n * Emits a speaking event using the configured voice model.\n * Can accept either a string or a readable stream as input.\n *\n * @param input - The text to convert to speech, or a readable stream containing the text\n * @param options - Optional configuration for this specific speech request\n * @param options.speaker - Override the voice to use for this specific request\n *\n * @throws {Error} If the input text is empty\n *\n * @example\n * ```typescript\n * // Simple text to speech\n * await voice.speak('Hello world');\n *\n * // With custom voice\n * await voice.speak('Hello world', { speaker: 'echo' });\n *\n * // Using a stream\n * const stream = fs.createReadStream('text.txt');\n * await voice.speak(stream);\n * ```\n */\n async speak(input: string | NodeJS.ReadableStream, options?: { speaker?: Realtime.Voice }): Promise<void> {\n if (typeof input !== 'string') {\n const chunks: Buffer[] = [];\n for await (const chunk of input) {\n chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));\n }\n input = Buffer.concat(chunks).toString('utf-8');\n }\n\n if (input.trim().length === 0) {\n throw new Error('Input text is empty');\n }\n\n this.sendEvent('response.create', {\n response: {\n instructions: `Repeat the following text: ${input}`,\n voice: options?.speaker ? options.speaker : undefined,\n },\n });\n }\n\n /**\n * Updates the session configuration for the voice instance.\n * This can be used to modify voice settings, turn detection, and other parameters.\n *\n * @param sessionConfig - New session configuration to apply\n *\n * @example\n * ```typescript\n * voice.updateConfig({\n * voice: 'echo',\n * turn_detection: {\n * type: 'server_vad',\n * threshold: 0.5,\n * silence_duration_ms: 1000\n * }\n * });\n * ```\n */\n updateConfig(sessionConfig: unknown): void {\n this.sendEvent('session.update', { session: sessionConfig });\n }\n\n /**\n * Checks if listening capabilities are enabled.\n *\n * @returns {Promise<{ enabled: boolean }>}\n */\n async getListener() {\n return { enabled: true };\n }\n\n /**\n * Processes audio input for speech recognition.\n * Takes a readable stream of audio data and emits a writing event.\n * The output of the writing event is int16 audio data.\n *\n * @param audioData - Readable stream containing the audio data to process\n * @param options - Optional configuration for audio processing\n *\n * @throws {Error} If the audio data format is not supported\n *\n * @example\n * ```typescript\n * // Process audio from a file\n * const audioStream = fs.createReadStream('audio.raw');\n * await voice.listen(audioStream);\n *\n * // Process audio with options\n * await voice.listen(microphoneStream, {\n * format: 'int16',\n * sampleRate: 24000\n * });\n * ```\n */\n async listen(audioData: NodeJS.ReadableStream): Promise<void> {\n if (isReadableStream(audioData)) {\n const chunks: Buffer[] = [];\n for await (const chunk of audioData) {\n const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);\n chunks.push(buffer);\n }\n\n const buffer = Buffer.concat(chunks);\n const int16Array = new Int16Array(buffer.buffer, buffer.byteOffset ?? 0, (buffer.byteLength ?? 0) / 2);\n const base64Audio = this.int16ArrayToBase64(int16Array);\n\n this.sendEvent('conversation.item.create', {\n item: {\n type: 'message',\n role: 'user',\n content: [{ type: 'input_audio', audio: base64Audio }],\n },\n });\n\n this.sendEvent('response.create', {\n response: {\n modalities: ['text'],\n instructions: `ONLY repeat the input and DO NOT say anything else`,\n },\n });\n } else {\n this.emit('error', new Error('Unsupported audio data format'));\n }\n }\n\n waitForOpen() {\n return new Promise(resolve => {\n this.ws?.on('open', resolve);\n });\n }\n\n waitForSessionCreated() {\n return new Promise(resolve => {\n this.client.on('session.created', resolve);\n });\n }\n\n /**\n * Establishes a connection to the OpenAI realtime service.\n * Must be called before using speak, listen, or relay functions.\n *\n * @throws {Error} If connection fails or session creation times out\n *\n * @example\n * ```typescript\n * await voice.open();\n * // Now ready for voice interactions\n * ```\n */\n async connect({ runtimeContext }: { runtimeContext?: RuntimeContext } = {}) {\n const url = `${this.options.url || DEFAULT_URL}?model=${this.options.model || DEFAULT_MODEL}`;\n const apiKey = this.options.apiKey || process.env.OPENAI_API_KEY;\n this.runtimeContext = runtimeContext;\n\n this.ws = new WebSocket(url, undefined, {\n headers: {\n Authorization: 'Bearer ' + apiKey,\n 'OpenAI-Beta': 'realtime=v1',\n },\n });\n\n this.setupEventListeners();\n await Promise.all([this.waitForOpen(), this.waitForSessionCreated()]);\n\n const openaiTools = transformTools(this.tools);\n this.updateConfig({\n instructions: this.instructions,\n tools: openaiTools.map(t => t.openaiTool),\n input_audio_transcription: {\n model: this.transcriber,\n },\n voice: this.speaker,\n });\n this.state = 'open';\n }\n\n disconnect() {\n this.state = 'close';\n this.ws?.close();\n }\n\n /**\n * Streams audio data in real-time to the OpenAI service.\n * Useful for continuous audio streaming scenarios like live microphone input.\n * Must be in 'open' state before calling this method.\n *\n * @param audioData - Readable stream of audio data to relay\n * @throws {Error} If audio format is not supported\n *\n * @example\n * ```typescript\n * // First connect\n * await voice.open();\n *\n * // Then relay audio\n * const micStream = getMicrophoneStream();\n * await voice.relay(micStream);\n * ```\n */\n async send(audioData: NodeJS.ReadableStream | Int16Array, eventId?: string): Promise<void> {\n if (!this.state || this.state !== 'open') {\n console.warn('Cannot relay audio when not open. Call open() first.');\n return;\n }\n\n if (isReadableStream(audioData)) {\n const stream = audioData as NodeJS.ReadableStream;\n stream.on('data', chunk => {\n try {\n const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);\n this.sendEvent('input_audio_buffer.append', { audio: buffer.toString('base64'), event_id: eventId });\n } catch (err) {\n this.emit('error', err);\n }\n });\n } else if (audioData instanceof Int16Array) {\n try {\n const base64Audio = this.int16ArrayToBase64(audioData);\n this.sendEvent('input_audio_buffer.append', { audio: base64Audio, event_id: eventId });\n } catch (err) {\n this.emit('error', err);\n }\n } else {\n this.emit('error', new Error('Unsupported audio data format'));\n }\n }\n\n /**\n * Sends a response to the OpenAI Realtime API.\n *\n * Trigger a response to the real-time session.\n *\n * @param {Object} params - The parameters object\n * @param {Realtime.ResponseConfig} params.options - Configuration options for the response\n * @returns {Promise<void>} A promise that resolves when the response has been sent\n *\n * @example\n * // Send a simple text response\n * await realtimeVoice.answer({\n * options: {\n * content: \"Hello, how can I help you today?\",\n * voice: \"alloy\"\n * }\n * });\n */\n async answer({ options }: { options?: Realtime.ResponseConfig }) {\n this.sendEvent('response.create', { response: options ?? {} });\n }\n\n /**\n * Registers an event listener for voice events.\n * Available events: 'speaking', 'writing, 'error'\n * Can listen to OpenAI Realtime events by prefixing with 'openAIRealtime:'\n * Such as 'openAIRealtime:conversation.item.completed', 'openAIRealtime:conversation.updated', etc.\n *\n * @param event - Name of the event to listen for\n * @param callback - Function to call when the event occurs\n *\n * @example\n * ```typescript\n * // Listen for speech events\n * voice.on('speaking', (audioData: Int16Array) => {\n * // Handle audio data\n * });\n *\n * // Handle errors\n * voice.on('error', (error: Error) => {\n * console.error('Voice error:', error);\n * });\n * ```\n */\n on(event: string, callback: EventCallback): void {\n if (!this.events[event]) {\n this.events[event] = [];\n }\n this.events[event].push(callback);\n }\n\n /**\n * Removes a previously registered event listener.\n *\n * @param event - Name of the event to stop listening to\n * @param callback - The specific callback function to remove\n *\n * @example\n * ```typescript\n * // Create event handler\n * const handleSpeech = (audioData: Int16Array) => {\n * // Handle audio data\n * };\n *\n * // Add listener\n * voice.on('speaking', handleSpeech);\n *\n * // Later, remove the listener\n * voice.off('speaking', handleSpeech);\n * ```\n */\n off(event: string, callback: EventCallback): void {\n if (!this.events[event]) return;\n\n const index = this.events[event].indexOf(callback);\n if (index !== -1) {\n this.events[event].splice(index, 1);\n }\n }\n\n /**\n * Emit an event with arguments\n * @param event Event name\n * @param args Arguments to pass to the callbacks\n */\n private emit(event: string, ...args: any[]): void {\n if (!this.events[event]) return;\n\n for (const callback of this.events[event]) {\n callback(...args);\n }\n }\n\n private setupEventListeners(): void {\n const speakerStreams = new Map<string, StreamWithId>();\n\n if (!this.ws) {\n throw new Error('WebSocket not initialized');\n }\n\n this.ws.on('message', message => {\n const data = JSON.parse(message.toString());\n this.client.emit(data.type, data);\n\n if (this.debug) {\n const { delta, ...fields } = data;\n console.log(data.type, fields, delta?.length < 100 ? delta : '');\n }\n });\n\n this.client.on('session.created', ev => {\n this.emit('session.created', ev);\n\n const queue = this.queue.splice(0, this.queue.length);\n for (const ev of queue) {\n this.ws?.send(JSON.stringify(ev));\n }\n });\n this.client.on('session.updated', ev => {\n this.emit('session.updated', ev);\n });\n this.client.on('response.created', ev => {\n this.emit('response.created', ev);\n\n const speakerStream = new PassThrough() as StreamWithId;\n\n speakerStream.id = ev.response.id;\n\n speakerStreams.set(ev.response.id, speakerStream);\n this.emit('speaker', speakerStream);\n });\n this.client.on('conversation.item.input_audio_transcription.delta', ev => {\n this.emit('writing', { text: ev.delta, response_id: ev.response_id, role: 'user' });\n });\n this.client.on('conversation.item.input_audio_transcription.done', ev => {\n this.emit('writing', { text: '\\n', response_id: ev.response_id, role: 'user' });\n });\n this.client.on('response.audio.delta', ev => {\n const audio = Buffer.from(ev.delta, 'base64');\n this.emit('speaking', { audio, response_id: ev.response_id });\n\n const stream = speakerStreams.get(ev.response_id);\n stream?.write(audio);\n });\n this.client.on('response.audio.done', ev => {\n this.emit('speaking.done', { response_id: ev.response_id });\n\n const stream = speakerStreams.get(ev.response_id);\n stream?.end();\n });\n this.client.on('response.audio_transcript.delta', ev => {\n this.emit('writing', { text: ev.delta, response_id: ev.response_id, role: 'assistant' });\n });\n this.client.on('response.audio_transcript.done', ev => {\n this.emit('writing', { text: '\\n', response_id: ev.response_id, role: 'assistant' });\n });\n this.client.on('response.text.delta', ev => {\n this.emit('writing', { text: ev.delta, response_id: ev.response_id, role: 'assistant' });\n });\n this.client.on('response.text.done', ev => {\n this.emit('writing', { text: '\\n', response_id: ev.response_id, role: 'assistant' });\n });\n this.client.on('response.done', async ev => {\n await this.handleFunctionCalls(ev);\n this.emit('response.done', ev);\n speakerStreams.delete(ev.response.id);\n });\n this.client.on('error', async ev => {\n this.emit('error', ev);\n });\n }\n\n private async handleFunctionCalls(ev: any) {\n for (const output of ev.response?.output ?? []) {\n if (output.type === 'function_call') {\n await this.handleFunctionCall(output);\n }\n }\n }\n\n private async handleFunctionCall(output: any) {\n try {\n const context = JSON.parse(output.arguments);\n const tool = this.tools?.[output.name];\n if (!tool) {\n console.warn(`Tool \"${output.name}\" not found`);\n return;\n }\n\n if (tool?.execute) {\n this.emit('tool-call-start', {\n toolCallId: output.call_id,\n toolName: output.name,\n toolDescription: tool.description,\n args: context,\n });\n }\n\n const result = await tool?.execute?.(\n { context, runtimeContext: this.runtimeContext },\n {\n toolCallId: output.call_id,\n messages: [],\n },\n );\n\n this.emit('tool-call-result', {\n toolCallId: output.call_id,\n toolName: output.name,\n toolDescription: tool.description,\n args: context,\n result,\n });\n\n this.sendEvent('conversation.item.create', {\n item: {\n type: 'function_call_output',\n call_id: output.call_id,\n output: JSON.stringify(result),\n },\n });\n } catch (e) {\n const err = e as Error;\n console.warn(`Error calling tool \"${output.name}\":`, err.message);\n this.sendEvent('conversation.item.create', {\n item: {\n type: 'function_call_output',\n call_id: output.call_id,\n output: JSON.stringify({ error: err.message }),\n },\n });\n } finally {\n this.sendEvent('response.create', {});\n }\n }\n\n private int16ArrayToBase64(int16Array: Int16Array): string {\n const buffer = new ArrayBuffer(int16Array.length * 2);\n const view = new DataView(buffer);\n for (let i = 0; i < int16Array.length; i++) {\n view.setInt16(i * 2, int16Array[i]!, true);\n }\n const uint8Array = new Uint8Array(buffer);\n let binary = '';\n for (let i = 0; i < uint8Array.length; i++) {\n binary += String.fromCharCode(uint8Array[i]!);\n }\n return btoa(binary);\n }\n\n private sendEvent(type: string, data: any) {\n if (!this.ws || this.ws.readyState !== this.ws.OPEN) {\n this.queue.push({ type: type, ...data });\n } else {\n this.ws?.send(\n JSON.stringify({\n type: type,\n ...data,\n }),\n );\n }\n }\n}\n"]}
|