rewrites urls to deal with client idiosyncrasies
This commit is contained in:
parent
44f667976a
commit
920612f1ce
|
@ -1,4 +1,4 @@
|
||||||
import { Request, Response, Router } from "express";
|
import { Request, Response, NextFunction, Router } from "express";
|
||||||
import * as http from "http";
|
import * as http from "http";
|
||||||
import { createProxyMiddleware } from "http-proxy-middleware";
|
import { createProxyMiddleware } from "http-proxy-middleware";
|
||||||
import { logger } from "../logger";
|
import { logger } from "../logger";
|
||||||
|
@ -69,6 +69,15 @@ const openaiProxy = createProxyMiddleware({
|
||||||
});
|
});
|
||||||
|
|
||||||
const openaiRouter = Router();
|
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.get("/v1/models", openaiProxy);
|
||||||
openaiRouter.post("/v1/chat/completions", ipLimiter, openaiProxy);
|
openaiRouter.post("/v1/chat/completions", ipLimiter, openaiProxy);
|
||||||
openaiRouter.use((req, res) => {
|
openaiRouter.use((req, res) => {
|
||||||
|
|
|
@ -15,14 +15,14 @@ router.use(auth);
|
||||||
router.use("/kobold", kobold);
|
router.use("/kobold", kobold);
|
||||||
router.use("/openai", openai);
|
router.use("/openai", openai);
|
||||||
|
|
||||||
// SillyTavern annoyingly just disregards the path in whatever URL users input,
|
// Each client handles the endpoints input by the user in slightly different
|
||||||
// so requests come in at /api/v1. We need to rewrite them to
|
// ways, eg TavernAI ignores everything after the hostname in Kobold mode
|
||||||
// /proxy/kobold/api/v1 so the request is routed to the correct handler.
|
|
||||||
function rewriteTavernRequests(
|
function rewriteTavernRequests(
|
||||||
req: express.Request,
|
req: express.Request,
|
||||||
_res: express.Response,
|
_res: express.Response,
|
||||||
next: express.NextFunction
|
next: express.NextFunction
|
||||||
) {
|
) {
|
||||||
|
// Requests coming into /api/v1 are actually requests to /proxy/kobold/api/v1
|
||||||
if (req.path.startsWith("/api/v1")) {
|
if (req.path.startsWith("/api/v1")) {
|
||||||
req.url = req.url.replace("/api/v1", "/proxy/kobold/api/v1");
|
req.url = req.url.replace("/api/v1", "/proxy/kobold/api/v1");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue