@hazeljs/agent 0.7.9 → 0.8.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/.eslintrc.js +4 -0
- package/IMPLEMENTATION_SUMMARY.md +36 -11
- package/PERSISTENCE.md +6 -0
- package/PRISMA_INTEGRATION.md +10 -5
- package/PRODUCTION_READINESS.md +37 -0
- package/QUICKSTART.md +12 -8
- package/README.md +72 -57
- package/STATE_VS_MEMORY.md +29 -12
- package/benchmarks/performance.benchmark.ts +1 -5
- package/coverage/clover.xml +616 -601
- package/coverage/lcov-report/index.html +33 -33
- package/coverage/lcov.info +957 -927
- package/dist/agent.module.js +2 -2
- package/dist/agent.module.js.map +1 -1
- package/dist/decorators/approval.decorator.d.ts +14 -0
- package/dist/decorators/approval.decorator.d.ts.map +1 -0
- package/dist/decorators/approval.decorator.js +20 -0
- package/dist/decorators/approval.decorator.js.map +1 -0
- package/dist/evaluation/agent-eval.d.ts +17 -0
- package/dist/evaluation/agent-eval.d.ts.map +1 -0
- package/dist/evaluation/agent-eval.js +18 -0
- package/dist/evaluation/agent-eval.js.map +1 -0
- package/dist/executor/agent.executor.d.ts +2 -0
- package/dist/executor/agent.executor.d.ts.map +1 -1
- package/dist/executor/agent.executor.js +4 -0
- package/dist/executor/agent.executor.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/runtime/agent.runtime.d.ts.map +1 -1
- package/dist/runtime/agent.runtime.js +1 -2
- package/dist/runtime/agent.runtime.js.map +1 -1
- package/dist/state/agent.state.d.ts.map +1 -1
- package/dist/state/agent.state.js +8 -7
- package/dist/state/agent.state.js.map +1 -1
- package/dist/state/database-state.manager.d.ts.map +1 -1
- package/dist/state/database-state.manager.js +8 -7
- package/dist/state/database-state.manager.js.map +1 -1
- package/dist/state/redis-state.manager.d.ts.map +1 -1
- package/dist/state/redis-state.manager.js +8 -7
- package/dist/state/redis-state.manager.js.map +1 -1
- package/dist/utils/circuit-breaker.d.ts +1 -1
- package/dist/utils/circuit-breaker.js +1 -1
- package/logs/combined.log +1 -1
- package/package.json +13 -7
- package/tsconfig.tsbuildinfo +1 -1
package/.eslintrc.js
CHANGED
|
@@ -44,12 +44,14 @@ packages/agent/
|
|
|
44
44
|
## 🎯 Core Features Implemented
|
|
45
45
|
|
|
46
46
|
### 1. Agent System ✅
|
|
47
|
+
|
|
47
48
|
- **@Agent decorator** - Declarative agent definition
|
|
48
49
|
- **Agent metadata** - Configuration and capabilities
|
|
49
50
|
- **Agent registry** - Centralized agent management
|
|
50
51
|
- **Agent lifecycle** - Full lifecycle management
|
|
51
52
|
|
|
52
53
|
### 2. Tool System ✅
|
|
54
|
+
|
|
53
55
|
- **@Tool decorator** - Declarative tool definition
|
|
54
56
|
- **Tool metadata** - Parameters, validation, policies
|
|
55
57
|
- **Tool registry** - Tool discovery and lookup
|
|
@@ -57,12 +59,14 @@ packages/agent/
|
|
|
57
59
|
- **Approval workflow** - Human-in-the-loop for sensitive operations
|
|
58
60
|
|
|
59
61
|
### 3. State Machine ✅
|
|
62
|
+
|
|
60
63
|
- **Agent states** - idle, thinking, using_tool, waiting_for_input, waiting_for_approval, completed, failed
|
|
61
64
|
- **State transitions** - Deterministic state flow
|
|
62
65
|
- **State persistence** - In-memory state management
|
|
63
66
|
- **Context management** - Full execution context
|
|
64
67
|
|
|
65
68
|
### 4. Execution Loop ✅
|
|
69
|
+
|
|
66
70
|
- **Controlled loop** - Step-by-step execution
|
|
67
71
|
- **LLM integration** - Decision making via LLM
|
|
68
72
|
- **Action execution** - Tool calls, responses, user input
|
|
@@ -70,6 +74,7 @@ packages/agent/
|
|
|
70
74
|
- **Pause/resume** - Support for long-running workflows
|
|
71
75
|
|
|
72
76
|
### 5. Memory Integration ✅
|
|
77
|
+
|
|
73
78
|
- **Conversation history** - Automatic persistence
|
|
74
79
|
- **Entity tracking** - Track mentioned entities
|
|
75
80
|
- **Fact storage** - Persistent knowledge
|
|
@@ -77,17 +82,20 @@ packages/agent/
|
|
|
77
82
|
- **Automatic sync** - Load before, persist after execution
|
|
78
83
|
|
|
79
84
|
### 6. RAG Integration ✅
|
|
85
|
+
|
|
80
86
|
- **Context retrieval** - Query RAG before reasoning
|
|
81
87
|
- **Context injection** - Add to system prompt
|
|
82
88
|
- **Configurable topK** - Control context size
|
|
83
89
|
|
|
84
90
|
### 7. Human-in-the-Loop ✅
|
|
91
|
+
|
|
85
92
|
- **Approval requests** - Tools can require approval
|
|
86
93
|
- **Approval workflow** - Request → Wait → Approve/Reject
|
|
87
94
|
- **User input** - Agents can ask questions
|
|
88
95
|
- **Pause/resume** - Resume after user interaction
|
|
89
96
|
|
|
90
97
|
### 8. Event System ✅
|
|
98
|
+
|
|
91
99
|
- **Comprehensive events** - 15+ event types
|
|
92
100
|
- **Pub/sub pattern** - Subscribe to specific or all events
|
|
93
101
|
- **Execution events** - Started, completed, failed
|
|
@@ -97,6 +105,7 @@ packages/agent/
|
|
|
97
105
|
- **RAG events** - RAG queries
|
|
98
106
|
|
|
99
107
|
### 9. Observability ✅
|
|
108
|
+
|
|
100
109
|
- **Event emission** - All actions emit events
|
|
101
110
|
- **State tracking** - Full state history
|
|
102
111
|
- **Step recording** - Complete step log
|
|
@@ -104,6 +113,7 @@ packages/agent/
|
|
|
104
113
|
- **Duration tracking** - Performance metrics
|
|
105
114
|
|
|
106
115
|
### 10. Error Handling ✅
|
|
116
|
+
|
|
107
117
|
- **Multi-level handling** - Tool, step, execution, runtime
|
|
108
118
|
- **Retry logic** - Configurable retries for tools
|
|
109
119
|
- **Timeout handling** - Prevent hanging operations
|
|
@@ -113,6 +123,7 @@ packages/agent/
|
|
|
113
123
|
## 🏗️ Architecture Highlights
|
|
114
124
|
|
|
115
125
|
### Design Patterns Used
|
|
126
|
+
|
|
116
127
|
- **Facade Pattern** - AgentRuntime provides simple interface
|
|
117
128
|
- **Template Method** - AgentExecutor defines execution skeleton
|
|
118
129
|
- **Chain of Responsibility** - Tool approval pipeline
|
|
@@ -163,9 +174,7 @@ import { Agent, Tool, AgentRuntime } from '@hazeljs/agent';
|
|
|
163
174
|
export class SupportAgent {
|
|
164
175
|
@Tool({
|
|
165
176
|
description: 'Look up order by ID',
|
|
166
|
-
parameters: [
|
|
167
|
-
{ name: 'orderId', type: 'string', required: true }
|
|
168
|
-
],
|
|
177
|
+
parameters: [{ name: 'orderId', type: 'string', required: true }],
|
|
169
178
|
})
|
|
170
179
|
async lookupOrder(input: { orderId: string }) {
|
|
171
180
|
return { orderId: input.orderId, status: 'shipped' };
|
|
@@ -197,11 +206,10 @@ runtime.on('tool.approval.requested', (event) => {
|
|
|
197
206
|
});
|
|
198
207
|
|
|
199
208
|
// 5. Execute Agent
|
|
200
|
-
const result = await runtime.execute(
|
|
201
|
-
'
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
);
|
|
209
|
+
const result = await runtime.execute('support-agent', 'Check order #12345', {
|
|
210
|
+
sessionId: 'session-123',
|
|
211
|
+
enableMemory: true,
|
|
212
|
+
});
|
|
205
213
|
```
|
|
206
214
|
|
|
207
215
|
## 🔄 Execution Flow
|
|
@@ -245,7 +253,9 @@ import { RagModule } from '@hazeljs/rag';
|
|
|
245
253
|
|
|
246
254
|
@HazelModule({
|
|
247
255
|
imports: [
|
|
248
|
-
RagModule.forRoot({
|
|
256
|
+
RagModule.forRoot({
|
|
257
|
+
/* ... */
|
|
258
|
+
}),
|
|
249
259
|
AgentModule.forRoot({
|
|
250
260
|
runtime: { defaultMaxSteps: 10 },
|
|
251
261
|
agents: [SupportAgent, SalesAgent],
|
|
@@ -274,6 +284,7 @@ export class MyService {
|
|
|
274
284
|
## 📊 Comparison with Traditional Approaches
|
|
275
285
|
|
|
276
286
|
### Traditional (Stateless)
|
|
287
|
+
|
|
277
288
|
```typescript
|
|
278
289
|
@Post('/chat')
|
|
279
290
|
async chat(@Body() body: { message: string }) {
|
|
@@ -281,14 +292,18 @@ async chat(@Body() body: { message: string }) {
|
|
|
281
292
|
return { response };
|
|
282
293
|
}
|
|
283
294
|
```
|
|
295
|
+
|
|
284
296
|
**Issues**: No memory, no tools, no state, no observability
|
|
285
297
|
|
|
286
298
|
### HazelJS Agent Runtime
|
|
299
|
+
|
|
287
300
|
```typescript
|
|
288
301
|
@Agent({ name: 'chat-agent', enableMemory: true })
|
|
289
302
|
export class ChatAgent {
|
|
290
303
|
@Tool()
|
|
291
|
-
async searchDocs(query: string) {
|
|
304
|
+
async searchDocs(query: string) {
|
|
305
|
+
/* ... */
|
|
306
|
+
}
|
|
292
307
|
}
|
|
293
308
|
|
|
294
309
|
// Execution
|
|
@@ -297,22 +312,26 @@ const result = await runtime.execute('chat-agent', message, {
|
|
|
297
312
|
enableMemory: true,
|
|
298
313
|
});
|
|
299
314
|
```
|
|
315
|
+
|
|
300
316
|
**Benefits**: Memory, tools, state, observability, resumable
|
|
301
317
|
|
|
302
318
|
## 🚀 Production Considerations
|
|
303
319
|
|
|
304
320
|
### Current Implementation
|
|
321
|
+
|
|
305
322
|
- In-memory state (Map-based)
|
|
306
323
|
- Single process
|
|
307
324
|
- No distributed coordination
|
|
308
325
|
|
|
309
326
|
### Production Recommendations
|
|
327
|
+
|
|
310
328
|
1. **State Persistence**: Replace Map with Redis/Database
|
|
311
329
|
2. **Distributed Approvals**: Use message queue
|
|
312
330
|
3. **Execution Queue**: Use job queue for long-running agents
|
|
313
331
|
4. **Event Bus**: Replace in-memory emitter with distributed bus
|
|
314
332
|
|
|
315
333
|
### Scaling Example
|
|
334
|
+
|
|
316
335
|
```typescript
|
|
317
336
|
class RedisStateManager extends AgentStateManager {
|
|
318
337
|
async getContext(executionId: string) {
|
|
@@ -335,6 +354,7 @@ const runtime = new AgentRuntime({
|
|
|
335
354
|
## ✨ Key Differentiators
|
|
336
355
|
|
|
337
356
|
### vs LangChain
|
|
357
|
+
|
|
338
358
|
- **Framework-native** - Built into backend framework
|
|
339
359
|
- **Type-safe** - Full TypeScript support
|
|
340
360
|
- **Declarative** - Decorator-based API
|
|
@@ -342,12 +362,14 @@ const runtime = new AgentRuntime({
|
|
|
342
362
|
- **Production-ready** - Error handling, retries, timeouts
|
|
343
363
|
|
|
344
364
|
### vs NestJS + LangChain
|
|
365
|
+
|
|
345
366
|
- **AI-native** - Memory and RAG as primitives
|
|
346
367
|
- **Simpler** - No need for separate agent library
|
|
347
368
|
- **Integrated** - Works with HazelJS modules
|
|
348
369
|
- **Lightweight** - No Express/Fastify dependency
|
|
349
370
|
|
|
350
371
|
### vs Custom Implementation
|
|
372
|
+
|
|
351
373
|
- **Framework-level** - Production-grade patterns
|
|
352
374
|
- **Extensible** - Multiple extension points
|
|
353
375
|
- **Observable** - Built-in monitoring
|
|
@@ -356,18 +378,21 @@ const runtime = new AgentRuntime({
|
|
|
356
378
|
## 🎯 Next Steps
|
|
357
379
|
|
|
358
380
|
### Immediate
|
|
381
|
+
|
|
359
382
|
1. Add unit tests for all components
|
|
360
383
|
2. Add integration tests for execution flow
|
|
361
384
|
3. Add E2E tests with real LLM
|
|
362
385
|
4. Create more examples (sales agent, RAG agent, multi-agent)
|
|
363
386
|
|
|
364
387
|
### Short-term
|
|
388
|
+
|
|
365
389
|
1. Implement Redis-based state persistence
|
|
366
390
|
2. Add distributed approval workflow
|
|
367
391
|
3. Add streaming responses
|
|
368
392
|
4. Add policy engine for tool authorization
|
|
369
393
|
|
|
370
394
|
### Long-term
|
|
395
|
+
|
|
371
396
|
1. Visual debugger for agent execution
|
|
372
397
|
2. Agent marketplace (shareable templates)
|
|
373
398
|
3. Multi-agent coordination
|
|
@@ -384,7 +409,7 @@ const runtime = new AgentRuntime({
|
|
|
384
409
|
✅ **Error handling** - Multi-level with retry logic
|
|
385
410
|
✅ **Extensibility** - Multiple extension points
|
|
386
411
|
✅ **Documentation** - Complete user and technical docs
|
|
387
|
-
✅ **Examples** - Working examples provided
|
|
412
|
+
✅ **Examples** - Working examples provided
|
|
388
413
|
|
|
389
414
|
## 🎉 Conclusion
|
|
390
415
|
|
package/PERSISTENCE.md
CHANGED
|
@@ -9,16 +9,19 @@ The `@hazeljs/agent` package supports multiple persistence backends for agent ex
|
|
|
9
9
|
## Available Backends
|
|
10
10
|
|
|
11
11
|
### 1. In-Memory (Default)
|
|
12
|
+
|
|
12
13
|
- **Best for**: Development, testing, single-instance deployments
|
|
13
14
|
- **Pros**: Fast, no setup required
|
|
14
15
|
- **Cons**: Data lost on restart, not suitable for distributed systems
|
|
15
16
|
|
|
16
17
|
### 2. Redis
|
|
18
|
+
|
|
17
19
|
- **Best for**: Production, distributed systems, high-performance scenarios
|
|
18
20
|
- **Pros**: Fast, distributed, TTL support, pub/sub capabilities
|
|
19
21
|
- **Cons**: Requires Redis infrastructure
|
|
20
22
|
|
|
21
23
|
### 3. Database (Prisma)
|
|
24
|
+
|
|
22
25
|
- **Best for**: Long-term persistence, audit trails, analytics
|
|
23
26
|
- **Pros**: Durable, queryable, full audit trail
|
|
24
27
|
- **Cons**: Slower than Redis, requires database setup
|
|
@@ -40,6 +43,7 @@ const runtime = new AgentRuntime({
|
|
|
40
43
|
### Redis Backend
|
|
41
44
|
|
|
42
45
|
1. Install Redis client:
|
|
46
|
+
|
|
43
47
|
```bash
|
|
44
48
|
npm install redis
|
|
45
49
|
```
|
|
@@ -75,6 +79,7 @@ const runtime = new AgentRuntime({
|
|
|
75
79
|
### Database Backend (Prisma)
|
|
76
80
|
|
|
77
81
|
1. Install Prisma:
|
|
82
|
+
|
|
78
83
|
```bash
|
|
79
84
|
npm install @prisma/client
|
|
80
85
|
```
|
|
@@ -84,6 +89,7 @@ npm install @prisma/client
|
|
|
84
89
|
- See [PRISMA_INTEGRATION.md](./PRISMA_INTEGRATION.md) for detailed instructions
|
|
85
90
|
|
|
86
91
|
3. Run migrations:
|
|
92
|
+
|
|
87
93
|
```bash
|
|
88
94
|
npx prisma migrate dev --name add_agent_state_models
|
|
89
95
|
npx prisma generate
|
package/PRISMA_INTEGRATION.md
CHANGED
|
@@ -296,11 +296,11 @@ npx prisma migrate dev
|
|
|
296
296
|
model AgentContext {
|
|
297
297
|
// Use your own ID format
|
|
298
298
|
id String @id @default(cuid()) // or @default(uuid())
|
|
299
|
-
|
|
299
|
+
|
|
300
300
|
// Add custom fields
|
|
301
301
|
priority Int @default(0)
|
|
302
302
|
tags String[]
|
|
303
|
-
|
|
303
|
+
|
|
304
304
|
// Customize JSON fields
|
|
305
305
|
metadata Json @default("{}")
|
|
306
306
|
}
|
|
@@ -325,11 +325,11 @@ model AgentContext {
|
|
|
325
325
|
```prisma
|
|
326
326
|
model AgentContext {
|
|
327
327
|
// ... fields ...
|
|
328
|
-
|
|
328
|
+
|
|
329
329
|
// Add composite indexes
|
|
330
330
|
@@index([agentId, state])
|
|
331
331
|
@@index([sessionId, createdAt])
|
|
332
|
-
|
|
332
|
+
|
|
333
333
|
// Add full-text search (PostgreSQL)
|
|
334
334
|
@@index([input(ops: Raw("gin_trgm_ops"))])
|
|
335
335
|
}
|
|
@@ -362,7 +362,7 @@ export class AppModule {
|
|
|
362
362
|
const stateManager = new DatabaseStateManager({
|
|
363
363
|
client: this.prisma,
|
|
364
364
|
});
|
|
365
|
-
|
|
365
|
+
|
|
366
366
|
// Use with AgentRuntime
|
|
367
367
|
// (configure in your agent service)
|
|
368
368
|
}
|
|
@@ -374,6 +374,7 @@ export class AppModule {
|
|
|
374
374
|
### Error: "Model AgentContext not found"
|
|
375
375
|
|
|
376
376
|
**Solution**: Make sure you've:
|
|
377
|
+
|
|
377
378
|
1. Added the model to your schema
|
|
378
379
|
2. Run `npx prisma generate`
|
|
379
380
|
3. Restarted your application
|
|
@@ -381,6 +382,7 @@ export class AppModule {
|
|
|
381
382
|
### Error: "Column does not exist"
|
|
382
383
|
|
|
383
384
|
**Solution**: Run migrations:
|
|
385
|
+
|
|
384
386
|
```bash
|
|
385
387
|
npx prisma migrate dev
|
|
386
388
|
```
|
|
@@ -394,6 +396,7 @@ npx prisma migrate dev
|
|
|
394
396
|
### Performance Issues
|
|
395
397
|
|
|
396
398
|
1. **Add indexes** on frequently queried fields:
|
|
399
|
+
|
|
397
400
|
```prisma
|
|
398
401
|
@@index([sessionId])
|
|
399
402
|
@@index([userId])
|
|
@@ -401,6 +404,7 @@ npx prisma migrate dev
|
|
|
401
404
|
```
|
|
402
405
|
|
|
403
406
|
2. **Use soft deletes** to avoid expensive DELETE operations:
|
|
407
|
+
|
|
404
408
|
```prisma
|
|
405
409
|
deletedAt DateTime?
|
|
406
410
|
@@index([deletedAt])
|
|
@@ -494,6 +498,7 @@ export class AppModule {}
|
|
|
494
498
|
5. ✅ Test with your agents
|
|
495
499
|
|
|
496
500
|
For more details, see:
|
|
501
|
+
|
|
497
502
|
- [PERSISTENCE.md](./PERSISTENCE.md) - Configuration guide
|
|
498
503
|
- [STATE_VS_MEMORY.md](./STATE_VS_MEMORY.md) - Understanding State vs Memory
|
|
499
504
|
- [prisma-schema.example.prisma](./prisma-schema.example.prisma) - Full schema example
|
package/PRODUCTION_READINESS.md
CHANGED
|
@@ -5,6 +5,7 @@ This document tracks the production readiness improvements made to `@hazeljs/age
|
|
|
5
5
|
## ✅ Completed
|
|
6
6
|
|
|
7
7
|
### 1. Type Safety Improvements
|
|
8
|
+
|
|
8
9
|
- **Created proper type definitions** for LLM and RAG providers
|
|
9
10
|
- `src/types/llm.types.ts` - LLMProvider interface with proper types
|
|
10
11
|
- `src/types/rag.types.ts` - RAGService interface with proper types
|
|
@@ -16,6 +17,7 @@ This document tracks the production readiness improvements made to `@hazeljs/age
|
|
|
16
17
|
- **Exported new types** from package index
|
|
17
18
|
|
|
18
19
|
### 2. Testing Infrastructure
|
|
20
|
+
|
|
19
21
|
- **Created Jest configuration** (`jest.config.js`)
|
|
20
22
|
- TypeScript support via ts-jest
|
|
21
23
|
- Coverage thresholds set to 80%
|
|
@@ -31,6 +33,7 @@ This document tracks the production readiness improvements made to `@hazeljs/age
|
|
|
31
33
|
- `tests/registry/tool.registry.test.ts` - Full coverage of ToolRegistry
|
|
32
34
|
|
|
33
35
|
### 3. Dependencies
|
|
36
|
+
|
|
34
37
|
- Added Jest and testing dependencies to package.json
|
|
35
38
|
- `@types/jest: ^29.5.11`
|
|
36
39
|
- `jest: ^29.7.0`
|
|
@@ -39,13 +42,16 @@ This document tracks the production readiness improvements made to `@hazeljs/age
|
|
|
39
42
|
## ✅ Completed Production Features
|
|
40
43
|
|
|
41
44
|
### 4. Rate Limiting
|
|
45
|
+
|
|
42
46
|
**Implementation**: `src/utils/rate-limiter.ts`
|
|
47
|
+
|
|
43
48
|
- Token bucket algorithm for rate limiting
|
|
44
49
|
- Configurable tokens per minute and burst size
|
|
45
50
|
- Automatic token refill over time
|
|
46
51
|
- Blocking and non-blocking consumption modes
|
|
47
52
|
|
|
48
53
|
**Usage**:
|
|
54
|
+
|
|
49
55
|
```typescript
|
|
50
56
|
const runtime = new AgentRuntime({
|
|
51
57
|
rateLimitPerMinute: 60, // 60 requests per minute
|
|
@@ -53,7 +59,9 @@ const runtime = new AgentRuntime({
|
|
|
53
59
|
```
|
|
54
60
|
|
|
55
61
|
### 5. Structured Logging
|
|
62
|
+
|
|
56
63
|
**Implementation**: `src/utils/logger.ts`
|
|
64
|
+
|
|
57
65
|
- Multiple log levels (DEBUG, INFO, WARN, ERROR, FATAL)
|
|
58
66
|
- Structured context with agent/execution metadata
|
|
59
67
|
- Colored console output for development
|
|
@@ -61,6 +69,7 @@ const runtime = new AgentRuntime({
|
|
|
61
69
|
- Custom log handlers support
|
|
62
70
|
|
|
63
71
|
**Usage**:
|
|
72
|
+
|
|
64
73
|
```typescript
|
|
65
74
|
const runtime = new AgentRuntime({
|
|
66
75
|
logLevel: LogLevel.INFO,
|
|
@@ -68,7 +77,9 @@ const runtime = new AgentRuntime({
|
|
|
68
77
|
```
|
|
69
78
|
|
|
70
79
|
### 6. Metrics Collection
|
|
80
|
+
|
|
71
81
|
**Implementation**: `src/utils/metrics.ts`
|
|
82
|
+
|
|
72
83
|
- Execution metrics (count, success rate, duration)
|
|
73
84
|
- Performance metrics (avg, min, max, p50, p95, p99)
|
|
74
85
|
- Tool usage tracking
|
|
@@ -76,19 +87,23 @@ const runtime = new AgentRuntime({
|
|
|
76
87
|
- Memory retrieval metrics
|
|
77
88
|
|
|
78
89
|
**Usage**:
|
|
90
|
+
|
|
79
91
|
```typescript
|
|
80
92
|
const metrics = runtime.getMetrics();
|
|
81
93
|
console.log(runtime.getMetricsSummary());
|
|
82
94
|
```
|
|
83
95
|
|
|
84
96
|
### 7. Retry Logic
|
|
97
|
+
|
|
85
98
|
**Implementation**: `src/utils/retry.ts`
|
|
99
|
+
|
|
86
100
|
- Exponential backoff with jitter
|
|
87
101
|
- Configurable retry attempts and delays
|
|
88
102
|
- Retryable error detection
|
|
89
103
|
- Retry callbacks for monitoring
|
|
90
104
|
|
|
91
105
|
**Usage**:
|
|
106
|
+
|
|
92
107
|
```typescript
|
|
93
108
|
const runtime = new AgentRuntime({
|
|
94
109
|
enableRetry: true, // Enabled by default
|
|
@@ -96,7 +111,9 @@ const runtime = new AgentRuntime({
|
|
|
96
111
|
```
|
|
97
112
|
|
|
98
113
|
### 8. Circuit Breaker
|
|
114
|
+
|
|
99
115
|
**Implementation**: `src/utils/circuit-breaker.ts`
|
|
116
|
+
|
|
100
117
|
- Three states: CLOSED, OPEN, HALF_OPEN
|
|
101
118
|
- Automatic state transitions
|
|
102
119
|
- Failure and success thresholds
|
|
@@ -104,6 +121,7 @@ const runtime = new AgentRuntime({
|
|
|
104
121
|
- Manual reset capability
|
|
105
122
|
|
|
106
123
|
**Usage**:
|
|
124
|
+
|
|
107
125
|
```typescript
|
|
108
126
|
const runtime = new AgentRuntime({
|
|
109
127
|
enableCircuitBreaker: true, // Enabled by default
|
|
@@ -113,26 +131,32 @@ const status = runtime.getCircuitBreakerStatus();
|
|
|
113
131
|
```
|
|
114
132
|
|
|
115
133
|
### 9. Health Checks
|
|
134
|
+
|
|
116
135
|
**Implementation**: `src/utils/health-check.ts`
|
|
136
|
+
|
|
117
137
|
- Component health monitoring (LLM, RAG, Memory)
|
|
118
138
|
- Latency tracking
|
|
119
139
|
- Overall health status (HEALTHY, DEGRADED, UNHEALTHY)
|
|
120
140
|
- Metrics integration
|
|
121
141
|
|
|
122
142
|
**Usage**:
|
|
143
|
+
|
|
123
144
|
```typescript
|
|
124
145
|
const health = await runtime.healthCheck();
|
|
125
146
|
console.log(health.status); // 'healthy' | 'degraded' | 'unhealthy'
|
|
126
147
|
```
|
|
127
148
|
|
|
128
149
|
### 10. Performance Benchmarks
|
|
150
|
+
|
|
129
151
|
**Implementation**: `benchmarks/performance.benchmark.ts`
|
|
152
|
+
|
|
130
153
|
- Agent registration benchmarks
|
|
131
154
|
- Tool registry lookup benchmarks
|
|
132
155
|
- Metrics collection benchmarks
|
|
133
156
|
- Health check benchmarks
|
|
134
157
|
|
|
135
158
|
**Usage**:
|
|
159
|
+
|
|
136
160
|
```bash
|
|
137
161
|
npm run benchmark
|
|
138
162
|
```
|
|
@@ -140,7 +164,9 @@ npm run benchmark
|
|
|
140
164
|
## 🚧 Remaining Tasks
|
|
141
165
|
|
|
142
166
|
### Additional Test Coverage
|
|
167
|
+
|
|
143
168
|
Create tests for remaining core components:
|
|
169
|
+
|
|
144
170
|
- `tests/runtime/agent.runtime.test.ts`
|
|
145
171
|
- `tests/executor/agent.executor.test.ts`
|
|
146
172
|
- `tests/executor/tool.executor.test.ts`
|
|
@@ -152,12 +178,14 @@ Create tests for remaining core components:
|
|
|
152
178
|
## 📋 Installation Instructions
|
|
153
179
|
|
|
154
180
|
### 1. Install Dependencies
|
|
181
|
+
|
|
155
182
|
```bash
|
|
156
183
|
cd /Users/muhammadarslan/repos/hazeljs.ai/hazeljs/packages/agent
|
|
157
184
|
npm install
|
|
158
185
|
```
|
|
159
186
|
|
|
160
187
|
### 2. Run Tests
|
|
188
|
+
|
|
161
189
|
```bash
|
|
162
190
|
# Run all tests
|
|
163
191
|
npm test
|
|
@@ -170,6 +198,7 @@ npm run test:watch
|
|
|
170
198
|
```
|
|
171
199
|
|
|
172
200
|
### 3. Build Package
|
|
201
|
+
|
|
173
202
|
```bash
|
|
174
203
|
npm run build
|
|
175
204
|
```
|
|
@@ -183,17 +212,22 @@ npm run build
|
|
|
183
212
|
## 📝 Notes
|
|
184
213
|
|
|
185
214
|
### Type Safety
|
|
215
|
+
|
|
186
216
|
All `any` types have been replaced with proper interfaces:
|
|
217
|
+
|
|
187
218
|
- `llmProvider?: any` → `llmProvider?: LLMProvider`
|
|
188
219
|
- `ragService?: any` → `ragService?: RAGService`
|
|
189
220
|
|
|
190
221
|
### Breaking Changes
|
|
222
|
+
|
|
191
223
|
The new type definitions may require updates to existing code:
|
|
224
|
+
|
|
192
225
|
- LLM providers must implement the `LLMProvider` interface
|
|
193
226
|
- RAG services must implement the `RAGService` interface
|
|
194
227
|
- Tool definitions are now converted to LLM format via `getToolDefinitionsForLLM()`
|
|
195
228
|
|
|
196
229
|
### Example: Creating a Custom LLM Provider
|
|
230
|
+
|
|
197
231
|
```typescript
|
|
198
232
|
import { LLMProvider, LLMChatRequest, LLMChatResponse } from '@hazeljs/agent';
|
|
199
233
|
|
|
@@ -238,6 +272,7 @@ class CustomLLMProvider implements LLMProvider {
|
|
|
238
272
|
**Production Ready**: 85% Complete
|
|
239
273
|
|
|
240
274
|
**Completed**:
|
|
275
|
+
|
|
241
276
|
- ✅ Type safety improvements (100%)
|
|
242
277
|
- ✅ Testing infrastructure (100%)
|
|
243
278
|
- ✅ Rate limiting (100%)
|
|
@@ -249,6 +284,7 @@ class CustomLLMProvider implements LLMProvider {
|
|
|
249
284
|
- ✅ Performance benchmarks (100%)
|
|
250
285
|
|
|
251
286
|
**Remaining**:
|
|
287
|
+
|
|
252
288
|
- ⏳ Integration tests (0%)
|
|
253
289
|
- ⏳ End-to-end tests (0%)
|
|
254
290
|
- ⏳ Security audit (0%)
|
|
@@ -256,6 +292,7 @@ class CustomLLMProvider implements LLMProvider {
|
|
|
256
292
|
- ⏳ Deployment guide (0%)
|
|
257
293
|
|
|
258
294
|
**Recommended Timeline**:
|
|
295
|
+
|
|
259
296
|
- Phase 1 (✅ Completed): Type safety + Testing infrastructure
|
|
260
297
|
- Phase 2 (✅ Completed): Rate limiting + Logging + Metrics
|
|
261
298
|
- Phase 3 (✅ Completed): Retry logic + Circuit breaker + Health checks
|
package/QUICKSTART.md
CHANGED
|
@@ -61,14 +61,10 @@ runtime.registerAgentInstance('my-agent', myAgent);
|
|
|
61
61
|
### Step 4: Execute
|
|
62
62
|
|
|
63
63
|
```typescript
|
|
64
|
-
const result = await runtime.execute(
|
|
65
|
-
'
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
sessionId: 'user-session-123',
|
|
69
|
-
enableMemory: true,
|
|
70
|
-
}
|
|
71
|
-
);
|
|
64
|
+
const result = await runtime.execute('my-agent', 'What time is it and what is 5 + 3?', {
|
|
65
|
+
sessionId: 'user-session-123',
|
|
66
|
+
enableMemory: true,
|
|
67
|
+
});
|
|
72
68
|
|
|
73
69
|
console.log(result.response);
|
|
74
70
|
// "The current time is 2024-12-13T15:30:00Z and 5 + 3 equals 8."
|
|
@@ -77,18 +73,23 @@ console.log(result.response);
|
|
|
77
73
|
## Key Concepts
|
|
78
74
|
|
|
79
75
|
### Agents
|
|
76
|
+
|
|
80
77
|
Stateful entities that execute over multiple steps with memory and tools.
|
|
81
78
|
|
|
82
79
|
### Tools
|
|
80
|
+
|
|
83
81
|
Functions that agents can call, with optional approval workflows.
|
|
84
82
|
|
|
85
83
|
### Memory
|
|
84
|
+
|
|
86
85
|
Automatic conversation history and context persistence.
|
|
87
86
|
|
|
88
87
|
### State Machine
|
|
88
|
+
|
|
89
89
|
Agents transition through states: idle → thinking → using_tool → completed
|
|
90
90
|
|
|
91
91
|
### Events
|
|
92
|
+
|
|
92
93
|
Subscribe to execution events for monitoring and debugging.
|
|
93
94
|
|
|
94
95
|
## Next Steps
|
|
@@ -101,6 +102,7 @@ Subscribe to execution events for monitoring and debugging.
|
|
|
101
102
|
## Common Patterns
|
|
102
103
|
|
|
103
104
|
### Tool with Approval
|
|
105
|
+
|
|
104
106
|
```typescript
|
|
105
107
|
@Tool({
|
|
106
108
|
description: 'Delete user account',
|
|
@@ -112,6 +114,7 @@ async deleteAccount(input: { userId: string }) {
|
|
|
112
114
|
```
|
|
113
115
|
|
|
114
116
|
### Subscribe to Events
|
|
117
|
+
|
|
115
118
|
```typescript
|
|
116
119
|
runtime.on('tool.approval.requested', (event) => {
|
|
117
120
|
console.log('Approval needed:', event.data);
|
|
@@ -120,6 +123,7 @@ runtime.on('tool.approval.requested', (event) => {
|
|
|
120
123
|
```
|
|
121
124
|
|
|
122
125
|
### Pause and Resume
|
|
126
|
+
|
|
123
127
|
```typescript
|
|
124
128
|
const result = await runtime.execute('agent', 'Start task');
|
|
125
129
|
|