xiaozuoassistant 0.2.38 → 0.2.39
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.
|
@@ -16,6 +16,25 @@ export class MemoryManager {
|
|
|
16
16
|
return MemoryManager.instance;
|
|
17
17
|
}
|
|
18
18
|
// --- Layer 1: Short-term (Session) ---
|
|
19
|
+
getFeishuAppId(sessionId) {
|
|
20
|
+
if (sessionId.startsWith('feishu:')) {
|
|
21
|
+
const parts = sessionId.split(':');
|
|
22
|
+
if (parts.length >= 2) {
|
|
23
|
+
const botName = parts[1];
|
|
24
|
+
const feishuConfig = config.channels?.feishu;
|
|
25
|
+
if (Array.isArray(feishuConfig)) {
|
|
26
|
+
const bot = feishuConfig.find(b => b.name === botName);
|
|
27
|
+
if (bot)
|
|
28
|
+
return bot.appId;
|
|
29
|
+
}
|
|
30
|
+
else if (feishuConfig) {
|
|
31
|
+
if (botName === 'default')
|
|
32
|
+
return feishuConfig.appId;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
19
38
|
async createSession(input) {
|
|
20
39
|
return this.shortTerm.createSession(input);
|
|
21
40
|
}
|
|
@@ -42,7 +61,8 @@ export class MemoryManager {
|
|
|
42
61
|
// to avoid cluttering.
|
|
43
62
|
if (message.content.length > 10) { // Simple filter
|
|
44
63
|
const userId = config.userId || 'default';
|
|
45
|
-
this.
|
|
64
|
+
const appId = this.getFeishuAppId(sessionId);
|
|
65
|
+
this.vector.addMemory(message.content, 'recent', { sessionId, role: message.role, userId, appId })
|
|
46
66
|
.catch(err => console.error('Background vector add failed:', err));
|
|
47
67
|
}
|
|
48
68
|
// 3. Extract Facts (Structured) - This usually requires an LLM call to extract
|
|
@@ -74,9 +94,19 @@ export class MemoryManager {
|
|
|
74
94
|
const history = await this.getHistory(sessionId);
|
|
75
95
|
const recentMessages = history.slice(-10).map(m => `${m.role}: ${m.content}`).join('\n');
|
|
76
96
|
// 2. Search Vector Memory (Recent & Long-term)
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
const
|
|
97
|
+
// We search more broadly to ensure we find both User-specific and Bot-specific memories
|
|
98
|
+
const vectorResults = await this.vector.search(query, undefined, 20);
|
|
99
|
+
const botAppId = this.getFeishuAppId(sessionId);
|
|
100
|
+
// Filter results: Include memories belonging to the user OR the current bot
|
|
101
|
+
const vectorFiltered = vectorResults.filter(r => {
|
|
102
|
+
const isUserMemory = (r.metadata?.userId || 'default') === uid;
|
|
103
|
+
const isBotMemory = botAppId && r.metadata?.appId === botAppId;
|
|
104
|
+
return isUserMemory || isBotMemory;
|
|
105
|
+
}).slice(0, 5); // Take top 5 relevant chunks
|
|
106
|
+
const vectorContext = vectorFiltered.map(r => {
|
|
107
|
+
const source = (botAppId && r.metadata?.appId === botAppId) ? `[Bot Knowledge]` : `[User Memory]`;
|
|
108
|
+
return `${source}: ${r.text}`;
|
|
109
|
+
}).join('\n');
|
|
80
110
|
// 3. Get User Profile (Structured)
|
|
81
111
|
const profile = await this.structured.getUserProfile(uid);
|
|
82
112
|
const profileContext = Object.entries(profile).map(([k, v]) => `[User Info] ${k}: ${v}`).join('\n');
|
|
@@ -135,6 +165,7 @@ ${recentMessages}
|
|
|
135
165
|
const content = chunk.map(m => `${m.role}: ${m.content}`).join('\n');
|
|
136
166
|
// 1. Memory Archiving (Project or General)
|
|
137
167
|
const projectIds = session.projectIds || (session.projectId ? [session.projectId] : []);
|
|
168
|
+
const appId = this.getFeishuAppId(session.id);
|
|
138
169
|
if (projectIds.length > 0) {
|
|
139
170
|
// Project Context
|
|
140
171
|
const projectInfo = await brain.extractKeyInformation(content, 'project');
|
|
@@ -143,7 +174,8 @@ ${recentMessages}
|
|
|
143
174
|
await this.vector.addMemory(projectInfo, 'project_archive', {
|
|
144
175
|
sessionId: session.id,
|
|
145
176
|
projectId: pid,
|
|
146
|
-
archivedAt: now
|
|
177
|
+
archivedAt: now,
|
|
178
|
+
appId
|
|
147
179
|
});
|
|
148
180
|
console.log(`[MemoryManager] Archived project info for Project ${pid} (Chunk ${i / CHUNK_SIZE + 1})`);
|
|
149
181
|
}
|
|
@@ -156,7 +188,8 @@ ${recentMessages}
|
|
|
156
188
|
await this.vector.addMemory(generalInfo, 'long_term', {
|
|
157
189
|
sessionId: session.id,
|
|
158
190
|
archivedAt: now,
|
|
159
|
-
source: 'auto_archive'
|
|
191
|
+
source: 'auto_archive',
|
|
192
|
+
appId
|
|
160
193
|
});
|
|
161
194
|
console.log(`[MemoryManager] Archived general info for Session ${session.id} (Chunk ${i / CHUNK_SIZE + 1})`);
|
|
162
195
|
}
|