@mastra/voice-openai-realtime 0.2.4 → 0.3.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +1,23 @@
1
1
 
2
- > @mastra/voice-openai-realtime@0.2.4-alpha.4 build /home/runner/work/mastra/mastra/voice/openai-realtime-api
2
+ > @mastra/voice-openai-realtime@0.3.0-alpha.1 build /home/runner/work/mastra/mastra/voice/openai-realtime-api
3
3
  > tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake
4
4
 
5
5
  CLI Building entry: src/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.4.0
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 8246ms
9
+ TSC ⚡️ Build success in 8301ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
12
  Analysis will use the bundled TypeScript version 5.8.3
13
13
  Writing package typings: /home/runner/work/mastra/mastra/voice/openai-realtime-api/dist/_tsup-dts-rollup.d.ts
14
14
  Analysis will use the bundled TypeScript version 5.8.3
15
15
  Writing package typings: /home/runner/work/mastra/mastra/voice/openai-realtime-api/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 10928ms
16
+ DTS ⚡️ Build success in 10933ms
17
17
  CLI Cleaning output folder
18
18
  ESM Build start
19
19
  CJS Build start
20
- CJS dist/index.cjs 19.08 KB
21
- CJS ⚡️ Build success in 1013ms
22
- ESM dist/index.js 19.03 KB
23
- ESM ⚡️ Build success in 1014ms
20
+ ESM dist/index.js 19.15 KB
21
+ ESM ⚡️ Build success in 965ms
22
+ CJS dist/index.cjs 19.20 KB
23
+ CJS ⚡️ Build success in 969ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  # @mastra/voice-openai-realtime
2
2
 
3
+ ## 0.3.0-alpha.1
4
+
5
+ ### Minor Changes
6
+
7
+ - 83da932: Move @mastra/core to peerdeps
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [b3a3d63]
12
+ - Updated dependencies [344f453]
13
+ - Updated dependencies [0a3ae6d]
14
+ - Updated dependencies [95911be]
15
+ - Updated dependencies [5eb5a99]
16
+ - Updated dependencies [7e632c5]
17
+ - Updated dependencies [1e9fbfa]
18
+ - Updated dependencies [b2ae5aa]
19
+ - Updated dependencies [a7292b0]
20
+ - Updated dependencies [0dcb9f0]
21
+ - @mastra/core@0.10.0-alpha.1
22
+
23
+ ## 0.2.5-alpha.0
24
+
25
+ ### Patch Changes
26
+
27
+ - d3628af: Add realtime context for openai realtime voice
28
+ - Updated dependencies [f53a6ac]
29
+ - Updated dependencies [eabdcd9]
30
+ - Updated dependencies [90be034]
31
+ - Updated dependencies [99f050a]
32
+ - Updated dependencies [d0ee3c6]
33
+ - Updated dependencies [23f258c]
34
+ - Updated dependencies [2672a05]
35
+ - @mastra/core@0.9.5-alpha.0
36
+
3
37
  ## 0.2.4
4
38
 
5
39
  ### Patch Changes
@@ -1,5 +1,6 @@
1
1
  import { MastraVoice } from '@mastra/core/voice';
2
2
  import type { Realtime } from 'openai-realtime-api';
3
+ import type { RuntimeContext } from '@mastra/core/runtime-context';
3
4
  import type { ToolsInput } from '@mastra/core/agent';
4
5
 
5
6
  /**
@@ -51,6 +52,7 @@ export declare class OpenAIRealtimeVoice extends MastraVoice {
51
52
  private debug;
52
53
  private queue;
53
54
  private transcriber;
55
+ private runtimeContext?;
54
56
  /**
55
57
  * Creates a new instance of OpenAIRealtimeVoice.
56
58
  *
@@ -216,7 +218,9 @@ export declare class OpenAIRealtimeVoice extends MastraVoice {
216
218
  * // Now ready for voice interactions
217
219
  * ```
218
220
  */
219
- connect(): Promise<void>;
221
+ connect({ runtimeContext }?: {
222
+ runtimeContext?: RuntimeContext;
223
+ }): Promise<void>;
220
224
  disconnect(): void;
221
225
  /**
222
226
  * Streams audio data in real-time to the OpenAI service.
@@ -315,16 +319,18 @@ export declare class OpenAIRealtimeVoice extends MastraVoice {
315
319
  private sendEvent;
316
320
  }
317
321
 
318
- export declare const transformTools: (tools?: TTools_2) => {
319
- openaiTool: {
320
- type: string;
321
- name: string;
322
- description: string;
323
- parameters: {
324
- [key: string]: any;
325
- };
322
+ declare type ToolDefinition = {
323
+ type: 'function';
324
+ name: string;
325
+ description: string;
326
+ parameters: {
327
+ [key: string]: any;
326
328
  };
327
- execute: (args: any) => Promise<any>;
329
+ };
330
+
331
+ export declare const transformTools: (tools?: TTools_2) => {
332
+ openaiTool: ToolDefinition;
333
+ execute: OpenAIExecuteFunction;
328
334
  }[];
329
335
 
330
336
  declare type TTools = ToolsInput;
@@ -1,5 +1,6 @@
1
1
  import { MastraVoice } from '@mastra/core/voice';
2
2
  import type { Realtime } from 'openai-realtime-api';
3
+ import type { RuntimeContext } from '@mastra/core/runtime-context';
3
4
  import type { ToolsInput } from '@mastra/core/agent';
4
5
 
5
6
  /**
@@ -51,6 +52,7 @@ export declare class OpenAIRealtimeVoice extends MastraVoice {
51
52
  private debug;
52
53
  private queue;
53
54
  private transcriber;
55
+ private runtimeContext?;
54
56
  /**
55
57
  * Creates a new instance of OpenAIRealtimeVoice.
56
58
  *
@@ -216,7 +218,9 @@ export declare class OpenAIRealtimeVoice extends MastraVoice {
216
218
  * // Now ready for voice interactions
217
219
  * ```
218
220
  */
219
- connect(): Promise<void>;
221
+ connect({ runtimeContext }?: {
222
+ runtimeContext?: RuntimeContext;
223
+ }): Promise<void>;
220
224
  disconnect(): void;
221
225
  /**
222
226
  * Streams audio data in real-time to the OpenAI service.
@@ -315,16 +319,18 @@ export declare class OpenAIRealtimeVoice extends MastraVoice {
315
319
  private sendEvent;
316
320
  }
317
321
 
318
- export declare const transformTools: (tools?: TTools_2) => {
319
- openaiTool: {
320
- type: string;
321
- name: string;
322
- description: string;
323
- parameters: {
324
- [key: string]: any;
325
- };
322
+ declare type ToolDefinition = {
323
+ type: 'function';
324
+ name: string;
325
+ description: string;
326
+ parameters: {
327
+ [key: string]: any;
326
328
  };
327
- execute: (args: any) => Promise<any>;
329
+ };
330
+
331
+ export declare const transformTools: (tools?: TTools_2) => {
332
+ openaiTool: ToolDefinition;
333
+ execute: OpenAIExecuteFunction;
328
334
  }[];
329
335
 
330
336
  declare type TTools = ToolsInput;
package/dist/index.cjs CHANGED
@@ -114,6 +114,7 @@ var OpenAIRealtimeVoice = class extends voice.MastraVoice {
114
114
  debug;
115
115
  queue = [];
116
116
  transcriber;
117
+ runtimeContext;
117
118
  /**
118
119
  * Returns a list of available voice speakers.
119
120
  *
@@ -310,9 +311,10 @@ var OpenAIRealtimeVoice = class extends voice.MastraVoice {
310
311
  * // Now ready for voice interactions
311
312
  * ```
312
313
  */
