in src/requests/request.ts [128:204]
headers: await getHeaders(url),
body,
},
};
}
export async function makeModelRequest(
model: string,
task: Task,
apiKey: string,
stream: boolean,
body: string,
requestOptions: SingleRequestOptions = {},
// Allows this to be stubbed for tests
fetchFn = fetch,
): Promise<Response> {
const { url, fetchOptions } = await constructModelRequest(
model,
task,
apiKey,
stream,
body,
requestOptions,
);
return makeRequest(url, fetchOptions, fetchFn);
}
export async function makeRequest(
url: string,
fetchOptions: RequestInit,
fetchFn = fetch,
): Promise<Response> {
let response;
try {
response = await fetchFn(url, fetchOptions);
} catch (e) {
handleResponseError(e, url);
}
if (!response.ok) {
await handleResponseNotOk(response, url);
}
return response;
}
function handleResponseError(e: Error, url: string): void {
let err = e;
if (err.name === "AbortError") {
err = new GoogleGenerativeAIAbortError(
`Request aborted when fetching ${url.toString()}: ${e.message}`,
);
err.stack = e.stack;
} else if (
!(
e instanceof GoogleGenerativeAIFetchError ||
e instanceof GoogleGenerativeAIRequestInputError
)
) {
err = new GoogleGenerativeAIError(
`Error fetching from ${url.toString()}: ${e.message}`,
);
err.stack = e.stack;
}
throw err;
}
async function handleResponseNotOk(
response: Response,
url: string,
): Promise<void> {
let message = "";
let errorDetails;
try {
const json = await response.json();
message = json.error.message;
if (json.error.details) {