import { type FunctionTool, OutputGuardrailTripwireTriggered, RunContext, RunToolApprovalItem, } from '@openai/agents-core'; import { RealtimeGuardrailMetadata } from './guardrail'; import { RealtimeItem } from './items'; import { RealtimeAgent } from './realtimeAgent'; import { TransportEvent, TransportLayerAudio } from './transportLayerEvents'; import { RealtimeContextData } from './realtimeSession'; import { protocol } from '@openai/agents-core'; type AgentWithOrWithoutHistory = | RealtimeAgent | RealtimeAgent>; export type RealtimeToolApprovalRequest = { type: 'function_approval'; tool: FunctionTool>; approvalItem: RunToolApprovalItem; }; export type RealtimeSessionError = { type: 'error'; error: unknown; }; export type RealtimeSessionEventTypes = { /** * Triggered when an agent starts its work on a response. */ agent_start: [ context: RunContext>, agent: AgentWithOrWithoutHistory, ]; /** * Triggered when an agent ends its work on a response. */ agent_end: [ context: RunContext>, agent: AgentWithOrWithoutHistory, output: string, ]; /** * Triggered when an agent hands off to another agent. */ agent_handoff: [ context: RunContext>, fromAgent: AgentWithOrWithoutHistory, toAgent: AgentWithOrWithoutHistory, ]; /** * Triggered when an agent starts a tool call. */ agent_tool_start: [ context: RunContext>, agent: AgentWithOrWithoutHistory, tool: FunctionTool>, details: { toolCall: protocol.ToolCallItem }, ]; /** * Triggered when an agent ends a tool call. */ agent_tool_end: [ context: RunContext>, agent: AgentWithOrWithoutHistory, tool: FunctionTool>, result: string, details: { toolCall: protocol.ToolCallItem }, ]; /** * Emits all the raw events from the transport layer. */ transport_event: [event: TransportEvent]; /** * Triggered when the agent starts generating audio. */ audio_start: [ context: RunContext>, agent: AgentWithOrWithoutHistory, ]; /** * Triggered when the agent stops generating audio. */ audio_stopped: [ context: RunContext>, agent: AgentWithOrWithoutHistory, ]; /** * Triggered when there is new audio data available for playing to the user. */ audio: [event: TransportLayerAudio]; /** * Triggered when the agent is interrupted. Can be listened to by the user to stop audio playback * or give visual indicators to the user. */ audio_interrupted: [ context: RunContext>, agent: AgentWithOrWithoutHistory, ]; /** * Triggered when an output guardrail is tripped. */ guardrail_tripped: [ context: RunContext>, agent: AgentWithOrWithoutHistory, error: OutputGuardrailTripwireTriggered, details: { itemId: string }, ]; /** * Triggered when the history got updated. Contains the full history of the conversation. */ history_updated: [history: RealtimeItem[]]; /** * Triggered when a new item is added to the history. At this point the transcript/response * might still be in progress. */ history_added: [item: RealtimeItem]; /** * Triggered when an error occurs. */ error: [error: RealtimeSessionError]; /** * Triggered when a tool approval is requested. */ tool_approval_requested: [ context: RunContext>, agent: AgentWithOrWithoutHistory, approvalRequest: RealtimeToolApprovalRequest, ]; };