@charivo/realtime 0.8.4 → 0.10.0

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.
@@ -3,7 +3,7 @@
3
3
  var core = require('@charivo/core');
4
4
  var agentsRealtime = require('@openai/agents-realtime');
5
5
 
6
- // src/remote/index.ts
6
+ // src/remote/client.ts
7
7
 
8
8
  // src/internal/microphone.ts
9
9
  var DEFAULT_MICROPHONE_CONSTRAINTS = {
@@ -120,14 +120,129 @@ async function fetchWithTimeout(input, init, timeoutMessage, timeoutMs = DEFAULT
120
120
  function isAbortError(error) {
121
121
  return error instanceof DOMException && error.name === "AbortError" || error instanceof Error && error.name === "AbortError";
122
122
  }
123
+
124
+ // src/instructions.ts
125
+ var DEFAULT_REALTIME_AGENT_INSTRUCTIONS = `
126
+ You are speaking in a realtime voice conversation.
127
+ Respond naturally and keep replies concise enough for spoken delivery.
128
+ Use available tools proactively when they make the conversation clearer or more present.
129
+ Let each tool call earn its place; don't chain rapid-fire actions just to look responsive.
130
+ Never break character. Never refer to yourself as an AI, model, or assistant.
131
+ Speak only the words you want heard aloud. Use tools for actions, but never mention tool calls, tool names, tool arguments, or bracketed/parenthetical action notes. If a tool is unavailable, omit the action instead of narrating it.
132
+ `.trim();
133
+
134
+ // src/openai/defaults.ts
135
+ var DEFAULT_OPENAI_REALTIME_MODEL = "gpt-realtime-mini";
136
+ var DEFAULT_OPENAI_REALTIME_VOICE = "marin";
137
+ var DEFAULT_OPENAI_REALTIME_AGENT_INSTRUCTIONS = DEFAULT_REALTIME_AGENT_INSTRUCTIONS;
138
+
139
+ // src/openai-agents/dev-bootstrap.ts
140
+ var CLIENT_SECRETS_URL = "https://api.openai.com/v1/realtime/client_secrets";
141
+ function toClientSecretsSession(session) {
142
+ const audio = {
143
+ output: {
144
+ voice: session.voice ?? DEFAULT_OPENAI_REALTIME_VOICE
145
+ }
146
+ };
147
+ const transcription = session.inputAudioTranscription;
148
+ if (transcription !== void 0) {
149
+ if (transcription.enabled === false) {
150
+ audio.input = { transcription: null };
151
+ } else if (transcription.model !== void 0) {
152
+ audio.input = { transcription: { model: transcription.model } };
153
+ }
154
+ }
155
+ const payload = {
156
+ type: "realtime",
157
+ model: session.model ?? DEFAULT_OPENAI_REALTIME_MODEL,
158
+ audio,
159
+ tool_choice: session.toolChoice ?? "auto"
160
+ };
161
+ if (session.instructions) {
162
+ payload.instructions = session.instructions;
163
+ }
164
+ if (session.temperature !== void 0) {
165
+ payload.temperature = session.temperature;
166
+ }
167
+ if (session.maxTokens !== void 0) {
168
+ payload.max_response_output_tokens = session.maxTokens;
169
+ }
170
+ if (session.tools?.length) {
171
+ payload.tools = session.tools;
172
+ }
173
+ return payload;
174
+ }
175
+ function extractClientSecret(payload) {
176
+ if (!isRecord(payload)) {
177
+ return null;
178
+ }
179
+ if (typeof payload.client_secret === "string") {
180
+ return payload.client_secret;
181
+ }
182
+ if (isRecord(payload.client_secret) && typeof payload.client_secret.value === "string") {
183
+ return payload.client_secret.value;
184
+ }
185
+ if (typeof payload.value === "string") {
186
+ return payload.value;
187
+ }
188
+ return null;
189
+ }
190
+ function createOpenAIRealtimeDevBootstrap(apiKey) {
191
+ return async (request) => {
192
+ const response = await fetchWithTimeout(
193
+ CLIENT_SECRETS_URL,
194
+ {
195
+ method: "POST",
196
+ headers: {
197
+ Authorization: `Bearer ${apiKey}`,
198
+ "Content-Type": "application/json"
199
+ },
200
+ body: JSON.stringify({
201
+ session: toClientSecretsSession(request.session)
202
+ })
203
+ },
204
+ `Realtime client secret request timed out after ${DEFAULT_REQUEST_TIMEOUT_MS}ms`
205
+ );
206
+ if (!response.ok) {
207
+ const errorText = await response.text();
208
+ throw new core.CharivoProviderError(
209
+ `Failed to mint OpenAI realtime client secret: ${errorText}`
210
+ );
211
+ }
212
+ let body;
213
+ try {
214
+ body = await response.json();
215
+ } catch {
216
+ throw new core.CharivoProviderError(
217
+ "Invalid client secret response: response body could not be parsed as JSON"
218
+ );
219
+ }
220
+ const clientSecret = extractClientSecret(body);
221
+ if (!clientSecret) {
222
+ throw new core.CharivoProviderError(
223
+ "Invalid client secret response: client_secret missing or empty"
224
+ );
225
+ }
226
+ return {
227
+ adapter: core.OPENAI_REALTIME_AGENTS_ADAPTER,
228
+ transport: "webrtc",
229
+ clientSecret
230
+ };
231
+ };
232
+ }
233
+
234
+ // src/openai-agents/bootstrap.ts
123
235
  async function getOpenAIRealtimeAgentsBootstrap(options, request) {
124
236
  if (options.sessionBootstrap) {
125
237
  return options.sessionBootstrap(request);
126
238
  }
127
239
  const apiEndpoint = options.apiEndpoint;
240
+ if (!apiEndpoint && options.apiKey) {
241
+ return createOpenAIRealtimeDevBootstrap(options.apiKey)(request);
242
+ }
128
243
  if (!apiEndpoint) {
129
244
  throw new core.CharivoStateError(
130
- "OpenAI agents realtime client requires apiEndpoint or sessionBootstrap"
245
+ "OpenAI agents realtime client requires sessionBootstrap, apiEndpoint, or apiKey"
131
246
  );
132
247
  }
133
248
  const response = await fetchWithTimeout(
@@ -289,21 +404,6 @@ var LipSyncAnalyzer = class {
289
404
  }
290
405
  };
291
406
 
292
- // src/instructions.ts
293
- var DEFAULT_REALTIME_AGENT_INSTRUCTIONS = `
294
- You are speaking in a realtime voice conversation.
295
- Respond naturally and keep replies concise enough for spoken delivery.
296
- Use available tools proactively when they make the conversation clearer or more present.
297
- Let each tool call earn its place; don't chain rapid-fire actions just to look responsive.
298
- Never break character. Never refer to yourself as an AI, model, or assistant.
299
- Speak only the words you want heard aloud. Use tools for actions, but never mention tool calls, tool names, tool arguments, or bracketed/parenthetical action notes. If a tool is unavailable, omit the action instead of narrating it.
300
- `.trim();
301
-
302
- // src/openai/defaults.ts
303
- var DEFAULT_OPENAI_REALTIME_MODEL = "gpt-realtime-mini";
304
- var DEFAULT_OPENAI_REALTIME_VOICE = "marin";
305
- var DEFAULT_OPENAI_REALTIME_AGENT_INSTRUCTIONS = DEFAULT_REALTIME_AGENT_INSTRUCTIONS;
306
-
307
407
  // src/openai-agents/session-config.ts
308
408
  function resolveInstructions(config) {
309
409
  return config?.instructions ?? DEFAULT_OPENAI_REALTIME_AGENT_INSTRUCTIONS;
@@ -1580,7 +1680,7 @@ function toOpenAIRealtimeSessionUpdate(config) {
1580
1680
  return session;
1581
1681
  }
1582
1682
 
1583
- // src/remote/index.ts
1683
+ // src/remote/client.ts
1584
1684
  var DEFAULT_REMOTE_REALTIME_ADAPTERS = {
1585
1685
  [core.OPENAI_REALTIME_AGENTS_ADAPTER]: (options) => createOpenAIRealtimeAgentsClient({
1586
1686
  debug: options.debug,
@@ -1710,12 +1810,13 @@ var RemoteRealtimeClient = class {
1710
1810
  return bootstrap;
1711
1811
  }
1712
1812
  };
1813
+
1814
+ // src/remote/index.ts
1713
1815
  function createRemoteRealtimeClient(config) {
1714
1816
  return new RemoteRealtimeClient(config);
1715
1817
  }
1716
1818
 
1717
1819
  exports.DEFAULT_REMOTE_REALTIME_ADAPTERS = DEFAULT_REMOTE_REALTIME_ADAPTERS;
1718
- exports.RemoteRealtimeClient = RemoteRealtimeClient;
1719
1820
  exports.createRemoteRealtimeClient = createRemoteRealtimeClient;
1720
1821
  //# sourceMappingURL=index.js.map
1721
1822
  //# sourceMappingURL=index.js.map