@ainetwork/adk-provider-memory-mongodb 0.2.4 → 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 +325 -255
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -22
- package/dist/index.d.ts +8 -22
- package/dist/index.js +216 -153
- 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 +35 -1
- package/implements/intent.memory.ts +19 -2
- package/implements/thread.memory.ts +17 -4
- package/index.ts +0 -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,191 +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
|
-
MongoDBMemory: () => MongoDBMemory,
|
|
35
|
-
MongoDBThread: () => MongoDBThread
|
|
33
|
+
MongoDBMemory: () => MongoDBMemory
|
|
36
34
|
});
|
|
37
35
|
module.exports = __toCommonJS(index_exports);
|
|
38
36
|
|
|
39
37
|
// implements/base.memory.ts
|
|
40
|
-
var
|
|
41
|
-
var
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
connectionConfig;
|
|
51
|
-
eventListenersSetup = false;
|
|
52
|
-
operationTimeoutMS;
|
|
53
|
-
constructor(config) {
|
|
54
|
-
const cfg = typeof config === "string" ? { uri: config } : config;
|
|
55
|
-
this.uri = cfg.uri;
|
|
56
|
-
this.maxReconnectAttempts = cfg.maxReconnectAttempts ?? 5;
|
|
57
|
-
this.reconnectInterval = cfg.reconnectInterval ?? 5e3;
|
|
58
|
-
this.operationTimeoutMS = cfg.operationTimeoutMS ?? 1e4;
|
|
59
|
-
this.connectionConfig = {
|
|
60
|
-
maxPoolSize: cfg.maxPoolSize ?? 1,
|
|
61
|
-
serverSelectionTimeoutMS: cfg.serverSelectionTimeoutMS ?? 3e4,
|
|
62
|
-
socketTimeoutMS: cfg.socketTimeoutMS ?? 45e3,
|
|
63
|
-
connectTimeoutMS: cfg.connectTimeoutMS ?? 3e4,
|
|
64
|
-
bufferCommands: false
|
|
65
|
-
};
|
|
66
|
-
if (!_MongoDBMemory.instance) {
|
|
67
|
-
_MongoDBMemory.instance = this;
|
|
68
|
-
this.setupMongooseEventListeners();
|
|
69
|
-
} else {
|
|
70
|
-
this.connected = _MongoDBMemory.instance.connected;
|
|
71
|
-
this.operationTimeoutMS = _MongoDBMemory.instance.operationTimeoutMS;
|
|
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
|
|
72
48
|
}
|
|
73
49
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.connected = false;
|
|
85
|
-
import_logger.loggers.agent.warn("MongoDB disconnected");
|
|
86
|
-
this.handleDisconnection();
|
|
87
|
-
});
|
|
88
|
-
import_mongoose.default.connection.on("error", (error) => {
|
|
89
|
-
this.connected = false;
|
|
90
|
-
import_logger.loggers.agent.error("MongoDB connection error:", error);
|
|
91
|
-
this.handleDisconnection();
|
|
92
|
-
});
|
|
93
|
-
import_mongoose.default.connection.on("reconnected", () => {
|
|
94
|
-
this.connected = true;
|
|
95
|
-
this.reconnectAttempts = 0;
|
|
96
|
-
this.reconnecting = false;
|
|
97
|
-
import_logger.loggers.agent.info("MongoDB reconnected successfully");
|
|
98
|
-
});
|
|
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;
|
|
99
60
|
}
|
|
100
|
-
async
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
`Attempting to reconnect to MongoDB (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`
|
|
109
|
-
);
|
|
110
|
-
try {
|
|
111
|
-
await import_mongoose.default.connect(this.uri, this.connectionConfig);
|
|
112
|
-
this.connected = true;
|
|
113
|
-
this.reconnectAttempts = 0;
|
|
114
|
-
this.reconnecting = false;
|
|
115
|
-
import_logger.loggers.agent.info("MongoDB reconnection successful");
|
|
116
|
-
return;
|
|
117
|
-
} catch (error) {
|
|
118
|
-
import_logger.loggers.agent.error(
|
|
119
|
-
`Reconnection attempt ${this.reconnectAttempts} failed:`,
|
|
120
|
-
error
|
|
121
|
-
);
|
|
122
|
-
if (this.reconnectAttempts < this.maxReconnectAttempts) {
|
|
123
|
-
await new Promise(
|
|
124
|
-
(resolve) => setTimeout(resolve, this.reconnectInterval)
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
this.reconnecting = false;
|
|
130
|
-
if (!this.isConnected) {
|
|
131
|
-
import_logger.loggers.agent.error(
|
|
132
|
-
`Failed to reconnect to MongoDB after ${this.maxReconnectAttempts} attempts`
|
|
133
|
-
);
|
|
134
|
-
}
|
|
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()");
|
|
135
69
|
}
|
|
136
|
-
async
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
this.reconnectAttempts = 0;
|
|
144
|
-
} catch (error) {
|
|
145
|
-
import_logger.loggers.agent.error("Failed to connect to MongoDB:", error);
|
|
146
|
-
throw error;
|
|
147
|
-
}
|
|
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()");
|
|
148
77
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
|
159
114
|
}
|
|
160
115
|
}
|
|
161
|
-
|
|
162
|
-
|
|
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;
|
|
163
126
|
}
|
|
164
|
-
async
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
while (this.reconnecting && Date.now() - startTime < maxWaitTime) {
|
|
171
|
-
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
172
|
-
}
|
|
173
|
-
if (!this.isConnected) {
|
|
174
|
-
throw new Error("MongoDB is not connected and reconnection failed");
|
|
175
|
-
}
|
|
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})`);
|
|
176
133
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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})`);
|
|
182
140
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
throw error;
|
|
209
|
-
}
|
|
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()`);
|
|
210
166
|
}
|
|
211
167
|
};
|
|
212
168
|
|
|
213
169
|
// models/threads.model.ts
|
|
214
170
|
var import_memory = require("@ainetwork/adk/types/memory");
|
|
215
|
-
var
|
|
216
|
-
var
|
|
217
|
-
var ThreadObjectSchema = new
|
|
171
|
+
var import_mongoose4 = require("mongoose");
|
|
172
|
+
var import_mongoose5 = __toESM(require("mongoose"), 1);
|
|
173
|
+
var ThreadObjectSchema = new import_mongoose4.Schema(
|
|
218
174
|
{
|
|
219
175
|
type: {
|
|
220
176
|
type: String,
|
|
@@ -245,20 +201,20 @@ var ThreadObjectSchema = new import_mongoose2.Schema(
|
|
|
245
201
|
}
|
|
246
202
|
}
|
|
247
203
|
);
|
|
248
|
-
var ThreadModel =
|
|
204
|
+
var ThreadModel = import_mongoose5.default.model("Thread", ThreadObjectSchema);
|
|
249
205
|
|
|
250
206
|
// models/messages.model.ts
|
|
251
207
|
var import_memory2 = require("@ainetwork/adk/types/memory");
|
|
252
|
-
var
|
|
253
|
-
var
|
|
254
|
-
var MessageContentObjectSchema = new
|
|
208
|
+
var import_mongoose6 = require("mongoose");
|
|
209
|
+
var import_mongoose7 = __toESM(require("mongoose"), 1);
|
|
210
|
+
var MessageContentObjectSchema = new import_mongoose6.Schema(
|
|
255
211
|
{
|
|
256
212
|
type: { type: String, required: true },
|
|
257
|
-
parts: { type: [
|
|
213
|
+
parts: { type: [import_mongoose6.Schema.Types.Mixed], required: true }
|
|
258
214
|
},
|
|
259
215
|
{ _id: false }
|
|
260
216
|
);
|
|
261
|
-
var MessageObjectSchema = new
|
|
217
|
+
var MessageObjectSchema = new import_mongoose6.Schema(
|
|
262
218
|
{
|
|
263
219
|
messageId: {
|
|
264
220
|
type: String,
|
|
@@ -289,18 +245,21 @@ var MessageObjectSchema = new import_mongoose4.Schema(
|
|
|
289
245
|
required: true
|
|
290
246
|
},
|
|
291
247
|
metadata: {
|
|
292
|
-
type:
|
|
248
|
+
type: import_mongoose6.Schema.Types.Mixed,
|
|
293
249
|
default: {}
|
|
294
250
|
}
|
|
295
251
|
}
|
|
296
252
|
);
|
|
297
|
-
var MessageModel =
|
|
253
|
+
var MessageModel = import_mongoose7.default.model("Message", MessageObjectSchema);
|
|
298
254
|
|
|
299
255
|
// implements/thread.memory.ts
|
|
300
|
-
var
|
|
301
|
-
var MongoDBThread = class
|
|
302
|
-
|
|
303
|
-
|
|
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;
|
|
304
263
|
}
|
|
305
264
|
async getThread(userId, threadId) {
|
|
306
265
|
return this.executeWithRetry(async () => {
|
|
@@ -308,7 +267,7 @@ var MongoDBThread = class extends MongoDBMemory {
|
|
|
308
267
|
const thread = await ThreadModel.findOne({ threadId, userId }).maxTimeMS(timeout);
|
|
309
268
|
const messages = await MessageModel.find({ threadId, userId }).sort({ timestamp: 1 }).maxTimeMS(timeout);
|
|
310
269
|
if (!thread) return void 0;
|
|
311
|
-
|
|
270
|
+
import_logger.loggers.agent.debug(`Found ${messages.length} messages for thread ${threadId}`);
|
|
312
271
|
const threadObject = {
|
|
313
272
|
threadId: thread.threadId,
|
|
314
273
|
userId: thread.userId,
|
|
@@ -389,92 +348,203 @@ var MongoDBThread = class extends MongoDBMemory {
|
|
|
389
348
|
}
|
|
390
349
|
};
|
|
391
350
|
|
|
392
|
-
//
|
|
393
|
-
var
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
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;
|
|
426
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
|
+
);
|
|
427
398
|
}
|
|
428
|
-
)
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
// implements/intent.memory.ts
|
|
432
|
-
var MongoDBIntent = class extends MongoDBMemory {
|
|
433
|
-
async getIntent(intentId) {
|
|
434
|
-
return this.executeWithRetry(async () => {
|
|
435
|
-
const timeout = this.getOperationTimeout();
|
|
436
|
-
const intent = await IntentModel.findOne({ id: intentId }).maxTimeMS(timeout).lean();
|
|
437
|
-
return intent || void 0;
|
|
438
|
-
}, `getIntent(${intentId})`);
|
|
399
|
+
getAgentMemory() {
|
|
400
|
+
return this.agentMemory;
|
|
439
401
|
}
|
|
440
|
-
|
|
441
|
-
return this.
|
|
442
|
-
const timeout = this.getOperationTimeout();
|
|
443
|
-
const intent = await IntentModel.findOne({ name: intentName }).maxTimeMS(timeout).lean();
|
|
444
|
-
return intent || void 0;
|
|
445
|
-
}, `getIntentByName(${intentName})`);
|
|
402
|
+
getThreadMemory() {
|
|
403
|
+
return this.threadMemory;
|
|
446
404
|
}
|
|
447
|
-
|
|
448
|
-
return this.
|
|
449
|
-
await IntentModel.create(intent);
|
|
450
|
-
}, `saveIntent(${intent.id})`);
|
|
405
|
+
getIntentMemory() {
|
|
406
|
+
return this.intentMemory;
|
|
451
407
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
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
|
+
});
|
|
459
433
|
}
|
|
460
|
-
async
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
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
|
+
}
|
|
465
469
|
}
|
|
466
|
-
async
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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
|
+
}
|
|
472
544
|
}
|
|
473
545
|
};
|
|
474
546
|
// Annotate the CommonJS export names for ESM import in node:
|
|
475
547
|
0 && (module.exports = {
|
|
476
|
-
|
|
477
|
-
MongoDBMemory,
|
|
478
|
-
MongoDBThread
|
|
548
|
+
MongoDBMemory
|
|
479
549
|
});
|
|
480
550
|
//# sourceMappingURL=index.cjs.map
|