graph context: a separate marker message before the first user turn

The map picks no longer glue into the opening prompt as seed text
(owner, 2026-10-04 late). POST /api/chats accepts graphContext
{propositions}; the route resolves the picks against the live graph,
derives the reasoning chains among them (chainsForSelection →
formatChainLine), and writes a graph-context custom marker as
message[0] — before the learner's first user message, in either mode.

- Core: newGraphContextMessage (model-facing content = the
  contextualization framing + proposition lines + chain lines;
  details = the structured pick); toLlmMessages flattens it to a user
  message like every marker, so the model reads it for free.
- View: projects to a user-side data-graph-context part.
- UI: GraphContextCard — minimal, muted, dashed, collapsed by default
  (counts only); expand reveals name — statement rows and the chain
  lines. Same compact-expandable pattern as the practice cards.
- Home: sends graphContext with the creation whenever picks exist;
  the prompt param carries the learner's own words only.
This commit is contained in:
amosblomqvist
2026-10-04 23:07:13 +03:00
parent 87f8e1e6f4
commit 90893ae236
8 changed files with 327 additions and 13 deletions
+30 -1
View File
@@ -1,3 +1,5 @@
import { chainsForSelection, formatChainLine } from "@/src/core";
import { newGraphContextMessage, type GraphContextPick } from "@/src/core";
import { createChat, isTurnRunning, listChats, readSettingsRecord, stores, titleFromPrompt } from "@/src/server";
import type { ChatListItem } from "@/src/core";
@@ -17,7 +19,7 @@ export async function GET() {
}
export async function POST(req: Request) {
const body = (await req.json().catch(() => ({}))) as { title?: unknown; kind?: unknown };
const body = (await req.json().catch(() => ({}))) as { title?: unknown; kind?: unknown; graphContext?: unknown };
const title = typeof body.title === "string" ? body.title : "";
// Practice sessions (owner, 2026-10-04): kind "practice" marks the
// record — the practice agent + its tools from turn 1, no extraction.
@@ -37,5 +39,32 @@ export async function POST(req: Request) {
thinkingLevel: strong.thinkingLevel,
});
}
// The session's map context (owner, 2026-10-04): the picked
// propositions ride the CREATION — message[0] is a graph-context
// marker (a separate custom message BEFORE the learner's first user
// message), carrying the picks plus the reasoning chains among them.
// The picks carry their own name/statement (what the learner saw at
// pick time); the chains derive from the CURRENT graph snapshot.
const rawPicks = (body.graphContext as { propositions?: unknown } | undefined)?.propositions;
const picks: GraphContextPick[] = Array.isArray(rawPicks)
? rawPicks
.filter(
(p): p is GraphContextPick =>
typeof p === "object" &&
p !== null &&
typeof (p as GraphContextPick).id === "string" &&
typeof (p as GraphContextPick).name === "string" &&
typeof (p as GraphContextPick).statement === "string",
)
.slice(0, 64)
: [];
if (picks.length > 0) {
const byId = new Map(stores.graphStore.readNodes().map((n) => [n.id, n]));
const pickedNodes = picks.map((p) => byId.get(p.id)).filter((n) => n !== undefined);
const chainLines = chainsForSelection(pickedNodes, stores.graphStore.readChains()).map((c) =>
formatChainLine(c, Object.fromEntries(byId)),
);
stores.chatStore.saveMessages(chat.id, [newGraphContextMessage(picks, chainLines)]);
}
return Response.json({ id: chat.id, title: chat.title });
}