Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

230 lines
7.0 KiB
TypeScript

import { readResponseTextWithLimit } from '@/lib/core/utils/stream-limits'
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'
const MAX_HTTP_RESPONSE_BODY_BYTES = 10 * 1024 * 1024
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)',
},
proxyUrl: {
type: 'string',
visibility: 'user-only',
description: 'Route the request through an http:// proxy (e.g. http://user:pass@host:port).',
},
timeout: {
type: 'number',
visibility: 'user-only',
description: 'Request timeout in milliseconds (default: 300000 = 5 minutes)',
},
retries: {
type: 'number',
visibility: 'hidden',
description:
'Number of retry attempts for retryable failures (timeouts, 429, 5xx). Default: 0 (no retries).',
},
retryDelayMs: {
type: 'number',
visibility: 'hidden',
description: 'Initial retry delay in milliseconds (default: 500)',
},
retryMaxDelayMs: {
type: 'number',
visibility: 'hidden',
description: 'Maximum delay between retries in milliseconds (default: 30000)',
},
retryNonIdempotent: {
type: 'boolean',
visibility: 'hidden',
description:
'Allow retries for non-idempotent methods like POST/PATCH (may create duplicate requests).',
},
},
request: {
url: (params: RequestParams) => {
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) {
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,
typeof value === 'object' ? JSON.stringify(value) : String(value)
)
}
})
return urlencoded.toString()
}
return params.body as Record<string, any>
}
return undefined
}) as (params: RequestParams) => Record<string, any> | string | FormData | undefined,
retry: {
enabled: true,
maxRetries: 0,
initialDelayMs: 500,
maxDelayMs: 30000,
retryIdempotentOnly: true,
},
},
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 responseText = await readResponseTextWithLimit(response, {
maxBytes: MAX_HTTP_RESPONSE_BODY_BYTES,
label: 'HTTP Request response body',
allowNoBodyFallback: true,
})
const data = contentType.includes('application/json') ? JSON.parse(responseText) : responseText
// 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 },
},
},
},
}