@elizaos/plugin-memory 1.1.0 → 1.1.2
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 +230 -330
- package/dist/actions/remember.d.ts +11 -0
- package/dist/browser/index.browser.js +205 -348
- package/dist/browser/index.browser.js.map +13 -23
- package/dist/cjs/index.node.cjs +936 -2193
- package/dist/cjs/index.node.js.map +13 -23
- package/dist/evaluators/long-term-extraction.d.ts +8 -0
- package/dist/evaluators/summarization.d.ts +25 -6
- package/dist/index.d.ts +32 -152
- package/dist/node/index.node.js +944 -2210
- package/dist/node/index.node.js.map +13 -23
- package/dist/providers/context-summary.d.ts +12 -0
- package/dist/providers/long-term-memory.d.ts +11 -18
- package/dist/schemas/index.d.ts +6 -16
- package/dist/schemas/long-term-memories.d.ts +70 -308
- package/dist/schemas/memory-access-logs.d.ts +154 -0
- package/dist/schemas/session-summaries.d.ts +283 -0
- package/dist/services/memory-service.d.ts +51 -95
- package/dist/types/index.d.ts +53 -298
- package/package.json +2 -84
- package/dist/evaluators/consolidation.d.ts +0 -19
- package/dist/prompts/consolidation.d.ts +0 -35
- package/dist/prompts/summarization.d.ts +0 -25
- package/dist/providers/action-results.d.ts +0 -2
- package/dist/providers/recent-conversation-summary.d.ts +0 -2
- package/dist/repositories/conversation-summary.d.ts +0 -33
- package/dist/repositories/index.d.ts +0 -17
- package/dist/repositories/long-term-memory.d.ts +0 -53
- package/dist/schemas/conversation-summaries.d.ts +0 -494
- package/dist/utils/db-mapping.d.ts +0 -20
- package/dist/utils/decay-scoring.d.ts +0 -41
- package/dist/utils/embedding.d.ts +0 -21
- package/dist/utils/formatting.d.ts +0 -17
- package/dist/utils/index.d.ts +0 -17
- package/dist/utils/search-merging.d.ts +0 -18
- package/dist/utils/token-counter.d.ts +0 -53
package/dist/cjs/index.node.cjs
CHANGED
|
@@ -29,2397 +29,1140 @@ var __export = (target, all) => {
|
|
|
29
29
|
// src/index.node.ts
|
|
30
30
|
var exports_index_node = {};
|
|
31
31
|
__export(exports_index_node, {
|
|
32
|
-
|
|
33
|
-
recentContextProvider: () => recentContextProvider,
|
|
34
|
-
mergeSearchResults: () => mergeSearchResults,
|
|
32
|
+
sessionSummaries: () => sessionSummaries,
|
|
35
33
|
memoryPlugin: () => memoryPlugin,
|
|
36
|
-
|
|
37
|
-
mapDbRowToConversationSummary: () => mapDbRowToConversationSummary,
|
|
34
|
+
memoryAccessLogs: () => memoryAccessLogs,
|
|
38
35
|
longTermMemoryProvider: () => longTermMemoryProvider,
|
|
39
|
-
longTermMemoryEmbeddings: () => longTermMemoryEmbeddings,
|
|
40
36
|
longTermMemories: () => longTermMemories,
|
|
41
|
-
generateEmbedding: () => generateEmbedding,
|
|
42
|
-
formatTokenCount: () => formatTokenCount,
|
|
43
|
-
formatMemoriesForContext: () => formatMemoriesForContext,
|
|
44
|
-
estimateTokensInSummary: () => estimateTokensInSummary,
|
|
45
|
-
estimateTokenCountForArray: () => estimateTokenCountForArray,
|
|
46
|
-
estimateTokenCount: () => estimateTokenCount,
|
|
47
37
|
default: () => src_default,
|
|
48
|
-
|
|
49
|
-
conversationSummaries: () => conversationSummaries,
|
|
50
|
-
cleanEmbedding: () => cleanEmbedding,
|
|
51
|
-
calculateDecayFactor: () => calculateDecayFactor,
|
|
52
|
-
calculateAccessBoost: () => calculateAccessBoost,
|
|
53
|
-
buildLevel1SummaryPrompt: () => buildLevel1SummaryPrompt,
|
|
54
|
-
buildHigherLevelSummaryPrompt: () => buildHigherLevelSummaryPrompt,
|
|
55
|
-
buildExtractionPrompt: () => buildExtractionPrompt,
|
|
56
|
-
buildContradictionPrompt: () => buildContradictionPrompt,
|
|
57
|
-
applyDecayScoring: () => applyDecayScoring,
|
|
58
|
-
actionResultsProvider: () => actionResultsProvider,
|
|
59
|
-
SUMMARIZATION_SYSTEM_PROMPT: () => SUMMARIZATION_SYSTEM_PROMPT,
|
|
60
|
-
MemoryType: () => MemoryType,
|
|
38
|
+
contextSummaryProvider: () => contextSummaryProvider,
|
|
61
39
|
MemoryService: () => MemoryService,
|
|
62
|
-
|
|
63
|
-
LongTermMemoryRepository: () => LongTermMemoryRepository,
|
|
64
|
-
HIGHER_LEVEL_SUMMARIZATION_SYSTEM_PROMPT: () => HIGHER_LEVEL_SUMMARIZATION_SYSTEM_PROMPT,
|
|
65
|
-
DecayFunction: () => DecayFunction,
|
|
66
|
-
ConversationSummaryRepository: () => ConversationSummaryRepository,
|
|
67
|
-
CONSOLIDATION_SYSTEM_PROMPT: () => CONSOLIDATION_SYSTEM_PROMPT
|
|
40
|
+
LongTermMemoryCategory: () => LongTermMemoryCategory
|
|
68
41
|
});
|
|
69
42
|
module.exports = __toCommonJS(exports_index_node);
|
|
70
43
|
|
|
71
44
|
// src/services/memory-service.ts
|
|
72
|
-
var
|
|
73
|
-
var
|
|
74
|
-
|
|
75
|
-
// src/types/index.ts
|
|
76
|
-
var MEMORY_DIMENSION_MAP = {
|
|
77
|
-
384: "dim384",
|
|
78
|
-
512: "dim512",
|
|
79
|
-
768: "dim768",
|
|
80
|
-
1024: "dim1024",
|
|
81
|
-
1536: "dim1536",
|
|
82
|
-
3072: "dim3072"
|
|
83
|
-
};
|
|
84
|
-
var MemoryType;
|
|
85
|
-
((MemoryType2) => {
|
|
86
|
-
MemoryType2["EPISODIC"] = "EPISODIC";
|
|
87
|
-
MemoryType2["SEMANTIC"] = "SEMANTIC";
|
|
88
|
-
MemoryType2["PROCEDURAL"] = "PROCEDURAL";
|
|
89
|
-
})(MemoryType ||= {});
|
|
90
|
-
var DecayFunction;
|
|
91
|
-
((DecayFunction2) => {
|
|
92
|
-
DecayFunction2["EXPONENTIAL"] = "EXPONENTIAL";
|
|
93
|
-
DecayFunction2["LINEAR"] = "LINEAR";
|
|
94
|
-
DecayFunction2["NONE"] = "NONE";
|
|
95
|
-
})(DecayFunction ||= {});
|
|
96
|
-
|
|
97
|
-
// src/repositories/long-term-memory.ts
|
|
98
|
-
var import_core3 = require("@elizaos/core");
|
|
99
|
-
var import_drizzle_orm3 = require("drizzle-orm");
|
|
45
|
+
var import_core = require("@elizaos/core");
|
|
46
|
+
var import_drizzle_orm4 = require("drizzle-orm");
|
|
100
47
|
|
|
101
48
|
// src/schemas/index.ts
|
|
102
49
|
var exports_schemas = {};
|
|
103
50
|
__export(exports_schemas, {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
conversationSummaries: () => conversationSummaries
|
|
51
|
+
sessionSummaries: () => sessionSummaries,
|
|
52
|
+
memoryAccessLogs: () => memoryAccessLogs,
|
|
53
|
+
longTermMemories: () => longTermMemories
|
|
108
54
|
});
|
|
109
55
|
|
|
110
56
|
// src/schemas/long-term-memories.ts
|
|
111
57
|
var import_drizzle_orm = require("drizzle-orm");
|
|
112
58
|
var import_pg_core = require("drizzle-orm/pg-core");
|
|
113
|
-
var import_core = require("@elizaos/core");
|
|
114
59
|
var longTermMemories = import_pg_core.pgTable("long_term_memories", {
|
|
115
60
|
id: import_pg_core.varchar("id", { length: 36 }).primaryKey(),
|
|
116
61
|
agentId: import_pg_core.varchar("agent_id", { length: 36 }).notNull(),
|
|
117
62
|
entityId: import_pg_core.varchar("entity_id", { length: 36 }).notNull(),
|
|
118
|
-
|
|
119
|
-
type: import_pg_core.text("type").notNull(),
|
|
63
|
+
category: import_pg_core.text("category").notNull(),
|
|
120
64
|
content: import_pg_core.text("content").notNull(),
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
65
|
+
metadata: import_pg_core.jsonb("metadata"),
|
|
66
|
+
embedding: import_pg_core.real("embedding").array(),
|
|
67
|
+
confidence: import_pg_core.real("confidence").default(1),
|
|
68
|
+
source: import_pg_core.text("source"),
|
|
125
69
|
createdAt: import_pg_core.timestamp("created_at").default(import_drizzle_orm.sql`now()`).notNull(),
|
|
70
|
+
updatedAt: import_pg_core.timestamp("updated_at").default(import_drizzle_orm.sql`now()`).notNull(),
|
|
126
71
|
lastAccessedAt: import_pg_core.timestamp("last_accessed_at"),
|
|
127
|
-
accessCount: import_pg_core.integer("access_count").default(0)
|
|
128
|
-
isActive: import_pg_core.boolean("is_active").default(true).notNull(),
|
|
129
|
-
source: import_pg_core.jsonb("source").notNull().default({}),
|
|
130
|
-
metadata: import_pg_core.jsonb("metadata").notNull().default({}),
|
|
131
|
-
supersedesId: import_pg_core.varchar("supersedes_id", { length: 36 })
|
|
72
|
+
accessCount: import_pg_core.integer("access_count").default(0)
|
|
132
73
|
}, (table) => ({
|
|
133
|
-
agentEntityIdx: import_pg_core.index("
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
confidenceIdx: import_pg_core.index("ltm_confidence_idx").on(table.confidence),
|
|
138
|
-
createdAtIdx: import_pg_core.index("ltm_created_at_idx").on(table.createdAt),
|
|
139
|
-
lastAccessedIdx: import_pg_core.index("ltm_last_accessed_idx").on(table.lastAccessedAt),
|
|
140
|
-
agentEntityActiveConfidenceIdx: import_pg_core.index("ltm_agent_entity_active_conf_idx").on(table.agentId, table.entityId, table.isActive, table.confidence)
|
|
74
|
+
agentEntityIdx: import_pg_core.index("long_term_memories_agent_entity_idx").on(table.agentId, table.entityId),
|
|
75
|
+
categoryIdx: import_pg_core.index("long_term_memories_category_idx").on(table.category),
|
|
76
|
+
confidenceIdx: import_pg_core.index("long_term_memories_confidence_idx").on(table.confidence),
|
|
77
|
+
createdAtIdx: import_pg_core.index("long_term_memories_created_at_idx").on(table.createdAt)
|
|
141
78
|
}));
|
|
142
|
-
|
|
143
|
-
id: import_pg_core.varchar("id", { length: 36 }).primaryKey(),
|
|
144
|
-
memoryId: import_pg_core.varchar("memory_id", { length: 36 }).notNull().references(() => longTermMemories.id, { onDelete: "cascade" }),
|
|
145
|
-
dim384: import_pg_core.vector("dim_384", { dimensions: import_core.VECTOR_DIMS.SMALL }),
|
|
146
|
-
dim512: import_pg_core.vector("dim_512", { dimensions: import_core.VECTOR_DIMS.MEDIUM }),
|
|
147
|
-
dim768: import_pg_core.vector("dim_768", { dimensions: import_core.VECTOR_DIMS.LARGE }),
|
|
148
|
-
dim1024: import_pg_core.vector("dim_1024", { dimensions: import_core.VECTOR_DIMS.XL }),
|
|
149
|
-
dim1536: import_pg_core.vector("dim_1536", { dimensions: import_core.VECTOR_DIMS.XXL }),
|
|
150
|
-
dim3072: import_pg_core.vector("dim_3072", { dimensions: import_core.VECTOR_DIMS.XXXL }),
|
|
151
|
-
createdAt: import_pg_core.timestamp("created_at").default(import_drizzle_orm.sql`now()`).notNull()
|
|
152
|
-
}, (table) => [
|
|
153
|
-
import_pg_core.index("idx_ltm_embedding_memory_id").on(table.memoryId),
|
|
154
|
-
import_pg_core.foreignKey({
|
|
155
|
-
name: "fk_ltm_embedding_memory",
|
|
156
|
-
columns: [table.memoryId],
|
|
157
|
-
foreignColumns: [longTermMemories.id]
|
|
158
|
-
}).onDelete("cascade")
|
|
159
|
-
]);
|
|
160
|
-
// src/schemas/conversation-summaries.ts
|
|
79
|
+
// src/schemas/session-summaries.ts
|
|
161
80
|
var import_drizzle_orm2 = require("drizzle-orm");
|
|
162
81
|
var import_pg_core2 = require("drizzle-orm/pg-core");
|
|
163
|
-
var
|
|
164
|
-
var conversationSummaries = import_pg_core2.pgTable("conversation_summaries", {
|
|
82
|
+
var sessionSummaries = import_pg_core2.pgTable("session_summaries", {
|
|
165
83
|
id: import_pg_core2.varchar("id", { length: 36 }).primaryKey(),
|
|
166
84
|
agentId: import_pg_core2.varchar("agent_id", { length: 36 }).notNull(),
|
|
167
|
-
entityId: import_pg_core2.varchar("entity_id", { length: 36 }).notNull(),
|
|
168
85
|
roomId: import_pg_core2.varchar("room_id", { length: 36 }).notNull(),
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
86
|
+
entityId: import_pg_core2.varchar("entity_id", { length: 36 }),
|
|
87
|
+
summary: import_pg_core2.text("summary").notNull(),
|
|
88
|
+
messageCount: import_pg_core2.integer("message_count").notNull(),
|
|
89
|
+
lastMessageOffset: import_pg_core2.integer("last_message_offset").notNull().default(0),
|
|
173
90
|
startTime: import_pg_core2.timestamp("start_time").notNull(),
|
|
174
91
|
endTime: import_pg_core2.timestamp("end_time").notNull(),
|
|
175
|
-
|
|
176
|
-
|
|
92
|
+
topics: import_pg_core2.jsonb("topics"),
|
|
93
|
+
metadata: import_pg_core2.jsonb("metadata"),
|
|
94
|
+
embedding: import_pg_core2.real("embedding").array(),
|
|
177
95
|
createdAt: import_pg_core2.timestamp("created_at").default(import_drizzle_orm2.sql`now()`).notNull(),
|
|
178
|
-
|
|
179
|
-
accessCount: import_pg_core2.integer("access_count").default(0).notNull(),
|
|
180
|
-
metadata: import_pg_core2.jsonb("metadata").notNull().default({})
|
|
96
|
+
updatedAt: import_pg_core2.timestamp("updated_at").default(import_drizzle_orm2.sql`now()`).notNull()
|
|
181
97
|
}, (table) => ({
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
timeRangeIdx: import_pg_core2.index("cs_time_range_idx").on(table.startTime, table.endTime),
|
|
186
|
-
createdAtIdx: import_pg_core2.index("cs_created_at_idx").on(table.createdAt),
|
|
187
|
-
lastAccessedIdx: import_pg_core2.index("cs_last_accessed_idx").on(table.lastAccessedAt),
|
|
188
|
-
entityRoomLevelTimeIdx: import_pg_core2.index("cs_entity_room_level_time_idx").on(table.entityId, table.roomId, table.level, table.createdAt)
|
|
98
|
+
agentRoomIdx: import_pg_core2.index("session_summaries_agent_room_idx").on(table.agentId, table.roomId),
|
|
99
|
+
entityIdx: import_pg_core2.index("session_summaries_entity_idx").on(table.entityId),
|
|
100
|
+
startTimeIdx: import_pg_core2.index("session_summaries_start_time_idx").on(table.startTime)
|
|
189
101
|
}));
|
|
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
|
-
content: row.content,
|
|
240
|
-
embedding: row.embedding,
|
|
241
|
-
tokenCount: row.tokenCount,
|
|
242
|
-
startTime: row.startTime,
|
|
243
|
-
endTime: row.endTime,
|
|
244
|
-
sourceCount: row.sourceCount,
|
|
245
|
-
sourceIds: row.sourceIds,
|
|
246
|
-
createdAt: row.createdAt,
|
|
247
|
-
lastAccessedAt: row.lastAccessedAt,
|
|
248
|
-
accessCount: row.accessCount,
|
|
249
|
-
metadata: row.metadata || {}
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
// src/repositories/long-term-memory.ts
|
|
254
|
-
class LongTermMemoryRepository {
|
|
255
|
-
runtime;
|
|
256
|
-
embeddingDimension;
|
|
257
|
-
constructor(runtime, embeddingDimension) {
|
|
258
|
-
this.runtime = runtime;
|
|
259
|
-
this.embeddingDimension = embeddingDimension;
|
|
102
|
+
// src/schemas/memory-access-logs.ts
|
|
103
|
+
var import_drizzle_orm3 = require("drizzle-orm");
|
|
104
|
+
var import_pg_core3 = require("drizzle-orm/pg-core");
|
|
105
|
+
var memoryAccessLogs = import_pg_core3.pgTable("memory_access_logs", {
|
|
106
|
+
id: import_pg_core3.varchar("id", { length: 36 }).primaryKey(),
|
|
107
|
+
agentId: import_pg_core3.varchar("agent_id", { length: 36 }).notNull(),
|
|
108
|
+
memoryId: import_pg_core3.varchar("memory_id", { length: 36 }).notNull(),
|
|
109
|
+
memoryType: import_pg_core3.text("memory_type").notNull(),
|
|
110
|
+
accessedAt: import_pg_core3.timestamp("accessed_at").default(import_drizzle_orm3.sql`now()`).notNull(),
|
|
111
|
+
roomId: import_pg_core3.varchar("room_id", { length: 36 }),
|
|
112
|
+
relevanceScore: import_pg_core3.real("relevance_score"),
|
|
113
|
+
wasUseful: import_pg_core3.integer("was_useful")
|
|
114
|
+
}, (table) => ({
|
|
115
|
+
memoryIdx: import_pg_core3.index("memory_access_logs_memory_idx").on(table.memoryId),
|
|
116
|
+
agentIdx: import_pg_core3.index("memory_access_logs_agent_idx").on(table.agentId),
|
|
117
|
+
accessedAtIdx: import_pg_core3.index("memory_access_logs_accessed_at_idx").on(table.accessedAt)
|
|
118
|
+
}));
|
|
119
|
+
// src/services/memory-service.ts
|
|
120
|
+
class MemoryService extends import_core.Service {
|
|
121
|
+
static serviceType = "memory";
|
|
122
|
+
sessionMessageCounts;
|
|
123
|
+
memoryConfig;
|
|
124
|
+
lastExtractionCheckpoints;
|
|
125
|
+
capabilityDescription = "Advanced memory management with short-term summarization and long-term persistent facts";
|
|
126
|
+
constructor(runtime) {
|
|
127
|
+
super(runtime);
|
|
128
|
+
this.sessionMessageCounts = new Map;
|
|
129
|
+
this.lastExtractionCheckpoints = new Map;
|
|
130
|
+
this.memoryConfig = {
|
|
131
|
+
shortTermSummarizationThreshold: 16,
|
|
132
|
+
shortTermRetainRecent: 6,
|
|
133
|
+
shortTermSummarizationInterval: 10,
|
|
134
|
+
longTermExtractionEnabled: true,
|
|
135
|
+
longTermVectorSearchEnabled: false,
|
|
136
|
+
longTermConfidenceThreshold: 0.85,
|
|
137
|
+
longTermExtractionThreshold: 30,
|
|
138
|
+
longTermExtractionInterval: 10,
|
|
139
|
+
summaryModelType: "TEXT_LARGE",
|
|
140
|
+
summaryMaxTokens: 2500,
|
|
141
|
+
summaryMaxNewMessages: 20
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
static async start(runtime) {
|
|
145
|
+
const service = new MemoryService(runtime);
|
|
146
|
+
await service.initialize(runtime);
|
|
147
|
+
return service;
|
|
148
|
+
}
|
|
149
|
+
async stop() {
|
|
150
|
+
import_core.logger.info("MemoryService stopped");
|
|
260
151
|
}
|
|
261
|
-
async
|
|
262
|
-
|
|
263
|
-
const
|
|
152
|
+
async initialize(runtime) {
|
|
153
|
+
this.runtime = runtime;
|
|
154
|
+
const threshold = runtime.getSetting("MEMORY_SUMMARIZATION_THRESHOLD");
|
|
155
|
+
if (threshold) {
|
|
156
|
+
this.memoryConfig.shortTermSummarizationThreshold = parseInt(threshold, 10);
|
|
157
|
+
}
|
|
158
|
+
const retainRecent = runtime.getSetting("MEMORY_RETAIN_RECENT");
|
|
159
|
+
if (retainRecent) {
|
|
160
|
+
this.memoryConfig.shortTermRetainRecent = parseInt(retainRecent, 10);
|
|
161
|
+
}
|
|
162
|
+
const summarizationInterval = runtime.getSetting("MEMORY_SUMMARIZATION_INTERVAL");
|
|
163
|
+
if (summarizationInterval) {
|
|
164
|
+
this.memoryConfig.shortTermSummarizationInterval = parseInt(summarizationInterval, 10);
|
|
165
|
+
}
|
|
166
|
+
const maxNewMessages = runtime.getSetting("MEMORY_MAX_NEW_MESSAGES");
|
|
167
|
+
if (maxNewMessages) {
|
|
168
|
+
this.memoryConfig.summaryMaxNewMessages = parseInt(maxNewMessages, 10);
|
|
169
|
+
}
|
|
170
|
+
const longTermEnabled = runtime.getSetting("MEMORY_LONG_TERM_ENABLED");
|
|
171
|
+
if (longTermEnabled === "false") {
|
|
172
|
+
this.memoryConfig.longTermExtractionEnabled = false;
|
|
173
|
+
} else if (longTermEnabled === "true") {
|
|
174
|
+
this.memoryConfig.longTermExtractionEnabled = true;
|
|
175
|
+
}
|
|
176
|
+
const confidenceThreshold = runtime.getSetting("MEMORY_CONFIDENCE_THRESHOLD");
|
|
177
|
+
if (confidenceThreshold) {
|
|
178
|
+
this.memoryConfig.longTermConfidenceThreshold = parseFloat(confidenceThreshold);
|
|
179
|
+
}
|
|
180
|
+
const extractionThreshold = runtime.getSetting("MEMORY_EXTRACTION_THRESHOLD");
|
|
181
|
+
if (extractionThreshold) {
|
|
182
|
+
this.memoryConfig.longTermExtractionThreshold = parseInt(extractionThreshold, 10);
|
|
183
|
+
}
|
|
184
|
+
const extractionInterval = runtime.getSetting("MEMORY_EXTRACTION_INTERVAL");
|
|
185
|
+
if (extractionInterval) {
|
|
186
|
+
this.memoryConfig.longTermExtractionInterval = parseInt(extractionInterval, 10);
|
|
187
|
+
}
|
|
188
|
+
import_core.logger.debug({
|
|
189
|
+
summarizationThreshold: this.memoryConfig.shortTermSummarizationThreshold,
|
|
190
|
+
summarizationInterval: this.memoryConfig.shortTermSummarizationInterval,
|
|
191
|
+
maxNewMessages: this.memoryConfig.summaryMaxNewMessages,
|
|
192
|
+
retainRecent: this.memoryConfig.shortTermRetainRecent,
|
|
193
|
+
longTermEnabled: this.memoryConfig.longTermExtractionEnabled,
|
|
194
|
+
extractionThreshold: this.memoryConfig.longTermExtractionThreshold,
|
|
195
|
+
extractionInterval: this.memoryConfig.longTermExtractionInterval,
|
|
196
|
+
confidenceThreshold: this.memoryConfig.longTermConfidenceThreshold
|
|
197
|
+
}, "MemoryService initialized");
|
|
198
|
+
}
|
|
199
|
+
getDb() {
|
|
200
|
+
const db = this.runtime.db;
|
|
264
201
|
if (!db) {
|
|
265
202
|
throw new Error("Database not available");
|
|
266
203
|
}
|
|
204
|
+
return db;
|
|
205
|
+
}
|
|
206
|
+
getConfig() {
|
|
207
|
+
return { ...this.memoryConfig };
|
|
208
|
+
}
|
|
209
|
+
updateConfig(updates) {
|
|
210
|
+
this.memoryConfig = { ...this.memoryConfig, ...updates };
|
|
211
|
+
}
|
|
212
|
+
incrementMessageCount(roomId) {
|
|
213
|
+
const current = this.sessionMessageCounts.get(roomId) || 0;
|
|
214
|
+
const newCount = current + 1;
|
|
215
|
+
this.sessionMessageCounts.set(roomId, newCount);
|
|
216
|
+
return newCount;
|
|
217
|
+
}
|
|
218
|
+
resetMessageCount(roomId) {
|
|
219
|
+
this.sessionMessageCounts.set(roomId, 0);
|
|
220
|
+
}
|
|
221
|
+
async shouldSummarize(roomId) {
|
|
222
|
+
const count = await this.runtime.countMemories(roomId, false, "messages");
|
|
223
|
+
return count >= this.memoryConfig.shortTermSummarizationThreshold;
|
|
224
|
+
}
|
|
225
|
+
getExtractionKey(entityId, roomId) {
|
|
226
|
+
return `memory:extraction:${entityId}:${roomId}`;
|
|
227
|
+
}
|
|
228
|
+
async getLastExtractionCheckpoint(entityId, roomId) {
|
|
229
|
+
const key = this.getExtractionKey(entityId, roomId);
|
|
230
|
+
const cached = this.lastExtractionCheckpoints.get(key);
|
|
231
|
+
if (cached !== undefined) {
|
|
232
|
+
return cached;
|
|
233
|
+
}
|
|
267
234
|
try {
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
const stillNotReady = await adapter.isReady();
|
|
273
|
-
if (stillNotReady === false) {
|
|
274
|
-
throw new Error("Database connection lost and could not reconnect");
|
|
275
|
-
}
|
|
276
|
-
}
|
|
235
|
+
const checkpoint = await this.runtime.getCache(key);
|
|
236
|
+
const messageCount = checkpoint ?? 0;
|
|
237
|
+
this.lastExtractionCheckpoints.set(key, messageCount);
|
|
238
|
+
return messageCount;
|
|
277
239
|
} catch (error) {
|
|
278
|
-
|
|
279
|
-
|
|
240
|
+
import_core.logger.warn({ error }, "Failed to get extraction checkpoint from cache");
|
|
241
|
+
return 0;
|
|
280
242
|
}
|
|
281
|
-
return db;
|
|
282
243
|
}
|
|
283
|
-
async
|
|
284
|
-
const
|
|
244
|
+
async setLastExtractionCheckpoint(entityId, roomId, messageCount) {
|
|
245
|
+
const key = this.getExtractionKey(entityId, roomId);
|
|
246
|
+
this.lastExtractionCheckpoints.set(key, messageCount);
|
|
247
|
+
try {
|
|
248
|
+
await this.runtime.setCache(key, messageCount);
|
|
249
|
+
import_core.logger.debug(`Set extraction checkpoint for ${entityId} in room ${roomId} at message count ${messageCount}`);
|
|
250
|
+
} catch (error) {
|
|
251
|
+
import_core.logger.error({ error }, "Failed to persist extraction checkpoint to cache");
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async shouldRunExtraction(entityId, roomId, currentMessageCount) {
|
|
255
|
+
const threshold = this.memoryConfig.longTermExtractionThreshold;
|
|
256
|
+
const interval = this.memoryConfig.longTermExtractionInterval;
|
|
257
|
+
if (currentMessageCount < threshold) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
const lastCheckpoint = await this.getLastExtractionCheckpoint(entityId, roomId);
|
|
261
|
+
const currentCheckpoint = Math.floor(currentMessageCount / interval) * interval;
|
|
262
|
+
const shouldRun = currentMessageCount >= threshold && currentCheckpoint > lastCheckpoint;
|
|
263
|
+
import_core.logger.debug({
|
|
264
|
+
entityId,
|
|
265
|
+
roomId,
|
|
266
|
+
currentMessageCount,
|
|
267
|
+
threshold,
|
|
268
|
+
interval,
|
|
269
|
+
lastCheckpoint,
|
|
270
|
+
currentCheckpoint,
|
|
271
|
+
shouldRun
|
|
272
|
+
}, "Extraction check");
|
|
273
|
+
return shouldRun;
|
|
274
|
+
}
|
|
275
|
+
async storeLongTermMemory(memory) {
|
|
276
|
+
const db = this.getDb();
|
|
285
277
|
const id = crypto.randomUUID();
|
|
286
278
|
const now = new Date;
|
|
287
279
|
const newMemory = {
|
|
288
280
|
id,
|
|
289
281
|
createdAt: now,
|
|
290
|
-
|
|
282
|
+
updatedAt: now,
|
|
291
283
|
accessCount: 0,
|
|
292
|
-
isActive: true,
|
|
293
|
-
embedding: embedding || [],
|
|
294
284
|
...memory
|
|
295
285
|
};
|
|
296
|
-
|
|
297
|
-
await
|
|
286
|
+
try {
|
|
287
|
+
await db.insert(longTermMemories).values({
|
|
298
288
|
id: newMemory.id,
|
|
299
289
|
agentId: newMemory.agentId,
|
|
300
290
|
entityId: newMemory.entityId,
|
|
301
|
-
|
|
302
|
-
type: newMemory.type,
|
|
291
|
+
category: newMemory.category,
|
|
303
292
|
content: newMemory.content,
|
|
304
|
-
|
|
293
|
+
metadata: newMemory.metadata || {},
|
|
294
|
+
embedding: newMemory.embedding,
|
|
305
295
|
confidence: newMemory.confidence,
|
|
306
|
-
decayRate: newMemory.decayRate,
|
|
307
|
-
decayFunction: newMemory.decayFunction,
|
|
308
|
-
createdAt: now,
|
|
309
|
-
lastAccessedAt: null,
|
|
310
|
-
accessCount: 0,
|
|
311
|
-
isActive: true,
|
|
312
296
|
source: newMemory.source,
|
|
313
|
-
|
|
314
|
-
|
|
297
|
+
accessCount: newMemory.accessCount,
|
|
298
|
+
createdAt: now,
|
|
299
|
+
updatedAt: now,
|
|
300
|
+
lastAccessedAt: newMemory.lastAccessedAt
|
|
315
301
|
});
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
};
|
|
322
|
-
embeddingValues[this.embeddingDimension] = embedding;
|
|
323
|
-
await tx.insert(longTermMemoryEmbeddings).values(embeddingValues);
|
|
324
|
-
}
|
|
325
|
-
});
|
|
326
|
-
import_core3.logger.info({
|
|
327
|
-
id: newMemory.id,
|
|
328
|
-
type: newMemory.type,
|
|
329
|
-
entityId: newMemory.entityId,
|
|
330
|
-
confidence: newMemory.confidence
|
|
331
|
-
}, "Stored new long-term memory");
|
|
302
|
+
} catch (error) {
|
|
303
|
+
import_core.logger.error({ error }, "Failed to store long-term memory");
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
import_core.logger.info(`Stored long-term memory: ${newMemory.category} for entity ${newMemory.entityId}`);
|
|
332
307
|
return newMemory;
|
|
333
308
|
}
|
|
334
|
-
async
|
|
335
|
-
const db =
|
|
336
|
-
const
|
|
337
|
-
|
|
338
|
-
|
|
309
|
+
async getLongTermMemories(entityId, category, limit = 10) {
|
|
310
|
+
const db = this.getDb();
|
|
311
|
+
const conditions = [
|
|
312
|
+
import_drizzle_orm4.eq(longTermMemories.agentId, this.runtime.agentId),
|
|
313
|
+
import_drizzle_orm4.eq(longTermMemories.entityId, entityId)
|
|
314
|
+
];
|
|
315
|
+
if (category) {
|
|
316
|
+
conditions.push(import_drizzle_orm4.eq(longTermMemories.category, category));
|
|
339
317
|
}
|
|
340
|
-
|
|
318
|
+
const results = await db.select().from(longTermMemories).where(import_drizzle_orm4.and(...conditions)).orderBy(import_drizzle_orm4.desc(longTermMemories.confidence), import_drizzle_orm4.desc(longTermMemories.updatedAt)).limit(limit);
|
|
319
|
+
return results.map((row) => ({
|
|
320
|
+
id: row.id,
|
|
321
|
+
agentId: row.agentId,
|
|
322
|
+
entityId: row.entityId,
|
|
323
|
+
category: row.category,
|
|
324
|
+
content: row.content,
|
|
325
|
+
metadata: row.metadata,
|
|
326
|
+
embedding: row.embedding,
|
|
327
|
+
confidence: row.confidence,
|
|
328
|
+
source: row.source,
|
|
329
|
+
createdAt: row.createdAt,
|
|
330
|
+
updatedAt: row.updatedAt,
|
|
331
|
+
lastAccessedAt: row.lastAccessedAt,
|
|
332
|
+
accessCount: row.accessCount
|
|
333
|
+
}));
|
|
341
334
|
}
|
|
342
|
-
async
|
|
343
|
-
const db =
|
|
344
|
-
const updateData = {
|
|
345
|
-
|
|
335
|
+
async updateLongTermMemory(id, entityId, updates) {
|
|
336
|
+
const db = this.getDb();
|
|
337
|
+
const updateData = {
|
|
338
|
+
updatedAt: new Date
|
|
339
|
+
};
|
|
340
|
+
if (updates.content !== undefined) {
|
|
346
341
|
updateData.content = updates.content;
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
if (updates.confidence !== undefined)
|
|
350
|
-
updateData.confidence = updates.confidence;
|
|
351
|
-
if (updates.decayRate !== undefined)
|
|
352
|
-
updateData.decayRate = updates.decayRate;
|
|
353
|
-
if (updates.decayFunction !== undefined)
|
|
354
|
-
updateData.decayFunction = updates.decayFunction;
|
|
355
|
-
if (updates.lastAccessedAt !== undefined)
|
|
356
|
-
updateData.lastAccessedAt = updates.lastAccessedAt;
|
|
357
|
-
if (updates.accessCount !== undefined)
|
|
358
|
-
updateData.accessCount = updates.accessCount;
|
|
359
|
-
if (updates.isActive !== undefined)
|
|
360
|
-
updateData.isActive = updates.isActive;
|
|
361
|
-
if (updates.source !== undefined)
|
|
362
|
-
updateData.source = updates.source;
|
|
363
|
-
if (updates.metadata !== undefined)
|
|
342
|
+
}
|
|
343
|
+
if (updates.metadata !== undefined) {
|
|
364
344
|
updateData.metadata = updates.metadata;
|
|
365
|
-
if (updates.supersedesId !== undefined)
|
|
366
|
-
updateData.supersedesId = updates.supersedesId;
|
|
367
|
-
await db.transaction(async (tx) => {
|
|
368
|
-
await tx.update(longTermMemories).set(updateData).where(import_drizzle_orm3.eq(longTermMemories.id, id));
|
|
369
|
-
if (newEmbedding && this.embeddingDimension) {
|
|
370
|
-
const embeddingUpdate = {};
|
|
371
|
-
embeddingUpdate[this.embeddingDimension] = newEmbedding;
|
|
372
|
-
await tx.update(longTermMemoryEmbeddings).set(embeddingUpdate).where(import_drizzle_orm3.eq(longTermMemoryEmbeddings.memoryId, id));
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
import_core3.logger.info({ id }, "Updated long-term memory");
|
|
376
|
-
}
|
|
377
|
-
async delete(id) {
|
|
378
|
-
const db = await this.getDb();
|
|
379
|
-
await db.delete(longTermMemories).where(import_drizzle_orm3.eq(longTermMemories.id, id));
|
|
380
|
-
import_core3.logger.info({ id }, "Deleted long-term memory");
|
|
381
|
-
}
|
|
382
|
-
async findByEntity(entityId, type, limit = 20, includeInactive = false) {
|
|
383
|
-
const db = await this.getDb();
|
|
384
|
-
const conditions = [
|
|
385
|
-
import_drizzle_orm3.eq(longTermMemories.agentId, this.runtime.agentId),
|
|
386
|
-
import_drizzle_orm3.eq(longTermMemories.entityId, entityId)
|
|
387
|
-
];
|
|
388
|
-
if (!includeInactive) {
|
|
389
|
-
conditions.push(import_drizzle_orm3.eq(longTermMemories.isActive, true));
|
|
390
345
|
}
|
|
391
|
-
if (
|
|
392
|
-
|
|
346
|
+
if (updates.confidence !== undefined) {
|
|
347
|
+
updateData.confidence = updates.confidence;
|
|
393
348
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
397
|
-
async vectorSearch(params, queryEmbedding, similarityThreshold = 0.3) {
|
|
398
|
-
if (!this.embeddingDimension) {
|
|
399
|
-
import_core3.logger.warn("Embedding dimension not set, skipping vector search");
|
|
400
|
-
return [];
|
|
349
|
+
if (updates.embedding !== undefined) {
|
|
350
|
+
updateData.embedding = updates.embedding;
|
|
401
351
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
const similarity = import_drizzle_orm3.sql`1 - (${import_drizzle_orm3.cosineDistance(longTermMemoryEmbeddings[this.embeddingDimension], queryEmbedding)})`;
|
|
405
|
-
const conditions = [
|
|
406
|
-
import_drizzle_orm3.eq(longTermMemories.agentId, this.runtime.agentId),
|
|
407
|
-
import_drizzle_orm3.eq(longTermMemories.entityId, params.entityId),
|
|
408
|
-
import_drizzle_orm3.sql`${longTermMemoryEmbeddings[this.embeddingDimension]} IS NOT NULL`,
|
|
409
|
-
import_drizzle_orm3.gte(similarity, similarityThreshold)
|
|
410
|
-
];
|
|
411
|
-
if (params.minConfidence) {
|
|
412
|
-
conditions.push(import_drizzle_orm3.gte(longTermMemories.confidence, params.minConfidence));
|
|
413
|
-
}
|
|
414
|
-
if (!params.includeInactive) {
|
|
415
|
-
conditions.push(import_drizzle_orm3.eq(longTermMemories.isActive, true));
|
|
416
|
-
}
|
|
417
|
-
if (params.type) {
|
|
418
|
-
conditions.push(import_drizzle_orm3.eq(longTermMemories.type, params.type));
|
|
419
|
-
}
|
|
420
|
-
if (params.roomId) {
|
|
421
|
-
conditions.push(import_drizzle_orm3.or(import_drizzle_orm3.eq(longTermMemories.roomId, params.roomId), import_drizzle_orm3.isNull(longTermMemories.roomId)));
|
|
422
|
-
}
|
|
423
|
-
const results = await db.select({
|
|
424
|
-
memory: longTermMemories,
|
|
425
|
-
embedding: longTermMemoryEmbeddings[this.embeddingDimension],
|
|
426
|
-
similarity
|
|
427
|
-
}).from(longTermMemories).innerJoin(longTermMemoryEmbeddings, import_drizzle_orm3.eq(longTermMemoryEmbeddings.memoryId, longTermMemories.id)).where(import_drizzle_orm3.and(...conditions)).orderBy(import_drizzle_orm3.desc(similarity)).limit(params.limit || 20);
|
|
428
|
-
return results.map((row) => ({
|
|
429
|
-
...mapDbRowToLongTermMemory(row.memory),
|
|
430
|
-
embedding: row.embedding,
|
|
431
|
-
relevanceScore: row.similarity,
|
|
432
|
-
activationScore: 0,
|
|
433
|
-
finalScore: 0
|
|
434
|
-
}));
|
|
435
|
-
} catch (error) {
|
|
436
|
-
import_core3.logger.error("Failed to execute vector search:", JSON.stringify(error));
|
|
437
|
-
return [];
|
|
352
|
+
if (updates.lastAccessedAt !== undefined) {
|
|
353
|
+
updateData.lastAccessedAt = updates.lastAccessedAt;
|
|
438
354
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
const db = await this.getDb();
|
|
442
|
-
const memories = await db.select().from(longTermMemories).where(import_drizzle_orm3.and(import_drizzle_orm3.eq(longTermMemories.agentId, this.runtime.agentId), import_drizzle_orm3.eq(longTermMemories.isActive, true)));
|
|
443
|
-
return memories.map((row) => ({
|
|
444
|
-
id: row.id,
|
|
445
|
-
content: row.content,
|
|
446
|
-
embeddingContext: row.embeddingContext
|
|
447
|
-
}));
|
|
448
|
-
}
|
|
449
|
-
async updateAccessMetadata(memoryIds) {
|
|
450
|
-
if (memoryIds.length === 0)
|
|
451
|
-
return;
|
|
452
|
-
const db = await this.getDb();
|
|
453
|
-
const now = new Date;
|
|
454
|
-
try {
|
|
455
|
-
for (const id of memoryIds) {
|
|
456
|
-
await db.update(longTermMemories).set({
|
|
457
|
-
lastAccessedAt: now,
|
|
458
|
-
accessCount: import_drizzle_orm3.sql`${longTermMemories.accessCount} + 1`
|
|
459
|
-
}).where(import_drizzle_orm3.eq(longTermMemories.id, id));
|
|
460
|
-
}
|
|
461
|
-
import_core3.logger.debug({ count: memoryIds.length }, "Updated access metadata");
|
|
462
|
-
} catch (error) {
|
|
463
|
-
import_core3.logger.error({ error }, "Failed to update access metadata");
|
|
355
|
+
if (updates.accessCount !== undefined) {
|
|
356
|
+
updateData.accessCount = updates.accessCount;
|
|
464
357
|
}
|
|
358
|
+
await db.update(longTermMemories).set(updateData).where(import_drizzle_orm4.and(import_drizzle_orm4.eq(longTermMemories.id, id), import_drizzle_orm4.eq(longTermMemories.agentId, this.runtime.agentId), import_drizzle_orm4.eq(longTermMemories.entityId, entityId)));
|
|
359
|
+
import_core.logger.info(`Updated long-term memory: ${id} for entity ${entityId}`);
|
|
465
360
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
var import_drizzle_orm4 = require("drizzle-orm");
|
|
471
|
-
class ConversationSummaryRepository {
|
|
472
|
-
runtime;
|
|
473
|
-
embeddingDimension;
|
|
474
|
-
constructor(runtime, embeddingDimension) {
|
|
475
|
-
this.runtime = runtime;
|
|
476
|
-
this.embeddingDimension = embeddingDimension;
|
|
361
|
+
async deleteLongTermMemory(id, entityId) {
|
|
362
|
+
const db = this.getDb();
|
|
363
|
+
await db.delete(longTermMemories).where(import_drizzle_orm4.and(import_drizzle_orm4.eq(longTermMemories.id, id), import_drizzle_orm4.eq(longTermMemories.agentId, this.runtime.agentId), import_drizzle_orm4.eq(longTermMemories.entityId, entityId)));
|
|
364
|
+
import_core.logger.info(`Deleted long-term memory: ${id} for entity ${entityId}`);
|
|
477
365
|
}
|
|
478
|
-
async
|
|
479
|
-
const
|
|
480
|
-
const
|
|
481
|
-
if (
|
|
482
|
-
|
|
483
|
-
}
|
|
484
|
-
try {
|
|
485
|
-
const isReady = await adapter.isReady();
|
|
486
|
-
if (!isReady) {
|
|
487
|
-
import_core4.logger.warn("[ConversationSummaryRepository] Database not ready, attempting reconnect...");
|
|
488
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
489
|
-
const stillNotReady = await adapter.isReady();
|
|
490
|
-
if (stillNotReady === false) {
|
|
491
|
-
throw new Error("Database connection lost and could not reconnect");
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
} catch (error) {
|
|
495
|
-
import_core4.logger.error("[ConversationSummaryRepository] Database health check failed:", error);
|
|
496
|
-
throw new Error("Database connection health check failed");
|
|
366
|
+
async getCurrentSessionSummary(roomId) {
|
|
367
|
+
const db = this.getDb();
|
|
368
|
+
const results = await db.select().from(sessionSummaries).where(import_drizzle_orm4.and(import_drizzle_orm4.eq(sessionSummaries.agentId, this.runtime.agentId), import_drizzle_orm4.eq(sessionSummaries.roomId, roomId))).orderBy(import_drizzle_orm4.desc(sessionSummaries.updatedAt)).limit(1);
|
|
369
|
+
if (results.length === 0) {
|
|
370
|
+
return null;
|
|
497
371
|
}
|
|
498
|
-
|
|
372
|
+
const row = results[0];
|
|
373
|
+
return {
|
|
374
|
+
id: row.id,
|
|
375
|
+
agentId: row.agentId,
|
|
376
|
+
roomId: row.roomId,
|
|
377
|
+
entityId: row.entityId,
|
|
378
|
+
summary: row.summary,
|
|
379
|
+
messageCount: row.messageCount,
|
|
380
|
+
lastMessageOffset: row.lastMessageOffset,
|
|
381
|
+
startTime: row.startTime,
|
|
382
|
+
endTime: row.endTime,
|
|
383
|
+
topics: row.topics || [],
|
|
384
|
+
metadata: row.metadata,
|
|
385
|
+
embedding: row.embedding,
|
|
386
|
+
createdAt: row.createdAt,
|
|
387
|
+
updatedAt: row.updatedAt
|
|
388
|
+
};
|
|
499
389
|
}
|
|
500
|
-
async
|
|
501
|
-
const db =
|
|
390
|
+
async storeSessionSummary(summary) {
|
|
391
|
+
const db = this.getDb();
|
|
502
392
|
const id = crypto.randomUUID();
|
|
503
393
|
const now = new Date;
|
|
504
394
|
const newSummary = {
|
|
505
395
|
id,
|
|
506
396
|
createdAt: now,
|
|
507
|
-
|
|
508
|
-
accessCount: 0,
|
|
509
|
-
embedding: embedding || [],
|
|
397
|
+
updatedAt: now,
|
|
510
398
|
...summary
|
|
511
399
|
};
|
|
512
|
-
await db.
|
|
513
|
-
await tx.insert(conversationSummaries).values({
|
|
514
|
-
id: newSummary.id,
|
|
515
|
-
agentId: newSummary.agentId,
|
|
516
|
-
entityId: newSummary.entityId,
|
|
517
|
-
roomId: newSummary.roomId,
|
|
518
|
-
level: newSummary.level,
|
|
519
|
-
parentSummaryId: newSummary.parentSummaryId || null,
|
|
520
|
-
content: newSummary.content,
|
|
521
|
-
tokenCount: newSummary.tokenCount,
|
|
522
|
-
startTime: newSummary.startTime,
|
|
523
|
-
endTime: newSummary.endTime,
|
|
524
|
-
sourceCount: newSummary.sourceCount,
|
|
525
|
-
sourceIds: newSummary.sourceIds,
|
|
526
|
-
createdAt: now,
|
|
527
|
-
lastAccessedAt: null,
|
|
528
|
-
accessCount: 0,
|
|
529
|
-
metadata: newSummary.metadata
|
|
530
|
-
});
|
|
531
|
-
if (embedding && this.embeddingDimension) {
|
|
532
|
-
const embeddingValues = {
|
|
533
|
-
id: crypto.randomUUID(),
|
|
534
|
-
summaryId: id,
|
|
535
|
-
createdAt: now
|
|
536
|
-
};
|
|
537
|
-
embeddingValues[this.embeddingDimension] = embedding;
|
|
538
|
-
await tx.insert(conversationSummaryEmbeddings).values(embeddingValues);
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
import_core4.logger.info({
|
|
400
|
+
await db.insert(sessionSummaries).values({
|
|
542
401
|
id: newSummary.id,
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
402
|
+
agentId: newSummary.agentId,
|
|
403
|
+
roomId: newSummary.roomId,
|
|
404
|
+
entityId: newSummary.entityId || null,
|
|
405
|
+
summary: newSummary.summary,
|
|
406
|
+
messageCount: newSummary.messageCount,
|
|
407
|
+
lastMessageOffset: newSummary.lastMessageOffset,
|
|
408
|
+
startTime: newSummary.startTime,
|
|
409
|
+
endTime: newSummary.endTime,
|
|
410
|
+
topics: newSummary.topics || [],
|
|
411
|
+
metadata: newSummary.metadata || {},
|
|
412
|
+
embedding: newSummary.embedding,
|
|
413
|
+
createdAt: now,
|
|
414
|
+
updatedAt: now
|
|
415
|
+
});
|
|
416
|
+
import_core.logger.info(`Stored session summary for room ${newSummary.roomId}`);
|
|
547
417
|
return newSummary;
|
|
548
418
|
}
|
|
549
|
-
async
|
|
550
|
-
const db =
|
|
551
|
-
const
|
|
552
|
-
|
|
419
|
+
async updateSessionSummary(id, roomId, updates) {
|
|
420
|
+
const db = this.getDb();
|
|
421
|
+
const updateData = {
|
|
422
|
+
updatedAt: new Date
|
|
423
|
+
};
|
|
424
|
+
if (updates.summary !== undefined) {
|
|
425
|
+
updateData.summary = updates.summary;
|
|
426
|
+
}
|
|
427
|
+
if (updates.messageCount !== undefined) {
|
|
428
|
+
updateData.messageCount = updates.messageCount;
|
|
429
|
+
}
|
|
430
|
+
if (updates.lastMessageOffset !== undefined) {
|
|
431
|
+
updateData.lastMessageOffset = updates.lastMessageOffset;
|
|
432
|
+
}
|
|
433
|
+
if (updates.endTime !== undefined) {
|
|
434
|
+
updateData.endTime = updates.endTime;
|
|
435
|
+
}
|
|
436
|
+
if (updates.topics !== undefined) {
|
|
437
|
+
updateData.topics = updates.topics;
|
|
438
|
+
}
|
|
439
|
+
if (updates.metadata !== undefined) {
|
|
440
|
+
updateData.metadata = updates.metadata;
|
|
441
|
+
}
|
|
442
|
+
if (updates.embedding !== undefined) {
|
|
443
|
+
updateData.embedding = updates.embedding;
|
|
444
|
+
}
|
|
445
|
+
await db.update(sessionSummaries).set(updateData).where(import_drizzle_orm4.and(import_drizzle_orm4.eq(sessionSummaries.id, id), import_drizzle_orm4.eq(sessionSummaries.agentId, this.runtime.agentId), import_drizzle_orm4.eq(sessionSummaries.roomId, roomId)));
|
|
446
|
+
import_core.logger.info(`Updated session summary: ${id} for room ${roomId}`);
|
|
447
|
+
}
|
|
448
|
+
async getSessionSummaries(roomId, limit = 5) {
|
|
449
|
+
const db = this.getDb();
|
|
450
|
+
const results = await db.select().from(sessionSummaries).where(import_drizzle_orm4.and(import_drizzle_orm4.eq(sessionSummaries.agentId, this.runtime.agentId), import_drizzle_orm4.eq(sessionSummaries.roomId, roomId))).orderBy(import_drizzle_orm4.desc(sessionSummaries.updatedAt)).limit(limit);
|
|
451
|
+
return results.map((row) => ({
|
|
452
|
+
id: row.id,
|
|
453
|
+
agentId: row.agentId,
|
|
454
|
+
roomId: row.roomId,
|
|
455
|
+
entityId: row.entityId,
|
|
456
|
+
summary: row.summary,
|
|
457
|
+
messageCount: row.messageCount,
|
|
458
|
+
lastMessageOffset: row.lastMessageOffset,
|
|
459
|
+
startTime: row.startTime,
|
|
460
|
+
endTime: row.endTime,
|
|
461
|
+
topics: row.topics || [],
|
|
462
|
+
metadata: row.metadata,
|
|
463
|
+
embedding: row.embedding,
|
|
464
|
+
createdAt: row.createdAt,
|
|
465
|
+
updatedAt: row.updatedAt
|
|
466
|
+
}));
|
|
553
467
|
}
|
|
554
|
-
async
|
|
555
|
-
if (!this.
|
|
556
|
-
|
|
557
|
-
return
|
|
468
|
+
async searchLongTermMemories(entityId, queryEmbedding, limit = 5, matchThreshold = 0.7) {
|
|
469
|
+
if (!this.memoryConfig.longTermVectorSearchEnabled) {
|
|
470
|
+
import_core.logger.warn("Vector search is not enabled, falling back to recent memories");
|
|
471
|
+
return this.getLongTermMemories(entityId, undefined, limit);
|
|
558
472
|
}
|
|
559
|
-
const db =
|
|
473
|
+
const db = this.getDb();
|
|
560
474
|
try {
|
|
561
|
-
const
|
|
475
|
+
const cleanVector = queryEmbedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
|
|
476
|
+
const similarity = import_drizzle_orm4.sql`1 - (${import_drizzle_orm4.cosineDistance(longTermMemories.embedding, cleanVector)})`;
|
|
477
|
+
const conditions = [
|
|
478
|
+
import_drizzle_orm4.eq(longTermMemories.agentId, this.runtime.agentId),
|
|
479
|
+
import_drizzle_orm4.eq(longTermMemories.entityId, entityId),
|
|
480
|
+
import_drizzle_orm4.sql`${longTermMemories.embedding} IS NOT NULL`
|
|
481
|
+
];
|
|
482
|
+
if (matchThreshold > 0) {
|
|
483
|
+
conditions.push(import_drizzle_orm4.gte(similarity, matchThreshold));
|
|
484
|
+
}
|
|
562
485
|
const results = await db.select({
|
|
563
|
-
|
|
564
|
-
embedding: conversationSummaryEmbeddings[this.embeddingDimension],
|
|
486
|
+
memory: longTermMemories,
|
|
565
487
|
similarity
|
|
566
|
-
}).from(
|
|
567
|
-
return results.map((
|
|
568
|
-
|
|
569
|
-
|
|
488
|
+
}).from(longTermMemories).where(import_drizzle_orm4.and(...conditions)).orderBy(import_drizzle_orm4.desc(similarity)).limit(limit);
|
|
489
|
+
return results.map((row) => ({
|
|
490
|
+
id: row.memory.id,
|
|
491
|
+
agentId: row.memory.agentId,
|
|
492
|
+
entityId: row.memory.entityId,
|
|
493
|
+
category: row.memory.category,
|
|
494
|
+
content: row.memory.content,
|
|
495
|
+
metadata: row.memory.metadata,
|
|
496
|
+
embedding: row.memory.embedding,
|
|
497
|
+
confidence: row.memory.confidence,
|
|
498
|
+
source: row.memory.source,
|
|
499
|
+
createdAt: row.memory.createdAt,
|
|
500
|
+
updatedAt: row.memory.updatedAt,
|
|
501
|
+
lastAccessedAt: row.memory.lastAccessedAt,
|
|
502
|
+
accessCount: row.memory.accessCount,
|
|
503
|
+
similarity: row.similarity
|
|
570
504
|
}));
|
|
571
505
|
} catch (error) {
|
|
572
|
-
|
|
573
|
-
return
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
async updateAccessMetadata(summaryIds) {
|
|
577
|
-
if (summaryIds.length === 0)
|
|
578
|
-
return;
|
|
579
|
-
const db = await this.getDb();
|
|
580
|
-
const now = new Date;
|
|
581
|
-
try {
|
|
582
|
-
for (const id of summaryIds) {
|
|
583
|
-
await db.update(conversationSummaries).set({
|
|
584
|
-
lastAccessedAt: now,
|
|
585
|
-
accessCount: import_drizzle_orm4.sql`${conversationSummaries.accessCount} + 1`
|
|
586
|
-
}).where(import_drizzle_orm4.eq(conversationSummaries.id, id));
|
|
587
|
-
}
|
|
588
|
-
import_core4.logger.debug({ count: summaryIds.length }, "Updated summary access metadata");
|
|
589
|
-
} catch (error) {
|
|
590
|
-
import_core4.logger.error({ error }, "Failed to update summary access metadata");
|
|
506
|
+
import_core.logger.warn({ error }, "Vector search failed, falling back to recent memories");
|
|
507
|
+
return this.getLongTermMemories(entityId, undefined, limit);
|
|
591
508
|
}
|
|
592
509
|
}
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
async function generateEmbedding(runtime, text3) {
|
|
598
|
-
try {
|
|
599
|
-
const embedding = await runtime.useModel(import_core5.ModelType.TEXT_EMBEDDING, text3);
|
|
600
|
-
return embedding;
|
|
601
|
-
} catch (error) {
|
|
602
|
-
import_core5.logger.error({ error }, "Failed to generate embedding");
|
|
603
|
-
return new Array(1536).fill(0);
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
function cleanEmbedding(embedding) {
|
|
607
|
-
return embedding.map((n) => Number.isFinite(n) ? Number(n.toFixed(6)) : 0);
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
// src/utils/decay-scoring.ts
|
|
611
|
-
function calculateDecayFactor(decayFunction, decayRate, timeDeltaDays) {
|
|
612
|
-
switch (decayFunction) {
|
|
613
|
-
case "EXPONENTIAL" /* EXPONENTIAL */:
|
|
614
|
-
return Math.exp(-decayRate * timeDeltaDays);
|
|
615
|
-
case "LINEAR" /* LINEAR */:
|
|
616
|
-
return Math.max(0, 1 - decayRate * timeDeltaDays);
|
|
617
|
-
case "NONE" /* NONE */:
|
|
618
|
-
return 1;
|
|
619
|
-
default:
|
|
620
|
-
return 1;
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
function calculateAccessBoost(accessCount) {
|
|
624
|
-
return 1 + Math.log(1 + accessCount) * 0.1;
|
|
625
|
-
}
|
|
626
|
-
function applyDecayScoring(memories) {
|
|
627
|
-
const now = Date.now();
|
|
628
|
-
return memories.map((memory) => {
|
|
629
|
-
const lastAccessed = memory.lastAccessedAt?.getTime() || memory.createdAt.getTime();
|
|
630
|
-
const timeDeltaDays = (now - lastAccessed) / (1000 * 60 * 60 * 24);
|
|
631
|
-
const decayFactor = calculateDecayFactor(memory.decayFunction, memory.decayRate, timeDeltaDays);
|
|
632
|
-
const accessBoost = calculateAccessBoost(memory.accessCount);
|
|
633
|
-
const activationScore = memory.confidence * decayFactor * accessBoost;
|
|
634
|
-
const finalScore = memory.relevanceScore * activationScore;
|
|
635
|
-
return {
|
|
636
|
-
...memory,
|
|
637
|
-
activationScore,
|
|
638
|
-
finalScore
|
|
639
|
-
};
|
|
640
|
-
});
|
|
641
|
-
}
|
|
642
|
-
|
|
643
|
-
// src/utils/search-merging.ts
|
|
644
|
-
function mergeSearchResults(vectorResults, bm25Results) {
|
|
645
|
-
const merged = new Map;
|
|
646
|
-
for (const result of vectorResults) {
|
|
647
|
-
merged.set(result.id, result);
|
|
648
|
-
}
|
|
649
|
-
for (const result of bm25Results) {
|
|
650
|
-
if (merged.has(result.id)) {
|
|
651
|
-
const existing = merged.get(result.id);
|
|
652
|
-
existing.relevanceScore = (existing.relevanceScore + result.relevanceScore) / 2;
|
|
653
|
-
} else {
|
|
654
|
-
merged.set(result.id, result);
|
|
510
|
+
async getFormattedLongTermMemories(entityId) {
|
|
511
|
+
const memories = await this.getLongTermMemories(entityId, undefined, 20);
|
|
512
|
+
if (memories.length === 0) {
|
|
513
|
+
return "";
|
|
655
514
|
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
if (memories.length === 0) {
|
|
663
|
-
return "";
|
|
664
|
-
}
|
|
665
|
-
const grouped = new Map;
|
|
666
|
-
for (const memory of memories) {
|
|
667
|
-
if (!grouped.has(memory.type)) {
|
|
668
|
-
grouped.set(memory.type, []);
|
|
515
|
+
const grouped = new Map;
|
|
516
|
+
for (const memory of memories) {
|
|
517
|
+
if (!grouped.has(memory.category)) {
|
|
518
|
+
grouped.set(memory.category, []);
|
|
519
|
+
}
|
|
520
|
+
grouped.get(memory.category)?.push(memory);
|
|
669
521
|
}
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
const facts = grouped.get("SEMANTIC" /* SEMANTIC */);
|
|
675
|
-
const items = facts.map((m) => `- ${m.content} (confidence: ${m.confidence.toFixed(2)})`).join(`
|
|
676
|
-
`);
|
|
677
|
-
sections.push(`**Semantic Knowledge (Facts)**:
|
|
678
|
-
${items}`);
|
|
679
|
-
}
|
|
680
|
-
if (grouped.has("EPISODIC" /* EPISODIC */)) {
|
|
681
|
-
const episodes = grouped.get("EPISODIC" /* EPISODIC */);
|
|
682
|
-
const items = episodes.map((m) => `- ${m.content} (${m.createdAt.toLocaleDateString()})`).join(`
|
|
683
|
-
`);
|
|
684
|
-
sections.push(`**Episodic Memory (Events)**:
|
|
685
|
-
${items}`);
|
|
686
|
-
}
|
|
687
|
-
if (grouped.has("PROCEDURAL" /* PROCEDURAL */)) {
|
|
688
|
-
const skills = grouped.get("PROCEDURAL" /* PROCEDURAL */);
|
|
689
|
-
const items = skills.map((m) => `- ${m.content}`).join(`
|
|
522
|
+
const sections = [];
|
|
523
|
+
for (const [category, categoryMemories] of grouped.entries()) {
|
|
524
|
+
const categoryName = category.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
|
|
525
|
+
const items = categoryMemories.map((m) => `- ${m.content}`).join(`
|
|
690
526
|
`);
|
|
691
|
-
|
|
527
|
+
sections.push(`**${categoryName}**:
|
|
692
528
|
${items}`);
|
|
693
|
-
|
|
694
|
-
|
|
529
|
+
}
|
|
530
|
+
return sections.join(`
|
|
695
531
|
|
|
696
532
|
`);
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
// src/utils/token-counter.ts
|
|
700
|
-
function estimateTokenCount(text3) {
|
|
701
|
-
if (!text3 || text3.length === 0) {
|
|
702
|
-
return 0;
|
|
703
533
|
}
|
|
704
|
-
const charCount = text3.length;
|
|
705
|
-
const estimatedTokens = Math.ceil(charCount / 4);
|
|
706
|
-
return estimatedTokens;
|
|
707
|
-
}
|
|
708
|
-
function estimateTokenCountForArray(texts) {
|
|
709
|
-
return texts.reduce((total, text3) => total + estimateTokenCount(text3), 0);
|
|
710
534
|
}
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
return result;
|
|
535
|
+
|
|
536
|
+
// src/evaluators/summarization.ts
|
|
537
|
+
var import_core2 = require("@elizaos/core");
|
|
538
|
+
async function getDialogueMessageCount(runtime, roomId) {
|
|
539
|
+
const messages = await runtime.getMemories({
|
|
540
|
+
tableName: "messages",
|
|
541
|
+
roomId,
|
|
542
|
+
count: 100,
|
|
543
|
+
unique: false
|
|
544
|
+
});
|
|
545
|
+
const dialogueMessages = messages.filter((msg) => !(msg.content?.type === "action_result" && msg.metadata?.type === "action_result") && (msg.metadata?.type === "agent_response_message" || msg.metadata?.type === "user_message"));
|
|
546
|
+
return dialogueMessages.length;
|
|
724
547
|
}
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
548
|
+
var initialSummarizationTemplate = `# Task: Summarize Conversation
|
|
549
|
+
|
|
550
|
+
You are analyzing a conversation to create a concise summary that captures the key points, topics, and important details.
|
|
551
|
+
|
|
552
|
+
# Recent Messages
|
|
553
|
+
{{recentMessages}}
|
|
554
|
+
|
|
555
|
+
# Instructions
|
|
556
|
+
Generate a summary that:
|
|
557
|
+
1. Captures the main topics discussed
|
|
558
|
+
2. Highlights key information shared
|
|
559
|
+
3. Notes any decisions made or questions asked
|
|
560
|
+
4. Maintains context for future reference
|
|
561
|
+
5. Is concise but comprehensive
|
|
562
|
+
|
|
563
|
+
**IMPORTANT**: Keep the summary under 2500 tokens. Be comprehensive but concise.
|
|
564
|
+
|
|
565
|
+
Also extract:
|
|
566
|
+
- **Topics**: List of main topics discussed (comma-separated)
|
|
567
|
+
- **Key Points**: Important facts or decisions (bullet points)
|
|
568
|
+
|
|
569
|
+
Respond in this XML format:
|
|
570
|
+
<summary>
|
|
571
|
+
<text>Your comprehensive summary here</text>
|
|
572
|
+
<topics>topic1, topic2, topic3</topics>
|
|
573
|
+
<keyPoints>
|
|
574
|
+
<point>First key point</point>
|
|
575
|
+
<point>Second key point</point>
|
|
576
|
+
</keyPoints>
|
|
577
|
+
</summary>`;
|
|
578
|
+
var updateSummarizationTemplate = `# Task: Update and Condense Conversation Summary
|
|
579
|
+
|
|
580
|
+
You are updating an existing conversation summary with new messages, while keeping the total summary concise.
|
|
581
|
+
|
|
582
|
+
# Existing Summary
|
|
583
|
+
{{existingSummary}}
|
|
584
|
+
|
|
585
|
+
# Existing Topics
|
|
586
|
+
{{existingTopics}}
|
|
587
|
+
|
|
588
|
+
# New Messages Since Last Summary
|
|
589
|
+
{{newMessages}}
|
|
590
|
+
|
|
591
|
+
# Instructions
|
|
592
|
+
Update the summary by:
|
|
593
|
+
1. Merging the existing summary with insights from the new messages
|
|
594
|
+
2. Removing redundant or less important details to stay under the token limit
|
|
595
|
+
3. Keeping the most important context and decisions
|
|
596
|
+
4. Adding new topics if they emerge
|
|
597
|
+
5. **CRITICAL**: Keep the ENTIRE updated summary under 2500 tokens
|
|
598
|
+
|
|
599
|
+
The goal is a rolling summary that captures the essence of the conversation without growing indefinitely.
|
|
600
|
+
|
|
601
|
+
Respond in this XML format:
|
|
602
|
+
<summary>
|
|
603
|
+
<text>Your updated and condensed summary here</text>
|
|
604
|
+
<topics>topic1, topic2, topic3</topics>
|
|
605
|
+
<keyPoints>
|
|
606
|
+
<point>First key point</point>
|
|
607
|
+
<point>Second key point</point>
|
|
608
|
+
</keyPoints>
|
|
609
|
+
</summary>`;
|
|
610
|
+
function parseSummaryXML(xml) {
|
|
611
|
+
const summaryMatch = xml.match(/<text>([\s\S]*?)<\/text>/);
|
|
612
|
+
const topicsMatch = xml.match(/<topics>([\s\S]*?)<\/topics>/);
|
|
613
|
+
const keyPointsMatches = xml.matchAll(/<point>([\s\S]*?)<\/point>/g);
|
|
614
|
+
const summary = summaryMatch ? summaryMatch[1].trim() : "Summary not available";
|
|
615
|
+
const topics = topicsMatch ? topicsMatch[1].split(",").map((t) => t.trim()).filter(Boolean) : [];
|
|
616
|
+
const keyPoints = Array.from(keyPointsMatches).map((match) => match[1].trim());
|
|
617
|
+
return { summary, topics, keyPoints };
|
|
733
618
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
minConfidence: 0.7,
|
|
747
|
-
enableVectorSearch: true,
|
|
748
|
-
enableBM25: true,
|
|
749
|
-
retrievalLimit: 5,
|
|
750
|
-
tokenBudget: 1000,
|
|
751
|
-
defaultDecayRates: {
|
|
752
|
-
["EPISODIC" /* EPISODIC */]: 0.05,
|
|
753
|
-
["SEMANTIC" /* SEMANTIC */]: 0.01,
|
|
754
|
-
["PROCEDURAL" /* PROCEDURAL */]: 0.02
|
|
755
|
-
},
|
|
756
|
-
enableContradictionDetection: true,
|
|
757
|
-
summarization: {
|
|
758
|
-
enabled: true,
|
|
759
|
-
messagesPerSummary: 7,
|
|
760
|
-
summariesPerLevel: 5,
|
|
761
|
-
maxDepth: 3,
|
|
762
|
-
summaryTokenBudget: 500
|
|
763
|
-
}
|
|
764
|
-
};
|
|
765
|
-
constructor(runtime) {
|
|
766
|
-
super(runtime);
|
|
767
|
-
this.config = { ...MemoryService.defaultConfig };
|
|
768
|
-
}
|
|
769
|
-
static async start(runtime) {
|
|
770
|
-
const service = new MemoryService(runtime);
|
|
771
|
-
await service.initialize(runtime);
|
|
772
|
-
return service;
|
|
773
|
-
}
|
|
774
|
-
async stop() {
|
|
775
|
-
import_core6.logger.info("MemoryService stopped");
|
|
776
|
-
}
|
|
777
|
-
async initialize(runtime) {
|
|
778
|
-
this.runtime = runtime;
|
|
779
|
-
this.loadConfiguration();
|
|
780
|
-
await this.ensureEmbeddingDimension();
|
|
781
|
-
this.longTermMemoryRepo = new LongTermMemoryRepository(runtime, this.embeddingDimension);
|
|
782
|
-
this.conversationSummaryRepo = new ConversationSummaryRepository(runtime, this.embeddingDimension);
|
|
783
|
-
import_core6.logger.info({ config: this.config }, "MemoryService initialized");
|
|
784
|
-
if (this.config.enableBM25) {
|
|
785
|
-
await this.rebuildBM25Index();
|
|
786
|
-
}
|
|
787
|
-
this.isInitialized = true;
|
|
788
|
-
}
|
|
789
|
-
loadConfiguration() {
|
|
790
|
-
const threshold = this.runtime.getSetting("MEMORY_CONSOLIDATION_THRESHOLD");
|
|
791
|
-
if (threshold) {
|
|
792
|
-
this.config.consolidationThreshold = parseInt(threshold, 10);
|
|
793
|
-
}
|
|
794
|
-
const minConfidence = this.runtime.getSetting("MEMORY_MIN_CONFIDENCE");
|
|
795
|
-
if (minConfidence) {
|
|
796
|
-
this.config.minConfidence = parseFloat(minConfidence);
|
|
797
|
-
}
|
|
798
|
-
const enableVector = this.runtime.getSetting("MEMORY_ENABLE_VECTOR_SEARCH");
|
|
799
|
-
import_core6.logger.debug({
|
|
800
|
-
enableVector,
|
|
801
|
-
type: typeof enableVector,
|
|
802
|
-
defaultValue: this.config.enableVectorSearch
|
|
803
|
-
}, "Loading MEMORY_ENABLE_VECTOR_SEARCH setting");
|
|
804
|
-
if (enableVector !== undefined && enableVector !== null && enableVector !== "") {
|
|
805
|
-
this.config.enableVectorSearch = enableVector === "true" || enableVector === true;
|
|
806
|
-
import_core6.logger.info({ enabled: this.config.enableVectorSearch }, "Vector search explicitly configured via environment variable");
|
|
807
|
-
} else {
|
|
808
|
-
import_core6.logger.info({ enabled: this.config.enableVectorSearch }, "Vector search using default configuration");
|
|
809
|
-
}
|
|
810
|
-
const enableBM25 = this.runtime.getSetting("MEMORY_ENABLE_BM25");
|
|
811
|
-
import_core6.logger.debug({
|
|
812
|
-
enableBM25,
|
|
813
|
-
type: typeof enableBM25,
|
|
814
|
-
defaultValue: this.config.enableBM25
|
|
815
|
-
}, "Loading MEMORY_ENABLE_BM25 setting");
|
|
816
|
-
if (enableBM25 !== undefined && enableBM25 !== null && enableBM25 !== "") {
|
|
817
|
-
this.config.enableBM25 = enableBM25 === "true" || enableBM25 === true;
|
|
818
|
-
import_core6.logger.info({ enabled: this.config.enableBM25 }, "BM25 search explicitly configured via environment variable");
|
|
819
|
-
} else {
|
|
820
|
-
import_core6.logger.info({ enabled: this.config.enableBM25 }, "BM25 search using default configuration");
|
|
821
|
-
}
|
|
822
|
-
const retrievalLimit = this.runtime.getSetting("MEMORY_RETRIEVAL_LIMIT");
|
|
823
|
-
if (retrievalLimit) {
|
|
824
|
-
this.config.retrievalLimit = parseInt(retrievalLimit, 10);
|
|
619
|
+
var summarizationEvaluator = {
|
|
620
|
+
name: "MEMORY_SUMMARIZATION",
|
|
621
|
+
description: "Automatically summarizes conversations to optimize context usage",
|
|
622
|
+
similes: [
|
|
623
|
+
"CONVERSATION_SUMMARY",
|
|
624
|
+
"CONTEXT_COMPRESSION",
|
|
625
|
+
"MEMORY_OPTIMIZATION"
|
|
626
|
+
],
|
|
627
|
+
alwaysRun: true,
|
|
628
|
+
validate: async (runtime, message) => {
|
|
629
|
+
if (!message.content?.text) {
|
|
630
|
+
return false;
|
|
825
631
|
}
|
|
826
|
-
const
|
|
827
|
-
if (
|
|
828
|
-
|
|
632
|
+
const memoryService = runtime.getService("memory");
|
|
633
|
+
if (!memoryService) {
|
|
634
|
+
return false;
|
|
829
635
|
}
|
|
830
|
-
const
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
if (summaryEnabled !== undefined && summaryEnabled !== null && summaryEnabled !== "") {
|
|
837
|
-
this.config.summarization.enabled = summaryEnabled === "true" || summaryEnabled === true;
|
|
838
|
-
import_core6.logger.info({ enabled: this.config.summarization.enabled }, "Summarization explicitly configured via environment variable");
|
|
636
|
+
const config = memoryService.getConfig();
|
|
637
|
+
const currentDialogueCount = await getDialogueMessageCount(runtime, message.roomId);
|
|
638
|
+
const existingSummary = await memoryService.getCurrentSessionSummary(message.roomId);
|
|
639
|
+
if (!existingSummary) {
|
|
640
|
+
const shouldSummarize = currentDialogueCount >= config.shortTermSummarizationThreshold;
|
|
641
|
+
return shouldSummarize;
|
|
839
642
|
} else {
|
|
840
|
-
|
|
643
|
+
const newDialogueCount = currentDialogueCount - existingSummary.lastMessageOffset;
|
|
644
|
+
const shouldUpdate = newDialogueCount >= config.shortTermSummarizationInterval;
|
|
645
|
+
return shouldUpdate;
|
|
841
646
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
this.config.summarization.summariesPerLevel = parseInt(summariesPerLevel, 10);
|
|
849
|
-
}
|
|
850
|
-
const maxDepth = this.runtime.getSetting("MEMORY_SUMMARY_MAX_DEPTH");
|
|
851
|
-
if (maxDepth) {
|
|
852
|
-
this.config.summarization.maxDepth = parseInt(maxDepth, 10);
|
|
853
|
-
}
|
|
854
|
-
const summaryTokenBudget = this.runtime.getSetting("MEMORY_SUMMARY_TOKEN_BUDGET");
|
|
855
|
-
if (summaryTokenBudget) {
|
|
856
|
-
this.config.summarization.summaryTokenBudget = parseInt(summaryTokenBudget, 10);
|
|
647
|
+
},
|
|
648
|
+
handler: async (runtime, message) => {
|
|
649
|
+
const memoryService = runtime.getService("memory");
|
|
650
|
+
if (!memoryService) {
|
|
651
|
+
import_core2.logger.error("MemoryService not found");
|
|
652
|
+
return;
|
|
857
653
|
}
|
|
858
|
-
|
|
859
|
-
|
|
654
|
+
const config = memoryService.getConfig();
|
|
655
|
+
const { roomId } = message;
|
|
860
656
|
try {
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
657
|
+
import_core2.logger.info(`Starting summarization for room ${roomId}`);
|
|
658
|
+
const existingSummary = await memoryService.getCurrentSessionSummary(roomId);
|
|
659
|
+
const lastOffset = existingSummary?.lastMessageOffset || 0;
|
|
660
|
+
const allMessages = await runtime.getMemories({
|
|
661
|
+
tableName: "messages",
|
|
662
|
+
roomId,
|
|
663
|
+
count: 1000,
|
|
664
|
+
unique: false
|
|
665
|
+
});
|
|
666
|
+
const allDialogueMessages = allMessages.filter((msg) => !(msg.content?.type === "action_result" && msg.metadata?.type === "action_result") && (msg.metadata?.type === "agent_response_message" || msg.metadata?.type === "user_message"));
|
|
667
|
+
const totalDialogueCount = allDialogueMessages.length;
|
|
668
|
+
const newDialogueCount = totalDialogueCount - lastOffset;
|
|
669
|
+
if (newDialogueCount === 0) {
|
|
670
|
+
import_core2.logger.debug("No new dialogue messages to summarize");
|
|
864
671
|
return;
|
|
865
672
|
}
|
|
866
|
-
const
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
throw new Error("Invalid embedding received from model");
|
|
673
|
+
const maxNewMessages = config.summaryMaxNewMessages || 50;
|
|
674
|
+
const messagesToProcess = Math.min(newDialogueCount, maxNewMessages);
|
|
675
|
+
if (newDialogueCount > maxNewMessages) {
|
|
676
|
+
import_core2.logger.warn(`Capping new dialogue messages at ${maxNewMessages} (${newDialogueCount} available). Oldest messages will be skipped.`);
|
|
871
677
|
}
|
|
872
|
-
const
|
|
873
|
-
const
|
|
874
|
-
if (
|
|
875
|
-
|
|
876
|
-
}
|
|
877
|
-
this.embeddingDimension = dimensionColumn;
|
|
878
|
-
import_core6.logger.info(`Memory embedding dimension set to ${dimension} (${dimensionColumn})`);
|
|
879
|
-
} catch (error) {
|
|
880
|
-
import_core6.logger.error("Failed to ensure embedding dimension:", JSON.stringify(error));
|
|
881
|
-
throw error;
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
getConfig() {
|
|
885
|
-
return { ...this.config };
|
|
886
|
-
}
|
|
887
|
-
updateConfig(updates) {
|
|
888
|
-
this.config = { ...this.config, ...updates };
|
|
889
|
-
if (updates.enableBM25 !== undefined) {
|
|
890
|
-
if (updates.enableBM25 && !this.bm25Index) {
|
|
891
|
-
this.rebuildBM25Index();
|
|
892
|
-
} else if (!updates.enableBM25) {
|
|
893
|
-
this.bm25Index = null;
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
}
|
|
897
|
-
async storeLongTermMemory(memory) {
|
|
898
|
-
let embedding;
|
|
899
|
-
if (this.embeddingDimension) {
|
|
900
|
-
try {
|
|
901
|
-
const rawEmbedding = await generateEmbedding(this.runtime, memory.embeddingContext);
|
|
902
|
-
embedding = cleanEmbedding(rawEmbedding);
|
|
903
|
-
} catch (error) {
|
|
904
|
-
import_core6.logger.warn("Failed to generate embedding for long-term memory:", JSON.stringify(error));
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
const stored = await this.longTermMemoryRepo.insert(memory, embedding);
|
|
908
|
-
if (this.config.enableBM25 && this.bm25Index) {
|
|
909
|
-
await this.rebuildBM25Index();
|
|
910
|
-
}
|
|
911
|
-
return stored;
|
|
912
|
-
}
|
|
913
|
-
async getLongTermMemory(id) {
|
|
914
|
-
return this.longTermMemoryRepo.findById(id);
|
|
915
|
-
}
|
|
916
|
-
async updateLongTermMemory(id, updates) {
|
|
917
|
-
let newEmbedding;
|
|
918
|
-
if (updates.embeddingContext !== undefined && this.embeddingDimension) {
|
|
919
|
-
try {
|
|
920
|
-
const rawEmbedding = await generateEmbedding(this.runtime, updates.embeddingContext);
|
|
921
|
-
newEmbedding = cleanEmbedding(rawEmbedding);
|
|
922
|
-
} catch (error) {
|
|
923
|
-
import_core6.logger.warn("Failed to regenerate embedding:", JSON.stringify(error));
|
|
924
|
-
}
|
|
925
|
-
}
|
|
926
|
-
await this.longTermMemoryRepo.update(id, updates, newEmbedding);
|
|
927
|
-
}
|
|
928
|
-
async deleteLongTermMemory(id) {
|
|
929
|
-
await this.longTermMemoryRepo.delete(id);
|
|
930
|
-
}
|
|
931
|
-
async getLongTermMemories(entityId, type, limit = 20, includeInactive = false) {
|
|
932
|
-
return this.longTermMemoryRepo.findByEntity(entityId, type, limit, includeInactive);
|
|
933
|
-
}
|
|
934
|
-
async handleContradiction(entityId, newMemory) {
|
|
935
|
-
const searchResults = await this.searchLongTermMemories({
|
|
936
|
-
entityId,
|
|
937
|
-
query: newMemory.content,
|
|
938
|
-
type: newMemory.type,
|
|
939
|
-
limit: 5,
|
|
940
|
-
includeInactive: false
|
|
941
|
-
});
|
|
942
|
-
if (searchResults.length === 0) {
|
|
943
|
-
await this.storeLongTermMemory(newMemory);
|
|
944
|
-
return;
|
|
945
|
-
}
|
|
946
|
-
const contradictingMemory = await this.detectContradiction(newMemory, searchResults);
|
|
947
|
-
if (contradictingMemory) {
|
|
948
|
-
import_core6.logger.info({
|
|
949
|
-
oldMemoryId: contradictingMemory.id,
|
|
950
|
-
newContent: newMemory.content
|
|
951
|
-
}, "Contradiction detected, superseding old memory");
|
|
952
|
-
await this.updateLongTermMemory(contradictingMemory.id, {
|
|
953
|
-
isActive: false
|
|
954
|
-
});
|
|
955
|
-
const stored = await this.storeLongTermMemory({
|
|
956
|
-
...newMemory,
|
|
957
|
-
supersedesId: contradictingMemory.id
|
|
958
|
-
});
|
|
959
|
-
import_core6.logger.info({ newMemoryId: stored.id }, "Stored superseding memory");
|
|
960
|
-
} else {
|
|
961
|
-
await this.storeLongTermMemory(newMemory);
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
async detectContradiction(newMemory, existingMemories) {
|
|
965
|
-
const prompt = `# TASK: Contradiction Detection
|
|
966
|
-
|
|
967
|
-
You are analyzing whether a new long-term memory contradicts any existing long-term memories.
|
|
968
|
-
|
|
969
|
-
## New Long-Term Memory:
|
|
970
|
-
"${newMemory.content}"
|
|
971
|
-
|
|
972
|
-
## Existing Long-Term Memories:
|
|
973
|
-
${existingMemories.map((m, idx) => `${idx + 1}. "${m.content}" (confidence: ${m.confidence}, created: ${m.createdAt.toISOString()})`).join(`
|
|
974
|
-
`)}
|
|
975
|
-
|
|
976
|
-
## Instructions:
|
|
977
|
-
Determine if the new long-term memory directly contradicts any of the existing long-term memories. A contradiction means the statements cannot both be true.
|
|
978
|
-
|
|
979
|
-
Examples of contradictions:
|
|
980
|
-
- "User likes blue" vs "User hates blue"
|
|
981
|
-
- "User lives in Paris" vs "User lives in London"
|
|
982
|
-
|
|
983
|
-
Examples of non-contradictions (these are compatible):
|
|
984
|
-
- "User likes blue" vs "User likes blue only for clothes" (nuance, not contradiction)
|
|
985
|
-
- "User was in Paris" vs "User moved to London" (state change over time)
|
|
986
|
-
|
|
987
|
-
## Output Format:
|
|
988
|
-
Return an XML response:
|
|
989
|
-
|
|
990
|
-
<response>
|
|
991
|
-
<hasContradiction>true or false</hasContradiction>
|
|
992
|
-
<contradictingMemoryIndex>number or null</contradictingMemoryIndex>
|
|
993
|
-
<reasoning>Explanation</reasoning>
|
|
994
|
-
</response>
|
|
995
|
-
|
|
996
|
-
If no contradiction is found, set hasContradiction to false and contradictingMemoryIndex to null.`;
|
|
997
|
-
try {
|
|
998
|
-
const response = await this.runtime.useModel(import_core6.ModelType.TEXT_LARGE, {
|
|
999
|
-
prompt,
|
|
1000
|
-
temperature: 0.2
|
|
1001
|
-
});
|
|
1002
|
-
const responseText = typeof response === "string" ? response : JSON.stringify(response);
|
|
1003
|
-
const result = import_core6.parseKeyValueXml(responseText);
|
|
1004
|
-
if (!result) {
|
|
1005
|
-
import_core6.logger.warn("Failed to parse contradiction detection XML response");
|
|
1006
|
-
return null;
|
|
1007
|
-
}
|
|
1008
|
-
const hasContradiction = import_core6.parseBooleanFromText(typeof result.hasContradiction === "boolean" ? result.hasContradiction ? "true" : "false" : result.hasContradiction);
|
|
1009
|
-
let contradictingMemoryIndex = null;
|
|
1010
|
-
if (result.contradictingMemoryIndex !== null && result.contradictingMemoryIndex !== "null") {
|
|
1011
|
-
const parsed = parseInt(result.contradictingMemoryIndex, 10);
|
|
1012
|
-
if (!isNaN(parsed)) {
|
|
1013
|
-
contradictingMemoryIndex = parsed;
|
|
1014
|
-
}
|
|
1015
|
-
}
|
|
1016
|
-
if (hasContradiction && contradictingMemoryIndex !== null) {
|
|
1017
|
-
const contradictingMemory = existingMemories[contradictingMemoryIndex];
|
|
1018
|
-
import_core6.logger.info({ reasoning: result.reasoning }, "Contradiction detected");
|
|
1019
|
-
return contradictingMemory;
|
|
1020
|
-
}
|
|
1021
|
-
return null;
|
|
1022
|
-
} catch (error) {
|
|
1023
|
-
import_core6.logger.error({ error }, "Failed to detect contradiction");
|
|
1024
|
-
return null;
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
async searchLongTermMemories(params) {
|
|
1028
|
-
const limit = params.limit || this.config.retrievalLimit;
|
|
1029
|
-
const tokenBudget = params.tokenBudget || this.config.tokenBudget;
|
|
1030
|
-
const minConfidence = params.minConfidence !== undefined ? params.minConfidence : this.config.minConfidence;
|
|
1031
|
-
const similarityThreshold = params.similarityThreshold !== undefined ? params.similarityThreshold : 0.3;
|
|
1032
|
-
import_core6.logger.debug({
|
|
1033
|
-
limit,
|
|
1034
|
-
tokenBudget,
|
|
1035
|
-
minConfidence,
|
|
1036
|
-
similarityThreshold,
|
|
1037
|
-
vectorSearchEnabled: this.config.enableVectorSearch,
|
|
1038
|
-
bm25Enabled: this.config.enableBM25
|
|
1039
|
-
}, "Searching long-term memories");
|
|
1040
|
-
let vectorResults = [];
|
|
1041
|
-
if (this.config.enableVectorSearch) {
|
|
1042
|
-
import_core6.logger.debug("Vector search enabled, searching...");
|
|
1043
|
-
vectorResults = await this.vectorSearch(params, similarityThreshold);
|
|
1044
|
-
}
|
|
1045
|
-
import_core6.logger.debug({ vectorResults: vectorResults.length }, "Vector search results");
|
|
1046
|
-
let bm25Results = [];
|
|
1047
|
-
if (this.config.enableBM25) {
|
|
1048
|
-
bm25Results = await this.bm25Search(params);
|
|
1049
|
-
}
|
|
1050
|
-
const mergedResults = mergeSearchResults(vectorResults, bm25Results);
|
|
1051
|
-
const confidenceFiltered = mergedResults.filter((m) => m.confidence >= minConfidence);
|
|
1052
|
-
const decayedResults = applyDecayScoring(confidenceFiltered);
|
|
1053
|
-
const sorted = decayedResults.sort((a, b) => b.finalScore - a.finalScore);
|
|
1054
|
-
const topResults = sorted.slice(0, limit);
|
|
1055
|
-
const budgetedResults = trimToTokenBudget(topResults, tokenBudget, (memory) => memory.content, 15);
|
|
1056
|
-
import_core6.logger.debug({
|
|
1057
|
-
totalResults: sorted.length,
|
|
1058
|
-
afterCountLimit: topResults.length,
|
|
1059
|
-
afterTokenBudget: budgetedResults.length,
|
|
1060
|
-
tokenBudget,
|
|
1061
|
-
estimatedTokens: budgetedResults.reduce((sum, m) => sum + estimateTokenCount(m.content) + 15, 0)
|
|
1062
|
-
}, "Applied token budget to memory retrieval");
|
|
1063
|
-
await this.longTermMemoryRepo.updateAccessMetadata(budgetedResults.map((r) => r.id));
|
|
1064
|
-
return budgetedResults;
|
|
1065
|
-
}
|
|
1066
|
-
async vectorSearch(params, similarityThreshold = 0.3) {
|
|
1067
|
-
if (!this.embeddingDimension) {
|
|
1068
|
-
import_core6.logger.warn("Embedding dimension not set, skipping vector search");
|
|
1069
|
-
return [];
|
|
1070
|
-
}
|
|
1071
|
-
try {
|
|
1072
|
-
const rawEmbedding = await generateEmbedding(this.runtime, params.query);
|
|
1073
|
-
const queryEmbedding = cleanEmbedding(rawEmbedding);
|
|
1074
|
-
if (!queryEmbedding || !Array.isArray(queryEmbedding)) {
|
|
1075
|
-
import_core6.logger.warn("Failed to generate query embedding");
|
|
1076
|
-
return [];
|
|
1077
|
-
}
|
|
1078
|
-
return await this.longTermMemoryRepo.vectorSearch(params, queryEmbedding, similarityThreshold);
|
|
1079
|
-
} catch (error) {
|
|
1080
|
-
import_core6.logger.error("Failed to execute vector search:", JSON.stringify(error));
|
|
1081
|
-
return [];
|
|
1082
|
-
}
|
|
1083
|
-
}
|
|
1084
|
-
async bm25Search(params) {
|
|
1085
|
-
if (!this.bm25Index) {
|
|
1086
|
-
return [];
|
|
1087
|
-
}
|
|
1088
|
-
try {
|
|
1089
|
-
const bm25Results = this.bm25Index.search(params.query, params.limit || 20);
|
|
1090
|
-
const memoryPromises = bm25Results.map(async (result) => {
|
|
1091
|
-
const doc = this.bm25Index.documents[result.index];
|
|
1092
|
-
if (!doc || !doc.id) {
|
|
1093
|
-
import_core6.logger.warn({ resultIndex: result.index }, "BM25 result has no document ID");
|
|
1094
|
-
return null;
|
|
1095
|
-
}
|
|
1096
|
-
const memory = await this.getLongTermMemory(doc.id);
|
|
1097
|
-
if (!memory)
|
|
1098
|
-
return null;
|
|
1099
|
-
if (memory.entityId !== params.entityId)
|
|
1100
|
-
return null;
|
|
1101
|
-
if (!params.includeInactive && !memory.isActive)
|
|
1102
|
-
return null;
|
|
1103
|
-
if (params.type && memory.type !== params.type)
|
|
1104
|
-
return null;
|
|
1105
|
-
if (params.minConfidence && memory.confidence < params.minConfidence)
|
|
1106
|
-
return null;
|
|
1107
|
-
return {
|
|
1108
|
-
...memory,
|
|
1109
|
-
relevanceScore: result.score,
|
|
1110
|
-
activationScore: 0,
|
|
1111
|
-
finalScore: 0
|
|
1112
|
-
};
|
|
1113
|
-
});
|
|
1114
|
-
const results = await Promise.all(memoryPromises);
|
|
1115
|
-
return results.filter((r) => r !== null);
|
|
1116
|
-
} catch (error) {
|
|
1117
|
-
import_core6.logger.error({ error }, "BM25 search failed");
|
|
1118
|
-
return [];
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
async rebuildBM25Index() {
|
|
1122
|
-
try {
|
|
1123
|
-
const memories = await this.longTermMemoryRepo.fetchAllActive();
|
|
1124
|
-
const documents = memories.map((row) => ({
|
|
1125
|
-
id: row.id,
|
|
1126
|
-
content: row.content,
|
|
1127
|
-
embeddingContext: row.embeddingContext
|
|
1128
|
-
}));
|
|
1129
|
-
this.bm25Index = new import_core7.BM25(documents, {
|
|
1130
|
-
k1: 1.2,
|
|
1131
|
-
b: 0.75,
|
|
1132
|
-
stemming: true,
|
|
1133
|
-
minLength: 2
|
|
1134
|
-
});
|
|
1135
|
-
import_core6.logger.info({ documentCount: documents.length }, "Rebuilt BM25 index");
|
|
1136
|
-
} catch (error) {
|
|
1137
|
-
import_core6.logger.error({ error }, "Failed to rebuild BM25 index");
|
|
1138
|
-
this.bm25Index = null;
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
async getFormattedLongTermMemoriesForContext(entityId, query, roomId) {
|
|
1142
|
-
const memories = await this.searchLongTermMemories({
|
|
1143
|
-
entityId,
|
|
1144
|
-
query,
|
|
1145
|
-
roomId,
|
|
1146
|
-
limit: this.config.retrievalLimit
|
|
1147
|
-
});
|
|
1148
|
-
return formatMemoriesForContext(memories);
|
|
1149
|
-
}
|
|
1150
|
-
async storeSummary(summary) {
|
|
1151
|
-
let embedding;
|
|
1152
|
-
if (this.embeddingDimension) {
|
|
1153
|
-
try {
|
|
1154
|
-
const rawEmbedding = await generateEmbedding(this.runtime, summary.content);
|
|
1155
|
-
embedding = cleanEmbedding(rawEmbedding);
|
|
1156
|
-
} catch (error) {
|
|
1157
|
-
import_core6.logger.warn("Failed to generate embedding for summary:", JSON.stringify(error));
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
|
-
return this.conversationSummaryRepo.insert(summary, embedding);
|
|
1161
|
-
}
|
|
1162
|
-
async getSummariesByLevel(roomId, level) {
|
|
1163
|
-
return this.conversationSummaryRepo.findByLevel(roomId, level);
|
|
1164
|
-
}
|
|
1165
|
-
async searchSummaries(params) {
|
|
1166
|
-
if (!this.embeddingDimension) {
|
|
1167
|
-
import_core6.logger.warn("Embedding dimension not set, skipping summary search");
|
|
1168
|
-
return [];
|
|
1169
|
-
}
|
|
1170
|
-
const limit = params.limit || 5;
|
|
1171
|
-
const tokenBudget = params.tokenBudget || this.config.summarization?.summaryTokenBudget || 500;
|
|
1172
|
-
try {
|
|
1173
|
-
const rawEmbedding = await generateEmbedding(this.runtime, params.query);
|
|
1174
|
-
const queryEmbedding = cleanEmbedding(rawEmbedding);
|
|
1175
|
-
if (!queryEmbedding || !Array.isArray(queryEmbedding)) {
|
|
1176
|
-
import_core6.logger.warn("Failed to generate query embedding");
|
|
1177
|
-
return [];
|
|
1178
|
-
}
|
|
1179
|
-
const summaries = await this.conversationSummaryRepo.vectorSearch(params.entityId, params.roomId, queryEmbedding, limit);
|
|
1180
|
-
const budgetedSummaries = trimToTokenBudget(summaries, tokenBudget, (s) => s.content, 10);
|
|
1181
|
-
await this.conversationSummaryRepo.updateAccessMetadata(budgetedSummaries.map((s) => s.id));
|
|
1182
|
-
return budgetedSummaries;
|
|
1183
|
-
} catch (error) {
|
|
1184
|
-
import_core6.logger.error("Failed to search summaries:", JSON.stringify(error));
|
|
1185
|
-
return [];
|
|
1186
|
-
}
|
|
1187
|
-
}
|
|
1188
|
-
async getMostRecentLevel1Summary(roomId, entityId) {
|
|
1189
|
-
try {
|
|
1190
|
-
const summaries = await this.conversationSummaryRepo.findByLevel(roomId, 1);
|
|
1191
|
-
if (summaries.length === 0) {
|
|
1192
|
-
return null;
|
|
1193
|
-
}
|
|
1194
|
-
const entitySummaries = summaries.filter((s) => s.entityId === entityId).sort((a, b) => b.endTime.getTime() - a.endTime.getTime());
|
|
1195
|
-
return entitySummaries.length > 0 ? entitySummaries[0] : null;
|
|
1196
|
-
} catch (error) {
|
|
1197
|
-
import_core6.logger.error("Failed to get most recent Level 1 summary:", JSON.stringify(error));
|
|
1198
|
-
return null;
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
|
|
1203
|
-
// src/evaluators/consolidation.ts
|
|
1204
|
-
var import_core8 = require("@elizaos/core");
|
|
1205
|
-
|
|
1206
|
-
// src/prompts/consolidation.ts
|
|
1207
|
-
var CONSOLIDATION_SYSTEM_PROMPT = `You are the "Cortex" — an advanced Memory Extraction Engine.
|
|
1208
|
-
Your function is to parse conversation logs and extract persistent facts into a structured database format.
|
|
1209
|
-
|
|
1210
|
-
# CORE DIRECTIVE: "Subject-First" Extraction
|
|
1211
|
-
You must rephrase memories to focus on the *topic*, not the user. This optimizes vector retrieval.
|
|
1212
|
-
- BAD: "User likes to trade Bitcoin." (Too generic)
|
|
1213
|
-
- GOOD: "Bitcoin (BTC) is a preferred trading asset." (Topic-focused)
|
|
1214
|
-
|
|
1215
|
-
# COMPRESSION RULES (CRITICAL)
|
|
1216
|
-
1. **Aggressive Filtering**: Most user chatter is noise. If it won't be relevant in 30 days, DO NOT extract it.
|
|
1217
|
-
2. **Merge & Dedupe**: Do not create three separate memories for one topic. Combine them.
|
|
1218
|
-
- *Input:* "I like Red. I also like Blue. And Green."
|
|
1219
|
-
- *Output:* "Red, Blue, and Green are the preferred colors."
|
|
1220
|
-
3. **Conflict Resolution**: If a new fact contradicts an old one, mark 'isContradiction' as true.
|
|
1221
|
-
|
|
1222
|
-
# OUTPUT FORMAT
|
|
1223
|
-
Phase 1: [ANALYSIS]
|
|
1224
|
-
- List extracted points.
|
|
1225
|
-
- MARK items as [TRANSIENT] (Ignore) or [MERGE] (Combine).
|
|
1226
|
-
- Refine the final wording.
|
|
1227
|
-
|
|
1228
|
-
Phase 2: [MEMORIES]
|
|
1229
|
-
Format: \`MEM|TYPE|CATEGORY|CONFIDENCE|IS_CONTRADICTION|CONTENT\`
|
|
1230
|
-
|
|
1231
|
-
Types: EPISODIC, SEMANTIC, PROCEDURAL
|
|
1232
|
-
Categories: bio, health, finance, preferences, relationships, skills, work
|
|
1233
|
-
`;
|
|
1234
|
-
function buildExtractionPrompt(conversationLog) {
|
|
1235
|
-
const refDate = new Date().toISOString();
|
|
1236
|
-
return `# INPUT DATA
|
|
1237
|
-
**Reference Date:** ${refDate} (Use this to resolve relative dates like "yesterday" or "next Friday")
|
|
1238
|
-
|
|
1239
|
-
<conversation_log>
|
|
1240
|
-
${conversationLog}
|
|
1241
|
-
</conversation_log>
|
|
1242
|
-
|
|
1243
|
-
# FEW-SHOT EXAMPLES (DENSE INPUTS)
|
|
1244
|
-
|
|
1245
|
-
<example_1_finance_consolidation>
|
|
1246
|
-
Input: "Okay, market is looking bad. I'm closing my ETH long. Too risky. Also, can you check the weather in Tokyo? I might fly there. Actually, cancel all my limit orders on Solana too, I want to go all cash for the weekend."
|
|
1247
|
-
Output:
|
|
1248
|
-
[ANALYSIS]
|
|
1249
|
-
- "Market looking bad" -> Context, not memory.
|
|
1250
|
-
- "Closing ETH long" -> Actionable preference change.
|
|
1251
|
-
- "Check weather" -> [TRANSIENT] Ignore.
|
|
1252
|
-
- "Fly to Tokyo" -> [TRANSIENT] "Might" implies uncertainty. Ignore until confirmed.
|
|
1253
|
-
- "Cancel SOL orders" -> Actionable strategy.
|
|
1254
|
-
- "Go all cash" -> High-level strategy.
|
|
1255
|
-
- MERGE: Combine ETH close, SOL cancel, and Cash strategy into one record.
|
|
1256
|
-
[MEMORIES]
|
|
1257
|
-
MEM|PROCEDURAL|finance|0.95|true|Portfolio Strategy: All positions (ETH, SOL) liquidated; Cash-only stance adopted for weekend.
|
|
1258
|
-
</example_1_finance_consolidation>
|
|
1259
|
-
|
|
1260
|
-
<example_2_companion_emotional>
|
|
1261
|
-
Input: "I had a huge fight with my sister, Jenny. She's so controlling. I don't want to talk about her anymore. Let's play a game. Maybe chess? Actually no, I hate chess, it's boring. Let's do a quiz."
|
|
1262
|
-
Output:
|
|
1263
|
-
[ANALYSIS]
|
|
1264
|
-
- "Fight with sister Jenny" -> Relationship dynamic.
|
|
1265
|
-
- "She's controlling" -> Character attribute.
|
|
1266
|
-
- "Don't want to talk about her" -> Boundary/Preference.
|
|
1267
|
-
- "Play a game" -> [TRANSIENT] Immediate desire.
|
|
1268
|
-
- "Hate chess" -> Negative Preference.
|
|
1269
|
-
- "Do a quiz" -> [TRANSIENT] Immediate desire.
|
|
1270
|
-
- MERGE: Combine Jenny details. Separate Chess preference.
|
|
1271
|
-
[MEMORIES]
|
|
1272
|
-
MEM|EPISODIC|relationships|0.9|false|Jenny (sister) is characterized as controlling; currently a sensitive topic to be avoided.
|
|
1273
|
-
MEM|SEMANTIC|preferences|0.95|false|Chess is a disliked activity (described as boring).
|
|
1274
|
-
</example_2_companion_emotional>
|
|
1275
|
-
|
|
1276
|
-
<example_3_coding_stack>
|
|
1277
|
-
Input: "This node_modules folder is huge. I'm done with NPM. From now on we only use Bun for all projects. It's faster. Also, help me debug this loop. It's printing 'undefined'."
|
|
1278
|
-
Output:
|
|
1279
|
-
[ANALYSIS]
|
|
1280
|
-
- "node_modules huge" -> Rationale.
|
|
1281
|
-
- "Done with NPM" -> Deprecation.
|
|
1282
|
-
- "Use Bun" -> New Standard.
|
|
1283
|
-
- "Debug this loop" -> [TRANSIENT] Immediate task.
|
|
1284
|
-
- MERGE: Bun adoption and NPM rejection.
|
|
1285
|
-
[MEMORIES]
|
|
1286
|
-
MEM|PROCEDURAL|skills|0.95|true|Bun is the mandated package manager; NPM usage is deprecated/forbidden.
|
|
1287
|
-
</example_3_coding_stack>
|
|
1288
|
-
|
|
1289
|
-
<example_4_health_routine>
|
|
1290
|
-
Input: "I ate a burger today, felt kinda heavy. I think I'm going to start intermittent fasting. 16/8 window. Start eating at 12pm, stop at 8pm. Remind me to drink water."
|
|
1291
|
-
Output:
|
|
1292
|
-
[ANALYSIS]
|
|
1293
|
-
- "Ate a burger" -> [TRANSIENT] One-off meal.
|
|
1294
|
-
- "Felt heavy" -> [TRANSIENT] Temporary sensation.
|
|
1295
|
-
- "Start intermittent fasting" -> New Health Protocol.
|
|
1296
|
-
- "16/8 window, 12-8" -> Specific details of protocol.
|
|
1297
|
-
- "Remind me to drink water" -> [TRANSIENT] Command.
|
|
1298
|
-
- MERGE: All fasting details into one concise protocol.
|
|
1299
|
-
[MEMORIES]
|
|
1300
|
-
MEM|PROCEDURAL|health|0.9|false|Intermittent Fasting (16/8 protocol) adopted: Eating window restricted to 12pm-8pm.
|
|
1301
|
-
</example_4_health_routine>
|
|
1302
|
-
|
|
1303
|
-
<example_5_work_milestones>
|
|
1304
|
-
Input: "Meeting went well. The client, Apex Corp, agreed to the $50k budget. But they want the deadline moved to March 1st. Can you write a thank you note? Oh, and I need to update my resume."
|
|
1305
|
-
Output:
|
|
1306
|
-
[ANALYSIS]
|
|
1307
|
-
- "Meeting went well" -> [TRANSIENT] Sentiment.
|
|
1308
|
-
- "Apex Corp" -> Client Entity.
|
|
1309
|
-
- "$50k budget" -> Financial Fact.
|
|
1310
|
-
- "Deadline March 1st" -> Project Constraint.
|
|
1311
|
-
- "Write note" -> [TRANSIENT] Task.
|
|
1312
|
-
- "Update resume" -> [TRANSIENT] Generic task unless specific details given.
|
|
1313
|
-
- MERGE: Client details, budget, and deadline.
|
|
1314
|
-
[MEMORIES]
|
|
1315
|
-
MEM|SEMANTIC|work|1.0|false|Apex Corp project secured: $50k budget with March 1st deadline.
|
|
1316
|
-
</example_5_work_milestones>
|
|
1317
|
-
|
|
1318
|
-
# EXTRACTION CHECKLIST
|
|
1319
|
-
1. **Search for STATE CHANGES**: Did the user move, change jobs, break up, or alter a portfolio? These are high-value.
|
|
1320
|
-
2. **Search for HARD CONSTRAINTS**: Look for phrases like "Never do X", "Always use Y", "I hate Z".
|
|
1321
|
-
3. **COMPRESSION**:
|
|
1322
|
-
- You have received a long conversation.
|
|
1323
|
-
- **MERGE** related details into single, dense records.
|
|
1324
|
-
- **IGNORE** all small talk, greetings, and transient requests.
|
|
1325
|
-
4. **QUALITY CONTROL**: If a fact feels temporary or weak, **DO NOT** extract it. Silence is better than noise.
|
|
1326
|
-
|
|
1327
|
-
Begin the [ANALYSIS] phase now.`;
|
|
1328
|
-
}
|
|
1329
|
-
function buildContradictionPrompt(newMemoryContent, existingMemories) {
|
|
1330
|
-
return `Does this new memory contradict any existing memories?
|
|
1331
|
-
|
|
1332
|
-
New: "${newMemoryContent}"
|
|
1333
|
-
|
|
1334
|
-
Existing:
|
|
1335
|
-
${existingMemories.map((m, idx) => `${idx + 1}. "${m.content}" (confidence: ${m.confidence})`).join(`
|
|
1336
|
-
`)}
|
|
1337
|
-
|
|
1338
|
-
A contradiction means both statements cannot be true simultaneously.
|
|
1339
|
-
|
|
1340
|
-
TRUE contradictions:
|
|
1341
|
-
- "User likes blue" vs "User hates blue"
|
|
1342
|
-
- "User lives in Paris" vs "User lives in London"
|
|
1343
|
-
- "User is vegetarian" vs "User eats meat"
|
|
1344
|
-
|
|
1345
|
-
NOT contradictions:
|
|
1346
|
-
- "User likes blue" vs "User likes blue for clothes" (nuance)
|
|
1347
|
-
- "User was in Paris" vs "User moved to London" (time change)
|
|
1348
|
-
- "User likes Python" vs "User likes JavaScript" (not exclusive)
|
|
1349
|
-
|
|
1350
|
-
<response>
|
|
1351
|
-
<hasContradiction>true or false</hasContradiction>
|
|
1352
|
-
<contradictingIndex>number or null</contradictingIndex>
|
|
1353
|
-
<reasoning>Brief explanation</reasoning>
|
|
1354
|
-
</response>`;
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
// src/evaluators/consolidation.ts
|
|
1358
|
-
var consolidationBuffers = new Map;
|
|
1359
|
-
var consolidationEvaluator = {
|
|
1360
|
-
name: "CONSOLIDATION",
|
|
1361
|
-
similes: ["MEMORY_CONSOLIDATION", "EXTRACT_FACTS", "MEMORY_BUFFER"],
|
|
1362
|
-
description: "Buffers conversation messages and performs periodic consolidation to extract persistent facts " + "using LLM analysis. Distinguishes transient intents from long-term knowledge.",
|
|
1363
|
-
validate: async (runtime, message) => {
|
|
1364
|
-
if (!consolidationBuffers.has(message.roomId)) {
|
|
1365
|
-
consolidationBuffers.set(message.roomId, []);
|
|
1366
|
-
}
|
|
1367
|
-
consolidationBuffers.get(message.roomId).push(message);
|
|
1368
|
-
const memoryService = runtime.getService("memory");
|
|
1369
|
-
if (!memoryService) {
|
|
1370
|
-
return false;
|
|
1371
|
-
}
|
|
1372
|
-
const config = memoryService.getConfig();
|
|
1373
|
-
const bufferSize = consolidationBuffers.get(message.roomId).length;
|
|
1374
|
-
const shouldConsolidate = bufferSize >= config.consolidationThreshold;
|
|
1375
|
-
if (shouldConsolidate) {
|
|
1376
|
-
import_core8.logger.info({
|
|
1377
|
-
roomId: message.roomId,
|
|
1378
|
-
bufferSize,
|
|
1379
|
-
threshold: config.consolidationThreshold
|
|
1380
|
-
}, "Consolidation threshold reached");
|
|
1381
|
-
}
|
|
1382
|
-
return shouldConsolidate;
|
|
1383
|
-
},
|
|
1384
|
-
handler: async (runtime, message) => {
|
|
1385
|
-
const roomId = message.roomId;
|
|
1386
|
-
try {
|
|
1387
|
-
const memoryService = runtime.getService("memory");
|
|
1388
|
-
if (!memoryService) {
|
|
1389
|
-
import_core8.logger.warn("Memory service not available for consolidation");
|
|
678
|
+
const sortedDialogueMessages = allDialogueMessages.sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0));
|
|
679
|
+
const newDialogueMessages = sortedDialogueMessages.slice(lastOffset, lastOffset + messagesToProcess);
|
|
680
|
+
if (newDialogueMessages.length === 0) {
|
|
681
|
+
import_core2.logger.debug("No new dialogue messages retrieved after filtering");
|
|
1390
682
|
return;
|
|
1391
683
|
}
|
|
1392
|
-
const
|
|
1393
|
-
|
|
1394
|
-
return
|
|
1395
|
-
}
|
|
1396
|
-
import_core8.logger.info({ roomId, messageCount: buffer.length }, "Starting memory consolidation");
|
|
1397
|
-
const conversationLog = buffer.map((m, idx) => {
|
|
1398
|
-
const content = typeof m.content === "string" ? m.content : m.content.text || JSON.stringify(m.content);
|
|
1399
|
-
return `[${idx + 1}] ${m.entityId}: ${content}`;
|
|
684
|
+
const formattedMessages = newDialogueMessages.map((msg) => {
|
|
685
|
+
const sender = msg.entityId === runtime.agentId ? runtime.character.name : "User";
|
|
686
|
+
return `${sender}: ${msg.content.text || "[non-text message]"}`;
|
|
1400
687
|
}).join(`
|
|
1401
688
|
`);
|
|
1402
|
-
const
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
689
|
+
const state = await runtime.composeState(message);
|
|
690
|
+
let prompt;
|
|
691
|
+
let template;
|
|
692
|
+
if (existingSummary) {
|
|
693
|
+
template = updateSummarizationTemplate;
|
|
694
|
+
prompt = import_core2.composePromptFromState({
|
|
695
|
+
state: {
|
|
696
|
+
...state,
|
|
697
|
+
existingSummary: existingSummary.summary,
|
|
698
|
+
existingTopics: existingSummary.topics?.join(", ") || "None",
|
|
699
|
+
newMessages: formattedMessages
|
|
700
|
+
},
|
|
701
|
+
template
|
|
702
|
+
});
|
|
703
|
+
} else {
|
|
704
|
+
const initialMessages = sortedDialogueMessages.map((msg) => {
|
|
705
|
+
const sender = msg.entityId === runtime.agentId ? runtime.character.name : "User";
|
|
706
|
+
return `${sender}: ${msg.content.text || "[non-text message]"}`;
|
|
707
|
+
}).join(`
|
|
708
|
+
`);
|
|
709
|
+
template = initialSummarizationTemplate;
|
|
710
|
+
prompt = import_core2.composePromptFromState({
|
|
711
|
+
state: {
|
|
712
|
+
...state,
|
|
713
|
+
recentMessages: initialMessages
|
|
714
|
+
},
|
|
715
|
+
template
|
|
1410
716
|
});
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
717
|
+
}
|
|
718
|
+
const response = await runtime.useModel(import_core2.ModelType.TEXT_LARGE, {
|
|
719
|
+
prompt,
|
|
720
|
+
maxTokens: config.summaryMaxTokens || 2500
|
|
721
|
+
});
|
|
722
|
+
const summaryResult = parseSummaryXML(response);
|
|
723
|
+
import_core2.logger.info(`${existingSummary ? "Updated" : "Generated"} summary: ${summaryResult.summary.substring(0, 100)}...`);
|
|
724
|
+
const newOffset = lastOffset + newDialogueMessages.length;
|
|
725
|
+
const firstMessage = newDialogueMessages[0];
|
|
726
|
+
const lastMessage = newDialogueMessages[newDialogueMessages.length - 1];
|
|
727
|
+
const startTime = existingSummary ? existingSummary.startTime : firstMessage?.createdAt && firstMessage.createdAt > 0 ? new Date(firstMessage.createdAt) : new Date;
|
|
728
|
+
const endTime = lastMessage?.createdAt && lastMessage.createdAt > 0 ? new Date(lastMessage.createdAt) : new Date;
|
|
729
|
+
if (existingSummary) {
|
|
730
|
+
await memoryService.updateSessionSummary(existingSummary.id, roomId, {
|
|
731
|
+
summary: summaryResult.summary,
|
|
732
|
+
messageCount: existingSummary.messageCount + newDialogueMessages.length,
|
|
733
|
+
lastMessageOffset: newOffset,
|
|
734
|
+
endTime,
|
|
735
|
+
topics: summaryResult.topics,
|
|
736
|
+
metadata: {
|
|
737
|
+
keyPoints: summaryResult.keyPoints
|
|
738
|
+
}
|
|
739
|
+
});
|
|
740
|
+
import_core2.logger.info(`Updated summary for room ${roomId}: ${newDialogueMessages.length} new dialogue messages processed (offset: ${lastOffset} → ${newOffset})`);
|
|
741
|
+
} else {
|
|
742
|
+
await memoryService.storeSessionSummary({
|
|
743
|
+
agentId: runtime.agentId,
|
|
744
|
+
roomId,
|
|
745
|
+
entityId: message.entityId !== runtime.agentId ? message.entityId : undefined,
|
|
746
|
+
summary: summaryResult.summary,
|
|
747
|
+
messageCount: totalDialogueCount,
|
|
748
|
+
lastMessageOffset: totalDialogueCount,
|
|
749
|
+
startTime,
|
|
750
|
+
endTime,
|
|
751
|
+
topics: summaryResult.topics,
|
|
752
|
+
metadata: {
|
|
753
|
+
keyPoints: summaryResult.keyPoints
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
import_core2.logger.info(`Created new summary for room ${roomId}: ${totalDialogueCount} dialogue messages summarized (offset: 0 → ${totalDialogueCount})`);
|
|
1430
757
|
}
|
|
1431
758
|
} catch (error) {
|
|
1432
|
-
|
|
759
|
+
import_core2.logger.error({ error }, "Error during summarization:");
|
|
1433
760
|
}
|
|
1434
761
|
},
|
|
1435
762
|
examples: []
|
|
1436
763
|
};
|
|
1437
|
-
function parseConsolidationResult(response) {
|
|
1438
|
-
try {
|
|
1439
|
-
const responseText = typeof response === "string" ? response : JSON.stringify(response);
|
|
1440
|
-
const analysisMatch = responseText.match(/\[ANALYSIS\](.*?)(?:\[MEMORIES\]|$)/s);
|
|
1441
|
-
const reasoningTrace = analysisMatch ? analysisMatch[1].trim() : "";
|
|
1442
|
-
const memoriesMatch = responseText.match(/\[MEMORIES\](.*?)$/s);
|
|
1443
|
-
const memoriesText = memoriesMatch ? memoriesMatch[1].trim() : "";
|
|
1444
|
-
const extractedMemories = [];
|
|
1445
|
-
if (memoriesText) {
|
|
1446
|
-
const lines = memoriesText.split(`
|
|
1447
|
-
`);
|
|
1448
|
-
for (const line of lines) {
|
|
1449
|
-
const trimmedLine = line.trim();
|
|
1450
|
-
if (!trimmedLine || !trimmedLine.startsWith("MEM|")) {
|
|
1451
|
-
continue;
|
|
1452
|
-
}
|
|
1453
|
-
const parts = trimmedLine.split("|");
|
|
1454
|
-
if (parts.length < 6) {
|
|
1455
|
-
import_core8.logger.warn({ line: trimmedLine }, "Skipping malformed memory line (insufficient fields)");
|
|
1456
|
-
continue;
|
|
1457
|
-
}
|
|
1458
|
-
const type = parts[1].trim();
|
|
1459
|
-
const category = parts[2].trim();
|
|
1460
|
-
const confidenceStr = parts[3].trim();
|
|
1461
|
-
const isContradictionStr = parts[4].trim();
|
|
1462
|
-
const content = parts.slice(5).join("|").trim();
|
|
1463
|
-
if (!["EPISODIC", "SEMANTIC", "PROCEDURAL"].includes(type)) {
|
|
1464
|
-
import_core8.logger.warn({ type, line: trimmedLine }, "Invalid memory type");
|
|
1465
|
-
continue;
|
|
1466
|
-
}
|
|
1467
|
-
if (!category) {
|
|
1468
|
-
import_core8.logger.warn({ line: trimmedLine }, "Missing category");
|
|
1469
|
-
continue;
|
|
1470
|
-
}
|
|
1471
|
-
const confidence = parseFloat(confidenceStr);
|
|
1472
|
-
if (isNaN(confidence) || confidence < 0 || confidence > 1) {
|
|
1473
|
-
import_core8.logger.warn({ confidenceStr, line: trimmedLine }, "Invalid confidence value");
|
|
1474
|
-
continue;
|
|
1475
|
-
}
|
|
1476
|
-
const isContradiction = isContradictionStr.toLowerCase() === "true";
|
|
1477
|
-
if (!content) {
|
|
1478
|
-
import_core8.logger.warn({ line: trimmedLine }, "Missing content");
|
|
1479
|
-
continue;
|
|
1480
|
-
}
|
|
1481
|
-
const metadata = {
|
|
1482
|
-
category
|
|
1483
|
-
};
|
|
1484
|
-
extractedMemories.push({
|
|
1485
|
-
type,
|
|
1486
|
-
content,
|
|
1487
|
-
confidence,
|
|
1488
|
-
isContradiction,
|
|
1489
|
-
metadata
|
|
1490
|
-
});
|
|
1491
|
-
}
|
|
1492
|
-
}
|
|
1493
|
-
return {
|
|
1494
|
-
reasoningTrace,
|
|
1495
|
-
transientSummary: "",
|
|
1496
|
-
extractedMemories
|
|
1497
|
-
};
|
|
1498
|
-
} catch (error) {
|
|
1499
|
-
import_core8.logger.error({ error }, "Failed to parse consolidation response");
|
|
1500
|
-
return {
|
|
1501
|
-
reasoningTrace: "Parse error",
|
|
1502
|
-
transientSummary: "",
|
|
1503
|
-
extractedMemories: []
|
|
1504
|
-
};
|
|
1505
|
-
}
|
|
1506
|
-
}
|
|
1507
|
-
async function storeExtractedMemory(runtime, memoryService, roomId, buffer, extracted) {
|
|
1508
|
-
const entityId = buffer[0]?.entityId || runtime.agentId;
|
|
1509
|
-
const embeddingContext = generateContextualString(extracted);
|
|
1510
|
-
const config = memoryService.getConfig();
|
|
1511
|
-
const decayRate = config.defaultDecayRates[extracted.type];
|
|
1512
|
-
const memoryData = {
|
|
1513
|
-
agentId: runtime.agentId,
|
|
1514
|
-
entityId,
|
|
1515
|
-
roomId,
|
|
1516
|
-
type: extracted.type,
|
|
1517
|
-
content: extracted.content,
|
|
1518
|
-
embeddingContext,
|
|
1519
|
-
confidence: extracted.confidence,
|
|
1520
|
-
decayRate,
|
|
1521
|
-
decayFunction: "EXPONENTIAL" /* EXPONENTIAL */,
|
|
1522
|
-
source: {
|
|
1523
|
-
sessionId: roomId,
|
|
1524
|
-
messageId: extracted.sourceMessageId,
|
|
1525
|
-
textSnippet: extracted.content.substring(0, 200)
|
|
1526
|
-
},
|
|
1527
|
-
metadata: extracted.metadata || {}
|
|
1528
|
-
};
|
|
1529
|
-
if (config.enableContradictionDetection && extracted.isContradiction) {
|
|
1530
|
-
await memoryService.handleContradiction(entityId, memoryData);
|
|
1531
|
-
} else {
|
|
1532
|
-
await memoryService.storeLongTermMemory(memoryData);
|
|
1533
|
-
}
|
|
1534
|
-
}
|
|
1535
|
-
function generateContextualString(extracted) {
|
|
1536
|
-
const typeLabel = {
|
|
1537
|
-
["EPISODIC" /* EPISODIC */]: "Event",
|
|
1538
|
-
["SEMANTIC" /* SEMANTIC */]: "Fact",
|
|
1539
|
-
["PROCEDURAL" /* PROCEDURAL */]: "Skill"
|
|
1540
|
-
}[extracted.type];
|
|
1541
|
-
const category = extracted.metadata?.category || "general";
|
|
1542
|
-
return `[${typeLabel} about ${category}]: ${extracted.content}`;
|
|
1543
|
-
}
|
|
1544
|
-
|
|
1545
|
-
// src/evaluators/summarization.ts
|
|
1546
|
-
var import_core9 = require("@elizaos/core");
|
|
1547
|
-
|
|
1548
|
-
// src/prompts/summarization.ts
|
|
1549
|
-
var SUMMARIZATION_SYSTEM_PROMPT = `You are "Chronos", a master summarizer.
|
|
1550
|
-
Your function is to condense conversation logs into concise, subject-first narrative summaries.
|
|
1551
|
-
|
|
1552
|
-
# CORE DIRECTIVE: "Subject-First" Summarization
|
|
1553
|
-
You must rephrase the narrative to focus on the *topic*, not the user. This optimizes vector retrieval.
|
|
1554
|
-
- BAD: "User asked about Python." (Too generic)
|
|
1555
|
-
- GOOD: "Python programming inquiries were addressed." (Topic-focused)
|
|
1556
|
-
|
|
1557
|
-
# COMPRESSION RULES
|
|
1558
|
-
1. **Be Concise**: Target 2-4 sentences. Maximum 100 words.
|
|
1559
|
-
2. **Be Factual**: No interpretation, no speculation. Only what actually happened.
|
|
1560
|
-
3. **Be Narrative**: Write as a story, not a bullet list.
|
|
1561
|
-
4. **Preserve Key Facts**: If the user revealed important information (preferences, identity, needs), include it.
|
|
1562
|
-
5. **Exclude Trivia**: Skip greetings, acknowledgments, and filler conversation.
|
|
1563
|
-
|
|
1564
|
-
# OUTPUT FORMAT
|
|
1565
|
-
Phase 1: [ANALYSIS]
|
|
1566
|
-
- Identify key topics.
|
|
1567
|
-
- Draft the summary.
|
|
1568
|
-
- Refine wording to be subject-first.
|
|
1569
|
-
|
|
1570
|
-
Phase 2: [RESULT]
|
|
1571
|
-
Format: \`SUMM|TAGS|CONTENT\`
|
|
1572
|
-
- TAGS: Comma-separated list of key topics (lowercase)
|
|
1573
|
-
- CONTENT: The narrative summary text (must be a single line, no newlines)
|
|
1574
|
-
`;
|
|
1575
|
-
function buildLevel1SummaryPrompt(formattedMessages, previousSummary) {
|
|
1576
|
-
const previousContext = previousSummary ? `
|
|
1577
|
-
# PREVIOUS SUMMARY
|
|
1578
|
-
<previous_summary>
|
|
1579
|
-
${previousSummary}
|
|
1580
|
-
</previous_summary>
|
|
1581
|
-
|
|
1582
|
-
**Note**: The above is what was discussed before. Ensure your new summary:
|
|
1583
|
-
- Does not duplicate information already captured in the previous summary
|
|
1584
|
-
- Focuses only on the NEW conversation below
|
|
1585
|
-
- Maintains continuity (e.g., "Conversation continued with...")
|
|
1586
|
-
` : "";
|
|
1587
|
-
return `${previousContext}
|
|
1588
|
-
# INPUT DATA
|
|
1589
|
-
<messages>
|
|
1590
|
-
${formattedMessages}
|
|
1591
|
-
</messages>
|
|
1592
|
-
|
|
1593
|
-
# FEW-SHOT EXAMPLES
|
|
1594
|
-
|
|
1595
|
-
<example_1_task_oriented>
|
|
1596
|
-
Input:
|
|
1597
|
-
[Message 1] User: I need help deploying my React app to Vercel
|
|
1598
|
-
[Message 2] Agent: Sure! First, make sure you have the Vercel CLI installed...
|
|
1599
|
-
[Message 3] User: Done. What's next?
|
|
1600
|
-
[Message 4] Agent: Run 'vercel' in your project directory...
|
|
1601
|
-
[Message 5] User: It worked! Thanks!
|
|
1602
764
|
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
- Topic: React app deployment to Vercel.
|
|
1606
|
-
- Action: CLI installation and deployment.
|
|
1607
|
-
- Outcome: Success.
|
|
1608
|
-
- Draft: User requested help... -> React app deployment...
|
|
1609
|
-
[RESULT]
|
|
1610
|
-
SUMM|deployment,react,vercel,cli|React app deployment to Vercel was successfully completed following CLI installation and configuration guidance.
|
|
1611
|
-
</example_1_task_oriented>
|
|
1612
|
-
|
|
1613
|
-
<example_2_identity_revelation>
|
|
1614
|
-
Input:
|
|
1615
|
-
[Message 1] User: I'm working on a side project in my spare time
|
|
1616
|
-
[Message 2] Agent: That's great! What kind of project?
|
|
1617
|
-
[Message 3] User: A machine learning app for analyzing stock data. I'm a data scientist by profession.
|
|
1618
|
-
[Message 4] Agent: Interesting! Are you using Python?
|
|
1619
|
-
[Message 5] User: Yeah, mostly PyTorch and pandas.
|
|
1620
|
-
|
|
1621
|
-
Output:
|
|
1622
|
-
[ANALYSIS]
|
|
1623
|
-
- Identity: Data scientist.
|
|
1624
|
-
- Project: Stock data analysis (ML).
|
|
1625
|
-
- Tech Stack: Python, PyTorch, pandas.
|
|
1626
|
-
- Draft: User discussed... -> Stock data analysis project...
|
|
1627
|
-
[RESULT]
|
|
1628
|
-
SUMM|machine learning,data science,python,stocks|Stock data analysis project (Machine Learning) utilizing Python (PyTorch, pandas) is in development by a data scientist.
|
|
1629
|
-
</example_2_identity_revelation>
|
|
1630
|
-
|
|
1631
|
-
<example_3_chitchat>
|
|
1632
|
-
Input:
|
|
1633
|
-
[Message 1] User: Hey, how's it going?
|
|
1634
|
-
[Message 2] Agent: I'm doing well, thanks for asking! How can I help you today?
|
|
1635
|
-
[Message 3] User: Just saying hi
|
|
1636
|
-
[Message 4] Agent: Nice to hear from you!
|
|
1637
|
-
|
|
1638
|
-
Output:
|
|
1639
|
-
[ANALYSIS]
|
|
1640
|
-
- Content: Greetings only.
|
|
1641
|
-
- Substance: None.
|
|
1642
|
-
[RESULT]
|
|
1643
|
-
SUMM|greeting,casual|Casual greeting exchanged; no substantive topics discussed.
|
|
1644
|
-
</example_3_chitchat>
|
|
1645
|
-
|
|
1646
|
-
Begin the [ANALYSIS] phase now.`;
|
|
1647
|
-
}
|
|
1648
|
-
var HIGHER_LEVEL_SUMMARIZATION_SYSTEM_PROMPT = `You are "Chronos", a Meta-Summarization Agent.
|
|
1649
|
-
Your task is to compress multiple conversation summaries into a single, higher-level summary.
|
|
1650
|
-
|
|
1651
|
-
# MISSION
|
|
1652
|
-
Transform a list of conversation summaries into one concise meta-summary that captures:
|
|
1653
|
-
1. **Overarching themes** across the summaries
|
|
1654
|
-
2. **Key events or milestones** (e.g., "User onboarded", "Project completed")
|
|
1655
|
-
3. **Evolving context** (e.g., "User's preferences shifted from X to Y")
|
|
1656
|
-
|
|
1657
|
-
# RULES
|
|
1658
|
-
- **Subject-First**: Focus on the topic, not the user.
|
|
1659
|
-
- **Abstract Higher**: Don't repeat specifics from each summary. Find the pattern.
|
|
1660
|
-
- **Chronological Flow**: Maintain temporal order if it matters.
|
|
1661
|
-
- **Preserve Critical Facts**: If summaries mention important identity or preferences, keep them.
|
|
1662
|
-
|
|
1663
|
-
# OUTPUT FORMAT
|
|
1664
|
-
Phase 1: [ANALYSIS]
|
|
1665
|
-
- Identify themes and milestones.
|
|
1666
|
-
- Combine related points.
|
|
1667
|
-
- Refine to subject-first.
|
|
1668
|
-
|
|
1669
|
-
Phase 2: [RESULT]
|
|
1670
|
-
Format: \`SUMM|TAGS|CONTENT\`
|
|
1671
|
-
- TAGS: Comma-separated list of key topics (lowercase)
|
|
1672
|
-
- CONTENT: The meta-summary text (must be a single line, no newlines)
|
|
1673
|
-
`;
|
|
1674
|
-
function buildHigherLevelSummaryPrompt(formattedSummaries) {
|
|
1675
|
-
return `# INPUT DATA
|
|
1676
|
-
<summaries>
|
|
1677
|
-
${formattedSummaries}
|
|
1678
|
-
</summaries>
|
|
1679
|
-
|
|
1680
|
-
# FEW-SHOT EXAMPLES
|
|
1681
|
-
|
|
1682
|
-
<example_1_meta_summary>
|
|
1683
|
-
Input:
|
|
1684
|
-
<summary1>Stock data analysis project (Machine Learning) utilizing Python (PyTorch, pandas) is in development by a data scientist.</summary1>
|
|
1685
|
-
<summary2>Flask and Vercel were suggested for ML model deployment to a web app.</summary2>
|
|
1686
|
-
<summary3>CORS configuration issues were resolved, leading to successful deployment.</summary3>
|
|
1687
|
-
|
|
1688
|
-
Output:
|
|
1689
|
-
[ANALYSIS]
|
|
1690
|
-
- Theme: ML App Development & Deployment.
|
|
1691
|
-
- Flow: Development -> Stack Choice -> Deployment -> Troubleshooting -> Success.
|
|
1692
|
-
- Draft: User, a data scientist... -> Machine learning stock analysis app...
|
|
1693
|
-
[RESULT]
|
|
1694
|
-
SUMM|data science,machine learning,python,deployment,flask,vercel|Machine learning stock analysis app (Python/PyTorch) was developed and successfully deployed to Vercel using Flask after resolving CORS configuration issues.
|
|
1695
|
-
</example_1_meta_summary>
|
|
765
|
+
// src/evaluators/long-term-extraction.ts
|
|
766
|
+
var import_core3 = require("@elizaos/core");
|
|
1696
767
|
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
768
|
+
// src/types/index.ts
|
|
769
|
+
var LongTermMemoryCategory;
|
|
770
|
+
((LongTermMemoryCategory2) => {
|
|
771
|
+
LongTermMemoryCategory2["EPISODIC"] = "episodic";
|
|
772
|
+
LongTermMemoryCategory2["SEMANTIC"] = "semantic";
|
|
773
|
+
LongTermMemoryCategory2["PROCEDURAL"] = "procedural";
|
|
774
|
+
})(LongTermMemoryCategory ||= {});
|
|
775
|
+
|
|
776
|
+
// src/evaluators/long-term-extraction.ts
|
|
777
|
+
var extractionTemplate = `# Task: Extract Long-Term Memory (Strict Criteria)
|
|
778
|
+
|
|
779
|
+
You are analyzing a conversation to extract ONLY the most critical, persistent information about the user using cognitive science memory categories.
|
|
780
|
+
|
|
781
|
+
# Recent Messages
|
|
782
|
+
{{recentMessages}}
|
|
783
|
+
|
|
784
|
+
# Current Long-Term Memories
|
|
785
|
+
{{existingMemories}}
|
|
786
|
+
|
|
787
|
+
# Memory Categories (Based on Cognitive Science)
|
|
788
|
+
|
|
789
|
+
## 1. EPISODIC Memory
|
|
790
|
+
Personal experiences and specific events with temporal/spatial context.
|
|
791
|
+
**Examples:**
|
|
792
|
+
- "User completed migration project from MongoDB to PostgreSQL in Q2 2024"
|
|
793
|
+
- "User encountered authentication bug in production on March 15th"
|
|
794
|
+
- "User had a negative experience with Docker networking in previous job"
|
|
795
|
+
|
|
796
|
+
**Requirements:**
|
|
797
|
+
- Must include WHO did WHAT, WHEN/WHERE
|
|
798
|
+
- Must be a specific, concrete event (not a pattern)
|
|
799
|
+
- Must have significant impact or relevance to future work
|
|
800
|
+
|
|
801
|
+
## 2. SEMANTIC Memory
|
|
802
|
+
General facts, concepts, knowledge, and established truths about the user.
|
|
803
|
+
**Examples:**
|
|
804
|
+
- "User is a senior backend engineer with 8 years experience"
|
|
805
|
+
- "User specializes in distributed systems and microservices architecture"
|
|
806
|
+
- "User's primary programming language is TypeScript"
|
|
807
|
+
- "User works at Acme Corp as technical lead"
|
|
808
|
+
|
|
809
|
+
**Requirements:**
|
|
810
|
+
- Must be factual, timeless information
|
|
811
|
+
- Must be explicitly stated or demonstrated conclusively
|
|
812
|
+
- No speculation or inference from single instances
|
|
813
|
+
- Core identity, expertise, or knowledge only
|
|
814
|
+
|
|
815
|
+
## 3. PROCEDURAL Memory
|
|
816
|
+
Skills, workflows, methodologies, and how-to knowledge.
|
|
817
|
+
**Examples:**
|
|
818
|
+
- "User follows strict TDD workflow: write tests first, then implementation"
|
|
819
|
+
- "User prefers git rebase over merge to maintain linear history"
|
|
820
|
+
- "User's debugging process: check logs → reproduce locally → binary search"
|
|
821
|
+
- "User always writes JSDoc comments before implementing functions"
|
|
822
|
+
|
|
823
|
+
**Requirements:**
|
|
824
|
+
- Must describe HOW user does something
|
|
825
|
+
- Must be a repeated, consistent pattern (seen 3+ times or explicitly stated as standard practice)
|
|
826
|
+
- Must be a workflow, methodology, or skill application
|
|
827
|
+
- Not one-off preferences
|
|
828
|
+
|
|
829
|
+
# ULTRA-STRICT EXTRACTION CRITERIA
|
|
830
|
+
|
|
831
|
+
## ✅ DO EXTRACT (Only These):
|
|
832
|
+
|
|
833
|
+
**EPISODIC:**
|
|
834
|
+
- Significant completed projects or milestones
|
|
835
|
+
- Important bugs, incidents, or problems encountered
|
|
836
|
+
- Major decisions made with lasting impact
|
|
837
|
+
- Formative experiences that shape future work
|
|
838
|
+
|
|
839
|
+
**SEMANTIC:**
|
|
840
|
+
- Professional identity (role, title, company)
|
|
841
|
+
- Core expertise and specializations (stated explicitly or demonstrated conclusively)
|
|
842
|
+
- Primary languages, frameworks, or tools (not exploratory use)
|
|
843
|
+
- Established facts about their work context
|
|
844
|
+
|
|
845
|
+
**PROCEDURAL:**
|
|
846
|
+
- Consistent workflows demonstrated 3+ times or explicitly stated
|
|
847
|
+
- Standard practices user always follows
|
|
848
|
+
- Methodology preferences with clear rationale
|
|
849
|
+
- Debugging, testing, or development processes
|
|
850
|
+
|
|
851
|
+
## ❌ NEVER EXTRACT:
|
|
852
|
+
|
|
853
|
+
- **One-time requests or tasks** (e.g., "can you generate an image", "help me debug this")
|
|
854
|
+
- **Casual conversations** without lasting significance
|
|
855
|
+
- **Exploratory questions** (e.g., "how does X work?")
|
|
856
|
+
- **Temporary context** (current bug, today's task)
|
|
857
|
+
- **Preferences from single occurrence** (e.g., user asked for code once)
|
|
858
|
+
- **Social pleasantries** (thank you, greetings)
|
|
859
|
+
- **Testing or experimentation** (trying out a feature)
|
|
860
|
+
- **Common patterns everyone has** (likes clear explanations)
|
|
861
|
+
- **Situational information** (working on feature X today)
|
|
862
|
+
- **Opinions without persistence** (single complaint, isolated praise)
|
|
863
|
+
- **General knowledge** (not specific to user)
|
|
864
|
+
|
|
865
|
+
# Quality Gates (ALL Must Pass)
|
|
866
|
+
|
|
867
|
+
1. **Significance Test**: Will this matter in 3+ months?
|
|
868
|
+
2. **Specificity Test**: Is this concrete and actionable?
|
|
869
|
+
3. **Evidence Test**: Is there strong evidence (3+ instances OR explicit self-identification)?
|
|
870
|
+
4. **Uniqueness Test**: Is this specific to THIS user (not generic)?
|
|
871
|
+
5. **Confidence Test**: Confidence must be >= 0.85 (be VERY conservative)
|
|
872
|
+
6. **Non-Redundancy Test**: Does this add NEW information not in existing memories?
|
|
873
|
+
|
|
874
|
+
# Confidence Scoring (Be Conservative)
|
|
875
|
+
|
|
876
|
+
- **0.95-1.0**: User explicitly stated as core identity/practice AND demonstrated multiple times
|
|
877
|
+
- **0.85-0.94**: User explicitly stated OR consistently demonstrated 5+ times
|
|
878
|
+
- **0.75-0.84**: Strong pattern (3-4 instances) with supporting context
|
|
879
|
+
- **Below 0.75**: DO NOT EXTRACT (insufficient evidence)
|
|
880
|
+
|
|
881
|
+
# Critical Instructions
|
|
882
|
+
|
|
883
|
+
1. **Default to NOT extracting** - When in doubt, skip it
|
|
884
|
+
2. **Require overwhelming evidence** - One or two mentions is NOT enough
|
|
885
|
+
3. **Focus on what's PERSISTENT** - Not what's temporary or situational
|
|
886
|
+
4. **Verify against existing memories** - Don't duplicate or contradict
|
|
887
|
+
5. **Maximum 2-3 extractions per run** - Quality over quantity
|
|
888
|
+
|
|
889
|
+
**If there are no qualifying facts (which is common), respond with <memories></memories>**
|
|
890
|
+
|
|
891
|
+
# Response Format
|
|
892
|
+
|
|
893
|
+
<memories>
|
|
894
|
+
<memory>
|
|
895
|
+
<category>semantic</category>
|
|
896
|
+
<content>User is a senior TypeScript developer with 8 years of backend experience</content>
|
|
897
|
+
<confidence>0.95</confidence>
|
|
898
|
+
</memory>
|
|
899
|
+
<memory>
|
|
900
|
+
<category>procedural</category>
|
|
901
|
+
<content>User follows TDD workflow: writes tests before implementation, runs tests after each change</content>
|
|
902
|
+
<confidence>0.88</confidence>
|
|
903
|
+
</memory>
|
|
904
|
+
<memory>
|
|
905
|
+
<category>episodic</category>
|
|
906
|
+
<content>User led database migration from MongoDB to PostgreSQL for payment system in Q2 2024</content>
|
|
907
|
+
<confidence>0.92</confidence>
|
|
908
|
+
</memory>
|
|
909
|
+
</memories>`;
|
|
910
|
+
function parseMemoryExtractionXML(xml) {
|
|
911
|
+
const memoryMatches = xml.matchAll(/<memory>[\s\S]*?<category>(.*?)<\/category>[\s\S]*?<content>(.*?)<\/content>[\s\S]*?<confidence>(.*?)<\/confidence>[\s\S]*?<\/memory>/g);
|
|
912
|
+
const extractions = [];
|
|
913
|
+
for (const match of memoryMatches) {
|
|
914
|
+
const category = match[1].trim();
|
|
915
|
+
const content = match[2].trim();
|
|
916
|
+
const confidence = parseFloat(match[3].trim());
|
|
917
|
+
if (!Object.values(LongTermMemoryCategory).includes(category)) {
|
|
918
|
+
import_core3.logger.warn(`Invalid memory category: ${category}`);
|
|
919
|
+
continue;
|
|
920
|
+
}
|
|
921
|
+
if (content && !isNaN(confidence)) {
|
|
922
|
+
extractions.push({ category, content, confidence });
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
return extractions;
|
|
1701
926
|
}
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
similes: ["HIERARCHICAL_SUMMARIZATION", "EPISODIC_COMPRESSION", "CONVERSATION_SUMMARY"],
|
|
1708
|
-
description: "Hierarchical conversation summarization that compresses message history into multi-level narrative summaries " + "for token-efficient long-term episodic memory.",
|
|
927
|
+
var longTermExtractionEvaluator = {
|
|
928
|
+
name: "LONG_TERM_MEMORY_EXTRACTION",
|
|
929
|
+
description: "Extracts long-term facts about users from conversations",
|
|
930
|
+
similes: ["MEMORY_EXTRACTION", "FACT_LEARNING", "USER_PROFILING"],
|
|
931
|
+
alwaysRun: true,
|
|
1709
932
|
validate: async (runtime, message) => {
|
|
933
|
+
if (message.entityId === runtime.agentId) {
|
|
934
|
+
return false;
|
|
935
|
+
}
|
|
936
|
+
if (!message.content?.text) {
|
|
937
|
+
return false;
|
|
938
|
+
}
|
|
1710
939
|
const memoryService = runtime.getService("memory");
|
|
1711
940
|
if (!memoryService) {
|
|
1712
941
|
return false;
|
|
1713
942
|
}
|
|
1714
943
|
const config = memoryService.getConfig();
|
|
1715
|
-
if (!config.
|
|
1716
|
-
|
|
944
|
+
if (!config.longTermExtractionEnabled) {
|
|
945
|
+
import_core3.logger.debug("Long-term memory extraction is disabled");
|
|
1717
946
|
return false;
|
|
1718
947
|
}
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
const currentCount = summarizationCounters.get(message.roomId) + 1;
|
|
1723
|
-
summarizationCounters.set(message.roomId, currentCount);
|
|
1724
|
-
const threshold = config.summarization.messagesPerSummary;
|
|
1725
|
-
import_core9.logger.debug({
|
|
1726
|
-
currentCount,
|
|
1727
|
-
threshold,
|
|
1728
|
-
messageId: message.id,
|
|
1729
|
-
entityId: message.entityId,
|
|
1730
|
-
roomId: message.roomId
|
|
1731
|
-
}, "Message counted for summarization (user + agent messages)");
|
|
1732
|
-
const shouldSummarize = currentCount >= threshold;
|
|
1733
|
-
if (shouldSummarize) {
|
|
1734
|
-
import_core9.logger.info({
|
|
1735
|
-
roomId: message.roomId,
|
|
1736
|
-
messageCount: currentCount,
|
|
1737
|
-
threshold
|
|
1738
|
-
}, "Summarization threshold reached (Level 1) - triggering summarization");
|
|
1739
|
-
}
|
|
1740
|
-
return shouldSummarize;
|
|
948
|
+
const currentMessageCount = await runtime.countMemories(message.roomId, false, "messages");
|
|
949
|
+
const shouldRun = await memoryService.shouldRunExtraction(message.entityId, message.roomId, currentMessageCount);
|
|
950
|
+
return shouldRun;
|
|
1741
951
|
},
|
|
1742
952
|
handler: async (runtime, message) => {
|
|
1743
953
|
const memoryService = runtime.getService("memory");
|
|
1744
954
|
if (!memoryService) {
|
|
1745
|
-
|
|
955
|
+
import_core3.logger.error("MemoryService not found");
|
|
1746
956
|
return;
|
|
1747
957
|
}
|
|
1748
958
|
const config = memoryService.getConfig();
|
|
1749
|
-
|
|
1750
|
-
return;
|
|
1751
|
-
}
|
|
1752
|
-
const messageCount = summarizationCounters.get(message.roomId) || 0;
|
|
1753
|
-
if (messageCount === 0) {
|
|
1754
|
-
return;
|
|
1755
|
-
}
|
|
1756
|
-
import_core9.logger.info({
|
|
1757
|
-
roomId: message.roomId,
|
|
1758
|
-
messageCount
|
|
1759
|
-
}, "Starting Level 1 summarization - pulling messages from database");
|
|
959
|
+
const { entityId, roomId } = message;
|
|
1760
960
|
try {
|
|
1761
|
-
|
|
1762
|
-
const
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
961
|
+
import_core3.logger.info(`Extracting long-term memories for entity ${entityId}`);
|
|
962
|
+
const recentMessages = await runtime.getMemories({
|
|
963
|
+
tableName: "messages",
|
|
964
|
+
roomId,
|
|
965
|
+
count: 20,
|
|
966
|
+
unique: false
|
|
967
|
+
});
|
|
968
|
+
const formattedMessages = recentMessages.sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0)).map((msg) => {
|
|
969
|
+
const sender = msg.entityId === runtime.agentId ? runtime.character.name : "User";
|
|
970
|
+
return `${sender}: ${msg.content.text || "[non-text message]"}`;
|
|
971
|
+
}).join(`
|
|
972
|
+
`);
|
|
973
|
+
const existingMemories = await memoryService.getLongTermMemories(entityId, undefined, 30);
|
|
974
|
+
const formattedExisting = existingMemories.length > 0 ? existingMemories.map((m) => `[${m.category}] ${m.content} (confidence: ${m.confidence})`).join(`
|
|
975
|
+
`) : "None yet";
|
|
976
|
+
const state = await runtime.composeState(message);
|
|
977
|
+
const prompt = import_core3.composePromptFromState({
|
|
978
|
+
state: {
|
|
979
|
+
...state,
|
|
980
|
+
recentMessages: formattedMessages,
|
|
981
|
+
existingMemories: formattedExisting
|
|
982
|
+
},
|
|
983
|
+
template: extractionTemplate
|
|
984
|
+
});
|
|
985
|
+
const response = await runtime.useModel(import_core3.ModelType.TEXT_LARGE, {
|
|
986
|
+
prompt
|
|
987
|
+
});
|
|
988
|
+
const extractions = parseMemoryExtractionXML(response);
|
|
989
|
+
import_core3.logger.info(`Extracted ${extractions.length} long-term memories`);
|
|
990
|
+
for (const extraction of extractions) {
|
|
991
|
+
if (extraction.confidence >= Math.max(config.longTermConfidenceThreshold, 0.85)) {
|
|
992
|
+
await memoryService.storeLongTermMemory({
|
|
993
|
+
agentId: runtime.agentId,
|
|
994
|
+
entityId,
|
|
995
|
+
category: extraction.category,
|
|
996
|
+
content: extraction.content,
|
|
997
|
+
confidence: extraction.confidence,
|
|
998
|
+
source: "conversation",
|
|
999
|
+
metadata: {
|
|
1000
|
+
roomId,
|
|
1001
|
+
extractedAt: new Date().toISOString()
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
import_core3.logger.info(`Stored long-term memory: [${extraction.category}] ${extraction.content.substring(0, 50)}...`);
|
|
1005
|
+
} else {
|
|
1006
|
+
import_core3.logger.debug(`Skipped low-confidence memory: ${extraction.content} (confidence: ${extraction.confidence}, threshold: ${Math.max(config.longTermConfidenceThreshold, 0.85)})`);
|
|
1007
|
+
}
|
|
1778
1008
|
}
|
|
1009
|
+
const currentMessageCount = await runtime.countMemories(roomId, false, "messages");
|
|
1010
|
+
await memoryService.setLastExtractionCheckpoint(entityId, roomId, currentMessageCount);
|
|
1011
|
+
import_core3.logger.debug(`Updated extraction checkpoint to ${currentMessageCount} for entity ${entityId} in room ${roomId}`);
|
|
1779
1012
|
} catch (error) {
|
|
1780
|
-
|
|
1781
|
-
error,
|
|
1782
|
-
roomId: message.roomId
|
|
1783
|
-
}, "Failed to create summary");
|
|
1013
|
+
import_core3.logger.error({ error }, "Error during long-term memory extraction:");
|
|
1784
1014
|
}
|
|
1785
1015
|
},
|
|
1786
1016
|
examples: []
|
|
1787
1017
|
};
|
|
1788
|
-
async function fetchMessagesSinceLastSummary(runtime, roomId, lastSummaryEndTime) {
|
|
1789
|
-
const allMessages = await runtime.getMemories({
|
|
1790
|
-
tableName: "messages",
|
|
1791
|
-
roomId,
|
|
1792
|
-
count: 1000,
|
|
1793
|
-
unique: false
|
|
1794
|
-
});
|
|
1795
|
-
const dialogueMessages = allMessages.filter((msg) => !(msg.content?.type === "action_result" && msg.metadata?.type === "action_result"));
|
|
1796
|
-
let messages = dialogueMessages;
|
|
1797
|
-
if (lastSummaryEndTime) {
|
|
1798
|
-
messages = dialogueMessages.filter((msg) => {
|
|
1799
|
-
if (!msg.createdAt)
|
|
1800
|
-
return false;
|
|
1801
|
-
const msgTime = new Date(msg.createdAt);
|
|
1802
|
-
return msgTime > lastSummaryEndTime;
|
|
1803
|
-
});
|
|
1804
|
-
}
|
|
1805
|
-
messages.sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0));
|
|
1806
|
-
import_core9.logger.debug({
|
|
1807
|
-
roomId,
|
|
1808
|
-
totalMessages: allMessages.length,
|
|
1809
|
-
dialogueMessages: dialogueMessages.length,
|
|
1810
|
-
filteredMessages: messages.length,
|
|
1811
|
-
lastSummaryEndTime: lastSummaryEndTime?.toISOString()
|
|
1812
|
-
}, "Fetched messages for summarization");
|
|
1813
|
-
return messages;
|
|
1814
|
-
}
|
|
1815
|
-
function parseSummarizationResult(response) {
|
|
1816
|
-
try {
|
|
1817
|
-
const responseText = typeof response === "string" ? response : JSON.stringify(response);
|
|
1818
|
-
const analysisMatch = responseText.match(/\[ANALYSIS\](.*?)(?:\[RESULT\]|$)/s);
|
|
1819
|
-
const reasoningTrace = analysisMatch ? analysisMatch[1].trim() : "";
|
|
1820
|
-
const resultMatch = responseText.match(/\[RESULT\](.*?)$/s);
|
|
1821
|
-
const resultText = resultMatch ? resultMatch[1].trim() : "";
|
|
1822
|
-
let summary = "";
|
|
1823
|
-
let keyTopics = "";
|
|
1824
|
-
if (resultText) {
|
|
1825
|
-
const lines = resultText.split(`
|
|
1826
|
-
`);
|
|
1827
|
-
for (const line of lines) {
|
|
1828
|
-
const trimmed = line.trim();
|
|
1829
|
-
if (trimmed.startsWith("SUMM|")) {
|
|
1830
|
-
const parts = trimmed.split("|");
|
|
1831
|
-
if (parts.length >= 3) {
|
|
1832
|
-
keyTopics = parts[1].trim();
|
|
1833
|
-
summary = parts.slice(2).join("|").trim();
|
|
1834
|
-
break;
|
|
1835
|
-
}
|
|
1836
|
-
}
|
|
1837
|
-
}
|
|
1838
|
-
}
|
|
1839
|
-
return { summary, keyTopics, reasoningTrace };
|
|
1840
|
-
} catch (error) {
|
|
1841
|
-
import_core9.logger.error({ error }, "Failed to parse summarization response");
|
|
1842
|
-
return { summary: "", keyTopics: "", reasoningTrace: "" };
|
|
1843
|
-
}
|
|
1844
|
-
}
|
|
1845
|
-
async function createLevel1Summary(runtime, memoryService, messages, roomId, previousSummary) {
|
|
1846
|
-
const formattedMessages = messages.map((m, i) => {
|
|
1847
|
-
const text3 = typeof m.content === "string" ? m.content : m.content.text || "";
|
|
1848
|
-
const author = m.entityId === runtime.agentId ? "Agent" : "User";
|
|
1849
|
-
const timestamp3 = m.createdAt ? new Date(m.createdAt).toISOString() : "Unknown time";
|
|
1850
|
-
return `[${timestamp3}] ${author}: ${text3}`;
|
|
1851
|
-
}).join(`
|
|
1852
|
-
`);
|
|
1853
|
-
let prompt;
|
|
1854
|
-
if (previousSummary) {
|
|
1855
|
-
prompt = buildLevel1SummaryPrompt(formattedMessages, previousSummary.content);
|
|
1856
|
-
} else {
|
|
1857
|
-
prompt = buildLevel1SummaryPrompt(formattedMessages);
|
|
1858
|
-
}
|
|
1859
|
-
const originalSystemPrompt = runtime.character.system;
|
|
1860
|
-
try {
|
|
1861
|
-
runtime.character.system = SUMMARIZATION_SYSTEM_PROMPT;
|
|
1862
|
-
import_core9.logger.debug("Calling LLM for Level 1 summarization with timestamped conversation log");
|
|
1863
|
-
const response = await runtime.useModel(import_core9.ModelType.TEXT_LARGE, {
|
|
1864
|
-
prompt,
|
|
1865
|
-
temperature: 0.3
|
|
1866
|
-
});
|
|
1867
|
-
runtime.character.system = originalSystemPrompt;
|
|
1868
|
-
const { summary, keyTopics, reasoningTrace } = parseSummarizationResult(response);
|
|
1869
|
-
if (!summary) {
|
|
1870
|
-
import_core9.logger.warn({ reasoningTrace }, "No summary extracted from LLM response");
|
|
1871
|
-
return null;
|
|
1872
|
-
}
|
|
1873
|
-
const tokenCount = estimateTokensInSummary(summary);
|
|
1874
|
-
const startTime = messages[0].createdAt ? new Date(messages[0].createdAt) : new Date;
|
|
1875
|
-
const endTime = messages[messages.length - 1].createdAt ? new Date(messages[messages.length - 1].createdAt) : new Date;
|
|
1876
|
-
const summaryData = {
|
|
1877
|
-
agentId: runtime.agentId,
|
|
1878
|
-
entityId: messages[0].entityId,
|
|
1879
|
-
roomId,
|
|
1880
|
-
level: 1,
|
|
1881
|
-
parentSummaryId: undefined,
|
|
1882
|
-
content: summary,
|
|
1883
|
-
tokenCount,
|
|
1884
|
-
startTime,
|
|
1885
|
-
endTime,
|
|
1886
|
-
sourceCount: messages.length,
|
|
1887
|
-
sourceIds: messages.map((m) => m.id),
|
|
1888
|
-
metadata: {
|
|
1889
|
-
keyTopics,
|
|
1890
|
-
hasPreviousSummary: !!previousSummary
|
|
1891
|
-
}
|
|
1892
|
-
};
|
|
1893
|
-
return await memoryService.storeSummary(summaryData);
|
|
1894
|
-
} catch (error) {
|
|
1895
|
-
import_core9.logger.error({ error }, "Failed to call LLM for Level 1 summarization");
|
|
1896
|
-
runtime.character.system = originalSystemPrompt;
|
|
1897
|
-
return null;
|
|
1898
|
-
}
|
|
1899
|
-
}
|
|
1900
|
-
async function checkAndTriggerHigherLevelSummarization(runtime, memoryService, roomId, currentLevel) {
|
|
1901
|
-
const config = memoryService.getConfig();
|
|
1902
|
-
if (!config.summarization) {
|
|
1903
|
-
return;
|
|
1904
|
-
}
|
|
1905
|
-
if (currentLevel >= config.summarization.maxDepth) {
|
|
1906
|
-
import_core9.logger.debug({ currentLevel, maxDepth: config.summarization.maxDepth }, "Max depth reached");
|
|
1907
|
-
return;
|
|
1908
|
-
}
|
|
1909
|
-
const summaries = await memoryService.getSummariesByLevel(roomId, currentLevel);
|
|
1910
|
-
const threshold = config.summarization.summariesPerLevel;
|
|
1911
|
-
if (summaries.length < threshold) {
|
|
1912
|
-
return;
|
|
1913
|
-
}
|
|
1914
|
-
import_core9.logger.info({
|
|
1915
|
-
level: currentLevel,
|
|
1916
|
-
count: summaries.length,
|
|
1917
|
-
nextLevel: currentLevel + 1
|
|
1918
|
-
}, `Triggering Level ${currentLevel + 1} summarization`);
|
|
1919
|
-
const higherSummary = await createHigherLevelSummary(runtime, memoryService, summaries, roomId, currentLevel + 1);
|
|
1920
|
-
if (higherSummary) {
|
|
1921
|
-
await checkAndTriggerHigherLevelSummarization(runtime, memoryService, roomId, currentLevel + 1);
|
|
1922
|
-
}
|
|
1923
|
-
}
|
|
1924
|
-
async function createHigherLevelSummary(runtime, memoryService, lowerSummaries, roomId, level) {
|
|
1925
|
-
const formattedSummaries = lowerSummaries.map((s, i) => `<summary${i + 1}>${s.content}</summary${i + 1}>`).join(`
|
|
1926
|
-
`);
|
|
1927
|
-
const prompt = buildHigherLevelSummaryPrompt(formattedSummaries);
|
|
1928
|
-
const originalSystemPrompt = runtime.character.system;
|
|
1929
|
-
try {
|
|
1930
|
-
runtime.character.system = HIGHER_LEVEL_SUMMARIZATION_SYSTEM_PROMPT;
|
|
1931
|
-
const response = await runtime.useModel(import_core9.ModelType.TEXT_LARGE, {
|
|
1932
|
-
prompt,
|
|
1933
|
-
temperature: 0.3
|
|
1934
|
-
});
|
|
1935
|
-
runtime.character.system = originalSystemPrompt;
|
|
1936
|
-
const { summary, keyTopics, reasoningTrace } = parseSummarizationResult(response);
|
|
1937
|
-
if (!summary) {
|
|
1938
|
-
import_core9.logger.warn({ reasoningTrace }, "No higher-level summary extracted from LLM response");
|
|
1939
|
-
return null;
|
|
1940
|
-
}
|
|
1941
|
-
const tokenCount = estimateTokensInSummary(summary);
|
|
1942
|
-
const startTime = new Date(Math.min(...lowerSummaries.map((s) => s.startTime.getTime())));
|
|
1943
|
-
const endTime = new Date(Math.max(...lowerSummaries.map((s) => s.endTime.getTime())));
|
|
1944
|
-
const summaryData = {
|
|
1945
|
-
agentId: runtime.agentId,
|
|
1946
|
-
entityId: lowerSummaries[0].entityId,
|
|
1947
|
-
roomId,
|
|
1948
|
-
level,
|
|
1949
|
-
parentSummaryId: undefined,
|
|
1950
|
-
content: summary,
|
|
1951
|
-
tokenCount,
|
|
1952
|
-
startTime,
|
|
1953
|
-
endTime,
|
|
1954
|
-
sourceCount: lowerSummaries.length,
|
|
1955
|
-
sourceIds: lowerSummaries.map((s) => s.id),
|
|
1956
|
-
metadata: {
|
|
1957
|
-
keyTopics,
|
|
1958
|
-
compressedSummaries: lowerSummaries.length
|
|
1959
|
-
}
|
|
1960
|
-
};
|
|
1961
|
-
return await memoryService.storeSummary(summaryData);
|
|
1962
|
-
} catch (error) {
|
|
1963
|
-
import_core9.logger.error({ error, level }, "Failed to create higher-level summary");
|
|
1964
|
-
runtime.character.system = originalSystemPrompt;
|
|
1965
|
-
return null;
|
|
1966
|
-
}
|
|
1967
|
-
}
|
|
1968
1018
|
|
|
1969
1019
|
// src/providers/long-term-memory.ts
|
|
1970
|
-
var
|
|
1020
|
+
var import_core4 = require("@elizaos/core");
|
|
1971
1021
|
var longTermMemoryProvider = {
|
|
1972
1022
|
name: "LONG_TERM_MEMORY",
|
|
1973
|
-
description: "
|
|
1974
|
-
position:
|
|
1975
|
-
get: async (runtime, message,
|
|
1023
|
+
description: "Persistent facts and preferences about the user",
|
|
1024
|
+
position: 50,
|
|
1025
|
+
get: async (runtime, message, _state) => {
|
|
1976
1026
|
try {
|
|
1977
1027
|
const memoryService = runtime.getService("memory");
|
|
1978
1028
|
if (!memoryService) {
|
|
1979
|
-
import_core10.logger.warn("Memory service not available");
|
|
1980
1029
|
return {
|
|
1981
|
-
data: {},
|
|
1982
|
-
values: {},
|
|
1030
|
+
data: { memories: [] },
|
|
1031
|
+
values: { longTermMemories: "" },
|
|
1983
1032
|
text: ""
|
|
1984
1033
|
};
|
|
1985
1034
|
}
|
|
1986
|
-
const entityId = message
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
}, "Retrieving unified memories");
|
|
1994
|
-
const config = memoryService.getConfig();
|
|
1995
|
-
const [semanticMemories, proceduralMemories, episodicMemories] = await Promise.all([
|
|
1996
|
-
memoryService.searchLongTermMemories({
|
|
1997
|
-
entityId,
|
|
1998
|
-
query,
|
|
1999
|
-
roomId,
|
|
2000
|
-
type: "SEMANTIC" /* SEMANTIC */,
|
|
2001
|
-
limit: config.retrievalLimit,
|
|
2002
|
-
minConfidence: config.minConfidence,
|
|
2003
|
-
similarityThreshold: 0.15
|
|
2004
|
-
}),
|
|
2005
|
-
memoryService.searchLongTermMemories({
|
|
2006
|
-
entityId,
|
|
2007
|
-
query,
|
|
2008
|
-
roomId,
|
|
2009
|
-
type: "PROCEDURAL" /* PROCEDURAL */,
|
|
2010
|
-
limit: Math.floor(config.retrievalLimit / 2),
|
|
2011
|
-
minConfidence: config.minConfidence,
|
|
2012
|
-
similarityThreshold: 0.15
|
|
2013
|
-
}),
|
|
2014
|
-
memoryService.searchLongTermMemories({
|
|
2015
|
-
entityId,
|
|
2016
|
-
query,
|
|
2017
|
-
roomId,
|
|
2018
|
-
type: "EPISODIC" /* EPISODIC */,
|
|
2019
|
-
limit: config.retrievalLimit,
|
|
2020
|
-
minConfidence: config.minConfidence,
|
|
2021
|
-
similarityThreshold: 0.15
|
|
2022
|
-
})
|
|
2023
|
-
]);
|
|
2024
|
-
const longTermSections = [];
|
|
2025
|
-
if (semanticMemories.length > 0) {
|
|
2026
|
-
const items = semanticMemories.map((m) => `- ${m.content} (confidence: ${m.confidence.toFixed(2)}, strength: ${m.activationScore.toFixed(2)})`).join(`
|
|
2027
|
-
`);
|
|
2028
|
-
longTermSections.push(import_core10.addHeader("## Semantic Knowledge (Facts)", items));
|
|
1035
|
+
const { entityId } = message;
|
|
1036
|
+
if (entityId === runtime.agentId) {
|
|
1037
|
+
return {
|
|
1038
|
+
data: { memories: [] },
|
|
1039
|
+
values: { longTermMemories: "" },
|
|
1040
|
+
text: ""
|
|
1041
|
+
};
|
|
2029
1042
|
}
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
1043
|
+
const memories = await memoryService.getLongTermMemories(entityId, undefined, 25);
|
|
1044
|
+
if (memories.length === 0) {
|
|
1045
|
+
return {
|
|
1046
|
+
data: { memories: [] },
|
|
1047
|
+
values: { longTermMemories: "" },
|
|
1048
|
+
text: ""
|
|
1049
|
+
};
|
|
2034
1050
|
}
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
1051
|
+
const formattedMemories = await memoryService.getFormattedLongTermMemories(entityId);
|
|
1052
|
+
const text4 = import_core4.addHeader("# What I Know About You", formattedMemories);
|
|
1053
|
+
const categoryCounts = new Map;
|
|
1054
|
+
for (const memory of memories) {
|
|
1055
|
+
const count = categoryCounts.get(memory.category) || 0;
|
|
1056
|
+
categoryCounts.set(memory.category, count + 1);
|
|
2039
1057
|
}
|
|
2040
|
-
const
|
|
2041
|
-
|
|
2042
|
-
`);
|
|
2043
|
-
const data = {
|
|
2044
|
-
semanticMemories,
|
|
2045
|
-
proceduralMemories,
|
|
2046
|
-
episodicMemories,
|
|
2047
|
-
config: {
|
|
2048
|
-
retrievalLimit: config.retrievalLimit,
|
|
2049
|
-
tokenBudget: config.tokenBudget
|
|
2050
|
-
}
|
|
2051
|
-
};
|
|
2052
|
-
const values = {
|
|
2053
|
-
longTermMemories: longTermMemoriesText
|
|
2054
|
-
};
|
|
2055
|
-
const text3 = longTermMemoriesText;
|
|
2056
|
-
import_core10.logger.info({
|
|
2057
|
-
semanticCount: semanticMemories.length,
|
|
2058
|
-
proceduralCount: proceduralMemories.length,
|
|
2059
|
-
episodicCount: episodicMemories.length
|
|
2060
|
-
}, "Retrieved long-term memory facts");
|
|
1058
|
+
const categoryList = Array.from(categoryCounts.entries()).map(([cat, count]) => `${cat}: ${count}`).join(", ");
|
|
2061
1059
|
return {
|
|
2062
|
-
data
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
} catch (error) {
|
|
2067
|
-
import_core10.logger.error({ error }, "Failed to retrieve long-term memories");
|
|
2068
|
-
return {
|
|
2069
|
-
data: {},
|
|
1060
|
+
data: {
|
|
1061
|
+
memories,
|
|
1062
|
+
categoryCounts: Object.fromEntries(categoryCounts)
|
|
1063
|
+
},
|
|
2070
1064
|
values: {
|
|
2071
|
-
longTermMemories:
|
|
1065
|
+
longTermMemories: text4,
|
|
1066
|
+
memoryCategories: categoryList
|
|
2072
1067
|
},
|
|
2073
|
-
text:
|
|
2074
|
-
};
|
|
2075
|
-
}
|
|
2076
|
-
}
|
|
2077
|
-
};
|
|
2078
|
-
|
|
2079
|
-
// src/providers/recent-conversation-summary.ts
|
|
2080
|
-
var import_core11 = require("@elizaos/core");
|
|
2081
|
-
var getProviderConfig = (runtime) => ({
|
|
2082
|
-
overlapUserMessageCount: parseInt(runtime.getSetting("CONTEXT_OVERLAP_USER_MESSAGES") || "2", 10)
|
|
2083
|
-
});
|
|
2084
|
-
async function fetchConversationData(runtime, roomId) {
|
|
2085
|
-
const [entities, room, allMessages] = await Promise.all([
|
|
2086
|
-
import_core11.getEntityDetails({ runtime, roomId }),
|
|
2087
|
-
runtime.getRoom(roomId),
|
|
2088
|
-
runtime.getMemories({
|
|
2089
|
-
tableName: "messages",
|
|
2090
|
-
roomId,
|
|
2091
|
-
count: 100,
|
|
2092
|
-
unique: false
|
|
2093
|
-
})
|
|
2094
|
-
]);
|
|
2095
|
-
return { entities, room, allMessages };
|
|
2096
|
-
}
|
|
2097
|
-
function filterAndSortDialogueMessages(allMessages) {
|
|
2098
|
-
const dialogueMessages = allMessages.filter((msg) => !(msg.content?.type === "action_result" && msg.metadata?.type === "action_result"));
|
|
2099
|
-
dialogueMessages.sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0));
|
|
2100
|
-
return dialogueMessages;
|
|
2101
|
-
}
|
|
2102
|
-
async function buildCompressedHistory(runtime, message, dialogueMessages, sessionStartTime) {
|
|
2103
|
-
let compressedHistoryText = "";
|
|
2104
|
-
let lastSummarizedIndex = -1;
|
|
2105
|
-
const memoryService = runtime.getService("memory");
|
|
2106
|
-
if (!memoryService?.searchSummaries) {
|
|
2107
|
-
return { compressedHistoryText, lastSummarizedIndex };
|
|
2108
|
-
}
|
|
2109
|
-
try {
|
|
2110
|
-
const summaries = await memoryService.searchSummaries({
|
|
2111
|
-
entityId: message.entityId,
|
|
2112
|
-
roomId: message.roomId,
|
|
2113
|
-
query: typeof message.content === "string" ? message.content : message.content.text || "",
|
|
2114
|
-
limit: 3,
|
|
2115
|
-
tokenBudget: 500
|
|
2116
|
-
});
|
|
2117
|
-
if (summaries.length === 0) {
|
|
2118
|
-
return { compressedHistoryText, lastSummarizedIndex };
|
|
2119
|
-
}
|
|
2120
|
-
lastSummarizedIndex = findLastSummarizedMessageIndex(summaries, dialogueMessages);
|
|
2121
|
-
const summaryItems = summaries.map((s) => {
|
|
2122
|
-
const levelLabel = s.level === 1 ? "Recent Session" : `Overview (L${s.level})`;
|
|
2123
|
-
return `**[${levelLabel}]** ${s.content}`;
|
|
2124
|
-
}).join(`
|
|
2125
|
-
|
|
2126
|
-
`);
|
|
2127
|
-
const headerText = `# Conversation History (Compressed)
|
|
2128
|
-
**Session Started:** ${formatDateTime(sessionStartTime)}`;
|
|
2129
|
-
compressedHistoryText = import_core11.addHeader(headerText, summaryItems);
|
|
2130
|
-
import_core11.logger.debug({
|
|
2131
|
-
summaryCount: summaries.length,
|
|
2132
|
-
totalTokens: summaries.reduce((sum, s) => sum + s.tokenCount, 0),
|
|
2133
|
-
lastSummarizedIndex
|
|
2134
|
-
}, "Using hierarchical summaries for compressed history");
|
|
2135
|
-
} catch (error) {
|
|
2136
|
-
import_core11.logger.warn({ error }, "Failed to retrieve summaries");
|
|
2137
|
-
}
|
|
2138
|
-
return { compressedHistoryText, lastSummarizedIndex };
|
|
2139
|
-
}
|
|
2140
|
-
function findLastSummarizedMessageIndex(summaries, dialogueMessages) {
|
|
2141
|
-
const level1Summaries = summaries.filter((s) => s.level === 1);
|
|
2142
|
-
if (level1Summaries.length === 0) {
|
|
2143
|
-
return -1;
|
|
2144
|
-
}
|
|
2145
|
-
const allSummarizedMessageIds = new Set;
|
|
2146
|
-
level1Summaries.forEach((summary) => {
|
|
2147
|
-
summary.sourceIds.forEach((id) => allSummarizedMessageIds.add(id));
|
|
2148
|
-
});
|
|
2149
|
-
for (let i = dialogueMessages.length - 1;i >= 0; i--) {
|
|
2150
|
-
if (allSummarizedMessageIds.has(dialogueMessages[i].id)) {
|
|
2151
|
-
import_core11.logger.debug({
|
|
2152
|
-
lastSummarizedIndex: i,
|
|
2153
|
-
totalMessages: dialogueMessages.length,
|
|
2154
|
-
summarizedCount: allSummarizedMessageIds.size
|
|
2155
|
-
}, "Determined last summarized message index");
|
|
2156
|
-
return i;
|
|
2157
|
-
}
|
|
2158
|
-
}
|
|
2159
|
-
return -1;
|
|
2160
|
-
}
|
|
2161
|
-
function calculateBufferMessages(runtime, dialogueMessages, lastSummarizedIndex, hasSummaries, overlapUserMessageCount) {
|
|
2162
|
-
let bufferMessages;
|
|
2163
|
-
if (lastSummarizedIndex >= 0 && hasSummaries) {
|
|
2164
|
-
const summarizedMessages = dialogueMessages.slice(0, lastSummarizedIndex + 1);
|
|
2165
|
-
const summarizedUserMessages = summarizedMessages.filter((m) => m.entityId !== runtime.agentId);
|
|
2166
|
-
const overlapUserMessages = summarizedUserMessages.slice(-overlapUserMessageCount);
|
|
2167
|
-
const newUnsummarizedMessages = dialogueMessages.slice(lastSummarizedIndex + 1);
|
|
2168
|
-
let overlapStartIndex = lastSummarizedIndex + 1;
|
|
2169
|
-
if (overlapUserMessages.length > 0) {
|
|
2170
|
-
const firstOverlapUserMessageId = overlapUserMessages[0].id;
|
|
2171
|
-
const foundIndex = dialogueMessages.findIndex((m) => m.id === firstOverlapUserMessageId);
|
|
2172
|
-
if (foundIndex >= 0) {
|
|
2173
|
-
overlapStartIndex = foundIndex;
|
|
2174
|
-
}
|
|
2175
|
-
}
|
|
2176
|
-
bufferMessages = dialogueMessages.slice(overlapStartIndex);
|
|
2177
|
-
import_core11.logger.debug({
|
|
2178
|
-
lastSummarizedIndex,
|
|
2179
|
-
summarizedMessageCount: summarizedMessages.length,
|
|
2180
|
-
overlapStartIndex,
|
|
2181
|
-
overlapSize: lastSummarizedIndex + 1 - overlapStartIndex,
|
|
2182
|
-
newUnsummarizedCount: newUnsummarizedMessages.length,
|
|
2183
|
-
bufferSize: bufferMessages.length,
|
|
2184
|
-
totalDialogueMessages: dialogueMessages.length,
|
|
2185
|
-
overlapUserMessageCount
|
|
2186
|
-
}, `Dynamic buffer: [overlap: last ${overlapUserMessageCount} user msgs from summary] + [all new unsummarized messages]`);
|
|
2187
|
-
} else {
|
|
2188
|
-
bufferMessages = dialogueMessages;
|
|
2189
|
-
import_core11.logger.debug({
|
|
2190
|
-
bufferSize: bufferMessages.length,
|
|
2191
|
-
totalMessages: dialogueMessages.length
|
|
2192
|
-
}, "Using full conversation: no summaries yet");
|
|
2193
|
-
}
|
|
2194
|
-
return { bufferMessages, lastSummarizedIndex };
|
|
2195
|
-
}
|
|
2196
|
-
function formatDateTime(timestamp3) {
|
|
2197
|
-
const date = new Date(timestamp3);
|
|
2198
|
-
return date.toLocaleString("en-US", {
|
|
2199
|
-
year: "numeric",
|
|
2200
|
-
month: "short",
|
|
2201
|
-
day: "numeric",
|
|
2202
|
-
hour: "2-digit",
|
|
2203
|
-
minute: "2-digit",
|
|
2204
|
-
hour12: false
|
|
2205
|
-
});
|
|
2206
|
-
}
|
|
2207
|
-
function removeEntityIds(text3) {
|
|
2208
|
-
return text3.replace(/\s*\[[\w\-]+\]/g, "").replace(/\s*\([^)]*'s internal thought:[^)]*\)/gi, "").split(`
|
|
2209
|
-
`).map((line) => line.trim()).join(`
|
|
2210
|
-
`).replace(/\n{3,}/g, `
|
|
2211
|
-
|
|
2212
|
-
`);
|
|
2213
|
-
}
|
|
2214
|
-
async function formatBufferMessages(bufferMessages, entities, room, sessionStartTime) {
|
|
2215
|
-
const isPostFormat = room?.type ? room.type === import_core11.ChannelType.FEED || room.type === import_core11.ChannelType.THREAD : false;
|
|
2216
|
-
const [formattedRecentMessages, formattedRecentPosts] = await Promise.all([
|
|
2217
|
-
import_core11.formatMessages({
|
|
2218
|
-
messages: bufferMessages,
|
|
2219
|
-
entities
|
|
2220
|
-
}),
|
|
2221
|
-
import_core11.formatPosts({
|
|
2222
|
-
messages: bufferMessages,
|
|
2223
|
-
entities,
|
|
2224
|
-
conversationHeader: false
|
|
2225
|
-
})
|
|
2226
|
-
]);
|
|
2227
|
-
let recentBufferText = (isPostFormat ? formattedRecentPosts : formattedRecentMessages) || "";
|
|
2228
|
-
recentBufferText = removeEntityIds(recentBufferText);
|
|
2229
|
-
const firstBufferTime = bufferMessages.length > 0 ? bufferMessages[0].createdAt : null;
|
|
2230
|
-
const lastBufferTime = bufferMessages.length > 0 ? bufferMessages[bufferMessages.length - 1].createdAt : null;
|
|
2231
|
-
let headerText = `# Recent Messages (Last ${bufferMessages.length})`;
|
|
2232
|
-
if (firstBufferTime && lastBufferTime) {
|
|
2233
|
-
headerText += `
|
|
2234
|
-
**Time Range:** ${formatDateTime(firstBufferTime)} - ${formatDateTime(lastBufferTime)}`;
|
|
2235
|
-
}
|
|
2236
|
-
return recentBufferText ? import_core11.addHeader(headerText, recentBufferText) : "";
|
|
2237
|
-
}
|
|
2238
|
-
function buildReceivedMessageSection(message, entities) {
|
|
2239
|
-
const metaData = message.metadata;
|
|
2240
|
-
const senderName = entities.find((entity) => entity.id === message.entityId)?.names[0] || metaData?.entityName || "Unknown User";
|
|
2241
|
-
const receivedMessageContent = message.content.text;
|
|
2242
|
-
const hasReceivedMessage = !!receivedMessageContent?.trim();
|
|
2243
|
-
const receivedMessageHeader = hasReceivedMessage ? import_core11.addHeader("# Current Message", `**From ${senderName}:** ${receivedMessageContent}`) : "";
|
|
2244
|
-
const focusHeader = hasReceivedMessage ? import_core11.addHeader("# Response Focus", `Reply to **${senderName}**'s current message above. Stay relevant to their question. Don't repeat previous responses unless asked again.`) : "";
|
|
2245
|
-
return { receivedMessageHeader, focusHeader };
|
|
2246
|
-
}
|
|
2247
|
-
function assembleContextSections(compressedHistoryText, recentBufferHeader, receivedMessageHeader, focusHeader) {
|
|
2248
|
-
const textSections = [
|
|
2249
|
-
compressedHistoryText,
|
|
2250
|
-
recentBufferHeader,
|
|
2251
|
-
receivedMessageHeader,
|
|
2252
|
-
focusHeader
|
|
2253
|
-
].filter(Boolean);
|
|
2254
|
-
return textSections.join(`
|
|
2255
|
-
|
|
2256
|
-
`);
|
|
2257
|
-
}
|
|
2258
|
-
var recentContextProvider = {
|
|
2259
|
-
name: "RECENT_CONVERSATION_SUMMARY",
|
|
2260
|
-
description: "Intelligent context management combining recent messages with hierarchical summaries for optimal token efficiency",
|
|
2261
|
-
position: 100,
|
|
2262
|
-
get: async (runtime, message) => {
|
|
2263
|
-
try {
|
|
2264
|
-
const { roomId } = message;
|
|
2265
|
-
const config = getProviderConfig(runtime);
|
|
2266
|
-
const { entities, room, allMessages } = await fetchConversationData(runtime, roomId);
|
|
2267
|
-
const dialogueMessages = filterAndSortDialogueMessages(allMessages);
|
|
2268
|
-
const sessionStartTime = dialogueMessages.length > 0 ? dialogueMessages[0].createdAt || Date.now() : Date.now();
|
|
2269
|
-
const { compressedHistoryText, lastSummarizedIndex } = await buildCompressedHistory(runtime, message, dialogueMessages, sessionStartTime);
|
|
2270
|
-
const hasSummaries = !!compressedHistoryText;
|
|
2271
|
-
const { bufferMessages } = calculateBufferMessages(runtime, dialogueMessages, lastSummarizedIndex, hasSummaries, config.overlapUserMessageCount);
|
|
2272
|
-
const recentBufferHeader = await formatBufferMessages(bufferMessages, entities, room, sessionStartTime);
|
|
2273
|
-
const { receivedMessageHeader, focusHeader } = buildReceivedMessageSection(message, entities);
|
|
2274
|
-
const text3 = assembleContextSections(compressedHistoryText, recentBufferHeader, receivedMessageHeader, focusHeader);
|
|
2275
|
-
const data = {
|
|
2276
|
-
dialogueMessages: bufferMessages,
|
|
2277
|
-
messageCount: dialogueMessages.length,
|
|
2278
|
-
lastSummarizedIndex,
|
|
2279
|
-
config
|
|
2280
|
-
};
|
|
2281
|
-
const isPostFormat = room?.type ? room.type === import_core11.ChannelType.FEED || room.type === import_core11.ChannelType.THREAD : false;
|
|
2282
|
-
const values = {
|
|
2283
|
-
compressedHistory: compressedHistoryText,
|
|
2284
|
-
recentMessages: recentBufferHeader,
|
|
2285
|
-
receivedMessage: receivedMessageHeader,
|
|
2286
|
-
focusInstruction: focusHeader,
|
|
2287
|
-
recentPosts: isPostFormat ? recentBufferHeader : ""
|
|
2288
|
-
};
|
|
2289
|
-
import_core11.logger.info({
|
|
2290
|
-
messageCount: dialogueMessages.length,
|
|
2291
|
-
bufferSize: bufferMessages.length,
|
|
2292
|
-
hasSummaries,
|
|
2293
|
-
lastSummarizedIndex,
|
|
2294
|
-
estimatedTokens: Math.ceil(text3.length / 4),
|
|
2295
|
-
overlapUserMessageCount: config.overlapUserMessageCount
|
|
2296
|
-
}, "Recent context assembled");
|
|
2297
|
-
return {
|
|
2298
|
-
data,
|
|
2299
|
-
values,
|
|
2300
|
-
text: text3
|
|
1068
|
+
text: text4
|
|
2301
1069
|
};
|
|
2302
1070
|
} catch (error) {
|
|
2303
|
-
|
|
1071
|
+
import_core4.logger.error({ error }, "Error in longTermMemoryProvider:");
|
|
2304
1072
|
return {
|
|
2305
|
-
data: {
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
lastSummarizedIndex: -1,
|
|
2309
|
-
config: getProviderConfig(runtime)
|
|
2310
|
-
},
|
|
2311
|
-
values: {
|
|
2312
|
-
compressedHistory: "",
|
|
2313
|
-
recentMessages: "",
|
|
2314
|
-
receivedMessage: "",
|
|
2315
|
-
focusInstruction: "",
|
|
2316
|
-
recentPosts: ""
|
|
2317
|
-
},
|
|
2318
|
-
text: "Error retrieving context."
|
|
1073
|
+
data: { memories: [] },
|
|
1074
|
+
values: { longTermMemories: "" },
|
|
1075
|
+
text: ""
|
|
2319
1076
|
};
|
|
2320
1077
|
}
|
|
2321
1078
|
}
|
|
2322
1079
|
};
|
|
2323
1080
|
|
|
2324
|
-
// src/providers/
|
|
2325
|
-
var
|
|
2326
|
-
var
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
return "";
|
|
2332
|
-
}
|
|
2333
|
-
const groupedByRun = new Map;
|
|
2334
|
-
for (const mem of actionResultMessages) {
|
|
2335
|
-
const runId = String(mem.content?.runId || "unknown");
|
|
2336
|
-
if (!groupedByRun.has(runId)) {
|
|
2337
|
-
groupedByRun.set(runId, []);
|
|
2338
|
-
}
|
|
2339
|
-
groupedByRun.get(runId).push(mem);
|
|
2340
|
-
}
|
|
2341
|
-
const formattedActionResults = Array.from(groupedByRun.entries()).slice(-limit).map(([runId, memories]) => {
|
|
2342
|
-
const sortedMemories = memories.sort((a, b) => (a.createdAt || 0) - (b.createdAt || 0));
|
|
2343
|
-
const thought = sortedMemories[0]?.content?.planThought || "";
|
|
2344
|
-
const runText = sortedMemories.map((mem) => {
|
|
2345
|
-
const actionName = mem.content?.actionName || "Unknown";
|
|
2346
|
-
const status = mem.content?.actionStatus || "unknown";
|
|
2347
|
-
const planStep = mem.content?.planStep || "";
|
|
2348
|
-
const text3 = mem.content?.text || "";
|
|
2349
|
-
const error = mem.content?.error || "";
|
|
2350
|
-
let memText = ` - ${actionName} (${status})`;
|
|
2351
|
-
if (planStep)
|
|
2352
|
-
memText += ` [${planStep}]`;
|
|
2353
|
-
if (error) {
|
|
2354
|
-
memText += `: Error - ${error}`;
|
|
2355
|
-
} else if (text3 && text3 !== `Executed action: ${actionName}`) {
|
|
2356
|
-
memText += `: ${text3}`;
|
|
2357
|
-
}
|
|
2358
|
-
return memText;
|
|
2359
|
-
}).join(`
|
|
2360
|
-
`);
|
|
2361
|
-
return `**Action Run ${runId.slice(0, 8)}**${thought ? ` - "${thought}"` : ""}
|
|
2362
|
-
${runText}`;
|
|
2363
|
-
}).join(`
|
|
2364
|
-
|
|
2365
|
-
`);
|
|
2366
|
-
return formattedActionResults ? import_core12.addHeader("# Recent Action Executions", formattedActionResults) : "";
|
|
2367
|
-
}
|
|
2368
|
-
var actionResultsProvider = {
|
|
2369
|
-
name: "ACTION_RESULTS",
|
|
2370
|
-
description: "Recent action executions with their outcomes (tool memory)",
|
|
2371
|
-
position: 101,
|
|
2372
|
-
get: async (runtime, message) => {
|
|
1081
|
+
// src/providers/context-summary.ts
|
|
1082
|
+
var import_core5 = require("@elizaos/core");
|
|
1083
|
+
var contextSummaryProvider = {
|
|
1084
|
+
name: "SUMMARIZED_CONTEXT",
|
|
1085
|
+
description: "Provides summarized context from previous conversations",
|
|
1086
|
+
position: 96,
|
|
1087
|
+
get: async (runtime, message, _state) => {
|
|
2373
1088
|
try {
|
|
1089
|
+
const memoryService = runtime.getService("memory");
|
|
2374
1090
|
const { roomId } = message;
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
1091
|
+
if (!memoryService) {
|
|
1092
|
+
return {
|
|
1093
|
+
data: {
|
|
1094
|
+
summary: null
|
|
1095
|
+
},
|
|
1096
|
+
values: {
|
|
1097
|
+
sessionSummaries: "",
|
|
1098
|
+
sessionSummariesWithTopics: ""
|
|
1099
|
+
},
|
|
1100
|
+
text: ""
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
const currentSummary = await memoryService.getCurrentSessionSummary(roomId);
|
|
1104
|
+
if (!currentSummary) {
|
|
1105
|
+
return {
|
|
1106
|
+
data: {
|
|
1107
|
+
summary: null
|
|
1108
|
+
},
|
|
1109
|
+
values: {
|
|
1110
|
+
sessionSummaries: "",
|
|
1111
|
+
sessionSummariesWithTopics: ""
|
|
1112
|
+
},
|
|
1113
|
+
text: ""
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
const messageRange = `${currentSummary.messageCount} messages`;
|
|
1117
|
+
const timeRange = new Date(currentSummary.startTime).toLocaleDateString();
|
|
1118
|
+
let summaryOnly = `**Previous Conversation** (${messageRange}, ${timeRange})
|
|
1119
|
+
`;
|
|
1120
|
+
summaryOnly += currentSummary.summary;
|
|
1121
|
+
let summaryWithTopics = summaryOnly;
|
|
1122
|
+
if (currentSummary.topics && currentSummary.topics.length > 0) {
|
|
1123
|
+
summaryWithTopics += `
|
|
1124
|
+
*Topics: ${currentSummary.topics.join(", ")}*`;
|
|
1125
|
+
}
|
|
1126
|
+
const sessionSummaries2 = import_core5.addHeader("# Conversation Summary", summaryOnly);
|
|
1127
|
+
const sessionSummariesWithTopics = import_core5.addHeader("# Conversation Summary", summaryWithTopics);
|
|
2389
1128
|
return {
|
|
2390
1129
|
data: {
|
|
2391
|
-
|
|
2392
|
-
config
|
|
1130
|
+
summary: currentSummary
|
|
2393
1131
|
},
|
|
2394
1132
|
values: {
|
|
2395
|
-
|
|
1133
|
+
sessionSummaries: sessionSummaries2,
|
|
1134
|
+
sessionSummariesWithTopics
|
|
2396
1135
|
},
|
|
2397
|
-
text:
|
|
1136
|
+
text: sessionSummariesWithTopics
|
|
2398
1137
|
};
|
|
2399
1138
|
} catch (error) {
|
|
2400
|
-
|
|
1139
|
+
import_core5.logger.error({ error }, "Error in contextSummaryProvider:");
|
|
2401
1140
|
return {
|
|
2402
1141
|
data: {
|
|
2403
|
-
|
|
2404
|
-
config: getActionResultsConfig(runtime)
|
|
1142
|
+
summary: null
|
|
2405
1143
|
},
|
|
2406
1144
|
values: {
|
|
2407
|
-
|
|
1145
|
+
sessionSummaries: "",
|
|
1146
|
+
sessionSummariesWithTopics: ""
|
|
2408
1147
|
},
|
|
2409
1148
|
text: ""
|
|
2410
1149
|
};
|
|
2411
1150
|
}
|
|
2412
1151
|
}
|
|
2413
1152
|
};
|
|
1153
|
+
|
|
2414
1154
|
// src/index.ts
|
|
2415
1155
|
var memoryPlugin = {
|
|
2416
1156
|
name: "memory",
|
|
2417
|
-
description: "
|
|
1157
|
+
description: "Advanced memory management with conversation summarization and long-term persistent memory",
|
|
2418
1158
|
services: [MemoryService],
|
|
2419
|
-
evaluators: [
|
|
2420
|
-
providers: [
|
|
1159
|
+
evaluators: [summarizationEvaluator, longTermExtractionEvaluator],
|
|
1160
|
+
providers: [
|
|
1161
|
+
longTermMemoryProvider,
|
|
1162
|
+
contextSummaryProvider
|
|
1163
|
+
],
|
|
2421
1164
|
schema: exports_schemas
|
|
2422
1165
|
};
|
|
2423
1166
|
var src_default = memoryPlugin;
|
|
2424
1167
|
|
|
2425
|
-
//# debugId=
|
|
1168
|
+
//# debugId=F9F8FE6AD7DE8BD864756E2164756E21
|