| 20 | * Uses content-based detection via isBinaryFile |
| 21 | */ |
| 22 | export class BinaryFileHandler implements FileHandler { |
| 23 | async canHandle(filePath: string): Promise<boolean> { |
| 24 | // Content-based binary detection using isBinaryFile |
| 25 | try { |
| 26 | return await isBinaryFile(filePath); |
| 27 | } catch (error) { |
| 28 | // If we can't check (file doesn't exist, etc.), don't handle it |
| 29 | return false; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | async read(filePath: string, options?: ReadOptions): Promise<FileResult> { |
| 34 | const instructions = this.getBinaryInstructions(filePath); |
| 35 | |
| 36 | return { |
| 37 | content: instructions, |
| 38 | mimeType: 'text/plain', |
| 39 | metadata: { |
| 40 | isBinary: true |
| 41 | } |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | async write(path: string, content: any): Promise<void> { |
| 46 | throw new Error('Cannot write binary files directly. Use start_process with appropriate tools (Python, Node.js libraries, command-line utilities).'); |
| 47 | } |
| 48 | |
| 49 | async getInfo(path: string): Promise<FileInfo> { |
| 50 | const stats = await fs.stat(path); |
| 51 | |
| 52 | return { |
| 53 | size: stats.size, |
| 54 | created: stats.birthtime, |
| 55 | modified: stats.mtime, |
| 56 | accessed: stats.atime, |
| 57 | isDirectory: stats.isDirectory(), |
| 58 | isFile: stats.isFile(), |
| 59 | permissions: stats.mode.toString(8).slice(-3), |
| 60 | fileType: 'binary', |
| 61 | metadata: { |
| 62 | isBinary: true |
| 63 | } |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Generate instructions for handling binary files |
| 69 | */ |
| 70 | private getBinaryInstructions(filePath: string): string { |
| 71 | const fileName = path.basename(filePath); |
| 72 | |
| 73 | return `Cannot read binary file as text: ${fileName} |
| 74 | |
| 75 | Use start_process + interact_with_process to analyze binary files with appropriate tools (Node.js or Python libraries, command-line utilities, etc.). |
| 76 | |
| 77 | The read_file tool only handles text files, images, and Excel files.`; |
| 78 | } |
| 79 | } |
nothing calls this directly
no outgoing calls
no test coverage detected