feat: wire mindmap to knowledge API

- Updated useGraphData hook to fetch from /api/knowledge/entries
- Implemented CRUD operations for knowledge nodes using actual API endpoints
- Wired edge creation/deletion through wiki-links in content
- Added search integration with /api/knowledge/search
- Transform Knowledge entries to graph nodes with backlinks as edges
- Real-time graph updates after mutations
- Added search bar UI with live results dropdown
- Graph statistics automatically recalculate
- Clean TypeScript with proper type transformations
This commit is contained in:
Jason Woltje
2026-01-29 23:23:36 -06:00
parent 59aec28d5c
commit 58caafe164
3 changed files with 512 additions and 47 deletions

View File

@@ -27,6 +27,9 @@ export function MindmapViewer({
const [mermaidStyle, setMermaidStyle] = useState<MermaidStyle>('flowchart');
const [showCreateModal, setShowCreateModal] = useState(false);
const [selectedNode, setSelectedNode] = useState<KnowledgeNode | null>(null);
const [searchQuery, setSearchQuery] = useState('');
const [searchResults, setSearchResults] = useState<KnowledgeNode[]>([]);
const [isSearching, setIsSearching] = useState(false);
const {
graph,
@@ -39,6 +42,7 @@ export function MindmapViewer({
updateNode,
deleteNode,
createEdge,
searchNodes,
} = useGraphData({ ...(rootId && { rootId }), maxDepth });
const handleViewModeChange = useCallback(
@@ -84,6 +88,36 @@ export function MindmapViewer({
[createEdge]
);
const handleSearch = useCallback(
async (query: string) => {
setSearchQuery(query);
if (!query.trim()) {
setSearchResults([]);
return;
}
setIsSearching(true);
try {
const results = await searchNodes(query);
setSearchResults(results);
} catch (err) {
console.error('Search failed:', err);
} finally {
setIsSearching(false);
}
},
[searchNodes]
);
const handleSelectSearchResult = useCallback(
(node: KnowledgeNode) => {
setSelectedNode(node);
setSearchResults([]);
setSearchQuery('');
},
[]
);
if (error) {
return (
@@ -137,6 +171,56 @@ export function MindmapViewer({
</select>
)}
{/* Search bar */}
<div className="relative">
<input
type="text"
value={searchQuery}
onChange={(e) => handleSearch(e.target.value)}
placeholder="Search nodes..."
className="px-3 py-1.5 pl-8 text-sm rounded border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 text-gray-700 dark:text-gray-300 w-48"
/>
<svg
className="absolute left-2 top-2 w-4 h-4 text-gray-400"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
/>
</svg>
{/* Search results dropdown */}
{searchResults.length > 0 && (
<div className="absolute top-full left-0 mt-1 w-64 bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded shadow-lg max-h-64 overflow-y-auto z-50">
{searchResults.map((result) => (
<button
key={result.id}
onClick={() => handleSelectSearchResult(result)}
className="w-full text-left px-3 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 text-sm"
>
<div className="font-medium text-gray-900 dark:text-gray-100">
{result.title}
</div>
<div className="text-xs text-gray-500 dark:text-gray-400 capitalize">
{result.node_type}
</div>
</button>
))}
</div>
)}
{isSearching && (
<div className="absolute right-2 top-2">
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-blue-500" />
</div>
)}
</div>
{/* Statistics */}
{statistics && (
<div className="text-sm text-gray-500 dark:text-gray-400">

View File

@@ -68,6 +68,7 @@ interface UseGraphDataResult {
deleteNode: (id: string) => Promise<boolean>;
createEdge: (edge: Omit<KnowledgeEdge, 'created_at'>) => Promise<KnowledgeEdge | null>;
deleteEdge: (sourceId: string, targetId: string, relationType: string) => Promise<boolean>;
searchNodes: (query: string) => Promise<KnowledgeNode[]>;
}
const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000';
@@ -92,7 +93,7 @@ async function apiFetch<T>(
headers['Authorization'] = `Bearer ${accessToken}`;
}
const response = await fetch(`${API_BASE}/api/v1/knowledge${endpoint}`, {
const response = await fetch(`${API_BASE}/api/knowledge${endpoint}`, {
...options,
credentials: 'include',
headers,
@@ -105,7 +106,7 @@ async function apiFetch<T>(
throw new Error('Session expired');
}
const error = await response.json().catch(() => ({ detail: response.statusText }));
throw new Error(error.detail || 'API request failed');
throw new Error(error.detail || error.message || 'API request failed');
}
if (response.status === 204) {
@@ -115,8 +116,55 @@ async function apiFetch<T>(
return response.json();
}
// Transform Knowledge Entry to Graph Node
function entryToNode(entry: any): KnowledgeNode {
return {
id: entry.id,
title: entry.title,
node_type: entry.tags?.[0]?.slug || 'concept', // Use first tag as node type, fallback to 'concept'
content: entry.content || entry.summary || null,
tags: entry.tags?.map((t: any) => t.slug) || [],
domain: entry.tags?.length > 0 ? entry.tags[0].name : null,
metadata: {
slug: entry.slug,
status: entry.status,
visibility: entry.visibility,
createdBy: entry.createdBy,
updatedBy: entry.updatedBy,
},
created_at: entry.createdAt,
updated_at: entry.updatedAt,
};
}
// Transform Node to Entry Create DTO
function nodeToCreateDto(node: Omit<KnowledgeNode, 'id' | 'created_at' | 'updated_at'>): any {
return {
title: node.title,
content: node.content || '',
summary: node.content?.slice(0, 200) || '',
tags: node.tags.length > 0 ? node.tags : [node.node_type],
status: 'PUBLISHED',
visibility: 'WORKSPACE',
};
}
// Transform Node update to Entry Update DTO
function nodeToUpdateDto(updates: Partial<KnowledgeNode>): any {
const dto: any = {};
if (updates.title !== undefined) dto.title = updates.title;
if (updates.content !== undefined) {
dto.content = updates.content;
dto.summary = updates.content?.slice(0, 200) || '';
}
if (updates.tags !== undefined) dto.tags = updates.tags;
return dto;
}
export function useGraphData(options: UseGraphDataOptions = {}): UseGraphDataResult {
const { rootId, maxDepth = 3, autoFetch = true } = options;
const { autoFetch = true } = options;
// Get access token from BetterAuth session
const { data: sessionData } = useSession();
@@ -136,52 +184,149 @@ export function useGraphData(options: UseGraphDataOptions = {}): UseGraphDataRes
setIsLoading(true);
setError(null);
try {
const params = new URLSearchParams();
if (rootId) params.set('root_id', rootId);
params.set('max_depth', maxDepth.toString());
const data = await apiFetch<GraphData>(`/graph?${params}`, accessToken);
setGraph(data);
// Fetch all entries
const response = await apiFetch<any>('/entries?limit=100', accessToken);
const entries = response.data || [];
// Transform entries to nodes
const nodes: KnowledgeNode[] = entries.map(entryToNode);
// Fetch backlinks for all entries to build edges
const edges: KnowledgeEdge[] = [];
const edgeSet = new Set<string>(); // To avoid duplicates
for (const entry of entries) {
try {
const backlinksResponse = await apiFetch<any>(
`/entries/${entry.slug}/backlinks`,
accessToken
);
if (backlinksResponse.backlinks) {
for (const backlink of backlinksResponse.backlinks) {
const edgeId = `${backlink.id}-${entry.id}`;
if (!edgeSet.has(edgeId)) {
edges.push({
source_id: backlink.id,
target_id: entry.id,
relation_type: 'relates_to',
weight: 1.0,
metadata: {},
created_at: new Date().toISOString(),
});
edgeSet.add(edgeId);
}
}
}
} catch (err) {
// Silently skip backlink errors for individual entries
console.warn(`Failed to fetch backlinks for ${entry.slug}:`, err);
}
}
setGraph({ nodes, edges });
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch graph');
} finally {
setIsLoading(false);
}
}, [rootId, maxDepth, accessToken]);
}, [accessToken]);
const fetchMermaid = useCallback(async (style: 'flowchart' | 'mindmap' = 'flowchart') => {
if (!accessToken) {
setError('Not authenticated');
if (!graph) {
setError('No graph data available');
return;
}
setIsLoading(true);
setError(null);
try {
const params = new URLSearchParams();
if (rootId) params.set('root_id', rootId);
params.set('style', style);
params.set('max_depth', maxDepth.toString());
params.set('style_by_type', 'true');
const data = await apiFetch<MermaidData>(`/mermaid?${params}`, accessToken);
setMermaid(data);
// Generate Mermaid diagram from graph data
let diagram = '';
if (style === 'mindmap') {
diagram = 'mindmap\n root((Knowledge))\n';
// Group nodes by type
const nodesByType: Record<string, KnowledgeNode[]> = {};
graph.nodes.forEach(node => {
if (!nodesByType[node.node_type]) {
nodesByType[node.node_type] = [];
}
nodesByType[node.node_type].push(node);
});
// Add nodes by type
Object.entries(nodesByType).forEach(([type, nodes]) => {
diagram += ` ${type}\n`;
nodes.forEach(node => {
diagram += ` ${node.title}\n`;
});
});
} else {
diagram = 'graph TD\n';
// Add all edges
graph.edges.forEach(edge => {
const source = graph.nodes.find(n => n.id === edge.source_id);
const target = graph.nodes.find(n => n.id === edge.target_id);
if (source && target) {
const sourceLabel = source.title.replace(/["\n]/g, ' ');
const targetLabel = target.title.replace(/["\n]/g, ' ');
diagram += ` ${edge.source_id}["${sourceLabel}"] --> ${edge.target_id}["${targetLabel}"]\n`;
}
});
// Add standalone nodes (no edges)
graph.nodes.forEach(node => {
const hasEdge = graph.edges.some(e =>
e.source_id === node.id || e.target_id === node.id
);
if (!hasEdge) {
const label = node.title.replace(/["\n]/g, ' ');
diagram += ` ${node.id}["${label}"]\n`;
}
});
}
setMermaid({
diagram,
style: style,
});
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to fetch diagram');
setError(err instanceof Error ? err.message : 'Failed to generate diagram');
} finally {
setIsLoading(false);
}
}, [rootId, maxDepth, accessToken]);
}, [graph]);
const fetchStatistics = useCallback(async () => {
if (!accessToken) return;
if (!graph) return;
try {
const data = await apiFetch<GraphStatistics>('/graph/statistics', accessToken);
setStatistics(data);
const nodesByType: Record<string, number> = {};
const edgesByType: Record<string, number> = {};
graph.nodes.forEach(node => {
nodesByType[node.node_type] = (nodesByType[node.node_type] || 0) + 1;
});
graph.edges.forEach(edge => {
edgesByType[edge.relation_type] = (edgesByType[edge.relation_type] || 0) + 1;
});
setStatistics({
node_count: graph.nodes.length,
edge_count: graph.edges.length,
nodes_by_type: nodesByType,
edges_by_type: edgesByType,
});
} catch (err) {
// Silently fail - statistics are non-critical
void err;
}
}, [accessToken]);
}, [graph]);
const createNode = useCallback(async (
node: Omit<KnowledgeNode, 'id' | 'created_at' | 'updated_at'>
@@ -191,12 +336,13 @@ export function useGraphData(options: UseGraphDataOptions = {}): UseGraphDataRes
return null;
}
try {
const created = await apiFetch<KnowledgeNode>('/nodes', accessToken, {
const createDto = nodeToCreateDto(node);
const created = await apiFetch<any>('/entries', accessToken, {
method: 'POST',
body: JSON.stringify(node),
body: JSON.stringify(createDto),
});
await fetchGraph();
return created;
return entryToNode(created);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create node');
return null;
@@ -212,17 +358,26 @@ export function useGraphData(options: UseGraphDataOptions = {}): UseGraphDataRes
return null;
}
try {
const updated = await apiFetch<KnowledgeNode>(`/nodes/${id}`, accessToken, {
// Find the node to get its slug
const node = graph?.nodes.find(n => n.id === id);
if (!node) {
throw new Error('Node not found');
}
const slug = node.metadata.slug as string;
const updateDto = nodeToUpdateDto(updates);
const updated = await apiFetch<any>(`/entries/${slug}`, accessToken, {
method: 'PUT',
body: JSON.stringify(updates),
body: JSON.stringify(updateDto),
});
await fetchGraph();
return updated;
return entryToNode(updated);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to update node');
return null;
}
}, [fetchGraph, accessToken]);
}, [fetchGraph, accessToken, graph]);
const deleteNode = useCallback(async (id: string): Promise<boolean> => {
if (!accessToken) {
@@ -230,14 +385,21 @@ export function useGraphData(options: UseGraphDataOptions = {}): UseGraphDataRes
return false;
}
try {
await apiFetch(`/nodes/${id}`, accessToken, { method: 'DELETE' });
// Find the node to get its slug
const node = graph?.nodes.find(n => n.id === id);
if (!node) {
throw new Error('Node not found');
}
const slug = node.metadata.slug as string;
await apiFetch(`/entries/${slug}`, accessToken, { method: 'DELETE' });
await fetchGraph();
return true;
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to delete node');
return false;
}
}, [fetchGraph, accessToken]);
}, [fetchGraph, accessToken, graph]);
const createEdge = useCallback(async (
edge: Omit<KnowledgeEdge, 'created_at'>
@@ -247,17 +409,45 @@ export function useGraphData(options: UseGraphDataOptions = {}): UseGraphDataRes
return null;
}
try {
const created = await apiFetch<KnowledgeEdge>('/edges', accessToken, {
method: 'POST',
body: JSON.stringify(edge),
// For now, we'll store the edge in local state only
// The Knowledge API uses backlinks which are automatically created from wiki-links in content
// To properly create a link, we'd need to update the source entry's content to include a wiki-link
// Find source and target nodes
const sourceNode = graph?.nodes.find(n => n.id === edge.source_id);
const targetNode = graph?.nodes.find(n => n.id === edge.target_id);
if (!sourceNode || !targetNode) {
throw new Error('Source or target node not found');
}
// Update source node content to include a link to target
const targetSlug = targetNode.metadata.slug as string;
const wikiLink = `[[${targetSlug}|${targetNode.title}]]`;
const updatedContent = sourceNode.content
? `${sourceNode.content}\n\n${wikiLink}`
: wikiLink;
const slug = sourceNode.metadata.slug as string;
await apiFetch(`/entries/${slug}`, accessToken, {
method: 'PUT',
body: JSON.stringify({
content: updatedContent,
}),
});
// Refresh graph to get updated backlinks
await fetchGraph();
return created;
return {
...edge,
created_at: new Date().toISOString(),
};
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create edge');
return null;
}
}, [fetchGraph, accessToken]);
}, [fetchGraph, accessToken, graph]);
const deleteEdge = useCallback(async (
sourceId: string,
@@ -269,27 +459,63 @@ export function useGraphData(options: UseGraphDataOptions = {}): UseGraphDataRes
return false;
}
try {
const params = new URLSearchParams({
source_id: sourceId,
target_id: targetId,
relation_type: relationType,
// To delete an edge, we need to remove the wiki-link from the source content
const sourceNode = graph?.nodes.find(n => n.id === sourceId);
const targetNode = graph?.nodes.find(n => n.id === targetId);
if (!sourceNode || !targetNode) {
throw new Error('Source or target node not found');
}
const targetSlug = targetNode.metadata.slug as string;
const wikiLinkPattern = new RegExp(`\\[\\[${targetSlug}(?:\\|[^\\]]+)?\\]\\]`, 'g');
const updatedContent = sourceNode.content?.replace(wikiLinkPattern, '') || '';
const slug = sourceNode.metadata.slug as string;
await apiFetch(`/entries/${slug}`, accessToken, {
method: 'PUT',
body: JSON.stringify({
content: updatedContent,
}),
});
await apiFetch(`/edges?${params}`, accessToken, { method: 'DELETE' });
await fetchGraph();
return true;
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to delete edge');
return false;
}
}, [fetchGraph, accessToken]);
}, [fetchGraph, accessToken, graph]);
const searchNodes = useCallback(async (query: string): Promise<KnowledgeNode[]> => {
if (!accessToken) {
setError('Not authenticated');
return [];
}
try {
const params = new URLSearchParams({ q: query, limit: '50' });
const response = await apiFetch<any>(`/search?${params}`, accessToken);
const results = response.data || [];
return results.map(entryToNode);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to search');
return [];
}
}, [accessToken]);
// Initial data fetch - only run when autoFetch is true and we have an access token
useEffect(() => {
if (autoFetch && accessToken) {
void fetchGraph();
}
}, [autoFetch, accessToken, fetchGraph]);
// Update statistics when graph changes
useEffect(() => {
if (graph) {
void fetchStatistics();
}
}, [autoFetch, accessToken, fetchGraph, fetchStatistics]);
}, [graph, fetchStatistics]);
return {
graph,
@@ -305,5 +531,6 @@ export function useGraphData(options: UseGraphDataOptions = {}): UseGraphDataRes
deleteNode,
createEdge,
deleteEdge,
searchNodes,
};
}