fix(core): replace response.text with collectBoundedResponseBody for websearch SSE handling#33464
Closed
enioclimacosalesjunior wants to merge 2 commits into
Closed
Conversation
…websearch to fix 400 error handling response.text from Effect's HttpClientResponse can fail for SSE responses (text/event-stream). Replace with collectBoundedResponseBody which reads raw bytes from the stream, manually tracks size limits, and converts to UTF-8 explicitly. This matches the upstream fix in commit 69f1ec2 that was applied after v1.17.9 but was missing from the tag. Also fixes the same issue in packages/opencode/src/tool/mcp-websearch.ts which had the same pattern.
Contributor
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
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.
Problem
The built-in
websearchtool returns HTTP 400 errors, reported by the model as "Tavily API error: 400 Bad Request". The root cause is that the websearch tool uses MCP endpoints (Exa/Parallel) that return Server-Sent Events (text/event-stream) responses. The v1.17.9 code reads the response body viaresponse.textfrom Effect'sHttpClientResponse, which can fail to properly decode SSE-formatted bodies.The same issue exists in both packages:
packages/core/src/tool/websearch.ts(the core tool implementation)packages/opencode/src/tool/mcp-websearch.ts(the opencode package MCP wrapper)Analysis
When
response.textis called on atext/event-streamresponse, the charset detection may behave incorrectly depending on the runtime and the Effect library version. The SSE response format (event: message\ndata: {...}\n\n) can cause decoding failures or produce malformed strings that the downstreamparseResponsefunction cannot handle. TheHttpClient.filterStatusOkcorrectly passes the 200 status, but the subsequent body decoding fails, resulting in aToolFailurethat the model interprets as a 400 error — even though the actual HTTP status is 200.Fix
Replace
response.textwithcollectBoundedResponseBody, which:response.streamMAX_RESPONSE_BYTES = 256 KB).toString("utf8")This approach is already used by the
webfetchtool (which has its owncollectBodyfunction) and was added upstream in this very commit69f1ec22e("fix(core): bound web tool failures").Changes
Added
packages/core/src/tool/http-body.ts— SharedcollectBoundedResponseBodyfunction that reads a bounded response body from a stream, checks content-length headers, and returns aBuffer. This file was referenced by the import inwebsearch.tsbut was missing from the v1.17.9 tag.Modified
packages/core/src/tool/websearch.ts— Import and usecollectBoundedResponseBodyinstead ofresponse.textpackages/opencode/src/tool/mcp-websearch.ts— Same fix applied to the opencode package's MCP websearch wrapperTesting
packages/core/test/tool-websearch.test.tscovers provider selection, plain JSON and SSE response parsing, oversized body rejection, and API key isolation. All tests pass with this fix.69f1ec22eon thedevbranch.Related
69f1ec22e("fix(core): bound web tool failures")