(tool: ToolConfig, params: Record<string, any>)
| 88 | * Format request parameters based on tool configuration and provided params |
| 89 | */ |
| 90 | export function formatRequestParams(tool: ToolConfig, params: Record<string, any>): RequestParams { |
| 91 | // Process URL |
| 92 | const url = typeof tool.request.url === 'function' ? tool.request.url(params) : tool.request.url |
| 93 | |
| 94 | // Process method |
| 95 | const method = |
| 96 | typeof tool.request.method === 'function' |
| 97 | ? tool.request.method(params) |
| 98 | : params.method || tool.request.method || 'GET' |
| 99 | |
| 100 | // Process headers |
| 101 | const headers = tool.request.headers ? tool.request.headers(params) : {} |
| 102 | |
| 103 | // Process body |
| 104 | const hasBody = method !== 'GET' && method !== 'HEAD' && !!tool.request.body |
| 105 | const bodyResult = tool.request.body ? tool.request.body(params) : undefined |
| 106 | |
| 107 | // Special handling for NDJSON content type or 'application/x-www-form-urlencoded' |
| 108 | const isPreformattedContent = |
| 109 | headers['Content-Type'] === 'application/x-ndjson' || |
| 110 | headers['Content-Type'] === 'application/x-www-form-urlencoded' |
| 111 | |
| 112 | let body: string | undefined |
| 113 | if (hasBody) { |
| 114 | if (isPreformattedContent) { |
| 115 | // Check if bodyResult is a string |
| 116 | if (typeof bodyResult === 'string') { |
| 117 | body = bodyResult |
| 118 | } |
| 119 | // Check if bodyResult is an object with a 'body' property (Twilio pattern) |
| 120 | else if (bodyResult && typeof bodyResult === 'object' && 'body' in bodyResult) { |
| 121 | body = bodyResult.body |
| 122 | } |
| 123 | // Otherwise JSON stringify it |
| 124 | else { |
| 125 | body = JSON.stringify(bodyResult) |
| 126 | } |
| 127 | } else { |
| 128 | body = typeof bodyResult === 'string' ? bodyResult : JSON.stringify(bodyResult) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | const MAX_TIMEOUT_MS = getMaxExecutionTimeout() |
| 133 | const rawTimeout = params.timeout |
| 134 | const timeout = rawTimeout != null ? Number(rawTimeout) : undefined |
| 135 | const validTimeout = |
| 136 | timeout != null && Number.isFinite(timeout) && timeout > 0 |
| 137 | ? Math.min(timeout, MAX_TIMEOUT_MS) |
| 138 | : undefined |
| 139 | |
| 140 | return { url, method, headers, body, timeout: validTimeout } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Formats a parameter name for user-friendly error messages |
no test coverage detected