@phren/cli 0.1.44 → 0.1.45
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/actions.d.ts +1 -1
- package/dist/cli/actions.js +7 -2
- package/dist/cli/config-proactivity.d.ts +6 -6
- package/dist/cli/hooks.d.ts +1 -1
- package/dist/cli/hooks.js +14 -3
- package/dist/cli/session-stop.d.ts +1 -1
- package/dist/cli/session-tool-hook.d.ts +1 -1
- package/dist/cli-registry.js +1 -1
- package/dist/data/access.js +2 -1
- package/dist/generated/memory-ui-graph.browser.js +481 -318
- package/dist/governance/locks.js +6 -0
- package/dist/graph-core/index.d.ts +2 -0
- package/dist/graph-core/index.js +2 -0
- package/dist/graph-core/model.d.ts +84 -0
- package/dist/graph-core/model.js +355 -0
- package/dist/graph-core/types.d.ts +110 -0
- package/dist/graph-core/types.js +43 -0
- package/dist/index.js +4 -1
- package/dist/init/init-uninstall.d.ts +1 -0
- package/dist/init/init-uninstall.js +1 -1
- package/dist/init/setup.js +11 -4
- package/dist/link/context.js +2 -1
- package/dist/link/link.js +3 -2
- package/dist/link/skills.js +2 -1
- package/dist/memory-ui-graph.runtime.js +481 -318
- package/dist/phren-core.d.ts +16 -0
- package/dist/phren-core.js +21 -0
- package/dist/phren-paths.js +8 -4
- package/dist/profile-store.js +3 -2
- package/dist/project-config.js +2 -1
- package/dist/shared/index.js +1 -1
- package/dist/shared/retrieval.d.ts +28 -0
- package/dist/shared/retrieval.js +45 -0
- package/dist/shell/entry.d.ts +3 -3
- package/dist/shell/entry.js +19 -44
- package/dist/shell/graph/canvas.d.ts +64 -0
- package/dist/shell/graph/canvas.js +256 -0
- package/dist/shell/graph/controller.d.ts +164 -0
- package/dist/shell/graph/controller.js +645 -0
- package/dist/shell/graph/glyphs.d.ts +12 -0
- package/dist/shell/graph/glyphs.js +31 -0
- package/dist/shell/graph/graph-view.d.ts +12 -0
- package/dist/shell/graph/graph-view.js +408 -0
- package/dist/shell/graph/layout.d.ts +68 -0
- package/dist/shell/graph/layout.js +267 -0
- package/dist/shell/input.d.ts +3 -0
- package/dist/shell/input.js +24 -4
- package/dist/shell/intro.d.ts +37 -0
- package/dist/shell/intro.js +82 -0
- package/dist/shell/logo-fx.d.ts +35 -0
- package/dist/shell/logo-fx.js +128 -0
- package/dist/shell/render.d.ts +2 -2
- package/dist/shell/render.js +12 -28
- package/dist/shell/shell.d.ts +13 -1
- package/dist/shell/shell.js +45 -7
- package/dist/shell/startup.d.ts +37 -0
- package/dist/shell/startup.js +105 -0
- package/dist/shell/state-store.d.ts +1 -1
- package/dist/shell/state-store.js +1 -1
- package/dist/shell/types.js +1 -0
- package/dist/shell/view.d.ts +3 -0
- package/dist/shell/view.js +24 -3
- package/dist/store-registry.js +3 -3
- package/dist/test-global-setup.d.ts +1 -0
- package/dist/test-global-setup.js +22 -0
- package/dist/tool-registry.js +1 -0
- package/dist/ui/data.d.ts +15 -7
- package/dist/ui/data.js +102 -3
- package/dist/utils-fts.js +2 -1
- package/dist/utils-helpers.d.ts +1 -0
- package/dist/utils-helpers.js +6 -0
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +1 -1
- package/package.json +11 -7
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* State + interaction for the terminal graph view.
|
|
3
|
+
*
|
|
4
|
+
* The controller is the terminal-side analogue of the browser's
|
|
5
|
+
* `browser/graph/state.ts` + `interactions.ts`: it owns the payload built by
|
|
6
|
+
* `buildGraph`, the normalized model from `graph-core`, the force layout, a 2D
|
|
7
|
+
* camera, selection/search/focus, and the key map. Rendering lives in
|
|
8
|
+
* `graph-view.ts`; this module never writes to the terminal.
|
|
9
|
+
*/
|
|
10
|
+
import { computePhrenLiveStateToken } from "../../phren-paths.js";
|
|
11
|
+
import { buildGraph } from "../../ui/data.js";
|
|
12
|
+
import { StoreColorAssigner, bestSearchMatch, buildFullAdjacency, buildVisibleData, nodeDetail, nodeRank, normalizeNode, recomputeSearchMatches, } from "../../graph-core/model.js";
|
|
13
|
+
import { errorMessage } from "../../utils.js";
|
|
14
|
+
import { logger } from "../../logger.js";
|
|
15
|
+
import { style } from "../render.js";
|
|
16
|
+
import { ForceSim, bounds } from "./layout.js";
|
|
17
|
+
/** Nodes drawn at once. Terminals have far fewer pixels than a browser tab. */
|
|
18
|
+
export const TUI_NODE_LIMIT = 350;
|
|
19
|
+
const ALL_TYPES = { project: true, finding: true, task: true, entity: true, reference: true };
|
|
20
|
+
export const FILTER_PRESETS = [
|
|
21
|
+
{ name: "all", types: ALL_TYPES, health: "all" },
|
|
22
|
+
{ name: "findings", types: { project: true, finding: true }, health: "all" },
|
|
23
|
+
{ name: "tasks", types: { project: true, task: true }, health: "all" },
|
|
24
|
+
{ name: "fragments", types: { project: true, entity: true, reference: true }, health: "all" },
|
|
25
|
+
{ name: "aging", types: ALL_TYPES, health: "aging" },
|
|
26
|
+
];
|
|
27
|
+
const DIRECTIONS = {
|
|
28
|
+
up: { x: 0, y: -1 },
|
|
29
|
+
down: { x: 0, y: 1 },
|
|
30
|
+
left: { x: -1, y: 0 },
|
|
31
|
+
right: { x: 1, y: 0 },
|
|
32
|
+
};
|
|
33
|
+
function normalizeArrow(rawKey) {
|
|
34
|
+
return /^\x1bO[A-D]$/.test(rawKey) ? `\x1b[${rawKey[2]}` : rawKey;
|
|
35
|
+
}
|
|
36
|
+
const ARROW_DIRECTION = {
|
|
37
|
+
"\x1b[A": "up",
|
|
38
|
+
"\x1b[B": "down",
|
|
39
|
+
"\x1b[D": "left",
|
|
40
|
+
"\x1b[C": "right",
|
|
41
|
+
};
|
|
42
|
+
/** Shift+arrow in the CSI 1;2 form most terminals emit. */
|
|
43
|
+
const SHIFT_ARROW_DIRECTION = {
|
|
44
|
+
"\x1b[1;2A": "up",
|
|
45
|
+
"\x1b[1;2B": "down",
|
|
46
|
+
"\x1b[1;2D": "left",
|
|
47
|
+
"\x1b[1;2C": "right",
|
|
48
|
+
};
|
|
49
|
+
const PAN_LETTER_DIRECTION = { K: "up", J: "down", H: "left", L: "right" };
|
|
50
|
+
export class GraphController {
|
|
51
|
+
phrenPath;
|
|
52
|
+
profile;
|
|
53
|
+
status = "idle";
|
|
54
|
+
errorText = "";
|
|
55
|
+
/** A rebuild is running while the previous picture stays on screen. */
|
|
56
|
+
get refreshing() { return this.building !== null && this.payload !== null; }
|
|
57
|
+
payload = null;
|
|
58
|
+
model = { rawNodes: [], rawLinks: [], nodeById: new Map(), fullAdjacency: new Map(), visibleAdjacency: new Map(), scores: {} };
|
|
59
|
+
filters = {
|
|
60
|
+
filterTypes: { ...ALL_TYPES },
|
|
61
|
+
filterTopics: {},
|
|
62
|
+
filterHealth: "all",
|
|
63
|
+
filterProject: "all",
|
|
64
|
+
filterStore: "all",
|
|
65
|
+
searchQuery: "",
|
|
66
|
+
nodeLimit: TUI_NODE_LIMIT,
|
|
67
|
+
};
|
|
68
|
+
visible = { nodes: [], links: [], visibleAdjacency: new Map() };
|
|
69
|
+
/** Project nodes in display order for `[`/`]`. */
|
|
70
|
+
projects = [];
|
|
71
|
+
presetIndex = 0;
|
|
72
|
+
selectedId = null;
|
|
73
|
+
search = { query: "", matchIds: new Set(), results: [], index: -1 };
|
|
74
|
+
camera = { cx: 0, cy: 0, scale: 2 };
|
|
75
|
+
sim = null;
|
|
76
|
+
lastPositions = new Map();
|
|
77
|
+
viewport = { width: 160, height: 80 };
|
|
78
|
+
dataToken = "";
|
|
79
|
+
building = null;
|
|
80
|
+
repaintHook = null;
|
|
81
|
+
timer = null;
|
|
82
|
+
cameraTarget = null;
|
|
83
|
+
/** Re-frame once the intro settle finishes so the whole map is in view. */
|
|
84
|
+
fitOnSettle = false;
|
|
85
|
+
/** Set by pan/zoom; until then a resize re-fits the whole map. */
|
|
86
|
+
userMoved = false;
|
|
87
|
+
builder;
|
|
88
|
+
tokenOf;
|
|
89
|
+
frameMs;
|
|
90
|
+
constructor(phrenPath, profile, opts = {}) {
|
|
91
|
+
this.phrenPath = phrenPath;
|
|
92
|
+
this.profile = profile;
|
|
93
|
+
this.builder = opts.builder ?? ((p, prof) => buildGraph(p, prof, undefined, null, { includeFragmentEdges: true, includeLifecycleEdges: true }));
|
|
94
|
+
this.tokenOf = opts.tokenOf ?? computePhrenLiveStateToken;
|
|
95
|
+
this.frameMs = opts.frameMs ?? 50;
|
|
96
|
+
}
|
|
97
|
+
// ── Data ────────────────────────────────────────────────────────────────
|
|
98
|
+
/**
|
|
99
|
+
* Let the shell trigger a repaint on its own (settle animation, fly-to,
|
|
100
|
+
* a build finishing). Without it the controller blocks the first render on
|
|
101
|
+
* the build and snaps every camera move.
|
|
102
|
+
*/
|
|
103
|
+
setRepaintHook(hook) {
|
|
104
|
+
this.repaintHook = hook;
|
|
105
|
+
}
|
|
106
|
+
get positions() {
|
|
107
|
+
return this.sim ? this.sim.positions : this.lastPositions;
|
|
108
|
+
}
|
|
109
|
+
lastToken = "";
|
|
110
|
+
lastTokenAt = 0;
|
|
111
|
+
/** The store token, re-read at most every 2s: animation repaints must not stat the whole store. */
|
|
112
|
+
currentToken() {
|
|
113
|
+
const now = Date.now();
|
|
114
|
+
if (now - this.lastTokenAt < 2000 && this.lastToken)
|
|
115
|
+
return this.lastToken;
|
|
116
|
+
try {
|
|
117
|
+
this.lastToken = this.tokenOf(this.phrenPath);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
this.lastToken = `err:${now}`;
|
|
121
|
+
}
|
|
122
|
+
this.lastTokenAt = now;
|
|
123
|
+
return this.lastToken;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Make sure a payload matching the store is loaded, or on its way. Blocks
|
|
127
|
+
* only on the very first build when there is no repaint hook to come back
|
|
128
|
+
* through; otherwise the old picture stays up with a refreshing badge.
|
|
129
|
+
*/
|
|
130
|
+
async ensureData() {
|
|
131
|
+
const token = this.currentToken();
|
|
132
|
+
if (this.payload && token === this.dataToken)
|
|
133
|
+
return;
|
|
134
|
+
if (!this.building) {
|
|
135
|
+
if (!this.payload)
|
|
136
|
+
this.status = "loading";
|
|
137
|
+
this.building = this.build(token).finally(() => { this.building = null; });
|
|
138
|
+
}
|
|
139
|
+
if (!this.payload && !this.repaintHook)
|
|
140
|
+
await this.building;
|
|
141
|
+
}
|
|
142
|
+
async build(token) {
|
|
143
|
+
try {
|
|
144
|
+
const payload = await this.builder(this.phrenPath, this.profile);
|
|
145
|
+
this.dataToken = token;
|
|
146
|
+
this.adopt(payload);
|
|
147
|
+
}
|
|
148
|
+
catch (err) {
|
|
149
|
+
this.errorText = errorMessage(err);
|
|
150
|
+
this.status = "error";
|
|
151
|
+
logger.debug("shell", `graph build failed: ${this.errorText}`);
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
this.repaintHook?.();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/** Load a payload directly (tests, or a host that already has one). */
|
|
158
|
+
adopt(payload) {
|
|
159
|
+
this.payload = payload;
|
|
160
|
+
const scores = payload.scores ?? {};
|
|
161
|
+
const storeColors = new StoreColorAssigner();
|
|
162
|
+
const rawNodes = (payload.nodes ?? []).map((node) => normalizeNode(node, scores, (s) => storeColors.color(s)));
|
|
163
|
+
const rawLinks = payload.links ?? [];
|
|
164
|
+
this.model = {
|
|
165
|
+
rawNodes,
|
|
166
|
+
rawLinks,
|
|
167
|
+
nodeById: new Map(rawNodes.map((node) => [node.id, node])),
|
|
168
|
+
fullAdjacency: buildFullAdjacency(rawNodes, rawLinks),
|
|
169
|
+
visibleAdjacency: new Map(),
|
|
170
|
+
scores,
|
|
171
|
+
};
|
|
172
|
+
this.projects = rawNodes.filter((node) => node.kind === "project").sort((a, b) => a.label.localeCompare(b.label));
|
|
173
|
+
if (this.filters.filterProject !== "all" && !this.projects.some((p) => (p.project || p.id) === this.filters.filterProject)) {
|
|
174
|
+
this.filters.filterProject = "all";
|
|
175
|
+
}
|
|
176
|
+
if (this.selectedId && !this.model.nodeById.has(this.selectedId))
|
|
177
|
+
this.selectedId = null;
|
|
178
|
+
this.status = rawNodes.length ? "ready" : "empty";
|
|
179
|
+
this.applyFilters(true);
|
|
180
|
+
}
|
|
181
|
+
// ── Filters / layout ────────────────────────────────────────────────────
|
|
182
|
+
get preset() {
|
|
183
|
+
return FILTER_PRESETS[this.presetIndex] ?? FILTER_PRESETS[0];
|
|
184
|
+
}
|
|
185
|
+
get focusedProject() {
|
|
186
|
+
return this.filters.filterProject === "all" ? null : this.filters.filterProject;
|
|
187
|
+
}
|
|
188
|
+
applyFilters(warm) {
|
|
189
|
+
this.visible = buildVisibleData(this.model, this.filters, this.selectedId);
|
|
190
|
+
this.model.visibleAdjacency = this.visible.visibleAdjacency;
|
|
191
|
+
const visibleIds = new Set(this.visible.nodes.map((node) => node.id));
|
|
192
|
+
if (this.selectedId && !visibleIds.has(this.selectedId))
|
|
193
|
+
this.selectedId = null;
|
|
194
|
+
this.rebuildLayout(warm);
|
|
195
|
+
this.refreshSearch();
|
|
196
|
+
}
|
|
197
|
+
rebuildLayout(warm) {
|
|
198
|
+
if (this.sim)
|
|
199
|
+
this.lastPositions = new Map([...this.sim.positions].map(([id, p]) => [id, { ...p }]));
|
|
200
|
+
const nodes = this.visible.nodes.map((node) => ({ id: node.id, kind: node.kind, project: node.project, size: node.size }));
|
|
201
|
+
const links = this.visible.links;
|
|
202
|
+
this.sim = new ForceSim(nodes, links);
|
|
203
|
+
if (warm && this.lastPositions.size)
|
|
204
|
+
this.sim.warmStart(this.lastPositions);
|
|
205
|
+
const fresh = !warm || !this.lastPositions.size;
|
|
206
|
+
if (this.repaintHook) {
|
|
207
|
+
this.sim.tick(warm ? 12 : 40);
|
|
208
|
+
if (fresh) {
|
|
209
|
+
this.fitAll();
|
|
210
|
+
this.fitOnSettle = true;
|
|
211
|
+
}
|
|
212
|
+
this.startAnimation();
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
this.sim.settle();
|
|
216
|
+
if (fresh)
|
|
217
|
+
this.fitAll();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The view reports its canvas size (in dots) before projecting. The first
|
|
222
|
+
* fit happens before any frame is drawn, so a size change re-fits unless
|
|
223
|
+
* the user has taken the camera over.
|
|
224
|
+
*/
|
|
225
|
+
setViewport(width, height) {
|
|
226
|
+
if (width === this.viewport.width && height === this.viewport.height)
|
|
227
|
+
return;
|
|
228
|
+
this.viewport = { width: Math.max(2, width), height: Math.max(4, height) };
|
|
229
|
+
if (!this.userMoved) {
|
|
230
|
+
const keepFlag = this.fitOnSettle;
|
|
231
|
+
this.fitAll();
|
|
232
|
+
this.fitOnSettle = keepFlag;
|
|
233
|
+
}
|
|
234
|
+
else if (this.selectedId)
|
|
235
|
+
this.keepInView(this.selectedId);
|
|
236
|
+
}
|
|
237
|
+
get viewportSize() {
|
|
238
|
+
return this.viewport;
|
|
239
|
+
}
|
|
240
|
+
/** World → dot coordinates for the current camera. */
|
|
241
|
+
project(p) {
|
|
242
|
+
return {
|
|
243
|
+
x: (p.x - this.camera.cx) * this.camera.scale + this.viewport.width / 2,
|
|
244
|
+
y: (p.y - this.camera.cy) * this.camera.scale + this.viewport.height / 2,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
fitAll() {
|
|
248
|
+
const box = bounds(this.positions.values());
|
|
249
|
+
this.cameraTarget = null;
|
|
250
|
+
this.fitOnSettle = false;
|
|
251
|
+
this.userMoved = false;
|
|
252
|
+
if (!box) {
|
|
253
|
+
this.camera = { cx: 0, cy: 0, scale: 2 };
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const w = Math.max(1, box.maxX - box.minX);
|
|
257
|
+
const h = Math.max(1, box.maxY - box.minY);
|
|
258
|
+
const pad = 0.86;
|
|
259
|
+
const scale = Math.min((this.viewport.width * pad) / w, (this.viewport.height * pad) / h);
|
|
260
|
+
this.camera = { cx: (box.minX + box.maxX) / 2, cy: (box.minY + box.maxY) / 2, scale: Math.max(0.2, Math.min(scale, 12)) };
|
|
261
|
+
}
|
|
262
|
+
zoom(factor) {
|
|
263
|
+
this.userMoved = true;
|
|
264
|
+
this.camera.scale = Math.max(0.2, Math.min(24, this.camera.scale * factor));
|
|
265
|
+
}
|
|
266
|
+
pan(direction) {
|
|
267
|
+
const d = DIRECTIONS[direction];
|
|
268
|
+
const stepX = (this.viewport.width * 0.12) / this.camera.scale;
|
|
269
|
+
const stepY = (this.viewport.height * 0.12) / this.camera.scale;
|
|
270
|
+
this.userMoved = true;
|
|
271
|
+
this.cameraTarget = null;
|
|
272
|
+
this.camera.cx += d.x * stepX;
|
|
273
|
+
this.camera.cy += d.y * stepY;
|
|
274
|
+
}
|
|
275
|
+
/** Centre the camera on a node, eased when animation is available. */
|
|
276
|
+
flyTo(nodeId) {
|
|
277
|
+
const p = this.positions.get(nodeId);
|
|
278
|
+
if (!p)
|
|
279
|
+
return;
|
|
280
|
+
if (this.repaintHook) {
|
|
281
|
+
this.cameraTarget = { x: p.x, y: p.y };
|
|
282
|
+
this.startAnimation();
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
this.camera.cx = p.x;
|
|
286
|
+
this.camera.cy = p.y;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
/** Fly only when the node sits outside the middle band of the viewport. */
|
|
290
|
+
keepInView(nodeId) {
|
|
291
|
+
const p = this.positions.get(nodeId);
|
|
292
|
+
if (!p)
|
|
293
|
+
return;
|
|
294
|
+
const d = this.project(p);
|
|
295
|
+
const { width, height } = this.viewport;
|
|
296
|
+
if (d.x < width * 0.18 || d.x > width * 0.82 || d.y < height * 0.18 || d.y > height * 0.82)
|
|
297
|
+
this.flyTo(nodeId);
|
|
298
|
+
}
|
|
299
|
+
relayout() {
|
|
300
|
+
this.lastPositions = new Map();
|
|
301
|
+
this.cameraTarget = null;
|
|
302
|
+
this.rebuildLayout(false);
|
|
303
|
+
this.fitAll();
|
|
304
|
+
}
|
|
305
|
+
// ── Animation ───────────────────────────────────────────────────────────
|
|
306
|
+
startAnimation() {
|
|
307
|
+
if (this.timer || !this.repaintHook)
|
|
308
|
+
return;
|
|
309
|
+
this.timer = setInterval(() => this.animationFrame(), this.frameMs);
|
|
310
|
+
this.timer.unref?.();
|
|
311
|
+
}
|
|
312
|
+
stopAnimation() {
|
|
313
|
+
if (this.timer) {
|
|
314
|
+
clearInterval(this.timer);
|
|
315
|
+
this.timer = null;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
get animating() {
|
|
319
|
+
return this.timer !== null;
|
|
320
|
+
}
|
|
321
|
+
animationFrame() {
|
|
322
|
+
let busy = false;
|
|
323
|
+
if (this.sim && !this.sim.settled) {
|
|
324
|
+
this.sim.tick(3);
|
|
325
|
+
busy = true;
|
|
326
|
+
if (this.sim.settled && this.fitOnSettle && !this.cameraTarget) {
|
|
327
|
+
this.fitAll();
|
|
328
|
+
this.fitOnSettle = false;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (this.cameraTarget) {
|
|
332
|
+
const t = this.cameraTarget;
|
|
333
|
+
this.camera.cx += (t.x - this.camera.cx) * 0.35;
|
|
334
|
+
this.camera.cy += (t.y - this.camera.cy) * 0.35;
|
|
335
|
+
if (Math.abs(t.x - this.camera.cx) < 0.05 && Math.abs(t.y - this.camera.cy) < 0.05) {
|
|
336
|
+
this.camera.cx = t.x;
|
|
337
|
+
this.camera.cy = t.y;
|
|
338
|
+
this.cameraTarget = null;
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
busy = true;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (!busy)
|
|
345
|
+
this.stopAnimation();
|
|
346
|
+
this.repaintHook?.();
|
|
347
|
+
}
|
|
348
|
+
/** Called by the shell when the view changes away or the shell closes. */
|
|
349
|
+
dispose() {
|
|
350
|
+
this.stopAnimation();
|
|
351
|
+
}
|
|
352
|
+
// ── Selection / search / focus ──────────────────────────────────────────
|
|
353
|
+
detail(nodeId) {
|
|
354
|
+
return nodeDetail(this.model, nodeId);
|
|
355
|
+
}
|
|
356
|
+
/** Visible neighbours of a node, best first — what the pane numbers 1-9. */
|
|
357
|
+
neighborsOf(nodeId) {
|
|
358
|
+
const ids = this.model.visibleAdjacency.get(nodeId);
|
|
359
|
+
if (!ids)
|
|
360
|
+
return [];
|
|
361
|
+
const nodes = [];
|
|
362
|
+
ids.forEach((id) => { const node = this.model.nodeById.get(id); if (node)
|
|
363
|
+
nodes.push(node); });
|
|
364
|
+
return nodes.sort((a, b) => nodeRank(b, this.filters, this.model.scores) - nodeRank(a, this.filters, this.model.scores));
|
|
365
|
+
}
|
|
366
|
+
select(nodeId, opts = {}) {
|
|
367
|
+
this.selectedId = nodeId;
|
|
368
|
+
if (nodeId) {
|
|
369
|
+
if (opts.fly)
|
|
370
|
+
this.flyTo(nodeId);
|
|
371
|
+
else
|
|
372
|
+
this.keepInView(nodeId);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
nearestToCenter() {
|
|
376
|
+
let best = null;
|
|
377
|
+
let bestDist = Infinity;
|
|
378
|
+
const center = { x: this.viewport.width / 2, y: this.viewport.height / 2 };
|
|
379
|
+
for (const node of this.visible.nodes) {
|
|
380
|
+
const p = this.positions.get(node.id);
|
|
381
|
+
if (!p)
|
|
382
|
+
continue;
|
|
383
|
+
const d = this.project(p);
|
|
384
|
+
const dist = (d.x - center.x) ** 2 + (d.y - center.y) ** 2;
|
|
385
|
+
if (dist < bestDist) {
|
|
386
|
+
bestDist = dist;
|
|
387
|
+
best = node;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return best;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Arrow-key traversal: prefer a connected neighbour in that direction; if
|
|
394
|
+
* there is none, take the nearest visible node in that half-plane so the
|
|
395
|
+
* walk never dead-ends on a leaf.
|
|
396
|
+
*/
|
|
397
|
+
walk(direction) {
|
|
398
|
+
if (!this.selectedId) {
|
|
399
|
+
const start = this.nearestToCenter();
|
|
400
|
+
if (start)
|
|
401
|
+
this.select(start.id);
|
|
402
|
+
return start;
|
|
403
|
+
}
|
|
404
|
+
const from = this.positions.get(this.selectedId);
|
|
405
|
+
if (!from)
|
|
406
|
+
return null;
|
|
407
|
+
const dir = DIRECTIONS[direction];
|
|
408
|
+
const pick = (candidates, minCos) => {
|
|
409
|
+
let best = null;
|
|
410
|
+
for (const id of candidates) {
|
|
411
|
+
if (id === this.selectedId)
|
|
412
|
+
continue;
|
|
413
|
+
const p = this.positions.get(id);
|
|
414
|
+
if (!p)
|
|
415
|
+
continue;
|
|
416
|
+
const dx = p.x - from.x;
|
|
417
|
+
const dy = p.y - from.y;
|
|
418
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
419
|
+
if (len === 0)
|
|
420
|
+
continue;
|
|
421
|
+
const cos = (dx * dir.x + dy * dir.y) / len;
|
|
422
|
+
if (cos <= minCos)
|
|
423
|
+
continue;
|
|
424
|
+
const score = len / Math.max(cos, 0.15);
|
|
425
|
+
if (!best || score < best.score)
|
|
426
|
+
best = { id, score };
|
|
427
|
+
}
|
|
428
|
+
return best;
|
|
429
|
+
};
|
|
430
|
+
const neighbors = this.model.visibleAdjacency.get(this.selectedId) ?? new Set();
|
|
431
|
+
const target = pick(neighbors, 0.1) ?? pick(this.visible.nodes.map((node) => node.id), 0.3);
|
|
432
|
+
if (!target)
|
|
433
|
+
return null;
|
|
434
|
+
this.select(target.id);
|
|
435
|
+
return this.model.nodeById.get(target.id) ?? null;
|
|
436
|
+
}
|
|
437
|
+
refreshSearch() {
|
|
438
|
+
const matches = recomputeSearchMatches(this.visible.nodes, this.search.query, this.filters, this.model.scores);
|
|
439
|
+
this.search.matchIds = matches.matchIds;
|
|
440
|
+
this.search.results = matches.results;
|
|
441
|
+
if (this.search.index >= matches.results.length)
|
|
442
|
+
this.search.index = matches.results.length ? 0 : -1;
|
|
443
|
+
}
|
|
444
|
+
/** Set the query, light up matches, and fly to the best one. */
|
|
445
|
+
applySearch(query) {
|
|
446
|
+
this.search.query = query.trim();
|
|
447
|
+
this.filters.searchQuery = this.search.query;
|
|
448
|
+
this.refreshSearch();
|
|
449
|
+
if (!this.search.query) {
|
|
450
|
+
this.search.index = -1;
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
const best = bestSearchMatch(this.search.results);
|
|
454
|
+
this.search.index = best ? 0 : -1;
|
|
455
|
+
if (best)
|
|
456
|
+
this.select(best.id, { fly: true });
|
|
457
|
+
return best;
|
|
458
|
+
}
|
|
459
|
+
clearSearch() {
|
|
460
|
+
this.applySearch("");
|
|
461
|
+
}
|
|
462
|
+
stepSearch(delta) {
|
|
463
|
+
const results = this.search.results;
|
|
464
|
+
if (!results.length)
|
|
465
|
+
return null;
|
|
466
|
+
this.search.index = ((this.search.index + delta) % results.length + results.length) % results.length;
|
|
467
|
+
const node = results[this.search.index];
|
|
468
|
+
this.select(node.id, { fly: true });
|
|
469
|
+
return node;
|
|
470
|
+
}
|
|
471
|
+
cyclePreset(delta = 1) {
|
|
472
|
+
this.presetIndex = ((this.presetIndex + delta) % FILTER_PRESETS.length + FILTER_PRESETS.length) % FILTER_PRESETS.length;
|
|
473
|
+
const preset = this.preset;
|
|
474
|
+
this.filters.filterTypes = { ...preset.types };
|
|
475
|
+
this.filters.filterHealth = preset.health;
|
|
476
|
+
this.applyFilters(true);
|
|
477
|
+
return preset;
|
|
478
|
+
}
|
|
479
|
+
/** Focus one project (name) or all (null). */
|
|
480
|
+
focusProject(name) {
|
|
481
|
+
this.filters.filterProject = name ?? "all";
|
|
482
|
+
this.applyFilters(true);
|
|
483
|
+
// Either way the visible set just changed shape: frame it, then keep the
|
|
484
|
+
// project selected so its neighbours are numbered.
|
|
485
|
+
this.cameraTarget = null;
|
|
486
|
+
this.fitAll();
|
|
487
|
+
if (name) {
|
|
488
|
+
const node = this.projects.find((p) => (p.project || p.id) === name);
|
|
489
|
+
if (node)
|
|
490
|
+
this.selectedId = node.id;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
cycleProject(delta) {
|
|
494
|
+
if (!this.projects.length)
|
|
495
|
+
return null;
|
|
496
|
+
const names = this.projects.map((p) => p.project || p.id);
|
|
497
|
+
const current = this.focusedProject ? names.indexOf(this.focusedProject) : -1;
|
|
498
|
+
// Slot -1 is "all projects"; the cycle runs all → first … last → all.
|
|
499
|
+
const slots = names.length + 1;
|
|
500
|
+
const next = (((current + 1 + delta) % slots) + slots) % slots - 1;
|
|
501
|
+
const name = next < 0 ? null : names[next];
|
|
502
|
+
this.focusProject(name);
|
|
503
|
+
return name;
|
|
504
|
+
}
|
|
505
|
+
jumpToNeighbor(n) {
|
|
506
|
+
if (!this.selectedId)
|
|
507
|
+
return null;
|
|
508
|
+
const node = this.neighborsOf(this.selectedId)[n - 1];
|
|
509
|
+
if (!node)
|
|
510
|
+
return null;
|
|
511
|
+
this.select(node.id);
|
|
512
|
+
return node;
|
|
513
|
+
}
|
|
514
|
+
// ── Keys ────────────────────────────────────────────────────────────────
|
|
515
|
+
/**
|
|
516
|
+
* Returns true when the key was consumed, undefined to let the shell's
|
|
517
|
+
* generic handler have it (q, :, ?, view shortcuts, final Esc).
|
|
518
|
+
*/
|
|
519
|
+
handleKey(rawKey, host) {
|
|
520
|
+
const key = normalizeArrow(rawKey);
|
|
521
|
+
if (this.status !== "ready") {
|
|
522
|
+
if (key === "r") {
|
|
523
|
+
this.dataToken = "";
|
|
524
|
+
host.setMessage(" Rebuilding graph…");
|
|
525
|
+
return true;
|
|
526
|
+
}
|
|
527
|
+
return undefined;
|
|
528
|
+
}
|
|
529
|
+
const arrow = ARROW_DIRECTION[key];
|
|
530
|
+
if (arrow) {
|
|
531
|
+
const node = this.walk(arrow);
|
|
532
|
+
host.setMessage(node ? this.describe(node) : ` ${style.dim("nothing further that way")}`);
|
|
533
|
+
return true;
|
|
534
|
+
}
|
|
535
|
+
const shifted = SHIFT_ARROW_DIRECTION[key] ?? PAN_LETTER_DIRECTION[key];
|
|
536
|
+
if (shifted) {
|
|
537
|
+
this.pan(shifted);
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
if (key === "+" || key === "=") {
|
|
541
|
+
this.zoom(1.25);
|
|
542
|
+
return true;
|
|
543
|
+
}
|
|
544
|
+
if (key === "-" || key === "_") {
|
|
545
|
+
this.zoom(1 / 1.25);
|
|
546
|
+
return true;
|
|
547
|
+
}
|
|
548
|
+
if (key === "0") {
|
|
549
|
+
this.fitAll();
|
|
550
|
+
host.setMessage(` ${style.dim("fit to screen")}`);
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
if (key === "r") {
|
|
554
|
+
this.relayout();
|
|
555
|
+
host.setMessage(` ${style.dim("re-laid out")}`);
|
|
556
|
+
return true;
|
|
557
|
+
}
|
|
558
|
+
if (key === "\r" || key === "\n") {
|
|
559
|
+
if (!this.selectedId) {
|
|
560
|
+
const node = this.nearestToCenter();
|
|
561
|
+
if (node) {
|
|
562
|
+
this.select(node.id);
|
|
563
|
+
host.setMessage(this.describe(node));
|
|
564
|
+
}
|
|
565
|
+
return true;
|
|
566
|
+
}
|
|
567
|
+
const node = this.model.nodeById.get(this.selectedId);
|
|
568
|
+
if (node?.kind === "project") {
|
|
569
|
+
const name = node.project || node.id;
|
|
570
|
+
const next = this.focusedProject === name ? null : name;
|
|
571
|
+
this.focusProject(next);
|
|
572
|
+
host.setMessage(next ? ` ${style.boldCyan("❖")} ${style.boldCyan(next)} ${style.dim("focused — ↵ again to release")}` : ` ${style.dim("all projects")}`);
|
|
573
|
+
}
|
|
574
|
+
else if (node) {
|
|
575
|
+
this.flyTo(node.id);
|
|
576
|
+
host.setMessage(this.describe(node));
|
|
577
|
+
}
|
|
578
|
+
return true;
|
|
579
|
+
}
|
|
580
|
+
if (/^[1-9]$/.test(key)) {
|
|
581
|
+
const node = this.jumpToNeighbor(Number(key));
|
|
582
|
+
if (node)
|
|
583
|
+
host.setMessage(this.describe(node));
|
|
584
|
+
else
|
|
585
|
+
host.setMessage(` ${style.dim(this.selectedId ? "no such neighbour" : "select a node first (↵)")}`);
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
if (key === "/") {
|
|
589
|
+
host.startInput("graph-search", this.search.query);
|
|
590
|
+
return true;
|
|
591
|
+
}
|
|
592
|
+
if (key === "n" || key === "N") {
|
|
593
|
+
const node = this.stepSearch(key === "n" ? 1 : -1);
|
|
594
|
+
host.setMessage(node
|
|
595
|
+
? ` ${style.yellow(`${this.search.index + 1}/${this.search.results.length}`)} ${this.describe(node).trimStart()}`
|
|
596
|
+
: ` ${style.dim("no search — press / first")}`);
|
|
597
|
+
return true;
|
|
598
|
+
}
|
|
599
|
+
if (key === "f") {
|
|
600
|
+
const preset = this.cyclePreset(1);
|
|
601
|
+
host.setMessage(` ${style.boldCyan("filter")} ${preset.name} ${style.dim(`${this.visible.nodes.length} nodes`)}`);
|
|
602
|
+
return true;
|
|
603
|
+
}
|
|
604
|
+
if (key === "F") {
|
|
605
|
+
const preset = this.cyclePreset(-1);
|
|
606
|
+
host.setMessage(` ${style.boldCyan("filter")} ${preset.name} ${style.dim(`${this.visible.nodes.length} nodes`)}`);
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
if (key === "]" || key === "[") {
|
|
610
|
+
const name = this.cycleProject(key === "]" ? 1 : -1);
|
|
611
|
+
host.setMessage(name ? ` ${style.boldCyan("❖")} ${style.boldCyan(name)}` : ` ${style.dim("all projects")}`);
|
|
612
|
+
return true;
|
|
613
|
+
}
|
|
614
|
+
if (key === "o") {
|
|
615
|
+
host.setMessage(` ${style.dim("open the 3D viewer in a browser:")} ${style.boldCyan("phren web-ui")}`);
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
if (key === "\x1b") {
|
|
619
|
+
if (this.search.query) {
|
|
620
|
+
this.clearSearch();
|
|
621
|
+
host.setMessage(` ${style.dim("search cleared")}`);
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
if (this.selectedId) {
|
|
625
|
+
this.select(null);
|
|
626
|
+
host.setMessage(` ${style.dim("selection cleared")}`);
|
|
627
|
+
return true;
|
|
628
|
+
}
|
|
629
|
+
if (this.focusedProject) {
|
|
630
|
+
this.focusProject(null);
|
|
631
|
+
host.setMessage(` ${style.dim("all projects")}`);
|
|
632
|
+
return true;
|
|
633
|
+
}
|
|
634
|
+
return undefined;
|
|
635
|
+
}
|
|
636
|
+
return undefined;
|
|
637
|
+
}
|
|
638
|
+
/** One-line description for the message bar. */
|
|
639
|
+
describe(node) {
|
|
640
|
+
const label = node.fullLabel || node.label;
|
|
641
|
+
const short = label.length > 70 ? `${label.slice(0, 68)}…` : label;
|
|
642
|
+
const where = node.project && node.kind !== "project" ? ` ${style.dim("·")} ${style.cyan(node.project)}` : "";
|
|
643
|
+
return ` ${style.dim(node.kind)} ${short}${where}`;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node glyphs for the terminal graph view.
|
|
3
|
+
*
|
|
4
|
+
* The default set is plain Unicode that every monospace font carries. With
|
|
5
|
+
* `PHREN_ICONS=nerd` the view switches to Nerd Font icons (the Font Awesome
|
|
6
|
+
* block, U+F000–U+F2E0, which has been stable across Nerd Font releases), for
|
|
7
|
+
* terminals running a patched font such as JetBrainsMono Nerd Font.
|
|
8
|
+
*/
|
|
9
|
+
import type { NodeKind } from "../../graph-core/types.js";
|
|
10
|
+
export type GlyphSet = Record<NodeKind, string>;
|
|
11
|
+
export declare function iconMode(env?: NodeJS.ProcessEnv): "unicode" | "nerd";
|
|
12
|
+
export declare function graphGlyphs(env?: NodeJS.ProcessEnv): GlyphSet;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node glyphs for the terminal graph view.
|
|
3
|
+
*
|
|
4
|
+
* The default set is plain Unicode that every monospace font carries. With
|
|
5
|
+
* `PHREN_ICONS=nerd` the view switches to Nerd Font icons (the Font Awesome
|
|
6
|
+
* block, U+F000–U+F2E0, which has been stable across Nerd Font releases), for
|
|
7
|
+
* terminals running a patched font such as JetBrainsMono Nerd Font.
|
|
8
|
+
*/
|
|
9
|
+
const UNICODE = {
|
|
10
|
+
project: "◉",
|
|
11
|
+
finding: "✦",
|
|
12
|
+
task: "▤",
|
|
13
|
+
entity: "❖",
|
|
14
|
+
reference: "▣",
|
|
15
|
+
other: "·",
|
|
16
|
+
};
|
|
17
|
+
const NERD = {
|
|
18
|
+
project: "", // nf-fa-folder
|
|
19
|
+
finding: "", // nf-fa-lightbulb_o
|
|
20
|
+
task: "", // nf-fa-check_square_o
|
|
21
|
+
entity: "", // nf-fa-share_alt (node graph)
|
|
22
|
+
reference: "", // nf-fa-book
|
|
23
|
+
other: "", // nf-fa-circle
|
|
24
|
+
};
|
|
25
|
+
export function iconMode(env = process.env) {
|
|
26
|
+
const raw = (env.PHREN_ICONS || "").trim().toLowerCase();
|
|
27
|
+
return raw === "nerd" || raw === "nerdfont" || raw === "nf" ? "nerd" : "unicode";
|
|
28
|
+
}
|
|
29
|
+
export function graphGlyphs(env = process.env) {
|
|
30
|
+
return iconMode(env) === "nerd" ? NERD : UNICODE;
|
|
31
|
+
}
|