Skip to content

Commit

Permalink
Merge branch 'TBXark:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
szthanatos authored Jul 8, 2023
2 parents 648726c + 7acd3d2 commit 7419622
Show file tree
Hide file tree
Showing 11 changed files with 726 additions and 599 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha": "eb7c605", "timestamp": 1684307553}
{"sha": "8cb6fa9", "timestamp": 1685351142}
61 changes: 39 additions & 22 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ var ENV = {
// 检查更新的分支
UPDATE_BRANCH: "master",
// 当前版本
BUILD_TIMESTAMP: 1684307553,
BUILD_TIMESTAMP: 1685351142,
// 当前版本 commit id
BUILD_VERSION: "eb7c605",
BUILD_VERSION: "8cb6fa9",
I18N: null,
LANGUAGE: "zh-cn",
// 使用流模式
Expand All @@ -53,7 +53,9 @@ var ENV = {
// 开发模式
DEV_MODE: false,
TELEGRAM_API_DOMAIN: "https://api.telegram.org",
OPENAI_API_DOMAIN: "https://api.openai.com"
OPENAI_API_DOMAIN: "https://api.openai.com",
AZURE_API_KEY: null,
AZURE_COMPLETIONS_API: null
};
var CONST = {
PASSWORD_KEY: "chat_history_password",
Expand All @@ -63,7 +65,9 @@ var CONST = {
var DATABASE = null;
var API_GUARD = null;
var ENV_VALUE_TYPE = {
API_KEY: []
API_KEY: [],
AZURE_API_KEY: "string",
AZURE_COMPLETIONS_API: "string"
};
function initEnv(env, i18n2) {
DATABASE = env.DATABASE;
Expand Down Expand Up @@ -282,6 +286,9 @@ var Context = class {
* @return {string|null}
*/
openAIKeyFromContext() {
if (ENV.AZURE_COMPLETIONS_API) {
return ENV.AZURE_API_KEY;
}
if (this.USER_CONFIG.OPENAI_API_KEY) {
return this.USER_CONFIG.OPENAI_API_KEY;
}
Expand All @@ -298,15 +305,14 @@ var Context = class {

// src/telegram.js
async function sendMessage(message, token, context) {
let body = {
const body = {
text: message
};
for (const key of Object.keys(context)) {
if (context[key] !== void 0 && context[key] !== null) {
body[key] = context[key];
}
}
body = JSON.stringify(body);
let method = "sendMessage";
if (context?.message_id) {
method = "editMessageText";
Expand All @@ -318,7 +324,7 @@ async function sendMessage(message, token, context) {
headers: {
"Content-Type": "application/json"
},
body
body: JSON.stringify(body)
}
);
}
Expand Down Expand Up @@ -364,7 +370,7 @@ function deleteMessageFromTelegramWithContext(context) {
};
}
async function sendPhotoToTelegram(url, token, context) {
let body = {
const body = {
photo: url
};
for (const key of Object.keys(context)) {
Expand Down Expand Up @@ -821,12 +827,20 @@ async function requestCompletionsFromOpenAI(message, history, context, onStream)
const { signal } = controller;
const timeout = 1e3 * 60 * 5;
setTimeout(() => controller.abort(), timeout);
let resp = await fetch(`${ENV.OPENAI_API_DOMAIN}/v1/chat/completions`, {
let url = `${ENV.OPENAI_API_DOMAIN}/v1/chat/completions`;
let header = {
"Content-Type": "application/json",
"Authorization": `Bearer ${key}`
};
if (ENV.AZURE_COMPLETIONS_API) {
url = ENV.AZURE_COMPLETIONS_API;
header["api-key"] = key;
delete header["Authorization"];
delete body.model;
}
const resp = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${key}`
},
headers: header,
body: JSON.stringify(body),
signal
});
Expand Down Expand Up @@ -863,19 +877,19 @@ ${ENV.I18N.message.loading}...`);
}
return contentFull;
}
resp = await resp.json();
if (resp.error?.message) {
if (ENV.DEV_MODE || ENV.DEV_MODE) {
const result = await resp.json();
if (result.error?.message) {
if (ENV.DEBUG_MODE || ENV.DEV_MODE) {
throw new Error(`OpenAI API Error
> ${resp.error.message}
> ${result.error.message}
Body: ${JSON.stringify(body)}`);
} else {
throw new Error(`OpenAI API Error
> ${resp.error.message}`);
> ${result.error.message}`);
}
}
setTimeout(() => updateBotUsage(resp.usage, context).catch(console.error), 0);
return resp.choices[0].message.content;
setTimeout(() => updateBotUsage(result.usage, context).catch(console.error), 0);
return result.choices[0].message.content;
}
async function requestImageFromOpenAI(prompt, context) {
const key = context.openAIKeyFromContext();
Expand Down Expand Up @@ -1047,7 +1061,7 @@ async function loadHistory(key, context) {
async function chatWithOpenAI(text, context, modifier) {
try {
try {
const msg = await sendMessageToTelegramWithContext(context)(ENV.I18N.message.loading, false).then((r) => r.json());
const msg = await sendMessageToTelegramWithContext(context)(ENV.I18N.message.loading).then((r) => r.json());
context.CURRENT_CHAT_CONTEXT.message_id = msg.result.message_id;
context.CURRENT_CHAT_CONTEXT.reply_markup = null;
} catch (e) {
Expand Down Expand Up @@ -1837,11 +1851,14 @@ async function telegramWebhook(request) {
}
async function telegramSafeHook(request) {
try {
if (API_GUARD === void 0 || API_GUARD === null) {
return telegramWebhook(request);
}
console.log("API_GUARD is enabled");
const url = new URL(request.url);
url.pathname = url.pathname.replace("/safehook", "/webhook");
request = new Request(url, request);
return makeResponse200(API_GUARD.fetch(request));
return makeResponse200(await API_GUARD.fetch(request));
} catch (e) {
console.error(e);
return new Response(errorToString(e), { status: 200 });
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1684307553
1685351142
Loading

0 comments on commit 7419622

Please sign in to comment.