@openai/agents-realtime
Version:
The OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows. This package contains the logic for building realtime voice agents on the server or in the browser.
90 lines • 3.29 kB
JavaScript
import { protocol } from '@openai/agents-core';
import { z } from 'zod';
const envelope = z.object({ type: z.string() }).passthrough();
const itemEvent = z.object({ item: z.object({}).passthrough() }).passthrough();
const responseEvent = z
.object({ response: z.object({}).passthrough() })
.passthrough();
const sessionEvent = z
.object({ session: z.object({}).passthrough() })
.passthrough();
const itemIdEvent = z.object({ item_id: z.string() }).passthrough();
const textDeltaEvent = z
.object({
delta: z.string(),
item_id: z.string(),
response_id: z.string(),
})
.passthrough();
// These are handler preconditions, not a second copy of the provider schema.
// Never validate passive metadata, status enums, or the complete event union.
const handlerInputs = {
'conversation.item.added': itemEvent,
'conversation.item.done': itemEvent,
'conversation.item.retrieved': itemEvent,
'response.output_item.added': itemEvent.extend({ response_id: z.string() }),
'response.output_item.done': itemEvent.extend({ response_id: z.string() }),
'response.created': responseEvent,
'response.done': responseEvent,
'session.created': sessionEvent,
'session.updated': sessionEvent,
'conversation.item.deleted': itemIdEvent,
'conversation.item.truncated': itemIdEvent,
'conversation.item.input_audio_transcription.completed': itemIdEvent,
'response.mcp_call.in_progress': itemIdEvent,
'mcp_list_tools.in_progress': z
.object({ item_id: z.string().optional() })
.passthrough(),
'response.output_text.delta': textDeltaEvent,
'response.output_audio_transcript.delta': textDeltaEvent,
'response.output_audio.delta': textDeltaEvent.extend({
content_index: z.number(),
}),
};
// Accounting requires numeric counters, but provider details remain opaque.
export const responseDoneEventSchema = z
.object({
response: z
.object({
id: z.string().nullish(),
output: z.array(z.any()).nullish(),
usage: z
.object({
input_tokens: z.number().optional(),
output_tokens: z.number().optional(),
input_token_details: protocol.TokenDetails.nullish(),
output_token_details: protocol.TokenDetails.nullish(),
})
.nullish(),
})
.passthrough(),
})
.passthrough();
/**
* Decode the provider envelope and check only handler prerequisites. Unknown
* events and extension fields remain available to listeners. SDK types describe
* known payloads; they are not a runtime allowlist of server events.
*/
export function parseRealtimeEvent(event) {
let raw;
try {
raw = JSON.parse(event.data.toString());
}
catch {
return { data: null, raw: null };
}
const parsed = envelope.safeParse(raw);
if (!parsed.success)
return { data: null, raw: null };
const payload = parsed.data;
const guard = Object.prototype.hasOwnProperty.call(handlerInputs, payload.type)
? handlerInputs[payload.type]
: undefined;
return {
data: guard && !guard.safeParse(payload).success
? null
: payload,
raw: payload,
};
}
//# sourceMappingURL=openaiRealtimeEvents.mjs.map