@livekit/agents-plugin-openai 1.0.30 → 1.0.32
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.
- package/dist/realtime/api_proto.cjs.map +1 -1
- package/dist/realtime/api_proto.d.cts +50 -12
- package/dist/realtime/api_proto.d.ts +50 -12
- package/dist/realtime/api_proto.d.ts.map +1 -1
- package/dist/realtime/api_proto.js.map +1 -1
- package/dist/realtime/index.cjs +19 -0
- package/dist/realtime/index.cjs.map +1 -1
- package/dist/realtime/index.d.cts +1 -0
- package/dist/realtime/index.d.ts +1 -0
- package/dist/realtime/index.d.ts.map +1 -1
- package/dist/realtime/index.js +4 -0
- package/dist/realtime/index.js.map +1 -1
- package/dist/realtime/realtime_model.cjs +69 -33
- package/dist/realtime/realtime_model.cjs.map +1 -1
- package/dist/realtime/realtime_model.d.cts +14 -6
- package/dist/realtime/realtime_model.d.ts +14 -6
- package/dist/realtime/realtime_model.d.ts.map +1 -1
- package/dist/realtime/realtime_model.js +69 -33
- package/dist/realtime/realtime_model.js.map +1 -1
- package/dist/realtime/realtime_model_beta.cjs +1300 -0
- package/dist/realtime/realtime_model_beta.cjs.map +1 -0
- package/dist/realtime/realtime_model_beta.d.cts +165 -0
- package/dist/realtime/realtime_model_beta.d.ts +165 -0
- package/dist/realtime/realtime_model_beta.d.ts.map +1 -0
- package/dist/realtime/realtime_model_beta.js +1280 -0
- package/dist/realtime/realtime_model_beta.js.map +1 -0
- package/package.json +5 -5
- package/src/realtime/api_proto.ts +76 -17
- package/src/realtime/index.ts +1 -0
- package/src/realtime/realtime_model.ts +86 -49
- package/src/realtime/realtime_model_beta.ts +1665 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/realtime/realtime_model_beta.ts"],"sourcesContent":["// SPDX-FileCopyrightText: 2024 LiveKit, Inc.\n//\n// SPDX-License-Identifier: Apache-2.0\nimport type { metrics } from '@livekit/agents';\nimport {\n type APIConnectOptions,\n APIConnectionError,\n APIError,\n AudioByteStream,\n DEFAULT_API_CONNECT_OPTIONS,\n Future,\n Queue,\n Task,\n cancelAndWait,\n delay,\n isAPIError,\n llm,\n log,\n shortuuid,\n stream,\n} from '@livekit/agents';\nimport { Mutex } from '@livekit/mutex';\nimport type { AudioResampler } from '@livekit/rtc-node';\nimport { AudioFrame, combineAudioFrames } from '@livekit/rtc-node';\nimport { type MessageEvent, WebSocket } from 'ws';\nimport * as api_proto from './api_proto.js';\n\n// if LK_OPENAI_DEBUG convert it to a number, otherwise set it to 0\nconst lkOaiDebug = process.env.LK_OPENAI_DEBUG ? Number(process.env.LK_OPENAI_DEBUG) : 0;\n\nconst SAMPLE_RATE = 24000;\nconst NUM_CHANNELS = 1;\nconst BASE_URL = 'https://api.openai.com/v1';\n\nconst MOCK_AUDIO_ID_PREFIX = 'lk_mock_audio_item_';\n\ntype Modality = 'text' | 'audio';\n\ninterface RealtimeOptions {\n model: api_proto.Model;\n voice: api_proto.Voice;\n temperature: number;\n toolChoice?: llm.ToolChoice;\n inputAudioTranscription?: api_proto.InputAudioTranscription | null;\n // TODO(shubhra): add inputAudioNoiseReduction\n turnDetection?: api_proto.TurnDetectionType | null;\n maxResponseOutputTokens?: number | 'inf';\n speed?: number;\n // TODO(shubhra): add openai tracing options\n apiKey?: string;\n baseURL: string;\n isAzure: boolean;\n azureDeployment?: string;\n entraToken?: string;\n apiVersion?: string;\n maxSessionDuration: number;\n // reset the connection after this many seconds if provided\n connOptions: APIConnectOptions;\n modalities: Modality[];\n}\n\ninterface MessageGeneration {\n messageId: string;\n textChannel: stream.StreamChannel<string>;\n audioChannel: stream.StreamChannel<AudioFrame>;\n audioTranscript: string;\n modalities: Future<('text' | 'audio')[]>;\n}\n\ninterface ResponseGeneration {\n messageChannel: stream.StreamChannel<llm.MessageGeneration>;\n functionChannel: stream.StreamChannel<llm.FunctionCall>;\n messages: Map<string, MessageGeneration>;\n\n /** @internal */\n _doneFut: Future;\n /** @internal */\n _createdTimestamp: number;\n /** @internal */\n _firstTokenTimestamp?: number;\n}\n\nclass CreateResponseHandle {\n instructions?: string;\n doneFut: Future<llm.GenerationCreatedEvent>;\n // TODO(shubhra): add timeout\n constructor({ instructions }: { instructions?: string }) {\n this.instructions = instructions;\n this.doneFut = new Future();\n }\n}\n\n// default values got from a \"default\" session from their API\nconst DEFAULT_FIRST_RETRY_INTERVAL_MS = 100;\nconst DEFAULT_TEMPERATURE = 0.8;\nconst DEFAULT_TURN_DETECTION: api_proto.TurnDetectionType = {\n type: 'semantic_vad',\n eagerness: 'medium',\n create_response: true,\n interrupt_response: true,\n};\nconst DEFAULT_INPUT_AUDIO_TRANSCRIPTION: api_proto.InputAudioTranscription = {\n model: 'gpt-4o-mini-transcribe',\n};\nconst DEFAULT_TOOL_CHOICE: llm.ToolChoice = 'auto';\nconst DEFAULT_MAX_RESPONSE_OUTPUT_TOKENS: number | 'inf' = 'inf';\n\nconst AZURE_DEFAULT_INPUT_AUDIO_TRANSCRIPTION: api_proto.InputAudioTranscription = {\n model: 'whisper-1',\n};\n\nconst AZURE_DEFAULT_TURN_DETECTION: api_proto.TurnDetectionType = {\n type: 'server_vad',\n threshold: 0.5,\n prefix_padding_ms: 300,\n silence_duration_ms: 200,\n create_response: true,\n};\n\nconst DEFAULT_MAX_SESSION_DURATION = 20 * 60 * 1000; // 20 minutes\n\nconst DEFAULT_REALTIME_MODEL_OPTIONS = {\n model: 'gpt-realtime',\n voice: 'marin',\n temperature: DEFAULT_TEMPERATURE,\n inputAudioTranscription: DEFAULT_INPUT_AUDIO_TRANSCRIPTION,\n turnDetection: DEFAULT_TURN_DETECTION,\n toolChoice: DEFAULT_TOOL_CHOICE,\n maxResponseOutputTokens: DEFAULT_MAX_RESPONSE_OUTPUT_TOKENS,\n maxSessionDuration: DEFAULT_MAX_SESSION_DURATION,\n connOptions: DEFAULT_API_CONNECT_OPTIONS,\n modalities: ['text', 'audio'] as Modality[],\n};\nexport class RealtimeModel extends llm.RealtimeModel {\n sampleRate = api_proto.SAMPLE_RATE;\n numChannels = api_proto.NUM_CHANNELS;\n inFrameSize = api_proto.IN_FRAME_SIZE;\n outFrameSize = api_proto.OUT_FRAME_SIZE;\n\n /* @internal */\n _options: RealtimeOptions;\n\n get model(): string {\n return this._options.model;\n }\n\n constructor(\n options: {\n model?: string;\n voice?: string;\n temperature?: number;\n toolChoice?: llm.ToolChoice;\n baseURL?: string;\n modalities?: Modality[];\n inputAudioTranscription?: api_proto.InputAudioTranscription | null;\n // TODO(shubhra): add inputAudioNoiseReduction\n turnDetection?: api_proto.TurnDetectionType | null;\n speed?: number;\n // TODO(shubhra): add openai tracing options\n azureDeployment?: string;\n apiKey?: string;\n entraToken?: string;\n apiVersion?: string;\n maxSessionDuration?: number;\n connOptions?: APIConnectOptions;\n } = {},\n ) {\n const modalities = (options.modalities ||\n DEFAULT_REALTIME_MODEL_OPTIONS.modalities) as Modality[];\n\n super({\n messageTruncation: true,\n turnDetection: options.turnDetection !== null,\n userTranscription: options.inputAudioTranscription !== null,\n autoToolReplyGeneration: false,\n audioOutput: modalities.includes('audio'),\n });\n\n const isAzure = !!(options.apiVersion || options.entraToken || options.azureDeployment);\n\n if (options.apiKey === '' && !isAzure) {\n throw new Error(\n 'OpenAI API key is required, either using the argument or by setting the OPENAI_API_KEY environment variable',\n );\n }\n\n const apiKey = options.apiKey || process.env.OPENAI_API_KEY;\n\n if (!apiKey && !isAzure) {\n throw new Error(\n 'OpenAI API key is required, either using the argument or by setting the OPENAI_API_KEY environment variable',\n );\n }\n\n if (!options.baseURL && isAzure) {\n const azureEndpoint = process.env.AZURE_OPENAI_ENDPOINT;\n if (!azureEndpoint) {\n throw new Error(\n 'Missing Azure endpoint. Please pass base_url or set AZURE_OPENAI_ENDPOINT environment variable.',\n );\n }\n options.baseURL = `${azureEndpoint.replace(/\\/$/, '')}/openai`;\n }\n\n const { modalities: _, ...optionsWithoutModalities } = options;\n this._options = {\n ...DEFAULT_REALTIME_MODEL_OPTIONS,\n ...optionsWithoutModalities,\n baseURL: options.baseURL || BASE_URL,\n apiKey,\n isAzure,\n model: options.model || DEFAULT_REALTIME_MODEL_OPTIONS.model,\n modalities,\n };\n }\n\n /**\n * Create a RealtimeModel instance configured for Azure OpenAI Service.\n *\n * @param azureDeployment - The name of your Azure OpenAI deployment.\n * @param azureEndpoint - The endpoint URL for your Azure OpenAI resource. If undefined, will attempt to read from the environment variable AZURE_OPENAI_ENDPOINT.\n * @param apiVersion - API version to use with Azure OpenAI Service. If undefined, will attempt to read from the environment variable OPENAI_API_VERSION.\n * @param apiKey - Azure OpenAI API key. If undefined, will attempt to read from the environment variable AZURE_OPENAI_API_KEY.\n * @param entraToken - Azure Entra authentication token. Required if not using API key authentication.\n * @param baseURL - Base URL for the API endpoint. If undefined, constructed from the azure_endpoint.\n * @param voice - Voice setting for audio outputs. Defaults to \"alloy\".\n * @param inputAudioTranscription - Options for transcribing input audio. Defaults to @see DEFAULT_INPUT_AUDIO_TRANSCRIPTION.\n * @param turnDetection - Options for server-based voice activity detection (VAD). Defaults to @see DEFAULT_SERVER_VAD_OPTIONS.\n * @param temperature - Sampling temperature for response generation. Defaults to @see DEFAULT_TEMPERATURE.\n * @param speed - Speed of the audio output. Defaults to 1.0.\n * @param maxResponseOutputTokens - Maximum number of tokens in the response. Defaults to @see DEFAULT_MAX_RESPONSE_OUTPUT_TOKENS.\n * @param maxSessionDuration - Maximum duration of the session in milliseconds. Defaults to @see DEFAULT_MAX_SESSION_DURATION.\n *\n * @returns A RealtimeModel instance configured for Azure OpenAI Service.\n *\n * @throws Error if required Azure parameters are missing or invalid.\n */\n static withAzure({\n azureDeployment,\n azureEndpoint,\n apiVersion,\n apiKey,\n entraToken,\n baseURL,\n voice = 'alloy',\n inputAudioTranscription = AZURE_DEFAULT_INPUT_AUDIO_TRANSCRIPTION,\n turnDetection = AZURE_DEFAULT_TURN_DETECTION,\n temperature = 0.8,\n speed,\n }: {\n azureDeployment: string;\n azureEndpoint?: string;\n apiVersion?: string;\n apiKey?: string;\n entraToken?: string;\n baseURL?: string;\n voice?: string;\n inputAudioTranscription?: api_proto.InputAudioTranscription;\n // TODO(shubhra): add inputAudioNoiseReduction\n turnDetection?: api_proto.TurnDetectionType;\n temperature?: number;\n speed?: number;\n }) {\n apiKey = apiKey || process.env.AZURE_OPENAI_API_KEY;\n if (!apiKey && !entraToken) {\n throw new Error(\n 'Missing credentials. Please pass one of `apiKey`, `entraToken`, or the `AZURE_OPENAI_API_KEY` environment variable.',\n );\n }\n\n apiVersion = apiVersion || process.env.OPENAI_API_VERSION;\n if (!apiVersion) {\n throw new Error(\n 'Must provide either the `apiVersion` argument or the `OPENAI_API_VERSION` environment variable',\n );\n }\n\n if (!baseURL) {\n azureEndpoint = azureEndpoint || process.env.AZURE_OPENAI_ENDPOINT;\n if (!azureEndpoint) {\n throw new Error(\n 'Missing Azure endpoint. Please pass the `azure_endpoint` parameter or set the `AZURE_OPENAI_ENDPOINT` environment variable.',\n );\n }\n baseURL = `${azureEndpoint.replace(/\\/$/, '')}/openai`;\n }\n\n return new RealtimeModel({\n voice,\n inputAudioTranscription,\n turnDetection,\n temperature,\n speed,\n apiKey,\n azureDeployment,\n apiVersion,\n entraToken,\n baseURL,\n });\n }\n\n session() {\n return new RealtimeSession(this);\n }\n\n async close() {\n return;\n }\n}\n\nfunction processBaseURL({\n baseURL,\n model,\n isAzure = false,\n azureDeployment,\n apiVersion,\n}: {\n baseURL: string;\n model: string;\n isAzure: boolean;\n azureDeployment?: string;\n apiVersion?: string;\n}): string {\n const url = new URL([baseURL, 'realtime'].join('/'));\n\n if (url.protocol === 'https:') {\n url.protocol = 'wss:';\n }\n\n // ensure \"/realtime\" is added if the path is empty OR \"/v1\"\n if (!url.pathname || ['', '/v1', '/openai'].includes(url.pathname.replace(/\\/$/, ''))) {\n url.pathname = url.pathname.replace(/\\/$/, '') + '/realtime';\n } else {\n url.pathname = url.pathname.replace(/\\/$/, '');\n }\n\n const queryParams: Record<string, string> = {};\n if (isAzure) {\n if (apiVersion) {\n queryParams['api-version'] = apiVersion;\n }\n if (azureDeployment) {\n queryParams['deployment'] = azureDeployment;\n }\n } else {\n queryParams['model'] = model;\n }\n\n for (const [key, value] of Object.entries(queryParams)) {\n url.searchParams.set(key, value);\n }\n\n return url.toString();\n}\n\n/**\n * A session for the OpenAI Realtime API.\n *\n * This class is used to interact with the OpenAI Realtime API.\n * It is responsible for sending events to the OpenAI Realtime API and receiving events from it.\n *\n * It exposes two more events:\n * - openai_server_event_received: expose the raw server events from the OpenAI Realtime API\n * - openai_client_event_queued: expose the raw client events sent to the OpenAI Realtime API\n */\nexport class RealtimeSession extends llm.RealtimeSession {\n private _tools: llm.ToolContext = {};\n private remoteChatCtx: llm.RemoteChatContext = new llm.RemoteChatContext();\n private messageChannel = new Queue<api_proto.ClientEvent>();\n private inputResampler?: AudioResampler;\n private instructions?: string;\n private oaiRealtimeModel: RealtimeModel;\n private currentGeneration?: ResponseGeneration;\n private responseCreatedFutures: { [id: string]: CreateResponseHandle } = {};\n\n private textModeRecoveryRetries: number = 0;\n\n private itemCreateFutures: { [id: string]: Future } = {};\n private itemDeleteFutures: { [id: string]: Future } = {};\n\n private updateChatCtxLock = new Mutex();\n private updateFuncCtxLock = new Mutex();\n\n // 100ms chunks\n private bstream = new AudioByteStream(SAMPLE_RATE, NUM_CHANNELS, SAMPLE_RATE / 10);\n\n private pushedDurationMs: number = 0;\n\n #logger = log();\n #task: Task<void>;\n #closed = false;\n\n constructor(realtimeModel: RealtimeModel) {\n super(realtimeModel);\n\n this.oaiRealtimeModel = realtimeModel;\n\n this.#task = Task.from(({ signal }) => this.#mainTask(signal));\n\n this.sendEvent(this.createSessionUpdateEvent());\n }\n\n sendEvent(command: api_proto.ClientEvent): void {\n this.messageChannel.put(command);\n }\n\n private createSessionUpdateEvent(): api_proto.SessionUpdateEvent {\n // OpenAI supports ['text'] or ['text', 'audio'] (audio always includes text transcript)\n // We normalize to ensure 'text' is always present when using audio\n const modalities: Modality[] = this.oaiRealtimeModel._options.modalities.includes('audio')\n ? ['text', 'audio']\n : ['text'];\n\n return {\n type: 'session.update',\n session: {\n model: this.oaiRealtimeModel._options.model,\n voice: this.oaiRealtimeModel._options.voice,\n input_audio_format: 'pcm16',\n output_audio_format: 'pcm16',\n modalities: modalities,\n turn_detection: this.oaiRealtimeModel._options.turnDetection,\n input_audio_transcription: this.oaiRealtimeModel._options.inputAudioTranscription,\n // TODO(shubhra): add inputAudioNoiseReduction\n temperature: this.oaiRealtimeModel._options.temperature,\n tool_choice: toOaiToolChoice(this.oaiRealtimeModel._options.toolChoice),\n max_response_output_tokens:\n this.oaiRealtimeModel._options.maxResponseOutputTokens === Infinity\n ? 'inf'\n : this.oaiRealtimeModel._options.maxResponseOutputTokens,\n // TODO(shubhra): add tracing options\n instructions: this.instructions,\n speed: this.oaiRealtimeModel._options.speed,\n },\n };\n }\n\n get chatCtx() {\n return this.remoteChatCtx.toChatCtx();\n }\n\n get tools() {\n return { ...this._tools } as llm.ToolContext;\n }\n\n async updateChatCtx(_chatCtx: llm.ChatContext): Promise<void> {\n const unlock = await this.updateChatCtxLock.lock();\n const events = this.createChatCtxUpdateEvents(_chatCtx);\n const futures: Future<void>[] = [];\n\n for (const event of events) {\n const future = new Future<void>();\n futures.push(future);\n\n if (event.type === 'conversation.item.create') {\n this.itemCreateFutures[event.item.id] = future;\n } else if (event.type == 'conversation.item.delete') {\n this.itemDeleteFutures[event.item_id] = future;\n }\n\n this.sendEvent(event);\n }\n\n if (futures.length === 0) {\n unlock();\n return;\n }\n\n try {\n // wait for futures to resolve or timeout\n await Promise.race([\n Promise.all(futures),\n delay(5000).then(() => {\n throw new Error('Chat ctx update events timed out');\n }),\n ]);\n } catch (e) {\n this.#logger.error((e as Error).message);\n throw e;\n } finally {\n unlock();\n }\n }\n\n private createChatCtxUpdateEvents(\n chatCtx: llm.ChatContext,\n addMockAudio: boolean = false,\n ): (api_proto.ConversationItemCreateEvent | api_proto.ConversationItemDeleteEvent)[] {\n const newChatCtx = chatCtx.copy();\n if (addMockAudio) {\n newChatCtx.items.push(createMockAudioItem());\n } else {\n // clean up existing mock audio items\n newChatCtx.items = newChatCtx.items.filter(\n (item) => !item.id.startsWith(MOCK_AUDIO_ID_PREFIX),\n );\n }\n\n const events: (\n | api_proto.ConversationItemCreateEvent\n | api_proto.ConversationItemDeleteEvent\n )[] = [];\n\n const diffOps = llm.computeChatCtxDiff(this.chatCtx, newChatCtx);\n for (const op of diffOps.toRemove) {\n events.push({\n type: 'conversation.item.delete',\n item_id: op,\n event_id: shortuuid('chat_ctx_delete_'),\n } as api_proto.ConversationItemDeleteEvent);\n }\n\n for (const [previousId, id] of diffOps.toCreate) {\n const chatItem = newChatCtx.getById(id);\n if (!chatItem) {\n throw new Error(`Chat item ${id} not found`);\n }\n events.push({\n type: 'conversation.item.create',\n item: livekitItemToOpenAIItem(chatItem),\n previous_item_id: previousId ?? undefined,\n event_id: shortuuid('chat_ctx_create_'),\n } as api_proto.ConversationItemCreateEvent);\n }\n return events;\n }\n\n async updateTools(_tools: llm.ToolContext): Promise<void> {\n const unlock = await this.updateFuncCtxLock.lock();\n const ev = this.createToolsUpdateEvent(_tools);\n this.sendEvent(ev);\n\n if (!ev.session.tools) {\n throw new Error('Tools are missing in the session update event');\n }\n\n // TODO(brian): these logics below are noops I think, leaving it here to keep\n // parity with the python but we should remove them later\n const retainedToolNames = new Set(ev.session.tools.map((tool) => tool.name));\n const retainedTools = Object.fromEntries(\n Object.entries(_tools).filter(\n ([name, tool]) => llm.isFunctionTool(tool) && retainedToolNames.has(name),\n ),\n );\n\n this._tools = retainedTools as llm.ToolContext;\n\n unlock();\n }\n\n private createToolsUpdateEvent(_tools: llm.ToolContext): api_proto.SessionUpdateEvent {\n const oaiTools: api_proto.Tool[] = [];\n\n for (const [name, tool] of Object.entries(_tools)) {\n if (!llm.isFunctionTool(tool)) {\n this.#logger.error({ name, tool }, \"OpenAI Realtime API doesn't support this tool type\");\n continue;\n }\n\n const { parameters: toolParameters, description } = tool;\n try {\n const parameters = llm.toJsonSchema(\n toolParameters,\n ) as unknown as api_proto.Tool['parameters'];\n\n oaiTools.push({\n name,\n description,\n parameters: parameters,\n type: 'function',\n });\n } catch (e) {\n this.#logger.error({ name, tool }, \"OpenAI Realtime API doesn't support this tool type\");\n continue;\n }\n }\n\n return {\n type: 'session.update',\n session: {\n model: this.oaiRealtimeModel._options.model,\n tools: oaiTools,\n },\n event_id: shortuuid('tools_update_'),\n };\n }\n\n async updateInstructions(_instructions: string): Promise<void> {\n const eventId = shortuuid('instructions_update_');\n this.sendEvent({\n type: 'session.update',\n session: {\n instructions: _instructions,\n },\n event_id: eventId,\n } as api_proto.SessionUpdateEvent);\n this.instructions = _instructions;\n }\n\n updateOptions({ toolChoice }: { toolChoice?: llm.ToolChoice }): void {\n const options: api_proto.SessionUpdateEvent['session'] = {};\n\n this.oaiRealtimeModel._options.toolChoice = toolChoice;\n options.tool_choice = toOaiToolChoice(toolChoice);\n\n // TODO(brian): add other options here\n\n this.sendEvent({\n type: 'session.update',\n session: options,\n event_id: shortuuid('options_update_'),\n });\n }\n\n pushAudio(frame: AudioFrame): void {\n for (const f of this.resampleAudio(frame)) {\n for (const nf of this.bstream.write(f.data.buffer as ArrayBuffer)) {\n this.sendEvent({\n type: 'input_audio_buffer.append',\n audio: Buffer.from(nf.data.buffer).toString('base64'),\n } as api_proto.InputAudioBufferAppendEvent);\n // TODO(AJS-102): use frame.durationMs once available in rtc-node\n this.pushedDurationMs += (nf.samplesPerChannel / nf.sampleRate) * 1000;\n }\n }\n }\n\n async commitAudio(): Promise<void> {\n if (this.pushedDurationMs > 100) {\n // OpenAI requires at least 100ms of audio\n this.sendEvent({\n type: 'input_audio_buffer.commit',\n } as api_proto.InputAudioBufferCommitEvent);\n this.pushedDurationMs = 0;\n }\n }\n\n async clearAudio(): Promise<void> {\n this.sendEvent({\n type: 'input_audio_buffer.clear',\n } as api_proto.InputAudioBufferClearEvent);\n this.pushedDurationMs = 0;\n }\n\n async generateReply(instructions?: string): Promise<llm.GenerationCreatedEvent> {\n const handle = this.createResponse({ instructions, userInitiated: true });\n this.textModeRecoveryRetries = 0;\n return handle.doneFut.await;\n }\n\n async interrupt(): Promise<void> {\n this.sendEvent({\n type: 'response.cancel',\n } as api_proto.ResponseCancelEvent);\n }\n\n async truncate(_options: {\n messageId: string;\n audioEndMs: number;\n modalities?: Modality[];\n audioTranscript?: string;\n }): Promise<void> {\n if (!_options.modalities || _options.modalities.includes('audio')) {\n this.sendEvent({\n type: 'conversation.item.truncate',\n content_index: 0,\n item_id: _options.messageId,\n audio_end_ms: _options.audioEndMs,\n } as api_proto.ConversationItemTruncateEvent);\n } else if (_options.audioTranscript !== undefined) {\n // sync it to the remote chat context\n const chatCtx = this.chatCtx.copy();\n const idx = chatCtx.indexById(_options.messageId);\n if (idx !== undefined) {\n const item = chatCtx.items[idx];\n if (item && item.type === 'message') {\n const newItem = llm.ChatMessage.create({\n ...item,\n content: [_options.audioTranscript],\n });\n chatCtx.items[idx] = newItem;\n const events = this.createChatCtxUpdateEvents(chatCtx);\n for (const ev of events) {\n this.sendEvent(ev);\n }\n }\n }\n }\n }\n\n private loggableEvent(\n event: api_proto.ClientEvent | api_proto.ServerEvent,\n ): Record<string, unknown> {\n const untypedEvent: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(event)) {\n if (value !== undefined) {\n untypedEvent[key] = value;\n }\n }\n\n if (untypedEvent.audio && typeof untypedEvent.audio === 'string') {\n return { ...untypedEvent, audio: '...' };\n }\n if (\n untypedEvent.delta &&\n typeof untypedEvent.delta === 'string' &&\n event.type === 'response.audio.delta'\n ) {\n return { ...untypedEvent, delta: '...' };\n }\n return untypedEvent;\n }\n\n private async createWsConn(): Promise<WebSocket> {\n const headers: Record<string, string> = {\n 'User-Agent': 'LiveKit-Agents-JS',\n };\n\n if (this.oaiRealtimeModel._options.isAzure) {\n // Microsoft API has two ways of authentication\n // 1. Entra token set as `Bearer` token\n // 2. API key set as `api_key` header (also accepts query string)\n if (this.oaiRealtimeModel._options.entraToken) {\n headers.Authorization = `Bearer ${this.oaiRealtimeModel._options.entraToken}`;\n } else if (this.oaiRealtimeModel._options.apiKey) {\n headers['api-key'] = this.oaiRealtimeModel._options.apiKey;\n } else {\n throw new Error('Microsoft API key or entraToken is required');\n }\n } else {\n headers.Authorization = `Bearer ${this.oaiRealtimeModel._options.apiKey}`;\n headers['OpenAI-Beta'] = 'realtime=v1';\n }\n\n const url = processBaseURL({\n baseURL: this.oaiRealtimeModel._options.baseURL,\n model: this.oaiRealtimeModel._options.model,\n isAzure: this.oaiRealtimeModel._options.isAzure,\n apiVersion: this.oaiRealtimeModel._options.apiVersion,\n azureDeployment: this.oaiRealtimeModel._options.azureDeployment,\n });\n\n if (lkOaiDebug) {\n this.#logger.debug(`Connecting to OpenAI Realtime API at ${url}`);\n }\n\n return new Promise((resolve, reject) => {\n const ws = new WebSocket(url, { headers });\n let waiting = true;\n\n const timeout = setTimeout(() => {\n ws.close();\n reject(new Error('WebSocket connection timeout'));\n }, this.oaiRealtimeModel._options.connOptions.timeoutMs);\n\n ws.once('open', () => {\n if (!waiting) return;\n waiting = false;\n clearTimeout(timeout);\n resolve(ws);\n });\n\n ws.once('close', () => {\n if (!waiting) return;\n waiting = false;\n clearTimeout(timeout);\n reject(new Error('OpenAI Realtime API connection closed'));\n });\n });\n }\n\n async #mainTask(signal: AbortSignal): Promise<void> {\n let reconnecting = false;\n let numRetries = 0;\n let wsConn: WebSocket | null = null;\n const maxRetries = this.oaiRealtimeModel._options.connOptions.maxRetry;\n\n const reconnect = async () => {\n this.#logger.debug(\n {\n maxSessionDuration: this.oaiRealtimeModel._options.maxSessionDuration,\n },\n 'Reconnecting to OpenAI Realtime API',\n );\n\n const events: api_proto.ClientEvent[] = [];\n\n // options and instructions\n events.push(this.createSessionUpdateEvent());\n\n // tools\n if (Object.keys(this._tools).length > 0) {\n events.push(this.createToolsUpdateEvent(this._tools));\n }\n\n // chat context\n const chatCtx = this.chatCtx.copy({\n excludeFunctionCall: true,\n excludeInstructions: true,\n excludeEmptyMessage: true,\n });\n\n const oldChatCtx = this.remoteChatCtx;\n this.remoteChatCtx = new llm.RemoteChatContext();\n events.push(...this.createChatCtxUpdateEvents(chatCtx));\n\n try {\n for (const ev of events) {\n this.emit('openai_client_event_queued', ev);\n wsConn!.send(JSON.stringify(ev));\n }\n } catch (error) {\n this.remoteChatCtx = oldChatCtx;\n throw new APIConnectionError({\n message: 'Failed to send message to OpenAI Realtime API during session re-connection',\n });\n }\n\n this.#logger.debug('Reconnected to OpenAI Realtime API');\n\n this.emit('session_reconnected', {} as llm.RealtimeSessionReconnectedEvent);\n };\n\n reconnecting = false;\n while (!this.#closed && !signal.aborted) {\n this.#logger.debug('Creating WebSocket connection to OpenAI Realtime API');\n wsConn = await this.createWsConn();\n if (signal.aborted) break;\n\n try {\n if (reconnecting) {\n await reconnect();\n if (signal.aborted) break;\n numRetries = 0;\n }\n\n await this.runWs(wsConn);\n if (signal.aborted) break;\n } catch (error) {\n if (!isAPIError(error)) {\n this.emitError({ error: error as Error, recoverable: false });\n throw error;\n }\n\n if (maxRetries === 0 || !error.retryable) {\n this.emitError({ error: error as Error, recoverable: false });\n throw error;\n }\n\n if (numRetries === maxRetries) {\n this.emitError({ error: error as Error, recoverable: false });\n throw new APIConnectionError({\n message: `OpenAI Realtime API connection failed after ${numRetries} attempts`,\n options: {\n body: error,\n retryable: false,\n },\n });\n }\n\n this.emitError({ error: error as Error, recoverable: true });\n const retryInterval =\n numRetries === 0\n ? DEFAULT_FIRST_RETRY_INTERVAL_MS\n : this.oaiRealtimeModel._options.connOptions.retryIntervalMs;\n this.#logger.warn(\n {\n attempt: numRetries,\n maxRetries,\n error,\n },\n `OpenAI Realtime API connection failed, retrying in ${retryInterval / 1000}s`,\n );\n\n await delay(retryInterval);\n numRetries++;\n }\n\n reconnecting = true;\n }\n }\n\n private async runWs(wsConn: WebSocket): Promise<void> {\n const forwardEvents = async (signal: AbortSignal): Promise<void> => {\n const abortFuture = new Future<void>();\n signal.addEventListener('abort', () => abortFuture.resolve());\n\n while (!this.#closed && wsConn.readyState === WebSocket.OPEN && !signal.aborted) {\n try {\n const event = await Promise.race([this.messageChannel.get(), abortFuture.await]);\n if (signal.aborted || abortFuture.done || event === undefined) {\n break;\n }\n\n if (lkOaiDebug) {\n this.#logger.debug(this.loggableEvent(event), `(client) -> ${event.type}`);\n }\n\n this.emit('openai_client_event_queued', event);\n wsConn.send(JSON.stringify(event));\n } catch (error) {\n break;\n }\n }\n\n wsConn.close();\n };\n\n const wsCloseFuture = new Future<void | Error>();\n\n wsConn.onerror = (error) => {\n wsCloseFuture.resolve(new APIConnectionError({ message: error.message }));\n };\n wsConn.onclose = () => {\n wsCloseFuture.resolve();\n };\n\n wsConn.onmessage = (message: MessageEvent) => {\n const event: api_proto.ServerEvent = JSON.parse(message.data as string);\n\n this.emit('openai_server_event_received', event);\n if (lkOaiDebug) {\n this.#logger.debug(this.loggableEvent(event), `(server) <- ${event.type}`);\n }\n\n switch (event.type) {\n case 'input_audio_buffer.speech_started':\n this.handleInputAudioBufferSpeechStarted(event);\n break;\n case 'input_audio_buffer.speech_stopped':\n this.handleInputAudioBufferSpeechStopped(event);\n break;\n case 'response.created':\n this.handleResponseCreated(event);\n break;\n case 'response.output_item.added':\n this.handleResponseOutputItemAdded(event);\n break;\n case 'conversation.item.created':\n this.handleConversationItemCreated(event);\n break;\n case 'conversation.item.deleted':\n this.handleConversationItemDeleted(event);\n break;\n case 'conversation.item.input_audio_transcription.completed':\n this.handleConversationItemInputAudioTranscriptionCompleted(event);\n break;\n case 'conversation.item.input_audio_transcription.failed':\n this.handleConversationItemInputAudioTranscriptionFailed(event);\n break;\n case 'response.content_part.added':\n this.handleResponseContentPartAdded(event);\n break;\n case 'response.content_part.done':\n this.handleResponseContentPartDone(event);\n break;\n case 'response.text.delta':\n this.handleResponseTextDelta(event);\n break;\n case 'response.text.done':\n this.handleResponseTextDone(event);\n break;\n case 'response.audio_transcript.delta':\n this.handleResponseAudioTranscriptDelta(event);\n break;\n case 'response.audio.delta':\n this.handleResponseAudioDelta(event);\n break;\n case 'response.audio_transcript.done':\n this.handleResponseAudioTranscriptDone(event);\n break;\n case 'response.audio.done':\n this.handleResponseAudioDone(event);\n break;\n case 'response.output_item.done':\n this.handleResponseOutputItemDone(event);\n break;\n case 'response.done':\n this.handleResponseDone(event);\n break;\n case 'error':\n this.handleError(event);\n break;\n default:\n if (lkOaiDebug) {\n this.#logger.debug(`unhandled event: ${event.type}`);\n }\n break;\n }\n };\n\n const sendTask = Task.from(({ signal }) => forwardEvents(signal));\n\n const wsTask = Task.from(({ signal }) => {\n const abortPromise = new Promise<void>((resolve) => {\n signal.addEventListener('abort', () => {\n resolve();\n });\n });\n\n return Promise.race([wsCloseFuture.await, abortPromise]);\n });\n\n const waitReconnectTask = Task.from(async ({ signal }) => {\n await delay(this.oaiRealtimeModel._options.maxSessionDuration, { signal });\n return new APIConnectionError({\n message: 'OpenAI Realtime API connection timeout',\n });\n });\n\n try {\n const result = await Promise.race([wsTask.result, sendTask.result, waitReconnectTask.result]);\n\n if (waitReconnectTask.done && this.currentGeneration) {\n await this.currentGeneration._doneFut.await;\n }\n\n if (result instanceof Error) {\n throw result;\n }\n } finally {\n await cancelAndWait([wsTask, sendTask, waitReconnectTask], 2000);\n wsConn.close();\n }\n }\n\n async close() {\n super.close();\n this.#closed = true;\n await this.#task;\n }\n\n private handleInputAudioBufferSpeechStarted(\n _event: api_proto.InputAudioBufferSpeechStartedEvent,\n ): void {\n this.emit('input_speech_started', {} as llm.InputSpeechStartedEvent);\n }\n\n private handleInputAudioBufferSpeechStopped(\n _event: api_proto.InputAudioBufferSpeechStoppedEvent,\n ): void {\n this.emit('input_speech_stopped', {\n userTranscriptionEnabled: this.oaiRealtimeModel._options.inputAudioTranscription !== null,\n } as llm.InputSpeechStoppedEvent);\n }\n\n private handleResponseCreated(event: api_proto.ResponseCreatedEvent): void {\n if (!event.response.id) {\n throw new Error('response.id is missing');\n }\n\n this.currentGeneration = {\n messageChannel: stream.createStreamChannel<llm.MessageGeneration>(),\n functionChannel: stream.createStreamChannel<llm.FunctionCall>(),\n messages: new Map(),\n _doneFut: new Future(),\n _createdTimestamp: Date.now(),\n };\n\n // Build generation event and resolve client future (if any) before emitting,\n // matching Python behavior.\n const generationEv = {\n messageStream: this.currentGeneration.messageChannel.stream(),\n functionStream: this.currentGeneration.functionChannel.stream(),\n userInitiated: false,\n responseId: event.response.id,\n } as llm.GenerationCreatedEvent;\n\n const clientEventId = event.response.metadata?.client_event_id;\n if (clientEventId) {\n const handle = this.responseCreatedFutures[clientEventId];\n if (handle) {\n delete this.responseCreatedFutures[clientEventId];\n generationEv.userInitiated = true;\n if (!handle.doneFut.done) {\n handle.doneFut.resolve(generationEv);\n }\n }\n }\n\n this.emit('generation_created', generationEv);\n }\n\n private handleResponseOutputItemAdded(event: api_proto.ResponseOutputItemAddedEvent): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n\n if (!event.item.type) {\n throw new Error('item.type is not set');\n }\n\n if (!event.response_id) {\n throw new Error('response_id is not set');\n }\n\n const itemType = event.item.type;\n const responseId = event.response_id;\n\n if (itemType !== 'message') {\n // emit immediately if it's not a message, otherwise wait response.content_part.added\n this.resolveGeneration(responseId);\n this.textModeRecoveryRetries = 0;\n return;\n }\n\n const itemId = event.item.id;\n if (!itemId) {\n throw new Error('item.id is not set');\n }\n\n const modalitiesFut = new Future<Modality[]>();\n const itemGeneration: MessageGeneration = {\n messageId: itemId,\n textChannel: stream.createStreamChannel<string>(),\n audioChannel: stream.createStreamChannel<AudioFrame>(),\n audioTranscript: '',\n modalities: modalitiesFut,\n };\n\n // If audioOutput is not supported, close audio channel immediately\n if (!this.oaiRealtimeModel.capabilities.audioOutput) {\n itemGeneration.audioChannel.close();\n modalitiesFut.resolve(['text']);\n }\n\n this.currentGeneration.messageChannel.write({\n messageId: itemId,\n textStream: itemGeneration.textChannel.stream(),\n audioStream: itemGeneration.audioChannel.stream(),\n modalities: modalitiesFut.await,\n });\n\n this.currentGeneration.messages.set(itemId, itemGeneration);\n }\n\n private handleConversationItemCreated(event: api_proto.ConversationItemCreatedEvent): void {\n if (!event.item.id) {\n throw new Error('item.id is not set');\n }\n\n try {\n this.remoteChatCtx.insert(event.previous_item_id, openAIItemToLivekitItem(event.item));\n } catch (error) {\n this.#logger.error({ error, itemId: event.item.id }, 'failed to insert conversation item');\n }\n\n const fut = this.itemCreateFutures[event.item.id];\n if (fut) {\n fut.resolve();\n delete this.itemCreateFutures[event.item.id];\n }\n }\n\n private handleConversationItemDeleted(event: api_proto.ConversationItemDeletedEvent): void {\n if (!event.item_id) {\n throw new Error('item_id is not set');\n }\n\n try {\n this.remoteChatCtx.delete(event.item_id);\n } catch (error) {\n this.#logger.error({ error, itemId: event.item_id }, 'failed to delete conversation item');\n }\n\n const fut = this.itemDeleteFutures[event.item_id];\n if (fut) {\n fut.resolve();\n delete this.itemDeleteFutures[event.item_id];\n }\n }\n\n private handleConversationItemInputAudioTranscriptionCompleted(\n event: api_proto.ConversationItemInputAudioTranscriptionCompletedEvent,\n ): void {\n const remoteItem = this.remoteChatCtx.get(event.item_id);\n if (!remoteItem) {\n return;\n }\n\n const item = remoteItem.item;\n if (item instanceof llm.ChatMessage) {\n item.content.push(event.transcript);\n } else {\n throw new Error('item is not a chat message');\n }\n\n this.emit('input_audio_transcription_completed', {\n itemId: event.item_id,\n transcript: event.transcript,\n isFinal: true,\n } as llm.InputTranscriptionCompleted);\n }\n\n private handleConversationItemInputAudioTranscriptionFailed(\n event: api_proto.ConversationItemInputAudioTranscriptionFailedEvent,\n ): void {\n this.#logger.error(\n { error: event.error },\n 'OpenAI Realtime API failed to transcribe input audio',\n );\n }\n\n private handleResponseContentPartAdded(event: api_proto.ResponseContentPartAddedEvent): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n\n const itemId = event.item_id;\n const itemType = event.part.type;\n\n const itemGeneration = this.currentGeneration.messages.get(itemId);\n if (!itemGeneration) {\n this.#logger.warn(`itemGeneration not found for itemId=${itemId}`);\n return;\n }\n\n if (itemType === 'text' && this.oaiRealtimeModel.capabilities.audioOutput) {\n this.#logger.warn('Text response received from OpenAI Realtime API in audio modality.');\n }\n\n if (!itemGeneration.modalities.done) {\n const modalityResult: Modality[] = itemType === 'text' ? ['text'] : ['audio', 'text'];\n itemGeneration.modalities.resolve(modalityResult);\n }\n\n if (this.currentGeneration._firstTokenTimestamp === undefined) {\n this.currentGeneration._firstTokenTimestamp = Date.now();\n }\n }\n\n private handleResponseContentPartDone(event: api_proto.ResponseContentPartDoneEvent): void {\n if (!event.part) {\n return;\n }\n if (event.part.type !== 'text') {\n return;\n }\n\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n\n // TODO(shubhra): handle text mode recovery\n }\n\n private handleResponseTextDelta(event: api_proto.ResponseTextDeltaEvent): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n\n const itemGeneration = this.currentGeneration.messages.get(event.item_id);\n if (!itemGeneration) {\n throw new Error('itemGeneration is not set');\n }\n\n if (\n !this.oaiRealtimeModel.capabilities.audioOutput &&\n !this.currentGeneration._firstTokenTimestamp\n ) {\n this.currentGeneration._firstTokenTimestamp = Date.now();\n }\n\n itemGeneration.textChannel.write(event.delta);\n itemGeneration.audioTranscript += event.delta;\n }\n\n private handleResponseTextDone(_event: api_proto.ResponseTextDoneEvent): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n }\n\n private handleResponseAudioTranscriptDelta(\n event: api_proto.ResponseAudioTranscriptDeltaEvent,\n ): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n\n const itemId = event.item_id;\n const delta = event.delta;\n\n // TODO (shubhra): add timed string support\n\n const itemGeneration = this.currentGeneration.messages.get(itemId);\n if (!itemGeneration) {\n throw new Error('itemGeneration is not set');\n } else {\n itemGeneration.textChannel.write(delta);\n itemGeneration.audioTranscript += delta;\n }\n }\n\n private handleResponseAudioDelta(event: api_proto.ResponseAudioDeltaEvent): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n\n const itemGeneration = this.currentGeneration.messages.get(event.item_id);\n if (!itemGeneration) {\n throw new Error('itemGeneration is not set');\n }\n\n if (this.currentGeneration._firstTokenTimestamp === undefined) {\n this.currentGeneration._firstTokenTimestamp = Date.now();\n }\n\n if (!itemGeneration.modalities.done) {\n itemGeneration.modalities.resolve(['audio', 'text']);\n }\n\n const binaryString = atob(event.delta);\n const len = binaryString.length;\n const bytes = new Uint8Array(len);\n for (let i = 0; i < len; i++) {\n bytes[i] = binaryString.charCodeAt(i);\n }\n\n itemGeneration.audioChannel.write(\n new AudioFrame(\n new Int16Array(bytes.buffer),\n api_proto.SAMPLE_RATE,\n api_proto.NUM_CHANNELS,\n bytes.length / 2,\n ),\n );\n }\n\n private handleResponseAudioTranscriptDone(\n _event: api_proto.ResponseAudioTranscriptDoneEvent,\n ): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n }\n\n private handleResponseAudioDone(_event: api_proto.ResponseAudioDoneEvent): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n }\n\n private handleResponseOutputItemDone(event: api_proto.ResponseOutputItemDoneEvent): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n\n const itemId = event.item.id;\n const itemType = event.item.type;\n\n if (itemType === 'function_call') {\n const item = event.item;\n if (!item.call_id || !item.name || !item.arguments) {\n throw new Error('item is not a function call');\n }\n this.currentGeneration.functionChannel.write(\n llm.FunctionCall.create({\n callId: item.call_id,\n name: item.name,\n args: item.arguments,\n }),\n );\n } else if (itemType === 'message') {\n const itemGeneration = this.currentGeneration.messages.get(itemId);\n if (!itemGeneration) {\n return;\n }\n // text response doesn't have itemGeneration\n itemGeneration.textChannel.close();\n itemGeneration.audioChannel.close();\n if (!itemGeneration.modalities.done) {\n // In case message modalities is not set, this shouldn't happen\n itemGeneration.modalities.resolve(this.oaiRealtimeModel._options.modalities);\n }\n }\n }\n\n private handleResponseDone(_event: api_proto.ResponseDoneEvent): void {\n if (!this.currentGeneration) {\n // OpenAI has a race condition where we could receive response.done without any\n // previous response.created (This happens generally during interruption)\n return;\n }\n\n const createdTimestamp = this.currentGeneration._createdTimestamp;\n const firstTokenTimestamp = this.currentGeneration._firstTokenTimestamp;\n\n this.#logger.debug(\n {\n messageCount: this.currentGeneration.messages.size,\n },\n 'Closing generation channels in handleResponseDone',\n );\n\n for (const generation of this.currentGeneration.messages.values()) {\n generation.textChannel.close();\n generation.audioChannel.close();\n if (!generation.modalities.done) {\n generation.modalities.resolve(this.oaiRealtimeModel._options.modalities);\n }\n }\n\n this.currentGeneration.functionChannel.close();\n this.currentGeneration.messageChannel.close();\n\n for (const itemId of this.currentGeneration.messages.keys()) {\n const remoteItem = this.remoteChatCtx.get(itemId);\n if (remoteItem && remoteItem.item instanceof llm.ChatMessage) {\n remoteItem.item.content.push(this.currentGeneration.messages.get(itemId)!.audioTranscript);\n }\n }\n\n this.currentGeneration._doneFut.resolve();\n this.currentGeneration = undefined;\n\n // Calculate and emit metrics\n const usage = _event.response.usage;\n const ttftMs = firstTokenTimestamp ? firstTokenTimestamp - createdTimestamp : -1;\n const durationMs = Date.now() - createdTimestamp;\n\n const realtimeMetrics: metrics.RealtimeModelMetrics = {\n type: 'realtime_model_metrics',\n timestamp: createdTimestamp,\n requestId: _event.response.id || '',\n ttftMs,\n durationMs,\n cancelled: _event.response.status === 'cancelled',\n label: 'openai_realtime',\n inputTokens: usage?.input_tokens ?? 0,\n outputTokens: usage?.output_tokens ?? 0,\n totalTokens: usage?.total_tokens ?? 0,\n tokensPerSecond: durationMs > 0 ? (usage?.output_tokens ?? 0) / (durationMs / 1000) : 0,\n inputTokenDetails: {\n audioTokens: usage?.input_token_details?.audio_tokens ?? 0,\n textTokens: usage?.input_token_details?.text_tokens ?? 0,\n imageTokens: 0, // Not supported yet\n cachedTokens: usage?.input_token_details?.cached_tokens ?? 0,\n cachedTokensDetails: usage?.input_token_details?.cached_tokens_details\n ? {\n audioTokens: usage?.input_token_details?.cached_tokens_details?.audio_tokens ?? 0,\n textTokens: usage?.input_token_details?.cached_tokens_details?.text_tokens ?? 0,\n imageTokens: usage?.input_token_details?.cached_tokens_details?.image_tokens ?? 0,\n }\n : undefined,\n },\n outputTokenDetails: {\n textTokens: usage?.output_token_details?.text_tokens ?? 0,\n audioTokens: usage?.output_token_details?.audio_tokens ?? 0,\n imageTokens: 0,\n },\n };\n\n this.emit('metrics_collected', realtimeMetrics);\n // TODO(brian): handle response done but not complete\n }\n\n private handleError(event: api_proto.ErrorEvent): void {\n if (event.error.message.startsWith('Cancellation failed')) {\n return;\n }\n\n this.#logger.error({ error: event.error }, 'OpenAI Realtime API returned an error');\n this.emitError({\n error: new APIError(event.error.message, {\n body: event.error,\n retryable: true,\n }),\n recoverable: true,\n });\n\n // TODO(brian): set error for response future if it exists\n }\n\n private emitError({ error, recoverable }: { error: Error; recoverable: boolean }): void {\n // IMPORTANT: only emit error if there are listeners; otherwise emit will throw an error\n this.emit('error', {\n timestamp: Date.now(),\n // TODO(brian): add label\n label: '',\n error,\n recoverable,\n } as llm.RealtimeModelError);\n }\n\n private *resampleAudio(frame: AudioFrame): Generator<AudioFrame> {\n yield frame;\n }\n\n private createResponse({\n userInitiated,\n instructions,\n oldHandle,\n }: {\n userInitiated: boolean;\n instructions?: string;\n oldHandle?: CreateResponseHandle;\n }): CreateResponseHandle {\n const handle = oldHandle || new CreateResponseHandle({ instructions });\n if (oldHandle && instructions) {\n handle.instructions = instructions;\n }\n\n const eventId = shortuuid('response_create_');\n if (userInitiated) {\n this.responseCreatedFutures[eventId] = handle;\n }\n\n const response: api_proto.ResponseCreateEvent['response'] = {};\n if (instructions) response.instructions = instructions;\n if (userInitiated) response.metadata = { client_event_id: eventId };\n\n this.sendEvent({\n type: 'response.create',\n event_id: eventId,\n response: Object.keys(response).length > 0 ? response : undefined,\n });\n\n return handle;\n }\n\n private resolveGeneration(responseId: string): void {\n if (!this.currentGeneration) {\n throw new Error('currentGeneration is not set');\n }\n\n const generation_ev = {\n messageStream: this.currentGeneration.messageChannel.stream(),\n functionStream: this.currentGeneration.functionChannel.stream(),\n userInitiated: false,\n responseId,\n } as llm.GenerationCreatedEvent;\n\n const handle = this.responseCreatedFutures[responseId];\n if (handle) {\n delete this.responseCreatedFutures[responseId];\n generation_ev.userInitiated = true;\n if (handle.doneFut.done) {\n this.#logger.warn({ responseId }, 'response received after timeout');\n } else {\n handle.doneFut.resolve(generation_ev);\n }\n }\n }\n}\n\nfunction livekitItemToOpenAIItem(item: llm.ChatItem): api_proto.ItemResource {\n switch (item.type) {\n case 'function_call':\n return {\n id: item.id,\n type: 'function_call',\n call_id: item.callId,\n name: item.name,\n arguments: item.args,\n } as api_proto.FunctionCallItem;\n case 'function_call_output':\n return {\n id: item.id,\n type: 'function_call_output',\n call_id: item.callId,\n output: item.output,\n } as api_proto.FunctionCallOutputItem;\n case 'message':\n const role = item.role === 'developer' ? 'system' : item.role;\n const contentList: api_proto.Content[] = [];\n for (const c of item.content) {\n if (typeof c === 'string') {\n contentList.push({\n type: role === 'assistant' ? 'text' : 'input_text',\n text: c,\n } as api_proto.InputTextContent);\n } else if (c.type === 'image_content') {\n // not supported for now\n continue;\n } else if (c.type === 'audio_content') {\n if (role === 'user') {\n const encodedAudio = Buffer.from(combineAudioFrames(c.frame).data).toString('base64');\n contentList.push({\n type: 'input_audio',\n audio: encodedAudio,\n } as api_proto.InputAudioContent);\n }\n }\n }\n return {\n id: item.id,\n type: 'message',\n role,\n content: contentList,\n } as api_proto.UserItem;\n default:\n throw new Error(`Unsupported item type: ${(item as any).type}`);\n }\n}\n\nfunction openAIItemToLivekitItem(item: api_proto.ItemResource): llm.ChatItem {\n if (!item.id) {\n throw new Error('item.id is not set');\n }\n\n switch (item.type) {\n case 'function_call':\n return llm.FunctionCall.create({\n id: item.id,\n callId: item.call_id,\n name: item.name,\n args: item.arguments,\n });\n case 'function_call_output':\n return llm.FunctionCallOutput.create({\n id: item.id,\n callId: item.call_id,\n output: item.output,\n isError: false,\n });\n case 'message':\n const content: llm.ChatContent[] = [];\n // item.content can be a single object or an array; normalize to array\n const contents = Array.isArray(item.content) ? item.content : [item.content];\n for (const c of contents) {\n if (c.type === 'text' || c.type === 'input_text') {\n content.push(c.text);\n }\n }\n return llm.ChatMessage.create({\n id: item.id,\n role: item.role,\n content,\n });\n }\n}\n\nfunction createMockAudioItem(durationSeconds: number = 2): llm.ChatMessage {\n const audioData = Buffer.alloc(durationSeconds * SAMPLE_RATE);\n return llm.ChatMessage.create({\n id: shortuuid(MOCK_AUDIO_ID_PREFIX),\n role: 'user',\n content: [\n {\n type: 'audio_content',\n frame: [\n new AudioFrame(\n new Int16Array(audioData.buffer),\n SAMPLE_RATE,\n NUM_CHANNELS,\n audioData.length / 2,\n ),\n ],\n } as llm.AudioContent,\n ],\n });\n}\n\nfunction toOaiToolChoice(toolChoice?: llm.ToolChoice): api_proto.ToolChoice {\n if (typeof toolChoice === 'string') {\n return toolChoice;\n }\n\n if (toolChoice?.type === 'function') {\n return toolChoice.function.name;\n }\n\n return 'auto';\n}\n"],"mappings":"AAIA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,aAAa;AAEtB,SAAS,YAAY,0BAA0B;AAC/C,SAA4B,iBAAiB;AAC7C,YAAY,eAAe;AAG3B,MAAM,aAAa,QAAQ,IAAI,kBAAkB,OAAO,QAAQ,IAAI,eAAe,IAAI;AAEvF,MAAM,cAAc;AACpB,MAAM,eAAe;AACrB,MAAM,WAAW;AAEjB,MAAM,uBAAuB;AAgD7B,MAAM,qBAAqB;AAAA,EACzB;AAAA,EACA;AAAA;AAAA,EAEA,YAAY,EA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,KAAK;AAAA,QACT,MAAM,KAAK;AAAA,QACX;AAAA,MACF,CAAC;AAAA,EACL;AACF;AAEA,SAAS,oBAAoB,kBAA0B,GAAoB;AACzE,QAAM,YAAY,OAAO,MAAM,kBAAkB,WAAW;AAC5D,SAAO,IAAI,YAAY,OAAO;AAAA,IAC5B,IAAI,UAAU,oBAAoB;AAAA,IAClC,MAAM;AAAA,IACN,SAAS;AAAA,MACP;AAAA,QACE,MAAM;AAAA,QACN,OAAO;AAAA,UACL,IAAI;AAAA,YACF,IAAI,WAAW,UAAU,MAAM;AAAA,YAC/B;AAAA,YACA;AAAA,YACA,UAAU,SAAS;AAAA,UACrB;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAEA,SAAS,gBAAgB,YAAmD;AAC1E,MAAI,OAAO,eAAe,UAAU;AAClC,WAAO;AAAA,EACT;AAEA,OAAI,yCAAY,UAAS,YAAY;AACnC,WAAO,WAAW,SAAS;AAAA,EAC7B;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livekit/agents-plugin-openai",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.32",
|
|
4
4
|
"description": "OpenAI plugin for LiveKit Node Agents",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"require": "dist/index.cjs",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"@types/ws": "^8.5.10",
|
|
31
31
|
"tsup": "^8.3.5",
|
|
32
32
|
"typescript": "^5.0.0",
|
|
33
|
-
"@livekit/agents
|
|
34
|
-
"@livekit/agents-
|
|
35
|
-
"@livekit/agents": "1.0.
|
|
33
|
+
"@livekit/agents": "1.0.32",
|
|
34
|
+
"@livekit/agents-plugin-silero": "1.0.32",
|
|
35
|
+
"@livekit/agents-plugins-test": "1.0.32"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@livekit/mutex": "^1.1.1",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
43
|
"@livekit/rtc-node": "^0.13.22",
|
|
44
|
-
"@livekit/agents": "1.0.
|
|
44
|
+
"@livekit/agents": "1.0.32"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "tsup --onSuccess \"pnpm build:types\"",
|
|
@@ -20,7 +20,9 @@ export type Voice =
|
|
|
20
20
|
| 'sage'
|
|
21
21
|
| 'verse'
|
|
22
22
|
| string;
|
|
23
|
-
|
|
23
|
+
|
|
24
|
+
/** @deprecated Use LegacyAudioFormat for Beta API string format or AudioFormat for GA API object format */
|
|
25
|
+
export type LegacyAudioFormat = 'pcm16'; // TODO: 'g711-ulaw' | 'g711-alaw' (Beta format)
|
|
24
26
|
export type Role = 'system' | 'assistant' | 'user' | 'tool';
|
|
25
27
|
export type GenerationFinishedReason = 'stop' | 'max_tokens' | 'content_filter' | 'interrupt';
|
|
26
28
|
export type InputTranscriptionModel = 'whisper-1' | string; // Open-ended, for future models
|
|
@@ -44,6 +46,7 @@ export type ClientEventType =
|
|
|
44
46
|
| 'conversation.item.delete'
|
|
45
47
|
| 'response.create'
|
|
46
48
|
| 'response.cancel';
|
|
49
|
+
|
|
47
50
|
export type ServerEventType =
|
|
48
51
|
| 'error'
|
|
49
52
|
| 'session.created'
|
|
@@ -53,7 +56,8 @@ export type ServerEventType =
|
|
|
53
56
|
| 'input_audio_buffer.cleared'
|
|
54
57
|
| 'input_audio_buffer.speech_started'
|
|
55
58
|
| 'input_audio_buffer.speech_stopped'
|
|
56
|
-
| 'conversation.item.created
|
|
59
|
+
| 'conversation.item.added' // GA: renamed from conversation.item.created
|
|
60
|
+
| 'conversation.item.created' // Beta: kept for backward compatibility
|
|
57
61
|
| 'conversation.item.input_audio_transcription.completed'
|
|
58
62
|
| 'conversation.item.input_audio_transcription.failed'
|
|
59
63
|
| 'conversation.item.truncated'
|
|
@@ -64,12 +68,18 @@ export type ServerEventType =
|
|
|
64
68
|
| 'response.output_item.done'
|
|
65
69
|
| 'response.content_part.added'
|
|
66
70
|
| 'response.content_part.done'
|
|
67
|
-
| 'response.text.delta
|
|
68
|
-
| 'response.text.done
|
|
69
|
-
| 'response.
|
|
70
|
-
| 'response.
|
|
71
|
-
| 'response.
|
|
72
|
-
| 'response.
|
|
71
|
+
| 'response.output_text.delta' // GA: renamed from response.text.delta
|
|
72
|
+
| 'response.output_text.done' // GA: renamed from response.text.done
|
|
73
|
+
| 'response.text.delta' // Beta: kept for backward compatibility
|
|
74
|
+
| 'response.text.done' // Beta: kept for backward compatibility
|
|
75
|
+
| 'response.output_audio_transcript.delta' // GA: renamed from response.audio_transcript.delta
|
|
76
|
+
| 'response.output_audio_transcript.done' // GA: renamed from response.audio_transcript.done
|
|
77
|
+
| 'response.audio_transcript.delta' // Beta: kept for backward compatibility
|
|
78
|
+
| 'response.audio_transcript.done' // Beta: kept for backward compatibility
|
|
79
|
+
| 'response.output_audio.delta' // GA: renamed from response.audio.delta
|
|
80
|
+
| 'response.output_audio.done' // GA: renamed from response.audio.done
|
|
81
|
+
| 'response.audio.delta' // Beta: kept for backward compatibility
|
|
82
|
+
| 'response.audio.done' // Beta: kept for backward compatibility
|
|
73
83
|
| 'response.function_call_arguments.delta'
|
|
74
84
|
| 'response.function_call_arguments.done'
|
|
75
85
|
| 'rate_limits.updated';
|
|
@@ -113,6 +123,35 @@ export type InputAudioTranscription = {
|
|
|
113
123
|
prompt?: string;
|
|
114
124
|
};
|
|
115
125
|
|
|
126
|
+
export type NoiseReductionType = 'near_field' | 'far_field';
|
|
127
|
+
|
|
128
|
+
export interface NoiseReduction {
|
|
129
|
+
type: NoiseReductionType;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface AudioFormat {
|
|
133
|
+
type: 'audio/pcm';
|
|
134
|
+
rate: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface RealtimeAudioConfigInput {
|
|
138
|
+
format?: AudioFormat;
|
|
139
|
+
noise_reduction?: NoiseReduction | null;
|
|
140
|
+
transcription?: InputAudioTranscription | null;
|
|
141
|
+
turn_detection?: TurnDetectionType | null;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export interface RealtimeAudioConfigOutput {
|
|
145
|
+
format?: AudioFormat;
|
|
146
|
+
speed?: number;
|
|
147
|
+
voice?: Voice;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export interface RealtimeAudioConfig {
|
|
151
|
+
input?: RealtimeAudioConfigInput;
|
|
152
|
+
output?: RealtimeAudioConfigOutput;
|
|
153
|
+
}
|
|
154
|
+
|
|
116
155
|
export interface InputTextContent {
|
|
117
156
|
type: 'input_text';
|
|
118
157
|
text: string;
|
|
@@ -136,9 +175,10 @@ export interface AudioContent {
|
|
|
136
175
|
|
|
137
176
|
export type Content = InputTextContent | InputAudioContent | TextContent | AudioContent;
|
|
138
177
|
export type ContentPart = {
|
|
139
|
-
type: 'text' | 'audio';
|
|
178
|
+
type: 'text' | 'audio' | 'output_text' | 'output_audio'; // GA: output_text/output_audio
|
|
140
179
|
audio?: AudioBase64Bytes;
|
|
141
180
|
transcript?: string;
|
|
181
|
+
text?: string; // GA: text field for output_text
|
|
142
182
|
};
|
|
143
183
|
|
|
144
184
|
export interface BaseItem {
|
|
@@ -193,8 +233,8 @@ export interface SessionResource {
|
|
|
193
233
|
modalities: Modality[]; // default: ["text", "audio"]
|
|
194
234
|
instructions: string;
|
|
195
235
|
voice: Voice; // default: "alloy"
|
|
196
|
-
input_audio_format:
|
|
197
|
-
output_audio_format:
|
|
236
|
+
input_audio_format: LegacyAudioFormat; // default: "pcm16"
|
|
237
|
+
output_audio_format: LegacyAudioFormat; // default: "pcm16"
|
|
198
238
|
input_audio_transcription: InputAudioTranscription | null;
|
|
199
239
|
turn_detection: TurnDetectionType | null;
|
|
200
240
|
tools: Tool[];
|
|
@@ -266,22 +306,34 @@ interface BaseClientEvent {
|
|
|
266
306
|
export interface SessionUpdateEvent extends BaseClientEvent {
|
|
267
307
|
type: 'session.update';
|
|
268
308
|
session: Partial<{
|
|
309
|
+
// GA fields
|
|
310
|
+
type?: 'realtime'; // GA: session type
|
|
311
|
+
output_modalities?: Modality[]; // GA: renamed from modalities
|
|
312
|
+
audio?: RealtimeAudioConfig; // GA: nested audio config
|
|
313
|
+
max_output_tokens?: number | 'inf'; // GA: renamed from max_response_output_tokens
|
|
314
|
+
tracing?: TracingConfig | null; // GA: tracing config
|
|
315
|
+
// Common fields
|
|
269
316
|
model: Model;
|
|
270
|
-
modalities: Modality[];
|
|
271
317
|
instructions: string;
|
|
318
|
+
tools: Tool[];
|
|
319
|
+
tool_choice: ToolChoice;
|
|
320
|
+
// Beta fields (kept for backward compatibility)
|
|
321
|
+
modalities: Modality[];
|
|
272
322
|
voice: Voice;
|
|
273
|
-
input_audio_format:
|
|
274
|
-
output_audio_format:
|
|
323
|
+
input_audio_format: LegacyAudioFormat;
|
|
324
|
+
output_audio_format: LegacyAudioFormat;
|
|
275
325
|
input_audio_transcription: InputAudioTranscription | null;
|
|
276
326
|
turn_detection: TurnDetectionType | null;
|
|
277
|
-
tools: Tool[];
|
|
278
|
-
tool_choice: ToolChoice;
|
|
279
327
|
temperature: number;
|
|
280
328
|
max_response_output_tokens?: number | 'inf';
|
|
281
329
|
speed?: number;
|
|
282
330
|
}>;
|
|
283
331
|
}
|
|
284
332
|
|
|
333
|
+
export interface TracingConfig {
|
|
334
|
+
enabled?: boolean;
|
|
335
|
+
}
|
|
336
|
+
|
|
285
337
|
export interface InputAudioBufferAppendEvent extends BaseClientEvent {
|
|
286
338
|
type: 'input_audio_buffer.append';
|
|
287
339
|
audio: AudioBase64Bytes;
|
|
@@ -353,7 +405,7 @@ export interface ResponseCreateEvent extends BaseClientEvent {
|
|
|
353
405
|
modalities: Modality[];
|
|
354
406
|
instructions: string;
|
|
355
407
|
voice: Voice;
|
|
356
|
-
output_audio_format:
|
|
408
|
+
output_audio_format: LegacyAudioFormat;
|
|
357
409
|
tools?: Tool[];
|
|
358
410
|
tool_choice: ToolChoice;
|
|
359
411
|
temperature: number;
|
|
@@ -435,6 +487,12 @@ export interface ConversationItemCreatedEvent extends BaseServerEvent {
|
|
|
435
487
|
item: ItemResource;
|
|
436
488
|
}
|
|
437
489
|
|
|
490
|
+
export interface ConversationItemAddedEvent extends BaseServerEvent {
|
|
491
|
+
type: 'conversation.item.added';
|
|
492
|
+
previous_item_id: string;
|
|
493
|
+
item: ItemResource;
|
|
494
|
+
}
|
|
495
|
+
|
|
438
496
|
export interface ConversationItemInputAudioTranscriptionCompletedEvent extends BaseServerEvent {
|
|
439
497
|
type: 'conversation.item.input_audio_transcription.completed';
|
|
440
498
|
item_id: string;
|
|
@@ -593,6 +651,7 @@ export type ServerEvent =
|
|
|
593
651
|
| InputAudioBufferSpeechStartedEvent
|
|
594
652
|
| InputAudioBufferSpeechStoppedEvent
|
|
595
653
|
| ConversationItemCreatedEvent
|
|
654
|
+
| ConversationItemAddedEvent // GA: renamed from conversation.item.created
|
|
596
655
|
| ConversationItemInputAudioTranscriptionCompletedEvent
|
|
597
656
|
| ConversationItemInputAudioTranscriptionFailedEvent
|
|
598
657
|
| ConversationItemTruncatedEvent
|
package/src/realtime/index.ts
CHANGED