fix(opencode): sanitize Bedrock document names from file attachments#37535
Draft
IbrahimKhan12 wants to merge 1 commit into
Draft
fix(opencode): sanitize Bedrock document names from file attachments#37535IbrahimKhan12 wants to merge 1 commit into
IbrahimKhan12 wants to merge 1 commit into
Conversation
Bedrock's Converse API rejects document names containing characters outside [alphanumeric, whitespace, hyphen, parentheses, square brackets]. File parts can carry a filesystem path or a synthetic URI (e.g. an MCP "slack-file://..." attachment, which surfaces because Bedrock does not accept PDFs in tool results, so opencode re-injects them as a user file part). The AI SDK passes that filename through verbatim as the Converse document name, so once such an attachment lands in history every subsequent request in the session fails with a ValidationException. Sanitize file-part filenames in the Bedrock branch of ProviderTransform.message (shared by the v1 and v2 request paths), mapping disallowed characters to a space and collapsing runs; drop the name entirely when nothing valid remains so the SDK generates one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #37191
Type of change
What does this PR do?
Bedrock's Converse API rejects a document
namethat contains anything outside[alphanumeric, whitespace, hyphen, parentheses, square brackets](or more than one consecutive whitespace), returningValidationExceptionand failing every subsequent request in the session.opencode hands the AI SDK a file part whose
filenamebecomes that document name (the@ai-sdk/amazon-bedrockSDK only strips the extension). Two sources produce invalid names:supportsMediaInToolResultinsession/message-v2.ts), so the attachment is re-injected as a user file part carrying its synthetic URI, e.g.slack-file://F0XXXXXXX— the:and/are rejected./Users/.../SOP.pdf.Fix: sanitize file-part filenames in the Bedrock branch of
ProviderTransform.message(the single place both the v1 and v2 request paths funnel through). Disallowed characters are mapped to a space and collapsed; if nothing valid remains the filename is dropped so the SDK falls back to its own generateddocument-Nname. Scoped to@ai-sdk/amazon-bedrocksince the restriction is Bedrock-specific; other providers are untouched.How did you verify your code works?
Added
bun testcoverage inpackages/opencode/test/provider/transform.test.tsthat drives the realProviderTransform.messagefor a Bedrock model and asserts the synthetic-URI and absolute-path cases produce a Bedrock-valid name, the empty case drops the filename, and non-Bedrock providers are unchanged. Confirmed the tests fail ondevwithout the change and pass with it.bun test test/provider/transform.test.ts— 349 pass, 0 failbun typecheck— cleanoxlint+prettier --checkon the changed files — cleanScreenshots / recordings
N/A — no UI change.
Checklist