({
action,
parameters,
organization,
userApiKey,
stream,
}: ActionToHttpRequest)
| 38 | } |
| 39 | |
| 40 | export function constructHttpRequest({ |
| 41 | action, |
| 42 | parameters, |
| 43 | organization, |
| 44 | userApiKey, |
| 45 | stream, |
| 46 | }: ActionToHttpRequest): { url: string; requestOptions: RequestInit } { |
| 47 | /** Constructs an HTTP request from an action and parameters |
| 48 | * |
| 49 | * @param action - The action to construct the request from |
| 50 | * @param parameters - The parameters output by the AI to use in the API call |
| 51 | * @param organization - The organization info to use for the API call |
| 52 | * @param userApiKey - The user's API key to use for the API call |
| 53 | * @param stream - The stream function to use for logging |
| 54 | * **/ |
| 55 | if (!action.path) { |
| 56 | throw new Error("Path is not provided"); |
| 57 | } |
| 58 | if (!action.request_method) { |
| 59 | throw new Error("Request method is not provided"); |
| 60 | } |
| 61 | if (!action.api.api_host) { |
| 62 | throw new Error("API host has not been provided"); |
| 63 | } |
| 64 | |
| 65 | console.log("Constructing http request for action:", action.name); |
| 66 | console.log("parameters:", parameters); |
| 67 | |
| 68 | const headers: Record<string, string> = {}; |
| 69 | // TODO: You can only overwrite this header if it's in the parameters |
| 70 | headers["Accept"] = "application/json"; |
| 71 | |
| 72 | if (action.api.api_host.includes("api/mock")) |
| 73 | headers["org_id"] = organization.id.toString(); |
| 74 | // This header is only required for requests with a body |
| 75 | if (action.request_body_contents) |
| 76 | headers["Content-Type"] = "application/json"; |
| 77 | action.headers |
| 78 | // Filter out headers without names |
| 79 | .filter((header) => header.name) |
| 80 | .forEach((header) => { |
| 81 | headers[header.name] = header.value; |
| 82 | }); |
| 83 | |
| 84 | const requestOptions: RequestInit = { |
| 85 | method: action.request_method.toUpperCase(), |
| 86 | }; |
| 87 | |
| 88 | // Request body |
| 89 | if (action.request_method !== "GET" && action.request_body_contents) { |
| 90 | const schema = bodyPropertiesFromRequestBodyContents( |
| 91 | action.request_body_contents, |
| 92 | ); |
| 93 | const allParams = fillNoChoiceRequiredParams(parameters, schema); |
| 94 | const body = buildBody(schema, allParams); |
| 95 | requestOptions.body = JSON.stringify(body); |
| 96 | } |
| 97 |
no test coverage detected