Skip to content

Commit 2a4927d

Browse files
author
lrhh123
committed
fix: 修复日志同步,add: 添加自动暂停功能
1 parent 1c5c955 commit 2a4927d

File tree

25 files changed

+402
-194
lines changed

25 files changed

+402
-194
lines changed

.env

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ PY_HOSTNAME=localhost
22
PY_PORT=9999
33
VAR=1234
44
BKEXE_PATH=./backend/__main__.exe
5-
PKG_VERSION=0.0.1
6-
DEBUG=true
5+
PKG_VERSION=0.0.1

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@
126126
"ms": "^2.1.3",
127127
"net": "^1.0.2",
128128
"node-cron": "^3.0.3",
129-
"node-fetch": "2.6.7",
130129
"openai": "^4.38.3",
131130
"pg-connection-string": "^2.6.4",
132131
"pg-hstore": "^2.3.4",

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/backend/backend.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import http from 'http';
66
import { Server } from 'socket.io';
77
import { BrowserWindow, shell } from 'electron';
88
import { sequelize } from './ormconfig';
9-
import { StrategyServiceStatusEnum } from './types';
109
import { ConfigController } from './controllers/configController';
1110
import { MessageController } from './controllers/messageController';
1211
import { KeywordReplyController } from './controllers/keywordReplyController';
@@ -196,7 +195,7 @@ class BKServer {
196195
const data = {
197196
appId: appId ? String(appId) : undefined,
198197
instanceId: instanceId ? String(instanceId) : undefined,
199-
type: type ? String(type) : 'generic',
198+
type: type ? String(type) : ('generic' as any),
200199
};
201200

202201
const obj = await configController.getConfigByType(data);
@@ -225,30 +224,15 @@ class BKServer {
225224
};
226225

227226
await configController.updateConfigByType(data);
227+
await this.dispatchService.syncConfig();
228228
res.json({ success: true });
229229
}),
230230
);
231231

232232
// Endpoint to update runner status based on incoming configuration
233-
this.app.post('/api/v1/base/runner', async (req, res) => {
234-
const {
235-
is_paused: isPaused,
236-
is_keyword_match: isKeywordMatch,
237-
is_use_gpt: isUseGptReply,
238-
} = req.body;
233+
this.app.post('/api/v1/base/sync', async (req, res) => {
239234
try {
240-
if (isPaused) {
241-
await this.dispatchService.updateStatus(
242-
StrategyServiceStatusEnum.STOPPED,
243-
);
244-
} else {
245-
await this.dispatchService.updateStatus(
246-
StrategyServiceStatusEnum.RUNNING,
247-
);
248-
}
249-
250-
this.messageService.updateKeywordMatch(isKeywordMatch, isUseGptReply);
251-
235+
await this.dispatchService.syncConfig();
252236
res.json({ success: true });
253237
} catch (error) {
254238
if (error instanceof Error) {
@@ -432,9 +416,9 @@ class BKServer {
432416
data: task,
433417
});
434418
} catch (error) {
435-
console.error(error);
436419
res.json({
437420
success: false,
421+
error: error instanceof Error ? error.message : String(error),
438422
data: null,
439423
});
440424
}

src/main/backend/constants/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ async function main(ctx, messages) {
109109
});
110110
111111
// 再检查是否使用关键词匹配
112-
if (rp.isKeywordMatch) {
112+
if (cfg.has_keyword_match) {
113113
const data = await rp.matchKeyword(ctx, lastUserMsg);
114114
if (data) return data;
115115
}
116116
117117
// 最后检查是否使用 GPT 生成回复
118-
if (rp.isUseGptReply) {
118+
if (cfg.has_use_gpt) {
119119
const data = await rp.getLLMResponse(cfg, ctx, messages);
120120
if (data) return data;
121121
}

src/main/backend/controllers/configController.ts

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
LLMConfig,
77
AccountConfig,
88
PluginConfig,
9+
DriverConfig,
910
} from '../types';
1011
import { CTX_APP_ID, CTX_INSTANCE_ID } from '../constants';
1112

@@ -41,6 +42,18 @@ export class ConfigController {
4142
config = await Config.create({
4243
global: true,
4344
});
45+
} else {
46+
const globalConfig = await Config.findOne({
47+
where: { global: true },
48+
});
49+
50+
// 这三个配置项是全局配置,需要合并到实例配置中
51+
if (globalConfig) {
52+
config.has_keyword_match = globalConfig.has_keyword_match;
53+
config.has_paused = globalConfig.has_paused;
54+
config.has_use_gpt = globalConfig.has_use_gpt;
55+
config.has_mouse_close = globalConfig.has_mouse_close;
56+
}
4457
}
4558

4659
return config;
@@ -156,9 +169,14 @@ export class ConfigController {
156169
}: {
157170
appId: string | undefined;
158171
instanceId: string | undefined;
159-
type: string;
172+
type: 'generic' | 'llm' | 'plugin' | 'driver' | 'account';
160173
}): Promise<
161-
GenericConfig | LLMConfig | AccountConfig | PluginConfig | undefined
174+
| GenericConfig
175+
| LLMConfig
176+
| AccountConfig
177+
| PluginConfig
178+
| DriverConfig
179+
| undefined
162180
> {
163181
let config = null;
164182
if (instanceId) {
@@ -206,6 +224,7 @@ export class ConfigController {
206224
defaultReply: config?.default_reply || '',
207225
};
208226
}
227+
209228
if (type === 'llm') {
210229
return {
211230
appId: config?.platform_id || '',
@@ -216,6 +235,7 @@ export class ConfigController {
216235
model: config?.model || 'gpt-3.5-turbo',
217236
};
218237
}
238+
219239
if (type === 'plugin') {
220240
let pluginCode = '';
221241

@@ -232,6 +252,15 @@ export class ConfigController {
232252
};
233253
}
234254

255+
if (type === 'driver') {
256+
return {
257+
hasPaused: config?.has_paused || false,
258+
hasKeywordMatch: config?.has_keyword_match || false,
259+
hasUseGpt: config?.has_use_gpt || false,
260+
hasMouseClose: config?.has_mouse_close || false,
261+
};
262+
}
263+
235264
return {
236265
activationCode: config?.activation_code || '',
237266
};
@@ -250,7 +279,12 @@ export class ConfigController {
250279
appId: string | undefined;
251280
instanceId: string | undefined;
252281
type: string;
253-
cfg: GenericConfig | LLMConfig | AccountConfig | PluginConfig;
282+
cfg:
283+
| GenericConfig
284+
| LLMConfig
285+
| AccountConfig
286+
| PluginConfig
287+
| DriverConfig;
254288
}) {
255289
let dbConfig = null;
256290
if (instanceId) {
@@ -328,11 +362,53 @@ export class ConfigController {
328362
use_plugin: config.usePlugin,
329363
plugin_id: pluginId,
330364
});
365+
} else if (type === 'driver') {
366+
// TODO: 目前只有全局配置,后续再实现实例配置
367+
const config = cfg as DriverConfig;
368+
dbConfig = await Config.findOne({
369+
where: { global: true },
370+
});
371+
if (!dbConfig) {
372+
throw new Error('Driver config not found');
373+
}
374+
await dbConfig.update({
375+
has_paused: config.hasPaused,
376+
has_keyword_match: config.hasKeywordMatch,
377+
has_use_gpt: config.hasUseGpt,
378+
has_mouse_close: config.hasMouseClose,
379+
});
331380
} else {
332381
const config = cfg as AccountConfig;
333382
await dbConfig.update({
334383
activation_code: config.activationCode,
335384
});
336385
}
337386
}
387+
388+
/**
389+
* 更新配置
390+
* @param
391+
*/
392+
public async moveMouseHandler(): Promise<boolean> {
393+
const dbConfig = await Config.findOne({
394+
where: { global: true },
395+
});
396+
397+
if (!dbConfig) {
398+
return false;
399+
}
400+
401+
// 检查是否开启了鼠标移动自动暂停功能
402+
if (dbConfig.has_mouse_close) {
403+
if (!dbConfig.has_paused) {
404+
await dbConfig.update({
405+
has_paused: true,
406+
});
407+
408+
return true;
409+
}
410+
}
411+
412+
return false;
413+
}
338414
}

src/main/backend/entities/config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ export class Config extends Model {
4545
declare activation_code: string;
4646

4747
declare version: string;
48+
49+
declare has_paused: boolean;
50+
51+
declare has_keyword_match: boolean;
52+
53+
declare has_use_gpt: boolean;
54+
55+
declare has_mouse_close: boolean; // 鼠标移动时是否自动关闭
4856
}
4957

5058
export function initConfig(sequelize: Sequelize) {
@@ -111,6 +119,7 @@ export function initConfig(sequelize: Sequelize) {
111119
default_reply: {
112120
type: DataTypes.STRING,
113121
allowNull: true,
122+
defaultValue: '当前消息有点多,我稍后再回复你',
114123
},
115124
context_count: {
116125
type: DataTypes.FLOAT,
@@ -147,6 +156,26 @@ export function initConfig(sequelize: Sequelize) {
147156
defaultValue: '1.0.0',
148157
allowNull: true,
149158
},
159+
has_paused: {
160+
type: DataTypes.BOOLEAN,
161+
defaultValue: true,
162+
allowNull: true,
163+
},
164+
has_keyword_match: {
165+
type: DataTypes.BOOLEAN,
166+
defaultValue: true,
167+
allowNull: true,
168+
},
169+
has_use_gpt: {
170+
type: DataTypes.BOOLEAN,
171+
defaultValue: true,
172+
allowNull: true,
173+
},
174+
has_mouse_close: {
175+
type: DataTypes.BOOLEAN,
176+
defaultValue: true,
177+
allowNull: true,
178+
},
150179
},
151180
{
152181
sequelize,

src/main/backend/services/appService.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ export class AppService {
6060
throw new Error('Failed to update tasks');
6161
}
6262

63+
// 遍历 result 检查,判断是否存在 error 属性
64+
const err_target = result.find((task) => task.error);
65+
if (err_target) {
66+
throw new Error(err_target.error);
67+
}
68+
6369
const target = result.find(
6470
(task) => task.task_id === String(instance.id),
6571
);

0 commit comments

Comments
 (0)