@multiplayer-app/ai-agent-mongo 0.1.0-beta.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.
- package/README.md +330 -0
- package/dist/__tests__/helpers.d.ts +22 -0
- package/dist/__tests__/helpers.d.ts.map +1 -0
- package/dist/__tests__/helpers.js +75 -0
- package/dist/__tests__/helpers.js.map +1 -0
- package/dist/__tests__/repositories/MongoAgentChatRepository.test.d.ts +2 -0
- package/dist/__tests__/repositories/MongoAgentChatRepository.test.d.ts.map +1 -0
- package/dist/__tests__/repositories/MongoAgentChatRepository.test.js +249 -0
- package/dist/__tests__/repositories/MongoAgentChatRepository.test.js.map +1 -0
- package/dist/__tests__/repositories/MongoAgentMessageRepository.test.d.ts +2 -0
- package/dist/__tests__/repositories/MongoAgentMessageRepository.test.d.ts.map +1 -0
- package/dist/__tests__/repositories/MongoAgentMessageRepository.test.js +311 -0
- package/dist/__tests__/repositories/MongoAgentMessageRepository.test.js.map +1 -0
- package/dist/__tests__/schemas/AgentChatSchema.test.d.ts +2 -0
- package/dist/__tests__/schemas/AgentChatSchema.test.d.ts.map +1 -0
- package/dist/__tests__/schemas/AgentChatSchema.test.js +120 -0
- package/dist/__tests__/schemas/AgentChatSchema.test.js.map +1 -0
- package/dist/__tests__/schemas/AgentMessageSchema.test.d.ts +2 -0
- package/dist/__tests__/schemas/AgentMessageSchema.test.d.ts.map +1 -0
- package/dist/__tests__/schemas/AgentMessageSchema.test.js +293 -0
- package/dist/__tests__/schemas/AgentMessageSchema.test.js.map +1 -0
- package/dist/__tests__/setup.d.ts +2 -0
- package/dist/__tests__/setup.d.ts.map +1 -0
- package/dist/__tests__/setup.js +25 -0
- package/dist/__tests__/setup.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/mongo/config.d.ts +10 -0
- package/dist/mongo/config.d.ts.map +1 -0
- package/dist/mongo/config.js +10 -0
- package/dist/mongo/config.js.map +1 -0
- package/dist/mongo/encryption.d.ts +66 -0
- package/dist/mongo/encryption.d.ts.map +1 -0
- package/dist/mongo/encryption.js +140 -0
- package/dist/mongo/encryption.js.map +1 -0
- package/dist/mongo/index.d.ts +25 -0
- package/dist/mongo/index.d.ts.map +1 -0
- package/dist/mongo/index.js +72 -0
- package/dist/mongo/index.js.map +1 -0
- package/dist/mongo/logger.d.ts +31 -0
- package/dist/mongo/logger.d.ts.map +1 -0
- package/dist/mongo/logger.js +36 -0
- package/dist/mongo/logger.js.map +1 -0
- package/dist/repositories/MongoAgentChatRepository.d.ts +37 -0
- package/dist/repositories/MongoAgentChatRepository.d.ts.map +1 -0
- package/dist/repositories/MongoAgentChatRepository.js +235 -0
- package/dist/repositories/MongoAgentChatRepository.js.map +1 -0
- package/dist/repositories/MongoAgentMessageRepository.d.ts +24 -0
- package/dist/repositories/MongoAgentMessageRepository.d.ts.map +1 -0
- package/dist/repositories/MongoAgentMessageRepository.js +140 -0
- package/dist/repositories/MongoAgentMessageRepository.js.map +1 -0
- package/dist/repositories/index.d.ts +3 -0
- package/dist/repositories/index.d.ts.map +1 -0
- package/dist/repositories/index.js +3 -0
- package/dist/repositories/index.js.map +1 -0
- package/dist/schemas/AgentChatSchema.d.ts +29 -0
- package/dist/schemas/AgentChatSchema.d.ts.map +1 -0
- package/dist/schemas/AgentChatSchema.js +26 -0
- package/dist/schemas/AgentChatSchema.js.map +1 -0
- package/dist/schemas/AgentMessageSchema.d.ts +30 -0
- package/dist/schemas/AgentMessageSchema.d.ts.map +1 -0
- package/dist/schemas/AgentMessageSchema.js +54 -0
- package/dist/schemas/AgentMessageSchema.js.map +1 -0
- package/dist/schemas/index.d.ts +3 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +3 -0
- package/dist/schemas/index.js.map +1 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
# @multiplayer-app/ai-agent-mongo
|
|
2
|
+
|
|
3
|
+
MongoDB implementation of the database repository interfaces for Multiplayer AI agent services. This library provides concrete Mongoose-based implementations of `AgentChatRepository` and `AgentMessageRepository`, along with MongoDB connection management and client-side encryption support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **MongoDB Implementation**: Complete Mongoose-based implementation of repository interfaces
|
|
8
|
+
- **Type-Safe Schemas**: Mongoose schemas with full TypeScript support
|
|
9
|
+
- **Client-Side Encryption**: Support for MongoDB client-side field-level encryption (CSFLE)
|
|
10
|
+
- **Connection Management**: Automatic connection handling with reconnection support
|
|
11
|
+
- **Indexed Queries**: Optimized database indexes for common query patterns
|
|
12
|
+
- **Aggregation Support**: Complex queries with message aggregation
|
|
13
|
+
- **Transaction Support**: MongoDB transaction support for multi-document operations
|
|
14
|
+
|
|
15
|
+
## Prerequisites
|
|
16
|
+
|
|
17
|
+
- Node.js >= 18
|
|
18
|
+
- TypeScript >= 5.0
|
|
19
|
+
- MongoDB >= 6.0 (for encryption features)
|
|
20
|
+
- MongoDB instance (local or remote)
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### Basic Setup
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import mongo from '@multiplayer-app/ai-agent-mongo';
|
|
28
|
+
import { MongoAgentChatRepository, MongoAgentMessageRepository } from '@multiplayer-app/ai-agent-mongo';
|
|
29
|
+
|
|
30
|
+
// Connect to MongoDB
|
|
31
|
+
await mongo.connect();
|
|
32
|
+
|
|
33
|
+
// Initialize repositories
|
|
34
|
+
const chatRepository = new MongoAgentChatRepository();
|
|
35
|
+
const messageRepository = new MongoAgentMessageRepository();
|
|
36
|
+
|
|
37
|
+
// Use repositories
|
|
38
|
+
const chat = await chatRepository.findById('chat-id');
|
|
39
|
+
const messages = await messageRepository.findByChatId('chat-id');
|
|
40
|
+
|
|
41
|
+
// Disconnect when done
|
|
42
|
+
await mongo.disconnect();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Environment Variables
|
|
46
|
+
|
|
47
|
+
Configure MongoDB connection and encryption via environment variables:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
# MongoDB Connection
|
|
51
|
+
MONGODB_URI=mongodb://localhost:27017/ai-agent
|
|
52
|
+
|
|
53
|
+
# MongoDB Debug Mode (optional)
|
|
54
|
+
MONGO_DEBUG=false
|
|
55
|
+
|
|
56
|
+
# Encryption Configuration
|
|
57
|
+
MONGODB_ENCRYPTION_KEY_VAULT_DB_NAME=encryption
|
|
58
|
+
MONGODB_ENCRYPTION_KEY_VAULT_COLLECTION_NAME=__keyVault
|
|
59
|
+
MONGODB_ENCRYPTION_DEK_NAME=data-key
|
|
60
|
+
MONGODB_ENCRYPTION_MASTER_KEY_PROVIDER=local # or 'aws'
|
|
61
|
+
|
|
62
|
+
# AWS KMS Configuration (if using AWS KMS)
|
|
63
|
+
AWS_KMS_KEY_ARN=arn:aws:kms:region:account:key/key-id
|
|
64
|
+
AWS_REGION=us-east-1
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Architecture
|
|
68
|
+
|
|
69
|
+
This library implements the abstract repository interfaces from `@multiplayer-app/ai-agent-db` using Mongoose as the ODM (Object Document Mapper) for MongoDB.
|
|
70
|
+
|
|
71
|
+
### Core Components
|
|
72
|
+
|
|
73
|
+
#### Repositories
|
|
74
|
+
|
|
75
|
+
- **`MongoAgentChatRepository`**: Implements `AgentChatRepository` interface
|
|
76
|
+
- **`MongoAgentMessageRepository`**: Implements `AgentMessageRepository` interface
|
|
77
|
+
|
|
78
|
+
#### Schemas
|
|
79
|
+
|
|
80
|
+
- **`AgentChatSchema`**: Mongoose schema for `AgentChat` entities
|
|
81
|
+
- **`AgentMessageSchema`**: Mongoose schema for `AgentMessage` entities
|
|
82
|
+
|
|
83
|
+
#### MongoDB Connection
|
|
84
|
+
|
|
85
|
+
- **`mongo.connect()`**: Establishes MongoDB connection with encryption setup
|
|
86
|
+
- **`mongo.disconnect()`**: Closes MongoDB connection
|
|
87
|
+
- **`mongo.connected()`**: Checks connection status
|
|
88
|
+
|
|
89
|
+
#### Encryption
|
|
90
|
+
|
|
91
|
+
- **`mongo.encryption.encrypt()`**: Encrypt data using client-side encryption
|
|
92
|
+
- **`mongo.encryption.decrypt()`**: Decrypt encrypted data
|
|
93
|
+
- Supports both local key provider and AWS KMS
|
|
94
|
+
|
|
95
|
+
## Usage
|
|
96
|
+
|
|
97
|
+
### Connection Management
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import mongo from '@multiplayer-app/ai-agent-mongo';
|
|
101
|
+
|
|
102
|
+
// Connect to MongoDB
|
|
103
|
+
await mongo.connect();
|
|
104
|
+
|
|
105
|
+
// Check connection status
|
|
106
|
+
if (mongo.connected()) {
|
|
107
|
+
console.log('Connected to MongoDB');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Disconnect
|
|
111
|
+
await mongo.disconnect();
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Using Repositories
|
|
115
|
+
|
|
116
|
+
#### AgentChatRepository
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { MongoAgentChatRepository } from '@multiplayer-app/ai-agent-mongo';
|
|
120
|
+
import { SortOrder } from '@multiplayer-app/ai-agent-types';
|
|
121
|
+
|
|
122
|
+
const chatRepository = new MongoAgentChatRepository();
|
|
123
|
+
|
|
124
|
+
// Find chat by ID
|
|
125
|
+
const chat = await chatRepository.findById('chat-id');
|
|
126
|
+
|
|
127
|
+
// Find chats by user
|
|
128
|
+
const userChats = await chatRepository.findByUserId('user-123');
|
|
129
|
+
|
|
130
|
+
// Find chats by context
|
|
131
|
+
const contextChats = await chatRepository.findByContextKey('context-key');
|
|
132
|
+
|
|
133
|
+
// Find chats with pagination and sorting
|
|
134
|
+
const recentChats = await chatRepository.find(
|
|
135
|
+
{ contextKey: 'my-context' },
|
|
136
|
+
{
|
|
137
|
+
sort: { field: 'updatedAt', order: SortOrder.Desc },
|
|
138
|
+
limit: 10
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
// Find chats with messages (aggregation)
|
|
143
|
+
const chatsWithMessages = await chatRepository.findWithMessages(
|
|
144
|
+
{ userId: 'user-123' },
|
|
145
|
+
{ sort: { field: 'createdAt', order: SortOrder.Desc } }
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
// Create a new chat
|
|
149
|
+
const newChat = await chatRepository.create({
|
|
150
|
+
title: 'New Chat',
|
|
151
|
+
contextKey: 'context-key',
|
|
152
|
+
userId: 'user-123',
|
|
153
|
+
status: AgentStatus.Active,
|
|
154
|
+
createdAt: new Date().toISOString(),
|
|
155
|
+
updatedAt: new Date().toISOString()
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Update chat title
|
|
159
|
+
await chatRepository.updateTitle('chat-id', 'Updated Title');
|
|
160
|
+
|
|
161
|
+
// Delete chat
|
|
162
|
+
await chatRepository.delete('chat-id');
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
#### AgentMessageRepository
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { MongoAgentMessageRepository } from '@multiplayer-app/ai-agent-mongo';
|
|
169
|
+
import { MessageRole } from '@multiplayer-app/ai-agent-types';
|
|
170
|
+
|
|
171
|
+
const messageRepository = new MongoAgentMessageRepository();
|
|
172
|
+
|
|
173
|
+
// Find messages by chat ID
|
|
174
|
+
const messages = await messageRepository.findByChatId('chat-id');
|
|
175
|
+
|
|
176
|
+
// Find messages by role
|
|
177
|
+
const assistantMessages = await messageRepository.findByRole(MessageRole.Assistant);
|
|
178
|
+
|
|
179
|
+
// Find messages by chat and role
|
|
180
|
+
const userMessages = await messageRepository.findByChatIdAndRole(
|
|
181
|
+
'chat-id',
|
|
182
|
+
MessageRole.User
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
// Find messages with tool calls
|
|
186
|
+
const toolCallMessages = await messageRepository.findWithToolCalls('chat-id');
|
|
187
|
+
|
|
188
|
+
// Find messages with attachments
|
|
189
|
+
const messagesWithAttachments = await messageRepository.findWithAttachments('chat-id');
|
|
190
|
+
|
|
191
|
+
// Create a new message
|
|
192
|
+
const newMessage = await messageRepository.create({
|
|
193
|
+
chat: 'chat-id',
|
|
194
|
+
role: MessageRole.User,
|
|
195
|
+
content: 'Hello, AI!',
|
|
196
|
+
createdAt: new Date().toISOString(),
|
|
197
|
+
updatedAt: new Date().toISOString()
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Delete messages by chat
|
|
201
|
+
await messageRepository.deleteByChatId('chat-id');
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Using Schemas Directly
|
|
205
|
+
|
|
206
|
+
You can also use the Mongoose models directly for advanced operations:
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
import { AgentChatModel, AgentMessageModel } from '@multiplayer-app/ai-agent-mongo';
|
|
210
|
+
|
|
211
|
+
// Direct Mongoose queries
|
|
212
|
+
const chat = await AgentChatModel.findOne({ userId: 'user-123' });
|
|
213
|
+
|
|
214
|
+
// Aggregation pipelines
|
|
215
|
+
const stats = await AgentChatModel.aggregate([
|
|
216
|
+
{ $match: { contextKey: 'context-key' } },
|
|
217
|
+
{ $group: { _id: '$status', count: { $sum: 1 } } }
|
|
218
|
+
]);
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
### Encryption
|
|
222
|
+
|
|
223
|
+
The library supports MongoDB client-side field-level encryption (CSFLE):
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
import mongo from '@multiplayer-app/ai-agent-mongo';
|
|
227
|
+
|
|
228
|
+
// Encrypt sensitive data
|
|
229
|
+
const encrypted = await mongo.encryption.encrypt('sensitive-data');
|
|
230
|
+
|
|
231
|
+
// Decrypt data
|
|
232
|
+
const decrypted = await mongo.encryption.decrypt(encrypted);
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
#### Encryption Providers
|
|
236
|
+
|
|
237
|
+
**Local Key Provider** (Development):
|
|
238
|
+
```bash
|
|
239
|
+
MONGODB_ENCRYPTION_MASTER_KEY_PROVIDER=local
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
**AWS KMS** (Production):
|
|
243
|
+
```bash
|
|
244
|
+
MONGODB_ENCRYPTION_MASTER_KEY_PROVIDER=aws
|
|
245
|
+
AWS_KMS_KEY_ARN=arn:aws:kms:region:account:key/key-id
|
|
246
|
+
AWS_REGION=us-east-1
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## Database Indexes
|
|
250
|
+
|
|
251
|
+
The library automatically creates indexes for optimal query performance:
|
|
252
|
+
|
|
253
|
+
### AgentChat Indexes
|
|
254
|
+
- `contextKey` (single field)
|
|
255
|
+
- `userId` (single field, sparse)
|
|
256
|
+
- `userId + contextKey` (compound)
|
|
257
|
+
|
|
258
|
+
### AgentMessage Indexes
|
|
259
|
+
- `chat` (single field)
|
|
260
|
+
- `role` (single field)
|
|
261
|
+
- `chat + role` (compound)
|
|
262
|
+
|
|
263
|
+
## Type Safety
|
|
264
|
+
|
|
265
|
+
All operations are fully typed using TypeScript:
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
import type { AgentChat, AgentMessage } from '@multiplayer-app/ai-agent-types';
|
|
269
|
+
import { MongoAgentChatRepository } from '@multiplayer-app/ai-agent-mongo';
|
|
270
|
+
|
|
271
|
+
const chatRepository = new MongoAgentChatRepository();
|
|
272
|
+
|
|
273
|
+
// TypeScript ensures type safety
|
|
274
|
+
const chat: AgentChat | null = await chatRepository.findById('id');
|
|
275
|
+
const chats: AgentChat[] = await chatRepository.findByUserId('user-id');
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## Error Handling
|
|
279
|
+
|
|
280
|
+
The library handles MongoDB connection errors and provides logging:
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
import mongo from '@multiplayer-app/ai-agent-mongo';
|
|
284
|
+
|
|
285
|
+
try {
|
|
286
|
+
await mongo.connect();
|
|
287
|
+
} catch (error) {
|
|
288
|
+
console.error('Failed to connect to MongoDB:', error);
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
## Testing
|
|
292
|
+
|
|
293
|
+
The library includes test utilities using `mongodb-memory-server` for in-memory MongoDB testing:
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
# Run tests
|
|
297
|
+
npm test
|
|
298
|
+
|
|
299
|
+
# Watch mode
|
|
300
|
+
npm run test:watch
|
|
301
|
+
|
|
302
|
+
# Coverage
|
|
303
|
+
npm run test:coverage
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
## Best Practices
|
|
307
|
+
|
|
308
|
+
1. **Connection Lifecycle**: Always call `mongo.connect()` before using repositories and `mongo.disconnect()` when shutting down
|
|
309
|
+
2. **Repository Instances**: Create repository instances once and reuse them (they're stateless)
|
|
310
|
+
3. **Error Handling**: Wrap repository calls in try-catch blocks for proper error handling
|
|
311
|
+
4. **Transactions**: Use MongoDB sessions for multi-document transactions when needed
|
|
312
|
+
5. **Indexes**: The library creates indexes automatically, but ensure your MongoDB instance has sufficient resources
|
|
313
|
+
6. **Encryption**: Use AWS KMS in production, local keys only for development
|
|
314
|
+
|
|
315
|
+
## Performance Considerations
|
|
316
|
+
|
|
317
|
+
- **Connection Pooling**: The library uses Mongoose connection pooling (minPoolSize: 3)
|
|
318
|
+
- **Indexes**: All common query patterns are indexed for optimal performance
|
|
319
|
+
- **Aggregation**: Complex queries use MongoDB aggregation pipelines for efficiency
|
|
320
|
+
- **ObjectId Conversion**: Automatic conversion between string IDs and MongoDB ObjectIds
|
|
321
|
+
|
|
322
|
+
## Related Packages
|
|
323
|
+
|
|
324
|
+
- `@multiplayer-app/ai-agent-db`: Abstract repository interfaces
|
|
325
|
+
- `@multiplayer-app/ai-agent-types`: Type definitions for AgentChat, AgentMessage, etc.
|
|
326
|
+
- `@multiplayer-app/ai-agent-node`: Node.js library that uses these repositories
|
|
327
|
+
|
|
328
|
+
## License
|
|
329
|
+
|
|
330
|
+
MIT
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AgentChat, AgentMessage } from '@multiplayer-app/ai-agent-types';
|
|
2
|
+
/**
|
|
3
|
+
* Helper to clear all collections between tests
|
|
4
|
+
*/
|
|
5
|
+
export declare function clearCollections(): Promise<void>;
|
|
6
|
+
/**
|
|
7
|
+
* Create a test AgentChat object
|
|
8
|
+
*/
|
|
9
|
+
export declare function createTestChat(overrides?: Partial<AgentChat>): Omit<AgentChat, 'id' | 'createdAt' | 'updatedAt'>;
|
|
10
|
+
/**
|
|
11
|
+
* Create a test AgentMessage object
|
|
12
|
+
*/
|
|
13
|
+
export declare function createTestMessage(chatId: string, overrides?: Partial<AgentMessage>): Omit<AgentMessage, 'id' | 'createdAt'>;
|
|
14
|
+
/**
|
|
15
|
+
* Create a test message with tool calls
|
|
16
|
+
*/
|
|
17
|
+
export declare function createTestMessageWithToolCalls(chatId: string, overrides?: Partial<AgentMessage>): Omit<AgentMessage, 'id' | 'createdAt'>;
|
|
18
|
+
/**
|
|
19
|
+
* Create a test message with attachments
|
|
20
|
+
*/
|
|
21
|
+
export declare function createTestMessageWithAttachments(chatId: string, overrides?: Partial<AgentMessage>): Omit<AgentMessage, 'id' | 'createdAt'>;
|
|
22
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/__tests__/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAI/E;;GAEG;AACH,wBAAsB,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC,CAKtD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,IAAI,CAAC,SAAS,EAAE,IAAI,GAAG,WAAW,GAAG,WAAW,CAAC,CAShH;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,SAAS,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,GAChC,IAAI,CAAC,YAAY,EAAE,IAAI,GAAG,WAAW,CAAC,CAUxC;AAED;;GAEG;AACH,wBAAgB,8BAA8B,CAC5C,MAAM,EAAE,MAAM,EACd,SAAS,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,GAChC,IAAI,CAAC,YAAY,EAAE,IAAI,GAAG,WAAW,CAAC,CAexC;AAED;;GAEG;AACH,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,MAAM,EACd,SAAS,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,GAChC,IAAI,CAAC,YAAY,EAAE,IAAI,GAAG,WAAW,CAAC,CAaxC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { MessageRole, AgentToolCallStatus, AgentAttachmentType } from '@multiplayer-app/ai-agent-types';
|
|
2
|
+
import { mongoose } from '../mongo';
|
|
3
|
+
/**
|
|
4
|
+
* Helper to clear all collections between tests
|
|
5
|
+
*/
|
|
6
|
+
export async function clearCollections() {
|
|
7
|
+
const collections = mongoose.connection.collections;
|
|
8
|
+
for (const key in collections) {
|
|
9
|
+
await collections[key].deleteMany({});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Create a test AgentChat object
|
|
14
|
+
*/
|
|
15
|
+
export function createTestChat(overrides) {
|
|
16
|
+
const now = new Date().toISOString();
|
|
17
|
+
return {
|
|
18
|
+
title: 'Test Chat',
|
|
19
|
+
contextKey: 'test-context',
|
|
20
|
+
model: 'gpt-4',
|
|
21
|
+
metadata: {},
|
|
22
|
+
...overrides,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create a test AgentMessage object
|
|
27
|
+
*/
|
|
28
|
+
export function createTestMessage(chatId, overrides) {
|
|
29
|
+
return {
|
|
30
|
+
chat: chatId,
|
|
31
|
+
role: MessageRole.User,
|
|
32
|
+
content: 'Test message',
|
|
33
|
+
reasoning: '',
|
|
34
|
+
toolCalls: [],
|
|
35
|
+
attachments: [],
|
|
36
|
+
...overrides,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create a test message with tool calls
|
|
41
|
+
*/
|
|
42
|
+
export function createTestMessageWithToolCalls(chatId, overrides) {
|
|
43
|
+
return createTestMessage(chatId, {
|
|
44
|
+
role: MessageRole.Assistant,
|
|
45
|
+
content: 'I will use a tool',
|
|
46
|
+
toolCalls: [
|
|
47
|
+
{
|
|
48
|
+
id: 'tool-call-1',
|
|
49
|
+
name: 'test_tool',
|
|
50
|
+
input: { query: 'test' },
|
|
51
|
+
status: AgentToolCallStatus.Pending,
|
|
52
|
+
requiresConfirmation: false,
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
...overrides,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create a test message with attachments
|
|
60
|
+
*/
|
|
61
|
+
export function createTestMessageWithAttachments(chatId, overrides) {
|
|
62
|
+
return createTestMessage(chatId, {
|
|
63
|
+
attachments: [
|
|
64
|
+
{
|
|
65
|
+
id: 'attachment-1',
|
|
66
|
+
type: AgentAttachmentType.File,
|
|
67
|
+
name: 'test.txt',
|
|
68
|
+
url: 'https://example.com/test.txt',
|
|
69
|
+
mimeType: 'text/plain',
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
...overrides,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/__tests__/helpers.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AACxG,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB;IACpC,MAAM,WAAW,GAAG,QAAQ,CAAC,UAAU,CAAC,WAAW,CAAC;IACpD,KAAK,MAAM,GAAG,IAAI,WAAW,EAAE,CAAC;QAC9B,MAAM,WAAW,CAAC,GAAG,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACxC,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,SAA8B;IAC3D,MAAM,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACrC,OAAO;QACL,KAAK,EAAE,WAAW;QAClB,UAAU,EAAE,cAAc;QAC1B,KAAK,EAAE,OAAO;QACd,QAAQ,EAAE,EAAE;QACZ,GAAG,SAAS;KACb,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,iBAAiB,CAC/B,MAAc,EACd,SAAiC;IAEjC,OAAO;QACL,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,WAAW,CAAC,IAAI;QACtB,OAAO,EAAE,cAAc;QACvB,SAAS,EAAE,EAAE;QACb,SAAS,EAAE,EAAE;QACb,WAAW,EAAE,EAAE;QACf,GAAG,SAAS;KACb,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,8BAA8B,CAC5C,MAAc,EACd,SAAiC;IAEjC,OAAO,iBAAiB,CAAC,MAAM,EAAE;QAC/B,IAAI,EAAE,WAAW,CAAC,SAAS;QAC3B,OAAO,EAAE,mBAAmB;QAC5B,SAAS,EAAE;YACT;gBACE,EAAE,EAAE,aAAa;gBACjB,IAAI,EAAE,WAAW;gBACjB,KAAK,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE;gBACxB,MAAM,EAAE,mBAAmB,CAAC,OAAO;gBACnC,oBAAoB,EAAE,KAAK;aAC5B;SACF;QACD,GAAG,SAAS;KACb,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gCAAgC,CAC9C,MAAc,EACd,SAAiC;IAEjC,OAAO,iBAAiB,CAAC,MAAM,EAAE;QAC/B,WAAW,EAAE;YACX;gBACE,EAAE,EAAE,cAAc;gBAClB,IAAI,EAAE,mBAAmB,CAAC,IAAI;gBAC9B,IAAI,EAAE,UAAU;gBAChB,GAAG,EAAE,8BAA8B;gBACnC,QAAQ,EAAE,YAAY;aACvB;SACF;QACD,GAAG,SAAS;KACb,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MongoAgentChatRepository.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/repositories/MongoAgentChatRepository.test.ts"],"names":[],"mappings":""}
|