import { REPORTS_ROOT } from "@/lib/paths"; import { LogError, markHit, readLog } from "@/lib/research/log.mjs"; import { fail, ok } from "../_respond"; export const dynamic = "force-dynamic"; // The reading log (lib/research/log.mjs), umtool-side under // REPORTS_ROOT/.umtool/research/sessions/ -- never in the corpus. // // GET ?session=<16 hex> -> { marks, error? } // POST { session, key: "/", state: read|skipped|unread, // query?, scope? } -> { key, state, at, marks } export async function GET(request: Request) { const session = new URL(request.url).searchParams.get("session") ?? ""; const log = await readLog(REPORTS_ROOT, session); if (log.error === "not a session id") return fail(new LogError(log.error)); return ok(log); } export async function POST(request: Request) { try { const body = (await request.json().catch(() => null)) as Record | null; if (!body) throw new LogError("expected a JSON body"); const scope = body.scope && typeof body.scope === "object" && !Array.isArray(body.scope) ? (body.scope as Record) : undefined; return ok( await markHit(REPORTS_ROOT, String(body.session ?? ""), String(body.key ?? ""), String(body.state ?? "") as "read", { ...(typeof body.query === "string" ? { query: body.query } : {}), // The scope is a description for a person reading the file: kept small. ...(scope && JSON.stringify(scope).length <= 4000 ? { scope } : {}), }), ); } catch (e) { return fail(e); } }