forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseKnowledgeGraph.ts
More file actions
198 lines (173 loc) · 5.64 KB
/
Copy pathuseKnowledgeGraph.ts
File metadata and controls
198 lines (173 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use client';
import { useState, useEffect, useCallback, useMemo, useRef } from 'react';
import type { KnowledgeGraph, KnowledgeGraphNode, KnowledgeGraphEdge, KnowledgeGraphNodeType } from '@/types/conversation';
import { getKnowledgeGraph } from '@/lib/api';
// Node colors matching mobile app
export const NODE_COLORS: Record<KnowledgeGraphNodeType | 'user', string> = {
person: '#00FFFF', // Cyan
place: '#00FF9D', // Green
organization: '#FFA500', // Orange
thing: '#A855F7', // Purple
concept: '#3B82F6', // Blue
user: '#FFFFFF', // White (center node)
};
// Graph data format for react-force-graph
export interface GraphData {
nodes: GraphNode[];
links: GraphLink[];
}
export interface GraphNode {
id: string;
label: string;
nodeType: KnowledgeGraphNodeType | 'user';
color: string;
aliases: string[];
memoryIds: string[];
val: number; // Node size
// Position properties added by force-graph during simulation
x?: number;
y?: number;
z?: number;
}
export interface GraphLink {
id: string;
source: string;
target: string;
label: string;
memoryIds: string[];
}
export interface UseKnowledgeGraphReturn {
graphData: GraphData | null;
loading: boolean;
error: string | null;
selectedNode: GraphNode | null;
// Actions
refresh: () => Promise<void>;
selectNode: (node: GraphNode | null) => void;
// Helpers
getNodeById: (id: string) => GraphNode | undefined;
getConnectedNodes: (nodeId: string) => GraphNode[];
getNodeMemoryCount: (nodeId: string) => number;
}
export function useKnowledgeGraph(): UseKnowledgeGraphReturn {
const [rawData, setRawData] = useState<KnowledgeGraph | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [selectedNode, setSelectedNode] = useState<GraphNode | null>(null);
const initialFetchDone = useRef(false);
// Transform raw data to graph format
const graphData = useMemo((): GraphData | null => {
if (!rawData) return null;
// Create user node as the center - make it large and white
const userNode: GraphNode = {
id: 'user',
label: 'You',
nodeType: 'user',
color: '#FFFFFF', // Pure white
aliases: [],
memoryIds: [],
val: 40, // Much larger size for center node
};
// Transform nodes
const nodes: GraphNode[] = [
userNode,
...rawData.nodes.map((node): GraphNode => ({
id: node.id,
label: node.label,
nodeType: node.node_type,
color: NODE_COLORS[node.node_type] || NODE_COLORS.thing,
aliases: node.aliases,
memoryIds: node.memory_ids,
val: Math.max(3, Math.min(10, node.memory_ids.length * 2)), // Size based on memory count
})),
];
// Transform edges/links
const links: GraphLink[] = rawData.edges.map((edge): GraphLink => ({
id: edge.id,
source: edge.source_id,
target: edge.target_id,
label: edge.label,
memoryIds: edge.memory_ids,
}));
// Add links from user to all nodes (so they cluster around the center)
const nodesWithConnections = new Set(links.flatMap((l) => [l.source, l.target]));
// For nodes without connections, link them directly to user
rawData.nodes.forEach((node) => {
if (!nodesWithConnections.has(node.id)) {
links.push({
id: `user-${node.id}`,
source: 'user',
target: node.id,
label: 'related to',
memoryIds: node.memory_ids,
});
}
});
return { nodes, links };
}, [rawData]);
// Fetch graph data
const fetchGraph = useCallback(async () => {
try {
setLoading(true);
setError(null);
const data = await getKnowledgeGraph();
setRawData(data);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load knowledge graph');
} finally {
setLoading(false);
}
}, []);
// Initial fetch
useEffect(() => {
if (!initialFetchDone.current) {
initialFetchDone.current = true;
fetchGraph();
}
}, [fetchGraph]);
// Refresh
const refresh = useCallback(async () => {
initialFetchDone.current = true;
await fetchGraph();
}, [fetchGraph]);
// Select node
const selectNode = useCallback((node: GraphNode | null) => {
setSelectedNode(node);
}, []);
// Get node by ID
const getNodeById = useCallback((id: string): GraphNode | undefined => {
return graphData?.nodes.find((n) => n.id === id);
}, [graphData]);
// Get connected nodes
const getConnectedNodes = useCallback((nodeId: string): GraphNode[] => {
if (!graphData) return [];
const connectedIds = new Set<string>();
graphData.links.forEach((link) => {
if (link.source === nodeId || (typeof link.source === 'object' && (link.source as GraphNode).id === nodeId)) {
const targetId = typeof link.target === 'object' ? (link.target as GraphNode).id : link.target;
connectedIds.add(targetId);
}
if (link.target === nodeId || (typeof link.target === 'object' && (link.target as GraphNode).id === nodeId)) {
const sourceId = typeof link.source === 'object' ? (link.source as GraphNode).id : link.source;
connectedIds.add(sourceId);
}
});
return graphData.nodes.filter((n) => connectedIds.has(n.id));
}, [graphData]);
// Get memory count for node
const getNodeMemoryCount = useCallback((nodeId: string): number => {
const node = getNodeById(nodeId);
return node?.memoryIds.length || 0;
}, [getNodeById]);
return {
graphData,
loading,
error,
selectedNode,
refresh,
selectNode,
getNodeById,
getConnectedNodes,
getNodeMemoryCount,
};
}