Skip to content

Commit

Permalink
perf: 限制USER_DEFINE keys
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Feb 27, 2024
1 parent 8a48d6a commit 98eb503
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha": "a24692d", "timestamp": 1709017600}
{"sha": "8a48d6a", "timestamp": 1709018751}
15 changes: 8 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var Environment = class {
// -- 版本数据 --
//
// 当前版本
BUILD_TIMESTAMP = 1709017600;
BUILD_TIMESTAMP = 1709018751;
// 当前版本 commit id
BUILD_VERSION = "a24692d";
BUILD_VERSION = "8a48d6a";
// -- 基础配置 --
/**
* @type {I18n | null}
Expand Down Expand Up @@ -271,6 +271,7 @@ var Context = class {
MISTRAL_CHAT_MODEL: ENV.MISTRAL_CHAT_MODEL
};
USER_DEFINE = {
VALID_KEYS: ["OPENAI_API_EXTRA_PARAMS", "SYSTEM_INIT_MESSAGE"],
// 自定义角色
ROLE: {}
};
Expand Down Expand Up @@ -340,7 +341,7 @@ var Context = class {
this.USER_CONFIG.DEFINE_KEYS = keys;
const userDefine = "USER_DEFINE";
if (userConfig[userDefine]) {
mergeObject(this.USER_DEFINE, userConfig[userDefine], null);
mergeObject(this.USER_DEFINE, userConfig[userDefine], this.USER_DEFINE.VALID_KEYS);
delete userConfig[userDefine];
}
mergeObject(this.USER_CONFIG, userConfig, keys);
Expand Down Expand Up @@ -1136,7 +1137,7 @@ async function requestCompletionsFromOpenAI(message, history, context, onStream)
"Content-Type": "application/json",
"Authorization": `Bearer ${openAIKeyFromContext(context)}`
};
return requestCompletionsLikeFromOpenAI(url, header, body, context, onStream, (result) => {
return requestCompletionsFromOpenAILikes(url, header, body, context, onStream, (result) => {
setTimeout(() => updateBotUsage(result?.usage, context).catch(console.error), 0);
});
}
Expand All @@ -1151,9 +1152,9 @@ async function requestCompletionsFromAzureOpenAI(message, history, context, onSt
"Content-Type": "application/json",
"api-key": azureKeyFromContext(context)
};
return requestCompletionsLikeFromOpenAI(url, header, body, context, onStream);
return requestCompletionsFromOpenAILikes(url, header, body, context, onStream);
}
async function requestCompletionsLikeFromOpenAI(url, header, body, context, onStream, onResult = null) {
async function requestCompletionsFromOpenAILikes(url, header, body, context, onStream, onResult = null) {
const controller = new AbortController();
const { signal } = controller;
const timeout = 1e3 * 60 * 5;
Expand Down Expand Up @@ -1413,7 +1414,7 @@ async function requestCompletionsFromMistralAI(message, history, context, onStre
"Content-Type": "application/json",
"Authorization": `Bearer ${context.USER_CONFIG.MISTRAL_API_KEY}`
};
return requestCompletionsLikeFromOpenAI(url, header, body, context, onStream);
return requestCompletionsFromOpenAILikes(url, header, body, context, onStream);
}

// src/llm.js
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1709017600
1709018751
9 changes: 4 additions & 5 deletions src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ async function commandUpdateUserConfig(message, command, subcommand, context) {
const key = subcommand.slice(0, kv);
const value = subcommand.slice(kv + 1);
if (ENV.LOCK_USER_CONFIG_KEYS.includes(key)) {
const msg = ENV.I18N.command.setenv.update_config_error(new Error(`Key ${key} is locked`))
const msg = ENV.I18N.command.setenv.update_config_error(new Error(`Key ${key} is locked`));
return sendMessageToTelegramWithContext(context)(msg);
}
try {
Expand Down Expand Up @@ -311,7 +311,7 @@ async function commandUpdateUserConfigs(message, command, subcommand, context) {
for (const ent of Object.entries(values)) {
const [key, value] = ent;
if (ENV.LOCK_USER_CONFIG_KEYS.includes(key)) {
const msg = ENV.I18N.command.setenv.update_config_error(new Error(`Key ${key} is locked`))
const msg = ENV.I18N.command.setenv.update_config_error(new Error(`Key ${key} is locked`));
return sendMessageToTelegramWithContext(context)(msg);
}
context.USER_CONFIG.DEFINE_KEYS.push(key);
Expand Down Expand Up @@ -340,7 +340,7 @@ async function commandUpdateUserConfigs(message, command, subcommand, context) {
*/
async function commandDeleteUserConfig(message, command, subcommand, context) {
if (ENV.LOCK_USER_CONFIG_KEYS.includes(subcommand)) {
const msg = ENV.I18N.command.setenv.update_config_error(new Error(`Key ${subcommand} is locked`))
const msg = ENV.I18N.command.setenv.update_config_error(new Error(`Key ${subcommand} is locked`));
return sendMessageToTelegramWithContext(context)(msg);
}
try {
Expand Down Expand Up @@ -368,7 +368,7 @@ async function commandDeleteUserConfig(message, command, subcommand, context) {
*/
async function commandClearUserConfig(message, command, subcommand, context) {
if (ENV.LOCK_USER_CONFIG_KEYS.includes(subcommand)) {
const msg = ENV.I18N.command.setenv.update_config_error(new Error(`Key ${subcommand} is locked`))
const msg = ENV.I18N.command.setenv.update_config_error(new Error(`Key ${subcommand} is locked`));
return sendMessageToTelegramWithContext(context)(msg);
}
try {
Expand All @@ -385,7 +385,6 @@ async function commandClearUserConfig(message, command, subcommand, context) {
}



/**
* /version 获得更新信息
*
Expand Down
5 changes: 3 additions & 2 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function mergeObject(target, source, keys) {
for (const key of Object.keys(target)) {
if (source[key]) {
if (keys !== null && !keys.includes(key)) {
continue
continue;
}
if (typeof source[key] === typeof target[key]) {
target[key] = source[key];
Expand Down Expand Up @@ -81,6 +81,7 @@ export class Context {
};

USER_DEFINE = {
VALID_KEYS: ['OPENAI_API_EXTRA_PARAMS', 'SYSTEM_INIT_MESSAGE'],
// 自定义角色
ROLE: {},
};
Expand Down Expand Up @@ -138,7 +139,7 @@ export class Context {
this.USER_CONFIG.DEFINE_KEYS = keys;
const userDefine = 'USER_DEFINE';
if (userConfig[userDefine]) {
mergeObject(this.USER_DEFINE, userConfig[userDefine], null);
mergeObject(this.USER_DEFINE, userConfig[userDefine], this.USER_DEFINE.VALID_KEYS);
delete userConfig[userDefine];
}
mergeObject(this.USER_CONFIG, userConfig, keys);
Expand Down
8 changes: 4 additions & 4 deletions src/llm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import {
isAzureEnable,
isOpenAIEnable,
requestCompletionsFromAzureOpenAI,
requestCompletionsFromOpenAI, requestCompletionsLikeFromOpenAI,
requestCompletionsFromOpenAI,
requestImageFromOpenAI,
} from './openai.js';
import {tokensCounter} from './utils.js';
import {isWorkersAIEnable, requestCompletionsFromWorkersAI, requestImageFromWorkersAI} from './workersai.js';
import {isGeminiAIEnable, requestCompletionsFromGeminiAI} from './gemini.js';
import {isMistralAIEnable, requestCompletionsFromMistralAI} from "./mistralai.js";
import {isMistralAIEnable, requestCompletionsFromMistralAI} from './mistralai.js';


/**
Expand Down Expand Up @@ -129,7 +129,7 @@ export function loadChatLLM(context) {
case 'gemini':
return requestCompletionsFromGeminiAI;
case 'mistral':
return requestCompletionsFromMistralAI
return requestCompletionsFromMistralAI;
default:
if (isAzureEnable(context)) {
return requestCompletionsFromAzureOpenAI;
Expand All @@ -144,7 +144,7 @@ export function loadChatLLM(context) {
return requestCompletionsFromGeminiAI;
}
if (isMistralAIEnable(context)) {
return requestCompletionsFromMistralAI
return requestCompletionsFromMistralAI;
}
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mistralai.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-vars */
import {Context} from './context.js';
import {requestCompletionsLikeFromOpenAI} from './openai.js';
import {requestCompletionsFromOpenAILikes} from './openai.js';

/**
* @param {Context} context
Expand Down Expand Up @@ -31,5 +31,5 @@ export async function requestCompletionsFromMistralAI(message, history, context,
'Content-Type': 'application/json',
'Authorization': `Bearer ${context.USER_CONFIG.MISTRAL_API_KEY}`,
};
return requestCompletionsLikeFromOpenAI(url, header, body, context, onStream);
return requestCompletionsFromOpenAILikes(url, header, body, context, onStream);
}
8 changes: 4 additions & 4 deletions src/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export async function requestCompletionsFromOpenAI(message, history, context, on
'Authorization': `Bearer ${openAIKeyFromContext(context)}`,
};

return requestCompletionsLikeFromOpenAI(url, header, body, context, onStream, (result) => {
return requestCompletionsFromOpenAILikes(url, header, body, context, onStream, (result) => {
setTimeout(() => updateBotUsage(result?.usage, context).catch(console.error), 0);
});
}
Expand Down Expand Up @@ -100,7 +100,7 @@ export async function requestCompletionsFromAzureOpenAI(message, history, contex
'api-key': azureKeyFromContext(context),
};

return requestCompletionsLikeFromOpenAI(url, header, body, context, onStream);
return requestCompletionsFromOpenAILikes(url, header, body, context, onStream);
}


Expand All @@ -115,7 +115,7 @@ export async function requestCompletionsFromAzureOpenAI(message, history, contex
* @param {function} onResult
* @return {Promise<string>}
*/
export async function requestCompletionsLikeFromOpenAI(url, header, body, context, onStream, onResult = null) {
export async function requestCompletionsFromOpenAILikes(url, header, body, context, onStream, onResult = null) {
const controller = new AbortController();
const {signal} = controller;
const timeout = 1000 * 60 * 5;
Expand Down Expand Up @@ -165,7 +165,7 @@ export async function requestCompletionsLikeFromOpenAI(url, header, body, contex
}

try {
onResult?.(result)
onResult?.(result);
return result.choices[0].message.content;
} catch (e) {
throw Error(result?.error?.message || JSON.stringify(result));
Expand Down

0 comments on commit 98eb503

Please sign in to comment.