Something went wrong. Try again.
My version of https://github.com/vercel-labs/fx
Something went wrong. Try again.
123456789101112131415161718192021222324252627import { createFxAgent } from 'libfx'import { model } from '../shared/model.mjs'import { errorResponse, gatewayFetch, readPrompt } from '../shared/gateway.mjs'
export async function POST(request) { try { const prompt = await readPrompt(request) const agent = await createFxAgent({ apiKey: process.env.AI_GATEWAY_API_KEY, model, fetch: gatewayFetch }) async function* reply() { try { const turn = agent.prompt(prompt, { signal: request.signal }) for await (const event of turn) { if (event.type === 'text_delta') yield event.delta } await turn.result } finally { await agent.close() } } return new Response(ReadableStream.from(reply()).pipeThrough(new TextEncoderStream()), { headers: { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store' }, }) } catch (error) { return errorResponse(error) }}