Skip to content

Commit

Permalink
fix: USER_CONFIG相关BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 28, 2024
1 parent 839f57f commit ef4e54b
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion dist/buildinfo.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 23 additions & 23 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/timestamp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "chatgpt-telegram-workers",
"type": "module",
"version": "1.9.0",
"version": "1.9.1",
"description": "The easiest and quickest way to deploy your own ChatGPT Telegram bot is to use a single file and simply copy and paste it. There is no need for any dependencies, local development environment configuration, domain names, or servers.",
"author": "TBXark",
"license": "MIT",
Expand All @@ -19,7 +19,7 @@
"dist"
],
"scripts": {
"lint": "eslint --fix *.js *.ts src adapter plugins scripts",
"lint": "eslint --fix *.js *.ts src plugins scripts",
"build": "vite build",
"build:local": "BUILD_MODE=local vite build",
"build:docker": "npm run build:local && cd dist && docker build -t chatgpt-telegram-workers:latest .",
Expand Down
3 changes: 1 addition & 2 deletions src/adapter/vercel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ENV } from '../../config/env';
import { createRouter } from '../../route';

export default async function (request: VercelRequest, response: VercelResponse) {
let redis: UpStashRedis | null = null;
const redis: UpStashRedis | null = null;
try {
const {
UPSTASH_REDIS_REST_URL,
Expand All @@ -22,7 +22,6 @@ export default async function (request: VercelRequest, response: VercelResponse)
}
const cache = UpStashRedis.create(UPSTASH_REDIS_REST_URL, UPSTASH_REDIS_REST_TOKEN);
// edge function 使用 redis 作为数据库容易出现连接数过多的问题,此处仅作为演示,请自行实现 `Cache` 接口
redis = cache;
ENV.merge({
...process.env,
DATABASE: cache,
Expand Down
15 changes: 0 additions & 15 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,4 @@ export class AnthropicConfig {

export class DefineKeys {
DEFINE_KEYS: string[] = [];

trim = (lock: string[]): Record<string, any> => {
const config: Record<string, any> = { ...this };
const keysSet = new Set<string>(this.DEFINE_KEYS || []);
for (const key of lock) {
keysSet.delete(key);
}
keysSet.add('DEFINE_KEYS');
for (const key of Object.keys(config)) {
if (!keysSet.has(key)) {
delete config[key];
}
}
return config;
};
}
2 changes: 1 addition & 1 deletion src/config/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class WorkerContext {
const USER_CONFIG = Object.assign({}, ENV.USER_CONFIG);
try {
const userConfig: AgentUserConfig = JSON.parse(await ENV.DATABASE.get(SHARE_CONTEXT.configStoreKey));
ConfigMerger.merge(USER_CONFIG, userConfig?.trim(ENV.LOCK_USER_CONFIG_KEYS) || {});
ConfigMerger.merge(USER_CONFIG, ConfigMerger.trim(userConfig, ENV.LOCK_USER_CONFIG_KEYS) || {});
} catch (e) {
console.warn(e);
}
Expand Down
17 changes: 17 additions & 0 deletions src/config/merger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { AgentUserConfig } from './env';

export class ConfigMerger {
private static parseArray(raw: string): string[] {
raw = raw.trim();
Expand All @@ -14,6 +16,21 @@ export class ConfigMerger {
return raw.split(',');
}

static trim(source: AgentUserConfig, lock: string[]): Record<string, any> {
const config: Record<string, any> = { ...source };
const keysSet = new Set<string>(source.DEFINE_KEYS || []);
for (const key of lock) {
keysSet.delete(key);
}
keysSet.add('DEFINE_KEYS');
for (const key of Object.keys(config)) {
if (!keysSet.has(key)) {
delete config[key];
}
}
return config;
};

static merge(target: Record<string, any>, source: Record<string, any>, exclude?: string[]) {
const sourceKeys = new Set(Object.keys(source));
for (const key of Object.keys(target)) {
Expand Down
8 changes: 4 additions & 4 deletions src/telegram/command/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class SetEnvCommandHandler implements CommandHandler {
console.log('Update user config: ', key, context.USER_CONFIG[key]);
await ENV.DATABASE.put(
context.SHARE_CONTEXT.configStoreKey,
JSON.stringify(context.USER_CONFIG.trim(ENV.LOCK_USER_CONFIG_KEYS)),
JSON.stringify(context.USER_CONFIG),
);
return sender.sendPlainText('Update user config success');
} catch (e) {
Expand Down Expand Up @@ -187,7 +187,7 @@ export class SetEnvsCommandHandler implements CommandHandler {
context.USER_CONFIG.DEFINE_KEYS = Array.from(new Set(context.USER_CONFIG.DEFINE_KEYS));
await ENV.DATABASE.put(
context.SHARE_CONTEXT.configStoreKey,
JSON.stringify(context.USER_CONFIG.trim(ENV.LOCK_USER_CONFIG_KEYS)),
JSON.stringify(context.USER_CONFIG),
);
return sender.sendPlainText('Update user config success');
} catch (e) {
Expand All @@ -210,7 +210,7 @@ export class DelEnvCommandHandler implements CommandHandler {
context.USER_CONFIG.DEFINE_KEYS = context.USER_CONFIG.DEFINE_KEYS.filter(key => key !== subcommand);
await ENV.DATABASE.put(
context.SHARE_CONTEXT.configStoreKey,
JSON.stringify(context.USER_CONFIG.trim(ENV.LOCK_USER_CONFIG_KEYS)),
JSON.stringify(context.USER_CONFIG),
);
return sender.sendPlainText('Delete user config success');
} catch (e) {
Expand Down Expand Up @@ -292,7 +292,7 @@ export class SystemCommandHandler implements CommandHandler {
context.USER_CONFIG.MISTRAL_API_KEY = '******';
context.USER_CONFIG.COHERE_API_KEY = '******';
context.USER_CONFIG.ANTHROPIC_API_KEY = '******';
const config = context.USER_CONFIG.trim(ENV.LOCK_USER_CONFIG_KEYS);
const config = context.USER_CONFIG;
msg = `<pre>\n${msg}`;
msg += `USER_CONFIG: ${JSON.stringify(config, null, 2)}\n`;
msg += `CHAT_CONTEXT: ${JSON.stringify(sender.context || {}, null, 2)}\n`;
Expand Down
1 change: 0 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import nodeExternals from 'rollup-plugin-node-externals';
import dts from 'vite-plugin-dts';
import { createVersionPlugin, versionDefine } from './scripts/plugins/version';
import { createDockerPlugin } from './scripts/plugins/docker';
import { createVercelPlugin } from './scripts/plugins/vercel';

const { BUILD_MODE } = process.env;
const plugins: Plugin[] = [
Expand Down

0 comments on commit ef4e54b

Please sign in to comment.