@ainetwork/adk-provider-memory-mongodb 0.2.3 → 0.3.0
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/dist/chunk-OWGGE5EW.js +17 -0
- package/dist/chunk-OWGGE5EW.js.map +1 -0
- package/dist/index.cjs +403 -179
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +41 -25
- package/dist/index.d.ts +41 -25
- package/dist/index.js +345 -129
- package/dist/index.js.map +1 -1
- package/dist/models/agent.model.cjs +52 -0
- package/dist/models/agent.model.cjs.map +1 -0
- package/dist/models/agent.model.d.cts +23 -0
- package/dist/models/agent.model.d.ts +23 -0
- package/dist/models/agent.model.js +9 -0
- package/dist/models/agent.model.js.map +1 -0
- package/implements/agent.memory.ts +46 -0
- package/implements/base.memory.ts +248 -36
- package/implements/intent.memory.ts +60 -20
- package/implements/thread.memory.ts +113 -87
- package/index.ts +2 -2
- package/models/agent.model.ts +16 -0
- package/package.json +3 -3
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// models/agent.model.ts
|
|
2
|
+
import { Schema } from "mongoose";
|
|
3
|
+
import mongoose from "mongoose";
|
|
4
|
+
var AgentObjectSchema = new Schema(
|
|
5
|
+
{
|
|
6
|
+
agent_prompt: {
|
|
7
|
+
type: String
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
);
|
|
11
|
+
var AgentModel = mongoose.model("Agent", AgentObjectSchema);
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
AgentObjectSchema,
|
|
15
|
+
AgentModel
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=chunk-OWGGE5EW.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../models/agent.model.ts"],"sourcesContent":["import { type Document, Schema } from \"mongoose\";\nimport mongoose from \"mongoose\";\n\nexport const AgentObjectSchema = new Schema(\n\t{\n\t\tagent_prompt: {\n\t\t\ttype: String,\n\t\t},\n\t},\n);\n\nexport interface AgentDocument extends Document {\n\tagent_prompt: string;\n}\n\nexport const AgentModel = mongoose.model<AgentDocument>(\"Agent\", AgentObjectSchema);\n"],"mappings":";AAAA,SAAwB,cAAc;AACtC,OAAO,cAAc;AAEd,IAAM,oBAAoB,IAAI;AAAA,EACpC;AAAA,IACC,cAAc;AAAA,MACb,MAAM;AAAA,IACP;AAAA,EACD;AACD;AAMO,IAAM,aAAa,SAAS,MAAqB,SAAS,iBAAiB;","names":[]}
|
package/dist/index.cjs
CHANGED
|
@@ -30,62 +30,147 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
|
|
34
|
-
MongoDBThread: () => MongoDBThread
|
|
33
|
+
MongoDBMemory: () => MongoDBMemory
|
|
35
34
|
});
|
|
36
35
|
module.exports = __toCommonJS(index_exports);
|
|
37
36
|
|
|
38
37
|
// implements/base.memory.ts
|
|
39
|
-
var
|
|
40
|
-
var
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
try {
|
|
52
|
-
await import_mongoose.default.connect(this._uri, {
|
|
53
|
-
maxPoolSize: 1,
|
|
54
|
-
serverSelectionTimeoutMS: 3e4,
|
|
55
|
-
socketTimeoutMS: 45e3,
|
|
56
|
-
connectTimeoutMS: 3e4,
|
|
57
|
-
bufferCommands: false
|
|
58
|
-
});
|
|
59
|
-
this._isConnected = true;
|
|
60
|
-
import_logger.loggers.agent.info("MongoDB connected successfully");
|
|
61
|
-
} catch (error) {
|
|
62
|
-
import_logger.loggers.agent.error("Failed to connect to MongoDB:", error);
|
|
63
|
-
throw error;
|
|
38
|
+
var import_mongoose8 = __toESM(require("mongoose"), 1);
|
|
39
|
+
var import_logger2 = require("@ainetwork/adk/utils/logger");
|
|
40
|
+
|
|
41
|
+
// models/agent.model.ts
|
|
42
|
+
var import_mongoose = require("mongoose");
|
|
43
|
+
var import_mongoose2 = __toESM(require("mongoose"), 1);
|
|
44
|
+
var AgentObjectSchema = new import_mongoose.Schema(
|
|
45
|
+
{
|
|
46
|
+
agent_prompt: {
|
|
47
|
+
type: String
|
|
64
48
|
}
|
|
65
49
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
50
|
+
);
|
|
51
|
+
var AgentModel = import_mongoose2.default.model("Agent", AgentObjectSchema);
|
|
52
|
+
|
|
53
|
+
// implements/agent.memory.ts
|
|
54
|
+
var MongoDBAgent = class {
|
|
55
|
+
executeWithRetry;
|
|
56
|
+
getOperationTimeout;
|
|
57
|
+
constructor(executeWithRetry, getOperationTimeout) {
|
|
58
|
+
this.executeWithRetry = executeWithRetry;
|
|
59
|
+
this.getOperationTimeout = getOperationTimeout;
|
|
60
|
+
}
|
|
61
|
+
async getAgentPrompt() {
|
|
62
|
+
return this.executeWithRetry(async () => {
|
|
63
|
+
const timeout = this.getOperationTimeout();
|
|
64
|
+
const metadata = await AgentModel.findOne({
|
|
65
|
+
id: "agent_metadata"
|
|
66
|
+
}).maxTimeMS(timeout).lean();
|
|
67
|
+
return metadata?.agent_prompt || "";
|
|
68
|
+
}, "getAgentPrompt()");
|
|
69
|
+
}
|
|
70
|
+
async updateAgentPrompt(prompt) {
|
|
71
|
+
return this.executeWithRetry(async () => {
|
|
72
|
+
const timeout = this.getOperationTimeout();
|
|
73
|
+
await AgentModel.updateOne({
|
|
74
|
+
id: "agent_metadata"
|
|
75
|
+
}, { "agent_prompt": prompt }).maxTimeMS(timeout);
|
|
76
|
+
}, "updateAgentPrompt()");
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// models/intent.model.ts
|
|
81
|
+
var import_mongoose3 = __toESM(require("mongoose"), 1);
|
|
82
|
+
var IntentObjectSchema = new import_mongoose3.Schema(
|
|
83
|
+
{
|
|
84
|
+
id: {
|
|
85
|
+
type: String,
|
|
86
|
+
required: true,
|
|
87
|
+
index: true,
|
|
88
|
+
unique: true
|
|
89
|
+
},
|
|
90
|
+
name: {
|
|
91
|
+
type: String,
|
|
92
|
+
required: true,
|
|
93
|
+
index: true
|
|
94
|
+
},
|
|
95
|
+
description: {
|
|
96
|
+
type: String,
|
|
97
|
+
required: true
|
|
98
|
+
},
|
|
99
|
+
prompt: {
|
|
100
|
+
type: String,
|
|
101
|
+
required: false
|
|
102
|
+
},
|
|
103
|
+
status: {
|
|
104
|
+
type: String,
|
|
105
|
+
required: true
|
|
106
|
+
},
|
|
107
|
+
triggeringSentences: {
|
|
108
|
+
type: [String],
|
|
109
|
+
required: false
|
|
110
|
+
},
|
|
111
|
+
tags: {
|
|
112
|
+
type: [String],
|
|
113
|
+
required: false
|
|
77
114
|
}
|
|
78
115
|
}
|
|
79
|
-
|
|
80
|
-
|
|
116
|
+
);
|
|
117
|
+
var IntentModel = import_mongoose3.default.model("Intent", IntentObjectSchema);
|
|
118
|
+
|
|
119
|
+
// implements/intent.memory.ts
|
|
120
|
+
var MongoDBIntent = class {
|
|
121
|
+
executeWithRetry;
|
|
122
|
+
getOperationTimeout;
|
|
123
|
+
constructor(executeWithRetry, getOperationTimeout) {
|
|
124
|
+
this.executeWithRetry = executeWithRetry;
|
|
125
|
+
this.getOperationTimeout = getOperationTimeout;
|
|
126
|
+
}
|
|
127
|
+
async getIntent(intentId) {
|
|
128
|
+
return this.executeWithRetry(async () => {
|
|
129
|
+
const timeout = this.getOperationTimeout();
|
|
130
|
+
const intent = await IntentModel.findOne({ id: intentId }).maxTimeMS(timeout).lean();
|
|
131
|
+
return intent || void 0;
|
|
132
|
+
}, `getIntent(${intentId})`);
|
|
133
|
+
}
|
|
134
|
+
async getIntentByName(intentName) {
|
|
135
|
+
return this.executeWithRetry(async () => {
|
|
136
|
+
const timeout = this.getOperationTimeout();
|
|
137
|
+
const intent = await IntentModel.findOne({ name: intentName }).maxTimeMS(timeout).lean();
|
|
138
|
+
return intent || void 0;
|
|
139
|
+
}, `getIntentByName(${intentName})`);
|
|
140
|
+
}
|
|
141
|
+
async saveIntent(intent) {
|
|
142
|
+
return this.executeWithRetry(async () => {
|
|
143
|
+
await IntentModel.create(intent);
|
|
144
|
+
}, `saveIntent(${intent.id})`);
|
|
145
|
+
}
|
|
146
|
+
async updateIntent(intentId, intent) {
|
|
147
|
+
return this.executeWithRetry(async () => {
|
|
148
|
+
const timeout = this.getOperationTimeout();
|
|
149
|
+
await IntentModel.updateOne({
|
|
150
|
+
id: intentId
|
|
151
|
+
}, intent).maxTimeMS(timeout);
|
|
152
|
+
}, `updateIntent(${intentId})`);
|
|
153
|
+
}
|
|
154
|
+
async deleteIntent(intentId) {
|
|
155
|
+
return this.executeWithRetry(async () => {
|
|
156
|
+
const timeout = this.getOperationTimeout();
|
|
157
|
+
await IntentModel.deleteOne({ id: intentId }).maxTimeMS(timeout);
|
|
158
|
+
}, `deleteIntent(${intentId})`);
|
|
159
|
+
}
|
|
160
|
+
async listIntents() {
|
|
161
|
+
return this.executeWithRetry(async () => {
|
|
162
|
+
const timeout = this.getOperationTimeout();
|
|
163
|
+
const intents = await IntentModel.find().maxTimeMS(timeout).lean();
|
|
164
|
+
return intents;
|
|
165
|
+
}, `listIntents()`);
|
|
81
166
|
}
|
|
82
167
|
};
|
|
83
168
|
|
|
84
169
|
// models/threads.model.ts
|
|
85
170
|
var import_memory = require("@ainetwork/adk/types/memory");
|
|
86
|
-
var
|
|
87
|
-
var
|
|
88
|
-
var ThreadObjectSchema = new
|
|
171
|
+
var import_mongoose4 = require("mongoose");
|
|
172
|
+
var import_mongoose5 = __toESM(require("mongoose"), 1);
|
|
173
|
+
var ThreadObjectSchema = new import_mongoose4.Schema(
|
|
89
174
|
{
|
|
90
175
|
type: {
|
|
91
176
|
type: String,
|
|
@@ -116,20 +201,20 @@ var ThreadObjectSchema = new import_mongoose2.Schema(
|
|
|
116
201
|
}
|
|
117
202
|
}
|
|
118
203
|
);
|
|
119
|
-
var ThreadModel =
|
|
204
|
+
var ThreadModel = import_mongoose5.default.model("Thread", ThreadObjectSchema);
|
|
120
205
|
|
|
121
206
|
// models/messages.model.ts
|
|
122
207
|
var import_memory2 = require("@ainetwork/adk/types/memory");
|
|
123
|
-
var
|
|
124
|
-
var
|
|
125
|
-
var MessageContentObjectSchema = new
|
|
208
|
+
var import_mongoose6 = require("mongoose");
|
|
209
|
+
var import_mongoose7 = __toESM(require("mongoose"), 1);
|
|
210
|
+
var MessageContentObjectSchema = new import_mongoose6.Schema(
|
|
126
211
|
{
|
|
127
212
|
type: { type: String, required: true },
|
|
128
|
-
parts: { type: [
|
|
213
|
+
parts: { type: [import_mongoose6.Schema.Types.Mixed], required: true }
|
|
129
214
|
},
|
|
130
215
|
{ _id: false }
|
|
131
216
|
);
|
|
132
|
-
var MessageObjectSchema = new
|
|
217
|
+
var MessageObjectSchema = new import_mongoose6.Schema(
|
|
133
218
|
{
|
|
134
219
|
messageId: {
|
|
135
220
|
type: String,
|
|
@@ -160,167 +245,306 @@ var MessageObjectSchema = new import_mongoose4.Schema(
|
|
|
160
245
|
required: true
|
|
161
246
|
},
|
|
162
247
|
metadata: {
|
|
163
|
-
type:
|
|
248
|
+
type: import_mongoose6.Schema.Types.Mixed,
|
|
164
249
|
default: {}
|
|
165
250
|
}
|
|
166
251
|
}
|
|
167
252
|
);
|
|
168
|
-
var MessageModel =
|
|
253
|
+
var MessageModel = import_mongoose7.default.model("Message", MessageObjectSchema);
|
|
169
254
|
|
|
170
255
|
// implements/thread.memory.ts
|
|
171
|
-
var
|
|
172
|
-
var MongoDBThread = class
|
|
173
|
-
|
|
174
|
-
|
|
256
|
+
var import_logger = require("@ainetwork/adk/utils/logger");
|
|
257
|
+
var MongoDBThread = class {
|
|
258
|
+
executeWithRetry;
|
|
259
|
+
getOperationTimeout;
|
|
260
|
+
constructor(executeWithRetry, getOperationTimeout) {
|
|
261
|
+
this.executeWithRetry = executeWithRetry;
|
|
262
|
+
this.getOperationTimeout = getOperationTimeout;
|
|
175
263
|
}
|
|
176
264
|
async getThread(userId, threadId) {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
265
|
+
return this.executeWithRetry(async () => {
|
|
266
|
+
const timeout = this.getOperationTimeout();
|
|
267
|
+
const thread = await ThreadModel.findOne({ threadId, userId }).maxTimeMS(timeout);
|
|
268
|
+
const messages = await MessageModel.find({ threadId, userId }).sort({ timestamp: 1 }).maxTimeMS(timeout);
|
|
269
|
+
if (!thread) return void 0;
|
|
270
|
+
import_logger.loggers.agent.debug(`Found ${messages.length} messages for thread ${threadId}`);
|
|
271
|
+
const threadObject = {
|
|
272
|
+
threadId: thread.threadId,
|
|
273
|
+
userId: thread.userId,
|
|
274
|
+
type: thread.type,
|
|
275
|
+
title: thread.title || "New thread",
|
|
276
|
+
messages: []
|
|
277
|
+
};
|
|
278
|
+
messages.forEach((message) => {
|
|
279
|
+
threadObject.messages.push({
|
|
280
|
+
messageId: message.messageId,
|
|
281
|
+
role: message.role,
|
|
282
|
+
content: message.content,
|
|
283
|
+
timestamp: message.timestamp,
|
|
284
|
+
metadata: message.metadata
|
|
285
|
+
});
|
|
197
286
|
});
|
|
198
|
-
|
|
199
|
-
|
|
287
|
+
return threadObject;
|
|
288
|
+
}, `getThread(${userId}, ${threadId})`);
|
|
200
289
|
}
|
|
201
290
|
async createThread(type, userId, threadId, title) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
291
|
+
return this.executeWithRetry(async () => {
|
|
292
|
+
const now = Date.now();
|
|
293
|
+
await ThreadModel.create({
|
|
294
|
+
type,
|
|
295
|
+
userId,
|
|
296
|
+
threadId,
|
|
297
|
+
title,
|
|
298
|
+
updated_at: now,
|
|
299
|
+
created_at: now
|
|
300
|
+
});
|
|
301
|
+
return { type, userId, threadId, title, messages: [] };
|
|
302
|
+
}, `createThread(${userId}, ${threadId})`);
|
|
212
303
|
}
|
|
213
304
|
async addMessagesToThread(userId, threadId, messages) {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
for (const message of messages) {
|
|
218
|
-
await MessageModel.create({
|
|
219
|
-
threadId,
|
|
220
|
-
messageId: message.messageId,
|
|
221
|
-
userId,
|
|
222
|
-
role: message.role,
|
|
223
|
-
content: message.content,
|
|
224
|
-
timestamp: message.timestamp,
|
|
225
|
-
metadata: message.metadata
|
|
305
|
+
return this.executeWithRetry(async () => {
|
|
306
|
+
await ThreadModel.updateOne({ threadId, userId }, {
|
|
307
|
+
updated_at: Date.now()
|
|
226
308
|
});
|
|
227
|
-
|
|
309
|
+
for (const message of messages) {
|
|
310
|
+
await MessageModel.create({
|
|
311
|
+
threadId,
|
|
312
|
+
messageId: message.messageId,
|
|
313
|
+
userId,
|
|
314
|
+
role: message.role,
|
|
315
|
+
content: message.content,
|
|
316
|
+
timestamp: message.timestamp,
|
|
317
|
+
metadata: message.metadata
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}, `addMessagesToThread(${userId}, ${threadId})`);
|
|
228
321
|
}
|
|
229
322
|
async deleteThread(userId, threadId) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
323
|
+
return this.executeWithRetry(async () => {
|
|
324
|
+
const timeout = this.getOperationTimeout();
|
|
325
|
+
const messages = await MessageModel.find({ userId, threadId }).sort({ timestamp: 1 }).maxTimeMS(timeout);
|
|
326
|
+
messages?.forEach((message) => {
|
|
327
|
+
message.deleteOne();
|
|
328
|
+
});
|
|
329
|
+
const thread = await ThreadModel.findOne({ userId, threadId }).maxTimeMS(timeout);
|
|
330
|
+
thread?.deleteOne();
|
|
331
|
+
}, `deleteThread(${userId}, ${threadId})`);
|
|
238
332
|
}
|
|
239
333
|
async listThreads(userId) {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
334
|
+
return this.executeWithRetry(async () => {
|
|
335
|
+
const timeout = this.getOperationTimeout();
|
|
336
|
+
const threads = await ThreadModel.find({ userId }).sort({ updated_at: -1 }).maxTimeMS(timeout);
|
|
337
|
+
const data = threads.map((thread) => {
|
|
338
|
+
return {
|
|
339
|
+
type: thread.type,
|
|
340
|
+
userId,
|
|
341
|
+
threadId: thread.threadId,
|
|
342
|
+
title: thread.title,
|
|
343
|
+
updatedAt: thread.updated_at
|
|
344
|
+
};
|
|
345
|
+
});
|
|
346
|
+
return data;
|
|
347
|
+
}, `listThreads(${userId})`);
|
|
253
348
|
}
|
|
254
349
|
};
|
|
255
350
|
|
|
256
|
-
//
|
|
257
|
-
var
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
351
|
+
// implements/base.memory.ts
|
|
352
|
+
var MongoDBMemory = class _MongoDBMemory {
|
|
353
|
+
static instance;
|
|
354
|
+
uri;
|
|
355
|
+
connected = false;
|
|
356
|
+
reconnectAttempts = 0;
|
|
357
|
+
maxReconnectAttempts;
|
|
358
|
+
reconnectInterval;
|
|
359
|
+
reconnecting = false;
|
|
360
|
+
connectionConfig;
|
|
361
|
+
eventListenersSetup = false;
|
|
362
|
+
operationTimeoutMS;
|
|
363
|
+
agentMemory;
|
|
364
|
+
intentMemory;
|
|
365
|
+
threadMemory;
|
|
366
|
+
constructor(config) {
|
|
367
|
+
const cfg = typeof config === "string" ? { uri: config } : config;
|
|
368
|
+
this.uri = cfg.uri;
|
|
369
|
+
this.maxReconnectAttempts = cfg.maxReconnectAttempts ?? 5;
|
|
370
|
+
this.reconnectInterval = cfg.reconnectInterval ?? 5e3;
|
|
371
|
+
this.operationTimeoutMS = cfg.operationTimeoutMS ?? 1e4;
|
|
372
|
+
this.connectionConfig = {
|
|
373
|
+
maxPoolSize: cfg.maxPoolSize ?? 1,
|
|
374
|
+
serverSelectionTimeoutMS: cfg.serverSelectionTimeoutMS ?? 3e4,
|
|
375
|
+
socketTimeoutMS: cfg.socketTimeoutMS ?? 45e3,
|
|
376
|
+
connectTimeoutMS: cfg.connectTimeoutMS ?? 3e4,
|
|
377
|
+
bufferCommands: false
|
|
378
|
+
};
|
|
379
|
+
if (!_MongoDBMemory.instance) {
|
|
380
|
+
_MongoDBMemory.instance = this;
|
|
381
|
+
this.setupMongooseEventListeners();
|
|
382
|
+
} else {
|
|
383
|
+
this.connected = _MongoDBMemory.instance.connected;
|
|
384
|
+
this.operationTimeoutMS = _MongoDBMemory.instance.operationTimeoutMS;
|
|
290
385
|
}
|
|
386
|
+
this.agentMemory = new MongoDBAgent(
|
|
387
|
+
this.executeWithRetry.bind(this),
|
|
388
|
+
this.getOperationTimeout.bind(this)
|
|
389
|
+
);
|
|
390
|
+
this.threadMemory = new MongoDBThread(
|
|
391
|
+
this.executeWithRetry.bind(this),
|
|
392
|
+
this.getOperationTimeout.bind(this)
|
|
393
|
+
);
|
|
394
|
+
this.intentMemory = new MongoDBIntent(
|
|
395
|
+
this.executeWithRetry.bind(this),
|
|
396
|
+
this.getOperationTimeout.bind(this)
|
|
397
|
+
);
|
|
291
398
|
}
|
|
292
|
-
)
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
// implements/intent.memory.ts
|
|
296
|
-
var MongoDBIntent = class extends MongoDBMemory {
|
|
297
|
-
async getIntent(intentId) {
|
|
298
|
-
const intent = await IntentModel.findOne({ id: intentId }).lean();
|
|
299
|
-
return intent || void 0;
|
|
399
|
+
getAgentMemory() {
|
|
400
|
+
return this.agentMemory;
|
|
300
401
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
return intent || void 0;
|
|
402
|
+
getThreadMemory() {
|
|
403
|
+
return this.threadMemory;
|
|
304
404
|
}
|
|
305
|
-
|
|
306
|
-
|
|
405
|
+
getIntentMemory() {
|
|
406
|
+
return this.intentMemory;
|
|
307
407
|
}
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
408
|
+
setupMongooseEventListeners() {
|
|
409
|
+
if (this.eventListenersSetup) return;
|
|
410
|
+
this.eventListenersSetup = true;
|
|
411
|
+
import_mongoose8.default.connection.on("connected", () => {
|
|
412
|
+
this.connected = true;
|
|
413
|
+
this.reconnectAttempts = 0;
|
|
414
|
+
this.reconnecting = false;
|
|
415
|
+
import_logger2.loggers.agent.info("MongoDB connected successfully");
|
|
416
|
+
});
|
|
417
|
+
import_mongoose8.default.connection.on("disconnected", () => {
|
|
418
|
+
this.connected = false;
|
|
419
|
+
import_logger2.loggers.agent.warn("MongoDB disconnected");
|
|
420
|
+
this.handleDisconnection();
|
|
421
|
+
});
|
|
422
|
+
import_mongoose8.default.connection.on("error", (error) => {
|
|
423
|
+
this.connected = false;
|
|
424
|
+
import_logger2.loggers.agent.error("MongoDB connection error:", error);
|
|
425
|
+
this.handleDisconnection();
|
|
426
|
+
});
|
|
427
|
+
import_mongoose8.default.connection.on("reconnected", () => {
|
|
428
|
+
this.connected = true;
|
|
429
|
+
this.reconnectAttempts = 0;
|
|
430
|
+
this.reconnecting = false;
|
|
431
|
+
import_logger2.loggers.agent.info("MongoDB reconnected successfully");
|
|
432
|
+
});
|
|
312
433
|
}
|
|
313
|
-
async
|
|
314
|
-
|
|
434
|
+
async handleDisconnection() {
|
|
435
|
+
if (this.reconnecting) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
this.reconnecting = true;
|
|
439
|
+
while (this.reconnectAttempts < this.maxReconnectAttempts && !this.isConnected) {
|
|
440
|
+
this.reconnectAttempts++;
|
|
441
|
+
import_logger2.loggers.agent.info(
|
|
442
|
+
`Attempting to reconnect to MongoDB (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`
|
|
443
|
+
);
|
|
444
|
+
try {
|
|
445
|
+
await import_mongoose8.default.connect(this.uri, this.connectionConfig);
|
|
446
|
+
this.connected = true;
|
|
447
|
+
this.reconnectAttempts = 0;
|
|
448
|
+
this.reconnecting = false;
|
|
449
|
+
import_logger2.loggers.agent.info("MongoDB reconnection successful");
|
|
450
|
+
return;
|
|
451
|
+
} catch (error) {
|
|
452
|
+
import_logger2.loggers.agent.error(
|
|
453
|
+
`Reconnection attempt ${this.reconnectAttempts} failed:`,
|
|
454
|
+
error
|
|
455
|
+
);
|
|
456
|
+
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
|
457
|
+
await new Promise(
|
|
458
|
+
(resolve) => setTimeout(resolve, this.reconnectInterval)
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
this.reconnecting = false;
|
|
464
|
+
if (!this.isConnected) {
|
|
465
|
+
import_logger2.loggers.agent.error(
|
|
466
|
+
`Failed to reconnect to MongoDB after ${this.maxReconnectAttempts} attempts`
|
|
467
|
+
);
|
|
468
|
+
}
|
|
315
469
|
}
|
|
316
|
-
async
|
|
317
|
-
|
|
318
|
-
|
|
470
|
+
async connect() {
|
|
471
|
+
if (this.connected) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
try {
|
|
475
|
+
await import_mongoose8.default.connect(this.uri, this.connectionConfig);
|
|
476
|
+
this.connected = true;
|
|
477
|
+
this.reconnectAttempts = 0;
|
|
478
|
+
} catch (error) {
|
|
479
|
+
import_logger2.loggers.agent.error("Failed to connect to MongoDB:", error);
|
|
480
|
+
throw error;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
async disconnect() {
|
|
484
|
+
if (!this.isConnected) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
try {
|
|
488
|
+
await import_mongoose8.default.disconnect();
|
|
489
|
+
this.connected = false;
|
|
490
|
+
} catch (error) {
|
|
491
|
+
import_logger2.loggers.agent.error("Failed to disconnect from MongoDB:", error);
|
|
492
|
+
throw error;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
isConnected() {
|
|
496
|
+
return this.connected;
|
|
497
|
+
}
|
|
498
|
+
async ensureConnection() {
|
|
499
|
+
if (!this.isConnected && !this.reconnecting) {
|
|
500
|
+
await this.connect();
|
|
501
|
+
}
|
|
502
|
+
const maxWaitTime = 3e4;
|
|
503
|
+
const startTime = Date.now();
|
|
504
|
+
while (this.reconnecting && Date.now() - startTime < maxWaitTime) {
|
|
505
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
506
|
+
}
|
|
507
|
+
if (!this.isConnected) {
|
|
508
|
+
throw new Error("MongoDB is not connected and reconnection failed");
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Get the operation timeout in milliseconds
|
|
513
|
+
*/
|
|
514
|
+
getOperationTimeout() {
|
|
515
|
+
return this.operationTimeoutMS;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Execute a database operation with automatic retry on connection errors
|
|
519
|
+
* Note: Use mongoose's maxTimeMS option in queries for timeout control
|
|
520
|
+
*/
|
|
521
|
+
async executeWithRetry(operation, operationName = "Database operation") {
|
|
522
|
+
await this.ensureConnection();
|
|
523
|
+
try {
|
|
524
|
+
return await operation();
|
|
525
|
+
} catch (error) {
|
|
526
|
+
if (error.code === 50 || error.message?.includes("operation exceeded time limit")) {
|
|
527
|
+
import_logger2.loggers.agent.error(`${operationName} exceeded time limit`);
|
|
528
|
+
throw error;
|
|
529
|
+
}
|
|
530
|
+
if (error.name === "MongoNetworkError" || error.name === "MongoServerError" || error.message?.includes("connection") || error.message?.includes("disconnect")) {
|
|
531
|
+
import_logger2.loggers.agent.warn(
|
|
532
|
+
`${operationName} failed due to connection issue, attempting reconnection...`
|
|
533
|
+
);
|
|
534
|
+
await this.ensureConnection();
|
|
535
|
+
try {
|
|
536
|
+
return await operation();
|
|
537
|
+
} catch (retryError) {
|
|
538
|
+
import_logger2.loggers.agent.error(`${operationName} failed after retry:`, retryError);
|
|
539
|
+
throw retryError;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
throw error;
|
|
543
|
+
}
|
|
319
544
|
}
|
|
320
545
|
};
|
|
321
546
|
// Annotate the CommonJS export names for ESM import in node:
|
|
322
547
|
0 && (module.exports = {
|
|
323
|
-
|
|
324
|
-
MongoDBThread
|
|
548
|
+
MongoDBMemory
|
|
325
549
|
});
|
|
326
550
|
//# sourceMappingURL=index.cjs.map
|