From a1ef91c53559cf1f6b26ac2e491e3c415ff97ab6 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sun, 10 Dec 2023 13:50:52 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20overwrite=20fetch=20to=20proxyFe?= =?UTF-8?q?tch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/llm/llms/Ollama.ts | 8 -------- core/util/index.ts | 14 ++++++++++++++ extensions/vscode/src/activation/proxy.ts | 13 ++++++++++--- extensions/vscode/src/debugPanel.ts | 2 +- gui/src/components/modelSelection/ModelSelect.tsx | 1 - gui/src/main.tsx | 4 ++++ gui/src/pages/history.tsx | 1 - 7 files changed, 29 insertions(+), 14 deletions(-) diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 8ac57cc68..6909230f1 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -53,14 +53,6 @@ class Ollama extends LLM { prompt: string, options: CompletionOptions ): AsyncGenerator { - // const response = await fetch(`http://localhost:65433`, { - // method: "POST", - // headers: { - // "Content-Type": "application/json", - // "x-continue-url": `${this.apiBase}/api/generate`, - // }, - // body: JSON.stringify(this._convertArgs(options, prompt)), - // }); const response = await fetch(`${this.apiBase}/api/generate`, { method: "POST", headers: { diff --git a/core/util/index.ts b/core/util/index.ts index 914481b33..721877a36 100644 --- a/core/util/index.ts +++ b/core/util/index.ts @@ -22,3 +22,17 @@ export function removeQuotesAndEscapes(output: string): string { return output; } + +export function proxyFetch(url: string, init?: RequestInit): Promise { + if (!(window as any)._fetch) { + throw new Error("Proxy fetch not initialized"); + } + + const headers = new Headers(init?.headers); + headers.append("x-continue-url", url); + + return (window as any)._fetch("http://localhost:65433", { + ...init, + headers, + }); +} diff --git a/extensions/vscode/src/activation/proxy.ts b/extensions/vscode/src/activation/proxy.ts index 9859b3078..5cae543aa 100644 --- a/extensions/vscode/src/activation/proxy.ts +++ b/extensions/vscode/src/activation/proxy.ts @@ -1,6 +1,7 @@ import cors from "cors"; import express from "express"; import http from "http"; +import https from "https"; const PROXY_PORT = 65433; const app = express(); @@ -19,7 +20,10 @@ app.use((req, res, next) => { // Proxy the request const { origin, ...headers } = req.headers; - const proxy = http.request(req.headers["x-continue-url"] as string, { + const url = req.headers["x-continue-url"] as string; + const protocolString = url.split("://")[0]; + const protocol = protocolString === "https" ? https : http; + const proxy = protocol.request(url, { method: req.method, headers: headers, }); @@ -28,9 +32,12 @@ app.use((req, res, next) => { response.pipe(res); }); - // Pipe the request stream directly to the proxy + proxy.on("error", (error) => { + console.error(error); + res.sendStatus(500); + }); + req.pipe(proxy); - proxy.end(); }); // http-middleware-proxy diff --git a/extensions/vscode/src/debugPanel.ts b/extensions/vscode/src/debugPanel.ts index bf1e978cf..5c916cfcf 100644 --- a/extensions/vscode/src/debugPanel.ts +++ b/extensions/vscode/src/debugPanel.ts @@ -348,7 +348,7 @@ export function getSidebarContent( ${page ? `` : ""} - + ${false && ``} `; } diff --git a/gui/src/components/modelSelection/ModelSelect.tsx b/gui/src/components/modelSelection/ModelSelect.tsx index 598369fb9..bdbd62c98 100644 --- a/gui/src/components/modelSelection/ModelSelect.tsx +++ b/gui/src/components/modelSelection/ModelSelect.tsx @@ -209,7 +209,6 @@ function ModelSelect(props: {}) { useEffect(() => { setOptions( allModels.map((model) => { - console.log(modelSelectTitle(model)); return { value: model.title, title: modelSelectTitle(model), diff --git a/gui/src/main.tsx b/gui/src/main.tsx index c1b9aca72..1688d937e 100644 --- a/gui/src/main.tsx +++ b/gui/src/main.tsx @@ -5,8 +5,12 @@ import App from "./App"; import "./index.css"; import { store } from "./redux/store"; +import { proxyFetch } from "core/util"; import CustomPostHogProvider from "./hooks/CustomPostHogProvider"; +(window as any)._fetch = window.fetch; +window.fetch = proxyFetch; + ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( diff --git a/gui/src/pages/history.tsx b/gui/src/pages/history.tsx index d0433f079..a45d4745c 100644 --- a/gui/src/pages/history.tsx +++ b/gui/src/pages/history.tsx @@ -78,7 +78,6 @@ function TableRow({ // Save current session saveSession(); - console.log(session); const json: PersistedSessionInfo = await loadSession( session.sessionId );