use custom fetch from separate package

This commit is contained in:
Nate 2024-12-08 19:40:59 -08:00
parent 608abeaa13
commit b155b29397
10 changed files with 55 additions and 123 deletions

View file

@ -3,6 +3,7 @@ import * as fs from "fs";
import os from "os";
import path from "path";
import { fetchwithRequestOptions } from "@continuedev/fetch";
import * as JSONC from "comment-json";
import * as tar from "tar";
@ -46,7 +47,6 @@ import { LLMReranker } from "../llm/llms/llm";
import TransformersJsEmbeddingsProvider from "../llm/llms/TransformersJsEmbeddingsProvider";
import { allTools } from "../tools";
import { copyOf } from "../util";
import { fetchwithRequestOptions } from "../util/fetchWithOptions";
import { GlobalContext } from "../util/GlobalContext";
import mergeJson from "../util/merge";
import {

View file

@ -1,5 +1,6 @@
import path from "path";
import { fetchwithRequestOptions } from "@continuedev/fetch";
import ignore from "ignore";
import { v4 as uuidv4 } from "uuid";
@ -27,7 +28,6 @@ import { callTool } from "./tools/callTool";
import { ChatDescriber } from "./util/chatDescriber";
import { logDevData } from "./util/devdata";
import { DevDataSqliteDb } from "./util/devdataSqlite";
import { fetchwithRequestOptions } from "./util/fetchWithOptions";
import { GlobalContext } from "./util/GlobalContext";
import historyManager from "./util/history";
import { editConfigJson, setupInitialDotContinueDirectory } from "./util/paths";

View file

@ -1,3 +1,4 @@
import { fetchwithRequestOptions } from "@continuedev/fetch";
import { findLlmInfo } from "@continuedev/llm-info";
import { BaseLlmApi, constructLlmApi } from "@continuedev/openai-adapters";
import Handlebars from "handlebars";
@ -18,7 +19,6 @@ import {
} from "../index.js";
import { logDevData } from "../util/devdata.js";
import { DevDataSqliteDb } from "../util/devdataSqlite.js";
import { fetchwithRequestOptions } from "../util/fetchWithOptions.js";
import mergeJson from "../util/merge.js";
import { renderChatMessage } from "../util/messageContent.js";
import { Telemetry } from "../util/posthog.js";

13
core/package-lock.json generated
View file

@ -13,6 +13,7 @@
"@aws-sdk/client-sagemaker-runtime": "^3.621.0",
"@aws-sdk/credential-providers": "^3.620.1",
"@continuedev/config-types": "^1.0.13",
"@continuedev/fetch": "^1.0.3",
"@continuedev/llm-info": "^1.0.2",
"@continuedev/openai-adapters": "../packages/openai-adapters",
"@modelcontextprotocol/sdk": "^1.0.0",
@ -3398,6 +3399,18 @@
"zod": "^3.23.8"
}
},
"node_modules/@continuedev/fetch": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@continuedev/fetch/-/fetch-1.0.3.tgz",
"integrity": "sha512-cGNCUEVnDfC+3O/n1PV+oYhS5yZRLR1Q7Njg929kVbA8kRHe5a3VdKINfFzWdKprOU4wipsXSliciZHCpbJJ9g==",
"dependencies": {
"@continuedev/config-types": "^1.0.5",
"follow-redirects": "^1.15.6",
"http-proxy-agent": "^7.0.2",
"https-proxy-agent": "^7.0.5",
"node-fetch": "^3.3.2"
}
},
"node_modules/@continuedev/llm-info": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@continuedev/llm-info/-/llm-info-1.0.2.tgz",

View file

@ -45,6 +45,7 @@
"@aws-sdk/client-sagemaker-runtime": "^3.621.0",
"@aws-sdk/credential-providers": "^3.620.1",
"@continuedev/config-types": "^1.0.13",
"@continuedev/fetch": "^1.0.3",
"@continuedev/llm-info": "^1.0.2",
"@continuedev/openai-adapters": "../packages/openai-adapters",
"@modelcontextprotocol/sdk": "^1.0.0",

View file

