inspiration-agent 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +368 -0
- package/dist/.env.example +53 -0
- package/dist/README.md +368 -0
- package/dist/agent/ai.agent.js +712 -0
- package/dist/channel/feishu-connection.js +238 -0
- package/dist/channel/feishu.service.js +222 -0
- package/dist/cli.js +659 -0
- package/dist/config/system.prompt.txt +312 -0
- package/dist/index.js +176 -0
- package/dist/model/base-llm.service.js +320 -0
- package/dist/model/deepseek.service.js +18 -0
- package/dist/model/kimi.service.js +19 -0
- package/dist/model/minimax.service.js +35 -0
- package/dist/model/zhipu.service.js +26 -0
- package/dist/services/database.service.js +697 -0
- package/dist/services/memory.manager.js +486 -0
- package/dist/services/message.queue.js +157 -0
- package/dist/tools/browser.tools.js +814 -0
- package/dist/tools/command.tools.js +361 -0
- package/dist/tools/file.tools.js +222 -0
- package/dist/tools/memory.tools.js +393 -0
- package/dist/tools/scheduler.tools.js +559 -0
- package/dist/tools/search.tools.js +208 -0
- package/dist/utils/logger.js +52 -0
- package/dist/utils/markdown-renderer.js +207 -0
- package/package.json +51 -0
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
const logger = require("../utils/logger");
|
|
2
|
+
|
|
3
|
+
class MemoryTools {
|
|
4
|
+
constructor(memoryManager) {
|
|
5
|
+
this.memoryManager = memoryManager;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
getFunctionDefinitions() {
|
|
9
|
+
return [
|
|
10
|
+
{
|
|
11
|
+
type: "function",
|
|
12
|
+
function: {
|
|
13
|
+
name: "save_memory",
|
|
14
|
+
description:
|
|
15
|
+
"将重要信息保存到长期记忆中。当用户明确要求记住某些信息,或者信息对后续对话非常重要时使用。长期记忆在清空上下文后仍然保留。",
|
|
16
|
+
parameters: {
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
content: {
|
|
20
|
+
type: "string",
|
|
21
|
+
description: "要记住的内容",
|
|
22
|
+
},
|
|
23
|
+
type: {
|
|
24
|
+
type: "string",
|
|
25
|
+
enum: ["important", "identity", "preference", "task", "file_info", "general"],
|
|
26
|
+
description:
|
|
27
|
+
"记忆类型: important(重要信息), identity(身份信息), preference(偏好), task(任务), file_info(文件信息), general(一般信息)",
|
|
28
|
+
},
|
|
29
|
+
importance: {
|
|
30
|
+
type: "integer",
|
|
31
|
+
minimum: 1,
|
|
32
|
+
maximum: 10,
|
|
33
|
+
description: "重要性分数,1-10,数字越大越重要。默认为5。",
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
required: ["content"],
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
type: "function",
|
|
42
|
+
function: {
|
|
43
|
+
name: "save_short_term_memory",
|
|
44
|
+
description:
|
|
45
|
+
"将信息保存到短期记忆中。短期记忆仅在当前会话中有效,清空上下文后会丢失。用于临时存储不需要长期保存的信息。",
|
|
46
|
+
parameters: {
|
|
47
|
+
type: "object",
|
|
48
|
+
properties: {
|
|
49
|
+
role: {
|
|
50
|
+
type: "string",
|
|
51
|
+
enum: ["user", "assistant", "system"],
|
|
52
|
+
description: "消息角色,默认为 system",
|
|
53
|
+
},
|
|
54
|
+
content: {
|
|
55
|
+
type: "string",
|
|
56
|
+
description: "要保存的内容",
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
required: ["content"],
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
type: "function",
|
|
65
|
+
function: {
|
|
66
|
+
name: "search_memories",
|
|
67
|
+
description: "搜索长期记忆中与关键词相关的内容。用于查找之前记住的信息。",
|
|
68
|
+
parameters: {
|
|
69
|
+
type: "object",
|
|
70
|
+
properties: {
|
|
71
|
+
query: {
|
|
72
|
+
type: "string",
|
|
73
|
+
description: "搜索关键词",
|
|
74
|
+
},
|
|
75
|
+
limit: {
|
|
76
|
+
type: "integer",
|
|
77
|
+
minimum: 1,
|
|
78
|
+
maximum: 20,
|
|
79
|
+
description: "返回结果数量限制,默认为5",
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
required: ["query"],
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
type: "function",
|
|
88
|
+
function: {
|
|
89
|
+
name: "get_all_memories",
|
|
90
|
+
description: "获取所有长期记忆列表。用于查看已记住的所有信息。",
|
|
91
|
+
parameters: {
|
|
92
|
+
type: "object",
|
|
93
|
+
properties: {
|
|
94
|
+
type: {
|
|
95
|
+
type: "string",
|
|
96
|
+
enum: [
|
|
97
|
+
"important",
|
|
98
|
+
"identity",
|
|
99
|
+
"preference",
|
|
100
|
+
"task",
|
|
101
|
+
"file_info",
|
|
102
|
+
"general",
|
|
103
|
+
"all",
|
|
104
|
+
],
|
|
105
|
+
description: "记忆类型过滤,'all' 表示获取所有类型,默认为 'all'",
|
|
106
|
+
},
|
|
107
|
+
limit: {
|
|
108
|
+
type: "integer",
|
|
109
|
+
minimum: 1,
|
|
110
|
+
maximum: 50,
|
|
111
|
+
description: "返回结果数量限制,默认为20",
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
type: "function",
|
|
119
|
+
function: {
|
|
120
|
+
name: "delete_memory",
|
|
121
|
+
description: "删除指定的长期记忆。当信息不再需要或已过时时使用。",
|
|
122
|
+
parameters: {
|
|
123
|
+
type: "object",
|
|
124
|
+
properties: {
|
|
125
|
+
memory_id: {
|
|
126
|
+
type: "integer",
|
|
127
|
+
description: "要删除的记忆ID",
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
required: ["memory_id"],
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
type: "function",
|
|
136
|
+
function: {
|
|
137
|
+
name: "clear_short_term_memory",
|
|
138
|
+
description: "清空短期记忆。保留长期记忆,仅清除当前会话的临时记忆。",
|
|
139
|
+
parameters: {
|
|
140
|
+
type: "object",
|
|
141
|
+
properties: {},
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
type: "function",
|
|
147
|
+
function: {
|
|
148
|
+
name: "get_memory_stats",
|
|
149
|
+
description: "获取记忆统计信息,包括短期记忆和长期记忆的数量等。",
|
|
150
|
+
parameters: {
|
|
151
|
+
type: "object",
|
|
152
|
+
properties: {},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async saveMemory(args) {
|
|
160
|
+
try {
|
|
161
|
+
const { content, type = "general", importance = 5 } = args;
|
|
162
|
+
|
|
163
|
+
if (!content || content.trim().length === 0) {
|
|
164
|
+
return {
|
|
165
|
+
success: false,
|
|
166
|
+
message: "记忆内容不能为空",
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const memoryType = this._mapMemoryType(type);
|
|
171
|
+
|
|
172
|
+
await this.memoryManager.dbService.saveLongTermMemory({
|
|
173
|
+
type: memoryType,
|
|
174
|
+
content: content.trim(),
|
|
175
|
+
keywords: this._extractSimpleKeywords(content),
|
|
176
|
+
importance: Math.min(10, Math.max(1, importance)),
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
logger.info(`已保存长期记忆: [${memoryType}] ${content.substring(0, 50)}...`);
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
success: true,
|
|
183
|
+
message: `已成功保存到长期记忆`,
|
|
184
|
+
memory: {
|
|
185
|
+
type: memoryType,
|
|
186
|
+
content: content.substring(0, 100),
|
|
187
|
+
importance,
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
} catch (error) {
|
|
191
|
+
logger.error(`保存长期记忆失败: ${error.message}`);
|
|
192
|
+
return {
|
|
193
|
+
success: false,
|
|
194
|
+
message: `保存记忆失败: ${error.message}`,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async saveShortTermMemory(args) {
|
|
200
|
+
try {
|
|
201
|
+
const { content, role = "system" } = args;
|
|
202
|
+
|
|
203
|
+
if (!content || content.trim().length === 0) {
|
|
204
|
+
return {
|
|
205
|
+
success: false,
|
|
206
|
+
message: "记忆内容不能为空",
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
this.memoryManager.addMessage(role, content.trim());
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
success: true,
|
|
214
|
+
message: `已保存到短期记忆`,
|
|
215
|
+
memory: {
|
|
216
|
+
role,
|
|
217
|
+
content: content.substring(0, 100),
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
} catch (error) {
|
|
221
|
+
logger.error(`保存短期记忆失败: ${error.message}`);
|
|
222
|
+
return {
|
|
223
|
+
success: false,
|
|
224
|
+
message: `保存短期记忆失败: ${error.message}`,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
async searchMemories(args) {
|
|
230
|
+
try {
|
|
231
|
+
const { query, limit = 5 } = args;
|
|
232
|
+
|
|
233
|
+
if (!query || query.trim().length === 0) {
|
|
234
|
+
return {
|
|
235
|
+
success: false,
|
|
236
|
+
message: "搜索关键词不能为空",
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const memories = await this.memoryManager.searchRelevantMemories(query.trim(), limit);
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
success: true,
|
|
244
|
+
message: `找到 ${memories.length} 条相关记忆`,
|
|
245
|
+
memories: memories.map((m) => ({
|
|
246
|
+
id: m.id,
|
|
247
|
+
type: m.type,
|
|
248
|
+
content: m.content,
|
|
249
|
+
importance: m.importance,
|
|
250
|
+
created_at: m.created_at,
|
|
251
|
+
})),
|
|
252
|
+
};
|
|
253
|
+
} catch (error) {
|
|
254
|
+
logger.error(`搜索记忆失败: ${error.message}`);
|
|
255
|
+
return {
|
|
256
|
+
success: false,
|
|
257
|
+
message: `搜索记忆失败: ${error.message}`,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async getAllMemories(args) {
|
|
263
|
+
try {
|
|
264
|
+
const { type = "all", limit = 20 } = args;
|
|
265
|
+
|
|
266
|
+
const memoryType = type === "all" ? null : this._mapMemoryType(type);
|
|
267
|
+
const memories = await this.memoryManager.dbService.getLongTermMemories(memoryType, limit);
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
success: true,
|
|
271
|
+
message: `共有 ${memories.length} 条记忆`,
|
|
272
|
+
memories: memories.map((m) => ({
|
|
273
|
+
id: m.id,
|
|
274
|
+
type: m.type,
|
|
275
|
+
content: m.content,
|
|
276
|
+
importance: m.importance,
|
|
277
|
+
created_at: m.created_at,
|
|
278
|
+
})),
|
|
279
|
+
};
|
|
280
|
+
} catch (error) {
|
|
281
|
+
logger.error(`获取记忆列表失败: ${error.message}`);
|
|
282
|
+
return {
|
|
283
|
+
success: false,
|
|
284
|
+
message: `获取记忆列表失败: ${error.message}`,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async deleteMemory(args) {
|
|
290
|
+
try {
|
|
291
|
+
const { memory_id } = args;
|
|
292
|
+
|
|
293
|
+
if (!memory_id) {
|
|
294
|
+
return {
|
|
295
|
+
success: false,
|
|
296
|
+
message: "记忆ID不能为空",
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const result = await this.memoryManager.dbService.deleteLongTermMemory(memory_id);
|
|
301
|
+
|
|
302
|
+
if (result.deleted) {
|
|
303
|
+
return {
|
|
304
|
+
success: true,
|
|
305
|
+
message: `已删除记忆 ID: ${memory_id}`,
|
|
306
|
+
};
|
|
307
|
+
} else {
|
|
308
|
+
return {
|
|
309
|
+
success: false,
|
|
310
|
+
message: `未找到记忆 ID: ${memory_id}`,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
} catch (error) {
|
|
314
|
+
logger.error(`删除记忆失败: ${error.message}`);
|
|
315
|
+
return {
|
|
316
|
+
success: false,
|
|
317
|
+
message: `删除记忆失败: ${error.message}`,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async clearShortTermMemory() {
|
|
323
|
+
try {
|
|
324
|
+
this.memoryManager.clearShortTerm();
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
success: true,
|
|
328
|
+
message: "已清空短期记忆,长期记忆保持不变",
|
|
329
|
+
};
|
|
330
|
+
} catch (error) {
|
|
331
|
+
logger.error(`清空短期记忆失败: ${error.message}`);
|
|
332
|
+
return {
|
|
333
|
+
success: false,
|
|
334
|
+
message: `清空短期记忆失败: ${error.message}`,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
async getMemoryStats() {
|
|
340
|
+
try {
|
|
341
|
+
const stats = await this.memoryManager.getStats();
|
|
342
|
+
|
|
343
|
+
return {
|
|
344
|
+
success: true,
|
|
345
|
+
message: "记忆统计信息",
|
|
346
|
+
stats: {
|
|
347
|
+
shortTermCount: stats.shortTermCount,
|
|
348
|
+
shortTermLimit: stats.shortTermLimit,
|
|
349
|
+
longTermCount: stats.longTermCount,
|
|
350
|
+
maxLongTermMemories: stats.maxLongTermMemories,
|
|
351
|
+
memoryExpiryDays: stats.memoryExpiryDays,
|
|
352
|
+
},
|
|
353
|
+
};
|
|
354
|
+
} catch (error) {
|
|
355
|
+
logger.error(`获取记忆统计失败: ${error.message}`);
|
|
356
|
+
return {
|
|
357
|
+
success: false,
|
|
358
|
+
message: `获取记忆统计失败: ${error.message}`,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
_mapMemoryType(type) {
|
|
364
|
+
const typeMap = {
|
|
365
|
+
important: "important",
|
|
366
|
+
identity: "identity",
|
|
367
|
+
preference: "preference",
|
|
368
|
+
task: "task",
|
|
369
|
+
file_info: "file_info",
|
|
370
|
+
general: "conversation",
|
|
371
|
+
conversation: "conversation",
|
|
372
|
+
};
|
|
373
|
+
return typeMap[type] || "conversation";
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
_extractSimpleKeywords(content) {
|
|
377
|
+
const keywords = [];
|
|
378
|
+
const words = content.match(/[\u4e00-\u9fa5]{2,8}/g) || [];
|
|
379
|
+
words.slice(0, 5).forEach((word) => {
|
|
380
|
+
if (
|
|
381
|
+
!keywords.includes(word) &&
|
|
382
|
+
!["这是", "那个", "这个", "什么", "怎么", "如何", "因为", "所以", "但是", "而且"].includes(
|
|
383
|
+
word
|
|
384
|
+
)
|
|
385
|
+
) {
|
|
386
|
+
keywords.push(word);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
return keywords;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
module.exports = MemoryTools;
|