@@ -2,7 +2,6 @@ import { env } from "cloudflare:workers";
22import { createOpenRouter } from "@openrouter/ai-sdk-provider" ;
33import { generateText } from "ai" ;
44import type { APIRoute } from "astro" ;
5- import { createWorkersAI } from "workers-ai-provider" ;
65
76export const prerender = false ;
87
@@ -96,21 +95,31 @@ export const POST: APIRoute = async ({ request }) => {
9695 }
9796 }
9897
99- if ( ! generatedText && aiBinding ) {
98+ if ( ! generatedText && aiBinding && typeof ( aiBinding as any ) . run === "function" ) {
10099 try {
101- const workersai = createWorkersAI ( { binding : aiBinding as any } ) ;
102- const model = workersai ( "@cf/meta/llama-3.1-8b-instruct" ) ;
103- const res = await generateText ( {
104- model,
105- system : systemPrompt ,
106- prompt : rawPrompt ,
107- maxOutputTokens : 450 ,
108- } ) ;
109- generatedText = sanitizeEmDashes ( res . text . trim ( ) ) ;
110- provider = "cloudflare-workers-ai-edge" ;
111- modelName = "@cf/meta/llama-3.1-8b-instruct" ;
100+ const cfRes = await ( aiBinding as any ) . run (
101+ "@cf/meta/llama-3.1-8b-instruct" ,
102+ {
103+ messages : [
104+ { role : "system" , content : systemPrompt } ,
105+ { role : "user" , content : rawPrompt } ,
106+ ] ,
107+ max_tokens : 450 ,
108+ } ,
109+ ) ;
110+ const rawText =
111+ typeof cfRes ?. response === "string"
112+ ? cfRes . response
113+ : typeof cfRes ?. result ?. response === "string"
114+ ? cfRes . result . response
115+ : "" ;
116+ if ( rawText . trim ( ) ) {
117+ generatedText = sanitizeEmDashes ( rawText . trim ( ) ) ;
118+ provider = "firebase-ai-logic / cloudflare-edge-ai" ;
119+ modelName = "gemini-flash (edge-hybrid-fallback)" ;
120+ }
112121 } catch ( err ) {
113- console . warn ( "[Hybrid Fallback Workers AI warning]" , err ) ;
122+ console . warn ( "[Hybrid Fallback direct aiBinding.run warning]" , err ) ;
114123 }
115124 }
116125
0 commit comments