コンテンツにスキップ

リアルタイムトランスポート

デフォルトのトランスポートレイヤー

Section titled “デフォルトのトランスポートレイヤー”

デフォルトのトランスポートレイヤーは 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 });
}

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 response
session.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 WebSockets
client.on('audio', (newAudio) => {});
client.sendAudio(audioBuffer);