Skip to content

Commit cfa2e0b

Browse files
authored
Merge pull request #5 from supermemoryai/use-graph-package
use published graph package
2 parents 0fdabf1 + cf0e073 commit cfa2e0b

21 files changed

+685
-4893
lines changed

app/(chat)/api/chat/route.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { streamText, convertToModelMessages, stepCountIs } from 'ai';
1+
import {
2+
streamText,
3+
convertToModelMessages,
4+
stepCountIs,
5+
type ToolSet,
6+
} from 'ai';
27
import { withSupermemory } from '@supermemory/tools/ai-sdk';
38
import { auth, type UserType } from '@/app/(auth)/auth';
49
import { type RequestHints, systemPrompt } from '@/lib/ai/prompts';
@@ -213,7 +218,7 @@ export async function POST(request: Request) {
213218
const toolsConfig = {
214219
searchMemories: memoryTools.searchMemories,
215220
webSearch: webSearchTool,
216-
};
221+
} as ToolSet;
217222

218223
// Log what messages we're sending to AI SDK
219224
const convertedMessages = convertToModelMessages(messages as any);

bun.lock

Lines changed: 524 additions & 715 deletions
Large diffs are not rendered by default.

components/memory-graph-dialog.tsx

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
'use client';
1+
"use client";
22

3-
import { useState, useCallback } from 'react';
4-
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog';
5-
import { MemoryGraph } from '@/lib/ui/memory-graph';
6-
import { Button } from '@/components/ui/button';
7-
import { Network } from 'lucide-react';
8-
import type { DocumentWithMemories } from '@/lib/types/supermemory';
3+
import { useState, useCallback } from "react";
4+
import {
5+
Dialog,
6+
DialogContent,
7+
DialogHeader,
8+
DialogTitle,
9+
} from "@/components/ui/dialog";
10+
import { MemoryGraph } from "@supermemory/memory-graph";
11+
import { Button } from "@/components/ui/button";
12+
import { Network } from "lucide-react";
13+
import type { DocumentWithMemories } from "@/lib/types/supermemory";
914

