@openai/agents-realtime 0.0.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.
Files changed (100) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -0
  3. package/dist/bundle/openai-realtime-agents.mjs +8777 -0
  4. package/dist/bundle/openai-realtime-agents.umd.js +11 -0
  5. package/dist/clientMessages.d.ts +40 -0
  6. package/dist/clientMessages.js +2 -0
  7. package/dist/clientMessages.js.map +1 -0
  8. package/dist/clientMessages.mjs +2 -0
  9. package/dist/clientMessages.mjs.map +1 -0
  10. package/dist/guardrail.d.ts +32 -0
  11. package/dist/guardrail.js +34 -0
  12. package/dist/guardrail.js.map +1 -0
  13. package/dist/guardrail.mjs +34 -0
  14. package/dist/guardrail.mjs.map +1 -0
  15. package/dist/index.d.ts +18 -0
  16. package/dist/index.js +13 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/index.mjs +13 -0
  19. package/dist/index.mjs.map +1 -0
  20. package/dist/items.d.ts +183 -0
  21. package/dist/items.js +47 -0
  22. package/dist/items.js.map +1 -0
  23. package/dist/items.mjs +47 -0
  24. package/dist/items.mjs.map +1 -0
  25. package/dist/logger.d.ts +2 -0
  26. package/dist/logger.js +4 -0
  27. package/dist/logger.js.map +1 -0
  28. package/dist/logger.mjs +4 -0
  29. package/dist/logger.mjs.map +1 -0
  30. package/dist/metadata.d.ts +9 -0
  31. package/dist/metadata.js +11 -0
  32. package/dist/metadata.js.map +1 -0
  33. package/dist/metadata.mjs +11 -0
  34. package/dist/metadata.mjs.map +1 -0
  35. package/dist/openaiRealtimeBase.d.ts +143 -0
  36. package/dist/openaiRealtimeBase.js +449 -0
  37. package/dist/openaiRealtimeBase.js.map +1 -0
  38. package/dist/openaiRealtimeBase.mjs +449 -0
  39. package/dist/openaiRealtimeBase.mjs.map +1 -0
  40. package/dist/openaiRealtimeEvents.d.ts +3242 -0
  41. package/dist/openaiRealtimeEvents.js +439 -0
  42. package/dist/openaiRealtimeEvents.js.map +1 -0
  43. package/dist/openaiRealtimeEvents.mjs +439 -0
  44. package/dist/openaiRealtimeEvents.mjs.map +1 -0
  45. package/dist/openaiRealtimeWebRtc.d.ts +102 -0
  46. package/dist/openaiRealtimeWebRtc.js +245 -0
  47. package/dist/openaiRealtimeWebRtc.js.map +1 -0
  48. package/dist/openaiRealtimeWebRtc.mjs +245 -0
  49. package/dist/openaiRealtimeWebRtc.mjs.map +1 -0
  50. package/dist/openaiRealtimeWebsocket.d.ts +126 -0
  51. package/dist/openaiRealtimeWebsocket.js +293 -0
  52. package/dist/openaiRealtimeWebsocket.js.map +1 -0
  53. package/dist/openaiRealtimeWebsocket.mjs +293 -0
  54. package/dist/openaiRealtimeWebsocket.mjs.map +1 -0
  55. package/dist/realtimeAgent.d.ts +49 -0
  56. package/dist/realtimeAgent.js +37 -0
  57. package/dist/realtimeAgent.js.map +1 -0
  58. package/dist/realtimeAgent.mjs +37 -0
  59. package/dist/realtimeAgent.mjs.map +1 -0
  60. package/dist/realtimeSession.d.ts +210 -0
  61. package/dist/realtimeSession.js +469 -0
  62. package/dist/realtimeSession.js.map +1 -0
  63. package/dist/realtimeSession.mjs +469 -0
  64. package/dist/realtimeSession.mjs.map +1 -0
  65. package/dist/realtimeSessionEvents.d.ts +118 -0
  66. package/dist/realtimeSessionEvents.js +2 -0
  67. package/dist/realtimeSessionEvents.js.map +1 -0
  68. package/dist/realtimeSessionEvents.mjs +2 -0
  69. package/dist/realtimeSessionEvents.mjs.map +1 -0
  70. package/dist/shims/shims-browser.d.ts +9 -0
  71. package/dist/shims/shims-browser.js +6 -0
  72. package/dist/shims/shims-browser.js.map +1 -0
  73. package/dist/shims/shims-browser.mjs +6 -0
  74. package/dist/shims/shims-browser.mjs.map +1 -0
  75. package/dist/shims/shims-node.d.ts +2 -0
  76. package/dist/shims/shims-node.js +5 -0
  77. package/dist/shims/shims-node.js.map +1 -0
  78. package/dist/shims/shims-node.mjs +5 -0
  79. package/dist/shims/shims-node.mjs.map +1 -0
  80. package/dist/shims/shims.d.ts +1 -0
  81. package/dist/shims/shims.js +2 -0
  82. package/dist/shims/shims.js.map +1 -0
  83. package/dist/shims/shims.mjs +2 -0
  84. package/dist/shims/shims.mjs.map +1 -0
  85. package/dist/transportLayer.d.ts +96 -0
  86. package/dist/transportLayer.js +2 -0
  87. package/dist/transportLayer.js.map +1 -0
  88. package/dist/transportLayer.mjs +2 -0
  89. package/dist/transportLayer.mjs.map +1 -0
  90. package/dist/transportLayerEvents.d.ts +99 -0
  91. package/dist/transportLayerEvents.js +2 -0
  92. package/dist/transportLayerEvents.js.map +1 -0
  93. package/dist/transportLayerEvents.mjs +2 -0
  94. package/dist/transportLayerEvents.mjs.map +1 -0
  95. package/dist/utils.d.ts +61 -0
  96. package/dist/utils.js +183 -0
  97. package/dist/utils.js.map +1 -0
  98. package/dist/utils.mjs +183 -0
  99. package/dist/utils.mjs.map +1 -0
  100. package/package.json +77 -0
