@agentxjs/runtime 1.8.1 → 2.0.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/LICENSE +21 -0
- package/README.md +159 -518
- package/dist/index.d.ts +4 -4
- package/dist/index.js +73 -37
- package/dist/index.js.map +16 -15
- package/package.json +15 -13
package/README.md
CHANGED
|
@@ -1,616 +1,257 @@
|
|
|
1
1
|
# @agentxjs/runtime
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Runtime for AI Agents - Agent execution, SystemBus, Environment, and complete lifecycle management
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
`@agentxjs/runtime` provides
|
|
8
|
-
|
|
9
|
-
- **SystemBus** - Event-driven communication backbone
|
|
10
|
-
- **Container** - Agent lifecycle management
|
|
11
|
-
- **Environment** (Receptor + Effector) - External world interface (Claude SDK)
|
|
12
|
-
- **Session** - Message persistence
|
|
13
|
-
- **Persistence** - Storage layer (SQLite, Memory)
|
|
7
|
+
The `@agentxjs/runtime` package provides complete runtime infrastructure for executing AI agents. It manages the full lifecycle from agent creation to destruction, handles communication through a centralized event bus, and integrates with the Claude SDK.
|
|
14
8
|
|
|
15
9
|
**Key Features:**
|
|
16
10
|
|
|
17
|
-
- **
|
|
18
|
-
- **
|
|
19
|
-
- **
|
|
20
|
-
- **
|
|
21
|
-
- **
|
|
11
|
+
- **Docker-Style Lifecycle**: Definition -> Image -> Agent -> Session pattern
|
|
12
|
+
- **Event-Driven Architecture**: Central SystemBus for all runtime communication
|
|
13
|
+
- **Image-First Model**: Persistent conversations with transient runtime agents
|
|
14
|
+
- **Environment Abstraction**: Pluggable receptor/effector pattern for LLM integration
|
|
15
|
+
- **Request/Response Pattern**: Command-based API with correlation support
|
|
22
16
|
|
|
23
17
|
## Installation
|
|
24
18
|
|
|
25
19
|
```bash
|
|
26
|
-
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
**Dependencies:**
|
|
30
|
-
|
|
31
|
-
- `@anthropic-ai/claude-agent-sdk` - Claude API client
|
|
32
|
-
- `better-sqlite3` - SQLite database (optional, for persistence)
|
|
33
|
-
- `unstorage` - Unified storage layer
|
|
34
|
-
|
|
35
|
-
---
|
|
36
|
-
|
|
37
|
-
## Quick Start
|
|
38
|
-
|
|
39
|
-
### Minimal Setup
|
|
40
|
-
|
|
41
|
-
```typescript
|
|
42
|
-
import { createRuntime } from "@agentxjs/runtime";
|
|
43
|
-
|
|
44
|
-
// Minimal - uses environment variable ANTHROPIC_API_KEY
|
|
45
|
-
const runtime = createRuntime();
|
|
46
|
-
|
|
47
|
-
// Subscribe to events
|
|
48
|
-
runtime.on("text_delta", (e) => {
|
|
49
|
-
console.log(e.data.text);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// Create container
|
|
53
|
-
const containerRes = await runtime.request("container_create_request", {
|
|
54
|
-
containerId: "my-container",
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
// Run agent
|
|
58
|
-
const agentRes = await runtime.request("agent_run_request", {
|
|
59
|
-
containerId: "my-container",
|
|
60
|
-
config: {
|
|
61
|
-
name: "Assistant",
|
|
62
|
-
systemPrompt: "You are a helpful assistant",
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// Send message
|
|
67
|
-
await runtime.request("agent_receive_request", {
|
|
68
|
-
agentId: agentRes.data.agentId,
|
|
69
|
-
content: "Hello!",
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
// Cleanup
|
|
73
|
-
await runtime.dispose();
|
|
20
|
+
bun add @agentxjs/runtime
|
|
74
21
|
```
|
|
75
22
|
|
|
76
|
-
### With Configuration
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
import { createRuntime, createPersistence } from "@agentxjs/runtime";
|
|
80
|
-
import { sqliteDriver } from "@agentxjs/persistence/sqlite";
|
|
81
|
-
|
|
82
|
-
const runtime = createRuntime({
|
|
83
|
-
// LLM configuration
|
|
84
|
-
llm: {
|
|
85
|
-
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
86
|
-
baseUrl: "https://api.anthropic.com",
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
// Persistence (using sqlite driver)
|
|
90
|
-
persistence: await createPersistence(sqliteDriver({ path: "./data.db" })),
|
|
91
|
-
|
|
92
|
-
// Logger
|
|
93
|
-
logger: {
|
|
94
|
-
level: "info",
|
|
95
|
-
enableTimestamp: true,
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
---
|
|
101
|
-
|
|
102
23
|
## Architecture
|
|
103
24
|
|
|
104
|
-
### System Overview
|
|
105
|
-
|
|
106
|
-
```text
|
|
107
|
-
┌────────────────────────────────────────────────────────────────┐
|
|
108
|
-
│ Runtime │
|
|
109
|
-
├────────────────────────────────────────────────────────────────┤
|
|
110
|
-
│ │
|
|
111
|
-
│ ┌─────────────────────────────────────────────────────────┐ │
|
|
112
|
-
│ │ SystemBus │ │
|
|
113
|
-
│ │ (Central event bus - all communication flows here) │ │
|
|
114
|
-
│ └──────────────────────┬──────────────────────────────────┘ │
|
|
115
|
-
│ │ │
|
|
116
|
-
│ ┌───────────────┼───────────────┐ │
|
|
117
|
-
│ │ │ │ │
|
|
118
|
-
│ ▼ ▼ ▼ │
|
|
119
|
-
│ ┌─────────────┐ ┌──────────┐ ┌────────────┐ │
|
|
120
|
-
│ │Environment │ │Container │ │Persistence │ │
|
|
121
|
-
│ │ │ │ │ │ │ │
|
|
122
|
-
│ │ Receptor │ │ Agent1 │ │ SQLite │ │
|
|
123
|
-
│ │ Effector │ │ Agent2 │ │ Images │ │
|
|
124
|
-
│ │ (Claude SDK)│ │ Agent3 │ │ Sessions │ │
|
|
125
|
-
│ └─────────────┘ └──────────┘ └────────────┘ │
|
|
126
|
-
│ │
|
|
127
|
-
└────────────────────────────────────────────────────────────────┘
|
|
128
25
|
```
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
|
133
|
-
|
|
|
134
|
-
|
|
|
135
|
-
|
|
|
136
|
-
|
|
|
137
|
-
|
|
|
138
|
-
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
26
|
+
+------------------------------------------------------------------+
|
|
27
|
+
| Runtime |
|
|
28
|
+
| |
|
|
29
|
+
| +----------------------+ +-----------------------------+ |
|
|
30
|
+
| | SystemBus | | CommandHandler | |
|
|
31
|
+
| | (Event Routing) |<-->| (Request/Response Logic) | |
|
|
32
|
+
| +----------------------+ +-----------------------------+ |
|
|
33
|
+
| | |
|
|
34
|
+
| v |
|
|
35
|
+
| +------------------------------------------------------------+ |
|
|
36
|
+
| | Container | |
|
|
37
|
+
| | +------------------------------------------------------+ | |
|
|
38
|
+
| | | RuntimeAgent | | |
|
|
39
|
+
| | | +----------------+ +----------------+ | | |
|
|
40
|
+
| | | | AgentEngine | | Session | | | |
|
|
41
|
+
| | | | (MealyMachine) | | (Storage) | | | |
|
|
42
|
+
| | | +----------------+ +----------------+ | | |
|
|
43
|
+
| | | | | |
|
|
44
|
+
| | | +------------------------------------------------+ | | |
|
|
45
|
+
| | | | Environment | | | |
|
|
46
|
+
| | | | +------------------+ +------------------+ | | | |
|
|
47
|
+
| | | | | Receptor | | Effector | | | | |
|
|
48
|
+
| | | | | (Claude -> Bus) | | (Bus -> Claude) | | | | |
|
|
49
|
+
| | | | +------------------+ +------------------+ | | | |
|
|
50
|
+
| | | +------------------------------------------------+ | | |
|
|
51
|
+
| | +------------------------------------------------------+ | |
|
|
52
|
+
| +------------------------------------------------------------+ |
|
|
53
|
+
+------------------------------------------------------------------+
|
|
156
54
|
```
|
|
157
55
|
|
|
158
|
-
|
|
56
|
+
## Quick Start
|
|
159
57
|
|
|
160
58
|
```typescript
|
|
161
|
-
|
|
162
|
-
runtime.on("text_delta", (event) => {
|
|
163
|
-
console.log(event.data.text);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
// Subscribe to multiple types
|
|
167
|
-
runtime.on(["message_start", "message_stop"], (event) => {
|
|
168
|
-
console.log(event.type);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
// Subscribe to all events
|
|
172
|
-
runtime.onAny((event) => {
|
|
173
|
-
console.log(event.type, event.data);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
// Unsubscribe
|
|
177
|
-
const unsubscribe = runtime.on("text_delta", handler);
|
|
178
|
-
unsubscribe();
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
### Available Commands
|
|
59
|
+
import { createRuntime, createPersistence, memoryDriver } from "@agentxjs/runtime";
|
|
182
60
|
|
|
183
|
-
|
|
61
|
+
// Create runtime with in-memory storage
|
|
62
|
+
const persistence = await createPersistence(memoryDriver());
|
|
184
63
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
64
|
+
const runtime = createRuntime({
|
|
65
|
+
persistence,
|
|
66
|
+
llmProvider: {
|
|
67
|
+
provide: () => ({
|
|
68
|
+
apiKey: process.env.ANTHROPIC_API_KEY!,
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
basePath: "./data",
|
|
189
72
|
});
|
|
190
73
|
|
|
191
|
-
//
|
|
192
|
-
await runtime.request("
|
|
193
|
-
containerId: "
|
|
74
|
+
// Create container for user
|
|
75
|
+
await runtime.request("container_create_request", {
|
|
76
|
+
containerId: "user-123",
|
|
194
77
|
});
|
|
195
|
-
```
|
|
196
78
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
// Run agent
|
|
201
|
-
const res = await runtime.request("agent_run_request", {
|
|
202
|
-
containerId: "my-container",
|
|
79
|
+
// Create conversation (image)
|
|
80
|
+
const imageRes = await runtime.request("image_create_request", {
|
|
81
|
+
containerId: "user-123",
|
|
203
82
|
config: {
|
|
204
|
-
name: "Assistant",
|
|
205
|
-
systemPrompt: "You are helpful",
|
|
83
|
+
name: "My Assistant",
|
|
84
|
+
systemPrompt: "You are a helpful assistant.",
|
|
206
85
|
},
|
|
207
86
|
});
|
|
208
87
|
|
|
209
|
-
//
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
content: "Hello!",
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
// Interrupt agent
|
|
216
|
-
await runtime.request("agent_interrupt_request", {
|
|
217
|
-
agentId: res.data.agentId,
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
// Destroy agent
|
|
221
|
-
await runtime.request("agent_destroy_request", {
|
|
222
|
-
agentId: res.data.agentId,
|
|
223
|
-
});
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
#### Session Commands
|
|
227
|
-
|
|
228
|
-
```typescript
|
|
229
|
-
// Create session
|
|
230
|
-
await runtime.request("session_create_request", {
|
|
231
|
-
sessionId: "session_123",
|
|
232
|
-
containerId: "my-container",
|
|
233
|
-
agentId: "agent_123",
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
// Get session
|
|
237
|
-
const res = await runtime.request("session_get_request", {
|
|
238
|
-
sessionId: "session_123",
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
// Delete session
|
|
242
|
-
await runtime.request("session_delete_request", {
|
|
243
|
-
sessionId: "session_123",
|
|
88
|
+
// Subscribe to text deltas
|
|
89
|
+
runtime.on("text_delta", (e) => {
|
|
90
|
+
process.stdout.write(e.data.text);
|
|
244
91
|
});
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
### Direct Command Emission
|
|
248
|
-
|
|
249
|
-
For advanced use cases:
|
|
250
92
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
agentId: "agent_123",
|
|
256
|
-
content: "Hello!",
|
|
93
|
+
// Send message (auto-runs image if needed)
|
|
94
|
+
await runtime.request("message_send_request", {
|
|
95
|
+
imageId: imageRes.data.record.imageId,
|
|
96
|
+
content: "Hello, how are you?",
|
|
257
97
|
});
|
|
258
98
|
|
|
259
|
-
//
|
|
260
|
-
runtime.
|
|
261
|
-
if (event.data.requestId === "req_123") {
|
|
262
|
-
console.log("Response:", event.data);
|
|
263
|
-
},
|
|
264
|
-
});
|
|
99
|
+
// Cleanup
|
|
100
|
+
await runtime.dispose();
|
|
265
101
|
```
|
|
266
102
|
|
|
267
|
-
|
|
103
|
+
## Core Components
|
|
268
104
|
|
|
269
|
-
|
|
105
|
+
### Runtime
|
|
270
106
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
### ClaudeEnvironment
|
|
274
|
-
|
|
275
|
-
Built-in implementation using `@anthropic-ai/claude-agent-sdk`:
|
|
107
|
+
Top-level API orchestrating all operations. Implements SystemBus interface and delegates to CommandHandler for request processing.
|
|
276
108
|
|
|
277
109
|
```typescript
|
|
278
|
-
import { createRuntime } from "@agentxjs/runtime";
|
|
279
|
-
|
|
280
110
|
const runtime = createRuntime({
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
},
|
|
111
|
+
persistence,
|
|
112
|
+
llmProvider: { provide: () => ({ apiKey: "..." }) },
|
|
113
|
+
basePath: "/path/to/.agentx",
|
|
114
|
+
defaultAgent: { name: "Assistant", systemPrompt: "..." },
|
|
285
115
|
});
|
|
286
116
|
```
|
|
287
117
|
|
|
288
|
-
###
|
|
289
|
-
|
|
290
|
-
```text
|
|
291
|
-
┌────────────────────────────────────────────────────────────┐
|
|
292
|
-
│ Environment │
|
|
293
|
-
├────────────────────────────────────────────────────────────┤
|
|
294
|
-
│ │
|
|
295
|
-
│ Receptor (Perceives external world → emits to SystemBus) │
|
|
296
|
-
│ │ │
|
|
297
|
-
│ │ Claude API Streaming Response │
|
|
298
|
-
│ ▼ │
|
|
299
|
-
│ ┌──────────────────────────────────────────────────────┐ │
|
|
300
|
-
│ │ Transforms Claude SDK events → DriveableEvent │ │
|
|
301
|
-
│ │ (message_delta, tool_use, etc.) │ │
|
|
302
|
-
│ └──────────────────────────────────────────────────────┘ │
|
|
303
|
-
│ │ │
|
|
304
|
-
│ │ emit to SystemBus │
|
|
305
|
-
│ ▼ │
|
|
306
|
-
│ SystemBus │
|
|
307
|
-
│ │ │
|
|
308
|
-
│ │ subscribe │
|
|
309
|
-
│ ▼ │
|
|
310
|
-
│ Effector (Subscribes to SystemBus → acts on external world)│
|
|
311
|
-
│ │ │
|
|
312
|
-
│ │ Executes tool calls │
|
|
313
|
-
│ ▼ │
|
|
314
|
-
│ External Tools (MCP, filesystem, etc.) │
|
|
315
|
-
│ │
|
|
316
|
-
└────────────────────────────────────────────────────────────┘
|
|
317
|
-
```
|
|
118
|
+
### Container
|
|
318
119
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
## Container - Agent Lifecycle
|
|
322
|
-
|
|
323
|
-
Container manages multiple agents:
|
|
324
|
-
|
|
325
|
-
```typescript
|
|
326
|
-
// Via command events (recommended)
|
|
327
|
-
const res = await runtime.request("container_create_request", {
|
|
328
|
-
containerId: "my-container",
|
|
329
|
-
});
|
|
120
|
+
Isolation boundary managing multiple agents. Tracks `imageId -> agentId` mapping for the Image-First model.
|
|
330
121
|
|
|
331
|
-
|
|
332
|
-
const agent1 = await runtime.request("agent_run_request", {
|
|
333
|
-
containerId: "my-container",
|
|
334
|
-
config: { name: "Agent1" },
|
|
335
|
-
});
|
|
122
|
+
### Agent (RuntimeAgent)
|
|
336
123
|
|
|
337
|
-
|
|
338
|
-
containerId: "my-container",
|
|
339
|
-
config: { name: "Agent2" },
|
|
340
|
-
});
|
|
124
|
+
Transient runtime entity processing user messages. Created by running an Image, destroyed when stopped.
|
|
341
125
|
|
|
342
|
-
|
|
343
|
-
await runtime.request("container_destroy_request", {
|
|
344
|
-
containerId: "my-container",
|
|
345
|
-
});
|
|
346
|
-
```
|
|
126
|
+
### Session (RuntimeSession)
|
|
347
127
|
|
|
348
|
-
|
|
128
|
+
Manages conversation history storage and retrieval via persistence layer.
|
|
349
129
|
|
|
350
|
-
|
|
130
|
+
### Image (RuntimeImage)
|
|
351
131
|
|
|
352
|
-
|
|
132
|
+
Persistent conversation entity. Users interact with Images (conversations), while Agents are transient instances.
|
|
353
133
|
|
|
354
|
-
###
|
|
134
|
+
### SystemBus
|
|
355
135
|
|
|
356
|
-
|
|
136
|
+
Central event bus using RxJS. Supports pub/sub, request/response, priority-based dispatch, and filtering.
|
|
357
137
|
|
|
358
138
|
```typescript
|
|
359
|
-
|
|
360
|
-
|
|
139
|
+
// Subscribe to events
|
|
140
|
+
runtime.on("text_delta", (e) => console.log(e.data.text));
|
|
141
|
+
runtime.on(["message_start", "message_stop"], (e) => console.log(e.type));
|
|
142
|
+
runtime.onAny((e) => console.log(e.type));
|
|
361
143
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
});
|
|
144
|
+
// Request/response pattern
|
|
145
|
+
const response = await runtime.request("image_get_request", { imageId: "..." });
|
|
365
146
|
```
|
|
366
147
|
|
|
367
|
-
|
|
148
|
+
### Environment
|
|
368
149
|
|
|
369
|
-
|
|
370
|
-
import { createRuntime, createPersistence, memoryDriver } from "@agentxjs/runtime";
|
|
150
|
+
Abstraction for LLM integration using Receptor/Effector pattern:
|
|
371
151
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
});
|
|
375
|
-
```
|
|
152
|
+
- **ClaudeReceptor**: Perceives Claude SDK responses, emits DriveableEvents to SystemBus
|
|
153
|
+
- **ClaudeEffector**: Subscribes to SystemBus events, sends to Claude SDK
|
|
376
154
|
|
|
377
|
-
|
|
155
|
+
## Command API
|
|
378
156
|
|
|
379
|
-
|
|
380
|
-
// Redis
|
|
381
|
-
import { redisDriver } from "@agentxjs/persistence/redis";
|
|
382
|
-
const persistence = await createPersistence(redisDriver({ url: "redis://localhost:6379" }));
|
|
383
|
-
|
|
384
|
-
// MongoDB
|
|
385
|
-
import { mongodbDriver } from "@agentxjs/persistence/mongodb";
|
|
386
|
-
const persistence = await createPersistence(mongodbDriver({ connectionString: "mongodb://..." }));
|
|
387
|
-
|
|
388
|
-
// MySQL
|
|
389
|
-
import { mysqlDriver } from "@agentxjs/persistence/mysql";
|
|
390
|
-
const persistence = await createPersistence(mysqlDriver({ host: "localhost", database: "agentx" }));
|
|
391
|
-
|
|
392
|
-
// PostgreSQL
|
|
393
|
-
import { postgresqlDriver } from "@agentxjs/persistence/postgresql";
|
|
394
|
-
const persistence = await createPersistence(
|
|
395
|
-
postgresqlDriver({ host: "localhost", database: "agentx" })
|
|
396
|
-
);
|
|
397
|
-
```
|
|
157
|
+
### Container Commands
|
|
398
158
|
|
|
399
|
-
|
|
159
|
+
| Command | Description |
|
|
160
|
+
| -------------------------- | ---------------------- |
|
|
161
|
+
| `container_create_request` | Create a new container |
|
|
162
|
+
| `container_get_request` | Get container by ID |
|
|
163
|
+
| `container_list_request` | List all containers |
|
|
400
164
|
|
|
401
|
-
|
|
165
|
+
### Image Commands
|
|
402
166
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
167
|
+
| Command | Description |
|
|
168
|
+
| ------------------------ | --------------------------------- |
|
|
169
|
+
| `image_create_request` | Create a new image (conversation) |
|
|
170
|
+
| `image_run_request` | Run an image (create agent) |
|
|
171
|
+
| `image_stop_request` | Stop an image (destroy agent) |
|
|
172
|
+
| `image_update_request` | Update image metadata |
|
|
173
|
+
| `image_list_request` | List all images |
|
|
174
|
+
| `image_get_request` | Get image by ID |
|
|
175
|
+
| `image_delete_request` | Delete an image |
|
|
176
|
+
| `image_messages_request` | Get messages for an image |
|
|
406
177
|
|
|
407
|
-
|
|
408
|
-
// Save agent image
|
|
409
|
-
const image = await runtime.request("image_snapshot_request", {
|
|
410
|
-
agentId: "agent_123",
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
// Resume from image
|
|
414
|
-
const agent = await runtime.request("image_resume_request", {
|
|
415
|
-
imageId: image.data.imageId,
|
|
416
|
-
});
|
|
417
|
-
```
|
|
178
|
+
### Agent Commands
|
|
418
179
|
|
|
419
|
-
|
|
180
|
+
| Command | Description |
|
|
181
|
+
| --------------------------- | --------------------------------- |
|
|
182
|
+
| `message_send_request` | Send message to agent |
|
|
183
|
+
| `agent_interrupt_request` | Interrupt agent operation |
|
|
184
|
+
| `agent_get_request` | Get agent by ID |
|
|
185
|
+
| `agent_list_request` | List agents in a container |
|
|
186
|
+
| `agent_destroy_request` | Destroy an agent |
|
|
187
|
+
| `agent_destroy_all_request` | Destroy all agents in a container |
|
|
420
188
|
|
|
421
|
-
##
|
|
189
|
+
## Event Types
|
|
422
190
|
|
|
423
|
-
###
|
|
191
|
+
### Stream Events (from Environment)
|
|
424
192
|
|
|
425
193
|
```typescript
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
async getItem(key) {
|
|
430
|
-
// Fetch from your backend
|
|
431
|
-
},
|
|
432
|
-
async setItem(key, value) {
|
|
433
|
-
// Save to your backend
|
|
434
|
-
},
|
|
435
|
-
async removeItem(key) {
|
|
436
|
-
// Delete from your backend
|
|
437
|
-
},
|
|
438
|
-
async getKeys(base) {
|
|
439
|
-
// List keys with prefix
|
|
440
|
-
},
|
|
441
|
-
async clear() {
|
|
442
|
-
// Clear all data
|
|
443
|
-
},
|
|
444
|
-
};
|
|
445
|
-
|
|
446
|
-
const runtime = createRuntime({
|
|
447
|
-
persistence: createPersistence({
|
|
448
|
-
driver: customDriver,
|
|
449
|
-
}),
|
|
450
|
-
});
|
|
194
|
+
{ type: "message_start", data: { message: { id, model } } }
|
|
195
|
+
{ type: "text_delta", data: { text: string } }
|
|
196
|
+
{ type: "message_stop", data: { stopReason: "end_turn" | "tool_use" | "max_tokens" } }
|
|
451
197
|
```
|
|
452
198
|
|
|
453
|
-
###
|
|
199
|
+
### Lifecycle Events
|
|
454
200
|
|
|
455
201
|
```typescript
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
(event) => {
|
|
460
|
-
console.log(event.data.text);
|
|
461
|
-
},
|
|
462
|
-
{
|
|
463
|
-
filter: (event) => event.context?.agentId === "agent_123",
|
|
464
|
-
}
|
|
465
|
-
);
|
|
466
|
-
|
|
467
|
-
// Priority execution
|
|
468
|
-
runtime.on("message_stop", handler, {
|
|
469
|
-
priority: 10, // Higher priority runs first
|
|
470
|
-
});
|
|
471
|
-
|
|
472
|
-
// One-time subscription
|
|
473
|
-
runtime.once("agent_created", (event) => {
|
|
474
|
-
console.log("Agent created:", event.data.agentId);
|
|
475
|
-
});
|
|
202
|
+
{ type: "container_created", data: { containerId, createdAt } }
|
|
203
|
+
{ type: "agent_registered", data: { containerId, agentId, registeredAt } }
|
|
204
|
+
{ type: "session_created", data: { sessionId, imageId, containerId, createdAt } }
|
|
476
205
|
```
|
|
477
206
|
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
## Testing
|
|
481
|
-
|
|
482
|
-
Runtime is designed for easy testing:
|
|
207
|
+
## Configuration
|
|
483
208
|
|
|
484
209
|
```typescript
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
describe("Runtime", () => {
|
|
489
|
-
it("creates and runs agent", async () => {
|
|
490
|
-
const runtime = createRuntime({
|
|
491
|
-
llm: {
|
|
492
|
-
apiKey: "test-key",
|
|
493
|
-
},
|
|
494
|
-
persistence: await createPersistence(memoryDriver()),
|
|
495
|
-
});
|
|
496
|
-
|
|
497
|
-
// Create container
|
|
498
|
-
const containerRes = await runtime.request("container_create_request", {
|
|
499
|
-
containerId: "test-container",
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
expect(containerRes.data.containerId).toBe("test-container");
|
|
503
|
-
|
|
504
|
-
// Run agent
|
|
505
|
-
const agentRes = await runtime.request("agent_run_request", {
|
|
506
|
-
containerId: "test-container",
|
|
507
|
-
config: { name: "TestAgent" },
|
|
508
|
-
});
|
|
509
|
-
|
|
510
|
-
expect(agentRes.data.name).toBe("TestAgent");
|
|
511
|
-
|
|
512
|
-
await runtime.dispose();
|
|
513
|
-
});
|
|
514
|
-
});
|
|
515
|
-
```
|
|
516
|
-
|
|
517
|
-
---
|
|
518
|
-
|
|
519
|
-
## Design Decisions
|
|
520
|
-
|
|
521
|
-
### Why Event-Driven?
|
|
522
|
-
|
|
523
|
-
Event-driven architecture enables:
|
|
524
|
-
|
|
525
|
-
1. **Decoupling** - Components communicate via events, not direct calls
|
|
526
|
-
2. **Extensibility** - Add new components without modifying existing ones
|
|
527
|
-
3. **Testability** - Mock events instead of entire components
|
|
528
|
-
4. **Observability** - All operations are visible as events
|
|
529
|
-
|
|
530
|
-
### Why SystemBus?
|
|
531
|
-
|
|
532
|
-
SystemBus provides:
|
|
533
|
-
|
|
534
|
-
1. **Single Source of Truth** - All communication flows through one point
|
|
535
|
-
2. **Type Safety** - Commands are fully typed
|
|
536
|
-
3. **Request/Response** - Async operations with correlation IDs
|
|
537
|
-
4. **Priority/Filtering** - Advanced subscription options
|
|
538
|
-
|
|
539
|
-
### Why Separate Environment?
|
|
540
|
-
|
|
541
|
-
Environment abstraction allows:
|
|
210
|
+
interface RuntimeConfig {
|
|
211
|
+
/** Persistence layer for data storage */
|
|
212
|
+
persistence: Persistence;
|
|
542
213
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
3. **Cross-Platform** - Different implementations for Node.js, Browser
|
|
546
|
-
4. **Clear Boundary** - External world vs. internal logic
|
|
214
|
+
/** LLM provider for AI model access */
|
|
215
|
+
llmProvider: LLMProvider<ClaudeLLMConfig>;
|
|
547
216
|
|
|
548
|
-
|
|
217
|
+
/** Base path for runtime data (containers, workdirs, etc.) */
|
|
218
|
+
basePath: string;
|
|
549
219
|
|
|
550
|
-
|
|
220
|
+
/** Optional environment factory for dependency injection */
|
|
221
|
+
environmentFactory?: EnvironmentFactory;
|
|
551
222
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
// LLM configuration
|
|
555
|
-
llm?: {
|
|
556
|
-
apiKey?: string; // Default: process.env.ANTHROPIC_API_KEY
|
|
557
|
-
baseUrl?: string; // Default: "https://api.anthropic.com"
|
|
558
|
-
};
|
|
559
|
-
|
|
560
|
-
// Persistence
|
|
561
|
-
persistence?: Persistence;
|
|
562
|
-
|
|
563
|
-
// Logger
|
|
564
|
-
logger?: {
|
|
565
|
-
level?: LogLevel;
|
|
566
|
-
enableTimestamp?: boolean;
|
|
567
|
-
enableColor?: boolean;
|
|
568
|
-
};
|
|
223
|
+
/** Default agent definition used when creating new images */
|
|
224
|
+
defaultAgent?: AgentDefinition;
|
|
569
225
|
}
|
|
570
226
|
```
|
|
571
227
|
|
|
572
|
-
---
|
|
573
|
-
|
|
574
228
|
## Environment Variables
|
|
575
229
|
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
ANTHROPIC_API_KEY
|
|
230
|
+
| Variable | Description | Default |
|
|
231
|
+
| -------------------- | -------------- | ----------------- |
|
|
232
|
+
| `ANTHROPIC_API_KEY` | Claude API key | Required |
|
|
233
|
+
| `ANTHROPIC_BASE_URL` | API endpoint | Anthropic default |
|
|
234
|
+
| `LOG_LEVEL` | Logging level | `info` |
|
|
579
235
|
|
|
580
|
-
|
|
581
|
-
ANTHROPIC_BASE_URL=https://api.anthropic.com # Custom endpoint
|
|
582
|
-
LOG_LEVEL=info # debug, info, warn, error
|
|
583
|
-
```
|
|
584
|
-
|
|
585
|
-
---
|
|
586
|
-
|
|
587
|
-
## Package Dependencies
|
|
588
|
-
|
|
589
|
-
```text
|
|
590
|
-
@agentxjs/types Type definitions
|
|
591
|
-
↑
|
|
592
|
-
@agentxjs/common Logger facade
|
|
593
|
-
↑
|
|
594
|
-
@agentxjs/persistence Storage layer (drivers as subpath exports)
|
|
595
|
-
↑
|
|
596
|
-
@agentxjs/agent AgentEngine
|
|
597
|
-
↑
|
|
598
|
-
@agentxjs/runtime This package (Runtime + Environment)
|
|
599
|
-
↑
|
|
600
|
-
agentxjs High-level unified API
|
|
601
|
-
```
|
|
236
|
+
## Dependencies
|
|
602
237
|
|
|
603
|
-
|
|
238
|
+
- `@agentxjs/agent` - Agent engine and event processing
|
|
239
|
+
- `@agentxjs/common` - Logging, ID generation
|
|
240
|
+
- `@agentxjs/persistence` - Storage layer
|
|
241
|
+
- `@agentxjs/types` - Type definitions
|
|
242
|
+
- `@anthropic-ai/claude-agent-sdk` - Claude SDK integration
|
|
243
|
+
- `rxjs` - Reactive event handling
|
|
604
244
|
|
|
605
245
|
## Related Packages
|
|
606
246
|
|
|
607
|
-
-
|
|
608
|
-
-
|
|
609
|
-
-
|
|
610
|
-
-
|
|
611
|
-
|
|
247
|
+
- [@agentxjs/agent](../agent) - Agent engine and event processing
|
|
248
|
+
- [@agentxjs/persistence](../persistence) - Storage layer
|
|
249
|
+
- [@agentxjs/types](../types) - Type definitions
|
|
250
|
+
- [agentxjs](../agentx) - Unified entry point
|
|
251
|
+
|
|
252
|
+
## Full Documentation
|
|
612
253
|
|
|
613
|
-
|
|
254
|
+
See [docs/packages/runtime.md](../../docs/packages/runtime.md) for complete documentation including advanced usage, custom environment factories, and integration examples.
|
|
614
255
|
|
|
615
256
|
## License
|
|
616
257
|
|