313
- async connect() {
314
+ async connect({ runtimeContext } = {}) {
314
315
  const url = `${this.options.url || DEFAULT_URL}?model=${this.options.model || DEFAULT_MODEL}`;
315
316
  const apiKey = this.options.apiKey || process.env.OPENAI_API_KEY;
317
+ this.runtimeContext = runtimeContext;
316
318
  this.ws = new ws.WebSocket(url, void 0, {
317
319
  headers: {
318
320
  Authorization: "Bearer " + apiKey,
@@ -555,7 +557,7 @@ var OpenAIRealtimeVoice = class extends voice.MastraVoice {
555
557
  });
556
558
  }
557
559
  const result = await tool?.execute?.(
558
- { context },
560
+ { context, runtimeContext: this.runtimeContext },
559
561
  {
560
562
  toolCallId: output.call_id,
561
563
  messages: []
package/dist/index.js CHANGED
@@ -112,6 +112,7 @@ var OpenAIRealtimeVoice = class extends MastraVoice {
112
112
  debug;
113
113
  queue = [];
114
114
  transcriber;
115
+ runtimeContext;
115
116
  /**
116
117
  * Returns a list of available voice speakers.
117
118
  *
@@ -308,9 +309,10 @@ var OpenAIRealtimeVoice = class extends MastraVoice {
308
309
  * // Now ready for voice interactions
309
310
  * ```
310
311
  */
311
- async connect() {
312
+ async connect({ runtimeContext } = {}) {
312
313
  const url = `${this.options.url || DEFAULT_URL}?model=${this.options.model || DEFAULT_MODEL}`;
313
314
  const apiKey = this.options.apiKey || process.env.OPENAI_API_KEY;
315
+ this.runtimeContext = runtimeContext;
314
316
  this.ws = new WebSocket(url, void 0, {
315
317
  headers: {
316
318
  Authorization: "Bearer " + apiKey,
@@ -553,7 +555,7 @@ var OpenAIRealtimeVoice = class extends MastraVoice {
553
555
  });
554
556
  }
555
557
  const result = await tool?.execute?.(
556
- { context },
558
+ { context, runtimeContext: this.runtimeContext },
557
559
  {
558
560
  toolCallId: output.call_id,
559
561
  messages: []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/voice-openai-realtime",
3
- "version": "0.2.4",
3
+ "version": "0.3.0-alpha.1",
4
4
  "description": "Mastra OpenAI Realtime API integration",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -22,8 +22,7 @@
22
22
  "dependencies": {
23
23
  "openai-realtime-api": "^1.0.7",
24
24
  "ws": "^8.18.1",
25
- "zod-to-json-schema": "^3.24.5",
26
- "@mastra/core": "^0.9.4"
25
+ "zod-to-json-schema": "^3.24.5"
27
26
  },
28
27
  "devDependencies": {
29
28
  "@microsoft/api-extractor": "^7.52.5",
@@ -33,7 +32,13 @@
33
32
  "tsup": "^8.4.0",
34
33
  "typescript": "^5.8.3",
35
34
  "vitest": "^2.1.9",
36
- "@internal/lint": "0.0.5"
35
+ "zod": "^3.24.3",
36
+ "@internal/lint": "0.0.5",
37
+ "@mastra/core": "0.10.0-alpha.1"
38
+ },
39
+ "peerDependencies": {
40
+ "@mastra/core": "^0.9.4",
41
+ "zod": "^3.0.0"
37
42
  },
38
43
  "scripts": {
39
44
  "build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake",
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { EventEmitter } from 'events';
2
2
  import { PassThrough } from 'stream';
3
3
  import type { ToolsInput } from '@mastra/core/agent';
4
+ import type { RuntimeContext } from '@mastra/core/runtime-context';
4
5
  import { MastraVoice } from '@mastra/core/voice';
5
6
  import type { Realtime, RealtimeServerEvents } from 'openai-realtime-api';
6
7
  import { WebSocket } from 'ws';
@@ -113,7 +114,7 @@ export class OpenAIRealtimeVoice extends MastraVoice {
113
114
  private debug: boolean;
114
115
  private queue: unknown[] = [];
115
116
  private transcriber: Realtime.AudioTranscriptionModel;
116
-
117
+ private runtimeContext?: RuntimeContext;
117
118
  /**
118
119
  * Creates a new instance of OpenAIRealtimeVoice.
119
120
  *
@@ -363,9 +364,11 @@ export class OpenAIRealtimeVoice extends MastraVoice {
363
364
  * // Now ready for voice interactions
364
365
  * ```
365
366
  */
366
- async connect() {
367
+ async connect({ runtimeContext }: { runtimeContext?: RuntimeContext } = {}) {
367
368
  const url = `${this.options.url || DEFAULT_URL}?model=${this.options.model || DEFAULT_MODEL}`;
368
369
  const apiKey = this.options.apiKey || process.env.OPENAI_API_KEY;
370
+ this.runtimeContext = runtimeContext;
371
+
369
372
  this.ws = new WebSocket(url, undefined, {
370
373
  headers: {
371
374
  Authorization: 'Bearer ' + apiKey,
@@ -634,7 +637,7 @@ export class OpenAIRealtimeVoice extends MastraVoice {
634
637
  }
635
638
 
636
639
  const result = await tool?.execute?.(
637
- { context },
640
+ { context, runtimeContext: this.runtimeContext },
638
641
  {
639
642
  toolCallId: output.call_id,
640
643
  messages: [],
@@ -0,0 +1,119 @@
1
+ import { createTool } from '@mastra/core/tools';
2
+ import { describe, it, expect } from 'vitest';
3
+ import { z } from 'zod';
4
+ import { transformTools } from './utils';
5
+
6
+ // Vitest provides these globals automatically, but we can import them explicitly for clarity
7
+
8
+ describe('transformTools', () => {
9
+ describe('Basic Tool Transformation', () => {
10
+ it('should transform a tool with Zod inputSchema to OpenAI format', () => {
11
+ // Create a test tool with Zod schema
12
+ const tool = createTool({
13
+ id: 'zodTool',
14
+ description: 'A tool with Zod schema',
15
+ inputSchema: z.object({
16
+ name: z.string(),
17
+ age: z.number().optional(),
18
+ }),
19
+ outputSchema: z.string(),
20
+ execute: async ({ context }) => {
21
+ return `Hello, ${context.name}`;
22
+ },
23
+ });
24
+
25
+ // Transform the tool
26
+ const transformedTools = transformTools({
27
+ zodTool: tool,
28
+ });
29
+
30
+ // Assert the transformation results
31
+ expect(transformedTools).toHaveLength(1);
32
+ const { openaiTool } = transformedTools[0];
33
+
34
+ expect(openaiTool).toMatchObject({
35
+ type: 'function',
36
+ name: 'zodTool',
37
+ description: 'A tool with Zod schema',
38
+ parameters: expect.objectContaining({
39
+ type: 'object',
40
+ properties: expect.objectContaining({
41
+ name: expect.objectContaining({ type: 'string' }),
42
+ age: expect.objectContaining({ type: 'number' }),
43
+ }),
44
+ required: ['name'],
45
+ }),
46
+ });
47
+ });
48
+
49
+ it('should transform a tool with JSON schema parameters to OpenAI format', () => {
50
+ // Create a test tool with direct JSON schema
51
+ const tool = {
52
+ id: 'jsonTool',
53
+ description: 'A tool with JSON schema',
54
+ parameters: {
55
+ type: 'object',
56
+ properties: {
57
+ query: { type: 'string' },
58
+ limit: { type: 'integer' },
59
+ },
60
+ required: ['query'],
61
+ },
62
+ execute: async (args: { query: string; limit?: number }) => {
63
+ return `Searched for: ${args.query}`;
64
+ },
65
+ };
66
+
67
+ // Transform the tool
68
+ const transformedTools = transformTools({
69
+ jsonTool: tool,
70
+ });
71
+
72
+ // Assert the transformation results
73
+ expect(transformedTools).toHaveLength(1);
74
+ const { openaiTool } = transformedTools[0];
75
+
76
+ expect(openaiTool).toMatchObject({
77
+ type: 'function',
78
+ name: 'jsonTool',
79
+ description: 'A tool with JSON schema',
80
+ parameters: expect.objectContaining({
81
+ type: 'object',
82
+ properties: expect.objectContaining({
83
+ query: expect.objectContaining({ type: 'string' }),
84
+ limit: expect.objectContaining({ type: 'integer' }),
85
+ }),
86
+ required: ['query'],
87
+ }),
88
+ });
89
+ });
90
+ });
91
+
92
+ describe('Tool Execution Tests', () => {
93
+ it('should create an adapter function for tool execution', async () => {
94
+ // Create a tool that expects context
95
+ const tool = createTool({
96
+ id: 'messageTool',
97
+ description: 'A tool that processes a message',
98
+ inputSchema: z.object({
99
+ message: z.string(),
100
+ }),
101
+ outputSchema: z.string(),
102
+ execute: async ({ context }) => {
103
+ return `Processed: ${context.message}`;
104
+ },
105
+ });
106
+
107
+ // Transform the tool
108
+ const transformedTools = transformTools({
109
+ messageTool: tool,
110
+ });
111
+
112
+ // Execute the transformed tool
113
+ const result = await transformedTools[0].execute({ message: 'Hello' });
114
+
115
+ // Verify the adapter correctly passes the context
116
+ expect(result).toBe('Processed: Hello');
117
+ });
118
+ });
119
+ });
package/src/utils.ts CHANGED
@@ -3,10 +3,18 @@ import type { ToolsInput } from '@mastra/core/agent';
3
3
  import { zodToJsonSchema } from 'zod-to-json-schema';
4
4
 
5
5
  export type OpenAIExecuteFunction = (args: any) => Promise<any>;
6
+ type ToolDefinition = {
7
+ type: 'function';
8
+ name: string;
9
+ description: string;
10
+ parameters: {
11
+ [key: string]: any;
12
+ };
13
+ };
6
14
 
7
15
  type TTools = ToolsInput;
8
16
  export const transformTools = (tools?: TTools) => {
9
- const openaiTools = [];
17
+ const openaiTools: { openaiTool: ToolDefinition; execute: OpenAIExecuteFunction }[] = [];
10
18
  for (const [name, tool] of Object.entries(tools || {})) {
11
19
  let parameters: { [key: string]: any };
12
20
 
@@ -28,7 +36,7 @@ export const transformTools = (tools?: TTools) => {
28
36
  console.warn(`Tool ${name} has neither inputSchema nor parameters, skipping`);
29
37
  continue;
30
38
  }
31
- const openaiTool = {
39
+ const openaiTool: ToolDefinition = {
32
40
  type: 'function',
33
41
  name,
34
42
  description: tool.description || `Tool: ${name}`,