MCPcopy
hub / github.com/supermemoryai/supermemory / deduplicateMemories

Function deduplicateMemories

packages/tools/src/tools-shared.ts:121–176  ·  view source on GitHub ↗
(
	data: ProfileWithMemories,
)

Source from the content-addressed store, hash-verified

119 * ```
120 */
121export function deduplicateMemories(
122 data: ProfileWithMemories,
123): DeduplicatedMemories {
124 const staticItems = data.static ?? []
125 const dynamicItems = data.dynamic ?? []
126 const searchItems = data.searchResults ?? []
127
128 const getMemoryString = (item: MemoryItem | string): string | null => {
129 if (!item) return null
130 // Handle both string format (from API) and object format
131 if (typeof item === "string") {
132 const trimmed = item.trim()
133 return trimmed.length > 0 ? trimmed : null
134 }
135 if (typeof item.memory !== "string") return null
136 const trimmed = item.memory.trim()
137 return trimmed.length > 0 ? trimmed : null
138 }
139
140 const staticMemories: string[] = []
141 const seenMemories = new Set<string>()
142
143 for (const item of staticItems as Array<MemoryItem | string>) {
144 const memory = getMemoryString(item)
145 if (memory !== null) {
146 staticMemories.push(memory)
147 seenMemories.add(memory)
148 }
149 }
150
151 const dynamicMemories: string[] = []
152
153 for (const item of dynamicItems as Array<MemoryItem | string>) {
154 const memory = getMemoryString(item)
155 if (memory !== null && !seenMemories.has(memory)) {
156 dynamicMemories.push(memory)
157 seenMemories.add(memory)
158 }
159 }
160
161 const searchMemories: string[] = []
162
163 for (const item of searchItems as Array<MemoryItem | string>) {
164 const memory = getMemoryString(item)
165 if (memory !== null && !seenMemories.has(memory)) {
166 searchMemories.push(memory)
167 seenMemories.add(memory)
168 }
169 }
170
171 return {
172 static: staticMemories,
173 dynamic: dynamicMemories,
174 searchResults: searchMemories,
175 }
176}

Callers 3

addSystemPromptFunction · 0.90
searchAndFormatMemoriesFunction · 0.90
buildMemoriesTextFunction · 0.90

Calls 2

getMemoryStringFunction · 0.85
hasMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…