show git revision
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import { createEvent } from "./utils.mjs";
|
import { createEvent, getGitRevision } from "./utils.mjs";
|
||||||
import YouProvider from "./provider.mjs";
|
import YouProvider from "./provider.mjs";
|
||||||
import localtunnel from 'localtunnel';
|
import localtunnel from "localtunnel";
|
||||||
import { v4 as uuidv4 } from 'uuid';
|
import { v4 as uuidv4 } from "uuid";
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = process.env.PORT || 8080;
|
const port = process.env.PORT || 8080;
|
||||||
const validApiKey = process.env.PASSWORD;
|
const validApiKey = process.env.PASSWORD;
|
||||||
@@ -48,15 +48,15 @@ await provider.init(config);
|
|||||||
|
|
||||||
// handle preflight request
|
// handle preflight request
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
if (req.method === "OPTIONS") {
|
if (req.method === "OPTIONS") {
|
||||||
res.setHeader("Access-Control-Allow-Origin", "*");
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
||||||
res.setHeader("Access-Control-Allow-Methods", "*");
|
res.setHeader("Access-Control-Allow-Methods", "*");
|
||||||
res.setHeader("Access-Control-Allow-Headers", "*");
|
res.setHeader("Access-Control-Allow-Headers", "*");
|
||||||
res.setHeader("Access-Control-Max-Age", "86400");
|
res.setHeader("Access-Control-Max-Age", "86400");
|
||||||
res.status(200).end();
|
res.status(200).end();
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// openai format model request
|
// openai format model request
|
||||||
app.get("/v1/models", OpenAIApiKeyAuth, (req, res) => {
|
app.get("/v1/models", OpenAIApiKeyAuth, (req, res) => {
|
||||||
@@ -106,7 +106,13 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
|
|
||||||
// call provider to get completion
|
// call provider to get completion
|
||||||
await provider
|
await provider
|
||||||
.getCompletion(randomSession, jsonBody.messages, jsonBody.stream ? true : false, jsonBody.model, process.env.USE_CUSTOM_MODE == "true" ? true : false)
|
.getCompletion(
|
||||||
|
randomSession,
|
||||||
|
jsonBody.messages,
|
||||||
|
jsonBody.stream ? true : false,
|
||||||
|
jsonBody.model,
|
||||||
|
process.env.USE_CUSTOM_MODE == "true" ? true : false
|
||||||
|
)
|
||||||
.then(({ completion, cancel }) => {
|
.then(({ completion, cancel }) => {
|
||||||
completion.on("start", (id) => {
|
completion.on("start", (id) => {
|
||||||
if (jsonBody.stream) {
|
if (jsonBody.stream) {
|
||||||
@@ -207,7 +213,9 @@ app.post("/v1/chat/completions", OpenAIApiKeyAuth, (req, res) => {
|
|||||||
sexual: { filtered: false, severity: "safe" },
|
sexual: { filtered: false, severity: "safe" },
|
||||||
violence: { filtered: false, severity: "safe" },
|
violence: { filtered: false, severity: "safe" },
|
||||||
},
|
},
|
||||||
delta: { content: "Error occurred, please check the log.\n\n出现错误,请检查日志:<pre>" + error.stack || error + "</pre>" },
|
delta: {
|
||||||
|
content: "Error occurred, please check the log.\n\n出现错误,请检查日志:<pre>" + error.stack || error + "</pre>",
|
||||||
|
},
|
||||||
finish_reason: null,
|
finish_reason: null,
|
||||||
index: 0,
|
index: 0,
|
||||||
},
|
},
|
||||||
@@ -418,7 +426,8 @@ app.post("/v1/messages", AnthropicApiKeyAuth, (req, res) => {
|
|||||||
|
|
||||||
// handle other
|
// handle other
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
res.status(404).send("Not Found");
|
const { revision, branch } = getGitRevision();
|
||||||
|
res.status(404).send("Not Found (YouChat_Proxy " + revision + "@" + branch + ")");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(port, async () => {
|
app.listen(port, async () => {
|
||||||
@@ -427,29 +436,29 @@ app.listen(port, async () => {
|
|||||||
console.log(`Proxy is currently running with no authentication`);
|
console.log(`Proxy is currently running with no authentication`);
|
||||||
}
|
}
|
||||||
console.log(`Custom mode: ${process.env.USE_CUSTOM_MODE == "true" ? "enabled" : "disabled"}`);
|
console.log(`Custom mode: ${process.env.USE_CUSTOM_MODE == "true" ? "enabled" : "disabled"}`);
|
||||||
|
|
||||||
// 检查是否启用隧道
|
|
||||||
if (process.env.ENABLE_TUNNEL === "true") {
|
|
||||||
// 输出等待创建隧道的提示
|
|
||||||
console.log("创建隧道中...");
|
|
||||||
|
|
||||||
// 设置隧道配置
|
// 检查是否启用隧道
|
||||||
const tunnelOptions = { port: port };
|
if (process.env.ENABLE_TUNNEL === "true") {
|
||||||
if (process.env.SUBDOMAIN) {
|
// 输出等待创建隧道的提示
|
||||||
tunnelOptions.subdomain = process.env.SUBDOMAIN;
|
console.log("创建隧道中...");
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
// 设置隧道配置
|
||||||
const tunnel = await localtunnel(tunnelOptions);
|
const tunnelOptions = { port: port };
|
||||||
console.log(`隧道已成功创建,可通过以下URL访问: ${tunnel.url}/v1`);
|
if (process.env.SUBDOMAIN) {
|
||||||
|
tunnelOptions.subdomain = process.env.SUBDOMAIN;
|
||||||
|
}
|
||||||
|
|
||||||
tunnel.on('close', () => {
|
try {
|
||||||
console.log('已关闭隧道');
|
const tunnel = await localtunnel(tunnelOptions);
|
||||||
});
|
console.log(`隧道已成功创建,可通过以下URL访问: ${tunnel.url}/v1`);
|
||||||
} catch (error) {
|
|
||||||
console.error('创建隧道失败:', error);
|
tunnel.on("close", () => {
|
||||||
}
|
console.log("已关闭隧道");
|
||||||
}
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("创建隧道失败:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function AnthropicApiKeyAuth(req, res, next) {
|
function AnthropicApiKeyAuth(req, res, next) {
|
||||||
|
|||||||
@@ -1,6 +1,18 @@
|
|||||||
import * as docx from "docx";
|
import * as docx from "docx";
|
||||||
import cookie from "cookie";
|
import cookie from "cookie";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import { execSync } from "child_process";
|
||||||
|
|
||||||
|
function getGitRevision() {
|
||||||
|
// get git revision and branch
|
||||||
|
try {
|
||||||
|
const revision = execSync("git rev-parse --short HEAD").toString().trim();
|
||||||
|
const branch = execSync("git rev-parse --abbrev-ref HEAD").toString().trim();
|
||||||
|
return { revision, branch };
|
||||||
|
} catch (e) {
|
||||||
|
return { revision: "unknown", branch: "unknown" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function createDirectoryIfNotExists(dirPath) {
|
function createDirectoryIfNotExists(dirPath) {
|
||||||
if (!fs.existsSync(dirPath)) {
|
if (!fs.existsSync(dirPath)) {
|
||||||
@@ -97,4 +109,4 @@ function createEvent(event, data) {
|
|||||||
return `event: ${event}\ndata: ${data}\n\n`;
|
return `event: ${event}\ndata: ${data}\n\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export { createEvent, createDirectoryIfNotExists, sleep, extractCookie, getSessionCookie, createDocx };
|
export { createEvent, createDirectoryIfNotExists, sleep, extractCookie, getSessionCookie, createDocx, getGitRevision };
|
||||||
|
|||||||
Reference in New Issue
Block a user