Skip to content

fix#954

Merged
waleedlatif1 merged 2 commits intomainfrom
staging
Aug 13, 2025
Merged

fix#954
waleedlatif1 merged 2 commits intomainfrom
staging

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

Brief description of what this PR does and why.

Fixes #(issue)

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

How has this been tested? What should reviewers focus on?

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Screenshots/Videos

* fix(double-read-http): double reading body json

* fix

* fix tests
@vercel
Copy link

vercel bot commented Aug 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Project Deployment Preview Comments Updated (UTC)
sim (staging) 🔄 Building Aug 13, 2025 4:11am
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
docs ⬜️ Skipped Aug 13, 2025 4:11am

@waleedlatif1 waleedlatif1 merged commit 4107948 into main Aug 13, 2025
5 of 7 checks passed
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greptile Summary

This PR fixes critical "double-read" issues in the API block tool execution system that were preventing proper HTTP request handling across multiple components. The changes address fundamental problems with HTTP response body consumption in JavaScript/Node.js, where response streams can only be read once before becoming unavailable.

The core issue stemmed from the tool execution system attempting to read HTTP response bodies multiple times - once for error checking and again for response transformation. This caused "body already read" errors that broke API block functionality. The PR implements several coordinated fixes:

In tools/utils.ts, the formatRequestParams function now properly handles dynamic HTTP method determination by supporting both function and string types for tool.request.method. This ensures compatibility with tools like the HTTP request tool that determine methods dynamically. The body handling logic was also corrected to check actual body content rather than just tool configuration.

The tools/index.ts file received the most critical fix - restructuring response handling to use a single clone for error checking while preserving the original response object for transformation. This prevents multiple consumption of the same response stream.

In tools/http/request.ts, the proxy handling mechanism was significantly simplified by removing complex URL encoding logic and routing all external requests uniformly through the POST /api/proxy endpoint. The transformResponse function was enhanced with response.clone() to safely inspect JSON responses without consuming the original body.

The proxy API route (app/api/proxy/route.ts) was updated to prevent request body parsing issues by reading requests as text first before JSON parsing, avoiding stream consumption conflicts.

Finally, test utilities in tools/__test-utils__/test-tools.ts were enhanced with proper response cloning support and a factory pattern that creates fresh response objects for each clone, ensuring tests don't fail when production code clones responses.

These changes work together to create a robust HTTP request handling system that properly manages response stream consumption while maintaining backward compatibility with existing tool configurations.

Confidence score: 4/5

  • This PR addresses critical production issues with well-coordinated fixes across multiple components
  • Score reflects comprehensive testing considerations and solid technical approach to HTTP stream management
  • Pay close attention to the response cloning logic in tools/index.ts and proxy routing changes in tools/http/request.ts

5 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

return {
success: response.ok,
output: {
data: jsonResponse ?? (await response.text()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Potential issue: If response.clone().json() succeeds but response.text() fails, this could throw an error since the original response body was already consumed by clone().json().

Suggested change
data: jsonResponse ?? (await response.text()),
data: jsonResponse ?? (await response.clone().text()),

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants