summaryrefslogtreecommitdiff
path: root/packages/core/src/utils/fileUtils.ts
diff options
context:
space:
mode:
authorSanthosh Kumar <[email protected]>2025-07-02 00:52:32 +0530
committerGitHub <[email protected]>2025-07-01 19:22:32 +0000
commit0275ab0108e9a411d90d8ef8c8d70e21e498d81a (patch)
tree702acf3ad2746005bae13bff1502f39c508f8c3f /packages/core/src/utils/fileUtils.ts
parent63ed8d649913df05736d47ff03deb7741603f73d (diff)
feat: add audio and video support to read_file (#2556)
Diffstat (limited to 'packages/core/src/utils/fileUtils.ts')
-rw-r--r--packages/core/src/utils/fileUtils.ts41
1 files changed, 32 insertions, 9 deletions
diff --git a/packages/core/src/utils/fileUtils.ts b/packages/core/src/utils/fileUtils.ts
index cb4d333a..5a05d513 100644
--- a/packages/core/src/utils/fileUtils.ts
+++ b/packages/core/src/utils/fileUtils.ts
@@ -94,19 +94,27 @@ export function isBinaryFile(filePath: string): boolean {
/**
* Detects the type of file based on extension and content.
* @param filePath Path to the file.
- * @returns 'text', 'image', 'pdf', or 'binary'.
+ * @returns 'text', 'image', 'pdf', 'audio', 'video', or 'binary'.
*/
export function detectFileType(
filePath: string,
-): 'text' | 'image' | 'pdf' | 'binary' {
+): 'text' | 'image' | 'pdf' | 'audio' | 'video' | 'binary' {
const ext = path.extname(filePath).toLowerCase();
const lookedUpMimeType = mime.lookup(filePath); // Returns false if not found, or the mime type string
- if (lookedUpMimeType && lookedUpMimeType.startsWith('image/')) {
- return 'image';
- }
- if (lookedUpMimeType && lookedUpMimeType === 'application/pdf') {
- return 'pdf';
+ if (lookedUpMimeType) {
+ if (lookedUpMimeType.startsWith('image/')) {
+ return 'image';
+ }
+ if (lookedUpMimeType.startsWith('audio/')) {
+ return 'audio';
+ }
+ if (lookedUpMimeType.startsWith('video/')) {
+ return 'video';
+ }
+ if (lookedUpMimeType === 'application/pdf') {
+ return 'pdf';
+ }
}
// Stricter binary check for common non-text extensions before content check
@@ -187,7 +195,7 @@ export async function processSingleFileContent(
error: `File not found: ${filePath}`,
};
}
- const stats = fs.statSync(filePath); // Sync check
+ const stats = await fs.promises.stat(filePath);
if (stats.isDirectory()) {
return {
llmContent: '',
@@ -196,6 +204,19 @@ export async function processSingleFileContent(
};
}
+ const fileSizeInBytes = stats.size;
+ // 20MB limit
+ const maxFileSize = 20 * 1024 * 1024;
+
+ if (fileSizeInBytes > maxFileSize) {
+ throw new Error(
+ `File size exceeds the 20MB limit: ${filePath} (${(
+ fileSizeInBytes /
+ (1024 * 1024)
+ ).toFixed(2)}MB)`,
+ );
+ }
+
const fileType = detectFileType(filePath);
const relativePathForDisplay = path
.relative(rootDirectory, filePath)
@@ -253,7 +274,9 @@ export async function processSingleFileContent(
};
}
case 'image':
- case 'pdf': {
+ case 'pdf':
+ case 'audio':
+ case 'video': {
const contentBuffer = await fs.promises.readFile(filePath);
const base64Data = contentBuffer.toString('base64');
return {