@@ -21,23 +21,16 @@ export class GeminiAgent extends CFAgent<Env, any> {
2121 this . doId = state . id . toString ( ) ;
2222 }
2323
24- @callable ( { streaming : true } )
25- async chat ( stream : any , prompt : string ) {
24+ @callable ( )
25+ async chat ( prompt : string , history ?: any [ ] ) {
2626 // 1. Construct the Cloudflare AI Gateway OpenAI-Compatible URL
27- // CLOUDFLARE_ACCOUNT_ID is a secret, so .get() is likely correct if typed as such
2827 const accountId = await this . env . CLOUDFLARE_ACCOUNT_ID . get ( ) ;
29- // AI_GATEWAY_NAME is a var (string), so no .get()
3028 const gateway = this . env . AI_GATEWAY_NAME ;
31- // CLOUDFLARE_API_TOKEN is a secret
32- // Note: If CLOUDFLARE_API_TOKEN is not in Env, check wrangler.jsonc. It is there.
33- // If it's a string in Env, .get() will fail. If it's a Secret, .get() is needed.
34- // Based on usage of WORKER_API_KEY.get(), we assume secrets use .get().
3529 const apiKey = await this . env . CLOUDFLARE_API_TOKEN . get ( ) ;
3630
3731 const baseURL = await this . env . AI . gateway ( gateway ) . getUrl ( 'worker-ai' ) ;
3832
3933 // 2. Hijack the global process environment to reroute ADK's underlying fetcher
40- // We pass the CF API Token as the "OpenAI" key, and the AI Gateway as the Base URL.
4134 ( globalThis as any ) . process = {
4235 env : {
4336 OPENAI_API_KEY : apiKey ,
@@ -51,7 +44,6 @@ export class GeminiAgent extends CFAgent<Env, any> {
5144 // 3. Initialize ADK with the Cloudflare Workers AI Model
5245 const agent = new LlmAgent ( {
5346 name : "cf_gateway_agent" ,
54- // The exact model requested, routed to Cloudflare Edge GPUs
5547 model : "@cf/openai/gpt-oss-120b" ,
5648 instruction : "You are an elite autonomous agent powered by Cloudflare Workers AI and Google ADK. Provide structured, highly accurate responses." ,
5749 } ) ;
@@ -61,19 +53,18 @@ export class GeminiAgent extends CFAgent<Env, any> {
6153
6254 const eventStream = runner . runAsync ( {
6355 userId : "user" ,
64- sessionId : this . doId , // Use captured Durable Object ID
56+ sessionId : this . doId ,
6557 newMessage : { role : "user" , parts : [ { text : prompt } ] } ,
6658 } ) ;
6759
6860 let fullResponse = "" ;
6961
70- // 5. Stream the chunks directly back to the assistant-ui frontend
62+ // 5. Accumulate the full response
7163 for await ( const event of eventStream ) {
7264 if ( event . content ?. parts ) {
7365 for ( const part of event . content . parts ) {
7466 if ( part . text ) {
7567 fullResponse += part . text ;
76- await stream . write ( part . text ) ;
7768 }
7869 }
7970 }
@@ -89,9 +80,11 @@ export class GeminiAgent extends CFAgent<Env, any> {
8980 status : "idle"
9081 } ) ;
9182
83+ return { response : fullResponse } ;
84+
9285 } catch ( error : any ) {
9386 this . setState ( { ...this . state , status : "error" } ) ;
94- await stream . write ( `\n\n [Agent Error]: ${ error . message } `) ;
87+ return { response : ` [Agent Error]: ${ error . message } ` } ;
9588 }
9689 }
9790}
0 commit comments