continue-custom/core/util/fetchWithOptions.ts
Nate Sesti ca884b8212
fix ollama onboarding (#1539)
* allow setting apiBase for bedrock provider

* ⚡️ better linking

* ✨ new prompt file action

* ⚡️ strip <COMPLETION> from GPT-4 completion

* continue proxy FIM support

* warn user if potentially dangerous command is generated

* tab autocomplete docs

* docs: update PR template (#1531)

* chore: add docs to install script (#1533)

* fix(extensions): schema for db ctx provider (#1534)

* fix(extensions): schema for db ctx provider

* fix: invalid enum

* prettierignore

* docs: add docs and schema for "OS" provider (#1536)

* tests (#1501)

* 👷 CI for jetbrains

* default working dir

* changelog

* build binaries

* binary testing setup

* idesettings

* core binary testing

* run binary tests in ci

* remove unused targets

* needs build

* console.log bin contents

* fix ci

* fix win32 binary download

* test

* no linux arm64

* macos latest

* macos-12

* binary permissions

* upload logs

* fix

* upload full folder as binary artifact

* test

* test macos only

* set full execute permissions

* copy sqlite binary

* cd

* it worked!

* build again in test job

* debug

* remove timeout

* info

* log

* log2

* more logs

* catch

* fewer logs

* test all platforms

* test downloaded artifact

* needs build

* updates

* build

* false

* release

* add tag and upload binaryes

* change tag name

* proper artifact upload

* jest updates

* ✅ generate a few unit tests with Continue

* fix imports related to IdeSettings

* run tsc on PRs

* remove shareSession command (unused)

* update release process

* update plugin version

* don't show ghost text when jetbrains completion visible

* run jetbrains ci in main

* check ts in dev

* ignore .env

* 🚑 fix constant warnings when onboarding with Ollama

---------

Co-authored-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>
2024-06-21 11:09:03 -07:00

102 lines
2.9 KiB
TypeScript

import * as followRedirects from "follow-redirects";
import { HttpProxyAgent } from "http-proxy-agent";
import { globalAgent } from "https";
import { HttpsProxyAgent } from "https-proxy-agent";
import fetch, { RequestInit, Response } from "node-fetch";
import * as fs from "node:fs";
import tls from "node:tls";
import { RequestOptions } from "../index.js";
const { http, https } = (followRedirects as any).default;
export function fetchwithRequestOptions(
url_: URL | string,
init?: RequestInit,
requestOptions?: RequestOptions,
): Promise<Response> {
let url = url_;
if (typeof url === "string") {
url = new URL(url);
}
const TIMEOUT = 7200; // 7200 seconds = 2 hours
let globalCerts: string[] = [];
if (process.env.IS_BINARY) {
if (Array.isArray(globalAgent.options.ca)) {
globalCerts = [...globalAgent.options.ca.map((cert) => cert.toString())];
} else if (typeof globalAgent.options.ca !== "undefined") {
globalCerts.push(globalAgent.options.ca.toString());
}
}
const ca = Array.from(new Set([...tls.rootCertificates, ...globalCerts]));
const customCerts =
typeof requestOptions?.caBundlePath === "string"
? [requestOptions?.caBundlePath]
: requestOptions?.caBundlePath;
if (customCerts) {
ca.push(
...customCerts.map((customCert) => fs.readFileSync(customCert, "utf8")),
);
}
const timeout = (requestOptions?.timeout ?? TIMEOUT) * 1000; // measured in ms
const agentOptions = {
ca,
rejectUnauthorized: requestOptions?.verifySsl,
timeout,
sessionTimeout: timeout,
keepAlive: true,
keepAliveMsecs: timeout,
};
const proxy = requestOptions?.proxy;
// Create agent
const protocol = url.protocol === "https:" ? https : http;
const agent =
proxy && !requestOptions?.noProxy?.includes(url.hostname)
? protocol === https
? new HttpsProxyAgent(proxy, agentOptions)
: new HttpProxyAgent(proxy, agentOptions)
: new protocol.Agent(agentOptions);
let headers: { [key: string]: string } = {};
for (const [key, value] of Object.entries(init?.headers || {})) {
headers[key] = value as string;
}
headers = {
...headers,
...requestOptions?.headers,
};
// Replace localhost with 127.0.0.1
if (url.hostname === "localhost") {
url.hostname = "127.0.0.1";
}
// add extra body properties if provided
let updatedBody: string | undefined = undefined;
try {
if (requestOptions?.extraBodyProperties && typeof init?.body === "string") {
const parsedBody = JSON.parse(init.body);
updatedBody = JSON.stringify({
...parsedBody,
...requestOptions.extraBodyProperties,
});
}
} catch (e) {
console.log("Unable to parse HTTP request body: ", e);
}
// fetch the request with the provided options
const resp = fetch(url, {
...init,
body: updatedBody ?? init?.body,
headers: headers,
agent: agent,
});
return resp;
}