@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
|
@@ -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
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
63
|
+
public async createThread(
|
|
64
|
+
type: ThreadType,
|
|
65
|
+
userId: string,
|
|
66
|
+
threadId: string,
|
|
67
|
+
title: string,
|
|
52
68
|
): Promise<ThreadObject> {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
84
|
+
public async addMessagesToThread(
|
|
67
85
|
userId: string,
|
|
68
86
|
threadId: string,
|
|
69
87
|
messages: MessageObject[]
|
|
70
88
|
): Promise<void> {
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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 {
|
|
2
|
-
export {
|
|
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.
|
|
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.
|
|
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": "
|
|
41
|
+
"gitHead": "77a4f8c8bb228f45383eea93f153c21bac3f8b3b"
|
|
42
42
|
}
|