ltcai 4.5.1 → 4.6.1

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 (34) hide show
  1. package/README.md +123 -179
  2. package/docs/CHANGELOG.md +120 -0
  3. package/docs/V4_6_0_LIVING_BRAIN_EXPERIENCE_REPORT.md +72 -0
  4. package/docs/V4_6_1_RELEASE_REFRESH_REPORT.md +42 -0
  5. package/docs/V4_DIGITAL_BRAIN_RECOVERY.md +19 -17
  6. package/frontend/index.html +2 -2
  7. package/frontend/src/App.tsx +653 -208
  8. package/frontend/src/api/client.ts +1 -0
  9. package/frontend/src/components/BrainConversation.tsx +309 -0
  10. package/frontend/src/components/FirstRunGuide.tsx +4 -4
  11. package/frontend/src/components/LivingBrain.tsx +212 -0
  12. package/frontend/src/components/ProductFlow.tsx +654 -0
  13. package/frontend/src/pages/Ask.tsx +2 -229
  14. package/frontend/src/pages/Brain.tsx +68 -49
  15. package/frontend/src/routes.ts +15 -26
  16. package/frontend/src/styles.css +2375 -87
  17. package/lattice_brain/__init__.py +1 -1
  18. package/lattice_brain/runtime/multi_agent.py +1 -1
  19. package/latticeai/__init__.py +1 -1
  20. package/latticeai/core/marketplace.py +1 -1
  21. package/latticeai/core/workspace_os.py +1 -1
  22. package/package.json +2 -2
  23. package/src-tauri/Cargo.lock +1 -1
  24. package/src-tauri/Cargo.toml +1 -1
  25. package/src-tauri/tauri.conf.json +1 -1
  26. package/static/app/asset-manifest.json +5 -5
  27. package/static/app/assets/index-7U86v70r.css +2 -0
  28. package/static/app/assets/index-D1jAPQws.js +16 -0
  29. package/static/app/assets/index-D1jAPQws.js.map +1 -0
  30. package/static/app/index.html +4 -4
  31. package/static/manifest.json +1 -1
  32. package/static/app/assets/index-3G8qcrIS.js +0 -336
  33. package/static/app/assets/index-3G8qcrIS.js.map +0 -1
  34. package/static/app/assets/index-C0wYZp7k.css +0 -2
@@ -1,259 +1,704 @@
1
1
  import * as React from "react";
2
- import { useQuery } from "@tanstack/react-query";
3
- import { BrainCircuit, CheckCircle2, Command, Menu, Moon, Search, Sparkles, Sun, X } from "lucide-react";
2
+ import { useQuery, useQueryClient } from "@tanstack/react-query";
3
+ import { ImagePlus, Search, Send } from "lucide-react";
4
4
  import { latticeApi } from "@/api/client";
5
5
  import { Button } from "@/components/ui/button";
