ltcai 4.0.0 → 4.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.
Files changed (195) hide show
  1. package/README.md +42 -33
  2. package/desktop/electron/main.cjs +44 -0
  3. package/docs/CHANGELOG.md +106 -0
  4. package/docs/REALTIME_COLLABORATION.md +3 -3
  5. package/docs/V3_FRONTEND.md +9 -8
  6. package/docs/V4_1_FRONTEND_ARCHITECTURE_REVIEW.md +65 -0
  7. package/docs/V4_1_FRONTEND_MIGRATION_REPORT.md +70 -0
  8. package/docs/V4_1_VALIDATION_REPORT.md +47 -0
  9. package/docs/V4_DIGITAL_BRAIN_RECOVERY.md +95 -45
  10. package/docs/kg-schema.md +6 -2
  11. package/docs/spec-vs-impl.md +10 -10
  12. package/frontend/index.html +24 -0
  13. package/frontend/openapi.json +14190 -0
  14. package/frontend/src/App.tsx +184 -0
  15. package/frontend/src/api/client.ts +317 -0
  16. package/frontend/src/api/openapi.ts +16637 -0
  17. package/frontend/src/components/primitives.tsx +204 -0
  18. package/frontend/src/components/ui/badge.tsx +27 -0
  19. package/frontend/src/components/ui/button.tsx +37 -0
  20. package/frontend/src/components/ui/card.tsx +22 -0
  21. package/frontend/src/components/ui/input.tsx +16 -0
  22. package/frontend/src/components/ui/textarea.tsx +16 -0
  23. package/frontend/src/lib/utils.ts +33 -0
  24. package/frontend/src/main.tsx +23 -0
  25. package/frontend/src/pages/Act.tsx +245 -0
  26. package/frontend/src/pages/Ask.tsx +200 -0
  27. package/frontend/src/pages/Brain.tsx +267 -0
  28. package/frontend/src/pages/Capture.tsx +158 -0
  29. package/frontend/src/pages/Library.tsx +187 -0
  30. package/frontend/src/pages/System.tsx +344 -0
  31. package/frontend/src/routes.ts +85 -0
  32. package/frontend/src/store/appStore.ts +54 -0
  33. package/frontend/src/styles.css +107 -0
  34. package/kg_schema.py +2 -603
  35. package/knowledge_graph.py +37 -4958
  36. package/latticeai/__init__.py +1 -1
  37. package/latticeai/api/admin.py +15 -16
  38. package/latticeai/api/agents.py +13 -6
  39. package/latticeai/api/auth.py +19 -11
  40. package/latticeai/api/invitations.py +100 -0
  41. package/latticeai/api/knowledge_graph.py +4 -11
  42. package/latticeai/api/plugins.py +3 -6
  43. package/latticeai/api/realtime.py +4 -7
  44. package/latticeai/api/setup.py +5 -4
  45. package/latticeai/api/static_routes.py +13 -16
  46. package/latticeai/api/ui_redirects.py +26 -0
  47. package/latticeai/api/workflow_designer.py +39 -6
  48. package/latticeai/api/workspace.py +24 -10
  49. package/latticeai/app_factory.py +88 -17
  50. package/latticeai/brain/_kg_common.py +1123 -0
  51. package/latticeai/brain/discovery.py +1455 -0
  52. package/latticeai/brain/documents.py +218 -0
  53. package/latticeai/brain/ingest.py +644 -0
  54. package/latticeai/brain/projection.py +561 -0
  55. package/latticeai/brain/provenance.py +401 -0
  56. package/latticeai/brain/retrieval.py +1316 -0
  57. package/latticeai/brain/schema.py +640 -0
  58. package/latticeai/brain/store.py +216 -0
  59. package/latticeai/brain/write_master.py +225 -0
  60. package/latticeai/core/invitations.py +131 -0
  61. package/latticeai/core/marketplace.py +1 -1
  62. package/latticeai/core/multi_agent.py +1 -1
  63. package/latticeai/core/policy.py +54 -0
  64. package/latticeai/core/realtime.py +65 -44
  65. package/latticeai/core/sessions.py +31 -5
  66. package/latticeai/core/users.py +147 -0
  67. package/latticeai/core/workspace_os.py +420 -20
  68. package/latticeai/services/agent_runtime.py +242 -4
  69. package/latticeai/services/run_executor.py +328 -0
  70. package/latticeai/services/workspace_service.py +27 -19
  71. package/package.json +54 -27
  72. package/scripts/build_frontend_assets.mjs +38 -0
  73. package/scripts/bump_version.py +1 -1
  74. package/scripts/export_openapi.py +31 -0
  75. package/scripts/lint_frontend.mjs +86 -0
  76. package/scripts/run_python.mjs +47 -0
  77. package/src-tauri/Cargo.lock +4833 -0
  78. package/src-tauri/Cargo.toml +19 -0
  79. package/src-tauri/build.rs +3 -0
  80. package/src-tauri/capabilities/default.json +7 -0
  81. package/src-tauri/src/main.rs +78 -0
  82. package/src-tauri/tauri.conf.json +36 -0
  83. package/static/app/asset-manifest.json +32 -0
  84. package/static/app/assets/core-CwxXejkd.js +2 -0
  85. package/static/app/assets/core-CwxXejkd.js.map +1 -0
  86. package/static/app/assets/index-CJRAzNnf.js +333 -0
  87. package/static/app/assets/index-CJRAzNnf.js.map +1 -0
  88. package/static/app/assets/index-CSwBBgf4.css +2 -0
  89. package/static/app/index.html +25 -0
  90. package/static/manifest.json +2 -2
  91. package/static/sw.js +4 -4
  92. package/scripts/build_v3_assets.mjs +0 -170
  93. package/scripts/lint_v3.mjs +0 -97
  94. package/static/account.html +0 -113
  95. package/static/activity.html +0 -73
  96. package/static/admin.html +0 -486
  97. package/static/agents.html +0 -139
  98. package/static/chat.html +0 -841
  99. package/static/css/reference/account.css +0 -439
  100. package/static/css/reference/admin.css +0 -610
  101. package/static/css/reference/base.css +0 -1661
  102. package/static/css/reference/chat.css +0 -4623
  103. package/static/css/reference/graph.css +0 -1016
  104. package/static/css/responsive.css +0 -861
  105. package/static/graph.html +0 -122
  106. package/static/platform.css +0 -104
  107. package/static/plugins.html +0 -136
  108. package/static/scripts/account.js +0 -238
  109. package/static/scripts/admin.js +0 -1614
  110. package/static/scripts/chat.js +0 -5081
  111. package/static/scripts/graph.js +0 -1804
  112. package/static/scripts/platform.js +0 -64
  113. package/static/scripts/ux.js +0 -167
  114. package/static/scripts/workspace.js +0 -948
  115. package/static/v3/asset-manifest.json +0 -56
  116. package/static/v3/css/lattice.base.49deefb5.css +0 -128
  117. package/static/v3/css/lattice.base.css +0 -128
  118. package/static/v3/css/lattice.components.cde18231.css +0 -472
  119. package/static/v3/css/lattice.components.css +0 -472
  120. package/static/v3/css/lattice.shell.29d36d85.css +0 -452
  121. package/static/v3/css/lattice.shell.css +0 -452
  122. package/static/v3/css/lattice.tokens.304cbc40.css +0 -135
  123. package/static/v3/css/lattice.tokens.css +0 -135
  124. package/static/v3/css/lattice.views.0a18b6c5.css +0 -360
  125. package/static/v3/css/lattice.views.css +0 -360
  126. package/static/v3/index.html +0 -68
  127. package/static/v3/js/app.356e6452.js +0 -26
  128. package/static/v3/js/app.js +0 -26
  129. package/static/v3/js/core/api.7a308b89.js +0 -568
  130. package/static/v3/js/core/api.js +0 -568
  131. package/static/v3/js/core/components.f25b3b93.js +0 -230
  132. package/static/v3/js/core/components.js +0 -230
  133. package/static/v3/js/core/dom.a2773eb0.js +0 -148
  134. package/static/v3/js/core/dom.js +0 -148
  135. package/static/v3/js/core/router.584570f2.js +0 -37
  136. package/static/v3/js/core/router.js +0 -37
  137. package/static/v3/js/core/routes.7222343d.js +0 -93
  138. package/static/v3/js/core/routes.js +0 -93
  139. package/static/v3/js/core/shell.a1657f20.js +0 -391
  140. package/static/v3/js/core/shell.js +0 -391
  141. package/static/v3/js/core/store.204a08b2.js +0 -113
  142. package/static/v3/js/core/store.js +0 -113
  143. package/static/v3/js/views/admin-audit.660a1fb1.js +0 -185
  144. package/static/v3/js/views/admin-audit.js +0 -185
  145. package/static/v3/js/views/admin-permissions.a7ae5f09.js +0 -177
  146. package/static/v3/js/views/admin-permissions.js +0 -177
  147. package/static/v3/js/views/admin-policies.3658fd86.js +0 -102
  148. package/static/v3/js/views/admin-policies.js +0 -102
  149. package/static/v3/js/views/admin-private-vpc.7d342d36.js +0 -135
  150. package/static/v3/js/views/admin-private-vpc.js +0 -135
  151. package/static/v3/js/views/admin-security.07c66b72.js +0 -180
  152. package/static/v3/js/views/admin-security.js +0 -180
  153. package/static/v3/js/views/admin-users.03bac88c.js +0 -168
  154. package/static/v3/js/views/admin-users.js +0 -168
  155. package/static/v3/js/views/agents.014d0b74.js +0 -541
  156. package/static/v3/js/views/agents.js +0 -541
  157. package/static/v3/js/views/chat.e6dd7dd0.js +0 -601
  158. package/static/v3/js/views/chat.js +0 -601
  159. package/static/v3/js/views/files.adad14c1.js +0 -365
  160. package/static/v3/js/views/files.js +0 -365
  161. package/static/v3/js/views/graph-canvas.17c15d65.js +0 -509
  162. package/static/v3/js/views/graph-canvas.js +0 -509
  163. package/static/v3/js/views/home.24f8b8ae.js +0 -200
  164. package/static/v3/js/views/home.js +0 -200
  165. package/static/v3/js/views/hooks.37895880.js +0 -220
  166. package/static/v3/js/views/hooks.js +0 -220
  167. package/static/v3/js/views/hybrid-search.2fb63ed9.js +0 -194
  168. package/static/v3/js/views/hybrid-search.js +0 -194
  169. package/static/v3/js/views/knowledge-graph.5e40cbeb.js +0 -509
  170. package/static/v3/js/views/knowledge-graph.js +0 -509
  171. package/static/v3/js/views/marketplace.ab0583d4.js +0 -141
  172. package/static/v3/js/views/marketplace.js +0 -141
  173. package/static/v3/js/views/mcp.99b5c6a7.js +0 -114
  174. package/static/v3/js/views/mcp.js +0 -114
  175. package/static/v3/js/views/memory.4ebdf474.js +0 -147
  176. package/static/v3/js/views/memory.js +0 -147
  177. package/static/v3/js/views/models.a1ffa147.js +0 -256
  178. package/static/v3/js/views/models.js +0 -256
  179. package/static/v3/js/views/my-computer.d9d9ae1c.js +0 -463
  180. package/static/v3/js/views/my-computer.js +0 -463
  181. package/static/v3/js/views/pipeline.c522f1ce.js +0 -157
  182. package/static/v3/js/views/pipeline.js +0 -157
  183. package/static/v3/js/views/planning.9ac3e313.js +0 -153
  184. package/static/v3/js/views/planning.js +0 -153
  185. package/static/v3/js/views/settings.8631fa5e.js +0 -318
  186. package/static/v3/js/views/settings.js +0 -318
  187. package/static/v3/js/views/skills.c6c2f965.js +0 -109
  188. package/static/v3/js/views/skills.js +0 -109
  189. package/static/v3/js/views/tools.e4f11276.js +0 -108
  190. package/static/v3/js/views/tools.js +0 -108
  191. package/static/v3/js/views/workflows.26c57290.js +0 -128
  192. package/static/v3/js/views/workflows.js +0 -128
  193. package/static/workflows.html +0 -146
  194. package/static/workspace.css +0 -1121
  195. package/static/workspace.html +0 -357
@@ -1,509 +0,0 @@
1
- /* ============================================================================
2
- * Graph canvas — force-directed renderer for the Knowledge Graph explorer.
3
- * Ports the legacy static/scripts/graph.js physics (pairwise repulsion +
4
- * edge springs + center gravity + velocity damping) onto a dependency-free
5
- * <canvas>. Interactions: pointer-event node drag, wheel zoom + drag pan,
6
- * two-pointer pinch zoom, hover highlight, click-to-select callback,
7
- * double-click to refit.
8
- *
9
- * Honesty + design rules:
10
- * - Renders ONLY the nodes/edges it is given — never fabricates data.
11
- * - Every color is resolved from design tokens via getComputedStyle at draw
12
- * time (light/dark aware); nothing is hardcoded.
13
- * - The RAF loop self-suspends when the simulation settles, when the
14
- * document is hidden, and when the canvas leaves the DOM; interactions,
15
- * resize, theme flips and re-attachment wake it.
16
- * ========================================================================== */
17
-
18
- const WORLD_W = 1000; // seed-layout world units (camera-independent)
19
- const WORLD_H = 620;
20
- const MIN_SCALE = 0.15;
21
- const MAX_SCALE = 4;
22
- const SETTLE_ENERGY = 0.05; // total kinetic energy below which RAF pauses
23
- const REPULSION = 2200;
24
- const SPRING = 0.0042;
25
- const DAMPING = 0.85;
26
- const REST_LENGTH = 120;
27
- const CLICK_SLOP_PX = 5; // pointer travel under this counts as a click
28
-
29
- /**
30
- * createGraphCanvas({ colorFor, onSelect })
31
- * - colorFor(type) → a token reference like "var(--lt3-pillar-graph)".
32
- * - onSelect(id|null) — fired on canvas click (toggle) and empty-space click.
33
- * Returns { el, setData({nodes, edges}), setSelected(id|null), destroy() }.
34
- */
35
- export function createGraphCanvas({ colorFor, onSelect } = {}) {
36
- const canvas = document.createElement("canvas");
37
- canvas.setAttribute("role", "img");
38
- canvas.setAttribute("aria-label", "Knowledge graph");
39
- const ctx = canvas.getContext("2d");
40
-
41
- let nodes = [];
42
- let edges = [];
43
- let byId = new Map();
44
- let cam = { scale: 1, tx: 0, ty: 0 };
45
- let width = 0;
46
- let height = 0;
47
- let hovered = null;
48
- let selectedId = null;
49
- let dragging = null;
50
- let panning = null;
51
- let pinch = null;
52
- let pressTravel = Infinity;
53
- const pointers = new Map();
54
- let raf = 0;
55
- let needsFit = false;
56
- let tokens = null;
57
- const typeColors = new Map();
58
- let destroyed = false;
59
-
60
- /* ── design tokens (resolved, never hardcoded) ─────────────────────────── */
61
- function readVar(style, name, fallback) {
62
- const v = (style.getPropertyValue(name) || "").trim();
63
- return v || fallback;
64
- }
65
-
66
- function refreshTokens() {
67
- const style = getComputedStyle(canvas.isConnected ? canvas : document.documentElement);
68
- const text = readVar(style, "--text", style.color);
69
- tokens = {
70
- text,
71
- surface: readVar(style, "--surface", readVar(style, "--bg", text)),
72
- edge: readVar(style, "--border-strong", readVar(style, "--border", text)),
73
- accent: readVar(style, "--accent", text),
74
- muted: readVar(style, "--muted", text),
75
- font: style.fontFamily || "system-ui",
76
- };
77
- typeColors.clear();
78
- }
79
-
80
- function nodeColor(type) {
81
- if (!typeColors.has(type)) {
82
- const ref = colorFor ? String(colorFor(type) || "") : "";
83
- const m = /^var\((--[\w-]+)/.exec(ref.trim());
84
- const style = getComputedStyle(document.documentElement);
85
- typeColors.set(type, m ? readVar(style, m[1], tokens.accent) : tokens.accent);
86
- }
87
- return typeColors.get(type);
88
- }
89
-
90
- /* ── data ──────────────────────────────────────────────────────────────── */
91
- function setData(data) {
92
- const src = data || {};
93
- byId = new Map();
94
- nodes = (src.nodes || []).map((n) => {
95
- const node = {
96
- id: n.id,
97
- label: String(n.label || n.id || ""),
98
- type: n.type || "Entity",
99
- weight: clamp(Number(n.weight) || 0, 0, 1),
100
- seedX: Number.isFinite(n.x) ? n.x : null,
101
- seedY: Number.isFinite(n.y) ? n.y : null,
102
- x: 0, y: 0, vx: 0, vy: 0, r: 10, degree: 0,
103
- };
104
- byId.set(node.id, node);
105
- return node;
106
- });
107
- edges = (src.edges || [])
108
- .filter((e) => byId.has(e.from) && byId.has(e.to))
109
- .map((e) => ({
110
- from: e.from,
111
- to: e.to,
112
- weight: Number(e.weight) || 1,
113
- source: byId.get(e.from),
114
- target: byId.get(e.to),
115
- }));
116
- edges.forEach((e) => { e.source.degree++; e.target.degree++; });
117
- nodes.forEach((n) => {
118
- n.r = clamp(8 + n.weight * 10 + Math.sqrt(n.degree) * 1.6, 8, 28);
119
- });
120
- if (selectedId && !byId.has(selectedId)) selectedId = null;
121
- hovered = null;
122
- dragging = null;
123
- seedLayout();
124
- needsFit = true;
125
- canvas.dataset.nodeCount = String(nodes.length);
126
- canvas.setAttribute("aria-label",
127
- `Knowledge graph: ${nodes.length} entities, ${edges.length} relations. Use the inspector list for keyboard access.`);
128
- wake();
129
- }
130
-
131
- function seedLayout() {
132
- if (!nodes.length) return;
133
- const hasCoords = nodes.every((n) => n.seedX !== null && n.seedY !== null);
134
- if (hasCoords) {
135
- // Backend-provided normalized [0..1] coordinates seed the simulation.
136
- nodes.forEach((n) => {
137
- n.x = 60 + n.seedX * (WORLD_W - 120);
138
- n.y = 50 + n.seedY * (WORLD_H - 100);
139
- });
140
- return;
141
- }
142
- // Golden-angle spiral by weight rank: heavy nodes near the center.
143
- const golden = Math.PI * (3 - Math.sqrt(5));
144
- const maxR = Math.min(WORLD_W, WORLD_H) * 0.42;
145
- const order = [...nodes].sort((a, b) => b.weight - a.weight);
146
- order.forEach((n, rank) => {
147
- const radius = rank === 0 ? 0 : maxR * Math.sqrt(rank / Math.max(1, nodes.length - 1));
148
- const angle = rank * golden;
149
- n.x = WORLD_W / 2 + Math.cos(angle) * radius;
150
- n.y = WORLD_H / 2 + Math.sin(angle) * radius * 0.72;
151
- });
152
- }
153
-
154
- /* ── physics (ported from legacy graph.js step()) ──────────────────────── */
155
- function step() {
156
- const centerPull = selectedId ? 0.00035 : 0.00055;
157
- for (let i = 0; i < nodes.length; i++) {
158
- for (let j = i + 1; j < nodes.length; j++) {
159
- const a = nodes[i];
160
- const b = nodes[j];
161
- const dx = a.x - b.x;
162
- const dy = a.y - b.y;
163
- const d2 = Math.max(120, dx * dx + dy * dy);
164
- const force = REPULSION / d2;
165
- a.vx += dx * force; a.vy += dy * force;
166
- b.vx -= dx * force; b.vy -= dy * force;
167
- }
168
- }
169
- edges.forEach((e) => {
170
- const dx = e.target.x - e.source.x;
171
- const dy = e.target.y - e.source.y;
172
- const dist = Math.max(1, Math.hypot(dx, dy));
173
- const force = (dist - REST_LENGTH) * (SPRING + Math.min(0.003, e.weight * 0.0012));
174
- e.source.vx += (dx / dist) * force;
175
- e.source.vy += (dy / dist) * force;
176
- e.target.vx -= (dx / dist) * force;
177
- e.target.vy -= (dy / dist) * force;
178
- });
179
- let energy = 0;
180
- nodes.forEach((n) => {
181
- if (n === dragging) return;
182
- n.vx += (WORLD_W / 2 - n.x) * centerPull;
183
- n.vy += (WORLD_H / 2 - n.y) * centerPull;
184
- n.vx *= DAMPING;
185
- n.vy *= DAMPING;
186
- n.x += n.vx;
187
- n.y += n.vy;
188
- energy += n.vx * n.vx + n.vy * n.vy;
189
- });
190
- return energy;
191
- }
192
-
193
- /* ── camera ────────────────────────────────────────────────────────────── */
194
- function toWorld(px, py) {
195
- return { x: (px - cam.tx) / cam.scale, y: (py - cam.ty) / cam.scale };
196
- }
197
-
198
- function applyZoom(px, py, factor) {
199
- const next = clamp(cam.scale * factor, MIN_SCALE, MAX_SCALE);
200
- cam.tx = px - (px - cam.tx) * (next / cam.scale);
201
- cam.ty = py - (py - cam.ty) * (next / cam.scale);
202
- cam.scale = next;
203
- }
204
-
205
- function fitToScreen() {
206
- if (!nodes.length || !width || !height) return;
207
- let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
208
- nodes.forEach((n) => {
209
- x0 = Math.min(x0, n.x - n.r); x1 = Math.max(x1, n.x + n.r);
210
- y0 = Math.min(y0, n.y - n.r); y1 = Math.max(y1, n.y + n.r);
211
- });
212
- const margin = 48;
213
- cam.scale = clamp(Math.min(
214
- (width - margin * 2) / Math.max(1, x1 - x0),
215
- (height - margin * 2) / Math.max(1, y1 - y0),
216
- ), MIN_SCALE, 2.5);
217
- cam.tx = (width - (x0 + x1) * cam.scale) / 2;
218
- cam.ty = (height - (y0 + y1) * cam.scale) / 2;
219
- }
220
-
221
- function centerOnNode(node) {
222
- cam.tx = width / 2 - node.x * cam.scale;
223
- cam.ty = height / 2 - node.y * cam.scale;
224
- }
225
-
226
- function nodeOnScreen(node) {
227
- const sx = node.x * cam.scale + cam.tx;
228
- const sy = node.y * cam.scale + cam.ty;
229
- return sx >= 0 && sx <= width && sy >= 0 && sy <= height;
230
- }
231
-
232
- function nodeAt(px, py) {
233
- const w = toWorld(px, py);
234
- let best = null;
235
- let bestDist = Infinity;
236
- nodes.forEach((n) => {
237
- const dist = Math.hypot(n.x - w.x, n.y - w.y);
238
- if (dist < n.r + 8 / cam.scale && dist < bestDist) { best = n; bestDist = dist; }
239
- });
240
- return best;
241
- }
242
-
243
- /* ── render loop (self-suspending) ─────────────────────────────────────── */
244
- function wake() {
245
- if (destroyed || raf || document.hidden || !canvas.isConnected) return;
246
- raf = requestAnimationFrame(draw);
247
- }
248
-
249
- function neighborSetOf(node) {
250
- const ids = new Set([node.id]);
251
- edges.forEach((e) => {
252
- if (e.from === node.id) ids.add(e.to);
253
- if (e.to === node.id) ids.add(e.from);
254
- });
255
- return ids;
256
- }
257
-
258
- function draw() {
259
- raf = 0;
260
- if (destroyed || document.hidden || !canvas.isConnected) return;
261
- if (!tokens) refreshTokens();
262
- const energy = step();
263
- if (needsFit) { fitToScreen(); needsFit = false; }
264
-
265
- ctx.clearRect(0, 0, width, height);
266
- ctx.save();
267
- ctx.translate(cam.tx, cam.ty);
268
- ctx.scale(cam.scale, cam.scale);
269
-
270
- const selected = selectedId ? byId.get(selectedId) : null;
271
- const active = hovered || selected;
272
- const neighbors = active ? neighborSetOf(active) : null;
273
- // LOD: drop labels when zoomed far out or the mesh is dense.
274
- const showLabels = cam.scale >= 0.55 && nodes.length <= 200;
275
-
276
- edges.forEach((e) => {
277
- const lit = neighbors && neighbors.has(e.from) && neighbors.has(e.to);
278
- ctx.globalAlpha = neighbors ? (lit ? 0.85 : 0.07) : 0.35;
279
- ctx.strokeStyle = tokens.edge;
280
- ctx.lineWidth = (1 + Math.min(2.4, e.weight * 0.6) + (lit ? 0.5 : 0)) / cam.scale;
281
- ctx.beginPath();
282
- ctx.moveTo(e.source.x, e.source.y);
283
- ctx.lineTo(e.target.x, e.target.y);
284
- ctx.stroke();
285
- });
286
-
287
- nodes.forEach((n) => {
288
- const isSelected = n === selected;
289
- const isHovered = n === hovered;
290
- const alpha = neighbors ? (neighbors.has(n.id) ? 1 : 0.14) : 1;
291
- const radius = n.r + (isSelected ? 3 : isHovered ? 2 : 0);
292
-
293
- ctx.globalAlpha = alpha;
294
- ctx.fillStyle = nodeColor(n.type);
295
- ctx.beginPath();
296
- ctx.arc(n.x, n.y, radius, 0, Math.PI * 2);
297
- ctx.fill();
298
- ctx.strokeStyle = tokens.surface;
299
- ctx.lineWidth = 2 / cam.scale;
300
- ctx.stroke();
301
-
302
- if (isSelected || isHovered) {
303
- ctx.strokeStyle = isSelected ? tokens.accent : nodeColor(n.type);
304
- ctx.lineWidth = (isSelected ? 2.6 : 1.8) / cam.scale;
305
- ctx.globalAlpha = alpha * 0.6;
306
- ctx.beginPath();
307
- ctx.arc(n.x, n.y, radius + 5 / cam.scale, 0, Math.PI * 2);
308
- ctx.stroke();
309
- ctx.globalAlpha = alpha;
310
- }
311
-
312
- if (showLabels || isSelected || isHovered) {
313
- const label = n.label.length > 22 ? n.label.slice(0, 21) + "…" : n.label;
314
- const fs = Math.max(9.5, 12 / cam.scale);
315
- ctx.font = `600 ${fs}px ${tokens.font}`;
316
- const lw = ctx.measureText(label).width;
317
- const lx = n.x - lw / 2;
318
- const ly = n.y + radius + (8 / cam.scale) + fs;
319
- const pad = 4 / cam.scale;
320
- ctx.globalAlpha = alpha > 0.5 ? alpha * 0.85 : alpha * 0.25;
321
- ctx.fillStyle = tokens.surface;
322
- ctx.beginPath();
323
- if (ctx.roundRect) ctx.roundRect(lx - pad, ly - fs, lw + pad * 2, fs + pad * 1.6, 5 / cam.scale);
324
- else ctx.rect(lx - pad, ly - fs, lw + pad * 2, fs + pad * 1.6);
325
- ctx.fill();
326
- ctx.globalAlpha = alpha;
327
- ctx.fillStyle = tokens.text;
328
- ctx.fillText(label, lx, ly);
329
- }
330
- });
331
-
332
- ctx.globalAlpha = 1;
333
- ctx.restore();
334
- if (energy > SETTLE_ENERGY || dragging) raf = requestAnimationFrame(draw);
335
- }
336
-
337
- /* ── interactions (pointer events: mouse + touch + pen) ────────────────── */
338
- function localPoint(e) {
339
- const rect = canvas.getBoundingClientRect();
340
- return { x: e.clientX - rect.left, y: e.clientY - rect.top };
341
- }
342
-
343
- function pinchState() {
344
- const [a, b] = [...pointers.values()];
345
- return { d: Math.hypot(a.x - b.x, a.y - b.y), cx: (a.x + b.x) / 2, cy: (a.y + b.y) / 2 };
346
- }
347
-
348
- function onPointerDown(e) {
349
- canvas.setPointerCapture(e.pointerId);
350
- pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
351
- if (pointers.size === 2) {
352
- dragging = null;
353
- panning = null;
354
- pinch = pinchState();
355
- return;
356
- }
357
- pressTravel = 0;
358
- const p = localPoint(e);
359
- const node = nodeAt(p.x, p.y);
360
- if (node) {
361
- dragging = node;
362
- } else {
363
- panning = { sx: e.clientX, sy: e.clientY, tx0: cam.tx, ty0: cam.ty };
364
- canvas.style.cursor = "grabbing";
365
- }
366
- wake();
367
- }
368
-
369
- function onPointerMove(e) {
370
- const prev = pointers.get(e.pointerId);
371
- if (prev) {
372
- pressTravel += Math.hypot(e.clientX - prev.x, e.clientY - prev.y);
373
- pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
374
- }
375
- if (pinch && pointers.size === 2) {
376
- const next = pinchState();
377
- const rect = canvas.getBoundingClientRect();
378
- applyZoom(next.cx - rect.left, next.cy - rect.top, next.d / Math.max(1, pinch.d));
379
- pinch = next;
380
- wake();
381
- return;
382
- }
383
- if (dragging) {
384
- const p = localPoint(e);
385
- const w = toWorld(p.x, p.y);
386
- dragging.x = w.x; dragging.y = w.y;
387
- dragging.vx = 0; dragging.vy = 0;
388
- wake();
389
- } else if (panning) {
390
- cam.tx = panning.tx0 + (e.clientX - panning.sx);
391
- cam.ty = panning.ty0 + (e.clientY - panning.sy);
392
- wake();
393
- } else {
394
- const p = localPoint(e);
395
- const node = nodeAt(p.x, p.y);
396
- if (node !== hovered) { hovered = node; wake(); }
397
- canvas.style.cursor = node ? "pointer" : "grab";
398
- }
399
- }
400
-
401
- function onPointerUp(e) {
402
- pointers.delete(e.pointerId);
403
- if (pointers.size < 2) pinch = null;
404
- dragging = null;
405
- panning = null;
406
- canvas.style.cursor = "grab";
407
- if (pressTravel < CLICK_SLOP_PX) {
408
- const p = localPoint(e);
409
- const node = nodeAt(p.x, p.y);
410
- const next = node && node.id !== selectedId ? node.id : null;
411
- selectedId = next;
412
- if (onSelect) onSelect(next);
413
- }
414
- pressTravel = Infinity;
415
- wake();
416
- }
417
-
418
- function onPointerCancel(e) {
419
- pointers.delete(e.pointerId);
420
- pinch = null;
421
- dragging = null;
422
- panning = null;
423
- pressTravel = Infinity;
424
- }
425
-
426
- function onPointerLeave() {
427
- if (hovered) { hovered = null; wake(); }
428
- }
429
-
430
- function onWheel(e) {
431
- e.preventDefault();
432
- const p = localPoint(e);
433
- applyZoom(p.x, p.y, e.deltaY < 0 ? 1.12 : 1 / 1.12);
434
- wake();
435
- }
436
-
437
- function onDblClick() {
438
- fitToScreen();
439
- wake();
440
- }
441
-
442
- function onVisibility() {
443
- if (!document.hidden) wake();
444
- }
445
-
446
- /* ── environment: resize (DPR-aware) + theme flips ─────────────────────── */
447
- function resize() {
448
- const rect = canvas.getBoundingClientRect();
449
- if (!rect.width || !rect.height) return;
450
- width = rect.width;
451
- height = rect.height;
452
- const dpr = window.devicePixelRatio || 1;
453
- canvas.width = Math.floor(width * dpr);
454
- canvas.height = Math.floor(height * dpr);
455
- ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
456
- wake();
457
- }
458
-
459
- const resizeObserver = new ResizeObserver(resize);
460
- resizeObserver.observe(canvas);
461
-
462
- const themeObserver = new MutationObserver(() => { refreshTokens(); wake(); });
463
- themeObserver.observe(document.documentElement, { attributes: true, attributeFilter: ["data-lt-theme"] });
464
- const colorScheme = window.matchMedia("(prefers-color-scheme: dark)");
465
- const onScheme = () => { refreshTokens(); wake(); };
466
- colorScheme.addEventListener("change", onScheme);
467
-
468
- canvas.addEventListener("pointerdown", onPointerDown);
469
- canvas.addEventListener("pointermove", onPointerMove);
470
- canvas.addEventListener("pointerup", onPointerUp);
471
- canvas.addEventListener("pointercancel", onPointerCancel);
472
- canvas.addEventListener("pointerleave", onPointerLeave);
473
- canvas.addEventListener("wheel", onWheel, { passive: false });
474
- canvas.addEventListener("dblclick", onDblClick);
475
- document.addEventListener("visibilitychange", onVisibility);
476
-
477
- function setSelected(id) {
478
- selectedId = id || null;
479
- const node = selectedId ? byId.get(selectedId) : null;
480
- if (node && width && !nodeOnScreen(node)) centerOnNode(node);
481
- wake();
482
- }
483
-
484
- function destroy() {
485
- destroyed = true;
486
- if (raf) { cancelAnimationFrame(raf); raf = 0; }
487
- resizeObserver.disconnect();
488
- themeObserver.disconnect();
489
- colorScheme.removeEventListener("change", onScheme);
490
- document.removeEventListener("visibilitychange", onVisibility);
491
- canvas.removeEventListener("pointerdown", onPointerDown);
492
- canvas.removeEventListener("pointermove", onPointerMove);
493
- canvas.removeEventListener("pointerup", onPointerUp);
494
- canvas.removeEventListener("pointercancel", onPointerCancel);
495
- canvas.removeEventListener("pointerleave", onPointerLeave);
496
- canvas.removeEventListener("wheel", onWheel);
497
- canvas.removeEventListener("dblclick", onDblClick);
498
- pointers.clear();
499
- nodes = [];
500
- edges = [];
501
- byId = new Map();
502
- }
503
-
504
- return { el: canvas, setData, setSelected, destroy };
505
- }
506
-
507
- function clamp(v, min, max) {
508
- return Math.max(min, Math.min(max, v));
509
- }