Skip to content

The Open Standard for Generative UI

3.9 KB
Raw Blame
import { createSupabaseServer } from "@/lib/supabase/server";
import { cloudInstructions } from "@/lib/cloud-prompt";
import { NextRequest, NextResponse } from "next/server";
import OpenAI from "openai";
import type { ChatCompletionMessageParam } from "openai/resources/chat/completions.mjs";

const MODEL = "google/gemini-3.6-flash-free";

/**
 * POST /api/chat
 *
 * Accepts an OpenAI-format message array and an optional threadId.
 * Streams the assistant reply as Server-Sent Events (openai-completions format).
 * After the stream finishes, persists the full conversation to Supabase so
 * that loadThread can restore it when the user reopens the thread.
 */
export async function POST(req: NextRequest) {
  const { messages, threadId } = (await req.json()) as {
    messages: ChatCompletionMessageParam[];
    threadId?: string | null;
  };

  // Create the Supabase client before streaming so the request context
  // (cookies) is still available when we persist messages afterwards.
  const supabase = await createSupabaseServer();

  // Chat Completions → POST /v1/embed/chat/completions
  // Matches openAIAdapter / openAIMessageFormat and the Postgres message rows.
  const client = new OpenAI({
    apiKey: process.env.THESYS_API_KEY,
    baseURL: "https://api.thesys.dev/v1/embed",
  });

  const createStream = () =>
    client.chat.completions.create({
      model: MODEL,
      messages: [
        { role: "system", content: cloudInstructions() },
        ...messages.filter((message) => message.role !== "system"),
      ],
      stream: true,
    });
  let stream: Awaited<ReturnType<typeof createStream>>;
  try {
    stream = await createStream();
  } catch (err) {
    // Return the real reason (e.g. "401 Incorrect API key provided") so the
    // chat can show it, instead of a bare 500.
    const status =
      err instanceof OpenAI.APIError && typeof err.status === "number" ? err.status : 500;
    return NextResponse.json(
      { error: err instanceof Error ? err.message : String(err) },
      { status },
    );
  }

  let assistantContent = "";
  const encoder = new TextEncoder();

  const readable = new ReadableStream({
    async start(controller) {
      const enqueue = (data: Uint8Array) => {
        try {
          controller.enqueue(data);
        } catch {
          // Controller already closed (client disconnected)
        }
      };

      for await (const chunk of stream) {
        const delta = chunk.choices[0]?.delta;
        if (delta?.content) {
          assistantContent += delta.content;
        }
        enqueue(encoder.encode(`data: ${JSON.stringify(chunk)}\n\n`));
      }

      enqueue(encoder.encode("data: [DONE]\n\n"));
      controller.close();

      // ── Persist the full conversation ──────────────────────────────────────
      // We replace all messages for this thread on every turn so that the
      // stored history always reflects the current state.  This is simple and
      // correct; for large threads you may prefer an append-only strategy.
      if (threadId) {
        try {
          const allMessages: ChatCompletionMessageParam[] = [
            ...messages,
            { role: "assistant", content: assistantContent },
          ];

          await supabase.from("messages").delete().eq("thread_id", threadId);

          await supabase.from("messages").insert(
            allMessages.map((m) => ({
              thread_id: threadId,
              role: m.role,
              content:
                typeof m.content === "string" ? m.content : JSON.stringify(m.content),
            })),
          );
        } catch (err) {
          console.error("[chat] Failed to persist messages:", err);
        }
      }
    },
  });

  return new Response(readable, {
    headers: {
      "Content-Type": "text/event-stream",
      "Cache-Control": "no-cache, no-transform",
      Connection: "keep-alive",
    },
  });
}