リアルタイムトランスポート
デフォルトのトランスポートレイヤー
Section titled “デフォルトのトランスポートレイヤー”WebRTC 経由の接続
Section titled “WebRTC 経由の接続”デフォルトのトランスポートレイヤーは WebRTC を使用します。音声はマイクから録音され、自動で再生されます。
独自のメディアストリームや audio 要素を使用する場合は、セッション作成時に 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 “独自のトランスポートメカニズムの構築”別の speech-to-speech API を使用したい場合や独自のトランスポートメカニズムを持っている場合は、RealtimeTransportLayer
インターフェースを実装し、RealtimeTranportEventTypes
イベントを発火することで独自実装が可能です。
Realtime API との直接的なインタラクション
Section titled “Realtime API との直接的なインタラクション”OpenAI 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);