Skip to content

Commit 5155587

Browse files
author
lrhh123
committed
feat: 修复插件服务
1 parent 5eef514 commit 5155587

File tree

8 files changed

+229
-100
lines changed

8 files changed

+229
-100
lines changed

src/main/backend/backend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,13 @@ class BKServer {
395395
// 检查插件是否正常工作
396396
this.app.post('/api/v1/base/plugin/check', async (req, res) => {
397397
try {
398-
const { code, message, ctx } = req.body;
398+
const { code, messages, ctx } = req.body;
399399
const ctxMap = new Map(Object.entries(ctx));
400400
const resp = await this.pluginService.checkPlugin(
401401
code,
402402
// @ts-ignore
403403
ctxMap,
404-
message,
404+
messages,
405405
);
406406
res.json(resp);
407407
} catch (error) {

src/main/backend/constants/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,63 @@ export const CTX_CURRENT_GOODS_ID = 'CTX_CURRENT_GOODS_ID'; // 当前商品 ID
6565
export const CTX_MEMBER_TAG = 'CTX_MEMBER_TAG'; // 会员标签
6666
export const CTX_FAN_TAG = 'CTX_FAN_TAG'; // 粉丝标签
6767
export const CTX_NEW_CUSTOMER_TAG = 'CTX_NEW_CUSTOMER_TAG'; // 新客标签
68+
69+
export const PluginDefaultRunCode = `
70+
const cc = require('config_srv');
71+
const rp = require('reply_srv');
72+
73+
/**
74+
* 插件主函数
75+
* @param {AppContext} ctx - 上下文信息
76+
* @param {Message[]} messages - 消息数组
77+
* @returns {Reply} 插件执行结果
78+
*/
79+
async function main(ctx, messages) {
80+
const appId = ctx.get('app_id');
81+
const instanceId = ctx.get('instance_id');
82+
83+
const cfg = cc.get(ctx);
84+
85+
console.log('插件开始执行....');
86+
console.log('当前应用 ID:', appId);
87+
console.log('当前客服实例 ID:', instanceId);
88+
console.log('当前配置:', ctx, messages);
89+
90+
// 先检查是否存在用户的消息
91+
const lastUserMsg = messages
92+
.slice()
93+
.reverse()
94+
.find((msg) => msg.role === 'OTHER');
95+
96+
if (!lastUserMsg) {
97+
return {
98+
type: 'TEXT',
99+
content: cfg.default_reply, // 默认回复
100+
};
101+
}
102+
103+
// 等待随机时间
104+
await new Promise((resolve) => {
105+
const min = cfg.reply_speed;
106+
const max = cfg.reply_random_speed + cfg.reply_speed;
107+
const randomTime = min + Math.random() * (max - min);
108+
setTimeout(resolve, randomTime * 1000);
109+
});
110+
111+
// 再检查是否使用关键词匹配
112+
if (rp.isKeywordMatch) {
113+
const data = await rp.matchKeyword(ctx, lastUserMsg);
114+
if (data) return data;
115+
}
116+
117+
// 最后检查是否使用 GPT 生成回复
118+
if (rp.isUseGptReply) {
119+
const data = await rp.getLLMResponse(cfg, ctx, messages);
120+
if (data) return data;
121+
}
122+
123+
return {
124+
type: 'TEXT',
125+
content: cfg.default_reply,
126+
};
127+
}`;

src/main/backend/entities/config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export class Config extends Model {
4343
declare model: string;
4444

4545
declare activation_code: string;
46+
47+
declare version: string;
4648
}
4749

4850
export function initConfig(sequelize: Sequelize) {
@@ -140,6 +142,11 @@ export function initConfig(sequelize: Sequelize) {
140142
type: DataTypes.STRING,
141143
allowNull: true,
142144
},
145+
version: {
146+
type: DataTypes.STRING(255),
147+
defaultValue: '1.0.0',
148+
allowNull: true,
149+
},
143150
},
144151
{
145152
sequelize,

src/main/backend/entities/plugin.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export class Plugin extends Model {
1414
declare instance_id: string; // 可能是作用于单个实例的插件
1515

1616
declare created_at: Date;
17+
18+
declare version: string;
1719
}
1820

1921
export function initPlugin(sequelize: Sequelize) {
@@ -48,6 +50,11 @@ export function initPlugin(sequelize: Sequelize) {
4850
type: DataTypes.DATE,
4951
allowNull: true,
5052
},
53+
version: {
54+
type: DataTypes.STRING(255),
55+
defaultValue: '1.0.0',
56+
allowNull: true,
57+
},
5158
},
5259
{
5360
sequelize,

src/main/backend/services/dispatchService.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import PluginService from './pluginService';
77
import { ConfigController } from '../controllers/configController';
88
import { MessageController } from '../controllers/messageController';
99
import { Instance } from '../entities/instance';
10+
import { PluginDefaultRunCode } from '../constants';
1011

1112
export class DispatchService {
1213
private mainWindow: BrowserWindow;
@@ -58,10 +59,19 @@ export class DispatchService {
5859

5960
// 检查是否使用插件
6061
const cfg = await this.configController.get(ctxMap);
62+
await this.messageService.extractMsgInfo(cfg, ctxMap, messages);
6163
if (cfg.use_plugin && cfg.plugin_id) {
62-
reply = this.pluginService.executePlugin(cfg.plugin_id, ctx, messages);
64+
reply = await this.pluginService.executePlugin(
65+
cfg.plugin_id,
66+
ctx,
67+
messages,
68+
);
6369
} else {
64-
reply = await this.messageService.getReply(ctxMap, messages);
70+
reply = await this.pluginService.executePluginCode(
71+
PluginDefaultRunCode,
72+
ctxMap,
73+
messages,
74+
);
6575
}
6676

6777
callback(reply);

src/main/backend/services/messageService.ts

Lines changed: 7 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -58,66 +58,13 @@ export class MessageService {
5858
this.isUseGptReply = isUseGptReply;
5959
}
6060

61-
/**
62-
* 获取回复消息
63-
* @param ctx
64-
* @param messages
65-
* @returns
66-
*/
67-
public async getReply(
68-
ctx: Context,
69-
messages: MessageDTO[],
70-
): Promise<ReplyDTO> {
71-
const cfg = await this.configController.get(ctx);
72-
73-
// 提取消息中的信息
74-
await this.extractMsgInfo(cfg, ctx, messages);
75-
76-
// 先检查是否存在用户的消息
77-
const lastUserMsg = messages
78-
.slice()
79-
.reverse()
80-
.find((msg) => msg.role === 'OTHER');
81-
if (!lastUserMsg) {
82-
return {
83-
type: 'TEXT',
84-
content: cfg.default_reply,
85-
};
86-
}
87-
88-
// 先等待随机时间
89-
await new Promise((resolve) => {
90-
const min = cfg.reply_speed; // 5 seconds
91-
const max = cfg.reply_random_speed + cfg.reply_speed; // 10 seconds
92-
const randomTime = min + Math.random() * (max - min);
93-
setTimeout(resolve, randomTime * 1000);
94-
});
95-
96-
// 再检查是否使用关键词匹配
97-
if (this.isKeywordMatch) {
98-
const data = await this.matchKeyword(ctx, lastUserMsg);
99-
if (data) return data;
100-
}
101-
102-
// 最后检查是否使用 GPT 生成回复
103-
if (this.isUseGptReply) {
104-
const data = await this.getLLMResponse(cfg, ctx, messages);
105-
if (data) return data;
106-
}
107-
108-
return {
109-
type: 'TEXT',
110-
content: cfg.default_reply,
111-
};
112-
}
113-
11461
/**
11562
* 匹配关键词
11663
* @param ctx
11764
* @param message
11865
* @returns
11966
*/
120-
private async matchKeyword(
67+
public async matchKeyword(
12168
ctx: Context,
12269
message: MessageDTO,
12370
): Promise<ReplyDTO | null> {
@@ -167,7 +114,7 @@ export class MessageService {
167114
* @param messages
168115
* @returns
169116
*/
170-
private async getLLMResponse(
117+
public async getLLMResponse(
171118
cfg: Config,
172119
ctx: Context,
173120
messages: MessageDTO[],
@@ -262,7 +209,11 @@ export class MessageService {
262209
* @param messages
263210
* @returns
264211
*/
265-
async extractMsgInfo(cfg: Config, ctx: Context, messages: MessageDTO[]) {
212+
public async extractMsgInfo(
213+
cfg: Config,
214+
ctx: Context,
215+
messages: MessageDTO[],
216+
) {
266217
if (!cfg.extract_phone && !cfg.extract_product) return;
267218
if (cfg.save_path === '') return;
268219

0 commit comments

Comments
 (0)