From 7c212248509bc0f6983a60023eabd4c9661a399b Mon Sep 17 00:00:00 2001 From: TBXark Date: Fri, 20 Oct 2023 14:00:31 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E6=9C=AA=E8=AE=BE=E7=BD=AEbotname?= =?UTF-8?q?=E6=97=B6=E4=BB=8Eapi=E4=B8=AD=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/message.js | 10 +++++++--- src/telegram.js | 11 ++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/message.js b/src/message.js index 0a19afd2..f29aa170 100644 --- a/src/message.js +++ b/src/message.js @@ -1,6 +1,6 @@ import {CONST, DATABASE, ENV} from './env.js'; import {Context} from './context.js'; -import {sendMessageToTelegramWithContext} from './telegram.js'; +import {getBot, sendMessageToTelegramWithContext} from './telegram.js'; import {handleCommandMessage} from './command.js'; import {errorToString} from './utils.js'; import {chatWithLLM} from './chat.js'; @@ -140,7 +140,6 @@ async function msgFilterNonTextMessage(message, context) { return null; } - /** * 处理群消息 * @@ -154,7 +153,12 @@ async function msgHandleGroupMessage(message, context) { return new Response('Non text message', {status: 200}); } // 处理群组消息,过滤掉AT部分 - const botName = context.SHARE_CONTEXT.currentBotName; + let botName = context.SHARE_CONTEXT.currentBotName; + if (!botName) { + const res = await getBot(context.SHARE_CONTEXT.currentBotToken) + context.SHARE_CONTEXT.currentBotName = res.info.name; + botName = res.info.name; + } if (botName) { let mentioned = false; // Reply消息 diff --git a/src/telegram.js b/src/telegram.js index d95d5049..a67fe40c 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -275,10 +275,19 @@ export async function getChatAdminister(chatId, token) { } // 获取机器人信息 +/** + * @typedef {object} BotInfo + * @property {boolean} ok + * @property {object} info + * @property {string} info.name + * @property {string} info.bot_name + * @property {boolean} info.can_join_groups + * @property {boolean} info.can_read_all_group_messages + */ /** * * @param {string} token - * @return {Promise} + * @return {Promise} */ export async function getBot(token) { const resp = await fetch( From ce653cab38e5f5c8eb9d4321471d1fb0cf6005ff Mon Sep 17 00:00:00 2001 From: TBXark Date: Fri, 20 Oct 2023 14:17:01 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix:=20=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?= =?UTF-8?q?`SYSTEM=5FINIT=5FMESSAGE`=E8=A2=AB=E8=A6=86=E7=9B=96BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/buildinfo.json | 2 +- dist/index.js | 13 +++++++++---- dist/timestamp | 2 +- src/env.js | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/dist/buildinfo.json b/dist/buildinfo.json index ced02c03..2199f7c5 100644 --- a/dist/buildinfo.json +++ b/dist/buildinfo.json @@ -1 +1 @@ -{"sha": "3713d4d", "timestamp": 1697520630} +{"sha": "7c21224", "timestamp": 1697782599} diff --git a/dist/index.js b/dist/index.js index 67d7f611..59c4ee98 100644 --- a/dist/index.js +++ b/dist/index.js @@ -41,9 +41,9 @@ var ENV = { // 检查更新的分支 UPDATE_BRANCH: "master", // 当前版本 - BUILD_TIMESTAMP: 1697520630, + BUILD_TIMESTAMP: 1697782599, // 当前版本 commit id - BUILD_VERSION: "3713d4d", + BUILD_VERSION: "7c21224", I18N: null, LANGUAGE: "zh-cn", // 使用流模式 @@ -83,6 +83,7 @@ function initEnv(env, i18n2) { DATABASE = env.DATABASE; API_GUARD = env.API_GUARD; AI = env.AI; + ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message; for (const key of Object.keys(ENV)) { const t = ENV_VALUE_TYPE[key] ? ENV_VALUE_TYPE[key] : typeof ENV[key]; if (env[key]) { @@ -128,7 +129,6 @@ function initEnv(env, i18n2) { } } ENV.I18N = i18n2((ENV.LANGUAGE || "cn").toLowerCase()); - ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message; console.log(ENV); } @@ -2170,7 +2170,12 @@ async function msgHandleGroupMessage(message, context) { if (!message.text) { return new Response("Non text message", { status: 200 }); } - const botName = context.SHARE_CONTEXT.currentBotName; + let botName = context.SHARE_CONTEXT.currentBotName; + if (!botName) { + const res = await getBot(context.SHARE_CONTEXT.currentBotToken); + context.SHARE_CONTEXT.currentBotName = res.info.name; + botName = res.info.name; + } if (botName) { let mentioned = false; if (message.reply_to_message) { diff --git a/dist/timestamp b/dist/timestamp index a2e8d554..d5a5f6dd 100644 --- a/dist/timestamp +++ b/dist/timestamp @@ -1 +1 @@ -1697520630 +1697782599 diff --git a/src/env.js b/src/env.js index c29e8211..77bd72b8 100644 --- a/src/env.js +++ b/src/env.js @@ -157,6 +157,7 @@ export function initEnv(env, i18n) { API_GUARD = env.API_GUARD; AI = env.AI; + ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message; for (const key of Object.keys(ENV)) { const t = ENV_VALUE_TYPE[key]?ENV_VALUE_TYPE[key]:(typeof ENV[key]); if (env[key]) { @@ -205,6 +206,5 @@ export function initEnv(env, i18n) { } } ENV.I18N = i18n((ENV.LANGUAGE || 'cn').toLowerCase()); - ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message; console.log(ENV); } From d38184ffe855a332c54668a0d2ef099149ca5995 Mon Sep 17 00:00:00 2001 From: TBXark Date: Fri, 20 Oct 2023 14:18:40 +0800 Subject: [PATCH 3/5] =?UTF-8?q?style:=20=E8=B0=83=E6=95=B4ENV=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/env.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/env.js b/src/env.js index 77bd72b8..30c64d31 100644 --- a/src/env.js +++ b/src/env.js @@ -115,7 +115,7 @@ export const ENV = { // OpenAI API Domain 可替换兼容openai api的其他服务商 OPENAI_API_DOMAIN: 'https://api.openai.com', // OpenAI API BASE `https://api.openai.com/v1` - OPENAI_API_BASE: '', + OPENAI_API_BASE: null, // Azure API Key AZURE_API_KEY: null, @@ -138,6 +138,7 @@ export let API_GUARD = null; export let AI = null; const ENV_VALUE_TYPE = { + OPENAI_API_BASE: 'string', AZURE_API_KEY: 'string', AZURE_COMPLETIONS_API: 'string', }; From 50e577b1a6c1a2eef3bb932413d27a38abc8758f Mon Sep 17 00:00:00 2001 From: TBXark Date: Fri, 20 Oct 2023 14:50:04 +0800 Subject: [PATCH 4/5] =?UTF-8?q?perf:=20ENV=E6=94=B9=E6=88=90class=E5=B0=81?= =?UTF-8?q?=E8=A3=85,=E5=87=8F=E5=B0=91JSDoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix: SYSTEM_INIT_MESSAGE初始化错误BUG --- dist/buildinfo.json | 2 +- dist/index.js | 93 +++++++++++++------------ dist/timestamp | 2 +- src/env.js | 157 ++++++++++++++++-------------------------- src/message.js | 2 +- wrangler-example.toml | 4 +- 6 files changed, 112 insertions(+), 148 deletions(-) diff --git a/dist/buildinfo.json b/dist/buildinfo.json index 2199f7c5..40567e20 100644 --- a/dist/buildinfo.json +++ b/dist/buildinfo.json @@ -1 +1 @@ -{"sha": "7c21224", "timestamp": 1697782599} +{"sha": "d38184f", "timestamp": 1697784536} diff --git a/dist/index.js b/dist/index.js index 59c4ee98..f2c24e6f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,91 +1,93 @@ // src/env.js -var ENV = { +var Environment = class { // OpenAI API Key - API_KEY: [], + API_KEY = []; // 允许访问的Telegram Token, 设置时以逗号分隔 - TELEGRAM_AVAILABLE_TOKENS: [], + TELEGRAM_AVAILABLE_TOKENS = []; // 允许所有人使用 - I_AM_A_GENEROUS_PERSON: false, + I_AM_A_GENEROUS_PERSON = false; // 白名单 - CHAT_WHITE_LIST: [], + CHAT_WHITE_LIST = []; // 允许访问的Telegram Token 对应的Bot Name, 设置时以逗号分隔 - TELEGRAM_BOT_NAME: [], + TELEGRAM_BOT_NAME = []; // 群组白名单 - CHAT_GROUP_WHITE_LIST: [], + CHAT_GROUP_WHITE_LIST = []; // 群组机器人开关 - GROUP_CHAT_BOT_ENABLE: true, + GROUP_CHAT_BOT_ENABLE = true; // 群组机器人共享模式,关闭后,一个群组只有一个会话和配置。开启的话群组的每个人都有自己的会话上下文 - GROUP_CHAT_BOT_SHARE_MODE: false, + GROUP_CHAT_BOT_SHARE_MODE = false; // OpenAI的模型名称 - CHAT_MODEL: "gpt-3.5-turbo", + CHAT_MODEL = "gpt-3.5-turbo"; // 为了避免4096字符限制,将消息删减 - AUTO_TRIM_HISTORY: true, + AUTO_TRIM_HISTORY = true; // 最大历史记录长度 - MAX_HISTORY_LENGTH: 20, + MAX_HISTORY_LENGTH = 20; // 最大消息长度 - MAX_TOKEN_LENGTH: 2048, + MAX_TOKEN_LENGTH = 2048; // 使用GPT3的TOKEN计数 - GPT3_TOKENS_COUNT: false, + GPT3_TOKENS_COUNT = false; // GPT3计数器资源地址 - GPT3_TOKENS_COUNT_REPO: "https://raw.githubusercontent.com/tbxark-arc/GPT-3-Encoder/master", + GPT3_TOKENS_COUNT_REPO = "https://raw.githubusercontent.com/tbxark-arc/GPT-3-Encoder/master"; // 全局默认初始化消息 - SYSTEM_INIT_MESSAGE: "You are a helpful assistant", + SYSTEM_INIT_MESSAGE = null; // 全局默认初始化消息角色 - SYSTEM_INIT_MESSAGE_ROLE: "system", + SYSTEM_INIT_MESSAGE_ROLE = "system"; // 是否开启使用统计 - ENABLE_USAGE_STATISTICS: false, + ENABLE_USAGE_STATISTICS = false; // 隐藏部分命令按钮 - HIDE_COMMAND_BUTTONS: ["/role"], + HIDE_COMMAND_BUTTONS = ["/role"]; // 显示快捷回复按钮 - SHOW_REPLY_BUTTON: false, + SHOW_REPLY_BUTTON = false; // 检查更新的分支 - UPDATE_BRANCH: "master", + UPDATE_BRANCH = "master"; // 当前版本 - BUILD_TIMESTAMP: 1697782599, + BUILD_TIMESTAMP = 1697784536; // 当前版本 commit id - BUILD_VERSION: "7c21224", - I18N: null, - LANGUAGE: "zh-cn", + BUILD_VERSION = "d38184f"; + I18N = null; + LANGUAGE = "zh-cn"; // 使用流模式 - STREAM_MODE: true, + STREAM_MODE = true; // 安全模式 - SAFE_MODE: true, + SAFE_MODE = true; // 调试模式 - DEBUG_MODE: false, + DEBUG_MODE = false; // 开发模式 - DEV_MODE: false, + DEV_MODE = false; // Telegram API Domain - TELEGRAM_API_DOMAIN: "https://api.telegram.org", + TELEGRAM_API_DOMAIN = "https://api.telegram.org"; // OpenAI API Domain 可替换兼容openai api的其他服务商 - OPENAI_API_DOMAIN: "https://api.openai.com", + OPENAI_API_DOMAIN = "https://api.openai.com"; // OpenAI API BASE `https://api.openai.com/v1` - OPENAI_API_BASE: "", + OPENAI_API_BASE = null; // Azure API Key - AZURE_API_KEY: null, + AZURE_API_KEY = null; // Azure Completions API - AZURE_COMPLETIONS_API: null, + AZURE_COMPLETIONS_API = null; // workers ai模型 - WORKERS_AI_MODEL: "@cf/meta/llama-2-7b-chat-int8" + WORKERS_AI_MODEL = "@cf/meta/llama-2-7b-chat-int8"; }; +var ENV = new Environment(); +var DATABASE = null; +var API_GUARD = null; +var AI = null; var CONST = { PASSWORD_KEY: "chat_history_password", GROUP_TYPES: ["group", "supergroup"], USER_AGENT: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.2 Safari/605.1.15" }; -var DATABASE = null; -var API_GUARD = null; -var AI = null; -var ENV_VALUE_TYPE = { - AZURE_API_KEY: "string", - AZURE_COMPLETIONS_API: "string" -}; function initEnv(env, i18n2) { DATABASE = env.DATABASE; API_GUARD = env.API_GUARD; AI = env.AI; - ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message; + const envValueTypes = { + SYSTEM_INIT_MESSAGE: "string", + OPENAI_API_BASE: "string", + AZURE_API_KEY: "string", + AZURE_COMPLETIONS_API: "string" + }; for (const key of Object.keys(ENV)) { - const t = ENV_VALUE_TYPE[key] ? ENV_VALUE_TYPE[key] : typeof ENV[key]; + const t = envValueTypes[key] ? envValueTypes[key] : typeof ENV[key]; if (env[key]) { switch (t) { case "number": @@ -127,6 +129,9 @@ function initEnv(env, i18n2) { if (!ENV.OPENAI_API_BASE) { ENV.OPENAI_API_BASE = `${ENV.OPENAI_API_DOMAIN}/v1`; } + if (!ENV.SYSTEM_INIT_MESSAGE) { + ENV.SYSTEM_INIT_MESSAGE = ENV.I18N?.env?.system_init_message || "You are a helpful assistant"; + } } ENV.I18N = i18n2((ENV.LANGUAGE || "cn").toLowerCase()); console.log(ENV); diff --git a/dist/timestamp b/dist/timestamp index d5a5f6dd..509c1461 100644 --- a/dist/timestamp +++ b/dist/timestamp @@ -1 +1 @@ -1697782599 +1697784536 diff --git a/src/env.js b/src/env.js index 30c64d31..98585580 100644 --- a/src/env.js +++ b/src/env.js @@ -1,146 +1,97 @@ -/** - * @typedef {Object} Environment - * - * @property {string[]} API_KEY - * @property {string[]} TELEGRAM_AVAILABLE_TOKENS - * - * @property {boolean} I_AM_A_GENEROUS_PERSON - * @property {string[]} CHAT_WHITE_LIST - * - * @property {string[]} TELEGRAM_BOT_NAME - * @property {string[]} CHAT_GROUP_WHITE_LIST - * @property {boolean} GROUP_CHAT_BOT_ENABLE - * @property {boolean} GROUP_CHAT_BOT_SHARE_MODE - * - * @property {string} CHAT_MODEL - * @property {boolean} AUTO_TRIM_HISTORY - * @property {number} MAX_HISTORY_LENGTH - * @property {number} MAX_TOKEN_LENGTH - * @property {boolean} GPT3_TOKENS_COUNT - * @property {string} GPT3_TOKENS_COUNT_REPO - * @property {string} SYSTEM_INIT_MESSAGE - * @property {string} SYSTEM_INIT_MESSAGE_ROLE - * - * @property {boolean} ENABLE_USAGE_STATISTICS - * @property {string[]} HIDE_COMMAND_BUTTONS - * @property {boolean} SHOW_REPLY_BUTTON - * - * @property {string} UPDATE_BRANCH - * @property {number} BUILD_TIMESTAMP - * @property {string} BUILD_VERSION - * - * @property {null | I18n} I18N - * @property {string} LANGUAGE - * - * @property {boolean} STREAM_MODE - * @property {boolean} SAFE_MODE - * @property {boolean} DEBUG_MODE - * @property {boolean} DEV_MODE - * - * @property {string} TELEGRAM_API_DOMAIN - * @property {string} OPENAI_API_DOMAIN - * - * @property {null | string} AZURE_API_KEY - * @property {null | string} AZURE_COMPLETIONS_API - * - * @property {string} WORKERS_AI_MODEL - */ -export const ENV = { - +class Environment { // OpenAI API Key - API_KEY: [], + API_KEY = []; // 允许访问的Telegram Token, 设置时以逗号分隔 - TELEGRAM_AVAILABLE_TOKENS: [], + TELEGRAM_AVAILABLE_TOKENS = []; // 允许所有人使用 - I_AM_A_GENEROUS_PERSON: false, + I_AM_A_GENEROUS_PERSON = false; // 白名单 - CHAT_WHITE_LIST: [], + CHAT_WHITE_LIST = []; // 允许访问的Telegram Token 对应的Bot Name, 设置时以逗号分隔 - TELEGRAM_BOT_NAME: [], + TELEGRAM_BOT_NAME = []; // 群组白名单 - CHAT_GROUP_WHITE_LIST: [], + CHAT_GROUP_WHITE_LIST = []; // 群组机器人开关 - GROUP_CHAT_BOT_ENABLE: true, + GROUP_CHAT_BOT_ENABLE = true; // 群组机器人共享模式,关闭后,一个群组只有一个会话和配置。开启的话群组的每个人都有自己的会话上下文 - GROUP_CHAT_BOT_SHARE_MODE: false, + GROUP_CHAT_BOT_SHARE_MODE = false; // OpenAI的模型名称 - CHAT_MODEL: 'gpt-3.5-turbo', + CHAT_MODEL = 'gpt-3.5-turbo'; // 为了避免4096字符限制,将消息删减 - AUTO_TRIM_HISTORY: true, + AUTO_TRIM_HISTORY = true; // 最大历史记录长度 - MAX_HISTORY_LENGTH: 20, + MAX_HISTORY_LENGTH = 20; // 最大消息长度 - MAX_TOKEN_LENGTH: 2048, + MAX_TOKEN_LENGTH = 2048; // 使用GPT3的TOKEN计数 - GPT3_TOKENS_COUNT: false, + GPT3_TOKENS_COUNT = false; // GPT3计数器资源地址 - GPT3_TOKENS_COUNT_REPO: 'https://raw.githubusercontent.com/tbxark-arc/GPT-3-Encoder/master', + GPT3_TOKENS_COUNT_REPO = 'https://raw.githubusercontent.com/tbxark-arc/GPT-3-Encoder/master'; // 全局默认初始化消息 - SYSTEM_INIT_MESSAGE: 'You are a helpful assistant', + SYSTEM_INIT_MESSAGE = null; // 全局默认初始化消息角色 - SYSTEM_INIT_MESSAGE_ROLE: 'system', + SYSTEM_INIT_MESSAGE_ROLE = 'system'; // 是否开启使用统计 - ENABLE_USAGE_STATISTICS: false, + ENABLE_USAGE_STATISTICS = false; // 隐藏部分命令按钮 - HIDE_COMMAND_BUTTONS: ['/role'], + HIDE_COMMAND_BUTTONS = ['/role']; // 显示快捷回复按钮 - SHOW_REPLY_BUTTON: false, + SHOW_REPLY_BUTTON = false; // 检查更新的分支 - UPDATE_BRANCH: 'master', + UPDATE_BRANCH = 'master'; // 当前版本 - BUILD_TIMESTAMP: process.env.BUILD_TIMESTAMP || 0, + BUILD_TIMESTAMP = process?.env?.BUILD_TIMESTAMP || 0; // 当前版本 commit id - BUILD_VERSION: process.env.BUILD_VERSION || '', + BUILD_VERSION = process?.env?.BUILD_VERSION || ''; - I18N: null, - LANGUAGE: 'zh-cn', + I18N = null; + LANGUAGE = 'zh-cn'; // 使用流模式 - STREAM_MODE: true, + STREAM_MODE = true; // 安全模式 - SAFE_MODE: true, + SAFE_MODE = true; // 调试模式 - DEBUG_MODE: false, + DEBUG_MODE = false; // 开发模式 - DEV_MODE: false, + DEV_MODE = false; // Telegram API Domain - TELEGRAM_API_DOMAIN: 'https://api.telegram.org', + TELEGRAM_API_DOMAIN = 'https://api.telegram.org'; // OpenAI API Domain 可替换兼容openai api的其他服务商 - OPENAI_API_DOMAIN: 'https://api.openai.com', + OPENAI_API_DOMAIN = 'https://api.openai.com'; // OpenAI API BASE `https://api.openai.com/v1` - OPENAI_API_BASE: null, + OPENAI_API_BASE = null; // Azure API Key - AZURE_API_KEY: null, + AZURE_API_KEY = null; // Azure Completions API - AZURE_COMPLETIONS_API: null, + AZURE_COMPLETIONS_API = null; // workers ai模型 - WORKERS_AI_MODEL: '@cf/meta/llama-2-7b-chat-int8', -}; + WORKERS_AI_MODEL = '@cf/meta/llama-2-7b-chat-int8'; +} -export const CONST = { - PASSWORD_KEY: 'chat_history_password', - GROUP_TYPES: ['group', 'supergroup'], - USER_AGENT: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.2 Safari/605.1.15', -}; +// Environment Variables: Separate configuration values from a Worker script with Environment Variables. +export const ENV = new Environment(); +// KV Namespace Bindings: Bind an instance of a KV Namespace to access its data in a Worker export let DATABASE = null; +// Service Bindings: Bind to another Worker to invoke it directly from your code. export let API_GUARD = null; - +// AI Bindings: Bind the Workers AI catalogue of generative AI models to this Worker. export let AI = null; -const ENV_VALUE_TYPE = { - OPENAI_API_BASE: 'string', - AZURE_API_KEY: 'string', - AZURE_COMPLETIONS_API: 'string', +export const CONST = { + PASSWORD_KEY: 'chat_history_password', + GROUP_TYPES: ['group', 'supergroup'], + USER_AGENT: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.2 Safari/605.1.15', }; /** @@ -148,7 +99,6 @@ const ENV_VALUE_TYPE = { * @param {string} language * @return {I18n} */ - /** * @param {object} env * @param {I18nGenerator} i18n @@ -158,9 +108,15 @@ export function initEnv(env, i18n) { API_GUARD = env.API_GUARD; AI = env.AI; - ENV.SYSTEM_INIT_MESSAGE = ENV.I18N.env.system_init_message; + const envValueTypes = { + SYSTEM_INIT_MESSAGE: 'string', + OPENAI_API_BASE: 'string', + AZURE_API_KEY: 'string', + AZURE_COMPLETIONS_API: 'string', + }; + for (const key of Object.keys(ENV)) { - const t = ENV_VALUE_TYPE[key]?ENV_VALUE_TYPE[key]:(typeof ENV[key]); + const t = envValueTypes[key]?envValueTypes[key]:(typeof ENV[key]); if (env[key]) { switch (t) { case 'number': @@ -201,10 +157,15 @@ export function initEnv(env, i18n) { ENV.TELEGRAM_AVAILABLE_TOKENS.push(env.TELEGRAM_TOKEN); } - // AUTO SET VALUES + // OPENAI_API_BASE if (!ENV.OPENAI_API_BASE) { ENV.OPENAI_API_BASE=`${ENV.OPENAI_API_DOMAIN}/v1`; } + + // SYSTEM_INIT_MESSAGE + if (!ENV.SYSTEM_INIT_MESSAGE) { + ENV.SYSTEM_INIT_MESSAGE = ENV.I18N?.env?.system_init_message || 'You are a helpful assistant'; + } } ENV.I18N = i18n((ENV.LANGUAGE || 'cn').toLowerCase()); console.log(ENV); diff --git a/src/message.js b/src/message.js index f29aa170..29eedb0b 100644 --- a/src/message.js +++ b/src/message.js @@ -155,7 +155,7 @@ async function msgHandleGroupMessage(message, context) { // 处理群组消息,过滤掉AT部分 let botName = context.SHARE_CONTEXT.currentBotName; if (!botName) { - const res = await getBot(context.SHARE_CONTEXT.currentBotToken) + const res = await getBot(context.SHARE_CONTEXT.currentBotToken); context.SHARE_CONTEXT.currentBotName = res.info.name; botName = res.info.name; } diff --git a/wrangler-example.toml b/wrangler-example.toml index 68865ff6..f9493879 100644 --- a/wrangler-example.toml +++ b/wrangler-example.toml @@ -13,9 +13,7 @@ kv_namespaces = [ # 如果使用openai则注释这一段 #[ai] -#ai = [ -# { binding = "AI" } -#] +#binding = "AI" [vars] From cc79d2d928e3b7ed8342ab70e677a921ccc57a05 Mon Sep 17 00:00:00 2001 From: TBXark Date: Fri, 20 Oct 2023 14:51:48 +0800 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20i18n=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E9=94=99=E8=AF=AFBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dist/buildinfo.json | 2 +- dist/index.js | 6 +++--- dist/timestamp | 2 +- src/env.js | 5 +++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/dist/buildinfo.json b/dist/buildinfo.json index 40567e20..735a9a5e 100644 --- a/dist/buildinfo.json +++ b/dist/buildinfo.json @@ -1 +1 @@ -{"sha": "d38184f", "timestamp": 1697784536} +{"sha": "50e577b", "timestamp": 1697784701} diff --git a/dist/index.js b/dist/index.js index f2c24e6f..3593caa4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -41,9 +41,9 @@ var Environment = class { // 检查更新的分支 UPDATE_BRANCH = "master"; // 当前版本 - BUILD_TIMESTAMP = 1697784536; + BUILD_TIMESTAMP = 1697784701; // 当前版本 commit id - BUILD_VERSION = "d38184f"; + BUILD_VERSION = "50e577b"; I18N = null; LANGUAGE = "zh-cn"; // 使用流模式 @@ -120,6 +120,7 @@ function initEnv(env, i18n2) { } } { + ENV.I18N = i18n2((ENV.LANGUAGE || "cn").toLowerCase()); if (env.TELEGRAM_TOKEN && !ENV.TELEGRAM_AVAILABLE_TOKENS.includes(env.TELEGRAM_TOKEN)) { if (env.BOT_NAME && ENV.TELEGRAM_AVAILABLE_TOKENS.length === ENV.TELEGRAM_BOT_NAME.length) { ENV.TELEGRAM_BOT_NAME.push(env.BOT_NAME); @@ -133,7 +134,6 @@ function initEnv(env, i18n2) { ENV.SYSTEM_INIT_MESSAGE = ENV.I18N?.env?.system_init_message || "You are a helpful assistant"; } } - ENV.I18N = i18n2((ENV.LANGUAGE || "cn").toLowerCase()); console.log(ENV); } diff --git a/dist/timestamp b/dist/timestamp index 509c1461..b31acd4d 100644 --- a/dist/timestamp +++ b/dist/timestamp @@ -1 +1 @@ -1697784536 +1697784701 diff --git a/src/env.js b/src/env.js index 98585580..e5c7a026 100644 --- a/src/env.js +++ b/src/env.js @@ -149,7 +149,9 @@ export function initEnv(env, i18n) { } } { - // 兼容性代码 兼容旧版本 + ENV.I18N = i18n((ENV.LANGUAGE || 'cn').toLowerCase()); + + // TELEGRAM_TOKEN 兼容旧版 if (env.TELEGRAM_TOKEN && !ENV.TELEGRAM_AVAILABLE_TOKENS.includes(env.TELEGRAM_TOKEN)) { if (env.BOT_NAME && ENV.TELEGRAM_AVAILABLE_TOKENS.length === ENV.TELEGRAM_BOT_NAME.length) { ENV.TELEGRAM_BOT_NAME.push(env.BOT_NAME); @@ -167,6 +169,5 @@ export function initEnv(env, i18n) { ENV.SYSTEM_INIT_MESSAGE = ENV.I18N?.env?.system_init_message || 'You are a helpful assistant'; } } - ENV.I18N = i18n((ENV.LANGUAGE || 'cn').toLowerCase()); console.log(ENV); }