peppa/apps/web/app/api/live/route.ts
2026-11-15 14:47:50 +05:00

24 lines
1.0 KiB
TypeScript

// Live updates (PEP-37): SSE of `change` (a watched scope changed — refresh) and `step`
// (an agent's tool call, PEP-33) events. ?scope=home | runners | project:<id> | task:<id> | steps:<sessionId>, comma-separated.
import { LiveError, liveSse, parseScopes } from "@peppa/runner";
import { currentUser } from "@/lib/auth";
import { db } from "@/lib/db";
import { getT } from "@/lib/i18n";
import { m } from "@peppa/shared/i18n";
export const dynamic = "force-dynamic";
const fail = async (status: number, error: string) => Response.json({ error: (await getT()).text(error) }, { status, headers: { "Cache-Control": "no-store" } });
export async function GET(req: Request) {
const user = await currentUser();
if (!user) return fail(401, m("Нужен вход"));
try {
return await liveSse(db, user, parseScopes(new URL(req.url).searchParams.get("scope")), req.signal);
} catch (e) {
if (e instanceof LiveError) return fail(e.status, e.message);
console.error("live:", e);
return fail(500, m("Ошибка потока"));
}
}