Skip to content

fix(opencode): use detected MIME type for --file attachments - #34369

Closed
weiconghe wants to merge 3 commits into
anomalyco:devfrom
weiconghe:fix-file-mime-type
Closed

weiconghe wants to merge 3 commits into
anomalyco:devfrom
weiconghe:fix-file-mime-type

Conversation

@weiconghe

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #34318

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When using the --file flag in local mode, all files were hardcoded as text/plain regardless of their actual type. Image files (PNG, JPEG, etc.) were passed to the LLM as garbled UTF-8 text instead of base64-encoded images.

Root cause: run.ts line 340 hardcoded mime to "text/plain" for all non-directory files. Filesystem.mimeType was already available but the result was discarded.

Fix: Changed line 340 to use await Filesystem.mimeType(resolvedPath) which correctly detects MIME type based on file extension, falling back to application/octet-stream for unknown types.

How did you verify your code works?

Added test/cli/run/file-mime.test.ts with 10 tests covering:

  • PNG files return image/png
  • JPEG files return image/jpeg
  • TXT files return text/plain
  • JSON files return application/json
  • Unknown extensions return application/octet-stream
  • GIF, WebP, PDF files return correct types
cd packages/opencode
bun test test/cli/run/file-mime.test.ts --timeout 30000

Result: 10 pass, 0 fail

Screenshots / recordings

N/A — logic fix, no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

The instance-context middleware unconditionally called InstanceStore.load
with whatever directory the client supplied, so requests that referenced
a stale path (e.g. after the user moved the project folder) caused the
server to bootstrap a phantom instance for a directory that doesn't exist
on disk. Downstream calls on that instance then surfaced as 503 errors,
and nothing pruned the stale entry from the client's cached project list.

Add an fs.existsSafe guard at the middleware boundary: when the resolved
directory does not exist, return 404 immediately so the client can drop
the stale entry rather than retrying against a phantom instance.

Fixes anomalyco#31888.
… variants

The OpenAI-compatible branch returned { high, max } but max is not part
of the OpenAI reasoning_effort enum. Spec-compliant upstreams (e.g.
hyper, Cloudflare AI Gateway) reject max with 400. The OpenRouter
branch already correctly uses xhigh. This aligns the OpenAI-compatible
branch to match.

Fixes anomalyco#34278
The --file flag hardcoded text/plain for all non-directory files,
discarding the result of Filesystem.mimeType(). This caused image files
(PNG, JPEG, etc.) to be passed to the LLM as garbled UTF-8 text instead
of base64-encoded images.

Now uses Filesystem.mimeType() to detect the correct MIME type based on
file extension, falling back to application/octet-stream for unknown
types.

Fixes anomalyco#34318
@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

POTENTIAL DUPLICATE FOUND:

This PR appears to be addressing the exact same issue as PR #34369 (the current PR). Both target fixing MIME type detection for --file attachments in local mode, where image files were previously hardcoded as text/plain instead of using their actual MIME types.

You should check if PR #34319 has already been merged or is still open, and coordinate with the other PR to avoid duplicate work.

@weiconghe

Copy link
Copy Markdown
Contributor Author

Closing as duplicate of #34319. My PR includes 10 tests in \ est/cli/run/file-mime.test.ts\ — feel free to cherry-pick if useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--file flag hardcodes text/plain for all attachments breaking image uploads

1 participant