import { RealtimeAgent, RealtimeSession } from "@openai/agents/realtime";
import { tool } from "@openai/agents";
import { z } from "zod";
const agent = new RealtimeAgent({
name: "AgentName",
instructions: "Your agent instructions here",
voice: "alloy", // or "echo", "fable", "onyx", "nova", "shimmer"
tools: [/* array of tools */],
});
const session = new RealtimeSession(agent, {
model: "gpt-realtime-mini",
});
const session = new RealtimeSession(agent, {
model: "gpt-realtime-mini",
config: {
audio: {
input: {
turnDetection: {
type: "semantic_vad",
eagerness: "low",
create_response: false,
interrupt_response: false,
},
},
},
},
});
// Connect with API key
await session.connect({ apiKey: "your-api-key" });
// Or with ephemeral token
await session.connect({ apiKey: ephemeralToken });
// Send a text message
await session.sendMessage("Hello, agent!");
// Send a message after connection
session.connect({ apiKey: token })
.then(() => session.sendMessage("Initial message"));
session.interrupt();
session.close();
const myTool = tool({
name: "toolName",
description: "What this tool does",
parameters: z.object({
param1: z.string().describe("Parameter description"),
param2: z.number().optional(),
}),
execute: async ({ param1, param2 }) => {
// Tool logic here
return "Result string";
},
});
const arrayTool = tool({
name: "arrayTool",
description: "Tool with array parameter",
parameters: z.object({
items: z.array(z.string()),
}),
execute: async ({ items }) => {
console.log(items);
return "Processed";
},
});
const noParamTool = tool({
name: "noParamTool",
description: "Tool without parameters",
parameters: z.object({}),
execute: async () => {
// Logic here
return "Result";
},
});