mirror of
https://github.com/vercel-labs/fx.git
synced 2026-10-28 18:25:19 +08:00
27 lines
926 B
JavaScript
27 lines
926 B
JavaScript
import { 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)
|
|
}
|
|
}
|