1015
interface MemoryGraphDialogProps {
1116
open?: boolean;
@@ -16,7 +21,7 @@ interface MemoryGraphDialogProps {
1621
export function MemoryGraphDialog({
1722
open: controlledOpen,
1823
onOpenChange: controlledOnOpenChange,
19-
triggerButton = false
24+
triggerButton = false,
2025
}: MemoryGraphDialogProps) {
2126
const [internalOpen, setInternalOpen] = useState(false);
2227
const [documents, setDocuments] = useState<DocumentWithMemories[]>([]);
@@ -32,32 +37,35 @@ export function MemoryGraphDialog({
3237
const onOpenChange = controlledOnOpenChange || setInternalOpen;
3338

3439
// Fetch documents when dialog opens
35-
const fetchDocuments = useCallback(async (page: number, limit: number = 500) => {
36-
try {
37-
const response = await fetch('/api/documents', {
38-
method: 'POST',
39-
headers: {
40-
'Content-Type': 'application/json',
41-
},
42-
body: JSON.stringify({
43-
page,
44-
limit,
45-
sort: 'createdAt',
46-
order: 'desc',
47-
}),
48-
});
40+
const fetchDocuments = useCallback(
41+
async (page: number, limit: number = 500) => {
42+
try {
43+
const response = await fetch("/api/documents", {
44+
method: "POST",
45+
headers: {
46+
"Content-Type": "application/json",
47+
},
48+
body: JSON.stringify({
49+
page,
50+
limit,
51+
sort: "createdAt",
52+
order: "desc",
53+
}),
54+
});
4955

50-
if (!response.ok) {
51-
throw new Error('Failed to fetch documents');
52-
}
56+
if (!response.ok) {
57+
throw new Error("Failed to fetch documents");
58+
}
5359

54-
const data = await response.json();
55-
return data;
56-
} catch (err) {
57-
console.error('Error fetching documents:', err);
58-
throw err;
59-
}
60-
}, []);
60+
const data = await response.json();
61+
return data;
62+
} catch (err) {
63+
console.error("Error fetching documents:", err);
64+
throw err;
65+
}
66+
},
67+
[],
68+
);
6169

6270
// Load initial documents
6371
const loadInitialDocuments = useCallback(async () => {
@@ -86,28 +94,31 @@ export function MemoryGraphDialog({
8694
const data = await fetchDocuments(nextPage, 100);
8795

8896
if (data.documents && data.documents.length > 0) {
89-
setDocuments(prev => [...prev, ...data.documents]);
90-
setTotalLoaded(prev => prev + data.documents.length);
97+
setDocuments((prev) => [...prev, ...data.documents]);
98+
setTotalLoaded((prev) => prev + data.documents.length);
9199
setCurrentPage(nextPage);
92100
setHasMore(data.pagination.currentPage < data.pagination.totalPages);
93101
} else {
94102
setHasMore(false);
95103
}
96104
} catch (err) {
97-
console.error('Error loading more documents:', err);
105+
console.error("Error loading more documents:", err);
98106
// Don't set error state for pagination failures
99107
} finally {
100108
setIsLoadingMore(false);
101109
}
102110
}, [currentPage, hasMore, isLoadingMore, fetchDocuments]);
103111

104112
// Handle dialog open state change
105-
const handleOpenChange = useCallback((newOpen: boolean) => {
106-
onOpenChange(newOpen);
107-
if (newOpen && documents.length === 0) {
108-
loadInitialDocuments();
109-
}
110-
}, [onOpenChange, documents.length, loadInitialDocuments]);
113+
const handleOpenChange = useCallback(
114+
(newOpen: boolean) => {
115+
onOpenChange(newOpen);
116+
if (newOpen && documents.length === 0) {
117+
loadInitialDocuments();
118+
}
119+
},
120+
[onOpenChange, documents.length, loadInitialDocuments],
121+
);
111122

112123
// Render trigger button if requested
113124
if (triggerButton && !controlledOpen) {
@@ -165,4 +176,4 @@ export function MemoryGraphDialog({
165176
</DialogContent>
166177
</Dialog>
167178
);
168-
}
179+
}

components/memory-graph-view.tsx

Lines changed: 82 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
'use client';
1+
"use client";
22

3-
import { useState, useCallback, useEffect } from 'react';
4-
import { MemoryGraph } from '@/lib/ui/memory-graph';
5-
import type { DocumentWithMemories } from '@/lib/types/supermemory';
3+
import { useState, useCallback, useEffect } from "react";
4+
import { MemoryGraph } from "@supermemory/memory-graph";
5+
import type { DocumentWithMemories } from "@/lib/types/supermemory";
66

77
interface MemoryGraphViewProps {
88
isActive?: boolean; // Whether the profile chat is currently active
@@ -19,40 +19,51 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
1919
const [hasInitialized, setHasInitialized] = useState(false);
2020

2121
// Fetch documents
22-
const fetchDocuments = useCallback(async (page: number, limit: number = 500) => {
23-
try {
24-
console.log('[MemoryGraph] Fetching documents - page:', page, 'limit:', limit);
25-
const response = await fetch('/api/documents', {
26-
method: 'POST',
27-
headers: {
28-
'Content-Type': 'application/json',
29-
},
30-
body: JSON.stringify({
22+
const fetchDocuments = useCallback(
23+
async (page: number, limit: number = 500) => {
24+
try {
25+
console.log(
26+
"[MemoryGraph] Fetching documents - page:",
3127
page,
28+
"limit:",
3229
limit,
33-
sort: 'createdAt',
34-
order: 'desc',
35-
}),
36-
});
37-
38-
if (!response.ok) {
39-
throw new Error('Failed to fetch documents');
30+
);
31+
const response = await fetch("/api/documents", {
32+
method: "POST",
33+
headers: {
34+
"Content-Type": "application/json",
35+
},
36+
body: JSON.stringify({
37+
page,
38+
limit,
39+
sort: "createdAt",
40+
order: "desc",
41+
}),
42+
});
43+
44+
if (!response.ok) {
45+
throw new Error("Failed to fetch documents");
46+
}
47+
48+
const data = await response.json();
49+
console.log(
50+
"[MemoryGraph] Received documents:",
51+
data.documents?.length || 0,
52+
);
53+
return data;
54+
} catch (err) {
55+
console.error("[MemoryGraph] Error fetching documents:", err);
56+
throw err;
4057
}
41-
42-
const data = await response.json();
43-
console.log('[MemoryGraph] Received documents:', data.documents?.length || 0);
44-
return data;
45-
} catch (err) {
46-
console.error('[MemoryGraph] Error fetching documents:', err);
47-
throw err;
48-
}
49-
}, []);
58+
},
59+
[],
60+
);
5061

5162
// Load initial documents when component becomes active
5263
const loadInitialDocuments = useCallback(async () => {
5364
if (!isActive) return;
5465

55-
console.log('[MemoryGraph] Loading initial documents...');
66+
console.log("[MemoryGraph] Loading initial documents...");
5667
setIsLoading(true);
5768
setError(null);
5869
try {
@@ -62,9 +73,12 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
6273
setCurrentPage(1);
6374
setHasMore(data.pagination.currentPage < data.pagination.totalPages);
6475
setHasInitialized(true); // Mark as initialized after first load
65-
console.log('[MemoryGraph] Initial load complete. Total loaded:', data.documents?.length || 0);
76+
console.log(
77+
"[MemoryGraph] Initial load complete. Total loaded:",
78+
data.documents?.length || 0,
79+
);
6680
} catch (err) {
67-
console.error('[MemoryGraph] Initial load error:', err);
81+
console.error("[MemoryGraph] Initial load error:", err);
6882
setError(err as Error);
6983
setHasInitialized(true); // Mark as initialized even on error
7084
} finally {
@@ -76,32 +90,44 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
7690
const loadMoreDocuments = useCallback(async () => {
7791
if (isLoadingMore || !hasMore || !isActive) return;
7892

79-
console.log('[MemoryGraph] Loading more documents...');
93+
console.log("[MemoryGraph] Loading more documents...");
8094
setIsLoadingMore(true);
8195
try {
8296
const nextPage = currentPage + 1;
8397
const data = await fetchDocuments(nextPage, 100);
8498

8599
if (data.documents && data.documents.length > 0) {
86-
setDocuments(prev => [...prev, ...data.documents]);
87-
setTotalLoaded(prev => prev + data.documents.length);
100+
setDocuments((prev) => [...prev, ...data.documents]);
101+
setTotalLoaded((prev) => prev + data.documents.length);
88102
setCurrentPage(nextPage);
89103
setHasMore(data.pagination.currentPage < data.pagination.totalPages);
90-
console.log('[MemoryGraph] Loaded more. Total now:', totalLoaded + data.documents.length);
104+
console.log(
105+
"[MemoryGraph] Loaded more. Total now:",
106+
totalLoaded + data.documents.length,
107+
);
91108
} else {
92109
setHasMore(false);
93110
}
94111
} catch (err) {
95-
console.error('[MemoryGraph] Error loading more documents:', err);
112+
console.error("[MemoryGraph] Error loading more documents:", err);
96113
} finally {
97114
setIsLoadingMore(false);
98115
}
99-
}, [currentPage, hasMore, isLoadingMore, isActive, fetchDocuments, totalLoaded]);
116+
}, [
117+
currentPage,
118+
hasMore,
119+
isLoadingMore,
120+
isActive,
121+
fetchDocuments,
122+
totalLoaded,
123+
]);
100124

101125
// Load documents when component becomes active
102126
useEffect(() => {
103127
if (isActive && !hasInitialized && !isLoading) {
104-
console.log('[MemoryGraph] Component became active, loading documents...');
128+
console.log(
129+
"[MemoryGraph] Component became active, loading documents...",
130+
);
105131
loadInitialDocuments();
106132
}
107133
}, [isActive, hasInitialized, isLoading, loadInitialDocuments]);
@@ -112,7 +138,7 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
112138

113139
// Refresh every 60 seconds while viewing
114140
const interval = setInterval(() => {
115-
console.log('[MemoryGraph] Auto-refreshing documents...');
141+
console.log("[MemoryGraph] Auto-refreshing documents...");
116142
loadInitialDocuments();
117143
}, 60000);
118144

@@ -125,11 +151,23 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
125151
<div className="absolute inset-0 flex items-center justify-center">
126152
<div className="text-center p-8">
127153
<div className="text-gray-400 mb-4">
128-
<svg className="w-16 h-16 mx-auto mb-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
129-
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
154+
<svg
155+
className="w-16 h-16 mx-auto mb-4"
156+
fill="none"
157+
viewBox="0 0 24 24"
158+
stroke="currentColor"
159+
>
160+
<path
161+
strokeLinecap="round"
162+
strokeLinejoin="round"
163+
strokeWidth={1.5}
164+
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
165+
/>
130166
</svg>
131167
</div>
132-
<h3 className="text-lg font-medium text-gray-300 mb-2">No Memories Yet</h3>
168+
<h3 className="text-lg font-medium text-gray-300 mb-2">
169+
No Memories Yet
170+
</h3>
133171
<p className="text-sm text-gray-500">
134172
Start chatting with Supermemory to create your memory graph
135173
</p>
@@ -150,4 +188,4 @@ export function MemoryGraphView({ isActive = false }: MemoryGraphViewProps) {
150188
/>
151189
</div>
152190
);
153-
}
191+
}

0 commit comments

Comments
 (0)