import { getCollection, type CollectionEntry } from "astro:content"; import { KnowledgeRepository, type KnowledgeEntityInput, type KnowledgeRelationshipInput, type KnowledgeEntityRecord } from "./KnowledgeRepository.ts"; export interface SceneNodeViewModel { id: string; label: string; subtitle?: string; href?: string; q: number; r: number; variant?: string; size?: string; active?: boolean; selected?: boolean; disabled?: boolean; } export interface SceneEdgeViewModel { from: string; to: string; active?: boolean; muted?: boolean; fromSocket?: string; toSocket?: string; } export interface LabSectionViewModel { id: string; eyebrow: string; title: string; summary: string; details: string[]; meta: string[]; location: string; } type PagesEntry = CollectionEntry<"pages">; let repositoryPromise: Promise | null = null; const LAB_NODE_IDS = ["gh", "about", "journal", "experience", "cv", "incidents", "organizations"]; const LAB_SECTION_IDS = ["gh", "about", "overview", "journal", "experience", "experience-history", "cv", "incidents", "organizations"]; interface HomeRingDefinition { id: string; label: string; subtitle: string; summary: string; details: string[]; meta: string[]; q: number; r: number; size: "root" | "md" | "sm"; variant: "root" | "anchor" | "reference"; } const HOME_RING_DEFINITIONS: HomeRingDefinition[] = [ { id: "operational-topology", label: "Operational Topology", subtitle: "Framework", summary: "Root context for the topology. Every other component branches from this shared world.", details: [ "The root keeps the system oriented.", "All scenes, signals, and layouts hang from the same world model.", "This is the entry point for the framework itself.", ], meta: ["Root", "Central context", "Shared world model"], q: 0, r: 0, size: "root", variant: "root", }, { id: "signals", label: "Signals", subtitle: "Propagation policy", summary: "Signal layers describe how activity moves through the topology.", details: [ "Ambient, origin, path, broadcast, and receiving stay distinct.", "Signals are transient and driven by interaction or navigation.", "The policy decides meaning; the engine renders motion.", ], meta: ["Interactive", "Signal policy"], q: 0, r: -2, size: "md", variant: "anchor", }, { id: "connectors", label: "Connectors", subtitle: "Routing lines", summary: "Connectors reveal relationships between occupied cells.", details: [ "They stay readable before routing becomes advanced.", "Center-to-center lines make the world easy to follow.", "Connector behavior is intentionally stable right now.", ], meta: ["Node", "Relationship lines"], q: 0, r: -4, size: "sm", variant: "reference", }, { id: "camera", label: "Camera", subtitle: "Observation", summary: "The camera decides what stays visible, safe, and focused.", details: [ "Viewport fit keeps the world visible on entry.", "Focus and fit are different policies, not the same behavior.", "Manual controls and minimap make navigation tangible.", ], meta: ["Anchor", "Viewport behavior"], q: 2, r: -2, size: "md", variant: "anchor", }, { id: "minimap", label: "Minimap", subtitle: "Spatial navigation", summary: "The minimap compresses the same world into a compact control surface.", details: [ "It shows viewport position and selected context.", "Occupants remain hex-shaped so the language stays consistent.", "Controls support zoom, center, and fit without leaving the scene.", ], meta: ["Node", "Navigation layer"], q: 4, r: -4, size: "sm", variant: "reference", }, { id: "topology", label: "Topology", subtitle: "World model", summary: "Topology is the canonical spatial model behind the scene.", details: [ "Axial coordinates remain the source of truth.", "Entities occupy cells and relationships connect them.", "The scene is a view over that world, not a replacement for it.", ], meta: ["Anchor", "Canonical spatial model"], q: 2, r: 0, size: "md", variant: "anchor", }, { id: "scene", label: "Scene", subtitle: "Presentation layer", summary: "The scene is the rendered surface where topology becomes visible.", details: [ "It composes grid, occupants, connectors, and signals.", "Different pages can reuse the same scene contract.", "The scene is where interaction becomes visible context.", ], meta: ["Node", "Rendered surface"], q: 4, r: 0, size: "sm", variant: "reference", }, { id: "grid", label: "Grid", subtitle: "Spatial structure", summary: "The grid provides the quiet underlying structure for the world.", details: [ "It stays understated so occupants and signals remain legible.", "Viewport-fit and world-grid are two ways to frame the same world.", "The grid keeps the scene grounded in axial space.", ], meta: ["Anchor", "Spatial structure"], q: 2, r: 2, size: "md", variant: "anchor", }, { id: "nodes", label: "Nodes", subtitle: "Occupants", summary: "Nodes are the entities that sit on top of the grid and hold meaning.", details: [ "They can be selected, focused, and routed through.", "Node state drives the inspector and nearby signals.", "Nodes are the visible atoms of the topology.", ], meta: ["Node", "Selectable occupants"], q: 4, r: 4, size: "sm", variant: "reference", }, { id: "presets", label: "Presets", subtitle: "Reusable templates", summary: "Presets define reusable site and scene compositions.", details: [ "They keep demos, transitions, and pages aligned.", "A preset can become a page, a slide, or a reusable demo.", "The same topology can be used in multiple contexts.", ], meta: ["Anchor", "Templates"], q: 0, r: 2, size: "md", variant: "anchor", }, { id: "presentation-mode", label: "Presentation Mode", subtitle: "Transition shell", summary: "Presentation mode turns the topology into a guided sequence.", details: [ "It supports stepwise scene handoff.", "Ring, linear, and future layouts can share the same framework.", "It is a bridge between a topology and a narrative run.", ], meta: ["Node", "Guided flow"], q: 0, r: 4, size: "sm", variant: "reference", }, { id: "labs", label: "Labs", subtitle: "Exploration space", summary: "Labs are where experiments, demos, and visual probes live.", details: [ "They let us test motion, signals, and layout ideas safely.", "Labs are intentionally more experimental than production pages.", "The framework can expose multiple labs with different behaviors.", ], meta: ["Anchor", "Experimentation"], q: -2, r: 2, size: "md", variant: "anchor", }, { id: "animations", label: "Animations", subtitle: "Motion language", summary: "Animations explain how the world appears, shifts, and settles.", details: [ "Entrance, emphasis, transition, and ending can be reused.", "The animation lab helps test timing and intensity separately.", "Motion should remain calm and purposeful.", ], meta: ["Node", "Motion system"], q: -4, r: 4, size: "sm", variant: "reference", }, { id: "themes", label: "Themes", subtitle: "Visual system", summary: "Themes separate semantic meaning from the final visual expression.", details: [ "Color tokens can shift without changing component logic.", "The framework keeps black/gold identity as the default language.", "Theme switches should stay runtime and accessible.", ], meta: ["Anchor", "Design tokens"], q: -2, r: 0, size: "md", variant: "anchor", }, { id: "renderer", label: "Renderer", subtitle: "Presentation engine", summary: "Renderers turn the model into SVG, HTML, and interactive layout.", details: [ "Renderers own presentation, not business meaning.", "The same rendering path supports home, labs, and dossiers.", "The renderer should stay deterministic and reusable.", ], meta: ["Node", "Presentation engine"], q: -4, r: 0, size: "sm", variant: "reference", }, { id: "developer-api", label: "Developer API", subtitle: "Inspection surface", summary: "The developer API exposes safe hooks for debugging and exploration.", details: [ "It helps inspect the world without reaching into internals.", "Developer tools should be explicit, not hidden inside renderers.", "This keeps the framework approachable for future users.", ], meta: ["Anchor", "Debugging hooks"], q: -2, r: -2, size: "md", variant: "anchor", }, { id: "the-framework", label: "The Framework", subtitle: "As such", summary: "This node summarizes the framework as a reusable system.", details: [ "It is the broader contract behind all the scenes.", "The same primitives can power sites, slides, and demos.", "That is the thing the home page is trying to communicate.", ], meta: ["Node", "Framework summary"], q: -4, r: -4, size: "sm", variant: "reference", }, ]; const HOME_RING_EDGE_META: Record = { "operational-topology->signals": { active: true, fromSocket: "n", toSocket: "s" }, "operational-topology->camera": { active: true, fromSocket: "ne", toSocket: "sw" }, "operational-topology->topology": { active: true, fromSocket: "e", toSocket: "w" }, "operational-topology->grid": { active: true, fromSocket: "se", toSocket: "nw" }, "operational-topology->presets": { active: true, fromSocket: "s", toSocket: "n" }, "operational-topology->labs": { active: true, fromSocket: "sw", toSocket: "ne" }, "operational-topology->themes": { active: true, fromSocket: "w", toSocket: "e" }, "operational-topology->developer-api": { active: true, fromSocket: "nw", toSocket: "se" }, "signals->connectors": { muted: true, fromSocket: "n", toSocket: "s" }, "camera->minimap": { muted: true, fromSocket: "ne", toSocket: "sw" }, "topology->scene": { muted: true, fromSocket: "e", toSocket: "w" }, "grid->nodes": { muted: true, fromSocket: "se", toSocket: "nw" }, "presets->presentation-mode": { muted: true, fromSocket: "s", toSocket: "n" }, "labs->animations": { muted: true, fromSocket: "sw", toSocket: "ne" }, "themes->renderer": { muted: true, fromSocket: "w", toSocket: "e" }, "developer-api->the-framework": { muted: true, fromSocket: "nw", toSocket: "se" }, }; const LAB_EDGE_META: Record = { "gh->about": { active: true, fromSocket: "sw", toSocket: "ne" }, "gh->journal": { active: true, fromSocket: "se", toSocket: "nw" }, "about->experience": { muted: true, fromSocket: "sw", toSocket: "ne" }, "about->cv": { muted: true, fromSocket: "se", toSocket: "nw" }, "experience->experience-history": { active: true, fromSocket: "s", toSocket: "n" }, "experience-history->cv": { muted: true, fromSocket: "se", toSocket: "nw" }, "journal->incidents": { muted: true, fromSocket: "se", toSocket: "nw" }, "journal->organizations": { muted: true, fromSocket: "sw", toSocket: "ne" }, }; function parseConnectionRef(input: string) { return input.includes(":") ? input.split(":").slice(1).join(":") : input; } function toKnowledgeEntity(entry: PagesEntry): KnowledgeEntityInput { const data = entry.data; return { id: data.id, type: data.type, title: data.title, subtitle: data.subtitle, summary: data.summary, description: data.description, body: entry.body, tags: data.tags, q: data.q, r: data.r, language: data.language, metadata: { href: data.href, variant: data.variant, size: data.size, active: data.active, selected: data.selected, disabled: data.disabled, eyebrow: data.eyebrow, summary: data.summary, details: data.details, meta: data.meta, }, }; } function toKnowledgeRelationships(entry: PagesEntry): KnowledgeRelationshipInput[] { const sourceId = entry.data.id; return (entry.data.connections ?? []).map((connection) => ({ from: sourceId, to: parseConnectionRef(connection), active: entry.data.active, muted: !entry.data.active, })); } async function loadRepository() { const pages = await getCollection("pages"); return new KnowledgeRepository({ entities: pages.map(toKnowledgeEntity), relationships: pages.flatMap(toKnowledgeRelationships), }); } async function getRepository() { repositoryPromise ??= loadRepository(); return repositoryPromise; } function toSceneNode(entity: KnowledgeEntityRecord): SceneNodeViewModel { const metadata = entity.metadata as Record; return { id: entity.id, label: entity.label, subtitle: entity.subtitle, href: typeof metadata.href === "string" ? metadata.href : undefined, q: entity.q ?? 0, r: entity.r ?? 0, variant: typeof metadata.variant === "string" ? metadata.variant : undefined, size: typeof metadata.size === "string" ? metadata.size : undefined, active: Boolean(metadata.active), selected: Boolean(metadata.selected), disabled: Boolean(metadata.disabled), }; } function toLabSection(entity: KnowledgeEntityRecord): LabSectionViewModel { const metadata = entity.metadata as Record; return { id: entity.id, eyebrow: typeof metadata.eyebrow === "string" ? metadata.eyebrow : "Node", title: entity.title, summary: typeof metadata.summary === "string" ? metadata.summary : "", details: Array.isArray(metadata.details) ? metadata.details.filter((detail): detail is string => typeof detail === "string") : [], meta: Array.isArray(metadata.meta) ? metadata.meta.filter((item): item is string => typeof item === "string") : [], location: typeof entity.q === "number" && typeof entity.r === "number" ? String(entity.q) + ", " + String(entity.r) : "", }; } function buildEdges(repository: KnowledgeRepository, ids: string[], metaMap: Record) { const allowed = new Set(ids); return repository.getAllRelationships() .filter((relationship) => allowed.has(relationship.from) && allowed.has(relationship.to)) .map((relationship) => { const key = relationship.from + "->" + relationship.to; const meta = metaMap[key] ?? { fromSocket: "s", toSocket: "n" }; return { from: relationship.from, to: relationship.to, active: meta.active ?? relationship.active, muted: meta.muted ?? relationship.muted, fromSocket: meta.fromSocket, toSocket: meta.toSocket, } satisfies SceneEdgeViewModel; }); } function pickNodes(repository: KnowledgeRepository, ids: string[]) { return ids .map((id) => repository.getEntity(id)) .filter((entity): entity is KnowledgeEntityRecord => Boolean(entity)) .map(toSceneNode); } function pickSections(repository: KnowledgeRepository, ids: string[]) { return ids .map((id) => repository.getEntity(id)) .filter((entity): entity is KnowledgeEntityRecord => Boolean(entity)) .map(toLabSection); } function createHomeRingNodes() { return HOME_RING_DEFINITIONS.map((item) => ({ id: item.id, label: item.label, subtitle: item.subtitle, q: item.q, r: item.r, variant: item.variant, size: item.size, active: item.id === "operational-topology", selected: item.id === "operational-topology", disabled: false, })); } function createHomeRingItems() { return HOME_RING_DEFINITIONS.map((item) => ({ id: item.id, label: item.label, subtitle: item.subtitle, summary: item.summary, details: item.details, meta: item.meta, q: item.q, r: item.r, })); } export async function createHomeSceneData() { const repository = await getRepository(); const items = createHomeRingItems(); const nodes = createHomeRingNodes(); const edges = Object.entries(HOME_RING_EDGE_META).map(([key, meta]) => { const [from, to] = key.split("->"); return { from, to, active: meta.active, muted: meta.muted, fromSocket: meta.fromSocket, toSocket: meta.toSocket, } satisfies SceneEdgeViewModel; }); const activeItem = items.find((item) => item.id === "operational-topology") ?? items[0]; const presets = [ ...items.map((item) => ({ presetId: item.id, label: item.label, subtitle: item.subtitle, summary: item.summary, })), { presetId: "matrix-layout", label: "Matrix Layout", subtitle: "Comparative grid", summary: "A two-dimensional matrix for comparing values across columns and rows.", }, { presetId: "truth-table", label: "Truth Table", subtitle: "Boolean proof grid", summary: "A truth-table matrix proving (a && b) || !(c && d) across all 16 combinations.", }, ]; return { repository, items, presets, nodes, edges, activeItem, activePreset: activeItem, }; } export async function createLabSceneData() { const repository = await getRepository(); return { repository, nodes: pickNodes(repository, LAB_NODE_IDS), edges: buildEdges(repository, LAB_NODE_IDS, LAB_EDGE_META), sections: pickSections(repository, LAB_SECTION_IDS), }; }