@@ -0,0 +1,37 @@
1
+ import { Agent, } from '@openai/agents-core';
2
+ /**
3
+ * A specialized agent instance that is meant to be used within a `RealtimeSession` to build
4
+ * voice agents. Due to the nature of this agent, some configuration options are not supported
5
+ * that are supported by regular `Agent` instances. For example:
6
+ * - `model` choice is not supported as all RealtimeAgents will be handled by the same model within
7
+ * a `RealtimeSession`
8
+ * - `modelSettings` is not supported as all RealtimeAgents will be handled by the same model within
9
+ * a `RealtimeSession`
10
+ * - `outputType` is not supported as RealtimeAgents do not support structured outputs
11
+ * - `toolUseBehavior` is not supported as all RealtimeAgents will be handled by the same model within
12
+ * a `RealtimeSession`
13
+ * - `voice` can be configured on an `Agent` level however it cannot be changed after the first
14
+ * agent within a `RealtimeSession` spoke
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const agent = new RealtimeAgent({
19
+ * name: 'my-agent',
20
+ * instructions: 'You are a helpful assistant that can answer questions and help with tasks.',
21
+ * })
22
+ *
23
+ * const session = new RealtimeSession(agent);
24
+ * ```
25
+ */
26
+ export class RealtimeAgent extends Agent {
27
+ /**
28
+ * The voice intended to be used by the agent. If another agent already spoke during the
29
+ * RealtimeSession, changing the voice during a handoff will fail.
30
+ */
31
+ voice;
32
+ constructor(config) {
33
+ super(config);
34
+ this.voice = config.voice ?? 'ash';
35
+ }
36
+ }
37
+ //# sourceMappingURL=realtimeAgent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"realtimeAgent.js","sourceRoot":"","sources":["../src/realtimeAgent.ts"],"names":[],"mappings":"OAAO,EACL,KAAK,GAKN,MAAM,qBAAqB;AAiC5B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,aAAyC,SAAQ,KAG7D;IACC;;;OAGG;IACM,KAAK,CAAS;IAEvB,YAAY,MAA4C;QACtD,KAAK,CACH,MAAuE,CACxE,CAAC;QACF,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,KAAK,CAAC;IACrC,CAAC;CACF"}
@@ -0,0 +1,37 @@
1
+ import { Agent, } from '@openai/agents-core';
2
+ /**
3
+ * A specialized agent instance that is meant to be used within a `RealtimeSession` to build
4
+ * voice agents. Due to the nature of this agent, some configuration options are not supported
5
+ * that are supported by regular `Agent` instances. For example:
6
+ * - `model` choice is not supported as all RealtimeAgents will be handled by the same model within
7
+ * a `RealtimeSession`
8
+ * - `modelSettings` is not supported as all RealtimeAgents will be handled by the same model within
9
+ * a `RealtimeSession`
10
+ * - `outputType` is not supported as RealtimeAgents do not support structured outputs
11
+ * - `toolUseBehavior` is not supported as all RealtimeAgents will be handled by the same model within
12
+ * a `RealtimeSession`
13
+ * - `voice` can be configured on an `Agent` level however it cannot be changed after the first
14
+ * agent within a `RealtimeSession` spoke
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * const agent = new RealtimeAgent({
19
+ * name: 'my-agent',
20
+ * instructions: 'You are a helpful assistant that can answer questions and help with tasks.',
21
+ * })
22
+ *
23
+ * const session = new RealtimeSession(agent);
24
+ * ```
25
+ */
26
+ export class RealtimeAgent extends Agent {
27
+ /**
28
+ * The voice intended to be used by the agent. If another agent already spoke during the
29
+ * RealtimeSession, changing the voice during a handoff will fail.
30
+ */
31
+ voice;
32
+ constructor(config) {
33
+ super(config);
34
+ this.voice = config.voice ?? 'ash';
35
+ }
36
+ }
37
+ //# sourceMappingURL=realtimeAgent.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"realtimeAgent.mjs","sourceRoot":"","sources":["../src/realtimeAgent.ts"],"names":[],"mappings":"OAAO,EACL,KAAK,GAKN,MAAM,qBAAqB;AAiC5B;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,OAAO,aAAyC,SAAQ,KAG7D;IACC;;;OAGG;IACM,KAAK,CAAS;IAEvB,YAAY,MAA4C;QACtD,KAAK,CACH,MAAuE,CACxE,CAAC;QACF,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,KAAK,CAAC;IACrC,CAAC;CACF"}
@@ -0,0 +1,210 @@
1
+ import { RunContext, Usage, RunToolApprovalItem } from '@openai/agents-core';
2
+ import { RuntimeEventEmitter } from '@openai/agents-core/_shims';
3
+ import type { RealtimeSessionConfig, RealtimeUserInput } from './clientMessages';
4
+ import { RealtimeOutputGuardrail, RealtimeOutputGuardrailSettings } from './guardrail';
5
+ import { RealtimeItem } from './items';
6
+ import { OpenAIRealtimeModels } from './openaiRealtimeBase';
7
+ import { RealtimeAgent } from './realtimeAgent';
8
+ import { RealtimeSessionEventTypes } from './realtimeSessionEvents';
9
+ import type { ApiKey, RealtimeTransportLayer } from './transportLayer';
10
+ /**
11
+ * The context data for a realtime session. This is the context data that is passed to the agent.
12
+ * The RealtimeSession will automatically add the current snapshot of the history to the context.
13
+ */
14
+ export type RealtimeContextData<TContext = unknown> = TContext & {
15
+ history: RealtimeItem[];
16
+ };
17
+ export type RealtimeSessionOptions<TContext = unknown> = {
18
+ /**
19
+ * The API key to use for the connection. Pass a function to lazily load the API key
20
+ */
21
+ apiKey: ApiKey;
22
+ /**
23
+ * The transport layer to use.
24
+ */
25
+ transport: 'webrtc' | 'websocket' | RealtimeTransportLayer;
26
+ /**
27
+ * The model to use.
28
+ */
29
+ model?: OpenAIRealtimeModels | (string & {});
30
+ /**
31
+ * Additional context to pass to the agent
32
+ */
33
+ context?: TContext;
34
+ /**
35
+ * Any output guardrails to apply to agent output in parallel
36
+ */
37
+ outputGuardrails?: RealtimeOutputGuardrail[];
38
+ /**
39
+ * Configure the behavior of your guardrails
40
+ */
41
+ outputGuardrailSettings?: RealtimeOutputGuardrailSettings;
42
+ /**
43
+ * Additional session config options. Overrides default client options.
44
+ */
45
+ config?: Partial<RealtimeSessionConfig>;
46
+ /**
47
+ * Whether the history copy should include a local copy of the audio data. By default it is not
48
+ * included in the history to save runtime memory on the client. If you wish to keep this data
49
+ * you can enable this option.
50
+ */
51
+ historyStoreAudio?: boolean;
52
+ /**
53
+ * Whether tracing is disabled for this session. If disabled, we will not trace the agent run.
54
+ */
55
+ tracingDisabled?: boolean;
56
+ /**
57
+ * A group identifier to use for tracing, to link multiple traces together. For example, if you
58
+ * want to connect your RealtimeSession traces with those of a backend text-based agent run.
59
+ */
60
+ groupId?: string;
61
+ /**
62
+ * An optional dictionary of additional metadata to include with the trace.
63
+ */
64
+ traceMetadata?: Record<string, any>;
65
+ /**
66
+ * The workflow name to use for tracing.
67
+ */
68
+ workflowName?: string;
69
+ };
70
+ export type RealtimeSessionConnectOptions = {
71
+ /**
72
+ * The API key to use for the connection. Pass a function to lazily load the API key. Overrides
73
+ * default client options.
74
+ */
75
+ apiKey: string | (() => string | Promise<string>);
76
+ /**
77
+ * The model to use for the connection.
78
+ */
79
+ model?: OpenAIRealtimeModels | (string & {});
80
+ /**
81
+ * The URL to use for the connection.
82
+ */
83
+ url?: string;
84
+ };
85
+ /**
86
+ * A `RealtimeSession` is the corner piece of building Voice Agents. It's the equivalent of a
87
+ * Runner in text-based agents except that it automatically handles multiple turns by maintaining a
88
+ * connection with the underlying transport layer.
89
+ *
90
+ * The session handles managing the local history copy, executes tools, runs output guardrails, and
91
+ * facilities handoffs.
92
+ *
93
+ * The actual audio handling and generation of model responses is handled by the underlying
94
+ * transport layer. By default if you are using a browser with WebRTC support, the session will
95
+ * automatically use the WebRTC version of the OpenAI Realtime API. On the server or if you pass
96
+ * `websocket` as the transport layer, the session will establish a connection using WebSockets.
97
+ *
98
+ * In the case of WebRTC, in the browser, the transport layer will also automatically configure the
99
+ * microphone and audio output to be used by the session.
100
+ *
101
+ * You can also create a transport layer instance yourself and pass it in to have more control over
102
+ * the configuration or even extend the existing ones. Check out the `TwilioRealtimeTransportLayer`
103
+ * for an example of how to create a custom transport layer.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * const agent = new RealtimeAgent({
108
+ * name: 'my-agent',
109
+ * instructions: 'You are a helpful assistant that can answer questions and help with tasks.',
110
+ * })
111
+ *
112
+ * const session = new RealtimeSession(agent);
113
+ * session.connect({
114
+ * apiKey: 'your-api-key',
115
+ * });
116
+ * ```
117
+ */
118
+ export declare class RealtimeSession<TBaseContext = unknown> extends RuntimeEventEmitter<RealtimeSessionEventTypes<TBaseContext>> {
119
+ #private;
120
+ readonly initialAgent: RealtimeAgent<TBaseContext> | RealtimeAgent<RealtimeContextData<TBaseContext>>;
121
+ readonly options: Partial<RealtimeSessionOptions<TBaseContext>>;
122
+ constructor(initialAgent: RealtimeAgent<TBaseContext> | RealtimeAgent<RealtimeContextData<TBaseContext>>, options?: Partial<RealtimeSessionOptions<TBaseContext>>);
123
+ /**
124
+ * The transport layer used by the session.
125
+ */
126
+ get transport(): RealtimeTransportLayer;
127
+ /**
128
+ * The current agent in the session.
129
+ */
130
+ get currentAgent(): RealtimeAgent<TBaseContext> | RealtimeAgent<RealtimeContextData<TBaseContext>>;
131
+ /**
132
+ * The current usage of the session.
133
+ */
134
+ get usage(): Usage;
135
+ /**
136
+ * The current context of the session.
137
+ */
138
+ get context(): RunContext<RealtimeContextData<TBaseContext>>;
139
+ /**
140
+ * Whether the session is muted. Might be `null` if the underlying transport layer does not
141
+ * support muting.
142
+ */
143
+ get muted(): boolean | null;
144
+ /**
145
+ * The history of the session.
146
+ */
147
+ get history(): RealtimeItem[];
148
+ updateAgent(newAgent: RealtimeAgent<TBaseContext>): Promise<RealtimeAgent<TBaseContext>>;
149
+ /**
150
+ * Connect to the session. This will establish the connection to the underlying transport layer
151
+ * and start the session.
152
+ *
153
+ * After connecting, the session will also emit a `history_updated` event with an empty history.
154
+ *
155
+ * @param options - The options for the connection.
156
+ */
157
+ connect(options: RealtimeSessionConnectOptions): Promise<void>;
158
+ /**
159
+ * Update the history of the session.
160
+ * @param newHistory - The new history to set.
161
+ */
162
+ updateHistory(newHistory: RealtimeItem[] | ((history: RealtimeItem[]) => RealtimeItem[])): void;
163
+ /**
164
+ * Send a message to the session.
165
+ * @param message - The message to send.
166
+ * @param otherEventData - Additional event data to send.
167
+ */
168
+ sendMessage(message: RealtimeUserInput, otherEventData?: Record<string, any>): void;
169
+ /**
170
+ * Mute the session.
171
+ * @param muted - Whether to mute the session.
172
+ */
173
+ mute(muted: boolean): void;
174
+ /**
175
+ * Disconnect from the session.
176
+ */
177
+ close(): void;
178
+ /**
179
+ * Send audio to the session.
180
+ * @param audio - The audio to send.
181
+ * @param options - Additional options.
182
+ * @param options.commit - Whether to finish the turn with this audio.
183
+ */
184
+ sendAudio(audio: ArrayBuffer, options?: {
185
+ commit?: boolean;
186
+ }): void;
187
+ /**
188
+ * Interrupt the session artificially for example if you want to build a "stop talking"
189
+ * button.
190
+ */
191
+ interrupt(): void;
192
+ /**
193
+ * Approve a tool call. This will also trigger the tool call to the agent.
194
+ * @param approvalItem - The approval item to approve.
195
+ * @param options - Additional options.
196
+ * @param options.alwaysApprove - Whether to always approve the tool call.
197
+ */
198
+ approve(approvalItem: RunToolApprovalItem, options?: {
199
+ alwaysApprove?: boolean;
200
+ }): Promise<void>;
201
+ /**
202
+ * Reject a tool call. This will also trigger the tool call to the agent.
203
+ * @param approvalItem - The approval item to reject.
204
+ * @param options - Additional options.
205
+ * @param options.alwaysReject - Whether to always reject the tool call.
206
+ */
207
+ reject(approvalItem: RunToolApprovalItem, options?: {
208
+ alwaysReject?: boolean;
209
+ }): Promise<void>;
210
+ }