@hazeljs/agent 0.2.0-alpha.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/IMPLEMENTATION_SUMMARY.md +400 -0
- package/LICENSE +192 -0
- package/PERSISTENCE.md +201 -0
- package/PRISMA_INTEGRATION.md +499 -0
- package/PRODUCTION_READINESS.md +264 -0
- package/QUICKSTART.md +135 -0
- package/README.md +541 -0
- package/STATE_VS_MEMORY.md +243 -0
- package/benchmarks/performance.benchmark.ts +157 -0
- package/coverage/clover.xml +1508 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +296 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov.info +2722 -0
- package/dist/agent.module.d.ts +57 -0
- package/dist/agent.module.d.ts.map +1 -0
- package/dist/agent.module.js +99 -0
- package/dist/agent.module.js.map +1 -0
- package/dist/context/agent.context.d.ts +27 -0
- package/dist/context/agent.context.d.ts.map +1 -0
- package/dist/context/agent.context.js +98 -0
- package/dist/context/agent.context.js.map +1 -0
- package/dist/decorators/agent.decorator.d.ts +21 -0
- package/dist/decorators/agent.decorator.d.ts.map +1 -0
- package/dist/decorators/agent.decorator.js +38 -0
- package/dist/decorators/agent.decorator.js.map +1 -0
- package/dist/decorators/delegate.decorator.d.ts +66 -0
- package/dist/decorators/delegate.decorator.d.ts.map +1 -0
- package/dist/decorators/delegate.decorator.js +108 -0
- package/dist/decorators/delegate.decorator.js.map +1 -0
- package/dist/decorators/tool.decorator.d.ts +23 -0
- package/dist/decorators/tool.decorator.d.ts.map +1 -0
- package/dist/decorators/tool.decorator.js +61 -0
- package/dist/decorators/tool.decorator.js.map +1 -0
- package/dist/events/event.emitter.d.ts +45 -0
- package/dist/events/event.emitter.d.ts.map +1 -0
- package/dist/events/event.emitter.js +96 -0
- package/dist/events/event.emitter.js.map +1 -0
- package/dist/executor/agent.executor.d.ts +58 -0
- package/dist/executor/agent.executor.d.ts.map +1 -0
- package/dist/executor/agent.executor.js +312 -0
- package/dist/executor/agent.executor.js.map +1 -0
- package/dist/executor/tool.executor.d.ts +55 -0
- package/dist/executor/tool.executor.d.ts.map +1 -0
- package/dist/executor/tool.executor.js +266 -0
- package/dist/executor/tool.executor.js.map +1 -0
- package/dist/graph/agent-graph.d.ts +131 -0
- package/dist/graph/agent-graph.d.ts.map +1 -0
- package/dist/graph/agent-graph.js +462 -0
- package/dist/graph/agent-graph.js.map +1 -0
- package/dist/graph/agent-graph.types.d.ts +210 -0
- package/dist/graph/agent-graph.types.d.ts.map +1 -0
- package/dist/graph/agent-graph.types.js +12 -0
- package/dist/graph/agent-graph.types.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/dist/prompts/agent-system.prompt.d.ts +10 -0
- package/dist/prompts/agent-system.prompt.d.ts.map +1 -0
- package/dist/prompts/agent-system.prompt.js +18 -0
- package/dist/prompts/agent-system.prompt.js.map +1 -0
- package/dist/prompts/index.d.ts +4 -0
- package/dist/prompts/index.d.ts.map +1 -0
- package/dist/prompts/index.js +20 -0
- package/dist/prompts/index.js.map +1 -0
- package/dist/prompts/supervisor-routing.prompt.d.ts +9 -0
- package/dist/prompts/supervisor-routing.prompt.d.ts.map +1 -0
- package/dist/prompts/supervisor-routing.prompt.js +22 -0
- package/dist/prompts/supervisor-routing.prompt.js.map +1 -0
- package/dist/prompts/supervisor-system.prompt.d.ts +9 -0
- package/dist/prompts/supervisor-system.prompt.d.ts.map +1 -0
- package/dist/prompts/supervisor-system.prompt.js +21 -0
- package/dist/prompts/supervisor-system.prompt.js.map +1 -0
- package/dist/registry/agent.registry.d.ts +49 -0
- package/dist/registry/agent.registry.d.ts.map +1 -0
- package/dist/registry/agent.registry.js +90 -0
- package/dist/registry/agent.registry.js.map +1 -0
- package/dist/registry/tool.registry.d.ts +54 -0
- package/dist/registry/tool.registry.d.ts.map +1 -0
- package/dist/registry/tool.registry.js +153 -0
- package/dist/registry/tool.registry.js.map +1 -0
- package/dist/runtime/agent.runtime.d.ts +218 -0
- package/dist/runtime/agent.runtime.d.ts.map +1 -0
- package/dist/runtime/agent.runtime.extensions.d.ts +49 -0
- package/dist/runtime/agent.runtime.extensions.d.ts.map +1 -0
- package/dist/runtime/agent.runtime.extensions.js +93 -0
- package/dist/runtime/agent.runtime.extensions.js.map +1 -0
- package/dist/runtime/agent.runtime.js +448 -0
- package/dist/runtime/agent.runtime.js.map +1 -0
- package/dist/state/agent-state.interface.d.ts +63 -0
- package/dist/state/agent-state.interface.d.ts.map +1 -0
- package/dist/state/agent-state.interface.js +7 -0
- package/dist/state/agent-state.interface.js.map +1 -0
- package/dist/state/agent.state.d.ts +67 -0
- package/dist/state/agent.state.d.ts.map +1 -0
- package/dist/state/agent.state.js +172 -0
- package/dist/state/agent.state.js.map +1 -0
- package/dist/state/database-state.manager.d.ts +63 -0
- package/dist/state/database-state.manager.d.ts.map +1 -0
- package/dist/state/database-state.manager.js +282 -0
- package/dist/state/database-state.manager.js.map +1 -0
- package/dist/state/redis-state.manager.d.ts +81 -0
- package/dist/state/redis-state.manager.d.ts.map +1 -0
- package/dist/state/redis-state.manager.js +253 -0
- package/dist/state/redis-state.manager.js.map +1 -0
- package/dist/supervisor/supervisor.d.ts +81 -0
- package/dist/supervisor/supervisor.d.ts.map +1 -0
- package/dist/supervisor/supervisor.js +220 -0
- package/dist/supervisor/supervisor.js.map +1 -0
- package/dist/types/agent.types.d.ts +166 -0
- package/dist/types/agent.types.d.ts.map +1 -0
- package/dist/types/agent.types.js +32 -0
- package/dist/types/agent.types.js.map +1 -0
- package/dist/types/event.types.d.ts +198 -0
- package/dist/types/event.types.d.ts.map +1 -0
- package/dist/types/event.types.js +46 -0
- package/dist/types/event.types.js.map +1 -0
- package/dist/types/llm.types.d.ts +66 -0
- package/dist/types/llm.types.d.ts.map +1 -0
- package/dist/types/llm.types.js +7 -0
- package/dist/types/llm.types.js.map +1 -0
- package/dist/types/rag.types.d.ts +40 -0
- package/dist/types/rag.types.d.ts.map +1 -0
- package/dist/types/rag.types.js +7 -0
- package/dist/types/rag.types.js.map +1 -0
- package/dist/types/tool.types.d.ts +118 -0
- package/dist/types/tool.types.d.ts.map +1 -0
- package/dist/types/tool.types.js +19 -0
- package/dist/types/tool.types.js.map +1 -0
- package/dist/utils/circuit-breaker.d.ts +9 -0
- package/dist/utils/circuit-breaker.d.ts.map +1 -0
- package/dist/utils/circuit-breaker.js +16 -0
- package/dist/utils/circuit-breaker.js.map +1 -0
- package/dist/utils/health-check.d.ts +71 -0
- package/dist/utils/health-check.d.ts.map +1 -0
- package/dist/utils/health-check.js +156 -0
- package/dist/utils/health-check.js.map +1 -0
- package/dist/utils/logger.d.ts +53 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +133 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/metrics.d.ts +90 -0
- package/dist/utils/metrics.d.ts.map +1 -0
- package/dist/utils/metrics.js +186 -0
- package/dist/utils/metrics.js.map +1 -0
- package/dist/utils/rate-limiter.d.ts +44 -0
- package/dist/utils/rate-limiter.d.ts.map +1 -0
- package/dist/utils/rate-limiter.js +82 -0
- package/dist/utils/rate-limiter.js.map +1 -0
- package/dist/utils/retry.d.ts +42 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +103 -0
- package/dist/utils/retry.js.map +1 -0
- package/jest.config.js +30 -0
- package/logs/combined.log +1 -0
- package/logs/error.log +0 -0
- package/package.json +74 -0
- package/prisma-schema.example.prisma +76 -0
- package/tests/setup.ts +17 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# Agent State vs Memory: Understanding the Difference
|
|
2
|
+
|
|
3
|
+
There are **two separate persistence layers** in the agent system that serve different purposes:
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
┌─────────────────────────────────────────────────────────┐
|
|
9
|
+
│ Agent Execution │
|
|
10
|
+
└─────────────────────────────────────────────────────────┘
|
|
11
|
+
│ │
|
|
12
|
+
▼ ▼
|
|
13
|
+
┌──────────────────────┐ ┌──────────────────────────────┐
|
|
14
|
+
│ Agent State │ │ Memory (RAG) │
|
|
15
|
+
│ Persistence │ │ Persistence │
|
|
16
|
+
│ │ │ │
|
|
17
|
+
│ - Execution flow │ │ - Conversation history │
|
|
18
|
+
│ - Steps │ │ - Entities │
|
|
19
|
+
│ - State transitions │ │ - Facts │
|
|
20
|
+
│ - Execution context │ │ - Working memory │
|
|
21
|
+
│ - Metadata │ │ - Long-term knowledge │
|
|
22
|
+
└──────────────────────┘ └──────────────────────────────┘
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Agent State Persistence
|
|
26
|
+
|
|
27
|
+
**Purpose**: Track the **execution flow** and **state machine** of agent runs
|
|
28
|
+
|
|
29
|
+
**What it stores**:
|
|
30
|
+
- ✅ Execution ID and context
|
|
31
|
+
- ✅ Current state (IDLE, THINKING, WAITING_FOR_APPROVAL, etc.)
|
|
32
|
+
- ✅ Execution steps (what the agent did)
|
|
33
|
+
- ✅ Step results and errors
|
|
34
|
+
- ✅ Execution metadata
|
|
35
|
+
- ✅ **Temporary** conversation history (during execution)
|
|
36
|
+
|
|
37
|
+
**Lifetime**:
|
|
38
|
+
- **Short-lived** - typically minutes to hours
|
|
39
|
+
- Ephemeral - deleted after execution completes (or TTL expires)
|
|
40
|
+
|
|
41
|
+
**Use cases**:
|
|
42
|
+
- Resume paused executions
|
|
43
|
+
- Track execution progress
|
|
44
|
+
- Debug failed executions
|
|
45
|
+
- Monitor active agent runs
|
|
46
|
+
- State machine transitions
|
|
47
|
+
|
|
48
|
+
**Backends**:
|
|
49
|
+
- In-Memory (default)
|
|
50
|
+
- Redis (production)
|
|
51
|
+
- Database/Prisma (audit)
|
|
52
|
+
|
|
53
|
+
**Example**:
|
|
54
|
+
```typescript
|
|
55
|
+
// Agent State tracks: "Agent executed step 1, 2, 3, now waiting for approval"
|
|
56
|
+
{
|
|
57
|
+
executionId: "abc-123",
|
|
58
|
+
state: "WAITING_FOR_APPROVAL",
|
|
59
|
+
steps: [
|
|
60
|
+
{ stepNumber: 1, action: "think", result: {...} },
|
|
61
|
+
{ stepNumber: 2, action: "use_tool", result: {...} },
|
|
62
|
+
{ stepNumber: 3, action: "ask_user", result: {...} }
|
|
63
|
+
]
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Memory Persistence (RAG MemoryManager)
|
|
68
|
+
|
|
69
|
+
**Purpose**: Store **long-term knowledge** and **context** across sessions
|
|
70
|
+
|
|
71
|
+
**What it stores**:
|
|
72
|
+
- ✅ Conversation history (across all sessions)
|
|
73
|
+
- ✅ Entities (people, places, things mentioned)
|
|
74
|
+
- ✅ Facts (learned information)
|
|
75
|
+
- ✅ Working memory (user preferences, session state)
|
|
76
|
+
- ✅ Events (important occurrences)
|
|
77
|
+
|
|
78
|
+
**Lifetime**:
|
|
79
|
+
- **Long-lived** - days, weeks, months
|
|
80
|
+
- Persistent - survives agent restarts
|
|
81
|
+
- Cross-session - shared across multiple agent runs
|
|
82
|
+
|
|
83
|
+
**Use cases**:
|
|
84
|
+
- Build context for new conversations
|
|
85
|
+
- Remember entities across sessions
|
|
86
|
+
- Store learned facts
|
|
87
|
+
- Maintain user preferences
|
|
88
|
+
- Semantic search of past conversations
|
|
89
|
+
|
|
90
|
+
**Backends**:
|
|
91
|
+
- BufferMemory (in-memory)
|
|
92
|
+
- VectorMemory (Pinecone, Weaviate, Qdrant, ChromaDB)
|
|
93
|
+
- HybridMemory (combination)
|
|
94
|
+
|
|
95
|
+
**Example**:
|
|
96
|
+
```typescript
|
|
97
|
+
// Memory stores: "User's name is John, likes coffee, mentioned Paris last week"
|
|
98
|
+
{
|
|
99
|
+
entities: [
|
|
100
|
+
{ name: "John", type: "person", attributes: {...} },
|
|
101
|
+
{ name: "Paris", type: "location", attributes: {...} }
|
|
102
|
+
],
|
|
103
|
+
facts: ["User prefers coffee over tea"],
|
|
104
|
+
workingMemory: { "user_preferences": { "drink": "coffee" } }
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Key Differences
|
|
109
|
+
|
|
110
|
+
| Aspect | Agent State | Memory (RAG) |
|
|
111
|
+
|--------|-------------|--------------|
|
|
112
|
+
| **Purpose** | Execution flow tracking | Long-term knowledge |
|
|
113
|
+
| **Lifetime** | Minutes to hours | Days to months |
|
|
114
|
+
| **Scope** | Single execution | Cross-session |
|
|
115
|
+
| **Data** | Steps, state, metadata | Conversations, entities, facts |
|
|
116
|
+
| **Query** | By executionId | Semantic search, by sessionId |
|
|
117
|
+
| **Backend** | Redis/DB | Vector stores (Pinecone, etc.) |
|
|
118
|
+
| **When used** | During execution | Before/after execution |
|
|
119
|
+
|
|
120
|
+
## How They Work Together
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
// 1. Agent starts execution
|
|
124
|
+
const context = stateManager.createContext(...);
|
|
125
|
+
|
|
126
|
+
// 2. Load memory (conversation history, entities) into context
|
|
127
|
+
await contextBuilder.buildWithMemory(context);
|
|
128
|
+
// ↑ This reads from Memory (RAG), populates context.memory
|
|
129
|
+
|
|
130
|
+
// 3. Agent executes (state tracked in Agent State)
|
|
131
|
+
await runtime.execute(...);
|
|
132
|
+
// ↑ State manager tracks steps, state transitions
|
|
133
|
+
|
|
134
|
+
// 4. After execution, persist to Memory
|
|
135
|
+
await contextBuilder.persistToMemory(context);
|
|
136
|
+
// ↑ This writes conversation history, entities to Memory (RAG)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Data Flow
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
┌─────────────────────────────────────────────────────────┐
|
|
143
|
+
│ Execution Start │
|
|
144
|
+
└─────────────────────────────────────────────────────────┘
|
|
145
|
+
│
|
|
146
|
+
▼
|
|
147
|
+
┌──────────────────────┐
|
|
148
|
+
│ Agent State │ ← Create execution context
|
|
149
|
+
│ (Redis/DB) │
|
|
150
|
+
└──────────────────────┘
|
|
151
|
+
│
|
|
152
|
+
▼
|
|
153
|
+
┌──────────────────────┐
|
|
154
|
+
│ Memory (RAG) │ ← Load conversation history, entities
|
|
155
|
+
│ (Vector Store) │ into execution context
|
|
156
|
+
└──────────────────────┘
|
|
157
|
+
│
|
|
158
|
+
▼
|
|
159
|
+
┌──────────────────────┐
|
|
160
|
+
│ Agent Executes │ ← State manager tracks steps
|
|
161
|
+
│ │ Memory provides context
|
|
162
|
+
└──────────────────────┘
|
|
163
|
+
│
|
|
164
|
+
▼
|
|
165
|
+
┌──────────────────────┐
|
|
166
|
+
│ Memory (RAG) │ ← Persist conversation, entities
|
|
167
|
+
│ (Vector Store) │ for future sessions
|
|
168
|
+
└──────────────────────┘
|
|
169
|
+
│
|
|
170
|
+
▼
|
|
171
|
+
┌──────────────────────┐
|
|
172
|
+
│ Agent State │ ← Archive execution (optional)
|
|
173
|
+
│ (Redis/DB) │
|
|
174
|
+
└──────────────────────┘
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Overlap: Conversation History
|
|
178
|
+
|
|
179
|
+
**Both** store conversation history, but for different reasons:
|
|
180
|
+
|
|
181
|
+
### Agent State Conversation History
|
|
182
|
+
- **Purpose**: Track messages **during** execution
|
|
183
|
+
- **Scope**: Single execution only
|
|
184
|
+
- **Lifetime**: Until execution completes
|
|
185
|
+
- **Use**: Resume paused executions, debug current run
|
|
186
|
+
|
|
187
|
+
### Memory Conversation History
|
|
188
|
+
- **Purpose**: Build context for **future** conversations
|
|
189
|
+
- **Scope**: All sessions for a user/session
|
|
190
|
+
- **Lifetime**: Long-term (weeks/months)
|
|
191
|
+
- **Use**: Semantic search, context building, continuity
|
|
192
|
+
|
|
193
|
+
## When to Use Which
|
|
194
|
+
|
|
195
|
+
### Use Agent State Persistence when:
|
|
196
|
+
- ✅ You need to resume paused executions
|
|
197
|
+
- ✅ You want to track execution progress
|
|
198
|
+
- ✅ You need to debug failed runs
|
|
199
|
+
- ✅ You want to monitor active agents
|
|
200
|
+
- ✅ You need execution audit trails
|
|
201
|
+
|
|
202
|
+
### Use Memory Persistence when:
|
|
203
|
+
- ✅ You want agents to remember past conversations
|
|
204
|
+
- ✅ You need entity tracking across sessions
|
|
205
|
+
- ✅ You want to store learned facts
|
|
206
|
+
- ✅ You need semantic search of conversations
|
|
207
|
+
- ✅ You want to maintain user preferences
|
|
208
|
+
|
|
209
|
+
## Recommended Setup
|
|
210
|
+
|
|
211
|
+
### Development
|
|
212
|
+
```typescript
|
|
213
|
+
// In-memory for both (default)
|
|
214
|
+
const runtime = new AgentRuntime({
|
|
215
|
+
// stateManager: default (in-memory)
|
|
216
|
+
// memoryManager: default (BufferMemory)
|
|
217
|
+
});
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Production
|
|
221
|
+
```typescript
|
|
222
|
+
// Redis for agent state (fast, distributed)
|
|
223
|
+
// Vector store (Pinecone) for memory (semantic search)
|
|
224
|
+
const stateManager = new RedisStateManager({ client: redisClient });
|
|
225
|
+
const memoryStore = new VectorMemory(pineconeStore, embeddings);
|
|
226
|
+
const memoryManager = new MemoryManager(memoryStore);
|
|
227
|
+
|
|
228
|
+
const runtime = new AgentRuntime({
|
|
229
|
+
stateManager, // ← Agent execution state
|
|
230
|
+
memoryManager, // ← Long-term memory
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Summary
|
|
235
|
+
|
|
236
|
+
- **Agent State** = "What is the agent doing right now?"
|
|
237
|
+
- **Memory** = "What does the agent know from past conversations?"
|
|
238
|
+
|
|
239
|
+
They complement each other:
|
|
240
|
+
- **State** enables resumable, trackable executions
|
|
241
|
+
- **Memory** enables context-aware, continuous conversations
|
|
242
|
+
|
|
243
|
+
Both are needed for a production-ready agent system!
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Performance Benchmarks
|
|
3
|
+
* Measure agent runtime performance under various conditions
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { AgentRuntime } from '../src/runtime/agent.runtime';
|
|
7
|
+
import { Agent } from '../src/decorators/agent.decorator';
|
|
8
|
+
import { Tool } from '../src/decorators/tool.decorator';
|
|
9
|
+
|
|
10
|
+
interface BenchmarkResult {
|
|
11
|
+
name: string;
|
|
12
|
+
iterations: number;
|
|
13
|
+
totalTime: number;
|
|
14
|
+
averageTime: number;
|
|
15
|
+
minTime: number;
|
|
16
|
+
maxTime: number;
|
|
17
|
+
throughput: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class PerformanceBenchmark {
|
|
21
|
+
/**
|
|
22
|
+
* Run a benchmark
|
|
23
|
+
*/
|
|
24
|
+
async runBenchmark(
|
|
25
|
+
name: string,
|
|
26
|
+
fn: () => Promise<void>,
|
|
27
|
+
iterations: number = 100
|
|
28
|
+
): Promise<BenchmarkResult> {
|
|
29
|
+
const times: number[] = [];
|
|
30
|
+
const startTotal = Date.now();
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < iterations; i++) {
|
|
33
|
+
const start = Date.now();
|
|
34
|
+
await fn();
|
|
35
|
+
const duration = Date.now() - start;
|
|
36
|
+
times.push(duration);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const totalTime = Date.now() - startTotal;
|
|
40
|
+
const averageTime = times.reduce((a, b) => a + b, 0) / times.length;
|
|
41
|
+
const minTime = Math.min(...times);
|
|
42
|
+
const maxTime = Math.max(...times);
|
|
43
|
+
const throughput = (iterations / totalTime) * 1000;
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
name,
|
|
47
|
+
iterations,
|
|
48
|
+
totalTime,
|
|
49
|
+
averageTime,
|
|
50
|
+
minTime,
|
|
51
|
+
maxTime,
|
|
52
|
+
throughput,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Format benchmark results
|
|
58
|
+
*/
|
|
59
|
+
formatResults(results: BenchmarkResult[]): string {
|
|
60
|
+
const lines: string[] = [
|
|
61
|
+
'Performance Benchmark Results',
|
|
62
|
+
'============================',
|
|
63
|
+
'',
|
|
64
|
+
];
|
|
65
|
+
|
|
66
|
+
for (const result of results) {
|
|
67
|
+
lines.push(`${result.name}:`);
|
|
68
|
+
lines.push(` Iterations: ${result.iterations}`);
|
|
69
|
+
lines.push(` Total Time: ${result.totalTime.toFixed(2)}ms`);
|
|
70
|
+
lines.push(` Average: ${result.averageTime.toFixed(2)}ms`);
|
|
71
|
+
lines.push(` Min: ${result.minTime.toFixed(2)}ms`);
|
|
72
|
+
lines.push(` Max: ${result.maxTime.toFixed(2)}ms`);
|
|
73
|
+
lines.push(` Throughput: ${result.throughput.toFixed(2)} ops/sec`);
|
|
74
|
+
lines.push('');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return lines.join('\n');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Example benchmarks
|
|
83
|
+
*/
|
|
84
|
+
async function runBenchmarks() {
|
|
85
|
+
const benchmark = new PerformanceBenchmark();
|
|
86
|
+
const results: BenchmarkResult[] = [];
|
|
87
|
+
|
|
88
|
+
// Benchmark 1: Agent Registration
|
|
89
|
+
@Agent({ name: 'bench-agent', description: 'Benchmark agent' })
|
|
90
|
+
class BenchAgent {
|
|
91
|
+
@Tool({ description: 'Benchmark tool', parameters: [] })
|
|
92
|
+
async benchTool() {
|
|
93
|
+
return { result: 'ok' };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const runtime = new AgentRuntime({
|
|
98
|
+
enableMetrics: true,
|
|
99
|
+
rateLimitPerMinute: 10000,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
results.push(
|
|
103
|
+
await benchmark.runBenchmark(
|
|
104
|
+
'Agent Registration',
|
|
105
|
+
async () => {
|
|
106
|
+
const agent = new BenchAgent();
|
|
107
|
+
runtime.registerAgentInstance('bench-agent', agent);
|
|
108
|
+
},
|
|
109
|
+
1000
|
|
110
|
+
)
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
// Benchmark 2: Tool Registry Lookup
|
|
114
|
+
const agent = new BenchAgent();
|
|
115
|
+
runtime.registerAgentInstance('bench-agent', agent);
|
|
116
|
+
|
|
117
|
+
results.push(
|
|
118
|
+
await benchmark.runBenchmark(
|
|
119
|
+
'Tool Registry Lookup',
|
|
120
|
+
async () => {
|
|
121
|
+
runtime.getAgentTools('bench-agent');
|
|
122
|
+
},
|
|
123
|
+
10000
|
|
124
|
+
)
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
// Benchmark 3: Metrics Collection
|
|
128
|
+
results.push(
|
|
129
|
+
await benchmark.runBenchmark(
|
|
130
|
+
'Metrics Collection',
|
|
131
|
+
async () => {
|
|
132
|
+
runtime.getMetrics();
|
|
133
|
+
},
|
|
134
|
+
10000
|
|
135
|
+
)
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
// Benchmark 4: Health Check
|
|
139
|
+
results.push(
|
|
140
|
+
await benchmark.runBenchmark(
|
|
141
|
+
'Health Check',
|
|
142
|
+
async () => {
|
|
143
|
+
await runtime.healthCheck();
|
|
144
|
+
},
|
|
145
|
+
100
|
|
146
|
+
)
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
console.log(benchmark.formatResults(results));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Run benchmarks if executed directly
|
|
153
|
+
if (require.main === module) {
|
|
154
|
+
runBenchmarks().catch(console.error);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export { PerformanceBenchmark, runBenchmarks };
|