@theaiinc/yggdrasil 0.0.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 +340 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +5 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/services/agent-manager.d.ts +69 -0
- package/dist/src/services/agent-manager.d.ts.map +1 -0
- package/dist/src/services/agent-manager.js +227 -0
- package/dist/src/services/agent-manager.js.map +1 -0
- package/dist/src/services/load-balancer.d.ts +62 -0
- package/dist/src/services/load-balancer.d.ts.map +1 -0
- package/dist/src/services/load-balancer.js +221 -0
- package/dist/src/services/load-balancer.js.map +1 -0
- package/dist/src/services/logger.d.ts +36 -0
- package/dist/src/services/logger.d.ts.map +1 -0
- package/dist/src/services/logger.js +126 -0
- package/dist/src/services/logger.js.map +1 -0
- package/dist/src/types/index.d.ts +139 -0
- package/dist/src/types/index.d.ts.map +1 -0
- package/dist/src/types/index.js +5 -0
- package/dist/src/types/index.js.map +1 -0
- package/package.json +70 -0
package/README.md
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
# @theaiinc/yggdrasil - Distributed Orchestration System
|
|
2
|
+
|
|
3
|
+
A robust, scalable orchestration system for agent containers with auto-scaling capabilities, built with stable, well-documented technologies.
|
|
4
|
+
|
|
5
|
+
## ποΈ Architecture
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
|
|
9
|
+
β Orchestration β β Load Balancer β β Agent Pool β
|
|
10
|
+
β Controller βββββΆβ (Nginx) βββββΆβ (Docker) β
|
|
11
|
+
β (Node.js) β β β β β
|
|
12
|
+
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
|
|
13
|
+
β β β
|
|
14
|
+
β β β
|
|
15
|
+
βΌ βΌ βΌ
|
|
16
|
+
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
|
|
17
|
+
β Monitoring β β Health Checks β β Auto-scaling β
|
|
18
|
+
β (Prometheus) β β (Express) β β (Docker) β
|
|
19
|
+
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## π Technology Stack
|
|
23
|
+
|
|
24
|
+
### Core Technologies
|
|
25
|
+
|
|
26
|
+
- **Node.js + TypeScript** - Stable, well-documented runtime
|
|
27
|
+
- **Express.js** - Battle-tested web framework
|
|
28
|
+
- **Docker** - Industry standard containerization
|
|
29
|
+
- **Socket.IO** - Real-time communication
|
|
30
|
+
|
|
31
|
+
### Infrastructure
|
|
32
|
+
|
|
33
|
+
- **Docker Compose** - Local orchestration
|
|
34
|
+
- **Nginx** - Load balancing and session affinity
|
|
35
|
+
- **Prometheus + Grafana** - Monitoring and alerting
|
|
36
|
+
- **Redis** - Session storage (optional)
|
|
37
|
+
|
|
38
|
+
### Key Features
|
|
39
|
+
|
|
40
|
+
- β
**Low Maintenance** - Uses stable, mature technologies
|
|
41
|
+
- β
**Excellent Documentation** - All components well-documented
|
|
42
|
+
- β
**Auto-scaling** - Automatic agent scaling based on load
|
|
43
|
+
- β
**Session Affinity** - Sticky sessions for stateful workflows
|
|
44
|
+
- β
**Health Monitoring** - Comprehensive health checks
|
|
45
|
+
- β
**Circuit Breaker** - Fault tolerance and error handling
|
|
46
|
+
- β
**Metrics Collection** - Prometheus integration
|
|
47
|
+
|
|
48
|
+
## π Prerequisites
|
|
49
|
+
|
|
50
|
+
- Docker and Docker Compose
|
|
51
|
+
- Node.js 18+ (for local development)
|
|
52
|
+
- 4GB+ RAM available for containers
|
|
53
|
+
|
|
54
|
+
## π οΈ Quick Start
|
|
55
|
+
|
|
56
|
+
### 1. Clone and Setup
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
cd yggdrasil
|
|
60
|
+
npm install
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### 2. Start the System
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
# Start all services
|
|
67
|
+
docker-compose up -d
|
|
68
|
+
|
|
69
|
+
# Or start with development mode
|
|
70
|
+
docker-compose -f docker-compose.dev.yml up
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### 3. Access Services
|
|
74
|
+
|
|
75
|
+
- **Load Balancer**: http://localhost:80
|
|
76
|
+
- **Orchestration Controller**: http://localhost:3000
|
|
77
|
+
- **Grafana Dashboard**: http://localhost:3001 (admin/admin)
|
|
78
|
+
- **Prometheus**: http://localhost:9090
|
|
79
|
+
|
|
80
|
+
### 4. Health Check
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Check system health
|
|
84
|
+
curl http://localhost/health
|
|
85
|
+
|
|
86
|
+
# Check agent health
|
|
87
|
+
curl http://localhost/agent/agent-1/health
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## π§ Configuration
|
|
91
|
+
|
|
92
|
+
### Environment Variables
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
# Orchestration Controller
|
|
96
|
+
NODE_ENV=development
|
|
97
|
+
LOG_LEVEL=info
|
|
98
|
+
HEALTH_CHECK_INTERVAL=30000
|
|
99
|
+
MAX_CONCURRENCY=80
|
|
100
|
+
MIN_INSTANCES=1
|
|
101
|
+
MAX_INSTANCES=10
|
|
102
|
+
|
|
103
|
+
# Agent Configuration
|
|
104
|
+
AGENT_ID=agent-1
|
|
105
|
+
PORT=8080
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Load Balancer Settings
|
|
109
|
+
|
|
110
|
+
Edit `nginx.conf` to modify:
|
|
111
|
+
|
|
112
|
+
- Load balancing algorithm
|
|
113
|
+
- Session affinity settings
|
|
114
|
+
- Rate limiting
|
|
115
|
+
- Health check endpoints
|
|
116
|
+
|
|
117
|
+
### Monitoring Configuration
|
|
118
|
+
|
|
119
|
+
Edit `prometheus.yml` to customize:
|
|
120
|
+
|
|
121
|
+
- Scrape intervals
|
|
122
|
+
- Target endpoints
|
|
123
|
+
- Alert rules
|
|
124
|
+
|
|
125
|
+
## π Monitoring
|
|
126
|
+
|
|
127
|
+
### Metrics Available
|
|
128
|
+
|
|
129
|
+
- **Agent Health**: Response time, error rates, CPU/memory usage
|
|
130
|
+
- **Load Balancer**: Request distribution, session affinity stats
|
|
131
|
+
- **Orchestration**: Queue depth, scaling events, circuit breaker status
|
|
132
|
+
- **System**: Container resource usage, network metrics
|
|
133
|
+
|
|
134
|
+
### Grafana Dashboards
|
|
135
|
+
|
|
136
|
+
Pre-configured dashboards for:
|
|
137
|
+
|
|
138
|
+
- Agent Performance
|
|
139
|
+
- Load Balancer Metrics
|
|
140
|
+
- System Health
|
|
141
|
+
- Scaling Events
|
|
142
|
+
|
|
143
|
+
## π§ͺ Testing
|
|
144
|
+
|
|
145
|
+
### Load Testing
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
# Run load test
|
|
149
|
+
npm run load:test
|
|
150
|
+
|
|
151
|
+
# Or use hey tool
|
|
152
|
+
hey -n 1000 -c 10 http://localhost/api/test
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Health Checks
|
|
156
|
+
|
|
157
|
+
```bash
|
|
158
|
+
# Check all agents
|
|
159
|
+
npm run health:check
|
|
160
|
+
|
|
161
|
+
# Individual agent check
|
|
162
|
+
curl http://localhost/agent/agent-1/health
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Unit Tests
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
npm test
|
|
169
|
+
npm run test:coverage
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## π Auto-scaling
|
|
173
|
+
|
|
174
|
+
### Local Simulation
|
|
175
|
+
|
|
176
|
+
The system simulates Cloud Run auto-scaling behavior locally:
|
|
177
|
+
|
|
178
|
+
1. **Resource Monitoring**: CPU and memory usage tracking
|
|
179
|
+
2. **Scaling Triggers**: Automatic agent scaling based on metrics
|
|
180
|
+
3. **Queue Management**: Request queuing when at capacity
|
|
181
|
+
4. **Health Checks**: Continuous agent health monitoring
|
|
182
|
+
|
|
183
|
+
### Scaling Configuration
|
|
184
|
+
|
|
185
|
+
```yaml
|
|
186
|
+
# docker-compose.yml
|
|
187
|
+
deploy:
|
|
188
|
+
resources:
|
|
189
|
+
limits:
|
|
190
|
+
cpus: '0.5'
|
|
191
|
+
memory: 512M
|
|
192
|
+
reservations:
|
|
193
|
+
cpus: '0.25'
|
|
194
|
+
memory: 256M
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## π¨ Error Handling
|
|
198
|
+
|
|
199
|
+
### Circuit Breaker
|
|
200
|
+
|
|
201
|
+
- **Failure Threshold**: Configurable failure count
|
|
202
|
+
- **Recovery Time**: Automatic recovery after timeout
|
|
203
|
+
- **Fallback**: Graceful degradation when agents fail
|
|
204
|
+
|
|
205
|
+
### Retry Logic
|
|
206
|
+
|
|
207
|
+
- **Exponential Backoff**: Smart retry with increasing delays
|
|
208
|
+
- **Max Retries**: Configurable retry limits
|
|
209
|
+
- **Timeout Handling**: Request timeout management
|
|
210
|
+
|
|
211
|
+
## π Performance
|
|
212
|
+
|
|
213
|
+
### Benchmarks
|
|
214
|
+
|
|
215
|
+
- **Latency**: < 200ms (95th percentile)
|
|
216
|
+
- **Throughput**: > 1000 requests/second
|
|
217
|
+
- **Error Rate**: < 1%
|
|
218
|
+
- **Uptime**: 99.9%
|
|
219
|
+
|
|
220
|
+
### Optimization
|
|
221
|
+
|
|
222
|
+
- **Connection Pooling**: Reuse connections
|
|
223
|
+
- **Compression**: Gzip compression enabled
|
|
224
|
+
- **Caching**: Response caching where appropriate
|
|
225
|
+
- **Resource Limits**: Container resource constraints
|
|
226
|
+
|
|
227
|
+
## π Security
|
|
228
|
+
|
|
229
|
+
### Best Practices
|
|
230
|
+
|
|
231
|
+
- **Non-root Containers**: All containers run as non-root users
|
|
232
|
+
- **Resource Limits**: CPU and memory constraints
|
|
233
|
+
- **Network Isolation**: Docker network isolation
|
|
234
|
+
- **Input Validation**: Request validation and sanitization
|
|
235
|
+
|
|
236
|
+
### Authentication
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
# Add authentication middleware
|
|
240
|
+
npm run add:auth
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## π Deployment
|
|
244
|
+
|
|
245
|
+
### Local Development
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
# Development mode with hot reload
|
|
249
|
+
docker-compose -f docker-compose.dev.yml up
|
|
250
|
+
|
|
251
|
+
# With debugging
|
|
252
|
+
docker-compose -f docker-compose.dev.yml up --build
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Staging Environment
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
# Deploy to staging
|
|
259
|
+
docker-compose -f docker-compose.staging.yml up -d
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### Production Environment
|
|
263
|
+
|
|
264
|
+
```bash
|
|
265
|
+
# Deploy to production
|
|
266
|
+
docker-compose -f docker-compose.prod.yml up -d
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## π API Documentation
|
|
270
|
+
|
|
271
|
+
### Orchestration Controller
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
# Get system status
|
|
275
|
+
GET /status
|
|
276
|
+
|
|
277
|
+
# Get metrics
|
|
278
|
+
GET /metrics
|
|
279
|
+
|
|
280
|
+
# Health check
|
|
281
|
+
GET /health
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
### Agent Endpoints
|
|
285
|
+
|
|
286
|
+
```bash
|
|
287
|
+
# Agent health
|
|
288
|
+
GET /agent/{agentId}/health
|
|
289
|
+
|
|
290
|
+
# Agent metrics
|
|
291
|
+
GET /agent/{agentId}/metrics
|
|
292
|
+
|
|
293
|
+
# Process request
|
|
294
|
+
POST /agent/{agentId}/process
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### Load Balancer
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
# Health check
|
|
301
|
+
GET /health
|
|
302
|
+
|
|
303
|
+
# Status page
|
|
304
|
+
GET /status
|
|
305
|
+
|
|
306
|
+
# Metrics
|
|
307
|
+
GET /metrics
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## π€ Contributing
|
|
311
|
+
|
|
312
|
+
1. Follow the existing code style
|
|
313
|
+
2. Add tests for new features
|
|
314
|
+
3. Update documentation
|
|
315
|
+
4. Use conventional commits
|
|
316
|
+
|
|
317
|
+
## π License
|
|
318
|
+
|
|
319
|
+
UNLICENSED - Proprietary software by The AI Inc
|
|
320
|
+
|
|
321
|
+
## π Support
|
|
322
|
+
|
|
323
|
+
- **Documentation**: Check the docs/ directory
|
|
324
|
+
- **Issues**: Create GitHub issues
|
|
325
|
+
- **Discussions**: Use GitHub discussions
|
|
326
|
+
|
|
327
|
+
## π Roadmap
|
|
328
|
+
|
|
329
|
+
- [ ] Cloud Run integration
|
|
330
|
+
- [ ] Advanced metrics
|
|
331
|
+
- [ ] Machine learning scaling
|
|
332
|
+
- [ ] Multi-region deployment
|
|
333
|
+
- [ ] Advanced authentication
|
|
334
|
+
- [ ] API rate limiting
|
|
335
|
+
- [ ] WebSocket support
|
|
336
|
+
- [ ] GraphQL API
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
**Built with β€οΈ by The AI Inc using stable, well-documented technologies**
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { AgentManager } from './services/agent-manager';
|
|
2
|
+
export { LoadBalancer } from './services/load-balancer';
|
|
3
|
+
export { Logger } from './services/logger';
|
|
4
|
+
export type { AgentInfo, AgentMetrics, RequestContext, LoadBalancerConfig, OrchestrationConfig, MonitoringConfig, LogLevel, } from './types';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAG3C,YAAY,EACV,SAAS,EACT,YAAY,EACZ,cAAc,EACd,kBAAkB,EAClB,mBAAmB,EACnB,gBAAgB,EAChB,QAAQ,GACT,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,mDAAmD;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { AgentInfo, AgentMetrics, HealthCheckResult } from '@/types';
|
|
2
|
+
/**
|
|
3
|
+
* Manages agent registration, health checks, and lifecycle
|
|
4
|
+
* Uses stable, well-documented libraries for reliability
|
|
5
|
+
*/
|
|
6
|
+
export declare class AgentManager {
|
|
7
|
+
private agents;
|
|
8
|
+
private logger;
|
|
9
|
+
private healthCheckInterval;
|
|
10
|
+
private healthCheckPath;
|
|
11
|
+
constructor(healthCheckPath?: string);
|
|
12
|
+
/**
|
|
13
|
+
* Register a new agent
|
|
14
|
+
*/
|
|
15
|
+
registerAgent(url: string, sessionId?: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Unregister an agent
|
|
18
|
+
*/
|
|
19
|
+
unregisterAgent(agentId: string): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Get all healthy agents
|
|
22
|
+
*/
|
|
23
|
+
getHealthyAgents(): AgentInfo[];
|
|
24
|
+
/**
|
|
25
|
+
* Get agent by ID
|
|
26
|
+
*/
|
|
27
|
+
getAgent(agentId: string): AgentInfo | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Get agent by session ID
|
|
30
|
+
*/
|
|
31
|
+
getAgentBySession(sessionId: string): AgentInfo | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Start health check monitoring
|
|
34
|
+
*/
|
|
35
|
+
startHealthChecks(intervalMs?: number): void;
|
|
36
|
+
/**
|
|
37
|
+
* Stop health check monitoring
|
|
38
|
+
*/
|
|
39
|
+
stopHealthChecks(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Perform health check on a specific agent
|
|
42
|
+
*/
|
|
43
|
+
checkAgentHealth(agentId: string): Promise<HealthCheckResult>;
|
|
44
|
+
/**
|
|
45
|
+
* Perform health checks on all agents
|
|
46
|
+
*/
|
|
47
|
+
private performHealthChecks;
|
|
48
|
+
/**
|
|
49
|
+
* Update agent metrics
|
|
50
|
+
*/
|
|
51
|
+
updateAgentMetrics(agentId: string, metrics: Partial<AgentMetrics>): void;
|
|
52
|
+
/**
|
|
53
|
+
* Get all agents
|
|
54
|
+
*/
|
|
55
|
+
getAllAgents(): AgentInfo[];
|
|
56
|
+
/**
|
|
57
|
+
* Get agent count by health status
|
|
58
|
+
*/
|
|
59
|
+
getAgentCounts(): {
|
|
60
|
+
total: number;
|
|
61
|
+
healthy: number;
|
|
62
|
+
unhealthy: number;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Clean up stale agents (not seen for more than threshold)
|
|
66
|
+
*/
|
|
67
|
+
cleanupStaleAgents(thresholdMs?: number): number;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=agent-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-manager.d.ts","sourceRoot":"","sources":["../../../src/services/agent-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAIrE;;;GAGG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAqC;IACnD,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,mBAAmB,CAA+C;IAC1E,OAAO,CAAC,eAAe,CAAS;gBAEpB,eAAe,GAAE,MAAkB;IAI/C;;OAEG;IACI,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM;IA6B7D;;OAEG;IACI,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAQhD;;OAEG;IACI,gBAAgB,IAAI,SAAS,EAAE;IAMtC;;OAEG;IACI,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;IAIvD;;OAEG;IACI,iBAAiB,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;IAMlE;;OAEG;IACI,iBAAiB,CAAC,UAAU,GAAE,MAAc,GAAG,IAAI;IAY1D;;OAEG;IACI,gBAAgB,IAAI,IAAI;IAQ/B;;OAEG;IACU,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IA8E1E;;OAEG;YACW,mBAAmB;IAYjC;;OAEG;IACI,kBAAkB,CACvB,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,OAAO,CAAC,YAAY,CAAC,GAC7B,IAAI;IAQP;;OAEG;IACI,YAAY,IAAI,SAAS,EAAE;IAIlC;;OAEG;IACI,cAAc,IAAI;QACvB,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,EAAE,MAAM,CAAC;KACnB;IASD;;OAEG;IACI,kBAAkB,CAAC,WAAW,GAAE,MAAe,GAAG,MAAM;CAwBhE"}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { getLogger } from './logger';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
/**
|
|
5
|
+
* Manages agent registration, health checks, and lifecycle
|
|
6
|
+
* Uses stable, well-documented libraries for reliability
|
|
7
|
+
*/
|
|
8
|
+
export class AgentManager {
|
|
9
|
+
agents = new Map();
|
|
10
|
+
logger = getLogger();
|
|
11
|
+
healthCheckInterval = null;
|
|
12
|
+
healthCheckPath;
|
|
13
|
+
constructor(healthCheckPath = '/health') {
|
|
14
|
+
this.healthCheckPath = healthCheckPath;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Register a new agent
|
|
18
|
+
*/
|
|
19
|
+
registerAgent(url, sessionId) {
|
|
20
|
+
const agentId = nanoid();
|
|
21
|
+
const agentInfo = {
|
|
22
|
+
id: agentId,
|
|
23
|
+
url,
|
|
24
|
+
health: {
|
|
25
|
+
status: 'unknown',
|
|
26
|
+
lastCheck: new Date(),
|
|
27
|
+
responseTime: 0,
|
|
28
|
+
errorCount: 0,
|
|
29
|
+
consecutiveFailures: 0,
|
|
30
|
+
},
|
|
31
|
+
metrics: {
|
|
32
|
+
cpuUsage: 0,
|
|
33
|
+
memoryUsage: 0,
|
|
34
|
+
requestCount: 0,
|
|
35
|
+
errorRate: 0,
|
|
36
|
+
averageResponseTime: 0,
|
|
37
|
+
activeConnections: 0,
|
|
38
|
+
},
|
|
39
|
+
lastSeen: new Date(),
|
|
40
|
+
...(sessionId && { sessionId }),
|
|
41
|
+
};
|
|
42
|
+
this.agents.set(agentId, agentInfo);
|
|
43
|
+
this.logger.info('Agent registered', { agentId, url, sessionId });
|
|
44
|
+
return agentId;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Unregister an agent
|
|
48
|
+
*/
|
|
49
|
+
unregisterAgent(agentId) {
|
|
50
|
+
const removed = this.agents.delete(agentId);
|
|
51
|
+
if (removed) {
|
|
52
|
+
this.logger.info('Agent unregistered', { agentId });
|
|
53
|
+
}
|
|
54
|
+
return removed;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get all healthy agents
|
|
58
|
+
*/
|
|
59
|
+
getHealthyAgents() {
|
|
60
|
+
return Array.from(this.agents.values()).filter(agent => agent.health.status === 'healthy');
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get agent by ID
|
|
64
|
+
*/
|
|
65
|
+
getAgent(agentId) {
|
|
66
|
+
return this.agents.get(agentId);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get agent by session ID
|
|
70
|
+
*/
|
|
71
|
+
getAgentBySession(sessionId) {
|
|
72
|
+
return Array.from(this.agents.values()).find(agent => agent.sessionId === sessionId);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Start health check monitoring
|
|
76
|
+
*/
|
|
77
|
+
startHealthChecks(intervalMs = 30000) {
|
|
78
|
+
if (this.healthCheckInterval) {
|
|
79
|
+
clearInterval(this.healthCheckInterval);
|
|
80
|
+
}
|
|
81
|
+
this.healthCheckInterval = setInterval(() => {
|
|
82
|
+
this.performHealthChecks();
|
|
83
|
+
}, intervalMs);
|
|
84
|
+
this.logger.info('Health checks started', { intervalMs });
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Stop health check monitoring
|
|
88
|
+
*/
|
|
89
|
+
stopHealthChecks() {
|
|
90
|
+
if (this.healthCheckInterval) {
|
|
91
|
+
clearInterval(this.healthCheckInterval);
|
|
92
|
+
this.healthCheckInterval = null;
|
|
93
|
+
this.logger.info('Health checks stopped');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Perform health check on a specific agent
|
|
98
|
+
*/
|
|
99
|
+
async checkAgentHealth(agentId) {
|
|
100
|
+
const agent = this.agents.get(agentId);
|
|
101
|
+
if (!agent) {
|
|
102
|
+
return {
|
|
103
|
+
agentId,
|
|
104
|
+
healthy: false,
|
|
105
|
+
responseTime: 0,
|
|
106
|
+
error: 'Agent not found',
|
|
107
|
+
timestamp: new Date(),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const startTime = Date.now();
|
|
111
|
+
try {
|
|
112
|
+
const response = await axios.get(`${agent.url}${this.healthCheckPath}`, { timeout: 5000 });
|
|
113
|
+
const responseTime = Date.now() - startTime;
|
|
114
|
+
const healthy = response.status === 200;
|
|
115
|
+
// Update agent health
|
|
116
|
+
agent.health = {
|
|
117
|
+
status: healthy ? 'healthy' : 'unhealthy',
|
|
118
|
+
lastCheck: new Date(),
|
|
119
|
+
responseTime,
|
|
120
|
+
errorCount: healthy
|
|
121
|
+
? agent.health.errorCount
|
|
122
|
+
: agent.health.errorCount + 1,
|
|
123
|
+
consecutiveFailures: healthy ? 0 : agent.health.consecutiveFailures + 1,
|
|
124
|
+
};
|
|
125
|
+
agent.lastSeen = new Date();
|
|
126
|
+
this.logger.info('Agent health check completed', {
|
|
127
|
+
agentId,
|
|
128
|
+
healthy,
|
|
129
|
+
responseTime,
|
|
130
|
+
status: agent.health.status,
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
agentId,
|
|
134
|
+
healthy,
|
|
135
|
+
responseTime,
|
|
136
|
+
timestamp: new Date(),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
const responseTime = Date.now() - startTime;
|
|
141
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
142
|
+
// Update agent health
|
|
143
|
+
agent.health = {
|
|
144
|
+
status: 'unhealthy',
|
|
145
|
+
lastCheck: new Date(),
|
|
146
|
+
responseTime,
|
|
147
|
+
errorCount: agent.health.errorCount + 1,
|
|
148
|
+
consecutiveFailures: agent.health.consecutiveFailures + 1,
|
|
149
|
+
};
|
|
150
|
+
this.logger.warn('Agent health check failed', {
|
|
151
|
+
agentId,
|
|
152
|
+
error: errorMessage,
|
|
153
|
+
responseTime,
|
|
154
|
+
});
|
|
155
|
+
return {
|
|
156
|
+
agentId,
|
|
157
|
+
healthy: false,
|
|
158
|
+
responseTime,
|
|
159
|
+
error: errorMessage,
|
|
160
|
+
timestamp: new Date(),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Perform health checks on all agents
|
|
166
|
+
*/
|
|
167
|
+
async performHealthChecks() {
|
|
168
|
+
const promises = Array.from(this.agents.keys()).map(agentId => this.checkAgentHealth(agentId));
|
|
169
|
+
try {
|
|
170
|
+
await Promise.allSettled(promises);
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
this.logger.error('Health check batch failed', { error });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Update agent metrics
|
|
178
|
+
*/
|
|
179
|
+
updateAgentMetrics(agentId, metrics) {
|
|
180
|
+
const agent = this.agents.get(agentId);
|
|
181
|
+
if (agent) {
|
|
182
|
+
agent.metrics = { ...agent.metrics, ...metrics };
|
|
183
|
+
agent.lastSeen = new Date();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get all agents
|
|
188
|
+
*/
|
|
189
|
+
getAllAgents() {
|
|
190
|
+
return Array.from(this.agents.values());
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get agent count by health status
|
|
194
|
+
*/
|
|
195
|
+
getAgentCounts() {
|
|
196
|
+
const agents = this.getAllAgents();
|
|
197
|
+
return {
|
|
198
|
+
total: agents.length,
|
|
199
|
+
healthy: agents.filter(a => a.health.status === 'healthy').length,
|
|
200
|
+
unhealthy: agents.filter(a => a.health.status === 'unhealthy').length,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Clean up stale agents (not seen for more than threshold)
|
|
205
|
+
*/
|
|
206
|
+
cleanupStaleAgents(thresholdMs = 300000) {
|
|
207
|
+
const now = new Date();
|
|
208
|
+
const staleAgents = [];
|
|
209
|
+
for (const [agentId, agent] of this.agents.entries()) {
|
|
210
|
+
const timeSinceLastSeen = now.getTime() - agent.lastSeen.getTime();
|
|
211
|
+
if (timeSinceLastSeen > thresholdMs) {
|
|
212
|
+
staleAgents.push(agentId);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
staleAgents.forEach(agentId => {
|
|
216
|
+
this.unregisterAgent(agentId);
|
|
217
|
+
});
|
|
218
|
+
if (staleAgents.length > 0) {
|
|
219
|
+
this.logger.info('Cleaned up stale agents', {
|
|
220
|
+
count: staleAgents.length,
|
|
221
|
+
agentIds: staleAgents,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
return staleAgents.length;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=agent-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-manager.js","sourceRoot":"","sources":["../../../src/services/agent-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAwB,MAAM,OAAO,CAAC;AAE7C,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC;;;GAGG;AACH,MAAM,OAAO,YAAY;IACf,MAAM,GAA2B,IAAI,GAAG,EAAE,CAAC;IAC3C,MAAM,GAAG,SAAS,EAAE,CAAC;IACrB,mBAAmB,GAA0C,IAAI,CAAC;IAClE,eAAe,CAAS;IAEhC,YAAY,kBAA0B,SAAS;QAC7C,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;IACzC,CAAC;IAED;;OAEG;IACI,aAAa,CAAC,GAAW,EAAE,SAAkB;QAClD,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC;QACzB,MAAM,SAAS,GAAc;YAC3B,EAAE,EAAE,OAAO;YACX,GAAG;YACH,MAAM,EAAE;gBACN,MAAM,EAAE,SAAS;gBACjB,SAAS,EAAE,IAAI,IAAI,EAAE;gBACrB,YAAY,EAAE,CAAC;gBACf,UAAU,EAAE,CAAC;gBACb,mBAAmB,EAAE,CAAC;aACvB;YACD,OAAO,EAAE;gBACP,QAAQ,EAAE,CAAC;gBACX,WAAW,EAAE,CAAC;gBACd,YAAY,EAAE,CAAC;gBACf,SAAS,EAAE,CAAC;gBACZ,mBAAmB,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