[PR #3292] Add binary file support #10594

Closed
opened 2026-02-16 18:15:17 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/3292

State: closed
Merged: Yes


Summary

Adds support for reading binary files (images, PDFs, archives, etc.) in the file module. Previously, the system only handled text files.

Changes

  • Refactored FileContent from a plain object to a discriminated union with two variants:

    • type: "text" - text files with optional diff/patch metadata
    • type: "binary" - binary files with base64-encoded content, MIME type, and encoding field
  • Added isBinaryFile() detection function:

    • Uses Bun's MIME type detection to identify non-text content types
    • Falls back to null-byte scanning for unknown types
    • Only reads first 512 bytes for detection (no memory overhead for large files)
  • Binary files are returned as base64 strings with their MIME type from Bun

  • Missing files now return empty text content instead of erroring

  • Switched to type-only import for BunFile to reduce bundle size

Implementation Notes

The binary detection is memory-efficient: it uses file.slice(0, 512) to check only the first 512 bytes before reading the full file. This prevents large files from being loaded into memory during the detection phase.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/3292 **State:** closed **Merged:** Yes --- ## Summary Adds support for reading binary files (images, PDFs, archives, etc.) in the file module. Previously, the system only handled text files. ## Changes - Refactored `FileContent` from a plain object to a discriminated union with two variants: - `type: "text"` - text files with optional diff/patch metadata - `type: "binary"` - binary files with base64-encoded content, MIME type, and encoding field - Added `isBinaryFile()` detection function: - Uses Bun's MIME type detection to identify non-text content types - Falls back to null-byte scanning for unknown types - Only reads first 512 bytes for detection (no memory overhead for large files) - Binary files are returned as base64 strings with their MIME type from Bun - Missing files now return empty text content instead of erroring - Switched to type-only import for `BunFile` to reduce bundle size ## Implementation Notes The binary detection is memory-efficient: it uses `file.slice(0, 512)` to check only the first 512 bytes before reading the full file. This prevents large files from being loaded into memory during the detection phase.
yindo added the pull-request label 2026-02-16 18:15:17 -05:00
yindo closed this issue 2026-02-16 18:15:18 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#10594