@phren/cli 0.1.44 → 0.1.46
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/agents/providers/herdr.d.ts +18 -0
- package/dist/agents/providers/herdr.js +94 -0
- package/dist/agents/providers/spawner.d.ts +13 -0
- package/dist/agents/providers/spawner.js +63 -0
- package/dist/agents/registry.d.ts +28 -0
- package/dist/agents/registry.js +82 -0
- package/dist/agents/types.d.ts +51 -0
- package/dist/agents/types.js +42 -0
- 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/search.js +29 -0
- package/dist/cli/session-stop.d.ts +1 -1
- package/dist/cli/session-tool-hook.d.ts +1 -1
- package/dist/cli-registry.d.ts +1 -1
- package/dist/cli-registry.js +2 -1
- package/dist/content/learning.js +24 -1
- package/dist/data/access.js +2 -1
- package/dist/generated/memory-ui-graph.browser.js +482 -319
- 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 +445 -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 +482 -319
- package/dist/phren-art.d.ts +7 -0
- package/dist/phren-art.js +6 -41
- 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/lookup-tail.d.ts +35 -0
- package/dist/shared/lookup-tail.js +100 -0
- 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/agents.d.ts +48 -0
- package/dist/shell/graph/agents.js +152 -0
- package/dist/shell/graph/canvas.d.ts +64 -0
- package/dist/shell/graph/canvas.js +256 -0
- package/dist/shell/graph/controller.d.ts +202 -0
- package/dist/shell/graph/controller.js +782 -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 +19 -0
- package/dist/shell/graph/graph-view.js +585 -0
- package/dist/shell/graph/layout.d.ts +87 -0
- package/dist/shell/graph/layout.js +335 -0
- package/dist/shell/graph/watch.d.ts +71 -0
- package/dist/shell/graph/watch.js +153 -0
- package/dist/shell/input.d.ts +3 -0
- package/dist/shell/input.js +26 -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 +15 -28
- package/dist/shell/shell.d.ts +15 -1
- package/dist/shell/shell.js +50 -7
- package/dist/shell/startup.d.ts +40 -0
- package/dist/shell/startup.js +113 -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 +72 -63
- 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/ui/server.js +6 -56
- 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,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Live coding agents on the knowledge graph.
|
|
3
|
+
*
|
|
4
|
+
* Watch mode shows what phren's memory is doing; this shows who is doing it.
|
|
5
|
+
* Agents are transient runtime state rather than memory, so they decorate the
|
|
6
|
+
* project nodes they are working in instead of becoming graph nodes of their
|
|
7
|
+
* own — the same shape `GraphWatch` uses, and it keeps `graph-core` (which is
|
|
8
|
+
* bundled for the browser) untouched.
|
|
9
|
+
*/
|
|
10
|
+
import { execFileSync } from "child_process";
|
|
11
|
+
import { commandExists } from "../../hooks.js";
|
|
12
|
+
import { detectProject } from "../../shared/index.js";
|
|
13
|
+
import { debugLog } from "../../shared.js";
|
|
14
|
+
import { errorMessage } from "../../utils.js";
|
|
15
|
+
import { agentsEnabled, collectAgents, joinAgents, sortAgents } from "../../agents/registry.js";
|
|
16
|
+
import { createHerdrProvider } from "../../agents/providers/herdr.js";
|
|
17
|
+
import { createSpawnerProvider } from "../../agents/providers/spawner.js";
|
|
18
|
+
const DEFAULT_POLL_MS = 2000;
|
|
19
|
+
const FOCUS_TIMEOUT_MS = 5000;
|
|
20
|
+
/** Run a provider-supplied focus command. Never throws. */
|
|
21
|
+
export function runFocusCommand(argv) {
|
|
22
|
+
if (!argv.length)
|
|
23
|
+
return false;
|
|
24
|
+
const [command, ...args] = argv;
|
|
25
|
+
try {
|
|
26
|
+
execFileSync(command, args, { stdio: ["ignore", "ignore", "ignore"], timeout: FOCUS_TIMEOUT_MS });
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
debugLog(`agent focus: ${errorMessage(err)}`);
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class GraphAgents {
|
|
35
|
+
phrenPath;
|
|
36
|
+
profile;
|
|
37
|
+
agents = [];
|
|
38
|
+
/** Index into `agents`; -1 when nothing is highlighted. */
|
|
39
|
+
highlighted = -1;
|
|
40
|
+
enabled;
|
|
41
|
+
timer = null;
|
|
42
|
+
onUpdate = null;
|
|
43
|
+
pollMs;
|
|
44
|
+
collectFn;
|
|
45
|
+
runFocus;
|
|
46
|
+
constructor(phrenPath, profile, opts = {}) {
|
|
47
|
+
this.phrenPath = phrenPath;
|
|
48
|
+
this.profile = profile;
|
|
49
|
+
this.pollMs = opts.pollMs ?? DEFAULT_POLL_MS;
|
|
50
|
+
this.runFocus = opts.runFocus ?? runFocusCommand;
|
|
51
|
+
this.enabled = opts.enabled ?? agentsEnabled();
|
|
52
|
+
this.collectFn = opts.collect ?? (() => {
|
|
53
|
+
const providers = [createHerdrProvider(commandExists), createSpawnerProvider(phrenPath)];
|
|
54
|
+
return joinAgents(collectAgents(providers), (cwd) => detectProject(phrenPath, cwd, profile));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
get running() {
|
|
58
|
+
return this.timer !== null || this.polledOnce;
|
|
59
|
+
}
|
|
60
|
+
polledOnce = false;
|
|
61
|
+
start(onUpdate) {
|
|
62
|
+
if (!this.enabled || this.timer)
|
|
63
|
+
return;
|
|
64
|
+
this.onUpdate = onUpdate;
|
|
65
|
+
this.poll();
|
|
66
|
+
this.timer = setInterval(() => {
|
|
67
|
+
const before = signature(this.agents);
|
|
68
|
+
this.poll();
|
|
69
|
+
if (signature(this.agents) !== before)
|
|
70
|
+
this.onUpdate?.();
|
|
71
|
+
}, this.pollMs);
|
|
72
|
+
this.timer.unref?.();
|
|
73
|
+
}
|
|
74
|
+
stop() {
|
|
75
|
+
if (this.timer) {
|
|
76
|
+
clearInterval(this.timer);
|
|
77
|
+
this.timer = null;
|
|
78
|
+
}
|
|
79
|
+
this.onUpdate = null;
|
|
80
|
+
this.polledOnce = false;
|
|
81
|
+
this.agents = [];
|
|
82
|
+
this.highlighted = -1;
|
|
83
|
+
}
|
|
84
|
+
toggle() {
|
|
85
|
+
this.enabled = !this.enabled;
|
|
86
|
+
if (!this.enabled) {
|
|
87
|
+
this.stop();
|
|
88
|
+
this.enabled = false;
|
|
89
|
+
}
|
|
90
|
+
return this.enabled;
|
|
91
|
+
}
|
|
92
|
+
/** Refresh the list. Never throws; a failing provider just yields nothing. */
|
|
93
|
+
poll() {
|
|
94
|
+
let next = [];
|
|
95
|
+
try {
|
|
96
|
+
next = sortAgents(this.collectFn());
|
|
97
|
+
}
|
|
98
|
+
catch (err) {
|
|
99
|
+
debugLog(`graph agents poll: ${errorMessage(err)}`);
|
|
100
|
+
next = [];
|
|
101
|
+
}
|
|
102
|
+
const previousId = this.highlighted >= 0 ? this.agents[this.highlighted]?.id : undefined;
|
|
103
|
+
this.agents = next;
|
|
104
|
+
this.polledOnce = true;
|
|
105
|
+
// Keep the highlight on the same agent across a refresh where possible.
|
|
106
|
+
this.highlighted = previousId ? next.findIndex((a) => a.id === previousId) : -1;
|
|
107
|
+
return next;
|
|
108
|
+
}
|
|
109
|
+
/** Agents grouped by the project they are working in. */
|
|
110
|
+
byProject() {
|
|
111
|
+
const map = new Map();
|
|
112
|
+
for (const agent of this.agents) {
|
|
113
|
+
if (!agent.project)
|
|
114
|
+
continue;
|
|
115
|
+
const list = map.get(agent.project);
|
|
116
|
+
if (list)
|
|
117
|
+
list.push(agent);
|
|
118
|
+
else
|
|
119
|
+
map.set(agent.project, [agent]);
|
|
120
|
+
}
|
|
121
|
+
return map;
|
|
122
|
+
}
|
|
123
|
+
get current() {
|
|
124
|
+
return this.highlighted >= 0 ? this.agents[this.highlighted] ?? null : null;
|
|
125
|
+
}
|
|
126
|
+
/** Move the highlight; wraps, and starts from the first agent. */
|
|
127
|
+
cycle(delta) {
|
|
128
|
+
if (!this.agents.length) {
|
|
129
|
+
this.highlighted = -1;
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
const next = this.highlighted < 0
|
|
133
|
+
? (delta >= 0 ? 0 : this.agents.length - 1)
|
|
134
|
+
: (this.highlighted + delta + this.agents.length) % this.agents.length;
|
|
135
|
+
this.highlighted = next;
|
|
136
|
+
return this.agents[next] ?? null;
|
|
137
|
+
}
|
|
138
|
+
clearHighlight() {
|
|
139
|
+
this.highlighted = -1;
|
|
140
|
+
}
|
|
141
|
+
/** Bring the highlighted agent to the front in whatever is hosting it. */
|
|
142
|
+
focusCurrent() {
|
|
143
|
+
const agent = this.current;
|
|
144
|
+
if (!agent?.focus?.length)
|
|
145
|
+
return null;
|
|
146
|
+
return this.runFocus(agent.focus) ? agent : null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/** Cheap change detector, so a poll that found nothing new costs no repaint. */
|
|
150
|
+
function signature(agents) {
|
|
151
|
+
return agents.map((a) => `${a.id}:${a.status}:${a.focused ? 1 : 0}:${a.project ?? ""}`).join("|");
|
|
152
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A braille dot canvas with a text overlay, rendered to ANSI lines.
|
|
3
|
+
*
|
|
4
|
+
* Each terminal cell is a 2×4 grid of braille dots (U+2800–U+28FF), which
|
|
5
|
+
* gives the graph 8× the resolution of plain block characters and — because
|
|
6
|
+
* a cell is roughly twice as tall as it is wide — dots that are close to
|
|
7
|
+
* square, so no aspect correction is needed. Edges and node blobs are drawn
|
|
8
|
+
* in dot space; glyphs, labels and the legend are written into a cell-level
|
|
9
|
+
* overlay that takes precedence when rendering.
|
|
10
|
+
*
|
|
11
|
+
* Every rendered line is exactly `cols` cells wide and built only from
|
|
12
|
+
* width-1 characters, so the shell's `truncateLine`/`displayWidth` pipeline
|
|
13
|
+
* measures it correctly.
|
|
14
|
+
*/
|
|
15
|
+
export type ColorMode = "truecolor" | "256";
|
|
16
|
+
/** Tests pin the mode; real terminals are sniffed from COLORTERM/TERM. */
|
|
17
|
+
export declare function setColorMode(mode: ColorMode | null): void;
|
|
18
|
+
export declare function colorMode(): ColorMode;
|
|
19
|
+
export declare function hexToRgb(hex: string): [number, number, number];
|
|
20
|
+
export declare function rgbToHex(r: number, g: number, b: number): string;
|
|
21
|
+
/** Linear blend from `from` toward `to` by `t` in [0, 1]. */
|
|
22
|
+
export declare function blendHex(from: string, to: string, t: number): string;
|
|
23
|
+
/** Nearest xterm-256 index: 6×6×6 colour cube, or the grey ramp for greys. */
|
|
24
|
+
export declare function hexToAnsi256(hex: string): number;
|
|
25
|
+
/** Foreground SGR for a hex colour in the active colour mode. */
|
|
26
|
+
export declare function hexToSgr(hex: string): string;
|
|
27
|
+
export interface DotOptions {
|
|
28
|
+
/** Higher z wins when two draws hit the same cell (node dots beat edges). */
|
|
29
|
+
z?: number;
|
|
30
|
+
/** Light every other dot along a line. */
|
|
31
|
+
dotted?: boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare class BrailleCanvas {
|
|
34
|
+
readonly cols: number;
|
|
35
|
+
readonly rows: number;
|
|
36
|
+
private readonly masks;
|
|
37
|
+
private readonly zs;
|
|
38
|
+
private readonly colors;
|
|
39
|
+
private readonly overlay;
|
|
40
|
+
constructor(cols: number, rows: number);
|
|
41
|
+
get dotWidth(): number;
|
|
42
|
+
get dotHeight(): number;
|
|
43
|
+
private cell;
|
|
44
|
+
setDot(x: number, y: number, color: string, z?: number): void;
|
|
45
|
+
/** Bresenham in dot space. */
|
|
46
|
+
line(x0: number, y0: number, x1: number, y1: number, color: string, opts?: DotOptions): void;
|
|
47
|
+
/** Filled disc in dot space; radius 0 lights a single dot. */
|
|
48
|
+
disc(cx: number, cy: number, radius: number, color: string, z?: number): void;
|
|
49
|
+
/**
|
|
50
|
+
* True when the cells [col, col+width) on `row` hold no overlay text —
|
|
51
|
+
* and, with `avoidDots`, no braille dots either, so a label does not
|
|
52
|
+
* paint over somebody else's node or edge.
|
|
53
|
+
*/
|
|
54
|
+
isFree(col: number, row: number, width: number, avoidDots?: boolean): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Write styled text into the overlay. Characters wider than one cell are
|
|
57
|
+
* replaced with `·` so the row stays exactly `cols` wide. Returns the number
|
|
58
|
+
* of cells written (0 when the text starts off-canvas).
|
|
59
|
+
*/
|
|
60
|
+
putText(col: number, row: number, text: string, sgr?: string): number;
|
|
61
|
+
/** Every cell in a rectangle is overlaid with a space, hiding dots underneath (pane backdrop). */
|
|
62
|
+
clearRect(col: number, row: number, width: number, height: number): void;
|
|
63
|
+
render(): string[];
|
|
64
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A braille dot canvas with a text overlay, rendered to ANSI lines.
|
|
3
|
+
*
|
|
4
|
+
* Each terminal cell is a 2×4 grid of braille dots (U+2800–U+28FF), which
|
|
5
|
+
* gives the graph 8× the resolution of plain block characters and — because
|
|
6
|
+
* a cell is roughly twice as tall as it is wide — dots that are close to
|
|
7
|
+
* square, so no aspect correction is needed. Edges and node blobs are drawn
|
|
8
|
+
* in dot space; glyphs, labels and the legend are written into a cell-level
|
|
9
|
+
* overlay that takes precedence when rendering.
|
|
10
|
+
*
|
|
11
|
+
* Every rendered line is exactly `cols` cells wide and built only from
|
|
12
|
+
* width-1 characters, so the shell's `truncateLine`/`displayWidth` pipeline
|
|
13
|
+
* measures it correctly.
|
|
14
|
+
*/
|
|
15
|
+
import { displayWidth } from "../render.js";
|
|
16
|
+
const RESET = "\x1b[0m";
|
|
17
|
+
// Braille bit for dot (x, y) inside a cell — x ∈ {0,1}, y ∈ {0..3}.
|
|
18
|
+
const DOT_BITS = [
|
|
19
|
+
[0x01, 0x08],
|
|
20
|
+
[0x02, 0x10],
|
|
21
|
+
[0x04, 0x20],
|
|
22
|
+
[0x40, 0x80],
|
|
23
|
+
];
|
|
24
|
+
let colorModeOverride = null;
|
|
25
|
+
/** Tests pin the mode; real terminals are sniffed from COLORTERM/TERM. */
|
|
26
|
+
export function setColorMode(mode) {
|
|
27
|
+
colorModeOverride = mode;
|
|
28
|
+
}
|
|
29
|
+
export function colorMode() {
|
|
30
|
+
if (colorModeOverride)
|
|
31
|
+
return colorModeOverride;
|
|
32
|
+
const colorterm = (process.env.COLORTERM || "").toLowerCase();
|
|
33
|
+
if (colorterm === "truecolor" || colorterm === "24bit")
|
|
34
|
+
return "truecolor";
|
|
35
|
+
const term = (process.env.TERM || "").toLowerCase();
|
|
36
|
+
if (term.includes("direct") || term.includes("truecolor"))
|
|
37
|
+
return "truecolor";
|
|
38
|
+
return "256";
|
|
39
|
+
}
|
|
40
|
+
export function hexToRgb(hex) {
|
|
41
|
+
const clean = hex.replace("#", "");
|
|
42
|
+
const full = clean.length === 3 ? clean.split("").map((c) => c + c).join("") : clean.padEnd(6, "0");
|
|
43
|
+
const value = Number.parseInt(full.slice(0, 6), 16);
|
|
44
|
+
if (!Number.isFinite(value))
|
|
45
|
+
return [127, 127, 127];
|
|
46
|
+
return [(value >> 16) & 255, (value >> 8) & 255, value & 255];
|
|
47
|
+
}
|
|
48
|
+
export function rgbToHex(r, g, b) {
|
|
49
|
+
const clampByte = (v) => Math.max(0, Math.min(255, Math.round(v)));
|
|
50
|
+
return `#${[clampByte(r), clampByte(g), clampByte(b)].map((v) => v.toString(16).padStart(2, "0")).join("")}`;
|
|
51
|
+
}
|
|
52
|
+
/** Linear blend from `from` toward `to` by `t` in [0, 1]. */
|
|
53
|
+
export function blendHex(from, to, t) {
|
|
54
|
+
const a = hexToRgb(from);
|
|
55
|
+
const b = hexToRgb(to);
|
|
56
|
+
const k = Math.max(0, Math.min(1, t));
|
|
57
|
+
return rgbToHex(a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k);
|
|
58
|
+
}
|
|
59
|
+
/** Nearest xterm-256 index: 6×6×6 colour cube, or the grey ramp for greys. */
|
|
60
|
+
export function hexToAnsi256(hex) {
|
|
61
|
+
const [r, g, b] = hexToRgb(hex);
|
|
62
|
+
if (Math.abs(r - g) < 10 && Math.abs(g - b) < 10) {
|
|
63
|
+
if (r < 8)
|
|
64
|
+
return 16;
|
|
65
|
+
if (r > 248)
|
|
66
|
+
return 231;
|
|
67
|
+
return 232 + Math.round(((r - 8) / 247) * 23);
|
|
68
|
+
}
|
|
69
|
+
const level = (v) => Math.round((v / 255) * 5);
|
|
70
|
+
return 16 + 36 * level(r) + 6 * level(g) + level(b);
|
|
71
|
+
}
|
|
72
|
+
const sgrCache = new Map();
|
|
73
|
+
/** Foreground SGR for a hex colour in the active colour mode. */
|
|
74
|
+
export function hexToSgr(hex) {
|
|
75
|
+
const key = `${colorMode()}:${hex}`;
|
|
76
|
+
const cached = sgrCache.get(key);
|
|
77
|
+
if (cached)
|
|
78
|
+
return cached;
|
|
79
|
+
const [r, g, b] = hexToRgb(hex);
|
|
80
|
+
const sgr = colorMode() === "truecolor" ? `\x1b[38;2;${r};${g};${b}m` : `\x1b[38;5;${hexToAnsi256(hex)}m`;
|
|
81
|
+
sgrCache.set(key, sgr);
|
|
82
|
+
return sgr;
|
|
83
|
+
}
|
|
84
|
+
export class BrailleCanvas {
|
|
85
|
+
cols;
|
|
86
|
+
rows;
|
|
87
|
+
masks;
|
|
88
|
+
zs;
|
|
89
|
+
colors;
|
|
90
|
+
overlay;
|
|
91
|
+
constructor(cols, rows) {
|
|
92
|
+
this.cols = cols;
|
|
93
|
+
this.rows = rows;
|
|
94
|
+
const cells = Math.max(0, cols * rows);
|
|
95
|
+
this.masks = new Uint8Array(cells);
|
|
96
|
+
this.zs = new Int16Array(cells).fill(-32768);
|
|
97
|
+
this.colors = new Array(cells);
|
|
98
|
+
this.overlay = new Array(cells);
|
|
99
|
+
}
|
|
100
|
+
get dotWidth() { return this.cols * 2; }
|
|
101
|
+
get dotHeight() { return this.rows * 4; }
|
|
102
|
+
cell(col, row) {
|
|
103
|
+
return row * this.cols + col;
|
|
104
|
+
}
|
|
105
|
+
setDot(x, y, color, z = 0) {
|
|
106
|
+
const dx = Math.round(x);
|
|
107
|
+
const dy = Math.round(y);
|
|
108
|
+
if (dx < 0 || dy < 0 || dx >= this.dotWidth || dy >= this.dotHeight)
|
|
109
|
+
return;
|
|
110
|
+
const col = dx >> 1;
|
|
111
|
+
const row = dy >> 2;
|
|
112
|
+
const idx = this.cell(col, row);
|
|
113
|
+
this.masks[idx] |= DOT_BITS[dy & 3][dx & 1];
|
|
114
|
+
if (z >= this.zs[idx]) {
|
|
115
|
+
this.zs[idx] = z;
|
|
116
|
+
this.colors[idx] = color;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** Bresenham in dot space. */
|
|
120
|
+
line(x0, y0, x1, y1, color, opts = {}) {
|
|
121
|
+
let ax = Math.round(x0);
|
|
122
|
+
let ay = Math.round(y0);
|
|
123
|
+
const bx = Math.round(x1);
|
|
124
|
+
const by = Math.round(y1);
|
|
125
|
+
const ddx = Math.abs(bx - ax);
|
|
126
|
+
const ddy = -Math.abs(by - ay);
|
|
127
|
+
const sx = ax < bx ? 1 : -1;
|
|
128
|
+
const sy = ay < by ? 1 : -1;
|
|
129
|
+
let err = ddx + ddy;
|
|
130
|
+
let n = 0;
|
|
131
|
+
for (;;) {
|
|
132
|
+
if (!opts.dotted || n % 2 === 0)
|
|
133
|
+
this.setDot(ax, ay, color, opts.z ?? 0);
|
|
134
|
+
if (ax === bx && ay === by)
|
|
135
|
+
break;
|
|
136
|
+
const e2 = 2 * err;
|
|
137
|
+
if (e2 >= ddy) {
|
|
138
|
+
err += ddy;
|
|
139
|
+
ax += sx;
|
|
140
|
+
}
|
|
141
|
+
if (e2 <= ddx) {
|
|
142
|
+
err += ddx;
|
|
143
|
+
ay += sy;
|
|
144
|
+
}
|
|
145
|
+
n++;
|
|
146
|
+
if (n > 100000)
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** Filled disc in dot space; radius 0 lights a single dot. */
|
|
151
|
+
disc(cx, cy, radius, color, z = 1) {
|
|
152
|
+
const r = Math.max(0, radius);
|
|
153
|
+
const r2 = r * r + 0.25;
|
|
154
|
+
for (let y = Math.floor(cy - r); y <= Math.ceil(cy + r); y++) {
|
|
155
|
+
for (let x = Math.floor(cx - r); x <= Math.ceil(cx + r); x++) {
|
|
156
|
+
const ddx = x - cx;
|
|
157
|
+
const ddy = y - cy;
|
|
158
|
+
if (ddx * ddx + ddy * ddy <= r2)
|
|
159
|
+
this.setDot(x, y, color, z);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* True when the cells [col, col+width) on `row` hold no overlay text —
|
|
165
|
+
* and, with `avoidDots`, no braille dots either, so a label does not
|
|
166
|
+
* paint over somebody else's node or edge.
|
|
167
|
+
*/
|
|
168
|
+
isFree(col, row, width, avoidDots = false) {
|
|
169
|
+
if (row < 0 || row >= this.rows || col < 0 || col + width > this.cols)
|
|
170
|
+
return false;
|
|
171
|
+
for (let c = col; c < col + width; c++) {
|
|
172
|
+
const idx = this.cell(c, row);
|
|
173
|
+
if (this.overlay[idx])
|
|
174
|
+
return false;
|
|
175
|
+
if (avoidDots && this.masks[idx])
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Write styled text into the overlay. Characters wider than one cell are
|
|
182
|
+
* replaced with `·` so the row stays exactly `cols` wide. Returns the number
|
|
183
|
+
* of cells written (0 when the text starts off-canvas).
|
|
184
|
+
*/
|
|
185
|
+
putText(col, row, text, sgr = "") {
|
|
186
|
+
if (row < 0 || row >= this.rows)
|
|
187
|
+
return 0;
|
|
188
|
+
let c = col;
|
|
189
|
+
let written = 0;
|
|
190
|
+
for (const raw of text) {
|
|
191
|
+
if (c >= this.cols)
|
|
192
|
+
break;
|
|
193
|
+
if (c >= 0) {
|
|
194
|
+
const width = displayWidth(raw);
|
|
195
|
+
const ch = width === 1 ? raw : width === 0 ? "" : "·";
|
|
196
|
+
if (ch) {
|
|
197
|
+
this.overlay[this.cell(c, row)] = { ch, sgr };
|
|
198
|
+
written++;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
c++;
|
|
205
|
+
}
|
|
206
|
+
return written;
|
|
207
|
+
}
|
|
208
|
+
/** Every cell in a rectangle is overlaid with a space, hiding dots underneath (pane backdrop). */
|
|
209
|
+
clearRect(col, row, width, height) {
|
|
210
|
+
for (let r = row; r < row + height; r++) {
|
|
211
|
+
for (let c = col; c < col + width; c++) {
|
|
212
|
+
if (r < 0 || r >= this.rows || c < 0 || c >= this.cols)
|
|
213
|
+
continue;
|
|
214
|
+
this.overlay[this.cell(c, r)] = { ch: " ", sgr: "" };
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
render() {
|
|
219
|
+
const lines = [];
|
|
220
|
+
for (let row = 0; row < this.rows; row++) {
|
|
221
|
+
let out = "";
|
|
222
|
+
let current = "";
|
|
223
|
+
let dirty = false;
|
|
224
|
+
for (let col = 0; col < this.cols; col++) {
|
|
225
|
+
const idx = this.cell(col, row);
|
|
226
|
+
const over = this.overlay[idx];
|
|
227
|
+
let ch;
|
|
228
|
+
let sgr;
|
|
229
|
+
if (over) {
|
|
230
|
+
ch = over.ch;
|
|
231
|
+
sgr = over.sgr;
|
|
232
|
+
}
|
|
233
|
+
else if (this.masks[idx]) {
|
|
234
|
+
ch = String.fromCharCode(0x2800 + this.masks[idx]);
|
|
235
|
+
sgr = hexToSgr(this.colors[idx] ?? "#7f8db3");
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
ch = " ";
|
|
239
|
+
sgr = "";
|
|
240
|
+
}
|
|
241
|
+
if (sgr !== current) {
|
|
242
|
+
if (current || dirty)
|
|
243
|
+
out += RESET;
|
|
244
|
+
out += sgr;
|
|
245
|
+
current = sgr;
|
|
246
|
+
dirty = true;
|
|
247
|
+
}
|
|
248
|
+
out += ch;
|
|
249
|
+
}
|
|
250
|
+
if (current || dirty)
|
|
251
|
+
out += RESET;
|
|
252
|
+
lines.push(out);
|
|
253
|
+
}
|
|
254
|
+
return lines;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
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 type { GraphFilters, GraphModel, VisibleData } from "../../graph-core/model.js";
|
|
11
|
+
import type { GraphPayload, NodeDetail, NodeKind, RuntimeNode } from "../../graph-core/types.js";
|
|
12
|
+
import { type Point } from "./layout.js";
|
|
13
|
+
import { GraphWatch } from "./watch.js";
|
|
14
|
+
import { GraphAgents } from "./agents.js";
|
|
15
|
+
/** Nodes drawn at once. Terminals have far fewer pixels than a browser tab. */
|
|
16
|
+
export declare const TUI_NODE_LIMIT = 350;
|
|
17
|
+
export type GraphStatus = "idle" | "loading" | "ready" | "empty" | "error";
|
|
18
|
+
export interface FilterPreset {
|
|
19
|
+
name: string;
|
|
20
|
+
types: Partial<Record<NodeKind, boolean>>;
|
|
21
|
+
health: string;
|
|
22
|
+
}
|
|
23
|
+
export declare const FILTER_PRESETS: FilterPreset[];
|
|
24
|
+
export interface Camera {
|
|
25
|
+
/** World coordinates at the centre of the viewport. */
|
|
26
|
+
cx: number;
|
|
27
|
+
cy: number;
|
|
28
|
+
/**
|
|
29
|
+
* Braille dots per world unit, per axis. A terminal canvas is much wider
|
|
30
|
+
* than it is tall, so fitting a roughly round graph to the shorter axis
|
|
31
|
+
* would waste most of the width. The two scales are allowed to diverge up
|
|
32
|
+
* to MAX_STRETCH, which fills the screen while keeping the shape readable.
|
|
33
|
+
*/
|
|
34
|
+
scaleX: number;
|
|
35
|
+
scaleY: number;
|
|
36
|
+
}
|
|
37
|
+
export interface SearchState {
|
|
38
|
+
query: string;
|
|
39
|
+
matchIds: Set<string>;
|
|
40
|
+
results: RuntimeNode[];
|
|
41
|
+
/** Cursor into results; -1 when nothing is active. */
|
|
42
|
+
index: number;
|
|
43
|
+
}
|
|
44
|
+
/** What the controller needs from the shell when a key lands. */
|
|
45
|
+
export interface GraphKeyHost {
|
|
46
|
+
setMessage(msg: string): void;
|
|
47
|
+
startInput(ctx: string, initial: string): void;
|
|
48
|
+
}
|
|
49
|
+
/** Injected so tests can feed a fixture instead of scanning a store. */
|
|
50
|
+
export type GraphBuilder = (phrenPath: string, profile: string) => Promise<GraphPayload>;
|
|
51
|
+
export interface GraphControllerOptions {
|
|
52
|
+
builder?: GraphBuilder;
|
|
53
|
+
tokenOf?: (phrenPath: string) => string;
|
|
54
|
+
/** Animation frame period; the tests shrink it. */
|
|
55
|
+
frameMs?: number;
|
|
56
|
+
/** Injected watch, so tests can feed events without a log file or timers. */
|
|
57
|
+
watch?: GraphWatch;
|
|
58
|
+
/** Injected agents poller, so tests never shell out. */
|
|
59
|
+
agents?: GraphAgents;
|
|
60
|
+
/** Start with watch mode off (`--no-live`). */
|
|
61
|
+
watchEnabled?: boolean;
|
|
62
|
+
}
|
|
63
|
+
declare const DIRECTIONS: Record<string, Point>;
|
|
64
|
+
export declare class GraphController {
|
|
65
|
+
readonly phrenPath: string;
|
|
66
|
+
readonly profile: string;
|
|
67
|
+
status: GraphStatus;
|
|
68
|
+
errorText: string;
|
|
69
|
+
/** A rebuild is running while the previous picture stays on screen. */
|
|
70
|
+
get refreshing(): boolean;
|
|
71
|
+
payload: GraphPayload | null;
|
|
72
|
+
model: GraphModel;
|
|
73
|
+
filters: GraphFilters;
|
|
74
|
+
visible: VisibleData;
|
|
75
|
+
/** Project nodes in display order for `[`/`]`. */
|
|
76
|
+
projects: RuntimeNode[];
|
|
77
|
+
presetIndex: number;
|
|
78
|
+
selectedId: string | null;
|
|
79
|
+
search: SearchState;
|
|
80
|
+
camera: Camera;
|
|
81
|
+
private sim;
|
|
82
|
+
private lastPositions;
|
|
83
|
+
private viewport;
|
|
84
|
+
private dataToken;
|
|
85
|
+
private building;
|
|
86
|
+
private repaintHook;
|
|
87
|
+
private timer;
|
|
88
|
+
private cameraTarget;
|
|
89
|
+
/** Re-frame once the intro settle finishes so the whole map is in view. */
|
|
90
|
+
private fitOnSettle;
|
|
91
|
+
/** Set by pan/zoom; until then a resize re-fits the whole map. */
|
|
92
|
+
private userMoved;
|
|
93
|
+
private readonly builder;
|
|
94
|
+
private readonly tokenOf;
|
|
95
|
+
private readonly frameMs;
|
|
96
|
+
/** Live tail of what phren is landing on, in this or any other terminal. */
|
|
97
|
+
readonly watch: GraphWatch;
|
|
98
|
+
/** Coding agents running on this machine, joined onto their projects. */
|
|
99
|
+
readonly agents: GraphAgents;
|
|
100
|
+
watchEnabled: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Wall-clock ms of the last navigation keypress. While the user is driving,
|
|
103
|
+
* incoming events still pulse and feed but do not steal the camera.
|
|
104
|
+
*/
|
|
105
|
+
private lastUserInputAt;
|
|
106
|
+
constructor(phrenPath: string, profile: string, opts?: GraphControllerOptions);
|
|
107
|
+
/** Idempotent; called every render so the tail starts with the view. */
|
|
108
|
+
startWatch(): void;
|
|
109
|
+
stopWatch(): void;
|
|
110
|
+
/** Idempotent; the poller only runs while the Graph view is open. */
|
|
111
|
+
startAgents(): void;
|
|
112
|
+
toggleAgents(): boolean;
|
|
113
|
+
toggleWatch(): boolean;
|
|
114
|
+
/** True while the user is actively driving, so the camera is left alone. */
|
|
115
|
+
private get userDriving();
|
|
116
|
+
/**
|
|
117
|
+
* New events landed. Light every node they touch, then follow the newest
|
|
118
|
+
* one that is actually on screen — unless the user is mid-navigation.
|
|
119
|
+
*/
|
|
120
|
+
private onWatchEvents;
|
|
121
|
+
/**
|
|
122
|
+
* Let the shell trigger a repaint on its own (settle animation, fly-to,
|
|
123
|
+
* a build finishing). Without it the controller blocks the first render on
|
|
124
|
+
* the build and snaps every camera move.
|
|
125
|
+
*/
|
|
126
|
+
setRepaintHook(hook: (() => void) | null): void;
|
|
127
|
+
get positions(): Map<string, Point>;
|
|
128
|
+
private lastToken;
|
|
129
|
+
private lastTokenAt;
|
|
130
|
+
/** The store token, re-read at most every 2s: animation repaints must not stat the whole store. */
|
|
131
|
+
private currentToken;
|
|
132
|
+
/**
|
|
133
|
+
* Make sure a payload matching the store is loaded, or on its way. Blocks
|
|
134
|
+
* only on the very first build when there is no repaint hook to come back
|
|
135
|
+
* through; otherwise the old picture stays up with a refreshing badge.
|
|
136
|
+
*/
|
|
137
|
+
ensureData(): Promise<void>;
|
|
138
|
+
private build;
|
|
139
|
+
/** Load a payload directly (tests, or a host that already has one). */
|
|
140
|
+
adopt(payload: GraphPayload): void;
|
|
141
|
+
get preset(): FilterPreset;
|
|
142
|
+
get focusedProject(): string | null;
|
|
143
|
+
private applyFilters;
|
|
144
|
+
private rebuildLayout;
|
|
145
|
+
/**
|
|
146
|
+
* The view reports its canvas size (in dots) before projecting. The first
|
|
147
|
+
* fit happens before any frame is drawn, so a size change re-fits unless
|
|
148
|
+
* the user has taken the camera over.
|
|
149
|
+
*/
|
|
150
|
+
setViewport(width: number, height: number): void;
|
|
151
|
+
get viewportSize(): {
|
|
152
|
+
width: number;
|
|
153
|
+
height: number;
|
|
154
|
+
};
|
|
155
|
+
/** World → dot coordinates for the current camera. */
|
|
156
|
+
project(p: Point): Point;
|
|
157
|
+
fitAll(): void;
|
|
158
|
+
zoom(factor: number): void;
|
|
159
|
+
pan(direction: keyof typeof DIRECTIONS): void;
|
|
160
|
+
/** Centre the camera on a node, eased when animation is available. */
|
|
161
|
+
flyTo(nodeId: string): void;
|
|
162
|
+
/** Fly only when the node sits outside the middle band of the viewport. */
|
|
163
|
+
private keepInView;
|
|
164
|
+
relayout(): void;
|
|
165
|
+
private startAnimation;
|
|
166
|
+
stopAnimation(): void;
|
|
167
|
+
get animating(): boolean;
|
|
168
|
+
private animationFrame;
|
|
169
|
+
/** Called by the shell when the view changes away or the shell closes. */
|
|
170
|
+
dispose(): void;
|
|
171
|
+
detail(nodeId: string): NodeDetail | null;
|
|
172
|
+
/** Visible neighbours of a node, best first — what the pane numbers 1-9. */
|
|
173
|
+
neighborsOf(nodeId: string): RuntimeNode[];
|
|
174
|
+
select(nodeId: string | null, opts?: {
|
|
175
|
+
fly?: boolean;
|
|
176
|
+
}): void;
|
|
177
|
+
private nearestToCenter;
|
|
178
|
+
/**
|
|
179
|
+
* Arrow-key traversal: prefer a connected neighbour in that direction; if
|
|
180
|
+
* there is none, take the nearest visible node in that half-plane so the
|
|
181
|
+
* walk never dead-ends on a leaf.
|
|
182
|
+
*/
|
|
183
|
+
walk(direction: keyof typeof DIRECTIONS): RuntimeNode | null;
|
|
184
|
+
private refreshSearch;
|
|
185
|
+
/** Set the query, light up matches, and fly to the best one. */
|
|
186
|
+
applySearch(query: string): RuntimeNode | null;
|
|
187
|
+
clearSearch(): void;
|
|
188
|
+
stepSearch(delta: number): RuntimeNode | null;
|
|
189
|
+
cyclePreset(delta?: number): FilterPreset;
|
|
190
|
+
/** Focus one project (name) or all (null). */
|
|
191
|
+
focusProject(name: string | null): void;
|
|
192
|
+
cycleProject(delta: number): string | null;
|
|
193
|
+
jumpToNeighbor(n: number): RuntimeNode | null;
|
|
194
|
+
/**
|
|
195
|
+
* Returns true when the key was consumed, undefined to let the shell's
|
|
196
|
+
* generic handler have it (q, :, ?, view shortcuts, final Esc).
|
|
197
|
+
*/
|
|
198
|
+
handleKey(rawKey: string, host: GraphKeyHost): true | undefined;
|
|
199
|
+
/** One-line description for the message bar. */
|
|
200
|
+
describe(node: RuntimeNode): string;
|
|
201
|
+
}
|
|
202
|
+
export {};
|