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/layout.ts
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import type { GraphEdge, GraphNode, NodeKind } from "../src/model";
|
|
2
|
+
|
|
3
|
+
export interface Point {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface EdgeGeometry {
|
|
9
|
+
d: string;
|
|
10
|
+
/** Centre of the label: always inside a gap between cards, next to the target node. */
|
|
11
|
+
lx: number;
|
|
12
|
+
ly: number;
|
|
13
|
+
/** True for a same-column edge, whose label sits in the row gap instead of the column gap. */
|
|
14
|
+
vertical: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A node list and the edges between those nodes. */
|
|
18
|
+
export interface GraphSlice {
|
|
19
|
+
nodes: readonly GraphNode[];
|
|
20
|
+
edges: readonly GraphEdge[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface TimelineRow {
|
|
24
|
+
node: GraphNode;
|
|
25
|
+
/** 0 for a spine row, 1 for a branch row indented under a spine row. */
|
|
26
|
+
depth: 0 | 1;
|
|
27
|
+
/** Spine node a branch row hangs under; unset for spine rows and for branch rows with no spine ancestor. */
|
|
28
|
+
parentId?: string;
|
|
29
|
+
/** Edge that leads into this node; its label is shown on the row. */
|
|
30
|
+
edge?: GraphEdge;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Fixed card footprint used by the layered DAG layout engine. */
|
|
34
|
+
export const NODE_W = 248;
|
|
35
|
+
export const NODE_H = 128;
|
|
36
|
+
export const COL_GAP = 120;
|
|
37
|
+
export const ROW_GAP = 42;
|
|
38
|
+
|
|
39
|
+
const SPINE_KIND: Record<NodeKind, boolean> = {
|
|
40
|
+
goal: true,
|
|
41
|
+
decision: true,
|
|
42
|
+
chosen: true,
|
|
43
|
+
loop: true,
|
|
44
|
+
rejected: false,
|
|
45
|
+
option: false,
|
|
46
|
+
side: false,
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Tells whether a node kind belongs to the main spine of the session (as opposed to a branch off it).
|
|
51
|
+
*
|
|
52
|
+
* Args:
|
|
53
|
+
* kind (NodeKind): Node kind to test.
|
|
54
|
+
*
|
|
55
|
+
* Returns:
|
|
56
|
+
* boolean: True for goal, decision, chosen and loop; false for rejected, option and side.
|
|
57
|
+
*/
|
|
58
|
+
export function isSpineKind(kind: NodeKind): boolean {
|
|
59
|
+
return SPINE_KIND[kind];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const KIND_PRIORITY: Record<NodeKind, number> = {
|
|
63
|
+
goal: 0,
|
|
64
|
+
chosen: 1,
|
|
65
|
+
decision: 2,
|
|
66
|
+
loop: 3,
|
|
67
|
+
option: 4,
|
|
68
|
+
rejected: 5,
|
|
69
|
+
side: 6,
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Computes collision-free top-left (x, y) coordinates for every node in a Decision & Path Graph.
|
|
74
|
+
* Places the main decision spine left-to-right across topological columns while stacking
|
|
75
|
+
* rejected, unchosen option, and side-question branches cleanly below their parent column.
|
|
76
|
+
*
|
|
77
|
+
* Args:
|
|
78
|
+
* nodes (readonly GraphNode[]): Graph nodes to place.
|
|
79
|
+
* edges (readonly GraphEdge[]): Directed edges between nodes.
|
|
80
|
+
*
|
|
81
|
+
* Returns:
|
|
82
|
+
* Map<string, Point>: Map of node id to top-left canvas position in px.
|
|
83
|
+
*/
|
|
84
|
+
export function layoutGraph(nodes: readonly GraphNode[], edges: readonly GraphEdge[]): Map<string, Point> {
|
|
85
|
+
const pos = new Map<string, Point>();
|
|
86
|
+
if (nodes.length === 0) return pos;
|
|
87
|
+
|
|
88
|
+
const byId = new Map(nodes.map((n) => [n.id, n] as const));
|
|
89
|
+
const outgoing = new Map<string, GraphEdge[]>();
|
|
90
|
+
const inDegree = new Map<string, number>();
|
|
91
|
+
for (const n of nodes) {
|
|
92
|
+
outgoing.set(n.id, []);
|
|
93
|
+
inDegree.set(n.id, 0);
|
|
94
|
+
}
|
|
95
|
+
for (const e of edges) {
|
|
96
|
+
if (!byId.has(e.from) || !byId.has(e.to) || e.from === e.to) continue;
|
|
97
|
+
outgoing.get(e.from)?.push(e);
|
|
98
|
+
if (e.kind !== "side") {
|
|
99
|
+
inDegree.set(e.to, (inDegree.get(e.to) ?? 0) + 1);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Assign topological column (layer) via bounded relaxation so cycles cannot loop forever.
|
|
104
|
+
const layer = new Map<string, number>();
|
|
105
|
+
for (const n of nodes) layer.set(n.id, 0);
|
|
106
|
+
|
|
107
|
+
const maxPasses = Math.min(nodes.length, 64);
|
|
108
|
+
for (let pass = 0; pass < maxPasses; pass++) {
|
|
109
|
+
let changed = false;
|
|
110
|
+
for (const e of edges) {
|
|
111
|
+
if (!byId.has(e.from) || !byId.has(e.to) || e.from === e.to) continue;
|
|
112
|
+
const fromL = layer.get(e.from) ?? 0;
|
|
113
|
+
const toL = layer.get(e.to) ?? 0;
|
|
114
|
+
if (e.kind === "side") {
|
|
115
|
+
if (toL < fromL) {
|
|
116
|
+
layer.set(e.to, fromL);
|
|
117
|
+
changed = true;
|
|
118
|
+
}
|
|
119
|
+
} else if (toL < fromL + 1) {
|
|
120
|
+
layer.set(e.to, fromL + 1);
|
|
121
|
+
changed = true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (!changed) break;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Group nodes into columns and sort each column so the main spine stays on row 0.
|
|
128
|
+
const columns = new Map<number, GraphNode[]>();
|
|
129
|
+
for (const n of nodes) {
|
|
130
|
+
const col = layer.get(n.id) ?? 0;
|
|
131
|
+
const list = columns.get(col) ?? [];
|
|
132
|
+
list.push(n);
|
|
133
|
+
columns.set(col, list);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const orderIndex = new Map(nodes.map((n, i) => [n.id, i] as const));
|
|
137
|
+
for (const [col, list] of columns) {
|
|
138
|
+
list.sort((a, b) => {
|
|
139
|
+
const pDiff = KIND_PRIORITY[a.kind] - KIND_PRIORITY[b.kind];
|
|
140
|
+
if (pDiff !== 0) return pDiff;
|
|
141
|
+
return (orderIndex.get(a.id) ?? 0) - (orderIndex.get(b.id) ?? 0);
|
|
142
|
+
});
|
|
143
|
+
const x = col * (NODE_W + COL_GAP);
|
|
144
|
+
list.forEach((n, row) => {
|
|
145
|
+
pos.set(n.id, { x, y: row * (NODE_H + ROW_GAP) });
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return pos;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function cubic(a: number, b: number, c: number, d: number, t: number): number {
|
|
153
|
+
const u = 1 - t;
|
|
154
|
+
return u * u * u * a + 3 * u * u * t * b + 3 * u * t * t * c + t * t * t * d;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Computes a smooth cubic-Bezier SVG path between two node positions and where its label goes.
|
|
159
|
+
* The label is placed in the gap just before the target node, so no card can cover it: the column
|
|
160
|
+
* gap for edges between columns, the row gap for edges inside one column.
|
|
161
|
+
*
|
|
162
|
+
* Args:
|
|
163
|
+
* from (Point): Top-left position of the source node.
|
|
164
|
+
* to (Point): Top-left position of the target node.
|
|
165
|
+
*
|
|
166
|
+
* Returns:
|
|
167
|
+
* EdgeGeometry: SVG path `d` string, label centre `(lx, ly)` and whether the edge is vertical.
|
|
168
|
+
*/
|
|
169
|
+
export function edgePath(from: Point, to: Point): EdgeGeometry {
|
|
170
|
+
if (Math.abs(from.x - to.x) < 40) {
|
|
171
|
+
const down = from.y <= to.y;
|
|
172
|
+
const sy = down ? from.y + NODE_H : from.y;
|
|
173
|
+
const ey = down ? to.y : to.y + NODE_H;
|
|
174
|
+
const cx = from.x + NODE_W / 2;
|
|
175
|
+
const midY = (sy + ey) / 2;
|
|
176
|
+
return {
|
|
177
|
+
d: `M ${cx} ${sy} C ${cx} ${midY}, ${cx} ${midY}, ${cx} ${ey}`,
|
|
178
|
+
lx: cx,
|
|
179
|
+
ly: down ? ey - ROW_GAP / 2 : ey + ROW_GAP / 2,
|
|
180
|
+
vertical: true,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const forward = to.x >= from.x;
|
|
185
|
+
const x1 = forward ? from.x + NODE_W : from.x;
|
|
186
|
+
const y1 = from.y + NODE_H / 2;
|
|
187
|
+
const x2 = forward ? to.x : to.x + NODE_W;
|
|
188
|
+
const y2 = to.y + NODE_H / 2;
|
|
189
|
+
const dx = Math.max(48, Math.abs(x2 - x1) * 0.45);
|
|
190
|
+
const c1x = forward ? x1 + dx : x1 - dx;
|
|
191
|
+
const c2x = forward ? x2 - dx : x2 + dx;
|
|
192
|
+
|
|
193
|
+
// Adjacent columns: the curve's midpoint is the gap's centre. Longer edges cross other columns,
|
|
194
|
+
// so find the curve point that lies in the middle of the last gap instead.
|
|
195
|
+
let t = 0.5;
|
|
196
|
+
if (Math.abs(x2 - x1) > COL_GAP) {
|
|
197
|
+
const target = forward ? x2 - COL_GAP / 2 : x2 + COL_GAP / 2;
|
|
198
|
+
let lo = 0;
|
|
199
|
+
let hi = 1;
|
|
200
|
+
for (let i = 0; i < 20; i++) {
|
|
201
|
+
t = (lo + hi) / 2;
|
|
202
|
+
if (cubic(x1, c1x, c2x, x2, t) < target === forward) lo = t;
|
|
203
|
+
else hi = t;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
d: `M ${x1} ${y1} C ${c1x} ${y1}, ${c2x} ${y2}, ${x2} ${y2}`,
|
|
208
|
+
lx: cubic(x1, c1x, c2x, x2, t),
|
|
209
|
+
ly: cubic(y1, y1, y2, y2, t),
|
|
210
|
+
vertical: false,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Removes nodes from a graph and keeps the rest connected. Edges between surviving nodes are kept
|
|
216
|
+
* as they are. Where a path ran through removed nodes, one bridging edge joins the surviving node
|
|
217
|
+
* before it to the surviving node after it, carrying the kind and label of the path's last edge.
|
|
218
|
+
*
|
|
219
|
+
* Args:
|
|
220
|
+
* nodes (readonly GraphNode[]): All graph nodes.
|
|
221
|
+
* edges (readonly GraphEdge[]): All graph edges.
|
|
222
|
+
* keep ((node: GraphNode) => boolean): Returns true for nodes that survive.
|
|
223
|
+
*
|
|
224
|
+
* Returns:
|
|
225
|
+
* GraphSlice: Surviving nodes in their original order, and the kept plus bridging edges.
|
|
226
|
+
*/
|
|
227
|
+
export function pruneGraph(
|
|
228
|
+
nodes: readonly GraphNode[],
|
|
229
|
+
edges: readonly GraphEdge[],
|
|
230
|
+
keep: (node: GraphNode) => boolean,
|
|
231
|
+
): GraphSlice {
|
|
232
|
+
const kept = nodes.filter(keep);
|
|
233
|
+
const keptIds = new Set(kept.map((n) => n.id));
|
|
234
|
+
const outgoing = new Map<string, GraphEdge[]>(nodes.map((n) => [n.id, []]));
|
|
235
|
+
const linked = new Map<string, Set<string>>(kept.map((n) => [n.id, new Set()]));
|
|
236
|
+
const out: GraphEdge[] = [];
|
|
237
|
+
|
|
238
|
+
for (const e of edges) {
|
|
239
|
+
const list = outgoing.get(e.from);
|
|
240
|
+
if (!list || !outgoing.has(e.to) || e.from === e.to) continue;
|
|
241
|
+
list.push(e);
|
|
242
|
+
if (keptIds.has(e.from) && keptIds.has(e.to)) {
|
|
243
|
+
out.push(e);
|
|
244
|
+
linked.get(e.from)?.add(e.to);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (kept.length === nodes.length) return { nodes: kept, edges: out };
|
|
248
|
+
|
|
249
|
+
for (const src of kept) {
|
|
250
|
+
const targets = linked.get(src.id) ?? new Set<string>();
|
|
251
|
+
const seen = new Set<string>();
|
|
252
|
+
const removed = (outgoing.get(src.id) ?? []).filter((e) => !keptIds.has(e.to)).map((e) => e.to);
|
|
253
|
+
for (let via = removed.pop(); via !== undefined; via = removed.pop()) {
|
|
254
|
+
if (seen.has(via)) continue;
|
|
255
|
+
seen.add(via);
|
|
256
|
+
for (const e of outgoing.get(via) ?? []) {
|
|
257
|
+
if (!keptIds.has(e.to)) {
|
|
258
|
+
removed.push(e.to);
|
|
259
|
+
} else if (e.to !== src.id && !targets.has(e.to)) {
|
|
260
|
+
targets.add(e.to);
|
|
261
|
+
out.push({ ...e, id: `${src.id}~${e.to}`, from: src.id });
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return { nodes: kept, edges: out };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** Topological order of `subset`; ties and cycles are broken by the order of `subset` itself. */
|
|
270
|
+
function runOrder(subset: readonly GraphNode[], outgoing: ReadonlyMap<string, GraphEdge[]>): GraphNode[] {
|
|
271
|
+
const waiting = new Map<string, number>(subset.map((n) => [n.id, 0]));
|
|
272
|
+
for (const n of subset) {
|
|
273
|
+
for (const e of outgoing.get(n.id) ?? []) {
|
|
274
|
+
const count = waiting.get(e.to);
|
|
275
|
+
if (count !== undefined) waiting.set(e.to, count + 1);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const left = [...subset];
|
|
279
|
+
const order: GraphNode[] = [];
|
|
280
|
+
while (left.length > 0) {
|
|
281
|
+
const ready = left.findIndex((n) => waiting.get(n.id) === 0);
|
|
282
|
+
const [next] = left.splice(Math.max(0, ready), 1);
|
|
283
|
+
if (!next) break;
|
|
284
|
+
order.push(next);
|
|
285
|
+
waiting.delete(next.id);
|
|
286
|
+
for (const e of outgoing.get(next.id) ?? []) {
|
|
287
|
+
const count = waiting.get(e.to);
|
|
288
|
+
if (count !== undefined) waiting.set(e.to, count - 1);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
return order;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Orders a graph as a top-to-bottom timeline. Spine nodes (goal, decision, chosen, loop) come in the
|
|
296
|
+
* order the edges run, starting at the goal; where the edges leave the order open, the order of
|
|
297
|
+
* `nodes` decides. Each rejected, option or side node becomes a branch row directly under the
|
|
298
|
+
* nearest spine node it descends from. Nodes that cannot be reached from a goal are appended after
|
|
299
|
+
* the reachable ones in the same way, and branch nodes with no spine ancestor come last.
|
|
300
|
+
*
|
|
301
|
+
* Args:
|
|
302
|
+
* nodes (readonly GraphNode[]): Graph nodes; every one appears in the result exactly once.
|
|
303
|
+
* edges (readonly GraphEdge[]): Directed edges between nodes.
|
|
304
|
+
*
|
|
305
|
+
* Returns:
|
|
306
|
+
* TimelineRow[]: Rows in display order.
|
|
307
|
+
*/
|
|
308
|
+
export function timelineRows(nodes: readonly GraphNode[], edges: readonly GraphEdge[]): TimelineRow[] {
|
|
309
|
+
const byId = new Map(nodes.map((n) => [n.id, n] as const));
|
|
310
|
+
const outgoing = new Map<string, GraphEdge[]>(nodes.map((n) => [n.id, []]));
|
|
311
|
+
const incoming = new Map<string, GraphEdge>();
|
|
312
|
+
for (const e of edges) {
|
|
313
|
+
const list = outgoing.get(e.from);
|
|
314
|
+
if (!list || !byId.has(e.to) || e.from === e.to) continue;
|
|
315
|
+
list.push(e);
|
|
316
|
+
if (!incoming.has(e.to)) incoming.set(e.to, e);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const reachable = new Set<string>();
|
|
320
|
+
const frontier = nodes.filter((n) => n.kind === "goal").map((n) => n.id);
|
|
321
|
+
for (let id = frontier.pop(); id !== undefined; id = frontier.pop()) {
|
|
322
|
+
if (reachable.has(id)) continue;
|
|
323
|
+
reachable.add(id);
|
|
324
|
+
for (const e of outgoing.get(id) ?? []) frontier.push(e.to);
|
|
325
|
+
}
|
|
326
|
+
const order = [
|
|
327
|
+
...runOrder(nodes.filter((n) => reachable.has(n.id)), outgoing),
|
|
328
|
+
...runOrder(nodes.filter((n) => !reachable.has(n.id)), outgoing),
|
|
329
|
+
];
|
|
330
|
+
|
|
331
|
+
const spineAncestor = (id: string): string | undefined => {
|
|
332
|
+
const seen = new Set([id]);
|
|
333
|
+
for (let up = incoming.get(id)?.from; up !== undefined && !seen.has(up); up = incoming.get(up)?.from) {
|
|
334
|
+
const parent = byId.get(up);
|
|
335
|
+
if (parent && isSpineKind(parent.kind)) return up;
|
|
336
|
+
seen.add(up);
|
|
337
|
+
}
|
|
338
|
+
return undefined;
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const branches = new Map<string, TimelineRow[]>();
|
|
342
|
+
const loose: TimelineRow[] = [];
|
|
343
|
+
for (const node of order) {
|
|
344
|
+
if (isSpineKind(node.kind)) continue;
|
|
345
|
+
const parentId = spineAncestor(node.id);
|
|
346
|
+
const row: TimelineRow = { node, depth: 1, parentId, edge: incoming.get(node.id) };
|
|
347
|
+
const siblings = parentId === undefined ? loose : branches.get(parentId);
|
|
348
|
+
if (siblings) siblings.push(row);
|
|
349
|
+
else if (parentId !== undefined) branches.set(parentId, [row]);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
const rows: TimelineRow[] = [];
|
|
353
|
+
for (const node of order) {
|
|
354
|
+
if (!isSpineKind(node.kind)) continue;
|
|
355
|
+
rows.push({ node, depth: 0, edge: incoming.get(node.id) }, ...(branches.get(node.id) ?? []));
|
|
356
|
+
}
|
|
357
|
+
rows.push(...loose);
|
|
358
|
+
return rows;
|
|
359
|
+
}
|