Skip to content

Commit fa1b892

Browse files
committed
Moves to use ephemeral tokens
1 parent 60fe011 commit fa1b892

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

public/script.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ peerConnection.ontrack = (event) => {
3636
document.body.appendChild(el);
3737
};
3838

39-
const dataChannel = peerConnection.createDataChannel('response');
39+
const dataChannel = peerConnection.createDataChannel('oai-events');
4040

4141
function configureData() {
4242
console.log('Configuring data channel');
@@ -137,22 +137,30 @@ navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
137137

138138
peerConnection.createOffer().then((offer) => {
139139
peerConnection.setLocalDescription(offer);
140+
fetch('/session')
141+
.then((tokenResponse) => tokenResponse.json())
142+
.then((data) => {
143+
const EPHEMERAL_KEY = data.result.client_secret.value;
144+
const baseUrl = 'https://api.openai.com/v1/realtime';
145+
const model = 'gpt-4o-realtime-preview-2024-12-17';
146+
fetch(`${baseUrl}?model=${model}`, {
147+
method: 'POST',
148+
body: offer.sdp,
149+
headers: {
150+
Authorization: `Bearer ${EPHEMERAL_KEY}`,
151+
'Content-Type': 'application/sdp',
152+
},
153+
})
154+
.then((r) => r.text())
155+
.then((answer) => {
156+
// Accept answer from Realtime WebRTC API
157+
peerConnection.setRemoteDescription({
158+
sdp: answer,
159+
type: 'answer',
160+
});
161+
});
162+
});
140163

141164
// Send WebRTC Offer to Workers Realtime WebRTC API Relay
142-
fetch('/rtc-connect', {
143-
method: 'POST',
144-
body: offer.sdp,
145-
headers: {
146-
'Content-Type': 'application/sdp',
147-
},
148-
})
149-
.then((r) => r.text())
150-
.then((answer) => {
151-
// Accept answer from Realtime WebRTC API
152-
peerConnection.setRemoteDescription({
153-
sdp: answer,
154-
type: 'answer',
155-
});
156-
});
157165
});
158166
});

src/index.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
import { Hono } from 'hono';
2+
import { cors } from 'hono/cors';
23

34
const app = new Hono<{ Bindings: Env }>();
5+
app.use(cors());
46

57
const DEFAULT_INSTRUCTIONS = `You are helpful and have some tools installed.
68
79
In the tools you have the ability to control a robot hand.
810
`;
911

10-
app.post('/rtc-connect', async (c) => {
11-
const body = await c.req.text();
12-
const url = new URL('https://api.openai.com/v1/realtime');
13-
url.searchParams.set('model', 'gpt-4o-realtime-preview-2024-12-17');
14-
url.searchParams.set('instructions', DEFAULT_INSTRUCTIONS);
15-
url.searchParams.set('voice', 'ash');
16-
17-
const response = await fetch(url.toString(), {
18-
method: 'POST',
19-
body,
12+
// Learn more: https://platform.openai.com/docs/api-reference/realtime-sessions/create
13+
app.get('/session', async (c) => {
14+
const response = await fetch("https://api.openai.com/v1/realtime/sessions", {
15+
method: "POST",
2016
headers: {
21-
Authorization: `Bearer ${c.env.OPENAI_API_KEY}`,
22-
'Content-Type': 'application/sdp',
17+
"Authorization": `Bearer ${c.env.OPENAI_API_KEY}`,
18+
"Content-Type": "application/json",
2319
},
24-
});
25-
26-
if (!response.ok) {
27-
throw new Error(`OpenAI API error: ${response.status}`);
28-
}
29-
const sdp = await response.text();
30-
return c.body(sdp, {
31-
headers: {
32-
'Content-Type': 'application/sdp',
33-
},
34-
});
20+
body: JSON.stringify({
21+
model: "gpt-4o-realtime-preview-2024-12-17",
22+
instructions: DEFAULT_INSTRUCTIONS,
23+
voice: "ash",
24+
}),
25+
});
26+
const result = await response.json();
27+
return c.json({result});
3528
});
3629

30+
3731
export default app;

0 commit comments

Comments
 (0)