@ainetwork/adk-provider-memory-mongodb 0.1.6 → 0.1.8
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-GPOFS7ZT.js +42 -0
- package/dist/chunk-GPOFS7ZT.js.map +1 -0
- package/dist/chunk-MLPXPX2L.js +50 -0
- package/dist/chunk-MLPXPX2L.js.map +1 -0
- package/dist/index.cjs +107 -87
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -14
- package/dist/index.d.ts +10 -14
- package/dist/index.js +85 -69
- package/dist/index.js.map +1 -1
- package/dist/models/intent.model.d.cts +2 -2
- package/dist/models/intent.model.d.ts +2 -2
- package/dist/models/{chats.model.cjs → messages.model.cjs} +29 -50
- package/dist/models/messages.model.cjs.map +1 -0
- package/dist/models/messages.model.d.cts +75 -0
- package/dist/models/messages.model.d.ts +75 -0
- package/dist/models/messages.model.js +11 -0
- package/dist/models/threads.model.cjs +77 -0
- package/dist/models/threads.model.cjs.map +1 -0
- package/dist/models/threads.model.d.cts +44 -0
- package/dist/models/threads.model.d.ts +44 -0
- package/dist/models/threads.model.js +9 -0
- package/dist/models/threads.model.js.map +1 -0
- package/implements/base.memory.ts +9 -9
- package/implements/intent.memory.ts +18 -6
- package/implements/thread.memory.ts +115 -0
- package/index.ts +1 -1
- package/models/messages.model.ts +61 -0
- package/models/threads.model.ts +46 -0
- package/package.json +7 -5
- package/dist/chunk-N5FAVJ4Z.js +0 -80
- package/dist/chunk-N5FAVJ4Z.js.map +0 -1
- package/dist/models/chats.model.cjs.map +0 -1
- package/dist/models/chats.model.d.cts +0 -105
- package/dist/models/chats.model.d.ts +0 -105
- package/dist/models/chats.model.js +0 -13
- package/implements/session.memory.ts +0 -104
- package/models/chats.model.ts +0 -99
- /package/dist/models/{chats.model.js.map → messages.model.js.map} +0 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// models/threads.model.ts
|
|
31
|
+
var threads_model_exports = {};
|
|
32
|
+
__export(threads_model_exports, {
|
|
33
|
+
ThreadModel: () => ThreadModel,
|
|
34
|
+
ThreadObjectSchema: () => ThreadObjectSchema
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(threads_model_exports);
|
|
37
|
+
var import_memory = require("@ainetwork/adk/types/memory");
|
|
38
|
+
var import_mongoose = require("mongoose");
|
|
39
|
+
var import_mongoose2 = __toESM(require("mongoose"), 1);
|
|
40
|
+
var ThreadObjectSchema = new import_mongoose.Schema(
|
|
41
|
+
{
|
|
42
|
+
type: {
|
|
43
|
+
type: String,
|
|
44
|
+
enum: Object.values(import_memory.ThreadType),
|
|
45
|
+
required: true
|
|
46
|
+
},
|
|
47
|
+
threadId: {
|
|
48
|
+
type: String,
|
|
49
|
+
required: true,
|
|
50
|
+
index: true
|
|
51
|
+
},
|
|
52
|
+
userId: {
|
|
53
|
+
type: String,
|
|
54
|
+
required: true,
|
|
55
|
+
index: true
|
|
56
|
+
},
|
|
57
|
+
title: {
|
|
58
|
+
type: String,
|
|
59
|
+
required: false
|
|
60
|
+
},
|
|
61
|
+
created_at: {
|
|
62
|
+
type: Number,
|
|
63
|
+
required: true
|
|
64
|
+
},
|
|
65
|
+
updated_at: {
|
|
66
|
+
type: Number,
|
|
67
|
+
required: true
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
var ThreadModel = import_mongoose2.default.model("Thread", ThreadObjectSchema);
|
|
72
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
73
|
+
0 && (module.exports = {
|
|
74
|
+
ThreadModel,
|
|
75
|
+
ThreadObjectSchema
|
|
76
|
+
});
|
|
77
|
+
//# sourceMappingURL=threads.model.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../models/threads.model.ts"],"sourcesContent":["import { ThreadType } from \"@ainetwork/adk/types/memory\";\nimport { type Document, Schema } from \"mongoose\";\nimport mongoose from \"mongoose\";\n\nexport const ThreadObjectSchema = new Schema(\n\t{\n\t\ttype: {\n\t\t\ttype: String,\n\t\t\tenum: Object.values(ThreadType),\n\t\t\trequired: true,\n\t\t},\n\t\tthreadId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\tuserId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\ttitle: {\n\t\t\ttype: String,\n\t\t\trequired: false,\n\t\t},\n\t\tcreated_at: {\n\t\t\ttype: Number,\n\t\t\trequired: true,\n\t\t},\n\t\tupdated_at: {\n\t\t\ttype: Number,\n\t\t\trequired: true,\n\t\t}\n\t},\n);\n\nexport interface ThreadDocument extends Document {\n\ttype: ThreadType;\n\tthreadId: string;\n\tuserId: string;\n\ttitle: string;\n\tcreated_at: number;\n\tupdated_at: number;\n}\n\nexport const ThreadModel = mongoose.model<ThreadDocument>(\"Thread\", ThreadObjectSchema);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA2B;AAC3B,sBAAsC;AACtC,IAAAA,mBAAqB;AAEd,IAAM,qBAAqB,IAAI;AAAA,EACrC;AAAA,IACC,MAAM;AAAA,MACL,MAAM;AAAA,MACN,MAAM,OAAO,OAAO,wBAAU;AAAA,MAC9B,UAAU;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACT,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,QAAQ;AAAA,MACP,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,OAAO;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,YAAY;AAAA,MACX,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,YAAY;AAAA,MACX,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,EACD;AACD;AAWO,IAAM,cAAc,iBAAAC,QAAS,MAAsB,UAAU,kBAAkB;","names":["import_mongoose","mongoose"]}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ThreadType } from '@ainetwork/adk/types/memory';
|
|
2
|
+
import mongoose, { Schema, Document } from 'mongoose';
|
|
3
|
+
|
|
4
|
+
declare const ThreadObjectSchema: Schema<any, mongoose.Model<any, any, any, any, any, any>, {}, {}, {}, {}, mongoose.DefaultSchemaOptions, {
|
|
5
|
+
type: ThreadType;
|
|
6
|
+
threadId: string;
|
|
7
|
+
userId: string;
|
|
8
|
+
created_at: number;
|
|
9
|
+
updated_at: number;
|
|
10
|
+
title?: string | null | undefined;
|
|
11
|
+
}, Document<unknown, {}, mongoose.FlatRecord<{
|
|
12
|
+
type: ThreadType;
|
|
13
|
+
threadId: string;
|
|
14
|
+
userId: string;
|
|
15
|
+
created_at: number;
|
|
16
|
+
updated_at: number;
|
|
17
|
+
title?: string | null | undefined;
|
|
18
|
+
}>, {}> & mongoose.FlatRecord<{
|
|
19
|
+
type: ThreadType;
|
|
20
|
+
threadId: string;
|
|
21
|
+
userId: string;
|
|
22
|
+
created_at: number;
|
|
23
|
+
updated_at: number;
|
|
24
|
+
title?: string | null | undefined;
|
|
25
|
+
}> & {
|
|
26
|
+
_id: mongoose.Types.ObjectId;
|
|
27
|
+
} & {
|
|
28
|
+
__v: number;
|
|
29
|
+
}>;
|
|
30
|
+
interface ThreadDocument extends Document {
|
|
31
|
+
type: ThreadType;
|
|
32
|
+
threadId: string;
|
|
33
|
+
userId: string;
|
|
34
|
+
title: string;
|
|
35
|
+
created_at: number;
|
|
36
|
+
updated_at: number;
|
|
37
|
+
}
|
|
38
|
+
declare const ThreadModel: mongoose.Model<ThreadDocument, {}, {}, {}, Document<unknown, {}, ThreadDocument, {}> & ThreadDocument & Required<{
|
|
39
|
+
_id: unknown;
|
|
40
|
+
}> & {
|
|
41
|
+
__v: number;
|
|
42
|
+
}, any>;
|
|
43
|
+
|
|
44
|
+
export { type ThreadDocument, ThreadModel, ThreadObjectSchema };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ThreadType } from '@ainetwork/adk/types/memory';
|
|
2
|
+
import mongoose, { Schema, Document } from 'mongoose';
|
|
3
|
+
|
|
4
|
+
declare const ThreadObjectSchema: Schema<any, mongoose.Model<any, any, any, any, any, any>, {}, {}, {}, {}, mongoose.DefaultSchemaOptions, {
|
|
5
|
+
type: ThreadType;
|
|
6
|
+
threadId: string;
|
|
7
|
+
userId: string;
|
|
8
|
+
created_at: number;
|
|
9
|
+
updated_at: number;
|
|
10
|
+
title?: string | null | undefined;
|
|
11
|
+
}, Document<unknown, {}, mongoose.FlatRecord<{
|
|
12
|
+
type: ThreadType;
|
|
13
|
+
threadId: string;
|
|
14
|
+
userId: string;
|
|
15
|
+
created_at: number;
|
|
16
|
+
updated_at: number;
|
|
17
|
+
title?: string | null | undefined;
|
|
18
|
+
}>, {}> & mongoose.FlatRecord<{
|
|
19
|
+
type: ThreadType;
|
|
20
|
+
threadId: string;
|
|
21
|
+
userId: string;
|
|
22
|
+
created_at: number;
|
|
23
|
+
updated_at: number;
|
|
24
|
+
title?: string | null | undefined;
|
|
25
|
+
}> & {
|
|
26
|
+
_id: mongoose.Types.ObjectId;
|
|
27
|
+
} & {
|
|
28
|
+
__v: number;
|
|
29
|
+
}>;
|
|
30
|
+
interface ThreadDocument extends Document {
|
|
31
|
+
type: ThreadType;
|
|
32
|
+
threadId: string;
|
|
33
|
+
userId: string;
|
|
34
|
+
title: string;
|
|
35
|
+
created_at: number;
|
|
36
|
+
updated_at: number;
|
|
37
|
+
}
|
|
38
|
+
declare const ThreadModel: mongoose.Model<ThreadDocument, {}, {}, {}, Document<unknown, {}, ThreadDocument, {}> & ThreadDocument & Required<{
|
|
39
|
+
_id: unknown;
|
|
40
|
+
}> & {
|
|
41
|
+
__v: number;
|
|
42
|
+
}, any>;
|
|
43
|
+
|
|
44
|
+
export { type ThreadDocument, ThreadModel, ThreadObjectSchema };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1,19 +1,13 @@
|
|
|
1
1
|
import { IMemory } from "node_modules/@ainetwork/adk/dist/esm/modules/memory/base.memory";
|
|
2
|
-
import mongoose
|
|
2
|
+
import mongoose from "mongoose";
|
|
3
3
|
import { loggers } from "@ainetwork/adk/utils/logger";
|
|
4
4
|
|
|
5
5
|
export class MongoDBMemory implements IMemory {
|
|
6
6
|
private _isConnected: boolean = false;
|
|
7
7
|
private _uri: string;
|
|
8
|
-
private _mongoose: Mongoose;
|
|
9
8
|
|
|
10
9
|
constructor(uri: string) {
|
|
11
10
|
this._uri = uri;
|
|
12
|
-
this._mongoose = new Mongoose();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public getInstance(): Mongoose {
|
|
16
|
-
return this._mongoose;
|
|
17
11
|
}
|
|
18
12
|
|
|
19
13
|
public async connect(): Promise<void> {
|
|
@@ -22,7 +16,13 @@ export class MongoDBMemory implements IMemory {
|
|
|
22
16
|
}
|
|
23
17
|
|
|
24
18
|
try {
|
|
25
|
-
await
|
|
19
|
+
await mongoose.connect(this._uri, {
|
|
20
|
+
maxPoolSize: 1,
|
|
21
|
+
serverSelectionTimeoutMS: 30000,
|
|
22
|
+
socketTimeoutMS: 45000,
|
|
23
|
+
connectTimeoutMS: 30000,
|
|
24
|
+
bufferCommands: false,
|
|
25
|
+
});
|
|
26
26
|
this._isConnected = true;
|
|
27
27
|
loggers.agent.info("MongoDB connected successfully");
|
|
28
28
|
} catch (error) {
|
|
@@ -37,7 +37,7 @@ export class MongoDBMemory implements IMemory {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
try {
|
|
40
|
-
await
|
|
40
|
+
await mongoose.disconnect();
|
|
41
41
|
this._isConnected = false;
|
|
42
42
|
loggers.agent.info("MongoDB disconnected successfully");
|
|
43
43
|
} catch (error) {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
1
|
import type { Intent } from "@ainetwork/adk/types/memory";
|
|
3
2
|
import { IIntentMemory } from "@ainetwork/adk/modules";
|
|
4
3
|
import { MongoDBMemory } from "./base.memory";
|
|
5
4
|
import { IntentModel } from "../models/intent.model";
|
|
5
|
+
import { Types } from "mongoose";
|
|
6
6
|
|
|
7
7
|
export class MongoDBIntent extends MongoDBMemory implements IIntentMemory {
|
|
8
8
|
public async getIntent(intentId: string): Promise<Intent | undefined> {
|
|
9
|
-
const intent = await IntentModel.findById(intentId);
|
|
9
|
+
const intent = await IntentModel.findById(new Types.ObjectId(intentId));
|
|
10
10
|
if (intent) {
|
|
11
11
|
return {
|
|
12
12
|
name: intent.name,
|
|
@@ -18,10 +18,22 @@ export class MongoDBIntent extends MongoDBMemory implements IIntentMemory {
|
|
|
18
18
|
return undefined;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
public async getIntentByName(intentName: string): Promise<Intent | undefined> {
|
|
22
|
+
const intent = await IntentModel.findOne({ name: intentName });
|
|
23
|
+
if (intent) {
|
|
24
|
+
return {
|
|
25
|
+
name: intent.name,
|
|
26
|
+
description: intent.description,
|
|
27
|
+
prompt: intent.prompt,
|
|
28
|
+
llm: intent.llm,
|
|
29
|
+
} as Intent;
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
21
34
|
public async saveIntent(intent: Intent): Promise<void> {
|
|
22
|
-
|
|
35
|
+
// ObjectId automatically generated (MongoDB automatically generates)
|
|
23
36
|
await IntentModel.create({
|
|
24
|
-
_id: newId,
|
|
25
37
|
name: intent.name,
|
|
26
38
|
description: intent.description,
|
|
27
39
|
prompt: intent.prompt,
|
|
@@ -31,7 +43,7 @@ export class MongoDBIntent extends MongoDBMemory implements IIntentMemory {
|
|
|
31
43
|
|
|
32
44
|
public async updateIntent(intentId: string, intent: Intent): Promise<void> {
|
|
33
45
|
await IntentModel.updateOne({
|
|
34
|
-
_id: intentId,
|
|
46
|
+
_id: new Types.ObjectId(intentId),
|
|
35
47
|
},{
|
|
36
48
|
name: intent.name,
|
|
37
49
|
description: intent.description,
|
|
@@ -41,7 +53,7 @@ export class MongoDBIntent extends MongoDBMemory implements IIntentMemory {
|
|
|
41
53
|
};
|
|
42
54
|
|
|
43
55
|
public async deleteIntent(intentId: string): Promise<void> {
|
|
44
|
-
await IntentModel.deleteOne({ _id: intentId });
|
|
56
|
+
await IntentModel.deleteOne({ _id: new Types.ObjectId(intentId) });
|
|
45
57
|
};
|
|
46
58
|
|
|
47
59
|
public async listIntents(): Promise<Intent[]> {
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type { MessageObject, ThreadMetadata, ThreadObject, ThreadType } from "@ainetwork/adk/types/memory";
|
|
3
|
+
import { MessageRole } from "@ainetwork/adk/types/memory";
|
|
4
|
+
import { IThreadMemory } from "@ainetwork/adk/modules";
|
|
5
|
+
import { MongoDBMemory } from "./base.memory";
|
|
6
|
+
import { ThreadDocument, ThreadModel } from "../models/threads.model";
|
|
7
|
+
import { MessageDocument, MessageModel } from "../models/messages.model";
|
|
8
|
+
import { loggers } from "@ainetwork/adk/utils/logger";
|
|
9
|
+
|
|
10
|
+
export class MongoDBThread extends MongoDBMemory implements IThreadMemory {
|
|
11
|
+
constructor(uri: string) {
|
|
12
|
+
super(uri);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public async getThread(
|
|
16
|
+
type: ThreadType,
|
|
17
|
+
userId: string,
|
|
18
|
+
threadId: string
|
|
19
|
+
): Promise<ThreadObject | undefined> {
|
|
20
|
+
const thread = await ThreadModel.findOne({ type, threadId, userId });
|
|
21
|
+
const messages = await MessageModel.find({ threadId, userId }).sort({
|
|
22
|
+
timestamp: 1,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (!thread) return undefined;
|
|
26
|
+
|
|
27
|
+
loggers.agent.debug(`Found ${messages.length} messages for thread ${threadId}`);
|
|
28
|
+
|
|
29
|
+
const threadObject: ThreadObject = {
|
|
30
|
+
type: thread.type as ThreadType,
|
|
31
|
+
title: thread.title || "New thread",
|
|
32
|
+
messages: {}
|
|
33
|
+
};
|
|
34
|
+
messages.forEach((message: MessageDocument) => {
|
|
35
|
+
const messageId = message._id?.toString() || message.id;
|
|
36
|
+
threadObject.messages[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;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
public async createThread(
|
|
48
|
+
type: ThreadType,
|
|
49
|
+
userId: string,
|
|
50
|
+
threadId: string,
|
|
51
|
+
title: string,
|
|
52
|
+
): Promise<ThreadMetadata> {
|
|
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, threadId, title, updatedAt: now };
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
public async addMessagesToThread(
|
|
67
|
+
userId: string,
|
|
68
|
+
threadId: string,
|
|
69
|
+
messages: MessageObject[]
|
|
70
|
+
): Promise<void> {
|
|
71
|
+
await ThreadModel.updateOne({ threadId, userId }, {
|
|
72
|
+
updated_at: Date.now(),
|
|
73
|
+
});
|
|
74
|
+
for (const message of messages) {
|
|
75
|
+
const newId = randomUUID();
|
|
76
|
+
await MessageModel.create({
|
|
77
|
+
threadId,
|
|
78
|
+
messageId: newId,
|
|
79
|
+
userId,
|
|
80
|
+
role: message.role,
|
|
81
|
+
content: message.content,
|
|
82
|
+
timestamp: message.timestamp,
|
|
83
|
+
metadata: message.metadata,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
public async deleteThread(userId: string, threadId: string): Promise<void> {
|
|
89
|
+
const messages = await MessageModel.find({ userId, threadId }).sort({
|
|
90
|
+
timestamp: 1,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
messages?.forEach((message: MessageDocument) => {
|
|
94
|
+
message.deleteOne();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const thread = await ThreadModel.findOne({ userId, threadId });
|
|
98
|
+
thread?.deleteOne();
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
public async listThreads(userId: string): Promise<ThreadMetadata[]> {
|
|
102
|
+
const threads = await ThreadModel.find({ userId }).sort({
|
|
103
|
+
updated_at: -1,
|
|
104
|
+
});
|
|
105
|
+
const data: ThreadMetadata[] = threads.map((thread: ThreadDocument) => {
|
|
106
|
+
return {
|
|
107
|
+
type: thread.type,
|
|
108
|
+
threadId: thread.threadId,
|
|
109
|
+
title: thread.title,
|
|
110
|
+
updatedAt: thread.updated_at
|
|
111
|
+
} as ThreadMetadata;
|
|
112
|
+
})
|
|
113
|
+
return data;
|
|
114
|
+
};
|
|
115
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { MongoDBThread } from "./implements/thread.memory";
|
|
2
2
|
export { MongoDBIntent } from "./implements/intent.memory";
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { MessageRole } from "@ainetwork/adk/types/memory";
|
|
2
|
+
import { type Document, Schema } from "mongoose";
|
|
3
|
+
import mongoose from "mongoose";
|
|
4
|
+
|
|
5
|
+
// MessageContentObject schema
|
|
6
|
+
export const MessageContentObjectSchema = new Schema(
|
|
7
|
+
{
|
|
8
|
+
type: { type: String, required: true },
|
|
9
|
+
parts: { type: [Schema.Types.Mixed], required: true },
|
|
10
|
+
},
|
|
11
|
+
{ _id: false },
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
// MessageObject schema - 개별 문서로 저장
|
|
15
|
+
export const MessageObjectSchema = new Schema(
|
|
16
|
+
{
|
|
17
|
+
threadId: {
|
|
18
|
+
type: String,
|
|
19
|
+
required: true,
|
|
20
|
+
index: true,
|
|
21
|
+
},
|
|
22
|
+
userId: {
|
|
23
|
+
type: String,
|
|
24
|
+
required: true,
|
|
25
|
+
index: true,
|
|
26
|
+
},
|
|
27
|
+
role: {
|
|
28
|
+
type: String,
|
|
29
|
+
enum: Object.values(MessageRole),
|
|
30
|
+
required: true,
|
|
31
|
+
},
|
|
32
|
+
content: {
|
|
33
|
+
type: MessageContentObjectSchema,
|
|
34
|
+
required: true,
|
|
35
|
+
},
|
|
36
|
+
timestamp: {
|
|
37
|
+
type: Number,
|
|
38
|
+
required: true,
|
|
39
|
+
},
|
|
40
|
+
metadata: {
|
|
41
|
+
type: Schema.Types.Mixed,
|
|
42
|
+
default: {},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
// Message Document interface
|
|
48
|
+
export interface MessageDocument extends Document {
|
|
49
|
+
threadId: string;
|
|
50
|
+
role: MessageRole;
|
|
51
|
+
content: {
|
|
52
|
+
type: string;
|
|
53
|
+
parts: any[];
|
|
54
|
+
};
|
|
55
|
+
timestamp: number;
|
|
56
|
+
metadata?: { [key: string]: unknown };
|
|
57
|
+
createdAt: Date;
|
|
58
|
+
updatedAt: Date;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const MessageModel = mongoose.model<MessageDocument>("Message", MessageObjectSchema);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ThreadType } from "@ainetwork/adk/types/memory";
|
|
2
|
+
import { type Document, Schema } from "mongoose";
|
|
3
|
+
import mongoose from "mongoose";
|
|
4
|
+
|
|
5
|
+
export const ThreadObjectSchema = new Schema(
|
|
6
|
+
{
|
|
7
|
+
type: {
|
|
8
|
+
type: String,
|
|
9
|
+
enum: Object.values(ThreadType),
|
|
10
|
+
required: true,
|
|
11
|
+
},
|
|
12
|
+
threadId: {
|
|
13
|
+
type: String,
|
|
14
|
+
required: true,
|
|
15
|
+
index: true,
|
|
16
|
+
},
|
|
17
|
+
userId: {
|
|
18
|
+
type: String,
|
|
19
|
+
required: true,
|
|
20
|
+
index: true,
|
|
21
|
+
},
|
|
22
|
+
title: {
|
|
23
|
+
type: String,
|
|
24
|
+
required: false,
|
|
25
|
+
},
|
|
26
|
+
created_at: {
|
|
27
|
+
type: Number,
|
|
28
|
+
required: true,
|
|
29
|
+
},
|
|
30
|
+
updated_at: {
|
|
31
|
+
type: Number,
|
|
32
|
+
required: true,
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
export interface ThreadDocument extends Document {
|
|
38
|
+
type: ThreadType;
|
|
39
|
+
threadId: string;
|
|
40
|
+
userId: string;
|
|
41
|
+
title: string;
|
|
42
|
+
created_at: number;
|
|
43
|
+
updated_at: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const ThreadModel = mongoose.model<ThreadDocument>("Thread", ThreadObjectSchema);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ainetwork/adk-provider-memory-mongodb",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"author": "AI Network (https://ainetwork.ai)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -13,12 +13,14 @@
|
|
|
13
13
|
".": {
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
15
|
"import": "./dist/index.js",
|
|
16
|
-
"require": "./dist/index.cjs"
|
|
16
|
+
"require": "./dist/index.cjs",
|
|
17
|
+
"default": "./dist/index.js"
|
|
17
18
|
},
|
|
18
19
|
"./models/*": {
|
|
19
20
|
"types": "./dist/models/*.d.ts",
|
|
20
21
|
"import": "./dist/models/*.js",
|
|
21
|
-
"require": "./dist/models/*.cjs"
|
|
22
|
+
"require": "./dist/models/*.cjs",
|
|
23
|
+
"default": "./dist/models/*.js"
|
|
22
24
|
}
|
|
23
25
|
},
|
|
24
26
|
"scripts": {
|
|
@@ -26,7 +28,7 @@
|
|
|
26
28
|
"clean": "rm -rf dist"
|
|
27
29
|
},
|
|
28
30
|
"dependencies": {
|
|
29
|
-
"@ainetwork/adk": "^0.
|
|
31
|
+
"@ainetwork/adk": "^0.2.0",
|
|
30
32
|
"mongoose": "^8.16.5"
|
|
31
33
|
},
|
|
32
34
|
"devDependencies": {
|
|
@@ -36,5 +38,5 @@
|
|
|
36
38
|
"publishConfig": {
|
|
37
39
|
"access": "public"
|
|
38
40
|
},
|
|
39
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "2c60e4331a05d404f244fd59085710cb4870f599"
|
|
40
42
|
}
|
package/dist/chunk-N5FAVJ4Z.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
// models/chats.model.ts
|
|
2
|
-
import { Schema } from "mongoose";
|
|
3
|
-
var ChatRole = /* @__PURE__ */ ((ChatRole2) => {
|
|
4
|
-
ChatRole2["USER"] = "USER";
|
|
5
|
-
ChatRole2["SYSTEM"] = "SYSTEM";
|
|
6
|
-
ChatRole2["MODEL"] = "MODEL";
|
|
7
|
-
return ChatRole2;
|
|
8
|
-
})(ChatRole || {});
|
|
9
|
-
var SessionObjectSchema = new Schema(
|
|
10
|
-
{
|
|
11
|
-
sessionId: {
|
|
12
|
-
type: String,
|
|
13
|
-
required: true,
|
|
14
|
-
index: true
|
|
15
|
-
},
|
|
16
|
-
userId: {
|
|
17
|
-
type: String,
|
|
18
|
-
required: true,
|
|
19
|
-
index: true
|
|
20
|
-
},
|
|
21
|
-
title: {
|
|
22
|
-
type: String,
|
|
23
|
-
required: false
|
|
24
|
-
},
|
|
25
|
-
created_at: {
|
|
26
|
-
type: Number,
|
|
27
|
-
required: true
|
|
28
|
-
},
|
|
29
|
-
updated_at: {
|
|
30
|
-
type: Number,
|
|
31
|
-
required: true
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
);
|
|
35
|
-
var ChatContentObjectSchema = new Schema(
|
|
36
|
-
{
|
|
37
|
-
type: { type: String, required: true },
|
|
38
|
-
parts: { type: [Schema.Types.Mixed], required: true }
|
|
39
|
-
},
|
|
40
|
-
{ _id: false }
|
|
41
|
-
);
|
|
42
|
-
var ChatObjectSchema = new Schema(
|
|
43
|
-
{
|
|
44
|
-
sessionId: {
|
|
45
|
-
type: String,
|
|
46
|
-
required: true,
|
|
47
|
-
index: true
|
|
48
|
-
},
|
|
49
|
-
userId: {
|
|
50
|
-
type: String,
|
|
51
|
-
required: true,
|
|
52
|
-
index: true
|
|
53
|
-
},
|
|
54
|
-
role: {
|
|
55
|
-
type: String,
|
|
56
|
-
enum: Object.values(ChatRole),
|
|
57
|
-
required: true
|
|
58
|
-
},
|
|
59
|
-
content: {
|
|
60
|
-
type: ChatContentObjectSchema,
|
|
61
|
-
required: true
|
|
62
|
-
},
|
|
63
|
-
timestamp: {
|
|
64
|
-
type: Number,
|
|
65
|
-
required: true
|
|
66
|
-
},
|
|
67
|
-
metadata: {
|
|
68
|
-
type: Schema.Types.Mixed,
|
|
69
|
-
default: {}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
export {
|
|
75
|
-
ChatRole,
|
|
76
|
-
SessionObjectSchema,
|
|
77
|
-
ChatContentObjectSchema,
|
|
78
|
-
ChatObjectSchema
|
|
79
|
-
};
|
|
80
|
-
//# sourceMappingURL=chunk-N5FAVJ4Z.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../models/chats.model.ts"],"sourcesContent":["import { type Document, Schema } from \"mongoose\";\n\n// ChatRole enum\nexport enum ChatRole {\n\tUSER = \"USER\",\n\tSYSTEM = \"SYSTEM\",\n\tMODEL = \"MODEL\",\n}\n\nexport const SessionObjectSchema = new Schema(\n\t{\n\t\tsessionId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\tuserId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\ttitle: {\n\t\t\ttype: String,\n\t\t\trequired: false,\n\t\t},\n\t\tcreated_at: {\n\t\t\ttype: Number,\n\t\t\trequired: true,\n\t\t},\n\t\tupdated_at: {\n\t\t\ttype: Number,\n\t\t\trequired: true,\n\t\t}\n\t},\n);\n\nexport interface SessionDocument extends Document {\n\tsessionId: string;\n\tuserId: string;\n\ttitle?: string;\n\tcreated_at: number;\n\tupdated_at: number;\n}\n\n// ChatContentObject schema\nexport const ChatContentObjectSchema = new Schema(\n\t{\n\t\ttype: { type: String, required: true },\n\t\tparts: { type: [Schema.Types.Mixed], required: true },\n\t},\n\t{ _id: false },\n);\n\n// ChatObject schema - 개별 문서로 저장\nexport const ChatObjectSchema = new Schema(\n\t{\n\t\tsessionId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\tuserId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\trole: {\n\t\t\ttype: String,\n\t\t\tenum: Object.values(ChatRole),\n\t\t\trequired: true,\n\t\t},\n\t\tcontent: {\n\t\t\ttype: ChatContentObjectSchema,\n\t\t\trequired: true,\n\t\t},\n\t\ttimestamp: {\n\t\t\ttype: Number,\n\t\t\trequired: true,\n\t\t},\n\t\tmetadata: {\n\t\t\ttype: Schema.Types.Mixed,\n\t\t\tdefault: {},\n\t\t},\n\t},\n);\n\n// Chat Document interface\nexport interface ChatDocument extends Document {\n\tsessionId: string;\n\trole: ChatRole;\n\tcontent: {\n\t\ttype: string;\n\t\tparts: any[];\n\t};\n\ttimestamp: number;\n\tmetadata?: { [key: string]: unknown };\n\tcreatedAt: Date;\n\tupdatedAt: Date;\n}\n"],"mappings":";AAAA,SAAwB,cAAc;AAG/B,IAAK,WAAL,kBAAKA,cAAL;AACN,EAAAA,UAAA,UAAO;AACP,EAAAA,UAAA,YAAS;AACT,EAAAA,UAAA,WAAQ;AAHG,SAAAA;AAAA,GAAA;AAML,IAAM,sBAAsB,IAAI;AAAA,EACtC;AAAA,IACC,WAAW;AAAA,MACV,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,QAAQ;AAAA,MACP,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,OAAO;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,YAAY;AAAA,MACX,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,YAAY;AAAA,MACX,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,EACD;AACD;AAWO,IAAM,0BAA0B,IAAI;AAAA,EAC1C;AAAA,IACC,MAAM,EAAE,MAAM,QAAQ,UAAU,KAAK;AAAA,IACrC,OAAO,EAAE,MAAM,CAAC,OAAO,MAAM,KAAK,GAAG,UAAU,KAAK;AAAA,EACrD;AAAA,EACA,EAAE,KAAK,MAAM;AACd;AAGO,IAAM,mBAAmB,IAAI;AAAA,EACnC;AAAA,IACC,WAAW;AAAA,MACV,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,QAAQ;AAAA,MACP,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,MAAM;AAAA,MACL,MAAM;AAAA,MACN,MAAM,OAAO,OAAO,QAAQ;AAAA,MAC5B,UAAU;AAAA,IACX;AAAA,IACA,SAAS;AAAA,MACR,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,WAAW;AAAA,MACV,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACT,MAAM,OAAO,MAAM;AAAA,MACnB,SAAS,CAAC;AAAA,IACX;AAAA,EACD;AACD;","names":["ChatRole"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../models/chats.model.ts"],"sourcesContent":["import { type Document, Schema } from \"mongoose\";\n\n// ChatRole enum\nexport enum ChatRole {\n\tUSER = \"USER\",\n\tSYSTEM = \"SYSTEM\",\n\tMODEL = \"MODEL\",\n}\n\nexport const SessionObjectSchema = new Schema(\n\t{\n\t\tsessionId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\tuserId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\ttitle: {\n\t\t\ttype: String,\n\t\t\trequired: false,\n\t\t},\n\t\tcreated_at: {\n\t\t\ttype: Number,\n\t\t\trequired: true,\n\t\t},\n\t\tupdated_at: {\n\t\t\ttype: Number,\n\t\t\trequired: true,\n\t\t}\n\t},\n);\n\nexport interface SessionDocument extends Document {\n\tsessionId: string;\n\tuserId: string;\n\ttitle?: string;\n\tcreated_at: number;\n\tupdated_at: number;\n}\n\n// ChatContentObject schema\nexport const ChatContentObjectSchema = new Schema(\n\t{\n\t\ttype: { type: String, required: true },\n\t\tparts: { type: [Schema.Types.Mixed], required: true },\n\t},\n\t{ _id: false },\n);\n\n// ChatObject schema - 개별 문서로 저장\nexport const ChatObjectSchema = new Schema(\n\t{\n\t\tsessionId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\tuserId: {\n\t\t\ttype: String,\n\t\t\trequired: true,\n\t\t\tindex: true,\n\t\t},\n\t\trole: {\n\t\t\ttype: String,\n\t\t\tenum: Object.values(ChatRole),\n\t\t\trequired: true,\n\t\t},\n\t\tcontent: {\n\t\t\ttype: ChatContentObjectSchema,\n\t\t\trequired: true,\n\t\t},\n\t\ttimestamp: {\n\t\t\ttype: Number,\n\t\t\trequired: true,\n\t\t},\n\t\tmetadata: {\n\t\t\ttype: Schema.Types.Mixed,\n\t\t\tdefault: {},\n\t\t},\n\t},\n);\n\n// Chat Document interface\nexport interface ChatDocument extends Document {\n\tsessionId: string;\n\trole: ChatRole;\n\tcontent: {\n\t\ttype: string;\n\t\tparts: any[];\n\t};\n\ttimestamp: number;\n\tmetadata?: { [key: string]: unknown };\n\tcreatedAt: Date;\n\tupdatedAt: Date;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAsC;AAG/B,IAAK,WAAL,kBAAKA,cAAL;AACN,EAAAA,UAAA,UAAO;AACP,EAAAA,UAAA,YAAS;AACT,EAAAA,UAAA,WAAQ;AAHG,SAAAA;AAAA,GAAA;AAML,IAAM,sBAAsB,IAAI;AAAA,EACtC;AAAA,IACC,WAAW;AAAA,MACV,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,QAAQ;AAAA,MACP,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,OAAO;AAAA,MACN,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,YAAY;AAAA,MACX,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,YAAY;AAAA,MACX,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,EACD;AACD;AAWO,IAAM,0BAA0B,IAAI;AAAA,EAC1C;AAAA,IACC,MAAM,EAAE,MAAM,QAAQ,UAAU,KAAK;AAAA,IACrC,OAAO,EAAE,MAAM,CAAC,uBAAO,MAAM,KAAK,GAAG,UAAU,KAAK;AAAA,EACrD;AAAA,EACA,EAAE,KAAK,MAAM;AACd;AAGO,IAAM,mBAAmB,IAAI;AAAA,EACnC;AAAA,IACC,WAAW;AAAA,MACV,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,QAAQ;AAAA,MACP,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,IACR;AAAA,IACA,MAAM;AAAA,MACL,MAAM;AAAA,MACN,MAAM,OAAO,OAAO,QAAQ;AAAA,MAC5B,UAAU;AAAA,IACX;AAAA,IACA,SAAS;AAAA,MACR,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,WAAW;AAAA,MACV,MAAM;AAAA,MACN,UAAU;AAAA,IACX;AAAA,IACA,UAAU;AAAA,MACT,MAAM,uBAAO,MAAM;AAAA,MACnB,SAAS,CAAC;AAAA,IACX;AAAA,EACD;AACD;","names":["ChatRole"]}
|