@agentxjs/core 1.9.1-dev
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/package.json +31 -0
- package/src/agent/AgentStateMachine.ts +151 -0
- package/src/agent/README.md +296 -0
- package/src/agent/__tests__/AgentStateMachine.test.ts +346 -0
- package/src/agent/__tests__/createAgent.test.ts +728 -0
- package/src/agent/__tests__/engine/internal/messageAssemblerProcessor.test.ts +567 -0
- package/src/agent/__tests__/engine/internal/stateEventProcessor.test.ts +315 -0
- package/src/agent/__tests__/engine/internal/turnTrackerProcessor.test.ts +340 -0
- package/src/agent/__tests__/engine/mealy/Mealy.test.ts +370 -0
- package/src/agent/__tests__/engine/mealy/Store.test.ts +123 -0
- package/src/agent/__tests__/engine/mealy/combinators.test.ts +322 -0
- package/src/agent/createAgent.ts +467 -0
- package/src/agent/engine/AgentProcessor.ts +106 -0
- package/src/agent/engine/MealyMachine.ts +184 -0
- package/src/agent/engine/internal/index.ts +35 -0
- package/src/agent/engine/internal/messageAssemblerProcessor.ts +550 -0
- package/src/agent/engine/internal/stateEventProcessor.ts +313 -0
- package/src/agent/engine/internal/turnTrackerProcessor.ts +239 -0
- package/src/agent/engine/mealy/Mealy.ts +308 -0
- package/src/agent/engine/mealy/Processor.ts +70 -0
- package/src/agent/engine/mealy/Sink.ts +56 -0
- package/src/agent/engine/mealy/Source.ts +51 -0
- package/src/agent/engine/mealy/Store.ts +98 -0
- package/src/agent/engine/mealy/combinators.ts +176 -0
- package/src/agent/engine/mealy/index.ts +45 -0
- package/src/agent/index.ts +106 -0
- package/src/agent/types/engine.ts +395 -0
- package/src/agent/types/event.ts +478 -0
- package/src/agent/types/index.ts +197 -0
- package/src/agent/types/message.ts +387 -0
- package/src/common/index.ts +8 -0
- package/src/common/logger/ConsoleLogger.ts +137 -0
- package/src/common/logger/LoggerFactoryImpl.ts +123 -0
- package/src/common/logger/index.ts +26 -0
- package/src/common/logger/types.ts +98 -0
- package/src/container/Container.ts +185 -0
- package/src/container/index.ts +44 -0
- package/src/container/types.ts +71 -0
- package/src/driver/index.ts +42 -0
- package/src/driver/types.ts +363 -0
- package/src/event/EventBus.ts +260 -0
- package/src/event/README.md +237 -0
- package/src/event/__tests__/EventBus.test.ts +251 -0
- package/src/event/index.ts +46 -0
- package/src/event/types/agent.ts +512 -0
- package/src/event/types/base.ts +241 -0
- package/src/event/types/bus.ts +429 -0
- package/src/event/types/command.ts +749 -0
- package/src/event/types/container.ts +471 -0
- package/src/event/types/driver.ts +452 -0
- package/src/event/types/index.ts +26 -0
- package/src/event/types/session.ts +314 -0
- package/src/image/Image.ts +203 -0
- package/src/image/index.ts +36 -0
- package/src/image/types.ts +77 -0
- package/src/index.ts +20 -0
- package/src/mq/OffsetGenerator.ts +48 -0
- package/src/mq/README.md +166 -0
- package/src/mq/__tests__/OffsetGenerator.test.ts +121 -0
- package/src/mq/index.ts +18 -0
- package/src/mq/types.ts +172 -0
- package/src/network/RpcClient.ts +455 -0
- package/src/network/index.ts +76 -0
- package/src/network/jsonrpc.ts +336 -0
- package/src/network/protocol.ts +90 -0
- package/src/network/types.ts +284 -0
- package/src/persistence/index.ts +27 -0
- package/src/persistence/types.ts +226 -0
- package/src/runtime/AgentXRuntime.ts +501 -0
- package/src/runtime/index.ts +56 -0
- package/src/runtime/types.ts +236 -0
- package/src/session/Session.ts +71 -0
- package/src/session/index.ts +25 -0
- package/src/session/types.ts +77 -0
- package/src/workspace/index.ts +27 -0
- package/src/workspace/types.ts +131 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base Event Types - SystemEvent, EventSource, EventIntent, EventCategory, EventContext
|
|
3
|
+
*
|
|
4
|
+
* Foundation types for the event system.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// ============================================================================
|
|
8
|
+
// SECTION 1: Legacy BusEvent Types (for backward compatibility)
|
|
9
|
+
// ============================================================================
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* BusEvent - Base event type for EventBus
|
|
13
|
+
*
|
|
14
|
+
* All events flowing through the EventBus must conform to this structure.
|
|
15
|
+
* This is a minimal event type that can be extended for specific use cases.
|
|
16
|
+
*
|
|
17
|
+
* @deprecated Use SystemEvent instead for full event system support
|
|
18
|
+
*/
|
|
19
|
+
export interface BusEvent<T extends string = string, D = unknown> {
|
|
20
|
+
/**
|
|
21
|
+
* Event type identifier (e.g., "text_delta", "assistant_message")
|
|
22
|
+
*/
|
|
23
|
+
readonly type: T;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Event timestamp (Unix milliseconds)
|
|
27
|
+
*/
|
|
28
|
+
readonly timestamp: number;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Event payload data
|
|
32
|
+
*/
|
|
33
|
+
readonly data: D;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Event source (optional, for routing/filtering)
|
|
37
|
+
*/
|
|
38
|
+
readonly source?: string;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Event category (optional, for classification)
|
|
42
|
+
*/
|
|
43
|
+
readonly category?: string;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Event intent (optional, for command pattern)
|
|
47
|
+
*/
|
|
48
|
+
readonly intent?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ============================================================================
|
|
52
|
+
// SECTION 2: Base Types (SystemEvent, EventSource, EventIntent, EventCategory)
|
|
53
|
+
// ============================================================================
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Event source - where the event originated
|
|
57
|
+
*/
|
|
58
|
+
export type EventSource =
|
|
59
|
+
| "driver" // LLM Driver (Claude API, etc.)
|
|
60
|
+
| "agent" // Agent internal
|
|
61
|
+
| "session" // Session operations
|
|
62
|
+
| "container" // Container operations
|
|
63
|
+
| "sandbox" // Sandbox resources (Workspace, MCP)
|
|
64
|
+
| "command"; // Command request/response (API operations)
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Event intent - what the event represents
|
|
68
|
+
*/
|
|
69
|
+
export type EventIntent =
|
|
70
|
+
| "request" // Request to perform action (may be forwarded or executed)
|
|
71
|
+
| "result" // Result of completed action
|
|
72
|
+
| "notification"; // State change notification (no action needed)
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Event category - fine-grained classification within source
|
|
76
|
+
*/
|
|
77
|
+
export type EventCategory =
|
|
78
|
+
// Environment categories
|
|
79
|
+
| "stream" // Streaming output from LLM
|
|
80
|
+
| "connection" // Network connection status
|
|
81
|
+
// Agent categories
|
|
82
|
+
| "state" // State transitions
|
|
83
|
+
| "message" // Complete messages
|
|
84
|
+
| "turn" // Conversation turns
|
|
85
|
+
| "error" // Errors
|
|
86
|
+
// Session categories
|
|
87
|
+
| "lifecycle" // Creation/destruction
|
|
88
|
+
| "persist" // Persistence operations
|
|
89
|
+
| "action" // User actions (resume, fork)
|
|
90
|
+
// Sandbox categories
|
|
91
|
+
| "workdir" // File operations
|
|
92
|
+
| "mcp" // MCP tool operations
|
|
93
|
+
// Command categories (API request/response)
|
|
94
|
+
| "request" // Request to perform action
|
|
95
|
+
| "response"; // Response with result
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* EventContext - Scope information attached to events
|
|
99
|
+
*/
|
|
100
|
+
export interface EventContext {
|
|
101
|
+
/**
|
|
102
|
+
* Container ID (isolation boundary)
|
|
103
|
+
*/
|
|
104
|
+
containerId?: string;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Image ID (persistent conversation identity)
|
|
108
|
+
*/
|
|
109
|
+
imageId?: string;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Agent ID (if event is agent-scoped)
|
|
113
|
+
*/
|
|
114
|
+
agentId?: string;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Session ID (if event is session-scoped)
|
|
118
|
+
*/
|
|
119
|
+
sessionId?: string;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Turn ID (for correlating events within a single turn)
|
|
123
|
+
* A turn = one user message + assistant response cycle
|
|
124
|
+
*/
|
|
125
|
+
turnId?: string;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Correlation ID (for request-response tracking)
|
|
129
|
+
*/
|
|
130
|
+
correlationId?: string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* SystemEvent - Base interface for ALL events in the system
|
|
135
|
+
*
|
|
136
|
+
* Every event has:
|
|
137
|
+
* - type: What happened (e.g., "text_delta", "session_saved")
|
|
138
|
+
* - timestamp: When it happened
|
|
139
|
+
* - data: Event payload
|
|
140
|
+
* - source: Where it came from
|
|
141
|
+
* - category: What kind of event
|
|
142
|
+
* - intent: What it means (notification/request/result)
|
|
143
|
+
* - context: Optional scope information
|
|
144
|
+
* - broadcastable: Whether to broadcast to external clients (default: true)
|
|
145
|
+
*/
|
|
146
|
+
export interface SystemEvent<
|
|
147
|
+
T extends string = string,
|
|
148
|
+
D = unknown,
|
|
149
|
+
S extends EventSource = EventSource,
|
|
150
|
+
C extends EventCategory = EventCategory,
|
|
151
|
+
I extends EventIntent = EventIntent,
|
|
152
|
+
> {
|
|
153
|
+
/**
|
|
154
|
+
* Event type identifier (e.g., "text_delta", "session_saved")
|
|
155
|
+
*/
|
|
156
|
+
readonly type: T;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Event timestamp (Unix milliseconds)
|
|
160
|
+
*/
|
|
161
|
+
readonly timestamp: number;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Event payload data
|
|
165
|
+
*/
|
|
166
|
+
readonly data: D;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Event source - where the event originated
|
|
170
|
+
*/
|
|
171
|
+
readonly source: S;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Event category - fine-grained classification
|
|
175
|
+
*/
|
|
176
|
+
readonly category: C;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Event intent - what the event represents
|
|
180
|
+
*/
|
|
181
|
+
readonly intent: I;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Event context - scope information (optional)
|
|
185
|
+
*/
|
|
186
|
+
readonly context?: EventContext;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Whether to broadcast this event to external clients (SSE/WebSocket)
|
|
190
|
+
*
|
|
191
|
+
* - true or undefined: Broadcast to all external clients
|
|
192
|
+
* - false: Only consumed internally, not broadcast
|
|
193
|
+
*
|
|
194
|
+
* Used for internal events like DriveableEvent that should be
|
|
195
|
+
* processed by the engine but not sent directly to clients.
|
|
196
|
+
* @default true
|
|
197
|
+
*/
|
|
198
|
+
readonly broadcastable?: boolean;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Type Guards for SystemEvent
|
|
202
|
+
/**
|
|
203
|
+
* Check if event is from a specific source
|
|
204
|
+
*/
|
|
205
|
+
export function isFromSource<S extends EventSource>(
|
|
206
|
+
event: SystemEvent,
|
|
207
|
+
source: S
|
|
208
|
+
): event is SystemEvent<string, unknown, S> {
|
|
209
|
+
return event.source === source;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Check if event has a specific intent
|
|
214
|
+
*/
|
|
215
|
+
export function hasIntent<I extends EventIntent>(
|
|
216
|
+
event: SystemEvent,
|
|
217
|
+
intent: I
|
|
218
|
+
): event is SystemEvent<string, unknown, EventSource, EventCategory, I> {
|
|
219
|
+
return event.intent === intent;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Check if event is a request
|
|
224
|
+
*/
|
|
225
|
+
export function isRequest(event: SystemEvent): boolean {
|
|
226
|
+
return event.intent === "request";
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Check if event is a result
|
|
231
|
+
*/
|
|
232
|
+
export function isResult(event: SystemEvent): boolean {
|
|
233
|
+
return event.intent === "result";
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Check if event is a notification
|
|
238
|
+
*/
|
|
239
|
+
export function isNotification(event: SystemEvent): boolean {
|
|
240
|
+
return event.intent === "notification";
|
|
241
|
+
}
|
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EventBus Interfaces - EventBus, EventProducer, EventConsumer
|
|
3
|
+
*
|
|
4
|
+
* Central event bus interfaces for ecosystem communication.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { BusEvent } from "./base";
|
|
8
|
+
import type {
|
|
9
|
+
CommandEventMap,
|
|
10
|
+
CommandRequestType,
|
|
11
|
+
ResponseEventFor,
|
|
12
|
+
RequestDataFor,
|
|
13
|
+
} from "./command";
|
|
14
|
+
|
|
15
|
+
// ============================================================================
|
|
16
|
+
// Types for Event Subscription
|
|
17
|
+
// ============================================================================
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Unsubscribe function type
|
|
21
|
+
*/
|
|
22
|
+
export type Unsubscribe = () => void;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Event handler function type
|
|
26
|
+
*/
|
|
27
|
+
export type BusEventHandler<E extends BusEvent = BusEvent> = (event: E) => void;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Subscription options for advanced event handling
|
|
31
|
+
*/
|
|
32
|
+
export interface SubscribeOptions<E extends BusEvent = BusEvent> {
|
|
33
|
+
/**
|
|
34
|
+
* Event filter - only events returning true will trigger the handler
|
|
35
|
+
* Useful for filtering by agentId, sessionId, etc.
|
|
36
|
+
*/
|
|
37
|
+
filter?: (event: E) => boolean;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Priority - higher numbers execute first (default: 0)
|
|
41
|
+
* Useful for ensuring certain handlers run before others
|
|
42
|
+
*/
|
|
43
|
+
priority?: number;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether to trigger only once then auto-unsubscribe
|
|
47
|
+
*/
|
|
48
|
+
once?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ============================================================================
|
|
52
|
+
// EventProducer Interface (Write-only view)
|
|
53
|
+
// ============================================================================
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* EventProducer interface - Write-only view
|
|
57
|
+
*
|
|
58
|
+
* Used by components that produce events:
|
|
59
|
+
* - Effector (emits DriveableEvents from Claude API)
|
|
60
|
+
* - Receptor (emits stream events)
|
|
61
|
+
* - Agent (emits state/message events)
|
|
62
|
+
*
|
|
63
|
+
* Producer can only emit events, cannot subscribe.
|
|
64
|
+
* This prevents accidental event loops and clarifies data flow.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* class Effector {
|
|
69
|
+
* constructor(private producer: EventProducer) {}
|
|
70
|
+
*
|
|
71
|
+
* async sendMessage(message: UserMessage) {
|
|
72
|
+
* // Can only emit, cannot subscribe
|
|
73
|
+
* this.producer.emit({
|
|
74
|
+
* type: "message_start",
|
|
75
|
+
* timestamp: Date.now(),
|
|
76
|
+
* data: { messageId: "msg-1", model: "claude" },
|
|
77
|
+
* });
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export interface EventProducer {
|
|
83
|
+
/**
|
|
84
|
+
* Emit a single event to the bus
|
|
85
|
+
*
|
|
86
|
+
* @param event - The event to emit
|
|
87
|
+
*/
|
|
88
|
+
emit(event: BusEvent): void;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Emit multiple events (batch operation)
|
|
92
|
+
*
|
|
93
|
+
* @param events - Array of events to emit
|
|
94
|
+
*/
|
|
95
|
+
emitBatch(events: BusEvent[]): void;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Emit a typed command event
|
|
99
|
+
*
|
|
100
|
+
* @param type - The command event type
|
|
101
|
+
* @param data - Event data (type-checked)
|
|
102
|
+
*/
|
|
103
|
+
emitCommand<T extends keyof CommandEventMap>(type: T, data: CommandEventMap[T]["data"]): void;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ============================================================================
|
|
107
|
+
// EventConsumer Interface (Read-only view)
|
|
108
|
+
// ============================================================================
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* EventConsumer interface - Read-only view
|
|
112
|
+
*
|
|
113
|
+
* Used by components that consume events:
|
|
114
|
+
* - BusDriver (subscribes to DriveableEvents)
|
|
115
|
+
* - UI components (subscribes to display events)
|
|
116
|
+
* - Presenters (subscribes to forward events)
|
|
117
|
+
*
|
|
118
|
+
* Consumer can only subscribe to events, cannot emit.
|
|
119
|
+
* This prevents accidental event emission and clarifies data flow.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* class BusDriver {
|
|
124
|
+
* constructor(private consumer: EventConsumer) {}
|
|
125
|
+
*
|
|
126
|
+
* async *receive(message: UserMessage) {
|
|
127
|
+
* // Can only subscribe, cannot emit
|
|
128
|
+
* this.consumer.on('message_start', (event) => {
|
|
129
|
+
* console.log('Received:', event.type);
|
|
130
|
+
* });
|
|
131
|
+
* }
|
|
132
|
+
* }
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export interface EventConsumer {
|
|
136
|
+
/**
|
|
137
|
+
* Subscribe to a specific event type
|
|
138
|
+
*
|
|
139
|
+
* @param type - The event type to listen for
|
|
140
|
+
* @param handler - Callback invoked when event is received
|
|
141
|
+
* @param options - Subscription options (filter, priority, once)
|
|
142
|
+
* @returns Unsubscribe function
|
|
143
|
+
*/
|
|
144
|
+
on<T extends string>(
|
|
145
|
+
type: T,
|
|
146
|
+
handler: BusEventHandler<BusEvent & { type: T }>,
|
|
147
|
+
options?: SubscribeOptions<BusEvent & { type: T }>
|
|
148
|
+
): Unsubscribe;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Subscribe to multiple event types
|
|
152
|
+
*
|
|
153
|
+
* @param types - Array of event types to listen for
|
|
154
|
+
* @param handler - Callback invoked when any matching event is received
|
|
155
|
+
* @param options - Subscription options
|
|
156
|
+
* @returns Unsubscribe function
|
|
157
|
+
*/
|
|
158
|
+
on(types: string[], handler: BusEventHandler, options?: SubscribeOptions): Unsubscribe;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Subscribe to all events
|
|
162
|
+
*
|
|
163
|
+
* @param handler - Callback invoked for every event
|
|
164
|
+
* @param options - Subscription options
|
|
165
|
+
* @returns Unsubscribe function
|
|
166
|
+
*/
|
|
167
|
+
onAny(handler: BusEventHandler, options?: SubscribeOptions): Unsubscribe;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Subscribe to an event type once (auto-unsubscribes after first trigger)
|
|
171
|
+
*
|
|
172
|
+
* @param type - Event type to subscribe to
|
|
173
|
+
* @param handler - Callback function
|
|
174
|
+
* @returns Unsubscribe function
|
|
175
|
+
*/
|
|
176
|
+
once<T extends string>(type: T, handler: BusEventHandler<BusEvent & { type: T }>): Unsubscribe;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Subscribe to a CommandEvent with full type safety
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* consumer.onCommand("container_create_request", (event) => {
|
|
184
|
+
* event.data.requestId; // string
|
|
185
|
+
* event.data.containerId; // string
|
|
186
|
+
* });
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* @param type - The command event type
|
|
190
|
+
* @param handler - Callback with fully typed event
|
|
191
|
+
* @returns Unsubscribe function
|
|
192
|
+
*/
|
|
193
|
+
onCommand<T extends keyof CommandEventMap>(
|
|
194
|
+
type: T,
|
|
195
|
+
handler: (event: CommandEventMap[T]) => void
|
|
196
|
+
): Unsubscribe;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Send a command request and wait for response
|
|
200
|
+
*
|
|
201
|
+
* Automatically generates requestId, emits request, waits for matching response.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```typescript
|
|
205
|
+
* const response = await consumer.request("container_create_request", {
|
|
206
|
+
* containerId: "my-container"
|
|
207
|
+
* });
|
|
208
|
+
* // response is ContainerCreateResponse
|
|
209
|
+
* console.log(response.data.containerId);
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @param type - The request event type
|
|
213
|
+
* @param data - Request data (without requestId)
|
|
214
|
+
* @param timeout - Timeout in milliseconds (default: 30000)
|
|
215
|
+
* @returns Promise of response event
|
|
216
|
+
*/
|
|
217
|
+
request<T extends CommandRequestType>(
|
|
218
|
+
type: T,
|
|
219
|
+
data: RequestDataFor<T>,
|
|
220
|
+
timeout?: number
|
|
221
|
+
): Promise<ResponseEventFor<T>>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ============================================================================
|
|
225
|
+
// EventBus Interface (Full access)
|
|
226
|
+
// ============================================================================
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* EventBus interface - Central event bus for ecosystem communication.
|
|
230
|
+
*
|
|
231
|
+
* Extends both Producer and Consumer interfaces for internal use.
|
|
232
|
+
* External components should use restricted views via asProducer/asConsumer.
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```typescript
|
|
236
|
+
* const bus = new EventBus();
|
|
237
|
+
*
|
|
238
|
+
* // Subscribe to specific event type
|
|
239
|
+
* bus.on('text_chunk', (e) => {
|
|
240
|
+
* console.log('Received text:', e.data.text);
|
|
241
|
+
* });
|
|
242
|
+
*
|
|
243
|
+
* // Subscribe with options
|
|
244
|
+
* bus.on('text_delta', handler, {
|
|
245
|
+
* filter: (e) => e.agentId === 'agent-1',
|
|
246
|
+
* priority: 10,
|
|
247
|
+
* once: true
|
|
248
|
+
* });
|
|
249
|
+
*
|
|
250
|
+
* // Subscribe to all events
|
|
251
|
+
* bus.onAny((e) => {
|
|
252
|
+
* console.log('Event:', e.type);
|
|
253
|
+
* });
|
|
254
|
+
*
|
|
255
|
+
* // Emit event
|
|
256
|
+
* bus.emit({ type: 'text_chunk', data: { text: 'Hello' } });
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
export interface EventBus {
|
|
260
|
+
/**
|
|
261
|
+
* Emit an event to the bus.
|
|
262
|
+
*
|
|
263
|
+
* All subscribed handlers will receive this event.
|
|
264
|
+
*
|
|
265
|
+
* @param event - The event to emit
|
|
266
|
+
*/
|
|
267
|
+
emit(event: BusEvent): void;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Emit multiple events (batch operation).
|
|
271
|
+
*
|
|
272
|
+
* @param events - Array of events to emit
|
|
273
|
+
*/
|
|
274
|
+
emitBatch(events: BusEvent[]): void;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Subscribe to a specific event type.
|
|
278
|
+
*
|
|
279
|
+
* @param type - The event type to listen for
|
|
280
|
+
* @param handler - Callback invoked when event is received
|
|
281
|
+
* @param options - Subscription options (filter, priority, once)
|
|
282
|
+
* @returns Unsubscribe function
|
|
283
|
+
*/
|
|
284
|
+
on<T extends string>(
|
|
285
|
+
type: T,
|
|
286
|
+
handler: BusEventHandler<BusEvent & { type: T }>,
|
|
287
|
+
options?: SubscribeOptions<BusEvent & { type: T }>
|
|
288
|
+
): Unsubscribe;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Subscribe to multiple event types.
|
|
292
|
+
*
|
|
293
|
+
* @param types - Array of event types to listen for
|
|
294
|
+
* @param handler - Callback invoked when any matching event is received
|
|
295
|
+
* @param options - Subscription options
|
|
296
|
+
* @returns Unsubscribe function
|
|
297
|
+
*/
|
|
298
|
+
on(types: string[], handler: BusEventHandler, options?: SubscribeOptions): Unsubscribe;
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Subscribe to all events.
|
|
302
|
+
*
|
|
303
|
+
* @param handler - Callback invoked for every event
|
|
304
|
+
* @param options - Subscription options
|
|
305
|
+
* @returns Unsubscribe function
|
|
306
|
+
*/
|
|
307
|
+
onAny(handler: BusEventHandler, options?: SubscribeOptions): Unsubscribe;
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Subscribe to an event type once (auto-unsubscribes after first trigger).
|
|
311
|
+
*
|
|
312
|
+
* @param type - Event type to subscribe to
|
|
313
|
+
* @param handler - Callback function
|
|
314
|
+
* @returns Unsubscribe function
|
|
315
|
+
*/
|
|
316
|
+
once<T extends string>(type: T, handler: BusEventHandler<BusEvent & { type: T }>): Unsubscribe;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Subscribe to a CommandEvent with full type safety.
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* ```typescript
|
|
323
|
+
* bus.onCommand("container_create_request", (event) => {
|
|
324
|
+
* event.data.requestId; // string
|
|
325
|
+
* event.data.containerId; // string
|
|
326
|
+
* });
|
|
327
|
+
* ```
|
|
328
|
+
*
|
|
329
|
+
* @param type - The command event type
|
|
330
|
+
* @param handler - Callback with fully typed event
|
|
331
|
+
* @returns Unsubscribe function
|
|
332
|
+
*/
|
|
333
|
+
onCommand<T extends keyof CommandEventMap>(
|
|
334
|
+
type: T,
|
|
335
|
+
handler: (event: CommandEventMap[T]) => void
|
|
336
|
+
): Unsubscribe;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Emit a CommandEvent with full type safety.
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```typescript
|
|
343
|
+
* bus.emitCommand("container_create_request", {
|
|
344
|
+
* requestId: "req_123",
|
|
345
|
+
* containerId: "my-container"
|
|
346
|
+
* });
|
|
347
|
+
* ```
|
|
348
|
+
*
|
|
349
|
+
* @param type - The command event type
|
|
350
|
+
* @param data - Event data (type-checked)
|
|
351
|
+
*/
|
|
352
|
+
emitCommand<T extends keyof CommandEventMap>(type: T, data: CommandEventMap[T]["data"]): void;
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Send a command request and wait for response.
|
|
356
|
+
*
|
|
357
|
+
* Automatically generates requestId, emits request, waits for matching response.
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```typescript
|
|
361
|
+
* const response = await bus.request("container_create_request", {
|
|
362
|
+
* containerId: "my-container"
|
|
363
|
+
* });
|
|
364
|
+
* // response is ContainerCreateResponse
|
|
365
|
+
* console.log(response.data.containerId);
|
|
366
|
+
* ```
|
|
367
|
+
*
|
|
368
|
+
* @param type - The request event type
|
|
369
|
+
* @param data - Request data (without requestId)
|
|
370
|
+
* @param timeout - Timeout in milliseconds (default: 30000)
|
|
371
|
+
* @returns Promise of response event
|
|
372
|
+
*/
|
|
373
|
+
request<T extends CommandRequestType>(
|
|
374
|
+
type: T,
|
|
375
|
+
data: RequestDataFor<T>,
|
|
376
|
+
timeout?: number
|
|
377
|
+
): Promise<ResponseEventFor<T>>;
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Get a read-only consumer view of the bus.
|
|
381
|
+
*
|
|
382
|
+
* Use this to safely expose event subscription to external code
|
|
383
|
+
* without allowing them to emit events.
|
|
384
|
+
*
|
|
385
|
+
* @example
|
|
386
|
+
* ```typescript
|
|
387
|
+
* class BusDriver {
|
|
388
|
+
* constructor(consumer: EventConsumer) {
|
|
389
|
+
* // Can only subscribe, cannot emit
|
|
390
|
+
* consumer.on('text_delta', ...);
|
|
391
|
+
* }
|
|
392
|
+
* }
|
|
393
|
+
* ```
|
|
394
|
+
*
|
|
395
|
+
* @returns EventConsumer - Read-only view
|
|
396
|
+
*/
|
|
397
|
+
asConsumer(): EventConsumer;
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Get a write-only producer view of the bus.
|
|
401
|
+
*
|
|
402
|
+
* Use this to give components the ability to emit events
|
|
403
|
+
* without exposing subscription capabilities.
|
|
404
|
+
*
|
|
405
|
+
* @example
|
|
406
|
+
* ```typescript
|
|
407
|
+
* class ClaudeReceptor {
|
|
408
|
+
* constructor(producer: EventProducer) {
|
|
409
|
+
* // Can only emit, cannot subscribe
|
|
410
|
+
* producer.emit({ type: 'text_delta', ... });
|
|
411
|
+
* }
|
|
412
|
+
* }
|
|
413
|
+
* ```
|
|
414
|
+
*
|
|
415
|
+
* @returns EventProducer - Write-only view
|
|
416
|
+
*/
|
|
417
|
+
asProducer(): EventProducer;
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Destroy the bus and clean up resources.
|
|
421
|
+
*
|
|
422
|
+
* All subscriptions will be terminated.
|
|
423
|
+
* After calling destroy():
|
|
424
|
+
* - All subscriptions are removed
|
|
425
|
+
* - New emissions are ignored
|
|
426
|
+
* - New subscriptions are no-ops
|
|
427
|
+
*/
|
|
428
|
+
destroy(): void;
|
|
429
|
+
}
|