@ -1,114 +0,0 @@
import { globalAgent } from "https";
import * as fs from "node:fs";
import tls from "node:tls";
import * as followRedirects from "follow-redirects";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import fetch, { RequestInit, Response } from "node-fetch";
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: {[key: string]: any} = {
ca,
rejectUnauthorized: requestOptions?.verifySsl,
timeout,
sessionTimeout: timeout,
keepAlive: true,
keepAliveMsecs: timeout,
};
// Handle ClientCertificateOptions
if (requestOptions?.clientCertificate){
agentOptions.cert = fs.readFileSync(requestOptions.clientCertificate.cert,"utf8");
agentOptions.key = fs.readFileSync(requestOptions.clientCertificate.key,"utf8");
if(requestOptions.clientCertificate.passphrase){
agentOptions.passphrase = requestOptions.clientCertificate.passphrase;
}
}
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;
}

View file

@ -9,6 +9,7 @@
"version": "0.9.241",
"license": "Apache-2.0",
"dependencies": {
"@continuedev/fetch": "^1.0.3",
"@electron/rebuild": "^3.2.10",
"@reduxjs/toolkit": "^1.9.3",
"@types/node-fetch": "^2.6.11",
@ -101,6 +102,7 @@
"@aws-sdk/client-sagemaker-runtime": "^3.621.0",
"@aws-sdk/credential-providers": "^3.620.1",
"@continuedev/config-types": "^1.0.13",
"@continuedev/fetch": "^1.0.3",
"@continuedev/llm-info": "^1.0.2",
"@continuedev/openai-adapters": "../packages/openai-adapters",
"@modelcontextprotocol/sdk": "^1.0.0",
@ -679,6 +681,26 @@
"node": ">=14.21.3"
}
},
"node_modules/@continuedev/config-types": {
"version": "1.0.13",
"resolved": "https://registry.npmjs.org/@continuedev/config-types/-/config-types-1.0.13.tgz",
"integrity": "sha512-5Qmy2x58uhW1GYknypFT/7iwxEJSbt8gMgfQReV7d6xjbJ09ISOTki3hIfCilKrTCo0gIY6pzKEfCoQkUTEi6Q==",
"dependencies": {
"zod": "^3.23.8"
}
},
"node_modules/@continuedev/fetch": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/@continuedev/fetch/-/fetch-1.0.3.tgz",
"integrity": "sha512-cGNCUEVnDfC+3O/n1PV+oYhS5yZRLR1Q7Njg929kVbA8kRHe5a3VdKINfFzWdKprOU4wipsXSliciZHCpbJJ9g==",
"dependencies": {
"@continuedev/config-types": "^1.0.5",
"follow-redirects": "^1.15.6",
"http-proxy-agent": "^7.0.2",
"https-proxy-agent": "^7.0.5",
"node-fetch": "^3.3.2"
}
},
"node_modules/@electron/node-gyp": {
"version": "10.2.0-electron.1",
"resolved": "git+ssh://git@github.com/electron/node-gyp.git#06b29aafb7708acef8b3669835c8a7857ebc92d2",
@ -14864,6 +14886,14 @@
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/zod": {
"version": "3.23.8",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz",
"integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
}
}
}

View file

@ -663,6 +663,7 @@
"vscode-extension-tester": "^8.10.0"
},
"dependencies": {
"@continuedev/fetch": "^1.0.3",
"@electron/rebuild": "^3.2.10",
"@reduxjs/toolkit": "^1.9.3",
"@types/node-fetch": "^2.6.11",

View file

@ -1,5 +1,5 @@
import { fetchwithRequestOptions } from "@continuedev/fetch";
import { ContinueConfig, IDE } from "core";
import { fetchwithRequestOptions } from "core/util/fetchWithOptions";
import { QuickPickItem, window } from "vscode";
export async function getContextProviderItems({

View file

@ -1,12 +1,13 @@
import { RequestOptions } from "@continuedev/config-types/src/index.js";
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 "@continuedev/config-types";
import * as followRedirects from "follow-redirects";
import { HttpProxyAgent } from "http-proxy-agent";
import { HttpsProxyAgent } from "https-proxy-agent";
import fetch, { RequestInit, Response } from "node-fetch";
const { http, https } = (followRedirects as any).default;
export function fetchwithRequestOptions(