🚧 overwrite fetch to proxyFetch

This commit is contained in:
Nate Sesti 2023-12-10 13:50:52 -08:00
parent 558899acf8
commit a1ef91c535
7 changed files with 29 additions and 14 deletions

View file

@ -53,14 +53,6 @@ class Ollama extends LLM {
prompt: string,
options: CompletionOptions
): AsyncGenerator<string> {
// 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: {

View file

@ -22,3 +22,17 @@ export function removeQuotesAndEscapes(output: string): string {
return output;
}
export function proxyFetch(url: string, init?: RequestInit): Promise<Response> {
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,
});
}

View file

@ -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

View file

@ -348,7 +348,7 @@ export function getSidebarContent(
${page ? `<script>window.location.pathname = "${page}"</script>` : ""}
<iframe src="http://localhost:65433/sw"></iframe>
${false && `<iframe src="http://localhost:65433/sw"></iframe>`}
</body>
</html>`;
}

View file

@ -209,7 +209,6 @@ function ModelSelect(props: {}) {
useEffect(() => {
setOptions(
allModels.map((model) => {
console.log(modelSelectTitle(model));
return {
value: model.title,
title: modelSelectTitle(model),

View file

@ -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(
<React.StrictMode>
<Provider store={store}>

View file

@ -78,7 +78,6 @@ function TableRow({
// Save current session
saveSession();
console.log(session);
const json: PersistedSessionInfo = await loadSession(
session.sessionId
);