Skip to content

Commit 0d3b8eb

Browse files
author
lrhh123
committed
fix: 修复编辑器保存问题
1 parent 5b6aa3b commit 0d3b8eb

File tree

12 files changed

+238
-102
lines changed

12 files changed

+238
-102
lines changed

src/main/backend/backend.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { MessageService } from './services/messageService';
1313
import { DispatchService } from './services/dispatchService';
1414
import { PluginService } from './services/pluginService';
1515
import { AppService } from './services/appService';
16+
import { LoggerService } from './services/loggerService';
1617

1718
class BKServer {
1819
private app: express.Application;
@@ -35,6 +36,8 @@ class BKServer {
3536

3637
private dispatchService: DispatchService;
3738

39+
private loggerService: LoggerService;
40+
3841
private appService: AppService;
3942

4043
constructor(port: number, mainWindow: BrowserWindow) {
@@ -60,16 +63,22 @@ class BKServer {
6063
this.configController = new ConfigController();
6164
this.messageController = new MessageController();
6265
this.keywordReplyController = new KeywordReplyController(port);
66+
this.loggerService = new LoggerService(mainWindow);
6367

64-
this.messageService = new MessageService(this.keywordReplyController);
68+
this.messageService = new MessageService(
69+
this.loggerService,
70+
this.keywordReplyController,
71+
);
6572

6673
this.pluginService = new PluginService(
74+
this.loggerService,
6775
this.configController,
6876
this.messageService,
6977
);
7078

7179
this.dispatchService = new DispatchService(
7280
mainWindow,
81+
this.loggerService,
7382
this.io,
7483
this.configController,
7584
this.messageService,

src/main/backend/controllers/configController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ export class ConfigController {
2323
let config;
2424

2525
config = await Config.findOne({
26-
where: { instance_id: instanceId },
26+
where: { instance_id: instanceId, active: true },
2727
});
2828

2929
if (!config && appId) {
3030
config = await Config.findOne({
31-
where: { platform_id: appId },
31+
where: { platform_id: appId, active: true },
3232
});
3333
}
3434

@@ -101,7 +101,7 @@ export class ConfigController {
101101
let config = null;
102102
if (instanceId) {
103103
config = await Config.findOne({
104-
where: { instance_id: instanceId },
104+
where: { instance_id: instanceId, active: true },
105105
});
106106
if (!config) {
107107
config = await Config.create({
@@ -113,7 +113,7 @@ export class ConfigController {
113113

114114
if (!config && appId) {
115115
config = await Config.findOne({
116-
where: { platform_id: appId },
116+
where: { platform_id: appId, active: true },
117117
});
118118
if (!config) {
119119
config = await Config.create({

src/main/backend/services/dispatchService.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,20 @@ import { ConfigController } from '../controllers/configController';
88
import { MessageController } from '../controllers/messageController';
99
import { Instance } from '../entities/instance';
1010
import { PluginDefaultRunCode } from '../constants';
11+
import { LoggerService } from './loggerService';
1112

1213
export class DispatchService {
13-
private mainWindow: BrowserWindow;
14-
15-
private messageService: MessageService;
16-
17-
private messageController: MessageController;
18-
19-
private io: socketIo.Server;
20-
21-
private configController: ConfigController;
22-
23-
private pluginService: PluginService;
24-
2514
constructor(
26-
mainWindow: BrowserWindow,
27-
io: socketIo.Server,
28-
configController: ConfigController,
29-
messageService: MessageService,
30-
messageController: MessageController,
31-
pluginService: PluginService,
15+
private mainWindow: BrowserWindow,
16+
private log: LoggerService,
17+
private io: socketIo.Server,
18+
private configController: ConfigController,
19+
private messageService: MessageService,
20+
private messageController: MessageController,
21+
private pluginService: PluginService,
3222
) {
3323
this.io = io;
24+
this.log = log;
3425
this.mainWindow = mainWindow;
3526
this.messageService = messageService;
3627
this.messageController = messageController;
@@ -87,6 +78,8 @@ export class DispatchService {
8778
ctxMap,
8879
msgs,
8980
);
81+
82+
this.log.info(`使用自定义插件回复: ${reply.content}`);
9083
} else {
9184
const reply_data = await this.pluginService.executePluginCode(
9285
PluginDefaultRunCode,
@@ -98,6 +91,12 @@ export class DispatchService {
9891
}
9992
} catch (error) {
10093
console.error('Failed to execute plugin', error);
94+
this.log.error(
95+
`回复失败: ${
96+
error instanceof Error ? error.message : String(error)
97+
},使用默认回复`,
98+
);
99+
101100
reply = {
102101
content: cfg.default_reply || 'Failed to execute plugin',
103102
type: 'TEXT',
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { BrowserWindow } from 'electron';
2+
3+
export class LoggerService {
4+
constructor(private mainWindow: BrowserWindow) {
5+
this.mainWindow = mainWindow;
6+
}
7+
8+
public log(msg: string) {
9+
console.log(msg);
10+
this.mainWindow.webContents.send('broadcast', {
11+
event: 'log_show',
12+
data: {
13+
time: new Date().toLocaleTimeString(),
14+
content: msg,
15+
},
16+
});
17+
}
18+
19+
public error(msg: string) {
20+
console.error('[ERROR]', msg);
21+
this.mainWindow.webContents.send('broadcast', {
22+
event: 'log_show',
23+
data: {
24+
time: new Date().toLocaleTimeString(),
25+
content: `[ERROR] ${msg}`,
26+
},
27+
});
28+
}
29+
30+
public info(msg: string) {
31+
console.info('[INFO]', msg);
32+
this.mainWindow.webContents.send('broadcast', {
33+
event: 'log_show',
34+
data: {
35+
time: new Date().toLocaleTimeString(),
36+
content: `[INFO] ${msg}`,
37+
},
38+
});
39+
}
40+
41+
public warn(msg: string) {
42+
console.warn('[WARN]', msg);
43+
this.mainWindow.webContents.send('broadcast', {
44+
event: 'log_show',
45+
data: {
46+
time: new Date().toLocaleTimeString(),
47+
content: `[WARN] ${msg}`,
48+
},
49+
});
50+
}
51+
52+
public success(msg: string) {
53+
console.log('[SUCCESS]', msg);
54+
this.mainWindow.webContents.send('broadcast', {
55+
event: 'log_show',
56+
data: {
57+
time: new Date().toLocaleTimeString(),
58+
content: `[SUCCESS] ${msg}`,
59+
},
60+
});
61+
}
62+
}

src/main/backend/services/messageService.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs/promises';
2+
import { BrowserWindow } from 'electron';
23
import { KeywordReplyController } from '../controllers/keywordReplyController';
34
import {
45
MessageDTO,
@@ -29,10 +30,9 @@ import {
2930
VYroAI,
3031
DifyAI,
3132
} from '../../gptproxy';
33+
import { LoggerService } from './loggerService';
3234

3335
export class MessageService {
34-
private autoReplyController: KeywordReplyController;
35-
3636
private llmClientMap: Map<
3737
string,
3838
| ErnieAI
@@ -46,8 +46,12 @@ export class MessageService {
4646
| DifyAI
4747
>;
4848

49-
constructor(keywordReplyController: KeywordReplyController) {
50-
this.autoReplyController = keywordReplyController;
49+
constructor(
50+
private log: LoggerService,
51+
private autoReplyController: KeywordReplyController,
52+
) {
53+
this.log = log;
54+
this.autoReplyController = autoReplyController;
5155

5256
this.llmClientMap = new Map();
5357
}
@@ -69,6 +73,7 @@ export class MessageService {
6973
};
7074

7175
if (!lastUserMsg) {
76+
this.log.warn(`未匹配到用户消息,所以使用默认回复: ${reply.content}`);
7277
return reply;
7378
}
7479

@@ -84,18 +89,27 @@ export class MessageService {
8489
if (cfg.has_keyword_match) {
8590
const data = await this.matchKeyword(ctx, lastUserMsg);
8691
if (data && data.content) {
92+
this.log.success(`匹配关键词: ${data.content}`);
8793
return data;
8894
}
95+
this.log.warn(`未匹配到关键词`);
8996
}
9097

9198
// 最后检查是否使用 GPT 生成回复
9299
if (cfg.has_use_gpt) {
100+
this.log.info(`开始使用 GPT 生成回复`);
101+
93102
const data = await this.getLLMResponse(cfg, ctx, messages);
103+
94104
if (data && data.content) {
105+
this.log.success(`GPT 生成回复: ${data.content}`);
95106
return data;
96107
}
108+
109+
this.log.warn(`AI 回复生成失败`);
97110
}
98111

112+
this.log.info('使用默认回复');
99113
return reply;
100114
}
101115

src/main/backend/services/pluginService.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import axios from 'axios';
88
import { ConfigController } from '../controllers/configController';
99
import { MessageDTO, ReplyDTO, Context } from '../types';
1010
import { MessageService } from './messageService';
11+
import { LoggerService } from './loggerService';
1112

1213
interface PreloadedModules {
1314
[key: string]: any;
@@ -38,15 +39,17 @@ const createSandbox = (contextOverrides: Record<string, any> = {}) => {
3839
};
3940

4041
export class PluginService {
41-
private configController: ConfigController;
42-
4342
constructor(
44-
configController: ConfigController,
45-
messageService: MessageService,
43+
private log: LoggerService,
44+
private configController: ConfigController,
45+
private messageService: MessageService,
4646
) {
47+
this.log = log;
4748
this.configController = configController;
48-
preloadedModules.config_srv = configController;
49-
preloadedModules.reply_srv = messageService;
49+
this.messageService = messageService;
50+
51+
preloadedModules.config_srv = this.configController;
52+
preloadedModules.reply_srv = this.messageService;
5053
}
5154

5255
async checkPlugin(
@@ -162,6 +165,7 @@ export class PluginService {
162165
console: customConsole,
163166
messages,
164167
require: (module: string) => {
168+
console.log('Require:', module);
165169
if (preloadedModules[module]) {
166170
return preloadedModules[module];
167171
}
@@ -178,10 +182,12 @@ export class PluginService {
178182
vm.runInContext(pluginCode, sandbox);
179183

180184
if (typeof sandbox.module.exports !== 'function') {
185+
this.log.error('插件格式错误,请检查是否导出函数');
181186
throw new Error('Plugin does not export a function');
182187
}
183188

184189
if (!messages || messages.length === 0) {
190+
this.log.error('未提供消息给插件');
185191
throw new Error('No messages provided to the plugin');
186192
}
187193

@@ -204,9 +210,15 @@ export class PluginService {
204210
return { data: data as ReplyDTO, consoleOutput };
205211
}
206212

213+
this.log.error('未返回有效响应');
214+
207215
throw new Error('Plugin function did not return a valid response');
208216
} catch (error: any) {
209217
console.error('Plugin execution error:', error);
218+
219+
this.log.error(
220+
`回复失败: ${error instanceof Error ? error.message : String(error)}`,
221+
);
210222
error.consoleOutput = consoleOutput;
211223
throw error;
212224
}

src/main/preload.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const electronHandler = {
5050
remove(channel: Channels) {
5151
ipcRenderer.removeAllListeners(channel);
5252
},
53+
removeListener(channel: Channels, func: (...args: unknown[]) => void) {
54+
ipcRenderer.removeListener(channel, func);
55+
},
5356
},
5457
store: {
5558
get(key: string) {

src/renderer/main-window/components/LogBox/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const LogBox = () => {
6464
</Button>
6565
</HStack>
6666

67-
<TableContainer overflowY="scroll" width="full">
67+
<TableContainer overflowY="scroll" width="full" maxH={'40vh'}>
6868
<Table size="sm">
6969
<Thead>
7070
<Tr>

0 commit comments

Comments
 (0)