1// @ts-ignore
2import { OpenAI } from "https://esm.town/v/std/openai?v=4";
3
4// --- AI BEHAVIORAL GUIDELINES ---
363 }
364
365 const openai = new OpenAI();
366
367 const completion = await openai.chat.completions.create({
368 model: "gpt-4o",
369 messages: [
1// @ts-ignore
2import { OpenAI } from "https://esm.town/v/std/openai?v=4";
3
4// --- TYPE DEFINITIONS ---
695 if (req.method === "POST") {
696 try {
697 const openai = new OpenAI();
698 const body = await req.json();
699 let prompt = "";
717 }
718
719 const completion = await openai.chat.completions.create({
720 model: "gpt-4o",
721 messages: [
1// @ts-ignore
2import { OpenAI } from "https://esm.town/v/std/openai?v=4";
3
4// --- TYPE DEFINITIONS ---
277 state: State,
278 options: { N?: number; T?: Exponents; preset?: "W1" | "W3" | "W5"; theme?: string },
279 openai: OpenAI,
280): Promise<State> {
281 const s = clone(state);
283
284 if (theme) {
285 const completion = await openai.chat.completions.create({
286 model: "gpt-4o",
287 messages: [
327}
328
329async function fire(state: State, openai: OpenAI): Promise<{ state: State; result: FireResult }> {
330 if (!state.boss) throw new Error("No boss to fire at.");
331 const s = clone(state);
395 result: { ...resultCore, damageApprox: resultCore.damageFraction.approx },
396 };
397 const completion = await openai.chat.completions.create({
398 model: "gpt-4o",
399 messages: [
584 }
585
586 const openai = new OpenAI();
587 const body = await req.json().catch(() => ({}));
588 if (!isObject(body) || !("action" in body)) {
619 payload.state,
620 { N: payload.N, T: payload.T, preset: payload.preset, theme: payload.theme },
621 openai,
622 );
623 return ok({ state });
625 case "fire": {
626 if (!payload.state) return err("fire requires { state } with a boss present");
627 const { state, result } = await fire(payload.state, openai);
628 return ok({ state, result });
629 }
101_2 or _3) to create a fresh table.
102
103### OpenAI
104
105```ts
106import { OpenAI } from "https://esm.town/v/std/openai";
107const openai = new OpenAI();
108const completion = await openai.chat.completions.create({
109 messages: [
110 { role: "user", content: "Say hello in a creative way" },
1const REALTIME_BASE_URL = "https://api.openai.com/v1/realtime";
2const OPENAI_API_KEY = Deno.env.get("OPENAI_API_KEY");
3if (!OPENAI_API_KEY) {
4 throw new Error("🔴 OpenAI API key not configured");
5}
6
24export function makeHeaders(contentType?: string) {
25 const obj: Record<string, string> = {
26 Authorization: `Bearer ${OPENAI_API_KEY}`,
27 };
28 if (contentType) obj["Content-Type"] = contentType;
9sip.post("/", async (c) => {
10 // Verify the webhook.
11 const OPENAI_SIGNING_SECRET = Deno.env.get("OPENAI_SIGNING_SECRET");
12 if (!OPENAI_SIGNING_SECRET) {
13 console.error("🔴 webhook secret not configured");
14 return c.text("Internal error", 500);
15 }
16 const webhook = new Webhook(OPENAI_SIGNING_SECRET);
17 const bodyStr = await c.req.text();
18 let callId: string | undefined;
1# hello-realtime
2
3**Hello Realtime** is a OpenAI Realtime app that supports both WebRTC and SIP
4(telephone) users. You can access the app via WebRTC at
5[hello-realtime.val.run](https://hello-realtime.val.run), or via SIP by calling
9server-side websocket interface.
10
11If you remix the app, you'll just need to pop in your own `OPENAI_API_KEY` (from
12[platform.openai.com](https://platform.openai.com)), and if you want SIP, the
13`OPENAI_SIGNING_SECRET`.
14
15## Architecture
18 - Browser connects to frontend
19 - creates WebRTC offer
20 - `/rtc` endpoint handles SDP negotiation with OpenAI
21 - observer established to monitor session
222. **SIP Flow**:
4 <meta charset="utf-8" />
5 <meta name="viewport" content="width=device-width, initial-scale=1" />
6 <title>OpenAI Realtime API Voice Agent</title>
7 <style>
8 :root {
6const NR_TYPE = "near_field";
7const INSTRUCTIONS = `
8 Greet the user in English, and thank them for trying the new OpenAI Realtime API.
9 Give them a brief summary based on the list below, and then ask if they have any questions.
10 Answer questions using the information below. For questions outside this scope,
23 - higher audio quality
24 - improved handling of alphanumerics (eg, properly understanding credit card and phone numbers)
25 - support for the OpenAI Prompts API
26 - support for MCP-based tools
27 - auto-truncation to reduce context size
1// @ts-ignore
2import { OpenAI } from "https://esm.town/v/std/openai?v=4";
3
4// --- AI BEHAVIORAL GUIDELINES ---
465 if (req.method === "POST" && action === "getProblem") {
466 try {
467 const openai = new OpenAI();
468 const body = await req.json();
469 const level = typeof body.level === "number" ? body.level : 1;
483 ];
484
485 const completion = await openai.chat.completions.create({
486 model: "gpt-4o",
487 messages: messages,
492 const content = completion.choices[0].message.content;
493 if (!content) {
494 throw new Error("OpenAI returned an empty response.");
495 }
496