peppa/apps/web/app/api/live/route.ts

26 lines
1.2 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();
try {
const scopes = parseScopes(new URL(req.url).searchParams.get("scope"));
// PEP-224: only `features` (state of platform features, no user data) is open without a sign-in.
if (!user && scopes.some((s) => s !== "features")) return fail(401, m("Нужен вход"));
return await liveSse(db, user, scopes, req.signal);
} catch (e) {
if (e instanceof LiveError) return fail(e.status, e.message);
console.error("live:", e);
return fail(500, m("Ошибка потока"));
}
}