@ainetwork/adk-provider-memory-mongodb 0.1.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.
@@ -0,0 +1,51 @@
1
+ // models/chats.model.ts
2
+ import mongoose, { 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 ChatContentObjectSchema = new Schema(
10
+ {
11
+ type: { type: String, required: true },
12
+ parts: { type: [Schema.Types.Mixed], required: true }
13
+ },
14
+ { _id: false }
15
+ );
16
+ var ChatObjectSchema = new Schema(
17
+ {
18
+ sessionId: {
19
+ type: String,
20
+ required: true,
21
+ index: true
22
+ },
23
+ role: {
24
+ type: String,
25
+ enum: Object.values(ChatRole),
26
+ required: true
27
+ },
28
+ content: {
29
+ type: ChatContentObjectSchema,
30
+ required: true
31
+ },
32
+ timestamp: {
33
+ type: Number,
34
+ required: true
35
+ },
36
+ metadata: {
37
+ type: Schema.Types.Mixed,
38
+ default: {}
39
+ }
40
+ },
41
+ {
42
+ timestamps: true
43
+ }
44
+ );
45
+ var ChatModel = mongoose.model("Chat", ChatObjectSchema);
46
+
47
+ export {
48
+ ChatRole,
49
+ ChatModel
50
+ };
51
+ //# sourceMappingURL=chunk-2SB2M62A.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../models/chats.model.ts"],"sourcesContent":["import mongoose, { type Document, Schema } from \"mongoose\";\n\n// ChatRole enum\nexport enum ChatRole {\n\tUSER = \"USER\",\n\tSYSTEM = \"SYSTEM\",\n\tMODEL = \"MODEL\",\n}\n\n// ChatContentObject schema\nconst 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 - 개별 문서로 저장\nconst 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\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\t{\n\t\ttimestamps: true,\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\n// Export the model\nexport const ChatModel = mongoose.model<ChatDocument>(\"Chat\", ChatObjectSchema);\n"],"mappings":";AAAA,OAAO,YAA2B,cAAc;AAGzC,IAAK,WAAL,kBAAKA,cAAL;AACN,EAAAA,UAAA,UAAO;AACP,EAAAA,UAAA,YAAS;AACT,EAAAA,UAAA,WAAQ;AAHG,SAAAA;AAAA,GAAA;AAOZ,IAAM,0BAA0B,IAAI;AAAA,EACnC;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;AAGA,IAAM,mBAAmB,IAAI;AAAA,EAC5B;AAAA,IACC,WAAW;AAAA,MACV,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;AAAA,EACA;AAAA,IACC,YAAY;AAAA,EACb;AACD;AAiBO,IAAM,YAAY,SAAS,MAAoB,QAAQ,gBAAgB;","names":["ChatRole"]}
package/dist/index.cjs ADDED
@@ -0,0 +1,167 @@
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
+ // index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ MongoDBMemory: () => MongoDBMemory
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var import_modules = require("@ainetwork/adk/modules");
37
+ var import_logger = require("@ainetwork/adk/utils/logger");
38
+ var import_mongoose2 = __toESM(require("mongoose"), 1);
39
+
40
+ // models/chats.model.ts
41
+ var import_mongoose = __toESM(require("mongoose"), 1);
42
+ var ChatRole = /* @__PURE__ */ ((ChatRole2) => {
43
+ ChatRole2["USER"] = "USER";
44
+ ChatRole2["SYSTEM"] = "SYSTEM";
45
+ ChatRole2["MODEL"] = "MODEL";
46
+ return ChatRole2;
47
+ })(ChatRole || {});
48
+ var ChatContentObjectSchema = new import_mongoose.Schema(
49
+ {
50
+ type: { type: String, required: true },
51
+ parts: { type: [import_mongoose.Schema.Types.Mixed], required: true }
52
+ },
53
+ { _id: false }
54
+ );
55
+ var ChatObjectSchema = new import_mongoose.Schema(
56
+ {
57
+ sessionId: {
58
+ type: String,
59
+ required: true,
60
+ index: true
61
+ },
62
+ role: {
63
+ type: String,
64
+ enum: Object.values(ChatRole),
65
+ required: true
66
+ },
67
+ content: {
68
+ type: ChatContentObjectSchema,
69
+ required: true
70
+ },
71
+ timestamp: {
72
+ type: Number,
73
+ required: true
74
+ },
75
+ metadata: {
76
+ type: import_mongoose.Schema.Types.Mixed,
77
+ default: {}
78
+ }
79
+ },
80
+ {
81
+ timestamps: true
82
+ }
83
+ );
84
+ var ChatModel = import_mongoose.default.model("Chat", ChatObjectSchema);
85
+
86
+ // index.ts
87
+ var MongoDBMemory = class extends import_modules.BaseMemory {
88
+ isConnected = false;
89
+ constructor(uri) {
90
+ super();
91
+ this.connect(uri);
92
+ }
93
+ async connect(uri) {
94
+ if (this.isConnected) {
95
+ return;
96
+ }
97
+ try {
98
+ await import_mongoose2.default.connect(uri);
99
+ this.isConnected = true;
100
+ import_logger.loggers.agent.info("MongoDB connected successfully");
101
+ } catch (error) {
102
+ import_logger.loggers.agent.error("Failed to connect to MongoDB:", error);
103
+ throw error;
104
+ }
105
+ }
106
+ async disconnect() {
107
+ if (!this.isConnected) {
108
+ return;
109
+ }
110
+ try {
111
+ await import_mongoose2.default.disconnect();
112
+ this.isConnected = false;
113
+ import_logger.loggers.agent.info("MongoDB disconnected successfully");
114
+ } catch (error) {
115
+ import_logger.loggers.agent.error("Failed to disconnect from MongoDB:", error);
116
+ throw error;
117
+ }
118
+ }
119
+ async getSessionHistory(sessionId) {
120
+ const chats = await ChatModel.find({ sessionId }).sort({
121
+ timestamp: 1
122
+ });
123
+ import_logger.loggers.agent.info(`Found ${chats.length} chats for session ${sessionId}`);
124
+ const sessionObject = { chats: {} };
125
+ chats.forEach((chat) => {
126
+ const chatId = chat._id?.toString() || chat.id;
127
+ sessionObject.chats[chatId] = {
128
+ role: chat.role,
129
+ content: chat.content,
130
+ timestamp: chat.timestamp,
131
+ metadata: chat.metadata
132
+ };
133
+ });
134
+ return sessionObject;
135
+ }
136
+ async updateSessionHistory(sessionId, chat) {
137
+ import_logger.loggers.agent.info(`Updating session history for session ${sessionId}`);
138
+ import_logger.loggers.agent.info(`Chat: ${JSON.stringify(chat)}`);
139
+ await ChatModel.create({
140
+ sessionId,
141
+ role: chat.role,
142
+ content: chat.content,
143
+ timestamp: chat.timestamp,
144
+ metadata: chat.metadata
145
+ });
146
+ }
147
+ async storeQueryAndIntent(query, intent, sessionId) {
148
+ const chat = {
149
+ role: "USER" /* USER */,
150
+ content: {
151
+ type: "text",
152
+ parts: [query]
153
+ },
154
+ timestamp: Date.now(),
155
+ metadata: {
156
+ intent,
157
+ query
158
+ }
159
+ };
160
+ await this.updateSessionHistory(sessionId, chat);
161
+ }
162
+ };
163
+ // Annotate the CommonJS export names for ESM import in node:
164
+ 0 && (module.exports = {
165
+ MongoDBMemory
166
+ });
167
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../index.ts","../models/chats.model.ts"],"sourcesContent":["import { BaseMemory } from \"@ainetwork/adk/modules\";\nimport type { ChatObject, SessionObject } from \"@ainetwork/adk/types/memory\";\nimport { loggers } from \"@ainetwork/adk/utils/logger\";\nimport mongoose from \"mongoose\";\nimport {\n\ttype ChatDocument,\n\tChatModel,\n\tChatRole,\n} from \"./models/chats.model\";\n\nexport class MongoDBMemory extends BaseMemory {\n\tprivate isConnected = false;\n\n\tconstructor(uri: string) {\n\t\tsuper();\n\t\tthis.connect(uri);\n\t}\n\n\tpublic async connect(uri: string): Promise<void> {\n\t\tif (this.isConnected) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tawait mongoose.connect(uri);\n\t\t\tthis.isConnected = true;\n\t\t\tloggers.agent.info(\"MongoDB connected successfully\");\n\t\t} catch (error) {\n\t\t\tloggers.agent.error(\"Failed to connect to MongoDB:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tpublic async disconnect(): Promise<void> {\n\t\tif (!this.isConnected) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tawait mongoose.disconnect();\n\t\t\tthis.isConnected = false;\n\t\t\tloggers.agent.info(\"MongoDB disconnected successfully\");\n\t\t} catch (error) {\n\t\t\tloggers.agent.error(\"Failed to disconnect from MongoDB:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tpublic async getSessionHistory(sessionId: string): Promise<SessionObject> {\n\t\tconst chats = await ChatModel.find({ sessionId }).sort({\n\t\t\ttimestamp: 1,\n\t\t});\n\n\t\tloggers.agent.info(`Found ${chats.length} chats for session ${sessionId}`);\n\n\t\tconst sessionObject: SessionObject = { chats: {} };\n\t\tchats.forEach((chat: ChatDocument) => {\n\t\t\tconst chatId = chat._id?.toString() || chat.id;\n\t\t\tsessionObject.chats[chatId] = {\n\t\t\t\trole: chat.role as ChatRole,\n\t\t\t\tcontent: chat.content,\n\t\t\t\ttimestamp: chat.timestamp,\n\t\t\t\tmetadata: chat.metadata,\n\t\t\t};\n\t\t});\n\n\t\treturn sessionObject;\n\t}\n\n\tpublic async updateSessionHistory(\n\t\tsessionId: string,\n\t\tchat: ChatObject,\n\t): Promise<void> {\n\t\tloggers.agent.info(`Updating session history for session ${sessionId}`);\n\t\tloggers.agent.info(`Chat: ${JSON.stringify(chat)}`);\n\n\t\tawait ChatModel.create({\n\t\t\tsessionId,\n\t\t\trole: chat.role,\n\t\t\tcontent: chat.content,\n\t\t\ttimestamp: chat.timestamp,\n\t\t\tmetadata: chat.metadata,\n\t\t});\n\t}\n\n\tpublic async storeQueryAndIntent(\n\t\tquery: string,\n\t\tintent: string,\n\t\tsessionId: string,\n\t): Promise<void> {\n\t\t// Intent 정보를 metadata에 저장\n\t\tconst chat: ChatObject = {\n\t\t\trole: ChatRole.USER,\n\t\t\tcontent: {\n\t\t\t\ttype: \"text\",\n\t\t\t\tparts: [query],\n\t\t\t},\n\t\t\ttimestamp: Date.now(),\n\t\t\tmetadata: {\n\t\t\t\tintent,\n\t\t\t\tquery,\n\t\t\t},\n\t\t};\n\n\t\tawait this.updateSessionHistory(sessionId, chat);\n\t}\n}\n","import mongoose, { type Document, Schema } from \"mongoose\";\n\n// ChatRole enum\nexport enum ChatRole {\n\tUSER = \"USER\",\n\tSYSTEM = \"SYSTEM\",\n\tMODEL = \"MODEL\",\n}\n\n// ChatContentObject schema\nconst 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 - 개별 문서로 저장\nconst 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\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\t{\n\t\ttimestamps: true,\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\n// Export the model\nexport const ChatModel = mongoose.model<ChatDocument>(\"Chat\", ChatObjectSchema);\n"],"mappings":"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","names":["import_mongoose","ChatRole","mongoose","mongoose"]}
@@ -0,0 +1,14 @@
1
+ import { BaseMemory } from '@ainetwork/adk/modules';
2
+ import { SessionObject, ChatObject } from '@ainetwork/adk/types/memory';
3
+
4
+ declare class MongoDBMemory extends BaseMemory {
5
+ private isConnected;
6
+ constructor(uri: string);
7
+ connect(uri: string): Promise<void>;
8
+ disconnect(): Promise<void>;
9
+ getSessionHistory(sessionId: string): Promise<SessionObject>;
10
+ updateSessionHistory(sessionId: string, chat: ChatObject): Promise<void>;
11
+ storeQueryAndIntent(query: string, intent: string, sessionId: string): Promise<void>;
12
+ }
13
+
14
+ export { MongoDBMemory };
@@ -0,0 +1,14 @@
1
+ import { BaseMemory } from '@ainetwork/adk/modules';
2
+ import { SessionObject, ChatObject } from '@ainetwork/adk/types/memory';
3
+
4
+ declare class MongoDBMemory extends BaseMemory {
5
+ private isConnected;
6
+ constructor(uri: string);
7
+ connect(uri: string): Promise<void>;
8
+ disconnect(): Promise<void>;
9
+ getSessionHistory(sessionId: string): Promise<SessionObject>;
10
+ updateSessionHistory(sessionId: string, chat: ChatObject): Promise<void>;
11
+ storeQueryAndIntent(query: string, intent: string, sessionId: string): Promise<void>;
12
+ }
13
+
14
+ export { MongoDBMemory };
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
1
+ import {
2
+ ChatModel
3
+ } from "./chunk-2SB2M62A.js";
4
+
5
+ // index.ts
6
+ import { BaseMemory } from "@ainetwork/adk/modules";
7
+ import { loggers } from "@ainetwork/adk/utils/logger";
8
+ import mongoose from "mongoose";
9
+ var MongoDBMemory = class extends BaseMemory {
10
+ isConnected = false;
11
+ constructor(uri) {
12
+ super();
13
+ this.connect(uri);
14
+ }
15
+ async connect(uri) {
16
+ if (this.isConnected) {
17
+ return;
18
+ }
19
+ try {
20
+ await mongoose.connect(uri);
21
+ this.isConnected = true;
22
+ loggers.agent.info("MongoDB connected successfully");
23
+ } catch (error) {
24
+ loggers.agent.error("Failed to connect to MongoDB:", error);
25
+ throw error;
26
+ }
27
+ }
28
+ async disconnect() {
29
+ if (!this.isConnected) {
30
+ return;
31
+ }
32
+ try {
33
+ await mongoose.disconnect();
34
+ this.isConnected = false;
35
+ loggers.agent.info("MongoDB disconnected successfully");
36
+ } catch (error) {
37
+ loggers.agent.error("Failed to disconnect from MongoDB:", error);
38
+ throw error;
39
+ }
40
+ }
41
+ async getSessionHistory(sessionId) {
42
+ const chats = await ChatModel.find({ sessionId }).sort({
43
+ timestamp: 1
44
+ });
45
+ loggers.agent.info(`Found ${chats.length} chats for session ${sessionId}`);
46
+ const sessionObject = { chats: {} };
47
+ chats.forEach((chat) => {
48
+ const chatId = chat._id?.toString() || chat.id;
49
+ sessionObject.chats[chatId] = {
50
+ role: chat.role,
51
+ content: chat.content,
52
+ timestamp: chat.timestamp,
53
+ metadata: chat.metadata
54
+ };
55
+ });
56
+ return sessionObject;
57
+ }
58
+ async updateSessionHistory(sessionId, chat) {
59
+ loggers.agent.info(`Updating session history for session ${sessionId}`);
60
+ loggers.agent.info(`Chat: ${JSON.stringify(chat)}`);
61
+ await ChatModel.create({
62
+ sessionId,
63
+ role: chat.role,
64
+ content: chat.content,
65
+ timestamp: chat.timestamp,
66
+ metadata: chat.metadata
67
+ });
68
+ }
69
+ async storeQueryAndIntent(query, intent, sessionId) {
70
+ const chat = {
71
+ role: "USER" /* USER */,
72
+ content: {
73
+ type: "text",
74
+ parts: [query]
75
+ },
76
+ timestamp: Date.now(),
77
+ metadata: {
78
+ intent,
79
+ query
80
+ }
81
+ };
82
+ await this.updateSessionHistory(sessionId, chat);
83
+ }
84
+ };
85
+ export {
86
+ MongoDBMemory
87
+ };
88
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../index.ts"],"sourcesContent":["import { BaseMemory } from \"@ainetwork/adk/modules\";\nimport type { ChatObject, SessionObject } from \"@ainetwork/adk/types/memory\";\nimport { loggers } from \"@ainetwork/adk/utils/logger\";\nimport mongoose from \"mongoose\";\nimport {\n\ttype ChatDocument,\n\tChatModel,\n\tChatRole,\n} from \"./models/chats.model\";\n\nexport class MongoDBMemory extends BaseMemory {\n\tprivate isConnected = false;\n\n\tconstructor(uri: string) {\n\t\tsuper();\n\t\tthis.connect(uri);\n\t}\n\n\tpublic async connect(uri: string): Promise<void> {\n\t\tif (this.isConnected) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tawait mongoose.connect(uri);\n\t\t\tthis.isConnected = true;\n\t\t\tloggers.agent.info(\"MongoDB connected successfully\");\n\t\t} catch (error) {\n\t\t\tloggers.agent.error(\"Failed to connect to MongoDB:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tpublic async disconnect(): Promise<void> {\n\t\tif (!this.isConnected) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tawait mongoose.disconnect();\n\t\t\tthis.isConnected = false;\n\t\t\tloggers.agent.info(\"MongoDB disconnected successfully\");\n\t\t} catch (error) {\n\t\t\tloggers.agent.error(\"Failed to disconnect from MongoDB:\", error);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tpublic async getSessionHistory(sessionId: string): Promise<SessionObject> {\n\t\tconst chats = await ChatModel.find({ sessionId }).sort({\n\t\t\ttimestamp: 1,\n\t\t});\n\n\t\tloggers.agent.info(`Found ${chats.length} chats for session ${sessionId}`);\n\n\t\tconst sessionObject: SessionObject = { chats: {} };\n\t\tchats.forEach((chat: ChatDocument) => {\n\t\t\tconst chatId = chat._id?.toString() || chat.id;\n\t\t\tsessionObject.chats[chatId] = {\n\t\t\t\trole: chat.role as ChatRole,\n\t\t\t\tcontent: chat.content,\n\t\t\t\ttimestamp: chat.timestamp,\n\t\t\t\tmetadata: chat.metadata,\n\t\t\t};\n\t\t});\n\n\t\treturn sessionObject;\n\t}\n\n\tpublic async updateSessionHistory(\n\t\tsessionId: string,\n\t\tchat: ChatObject,\n\t): Promise<void> {\n\t\tloggers.agent.info(`Updating session history for session ${sessionId}`);\n\t\tloggers.agent.info(`Chat: ${JSON.stringify(chat)}`);\n\n\t\tawait ChatModel.create({\n\t\t\tsessionId,\n\t\t\trole: chat.role,\n\t\t\tcontent: chat.content,\n\t\t\ttimestamp: chat.timestamp,\n\t\t\tmetadata: chat.metadata,\n\t\t});\n\t}\n\n\tpublic async storeQueryAndIntent(\n\t\tquery: string,\n\t\tintent: string,\n\t\tsessionId: string,\n\t): Promise<void> {\n\t\t// Intent 정보를 metadata에 저장\n\t\tconst chat: ChatObject = {\n\t\t\trole: ChatRole.USER,\n\t\t\tcontent: {\n\t\t\t\ttype: \"text\",\n\t\t\t\tparts: [query],\n\t\t\t},\n\t\t\ttimestamp: Date.now(),\n\t\t\tmetadata: {\n\t\t\t\tintent,\n\t\t\t\tquery,\n\t\t\t},\n\t\t};\n\n\t\tawait this.updateSessionHistory(sessionId, chat);\n\t}\n}\n"],"mappings":";;;;;AAAA,SAAS,kBAAkB;AAE3B,SAAS,eAAe;AACxB,OAAO,cAAc;AAOd,IAAM,gBAAN,cAA4B,WAAW;AAAA,EACrC,cAAc;AAAA,EAEtB,YAAY,KAAa;AACxB,UAAM;AACN,SAAK,QAAQ,GAAG;AAAA,EACjB;AAAA,EAEA,MAAa,QAAQ,KAA4B;AAChD,QAAI,KAAK,aAAa;AACrB;AAAA,IACD;AAEA,QAAI;AACH,YAAM,SAAS,QAAQ,GAAG;AAC1B,WAAK,cAAc;AACnB,cAAQ,MAAM,KAAK,gCAAgC;AAAA,IACpD,SAAS,OAAO;AACf,cAAQ,MAAM,MAAM,iCAAiC,KAAK;AAC1D,YAAM;AAAA,IACP;AAAA,EACD;AAAA,EAEA,MAAa,aAA4B;AACxC,QAAI,CAAC,KAAK,aAAa;AACtB;AAAA,IACD;AAEA,QAAI;AACH,YAAM,SAAS,WAAW;AAC1B,WAAK,cAAc;AACnB,cAAQ,MAAM,KAAK,mCAAmC;AAAA,IACvD,SAAS,OAAO;AACf,cAAQ,MAAM,MAAM,sCAAsC,KAAK;AAC/D,YAAM;AAAA,IACP;AAAA,EACD;AAAA,EAEA,MAAa,kBAAkB,WAA2C;AACzE,UAAM,QAAQ,MAAM,UAAU,KAAK,EAAE,UAAU,CAAC,EAAE,KAAK;AAAA,MACtD,WAAW;AAAA,IACZ,CAAC;AAED,YAAQ,MAAM,KAAK,SAAS,MAAM,MAAM,sBAAsB,SAAS,EAAE;AAEzE,UAAM,gBAA+B,EAAE,OAAO,CAAC,EAAE;AACjD,UAAM,QAAQ,CAAC,SAAuB;AACrC,YAAM,SAAS,KAAK,KAAK,SAAS,KAAK,KAAK;AAC5C,oBAAc,MAAM,MAAM,IAAI;AAAA,QAC7B,MAAM,KAAK;AAAA,QACX,SAAS,KAAK;AAAA,QACd,WAAW,KAAK;AAAA,QAChB,UAAU,KAAK;AAAA,MAChB;AAAA,IACD,CAAC;AAED,WAAO;AAAA,EACR;AAAA,EAEA,MAAa,qBACZ,WACA,MACgB;AAChB,YAAQ,MAAM,KAAK,wCAAwC,SAAS,EAAE;AACtE,YAAQ,MAAM,KAAK,SAAS,KAAK,UAAU,IAAI,CAAC,EAAE;AAElD,UAAM,UAAU,OAAO;AAAA,MACtB;AAAA,MACA,MAAM,KAAK;AAAA,MACX,SAAS,KAAK;AAAA,MACd,WAAW,KAAK;AAAA,MAChB,UAAU,KAAK;AAAA,IAChB,CAAC;AAAA,EACF;AAAA,EAEA,MAAa,oBACZ,OACA,QACA,WACgB;AAEhB,UAAM,OAAmB;AAAA,MACxB;AAAA,MACA,SAAS;AAAA,QACR,MAAM;AAAA,QACN,OAAO,CAAC,KAAK;AAAA,MACd;AAAA,MACA,WAAW,KAAK,IAAI;AAAA,MACpB,UAAU;AAAA,QACT;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAEA,UAAM,KAAK,qBAAqB,WAAW,IAAI;AAAA,EAChD;AACD;","names":[]}
@@ -0,0 +1,86 @@
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/chats.model.ts
31
+ var chats_model_exports = {};
32
+ __export(chats_model_exports, {
33
+ ChatModel: () => ChatModel,
34
+ ChatRole: () => ChatRole
35
+ });
36
+ module.exports = __toCommonJS(chats_model_exports);
37
+ var import_mongoose = __toESM(require("mongoose"), 1);
38
+ var ChatRole = /* @__PURE__ */ ((ChatRole2) => {
39
+ ChatRole2["USER"] = "USER";
40
+ ChatRole2["SYSTEM"] = "SYSTEM";
41
+ ChatRole2["MODEL"] = "MODEL";
42
+ return ChatRole2;
43
+ })(ChatRole || {});
44
+ var ChatContentObjectSchema = new import_mongoose.Schema(
45
+ {
46
+ type: { type: String, required: true },
47
+ parts: { type: [import_mongoose.Schema.Types.Mixed], required: true }
48
+ },
49
+ { _id: false }
50
+ );
51
+ var ChatObjectSchema = new import_mongoose.Schema(
52
+ {
53
+ sessionId: {
54
+ type: String,
55
+ required: true,
56
+ index: true
57
+ },
58
+ role: {
59
+ type: String,
60
+ enum: Object.values(ChatRole),
61
+ required: true
62
+ },
63
+ content: {
64
+ type: ChatContentObjectSchema,
65
+ required: true
66
+ },
67
+ timestamp: {
68
+ type: Number,
69
+ required: true
70
+ },
71
+ metadata: {
72
+ type: import_mongoose.Schema.Types.Mixed,
73
+ default: {}
74
+ }
75
+ },
76
+ {
77
+ timestamps: true
78
+ }
79
+ );
80
+ var ChatModel = import_mongoose.default.model("Chat", ChatObjectSchema);
81
+ // Annotate the CommonJS export names for ESM import in node:
82
+ 0 && (module.exports = {
83
+ ChatModel,
84
+ ChatRole
85
+ });
86
+ //# sourceMappingURL=chats.model.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../models/chats.model.ts"],"sourcesContent":["import mongoose, { type Document, Schema } from \"mongoose\";\n\n// ChatRole enum\nexport enum ChatRole {\n\tUSER = \"USER\",\n\tSYSTEM = \"SYSTEM\",\n\tMODEL = \"MODEL\",\n}\n\n// ChatContentObject schema\nconst 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 - 개별 문서로 저장\nconst 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\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\t{\n\t\ttimestamps: true,\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\n// Export the model\nexport const ChatModel = mongoose.model<ChatDocument>(\"Chat\", ChatObjectSchema);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAgD;AAGzC,IAAK,WAAL,kBAAKA,cAAL;AACN,EAAAA,UAAA,UAAO;AACP,EAAAA,UAAA,YAAS;AACT,EAAAA,UAAA,WAAQ;AAHG,SAAAA;AAAA,GAAA;AAOZ,IAAM,0BAA0B,IAAI;AAAA,EACnC;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;AAGA,IAAM,mBAAmB,IAAI;AAAA,EAC5B;AAAA,IACC,WAAW;AAAA,MACV,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;AAAA,EACA;AAAA,IACC,YAAY;AAAA,EACb;AACD;AAiBO,IAAM,YAAY,gBAAAC,QAAS,MAAoB,QAAQ,gBAAgB;","names":["ChatRole","mongoose"]}
@@ -0,0 +1,28 @@
1
+ import mongoose, { Document } from 'mongoose';
2
+
3
+ declare enum ChatRole {
4
+ USER = "USER",
5
+ SYSTEM = "SYSTEM",
6
+ MODEL = "MODEL"
7
+ }
8
+ interface ChatDocument extends Document {
9
+ sessionId: string;
10
+ role: ChatRole;
11
+ content: {
12
+ type: string;
13
+ parts: any[];
14
+ };
15
+ timestamp: number;
16
+ metadata?: {
17
+ [key: string]: unknown;
18
+ };
19
+ createdAt: Date;
20
+ updatedAt: Date;
21
+ }
22
+ declare const ChatModel: mongoose.Model<ChatDocument, {}, {}, {}, mongoose.Document<unknown, {}, ChatDocument, {}> & ChatDocument & Required<{
23
+ _id: unknown;
24
+ }> & {
25
+ __v: number;
26
+ }, any>;
27
+
28
+ export { type ChatDocument, ChatModel, ChatRole };
@@ -0,0 +1,28 @@
1
+ import mongoose, { Document } from 'mongoose';
2
+
3
+ declare enum ChatRole {
4
+ USER = "USER",
5
+ SYSTEM = "SYSTEM",
6
+ MODEL = "MODEL"
7
+ }
8
+ interface ChatDocument extends Document {
9
+ sessionId: string;
10
+ role: ChatRole;
11
+ content: {
12
+ type: string;
13
+ parts: any[];
14
+ };
15
+ timestamp: number;
16
+ metadata?: {
17
+ [key: string]: unknown;
18
+ };
19
+ createdAt: Date;
20
+ updatedAt: Date;
21
+ }
22
+ declare const ChatModel: mongoose.Model<ChatDocument, {}, {}, {}, mongoose.Document<unknown, {}, ChatDocument, {}> & ChatDocument & Required<{
23
+ _id: unknown;
24
+ }> & {
25
+ __v: number;
26
+ }, any>;
27
+
28
+ export { type ChatDocument, ChatModel, ChatRole };
@@ -0,0 +1,9 @@
1
+ import {
2
+ ChatModel,
3
+ ChatRole
4
+ } from "../chunk-2SB2M62A.js";
5
+ export {
6
+ ChatModel,
7
+ ChatRole
8
+ };
9
+ //# sourceMappingURL=chats.model.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,69 @@
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/intentTriggeringInfos.model.ts
31
+ var intentTriggeringInfos_model_exports = {};
32
+ __export(intentTriggeringInfos_model_exports, {
33
+ IntentTriggeringInfoModel: () => IntentTriggeringInfoModel
34
+ });
35
+ module.exports = __toCommonJS(intentTriggeringInfos_model_exports);
36
+ var import_mongoose = __toESM(require("mongoose"), 1);
37
+ var MessageSchema = new import_mongoose.Schema(
38
+ {
39
+ role: {
40
+ type: String,
41
+ enum: ["system", "user", "assistant", "tool", "function"],
42
+ required: true
43
+ },
44
+ content: {
45
+ type: import_mongoose.Schema.Types.Mixed,
46
+ // string 또는 object array
47
+ required: true
48
+ }
49
+ },
50
+ { _id: false }
51
+ );
52
+ var IntentTriggeringInfoSchema = new import_mongoose.Schema({
53
+ context: {
54
+ messages: [MessageSchema]
55
+ },
56
+ intent: {
57
+ name: { type: String, required: true },
58
+ description: { type: String, required: true }
59
+ }
60
+ });
61
+ var IntentTriggeringInfoModel = import_mongoose.default.model(
62
+ "IntentTriggeringInfo",
63
+ IntentTriggeringInfoSchema
64
+ );
65
+ // Annotate the CommonJS export names for ESM import in node:
66
+ 0 && (module.exports = {
67
+ IntentTriggeringInfoModel
68
+ });
69
+ //# sourceMappingURL=intentTriggeringInfos.model.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../models/intentTriggeringInfos.model.ts"],"sourcesContent":["import mongoose, { type Document, Schema } from \"mongoose\";\n\nconst MessageSchema = new Schema(\n\t{\n\t\trole: {\n\t\t\ttype: String,\n\t\t\tenum: [\"system\", \"user\", \"assistant\", \"tool\", \"function\"],\n\t\t\trequired: true,\n\t\t},\n\t\tcontent: {\n\t\t\ttype: Schema.Types.Mixed, // string 또는 object array\n\t\t\trequired: true,\n\t\t},\n\t},\n\t{ _id: false },\n);\n\nexport interface IntentTriggeringInfoDocument extends Document {\n\tcontext: {\n\t\tmessages: Array<{\n\t\t\trole: string;\n\t\t\tcontent:\n\t\t\t\t| string\n\t\t\t\t| Array<\n\t\t\t\t\t\t| { type: \"text\"; text: string }\n\t\t\t\t\t\t| { type: \"image_url\"; image_url: { url: string } }\n\t\t\t\t >;\n\t\t}>;\n\t};\n\tintent: {\n\t\tname: string;\n\t\tdescription: string;\n\t};\n}\n\nconst IntentTriggeringInfoSchema = new Schema<IntentTriggeringInfoDocument>({\n\tcontext: {\n\t\tmessages: [MessageSchema],\n\t},\n\tintent: {\n\t\tname: { type: String, required: true },\n\t\tdescription: { type: String, required: true },\n\t},\n});\n\nexport const IntentTriggeringInfoModel =\n\tmongoose.model<IntentTriggeringInfoDocument>(\n\t\t\"IntentTriggeringInfo\",\n\t\tIntentTriggeringInfoSchema,\n\t);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAgD;AAEhD,IAAM,gBAAgB,IAAI;AAAA,EACzB;AAAA,IACC,MAAM;AAAA,MACL,MAAM;AAAA,MACN,MAAM,CAAC,UAAU,QAAQ,aAAa,QAAQ,UAAU;AAAA,MACxD,UAAU;AAAA,IACX;AAAA,IACA,SAAS;AAAA,MACR,MAAM,uBAAO,MAAM;AAAA;AAAA,MACnB,UAAU;AAAA,IACX;AAAA,EACD;AAAA,EACA,EAAE,KAAK,MAAM;AACd;AAoBA,IAAM,6BAA6B,IAAI,uBAAqC;AAAA,EAC3E,SAAS;AAAA,IACR,UAAU,CAAC,aAAa;AAAA,EACzB;AAAA,EACA,QAAQ;AAAA,IACP,MAAM,EAAE,MAAM,QAAQ,UAAU,KAAK;AAAA,IACrC,aAAa,EAAE,MAAM,QAAQ,UAAU,KAAK;AAAA,EAC7C;AACD,CAAC;AAEM,IAAM,4BACZ,gBAAAA,QAAS;AAAA,EACR;AAAA,EACA;AACD;","names":["mongoose"]}
@@ -0,0 +1,29 @@
1
+ import mongoose, { Document } from 'mongoose';
2
+
3
+ interface IntentTriggeringInfoDocument extends Document {
4
+ context: {
5
+ messages: Array<{
6
+ role: string;
7
+ content: string | Array<{
8
+ type: "text";
9
+ text: string;
10
+ } | {
11
+ type: "image_url";
12
+ image_url: {
13
+ url: string;
14
+ };
15
+ }>;
16
+ }>;
17
+ };
18
+ intent: {
19
+ name: string;
20
+ description: string;
21
+ };
22
+ }
23
+ declare const IntentTriggeringInfoModel: mongoose.Model<IntentTriggeringInfoDocument, {}, {}, {}, mongoose.Document<unknown, {}, IntentTriggeringInfoDocument, {}> & IntentTriggeringInfoDocument & Required<{
24
+ _id: unknown;
25
+ }> & {
26
+ __v: number;
27
+ }, any>;
28
+
29
+ export { type IntentTriggeringInfoDocument, IntentTriggeringInfoModel };
@@ -0,0 +1,29 @@
1
+ import mongoose, { Document } from 'mongoose';
2
+
3
+ interface IntentTriggeringInfoDocument extends Document {
4
+ context: {
5
+ messages: Array<{
6
+ role: string;
7
+ content: string | Array<{
8
+ type: "text";
9
+ text: string;
10
+ } | {
11
+ type: "image_url";
12
+ image_url: {
13
+ url: string;
14
+ };
15
+ }>;
16
+ }>;
17
+ };
18
+ intent: {
19
+ name: string;
20
+ description: string;
21
+ };
22
+ }
23
+ declare const IntentTriggeringInfoModel: mongoose.Model<IntentTriggeringInfoDocument, {}, {}, {}, mongoose.Document<unknown, {}, IntentTriggeringInfoDocument, {}> & IntentTriggeringInfoDocument & Required<{
24
+ _id: unknown;
25
+ }> & {
26
+ __v: number;
27
+ }, any>;
28
+
29
+ export { type IntentTriggeringInfoDocument, IntentTriggeringInfoModel };
@@ -0,0 +1,34 @@
1
+ // models/intentTriggeringInfos.model.ts
2
+ import mongoose, { Schema } from "mongoose";
3
+ var MessageSchema = new Schema(
4
+ {
5
+ role: {
6
+ type: String,
7
+ enum: ["system", "user", "assistant", "tool", "function"],
8
+ required: true
9
+ },
10
+ content: {
11
+ type: Schema.Types.Mixed,
12
+ // string 또는 object array
13
+ required: true
14
+ }
15
+ },
16
+ { _id: false }
17
+ );
18
+ var IntentTriggeringInfoSchema = new Schema({
19
+ context: {
20
+ messages: [MessageSchema]
21
+ },
22
+ intent: {
23
+ name: { type: String, required: true },
24
+ description: { type: String, required: true }
25
+ }
26
+ });
27
+ var IntentTriggeringInfoModel = mongoose.model(
28
+ "IntentTriggeringInfo",
29
+ IntentTriggeringInfoSchema
30
+ );
31
+ export {
32
+ IntentTriggeringInfoModel
33
+ };
34
+ //# sourceMappingURL=intentTriggeringInfos.model.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../models/intentTriggeringInfos.model.ts"],"sourcesContent":["import mongoose, { type Document, Schema } from \"mongoose\";\n\nconst MessageSchema = new Schema(\n\t{\n\t\trole: {\n\t\t\ttype: String,\n\t\t\tenum: [\"system\", \"user\", \"assistant\", \"tool\", \"function\"],\n\t\t\trequired: true,\n\t\t},\n\t\tcontent: {\n\t\t\ttype: Schema.Types.Mixed, // string 또는 object array\n\t\t\trequired: true,\n\t\t},\n\t},\n\t{ _id: false },\n);\n\nexport interface IntentTriggeringInfoDocument extends Document {\n\tcontext: {\n\t\tmessages: Array<{\n\t\t\trole: string;\n\t\t\tcontent:\n\t\t\t\t| string\n\t\t\t\t| Array<\n\t\t\t\t\t\t| { type: \"text\"; text: string }\n\t\t\t\t\t\t| { type: \"image_url\"; image_url: { url: string } }\n\t\t\t\t >;\n\t\t}>;\n\t};\n\tintent: {\n\t\tname: string;\n\t\tdescription: string;\n\t};\n}\n\nconst IntentTriggeringInfoSchema = new Schema<IntentTriggeringInfoDocument>({\n\tcontext: {\n\t\tmessages: [MessageSchema],\n\t},\n\tintent: {\n\t\tname: { type: String, required: true },\n\t\tdescription: { type: String, required: true },\n\t},\n});\n\nexport const IntentTriggeringInfoModel =\n\tmongoose.model<IntentTriggeringInfoDocument>(\n\t\t\"IntentTriggeringInfo\",\n\t\tIntentTriggeringInfoSchema,\n\t);\n"],"mappings":";AAAA,OAAO,YAA2B,cAAc;AAEhD,IAAM,gBAAgB,IAAI;AAAA,EACzB;AAAA,IACC,MAAM;AAAA,MACL,MAAM;AAAA,MACN,MAAM,CAAC,UAAU,QAAQ,aAAa,QAAQ,UAAU;AAAA,MACxD,UAAU;AAAA,IACX;AAAA,IACA,SAAS;AAAA,MACR,MAAM,OAAO,MAAM;AAAA;AAAA,MACnB,UAAU;AAAA,IACX;AAAA,EACD;AAAA,EACA,EAAE,KAAK,MAAM;AACd;AAoBA,IAAM,6BAA6B,IAAI,OAAqC;AAAA,EAC3E,SAAS;AAAA,IACR,UAAU,CAAC,aAAa;AAAA,EACzB;AAAA,EACA,QAAQ;AAAA,IACP,MAAM,EAAE,MAAM,QAAQ,UAAU,KAAK;AAAA,IACrC,aAAa,EAAE,MAAM,QAAQ,UAAU,KAAK;AAAA,EAC7C;AACD,CAAC;AAEM,IAAM,4BACZ,SAAS;AAAA,EACR;AAAA,EACA;AACD;","names":[]}
package/index.ts ADDED
@@ -0,0 +1,107 @@
1
+ import { BaseMemory } from "@ainetwork/adk/modules";
2
+ import type { ChatObject, SessionObject } from "@ainetwork/adk/types/memory";
3
+ import { loggers } from "@ainetwork/adk/utils/logger";
4
+ import mongoose from "mongoose";
5
+ import {
6
+ type ChatDocument,
7
+ ChatModel,
8
+ ChatRole,
9
+ } from "./models/chats.model";
10
+
11
+ export class MongoDBMemory extends BaseMemory {
12
+ private isConnected = false;
13
+
14
+ constructor(uri: string) {
15
+ super();
16
+ this.connect(uri);
17
+ }
18
+
19
+ public async connect(uri: string): Promise<void> {
20
+ if (this.isConnected) {
21
+ return;
22
+ }
23
+
24
+ try {
25
+ await mongoose.connect(uri);
26
+ this.isConnected = true;
27
+ loggers.agent.info("MongoDB connected successfully");
28
+ } catch (error) {
29
+ loggers.agent.error("Failed to connect to MongoDB:", error);
30
+ throw error;
31
+ }
32
+ }
33
+
34
+ public async disconnect(): Promise<void> {
35
+ if (!this.isConnected) {
36
+ return;
37
+ }
38
+
39
+ try {
40
+ await mongoose.disconnect();
41
+ this.isConnected = false;
42
+ loggers.agent.info("MongoDB disconnected successfully");
43
+ } catch (error) {
44
+ loggers.agent.error("Failed to disconnect from MongoDB:", error);
45
+ throw error;
46
+ }
47
+ }
48
+
49
+ public async getSessionHistory(sessionId: string): Promise<SessionObject> {
50
+ const chats = await ChatModel.find({ sessionId }).sort({
51
+ timestamp: 1,
52
+ });
53
+
54
+ loggers.agent.info(`Found ${chats.length} chats for session ${sessionId}`);
55
+
56
+ const sessionObject: SessionObject = { chats: {} };
57
+ chats.forEach((chat: ChatDocument) => {
58
+ const chatId = chat._id?.toString() || chat.id;
59
+ sessionObject.chats[chatId] = {
60
+ role: chat.role as ChatRole,
61
+ content: chat.content,
62
+ timestamp: chat.timestamp,
63
+ metadata: chat.metadata,
64
+ };
65
+ });
66
+
67
+ return sessionObject;
68
+ }
69
+
70
+ public async updateSessionHistory(
71
+ sessionId: string,
72
+ chat: ChatObject,
73
+ ): Promise<void> {
74
+ loggers.agent.info(`Updating session history for session ${sessionId}`);
75
+ loggers.agent.info(`Chat: ${JSON.stringify(chat)}`);
76
+
77
+ await ChatModel.create({
78
+ sessionId,
79
+ role: chat.role,
80
+ content: chat.content,
81
+ timestamp: chat.timestamp,
82
+ metadata: chat.metadata,
83
+ });
84
+ }
85
+
86
+ public async storeQueryAndIntent(
87
+ query: string,
88
+ intent: string,
89
+ sessionId: string,
90
+ ): Promise<void> {
91
+ // Intent 정보를 metadata에 저장
92
+ const chat: ChatObject = {
93
+ role: ChatRole.USER,
94
+ content: {
95
+ type: "text",
96
+ parts: [query],
97
+ },
98
+ timestamp: Date.now(),
99
+ metadata: {
100
+ intent,
101
+ query,
102
+ },
103
+ };
104
+
105
+ await this.updateSessionHistory(sessionId, chat);
106
+ }
107
+ }
@@ -0,0 +1,65 @@
1
+ import mongoose, { type Document, Schema } from "mongoose";
2
+
3
+ // ChatRole enum
4
+ export enum ChatRole {
5
+ USER = "USER",
6
+ SYSTEM = "SYSTEM",
7
+ MODEL = "MODEL",
8
+ }
9
+
10
+ // ChatContentObject schema
11
+ const ChatContentObjectSchema = new Schema(
12
+ {
13
+ type: { type: String, required: true },
14
+ parts: { type: [Schema.Types.Mixed], required: true },
15
+ },
16
+ { _id: false },
17
+ );
18
+
19
+ // ChatObject schema - 개별 문서로 저장
20
+ const ChatObjectSchema = new Schema(
21
+ {
22
+ sessionId: {
23
+ type: String,
24
+ required: true,
25
+ index: true,
26
+ },
27
+ role: {
28
+ type: String,
29
+ enum: Object.values(ChatRole),
30
+ required: true,
31
+ },
32
+ content: {
33
+ type: ChatContentObjectSchema,
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
+ timestamps: true,
47
+ },
48
+ );
49
+
50
+ // Chat Document interface
51
+ export interface ChatDocument extends Document {
52
+ sessionId: string;
53
+ role: ChatRole;
54
+ content: {
55
+ type: string;
56
+ parts: any[];
57
+ };
58
+ timestamp: number;
59
+ metadata?: { [key: string]: unknown };
60
+ createdAt: Date;
61
+ updatedAt: Date;
62
+ }
63
+
64
+ // Export the model
65
+ export const ChatModel = mongoose.model<ChatDocument>("Chat", ChatObjectSchema);
@@ -0,0 +1,50 @@
1
+ import mongoose, { type Document, Schema } from "mongoose";
2
+
3
+ const MessageSchema = new Schema(
4
+ {
5
+ role: {
6
+ type: String,
7
+ enum: ["system", "user", "assistant", "tool", "function"],
8
+ required: true,
9
+ },
10
+ content: {
11
+ type: Schema.Types.Mixed, // string 또는 object array
12
+ required: true,
13
+ },
14
+ },
15
+ { _id: false },
16
+ );
17
+
18
+ export interface IntentTriggeringInfoDocument extends Document {
19
+ context: {
20
+ messages: Array<{
21
+ role: string;
22
+ content:
23
+ | string
24
+ | Array<
25
+ | { type: "text"; text: string }
26
+ | { type: "image_url"; image_url: { url: string } }
27
+ >;
28
+ }>;
29
+ };
30
+ intent: {
31
+ name: string;
32
+ description: string;
33
+ };
34
+ }
35
+
36
+ const IntentTriggeringInfoSchema = new Schema<IntentTriggeringInfoDocument>({
37
+ context: {
38
+ messages: [MessageSchema],
39
+ },
40
+ intent: {
41
+ name: { type: String, required: true },
42
+ description: { type: String, required: true },
43
+ },
44
+ });
45
+
46
+ export const IntentTriggeringInfoModel =
47
+ mongoose.model<IntentTriggeringInfoDocument>(
48
+ "IntentTriggeringInfo",
49
+ IntentTriggeringInfoSchema,
50
+ );
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@ainetwork/adk-provider-memory-mongodb",
3
+ "version": "0.1.1",
4
+ "author": "AI Network (https://ainetwork.ai)",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=20"
8
+ },
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ },
18
+ "./models/*": {
19
+ "types": "./dist/models/*.d.ts",
20
+ "import": "./dist/models/*.js",
21
+ "require": "./dist/models/*.cjs"
22
+ }
23
+ },
24
+ "scripts": {
25
+ "build": "tsup",
26
+ "clean": "rm -rf dist"
27
+ },
28
+ "dependencies": {
29
+ "@ainetwork/adk": "^0.1.2",
30
+ "mongoose": "^8.16.5"
31
+ },
32
+ "devDependencies": {
33
+ "typescript": "^5.0.0"
34
+ },
35
+ "license": "MIT",
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "gitHead": "cb6205738fd84533ce56bbeeab00ea9dfe2aaf3a"
40
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": ".",
6
+ },
7
+ "include": ["index.ts", "models/**/*.ts"],
8
+ "exclude": ["node_modules", "dist"]
9
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from 'tsup'
2
+
3
+ export default defineConfig({
4
+ entry: ['index.ts', 'models/**/*.ts'],
5
+ format: ['cjs', 'esm'],
6
+ dts: true,
7
+ sourcemap: true,
8
+ clean: true,
9
+ })