import { streamText } from "ai";
import { createOpenAI } from "@ai-sdk/openai";
import { buildAiMessages } from "@/lib/ai/build-messages";
import {
  getOpenAiApiKey,
  getOpenAiBaseUrl,
  getOpenAiModel,
} from "@/lib/ai/config";
import type { AiRequestBody } from "@/lib/ai/types";

export const runtime = "nodejs";

const STREAM_HEADERS = {
  "Cache-Control": "no-cache, no-transform",
  "X-Accel-Buffering": "no",
};

export async function POST(req: Request) {
  try {
    const body = (await req.json()) as AiRequestBody;
    if (!body?.mode || !body.payload) {
      return new Response("请求格式错误", { status: 400 });
    }

    const { system, user } = await buildAiMessages(body);
    const openai = createOpenAI({
      apiKey: getOpenAiApiKey(),
      baseURL: getOpenAiBaseUrl(),
    });

    const result = streamText({
      temperature: 0.5,
      model: openai(getOpenAiModel()),
      messages: [
        { role: "system", content: system },
        { role: "user", content: user },
      ],
      maxRetries: 0,
    });

    const response = result.toTextStreamResponse();
    for (const [key, value] of Object.entries(STREAM_HEADERS)) {
      response.headers.set(key, value);
    }
    return response;
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    return new Response(message, {
      status: 500,
      headers: {
        "Content-Type": "text/plain; charset=utf-8",
        ...STREAM_HEADERS,
      },
    });
  }
}
