-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.js
More file actions
388 lines (329 loc) · 10.1 KB
/
Copy pathplugins.js
File metadata and controls
388 lines (329 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/**
* 轻量级插件系统
*
* 插件目录: ./plugins/
* 插件结构:
* plugins/
* ├── my-plugin/
* │ ├── index.js # 插件入口
* │ ├── manifest.json # 插件配置
* │ └── ...
*
* manifest.json 格式:
* {
* "name": "插件名称",
* "version": "1.0.0",
* "description": "插件描述",
* "author": "作者",
* "hooks": ["before_message", "after_message", "on_start", "on_stop"],
* "dependencies": {}
* }
*/
const fs = require('fs');
const path = require('path');
// 读取 .env
function loadEnv() {
const envPath = path.join(__dirname, '.env');
if (!fs.existsSync(envPath)) return {};
const env = {};
fs.readFileSync(envPath, 'utf8').split('\n').forEach(line => {
line = line.trim();
if (line && !line.startsWith('#')) {
const [key, ...valueParts] = line.split('=');
if (key && valueParts.length > 0) {
env[key.trim()] = valueParts.join('=').trim();
}
}
});
return env;
}
const env = loadEnv();
// 插件目录
const PLUGINS_DIR = path.join(__dirname, 'plugins');
// 插件系统类
class PluginSystem {
constructor() {
this.plugins = new Map();
this.hooks = {
before_message: [],
after_message: [],
before_llm: [],
after_llm: [],
on_start: [],
on_stop: [],
on_tool_call: [],
on_cron: []
};
this.api = null;
}
// 初始化 API
initApi(gatewayApi) {
this.api = {
// 注册钩子
registerHook: (event, handler) => {
if (this.hooks[event]) {
this.hooks[event].push(handler);
}
},
// 发送消息
sendMessage: (channel, to, content) => {
// 通过网关发送消息
console.log(`[Plugin] 发送消息到 ${channel}: ${content.slice(0, 50)}`);
return { success: true };
},
// 调用 LLM
callLLM: async (messages, options = {}) => {
const axios = require('axios');
const LLM_BASE_URL = env.LLM_BASE_URL || 'http://192.168.111.90:8016/v1';
const LLM_API_KEY = env.LLM_API_KEY || '';
const LLM_MODEL = env.LLM_MODEL || 'claude-opus-4-6';
const response = await axios.post(
`${LLM_BASE_URL}/chat/completions`,
{
model: options.model || LLM_MODEL,
messages,
temperature: options.temperature || 0.7,
max_tokens: options.max_tokens || 2000,
...options
},
{
headers: {
'Authorization': `Bearer ${LLM_API_KEY}`,
'Content-Type': 'application/json'
},
timeout: options.timeout || 120000
}
);
return response.data.choices[0].message.content;
},
// 获取配置
getConfig: (key, defaultValue) => {
return env[key] || defaultValue;
},
// 存储
storage: {
get: (key) => {
const storageFile = path.join(PLUGINS_DIR, '.storage.json');
if (fs.existsSync(storageFile)) {
const data = JSON.parse(fs.readFileSync(storageFile, 'utf8'));
return data[key];
}
return null;
},
set: (key, value) => {
const storageFile = path.join(PLUGINS_DIR, '.storage.json');
let data = {};
if (fs.existsSync(storageFile)) {
data = JSON.parse(fs.readFileSync(storageFile, 'utf8'));
}
data[key] = value;
fs.writeFileSync(storageFile, JSON.stringify(data, null, 2));
}
},
// 日志
log: {
info: (...args) => console.log('[Plugin]', ...args),
warn: (...args) => console.warn('[Plugin]', ...args),
error: (...args) => console.error('[Plugin]', ...args)
}
};
}
// 加载所有插件
loadAll() {
if (!fs.existsSync(PLUGINS_DIR)) {
fs.mkdirSync(PLUGINS_DIR, { recursive: true });
console.log('[Plugin] 插件目录已创建:', PLUGINS_DIR);
return;
}
const entries = fs.readdirSync(PLUGINS_DIR);
for (const entry of entries) {
const pluginPath = path.join(PLUGINS_DIR, entry);
const stat = fs.statSync(pluginPath);
if (stat.isDirectory()) {
this.loadPlugin(entry, pluginPath);
}
}
console.log(`[Plugin] 已加载 ${this.plugins.size} 个插件`);
}
// 加载单个插件
loadPlugin(name, pluginPath) {
const manifestPath = path.join(pluginPath, 'manifest.json');
const indexPath = path.join(pluginPath, 'index.js');
if (!fs.existsSync(manifestPath)) {
console.warn(`[Plugin] 插件 ${name} 缺少 manifest.json,跳过`);
return;
}
try {
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
// 检查依赖
if (manifest.dependencies) {
for (const [dep, version] of Object.entries(manifest.dependencies)) {
if (!this.plugins.has(dep)) {
console.warn(`[Plugin] 插件 ${name} 依赖 ${dep} 未安装,跳过`);
return;
}
}
}
// 加载插件代码
let plugin = null;
if (fs.existsSync(indexPath)) {
plugin = require(indexPath);
}
// 初始化插件
const pluginInstance = {
name: manifest.name,
version: manifest.version,
description: manifest.description,
author: manifest.author,
path: pluginPath,
manifest,
instance: plugin
};
// 注册钩子
if (manifest.hooks && Array.isArray(manifest.hooks)) {
for (const hook of manifest.hooks) {
if (this.hooks[hook]) {
// 插件可以提供一个函数,或者在钩子数组中指定
}
}
}
// 如果插件有 register 方法,调用它
if (plugin && typeof plugin.register === 'function') {
plugin.register(this.api, manifest);
}
this.plugins.set(name, pluginInstance);
console.log(`[Plugin] 加载插件: ${manifest.name} v${manifest.version}`);
} catch (error) {
console.error(`[Plugin] 加载插件 ${name} 失败:`, error.message);
}
}
// 触发钩子
async triggerHook(event, data) {
const handlers = this.hooks[event] || [];
let result = data;
for (const handler of handlers) {
try {
if (typeof handler === 'function') {
result = await handler(result) || result;
}
} catch (error) {
console.error(`[Plugin] 钩子 ${event} 执行失败:`, error.message);
}
}
return result;
}
// 卸载插件
unloadPlugin(name) {
const plugin = this.plugins.get(name);
if (plugin && typeof plugin.instance?.unload === 'function') {
plugin.instance.unload(this.api);
}
this.plugins.delete(name);
console.log(`[Plugin] 卸载插件: ${name}`);
}
// 列出插件
listPlugins() {
return Array.from(this.plugins.values()).map(p => ({
name: p.name,
version: p.version,
description: p.description,
author: p.author
}));
}
}
const pluginSystem = new PluginSystem();
module.exports = function(app) {
// 初始化插件 API
pluginSystem.initApi({});
// 加载所有插件
pluginSystem.loadAll();
// 触发钩子(供其他模块调用)
pluginSystem.triggerHook = pluginSystem.triggerHook.bind(pluginSystem);
// API 接口
// 列出插件
app.get('/api/plugins/list', (req, res) => {
res.json({ success: true, plugins: pluginSystem.listPlugins() });
});
// 加载插件
app.post('/api/plugins/load/:name', (req, res) => {
const { name } = req.params;
const pluginPath = path.join(PLUGINS_DIR, name);
if (!fs.existsSync(pluginPath)) {
return res.json({ success: false, error: '插件不存在' });
}
pluginSystem.loadPlugin(name, pluginPath);
res.json({ success: true });
});
// 卸载插件
app.post('/api/plugins/unload/:name', (req, res) => {
const { name } = req.params;
pluginSystem.unloadPlugin(name);
res.json({ success: true });
});
// 重新加载插件
app.post('/api/plugins/reload', (req, res) => {
pluginSystem.loadAll();
res.json({ success: true });
});
// 创建示例插件
app.post('/api/plugins/create', (req, res) => {
const { name, description } = req.body;
if (!name) {
return res.json({ success: false, error: '缺少插件名称' });
}
const pluginPath = path.join(PLUGINS_DIR, name);
if (fs.existsSync(pluginPath)) {
return res.json({ success: false, error: '插件已存在' });
}
// 创建插件目录
fs.mkdirSync(pluginPath, { recursive: true });
// 创建 manifest.json
const manifest = {
name,
version: '1.0.0',
description: description || '这是一个新插件',
author: 'mini-openclaw',
hooks: ['on_start', 'on_stop']
};
fs.writeFileSync(
path.join(pluginPath, 'manifest.json'),
JSON.stringify(manifest, null, 2)
);
// 创建示例代码
const exampleCode = `/**
* \${name} 插件
*/
module.exports = {
// 注册插件
register(api, manifest) {
console.log(\`[Plugin] \${manifest.name} 已加载\`);
// 注册钩子
api.registerHook('on_start', () => {
console.log('[Plugin] 插件启动');
});
api.registerHook('on_stop', () => {
console.log('[Plugin] 插件停止');
});
// 注册消息处理钩子
api.registerHook('before_message', async (message) => {
console.log('[Plugin] 收到消息:', message);
return message;
});
api.registerHook('after_message', async (reply) => {
console.log('[Plugin] 发送回复:', reply);
return reply;
});
},
// 卸载插件
unload(api) {
console.log('[Plugin] 插件已卸载');
}
};
`;
fs.writeFileSync(path.join(pluginPath, 'index.js'), exampleCode);
res.json({ success: true, path: pluginPath });
});
console.log('[Plugin] 插件系统已加载');
};
// 导出供其他模块使用
module.exports.pluginSystem = pluginSystem;