|
| 1 | +/** |
| 2 | + * 文件夹笔记预处理器 |
| 3 | + * 负责管理已读文件夹状态和格式化文件夹笔记消息 |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + getParentFolderPaths, |
| 8 | + readNotebookData, |
| 9 | + type FolderNotebook, |
| 10 | +} from './notebookManager.js'; |
| 11 | + |
| 12 | +/** |
| 13 | + * 已读文件夹集合(每个 Agent 实例独立维护) |
| 14 | + * 记录当前会话中已经读取过的文件所属的文件夹路径 |
| 15 | + */ |
| 16 | +let readFolders: Set<string> = new Set(); |
| 17 | + |
| 18 | +/** |
| 19 | + * 更新已读文件夹集合 |
| 20 | + * @param filePath 读取的文件路径 |
| 21 | + * @returns 新添加的文件夹列表(用于判断是否有新内容需要展示) |
| 22 | + */ |
| 23 | +export function updateReadFolders(filePath: string): string[] { |
| 24 | + const parentFolders = getParentFolderPaths(filePath); |
| 25 | + const newFolders: string[] = []; |
| 26 | + |
| 27 | + for (const folder of parentFolders) { |
| 28 | + if (!readFolders.has(folder)) { |
| 29 | + readFolders.add(folder); |
| 30 | + newFolders.push(folder); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return newFolders; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * 清空已读文件夹集合 |
| 39 | + * 通常在压缩对话历史后调用 |
| 40 | + */ |
| 41 | +export function clearReadFolders(): void { |
| 42 | + readFolders.clear(); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * 获取当前已读文件夹集合 |
| 47 | + * @returns 已读文件夹的 Set 副本 |
| 48 | + */ |
| 49 | +export function getReadFolders(): Set<string> { |
| 50 | + return new Set(readFolders); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * 设置已读文件夹集合 |
| 55 | + * 用于在子 Agent 执行后恢复主 Agent 的状态 |
| 56 | + * @param folders 要设置的文件夹集合 |
| 57 | + */ |
| 58 | +export function setReadFolders(folders: Set<string>): void { |
| 59 | + readFolders.clear(); |
| 60 | + for (const folder of folders) { |
| 61 | + readFolders.add(folder); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * 独立的文件夹笔记预处理器实例接口 |
| 67 | + */ |
| 68 | +export interface FolderNotebookPreprocessorInstance { |
| 69 | + updateReadFolders: (filePath: string) => string[]; |
| 70 | + clearReadFolders: () => void; |
| 71 | + getReadFolders: () => Set<string>; |
| 72 | + formatFolderNotebookContext: (foldersToShow?: string[]) => string; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * 创建独立的文件夹笔记预处理器实例 |
| 77 | + * 用于子 Agent,避免与主 Agent 共享状态 |
| 78 | + * @returns 独立的预处理器实例 |
| 79 | + */ |
| 80 | +export function createFolderNotebookPreprocessor(): FolderNotebookPreprocessorInstance { |
| 81 | + // 独立的已读文件夹集合 |
| 82 | + const instanceReadFolders = new Set<string>(); |
| 83 | + |
| 84 | + return { |
| 85 | + /** |
| 86 | + * 更新已读文件夹集合 |
| 87 | + */ |
| 88 | + updateReadFolders: (filePath: string): string[] => { |
| 89 | + const parentFolders = getParentFolderPaths(filePath); |
| 90 | + const newFolders: string[] = []; |
| 91 | + |
| 92 | + for (const folder of parentFolders) { |
| 93 | + if (!instanceReadFolders.has(folder)) { |
| 94 | + instanceReadFolders.add(folder); |
| 95 | + newFolders.push(folder); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return newFolders; |
| 100 | + }, |
| 101 | + |
| 102 | + /** |
| 103 | + * 清空已读文件夹集合 |
| 104 | + */ |
| 105 | + clearReadFolders: (): void => { |
| 106 | + instanceReadFolders.clear(); |
| 107 | + }, |
| 108 | + |
| 109 | + /** |
| 110 | + * 获取当前已读文件夹集合 |
| 111 | + */ |
| 112 | + getReadFolders: (): Set<string> => { |
| 113 | + return new Set(instanceReadFolders); |
| 114 | + }, |
| 115 | + |
| 116 | + /** |
| 117 | + * 格式化文件夹笔记为 user 消息内容 |
| 118 | + */ |
| 119 | + formatFolderNotebookContext: (foldersToShow?: string[]): string => { |
| 120 | + // 收集需要展示的文件夹 |
| 121 | + const folders = foldersToShow ?? Array.from(instanceReadFolders); |
| 122 | + |
| 123 | + if (folders.length === 0) { |
| 124 | + return ''; |
| 125 | + } |
| 126 | + |
| 127 | + // 收集所有文件夹的笔记 |
| 128 | + const allNotebooks: FolderNotebook[] = []; |
| 129 | + const notebookData = readNotebookData(); |
| 130 | + |
| 131 | + for (const folder of folders) { |
| 132 | + const entries = notebookData[folder]; |
| 133 | + if (entries && entries.length > 0) { |
| 134 | + allNotebooks.push({ |
| 135 | + folderPath: folder, |
| 136 | + entries: entries.slice(0, 5), // 每个文件夹最新5条 |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (allNotebooks.length === 0) { |
| 142 | + return ''; |
| 143 | + } |
| 144 | + |
| 145 | + // 按路径深度排序(从浅到深) |
| 146 | + allNotebooks.sort((a, b) => { |
| 147 | + const depthA = a.folderPath.split('/').length; |
| 148 | + const depthB = b.folderPath.split('/').length; |
| 149 | + if (depthA !== depthB) return depthA - depthB; |
| 150 | + return a.folderPath.localeCompare(b.folderPath); |
| 151 | + }); |
| 152 | + |
| 153 | + // 格式化输出 |
| 154 | + let output = `## 📂 Folder Notebooks (Context from read files)\n\n`; |
| 155 | + output += `The following folder notebooks are relevant to files you've read in this session.\n\n`; |
| 156 | + |
| 157 | + for (const notebook of allNotebooks) { |
| 158 | + const folderName = |
| 159 | + notebook.folderPath === '/' |
| 160 | + ? '/ (project root)' |
| 161 | + : notebook.folderPath; |
| 162 | + output += `### ${folderName}\n`; |
| 163 | + notebook.entries.forEach((entry, index) => { |
| 164 | + output += ` ${index + 1}. [${entry.createdAt}] ${entry.note}\n`; |
| 165 | + }); |
| 166 | + output += '\n'; |
| 167 | + } |
| 168 | + |
| 169 | + output += `---\n💡 These notes are from folders containing files you've read. They won't repeat.`; |
| 170 | + |
| 171 | + return output; |
| 172 | + }, |
| 173 | + }; |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * 格式化文件夹笔记为 user 消息内容 |
| 178 | + * @param foldersToShow 需要展示笔记的文件夹列表,如果不传则使用当前 readFolders 集合 |
| 179 | + * @returns 格式化后的笔记内容,如果没有笔记则返回空字符串 |
| 180 | + */ |
| 181 | +export function formatFolderNotebookContext(foldersToShow?: string[]): string { |
| 182 | + // 收集需要展示的文件夹 |
| 183 | + const folders = foldersToShow ?? Array.from(readFolders); |
| 184 | + |
| 185 | + if (folders.length === 0) { |
| 186 | + return ''; |
| 187 | + } |
| 188 | + |
| 189 | + // 收集所有文件夹的笔记 |
| 190 | + const allNotebooks: FolderNotebook[] = []; |
| 191 | + const notebookData = readNotebookData(); |
| 192 | + |
| 193 | + for (const folder of folders) { |
| 194 | + const entries = notebookData[folder]; |
| 195 | + if (entries && entries.length > 0) { |
| 196 | + allNotebooks.push({ |
| 197 | + folderPath: folder, |
| 198 | + entries: entries.slice(0, 5), // 每个文件夹最新5条 |
| 199 | + }); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + if (allNotebooks.length === 0) { |
| 204 | + return ''; |
| 205 | + } |
| 206 | + |
| 207 | + // 按路径深度排序(从浅到深) |
| 208 | + allNotebooks.sort((a, b) => { |
| 209 | + const depthA = a.folderPath.split('/').length; |
| 210 | + const depthB = b.folderPath.split('/').length; |
| 211 | + if (depthA !== depthB) return depthA - depthB; |
| 212 | + return a.folderPath.localeCompare(b.folderPath); |
| 213 | + }); |
| 214 | + |
| 215 | + // 格式化输出 |
| 216 | + let output = `## 📂 Folder Notebooks (Context from read files)\n\n`; |
| 217 | + output += `The following folder notebooks are relevant to files you've read in this session.\n\n`; |
| 218 | + |
| 219 | + for (const notebook of allNotebooks) { |
| 220 | + const folderName = |
| 221 | + notebook.folderPath === '/' ? '/ (project root)' : notebook.folderPath; |
| 222 | + output += `### ${folderName}\n`; |
| 223 | + notebook.entries.forEach((entry, index) => { |
| 224 | + output += ` ${index + 1}. [${entry.createdAt}] ${entry.note}\n`; |
| 225 | + }); |
| 226 | + output += '\n'; |
| 227 | + } |
| 228 | + |
| 229 | + output += `---\n💡 These notes are from folders containing files you've read. They won't repeat.`; |
| 230 | + |
| 231 | + return output; |
| 232 | +} |
0 commit comments