diff --git a/core/config/load.ts b/core/config/load.ts index 0df921486..b28734c46 100644 --- a/core/config/load.ts +++ b/core/config/load.ts @@ -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 { diff --git a/core/core.ts b/core/core.ts index ddfacf5b9..012931315 100644 --- a/core/core.ts +++ b/core/core.ts @@ -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"; diff --git a/core/llm/index.ts b/core/llm/index.ts index c2254c767..a4a6cabff 100644 --- a/core/llm/index.ts +++ b/core/llm/index.ts @@ -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"; diff --git a/core/package-lock.json b/core/package-lock.json index a4f3c9c1d..86f6c3c77 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -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", diff --git a/core/package.json b/core/package.json index ed66a7252..a65847b5a 100644 --- a/core/package.json +++ b/core/package.json @@ -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", diff --git a/core/util/fetchWithOptions.ts b/core/util/fetchWithOptions.ts deleted file mode 100644 index 1d2a8e94f..000000000 --- a/core/util/fetchWithOptions.ts +++ /dev/null @@ -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 { - 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; -} diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 27a4e01fb..ed21c3a69 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -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" + } } } } diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 9cf7db2b1..320a08fab 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -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", diff --git a/extensions/vscode/src/quickEdit/ContextProvidersQuickPick.ts b/extensions/vscode/src/quickEdit/ContextProvidersQuickPick.ts index 02521c2ae..1296a3466 100644 --- a/extensions/vscode/src/quickEdit/ContextProvidersQuickPick.ts +++ b/extensions/vscode/src/quickEdit/ContextProvidersQuickPick.ts @@ -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({ diff --git a/packages/fetch/src/fetch.ts b/packages/fetch/src/fetch.ts index 8e9ffc833..01fc631db 100644 --- a/packages/fetch/src/fetch.ts +++ b/packages/fetch/src/fetch.ts @@ -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(