@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.
94 lines • 3.64 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.responseDoneEventSchema = void 0;
exports.parseRealtimeEvent = parseRealtimeEvent;
const agents_core_1 = require("@openai/agents-core");
const zod_1 = require("zod");
const envelope = zod_1.z.object({ type: zod_1.z.string() }).passthrough();
const itemEvent = zod_1.z.object({ item: zod_1.z.object({}).passthrough() }).passthrough();
const responseEvent = zod_1.z
.object({ response: zod_1.z.object({}).passthrough() })
.passthrough();
const sessionEvent = zod_1.z
.object({ session: zod_1.z.object({}).passthrough() })
.passthrough();
const itemIdEvent = zod_1.z.object({ item_id: zod_1.z.string() }).passthrough();
const textDeltaEvent = zod_1.z
.object({
delta: zod_1.z.string(),
item_id: zod_1.z.string(),
response_id: zod_1.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: zod_1.z.string() }),
'response.output_item.done': itemEvent.extend({ response_id: zod_1.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': zod_1.z
.object({ item_id: zod_1.z.string().optional() })
.passthrough(),
'response.output_text.delta': textDeltaEvent,
'response.output_audio_transcript.delta': textDeltaEvent,
'response.output_audio.delta': textDeltaEvent.extend({
content_index: zod_1.z.number(),
}),
};
// Accounting requires numeric counters, but provider details remain opaque.
exports.responseDoneEventSchema = zod_1.z
.object({
response: zod_1.z
.object({
id: zod_1.z.string().nullish(),
output: zod_1.z.array(zod_1.z.any()).nullish(),
usage: zod_1.z
.object({
input_tokens: zod_1.z.number().optional(),
output_tokens: zod_1.z.number().optional(),
input_token_details: agents_core_1.protocol.TokenDetails.nullish(),
output_token_details: agents_core_1.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.
*/
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.js.map