hippo-memory 1.19.0 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +133 -3
- package/dist/cli.js.map +1 -1
- package/dist/graph-stream.d.ts +71 -0
- package/dist/graph-stream.d.ts.map +1 -0
- package/dist/graph-stream.js +170 -0
- package/dist/graph-stream.js.map +1 -0
- package/dist/graph-view.d.ts +72 -0
- package/dist/graph-view.d.ts.map +1 -0
- package/dist/graph-view.js +310 -0
- package/dist/graph-view.js.map +1 -0
- package/dist/graph.d.ts +30 -4
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +104 -11
- package/dist/graph.js.map +1 -1
- package/dist/search.d.ts +16 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +31 -1
- package/dist/search.js.map +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +19 -0
- package/dist/server.js.map +1 -1
- package/dist/src/cli.js +133 -3
- package/dist/src/cli.js.map +1 -1
- package/dist/src/graph-stream.js +170 -0
- package/dist/src/graph-stream.js.map +1 -0
- package/dist/src/graph-view.js +310 -0
- package/dist/src/graph-view.js.map +1 -0
- package/dist/src/graph.js +104 -11
- package/dist/src/graph.js.map +1 -1
- package/dist/src/search.js +31 -1
- package/dist/src/search.js.map +1 -1
- package/dist/src/server.js +19 -0
- package/dist/src/server.js.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/extensions/openclaw-plugin/openclaw.plugin.json +1 -1
- package/extensions/openclaw-plugin/package.json +1 -1
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L1 — graph-retrieval ranked-list stream for RRF fusion
|
|
3
|
+
* (docs/plans/2026-06-02-l1-graph-rrf-stream.md).
|
|
4
|
+
*
|
|
5
|
+
* READ-ONLY consumer of the E3 graph substrate (entities/relations built by E3.1,
|
|
6
|
+
* guarded by E3.3). Produces a ranked list of `entries[]` indices ordered by graph
|
|
7
|
+
* proximity to the strong lexical seeds, for use as a 3rd fusion input to `rrfFuse`
|
|
8
|
+
* beside BM25 + dense (src/search.ts hybridSearch, scoring:'rrf').
|
|
9
|
+
*
|
|
10
|
+
* DISTINCT from graph-recall.ts: that INJECTS out-of-pool neighbours post-hoc; this
|
|
11
|
+
* RE-RANKS within the already-filtered candidate pool. It only assigns ranks to
|
|
12
|
+
* entries that are (a) graph-reached from a seed AND (b) present in `entries[]`. Seeds
|
|
13
|
+
* themselves are never scored (they already rank via BM25/dense; scoring them would
|
|
14
|
+
* double-count and dilute the orthogonal graph signal).
|
|
15
|
+
*
|
|
16
|
+
* Reuses the E3.2 BFS traversal shape from graph-recall.ts (loadEntitiesByMemoryId
|
|
17
|
+
* seeds -> loadNeighborRelations BFS both directions, per-hop fanout cap, visited set
|
|
18
|
+
* -> loadEntitiesByIds to resolve reached -> memoryId). Expands across the local AND
|
|
19
|
+
* global stores. Pure reads (SELECTs only via graph.ts helpers), so the E3.3
|
|
20
|
+
* check-graph-writes lint permits this module living outside graph.ts.
|
|
21
|
+
*
|
|
22
|
+
* The graph stream's score scale (1/lexRank seed strength x decay^hops) only sets the
|
|
23
|
+
* WITHIN-graph-stream ORDERING; RRF then re-ranks the list by 1/(k + graphRank), so the
|
|
24
|
+
* absolute magnitude is washed out by fusion. Do not tune the scale expecting a
|
|
25
|
+
* fused-score effect — only the induced order matters.
|
|
26
|
+
*/
|
|
27
|
+
import type { MemoryEntry } from './memory.js';
|
|
28
|
+
/** Default hops expanded from each seed (MVP; hard cap MAX_HOPS=3 reused from graph-recall). */
|
|
29
|
+
export declare const DEFAULT_GRAPH_HOPS = 2;
|
|
30
|
+
/** Default per-hop multiplicative decay applied to the seed strength. */
|
|
31
|
+
export declare const DEFAULT_GRAPH_DECAY = 0.5;
|
|
32
|
+
/** Default number of top lexical seeds expanded from. */
|
|
33
|
+
export declare const DEFAULT_GRAPH_SEED_COUNT = 10;
|
|
34
|
+
/** Recommended RRF weight for the graph stream — a CLI-only convenience default. The
|
|
35
|
+
* library `graphStream.weight` option stays REQUIRED (opt-in is explicit). */
|
|
36
|
+
export declare const DEFAULT_GRAPH_STREAM_WEIGHT = 0.5;
|
|
37
|
+
/** A lexical seed to expand the graph from: a candidate index + its lexical strength. */
|
|
38
|
+
export interface GraphSeed {
|
|
39
|
+
/** Index into the caller's `entries[]`. */
|
|
40
|
+
index: number;
|
|
41
|
+
/** Lexical strength (1/lexRank); higher = stronger seed. Propagated x decay^hops. */
|
|
42
|
+
strength: number;
|
|
43
|
+
}
|
|
44
|
+
export interface GraphStreamOpts {
|
|
45
|
+
hippoRoot: string;
|
|
46
|
+
tenantId: string;
|
|
47
|
+
/** The global store root, when distinct + initialized (where global seeds' graph lives). */
|
|
48
|
+
globalRoot?: string;
|
|
49
|
+
/** Hops to expand from each seed. Clamped to [1, MAX_HOPS]. Default DEFAULT_GRAPH_HOPS. */
|
|
50
|
+
hops?: number;
|
|
51
|
+
/** Per-hop multiplicative decay on the seed strength. Default DEFAULT_GRAPH_DECAY. */
|
|
52
|
+
decay?: number;
|
|
53
|
+
/** Per-hop fanout cap. Default DEFAULT_MAX_NEIGHBORS. */
|
|
54
|
+
maxNeighbors?: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Pick the top `seedCount` candidates by best lexical rank (lowest position across the
|
|
58
|
+
* BM25 and dense ranked lists), with strength = 1/(bestRank + 1). Pure; exported for
|
|
59
|
+
* direct unit testing. A candidate present in either ranked list is eligible.
|
|
60
|
+
*/
|
|
61
|
+
export declare function selectGraphSeeds(bm25Ranked: ReadonlyArray<number>, cosineRanked: ReadonlyArray<number>, seedCount: number): GraphSeed[];
|
|
62
|
+
/**
|
|
63
|
+
* Produce the graph-retrieval ranked list: `entries[]` indices ordered by graph
|
|
64
|
+
* proximity (desc) to the lexical `seeds`. Only graph-reached, in-pool, non-seed
|
|
65
|
+
* indices appear; the rest are absent (-> rrfFuse absentRank). Pure reads.
|
|
66
|
+
*
|
|
67
|
+
* Returns `[]` when there are no seeds/entries, the graph is empty, no seed maps to an
|
|
68
|
+
* entity, or nothing reached is in-pool — the caller then skips the 3rd fusion list.
|
|
69
|
+
*/
|
|
70
|
+
export declare function graphRankStream(entries: ReadonlyArray<MemoryEntry>, seeds: ReadonlyArray<GraphSeed>, opts: GraphStreamOpts): number[];
|
|
71
|
+
//# sourceMappingURL=graph-stream.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-stream.d.ts","sourceRoot":"","sources":["../src/graph-stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAQ/C,gGAAgG;AAChG,eAAO,MAAM,kBAAkB,IAAI,CAAC;AACpC,yEAAyE;AACzE,eAAO,MAAM,mBAAmB,MAAM,CAAC;AACvC,yDAAyD;AACzD,eAAO,MAAM,wBAAwB,KAAK,CAAC;AAC3C;+EAC+E;AAC/E,eAAO,MAAM,2BAA2B,MAAM,CAAC;AAE/C,yFAAyF;AACzF,MAAM,WAAW,SAAS;IACxB,2CAA2C;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,qFAAqF;IACrF,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,4FAA4F;IAC5F,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,2FAA2F;IAC3F,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,sFAAsF;IACtF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yDAAyD;IACzD,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,UAAU,EAAE,aAAa,CAAC,MAAM,CAAC,EACjC,YAAY,EAAE,aAAa,CAAC,MAAM,CAAC,EACnC,SAAS,EAAE,MAAM,GAChB,SAAS,EAAE,CAgBb;AAmGD;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,aAAa,CAAC,WAAW,CAAC,EACnC,KAAK,EAAE,aAAa,CAAC,SAAS,CAAC,EAC/B,IAAI,EAAE,eAAe,GACpB,MAAM,EAAE,CA+BV"}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { loadEntitiesByMemoryId, loadEntitiesByIds, loadNeighborRelations, } from './graph.js';
|
|
2
|
+
import { MAX_HOPS, DEFAULT_MAX_NEIGHBORS } from './graph-recall.js';
|
|
3
|
+
/** Default hops expanded from each seed (MVP; hard cap MAX_HOPS=3 reused from graph-recall). */
|
|
4
|
+
export const DEFAULT_GRAPH_HOPS = 2;
|
|
5
|
+
/** Default per-hop multiplicative decay applied to the seed strength. */
|
|
6
|
+
export const DEFAULT_GRAPH_DECAY = 0.5;
|
|
7
|
+
/** Default number of top lexical seeds expanded from. */
|
|
8
|
+
export const DEFAULT_GRAPH_SEED_COUNT = 10;
|
|
9
|
+
/** Recommended RRF weight for the graph stream — a CLI-only convenience default. The
|
|
10
|
+
* library `graphStream.weight` option stays REQUIRED (opt-in is explicit). */
|
|
11
|
+
export const DEFAULT_GRAPH_STREAM_WEIGHT = 0.5;
|
|
12
|
+
/**
|
|
13
|
+
* Pick the top `seedCount` candidates by best lexical rank (lowest position across the
|
|
14
|
+
* BM25 and dense ranked lists), with strength = 1/(bestRank + 1). Pure; exported for
|
|
15
|
+
* direct unit testing. A candidate present in either ranked list is eligible.
|
|
16
|
+
*/
|
|
17
|
+
export function selectGraphSeeds(bm25Ranked, cosineRanked, seedCount) {
|
|
18
|
+
if (seedCount <= 0)
|
|
19
|
+
return [];
|
|
20
|
+
const bestPos = new Map();
|
|
21
|
+
const consider = (list) => {
|
|
22
|
+
for (let p = 0; p < list.length; p++) {
|
|
23
|
+
const idx = list[p];
|
|
24
|
+
const prev = bestPos.get(idx);
|
|
25
|
+
if (prev === undefined || p < prev)
|
|
26
|
+
bestPos.set(idx, p);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
consider(bm25Ranked);
|
|
30
|
+
consider(cosineRanked);
|
|
31
|
+
return [...bestPos.entries()]
|
|
32
|
+
.sort((a, b) => a[1] - b[1] || a[0] - b[0]) // best rank asc, then index asc (deterministic)
|
|
33
|
+
.slice(0, seedCount)
|
|
34
|
+
.map(([index, best]) => ({ index, strength: 1 / (best + 1) }));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Accumulate per-entryIndex graph-proximity scores from ONE store's graph into
|
|
38
|
+
* `graphScore`. Pure reads. `seeds` are the lexical seeds (index + strength); only the
|
|
39
|
+
* seeds whose entities live in THIS store are expanded. The origin seed strength is
|
|
40
|
+
* carried UNCHANGED along each BFS path; the per-hop decay is applied as decay^depth so
|
|
41
|
+
* a neighbour's score = originSeedStrength x decay^(graph distance).
|
|
42
|
+
*/
|
|
43
|
+
function accumulateForRoot(root, seeds, entries, memIdToIndex, graphScore, hops, decay, maxNeighbors, tenantId) {
|
|
44
|
+
if (seeds.length === 0)
|
|
45
|
+
return;
|
|
46
|
+
// The strongest seed strength per source memory id (a memId could appear once, but
|
|
47
|
+
// guard against dup indices mapping to the same memId).
|
|
48
|
+
const strengthByMemId = new Map();
|
|
49
|
+
for (const s of seeds) {
|
|
50
|
+
const memId = entries[s.index].id;
|
|
51
|
+
strengthByMemId.set(memId, Math.max(strengthByMemId.get(memId) ?? 0, s.strength));
|
|
52
|
+
}
|
|
53
|
+
const seedEntities = loadEntitiesByMemoryId(root, tenantId, [...strengthByMemId.keys()]);
|
|
54
|
+
if (seedEntities.length === 0)
|
|
55
|
+
return;
|
|
56
|
+
// entityId -> origin seed strength (carried unchanged along the path).
|
|
57
|
+
const originStrength = new Map();
|
|
58
|
+
for (const e of seedEntities) {
|
|
59
|
+
const st = strengthByMemId.get(e.memoryId) ?? 0;
|
|
60
|
+
originStrength.set(e.id, Math.max(originStrength.get(e.id) ?? 0, st));
|
|
61
|
+
}
|
|
62
|
+
const visited = new Set(seedEntities.map((e) => e.id)); // seeds never re-reached
|
|
63
|
+
const reachedScore = new Map(); // entityId -> best score
|
|
64
|
+
let frontier = seedEntities.map((e) => e.id);
|
|
65
|
+
let frontierStrength = new Map(originStrength); // entityId -> seed strength
|
|
66
|
+
for (let depth = 1; depth <= hops && frontier.length > 0; depth++) {
|
|
67
|
+
const frontierSet = new Set(frontier);
|
|
68
|
+
const rels = loadNeighborRelations(root, tenantId, frontier, {
|
|
69
|
+
limit: Math.max(maxNeighbors, maxNeighbors * frontier.length),
|
|
70
|
+
});
|
|
71
|
+
const hopFactor = Math.pow(decay, depth);
|
|
72
|
+
// Pass 1: accumulate the STRONGEST reaching-seed strength per new neighbour across ALL
|
|
73
|
+
// relations at this depth BEFORE committing any to `visited` (codex P2). Marking a node
|
|
74
|
+
// visited mid-loop would lock it to whichever relation SQLite returned first, so a later
|
|
75
|
+
// edge from a STRONGER lexical seed would be dropped and the neighbour mis-scored. A node
|
|
76
|
+
// already in `visited` was committed at an earlier (shorter) depth and keeps that score.
|
|
77
|
+
const bestStrengthThisDepth = new Map();
|
|
78
|
+
for (const rel of rels) {
|
|
79
|
+
const fromIn = frontierSet.has(rel.fromEntityId);
|
|
80
|
+
const toIn = frontierSet.has(rel.toEntityId);
|
|
81
|
+
let neighborId;
|
|
82
|
+
let reacherId;
|
|
83
|
+
if (fromIn && !toIn) {
|
|
84
|
+
neighborId = rel.toEntityId;
|
|
85
|
+
reacherId = rel.fromEntityId;
|
|
86
|
+
}
|
|
87
|
+
else if (toIn && !fromIn) {
|
|
88
|
+
neighborId = rel.fromEntityId;
|
|
89
|
+
reacherId = rel.toEntityId;
|
|
90
|
+
}
|
|
91
|
+
else
|
|
92
|
+
continue;
|
|
93
|
+
if (visited.has(neighborId))
|
|
94
|
+
continue;
|
|
95
|
+
const seedStrength = frontierStrength.get(reacherId) ?? originStrength.get(reacherId) ?? 0;
|
|
96
|
+
bestStrengthThisDepth.set(neighborId, Math.max(bestStrengthThisDepth.get(neighborId) ?? 0, seedStrength));
|
|
97
|
+
}
|
|
98
|
+
// Pass 2: commit strongest-first (then id asc — deterministic), so the per-hop fanout cap
|
|
99
|
+
// keeps the highest-scoring neighbours rather than whichever SQLite happened to return.
|
|
100
|
+
const nextFrontier = [];
|
|
101
|
+
const nextStrength = new Map();
|
|
102
|
+
const ordered = [...bestStrengthThisDepth.keys()].sort((a, b) => {
|
|
103
|
+
const d = bestStrengthThisDepth.get(b) - bestStrengthThisDepth.get(a);
|
|
104
|
+
return d !== 0 ? d : a - b;
|
|
105
|
+
});
|
|
106
|
+
for (const neighborId of ordered) {
|
|
107
|
+
if (nextFrontier.length >= maxNeighbors)
|
|
108
|
+
break; // per-hop fanout cap (strongest kept)
|
|
109
|
+
const seedStrength = bestStrengthThisDepth.get(neighborId);
|
|
110
|
+
visited.add(neighborId);
|
|
111
|
+
reachedScore.set(neighborId, Math.max(reachedScore.get(neighborId) ?? 0, seedStrength * hopFactor));
|
|
112
|
+
nextStrength.set(neighborId, seedStrength);
|
|
113
|
+
nextFrontier.push(neighborId);
|
|
114
|
+
}
|
|
115
|
+
frontier = nextFrontier;
|
|
116
|
+
frontierStrength = nextStrength;
|
|
117
|
+
}
|
|
118
|
+
if (reachedScore.size === 0)
|
|
119
|
+
return;
|
|
120
|
+
// Reached entity ids -> source memory ids -> in-pool entry indices.
|
|
121
|
+
const reachedEntities = loadEntitiesByIds(root, tenantId, [...reachedScore.keys()]);
|
|
122
|
+
for (const ent of reachedEntities) {
|
|
123
|
+
const idx = memIdToIndex.get(ent.memoryId);
|
|
124
|
+
if (idx === undefined)
|
|
125
|
+
continue; // reached memory is not in the candidate pool
|
|
126
|
+
const score = reachedScore.get(ent.id) ?? 0;
|
|
127
|
+
if (score <= 0)
|
|
128
|
+
continue;
|
|
129
|
+
graphScore.set(idx, Math.max(graphScore.get(idx) ?? 0, score));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Produce the graph-retrieval ranked list: `entries[]` indices ordered by graph
|
|
134
|
+
* proximity (desc) to the lexical `seeds`. Only graph-reached, in-pool, non-seed
|
|
135
|
+
* indices appear; the rest are absent (-> rrfFuse absentRank). Pure reads.
|
|
136
|
+
*
|
|
137
|
+
* Returns `[]` when there are no seeds/entries, the graph is empty, no seed maps to an
|
|
138
|
+
* entity, or nothing reached is in-pool — the caller then skips the 3rd fusion list.
|
|
139
|
+
*/
|
|
140
|
+
export function graphRankStream(entries, seeds, opts) {
|
|
141
|
+
if (seeds.length === 0 || entries.length === 0)
|
|
142
|
+
return [];
|
|
143
|
+
const hops = Math.min(Math.max(opts.hops ?? DEFAULT_GRAPH_HOPS, 1), MAX_HOPS);
|
|
144
|
+
const decay = opts.decay ?? DEFAULT_GRAPH_DECAY;
|
|
145
|
+
const maxNeighbors = opts.maxNeighbors ?? DEFAULT_MAX_NEIGHBORS;
|
|
146
|
+
const memIdToIndex = new Map();
|
|
147
|
+
for (let i = 0; i < entries.length; i++)
|
|
148
|
+
memIdToIndex.set(entries[i].id, i);
|
|
149
|
+
const graphScore = new Map();
|
|
150
|
+
const roots = opts.globalRoot && opts.globalRoot !== opts.hippoRoot
|
|
151
|
+
? [opts.hippoRoot, opts.globalRoot]
|
|
152
|
+
: [opts.hippoRoot];
|
|
153
|
+
for (const root of roots) {
|
|
154
|
+
accumulateForRoot(root, seeds, entries, memIdToIndex, graphScore, hops, decay, maxNeighbors, opts.tenantId);
|
|
155
|
+
}
|
|
156
|
+
// Seed-exclusion guard (plan-eng-critic MED): graphScore is keyed by entryIndex
|
|
157
|
+
// GLOBALLY across roots, but each root's BFS visited-set is per-root, so a memory that
|
|
158
|
+
// is a seed in one store could be reached as a neighbour in the other store and pick up
|
|
159
|
+
// a score via max(). Drop every seed index so the "seeds are never scored by the graph
|
|
160
|
+
// stream" invariant holds across roots, not just within a single store's traversal.
|
|
161
|
+
for (const s of seeds)
|
|
162
|
+
graphScore.delete(s.index);
|
|
163
|
+
if (graphScore.size === 0)
|
|
164
|
+
return [];
|
|
165
|
+
return [...graphScore.keys()].sort((a, b) => {
|
|
166
|
+
const d = (graphScore.get(b) ?? 0) - (graphScore.get(a) ?? 0);
|
|
167
|
+
return d !== 0 ? d : a - b; // score desc, then index asc (deterministic)
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=graph-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-stream.js","sourceRoot":"","sources":["../src/graph-stream.ts"],"names":[],"mappings":"AA2BA,OAAO,EACL,sBAAsB,EACtB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,QAAQ,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAEpE,gGAAgG;AAChG,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,CAAC;AACpC,yEAAyE;AACzE,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC;AACvC,yDAAyD;AACzD,MAAM,CAAC,MAAM,wBAAwB,GAAG,EAAE,CAAC;AAC3C;+EAC+E;AAC/E,MAAM,CAAC,MAAM,2BAA2B,GAAG,GAAG,CAAC;AAuB/C;;;;GAIG;AACH,MAAM,UAAU,gBAAgB,CAC9B,UAAiC,EACjC,YAAmC,EACnC,SAAiB;IAEjB,IAAI,SAAS,IAAI,CAAC;QAAE,OAAO,EAAE,CAAC;IAC9B,MAAM,OAAO,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC1C,MAAM,QAAQ,GAAG,CAAC,IAA2B,EAAE,EAAE;QAC/C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACrC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,C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
|
1
|
+
/**
|
|
2
|
+
* E3 graph observability + visualization — READ-ONLY over the entity/relation
|
|
3
|
+
* graph (docs/plans/2026-06-02-graph-observability.md).
|
|
4
|
+
*
|
|
5
|
+
* This module only READS the graph (`loadEntities` / `loadRelations`) and renders
|
|
6
|
+
* view-models; it issues no INSERT/UPDATE/DELETE, so `scripts/check-graph-writes.mjs`
|
|
7
|
+
* stays green. Used by the CLI (`hippo graph show` / `hippo graph view`) and the
|
|
8
|
+
* HTTP `GET /v1/graph` route, which all build the same `GraphModel`.
|
|
9
|
+
*/
|
|
10
|
+
import { type EntityType, type RelationType } from './graph.js';
|
|
11
|
+
export interface GraphNode {
|
|
12
|
+
id: number;
|
|
13
|
+
type: EntityType;
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
export interface GraphEdge {
|
|
17
|
+
from: number;
|
|
18
|
+
to: number;
|
|
19
|
+
relType: RelationType;
|
|
20
|
+
}
|
|
21
|
+
export interface GraphModel {
|
|
22
|
+
nodes: GraphNode[];
|
|
23
|
+
edges: GraphEdge[];
|
|
24
|
+
/** Conservative "maybe incomplete" flag: true when a loader returned exactly
|
|
25
|
+
* its limit (the graph MAY be truncated). Can over-report, never under-report. */
|
|
26
|
+
truncated: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** Default bound for the CLI viewer / show so an unbounded set is never laid out. */
|
|
29
|
+
export declare const DEFAULT_VIEW_LIMIT = 500;
|
|
30
|
+
/**
|
|
31
|
+
* Build the view-model from the graph (reads only). With `opts.entity`, returns a
|
|
32
|
+
* focus subgraph: every entity whose name === entity, plus their 1-hop neighbours,
|
|
33
|
+
* plus the edges among that union. Dangling edges (an endpoint outside the node
|
|
34
|
+
* set) are always dropped.
|
|
35
|
+
*/
|
|
36
|
+
export declare function buildGraphModel(hippoRoot: string, tenantId: string, opts?: {
|
|
37
|
+
entity?: string;
|
|
38
|
+
limit?: number;
|
|
39
|
+
}): GraphModel;
|
|
40
|
+
/**
|
|
41
|
+
* Deterministic Fruchterman-Reingold-style force layout. Seeded circular init +
|
|
42
|
+
* fixed iterations, NO `Math.random`, so the same model always yields the same
|
|
43
|
+
* positions (testable, stable output). Non-finite coordinates from a degenerate
|
|
44
|
+
* step are clamped to the viewport centre; all positions are clamped in-bounds.
|
|
45
|
+
*/
|
|
46
|
+
export declare function layoutGraph(model: GraphModel, opts?: {
|
|
47
|
+
width?: number;
|
|
48
|
+
height?: number;
|
|
49
|
+
iterations?: number;
|
|
50
|
+
}): Map<number, {
|
|
51
|
+
x: number;
|
|
52
|
+
y: number;
|
|
53
|
+
}>;
|
|
54
|
+
/** HTML-escape for SVG text / title / markup content (XSS guard). */
|
|
55
|
+
export declare function escapeHtml(s: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* Render the model as a SELF-CONTAINED, dependency-free, offline interactive HTML
|
|
58
|
+
* node-link diagram. Positions are computed server-side (deterministic). User
|
|
59
|
+
* strings are escaped per sink: SVG `<text>`/`<title>` via `escapeHtml`; the model
|
|
60
|
+
* is inlined in a `<script type="application/json">` block with `<`/`>`/`&`
|
|
61
|
+
* unicode-escaped so a `</script>` inside an entity name cannot break out (the
|
|
62
|
+
* client `JSON.parse`s it back and never `innerHTML`s a user string).
|
|
63
|
+
*/
|
|
64
|
+
export declare function renderGraphHtml(model: GraphModel): string;
|
|
65
|
+
/**
|
|
66
|
+
* Render the model as a JSON Canvas (jsoncanvas.org) document — `nodes[]` of
|
|
67
|
+
* type `text` positioned by the same deterministic layout, `edges[]` linking
|
|
68
|
+
* them by id. Opens natively in Obsidian. Pure JSON; entity names live in the
|
|
69
|
+
* `text` field (Obsidian renders/sanitizes them).
|
|
70
|
+
*/
|
|
71
|
+
export declare function renderGraphCanvas(model: GraphModel): string;
|
|
72
|
+
//# sourceMappingURL=graph-view.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"graph-view.d.ts","sourceRoot":"","sources":["../src/graph-view.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AA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GAAG,MAAM,CAqB3D"}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* E3 graph observability + visualization — READ-ONLY over the entity/relation
|
|
3
|
+
* graph (docs/plans/2026-06-02-graph-observability.md).
|
|
4
|
+
*
|
|
5
|
+
* This module only READS the graph (`loadEntities` / `loadRelations`) and renders
|
|
6
|
+
* view-models; it issues no INSERT/UPDATE/DELETE, so `scripts/check-graph-writes.mjs`
|
|
7
|
+
* stays green. Used by the CLI (`hippo graph show` / `hippo graph view`) and the
|
|
8
|
+
* HTTP `GET /v1/graph` route, which all build the same `GraphModel`.
|
|
9
|
+
*/
|
|
10
|
+
import { loadEntities, loadEntitiesByName, loadEntitiesByIds, loadRelations, loadNeighborRelations, loadRelationsAmong, withGraphReadSnapshot, } from './graph.js';
|
|
11
|
+
/** Default bound for the CLI viewer / show so an unbounded set is never laid out. */
|
|
12
|
+
export const DEFAULT_VIEW_LIMIT = 500;
|
|
13
|
+
/**
|
|
14
|
+
* Build the view-model from the graph (reads only). With `opts.entity`, returns a
|
|
15
|
+
* focus subgraph: every entity whose name === entity, plus their 1-hop neighbours,
|
|
16
|
+
* plus the edges among that union. Dangling edges (an endpoint outside the node
|
|
17
|
+
* set) are always dropped.
|
|
18
|
+
*/
|
|
19
|
+
export function buildGraphModel(hippoRoot, tenantId, opts = {}) {
|
|
20
|
+
const limit = opts.limit ?? DEFAULT_VIEW_LIMIT;
|
|
21
|
+
// All reads run inside ONE read snapshot so a concurrent `graph extract` /
|
|
22
|
+
// sleep-drain rebuild can't make the model mix old entity ids with new relation
|
|
23
|
+
// ids (codex P2). Every load* call below is passed the snapshot connection `db`.
|
|
24
|
+
return withGraphReadSnapshot(hippoRoot, (db) => {
|
|
25
|
+
let nodeEntities;
|
|
26
|
+
let relations;
|
|
27
|
+
let truncated;
|
|
28
|
+
if (opts.entity !== undefined) {
|
|
29
|
+
// Focus subgraph. (1) Query the named entity DIRECTLY (by name, not from a
|
|
30
|
+
// globally-capped list) so it is found even on a graph larger than `limit`.
|
|
31
|
+
// (2) A name can map to MANY entities (e.g. many notes for one customer), so
|
|
32
|
+
// cap the focus matches. (3) Discover 1-hop neighbours and cap the UNION to
|
|
33
|
+
// `limit` nodes. (4) Load ALL edges AMONG the union so neighbour-to-neighbour
|
|
34
|
+
// edges that don't touch the focus are included too. (codex P2s.)
|
|
35
|
+
const focus = loadEntitiesByName(hippoRoot, tenantId, opts.entity, { limit }, db);
|
|
36
|
+
if (focus.length === 0)
|
|
37
|
+
return { nodes: [], edges: [], truncated: false };
|
|
38
|
+
const focusIds = focus.map((e) => e.id);
|
|
39
|
+
const hop = loadNeighborRelations(hippoRoot, tenantId, focusIds, { limit }, db);
|
|
40
|
+
const union = new Set(focusIds);
|
|
41
|
+
let neighboursCapped = false;
|
|
42
|
+
for (const r of hop) {
|
|
43
|
+
if (union.size >= limit) {
|
|
44
|
+
neighboursCapped = true; // node cap filled before all neighbours were added
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
union.add(r.fromEntityId);
|
|
48
|
+
union.add(r.toEntityId);
|
|
49
|
+
}
|
|
50
|
+
const unionIds = [...union].slice(0, limit);
|
|
51
|
+
nodeEntities = loadEntitiesByIds(hippoRoot, tenantId, unionIds, db);
|
|
52
|
+
// Edges AMONG the union (BOTH endpoints in the set): includes
|
|
53
|
+
// neighbour-to-neighbour edges, and the LIMIT can never drop a valid in-union
|
|
54
|
+
// edge in favour of out-of-union rows (codex P2).
|
|
55
|
+
relations = loadRelationsAmong(hippoRoot, tenantId, unionIds, { limit }, db);
|
|
56
|
+
truncated =
|
|
57
|
+
focus.length >= limit ||
|
|
58
|
+
hop.length >= limit || // neighbour scan capped -> a 1-hop neighbour may be omitted (codex P2)
|
|
59
|
+
neighboursCapped || // node cap filled before all neighbours were consumed (codex P2)
|
|
60
|
+
union.size > unionIds.length ||
|
|
61
|
+
relations.length >= limit;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
nodeEntities = loadEntities(hippoRoot, tenantId, { limit }, db);
|
|
65
|
+
relations = loadRelations(hippoRoot, tenantId, { limit }, db);
|
|
66
|
+
truncated = nodeEntities.length >= limit || relations.length >= limit;
|
|
67
|
+
}
|
|
68
|
+
const nodeIds = new Set(nodeEntities.map((e) => e.id));
|
|
69
|
+
const nodes = nodeEntities.map((e) => ({
|
|
70
|
+
id: e.id,
|
|
71
|
+
type: e.entityType,
|
|
72
|
+
name: e.name,
|
|
73
|
+
}));
|
|
74
|
+
const edges = relations
|
|
75
|
+
.filter((r) => nodeIds.has(r.fromEntityId) && nodeIds.has(r.toEntityId))
|
|
76
|
+
.map((r) => ({ from: r.fromEntityId, to: r.toEntityId, relType: r.relType }));
|
|
77
|
+
return { nodes, edges, truncated };
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const LAYOUT_W = 1000;
|
|
81
|
+
const LAYOUT_H = 700;
|
|
82
|
+
/**
|
|
83
|
+
* Deterministic Fruchterman-Reingold-style force layout. Seeded circular init +
|
|
84
|
+
* fixed iterations, NO `Math.random`, so the same model always yields the same
|
|
85
|
+
* positions (testable, stable output). Non-finite coordinates from a degenerate
|
|
86
|
+
* step are clamped to the viewport centre; all positions are clamped in-bounds.
|
|
87
|
+
*/
|
|
88
|
+
export function layoutGraph(model, opts = {}) {
|
|
89
|
+
const width = opts.width ?? LAYOUT_W;
|
|
90
|
+
const height = opts.height ?? LAYOUT_H;
|
|
91
|
+
const iterations = opts.iterations ?? 300;
|
|
92
|
+
const cx = width / 2;
|
|
93
|
+
const cy = height / 2;
|
|
94
|
+
const nodes = model.nodes;
|
|
95
|
+
const n = nodes.length;
|
|
96
|
+
const pos = new Map();
|
|
97
|
+
if (n === 0)
|
|
98
|
+
return pos;
|
|
99
|
+
if (n === 1) {
|
|
100
|
+
pos.set(nodes[0].id, { x: cx, y: cy });
|
|
101
|
+
return pos;
|
|
102
|
+
}
|
|
103
|
+
const radius = Math.min(width, height) * 0.4;
|
|
104
|
+
nodes.forEach((node, i) => {
|
|
105
|
+
const angle = (2 * Math.PI * i) / n;
|
|
106
|
+
pos.set(node.id, { x: cx + radius * Math.cos(angle), y: cy + radius * Math.sin(angle) });
|
|
107
|
+
});
|
|
108
|
+
const idIndex = new Map(nodes.map((node, i) => [node.id, i]));
|
|
109
|
+
const k = Math.sqrt((width * height) / n); // ideal edge length
|
|
110
|
+
for (let iter = 0; iter < iterations; iter++) {
|
|
111
|
+
const disp = nodes.map(() => ({ x: 0, y: 0 }));
|
|
112
|
+
// Repulsion between all pairs.
|
|
113
|
+
for (let i = 0; i < n; i++) {
|
|
114
|
+
const pi = pos.get(nodes[i].id);
|
|
115
|
+
for (let j = i + 1; j < n; j++) {
|
|
116
|
+
const pj = pos.get(nodes[j].id);
|
|
117
|
+
const dx = pi.x - pj.x;
|
|
118
|
+
const dy = pi.y - pj.y;
|
|
119
|
+
const dist = Math.hypot(dx, dy) || 0.01;
|
|
120
|
+
const rep = (k * k) / dist;
|
|
121
|
+
const ux = dx / dist;
|
|
122
|
+
const uy = dy / dist;
|
|
123
|
+
disp[i].x += ux * rep;
|
|
124
|
+
disp[i].y += uy * rep;
|
|
125
|
+
disp[j].x -= ux * rep;
|
|
126
|
+
disp[j].y -= uy * rep;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// Attraction along edges.
|
|
130
|
+
for (const e of model.edges) {
|
|
131
|
+
const i = idIndex.get(e.from);
|
|
132
|
+
const j = idIndex.get(e.to);
|
|
133
|
+
if (i === undefined || j === undefined)
|
|
134
|
+
continue;
|
|
135
|
+
const pi = pos.get(e.from);
|
|
136
|
+
const pj = pos.get(e.to);
|
|
137
|
+
const dx = pi.x - pj.x;
|
|
138
|
+
const dy = pi.y - pj.y;
|
|
139
|
+
const dist = Math.hypot(dx, dy) || 0.01;
|
|
140
|
+
const att = (dist * dist) / k;
|
|
141
|
+
const ux = dx / dist;
|
|
142
|
+
const uy = dy / dist;
|
|
143
|
+
disp[i].x -= ux * att;
|
|
144
|
+
disp[i].y -= uy * att;
|
|
145
|
+
disp[j].x += ux * att;
|
|
146
|
+
disp[j].y += uy * att;
|
|
147
|
+
}
|
|
148
|
+
// Apply with cooling; clamp.
|
|
149
|
+
const temp = Math.max(1, (width / 10) * (1 - iter / iterations));
|
|
150
|
+
nodes.forEach((node, i) => {
|
|
151
|
+
const p = pos.get(node.id);
|
|
152
|
+
const dl = Math.hypot(disp[i].x, disp[i].y) || 0.01;
|
|
153
|
+
let nx = p.x + (disp[i].x / dl) * Math.min(dl, temp);
|
|
154
|
+
let ny = p.y + (disp[i].y / dl) * Math.min(dl, temp);
|
|
155
|
+
if (!Number.isFinite(nx))
|
|
156
|
+
nx = cx;
|
|
157
|
+
if (!Number.isFinite(ny))
|
|
158
|
+
ny = cy;
|
|
159
|
+
p.x = Math.max(20, Math.min(width - 20, nx));
|
|
160
|
+
p.y = Math.max(20, Math.min(height - 20, ny));
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return pos;
|
|
164
|
+
}
|
|
165
|
+
/** HTML-escape for SVG text / title / markup content (XSS guard). */
|
|
166
|
+
export function escapeHtml(s) {
|
|
167
|
+
return s
|
|
168
|
+
.replace(/&/g, '&')
|
|
169
|
+
.replace(/</g, '<')
|
|
170
|
+
.replace(/>/g, '>')
|
|
171
|
+
.replace(/"/g, '"')
|
|
172
|
+
.replace(/'/g, ''');
|
|
173
|
+
}
|
|
174
|
+
const NODE_COLORS = {
|
|
175
|
+
decision: '#6366f1',
|
|
176
|
+
policy: '#10b981',
|
|
177
|
+
customer: '#f59e0b',
|
|
178
|
+
project: '#ec4899',
|
|
179
|
+
person: '#3b82f6',
|
|
180
|
+
system: '#64748b',
|
|
181
|
+
};
|
|
182
|
+
// Client script: pan (drag bg), zoom (wheel), click-to-highlight a node's edges +
|
|
183
|
+
// neighbours. Reads the inlined model via JSON.parse (never innerHTML), so user
|
|
184
|
+
// strings never reach an HTML sink here. No template literals / `${}` so it nests
|
|
185
|
+
// safely inside this module's own template strings.
|
|
186
|
+
const CLIENT_JS = [
|
|
187
|
+
"(function(){",
|
|
188
|
+
" var svg=document.getElementById('g');",
|
|
189
|
+
" var vb=svg.getAttribute('viewBox').split(' ').map(Number);",
|
|
190
|
+
" var view={x:vb[0],y:vb[1],w:vb[2],h:vb[3]};",
|
|
191
|
+
" function apply(){svg.setAttribute('viewBox',view.x+' '+view.y+' '+view.w+' '+view.h);}",
|
|
192
|
+
" var panning=false,sx=0,sy=0;",
|
|
193
|
+
" svg.addEventListener('mousedown',function(e){if(e.target.closest('.node'))return;panning=true;sx=e.clientX;sy=e.clientY;});",
|
|
194
|
+
" window.addEventListener('mouseup',function(){panning=false;});",
|
|
195
|
+
" window.addEventListener('mousemove',function(e){if(!panning)return;var r=svg.getBoundingClientRect();var k=view.w/r.width;view.x-=(e.clientX-sx)*k;view.y-=(e.clientY-sy)*k;sx=e.clientX;sy=e.clientY;apply();});",
|
|
196
|
+
" svg.addEventListener('wheel',function(e){e.preventDefault();var r=svg.getBoundingClientRect();var mx=view.x+(e.clientX-r.left)/r.width*view.w;var my=view.y+(e.clientY-r.top)/r.height*view.h;var f=e.deltaY<0?0.9:1.1;view.x=mx-(mx-view.x)*f;view.y=my-(my-view.y)*f;view.w*=f;view.h*=f;apply();},{passive:false});",
|
|
197
|
+
" var data={};try{data=JSON.parse(document.getElementById('graph-data').textContent);}catch(_){}",
|
|
198
|
+
" var adj={};(data.edges||[]).forEach(function(e){(adj[e.from]=adj[e.from]||[]).push(e.to);(adj[e.to]=adj[e.to]||[]).push(e.from);});",
|
|
199
|
+
" var active=null;",
|
|
200
|
+
" document.querySelectorAll('.node').forEach(function(g){g.addEventListener('click',function(ev){ev.stopPropagation();var id=g.getAttribute('data-id');if(active===id){clearHi();active=null;return;}active=id;highlight(id);});});",
|
|
201
|
+
" svg.addEventListener('click',function(){clearHi();active=null;});",
|
|
202
|
+
" function clearHi(){svg.classList.remove('focused');document.querySelectorAll('.hi').forEach(function(el){el.classList.remove('hi');});}",
|
|
203
|
+
" function highlight(id){clearHi();svg.classList.add('focused');var keep={};keep[id]=1;(adj[id]||[]).forEach(function(n){keep[n]=1;});document.querySelectorAll('.node').forEach(function(g){if(keep[g.getAttribute('data-id')])g.classList.add('hi');});document.querySelectorAll('.edge').forEach(function(l){if(l.getAttribute('data-from')===id||l.getAttribute('data-to')===id)l.classList.add('hi');});}",
|
|
204
|
+
"})();",
|
|
205
|
+
].join("\n");
|
|
206
|
+
const STYLE = [
|
|
207
|
+
"html,body{margin:0;height:100%;background:#0b1020;font-family:ui-sans-serif,system-ui,sans-serif}",
|
|
208
|
+
"#g{width:100vw;height:100vh;cursor:grab}",
|
|
209
|
+
"#g:active{cursor:grabbing}",
|
|
210
|
+
".edge{stroke:#475569;stroke-width:1}",
|
|
211
|
+
".node circle{stroke:#0b1020;stroke-width:1.5}",
|
|
212
|
+
".node text{fill:#e2e8f0;font-size:11px;pointer-events:none}",
|
|
213
|
+
".node{cursor:pointer}",
|
|
214
|
+
"#g.focused .node{opacity:.18}",
|
|
215
|
+
"#g.focused .edge{opacity:.07}",
|
|
216
|
+
"#g.focused .node.hi{opacity:1}",
|
|
217
|
+
"#g.focused .edge.hi{opacity:1;stroke:#e2e8f0}",
|
|
218
|
+
".legend{position:fixed;top:10px;left:12px;color:#94a3b8;font-size:12px;line-height:1.6}",
|
|
219
|
+
".legend b{color:#e2e8f0}",
|
|
220
|
+
].join("\n");
|
|
221
|
+
/**
|
|
222
|
+
* Render the model as a SELF-CONTAINED, dependency-free, offline interactive HTML
|
|
223
|
+
* node-link diagram. Positions are computed server-side (deterministic). User
|
|
224
|
+
* strings are escaped per sink: SVG `<text>`/`<title>` via `escapeHtml`; the model
|
|
225
|
+
* is inlined in a `<script type="application/json">` block with `<`/`>`/`&`
|
|
226
|
+
* unicode-escaped so a `</script>` inside an entity name cannot break out (the
|
|
227
|
+
* client `JSON.parse`s it back and never `innerHTML`s a user string).
|
|
228
|
+
*/
|
|
229
|
+
export function renderGraphHtml(model) {
|
|
230
|
+
const pos = layoutGraph(model);
|
|
231
|
+
const color = (t) => NODE_COLORS[t] ?? '#64748b';
|
|
232
|
+
const edgeSvg = model.edges
|
|
233
|
+
.map((e) => {
|
|
234
|
+
const a = pos.get(e.from);
|
|
235
|
+
const b = pos.get(e.to);
|
|
236
|
+
if (!a || !b)
|
|
237
|
+
return '';
|
|
238
|
+
return (`<line class="edge" data-from="${e.from}" data-to="${e.to}" ` +
|
|
239
|
+
`x1="${a.x.toFixed(1)}" y1="${a.y.toFixed(1)}" x2="${b.x.toFixed(1)}" y2="${b.y.toFixed(1)}"/>`);
|
|
240
|
+
})
|
|
241
|
+
.join('');
|
|
242
|
+
const nodeSvg = model.nodes
|
|
243
|
+
.map((node) => {
|
|
244
|
+
const p = pos.get(node.id);
|
|
245
|
+
if (!p)
|
|
246
|
+
return '';
|
|
247
|
+
const label = node.name.length > 22 ? node.name.slice(0, 21) + '…' : node.name;
|
|
248
|
+
return (`<g class="node" data-id="${node.id}" transform="translate(${p.x.toFixed(1)},${p.y.toFixed(1)})">` +
|
|
249
|
+
`<title>${escapeHtml(node.type + ': ' + node.name)}</title>` +
|
|
250
|
+
`<circle r="7" fill="${color(node.type)}"/>` +
|
|
251
|
+
`<text x="10" y="4">${escapeHtml(label)}</text>` +
|
|
252
|
+
`</g>`);
|
|
253
|
+
})
|
|
254
|
+
.join('');
|
|
255
|
+
// Safe inline JSON for the client: unicode-escape the HTML-significant chars so
|
|
256
|
+
// the content cannot terminate the <script> block; JSON.parse restores them.
|
|
257
|
+
const dataJson = JSON.stringify(model)
|
|
258
|
+
.replace(/</g, '\\u003c')
|
|
259
|
+
.replace(/>/g, '\\u003e')
|
|
260
|
+
.replace(/&/g, '\\u0026');
|
|
261
|
+
const types = [...new Set(model.nodes.map((nd) => nd.type))];
|
|
262
|
+
const legend = types
|
|
263
|
+
.map((t) => `<span style="color:${color(t)}">●</span> ${escapeHtml(t)}`)
|
|
264
|
+
.join(' ');
|
|
265
|
+
return [
|
|
266
|
+
'<!doctype html>',
|
|
267
|
+
'<html lang="en"><head><meta charset="utf-8">',
|
|
268
|
+
'<meta name="viewport" content="width=device-width, initial-scale=1">',
|
|
269
|
+
'<title>hippo graph</title>',
|
|
270
|
+
'<style>' + STYLE + '</style></head><body>',
|
|
271
|
+
`<div class="legend"><b>hippo graph</b> · ${model.nodes.length} entities · ${model.edges.length} relations` +
|
|
272
|
+
(model.truncated ? ' · <b style="color:#f59e0b">truncated</b>' : '') +
|
|
273
|
+
`<br>${legend}<br><span style="color:#64748b">drag to pan · scroll to zoom · click a node to focus</span></div>`,
|
|
274
|
+
`<svg id="g" viewBox="0 0 ${LAYOUT_W} ${LAYOUT_H}" preserveAspectRatio="xMidYMid meet">`,
|
|
275
|
+
`<g id="edges">${edgeSvg}</g><g id="nodes">${nodeSvg}</g>`,
|
|
276
|
+
'</svg>',
|
|
277
|
+
`<script type="application/json" id="graph-data">${dataJson}</script>`,
|
|
278
|
+
'<script>' + CLIENT_JS + '</script>',
|
|
279
|
+
'</body></html>',
|
|
280
|
+
].join('\n');
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Render the model as a JSON Canvas (jsoncanvas.org) document — `nodes[]` of
|
|
284
|
+
* type `text` positioned by the same deterministic layout, `edges[]` linking
|
|
285
|
+
* them by id. Opens natively in Obsidian. Pure JSON; entity names live in the
|
|
286
|
+
* `text` field (Obsidian renders/sanitizes them).
|
|
287
|
+
*/
|
|
288
|
+
export function renderGraphCanvas(model) {
|
|
289
|
+
const pos = layoutGraph(model, { width: 2400, height: 1600 });
|
|
290
|
+
const nodes = model.nodes.map((node) => {
|
|
291
|
+
const p = pos.get(node.id) ?? { x: 0, y: 0 };
|
|
292
|
+
return {
|
|
293
|
+
id: `n${node.id}`,
|
|
294
|
+
type: 'text',
|
|
295
|
+
x: Math.round(p.x),
|
|
296
|
+
y: Math.round(p.y),
|
|
297
|
+
width: 240,
|
|
298
|
+
height: 60,
|
|
299
|
+
text: `**${node.type}**\n${node.name}`,
|
|
300
|
+
};
|
|
301
|
+
});
|
|
302
|
+
const edges = model.edges.map((e, i) => ({
|
|
303
|
+
id: `e${i}`,
|
|
304
|
+
fromNode: `n${e.from}`,
|
|
305
|
+
toNode: `n${e.to}`,
|
|
306
|
+
label: e.relType,
|
|
307
|
+
}));
|
|
308
|
+
return JSON.stringify({ nodes, edges }, null, 2);
|
|
309
|
+
}
|
|
310
|
+
//# sourceMappingURL=graph-view.js.map
|