peppa/apps/web/app/api/live/route.ts
peppa-bot acb6e3fb81
All checks were successful
peppa/lint lint: success за 1с
peppa/typecheck typecheck: success за 46с
peppa/build build: success за 34с
peppa/test test: success за 1325с
peppa-ci / ci (pull_request) Successful in 24m11s
PEP-224: статус функций из мониторинга (сведено с main после PEP-225)
Функция → мониторинги (core/feature-status.ts), серая кнопка входа через eGov и статус MCP,
отказ startEgovFlow, журнал и сообщения в канал. После переезда живых обновлений на
WebSocket (PEP-225): область `features` в LIVE_PLAIN_SCOPES, открыта без входа (SSE и
WebSocket с `anonymous: true` — только открытые области), смотрится по таймеру вместе с
`observe`, смена сбрасывает кэш статусов.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 15:00:03 +00:00

29 lines
1.5 KiB
TypeScript

// Live updates (PEP-37): SSE of `change` (a watched scope changed), `step` (an agent's tool call,
// PEP-33) and `data` events — since PEP-225 the fallback of the tab's WebSocket /api/ws. ?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 q = new URL(req.url).searchParams;
const scopes = parseScopes(q.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("Нужен вход"));
// PEP-225: `since.<scope>=<version>` — what the tab saw before a drop (fallback of the WebSocket).
const since = Object.fromEntries([...q].filter(([k]) => k.startsWith("since.")).map(([k, v]) => [k.slice(6), v]));
return await liveSse(db, user, scopes, req.signal, { since });
} catch (e) {
if (e instanceof LiveError) return fail(e.status, e.message);
console.error("live:", e);
return fail(500, m("Ошибка потока"));
}
}