MCPcopy Create free account
hub / github.com/promptfoo/promptfoo / readFile

Function readFile

src/util/agent/fsOperations.ts:66–88  ·  view source on GitHub ↗
(filePath: string, rootDir: string)

Source from the content-addressed store, hash-verified

64 * without loading arbitrarily large files into memory.
65 */
66export async function readFile(filePath: string, rootDir: string): Promise<string> {
67 const resolved = await resolveAndValidate(filePath, rootDir);
68 const stat = await fs.stat(resolved);
69
70 if (!stat.isFile()) {
71 throw new Error(`Not a file: ${filePath}`);
72 }
73
74 if (stat.size <= MAX_FILE_SIZE) {
75 return fs.readFile(resolved, 'utf-8');
76 }
77
78 if (stat.size <= MAX_FILE_SCAN_BYTES) {
79 const content = await fs.readFile(resolved, 'utf-8');
80 if (content.length <= MAX_FILE_SIZE) {
81 return content;
82 }
83 return content.slice(0, MAX_FILE_SIZE) + `\n\n[truncated — file is ${stat.size} bytes]`;
84 }
85
86 const content = await readFilePrefix(resolved, MAX_FILE_SCAN_BYTES);
87 return content.slice(0, MAX_FILE_SIZE) + `\n\n[truncated — file is ${stat.size} bytes]`;
88}
89
90/**
91 * List directory entries relative to rootDir.

Callers 3

attachTargetLinkFsFunction · 0.90
processExecutableFileFunction · 0.50

Calls 2

resolveAndValidateFunction · 0.85
readFilePrefixFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…