Implementation:0xK3vin MegaMemory Web Graph Explorer Server
| Knowledge Sources | |
|---|---|
| Domains | Visualization, HTTP_Server, SSE, Timeline |
| Last Updated | 2026-10-27 12:00 GMT |
Overview
Concrete HTTP server for visualizing, searching, and time-traveling through the project knowledge graph provided by the MegaMemory library.
Description
The Web Graph Explorer Server (src/web.ts) implements megamemory serve, which starts a local HTTP server with a bundled single-file frontend (web/index.html, d3-force + Canvas). It exposes REST endpoints for graph visualization, semantic search, timeline viewing, and time-travel reconstruction, plus a Server-Sent Events (SSE) stream for live updates.
Key components:
- runServe — opens the DB, preloads the embedding model (best-effort), starts the HTTP server, and handles EADDRINUSE by prompting for an alternative port
- buildGraphPayload — assembles all active nodes (with computed edge_count) and edges
- buildMergedTimelineEntries — merges real timeline table rows with synthetic entries reconstructed from node created_at/updated_at/removed_at timestamps, deduping by (tool, affected_id)
- SSE /api/events — sends an initial init event, then polls the DB every 1.5s and broadcasts node_added/node_updated/node_removed/edge_added/edge_removed/stats events; heartbeat every 30s
Usage
Import this module when you need to:
- Launch the web explorer: call runServe to start the HTTP server (default port 4321)
- Serve graph data via HTTP: the endpoints are internal to runServe but buildMergedTimelineEntries is reusable for timeline logic
- Reuse timeline merging logic:. Call buildMergedTimelineEntries to get merged real + synthetic timeline entries with dedup
Code Reference
Source Location
- Repository: 0xK3vin_MegaMemory
- File: src/web.ts
- Key functions:
- runServe: L109-791
- buildGraphPayload: L130-172
- buildNodeTimestampEntries: L217-289
- buildMergedTimelineEntries: L291-364
- initializeSseSnapshot: L385-392
- broadcast: L394-404
- detectChanges: L406-519
- listenWithRetry: L71-107
Signature
export async function runServe(port: number): Promise<void>;
// Starts HTTP server on `port`; default 4321. Handles EADDRINUSE with retry.
// Preloads embedding model (best-effort); /api/search retries on demand if preload failed.
// Internal closures within runServe:
function buildGraphPayload(): {
nodes: Array<{ id, name, kind, summary, parent_id, edge_count }>;
edges: Array<{ from, to, relation, description }>;
};
function buildMergedTimelineEntries(options?: TimelineQueryOptions): {
entries: TimelineEntryView[];
source: "merged" | "timeline" | "node_timestamps";
};
// TimelineQueryOptions = { writesOnly?, tool?, since?, until?, limit? }
// REST endpoints (all GET):
// / → HTML frontend
// /api/graph → { nodes, edges }
// /api/search?q=&top_k= → UnderstandOutput
// /api/node/:id → NodeWithContext + timestamps
// /api/stats → { nodes, edges, removed, kinds }
// /api/events → SSE stream (init + incremental changes)
// /api/timeline?writes_only&tool=&since=&until=&limit= → { entries, source }
// /api/timeline/bounds → { first, last, count, source }
// /api/timeline/ticks?n= → { ticks, source }
// /api/graph/at?t= → { nodes, edges } as they existed at time t
Import
import { runServe } from "./web.js";
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| port | number | Yes | HTTP port; default 4321 from src/index.ts |
| MEGAMEMORY_DB_PATH | env | No | Override DB path; default cwd/.megamemory/knowledge.db |
Outputs
| Name | Type | Description |
|---|---|---|
| runServe() | Promise<void> | Listens until SIGINT/SIGTERM; closes DB on shutdown |
| /api/graph | JSON | All active nodes + edges |
| /api/search | JSON | UnderstandOutput with similarity scores; 503 if embeddings unavailable |
| /api/timeline | JSON | Merged real + synthetic timeline entries |
| /api/graph/at | JSON | Graph state at timestamp t (nodes created by t, not removed by t) |
| /api/events | SSE | init event then incremental change events every 1.5s |
Usage Examples
Launch the Explorer
# Default port
megamemory serve
# Custom port
megamemory serve --port 8080
Programmatic Launch
import { runServe } from "./web.js";
await runServe(4321);
// Server listens; prints banner with URL and DB path
// SIGINT/SIGTERM triggers graceful shutdown (clears intervals, closes DB)
HTTP API Usage
# Get current graph
curl http://localhost:4321/api/graph | jq '.nodes | length'
# Semantic search
curl 'http://localhost:4321/api/search?q=merge+algorithm&top_k=5' | jq '.matches[0].similarity'
# Time-travel to a specific timestamp
curl 'http://localhost:4321/api/graph/at?t=2026-10-01+12:00:00' | jq '.nodes | length'
# Subscribe to live updates (SSE)
curl -N http://localhost:4321/api/events
# data: {"type":"init","data":{"nodes":[...],"edges":[...],"stats":{...}}}
# data: {"type":"node_added","data":{"id":"new-concept",...}}
Related Pages
Implements Principle
- Principle:0xK3vin_MegaMemory_Live_Graph_Visualization
- Principle:0xK3vin_MegaMemory_Timeline_Reconstruction
- Principle:0xK3vin_MegaMemory_Semantic_Search