UNPKG

@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.

82 lines • 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OpenAIRealtimeSIP = void 0; const agents_core_1 = require("@openai/agents-core"); const openaiRealtimeWebsocket_1 = require("./openaiRealtimeWebsocket.js"); const realtimeSession_1 = require("./realtimeSession.js"); const SIP_UNSUPPORTED_TURN_DETECTION_FIELDS = [ ['threshold', 'threshold'], ['prefix_padding_ms', 'prefixPaddingMs/prefix_padding_ms'], ['silence_duration_ms', 'silenceDurationMs/silence_duration_ms'], ]; function formatFieldList(fields) { if (fields.length <= 1) { return fields[0] ?? ''; } if (fields.length === 2) { return `${fields[0]} and ${fields[1]}`; } return `${fields.slice(0, -1).join(', ')}, and ${fields.at(-1)}`; } /** * Transport layer that connects to an existing SIP-initiated Realtime call via call ID. */ class OpenAIRealtimeSIP extends openaiRealtimeWebsocket_1.OpenAIRealtimeWebSocket { constructor(options = {}) { super(options); } /** * Build the initial session payload for a SIP-attached session, matching the config that a RealtimeSession would send on connect. * * This enables SIP deployments to accept an incoming call with a payload that already reflects * the active agent's instructions, tools, prompt, and tracing metadata without duplicating the * session logic outside of the SDK. The returned object structurally matches the REST * `CallAcceptParams` interface, so it can be forwarded directly to * `openai.realtime.calls.accept(...)`. * * @param agent - The starting agent used to seed the session instructions, tools, and prompt. * @param options - Optional session options that mirror the ones passed to the RealtimeSession constructor. * @param overrides - Additional config overrides applied on top of the session options. */ static async buildInitialConfig(agent, options = {}, overrides = {}) { const sessionConfig = await realtimeSession_1.RealtimeSession.computeInitialSessionConfig(agent, options, overrides); const transport = new OpenAIRealtimeSIP(); const payload = transport.buildSessionPayload(sessionConfig); OpenAIRealtimeSIP.assertSupportedInitialConfigPayload(payload); return payload; } sendAudio(_audio, _options = {}) { // SIP integrations stream audio to OpenAI directly through the telephony provider, so the // transport deliberately prevents userland code from sending duplicate buffers. throw new Error('OpenAIRealtimeSIP does not support sending audio buffers; audio is handled by the SIP call.'); } interrupt(cancelOngoingResponse = true) { if (this.status !== 'connected') { return; } if (cancelOngoingResponse) { this._cancelResponse(); } // SIP playback and its truncation boundary are owned by the server. this.sendEvent({ type: 'output_audio_buffer.clear' }); } async connect(options) { if (!options.callId) { throw new agents_core_1.UserError('OpenAIRealtimeSIP requires `callId` in the connect options.'); } await super.connect(options); } static assertSupportedInitialConfigPayload(payload) { const turnDetection = payload.audio?.input?.turn_detection; if (!turnDetection || typeof turnDetection !== 'object') { return; } const unsupportedFields = SIP_UNSUPPORTED_TURN_DETECTION_FIELDS.flatMap(([key, label]) => typeof turnDetection[key] === 'undefined' ? [] : [label]); if (unsupportedFields.length === 0) { return; } throw new agents_core_1.UserError(`OpenAIRealtimeSIP.buildInitialConfig() does not support SIP turn-detection fields ${formatFieldList(unsupportedFields)}. The Realtime Calls API rejects these session.audio.input.turn_detection properties for SIP sessions, so remove them before accepting the call.`); } } exports.OpenAIRealtimeSIP = OpenAIRealtimeSIP; //# sourceMappingURL=openaiRealtimeSip.js.map