11import type { RequestParams , RequestResponse } from '@/tools/http/types'
2- import { getDefaultHeaders , processUrl , shouldUseProxy , transformTable } from '@/tools/http/utils'
2+ import { getDefaultHeaders , processUrl , transformTable } from '@/tools/http/utils'
33import type { ToolConfig } from '@/tools/types'
44
55export const requestTool : ToolConfig < RequestParams , RequestResponse > = {
@@ -53,34 +53,18 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
5353
5454 request : {
5555 url : ( params : RequestParams ) => {
56- // Process the URL first to handle path/query params
57- const processedUrl = processUrl ( params . url , params . pathParams , params . params )
58-
59- // For external URLs that need proxying in the browser, we still return the
60- // external URL here and let executeTool route through the POST /api/proxy
61- // endpoint uniformly. This avoids querystring body encoding and prevents
62- // the proxy GET route from being hit from the client.
63- if ( shouldUseProxy ( processedUrl ) ) {
64- return processedUrl
65- }
66-
67- return processedUrl
56+ // Process the URL once and cache the result
57+ return processUrl ( params . url , params . pathParams , params . params )
6858 } ,
6959
70- method : ( params : RequestParams ) => params . method || 'GET' ,
60+ method : ( params : RequestParams ) => {
61+ // Always return the user's intended method - executeTool handles proxy routing
62+ return params . method || 'GET'
63+ } ,
7164
7265 headers : ( params : RequestParams ) => {
7366 const headers = transformTable ( params . headers || null )
7467 const processedUrl = processUrl ( params . url , params . pathParams , params . params )
75-
76- // For proxied requests, we only need minimal headers
77- if ( shouldUseProxy ( processedUrl ) ) {
78- return {
79- 'Content-Type' : 'application/json' ,
80- }
81- }
82-
83- // For direct requests, add all our standard headers
8468 const allHeaders = getDefaultHeaders ( headers , processedUrl )
8569
8670 // Set appropriate Content-Type
@@ -96,13 +80,6 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
9680 } ,
9781
9882 body : ( params : RequestParams ) => {
99- const processedUrl = processUrl ( params . url , params . pathParams , params . params )
100-
101- // For proxied requests, we don't need a body
102- if ( shouldUseProxy ( processedUrl ) ) {
103- return undefined
104- }
105-
10683 if ( params . formData ) {
10784 const formData = new FormData ( )
10885 Object . entries ( params . formData ) . forEach ( ( [ key , value ] ) => {
@@ -120,63 +97,46 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
12097 } ,
12198
12299 transformResponse : async ( response : Response ) => {
123- // Build headers once for consistent return structures
100+ const contentType = response . headers . get ( 'content-type' ) || ''
101+
102+ // Standard response handling
124103 const headers : Record < string , string > = { }
125104 response . headers . forEach ( ( value , key ) => {
126105 headers [ key ] = value
127106 } )
128107
129- const contentType = response . headers . get ( 'content-type' ) || ''
130- const isJson = contentType . includes ( 'application/json' )
131-
132- if ( isJson ) {
133- // Use a clone to safely inspect JSON without consuming the original body
134- let jsonResponse : any
135- try {
136- jsonResponse = await response . clone ( ) . json ( )
137- } catch ( _e ) {
138- jsonResponse = undefined
139- }
140-
141- // Proxy responses wrap the real payload
142- if ( jsonResponse && jsonResponse . data !== undefined && jsonResponse . status !== undefined ) {
143- return {
144- success : jsonResponse . success ,
145- output : {
146- data : jsonResponse . data ,
147- status : jsonResponse . status ,
148- headers : jsonResponse . headers || { } ,
149- } ,
150- error : jsonResponse . success
151- ? undefined
152- : jsonResponse . data && typeof jsonResponse . data === 'object' && jsonResponse . data . error
153- ? `HTTP error ${ jsonResponse . status } : ${ jsonResponse . data . error . message || JSON . stringify ( jsonResponse . data . error ) } `
154- : jsonResponse . error || `HTTP error ${ jsonResponse . status } ` ,
155- }
156- }
157-
158- // Non-proxy JSON response: return parsed JSON directly
108+ const data = await ( contentType . includes ( 'application/json' )
109+ ? response . json ( )
110+ : response . text ( ) )
111+
112+ // Check if this is a proxy response (structured response from /api/proxy)
113+ if (
114+ contentType . includes ( 'application/json' ) &&
115+ typeof data === 'object' &&
116+ data !== null &&
117+ data . data !== undefined &&
118+ data . status !== undefined
119+ ) {
159120 return {
160- success : response . ok ,
121+ success : data . success ,
161122 output : {
162- data : jsonResponse ?? ( await response . text ( ) ) ,
163- status : response . status ,
164- headers,
123+ data : data . data ,
124+ status : data . status ,
125+ headers : data . headers || { } ,
165126 } ,
166- error : response . ok ? undefined : `HTTP error ${ response . status } : ${ response . statusText } ` ,
127+ error : data . success ? undefined : data . error ,
167128 }
168129 }
169130
170- // Non-JSON response: return text
171- const textData = await response . text ( )
131+ // Direct response handling
172132 return {
173133 success : response . ok ,
174134 output : {
175- data : textData ,
135+ data,
176136 status : response . status ,
177137 headers,
178138 } ,
179- error : response . ok ? undefined : `HTTP error ${ response . status } : ${ response . statusText } ` ,
139+ error : undefined , // Errors are handled upstream in executeTool
180140 }
181141 } ,
182142
0 commit comments