6
- import { Input } from "@/components/ui/input";
7
- import { FirstRunGuide } from "@/components/FirstRunGuide";
8
- import { useAppStore, WorkspaceMode } from "@/store/appStore";
9
- import { commandRoutes, go, parseHash, primaryRoutes, PrimaryRoute } from "@/routes";
10
- import { BrainPage } from "@/pages/Brain";
11
- import { AskPage } from "@/pages/Ask";
12
- import { CapturePage } from "@/pages/Capture";
13
- import { ActPage } from "@/pages/Act";
14
- import { LibraryPage } from "@/pages/Library";
15
- import { SystemPage } from "@/pages/System";
16
- import { cn } from "@/lib/utils";
17
-
18
- const modes: Array<{ id: WorkspaceMode; label: string }> = [
19
- { id: "basic", label: "Calm" },
20
- { id: "advanced", label: "Deep" },
21
- { id: "admin", label: "Admin" },
6
+ import { type BrainState, LivingBrain, triggerBrainRecall } from "@/components/LivingBrain";
7
+ import { ProductFlow, readProductFlowComplete } from "@/components/ProductFlow";
8
+ import { useAppStore } from "@/store/appStore";
9
+ import { asArray } from "@/lib/utils";
10
+
11
+ type ApiRecord = Record<string, unknown>;
12
+ type BrainDepth = 1 | 2 | 3 | 4 | 5;
13
+
14
+ type Message = {
15
+ role: "user" | "assistant";
16
+ content: string;
17
+ };
18
+
19
+ type MemoryFragment = {
20
+ id: string;
21
+ title: string;
22
+ kind: string;
23
+ };
24
+
25
+ type KnowledgeConcept = {
26
+ id: string;
27
+ label: string;
28
+ type: string;
29
+ summary: string;
30
+ importance: number;
31
+ };
32
+
33
+ type RelationshipThread = {
34
+ id: string;
35
+ source: string;
36
+ target: string;
37
+ label: string;
38
+ weight: number;
39
+ };
40
+
41
+ type KnowledgeGraphModel = {
42
+ nodes: KnowledgeConcept[];
43
+ edges: RelationshipThread[];
44
+ };
45
+
46
+ const DEPTHS: Array<{ level: BrainDepth; label: string; state: BrainState }> = [
47
+ { level: 1, label: "Living Brain", state: "idle" },
48
+ { level: 2, label: "Memory Layer", state: "recalling" },
49
+ { level: 3, label: "Knowledge Layer", state: "synthesizing" },
50
+ { level: 4, label: "Relationship Layer", state: "planning" },
51
+ { level: 5, label: "Knowledge Graph", state: "synthesizing" },
22
52
  ];
23
53
 
24
- function useRoute() {
25
- const [route, setRoute] = React.useState(parseHash);
54
+ export default function App() {
55
+ const theme = useAppStore((state) => state.theme);
56
+ const [flowComplete, setFlowComplete] = React.useState(readProductFlowComplete);
57
+ const { state: brainState, intensity, setBrain } = useBrainState();
58
+
26
59
  React.useEffect(() => {
27
- const onHash = () => setRoute(parseHash());
28
- window.addEventListener("hashchange", onHash);
29
- return () => window.removeEventListener("hashchange", onHash);
60
+ document.documentElement.dataset.theme = theme;
61
+ }, [theme]);
62
+
63
+ React.useEffect(() => {
64
+ const onKey = (event: KeyboardEvent) => {
65
+ if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
66
+ event.preventDefault();
67
+ document.querySelector<HTMLTextAreaElement>(".brain-composer textarea")?.focus();
68
+ }
69
+ };
70
+ window.addEventListener("keydown", onKey);
71
+ return () => window.removeEventListener("keydown", onKey);
30
72
  }, []);
31
- return route;
32
- }
33
73
 
34
- function Page({ primary, tab }: { primary: PrimaryRoute; tab?: string }) {
35
- if (primary === "ask") return <AskPage />;
36
- if (primary === "capture") return <CapturePage initialTab={tab} />;
37
- if (primary === "act") return <ActPage initialTab={tab} />;
38
- if (primary === "library") return <LibraryPage initialTab={tab} />;
39
- if (primary === "system") return <SystemPage initialTab={tab} />;
40
- return <BrainPage initialTab={tab} />;
41
- }
74
+ if (!flowComplete) {
75
+ return <ProductFlow onComplete={() => setFlowComplete(true)} />;
76
+ }
42
77
 
43
- function AmbientBrain() {
44
78
  return (
45
- <div className="ambient-brain" aria-hidden="true">
46
- <span className="signal-line signal-line-a" />
47
- <span className="signal-line signal-line-b" />
48
- <span className="signal-line signal-line-c" />
49
- <span className="signal-tile signal-tile-a" />
50
- <span className="signal-tile signal-tile-b" />
51
- <span className="signal-tile signal-tile-c" />
79
+ <div className="brain-space">
80
+ <div className="brain-field" />
81
+ <BrainHome brainState={brainState} intensity={intensity} onBrainChange={setBrain} />
52
82
  </div>
53
83
  );
54
84
  }
55
85
 
56
- function CommandPalette({ open, onClose }: { open: boolean; onClose: () => void }) {
57
- const [query, setQuery] = React.useState("");
58
- const normalized = query.trim().toLowerCase();
59
- const matches = commandRoutes.filter((route) => (
60
- route.label.toLowerCase().includes(normalized) || route.key.includes(normalized)
61
- ));
86
+ function useBrainState() {
87
+ const [state, setState] = React.useState<BrainState>("idle");
88
+ const [intensity, setIntensity] = React.useState(0.58);
89
+
90
+ const setBrain = React.useCallback((next: BrainState, nextIntensity?: number) => {
91
+ setState(next);
92
+ if (nextIntensity !== undefined) setIntensity(clamp(nextIntensity, 0.38, 1));
93
+ }, []);
94
+
95
+ return { state, intensity, setBrain };
96
+ }
97
+
98
+ function BrainHome({
99
+ brainState,
100
+ intensity,
101
+ onBrainChange,
102
+ }: {
103
+ brainState: BrainState;
104
+ intensity: number;
105
+ onBrainChange: (state: BrainState, intensity?: number) => void;
106
+ }) {
107
+ const qc = useQueryClient();
108
+ const [messages, setMessages] = React.useState<Message[]>([]);
109
+ const [draft, setDraft] = React.useState("");
110
+ const [imageData, setImageData] = React.useState<string | null>(null);
111
+ const [streaming, setStreaming] = React.useState(false);
112
+ const [conversationId, setConversationId] = React.useState<string | null>(null);
113
+ const [explorationDepth, setExplorationDepth] = React.useState<BrainDepth>(1);
114
+ const [graphSearch, setGraphSearch] = React.useState("");
115
+ const [selectedGraphId, setSelectedGraphId] = React.useState<string | null>(null);
116
+ const streamRef = React.useRef<HTMLDivElement>(null);
117
+ const recallTimerRef = React.useRef<number | null>(null);
118
+
119
+ const memoriesQ = useQuery({ queryKey: ["memoryManager"], queryFn: latticeApi.memoryManager });
120
+ const historyQ = useQuery({ queryKey: ["chatHistory"], queryFn: latticeApi.chatHistory });
121
+ const graphQ = useQuery({ queryKey: ["graph"], queryFn: latticeApi.graph });
122
+ const modelsQ = useQuery({ queryKey: ["models"], queryFn: latticeApi.models });
123
+
124
+ const memoryFragments = React.useMemo(
125
+ () => buildMemoryFragments(memoriesQ.data?.data, historyQ.data?.data),
126
+ [memoriesQ.data, historyQ.data],
127
+ );
128
+ const graphModel = React.useMemo(() => parseKnowledgeGraph(graphQ.data?.data), [graphQ.data]);
129
+ const knowledgeConcepts = React.useMemo(
130
+ () => graphModel.nodes.slice(0, 10),
131
+ [graphModel.nodes],
132
+ );
133
+ const relationshipThreads = React.useMemo(
134
+ () => graphModel.edges.slice(0, 10),
135
+ [graphModel.edges],
136
+ );
137
+ const modelName = React.useMemo(() => currentModelName(modelsQ.data?.data), [modelsQ.data]);
138
+ const currentDepth = DEPTHS[explorationDepth - 1];
62
139
 
63
140
  React.useEffect(() => {
64
- if (!open) return;
65
- const onKey = (event: KeyboardEvent) => {
66
- if (event.key === "Escape") onClose();
141
+ if (streaming) onBrainChange("thinking", 0.94);
142
+ else if (draft.trim().length > 4) onBrainChange("listening", 0.76);
143
+ else onBrainChange(currentDepth.state, explorationDepth === 1 ? 0.58 : 0.66 + explorationDepth * 0.06);
144
+ }, [streaming, draft, currentDepth.state, explorationDepth, onBrainChange]);
145
+
146
+ React.useEffect(() => {
147
+ const stream = streamRef.current;
148
+ if (stream) stream.scrollTop = stream.scrollHeight;
149
+ }, [messages]);
150
+
151
+ React.useEffect(() => {
152
+ return () => {
153
+ if (recallTimerRef.current !== null) window.clearTimeout(recallTimerRef.current);
67
154
  };
68
- window.addEventListener("keydown", onKey);
69
- return () => window.removeEventListener("keydown", onKey);
70
- }, [open, onClose]);
155
+ }, []);
156
+
157
+ async function send() {
158
+ const text = draft.trim();
159
+ if (!text || streaming) return;
160
+ const activeConversationId = conversationId || `brain-${Date.now()}`;
161
+ if (!conversationId) setConversationId(activeConversationId);
162
+
163
+ setMessages((items) => [...items, { role: "user", content: text }, { role: "assistant", content: "" }]);
164
+ setDraft("");
165
+ setImageData(null);
166
+ setStreaming(true);
167
+ onBrainChange("thinking", 0.96);
168
+
169
+ try {
170
+ const result = await latticeApi.streamChat(
171
+ { message: text, conversation_id: activeConversationId, image_data: imageData || undefined },
172
+ {
173
+ onChunk: (_delta, fullText) => {
174
+ setMessages((items) => {
175
+ const next = [...items];
176
+ next[next.length - 1] = { role: "assistant", content: fullText };
177
+ return next;
178
+ });
179
+ },
180
+ onTrace: (trace) => {
181
+ if (!trace) return;
182
+ onBrainChange("recalling", 0.9);
183
+ triggerBrainRecall();
184
+ if (recallTimerRef.current !== null) window.clearTimeout(recallTimerRef.current);
185
+ recallTimerRef.current = window.setTimeout(() => onBrainChange("thinking", 0.9), 900);
186
+ },
187
+ },
188
+ );
189
+ if (result.error) {
190
+ setMessages((items) => {
191
+ const next = [...items];
192
+ next[next.length - 1] = { role: "assistant", content: `Unavailable: ${result.error}` };
193
+ return next;
194
+ });
195
+ }
196
+ } finally {
197
+ setStreaming(false);
198
+ void qc.invalidateQueries({ queryKey: ["chatHistory"] });
199
+ void qc.invalidateQueries({ queryKey: ["memoryManager"] });
200
+ void qc.invalidateQueries({ queryKey: ["graph"] });
201
+ }
202
+ }
203
+
204
+ function deepen() {
205
+ setExplorationDepth((depth) => {
206
+ const next = Math.min(5, depth + 1) as BrainDepth;
207
+ const nextDepth = DEPTHS[next - 1];
208
+ onBrainChange(nextDepth.state, 0.66 + next * 0.06);
209
+ if (next >= 2) triggerBrainRecall();
210
+ return next;
211
+ });
212
+ }
213
+
214
+ function surface() {
215
+ setExplorationDepth(1);
216
+ setSelectedGraphId(null);
217
+ setGraphSearch("");
218
+ onBrainChange("idle", 0.58);
219
+ }
220
+
221
+ function recallMemory(fragment: MemoryFragment) {
222
+ triggerBrainRecall();
223
+ setExplorationDepth((depth) => Math.max(depth, 2) as BrainDepth);
224
+ setMessages((items) => [
225
+ ...items,
226
+ { role: "assistant", content: `I am recalling ${fragment.kind.toLowerCase()}: ${fragment.title}` },
227
+ ]);
228
+ }
71
229
 
72
- if (!open) return null;
73
230
  return (
74
- <div className="command-scrim" role="dialog" aria-modal="true" aria-label="Lattice command palette">
75
- <div className="command-panel">
76
- <div className="command-search">
77
- <Search className="h-4 w-4 text-muted-foreground" />
78
- <Input value={query} onChange={(event) => setQuery(event.target.value)} autoFocus placeholder="Jump to anything in Lattice" />
79
- <Button variant="ghost" size="icon" onClick={onClose} aria-label="Close command palette"><X className="h-4 w-4" /></Button>
231
+ <main className="brain-home" aria-label="Lattice Brain">
232
+ <section className="brain-presence" aria-label="Brain exploration">
233
+ <div className="brain-exploration" data-depth={explorationDepth}>
234
+ <LivingBrain
235
+ state={brainState}
236
+ intensity={intensity + explorationDepth * 0.035}
237
+ size="large"
238
+ depth={explorationDepth}
239
+ showLabel={false}
240
+ onInteract={deepen}
241
+ />
242
+
243
+ <div className="brain-depth-badge" aria-live="polite">
244
+ <span>Level {explorationDepth}</span>
245
+ <strong>{currentDepth.label}</strong>
246
+ </div>
247
+
248
+ <div className="brain-field-layer" aria-hidden={explorationDepth < 2}>
249
+ <DepthEmergence
250
+ depth={explorationDepth}
251
+ memories={memoryFragments}
252
+ concepts={knowledgeConcepts}
253
+ relationships={relationshipThreads}
254
+ graphModel={graphModel}
255
+ graphSearch={graphSearch}
256
+ selectedGraphId={selectedGraphId}
257
+ onGraphSearch={setGraphSearch}
258
+ onSelectGraphNode={setSelectedGraphId}
259
+ onRecallMemory={recallMemory}
260
+ />
261
+ </div>
262
+
263
+ {explorationDepth > 1 ? (
264
+ <button className="brain-surface-control" type="button" onClick={surface}>
265
+ Surface
266
+ </button>
267
+ ) : null}
80
268
  </div>
81
- <div className="command-list soft-scrollbar">
82
- {matches.map((route) => {
83
- const Icon = route.icon;
84
- return (
85
- <button
86
- key={route.key}
87
- onClick={() => {
88
- go(route.key);
89
- onClose();
269
+ </section>
270
+
271
+ <section className="brain-conversation" aria-label="Conversation">
272
+ <div className="brain-conversation-header">
273
+ <div>
274
+ <h1>Lattice Brain</h1>
275
+ <span>{currentDepth.label}</span>
276
+ </div>
277
+ <div>{modelName}</div>
278
+ </div>
279
+
280
+ <div ref={streamRef} className="brain-stream">
281
+ {messages.length === 0 ? (
282
+ <div className="mind-empty">
283
+ <div className="mind-empty-kicker">Begin</div>
284
+ <div>What are you thinking about?</div>
285
+ </div>
286
+ ) : (
287
+ messages.map((message, index) => (
288
+ <div key={`${message.role}-${index}`} className={`brain-message ${message.role}`}>
289
+ <div className="brain-message-bubble">{message.content}</div>
290
+ </div>
291
+ ))
292
+ )}
293
+ </div>
294
+
295
+ <div className="brain-composer">
296
+ <textarea
297
+ value={draft}
298
+ onChange={(event) => setDraft(event.target.value)}
299
+ onKeyDown={(event) => {
300
+ if (event.key === "Enter" && !event.shiftKey) {
301
+ event.preventDefault();
302
+ void send();
303
+ }
304
+ }}
305
+ placeholder="Talk to your Brain..."
306
+ />
307
+ <div className="brain-composer-actions">
308
+ <label className="brain-image-input">
309
+ <ImagePlus className="h-3.5 w-3.5" />
310
+ <span>Image</span>
311
+ <input
312
+ type="file"
313
+ accept="image/*"
314
+ className="sr-only"
315
+ onChange={async (event) => {
316
+ const file = event.target.files?.[0];
317
+ if (file) setImageData(await fileToDataUrl(file));
90
318
  }}
91
- className="command-row"
92
- >
93
- <span className="command-icon"><Icon className="h-4 w-4" /></span>
94
- <span>
95
- <span className="block text-sm font-semibold">{route.label}</span>
96
- <span className="block text-xs text-muted-foreground">Open {route.key.replace(/[-/]/g, " ")}</span>
97
- </span>
98
- </button>
99
- );
100
- })}
319
+ />
320
+ </label>
321
+ {imageData ? <span className="brain-quiet-success">Image attached</span> : null}
322
+ <Button onClick={() => void send()} disabled={!draft.trim() || streaming} className="rounded-full px-5">
323
+ <Send className="h-4 w-4" /> Send
324
+ </Button>
325
+ </div>
101
326
  </div>
102
- </div>
103
- </div>
327
+ </section>
328
+ </main>
329
+ );
330
+ }
331
+
332
+ function DepthEmergence({
333
+ depth,
334
+ memories,
335
+ concepts,
336
+ relationships,
337
+ graphModel,
338
+ graphSearch,
339
+ selectedGraphId,
340
+ onGraphSearch,
341
+ onSelectGraphNode,
342
+ onRecallMemory,
343
+ }: {
344
+ depth: BrainDepth;
345
+ memories: MemoryFragment[];
346
+ concepts: KnowledgeConcept[];
347
+ relationships: RelationshipThread[];
348
+ graphModel: KnowledgeGraphModel;
349
+ graphSearch: string;
350
+ selectedGraphId: string | null;
351
+ onGraphSearch: (value: string) => void;
352
+ onSelectGraphNode: (id: string | null) => void;
353
+ onRecallMemory: (fragment: MemoryFragment) => void;
354
+ }) {
355
+ if (depth === 1) return null;
356
+
357
+ return (
358
+ <>
359
+ {depth >= 2 ? (
360
+ <MemoryLayer memories={memories} depth={depth} onRecallMemory={onRecallMemory} />
361
+ ) : null}
362
+ {depth >= 3 && depth < 5 ? (
363
+ <KnowledgeLayer concepts={concepts} depth={depth} />
364
+ ) : null}
365
+ {depth >= 4 && depth < 5 ? (
366
+ <RelationshipLayer concepts={concepts} relationships={relationships} />
367
+ ) : null}
368
+ {depth >= 5 ? (
369
+ <EmergentKnowledgeGraph
370
+ model={graphModel}
371
+ search={graphSearch}
372
+ selectedId={selectedGraphId}
373
+ onSearch={onGraphSearch}
374
+ onSelect={onSelectGraphNode}
375
+ />
376
+ ) : null}
377
+ </>
104
378
  );
105
379
  }
106
380
 
107
- function PrimaryDock({ active, onNavigate }: { active: PrimaryRoute; onNavigate?: () => void }) {
381
+ function MemoryLayer({
382
+ memories,
383
+ depth,
384
+ onRecallMemory,
385
+ }: {
386
+ memories: MemoryFragment[];
387
+ depth: BrainDepth;
388
+ onRecallMemory: (fragment: MemoryFragment) => void;
389
+ }) {
390
+ const visible = memories.slice(0, depth >= 3 ? 8 : 6);
391
+ if (!visible.length) return <div className="memory-fragment is-empty">Memory is quiet</div>;
392
+
108
393
  return (
109
- <nav className="primary-dock" aria-label="Primary navigation">
110
- {primaryRoutes.map((item) => {
111
- const Icon = item.icon;
112
- const selected = active === item.id;
394
+ <>
395
+ {visible.map((memory, index) => {
396
+ const point = polarPoint(index, visible.length, depth >= 3 ? 39 : 31, depth >= 3 ? 24 : 18, -112);
113
397
  return (
114
398
  <button
115
- key={item.id}
116
- className={cn("dock-button", selected && "is-active")}
117
- onClick={() => {
118
- go(item.id);
119
- onNavigate?.();
120
- }}
121
- aria-current={selected ? "page" : undefined}
399
+ key={memory.id}
400
+ type="button"
401
+ className="memory-fragment"
402
+ style={layerStyle({ "--x": `${point.x}%`, "--y": `${point.y}%`, "--delay": `${index * 55}ms` })}
403
+ onClick={() => onRecallMemory(memory)}
122
404
  >
123
- <Icon className="h-4 w-4" />
124
- <span>{item.label}</span>
405
+ <span>{memory.kind}</span>
406
+ <strong>{memory.title}</strong>
125
407
  </button>
126
408
  );
127
409
  })}
128
- </nav>
410
+ </>
129
411
  );
130
412
  }
131
413
 
132
- function ModeSwitch({ mode, setMode }: { mode: WorkspaceMode; setMode: (mode: WorkspaceMode) => void }) {
414
+ function KnowledgeLayer({ concepts, depth }: { concepts: KnowledgeConcept[]; depth: BrainDepth }) {
415
+ const visible = concepts.slice(0, depth >= 4 ? 10 : 7);
416
+ if (!visible.length) return <div className="concept-signal is-empty">Knowledge is forming</div>;
417
+
133
418
  return (
134
- <div className="mode-switch" aria-label="Experience mode">
135
- {modes.map((item) => (
136
- <button
137
- key={item.id}
138
- className={cn(mode === item.id && "is-active")}
139
- onClick={() => setMode(item.id)}
140
- aria-pressed={mode === item.id}
141
- >
142
- {item.label}
143
- </button>
144
- ))}
145
- </div>
419
+ <>
420
+ {visible.map((concept, index) => {
421
+ const point = polarPoint(index, visible.length, 24, 15, -70);
422
+ return (
423
+ <button
424
+ key={concept.id}
425
+ type="button"
426
+ className="concept-signal"
427
+ style={layerStyle({ "--x": `${point.x}%`, "--y": `${point.y}%`, "--delay": `${index * 45}ms` })}
428
+ title={concept.summary || concept.type}
429
+ >
430
+ <span>{concept.type}</span>
431
+ {concept.label}
432
+ </button>
433
+ );
434
+ })}
435
+ </>
146
436
  );
147
437
  }
148
438
 
149
- export default function App() {
150
- const route = useRoute();
151
- const { theme, setTheme, mode, setMode } = useAppStore();
152
- const [drawer, setDrawer] = React.useState(false);
153
- const [palette, setPalette] = React.useState(false);
154
- const health = useQuery({ queryKey: ["health"], queryFn: latticeApi.health });
155
- const desktop = useQuery({
156
- queryKey: ["desktopBackendStatus"],
157
- queryFn: latticeApi.desktopBackendStatus,
158
- enabled: Boolean(window.__TAURI_INTERNALS__),
159
- refetchInterval: 5000,
160
- });
161
- const workspace = useQuery({ queryKey: ["workspaceOs"], queryFn: latticeApi.workspaceOs });
439
+ function RelationshipLayer({
440
+ concepts,
441
+ relationships,
442
+ }: {
443
+ concepts: KnowledgeConcept[];
444
+ relationships: RelationshipThread[];
445
+ }) {
446
+ const visibleConcepts = concepts.slice(0, 10);
447
+ const layout = layoutGraphNodes(visibleConcepts, 30, 20);
448
+ const positionById = new Map(layout.map((item) => [item.node.id, item]));
449
+ const visibleRelationships = relationships
450
+ .map((relationship, index) => {
451
+ const source = positionById.get(relationship.source) || layout[index % Math.max(layout.length, 1)];
452
+ const target = positionById.get(relationship.target) || layout[(index + 3) % Math.max(layout.length, 1)];
453
+ return source && target && source.node.id !== target.node.id ? { relationship, source, target } : null;
454
+ })
455
+ .filter(Boolean)
456
+ .slice(0, 8) as Array<{
457
+ relationship: RelationshipThread;
458
+ source: ReturnType<typeof layoutGraphNodes>[number];
459
+ target: ReturnType<typeof layoutGraphNodes>[number];
460
+ }>;
162
461
 
163
- React.useEffect(() => {
164
- document.documentElement.dataset.theme = theme;
165
- }, [theme]);
462
+ if (!visibleRelationships.length) return null;
166
463
 
167
- React.useEffect(() => {
168
- const onKey = (event: KeyboardEvent) => {
169
- if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
170
- event.preventDefault();
171
- setPalette(true);
172
- }
173
- };
174
- window.addEventListener("keydown", onKey);
175
- return () => window.removeEventListener("keydown", onKey);
176
- }, []);
464
+ return (
465
+ <svg className="relationship-weave" viewBox="0 0 100 100" aria-hidden>
466
+ {visibleRelationships.map(({ relationship, source, target }, index) => (
467
+ <line
468
+ key={`${relationship.id}-${index}`}
469
+ x1={source.x}
470
+ y1={source.y}
471
+ x2={target.x}
472
+ y2={target.y}
473
+ style={{ animationDelay: `${index * 80}ms` }}
474
+ />
475
+ ))}
476
+ </svg>
477
+ );
478
+ }
177
479
 
178
- const healthData = (health.data?.data || {}) as Record<string, unknown>;
179
- const workspaceData = (workspace.data?.data || {}) as Record<string, unknown>;
180
- const desktopData = (desktop.data?.data || {}) as Record<string, unknown>;
181
- const appVersion = typeof healthData.version === "string" ? healthData.version : null;
182
- const activeRoute = primaryRoutes.find((item) => item.id === route.primary);
183
- const workspaceName = String(workspaceData.active_workspace || "Personal space");
184
- const backendReady = Boolean(health.data?.ok);
185
- const desktopReady = !window.__TAURI_INTERNALS__ || Boolean(desktopData.running);
480
+ function EmergentKnowledgeGraph({
481
+ model,
482
+ search,
483
+ selectedId,
484
+ onSearch,
485
+ onSelect,
486
+ }: {
487
+ model: KnowledgeGraphModel;
488
+ search: string;
489
+ selectedId: string | null;
490
+ onSearch: (value: string) => void;
491
+ onSelect: (id: string | null) => void;
492
+ }) {
493
+ const query = search.trim().toLowerCase();
494
+ const visibleNodes = React.useMemo(() => {
495
+ const filtered = model.nodes.filter((node) => {
496
+ if (!query) return true;
497
+ return `${node.label} ${node.type} ${node.summary}`.toLowerCase().includes(query);
498
+ });
499
+ return filtered.slice(0, 18);
500
+ }, [model.nodes, query]);
501
+ const layout = React.useMemo(() => layoutGraphNodes(visibleNodes, 38, 24), [visibleNodes]);
502
+ const positionById = React.useMemo(() => new Map(layout.map((item) => [item.node.id, item])), [layout]);
503
+ const visibleEdges = React.useMemo(
504
+ () => model.edges.filter((edge) => positionById.has(edge.source) && positionById.has(edge.target)).slice(0, 36),
505
+ [model.edges, positionById],
506
+ );
507
+ const selected = visibleNodes.find((node) => node.id === selectedId) || visibleNodes[0] || null;
186
508
 
187
509
  return (
188
- <div className="app-backdrop min-h-screen text-foreground">
189
- <AmbientBrain />
190
- <CommandPalette open={palette} onClose={() => setPalette(false)} />
191
-
192
- <header className="app-chrome">
193
- <div className="brand-lockup">
194
- <button className="mobile-menu" onClick={() => setDrawer(true)} aria-label="Open navigation"><Menu className="h-5 w-5" /></button>
195
- <button className="brand-mark" onClick={() => go("brain")} aria-label="Open Lattice home">
196
- <BrainCircuit className="h-5 w-5" />
197
- </button>
198
- <div className="brand-copy">
199
- <div className="brand-name">Lattice</div>
200
- <div className="brand-subtitle">Digital Brain</div>
201
- </div>
510
+ <section className="mind-core-graph" data-testid="emergent-knowledge-graph" aria-label="Knowledge Graph">
511
+ <div className="brain-graph-head">
512
+ <div>
513
+ <span>Level 5</span>
514
+ <strong>Knowledge Graph</strong>
202
515
  </div>
516
+ <label className="brain-graph-search">
517
+ <Search className="h-3.5 w-3.5" />
518
+ <input
519
+ value={search}
520
+ onChange={(event) => onSearch(event.target.value)}
521
+ placeholder="Search"
522
+ aria-label="Search knowledge graph"
523
+ />
524
+ </label>
525
+ </div>
203
526
 
204
- <div className="desktop-dock">
205
- <PrimaryDock active={route.primary} />
527
+ {visibleNodes.length ? (
528
+ <div className="brain-graph-canvas">
529
+ <svg className="brain-graph-edges" viewBox="0 0 100 100" aria-hidden>
530
+ {visibleEdges.map((edge, index) => {
531
+ const source = positionById.get(edge.source);
532
+ const target = positionById.get(edge.target);
533
+ if (!source || !target) return null;
534
+ return (
535
+ <line
536
+ key={`${edge.id}-${index}`}
537
+ x1={source.x}
538
+ y1={source.y}
539
+ x2={target.x}
540
+ y2={target.y}
541
+ style={{ "--weight": String(clamp(edge.weight, 0.4, 2.8)) } as React.CSSProperties}
542
+ />
543
+ );
544
+ })}
545
+ </svg>
546
+ {layout.map(({ node, x, y }, index) => (
547
+ <button
548
+ key={node.id}
549
+ type="button"
550
+ className={`graph-node ${selected?.id === node.id ? "is-selected" : ""}`}
551
+ style={layerStyle({ "--x": `${x}%`, "--y": `${y}%`, "--delay": `${index * 35}ms` })}
552
+ onClick={() => onSelect(node.id)}
553
+ >
554
+ <span>{node.type}</span>
555
+ {node.label}
556
+ </button>
557
+ ))}
206
558
  </div>
559
+ ) : (
560
+ <div className="brain-graph-empty">No matching knowledge yet</div>
561
+ )}
207
562
 
208
- <div className="chrome-actions">
209
- <button className="status-chip" onClick={() => go("settings")}>
210
- <span className={cn("status-light", backendReady && desktopReady ? "is-ready" : "is-waiting")} />
211
- <span>{backendReady && desktopReady ? "Ready" : "Starting"}</span>
212
- </button>
213
- <Button variant="outline" onClick={() => setPalette(true)}><Command className="h-4 w-4" /> Find</Button>
214
- <Button variant="outline" size="icon" onClick={() => setTheme(theme === "dark" ? "light" : "dark")} aria-label="Toggle theme">
215
- {theme === "dark" ? <Sun className="h-4 w-4" /> : <Moon className="h-4 w-4" />}
216
- </Button>
217
- </div>
218
- </header>
219
-
220
- {drawer ? (
221
- <div className="mobile-drawer">
222
- <button className="drawer-scrim" aria-label="Close navigation" onClick={() => setDrawer(false)} />
223
- <div className="drawer-panel">
224
- <div className="drawer-header">
225
- <div>
226
- <div className="font-semibold">Lattice</div>
227
- <div className="text-xs text-muted-foreground">Choose a room</div>
228
- </div>
229
- <Button variant="ghost" size="icon" onClick={() => setDrawer(false)} aria-label="Close navigation"><X className="h-4 w-4" /></Button>
230
- </div>
231
- <PrimaryDock active={route.primary} onNavigate={() => setDrawer(false)} />
232
- </div>
233
- </div>
234
- ) : null}
563
+ <div className="brain-graph-focus">
564
+ {selected ? (
565
+ <>
566
+ <span>{selected.type}</span>
567
+ <strong>{selected.label}</strong>
568
+ <p>{selected.summary || "This concept is part of the deepest knowledge layer."}</p>
569
+ </>
570
+ ) : (
571
+ <p>Capture documents, conversations, or projects to grow the graph.</p>
572
+ )}
573
+ </div>
574
+ </section>
575
+ );
576
+ }
235
577
 
236
- <main className="page-shell">
237
- <section className="workspace-ribbon" aria-label="Current workspace">
238
- <div className="min-w-0">
239
- <div className="ribbon-kicker"><Sparkles className="h-4 w-4" /> {activeRoute?.label || "Home"}</div>
240
- <h1>{activeRoute?.description || "A calm place to think with your knowledge."}</h1>
241
- </div>
242
- <div className="ribbon-meta">
243
- <div className="meta-card">
244
- <CheckCircle2 className="h-4 w-4 text-primary" />
245
- <span>{workspaceName}</span>
246
- </div>
247
- <div className="meta-card">
248
- <span>{appVersion ? `v${appVersion}` : "Version checking"}</span>
249
- </div>
250
- <ModeSwitch mode={mode} setMode={setMode} />
251
- </div>
252
- </section>
578
+ function buildMemoryFragments(memoryData: unknown, historyData: unknown): MemoryFragment[] {
579
+ const memory = isRecord(memoryData) ? memoryData : {};
580
+ const sourceRows = asArray<ApiRecord>(memory.sources).length
581
+ ? asArray<ApiRecord>(memory.sources)
582
+ : asArray<ApiRecord>(memory.tiers);
583
+ const sourceFragments = sourceRows.map((item, index) => ({
584
+ id: textValue(item, ["id", "source", "label"], `memory-${index}`),
585
+ title: textValue(item, ["title", "label", "source", "path", "name"], "Workspace memory"),
586
+ kind: titleValue(item, ["type", "source_type", "kind", "health"], "Memory"),
587
+ }));
588
+ const conversationFragments = asArray<ApiRecord>(historyData).map((item, index) => ({
589
+ id: textValue(item, ["id", "conversation_id"], `conversation-${index}`),
590
+ title: textValue(item, ["title", "summary", "id"], "Conversation"),
591
+ kind: "Conversation",
592
+ }));
253
593
 
254
- <FirstRunGuide />
255
- <Page primary={route.primary} tab={route.tab} />
256
- </main>
257
- </div>
258
- );
594
+ return uniqueById([...sourceFragments, ...conversationFragments]).slice(0, 10);
595
+ }
596
+
597
+ function parseKnowledgeGraph(data: unknown): KnowledgeGraphModel {
598
+ const graph = isRecord(data) ? data : {};
599
+ const rawNodes = asArray<ApiRecord>(graph.nodes);
600
+ const rawEdges = asArray<ApiRecord>(graph.edges);
601
+ const nodes = rawNodes.flatMap((node): KnowledgeConcept[] => {
602
+ const id = textValue(node, ["id", "node_id", "title", "label"]);
603
+ if (!id) return [];
604
+ const metadata = isRecord(node.metadata) ? node.metadata : {};
605
+ const type = titleValue(node, ["type", "kind", "category"], "Concept");
606
+ const label = textValue(node, ["title", "label", "name"], id.replace(/^[^:]+:/, ""));
607
+ const summary = textValue(node, ["summary", "description", "snippet"]) || textValue(metadata, ["summary", "description", "relative_path", "filename"]);
608
+ const importance = clamp(numberValue(node, ["importance_norm", "importance", "score"]) || 0.5, 0.08, 1);
609
+ return [{ id, label, type, summary, importance }];
610
+ }).sort((left, right) => right.importance - left.importance);
611
+ const ids = new Set(nodes.map((node) => node.id));
612
+ const edges = rawEdges.flatMap((edge, index): RelationshipThread[] => {
613
+ const source = textValue(edge, ["from", "source", "source_id"]);
614
+ const target = textValue(edge, ["to", "target", "target_id"]);
615
+ if (!source || !target || !ids.has(source) || !ids.has(target)) return [];
616
+ return [{
617
+ id: textValue(edge, ["id"], `edge-${index}`),
618
+ source,
619
+ target,
620
+ label: titleValue(edge, ["type", "label", "relationship"], "Relates"),
621
+ weight: numberValue(edge, ["weight", "score", "confidence"]) || 1,
622
+ }];
623
+ });
624
+ return { nodes, edges };
625
+ }
626
+
627
+ function currentModelName(data: unknown) {
628
+ const record = isRecord(data) ? data : {};
629
+ const current = textValue(record, ["current", "current_model", "local_model"]);
630
+ if (current) return current;
631
+ const loaded = asArray<ApiRecord>(record.loaded || record.loaded_models);
632
+ const firstLoaded = loaded.find((item) => item.id || item.name || item.model_id);
633
+ return firstLoaded ? textValue(firstLoaded, ["name", "id", "model_id"], "local mind") : "local mind";
634
+ }
635
+
636
+ function fileToDataUrl(file: File) {
637
+ return new Promise<string>((resolve, reject) => {
638
+ const reader = new FileReader();
639
+ reader.onload = () => resolve(String(reader.result || ""));
640
+ reader.onerror = () => reject(reader.error);
641
+ reader.readAsDataURL(file);
642
+ });
643
+ }
644
+
645
+ function layoutGraphNodes(nodes: KnowledgeConcept[], radiusX: number, radiusY: number) {
646
+ return nodes.map((node, index) => {
647
+ const point = polarPoint(index, nodes.length, radiusX, radiusY, -88);
648
+ return { node, x: point.x, y: point.y };
649
+ });
650
+ }
651
+
652
+ function polarPoint(index: number, total: number, radiusX: number, radiusY: number, offsetDegrees = -90) {
653
+ const count = Math.max(total, 1);
654
+ const angle = ((360 / count) * index + offsetDegrees) * Math.PI / 180;
655
+ return {
656
+ x: 50 + Math.cos(angle) * radiusX,
657
+ y: 50 + Math.sin(angle) * radiusY,
658
+ };
659
+ }
660
+
661
+ function layerStyle(values: Record<string, string>) {
662
+ return values as React.CSSProperties;
663
+ }
664
+
665
+ function uniqueById<T extends { id: string }>(items: T[]) {
666
+ const seen = new Set<string>();
667
+ return items.filter((item) => {
668
+ if (seen.has(item.id)) return false;
669
+ seen.add(item.id);
670
+ return true;
671
+ });
672
+ }
673
+
674
+ function isRecord(value: unknown): value is ApiRecord {
675
+ return Boolean(value && typeof value === "object" && !Array.isArray(value));
676
+ }
677
+
678
+ function textValue(record: ApiRecord, keys: string[], fallback = "") {
679
+ for (const key of keys) {
680
+ const value = record[key];
681
+ if (typeof value === "string" && value.trim()) return value;
682
+ if (typeof value === "number" && Number.isFinite(value)) return String(value);
683
+ }
684
+ return fallback;
685
+ }
686
+
687
+ function titleValue(record: ApiRecord, keys: string[], fallback = "") {
688
+ const value = textValue(record, keys, fallback);
689
+ return value
690
+ .replace(/[_-]+/g, " ")
691
+ .replace(/\b\w/g, (character) => character.toUpperCase());
692
+ }
693
+
694
+ function numberValue(record: ApiRecord, keys: string[]) {
695
+ for (const key of keys) {
696
+ const value = Number(record[key]);
697
+ if (Number.isFinite(value)) return value;
698
+ }
699
+ return 0;
700
+ }
701
+
702
+ function clamp(value: number, min: number, max: number) {
703
+ return Math.max(min, Math.min(max, value));
259
704
  }