Skip to content

Commit c895741

Browse files
author
Ramyak Mehra
committed
feat: update realtime agents docs with latest examples
1 parent ccc34f3 commit c895741

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

src/content/docs/realtime/agents/getting-started.mdx

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ head:
99
description: Deploy your first Realtime Agent using the CLI.
1010
---
1111

12-
import { Render, PackageManagers, WranglerConfig, TypeScriptExample } from "~/components";
12+
import {
13+
Render,
14+
PackageManagers,
15+
WranglerConfig,
16+
TypeScriptExample,
17+
} from "~/components";
1318

1419
:::caution
1520
This guide is experimental, Realtime agents will be consolidated into the [Agents SDK](/agents/) in a future release
@@ -80,7 +85,13 @@ Update the `index.ts` file in your `hello-agent` application directory with the
8085
<TypeScriptExample filename="index.ts">
8186

8287
```ts
83-
import { DeepgramSTT, TextComponent, RealtimeKitTransport, ElevenLabsTTS, RealtimeAgent } from '@cloudflare/realtime-agents';
88+
import {
89+
DeepgramSTT,
90+
TextComponent,
91+
RealtimeKitTransport,
92+
ElevenLabsTTS,
93+
RealtimeAgent,
94+
} from "@cloudflare/realtime-agents";
8495

8596
class MyTextProcessor extends TextComponent {
8697
env: Env;
@@ -90,11 +101,15 @@ class MyTextProcessor extends TextComponent {
90101
this.env = env;
91102
}
92103

93-
async onTranscript(text: string, reply: (text: string) => void) {
94-
const { response } = await this.env.AI.run('@cf/meta/llama-3.1-8b-instruct', {
104+
async onTranscript(
105+
text: string,
106+
reply: (text: string | ReadableStream<Uint8Array>) => void,
107+
) {
108+
const stream = await this.env.AI.run("@cf/meta/llama-3.1-8b-instruct", {
95109
prompt: text,
110+
stream: true,
96111
});
97-
reply(response!);
112+
reply(stream);
98113
}
99114
}
100115

@@ -103,11 +118,17 @@ export class MyAgent extends RealtimeAgent<Env> {
103118
super(ctx, env);
104119
}
105120

106-
async init(agentId: string, meetingId: string, authToken: string, workerUrl: string, accountId: string, apiToken: string) {
121+
async init(
122+
agentId: string,
123+
authToken: string,
124+
workerUrl: string,
125+
accountId: string,
126+
apiToken: string,
127+
) {
107128
// Construct your text processor for generating responses to text
108129
const textProcessor = new MyTextProcessor(this.env);
109130
// Construct a Meeting object to join the RTK meeting
110-
const rtkTransport = new RealtimeKitTransport(meetingId, authToken);
131+
const rtkTransport = new RealtimeKitTransport(authToken);
111132

112133
// Construct a pipeline to take in meeting audio, transcribe it using
113134
// Deepgram, and pass our generated responses through ElevenLabs to
@@ -131,10 +152,10 @@ export class MyAgent extends RealtimeAgent<Env> {
131152
// The RTK meeting object is accessible to us, so we can register handlers
132153
// on various events like participant joins/leaves, chat, etc.
133154
// This is optional
134-
meeting.participants.joined.on('participantJoined', (participant) => {
155+
meeting.participants.joined.on("participantJoined", (participant) => {
135156
textProcessor.speak(`Participant Joined ${participant.name}`);
136157
});
137-
meeting.participants.joined.on('participantLeft', (participant) => {
158+
meeting.participants.joined.on("participantLeft", (participant) => {
138159
textProcessor.speak(`Participant Left ${participant.name}`);
139160
});
140161

@@ -151,43 +172,49 @@ export class MyAgent extends RealtimeAgent<Env> {
151172
export default {
152173
async fetch(request, env, _ctx): Promise<Response> {
153174
const url = new URL(request.url);
154-
const meetingId = url.searchParams.get('meetingId');
155-
if (!meetingId) {
175+
176+
const agentId = url.searchParams.get("agentId");
177+
if (!agentId) {
156178
return new Response(null, { status: 400 });
157179
}
158180

159-
const agentId = meetingId;
160-
const agent = env.MY_AGENT.idFromName(meetingId);
181+
const agent = env.MY_AGENT.idFromName(agentId);
161182
const stub = env.MY_AGENT.get(agent);
162183
// The fetch method is implemented for handling internal pipeline logic
163-
if (url.pathname.startsWith('/agentsInternal')) {
184+
if (url.pathname.startsWith("/agentsInternal")) {
164185
return stub.fetch(request);
165186
}
166187

167188
// Your logic continues here
168189
switch (url.pathname) {
169-
case '/init':
190+
case "/init":
170191
// This is the authToken for joining a meeting, it can be passed
171192
// in query parameters as well if needed
172-
const authHeader = request.headers.get('Authorization');
193+
const authHeader = request.headers.get("Authorization");
173194
if (!authHeader) {
174195
return new Response(null, { status: 401 });
175196
}
176197

177198
// We just need the part after `Bearer `
178-
await stub.init(agentId, meetingId, authHeader.split(' ')[1], url.host, env.ACCOUNT_ID, env.API_TOKEN);
199+
await stub.init(
200+
agentId,
201+
authHeader.split(" ")[1],
202+
url.host,
203+
env.ACCOUNT_ID,
204+
env.API_TOKEN,
205+
);
179206

180207
return new Response(null, { status: 200 });
181-
case '/deinit':
208+
case "/deinit":
182209
await stub.deinit();
183210
return new Response(null, { status: 200 });
184211
}
185212

186213
return new Response(null, { status: 404 });
187214
},
188215
} satisfies ExportedHandler<Env>;
189-
190216
```
217+
191218
</TypeScriptExample>
192219

193220
The Realtime Agents SDK provides several elements that work together to create an end-to-end pipeline
@@ -267,7 +294,7 @@ Finally, to invoke the worker, we need to generate a RealtimeKit token from the
267294
Finally, invoke the worker to make the agent join a meeting:
268295

269296
```sh
270-
curl -X POST https://hello-agent.<YOUR_SUBDOMAIN>.workers.dev/init?meetingId=<REALTIME_KIT_MEETING_ID> -H "Authorization: Bearer <REALTIME_KIT_AUTH_TOKEN>"
297+
curl -X POST https://hello-agent.<YOUR_SUBDOMAIN>.workers.dev/init?agentId=<UNIQUE_AGENT_ID> -H "Authorization: Bearer <REALTIME_KIT_AUTH_TOKEN>"
271298
```
272299

273300
## Related resources

0 commit comments

Comments
 (0)