intent-prompt-graph 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +110 -2
- package/integrations/claude/commands/ipg-log.md +10 -0
- package/integrations/claude/commands/ipg-rejected.md +10 -0
- package/integrations/claude/commands/ipg.md +10 -0
- package/integrations/omp/intent-graph.ts +65 -0
- package/integrations/omp/types.d.ts +30 -0
- package/package.json +47 -4
- package/src/cli.ts +212 -0
- package/src/config.ts +114 -0
- package/src/export.ts +71 -0
- package/src/harness/claude.ts +343 -0
- package/src/harness/harness.ts +32 -0
- package/src/harness/omp.ts +225 -0
- package/src/heuristic.ts +440 -0
- package/src/labeler.ts +496 -0
- package/src/model.ts +67 -0
- package/src/server.ts +173 -0
- package/src/session.ts +203 -0
- package/src/store.ts +122 -0
- package/src/tail.ts +89 -0
- package/web/app.ts +349 -0
- package/web/index.html +70 -0
- package/web/layout.ts +359 -0
- package/web/style.css +640 -0
- package/web/view.ts +654 -0
package/web/view.ts
ADDED
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
import type { GraphNode, NodeKind, SessionGraph, Turn } from "../src/model";
|
|
2
|
+
import {
|
|
3
|
+
COL_GAP,
|
|
4
|
+
edgePath,
|
|
5
|
+
isSpineKind,
|
|
6
|
+
layoutGraph,
|
|
7
|
+
NODE_H,
|
|
8
|
+
NODE_W,
|
|
9
|
+
pruneGraph,
|
|
10
|
+
timelineRows,
|
|
11
|
+
type GraphSlice,
|
|
12
|
+
type Point,
|
|
13
|
+
type TimelineRow,
|
|
14
|
+
} from "./layout";
|
|
15
|
+
|
|
16
|
+
export type FilterMode = "all" | "chosen" | "decisions";
|
|
17
|
+
export type ViewMode = "timeline" | "graph";
|
|
18
|
+
export type ColorGroup = "chosen" | "rejected" | "other";
|
|
19
|
+
|
|
20
|
+
export interface State {
|
|
21
|
+
connected: boolean;
|
|
22
|
+
labelerError?: string;
|
|
23
|
+
turns: Turn[];
|
|
24
|
+
graph: SessionGraph;
|
|
25
|
+
selected?: string;
|
|
26
|
+
filter: FilterMode;
|
|
27
|
+
viewMode: ViewMode;
|
|
28
|
+
follow: boolean;
|
|
29
|
+
seenNodeId?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface View {
|
|
33
|
+
z: number;
|
|
34
|
+
tx: number;
|
|
35
|
+
ty: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface Els {
|
|
39
|
+
statusDot: HTMLElement;
|
|
40
|
+
sessionTitle: HTMLElement;
|
|
41
|
+
modePill: HTMLElement;
|
|
42
|
+
sidebar: HTMLElement;
|
|
43
|
+
viewport: HTMLElement;
|
|
44
|
+
tlSummary: HTMLElement;
|
|
45
|
+
tlRows: HTMLElement;
|
|
46
|
+
world: HTMLElement;
|
|
47
|
+
edges: SVGSVGElement;
|
|
48
|
+
nodes: HTMLElement;
|
|
49
|
+
labels: HTMLElement;
|
|
50
|
+
keyed: Map<string, HTMLElement>;
|
|
51
|
+
panel: HTMLElement;
|
|
52
|
+
banner: HTMLElement;
|
|
53
|
+
toast: HTMLElement;
|
|
54
|
+
bar: HTMLElement;
|
|
55
|
+
follow: HTMLElement;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const MIN_ZOOM = 0.25;
|
|
59
|
+
const MAX_ZOOM = 2.2;
|
|
60
|
+
/** Lowest zoom at which card text is still readable; Fit never zooms out further than this. */
|
|
61
|
+
export const FIT_FLOOR = 0.6;
|
|
62
|
+
/** Free space kept around the graph, in px, and the extra bottom inset that clears the floating #bar. */
|
|
63
|
+
const PAD = 40;
|
|
64
|
+
const BAR_CLEARANCE = 48;
|
|
65
|
+
const ENTITIES: Record<string, string> = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" };
|
|
66
|
+
|
|
67
|
+
const COLOR_GROUP: Record<NodeKind, ColorGroup> = {
|
|
68
|
+
goal: "chosen",
|
|
69
|
+
chosen: "chosen",
|
|
70
|
+
rejected: "rejected",
|
|
71
|
+
option: "rejected",
|
|
72
|
+
decision: "other",
|
|
73
|
+
loop: "other",
|
|
74
|
+
side: "other",
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const painted = new WeakMap<Element, string>();
|
|
78
|
+
|
|
79
|
+
function esc(s: string): string {
|
|
80
|
+
return s.replace(/[&<>"']/g, (ch) => ENTITIES[ch] ?? ch);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Replaces an element's content only when it changed, so scroll position and hover survive repaints. */
|
|
84
|
+
function setHtml(el: Element, html: string): void {
|
|
85
|
+
if (painted.get(el) === html) return;
|
|
86
|
+
painted.set(el, html);
|
|
87
|
+
el.innerHTML = html;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function clock(at: number): string {
|
|
91
|
+
if (!at) return "";
|
|
92
|
+
return new Date(at).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Maps a node kind to one of the three colour groups of the palette. A card shows its group as a
|
|
97
|
+
* left bar; an edge takes the group of the node it leads to.
|
|
98
|
+
*
|
|
99
|
+
* Args:
|
|
100
|
+
* kind (NodeKind): Node kind.
|
|
101
|
+
*
|
|
102
|
+
* Returns:
|
|
103
|
+
* ColorGroup: "chosen" for goal/chosen, "rejected" for rejected/option, "other" for decision/loop/side.
|
|
104
|
+
*/
|
|
105
|
+
export function colorGroup(kind: NodeKind): ColorGroup {
|
|
106
|
+
return COLOR_GROUP[kind];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Determines whether a node is shown under the active filter mode.
|
|
111
|
+
*
|
|
112
|
+
* Args:
|
|
113
|
+
* node (GraphNode): Node to evaluate.
|
|
114
|
+
* filter (FilterMode): Active filter mode ("all" | "chosen" | "decisions").
|
|
115
|
+
*
|
|
116
|
+
* Returns:
|
|
117
|
+
* boolean: True when the node stays in the view.
|
|
118
|
+
*/
|
|
119
|
+
export function isNodeVisible(node: GraphNode, filter: FilterMode): boolean {
|
|
120
|
+
if (filter === "all") return true;
|
|
121
|
+
if (filter === "chosen") return isSpineKind(node.kind);
|
|
122
|
+
return node.kind === "goal" || node.kind === "decision" || node.kind === "rejected";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Applies a filter mode to a graph: filtered-out nodes and their edges are removed, and the
|
|
127
|
+
* remaining nodes are bridged across the removed ones so they stay connected.
|
|
128
|
+
*
|
|
129
|
+
* Args:
|
|
130
|
+
* graph (GraphSlice): Full graph.
|
|
131
|
+
* filter (FilterMode): Active filter mode.
|
|
132
|
+
*
|
|
133
|
+
* Returns:
|
|
134
|
+
* GraphSlice: The nodes and edges to show. The input itself when the filter is "all".
|
|
135
|
+
*/
|
|
136
|
+
export function filterGraph(graph: GraphSlice, filter: FilterMode): GraphSlice {
|
|
137
|
+
if (filter === "all") return graph;
|
|
138
|
+
return pruneGraph(graph.nodes, graph.edges, (n) => isNodeVisible(n, filter));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Returns the node Follow tracks: the latest spine node (goal, decision, chosen or loop), so a
|
|
143
|
+
* stray side question or an unchosen option never takes the selection. Falls back to the latest
|
|
144
|
+
* node of any kind when the graph has no spine node.
|
|
145
|
+
*
|
|
146
|
+
* Args:
|
|
147
|
+
* graph (SessionGraph): Current session graph.
|
|
148
|
+
*
|
|
149
|
+
* Returns:
|
|
150
|
+
* GraphNode | undefined: The follow target, or undefined when the graph is empty.
|
|
151
|
+
*/
|
|
152
|
+
export function followTarget(graph: SessionGraph): GraphNode | undefined {
|
|
153
|
+
let spine: GraphNode | undefined;
|
|
154
|
+
let any: GraphNode | undefined;
|
|
155
|
+
for (const n of graph.nodes) {
|
|
156
|
+
if (!any || n.at >= any.at) any = n;
|
|
157
|
+
if (isSpineKind(n.kind) && (!spine || n.at >= spine.at)) spine = n;
|
|
158
|
+
}
|
|
159
|
+
return spine ?? any;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Resolves the currently selected node, falling back to the follow target.
|
|
164
|
+
*
|
|
165
|
+
* Args:
|
|
166
|
+
* s (State): Current UI state.
|
|
167
|
+
*
|
|
168
|
+
* Returns:
|
|
169
|
+
* GraphNode | undefined: Selected node if the graph has any nodes.
|
|
170
|
+
*/
|
|
171
|
+
export function selectedNode(s: State): GraphNode | undefined {
|
|
172
|
+
if (s.selected) {
|
|
173
|
+
const found = s.graph.nodes.find((n) => n.id === s.selected);
|
|
174
|
+
if (found) return found;
|
|
175
|
+
}
|
|
176
|
+
return followTarget(s.graph);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Picks the node an Up/Down key press moves the selection to. The selection stops at both ends.
|
|
181
|
+
*
|
|
182
|
+
* Args:
|
|
183
|
+
* ids (readonly string[]): Node ids in display order.
|
|
184
|
+
* current (string | undefined): Selected node id; may be absent from `ids`.
|
|
185
|
+
* delta (1 | -1): 1 for Down, -1 for Up.
|
|
186
|
+
*
|
|
187
|
+
* Returns:
|
|
188
|
+
* string | undefined: Id to select, or undefined when the selection cannot move.
|
|
189
|
+
*/
|
|
190
|
+
export function stepSelection(ids: readonly string[], current: string | undefined, delta: 1 | -1): string | undefined {
|
|
191
|
+
const at = current === undefined ? -1 : ids.indexOf(current);
|
|
192
|
+
if (at < 0) return delta === 1 ? ids[0] : ids[ids.length - 1];
|
|
193
|
+
return ids[at + delta];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface GraphSummary {
|
|
197
|
+
/** First goal node. */
|
|
198
|
+
goal?: GraphNode;
|
|
199
|
+
/** Where the session stands: the latest chosen node, else the latest spine node. */
|
|
200
|
+
now?: GraphNode;
|
|
201
|
+
outcomes: GraphNode[];
|
|
202
|
+
rejected: GraphNode[];
|
|
203
|
+
decisionsCount: number;
|
|
204
|
+
loopsCount: number;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Summarizes the graph for the left sidebar and the summary at the top of the timeline.
|
|
209
|
+
*
|
|
210
|
+
* Args:
|
|
211
|
+
* graph (SessionGraph): Current session graph.
|
|
212
|
+
*
|
|
213
|
+
* Returns:
|
|
214
|
+
* GraphSummary: Goal, current position, chosen outcomes, rejected/unchosen paths, and counts.
|
|
215
|
+
*/
|
|
216
|
+
export function summarizeGraph(graph: SessionGraph): GraphSummary {
|
|
217
|
+
const outcomes = graph.nodes.filter((n) => n.kind === "chosen");
|
|
218
|
+
const rejected = graph.nodes.filter((n) => n.kind === "rejected" || n.kind === "option");
|
|
219
|
+
const decisionsCount = graph.nodes.filter((n) => n.kind === "decision" || n.kind === "rejected").length;
|
|
220
|
+
const loopsCount = graph.nodes.filter((n) => n.kind === "loop").length;
|
|
221
|
+
let now: GraphNode | undefined;
|
|
222
|
+
for (const n of outcomes) {
|
|
223
|
+
if (!now || n.at >= now.at) now = n;
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
goal: graph.nodes.find((n) => n.kind === "goal"),
|
|
227
|
+
now: now ?? followTarget(graph),
|
|
228
|
+
outcomes,
|
|
229
|
+
rejected,
|
|
230
|
+
decisionsCount,
|
|
231
|
+
loopsCount,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function turnsLabel(n: GraphNode): string {
|
|
236
|
+
const count = n.turnIds.length;
|
|
237
|
+
const time = clock(n.at);
|
|
238
|
+
if (count === 0) return time;
|
|
239
|
+
const tPart = count === 1 ? "1 turn" : `${count} turns`;
|
|
240
|
+
return time ? `${tPart} · ${time}` : tPart;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Renders the top bar (status dot, session title, LLM/Heuristic mode pill).
|
|
245
|
+
*
|
|
246
|
+
* Args:
|
|
247
|
+
* s (State): Current UI state.
|
|
248
|
+
* els (Els): Cached DOM element references.
|
|
249
|
+
*
|
|
250
|
+
* Returns:
|
|
251
|
+
* void
|
|
252
|
+
*/
|
|
253
|
+
export function paintTopbar(s: State, els: Els): void {
|
|
254
|
+
els.statusDot.className = s.connected ? "dot" : "dot offline";
|
|
255
|
+
els.sessionTitle.textContent = s.graph.title || "intent-prompt-graph";
|
|
256
|
+
els.modePill.textContent = s.graph.mode === "llm" ? "LLM graph" : "Heuristic graph";
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Renders the Left Executive Summary sidebar (Session Overview, Chosen Outcomes, Rejected Paths).
|
|
261
|
+
*
|
|
262
|
+
* Args:
|
|
263
|
+
* s (State): Current UI state.
|
|
264
|
+
* els (Els): Cached DOM element references.
|
|
265
|
+
*
|
|
266
|
+
* Returns:
|
|
267
|
+
* void
|
|
268
|
+
*/
|
|
269
|
+
export function paintSidebar(s: State, els: Els): void {
|
|
270
|
+
const sum = summarizeGraph(s.graph);
|
|
271
|
+
const selId = selectedNode(s)?.id;
|
|
272
|
+
const turnCount = s.turns.length;
|
|
273
|
+
const nodeCount = s.graph.nodes.length;
|
|
274
|
+
|
|
275
|
+
const pills = [
|
|
276
|
+
`${turnCount} turns → ${nodeCount} nodes`,
|
|
277
|
+
sum.decisionsCount > 0 ? `${sum.decisionsCount} decisions` : "",
|
|
278
|
+
sum.rejected.length > 0 ? `${sum.rejected.length} ruled out` : "",
|
|
279
|
+
sum.loopsCount > 0 ? `${sum.loopsCount} loops collapsed` : "",
|
|
280
|
+
]
|
|
281
|
+
.filter(Boolean)
|
|
282
|
+
.map((p) => `<span class="pill">${esc(p)}</span>`)
|
|
283
|
+
.join("");
|
|
284
|
+
|
|
285
|
+
const renderList = (items: GraphNode[], group: ColorGroup, empty: string): string => {
|
|
286
|
+
if (items.length === 0) return `<p class="empty-note">${esc(empty)}</p>`;
|
|
287
|
+
return `<ul class="summary-list">${items
|
|
288
|
+
.map(
|
|
289
|
+
(n) =>
|
|
290
|
+
`<li class="summary-item grp-${group}${n.id === selId ? " on" : ""}" data-jump="${esc(n.id)}">` +
|
|
291
|
+
`<div class="st">${esc(n.title)}</div>` +
|
|
292
|
+
`<div class="sd">${esc(n.desc)}</div>` +
|
|
293
|
+
`</li>`,
|
|
294
|
+
)
|
|
295
|
+
.join("")}</ul>`;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
setHtml(
|
|
299
|
+
els.sidebar,
|
|
300
|
+
`<div><div class="sec-title">Session Overview</div><h2 class="session-heading">${esc(s.graph.title || "Waiting for turns")}</h2><div class="session-meta">${pills}</div></div>` +
|
|
301
|
+
`<div><div class="sec-title">Chosen Path & Outcomes</div>${renderList(sum.outcomes, "chosen", "No final outcomes yet.")}</div>` +
|
|
302
|
+
`<div><div class="sec-title">Rejected / Unchosen Paths</div>${renderList(sum.rejected, "rejected", "No paths rejected or skipped yet.")}</div>`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Renders the layered DAG into the graph stage: nodes left by the active filter, cubic-Bezier SVG
|
|
308
|
+
* edges, and edge labels in a layer above the cards.
|
|
309
|
+
*
|
|
310
|
+
* Args:
|
|
311
|
+
* s (State): Current UI state.
|
|
312
|
+
* els (Els): Cached DOM element references.
|
|
313
|
+
*
|
|
314
|
+
* Returns:
|
|
315
|
+
* Map<string, Point>: Computed layout positions for all rendered nodes.
|
|
316
|
+
*/
|
|
317
|
+
export function paintGraph(s: State, els: Els): Map<string, Point> {
|
|
318
|
+
const shown = filterGraph(s.graph, s.filter);
|
|
319
|
+
const positions = layoutGraph(shown.nodes, shown.edges);
|
|
320
|
+
const selId = selectedNode(s)?.id;
|
|
321
|
+
const latestId = followTarget(s.graph)?.id;
|
|
322
|
+
const kindOf = new Map(shown.nodes.map((n) => [n.id, n.kind] as const));
|
|
323
|
+
|
|
324
|
+
let paths = "";
|
|
325
|
+
let labels = "";
|
|
326
|
+
for (const e of shown.edges) {
|
|
327
|
+
const a = positions.get(e.from);
|
|
328
|
+
const b = positions.get(e.to);
|
|
329
|
+
const toKind = kindOf.get(e.to);
|
|
330
|
+
if (!a || !b || !toKind || e.from === e.to) continue;
|
|
331
|
+
const geom = edgePath(a, b);
|
|
332
|
+
paths += `<path class="edge grp-${colorGroup(toKind)}" d="${geom.d}"/>`;
|
|
333
|
+
if (!e.label) continue;
|
|
334
|
+
// A label is as wide as the gap it sits in. On a level edge it rides above the line.
|
|
335
|
+
const width = geom.vertical ? NODE_W - 24 : COL_GAP - 12;
|
|
336
|
+
const place = geom.vertical ? " vertical" : a.y === b.y ? " above" : "";
|
|
337
|
+
labels += `<div class="edge-label${place}" style="left:${geom.lx}px;top:${geom.ly}px;max-width:${width}px">${esc(e.label)}</div>`;
|
|
338
|
+
}
|
|
339
|
+
setHtml(els.edges, paths);
|
|
340
|
+
setHtml(els.labels, labels);
|
|
341
|
+
|
|
342
|
+
const keep = new Set<string>();
|
|
343
|
+
for (const n of shown.nodes) {
|
|
344
|
+
const p = positions.get(n.id);
|
|
345
|
+
if (!p) continue;
|
|
346
|
+
keep.add(n.id);
|
|
347
|
+
let el = els.keyed.get(n.id);
|
|
348
|
+
if (!el) {
|
|
349
|
+
el = document.createElement("div");
|
|
350
|
+
els.keyed.set(n.id, el);
|
|
351
|
+
els.nodes.append(el);
|
|
352
|
+
}
|
|
353
|
+
const cls = [
|
|
354
|
+
"node",
|
|
355
|
+
`kind-${n.kind}`,
|
|
356
|
+
`grp-${colorGroup(n.kind)}`,
|
|
357
|
+
n.id === selId && "selected",
|
|
358
|
+
n.id === latestId && "latest",
|
|
359
|
+
]
|
|
360
|
+
.filter(Boolean)
|
|
361
|
+
.join(" ");
|
|
362
|
+
el.className = cls;
|
|
363
|
+
el.dataset.node = n.id;
|
|
364
|
+
el.style.left = `${p.x}px`;
|
|
365
|
+
el.style.top = `${p.y}px`;
|
|
366
|
+
setHtml(
|
|
367
|
+
el,
|
|
368
|
+
`<div class="node-head"><span>${esc(n.badge)}</span><span class="node-turns">${esc(turnsLabel(n))}</span></div>` +
|
|
369
|
+
`<div class="node-title">${esc(n.title)}</div>` +
|
|
370
|
+
`<div class="node-desc">${esc(n.desc)}</div>`,
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
for (const [id, el] of els.keyed) {
|
|
375
|
+
if (keep.has(id)) continue;
|
|
376
|
+
el.remove();
|
|
377
|
+
els.keyed.delete(id);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return positions;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function summaryLine(key: string, value: string, jump: GraphNode | undefined): string {
|
|
384
|
+
const target = jump ? ` data-jump="${esc(jump.id)}"` : "";
|
|
385
|
+
return (
|
|
386
|
+
`<div class="tl-sum${jump ? "" : " none"}"${target}>` +
|
|
387
|
+
`<span class="k">${key}</span><span class="v">${esc(value)}</span></div>`
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function rowHtml(row: TimelineRow, selId: string | undefined, latestId: string | undefined): string {
|
|
392
|
+
const n = row.node;
|
|
393
|
+
const cls = [
|
|
394
|
+
"tl-row",
|
|
395
|
+
`kind-${n.kind}`,
|
|
396
|
+
`grp-${colorGroup(n.kind)}`,
|
|
397
|
+
n.id === selId && "selected",
|
|
398
|
+
n.id === latestId && "latest",
|
|
399
|
+
]
|
|
400
|
+
.filter(Boolean)
|
|
401
|
+
.join(" ");
|
|
402
|
+
const via = row.edge?.label ? `<div class="tl-via">${esc(row.edge.label)}</div>` : "";
|
|
403
|
+
const rounds =
|
|
404
|
+
n.kind === "loop" && n.rounds !== undefined
|
|
405
|
+
? `<div class="tl-rounds"><b>${n.rounds}</b><span>rounds</span></div>`
|
|
406
|
+
: "";
|
|
407
|
+
const latest = n.id === latestId ? `<span class="tl-latest">latest</span>` : "";
|
|
408
|
+
const desc = n.desc ? `<div class="tl-desc">${esc(n.desc)}</div>` : "";
|
|
409
|
+
const why =
|
|
410
|
+
n.kind === "rejected" && n.rationale
|
|
411
|
+
? `<div class="tl-why"><span class="lbl">${esc(n.rationaleLabel || "Why rejected")}</span>${esc(n.rationale)}</div>`
|
|
412
|
+
: "";
|
|
413
|
+
return (
|
|
414
|
+
`<div class="tl-step ${row.depth === 0 ? "spine" : "branch"}">${via}` +
|
|
415
|
+
`<div class="${cls}" data-node="${esc(n.id)}">${rounds}<div class="tl-main">` +
|
|
416
|
+
`<div class="tl-head"><span class="tl-badge">${esc(n.badge)}</span>${latest}<span class="tl-meta">${esc(turnsLabel(n))}</span></div>` +
|
|
417
|
+
`<div class="tl-title">${esc(n.title)}</div>${desc}${why}` +
|
|
418
|
+
`</div></div></div>`
|
|
419
|
+
);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Renders the timeline view: a three-line summary (Goal, Now, Rejected) and one row per node left
|
|
424
|
+
* by the active filter, spine rows full width and branch rows indented under them.
|
|
425
|
+
*
|
|
426
|
+
* Args:
|
|
427
|
+
* s (State): Current UI state.
|
|
428
|
+
* els (Els): Cached DOM element references.
|
|
429
|
+
*
|
|
430
|
+
* Returns:
|
|
431
|
+
* void
|
|
432
|
+
*/
|
|
433
|
+
export function paintTimeline(s: State, els: Els): void {
|
|
434
|
+
const sum = summarizeGraph(s.graph);
|
|
435
|
+
const ruledOut = sum.rejected.length;
|
|
436
|
+
setHtml(
|
|
437
|
+
els.tlSummary,
|
|
438
|
+
summaryLine("Goal", sum.goal?.title ?? "No goal identified yet", sum.goal) +
|
|
439
|
+
summaryLine("Now", sum.now?.title ?? "Waiting for turns in this session…", sum.now) +
|
|
440
|
+
summaryLine(
|
|
441
|
+
"Rejected",
|
|
442
|
+
ruledOut === 0 ? "Nothing ruled out yet" : `${ruledOut} ${ruledOut === 1 ? "path" : "paths"} ruled out · show the first`,
|
|
443
|
+
sum.rejected[0],
|
|
444
|
+
),
|
|
445
|
+
);
|
|
446
|
+
|
|
447
|
+
const shown = filterGraph(s.graph, s.filter);
|
|
448
|
+
const rows = timelineRows(shown.nodes, shown.edges);
|
|
449
|
+
const selId = selectedNode(s)?.id;
|
|
450
|
+
const latestId = followTarget(s.graph)?.id;
|
|
451
|
+
const empty = s.graph.nodes.length === 0 ? "Waiting for turns in this session…" : "No node matches this filter.";
|
|
452
|
+
setHtml(
|
|
453
|
+
els.tlRows,
|
|
454
|
+
rows.length === 0
|
|
455
|
+
? `<p class="empty-note">${empty}</p>`
|
|
456
|
+
: `<div class="tl-list">${rows.map((r) => rowHtml(r, selId, latestId)).join("")}</div>`,
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Computes a signature for the right inspector panel so it is only re-rendered when its content changes.
|
|
462
|
+
*
|
|
463
|
+
* Args:
|
|
464
|
+
* s (State): Current UI state.
|
|
465
|
+
*
|
|
466
|
+
* Returns:
|
|
467
|
+
* string: Stable cache key for the currently inspected node and its source turns.
|
|
468
|
+
*/
|
|
469
|
+
export function inspectorSignature(s: State): string {
|
|
470
|
+
const n = selectedNode(s);
|
|
471
|
+
if (!n) return "empty";
|
|
472
|
+
const byTurn = new Map(s.turns.map((t) => [t.id, t] as const));
|
|
473
|
+
const turnSig = n.turnIds.map((id) => {
|
|
474
|
+
const t = byTurn.get(id);
|
|
475
|
+
return t ? `${t.id}:${t.text.length}:${t.actions.length}:${t.open ? 1 : 0}` : id;
|
|
476
|
+
});
|
|
477
|
+
return `${n.id}|${n.kind}|${n.title}|${n.rationale}|${turnSig.join(",")}`;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Renders the Right Inspector panel with rationale and full source transcript excerpts.
|
|
482
|
+
*
|
|
483
|
+
* Args:
|
|
484
|
+
* s (State): Current UI state.
|
|
485
|
+
* els (Els): Cached DOM element references.
|
|
486
|
+
*
|
|
487
|
+
* Returns:
|
|
488
|
+
* void
|
|
489
|
+
*/
|
|
490
|
+
export function paintInspector(s: State, els: Els): void {
|
|
491
|
+
const n = selectedNode(s);
|
|
492
|
+
if (!n) {
|
|
493
|
+
els.panel.innerHTML = `<p class="empty-note">Waiting for turns in this session…</p>`;
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
const byTurn = new Map(s.turns.map((t) => [t.id, t] as const));
|
|
497
|
+
const turns = n.turnIds.map((id) => byTurn.get(id)).filter((t): t is Turn => t !== undefined);
|
|
498
|
+
|
|
499
|
+
const turnsHtml =
|
|
500
|
+
turns.length === 0
|
|
501
|
+
? `<p class="empty-note">No linked transcript turns.</p>`
|
|
502
|
+
: `<div class="turn-list">${turns
|
|
503
|
+
.map((t) => {
|
|
504
|
+
const whoLabel = t.author === "owner" ? (t.guest ? "GUEST" : "YOU") : "AGENT";
|
|
505
|
+
const actionsHtml =
|
|
506
|
+
t.actions.length > 0
|
|
507
|
+
? `<ul class="turn-actions">${t.actions
|
|
508
|
+
.slice(0, 8)
|
|
509
|
+
.map((a) => `<li><b>${esc(a.tool)}</b> ${esc(a.summary)}</li>`)
|
|
510
|
+
.join("")}</ul>`
|
|
511
|
+
: "";
|
|
512
|
+
return (
|
|
513
|
+
`<div class="turn-card">` +
|
|
514
|
+
`<div class="turn-who"><span>${whoLabel}</span><span>${esc(clock(t.at))}</span></div>` +
|
|
515
|
+
`<div class="turn-body">${esc(t.text || "(tool calls only)")}</div>` +
|
|
516
|
+
actionsHtml +
|
|
517
|
+
`</div>`
|
|
518
|
+
);
|
|
519
|
+
})
|
|
520
|
+
.join("")}</div>`;
|
|
521
|
+
|
|
522
|
+
els.panel.innerHTML =
|
|
523
|
+
`<div class="panel-head grp-${colorGroup(n.kind)}">` +
|
|
524
|
+
`<span class="panel-badge">${esc(n.badge)} · ${esc(turnsLabel(n))}</span>` +
|
|
525
|
+
`<h2 class="panel-title">${esc(n.title)}</h2>` +
|
|
526
|
+
`<div class="rationale-box"><div class="lbl">${esc(n.rationaleLabel)}</div><p>${esc(n.rationale)}</p></div>` +
|
|
527
|
+
`</div>` +
|
|
528
|
+
`<div><div class="sec-title">Source Transcript (${turns.length})</div>${turnsHtml}</div>`;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* Updates the chrome around the views: which view is shown, the pressed state of the view and
|
|
533
|
+
* filter buttons, the error/disconnect banner, the new-activity toast, and the Follow button.
|
|
534
|
+
*
|
|
535
|
+
* Args:
|
|
536
|
+
* s (State): Current UI state.
|
|
537
|
+
* els (Els): Cached DOM element references.
|
|
538
|
+
*
|
|
539
|
+
* Returns:
|
|
540
|
+
* void
|
|
541
|
+
*/
|
|
542
|
+
export function paintChrome(s: State, els: Els): void {
|
|
543
|
+
els.viewport.dataset.view = s.viewMode;
|
|
544
|
+
for (const b of els.bar.querySelectorAll<HTMLElement>("[data-set-view]")) {
|
|
545
|
+
b.classList.toggle("active", b.dataset.setView === s.viewMode);
|
|
546
|
+
}
|
|
547
|
+
for (const b of els.bar.querySelectorAll<HTMLElement>("[data-filter]")) {
|
|
548
|
+
b.classList.toggle("active", b.dataset.filter === s.filter);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
els.banner.hidden = s.connected && !s.labelerError;
|
|
552
|
+
els.banner.textContent = s.connected ? `Labeler error: ${s.labelerError ?? ""}` : "Connection lost. Reconnecting…";
|
|
553
|
+
|
|
554
|
+
const latest = followTarget(s.graph);
|
|
555
|
+
const unseen = !s.follow && latest !== undefined && latest.id !== s.seenNodeId;
|
|
556
|
+
els.toast.hidden = !unseen;
|
|
557
|
+
if (latest && unseen) {
|
|
558
|
+
els.toast.textContent = `New update: ${latest.title} — jump`;
|
|
559
|
+
els.toast.dataset.jump = latest.id;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
els.follow.textContent = `Follow: ${s.follow ? "on" : "off"}`;
|
|
563
|
+
els.follow.setAttribute("aria-pressed", String(s.follow));
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export function clampZoom(z: number): number {
|
|
567
|
+
return Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, z));
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
export function applyView(world: HTMLElement, v: View): void {
|
|
571
|
+
world.style.transform = `translate(${v.tx}px, ${v.ty}px) scale(${v.z})`;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Translation along one axis: content that fits between the insets is centred there; content that
|
|
576
|
+
* does not is moved so the focus point is centred, without leaving empty space past either end.
|
|
577
|
+
*/
|
|
578
|
+
function axisOffset(lo: number, hi: number, focus: number, size: number, z: number, padLo: number, padHi: number): number {
|
|
579
|
+
const room = size - padLo - padHi;
|
|
580
|
+
if ((hi - lo) * z <= room) return padLo + (room - (hi - lo) * z) / 2 - lo * z;
|
|
581
|
+
const centred = padLo + room / 2 - focus * z;
|
|
582
|
+
return Math.min(padLo - lo * z, Math.max(size - padHi - hi * z, centred));
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Computes zoom and translation for the Fit and 100% buttons, from the viewport's client size and
|
|
587
|
+
* the real bounds of the cards. Fit shows the whole graph with padding, never enlarged past 100%.
|
|
588
|
+
* When that would need a zoom below FIT_FLOOR, the zoom stays at FIT_FLOOR. On each axis the graph
|
|
589
|
+
* is centred when it fits; otherwise the focus node is centred, stopping at the graph's edges.
|
|
590
|
+
*
|
|
591
|
+
* Args:
|
|
592
|
+
* positions (ReadonlyMap<string, Point>): Top-left coordinates of all rendered nodes.
|
|
593
|
+
* vw (number): Viewport client width in px.
|
|
594
|
+
* vh (number): Viewport client height in px.
|
|
595
|
+
* focusId (string | undefined): Selected node, kept in view when the graph does not fit whole.
|
|
596
|
+
* The graph's top-left corner is used when it is absent or not rendered.
|
|
597
|
+
* zoom (number | undefined): Fixed zoom to use instead of the fitted one.
|
|
598
|
+
*
|
|
599
|
+
* Returns:
|
|
600
|
+
* View: Target zoom and translation coordinates.
|
|
601
|
+
*/
|
|
602
|
+
export function fitView(
|
|
603
|
+
positions: ReadonlyMap<string, Point>,
|
|
604
|
+
vw: number,
|
|
605
|
+
vh: number,
|
|
606
|
+
focusId?: string,
|
|
607
|
+
zoom?: number,
|
|
608
|
+
): View {
|
|
609
|
+
if (positions.size === 0) return { z: zoom ?? 1, tx: PAD, ty: PAD };
|
|
610
|
+
let x0 = Infinity;
|
|
611
|
+
let y0 = Infinity;
|
|
612
|
+
let x1 = -Infinity;
|
|
613
|
+
let y1 = -Infinity;
|
|
614
|
+
for (const p of positions.values()) {
|
|
615
|
+
x0 = Math.min(x0, p.x);
|
|
616
|
+
y0 = Math.min(y0, p.y);
|
|
617
|
+
x1 = Math.max(x1, p.x + NODE_W);
|
|
618
|
+
y1 = Math.max(y1, p.y + NODE_H);
|
|
619
|
+
}
|
|
620
|
+
const padBottom = PAD + BAR_CLEARANCE;
|
|
621
|
+
const whole = Math.min(1, (vw - 2 * PAD) / (x1 - x0), (vh - PAD - padBottom) / (y1 - y0));
|
|
622
|
+
const z = zoom ?? Math.max(FIT_FLOOR, whole);
|
|
623
|
+
const focus = (focusId !== undefined && positions.get(focusId)) || { x: x0, y: y0 };
|
|
624
|
+
return {
|
|
625
|
+
z,
|
|
626
|
+
tx: axisOffset(x0, x1, focus.x + NODE_W / 2, vw, z, PAD, PAD),
|
|
627
|
+
ty: axisOffset(y0, y1, focus.y + NODE_H / 2, vh, z, PAD, padBottom),
|
|
628
|
+
};
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/**
|
|
632
|
+
* Pans the view so a node is visible, keeping the zoom. A node already fully inside the viewport
|
|
633
|
+
* leaves the view untouched.
|
|
634
|
+
*
|
|
635
|
+
* Args:
|
|
636
|
+
* v (View): Current view.
|
|
637
|
+
* p (Point): Top-left position of the node to reveal.
|
|
638
|
+
* vw (number): Viewport client width in px.
|
|
639
|
+
* vh (number): Viewport client height in px.
|
|
640
|
+
*
|
|
641
|
+
* Returns:
|
|
642
|
+
* View: The same object when nothing has to move, else a view centred on the node.
|
|
643
|
+
*/
|
|
644
|
+
export function revealView(v: View, p: Point, vw: number, vh: number): View {
|
|
645
|
+
const left = v.tx + p.x * v.z;
|
|
646
|
+
const top = v.ty + p.y * v.z;
|
|
647
|
+
const inside = left >= 0 && top >= 0 && left + NODE_W * v.z <= vw && top + NODE_H * v.z <= vh - BAR_CLEARANCE;
|
|
648
|
+
if (inside) return v;
|
|
649
|
+
return {
|
|
650
|
+
z: v.z,
|
|
651
|
+
tx: vw / 2 - (p.x + NODE_W / 2) * v.z,
|
|
652
|
+
ty: (vh - BAR_CLEARANCE) / 2 - (p.y + NODE_H / 2) * v.z,
|
|
653
|
+
};
|
|
654
|
+
}
|