Skip to content

Commit 349a140

Browse files
committed
sdk: fork getFiles implementation
1 parent ba79fe2 commit 349a140

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

sdk/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
getInitialSessionState,
1414
SessionState,
1515
} from '../../common/src/types/session-state'
16-
import { getFiles } from '../../npm-app/src/project-files'
1716
import { PrintModeEvent } from '../../common/src/types/print-mode'
17+
import { getFiles } from './tools/read-files'
1818

1919
type ClientToolName = 'write_file' | 'run_terminal_command'
2020

@@ -215,7 +215,7 @@ export class CodebuffClient {
215215
const overrideResult = await override(filePath)
216216
return overrideResult.files
217217
}
218-
return getFiles(filePath)
218+
return getFiles(filePath, this.cwd)
219219
}
220220

221221
private async handleToolCall(

sdk/src/tools/read-files.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { FILE_READ_STATUS } from '../../../common/src/constants'
2+
import path, { isAbsolute } from 'path'
3+
import fs from 'fs'
4+
5+
export function getFiles(filePaths: string[], cwd: string) {
6+
const result: Record<string, string | null> = {}
7+
const MAX_FILE_SIZE = 1024 * 1024 // 1MB in bytes
8+
9+
for (const filePath of filePaths) {
10+
if (!filePath) {
11+
continue
12+
}
13+
14+
// Convert absolute paths within project to relative paths
15+
const relativePath = filePath.startsWith(cwd)
16+
? path.relative(cwd, filePath)
17+
: filePath
18+
const fullPath = path.join(cwd, relativePath)
19+
if (isAbsolute(relativePath) || !fullPath.startsWith(cwd)) {
20+
result[relativePath] = FILE_READ_STATUS.OUTSIDE_PROJECT
21+
continue
22+
}
23+
try {
24+
const stats = fs.statSync(fullPath)
25+
if (stats.size > MAX_FILE_SIZE) {
26+
result[relativePath] =
27+
FILE_READ_STATUS.TOO_LARGE +
28+
` [${(stats.size / (1024 * 1024)).toFixed(2)}MB]`
29+
} else {
30+
const content = fs.readFileSync(fullPath, 'utf8')
31+
result[relativePath] = content
32+
}
33+
} catch (error) {
34+
if (
35+
error &&
36+
typeof error === 'object' &&
37+
'code' in error &&
38+
error.code === 'ENOENT'
39+
) {
40+
result[relativePath] = FILE_READ_STATUS.DOES_NOT_EXIST
41+
} else {
42+
result[relativePath] = FILE_READ_STATUS.ERROR
43+
}
44+
}
45+
}
46+
return result
47+
}

0 commit comments

Comments
 (0)