@snap-agent/core 0.1.4 → 0.1.5
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-4NN3ADDP.mjs +0 -0
- package/dist/chunk-52OS4FTG.mjs +382 -0
- package/dist/chunk-5BAE6S5E.js +382 -0
- package/dist/chunk-BSSVNLSO.mjs +157 -0
- package/dist/chunk-DKNHIS7Z.js +2227 -0
- package/dist/chunk-DQRG2GHR.js +157 -0
- package/dist/chunk-GHRDUPHS.js +1 -0
- package/dist/chunk-JQK3HEJ5.mjs +268 -0
- package/dist/chunk-JRIKISMX.js +268 -0
- package/dist/dist-GSDAIZQP.js +1825 -0
- package/dist/dist-HTV3MRC3.js +3216 -0
- package/dist/{index-m2vDW79n.d.mts → index-Ek8b39d1.d.mts} +1 -173
- package/dist/{index-m2vDW79n.d.ts → index-Ek8b39d1.d.ts} +1 -173
- package/dist/index.d.mts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +1270 -9400
- package/dist/index.mjs +8 -3
- package/dist/storage/MemoryStorage.d.mts +46 -0
- package/dist/storage/MemoryStorage.d.ts +46 -0
- package/dist/storage/MemoryStorage.js +6 -0
- package/dist/storage/MemoryStorage.mjs +6 -0
- package/dist/storage/MongoDBStorage.d.mts +44 -0
- package/dist/storage/MongoDBStorage.d.ts +44 -0
- package/dist/storage/MongoDBStorage.js +6 -0
- package/dist/storage/MongoDBStorage.mjs +6 -0
- package/dist/storage/UpstashStorage.d.mts +91 -0
- package/dist/storage/UpstashStorage.d.ts +91 -0
- package/dist/storage/UpstashStorage.js +6 -0
- package/dist/storage/UpstashStorage.mjs +6 -0
- package/dist/storage/index.d.mts +4 -1
- package/dist/storage/index.d.ts +4 -1
- package/dist/storage/index.js +11 -827
- package/dist/storage/index.mjs +8 -3
- package/package.json +18 -3
- package/dist/chunk-FS7G3ID4.mjs +0 -803
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/storage/MemoryStorage.ts
|
|
2
|
+
var MemoryStorage = class {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.agents = /* @__PURE__ */ new Map();
|
|
5
|
+
this.threads = /* @__PURE__ */ new Map();
|
|
6
|
+
this.idCounter = 0;
|
|
7
|
+
}
|
|
8
|
+
generateId() {
|
|
9
|
+
return `${Date.now()}-${++this.idCounter}`;
|
|
10
|
+
}
|
|
11
|
+
// ============================================================================
|
|
12
|
+
// Agent Operations
|
|
13
|
+
// ============================================================================
|
|
14
|
+
async createAgent(config) {
|
|
15
|
+
const id = this.generateId();
|
|
16
|
+
const agent = {
|
|
17
|
+
id,
|
|
18
|
+
...config,
|
|
19
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
20
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
21
|
+
files: []
|
|
22
|
+
};
|
|
23
|
+
this.agents.set(id, agent);
|
|
24
|
+
return id;
|
|
25
|
+
}
|
|
26
|
+
async getAgent(agentId) {
|
|
27
|
+
return this.agents.get(agentId) || null;
|
|
28
|
+
}
|
|
29
|
+
async updateAgent(agentId, updates) {
|
|
30
|
+
const agent = this.agents.get(agentId);
|
|
31
|
+
if (!agent) return;
|
|
32
|
+
Object.assign(agent, updates, { updatedAt: /* @__PURE__ */ new Date() });
|
|
33
|
+
this.agents.set(agentId, agent);
|
|
34
|
+
}
|
|
35
|
+
async deleteAgent(agentId) {
|
|
36
|
+
this.agents.delete(agentId);
|
|
37
|
+
const threadsToDelete = [];
|
|
38
|
+
for (const [threadId, thread] of this.threads.entries()) {
|
|
39
|
+
if (thread.agentId === agentId) {
|
|
40
|
+
threadsToDelete.push(threadId);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (const threadId of threadsToDelete) {
|
|
44
|
+
this.threads.delete(threadId);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async listAgents(userId, organizationId) {
|
|
48
|
+
const agents = Array.from(this.agents.values()).filter((agent) => {
|
|
49
|
+
if (agent.userId !== userId) return false;
|
|
50
|
+
if (organizationId && agent.organizationId !== organizationId) return false;
|
|
51
|
+
return true;
|
|
52
|
+
});
|
|
53
|
+
return agents.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
|
|
54
|
+
}
|
|
55
|
+
// ============================================================================
|
|
56
|
+
// Thread Operations
|
|
57
|
+
// ============================================================================
|
|
58
|
+
async createThread(config) {
|
|
59
|
+
const id = this.generateId();
|
|
60
|
+
const thread = {
|
|
61
|
+
id,
|
|
62
|
+
...config,
|
|
63
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
64
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
65
|
+
messages: [],
|
|
66
|
+
isPendingThread: true
|
|
67
|
+
};
|
|
68
|
+
this.threads.set(id, thread);
|
|
69
|
+
return id;
|
|
70
|
+
}
|
|
71
|
+
async getThread(threadId) {
|
|
72
|
+
return this.threads.get(threadId) || null;
|
|
73
|
+
}
|
|
74
|
+
async updateThread(threadId, updates) {
|
|
75
|
+
const thread = this.threads.get(threadId);
|
|
76
|
+
if (!thread) return;
|
|
77
|
+
Object.assign(thread, updates, { updatedAt: /* @__PURE__ */ new Date() });
|
|
78
|
+
this.threads.set(threadId, thread);
|
|
79
|
+
}
|
|
80
|
+
async deleteThread(threadId) {
|
|
81
|
+
this.threads.delete(threadId);
|
|
82
|
+
}
|
|
83
|
+
async listThreads(filters) {
|
|
84
|
+
const threads = Array.from(this.threads.values()).filter((thread) => {
|
|
85
|
+
if (filters.userId && thread.userId !== filters.userId) return false;
|
|
86
|
+
if (filters.agentId && thread.agentId !== filters.agentId) return false;
|
|
87
|
+
if (filters.organizationId && thread.organizationId !== filters.organizationId) return false;
|
|
88
|
+
return true;
|
|
89
|
+
});
|
|
90
|
+
return threads.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
|
|
91
|
+
}
|
|
92
|
+
// ============================================================================
|
|
93
|
+
// Message Operations
|
|
94
|
+
// ============================================================================
|
|
95
|
+
async addMessage(threadId, role, content, attachments) {
|
|
96
|
+
const thread = this.threads.get(threadId);
|
|
97
|
+
if (!thread) {
|
|
98
|
+
throw new Error(`Thread not found: ${threadId}`);
|
|
99
|
+
}
|
|
100
|
+
const messageId = this.generateId();
|
|
101
|
+
const message = {
|
|
102
|
+
id: messageId,
|
|
103
|
+
role,
|
|
104
|
+
content,
|
|
105
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
106
|
+
attachments
|
|
107
|
+
};
|
|
108
|
+
thread.messages.push(message);
|
|
109
|
+
thread.updatedAt = /* @__PURE__ */ new Date();
|
|
110
|
+
this.threads.set(threadId, thread);
|
|
111
|
+
return messageId;
|
|
112
|
+
}
|
|
113
|
+
async getMessages(threadId, limit) {
|
|
114
|
+
const thread = this.threads.get(threadId);
|
|
115
|
+
if (!thread) return [];
|
|
116
|
+
const messages = [...thread.messages];
|
|
117
|
+
if (limit) {
|
|
118
|
+
return messages.slice(-limit);
|
|
119
|
+
}
|
|
120
|
+
return messages;
|
|
121
|
+
}
|
|
122
|
+
async getConversationContext(threadId, maxMessages = 20) {
|
|
123
|
+
const messages = await this.getMessages(threadId, maxMessages);
|
|
124
|
+
return messages.map((msg) => ({
|
|
125
|
+
role: msg.role,
|
|
126
|
+
content: msg.content
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
// ============================================================================
|
|
130
|
+
// Utility Methods
|
|
131
|
+
// ============================================================================
|
|
132
|
+
/**
|
|
133
|
+
* Clear all stored data
|
|
134
|
+
*/
|
|
135
|
+
clear() {
|
|
136
|
+
this.agents.clear();
|
|
137
|
+
this.threads.clear();
|
|
138
|
+
this.idCounter = 0;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get statistics about stored data
|
|
142
|
+
*/
|
|
143
|
+
getStats() {
|
|
144
|
+
return {
|
|
145
|
+
agents: this.agents.size,
|
|
146
|
+
threads: this.threads.size,
|
|
147
|
+
messages: Array.from(this.threads.values()).reduce(
|
|
148
|
+
(sum, thread) => sum + thread.messages.length,
|
|
149
|
+
0
|
|
150
|
+
)
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
exports.MemoryStorage = MemoryStorage;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// src/storage/MongoDBStorage.ts
|
|
2
|
+
import { MongoClient, ObjectId } from "mongodb";
|
|
3
|
+
var MongoDBStorage = class {
|
|
4
|
+
constructor(config) {
|
|
5
|
+
this.db = null;
|
|
6
|
+
if (typeof config === "string") {
|
|
7
|
+
this.config = {
|
|
8
|
+
uri: config,
|
|
9
|
+
dbName: "agentStudio",
|
|
10
|
+
agentsCollection: "v2_agents",
|
|
11
|
+
threadsCollection: "v2_threads"
|
|
12
|
+
};
|
|
13
|
+
} else {
|
|
14
|
+
this.config = {
|
|
15
|
+
uri: config.uri,
|
|
16
|
+
dbName: config.dbName || "agentStudio",
|
|
17
|
+
agentsCollection: config.agentsCollection || "v2_agents",
|
|
18
|
+
threadsCollection: config.threadsCollection || "v2_threads"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
this.client = new MongoClient(this.config.uri);
|
|
22
|
+
}
|
|
23
|
+
async ensureConnection() {
|
|
24
|
+
if (!this.db) {
|
|
25
|
+
await this.client.connect();
|
|
26
|
+
this.db = this.client.db(this.config.dbName);
|
|
27
|
+
}
|
|
28
|
+
return this.db;
|
|
29
|
+
}
|
|
30
|
+
async disconnect() {
|
|
31
|
+
await this.client.close();
|
|
32
|
+
this.db = null;
|
|
33
|
+
}
|
|
34
|
+
// ============================================================================
|
|
35
|
+
// Agent Operations
|
|
36
|
+
// ============================================================================
|
|
37
|
+
async createAgent(config) {
|
|
38
|
+
const db = await this.ensureConnection();
|
|
39
|
+
const collection = db.collection(
|
|
40
|
+
this.config.agentsCollection
|
|
41
|
+
);
|
|
42
|
+
const doc = {
|
|
43
|
+
organizationId: config.organizationId,
|
|
44
|
+
userId: config.userId,
|
|
45
|
+
phone: config.phone,
|
|
46
|
+
name: config.name,
|
|
47
|
+
description: config.description,
|
|
48
|
+
instructions: config.instructions,
|
|
49
|
+
provider: config.provider,
|
|
50
|
+
model: config.model,
|
|
51
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
52
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
53
|
+
files: [],
|
|
54
|
+
metadata: config.metadata || {},
|
|
55
|
+
pluginConfigs: config.pluginConfigs || []
|
|
56
|
+
};
|
|
57
|
+
const result = await collection.insertOne(doc);
|
|
58
|
+
return result.insertedId.toString();
|
|
59
|
+
}
|
|
60
|
+
async getAgent(agentId) {
|
|
61
|
+
const db = await this.ensureConnection();
|
|
62
|
+
const collection = db.collection(
|
|
63
|
+
this.config.agentsCollection
|
|
64
|
+
);
|
|
65
|
+
const doc = await collection.findOne({ _id: new ObjectId(agentId) });
|
|
66
|
+
if (!doc) return null;
|
|
67
|
+
return this.agentDocToData(doc);
|
|
68
|
+
}
|
|
69
|
+
async updateAgent(agentId, updates) {
|
|
70
|
+
const db = await this.ensureConnection();
|
|
71
|
+
const collection = db.collection(
|
|
72
|
+
this.config.agentsCollection
|
|
73
|
+
);
|
|
74
|
+
await collection.updateOne(
|
|
75
|
+
{ _id: new ObjectId(agentId) },
|
|
76
|
+
{
|
|
77
|
+
$set: {
|
|
78
|
+
...updates,
|
|
79
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
async deleteAgent(agentId) {
|
|
85
|
+
const db = await this.ensureConnection();
|
|
86
|
+
const collection = db.collection(
|
|
87
|
+
this.config.agentsCollection
|
|
88
|
+
);
|
|
89
|
+
await collection.deleteOne({ _id: new ObjectId(agentId) });
|
|
90
|
+
}
|
|
91
|
+
async listAgents(userId, organizationId) {
|
|
92
|
+
const db = await this.ensureConnection();
|
|
93
|
+
const collection = db.collection(
|
|
94
|
+
this.config.agentsCollection
|
|
95
|
+
);
|
|
96
|
+
const query = { userId };
|
|
97
|
+
if (organizationId) {
|
|
98
|
+
query.organizationId = organizationId;
|
|
99
|
+
}
|
|
100
|
+
const docs = await collection.find(query).sort({ updatedAt: -1 }).toArray();
|
|
101
|
+
return docs.map((doc) => this.agentDocToData(doc));
|
|
102
|
+
}
|
|
103
|
+
// ============================================================================
|
|
104
|
+
// Thread Operations
|
|
105
|
+
// ============================================================================
|
|
106
|
+
async createThread(config) {
|
|
107
|
+
const db = await this.ensureConnection();
|
|
108
|
+
const collection = db.collection(
|
|
109
|
+
this.config.threadsCollection
|
|
110
|
+
);
|
|
111
|
+
const doc = {
|
|
112
|
+
organizationId: config.organizationId,
|
|
113
|
+
agentId: config.agentId,
|
|
114
|
+
userId: config.userId,
|
|
115
|
+
endUserId: config.endUserId,
|
|
116
|
+
name: config.name,
|
|
117
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
118
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
119
|
+
messages: [],
|
|
120
|
+
isPendingThread: true,
|
|
121
|
+
metadata: config.metadata || {}
|
|
122
|
+
};
|
|
123
|
+
const result = await collection.insertOne(doc);
|
|
124
|
+
return result.insertedId.toString();
|
|
125
|
+
}
|
|
126
|
+
async getThread(threadId) {
|
|
127
|
+
const db = await this.ensureConnection();
|
|
128
|
+
const collection = db.collection(
|
|
129
|
+
this.config.threadsCollection
|
|
130
|
+
);
|
|
131
|
+
const doc = await collection.findOne({ _id: new ObjectId(threadId) });
|
|
132
|
+
if (!doc) return null;
|
|
133
|
+
return this.threadDocToData(doc);
|
|
134
|
+
}
|
|
135
|
+
async updateThread(threadId, updates) {
|
|
136
|
+
const db = await this.ensureConnection();
|
|
137
|
+
const collection = db.collection(
|
|
138
|
+
this.config.threadsCollection
|
|
139
|
+
);
|
|
140
|
+
await collection.updateOne(
|
|
141
|
+
{ _id: new ObjectId(threadId) },
|
|
142
|
+
{
|
|
143
|
+
$set: {
|
|
144
|
+
...updates,
|
|
145
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
async deleteThread(threadId) {
|
|
151
|
+
const db = await this.ensureConnection();
|
|
152
|
+
const collection = db.collection(
|
|
153
|
+
this.config.threadsCollection
|
|
154
|
+
);
|
|
155
|
+
await collection.deleteOne({ _id: new ObjectId(threadId) });
|
|
156
|
+
}
|
|
157
|
+
async listThreads(filters) {
|
|
158
|
+
const db = await this.ensureConnection();
|
|
159
|
+
const collection = db.collection(
|
|
160
|
+
this.config.threadsCollection
|
|
161
|
+
);
|
|
162
|
+
const query = {};
|
|
163
|
+
if (filters.userId) query.userId = filters.userId;
|
|
164
|
+
if (filters.agentId) query.agentId = filters.agentId;
|
|
165
|
+
if (filters.organizationId) query.organizationId = filters.organizationId;
|
|
166
|
+
const docs = await collection.find(query).sort({ updatedAt: -1 }).toArray();
|
|
167
|
+
return docs.map((doc) => this.threadDocToData(doc));
|
|
168
|
+
}
|
|
169
|
+
// ============================================================================
|
|
170
|
+
// Message Operations
|
|
171
|
+
// ============================================================================
|
|
172
|
+
async addMessage(threadId, role, content, attachments) {
|
|
173
|
+
const db = await this.ensureConnection();
|
|
174
|
+
const collection = db.collection(
|
|
175
|
+
this.config.threadsCollection
|
|
176
|
+
);
|
|
177
|
+
const messageId = new ObjectId();
|
|
178
|
+
const message = {
|
|
179
|
+
_id: messageId,
|
|
180
|
+
role,
|
|
181
|
+
content,
|
|
182
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
183
|
+
attachments
|
|
184
|
+
};
|
|
185
|
+
await collection.updateOne(
|
|
186
|
+
{ _id: new ObjectId(threadId) },
|
|
187
|
+
{
|
|
188
|
+
$push: { messages: message },
|
|
189
|
+
$set: { updatedAt: /* @__PURE__ */ new Date() }
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
return messageId.toString();
|
|
193
|
+
}
|
|
194
|
+
async getMessages(threadId, limit) {
|
|
195
|
+
const db = await this.ensureConnection();
|
|
196
|
+
const collection = db.collection(
|
|
197
|
+
this.config.threadsCollection
|
|
198
|
+
);
|
|
199
|
+
const doc = await collection.findOne({ _id: new ObjectId(threadId) });
|
|
200
|
+
if (!doc) return [];
|
|
201
|
+
let messages = doc.messages.map((msg) => ({
|
|
202
|
+
id: msg._id?.toString() || "",
|
|
203
|
+
role: msg.role,
|
|
204
|
+
content: msg.content,
|
|
205
|
+
timestamp: msg.timestamp,
|
|
206
|
+
metadata: msg.metadata,
|
|
207
|
+
attachments: msg.attachments
|
|
208
|
+
}));
|
|
209
|
+
if (limit) {
|
|
210
|
+
messages = messages.slice(-limit);
|
|
211
|
+
}
|
|
212
|
+
return messages;
|
|
213
|
+
}
|
|
214
|
+
async getConversationContext(threadId, maxMessages = 20) {
|
|
215
|
+
const messages = await this.getMessages(threadId, maxMessages);
|
|
216
|
+
return messages.map((msg) => ({
|
|
217
|
+
role: msg.role,
|
|
218
|
+
content: msg.content
|
|
219
|
+
}));
|
|
220
|
+
}
|
|
221
|
+
// ============================================================================
|
|
222
|
+
// Helper Methods
|
|
223
|
+
// ============================================================================
|
|
224
|
+
agentDocToData(doc) {
|
|
225
|
+
return {
|
|
226
|
+
id: doc._id.toString(),
|
|
227
|
+
organizationId: doc.organizationId,
|
|
228
|
+
userId: doc.userId,
|
|
229
|
+
phone: doc.phone,
|
|
230
|
+
name: doc.name,
|
|
231
|
+
description: doc.description,
|
|
232
|
+
instructions: doc.instructions,
|
|
233
|
+
provider: doc.provider,
|
|
234
|
+
model: doc.model,
|
|
235
|
+
createdAt: doc.createdAt,
|
|
236
|
+
updatedAt: doc.updatedAt,
|
|
237
|
+
files: doc.files,
|
|
238
|
+
metadata: doc.metadata,
|
|
239
|
+
pluginConfigs: doc.pluginConfigs
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
threadDocToData(doc) {
|
|
243
|
+
return {
|
|
244
|
+
id: doc._id.toString(),
|
|
245
|
+
organizationId: doc.organizationId,
|
|
246
|
+
agentId: doc.agentId,
|
|
247
|
+
userId: doc.userId,
|
|
248
|
+
endUserId: doc.endUserId,
|
|
249
|
+
name: doc.name,
|
|
250
|
+
createdAt: doc.createdAt,
|
|
251
|
+
updatedAt: doc.updatedAt,
|
|
252
|
+
messages: doc.messages.map((msg) => ({
|
|
253
|
+
id: msg._id?.toString() || "",
|
|
254
|
+
role: msg.role,
|
|
255
|
+
content: msg.content,
|
|
256
|
+
timestamp: msg.timestamp,
|
|
257
|
+
metadata: msg.metadata,
|
|
258
|
+
attachments: msg.attachments
|
|
259
|
+
})),
|
|
260
|
+
isPendingThread: doc.isPendingThread,
|
|
261
|
+
metadata: doc.metadata
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
export {
|
|
267
|
+
MongoDBStorage
|
|
268
|
+
};
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/storage/MongoDBStorage.ts
|
|
2
|
+
var _mongodb = require('mongodb');
|
|
3
|
+
var MongoDBStorage = class {
|
|
4
|
+
constructor(config) {
|
|
5
|
+
this.db = null;
|
|
6
|
+
if (typeof config === "string") {
|
|
7
|
+
this.config = {
|
|
8
|
+
uri: config,
|
|
9
|
+
dbName: "agentStudio",
|
|
10
|
+
agentsCollection: "v2_agents",
|
|
11
|
+
threadsCollection: "v2_threads"
|
|
12
|
+
};
|
|
13
|
+
} else {
|
|
14
|
+
this.config = {
|
|
15
|
+
uri: config.uri,
|
|
16
|
+
dbName: config.dbName || "agentStudio",
|
|
17
|
+
agentsCollection: config.agentsCollection || "v2_agents",
|
|
18
|
+
threadsCollection: config.threadsCollection || "v2_threads"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
this.client = new (0, _mongodb.MongoClient)(this.config.uri);
|
|
22
|
+
}
|
|
23
|
+
async ensureConnection() {
|
|
24
|
+
if (!this.db) {
|
|
25
|
+
await this.client.connect();
|
|
26
|
+
this.db = this.client.db(this.config.dbName);
|
|
27
|
+
}
|
|
28
|
+
return this.db;
|
|
29
|
+
}
|
|
30
|
+
async disconnect() {
|
|
31
|
+
await this.client.close();
|
|
32
|
+
this.db = null;
|
|
33
|
+
}
|
|
34
|
+
// ============================================================================
|
|
35
|
+
// Agent Operations
|
|
36
|
+
// ============================================================================
|
|
37
|
+
async createAgent(config) {
|
|
38
|
+
const db = await this.ensureConnection();
|
|
39
|
+
const collection = db.collection(
|
|
40
|
+
this.config.agentsCollection
|
|
41
|
+
);
|
|
42
|
+
const doc = {
|
|
43
|
+
organizationId: config.organizationId,
|
|
44
|
+
userId: config.userId,
|
|
45
|
+
phone: config.phone,
|
|
46
|
+
name: config.name,
|
|
47
|
+
description: config.description,
|
|
48
|
+
instructions: config.instructions,
|
|
49
|
+
provider: config.provider,
|
|
50
|
+
model: config.model,
|
|
51
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
52
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
53
|
+
files: [],
|
|
54
|
+
metadata: config.metadata || {},
|
|
55
|
+
pluginConfigs: config.pluginConfigs || []
|
|
56
|
+
};
|
|
57
|
+
const result = await collection.insertOne(doc);
|
|
58
|
+
return result.insertedId.toString();
|
|
59
|
+
}
|
|
60
|
+
async getAgent(agentId) {
|
|
61
|
+
const db = await this.ensureConnection();
|
|
62
|
+
const collection = db.collection(
|
|
63
|
+
this.config.agentsCollection
|
|
64
|
+
);
|
|
65
|
+
const doc = await collection.findOne({ _id: new (0, _mongodb.ObjectId)(agentId) });
|
|
66
|
+
if (!doc) return null;
|
|
67
|
+
return this.agentDocToData(doc);
|
|
68
|
+
}
|
|
69
|
+
async updateAgent(agentId, updates) {
|
|
70
|
+
const db = await this.ensureConnection();
|
|
71
|
+
const collection = db.collection(
|
|
72
|
+
this.config.agentsCollection
|
|
73
|
+
);
|
|
74
|
+
await collection.updateOne(
|
|
75
|
+
{ _id: new (0, _mongodb.ObjectId)(agentId) },
|
|
76
|
+
{
|
|
77
|
+
$set: {
|
|
78
|
+
...updates,
|
|
79
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
async deleteAgent(agentId) {
|
|
85
|
+
const db = await this.ensureConnection();
|
|
86
|
+
const collection = db.collection(
|
|
87
|
+
this.config.agentsCollection
|
|
88
|
+
);
|
|
89
|
+
await collection.deleteOne({ _id: new (0, _mongodb.ObjectId)(agentId) });
|
|
90
|
+
}
|
|
91
|
+
async listAgents(userId, organizationId) {
|
|
92
|
+
const db = await this.ensureConnection();
|
|
93
|
+
const collection = db.collection(
|
|
94
|
+
this.config.agentsCollection
|
|
95
|
+
);
|
|
96
|
+
const query = { userId };
|
|
97
|
+
if (organizationId) {
|
|
98
|
+
query.organizationId = organizationId;
|
|
99
|
+
}
|
|
100
|
+
const docs = await collection.find(query).sort({ updatedAt: -1 }).toArray();
|
|
101
|
+
return docs.map((doc) => this.agentDocToData(doc));
|
|
102
|
+
}
|
|
103
|
+
// ============================================================================
|
|
104
|
+
// Thread Operations
|
|
105
|
+
// ============================================================================
|
|
106
|
+
async createThread(config) {
|
|
107
|
+
const db = await this.ensureConnection();
|
|
108
|
+
const collection = db.collection(
|
|
109
|
+
this.config.threadsCollection
|
|
110
|
+
);
|
|
111
|
+
const doc = {
|
|
112
|
+
organizationId: config.organizationId,
|
|
113
|
+
agentId: config.agentId,
|
|
114
|
+
userId: config.userId,
|
|
115
|
+
endUserId: config.endUserId,
|
|
116
|
+
name: config.name,
|
|
117
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
118
|
+
updatedAt: /* @__PURE__ */ new Date(),
|
|
119
|
+
messages: [],
|
|
120
|
+
isPendingThread: true,
|
|
121
|
+
metadata: config.metadata || {}
|
|
122
|
+
};
|
|
123
|
+
const result = await collection.insertOne(doc);
|
|
124
|
+
return result.insertedId.toString();
|
|
125
|
+
}
|
|
126
|
+
async getThread(threadId) {
|
|
127
|
+
const db = await this.ensureConnection();
|
|
128
|
+
const collection = db.collection(
|
|
129
|
+
this.config.threadsCollection
|
|
130
|
+
);
|
|
131
|
+
const doc = await collection.findOne({ _id: new (0, _mongodb.ObjectId)(threadId) });
|
|
132
|
+
if (!doc) return null;
|
|
133
|
+
return this.threadDocToData(doc);
|
|
134
|
+
}
|
|
135
|
+
async updateThread(threadId, updates) {
|
|
136
|
+
const db = await this.ensureConnection();
|
|
137
|
+
const collection = db.collection(
|
|
138
|
+
this.config.threadsCollection
|
|
139
|
+
);
|
|
140
|
+
await collection.updateOne(
|
|
141
|
+
{ _id: new (0, _mongodb.ObjectId)(threadId) },
|
|
142
|
+
{
|
|
143
|
+
$set: {
|
|
144
|
+
...updates,
|
|
145
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
async deleteThread(threadId) {
|
|
151
|
+
const db = await this.ensureConnection();
|
|
152
|
+
const collection = db.collection(
|
|
153
|
+
this.config.threadsCollection
|
|
154
|
+
);
|
|
155
|
+
await collection.deleteOne({ _id: new (0, _mongodb.ObjectId)(threadId) });
|
|
156
|
+
}
|
|
157
|
+
async listThreads(filters) {
|
|
158
|
+
const db = await this.ensureConnection();
|
|
159
|
+
const collection = db.collection(
|
|
160
|
+
this.config.threadsCollection
|
|
161
|
+
);
|
|
162
|
+
const query = {};
|
|
163
|
+
if (filters.userId) query.userId = filters.userId;
|
|
164
|
+
if (filters.agentId) query.agentId = filters.agentId;
|
|
165
|
+
if (filters.organizationId) query.organizationId = filters.organizationId;
|
|
166
|
+
const docs = await collection.find(query).sort({ updatedAt: -1 }).toArray();
|
|
167
|
+
return docs.map((doc) => this.threadDocToData(doc));
|
|
168
|
+
}
|
|
169
|
+
// ============================================================================
|
|
170
|
+
// Message Operations
|
|
171
|
+
// ============================================================================
|
|
172
|
+
async addMessage(threadId, role, content, attachments) {
|
|
173
|
+
const db = await this.ensureConnection();
|
|
174
|
+
const collection = db.collection(
|
|
175
|
+
this.config.threadsCollection
|
|
176
|
+
);
|
|
177
|
+
const messageId = new (0, _mongodb.ObjectId)();
|
|
178
|
+
const message = {
|
|
179
|
+
_id: messageId,
|
|
180
|
+
role,
|
|
181
|
+
content,
|
|
182
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
183
|
+
attachments
|
|
184
|
+
};
|
|
185
|
+
await collection.updateOne(
|
|
186
|
+
{ _id: new (0, _mongodb.ObjectId)(threadId) },
|
|
187
|
+
{
|
|
188
|
+
$push: { messages: message },
|
|
189
|
+
$set: { updatedAt: /* @__PURE__ */ new Date() }
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
return messageId.toString();
|
|
193
|
+
}
|
|
194
|
+
async getMessages(threadId, limit) {
|
|
195
|
+
const db = await this.ensureConnection();
|
|
196
|
+
const collection = db.collection(
|
|
197
|
+
this.config.threadsCollection
|
|
198
|
+
);
|
|
199
|
+
const doc = await collection.findOne({ _id: new (0, _mongodb.ObjectId)(threadId) });
|
|
200
|
+
if (!doc) return [];
|
|
201
|
+
let messages = doc.messages.map((msg) => ({
|
|
202
|
+
id: _optionalChain([msg, 'access', _ => _._id, 'optionalAccess', _2 => _2.toString, 'call', _3 => _3()]) || "",
|
|
203
|
+
role: msg.role,
|
|
204
|
+
content: msg.content,
|
|
205
|
+
timestamp: msg.timestamp,
|
|
206
|
+
metadata: msg.metadata,
|
|
207
|
+
attachments: msg.attachments
|
|
208
|
+
}));
|
|
209
|
+
if (limit) {
|
|
210
|
+
messages = messages.slice(-limit);
|
|
211
|
+
}
|
|
212
|
+
return messages;
|
|
213
|
+
}
|
|
214
|
+
async getConversationContext(threadId, maxMessages = 20) {
|
|
215
|
+
const messages = await this.getMessages(threadId, maxMessages);
|
|
216
|
+
return messages.map((msg) => ({
|
|
217
|
+
role: msg.role,
|
|
218
|
+
content: msg.content
|
|
219
|
+
}));
|
|
220
|
+
}
|
|
221
|
+
// ============================================================================
|
|
222
|
+
// Helper Methods
|
|
223
|
+
// ============================================================================
|
|
224
|
+
agentDocToData(doc) {
|
|
225
|
+
return {
|
|
226
|
+
id: doc._id.toString(),
|
|
227
|
+
organizationId: doc.organizationId,
|
|
228
|
+
userId: doc.userId,
|
|
229
|
+
phone: doc.phone,
|
|
230
|
+
name: doc.name,
|
|
231
|
+
description: doc.description,
|
|
232
|
+
instructions: doc.instructions,
|
|
233
|
+
provider: doc.provider,
|
|
234
|
+
model: doc.model,
|
|
235
|
+
createdAt: doc.createdAt,
|
|
236
|
+
updatedAt: doc.updatedAt,
|
|
237
|
+
files: doc.files,
|
|
238
|
+
metadata: doc.metadata,
|
|
239
|
+
pluginConfigs: doc.pluginConfigs
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
threadDocToData(doc) {
|
|
243
|
+
return {
|
|
244
|
+
id: doc._id.toString(),
|
|
245
|
+
organizationId: doc.organizationId,
|
|
246
|
+
agentId: doc.agentId,
|
|
247
|
+
userId: doc.userId,
|
|
248
|
+
endUserId: doc.endUserId,
|
|
249
|
+
name: doc.name,
|
|
250
|
+
createdAt: doc.createdAt,
|
|
251
|
+
updatedAt: doc.updatedAt,
|
|
252
|
+
messages: doc.messages.map((msg) => ({
|
|
253
|
+
id: _optionalChain([msg, 'access', _4 => _4._id, 'optionalAccess', _5 => _5.toString, 'call', _6 => _6()]) || "",
|
|
254
|
+
role: msg.role,
|
|
255
|
+
content: msg.content,
|
|
256
|
+
timestamp: msg.timestamp,
|
|
257
|
+
metadata: msg.metadata,
|
|
258
|
+
attachments: msg.attachments
|
|
259
|
+
})),
|
|
260
|
+
isPendingThread: doc.isPendingThread,
|
|
261
|
+
metadata: doc.metadata
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
exports.MongoDBStorage = MongoDBStorage;
|