Skip to content

Commit 36ecf47

Browse files
authored
Fix modal memory graph scoping by document and space (supermemoryai#948)
1 parent 28a60f5 commit 36ecf47

3 files changed

Lines changed: 56 additions & 16 deletions

File tree

apps/web/components/document-modal/graph-list-memories.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useState } from "react"
22
import { cn } from "@lib/utils"
33
import { Tabs, TabsList, TabsTrigger, TabsContent } from "@ui/components/tabs"
44
import { MemoryGraph } from "../memory-graph"
5+
import { useProject } from "@/stores"
56

67
export interface MemoryEntry {
78
id: string
@@ -172,6 +173,7 @@ export function GraphListMemories({
172173
memoryEntries: MemoryEntry[]
173174
documentId?: string
174175
}) {
176+
const { effectiveContainerTags } = useProject()
175177
const [expandedMemories, setExpandedMemories] = useState<Set<string>>(
176178
new Set(),
177179
)
@@ -263,7 +265,10 @@ export function GraphListMemories({
263265
<TabsContent value="graph" className="flex-1 min-h-0 mt-3">
264266
<div className="size-full rounded-lg overflow-hidden">
265267
<MemoryGraph
268+
containerTags={effectiveContainerTags}
266269
documentIds={documentId ? [documentId] : undefined}
270+
highlightDocumentIds={documentId ? [documentId] : undefined}
271+
highlightsVisible
267272
variant="consumer"
268273
maxNodes={50}
269274
/>

apps/web/components/memory-graph/hooks/use-graph-api.ts

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const PAGE_SIZE = 100
1313

1414
interface UseGraphApiOptions {
1515
containerTags?: string[]
16+
documentIds?: string[]
1617
enabled?: boolean
1718
}
1819

@@ -81,20 +82,36 @@ function toGraphMemory(mem: ApiMemoryEntry): GraphApiMemory {
8182
}
8283
}
8384

84-
function toGraphDocument(doc: ApiDocument): GraphApiDocument {
85+
function toGraphDocument(
86+
doc: ApiDocument,
87+
containerTags?: string[],
88+
): GraphApiDocument {
89+
const allowedContainerTags = new Set(containerTags?.filter(Boolean) ?? [])
90+
const memoryEntries =
91+
allowedContainerTags.size > 0
92+
? doc.memoryEntries.filter(
93+
(mem) =>
94+
mem.spaceContainerTag != null &&
95+
allowedContainerTags.has(mem.spaceContainerTag),
96+
)
97+
: doc.memoryEntries
98+
8599
return {
86100
id: doc.id,
87101
title: doc.title,
88102
summary: doc.summary ?? null,
89103
documentType: doc.type,
90104
createdAt: doc.createdAt,
91105
updatedAt: doc.updatedAt,
92-
memories: doc.memoryEntries.map(toGraphMemory),
106+
memories: memoryEntries.map(toGraphMemory),
93107
}
94108
}
95109

96110
export function useGraphApi(options: UseGraphApiOptions = {}) {
97-
const { containerTags, enabled = true } = options
111+
const { containerTags, documentIds, enabled = true } = options
112+
const filteredDocumentIds = documentIds?.filter(Boolean)
113+
const hasDocumentIds =
114+
filteredDocumentIds != null && filteredDocumentIds.length > 0
98115

99116
const {
100117
data,
@@ -104,19 +121,33 @@ export function useGraphApi(options: UseGraphApiOptions = {}) {
104121
hasNextPage,
105122
fetchNextPage,
106123
} = useInfiniteQuery<ApiDocumentsResponse, Error>({
107-
queryKey: ["documents-with-memories", containerTags, []],
124+
queryKey: [
125+
"documents-with-memories",
126+
containerTags,
127+
[],
128+
filteredDocumentIds,
129+
],
108130
initialPageParam: 1,
109131
queryFn: async ({ pageParam }) => {
110-
const response = await $fetch("@post/documents/documents", {
111-
body: {
112-
page: pageParam as number,
113-
limit: PAGE_SIZE,
114-
sort: "createdAt",
115-
order: "desc",
116-
containerTags,
117-
},
118-
disableValidation: true,
119-
})
132+
const response = hasDocumentIds
133+
? await $fetch("@post/documents/documents/by-ids", {
134+
body: {
135+
ids: filteredDocumentIds,
136+
by: "id",
137+
containerTags,
138+
},
139+
disableValidation: true,
140+
})
141+
: await $fetch("@post/documents/documents", {
142+
body: {
143+
page: pageParam as number,
144+
limit: PAGE_SIZE,
145+
sort: "createdAt",
146+
order: "desc",
147+
containerTags,
148+
},
149+
disableValidation: true,
150+
})
120151

121152
if (response.error) {
122153
throw new Error(response.error?.message || "Failed to fetch documents")
@@ -134,8 +165,10 @@ export function useGraphApi(options: UseGraphApiOptions = {}) {
134165

135166
const documents = useMemo(() => {
136167
if (!data?.pages) return []
137-
return data.pages.flatMap((page) => page.documents.map(toGraphDocument))
138-
}, [data])
168+
return data.pages.flatMap((page) =>
169+
page.documents.map((doc) => toGraphDocument(doc, containerTags)),
170+
)
171+
}, [data, containerTags])
139172

140173
const totalCount = data?.pages[0]?.pagination.totalItems ?? 0
141174

apps/web/components/memory-graph/memory-graph-wrapper.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export function MemoryGraph({
2929
error: externalError = null,
3030
variant = "console",
3131
containerTags,
32+
documentIds,
3233
maxNodes,
3334
canvasRef,
3435
...rest
@@ -57,6 +58,7 @@ export function MemoryGraph({
5758
totalCount,
5859
} = useGraphApi({
5960
containerTags,
61+
documentIds,
6062
enabled: containerSize.width > 0 && containerSize.height > 0,
6163
})
6264

0 commit comments

Comments
 (0)