[PR #3108] Refactor File.Content to use discriminated union with MIME types #10536

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

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

State: closed
Merged: No


Summary

  • Refactor File.Content to a discriminated union for type-safe text vs binary
    handling
  • Add MIME detection and include it in binary responses; update SDK types and do
    cs

Changes

  • Replace File.Content with a discriminated union keyed by type:
    • text: { type: "text", content, diff?, patch? }
    • binary: { type: "binary", content /* base64 */, mimeType }
  • Add getMimeType(path) with common extension map and application/octet-strea m fallback
  • Add isBinaryFile(path, file) using extension heuristics plus a null-byte sca
    n of the first up to 512 bytes
  • Update read(path):
    • Return binary with base64 content and mimeType for detected binaries
    • Return text content; when tracked in git, compute diff and full patch
      (tries unstaged, then staged)
  • Update SDK generated types to expose FileContent as a discriminated union
  • Update SDK docs with usage examples for binary (data URL) vs text content

Benefits

  • Strong compile-time guarantees when handling text vs binary content across ser
    ver and SDK
  • Explicit mimeType metadata enables correct client-side rendering and process
    ing
  • Clear API: diff/patch only on text variant; binary is base64 with metadata
  • Simpler client logic using content.type discriminator
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/3108 **State:** closed **Merged:** No --- ## Summary - Refactor `File.Content` to a discriminated union for type-safe text vs binary handling - Add MIME detection and include it in binary responses; update SDK types and do cs ## Changes - Replace `File.Content` with a discriminated union keyed by `type`: - `text`: `{ type: "text", content, diff?, patch? }` - `binary`: `{ type: "binary", content /* base64 */, mimeType }` - Add `getMimeType(path)` with common extension map and `application/octet-strea m` fallback - Add `isBinaryFile(path, file)` using extension heuristics plus a null-byte sca n of the first up to 512 bytes - Update `read(path)`: - Return `binary` with base64 content and `mimeType` for detected binaries - Return `text` content; when tracked in git, compute `diff` and full `patch` (tries unstaged, then staged) - Update SDK generated types to expose `FileContent` as a discriminated union - Update SDK docs with usage examples for binary (data URL) vs text content ## Benefits - Strong compile-time guarantees when handling text vs binary content across ser ver and SDK - Explicit `mimeType` metadata enables correct client-side rendering and process ing - Clear API: diff/patch only on `text` variant; binary is base64 with metadata - Simpler client logic using `content.type` discriminator
yindo added the pull-request label 2026-02-16 18:15:13 -05:00
yindo closed this issue 2026-02-16 18:15:13 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#10536