@ainetwork/adk-provider-memory-mongodb 0.6.2 → 0.7.1

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.
@@ -25,12 +25,12 @@ declare const MessageObjectSchema: Schema<any, mongoose.Model<any, any, any, any
25
25
  } & {
26
26
  threadId: string;
27
27
  userId: string;
28
- messageId: string;
29
- role: MessageRole;
30
28
  content: {
31
29
  type: string;
32
30
  parts: any[];
33
31
  };
32
+ messageId: string;
33
+ role: MessageRole;
34
34
  timestamp: number;
35
35
  metadata: any;
36
36
  }, Document<unknown, {}, mongoose.FlatRecord<{
@@ -39,12 +39,12 @@ declare const MessageObjectSchema: Schema<any, mongoose.Model<any, any, any, any
39
39
  } & {
40
40
  threadId: string;
41
41
  userId: string;
42
- messageId: string;
43
- role: MessageRole;
44
42
  content: {
45
43
  type: string;
46
44
  parts: any[];
47
45
  };
46
+ messageId: string;
47
+ role: MessageRole;
48
48
  timestamp: number;
49
49
  metadata: any;
50
50
  }>, {}> & mongoose.FlatRecord<{
@@ -53,12 +53,12 @@ declare const MessageObjectSchema: Schema<any, mongoose.Model<any, any, any, any
53
53
  } & {
54
54
  threadId: string;
55
55
  userId: string;
56
- messageId: string;
57
- role: MessageRole;
58
56
  content: {
59
57
  type: string;
60
58
  parts: any[];
61
59
  };
60
+ messageId: string;
61
+ role: MessageRole;
62
62
  timestamp: number;
63
63
  metadata: any;
64
64
  }> & {
@@ -7,8 +7,8 @@ declare const WorkflowTemplateObjectSchema: Schema<any, mongoose.Model<any, any,
7
7
  createdAt: NativeDate;
8
8
  updatedAt: NativeDate;
9
9
  } & {
10
- description: string;
11
10
  title: string;
11
+ description: string;
12
12
  content: string;
13
13
  active: boolean;
14
14
  templateId: string;
@@ -19,8 +19,8 @@ declare const WorkflowTemplateObjectSchema: Schema<any, mongoose.Model<any, any,
19
19
  createdAt: NativeDate;
20
20
  updatedAt: NativeDate;
21
21
  } & {
22
- description: string;
23
22
  title: string;
23
+ description: string;
24
24
  content: string;
25
25
  active: boolean;
26
26
  templateId: string;
@@ -31,8 +31,8 @@ declare const WorkflowTemplateObjectSchema: Schema<any, mongoose.Model<any, any,
31
31
  createdAt: NativeDate;
32
32
  updatedAt: NativeDate;
33
33
  } & {
34
- description: string;
35
34
  title: string;
35
+ description: string;
36
36
  content: string;
37
37
  active: boolean;
38
38
  templateId: string;
@@ -7,8 +7,8 @@ declare const WorkflowTemplateObjectSchema: Schema<any, mongoose.Model<any, any,
7
7
  createdAt: NativeDate;
8
8
  updatedAt: NativeDate;
9
9
  } & {
10
- description: string;
11
10
  title: string;
11
+ description: string;
12
12
  content: string;
13
13
  active: boolean;
14
14
  templateId: string;
@@ -19,8 +19,8 @@ declare const WorkflowTemplateObjectSchema: Schema<any, mongoose.Model<any, any,
19
19
  createdAt: NativeDate;
20
20
  updatedAt: NativeDate;
21
21
  } & {
22
- description: string;
23
22
  title: string;
23
+ description: string;
24
24
  content: string;
25
25
  active: boolean;
26
26
  templateId: string;
@@ -31,8 +31,8 @@ declare const WorkflowTemplateObjectSchema: Schema<any, mongoose.Model<any, any,
31
31
  createdAt: NativeDate;
32
32
  updatedAt: NativeDate;
33
33
  } & {
34
- description: string;
35
34
  title: string;
35
+ description: string;
36
36
  content: string;
37
37
  active: boolean;
38
38
  templateId: string;
@@ -1,7 +1,8 @@
1
- import { IAgentMemory, IIntentMemory, IMemory, IUserWorkflowMemory, IThreadMemory, IWorkflowTemplateMemory } from "@ainetwork/adk/modules";
1
+ import { IAgentMemory, IDocumentMemory, IIntentMemory, IMemory, IUserWorkflowMemory, IThreadMemory, IWorkflowTemplateMemory } from "@ainetwork/adk/modules";
2
2
  import mongoose from "mongoose";
3
3
  import { loggers } from "@ainetwork/adk/utils/logger";
4
4
  import { MongoDBAgent } from "./agent.memory";
5
+ import { MongoDBDocument } from "./document.memory";
5
6
  import { MongoDBIntent } from "./intent.memory";
6
7
  import { MongoDBThread } from "./thread.memory";
7
8
  import { MongoDBUserWorkflow } from "./user-workflow.memory";
@@ -39,6 +40,7 @@ export class MongoDBMemory implements IMemory {
39
40
  private threadMemory: MongoDBThread;
40
41
  private workflowTemplateMemory: MongoDBWorkflowTemplate;
41
42
  private userWorkflowMemory: MongoDBUserWorkflow;
43
+ private documentMemory: MongoDBDocument;
42
44
 
43
45
  constructor(config: string | MongoDBMemoryConfig) {
44
46
  const cfg = typeof config === 'string' ? { uri: config } : config;
@@ -93,6 +95,11 @@ export class MongoDBMemory implements IMemory {
93
95
  this.executeWithRetry.bind(this),
94
96
  this.getOperationTimeout.bind(this)
95
97
  );
98
+
99
+ this.documentMemory = new MongoDBDocument(
100
+ this.executeWithRetry.bind(this),
101
+ this.getOperationTimeout.bind(this)
102
+ );
96
103
  }
97
104
 
98
105
  public getAgentMemory(): IAgentMemory {
@@ -115,6 +122,10 @@ export class MongoDBMemory implements IMemory {
115
122
  return this.userWorkflowMemory;
116
123
  }
117
124
 
125
+ public getDocumentMemory(): IDocumentMemory {
126
+ return this.documentMemory;
127
+ }
128
+
118
129
  private setupMongooseEventListeners(): void {
119
130
  if (this.eventListenersSetup) return;
120
131
 
@@ -0,0 +1,125 @@
1
+ import type { IDocumentMemory } from "@ainetwork/adk/modules";
2
+ import type {
3
+ Document,
4
+ DocumentFilter,
5
+ DocumentSlot,
6
+ } from "@ainetwork/adk/types/document";
7
+ import { DocumentModel } from "../models/document.model";
8
+
9
+ export type ExecuteWithRetryFn = <T>(
10
+ operation: () => Promise<T>,
11
+ operationName?: string
12
+ ) => Promise<T>;
13
+
14
+ export type GetOperationTimeoutFn = () => number;
15
+
16
+ export class MongoDBDocument implements IDocumentMemory {
17
+ private executeWithRetry: ExecuteWithRetryFn;
18
+ private getOperationTimeout: GetOperationTimeoutFn;
19
+
20
+ constructor(
21
+ executeWithRetry: ExecuteWithRetryFn,
22
+ getOperationTimeout: GetOperationTimeoutFn
23
+ ) {
24
+ this.executeWithRetry = executeWithRetry;
25
+ this.getOperationTimeout = getOperationTimeout;
26
+ }
27
+
28
+ public async getDocument(documentId: string): Promise<Document | undefined> {
29
+ return this.executeWithRetry(async () => {
30
+ const timeout = this.getOperationTimeout();
31
+ const document = await DocumentModel.findOne({ documentId })
32
+ .maxTimeMS(timeout)
33
+ .lean<Document>();
34
+ return document || undefined;
35
+ }, "getDocument()");
36
+ }
37
+
38
+ public async createDocument(document: Document): Promise<Document> {
39
+ return this.executeWithRetry(async () => {
40
+ const created = await DocumentModel.create(document);
41
+ return created.toObject() as Document;
42
+ }, "createDocument()");
43
+ }
44
+
45
+ public async updateDocument(
46
+ documentId: string,
47
+ document: Partial<Document>
48
+ ): Promise<void> {
49
+ // `documentId` is immutable; never let a payload reassign it.
50
+ const { documentId: _documentId, ...mutableUpdates } = document;
51
+
52
+ return this.executeWithRetry(async () => {
53
+ const timeout = this.getOperationTimeout();
54
+ await DocumentModel.updateOne(
55
+ { documentId },
56
+ { $set: mutableUpdates }
57
+ ).maxTimeMS(timeout);
58
+ }, "updateDocument()");
59
+ }
60
+
61
+ public async updateDocumentSlot(
62
+ documentId: string,
63
+ slotId: string,
64
+ patch: Partial<DocumentSlot>
65
+ ): Promise<void> {
66
+ // Target only the matched slot via the positional `$` operator so
67
+ // concurrent fills of other slots are never overwritten. `slotId` is the
68
+ // slot's identity and must not be patched.
69
+ const { slotId: _slotId, ...fields } = patch;
70
+ const set: Record<string, unknown> = { updatedAt: new Date().toISOString() };
71
+ const unset: Record<string, unknown> = {};
72
+ for (const [key, value] of Object.entries(fields)) {
73
+ if (value === undefined) {
74
+ unset[`slots.$.${key}`] = "";
75
+ } else {
76
+ set[`slots.$.${key}`] = value;
77
+ }
78
+ }
79
+
80
+ const update: Record<string, unknown> = { $set: set, $inc: { version: 1 } };
81
+ if (Object.keys(unset).length > 0) {
82
+ update.$unset = unset;
83
+ }
84
+
85
+ return this.executeWithRetry(async () => {
86
+ const timeout = this.getOperationTimeout();
87
+ await DocumentModel.updateOne(
88
+ { documentId, "slots.slotId": slotId },
89
+ update
90
+ ).maxTimeMS(timeout);
91
+ }, "updateDocumentSlot()");
92
+ }
93
+
94
+ public async deleteDocument(documentId: string): Promise<void> {
95
+ return this.executeWithRetry(async () => {
96
+ const timeout = this.getOperationTimeout();
97
+ await DocumentModel.deleteOne({ documentId }).maxTimeMS(timeout);
98
+ }, "deleteDocument()");
99
+ }
100
+
101
+ public async listDocuments(
102
+ userId?: string,
103
+ filter?: DocumentFilter
104
+ ): Promise<Document[]> {
105
+ return this.executeWithRetry(async () => {
106
+ const timeout = this.getOperationTimeout();
107
+ const query: Record<string, unknown> = {};
108
+ if (userId) query.userId = userId;
109
+ if (filter?.workflowId) query.workflowId = filter.workflowId;
110
+ if (filter?.threadId) query.threadId = filter.threadId;
111
+ if (filter?.source) query.source = filter.source;
112
+ if (filter?.labels) {
113
+ // Subset match: every provided label must equal the stored value.
114
+ for (const [key, value] of Object.entries(filter.labels)) {
115
+ query[`labels.${key}`] = value;
116
+ }
117
+ }
118
+
119
+ const documents = await DocumentModel.find(query)
120
+ .maxTimeMS(timeout)
121
+ .lean<Document[]>();
122
+ return documents;
123
+ }, "listDocuments()");
124
+ }
125
+ }
@@ -0,0 +1,113 @@
1
+ import {
2
+ type DocumentAdvice,
3
+ DocumentFormat,
4
+ type DocumentSlot,
5
+ DocumentSource,
6
+ } from "@ainetwork/adk/types/document";
7
+ import type { WorkflowRenderedBlock } from "@ainetwork/adk/types/memory";
8
+ import { type Document, Schema } from "mongoose";
9
+ import mongoose from "mongoose";
10
+
11
+ export const DocumentObjectSchema = new Schema(
12
+ {
13
+ documentId: {
14
+ type: String,
15
+ required: true,
16
+ unique: true,
17
+ },
18
+ userId: {
19
+ type: String,
20
+ required: true,
21
+ index: true,
22
+ },
23
+ title: {
24
+ type: String,
25
+ required: true,
26
+ },
27
+ format: {
28
+ type: String,
29
+ enum: Object.values(DocumentFormat),
30
+ required: true,
31
+ default: DocumentFormat.MARKDOWN,
32
+ },
33
+ content: {
34
+ type: String,
35
+ required: true,
36
+ },
37
+ blocks: {
38
+ type: Schema.Types.Mixed,
39
+ },
40
+ slots: {
41
+ type: Schema.Types.Mixed,
42
+ },
43
+ // Cached AI advice ({ content, generatedAt }). Free-form so the schema
44
+ // doesn't strip it on update (mongoose strict mode).
45
+ advice: {
46
+ type: Schema.Types.Mixed,
47
+ },
48
+ // Faceted grouping (e.g. category/workplaceId/month). Stored as a free
49
+ // map; index specific keys (e.g. `labels.workplaceId`) if query volume
50
+ // warrants it.
51
+ labels: {
52
+ type: Schema.Types.Mixed,
53
+ },
54
+ source: {
55
+ type: String,
56
+ enum: Object.values(DocumentSource),
57
+ required: true,
58
+ },
59
+ workflowId: {
60
+ type: String,
61
+ index: true,
62
+ },
63
+ threadId: {
64
+ type: String,
65
+ index: true,
66
+ },
67
+ version: {
68
+ type: Number,
69
+ required: true,
70
+ default: 1,
71
+ },
72
+ editedManually: {
73
+ type: Boolean,
74
+ },
75
+ createdAt: {
76
+ type: String,
77
+ required: true,
78
+ },
79
+ updatedAt: {
80
+ type: String,
81
+ required: true,
82
+ },
83
+ },
84
+ // `createdAt`/`updatedAt` are caller-supplied ISO strings (see Document type),
85
+ // so mongoose's automatic Date timestamps are intentionally disabled.
86
+ {
87
+ timestamps: false,
88
+ }
89
+ );
90
+
91
+ export interface DocumentDocument extends Document {
92
+ documentId: string;
93
+ userId: string;
94
+ title: string;
95
+ format: DocumentFormat;
96
+ content: string;
97
+ blocks?: WorkflowRenderedBlock[];
98
+ slots?: DocumentSlot[];
99
+ advice?: DocumentAdvice;
100
+ labels?: Record<string, string>;
101
+ source: DocumentSource;
102
+ workflowId?: string;
103
+ threadId?: string;
104
+ version: number;
105
+ editedManually?: boolean;
106
+ createdAt: string;
107
+ updatedAt: string;
108
+ }
109
+
110
+ export const DocumentModel = mongoose.model<DocumentDocument>(
111
+ "Document",
112
+ DocumentObjectSchema
113
+ );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ainetwork/adk-provider-memory-mongodb",
3
- "version": "0.6.2",
3
+ "version": "0.7.1",
4
4
  "author": "AI Network (https://ainetwork.ai)",
5
5
  "type": "module",
6
6
  "engines": {
@@ -31,7 +31,7 @@
31
31
  "mongoose": "^8.16.5"
32
32
  },
33
33
  "peerDependencies": {
34
- "@ainetwork/adk": "^0.6.3"
34
+ "@ainetwork/adk": "^0.7.1"
35
35
  },
36
36
  "devDependencies": {
37
37
  "typescript": "^5.0.0"
@@ -40,5 +40,5 @@
40
40
  "publishConfig": {
41
41
  "access": "public"
42
42
  },
43
- "gitHead": "ae4661ecacd1bb3c8f23cc903a62bebf71db7e5d"
43
+ "gitHead": "b5988a3d1333127538f41a55cee36fc96632b69c"
44
44
  }