// POST /api/ai  { "prompt": "..." }  → Claude's reply.
// Set ANTHROPIC_API_KEY in your environment (e.g. a .env file) to enable this.
import { createAI } from "@wrnexus/ai";
import type { Context } from "@wrnexus/core";

const ai = createAI(); // reads ANTHROPIC_API_KEY; defaults to claude-opus-4-8

export const POST = async (ctx: Context) => {
  if (!process.env.ANTHROPIC_API_KEY) {
    return Response.json({ error: "Set ANTHROPIC_API_KEY to use AI." }, { status: 501 });
  }
  const { prompt } = await ctx.req.json().catch(() => ({}));
  if (!prompt) return Response.json({ error: "Provide a 'prompt'." }, { status: 400 });

  // Stream the reply back as plain text. Use `ai.generate(prompt)` for a one-shot string.
  return ai.streamResponse(prompt);
};
