Skip to content

Commit 9947bc4

Browse files
authored
Merge pull request #3 from activescott/feat/emitter-event-target
feat: Migrates to the more browser-familiar EventTarget rather than the node-like EventEmitter. Also misc minor fixes made throughout as it is used in production
2 parents b98f5a3 + b369144 commit 9947bc4

File tree

12 files changed

+2409
-200
lines changed

12 files changed

+2409
-200
lines changed

.github/workflows/build.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
on:
2-
# Runs on pushes targeting the default branch
32
push:
4-
branches: ["main"]
3+
branches: ["main", "beta"]
4+
pull_request:
5+
branches: ["main", "beta"]
56

67
# Allows you to run this workflow manually from the Actions tab
78
workflow_dispatch:
@@ -44,6 +45,7 @@ jobs:
4445
npx semantic-release@24
4546
4647
pages:
48+
if: github.ref == 'refs/heads/main'
4749
needs: build
4850
runs-on: ubuntu-latest
4951
permissions:

apps/browser-example/src/App.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useKeyManager } from "./hooks/key"
44
import { OfficialSDKWebSocketExample } from "./pages/OfficialSDKWebSocketExample"
55
import { WebRTCExample } from "./pages/WebRTCExample"
66
import { PageProps } from "./pages/props"
7+
import { RealtimeServerEventEvent } from "@tsorta/browser/WebRTC/events"
78

89
export function App() {
910
const [events, setEvents] = useState<any[]>([])
@@ -18,8 +19,9 @@ export function App() {
1819
}
1920
}, [key])
2021

21-
const onServerEvent = (event: any) =>
22-
setEvents((events) => [...events, event])
22+
const onServerEvent = (event: RealtimeServerEventEvent) => {
23+
setEvents((events) => [...events, event.event])
24+
}
2325

2426
const [routes] = useState({
2527
WebRTC: {

apps/browser-example/src/pages/WebRTCExample.tsx

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactNode, useRef, useState } from "react"
1+
import { ReactNode, useCallback, useRef, useState } from "react"
22
import { RealtimeSessionView } from "../components/RealtimeSessionView"
33
import { RealtimeClient } from "@tsorta/browser/WebRTC"
44
import { PageProps } from "./props"
@@ -14,30 +14,40 @@ export function WebRTCExample({
1414

1515
const [client, setClient] = useState<RealtimeClient | undefined>(undefined)
1616

17-
async function startSession(): Promise<void> {
18-
if (!apiKey) {
19-
throw new Error("API key is required")
20-
}
21-
if (!audioElementRef?.current) {
22-
throw new Error("Audio element not found")
23-
}
24-
25-
const client = new RealtimeClient(
26-
navigator,
27-
() => apiKey,
28-
audioElementRef.current,
29-
{ model: "gpt-4o-realtime-preview-2024-12-17" }
30-
)
31-
client.on("event", onServerEvent)
32-
setClient(client)
33-
await client.start()
34-
onSessionStatusChanged("recording")
35-
}
36-
37-
async function stopSession(): Promise<void> {
38-
await client?.stop()
39-
onSessionStatusChanged("stopped")
40-
}
17+
const startSession = useCallback(
18+
async function startSession(): Promise<void> {
19+
if (!apiKey) {
20+
throw new Error("API key is required")
21+
}
22+
if (!audioElementRef?.current) {
23+
throw new Error("Audio element not found")
24+
}
25+
26+
const client = new RealtimeClient(
27+
navigator,
28+
() => apiKey,
29+
audioElementRef.current,
30+
{ model: "gpt-4o-realtime-preview-2024-12-17" }
31+
)
32+
setClient(client)
33+
34+
client.addEventListener("serverEvent", (event) => {
35+
onServerEvent(event)
36+
})
37+
await client.start()
38+
39+
onSessionStatusChanged("recording")
40+
},
41+
[apiKey, audioElementRef, onServerEvent, onSessionStatusChanged, navigator]
42+
)
43+
44+
const stopSession = useCallback(
45+
async function stopSession(): Promise<void> {
46+
await client?.stop()
47+
onSessionStatusChanged("stopped")
48+
},
49+
[client, onSessionStatusChanged]
50+
)
4151

4252
return (
4353
<div className="container">
@@ -46,7 +56,7 @@ export function WebRTCExample({
4656
This example demonstrates how to use the OpenAI Realtime API directly.
4757
It is using the TypeScript client from the article{" "}
4858
<a href="https://scott.willeke.com/ai-typescript-client-for-openai-realtime-api">
49-
AI Learns to Listen: TypeScript Client for OpenAIs Realtime API
59+
AI Learns to Listen: TypeScript Client for OpenAI's Realtime API
5060
</a>{" "}
5161
by Scott Willeke.
5262
</p>

0 commit comments

Comments
 (0)