@memberjunction/ai-agents 2.44.0
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 +551 -0
- package/dist/base-agent.d.ts +3 -0
- package/dist/base-agent.d.ts.map +1 -0
- package/dist/base-agent.js +7 -0
- package/dist/base-agent.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,551 @@
|
|
|
1
|
+
# @memberjunction/ai-agents
|
|
2
|
+
|
|
3
|
+
The MemberJunction AI Agents package provides a comprehensive framework for creating, managing, and executing AI agents within the MemberJunction ecosystem. This package serves as the foundation for building sophisticated agentic AI applications with hierarchical composition, context management, and intelligent execution strategies.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **🤖 BaseAgent Class**: Core foundation for all AI agent implementations
|
|
8
|
+
- **🏗️ Agent Composition**: Hierarchical agent architecture with parent-child relationships
|
|
9
|
+
- **🧠 Context Management**: Intelligent conversation context handling and compression
|
|
10
|
+
- **🔄 Execution Strategies**: Sequential and parallel agent execution modes
|
|
11
|
+
- **📝 Learning System**: Agent note-taking and knowledge retention capabilities
|
|
12
|
+
- **🎯 Action Framework**: Extensible action system for agent capabilities
|
|
13
|
+
- **🔧 Subclassing Support**: Easy extension and customization of agent behavior
|
|
14
|
+
- **🔐 Metadata-Driven**: Database-driven configuration for agents and behaviors
|
|
15
|
+
- **📊 Analytics**: Comprehensive execution logging and performance tracking
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @memberjunction/ai-agents
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Requirements
|
|
24
|
+
|
|
25
|
+
- Node.js 16+
|
|
26
|
+
- MemberJunction Core libraries
|
|
27
|
+
- [@memberjunction/ai-prompts](../Prompts/README.md) for advanced prompt management
|
|
28
|
+
- [@memberjunction/aiengine](../Engine/README.md) for AI model orchestration
|
|
29
|
+
|
|
30
|
+
## Core Architecture
|
|
31
|
+
|
|
32
|
+
### BaseAgent Class
|
|
33
|
+
|
|
34
|
+
The `BaseAgent` class is the central component of the AI Agents framework, providing:
|
|
35
|
+
|
|
36
|
+
- **Standard Interface**: Consistent API for all agent implementations
|
|
37
|
+
- **Lifecycle Management**: Initialization, execution, and cleanup workflows
|
|
38
|
+
- **Context Handling**: Automatic conversation context management
|
|
39
|
+
- **Error Handling**: Robust error recovery and logging
|
|
40
|
+
- **Extensibility**: Clean extension points for custom behavior
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { BaseAgent } from '@memberjunction/ai-agents';
|
|
44
|
+
import { AIAgentEntity } from '@memberjunction/core-entities';
|
|
45
|
+
|
|
46
|
+
// Create a custom agent by extending BaseAgent
|
|
47
|
+
class CustomerSupportAgent extends BaseAgent {
|
|
48
|
+
// Agent metadata from database
|
|
49
|
+
private agentEntity: AIAgentEntity;
|
|
50
|
+
|
|
51
|
+
constructor(agentEntity: AIAgentEntity) {
|
|
52
|
+
super();
|
|
53
|
+
this.agentEntity = agentEntity;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async initialize(): Promise<void> {
|
|
57
|
+
// Custom initialization logic
|
|
58
|
+
await super.initialize();
|
|
59
|
+
this.setupCustomCapabilities();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async execute(context: AgentContext): Promise<AgentResult> {
|
|
63
|
+
// Custom execution logic
|
|
64
|
+
const result = await super.execute(context);
|
|
65
|
+
return this.enhanceResult(result);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async handleAction(actionName: string, params: any): Promise<any> {
|
|
69
|
+
// Handle agent-specific actions
|
|
70
|
+
switch(actionName) {
|
|
71
|
+
case 'searchKnowledgeBase':
|
|
72
|
+
return this.searchKnowledgeBase(params);
|
|
73
|
+
case 'createTicket':
|
|
74
|
+
return this.createSupportTicket(params);
|
|
75
|
+
default:
|
|
76
|
+
return super.handleAction(actionName, params);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
## Architecture Deep Dive
|
|
84
|
+
|
|
85
|
+
For comprehensive details about the AI Agents framework architecture, data models, workflows, and implementation guidelines, see the [Agent Architecture.md](./Agent%20Architecture.md) document.
|
|
86
|
+
|
|
87
|
+
Key architectural concepts covered include:
|
|
88
|
+
|
|
89
|
+
- **Hierarchical Agent Composition**: How agents are organized and orchestrated
|
|
90
|
+
- **Metadata-Driven Configuration**: Database-driven agent and prompt management
|
|
91
|
+
- **Execution Workflows**: Detailed execution patterns and context management
|
|
92
|
+
- **Performance Optimization**: Caching, parallel execution, and resource management
|
|
93
|
+
- **Extensibility Patterns**: Guidelines for custom agent development
|
|
94
|
+
|
|
95
|
+
## Usage Examples
|
|
96
|
+
|
|
97
|
+
### Basic Agent Implementation
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { BaseAgent } from '@memberjunction/ai-agents';
|
|
101
|
+
import { Metadata } from '@memberjunction/core';
|
|
102
|
+
import { AIAgentEntity } from '@memberjunction/core-entities';
|
|
103
|
+
|
|
104
|
+
// Load agent metadata from database
|
|
105
|
+
const md = new Metadata();
|
|
106
|
+
const agentEntity = await md.GetEntityObject<AIAgentEntity>('AI Agents');
|
|
107
|
+
await agentEntity.Load('Customer Support Agent');
|
|
108
|
+
|
|
109
|
+
// Create agent instance
|
|
110
|
+
const agent = new CustomerSupportAgent(agentEntity);
|
|
111
|
+
await agent.initialize();
|
|
112
|
+
|
|
113
|
+
// Execute agent with context
|
|
114
|
+
const result = await agent.execute({
|
|
115
|
+
conversationId: 'conv-123',
|
|
116
|
+
messages: [
|
|
117
|
+
{ role: 'user', content: 'I need help with my order' }
|
|
118
|
+
],
|
|
119
|
+
metadata: {
|
|
120
|
+
userId: 'user-456',
|
|
121
|
+
sessionId: 'session-789'
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
console.log(result.response);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Hierarchical Agent Composition
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
import { BaseAgent, ConductorAgent } from '@memberjunction/ai-agents';
|
|
132
|
+
|
|
133
|
+
// Create a conductor agent that manages other agents
|
|
134
|
+
class CustomerServiceConductor extends ConductorAgent {
|
|
135
|
+
async routeToSubAgent(context: AgentContext): Promise<string> {
|
|
136
|
+
// Analyze context to determine which sub-agent to use
|
|
137
|
+
const intent = await this.analyzeIntent(context);
|
|
138
|
+
|
|
139
|
+
switch(intent.category) {
|
|
140
|
+
case 'billing':
|
|
141
|
+
return 'BillingAgent';
|
|
142
|
+
case 'technical':
|
|
143
|
+
return 'TechnicalSupportAgent';
|
|
144
|
+
case 'general':
|
|
145
|
+
return 'GeneralInquiryAgent';
|
|
146
|
+
default:
|
|
147
|
+
return 'DefaultAgent';
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Use the conductor
|
|
153
|
+
const conductor = new CustomerServiceConductor(conductorEntity);
|
|
154
|
+
await conductor.initialize();
|
|
155
|
+
|
|
156
|
+
// Add child agents
|
|
157
|
+
await conductor.addChildAgent(billingAgent);
|
|
158
|
+
await conductor.addChildAgent(technicalAgent);
|
|
159
|
+
await conductor.addChildAgent(generalAgent);
|
|
160
|
+
|
|
161
|
+
// Execute - conductor will route to appropriate sub-agent
|
|
162
|
+
const result = await conductor.execute(context);
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Context Management and Compression
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
import { BaseAgent } from '@memberjunction/ai-agents';
|
|
169
|
+
|
|
170
|
+
class LongConversationAgent extends BaseAgent {
|
|
171
|
+
async execute(context: AgentContext): Promise<AgentResult> {
|
|
172
|
+
// Check if context needs compression
|
|
173
|
+
if (context.messages.length > this.agentEntity.ContextCompressionMessageThreshold) {
|
|
174
|
+
// Compress older messages while keeping recent ones
|
|
175
|
+
context = await this.compressContext(context);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Continue with execution
|
|
179
|
+
return super.execute(context);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private async compressContext(context: AgentContext): Promise<AgentContext> {
|
|
183
|
+
// Use configured compression prompt
|
|
184
|
+
const compressionPrompt = await this.getCompressionPrompt();
|
|
185
|
+
const compressed = await compressionPrompt.execute({
|
|
186
|
+
messages: context.messages.slice(0, -this.agentEntity.ContextCompressionMessageRetentionCount)
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// Return new context with compressed history
|
|
190
|
+
return {
|
|
191
|
+
...context,
|
|
192
|
+
messages: [
|
|
193
|
+
{ role: 'system', content: compressed.summary },
|
|
194
|
+
...context.messages.slice(-this.agentEntity.ContextCompressionMessageRetentionCount)
|
|
195
|
+
]
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## API Reference
|
|
202
|
+
|
|
203
|
+
### BaseAgent Class
|
|
204
|
+
|
|
205
|
+
The foundation class for all AI agents.
|
|
206
|
+
|
|
207
|
+
#### Constructor
|
|
208
|
+
```typescript
|
|
209
|
+
constructor(agentEntity?: AIAgentEntity)
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
#### Properties
|
|
213
|
+
|
|
214
|
+
| Property | Type | Description |
|
|
215
|
+
|----------|------|-------------|
|
|
216
|
+
| `agentEntity` | `AIAgentEntity` | The database entity containing agent configuration |
|
|
217
|
+
| `isInitialized` | `boolean` | Whether the agent has been initialized |
|
|
218
|
+
| `actions` | `AIAgentActionEntity[]` | Available actions for this agent |
|
|
219
|
+
| `notes` | `AIAgentNoteEntity[]` | Learning notes accumulated by the agent |
|
|
220
|
+
|
|
221
|
+
#### Methods
|
|
222
|
+
|
|
223
|
+
##### `initialize(): Promise<void>`
|
|
224
|
+
Initializes the agent, loading configuration and preparing for execution.
|
|
225
|
+
|
|
226
|
+
##### `execute(context: AgentContext): Promise<AgentResult>`
|
|
227
|
+
Executes the agent with the provided context.
|
|
228
|
+
|
|
229
|
+
**Parameters:**
|
|
230
|
+
- `context: AgentContext` - The execution context containing conversation history and metadata
|
|
231
|
+
|
|
232
|
+
**Returns:** `Promise<AgentResult>` - The agent's response and metadata
|
|
233
|
+
|
|
234
|
+
##### `handleAction(actionName: string, params: any): Promise<any>`
|
|
235
|
+
Handles agent-specific actions.
|
|
236
|
+
|
|
237
|
+
**Parameters:**
|
|
238
|
+
- `actionName: string` - The name of the action to execute
|
|
239
|
+
- `params: any` - Parameters for the action
|
|
240
|
+
|
|
241
|
+
**Returns:** `Promise<any>` - The action result
|
|
242
|
+
|
|
243
|
+
##### `addNote(content: string, type: string): Promise<void>`
|
|
244
|
+
Adds a learning note for the agent.
|
|
245
|
+
|
|
246
|
+
**Parameters:**
|
|
247
|
+
- `content: string` - The note content
|
|
248
|
+
- `type: string` - The type of note (e.g., 'learning', 'error', 'improvement')
|
|
249
|
+
|
|
250
|
+
### Types and Interfaces
|
|
251
|
+
|
|
252
|
+
#### AgentContext
|
|
253
|
+
```typescript
|
|
254
|
+
interface AgentContext {
|
|
255
|
+
conversationId: string;
|
|
256
|
+
messages: Message[];
|
|
257
|
+
metadata?: Record<string, any>;
|
|
258
|
+
parentContext?: AgentContext;
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
#### AgentResult
|
|
263
|
+
```typescript
|
|
264
|
+
interface AgentResult {
|
|
265
|
+
response: string;
|
|
266
|
+
success: boolean;
|
|
267
|
+
metadata?: {
|
|
268
|
+
tokensUsed?: number;
|
|
269
|
+
executionTimeMs?: number;
|
|
270
|
+
modelUsed?: string;
|
|
271
|
+
childAgentResults?: AgentResult[];
|
|
272
|
+
};
|
|
273
|
+
error?: string;
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
#### Message
|
|
278
|
+
```typescript
|
|
279
|
+
interface Message {
|
|
280
|
+
role: 'system' | 'user' | 'assistant';
|
|
281
|
+
content: string;
|
|
282
|
+
timestamp?: Date;
|
|
283
|
+
metadata?: Record<string, any>;
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
## Configuration
|
|
288
|
+
|
|
289
|
+
Agents are configured through the MemberJunction metadata system. Key configuration options include:
|
|
290
|
+
|
|
291
|
+
### Agent Configuration (AIAgent Entity)
|
|
292
|
+
|
|
293
|
+
| Field | Description | Default |
|
|
294
|
+
|-------|-------------|---------|
|
|
295
|
+
| `Name` | Unique agent identifier | Required |
|
|
296
|
+
| `Description` | Agent purpose and capabilities | Required |
|
|
297
|
+
| `ParentID` | Parent agent for hierarchical composition | null |
|
|
298
|
+
| `ExecutionMode` | How child agents execute (Sequential/Parallel) | Sequential |
|
|
299
|
+
| `EnableContextCompression` | Whether to compress long conversations | false |
|
|
300
|
+
| `ContextCompressionMessageThreshold` | Messages before compression triggers | 50 |
|
|
301
|
+
| `ContextCompressionMessageRetentionCount` | Recent messages to keep uncompressed | 10 |
|
|
302
|
+
|
|
303
|
+
### Integration with Prompts
|
|
304
|
+
|
|
305
|
+
Agents use prompts through the `AIAgentPrompt` entity:
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
// Example: Associate a prompt with an agent
|
|
309
|
+
const agentPrompt = await md.GetEntityObject<AIAgentPromptEntity>('AI Agent Prompts');
|
|
310
|
+
agentPrompt.NewRecord();
|
|
311
|
+
agentPrompt.AgentID = agent.ID;
|
|
312
|
+
agentPrompt.PromptID = prompt.ID;
|
|
313
|
+
agentPrompt.Purpose = 'Main conversation handler';
|
|
314
|
+
agentPrompt.ExecutionOrder = 1;
|
|
315
|
+
agentPrompt.ContextBehavior = 'Recent'; // or 'Full', 'None'
|
|
316
|
+
agentPrompt.ContextMessageCount = 20;
|
|
317
|
+
await agentPrompt.Save();
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## Dependencies
|
|
321
|
+
|
|
322
|
+
- `@memberjunction/core`: ^2.43.0 - MemberJunction core library
|
|
323
|
+
- `@memberjunction/global`: ^2.43.0 - MemberJunction global utilities
|
|
324
|
+
- `@memberjunction/core-entities`: ^2.43.0 - MemberJunction entity definitions
|
|
325
|
+
- `@memberjunction/ai`: ^2.43.0 - Base AI functionality
|
|
326
|
+
- `@memberjunction/aiengine`: ^2.43.0 - AI model orchestration
|
|
327
|
+
- `@memberjunction/ai-prompts`: ^2.43.0 - Advanced prompt management
|
|
328
|
+
- `@memberjunction/templates`: ^2.43.0 - Template rendering support
|
|
329
|
+
- `rxjs`: ^7.8.1 - Reactive programming support
|
|
330
|
+
- `dotenv`: ^16.4.1 - Environment configuration
|
|
331
|
+
|
|
332
|
+
## Related Packages
|
|
333
|
+
|
|
334
|
+
- `@memberjunction/aiengine`: Core AI engine and model management
|
|
335
|
+
- `@memberjunction/ai-prompts`: Advanced prompt execution and management
|
|
336
|
+
- `@memberjunction/templates`: Template rendering for dynamic content
|
|
337
|
+
|
|
338
|
+
## Advanced Features
|
|
339
|
+
|
|
340
|
+
### Parallel Agent Execution
|
|
341
|
+
|
|
342
|
+
```typescript
|
|
343
|
+
class ParallelAnalysisAgent extends BaseAgent {
|
|
344
|
+
async execute(context: AgentContext): Promise<AgentResult> {
|
|
345
|
+
// Execute multiple sub-agents in parallel
|
|
346
|
+
const [sentiment, intent, entities] = await Promise.all([
|
|
347
|
+
this.sentimentAgent.execute(context),
|
|
348
|
+
this.intentAgent.execute(context),
|
|
349
|
+
this.entityExtractorAgent.execute(context)
|
|
350
|
+
]);
|
|
351
|
+
|
|
352
|
+
// Combine results
|
|
353
|
+
return {
|
|
354
|
+
response: this.synthesizeResults(sentiment, intent, entities),
|
|
355
|
+
success: true,
|
|
356
|
+
metadata: {
|
|
357
|
+
childAgentResults: [sentiment, intent, entities]
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### Agent Learning and Adaptation
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
class LearningAgent extends BaseAgent {
|
|
368
|
+
async execute(context: AgentContext): Promise<AgentResult> {
|
|
369
|
+
try {
|
|
370
|
+
const result = await super.execute(context);
|
|
371
|
+
|
|
372
|
+
// Learn from successful execution
|
|
373
|
+
if (result.success) {
|
|
374
|
+
await this.addNote(
|
|
375
|
+
`Successfully handled query type: ${this.classifyQuery(context)}`,
|
|
376
|
+
'learning'
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
return result;
|
|
381
|
+
} catch (error) {
|
|
382
|
+
// Learn from errors
|
|
383
|
+
await this.addNote(
|
|
384
|
+
`Error handling query: ${error.message}`,
|
|
385
|
+
'error'
|
|
386
|
+
);
|
|
387
|
+
throw error;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
### Custom Action Implementation
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
class DataAnalysisAgent extends BaseAgent {
|
|
397
|
+
async initialize(): Promise<void> {
|
|
398
|
+
await super.initialize();
|
|
399
|
+
|
|
400
|
+
// Register custom actions
|
|
401
|
+
this.registerAction('analyzeData', this.analyzeData.bind(this));
|
|
402
|
+
this.registerAction('generateReport', this.generateReport.bind(this));
|
|
403
|
+
this.registerAction('exportResults', this.exportResults.bind(this));
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
private async analyzeData(params: { datasetId: string }): Promise<any> {
|
|
407
|
+
// Implementation for data analysis
|
|
408
|
+
const dataset = await this.loadDataset(params.datasetId);
|
|
409
|
+
return this.performAnalysis(dataset);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
## Error Handling
|
|
415
|
+
|
|
416
|
+
The framework provides comprehensive error handling:
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
try {
|
|
420
|
+
const result = await agent.execute(context);
|
|
421
|
+
console.log('Success:', result);
|
|
422
|
+
} catch (error) {
|
|
423
|
+
if (error instanceof AgentExecutionError) {
|
|
424
|
+
console.error('Execution failed:', error.message);
|
|
425
|
+
console.error('Agent:', error.agentName);
|
|
426
|
+
console.error('Context:', error.context);
|
|
427
|
+
} else if (error instanceof AgentInitializationError) {
|
|
428
|
+
console.error('Failed to initialize agent:', error.message);
|
|
429
|
+
} else {
|
|
430
|
+
console.error('Unexpected error:', error);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
## Performance Considerations
|
|
436
|
+
|
|
437
|
+
1. **Context Compression**: Enable for long conversations to reduce token usage
|
|
438
|
+
2. **Caching**: Leverage result caching for repeated queries
|
|
439
|
+
3. **Parallel Execution**: Use parallel mode for independent sub-agents
|
|
440
|
+
4. **Resource Limits**: Configure appropriate timeouts and token limits
|
|
441
|
+
|
|
442
|
+
## Development Status
|
|
443
|
+
|
|
444
|
+
🚧 **Under Active Development** - This package is currently being built and will house all functionality for the MJ AI Agent framework. The `BaseAgent` class and core infrastructure are being implemented to provide the foundation for all agentic execution work in the MemberJunction ecosystem.
|
|
445
|
+
|
|
446
|
+
### Current Implementation Status
|
|
447
|
+
|
|
448
|
+
- ✅ Package structure and configuration
|
|
449
|
+
- 🚧 BaseAgent class implementation (in progress)
|
|
450
|
+
- 📋 ConductorAgent for hierarchical composition (planned)
|
|
451
|
+
- 📋 Context management and compression (planned)
|
|
452
|
+
- 📋 Action framework integration (planned)
|
|
453
|
+
- 📋 Learning and note system (planned)
|
|
454
|
+
- 📋 Execution logging and analytics (planned)
|
|
455
|
+
|
|
456
|
+
### Roadmap
|
|
457
|
+
|
|
458
|
+
1. **Phase 1**: Core BaseAgent implementation with basic execution
|
|
459
|
+
2. **Phase 2**: Hierarchical agent composition and conductor pattern
|
|
460
|
+
3. **Phase 3**: Advanced context management and compression
|
|
461
|
+
4. **Phase 4**: Action framework and extensibility
|
|
462
|
+
5. **Phase 5**: Learning system and performance optimization
|
|
463
|
+
|
|
464
|
+
## License
|
|
465
|
+
|
|
466
|
+
ISC
|
|
467
|
+
|
|
468
|
+
---
|
|
469
|
+
|
|
470
|
+
## Testing
|
|
471
|
+
|
|
472
|
+
```typescript
|
|
473
|
+
import { BaseAgent } from '@memberjunction/ai-agents';
|
|
474
|
+
import { MockAgentEntity } from './test-utils';
|
|
475
|
+
|
|
476
|
+
describe('CustomerSupportAgent', () => {
|
|
477
|
+
let agent: CustomerSupportAgent;
|
|
478
|
+
|
|
479
|
+
beforeEach(async () => {
|
|
480
|
+
const mockEntity = new MockAgentEntity({
|
|
481
|
+
Name: 'Test Agent',
|
|
482
|
+
EnableContextCompression: true,
|
|
483
|
+
ContextCompressionMessageThreshold: 10
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
agent = new CustomerSupportAgent(mockEntity);
|
|
487
|
+
await agent.initialize();
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
test('should handle customer query', async () => {
|
|
491
|
+
const result = await agent.execute({
|
|
492
|
+
conversationId: 'test-123',
|
|
493
|
+
messages: [
|
|
494
|
+
{ role: 'user', content: 'What is my order status?' }
|
|
495
|
+
]
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
expect(result.success).toBe(true);
|
|
499
|
+
expect(result.response).toContain('order');
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
test('should compress long conversations', async () => {
|
|
503
|
+
const longContext = {
|
|
504
|
+
conversationId: 'test-456',
|
|
505
|
+
messages: Array(15).fill(null).map((_, i) => ({
|
|
506
|
+
role: i % 2 === 0 ? 'user' : 'assistant',
|
|
507
|
+
content: `Message ${i}`
|
|
508
|
+
}))
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
const result = await agent.execute(longContext);
|
|
512
|
+
expect(result.metadata.contextCompressed).toBe(true);
|
|
513
|
+
});
|
|
514
|
+
});
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
## Troubleshooting
|
|
518
|
+
|
|
519
|
+
### Common Issues
|
|
520
|
+
|
|
521
|
+
1. **Agent Not Found**: Ensure the agent is properly registered in the database
|
|
522
|
+
2. **Initialization Failures**: Check that all required prompts and configurations exist
|
|
523
|
+
3. **Context Overflow**: Enable context compression for long conversations
|
|
524
|
+
4. **Performance Issues**: Review parallel execution settings and caching configuration
|
|
525
|
+
|
|
526
|
+
### Debug Mode
|
|
527
|
+
|
|
528
|
+
```typescript
|
|
529
|
+
// Enable debug logging
|
|
530
|
+
process.env.MJ_AI_AGENT_DEBUG = 'true';
|
|
531
|
+
|
|
532
|
+
const agent = new MyAgent(entity);
|
|
533
|
+
agent.on('debug', (message) => {
|
|
534
|
+
console.log('[Agent Debug]:', message);
|
|
535
|
+
});
|
|
536
|
+
```
|
|
537
|
+
|
|
538
|
+
## Contributing
|
|
539
|
+
|
|
540
|
+
When developing agents using this framework:
|
|
541
|
+
|
|
542
|
+
1. **Always extend BaseAgent** for consistency and built-in functionality
|
|
543
|
+
2. **Follow the lifecycle patterns** defined in the base class
|
|
544
|
+
3. **Use meaningful names and descriptions** for agents and actions
|
|
545
|
+
4. **Implement proper error handling** in custom execution logic
|
|
546
|
+
5. **Leverage the note system** for agent learning and improvement
|
|
547
|
+
6. **Test with various context scenarios** to ensure robustness
|
|
548
|
+
7. **Document custom actions and behaviors** in your agent implementations
|
|
549
|
+
8. **Follow TypeScript best practices** and avoid `any` types
|
|
550
|
+
|
|
551
|
+
For detailed development guidelines and best practices, refer to the [Agent Architecture.md](./Agent%20Architecture.md) documentation.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-agent.d.ts","sourceRoot":"","sources":["../src/base-agent.ts"],"names":[],"mappings":"AAAA,qBAAa,SAAS;CAErB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-agent.js","sourceRoot":"","sources":["../src/base-agent.ts"],"names":[],"mappings":";;;AAAA,MAAa,SAAS;CAErB;AAFD,8BAEC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./base-agent"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@memberjunction/ai-agents",
|
|
3
|
+
"version": "2.44.0",
|
|
4
|
+
"description": "MemberJunction: AI Agent Execution and Management",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"/dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"start": "ts-node-dev src/index.ts",
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
14
|
+
},
|
|
15
|
+
"author": "MemberJunction.com",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@memberjunction/core": "2.44.0",
|
|
19
|
+
"@memberjunction/global": "2.44.0",
|
|
20
|
+
"@memberjunction/core-entities": "2.44.0",
|
|
21
|
+
"@memberjunction/ai": "2.44.0",
|
|
22
|
+
"@memberjunction/aiengine": "2.44.0",
|
|
23
|
+
"@memberjunction/ai-prompts": "2.44.0",
|
|
24
|
+
"@memberjunction/templates": "2.44.0",
|
|
25
|
+
"dotenv": "^16.4.1",
|
|
26
|
+
"rxjs": "^7.8.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "20.14.2",
|
|
30
|
+
"ts-node-dev": "^2.0.0",
|
|
31
|
+
"typescript": "^5.4.5"
|
|
32
|
+
}
|
|
33
|
+
}
|