@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.
@@ -1,115 +1,141 @@
1
1
  import type { MessageObject, ThreadMetadata, ThreadObject, ThreadType } from "@ainetwork/adk/types/memory";
2
2
  import { MessageRole } from "@ainetwork/adk/types/memory";
3
3
  import { IThreadMemory } from "@ainetwork/adk/modules";
4
- import { MongoDBMemory } from "./base.memory";
5
4
  import { ThreadDocument, ThreadModel } from "../models/threads.model";
6
5
  import { MessageDocument, MessageModel } from "../models/messages.model";
7
6
  import { loggers } from "@ainetwork/adk/utils/logger";
8
7
 
9
- export class MongoDBThread extends MongoDBMemory implements IThreadMemory {
10
- constructor(uri: string) {
11
- super(uri);
8
+ export type ExecuteWithRetryFn = <T>(
9
+ operation: () => Promise<T>,
10
+ operationName?: string
11
+ ) => Promise<T>;
12
+
13
+ export type GetOperationTimeoutFn = () => number;
14
+
15
+ export class MongoDBThread implements IThreadMemory {
16
+ private executeWithRetry: ExecuteWithRetryFn;
17
+ private getOperationTimeout: GetOperationTimeoutFn;
18
+
19
+ constructor(
20
+ executeWithRetry: ExecuteWithRetryFn,
21
+ getOperationTimeout: GetOperationTimeoutFn
22
+ ) {
23
+ this.executeWithRetry = executeWithRetry;
24
+ this.getOperationTimeout = getOperationTimeout;
12
25
  }
13
26
 
14
27
  public async getThread(
15
28
  userId: string,
16
29
  threadId: string
17
30
  ): Promise<ThreadObject | undefined> {
18
- const thread = await ThreadModel.findOne({ threadId, userId });
19
- const messages = await MessageModel.find({ threadId, userId }).sort({
20
- timestamp: 1,
21
- });
22
-
23
- if (!thread) return undefined;
24
-
25
- loggers.agent.debug(`Found ${messages.length} messages for thread ${threadId}`);
26
-
27
- const threadObject: ThreadObject = {
28
- threadId: thread.threadId,
29
- userId: thread.userId,
30
- type: thread.type as ThreadType,
31
- title: thread.title || "New thread",
32
- messages: []
33
- };
34
- messages.forEach((message: MessageDocument) => {
35
- threadObject.messages.push({
36
- messageId: message.messageId,
37
- role: message.role as MessageRole,
38
- content: message.content,
39
- timestamp: message.timestamp,
40
- metadata: message.metadata,
41
- });
42
- });
43
-
44
- return threadObject;
31
+ return this.executeWithRetry(async () => {
32
+ const timeout = this.getOperationTimeout();
33
+ const thread = await ThreadModel.findOne({ threadId, userId }).maxTimeMS(timeout);
34
+ const messages = await MessageModel.find({ threadId, userId })
35
+ .sort({ timestamp: 1 })
36
+ .maxTimeMS(timeout);
37
+
38
+ if (!thread) return undefined;
39
+
40
+ loggers.agent.debug(`Found ${messages.length} messages for thread ${threadId}`);
41
+
42
+ const threadObject: ThreadObject = {
43
+ threadId: thread.threadId,
44
+ userId: thread.userId,
45
+ type: thread.type as ThreadType,
46
+ title: thread.title || "New thread",
47
+ messages: []
48
+ };
49
+ messages.forEach((message: MessageDocument) => {
50
+ threadObject.messages.push({
51
+ messageId: message.messageId,
52
+ role: message.role as MessageRole,
53
+ content: message.content,
54
+ timestamp: message.timestamp,
55
+ metadata: message.metadata,
56
+ });
57
+ });
58
+
59
+ return threadObject;
60
+ }, `getThread(${userId}, ${threadId})`);
45
61
  };
46
62
 
47
- public async createThread(
48
- type: ThreadType,
49
- userId: string,
50
- threadId: string,
51
- title: string,
63
+ public async createThread(
64
+ type: ThreadType,
65
+ userId: string,
66
+ threadId: string,
67
+ title: string,
52
68
  ): Promise<ThreadObject> {
53
- const now = Date.now();
54
- await ThreadModel.create({
55
- type,
56
- userId,
57
- threadId,
58
- title,
59
- updated_at: now,
60
- created_at: now,
61
- });
62
-
63
- return { type, userId, threadId, title, messages: []};
69
+ return this.executeWithRetry(async () => {
70
+ const now = Date.now();
71
+ await ThreadModel.create({
72
+ type,
73
+ userId,
74
+ threadId,
75
+ title,
76
+ updated_at: now,
77
+ created_at: now,
78
+ });
79
+
80
+ return { type, userId, threadId, title, messages: []};
81
+ }, `createThread(${userId}, ${threadId})`);
64
82
  };
65
83
 
66
- public async addMessagesToThread(
84
+ public async addMessagesToThread(
67
85
  userId: string,
68
86
  threadId: string,
69
87
  messages: MessageObject[]
70
88
  ): Promise<void> {
71
- await ThreadModel.updateOne({ threadId, userId }, {
72
- updated_at: Date.now(),
73
- });
74
- for (const message of messages) {
75
- await MessageModel.create({
76
- threadId,
77
- messageId: message.messageId,
78
- userId,
79
- role: message.role,
80
- content: message.content,
81
- timestamp: message.timestamp,
82
- metadata: message.metadata,
89
+ return this.executeWithRetry(async () => {
90
+ await ThreadModel.updateOne({ threadId, userId }, {
91
+ updated_at: Date.now(),
83
92
  });
84
- }
93
+ for (const message of messages) {
94
+ await MessageModel.create({
95
+ threadId,
96
+ messageId: message.messageId,
97
+ userId,
98
+ role: message.role,
99
+ content: message.content,
100
+ timestamp: message.timestamp,
101
+ metadata: message.metadata,
102
+ });
103
+ }
104
+ }, `addMessagesToThread(${userId}, ${threadId})`);
85
105
  };
86
106
 
87
- public async deleteThread(userId: string, threadId: string): Promise<void> {
88
- const messages = await MessageModel.find({ userId, threadId }).sort({
89
- timestamp: 1,
90
- });
91
-
92
- messages?.forEach((message: MessageDocument) => {
93
- message.deleteOne();
94
- });
95
-
96
- const thread = await ThreadModel.findOne({ userId, threadId });
97
- thread?.deleteOne();
107
+ public async deleteThread(userId: string, threadId: string): Promise<void> {
108
+ return this.executeWithRetry(async () => {
109
+ const timeout = this.getOperationTimeout();
110
+ const messages = await MessageModel.find({ userId, threadId })
111
+ .sort({ timestamp: 1 })
112
+ .maxTimeMS(timeout);
113
+
114
+ messages?.forEach((message: MessageDocument) => {
115
+ message.deleteOne();
116
+ });
117
+
118
+ const thread = await ThreadModel.findOne({ userId, threadId }).maxTimeMS(timeout);
119
+ thread?.deleteOne();
120
+ }, `deleteThread(${userId}, ${threadId})`);
98
121
  };
99
122
 
100
- public async listThreads(userId: string): Promise<ThreadMetadata[]> {
101
- const threads = await ThreadModel.find({ userId }).sort({
102
- updated_at: -1,
103
- });
104
- const data: ThreadMetadata[] = threads.map((thread: ThreadDocument) => {
105
- return {
106
- type: thread.type,
107
- userId,
108
- threadId: thread.threadId,
109
- title: thread.title,
110
- updatedAt: thread.updated_at
111
- } as ThreadMetadata;
112
- })
113
- return data;
123
+ public async listThreads(userId: string): Promise<ThreadMetadata[]> {
124
+ return this.executeWithRetry(async () => {
125
+ const timeout = this.getOperationTimeout();
126
+ const threads = await ThreadModel.find({ userId })
127
+ .sort({ updated_at: -1 })
128
+ .maxTimeMS(timeout);
129
+ const data: ThreadMetadata[] = threads.map((thread: ThreadDocument) => {
130
+ return {
131
+ type: thread.type,
132
+ userId,
133
+ threadId: thread.threadId,
134
+ title: thread.title,
135
+ updatedAt: thread.updated_at
136
+ } as ThreadMetadata;
137
+ })
138
+ return data;
139
+ }, `listThreads(${userId})`);
114
140
  };
115
- }
141
+ }
package/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { MongoDBThread } from "./implements/thread.memory";
2
- export { MongoDBIntent } from "./implements/intent.memory";
1
+ export { MongoDBMemory } from "./implements/base.memory";
2
+ export type { MongoDBMemoryConfig } from "./implements/base.memory";
@@ -0,0 +1,16 @@
1
+ import { type Document, Schema } from "mongoose";
2
+ import mongoose from "mongoose";
3
+
4
+ export const AgentObjectSchema = new Schema(
5
+ {
6
+ agent_prompt: {
7
+ type: String,
8
+ },
9
+ },
10
+ );
11
+
12
+ export interface AgentDocument extends Document {
13
+ agent_prompt: string;
14
+ }
15
+
16
+ export const AgentModel = mongoose.model<AgentDocument>("Agent", AgentObjectSchema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ainetwork/adk-provider-memory-mongodb",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "author": "AI Network (https://ainetwork.ai)",
5
5
  "type": "module",
6
6
  "engines": {
@@ -28,7 +28,7 @@
28
28
  "clean": "rm -rf dist"
29
29
  },
30
30
  "dependencies": {
31
- "@ainetwork/adk": "0.2.9",
31
+ "@ainetwork/adk": "^0.3.0",
32
32
  "mongoose": "^8.16.5"
33
33
  },
34
34
  "devDependencies": {
@@ -38,5 +38,5 @@
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  },
41
- "gitHead": "da5af7747935eeb8cedeb932cb2e918da6dc36c3"
41
+ "gitHead": "77a4f8c8bb228f45383eea93f153c21bac3f8b3b"
42
42
  }