InternalsRendering the Memory Graph

Rendering the Memory Graph

Mnemosyne models a workspace’s memory as a graph: entities, the episodes and decisions attached to them, and the typed relations between them. The server returns that graph as JSON at GET /graph. Drawing it is up to your app: there is no <MemoryGraph/> component to install. This page describes the data and two ways to render it.

A runnable version lives in examples/graph-viewer. The dashboard that ships with the server also draws the graph.


Getting the graph

curl "http://localhost:3939/graph" \
  -H "Authorization: Bearer mns_live_..."

With the TypeScript client:

import { MnemosyneClient, type GraphResponse } from "@mnemo-ai/client-ts";
 
const memory = new MnemosyneClient({
  url: "http://localhost:3939",
  apiKey: process.env.MNEMO_KEY!,
});
 
const graph: GraphResponse = await memory.graph();

The workspace comes from the API key, never from the request. Call the server from your backend, so the key does not reach the browser, and pass the JSON to the client.

Query parameters

ParameterWhat it does
focusAn entity id. Returns only that entity and its 1-hop neighbours.
includeSemantictrue adds the precomputed semantic-neighbour edges (relation semantically_related).
includeMentionstrue adds an episode → entity edge (relation mentions, provenance episode_facts) for each entity the episode’s facts are about. These edges are derived, not stored.
asOfAn ISO 8601 timestamp. Returns the graph as it stood at that instant.

memory.graph() accepts focus, includeSemantic and asOf. To send includeMentions, call the route directly, as the example does.

The response is sent with Cache-Control: private, max-age=30.

  • GET /graph/traverse: a breadth-first walk from a root node, up to 5 hops, returning { id, hop, relevance } for each node reached.
  • GET /graph/path: the shortest path between two nodes.
  • GET /graph/stats: counts for the workspace graph.

The data contract

The payload is two flat arrays and a meta block of counts. The types are exported by @mnemo-ai/client-ts.

export interface GraphNode {
  id: string;
  kind: "entity" | "episode" | "decision";
  // present only when kind === "entity"
  entityKind?: "person" | "organization" | "project" | "concept" | "place" | "other";
  label: string;
  description?: string | null;
  mentionCount: number;
  factCount: number;
  avgMemoryStrength: number;
  createdAt: string;                  // ISO 8601
}
 
export interface GraphEdge {
  id: string;
  source: string;                     // GraphNode.id
  target: string;                     // GraphNode.id
  relation: string;                   // a relation verb (see below)
  confidence: number;                 // 0..1
  provenance: string | null;
}
 
export interface GraphResponse {
  nodes: GraphNode[];
  edges: GraphEdge[];
  meta: {
    entityCount: number;
    episodeCount: number;
    decisionCount: number;
    relationCount: number;
  };
}

What is in the graph

kindWhat is included
entityCanonical entities. An entity merged into another one is left out.
episodeEpisodes that are not synthetic.
decisionActive decisions.

Edges are the open relations (validTo is null) that have not been dismissed.

A node above the caller’s sensitivity ceiling is omitted, together with the edges that touch it. factCount counts only the facts the caller may read, and an entity’s mentionCount is its factCount plus the related episodes the caller may read.

Relation verbs

Edges are directional. The stored verbs are related, compatible, not_conflict, conflicts_with, derived_from, scoped, supersedes, part_of and member_of. The two optional overlays add mentions and semantically_related. Treat relation as an open string and give unknown verbs a default style.


Recipe A: react-force-graph-2d

The node and edge shapes fit the force-graph libraries after one rename: edges becomes links.

"use client";
 
import ForceGraph2D from "react-force-graph-2d";
import type { GraphNode, GraphResponse } from "@mnemo-ai/client-ts";
 
const KIND_COLOR: Record<string, string> = {
  person: "#7c3aed",
  organization: "#2563eb",
  project: "#16a34a",
  concept: "#d97706",
  place: "#0891b2",
  other: "#52525b",
  episode: "#0e7490",
  decision: "#9333ea",
};
 
export function MemoryGraph({ graph }: { graph: GraphResponse }) {
  const maxMentions = Math.max(1, ...graph.nodes.map((n) => n.mentionCount));
 
  return (
    <ForceGraph2D
      graphData={{ nodes: graph.nodes, links: graph.edges }}
      nodeId="id"
      nodeLabel={(n) => (n as GraphNode).label}
      nodeColor={(n) => {
        const node = n as GraphNode;
        return KIND_COLOR[node.entityKind ?? node.kind] ?? KIND_COLOR.other;
      }}
      nodeVal={(n) => 8 + ((n as GraphNode).mentionCount / maxMentions) * 20}
      linkLabel={(l) => (l as { relation: string }).relation}
      linkWidth={(l) => 0.5 + (l as { confidence: number }).confidence * 0.5}
      linkDirectionalArrowLength={3.5}
      linkDirectionalArrowRelPos={1}
    />
  );
}

In Next.js, load the library with next/dynamic and ssr: false, because it reads window when it is imported.

Recipe B: 2D and 3D toggle

react-force-graph-3d takes the same graphData and the same accessors, so switching is one import:

"use client";
 
import { useState } from "react";
import ForceGraph2D from "react-force-graph-2d";
import ForceGraph3D from "react-force-graph-3d";
import type { GraphNode, GraphResponse } from "@mnemo-ai/client-ts";
 
export function ToggleGraph({ graph }: { graph: GraphResponse }) {
  const [is3D, setIs3D] = useState(false);
 
  const common = {
    graphData: { nodes: graph.nodes, links: graph.edges },
    nodeId: "id",
    nodeLabel: (n: object) => (n as GraphNode).label,
    nodeVal: (n: object) => 1 + (n as GraphNode).factCount,
  };
 
  return (
    <>
      <button onClick={() => setIs3D((v) => !v)}>{is3D ? "2D" : "3D"}</button>
      {is3D ? <ForceGraph3D {...common} /> : <ForceGraph2D {...common} />}
    </>
  );
}

The drawing helpers in the repository

The repository has a set of canvas helpers in packages/core/src/graph:

ExportWhat it does
drawNodeDraws a node on a 2D canvas, with a shape per kind: circle for people, hexagon for organizations, rounded rectangle for projects and episodes, diamond for concepts and decisions, pentagon for places.
drawEdgeDraws a relation with a colour and dash per verb, a width scaled by confidence, and an arrowhead at the target.
nodeRadiusMaps mentionCount to a radius between 8 and 28.
ENTITY_KIND_COLOR, EDGE_STYLESThe colour and line tables.
defaultForceConfigCharge, link distance, centre strength and alpha decay for d3-force.

They belong to @mnemo-ai/core, which is an internal package and is not on npm. examples/graph-viewer imports them from the workspace (@mnemo-ai/core/graph), so it runs only inside the monorepo:

git clone https://github.com/lucasmailland/mnemosyne
cd mnemosyne
pnpm install
pnpm --filter "graph-viewer^..." build
pnpm --filter graph-viewer dev

With no MNEMO_URL set, the example renders a bundled sample graph. To draw your own workspace, copy examples/graph-viewer/.env.example to .env.local and set MNEMO_URL and MNEMO_KEY.

In your own app, use the recipes above, or copy the helper files: they have no dependencies and the repository is Apache-2.0.


What the server owns and what you own

  • The server owns the data: which nodes and edges exist, the sensitivity filter and the counts.
  • You own the component, the framework, the theme, panning and zoom, and click and hover behaviour.