リアルタイムトランスポート
デフォルトのトランスポート層
Section titled “デフォルトのトランスポート層”WebRTC での接続
Section titled “WebRTC での接続”デフォルトのトランスポート層は WebRTC を使用します。音声はマイクから録音され、自動的に再生されます。
独自のメディアストリームまたはオーディオ要素を使用したい場合は、セッションを作成するときに OpenAIRealtimeWebRTC
インスタンスを指定してください。
import { RealtimeAgent, RealtimeSession, OpenAIRealtimeWebRTC } from '@openai/agents/realtime';
const agent = new RealtimeAgent({ name: 'Greeter', instructions: 'Greet the user with cheer and answer questions.',});
async function main() { const transport = new OpenAIRealtimeWebRTC({ mediaStream: await navigator.mediaDevices.getUserMedia({ audio: true }), audioElement: document.createElement('audio'), });
const customSession = new RealtimeSession(agent, { transport });}
WebSocket での接続
Section titled “WebSocket での接続”WebRTC の代わりに WebSocket 接続を使用するには、セッション作成時に transport: 'websocket'
もしくは OpenAIRealtimeWebSocket
インスタンスを渡します。これは Twilio を使った電話エージェントのようなサーバーサイドのユースケースに適しています。
import { RealtimeAgent, RealtimeSession } from '@openai/agents/realtime';
const agent = new RealtimeAgent({ name: 'Greeter', instructions: 'Greet the user with cheer and answer questions.',});
const myRecordedArrayBuffer = new ArrayBuffer(0);
const wsSession = new RealtimeSession(agent, { transport: 'websocket', model: 'gpt-4o-realtime-preview-2025-06-03',});await wsSession.connect({ apiKey: process.env.OPENAI_API_KEY! });
wsSession.on('audio', (event) => { // event.data is a chunk of PCM16 audio});
wsSession.sendAudio(myRecordedArrayBuffer);
生の PCM16 オーディオバイトの取り扱いには任意の録音/再生ライブラリを使用できます。
独自トランスポートメカニズムの構築
Section titled “独自トランスポートメカニズムの構築”別の音声―音声 API を利用したい場合や独自のトランスポートメカニズムを用意したい場合は、RealtimeTransportLayer
インターフェースを実装し、RealtimeTransportEventTypes
イベントを発火させることで独自実装を作成できます。
Realtime API との直接的なインタラクション
Section titled “Realtime API との直接的なインタラクション”OpenAI Realtime API を使用しつつ、より直接的に Realtime API にアクセスしたい場合は、次の 2 つの方法があります。
オプション 1 — トランスポート層へのアクセス
Section titled “オプション 1 — トランスポート層へのアクセス”RealtimeSession
のすべての機能を活用しつつアクセスしたい場合は、session.transport
からトランスポート層にアクセスできます。
トランスポート層は受信したすべてのイベントを *
イベントとして発火し、sendEvent()
メソッドを用いて元イベントを送信できます。
import { RealtimeAgent, RealtimeSession } from '@openai/agents/realtime';
const agent = new RealtimeAgent({ name: 'Greeter', instructions: 'Greet the user with cheer and answer questions.',});
const session = new RealtimeSession(agent, { model: 'gpt-4o-realtime-preview-2025-06-03',});
session.transport.on('*', (event) => { // JSON parsed version of the event received on the connection});
// Send any valid event as JSON. For example triggering a new responsesession.transport.sendEvent({ type: 'response.create', // ...});
オプション 2 — トランスポート層のみを使用
Section titled “オプション 2 — トランスポート層のみを使用”自動ツール実行やガードレールなどが不要な場合、トランスポート層を接続と割り込みのみを管理する “薄い” クライアントとして使用することもできます。
import { OpenAIRealtimeWebRTC } from '@openai/agents/realtime';
const client = new OpenAIRealtimeWebRTC();const audioBuffer = new ArrayBuffer(0);
await client.connect({ apiKey: '<api key>', model: 'gpt-4o-mini-realtime-preview', initialSessionConfig: { instructions: 'Speak like a pirate', voice: 'ash', modalities: ['text', 'audio'], inputAudioFormat: 'pcm16', outputAudioFormat: 'pcm16', },});
// optionally for WebSocketsclient.on('audio', (newAudio) => {});
client.sendAudio(audioBuffer);