Files
explicata/server/links/routes.ts
T
thalladaandClaude Opus 5.5 9f8245e6d7 Links: drop what the scanner computed or accepted for nobody
scan() also built a Related list that no caller reads (the page asks /links/related, which
related() answers); the `within` option was never sent; near() was only ever given a vector, so its
embed-the-text fallback and null answer could not happen. extract.ts and words.ts re-exported
phraseKey and singular from src/core/links, where they now come from directly. The suggestions
are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
2026-11-17 16:10:45 -05:00

196 lines
13 KiB
TypeScript

/**
* HTTP routes for one library's link discovery, under /api/l/<id>/links/ (server/bundle.ts).
*
* POST /api/links/scan { node, text?, title?, top?, topTitle?, ancestors?, occupied?, rejected?, forced?, save?, review? }
* → { suggestions, unresolved, degraded, stats, entries?, apply?, review? }; save (default
* true) records the suggestions as pending for the node, and `entries` is then everything
* stored for it, with determinations. review ("all", or "unmarked" to leave out [[marks]],
* which a person typed) has the model reviewer judge what is undecided; `apply` is then the
* approved links handed to the caller to make, and `review` the reviewer's report
* GET /api/links/pending ?node=<id>&subtree=1&tier=auto|suggest&limit=&offset=&all=1&claim=1
* → { total, suggestions: [{ …, nodeTitle, top, path, sentence }], apply? }; node alone is
* that node, with subtree=1 everything under it too, and without node the whole library;
* all=1 lists every entry whatever was decided, with its determinations; claim=1 hands
* out the approved links no page has made yet (in `apply`), to be made by the caller
* POST /api/links/accept { ids, author? } → applied with applyLinks as one commit by "linker"
* POST /api/links/reject { ids, author? } → the phrases recorded in each node's `nolink` (one commit)
* POST /api/links/decide { ids, link, by } → a person's decision, made in their own replica, recorded
* GET /api/links/related ?node=<id> → { related, degraded }; or POST { node, text, title, top, topTitle, ancestors }
* GET /api/links/nearby ?node=<id>&k=5 → { nearby: [{ handle, title, top }] } (hybrid search on the node);
* or POST { node, text, title, ancestors }
* POST /api/links/jobs { scope: "node" | "library", node?, review? } → the job's state (node: a node's id, scanned with
* everything under it; on the library root, the whole library); it runs in the background
* (with review, the model reviewer judges what it finds, within IE_REVIEW_CALLS calls)
* GET /api/links/jobs → { job } (the current or last one)
* DELETE /api/links/jobs → cancel the running job
* GET /api/links/status → embeddings, the node index, the pending count, the job
*
* Job progress is also announced on /api/events as { linkJob: state }.
*/
import type { IncomingMessage, ServerResponse } from "node:http";
import { applyLinks, type LinkSpec } from "../../src/core/links";
import { descendants, isLibraryRoot, label, tops, type Doc, type NodeId, type Op, type Snap } from "../../src/core/model";
import type { Embedder } from "../embed";
import { HttpError, abortOnClose, readJson as readBody, sendJson as send } from "../util";
import type { Jobs } from "./jobs";
import type { NodeIndex } from "./nodeIndex";
import type { PendingStore } from "./pending";
import { SCAN_CALLS, describe, type Reviewer } from "./review";
import type { Linker, ScanInput, Suggestion } from "./scan";
interface LinkDeps {
main: () => Doc;
linker: () => Linker;
index: NodeIndex;
pending: PendingStore;
jobs: Jobs;
embedder: Embedder | null;
reviewer?: Reviewer;
/** A server-side write: saved soon and announced to every replica. */
wrote: () => void;
}
class Bad extends HttpError {
constructor(message: string) {
super(400, message);
}
}
const readJson = <T>(req: IncomingMessage) => readBody<T>(req, 4 * 1024 * 1024);
/**
* A node and everything under it (or, for none or the library root, the whole library but the
* Wikipedia container), in reading order: the sections and notes with text.
*/
export function scopeNodes(snap: Snap, node: NodeId | null): NodeId[] {
const roots = node && !isLibraryRoot(snap, node) ? [node] : tops(snap);
const out: NodeId[] = [];
for (const r of roots) for (const id of [r, ...descendants(snap, r)]) if (snap.nodes[id].body.trim() && !isLibraryRoot(snap, id)) out.push(id);
return out;
}
export function linkRoutes(deps: LinkDeps) {
return async function handle(req: IncomingMessage, res: ServerResponse, url: URL): Promise<boolean> {
if (!url.pathname.startsWith("/api/links/")) return false;
const route = url.pathname.slice("/api/links/".length).replace(/\/$/, "");
const method = req.method ?? "GET";
try {
const snap = deps.main().snap();
if (route === "scan" && method === "POST") {
const input = await readJson<ScanInput & { save?: boolean; review?: "all" | "unmarked" }>(req);
if (typeof input.node !== "string" || !input.node) throw new Bad("give the node's id");
if (input.text !== undefined && typeof input.text !== "string") throw new Bad("text must be a string");
if ((input.text?.length ?? 0) > 200_000) throw new Bad("text is too long");
const signal = abortOnClose(res);
// What the reviewer rejected here is not looked for again, any more than what a person declined.
const reviewed = deps.pending.rejectedKeys(input.node);
const rejected = reviewed.length ? [...(input.rejected ?? snap.nodes[input.node]?.nolink ?? []), ...reviewed] : input.rejected;
const r = await deps.linker().scan({ ...input, rejected, trace: undefined }, signal);
if (input.save === false) return send(res, 200, r), true;
deps.pending.put(input.node, r.suggestions);
if (!input.review) return send(res, 200, { ...r, entries: deps.pending.of(input.node) }), true;
// The reviewer sees what this scan found that nobody has judged yet (a person's [[marks]] are
// theirs, not its), within the scan's budget. Found against the text the caller sent, which
// may be ahead of the server's copy.
const found = new Set(r.suggestions.map((s) => s.id));
const mine = deps.pending.of(input.node).filter((s) => found.has(s.id));
const todo = mine.filter((s) => !s.review && !s.decided && (input.review === "all" || s.kind !== "forced"));
const report = deps.reviewer ? await deps.reviewer.review(todo, { calls: 0, max: SCAN_CALLS }, signal, { node: input.node, text: input.text, title: input.title, top: input.top }) : { approved: 0, rejected: 0, calls: 0, left: todo.length, fallback: "no reviewer" };
const apply = deps.pending.handOut(mine);
return send(res, 200, { ...r, entries: deps.pending.of(input.node), apply, review: report }), true;
}
if (route === "pending" && method === "GET") {
const node = url.searchParams.get("node") as NodeId | null;
const subtree = url.searchParams.get("subtree") === "1";
if (node && subtree && !snap.nodes[node]) throw new Bad(`no node ${node}`);
const scope = node ? new Set(subtree ? scopeNodes(snap, node) : [node]) : undefined;
const tier = url.searchParams.get("tier");
const all = url.searchParams.get("all") === "1";
const apply = url.searchParams.get("claim") === "1" ? deps.pending.claim(snap, scope ?? scopeNodes(snap, null)) : undefined;
let list = deps.pending.list(snap, scope, all);
if (tier) list = list.filter((s) => s.tier === tier);
const order = new Map((scope ? [...scope] : scopeNodes(snap, null)).map((id, i) => [id, i]));
list.sort((a, b) => (order.get(a.node) ?? 1e9) - (order.get(b.node) ?? 1e9) || a.range[0] - b.range[0]);
const offset = Math.max(0, Number(url.searchParams.get("offset") ?? 0) || 0);
const limit = Math.min(5000, Math.max(1, Number(url.searchParams.get("limit") ?? 1000) || 1000));
return send(res, 200, { total: list.length, suggestions: list.slice(offset, offset + limit).map((s) => describe(snap, s)), ...(apply ? { apply } : {}) }), true;
}
if ((route === "accept" || route === "reject") && method === "POST") {
const input = await readJson<{ ids?: unknown; author?: unknown }>(req);
if (!Array.isArray(input.ids) || !input.ids.every((x) => typeof x === "string")) throw new Bad("give { ids: [...] }");
const found = (input.ids as string[]).map((id) => deps.pending.get(snap, id)).filter((s): s is Suggestion => !!s);
const author = typeof input.author === "string" && input.author.trim() ? input.author.trim().slice(0, 100) : "linker";
const main = deps.main();
if (route === "accept") {
const specs: LinkSpec[] = found.map((s) => ({ node: s.node, range: s.range, target: s.target }));
const r = applyLinks(main, specs, { author, actor: "llm", tag: "autolink", message: `Link ${specs.length} phrase${specs.length === 1 ? "" : "s"} (reviewed suggestions)` });
deps.pending.decide(found.map((s) => s.id), true, author);
if (r.created.length || r.merged.length) deps.wrote();
return send(res, 200, { accepted: found.length, unknown: input.ids.length - found.length, ...r }), true;
}
const byNode = new Map<NodeId, Set<string>>();
for (const s of found) byNode.set(s.node, (byNode.get(s.node) ?? new Set()).add(s.key));
const ops: Op[] = [...byNode].map(([id, keys]) => ({ op: "update", id, nolink: [...keys] }));
if (ops.length) {
main.as(author, () => main.apply(ops, `Decline ${found.length} link suggestion${found.length === 1 ? "" : "s"}`, { actor: "human", tag: "nolink" }));
deps.wrote();
}
deps.pending.decide(found.map((s) => s.id), false, author);
return send(res, 200, { rejected: found.length, unknown: input.ids.length - found.length }), true;
}
if (route === "decide" && method === "POST") {
const input = await readJson<{ ids?: unknown; link?: unknown; by?: unknown }>(req);
if (!Array.isArray(input.ids) || !input.ids.every((x) => typeof x === "string") || typeof input.link !== "boolean") throw new Bad("give { ids: [...], link: true | false, by }");
const by = typeof input.by === "string" && input.by.trim() ? input.by.trim().slice(0, 100) : "someone";
return send(res, 200, { decided: deps.pending.decide(input.ids as string[], input.link, by) }), true;
}
if (route === "related" && (method === "GET" || method === "POST")) {
// GET ?node=<id> for a node the server has; POST { node, text, title, top, topTitle, ancestors } for one it may not have yet.
const input = method === "POST" ? await readJson<ScanInput>(req) : { node: url.searchParams.get("node") as NodeId };
if (!input.node || (!snap.nodes[input.node] && typeof input.text !== "string")) throw new Bad("give an existing node, or its text");
const r = await deps.linker().related(input.node, 5, input);
return send(res, 200, r), true;
}
if (route === "nearby" && (method === "GET" || method === "POST")) {
const input = method === "POST" ? await readJson<ScanInput>(req) : { node: url.searchParams.get("node") as NodeId };
if (!input.node || (!snap.nodes[input.node] && typeof input.text !== "string")) throw new Bad("give an existing node, or its text");
const k = Math.min(10, Math.max(1, Number(url.searchParams.get("k") ?? 5) || 5));
return send(res, 200, { nearby: await deps.linker().nearby(input.node, k, input) }), true;
}
if (route === "jobs") {
if (method === "GET") return send(res, 200, { job: deps.jobs.current }), true;
if (method === "DELETE") return send(res, 200, { cancelled: deps.jobs.cancel(), job: deps.jobs.current }), true;
if (method === "POST") {
const input = await readJson<{ scope?: string; node?: string; review?: boolean }>(req);
if (input.scope !== "node" && input.scope !== "library") throw new Bad('scope is "node" or "library"');
const id = input.scope === "node" ? ((input.node ?? "") as NodeId) : null;
if (id !== null && !snap.nodes[id]) throw new Bad(`no node ${input.node ?? ""}`);
// The library root is the whole library, as a library job.
const at = id && !isLibraryRoot(snap, id) ? id : null;
const nodes = scopeNodes(snap, at);
const title = at ? label(snap, at) : "the library";
return send(res, 202, { job: deps.jobs.start(at ? "node" : "library", nodes, title, at ?? undefined, input.review === true) }), true;
}
}
if (route === "status" && method === "GET") {
return send(res, 200, {
embeddings: deps.embedder ? { state: deps.embedder.state, model: deps.embedder.spec.id, error: deps.embedder.error } : { state: "off" },
index: deps.index.status,
pending: deps.pending.size,
reviewer: deps.reviewer?.available ?? false,
job: deps.jobs.current,
}), true;
}
send(res, 404, { error: `no route ${method} /api/links/${route}` });
return true;
} catch (e) {
if (e instanceof HttpError) send(res, e.status, { error: e.message });
else {
console.error("links:", e);
send(res, 500, { error: (e as Error).message ?? String(e) });
}
return true;
}
};
}