-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathrequest.ts
More file actions
187 lines (169 loc) · 5.63 KB
/
request.ts
File metadata and controls
187 lines (169 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type { RequestParams, RequestResponse } from '@/tools/http/types'
import { getDefaultHeaders, processUrl } from '@/tools/http/utils'
import { transformTable } from '@/tools/shared/table'
import type { ToolConfig } from '@/tools/types'
export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
id: 'http_request',
name: 'HTTP Request',
description:
'Make HTTP requests with comprehensive support for methods, headers, query parameters, path parameters, and form data. Features configurable timeout and status validation for robust API interactions.',
version: '1.0.0',
params: {
url: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'The URL to send the request to',
},
method: {
type: 'string',
default: 'GET',
visibility: 'user-or-llm',
description: 'HTTP method (GET, POST, PUT, PATCH, DELETE)',
},
headers: {
type: 'object',
visibility: 'user-or-llm',
description: 'HTTP headers to include',
},
body: {
type: 'object',
visibility: 'user-or-llm',
description: 'Request body (for POST, PUT, PATCH)',
},
params: {
type: 'object',
visibility: 'user-or-llm',
description: 'URL query parameters to append',
},
pathParams: {
type: 'object',
visibility: 'user-or-llm',
description: 'URL path parameters to replace (e.g., :id in /users/:id)',
},
formData: {
type: 'object',
visibility: 'user-or-llm',
description: 'Form data to send (will set appropriate Content-Type)',
},
timeout: {
type: 'number',
visibility: 'user-only',
description: 'Request timeout in milliseconds (default: 300000 = 5 minutes)',
},
},
request: {
url: (params: RequestParams) => {
// Process the URL once and cache the result
return processUrl(params.url, params.pathParams, params.params)
},
method: (params: RequestParams) => {
// Always return the user's intended method - executeTool handles proxy routing
return params.method || 'GET'
},
headers: (params: RequestParams) => {
const headers = transformTable(params.headers || null)
const processedUrl = processUrl(params.url, params.pathParams, params.params)
const allHeaders = getDefaultHeaders(headers, processedUrl)
// Set appropriate Content-Type only if not already specified by user
if (params.formData) {
// Don't set Content-Type for FormData, browser will set it with boundary
return allHeaders
}
if (params.body && !allHeaders['Content-Type'] && !allHeaders['content-type']) {
allHeaders['Content-Type'] = 'application/json'
}
return allHeaders
},
body: ((params: RequestParams) => {
if (params.formData) {
const formData = new FormData()
Object.entries(params.formData).forEach(([key, value]) => {
formData.append(key, value)
})
return formData
}
if (params.body) {
// Check if user wants URL-encoded form data
const headers = transformTable(params.headers || null)
const contentType = headers['Content-Type'] || headers['content-type']
if (
contentType === 'application/x-www-form-urlencoded' &&
typeof params.body === 'object'
) {
// Convert JSON object to URL-encoded string
const urlencoded = new URLSearchParams()
Object.entries(params.body as Record<string, unknown>).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
urlencoded.append(key, String(value))
}
})
return urlencoded.toString()
}
return params.body as Record<string, any>
}
return undefined
}) as (params: RequestParams) => Record<string, any> | string | FormData | undefined,
},
transformResponse: async (response: Response) => {
const contentType = response.headers.get('content-type') || ''
// Standard response handling
const headers: Record<string, string> = {}
response.headers.forEach((value, key) => {
headers[key] = value
})
const data = await (contentType.includes('application/json')
? response.json()
: response.text())
// Check if this is a proxy response (structured response from /api/proxy)
if (
contentType.includes('application/json') &&
typeof data === 'object' &&
data !== null &&
data.data !== undefined &&
data.status !== undefined
) {
return {
success: data.success,
output: {
data: data.data,
status: data.status,
headers: data.headers || {},
},
error: data.success ? undefined : data.error,
}
}
// Direct response handling
return {
success: response.ok,
output: {
data,
status: response.status,
headers,
},
error: undefined, // Errors are handled upstream in executeTool
}
},
outputs: {
data: {
type: 'json',
description: 'Response data from the HTTP request (JSON object, text, or other format)',
},
status: {
type: 'number',
description: 'HTTP status code of the response (e.g., 200, 404, 500)',
},
headers: {
type: 'object',
description: 'Response headers as key-value pairs',
properties: {
'content-type': {
type: 'string',
description: 'Content type of the response',
optional: true,
},
'content-length': { type: 'string', description: 'Content length', optional: true },
},
},
},
}