* Computes the new content that would result from adding a memory entry
(currentContent: string, fact: string)
| 158 | * Computes the new content that would result from adding a memory entry |
| 159 | */ |
| 160 | function computeNewContent(currentContent: string, fact: string): string { |
| 161 | let processedText = fact.trim(); |
| 162 | processedText = processedText.replace(/^(-+\s*)+/, '').trim(); |
| 163 | const newMemoryItem = `- ${processedText}`; |
| 164 | |
| 165 | const headerIndex = currentContent.indexOf(MEMORY_SECTION_HEADER); |
| 166 | |
| 167 | if (headerIndex === -1) { |
| 168 | // Header not found, append header and then the entry |
| 169 | const separator = ensureNewlineSeparation(currentContent); |
| 170 | return ( |
| 171 | currentContent + |
| 172 | `${separator}${MEMORY_SECTION_HEADER}\n${newMemoryItem}\n` |
| 173 | ); |
| 174 | } else { |
| 175 | // Header found, find where to insert the new memory entry |
| 176 | const startOfSectionContent = headerIndex + MEMORY_SECTION_HEADER.length; |
| 177 | let endOfSectionIndex = currentContent.indexOf( |
| 178 | '\n## ', |
| 179 | startOfSectionContent, |
| 180 | ); |
| 181 | if (endOfSectionIndex === -1) { |
| 182 | endOfSectionIndex = currentContent.length; // End of file |
| 183 | } |
| 184 | |
| 185 | const beforeSectionMarker = currentContent |
| 186 | .substring(0, startOfSectionContent) |
| 187 | .trimEnd(); |
| 188 | let sectionContent = currentContent |
| 189 | .substring(startOfSectionContent, endOfSectionIndex) |
| 190 | .trimEnd(); |
| 191 | const afterSectionMarker = currentContent.substring(endOfSectionIndex); |
| 192 | |
| 193 | sectionContent += `\n${newMemoryItem}`; |
| 194 | return ( |
| 195 | `${beforeSectionMarker}\n${sectionContent.trimStart()}\n${afterSectionMarker}`.trimEnd() + |
| 196 | '\n' |
| 197 | ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | class MemoryToolInvocation extends BaseToolInvocation< |
| 202 | SaveMemoryParams, |
no test coverage detected