* Gets file or folder content and returns it as a MentionContentBlock * formatted to look like a read_file tool result.
( mentionPath: string, cwd: string, rooIgnoreController?: any, showRooIgnoredFiles: boolean = false, fileContextTracker?: FileContextTracker, )
| 263 | * formatted to look like a read_file tool result. |
| 264 | */ |
| 265 | async function getFileOrFolderContentWithMetadata( |
| 266 | mentionPath: string, |
| 267 | cwd: string, |
| 268 | rooIgnoreController?: any, |
| 269 | showRooIgnoredFiles: boolean = false, |
| 270 | fileContextTracker?: FileContextTracker, |
| 271 | ): Promise<MentionContentBlock> { |
| 272 | const unescapedPath = unescapeSpaces(mentionPath) |
| 273 | const absPath = path.resolve(cwd, unescapedPath) |
| 274 | const isFolder = mentionPath.endsWith("/") |
| 275 | |
| 276 | try { |
| 277 | const stats = await fs.stat(absPath) |
| 278 | |
| 279 | if (stats.isFile()) { |
| 280 | // Avoid trying to include image binary content as text context. |
| 281 | // Image mentions are handled separately via image attachment flow. |
| 282 | const isBinary = await isBinaryFile(absPath).catch(() => false) |
| 283 | if (isBinary) { |
| 284 | return { |
| 285 | type: "file", |
| 286 | path: mentionPath, |
| 287 | content: `[read_file for '${mentionPath}']\nNote: Binary file omitted from context.`, |
| 288 | } |
| 289 | } |
| 290 | if (rooIgnoreController && !rooIgnoreController.validateAccess(unescapedPath)) { |
| 291 | return { |
| 292 | type: "file", |
| 293 | path: mentionPath, |
| 294 | content: `[read_file for '${mentionPath}']\nNote: File is ignored by .rooignore.`, |
| 295 | } |
| 296 | } |
| 297 | try { |
| 298 | const result = await extractTextFromFileWithMetadata(absPath) |
| 299 | |
| 300 | // Track file context |
| 301 | if (fileContextTracker) { |
| 302 | await fileContextTracker.trackFileContext(mentionPath, "file_mentioned") |
| 303 | } |
| 304 | |
| 305 | return { |
| 306 | type: "file", |
| 307 | path: mentionPath, |
| 308 | content: formatFileReadResult(mentionPath, result), |
| 309 | metadata: { |
| 310 | totalLines: result.totalLines, |
| 311 | returnedLines: result.returnedLines, |
| 312 | wasTruncated: result.wasTruncated, |
| 313 | linesShown: result.linesShown, |
| 314 | }, |
| 315 | } |
| 316 | } catch (error) { |
| 317 | const errorMsg = error instanceof Error ? error.message : String(error) |
| 318 | return { |
| 319 | type: "file", |
| 320 | path: mentionPath, |
| 321 | content: `[read_file for '${mentionPath}']\nError: ${errorMsg}`, |
| 322 | } |
no test coverage detected