route.ts 910 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { streamText } from "ai";
  2. import { gateway } from "@ai-sdk/gateway";
  3. import { buildUserPrompt, type Spec } from "@json-render/core";
  4. import { pdfCatalog } from "@/lib/catalog";
  5. export const maxDuration = 60;
  6. const SYSTEM_PROMPT = pdfCatalog.prompt();
  7. const DEFAULT_MODEL = "anthropic/claude-haiku-4.5";
  8. export async function POST(req: Request) {
  9. const { prompt, startingSpec } = (await req.json()) as {
  10. prompt: string;
  11. startingSpec?: Spec | null;
  12. };
  13. if (!prompt || typeof prompt !== "string") {
  14. return Response.json({ error: "prompt is required" }, { status: 400 });
  15. }
  16. const userPrompt = buildUserPrompt({
  17. prompt,
  18. currentSpec: startingSpec,
  19. });
  20. const result = streamText({
  21. model: gateway(process.env.AI_GATEWAY_MODEL ?? DEFAULT_MODEL),
  22. system: SYSTEM_PROMPT,
  23. prompt: userPrompt,
  24. temperature: 0.7,
  25. });
  26. return result.toTextStreamResponse();
  27. }