@ainetwork/adk-provider-memory-mongodb 0.2.4 → 0.3.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/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,23 @@
|
|
|
1
|
+
import mongoose, { Schema, Document } from 'mongoose';
|
|
2
|
+
|
|
3
|
+
declare const AgentObjectSchema: Schema<any, mongoose.Model<any, any, any, any, any, any>, {}, {}, {}, {}, mongoose.DefaultSchemaOptions, {
|
|
4
|
+
agent_prompt?: string | null | undefined;
|
|
5
|
+
}, Document<unknown, {}, mongoose.FlatRecord<{
|
|
6
|
+
agent_prompt?: string | null | undefined;
|
|
7
|
+
}>, {}> & mongoose.FlatRecord<{
|
|
8
|
+
agent_prompt?: string | null | undefined;
|
|
9
|
+
}> & {
|
|
10
|
+
_id: mongoose.Types.ObjectId;
|
|
11
|
+
} & {
|
|
12
|
+
__v: number;
|
|
13
|
+
}>;
|
|
14
|
+
interface AgentDocument extends Document {
|
|
15
|
+
agent_prompt: string;
|
|
16
|
+
}
|
|
17
|
+
declare const AgentModel: mongoose.Model<AgentDocument, {}, {}, {}, Document<unknown, {}, AgentDocument, {}> & AgentDocument & Required<{
|
|
18
|
+
_id: unknown;
|
|
19
|
+
}> & {
|
|
20
|
+
__v: number;
|
|
21
|
+
}, any>;
|
|
22
|
+
|
|
23
|
+
export { type AgentDocument, AgentModel, AgentObjectSchema };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { IAgentMemory } from "@ainetwork/adk/modules";
|
|
2
|
+
import { AgentModel } from "../models/agent.model";
|
|
3
|
+
|
|
4
|
+
export type ExecuteWithRetryFn = <T>(
|
|
5
|
+
operation: () => Promise<T>,
|
|
6
|
+
operationName?: string
|
|
7
|
+
) => Promise<T>;
|
|
8
|
+
|
|
9
|
+
export type GetOperationTimeoutFn = () => number;
|
|
10
|
+
|
|
11
|
+
type AgentMetadata = {
|
|
12
|
+
agent_prompt: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class MongoDBAgent implements IAgentMemory {
|
|
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;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public async getAgentPrompt(): Promise<string> {
|
|
28
|
+
return this.executeWithRetry(async () => {
|
|
29
|
+
const timeout = this.getOperationTimeout();
|
|
30
|
+
const metadata = await AgentModel.findOne({
|
|
31
|
+
id: "agent_metadata"
|
|
32
|
+
}).maxTimeMS(timeout)
|
|
33
|
+
.lean<AgentMetadata>();
|
|
34
|
+
return metadata?.agent_prompt || "";
|
|
35
|
+
}, "getAgentPrompt()");
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
public async updateAgentPrompt(prompt: string): Promise<void> {
|
|
39
|
+
return this.executeWithRetry(async () => {
|
|
40
|
+
const timeout = this.getOperationTimeout();
|
|
41
|
+
await AgentModel.updateOne({
|
|
42
|
+
id: "agent_metadata",
|
|
43
|
+
}, { "agent_prompt": prompt }).maxTimeMS(timeout);
|
|
44
|
+
}, "updateAgentPrompt()");
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { IMemory } from "node_modules/@ainetwork/adk/dist/esm/modules/memory/base.memory";
|
|
1
|
+
import { IAgentMemory, IIntentMemory, IMemory, IThreadMemory } from "node_modules/@ainetwork/adk/dist/esm/modules/memory/base.memory";
|
|
2
2
|
import mongoose from "mongoose";
|
|
3
3
|
import { loggers } from "@ainetwork/adk/utils/logger";
|
|
4
|
+
import { MongoDBAgent } from "./agent.memory";
|
|
5
|
+
import { MongoDBIntent } from "./intent.memory";
|
|
6
|
+
import { MongoDBThread } from "./thread.memory";
|
|
4
7
|
|
|
5
8
|
export interface MongoDBMemoryConfig {
|
|
6
9
|
uri: string;
|
|
@@ -25,6 +28,10 @@ export class MongoDBMemory implements IMemory {
|
|
|
25
28
|
private eventListenersSetup: boolean = false;
|
|
26
29
|
private operationTimeoutMS: number;
|
|
27
30
|
|
|
31
|
+
private agentMemory: MongoDBAgent;
|
|
32
|
+
private intentMemory: MongoDBIntent;
|
|
33
|
+
private threadMemory: MongoDBThread;
|
|
34
|
+
|
|
28
35
|
constructor(config: string | MongoDBMemoryConfig) {
|
|
29
36
|
const cfg = typeof config === 'string' ? { uri: config } : config;
|
|
30
37
|
|
|
@@ -48,6 +55,33 @@ export class MongoDBMemory implements IMemory {
|
|
|
48
55
|
this.connected = MongoDBMemory.instance.connected;
|
|
49
56
|
this.operationTimeoutMS = MongoDBMemory.instance.operationTimeoutMS;
|
|
50
57
|
}
|
|
58
|
+
|
|
59
|
+
this.agentMemory = new MongoDBAgent(
|
|
60
|
+
this.executeWithRetry.bind(this),
|
|
61
|
+
this.getOperationTimeout.bind(this)
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
this.threadMemory = new MongoDBThread(
|
|
65
|
+
this.executeWithRetry.bind(this),
|
|
66
|
+
this.getOperationTimeout.bind(this)
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
this.intentMemory = new MongoDBIntent(
|
|
70
|
+
this.executeWithRetry.bind(this),
|
|
71
|
+
this.getOperationTimeout.bind(this)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
public getAgentMemory(): IAgentMemory {
|
|
76
|
+
return this.agentMemory;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public getThreadMemory(): IThreadMemory {
|
|
80
|
+
return this.threadMemory;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public getIntentMemory(): IIntentMemory {
|
|
84
|
+
return this.intentMemory;
|
|
51
85
|
}
|
|
52
86
|
|
|
53
87
|
private setupMongooseEventListeners(): void {
|
|
@@ -1,9 +1,26 @@
|
|
|
1
1
|
import type { Intent } from "@ainetwork/adk/types/memory";
|
|
2
2
|
import { IIntentMemory } from "@ainetwork/adk/modules";
|
|
3
|
-
import { MongoDBMemory } from "./base.memory";
|
|
4
3
|
import { IntentModel } from "../models/intent.model";
|
|
5
4
|
|
|
6
|
-
export
|
|
5
|
+
export type ExecuteWithRetryFn = <T>(
|
|
6
|
+
operation: () => Promise<T>,
|
|
7
|
+
operationName?: string
|
|
8
|
+
) => Promise<T>;
|
|
9
|
+
|
|
10
|
+
export type GetOperationTimeoutFn = () => number;
|
|
11
|
+
|
|
12
|
+
export class MongoDBIntent implements IIntentMemory {
|
|
13
|
+
private executeWithRetry: ExecuteWithRetryFn;
|
|
14
|
+
private getOperationTimeout: GetOperationTimeoutFn;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
executeWithRetry: ExecuteWithRetryFn,
|
|
18
|
+
getOperationTimeout: GetOperationTimeoutFn
|
|
19
|
+
) {
|
|
20
|
+
this.executeWithRetry = executeWithRetry;
|
|
21
|
+
this.getOperationTimeout = getOperationTimeout;
|
|
22
|
+
}
|
|
23
|
+
|
|
7
24
|
public async getIntent(intentId: string): Promise<Intent | undefined> {
|
|
8
25
|
return this.executeWithRetry(async () => {
|
|
9
26
|
const timeout = this.getOperationTimeout();
|
|
@@ -1,14 +1,27 @@
|
|
|
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(
|
package/index.ts
CHANGED
|
@@ -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
|
+
"version": "0.3.2",
|
|
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
|
|
31
|
+
"@ainetwork/adk": "^0.3.2",
|
|
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": "4bfa5afae29304e6cb5f106c7327f5f2092f6601"
|
|
42
42
|
}
|