コンテンツにスキップ

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

既定のトランスポート層は 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 });
}

transport: 'websocket' または OpenAIRealtimeWebSocket インスタンスをセッション作成時に指定すると、 WebRTC の代わりに WebSocket 接続を使用します。これは、たとえば 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 を利用しつつ、より低レベルで 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);