( options: BuildMemoriesTextOptions, )
| 86 | * @returns The final formatted memories string ready for injection |
| 87 | */ |
| 88 | export const buildMemoriesText = async ( |
| 89 | options: BuildMemoriesTextOptions, |
| 90 | ): Promise<string> => { |
| 91 | const { |
| 92 | containerTag, |
| 93 | queryText, |
| 94 | mode, |
| 95 | baseUrl, |
| 96 | apiKey, |
| 97 | logger, |
| 98 | promptTemplate = defaultPromptTemplate, |
| 99 | signal, |
| 100 | } = options |
| 101 | |
| 102 | const memoriesResponse = await supermemoryProfileSearch( |
| 103 | containerTag, |
| 104 | queryText, |
| 105 | baseUrl, |
| 106 | apiKey, |
| 107 | signal, |
| 108 | ) |
| 109 | |
| 110 | const memoryCountStatic = memoriesResponse.profile.static?.length || 0 |
| 111 | const memoryCountDynamic = memoriesResponse.profile.dynamic?.length || 0 |
| 112 | |
| 113 | logger.info("Memory search completed", { |
| 114 | containerTag, |
| 115 | memoryCountStatic, |
| 116 | memoryCountDynamic, |
| 117 | queryText: |
| 118 | queryText.substring(0, 100) + (queryText.length > 100 ? "..." : ""), |
| 119 | mode, |
| 120 | }) |
| 121 | |
| 122 | const deduplicated = deduplicateMemories({ |
| 123 | static: memoriesResponse.profile.static, |
| 124 | dynamic: memoriesResponse.profile.dynamic, |
| 125 | searchResults: memoriesResponse.searchResults?.results, |
| 126 | }) |
| 127 | |
| 128 | logger.debug("Memory deduplication completed", { |
| 129 | static: { |
| 130 | original: memoryCountStatic, |
| 131 | deduplicated: deduplicated.static.length, |
| 132 | }, |
| 133 | dynamic: { |
| 134 | original: memoryCountDynamic, |
| 135 | deduplicated: deduplicated.dynamic.length, |
| 136 | }, |
| 137 | searchResults: { |
| 138 | original: memoriesResponse.searchResults?.results?.length, |
| 139 | deduplicated: deduplicated.searchResults?.length, |
| 140 | }, |
| 141 | }) |
| 142 | |
| 143 | const userMemories = |
| 144 | mode !== "query" |
| 145 | ? convertProfileToMarkdown({ |
no test coverage detected
searching dependent graphs…