rewrites urls to deal with client idiosyncrasies

This commit is contained in:
nai-degen 2023-04-11 03:20:19 -07:00 committed by nai-degen
parent 44f667976a
commit 920612f1ce
2 changed files with 13 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import { Request, Response, Router } from "express";
import { Request, Response, NextFunction, Router } from "express";
import * as http from "http";
import { createProxyMiddleware } from "http-proxy-middleware";
import { logger } from "../logger";
@ -69,6 +69,15 @@ const openaiProxy = createProxyMiddleware({
});
const openaiRouter = Router();
// Some clients don't include the /v1/ prefix in their requests and users get
// confused when they get a 404. Just fix the route for them so I don't have to
// provide a bunch of different routes for each client's idiosyncrasies.
openaiRouter.use((req, _res, next) => {
if (!req.path.startsWith("/v1/")) {
req.url = `/v1${req.url}`;
}
next();
});
openaiRouter.get("/v1/models", openaiProxy);
openaiRouter.post("/v1/chat/completions", ipLimiter, openaiProxy);
openaiRouter.use((req, res) => {

View File

@ -15,14 +15,14 @@ router.use(auth);
router.use("/kobold", kobold);
router.use("/openai", openai);
// SillyTavern annoyingly just disregards the path in whatever URL users input,
// so requests come in at /api/v1. We need to rewrite them to
// /proxy/kobold/api/v1 so the request is routed to the correct handler.
// Each client handles the endpoints input by the user in slightly different
// ways, eg TavernAI ignores everything after the hostname in Kobold mode
function rewriteTavernRequests(
req: express.Request,
_res: express.Response,
next: express.NextFunction
) {
// Requests coming into /api/v1 are actually requests to /proxy/kobold/api/v1
if (req.path.startsWith("/api/v1")) {
req.url = req.url.replace("/api/v1", "/proxy/kobold/api/v1");
}