99description : 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" ;
18+
19+ :::caution
20+ This guide is experimental, Realtime agents will be consolidated into the [ Agents SDK] ( /agents/ ) in a future release
21+ :::
1322
1423This guide will instruct you through setting up and deploying your first Realtime Agents project. You will use [ Workers] ( /workers/ ) , the Realtime Agents SDK, a Workers AI binding, and a large language model (LLM) to deploy your first AI-powered application on the Cloudflare global network.
1524
@@ -76,7 +85,13 @@ Update the `index.ts` file in your `hello-agent` application directory with the
7685<TypeScriptExample filename = " index.ts" >
7786
7887``` ts
79- 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" ;
8095
8196class MyTextProcessor extends TextComponent {
8297 env: Env ;
@@ -87,9 +102,12 @@ class MyTextProcessor extends TextComponent {
87102 }
88103
89104 async onTranscript(text : string , reply : (text : string ) => void ) {
90- const { response } = await this .env .AI .run (' @cf/meta/llama-3.1-8b-instruct' , {
91- prompt: text ,
92- });
105+ const { response } = await this .env .AI .run (
106+ " @cf/meta/llama-3.1-8b-instruct" ,
107+ {
108+ prompt: text ,
109+ },
110+ );
93111 reply (response ! );
94112 }
95113}
@@ -99,7 +117,14 @@ export class MyAgent extends RealtimeAgent<Env> {
99117 super (ctx , env );
100118 }
101119
102- async init(agentId : string , meetingId : string , authToken : string , workerUrl : string , accountId : string , apiToken : string ) {
120+ async init(
121+ agentId : string ,
122+ meetingId : string ,
123+ authToken : string ,
124+ workerUrl : string ,
125+ accountId : string ,
126+ apiToken : string ,
127+ ) {
103128 // Construct your text processor for generating responses to text
104129 const textProcessor = new MyTextProcessor (this .env );
105130 // Construct a Meeting object to join the RTK meeting
@@ -127,10 +152,10 @@ export class MyAgent extends RealtimeAgent<Env> {
127152 // The RTK meeting object is accessible to us, so we can register handlers
128153 // on various events like participant joins/leaves, chat, etc.
129154 // This is optional
130- meeting .participants .joined .on (' participantJoined' , (participant ) => {
155+ meeting .participants .joined .on (" participantJoined" , (participant ) => {
131156 textProcessor .speak (` Participant Joined ${participant .name } ` );
132157 });
133- meeting .participants .joined .on (' participantLeft' , (participant ) => {
158+ meeting .participants .joined .on (" participantLeft" , (participant ) => {
134159 textProcessor .speak (` Participant Left ${participant .name } ` );
135160 });
136161
@@ -147,7 +172,7 @@ export class MyAgent extends RealtimeAgent<Env> {
147172export default {
148173 async fetch(request , env , _ctx ): Promise <Response > {
149174 const url = new URL (request .url );
150- const meetingId = url .searchParams .get (' meetingId' );
175+ const meetingId = url .searchParams .get (" meetingId" );
151176 if (! meetingId ) {
152177 return new Response (null , { status: 400 });
153178 }
@@ -156,34 +181,41 @@ export default {
156181 const agent = env .MY_AGENT .idFromName (meetingId );
157182 const stub = env .MY_AGENT .get (agent );
158183 // The fetch method is implemented for handling internal pipeline logic
159- if (url .pathname .startsWith (' /agentsInternal' )) {
184+ if (url .pathname .startsWith (" /agentsInternal" )) {
160185 return stub .fetch (request );
161186 }
162187
163188 // Your logic continues here
164189 switch (url .pathname ) {
165- case ' /init' :
190+ case " /init" :
166191 // This is the authToken for joining a meeting, it can be passed
167192 // in query parameters as well if needed
168- const authHeader = request .headers .get (' Authorization' );
193+ const authHeader = request .headers .get (" Authorization" );
169194 if (! authHeader ) {
170195 return new Response (null , { status: 401 });
171196 }
172197
173198 // We just need the part after `Bearer `
174- await stub .init (agentId , meetingId , authHeader .split (' ' )[1 ], url .host , env .ACCOUNT_ID , env .API_TOKEN );
199+ await stub .init (
200+ agentId ,
201+ meetingId ,
202+ authHeader .split (" " )[1 ],
203+ url .host ,
204+ env .ACCOUNT_ID ,
205+ env .API_TOKEN ,
206+ );
175207
176208 return new Response (null , { status: 200 });
177- case ' /deinit' :
209+ case " /deinit" :
178210 await stub .deinit ();
179211 return new Response (null , { status: 200 });
180212 }
181213
182214 return new Response (null , { status: 404 });
183215 },
184216} satisfies ExportedHandler <Env >;
185-
186217```
218+
187219</TypeScriptExample >
188220
189221The Realtime Agents SDK provides several elements that work together to create an end-to-end pipeline
0 commit comments