import { NextResponse } from "next/server" import { mcpCallTool, FLEET_BOARD_MCP_URL, readChatThreads } from "@/app/lib/fleet/fleetSource" export const dynamic = "force-dynamic" /** * GET /api/fleet/owner/chat — Thread-Liste + Nachrichten (Kernfeature 5). * POST /api/fleet/owner/chat — neue Owner-Nachricht in einen Thread via * board-service submit_owner_task (type=chat, thread_id, topic) — der Tower * schreibt NIE direkt in den Spool (Single-Writer-Disziplin via board-service). * from=tower-owner, to=ceo; CEO-Antworten erscheinen als ceo->tower-owner? * NEIN: to muss 9er-Org sein -> CEO-Antworten sind Tasks von ceo mit * thread_id + reply_to; der Chat-View rendert alle type=chat-Tasks eines * thread_id unabhaengig von from/to (solange from in 9er-Org oder tower-owner). */ export async function GET() { try { const threads = readChatThreads() return NextResponse.json( { threads, count: threads.length }, { headers: { "Cache-Control": "no-store" } } ) } catch (err) { return NextResponse.json( { error: `OWNER_CHAT_FAILED: ${err instanceof Error ? err.message : String(err)}` }, { status: 500 } ) } } export async function POST(request: Request) { try { const body = (await request.json()) as { thread_id?: string; topic?: string; message?: string } const thread_id = String(body.thread_id || "").trim() const topic = String(body.topic || "").trim() const message = String(body.message || "").trim() if (!thread_id || !message) { return NextResponse.json({ error: "thread_id und message sind Pflicht" }, { status: 400 }) } const res = await mcpCallTool(FLEET_BOARD_MCP_URL, "submit_owner_task", { to: "ceo", type: "chat", priority: "P3", topic: topic || thread_id, thread_id, body: message, requires_ack: false, }) if (!res.ok) { return NextResponse.json({ error: `submit_owner_task: ${res.error}` }, { status: 500 }) } return NextResponse.json(res.data, { headers: { "Cache-Control": "no-store" } }) } catch (err) { getErrLog(err) } } function getErrLog(err: unknown) { return NextResponse.json( { error: `OWNER_CHAT_POST_FAILED: ${err instanceof Error ? err.message : String(err)}` }, { status: 500 } ) }