import {
RealtimeAgent,
RealtimeSession,
tool
} from '@openai/agents/realtime';
import {
z
} from 'zod';
const lookupBooking = tool({
name: 'lookup_booking',
description: 'Find a booking by its reference code.',
parameters: z.object({
ref: z.string()
}),
async execute({
ref
}) {
return `Booking ${ref}: 2 nights, check-in Friday.`;
},
});
const agent = new RealtimeAgent({
name: 'Front desk',
instructions: 'Speak briefly. Confirm details before acting.',
tools: [lookupBooking],
});
// Your server mints this ek_ token via POST /v1/realtime/client_secrets
const {
value
} = await fetch('/api/realtime-token').then((r) => r.json());
const session = new RealtimeSession(agent, {
model: 'gpt-realtime-2.1'
});
await session.connect({
apiKey: value
}); // WebRTC: mic and playback handled