@agentxjs/runtime 0.1.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/dist/index.js ADDED
@@ -0,0 +1,3176 @@
1
+ // src/internal/SystemBusImpl.ts
2
+ import { Subject } from "rxjs";
3
+ import { createLogger } from "@agentxjs/common";
4
+ var logger = createLogger("runtime/SystemBusImpl");
5
+ var SystemBusImpl = class {
6
+ subject = new Subject();
7
+ subscriptions = [];
8
+ nextId = 0;
9
+ isDestroyed = false;
10
+ // Cached restricted views
11
+ producerView = null;
12
+ consumerView = null;
13
+ constructor() {
14
+ this.subject.subscribe((event) => {
15
+ this.dispatch(event);
16
+ });
17
+ }
18
+ emit(event) {
19
+ if (this.isDestroyed) return;
20
+ this.subject.next(event);
21
+ }
22
+ emitBatch(events) {
23
+ for (const event of events) {
24
+ this.emit(event);
25
+ }
26
+ }
27
+ on(typeOrTypes, handler, options) {
28
+ if (this.isDestroyed) return () => {
29
+ };
30
+ const subscription = {
31
+ id: this.nextId++,
32
+ type: typeOrTypes,
33
+ handler,
34
+ filter: options?.filter,
35
+ priority: options?.priority ?? 0,
36
+ once: options?.once ?? false
37
+ };
38
+ this.subscriptions.push(subscription);
39
+ this.sortByPriority();
40
+ return () => this.removeSubscription(subscription.id);
41
+ }
42
+ onAny(handler, options) {
43
+ if (this.isDestroyed) return () => {
44
+ };
45
+ const subscription = {
46
+ id: this.nextId++,
47
+ type: "*",
48
+ handler,
49
+ filter: options?.filter,
50
+ priority: options?.priority ?? 0,
51
+ once: options?.once ?? false
52
+ };
53
+ this.subscriptions.push(subscription);
54
+ this.sortByPriority();
55
+ return () => this.removeSubscription(subscription.id);
56
+ }
57
+ once(type, handler) {
58
+ return this.on(type, handler, { once: true });
59
+ }
60
+ onCommand(type, handler) {
61
+ return this.on(type, handler);
62
+ }
63
+ emitCommand(type, data) {
64
+ this.emit({
65
+ type,
66
+ timestamp: Date.now(),
67
+ data,
68
+ source: "command",
69
+ category: type.endsWith("_response") ? "response" : "request",
70
+ intent: type.endsWith("_response") ? "result" : "request"
71
+ });
72
+ }
73
+ request(type, data, timeout = 3e4) {
74
+ return new Promise((resolve, reject) => {
75
+ const requestId = `req_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
76
+ const responseType = type.replace("_request", "_response");
77
+ const timer = setTimeout(() => {
78
+ unsubscribe();
79
+ reject(new Error(`Request timeout: ${type}`));
80
+ }, timeout);
81
+ const unsubscribe = this.onCommand(responseType, (event) => {
82
+ if (event.data.requestId === requestId) {
83
+ clearTimeout(timer);
84
+ unsubscribe();
85
+ resolve(event);
86
+ }
87
+ });
88
+ this.emitCommand(type, { ...data, requestId });
89
+ });
90
+ }
91
+ destroy() {
92
+ if (this.isDestroyed) return;
93
+ this.isDestroyed = true;
94
+ this.subscriptions = [];
95
+ this.subject.complete();
96
+ }
97
+ dispatch(event) {
98
+ const toRemove = [];
99
+ for (const sub of this.subscriptions) {
100
+ if (!this.matchesType(sub.type, event.type)) continue;
101
+ if (sub.filter && !sub.filter(event)) continue;
102
+ try {
103
+ sub.handler(event);
104
+ } catch (err) {
105
+ logger.error("Event handler error", {
106
+ eventType: event.type,
107
+ subscriptionType: sub.type,
108
+ error: err instanceof Error ? err.message : String(err),
109
+ stack: err instanceof Error ? err.stack : void 0
110
+ });
111
+ }
112
+ if (sub.once) {
113
+ toRemove.push(sub.id);
114
+ }
115
+ }
116
+ for (const id of toRemove) {
117
+ this.removeSubscription(id);
118
+ }
119
+ }
120
+ matchesType(subscriptionType, eventType) {
121
+ if (subscriptionType === "*") return true;
122
+ if (Array.isArray(subscriptionType)) return subscriptionType.includes(eventType);
123
+ return subscriptionType === eventType;
124
+ }
125
+ sortByPriority() {
126
+ this.subscriptions.sort((a, b) => b.priority - a.priority);
127
+ }
128
+ removeSubscription(id) {
129
+ this.subscriptions = this.subscriptions.filter((s) => s.id !== id);
130
+ }
131
+ /**
132
+ * Get a read-only consumer view (only subscribe methods)
133
+ */
134
+ asConsumer() {
135
+ if (!this.consumerView) {
136
+ this.consumerView = {
137
+ on: this.on.bind(this),
138
+ onAny: this.onAny.bind(this),
139
+ once: this.once.bind(this),
140
+ onCommand: this.onCommand.bind(this),
141
+ request: this.request.bind(this)
142
+ };
143
+ }
144
+ return this.consumerView;
145
+ }
146
+ /**
147
+ * Get a write-only producer view (only emit methods)
148
+ */
149
+ asProducer() {
150
+ if (!this.producerView) {
151
+ this.producerView = {
152
+ emit: this.emit.bind(this),
153
+ emitBatch: this.emitBatch.bind(this),
154
+ emitCommand: this.emitCommand.bind(this)
155
+ };
156
+ }
157
+ return this.producerView;
158
+ }
159
+ };
160
+
161
+ // src/internal/BusDriver.ts
162
+ import { createLogger as createLogger2 } from "@agentxjs/common";
163
+ var logger2 = createLogger2("runtime/BusDriver");
164
+ var BusDriver = class {
165
+ name = "BusDriver";
166
+ description = "Driver that listens to SystemBus for DriveableEvents";
167
+ config;
168
+ unsubscribe;
169
+ constructor(consumer, config) {
170
+ this.config = config;
171
+ logger2.debug("BusDriver created, subscribing to bus", {
172
+ agentId: config.agentId
173
+ });
174
+ this.unsubscribe = consumer.onAny(((event) => {
175
+ this.handleEvent(event);
176
+ }));
177
+ }
178
+ /**
179
+ * Handle incoming event from bus
180
+ */
181
+ handleEvent(event) {
182
+ if (!this.isDriveableEventForThisAgent(event)) {
183
+ return;
184
+ }
185
+ const driveableEvent = event;
186
+ logger2.debug("BusDriver received DriveableEvent", {
187
+ type: driveableEvent.type,
188
+ agentId: this.config.agentId,
189
+ requestId: driveableEvent.requestId
190
+ });
191
+ const streamEvent = this.toStreamEvent(driveableEvent);
192
+ this.config.onStreamEvent(streamEvent);
193
+ if (driveableEvent.type === "message_stop") {
194
+ this.config.onStreamComplete?.("message_stop");
195
+ } else if (driveableEvent.type === "interrupted") {
196
+ this.config.onStreamComplete?.("interrupted");
197
+ }
198
+ }
199
+ /**
200
+ * Check if event is a DriveableEvent for this agent
201
+ *
202
+ * Must check:
203
+ * 1. source === "environment" (from Claude)
204
+ * 2. context.agentId === this.config.agentId (for this agent)
205
+ */
206
+ isDriveableEventForThisAgent(event) {
207
+ const driveableTypes = [
208
+ "message_start",
209
+ "message_delta",
210
+ "message_stop",
211
+ "text_content_block_start",
212
+ "text_delta",
213
+ "text_content_block_stop",
214
+ "tool_use_content_block_start",
215
+ "input_json_delta",
216
+ "tool_use_content_block_stop",
217
+ "tool_call",
218
+ "tool_result",
219
+ "interrupted",
220
+ "error_received"
221
+ ];
222
+ if (event === null || typeof event !== "object" || !("type" in event) || typeof event.type !== "string") {
223
+ return false;
224
+ }
225
+ const e = event;
226
+ if (e.source !== "environment") {
227
+ return false;
228
+ }
229
+ if (!driveableTypes.includes(e.type)) {
230
+ return false;
231
+ }
232
+ if (e.context?.agentId !== this.config.agentId) {
233
+ return false;
234
+ }
235
+ return true;
236
+ }
237
+ /**
238
+ * Convert DriveableEvent to StreamEvent
239
+ */
240
+ toStreamEvent(event) {
241
+ const { type, timestamp, data } = event;
242
+ switch (type) {
243
+ case "message_start": {
244
+ const d = data;
245
+ return {
246
+ type: "message_start",
247
+ timestamp,
248
+ data: {
249
+ messageId: d.message?.id ?? "",
250
+ model: d.message?.model ?? ""
251
+ }
252
+ };
253
+ }
254
+ case "message_stop": {
255
+ const d = data;
256
+ return {
257
+ type: "message_stop",
258
+ timestamp,
259
+ data: {
260
+ stopReason: d.stopReason
261
+ }
262
+ };
263
+ }
264
+ case "text_delta": {
265
+ const d = data;
266
+ return {
267
+ type: "text_delta",
268
+ timestamp,
269
+ data: { text: d.text }
270
+ };
271
+ }
272
+ case "tool_use_content_block_start": {
273
+ const d = data;
274
+ return {
275
+ type: "tool_use_start",
276
+ timestamp,
277
+ data: {
278
+ toolCallId: d.toolCallId ?? d.id ?? "",
279
+ toolName: d.toolName ?? d.name ?? ""
280
+ }
281
+ };
282
+ }
283
+ case "input_json_delta": {
284
+ const d = data;
285
+ return {
286
+ type: "input_json_delta",
287
+ timestamp,
288
+ data: { partialJson: d.partialJson }
289
+ };
290
+ }
291
+ case "tool_use_content_block_stop": {
292
+ const d = data;
293
+ return {
294
+ type: "tool_use_stop",
295
+ timestamp,
296
+ data: {
297
+ toolCallId: d.toolCallId ?? d.id ?? "",
298
+ toolName: d.toolName ?? d.name ?? "",
299
+ input: d.input ?? {}
300
+ }
301
+ };
302
+ }
303
+ case "tool_result": {
304
+ const d = data;
305
+ return {
306
+ type: "tool_result",
307
+ timestamp,
308
+ data: {
309
+ toolCallId: d.toolCallId ?? d.toolUseId ?? "",
310
+ result: d.result,
311
+ isError: d.isError
312
+ }
313
+ };
314
+ }
315
+ case "interrupted": {
316
+ return {
317
+ type: "message_stop",
318
+ timestamp,
319
+ data: { stopReason: "end_turn" }
320
+ // Use valid StopReason
321
+ };
322
+ }
323
+ case "error_received": {
324
+ const d = data;
325
+ return {
326
+ type: "error_received",
327
+ timestamp,
328
+ data: {
329
+ message: d.message,
330
+ errorCode: d.errorCode
331
+ }
332
+ };
333
+ }
334
+ default:
335
+ return { type, timestamp, data };
336
+ }
337
+ }
338
+ /**
339
+ * Dispose and stop listening
340
+ */
341
+ dispose() {
342
+ logger2.debug("BusDriver disposing", { agentId: this.config.agentId });
343
+ this.unsubscribe();
344
+ }
345
+ };
346
+
347
+ // src/internal/AgentInteractor.ts
348
+ import { createLogger as createLogger3 } from "@agentxjs/common";
349
+ var logger3 = createLogger3("runtime/AgentInteractor");
350
+ var AgentInteractor = class {
351
+ producer;
352
+ session;
353
+ context;
354
+ constructor(producer, session, context) {
355
+ this.producer = producer;
356
+ this.session = session;
357
+ this.context = context;
358
+ logger3.debug("AgentInteractor created", { agentId: context.agentId });
359
+ }
360
+ /**
361
+ * Receive user message
362
+ *
363
+ * @param content - Message content
364
+ * @param requestId - Request ID for correlation
365
+ */
366
+ async receive(content, requestId) {
367
+ logger3.debug("AgentInteractor.receive", {
368
+ requestId,
369
+ agentId: this.context.agentId,
370
+ contentPreview: content.substring(0, 50)
371
+ });
372
+ const userMessage = {
373
+ id: `msg_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`,
374
+ role: "user",
375
+ subtype: "user",
376
+ content,
377
+ timestamp: Date.now()
378
+ };
379
+ await this.session.addMessage(userMessage);
380
+ logger3.debug("UserMessage persisted", {
381
+ messageId: userMessage.id,
382
+ requestId
383
+ });
384
+ const eventContext = {
385
+ agentId: this.context.agentId,
386
+ imageId: this.context.imageId,
387
+ containerId: this.context.containerId,
388
+ sessionId: this.context.sessionId
389
+ };
390
+ this.producer.emit({
391
+ type: "user_message",
392
+ timestamp: Date.now(),
393
+ data: userMessage,
394
+ source: "agent",
395
+ category: "message",
396
+ intent: "request",
397
+ requestId,
398
+ context: eventContext,
399
+ broadcastable: false
400
+ // Internal event for ClaudeEffector
401
+ });
402
+ logger3.info("user_message event emitted to bus", {
403
+ messageId: userMessage.id,
404
+ requestId,
405
+ agentId: this.context.agentId,
406
+ eventType: "user_message"
407
+ });
408
+ return userMessage;
409
+ }
410
+ /**
411
+ * Interrupt current operation
412
+ *
413
+ * @param requestId - Optional request ID for correlation
414
+ */
415
+ interrupt(requestId) {
416
+ logger3.debug("AgentInteractor.interrupt", {
417
+ requestId,
418
+ agentId: this.context.agentId
419
+ });
420
+ const eventContext = {
421
+ agentId: this.context.agentId,
422
+ imageId: this.context.imageId,
423
+ containerId: this.context.containerId,
424
+ sessionId: this.context.sessionId
425
+ };
426
+ this.producer.emit({
427
+ type: "interrupt",
428
+ timestamp: Date.now(),
429
+ data: { agentId: this.context.agentId },
430
+ source: "agent",
431
+ category: "action",
432
+ intent: "request",
433
+ requestId,
434
+ context: eventContext,
435
+ broadcastable: false
436
+ });
437
+ }
438
+ };
439
+
440
+ // src/internal/RuntimeAgent.ts
441
+ import { createAgent } from "@agentxjs/agent";
442
+ import { createLogger as createLogger6 } from "@agentxjs/common";
443
+
444
+ // src/environment/ClaudeReceptor.ts
445
+ import { createLogger as createLogger4 } from "@agentxjs/common";
446
+ var logger4 = createLogger4("ecosystem/ClaudeReceptor");
447
+ var ClaudeReceptor = class {
448
+ producer = null;
449
+ currentMeta = null;
450
+ /** Context for tracking content block state */
451
+ blockContext = {
452
+ currentBlockType: null,
453
+ currentBlockIndex: 0,
454
+ currentToolId: null,
455
+ currentToolName: null,
456
+ lastStopReason: null,
457
+ lastStopSequence: null
458
+ };
459
+ /**
460
+ * Connect to SystemBus producer to emit events
461
+ */
462
+ connect(producer) {
463
+ this.producer = producer;
464
+ logger4.debug("ClaudeReceptor connected to SystemBusProducer");
465
+ }
466
+ /**
467
+ * Feed SDK message to receptor with correlation metadata
468
+ * @param sdkMsg - SDK message from Claude
469
+ * @param meta - Request metadata for event correlation
470
+ */
471
+ feed(sdkMsg, meta) {
472
+ this.currentMeta = meta;
473
+ this.processStreamEvent(sdkMsg);
474
+ }
475
+ /**
476
+ * Emit interrupted event
477
+ */
478
+ emitInterrupted(reason, meta) {
479
+ const eventMeta = meta || this.currentMeta;
480
+ this.emitToBus({
481
+ type: "interrupted",
482
+ timestamp: Date.now(),
483
+ source: "environment",
484
+ category: "stream",
485
+ intent: "notification",
486
+ broadcastable: false,
487
+ requestId: eventMeta?.requestId,
488
+ context: eventMeta?.context,
489
+ data: { reason }
490
+ });
491
+ }
492
+ /**
493
+ * Emit error_received event
494
+ *
495
+ * Used when an error is received from the environment (e.g., Claude API error).
496
+ * This drives the MealyMachine to produce error_occurred + error_message events.
497
+ */
498
+ emitError(message, errorCode, meta) {
499
+ const eventMeta = meta || this.currentMeta;
500
+ this.emitToBus({
501
+ type: "error_received",
502
+ timestamp: Date.now(),
503
+ source: "environment",
504
+ category: "stream",
505
+ intent: "notification",
506
+ broadcastable: false,
507
+ requestId: eventMeta?.requestId,
508
+ context: eventMeta?.context,
509
+ data: { message, errorCode }
510
+ });
511
+ }
512
+ /**
513
+ * Feed SDK user message (contains tool_result) to receptor
514
+ * @param sdkMsg - SDK user message from Claude
515
+ * @param meta - Request metadata for event correlation
516
+ */
517
+ feedUserMessage(sdkMsg, meta) {
518
+ this.currentMeta = meta;
519
+ const { requestId, context } = meta;
520
+ if (!sdkMsg.message || !Array.isArray(sdkMsg.message.content)) {
521
+ return;
522
+ }
523
+ for (const block of sdkMsg.message.content) {
524
+ if (block && typeof block === "object" && "type" in block && block.type === "tool_result") {
525
+ const toolResultBlock = block;
526
+ this.emitToBus({
527
+ type: "tool_result",
528
+ timestamp: Date.now(),
529
+ source: "environment",
530
+ category: "stream",
531
+ intent: "notification",
532
+ broadcastable: false,
533
+ requestId,
534
+ context,
535
+ data: {
536
+ toolUseId: toolResultBlock.tool_use_id,
537
+ result: toolResultBlock.content,
538
+ isError: toolResultBlock.is_error || false
539
+ }
540
+ });
541
+ }
542
+ }
543
+ }
544
+ /**
545
+ * Process stream_event from SDK and emit corresponding DriveableEvent
546
+ *
547
+ * Uses currentMeta for requestId and context correlation.
548
+ */
549
+ processStreamEvent(sdkMsg) {
550
+ const event = sdkMsg.event;
551
+ const { requestId, context } = this.currentMeta || {};
552
+ switch (event.type) {
553
+ case "message_start":
554
+ this.blockContext = {
555
+ currentBlockType: null,
556
+ currentBlockIndex: 0,
557
+ currentToolId: null,
558
+ currentToolName: null,
559
+ lastStopReason: null,
560
+ lastStopSequence: null
561
+ };
562
+ this.emitToBus({
563
+ type: "message_start",
564
+ timestamp: Date.now(),
565
+ source: "environment",
566
+ category: "stream",
567
+ intent: "notification",
568
+ broadcastable: false,
569
+ requestId,
570
+ context,
571
+ data: {
572
+ message: {
573
+ id: event.message.id,
574
+ model: event.message.model
575
+ }
576
+ }
577
+ });
578
+ break;
579
+ case "content_block_start": {
580
+ const contentBlock = event.content_block;
581
+ this.blockContext.currentBlockIndex = event.index;
582
+ logger4.debug("content_block_start received", { contentBlock, index: event.index });
583
+ if (contentBlock.type === "text") {
584
+ this.blockContext.currentBlockType = "text";
585
+ this.emitToBus({
586
+ type: "text_content_block_start",
587
+ timestamp: Date.now(),
588
+ source: "environment",
589
+ category: "stream",
590
+ intent: "notification",
591
+ broadcastable: false,
592
+ index: event.index,
593
+ requestId,
594
+ context,
595
+ data: {}
596
+ });
597
+ } else if (contentBlock.type === "tool_use") {
598
+ this.blockContext.currentBlockType = "tool_use";
599
+ this.blockContext.currentToolId = contentBlock.id || null;
600
+ this.blockContext.currentToolName = contentBlock.name || null;
601
+ this.emitToBus({
602
+ type: "tool_use_content_block_start",
603
+ timestamp: Date.now(),
604
+ source: "environment",
605
+ category: "stream",
606
+ intent: "notification",
607
+ broadcastable: false,
608
+ index: event.index,
609
+ requestId,
610
+ context,
611
+ data: {
612
+ id: contentBlock.id || "",
613
+ name: contentBlock.name || ""
614
+ }
615
+ });
616
+ }
617
+ break;
618
+ }
619
+ case "content_block_delta": {
620
+ const delta = event.delta;
621
+ if (delta.type === "text_delta") {
622
+ this.emitToBus({
623
+ type: "text_delta",
624
+ timestamp: Date.now(),
625
+ source: "environment",
626
+ category: "stream",
627
+ intent: "notification",
628
+ broadcastable: false,
629
+ requestId,
630
+ context,
631
+ data: { text: delta.text || "" }
632
+ });
633
+ } else if (delta.type === "input_json_delta") {
634
+ this.emitToBus({
635
+ type: "input_json_delta",
636
+ timestamp: Date.now(),
637
+ source: "environment",
638
+ category: "stream",
639
+ intent: "notification",
640
+ broadcastable: false,
641
+ index: this.blockContext.currentBlockIndex,
642
+ requestId,
643
+ context,
644
+ data: { partialJson: delta.partial_json || "" }
645
+ });
646
+ }
647
+ break;
648
+ }
649
+ case "content_block_stop":
650
+ if (this.blockContext.currentBlockType === "tool_use" && this.blockContext.currentToolId) {
651
+ this.emitToBus({
652
+ type: "tool_use_content_block_stop",
653
+ timestamp: Date.now(),
654
+ source: "environment",
655
+ category: "stream",
656
+ intent: "notification",
657
+ broadcastable: false,
658
+ index: this.blockContext.currentBlockIndex,
659
+ requestId,
660
+ context,
661
+ data: {}
662
+ });
663
+ } else {
664
+ this.emitToBus({
665
+ type: "text_content_block_stop",
666
+ timestamp: Date.now(),
667
+ source: "environment",
668
+ category: "stream",
669
+ intent: "notification",
670
+ broadcastable: false,
671
+ index: this.blockContext.currentBlockIndex,
672
+ requestId,
673
+ context,
674
+ data: {}
675
+ });
676
+ }
677
+ this.blockContext.currentBlockType = null;
678
+ this.blockContext.currentToolId = null;
679
+ this.blockContext.currentToolName = null;
680
+ break;
681
+ case "message_delta": {
682
+ const msgDelta = event.delta;
683
+ if (msgDelta.stop_reason) {
684
+ this.blockContext.lastStopReason = msgDelta.stop_reason;
685
+ this.blockContext.lastStopSequence = msgDelta.stop_sequence || null;
686
+ }
687
+ break;
688
+ }
689
+ case "message_stop":
690
+ this.emitToBus({
691
+ type: "message_stop",
692
+ timestamp: Date.now(),
693
+ source: "environment",
694
+ category: "stream",
695
+ intent: "notification",
696
+ broadcastable: false,
697
+ requestId,
698
+ context,
699
+ data: {
700
+ stopReason: this.blockContext.lastStopReason || "end_turn",
701
+ stopSequence: this.blockContext.lastStopSequence || void 0
702
+ }
703
+ });
704
+ this.blockContext.lastStopReason = null;
705
+ this.blockContext.lastStopSequence = null;
706
+ break;
707
+ }
708
+ }
709
+ emitToBus(event) {
710
+ if (this.producer) {
711
+ this.producer.emit(event);
712
+ }
713
+ }
714
+ };
715
+
716
+ // src/environment/ClaudeEffector.ts
717
+ import { query } from "@anthropic-ai/claude-agent-sdk";
718
+ import { Subject as Subject2 } from "rxjs";
719
+ import { createLogger as createLogger5 } from "@agentxjs/common";
720
+
721
+ // src/environment/buildOptions.ts
722
+ function buildOptions(context, abortController) {
723
+ const options = {
724
+ abortController,
725
+ includePartialMessages: true
726
+ };
727
+ if (context.cwd) {
728
+ options.cwd = context.cwd;
729
+ }
730
+ const env = {
731
+ ...process.env
732
+ };
733
+ if (context.baseUrl) {
734
+ env.ANTHROPIC_BASE_URL = context.baseUrl;
735
+ }
736
+ if (context.apiKey) {
737
+ env.ANTHROPIC_API_KEY = context.apiKey;
738
+ }
739
+ options.env = env;
740
+ options.executable = process.execPath;
741
+ if (context.model) options.model = context.model;
742
+ if (context.systemPrompt) options.systemPrompt = context.systemPrompt;
743
+ if (context.maxTurns) options.maxTurns = context.maxTurns;
744
+ if (context.maxThinkingTokens) options.maxThinkingTokens = context.maxThinkingTokens;
745
+ if (context.resume) options.resume = context.resume;
746
+ if (context.permissionMode) {
747
+ options.permissionMode = context.permissionMode;
748
+ } else {
749
+ options.permissionMode = "bypassPermissions";
750
+ }
751
+ return options;
752
+ }
753
+
754
+ // src/environment/helpers.ts
755
+ function buildPrompt(message) {
756
+ if (typeof message.content === "string") {
757
+ return message.content;
758
+ }
759
+ if (Array.isArray(message.content)) {
760
+ return message.content.filter((part) => part.type === "text").map((part) => part.text ?? "").join("\n");
761
+ }
762
+ return "";
763
+ }
764
+ function buildSDKUserMessage(message, sessionId) {
765
+ return {
766
+ type: "user",
767
+ message: { role: "user", content: buildPrompt(message) },
768
+ parent_tool_use_id: null,
769
+ session_id: sessionId
770
+ };
771
+ }
772
+
773
+ // src/environment/observableToAsyncIterable.ts
774
+ async function* observableToAsyncIterable(observable) {
775
+ const queue = [];
776
+ let resolve = null;
777
+ let reject = null;
778
+ let done = false;
779
+ let error = null;
780
+ const subscription = observable.subscribe({
781
+ next: (value) => {
782
+ if (resolve) {
783
+ resolve({ value, done: false });
784
+ resolve = null;
785
+ reject = null;
786
+ } else {
787
+ queue.push(value);
788
+ }
789
+ },
790
+ error: (err) => {
791
+ error = err instanceof Error ? err : new Error(String(err));
792
+ done = true;
793
+ if (reject) {
794
+ reject(error);
795
+ resolve = null;
796
+ reject = null;
797
+ }
798
+ },
799
+ complete: () => {
800
+ done = true;
801
+ if (resolve) {
802
+ resolve({ value: void 0, done: true });
803
+ resolve = null;
804
+ reject = null;
805
+ }
806
+ }
807
+ });
808
+ try {
809
+ while (!done || queue.length > 0) {
810
+ if (error) {
811
+ throw error;
812
+ }
813
+ if (queue.length > 0) {
814
+ yield queue.shift();
815
+ } else if (!done) {
816
+ const result = await new Promise((res, rej) => {
817
+ resolve = (iterResult) => {
818
+ if (iterResult.done) {
819
+ done = true;
820
+ res({ done: true });
821
+ } else {
822
+ res({ value: iterResult.value, done: false });
823
+ }
824
+ };
825
+ reject = rej;
826
+ });
827
+ if (!result.done) {
828
+ yield result.value;
829
+ }
830
+ }
831
+ }
832
+ } finally {
833
+ subscription.unsubscribe();
834
+ }
835
+ }
836
+
837
+ // src/environment/ClaudeEffector.ts
838
+ var logger5 = createLogger5("ecosystem/ClaudeEffector");
839
+ var DEFAULT_TIMEOUT = 3e4;
840
+ var ClaudeEffector = class {
841
+ config;
842
+ receptor;
843
+ promptSubject = new Subject2();
844
+ currentAbortController = null;
845
+ claudeQuery = null;
846
+ isInitialized = false;
847
+ wasInterrupted = false;
848
+ currentMeta = null;
849
+ constructor(config, receptor) {
850
+ this.config = config;
851
+ this.receptor = receptor;
852
+ }
853
+ /**
854
+ * Connect to SystemBus consumer to subscribe to events
855
+ */
856
+ connect(consumer) {
857
+ logger5.debug("ClaudeEffector connected to SystemBusConsumer", {
858
+ agentId: this.config.agentId
859
+ });
860
+ consumer.on("user_message", async (event) => {
861
+ const typedEvent = event;
862
+ logger5.debug("user_message event received", {
863
+ eventAgentId: typedEvent.context?.agentId,
864
+ myAgentId: this.config.agentId,
865
+ matches: typedEvent.context?.agentId === this.config.agentId
866
+ });
867
+ if (typedEvent.context?.agentId !== this.config.agentId) {
868
+ return;
869
+ }
870
+ const message = typedEvent.data;
871
+ const meta = {
872
+ requestId: typedEvent.requestId || "",
873
+ context: typedEvent.context || {}
874
+ };
875
+ await this.send(message, meta);
876
+ });
877
+ consumer.on("interrupt", (event) => {
878
+ const typedEvent = event;
879
+ if (typedEvent.context?.agentId !== this.config.agentId) {
880
+ return;
881
+ }
882
+ const meta = {
883
+ requestId: typedEvent.requestId || "",
884
+ context: typedEvent.context || {}
885
+ };
886
+ this.interrupt(meta);
887
+ });
888
+ }
889
+ /**
890
+ * Send a message to Claude SDK
891
+ */
892
+ async send(message, meta) {
893
+ this.wasInterrupted = false;
894
+ this.currentAbortController = new AbortController();
895
+ this.currentMeta = meta;
896
+ const timeout = this.config.timeout ?? DEFAULT_TIMEOUT;
897
+ const timeoutId = setTimeout(() => {
898
+ logger5.warn("Request timeout", { timeout });
899
+ this.currentAbortController?.abort(new Error(`Request timeout after ${timeout}ms`));
900
+ }, timeout);
901
+ try {
902
+ await this.initialize(this.currentAbortController);
903
+ const sessionId = this.config.sessionId || "default";
904
+ const sdkUserMessage = buildSDKUserMessage(message, sessionId);
905
+ logger5.debug("Sending message to Claude", {
906
+ content: typeof message.content === "string" ? message.content.substring(0, 80) : "[structured]",
907
+ timeout,
908
+ requestId: meta.requestId
909
+ });
910
+ this.promptSubject.next(sdkUserMessage);
911
+ } finally {
912
+ clearTimeout(timeoutId);
913
+ this.currentAbortController = null;
914
+ this.wasInterrupted = false;
915
+ }
916
+ }
917
+ /**
918
+ * Interrupt current operation
919
+ */
920
+ interrupt(meta) {
921
+ if (this.claudeQuery) {
922
+ logger5.debug("Interrupting Claude query", { requestId: meta?.requestId });
923
+ this.wasInterrupted = true;
924
+ if (meta) {
925
+ this.currentMeta = meta;
926
+ }
927
+ this.claudeQuery.interrupt().catch((err) => {
928
+ logger5.debug("SDK interrupt() error (may be expected)", { error: err });
929
+ });
930
+ }
931
+ }
932
+ /**
933
+ * Initialize the Claude SDK query (lazy initialization)
934
+ */
935
+ async initialize(abortController) {
936
+ if (this.isInitialized) return;
937
+ logger5.info("Initializing ClaudeEffector");
938
+ const context = {
939
+ apiKey: this.config.apiKey,
940
+ baseUrl: this.config.baseUrl,
941
+ model: this.config.model,
942
+ systemPrompt: this.config.systemPrompt,
943
+ cwd: this.config.cwd,
944
+ resume: this.config.resumeSessionId
945
+ };
946
+ const sdkOptions = buildOptions(context, abortController);
947
+ const promptStream = observableToAsyncIterable(this.promptSubject);
948
+ this.claudeQuery = query({
949
+ prompt: promptStream,
950
+ options: sdkOptions
951
+ });
952
+ this.isInitialized = true;
953
+ this.startBackgroundListener();
954
+ logger5.info("ClaudeEffector initialized");
955
+ }
956
+ /**
957
+ * Start background listener for SDK responses
958
+ */
959
+ startBackgroundListener() {
960
+ (async () => {
961
+ try {
962
+ for await (const sdkMsg of this.claudeQuery) {
963
+ logger5.debug("SDK message received", {
964
+ type: sdkMsg.type,
965
+ subtype: sdkMsg.subtype,
966
+ sessionId: sdkMsg.session_id,
967
+ hasCurrentMeta: !!this.currentMeta
968
+ });
969
+ if (sdkMsg.type === "stream_event" && this.currentMeta) {
970
+ this.receptor.feed(sdkMsg, this.currentMeta);
971
+ }
972
+ if (sdkMsg.type === "user" && this.currentMeta) {
973
+ this.receptor.feedUserMessage(sdkMsg, this.currentMeta);
974
+ }
975
+ if (sdkMsg.session_id && this.config.onSessionIdCaptured) {
976
+ this.config.onSessionIdCaptured(sdkMsg.session_id);
977
+ }
978
+ if (sdkMsg.type === "result") {
979
+ const resultMsg = sdkMsg;
980
+ logger5.info("SDK result received (full)", {
981
+ fullResult: JSON.stringify(sdkMsg, null, 2)
982
+ });
983
+ logger5.info("SDK result received", {
984
+ subtype: resultMsg.subtype,
985
+ isError: resultMsg.is_error,
986
+ errors: resultMsg.errors,
987
+ wasInterrupted: this.wasInterrupted
988
+ });
989
+ if (resultMsg.subtype === "error_during_execution" && this.wasInterrupted) {
990
+ this.receptor.emitInterrupted("user_interrupt", this.currentMeta || void 0);
991
+ } else if (resultMsg.is_error && this.currentMeta) {
992
+ const fullResult = sdkMsg;
993
+ const errorMessage = fullResult.error?.message || fullResult.errors?.join(", ") || (typeof fullResult.result === "string" ? fullResult.result : null) || "An error occurred";
994
+ const errorCode = fullResult.error?.type || resultMsg.subtype || "api_error";
995
+ this.receptor.emitError(errorMessage, errorCode, this.currentMeta);
996
+ }
997
+ }
998
+ }
999
+ } catch (error) {
1000
+ if (this.isAbortError(error)) {
1001
+ logger5.debug("Background listener aborted (expected during interrupt)");
1002
+ this.resetState();
1003
+ } else {
1004
+ logger5.error("Background listener error", { error });
1005
+ if (this.currentMeta) {
1006
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
1007
+ this.receptor.emitError(errorMessage, "runtime_error", this.currentMeta);
1008
+ }
1009
+ }
1010
+ }
1011
+ })();
1012
+ }
1013
+ /**
1014
+ * Check if an error is an abort error
1015
+ */
1016
+ isAbortError(error) {
1017
+ if (error instanceof Error) {
1018
+ if (error.name === "AbortError") return true;
1019
+ if (error.message.includes("aborted")) return true;
1020
+ if (error.message.includes("abort")) return true;
1021
+ }
1022
+ return false;
1023
+ }
1024
+ /**
1025
+ * Reset state after abort
1026
+ */
1027
+ resetState() {
1028
+ this.isInitialized = false;
1029
+ this.claudeQuery = null;
1030
+ this.promptSubject = new Subject2();
1031
+ }
1032
+ /**
1033
+ * Dispose and cleanup resources
1034
+ */
1035
+ dispose() {
1036
+ logger5.debug("Disposing ClaudeEffector");
1037
+ if (this.currentAbortController) {
1038
+ this.currentAbortController.abort();
1039
+ }
1040
+ this.promptSubject.complete();
1041
+ this.resetState();
1042
+ }
1043
+ };
1044
+
1045
+ // src/environment/ClaudeEnvironment.ts
1046
+ var ClaudeEnvironment = class {
1047
+ name = "claude";
1048
+ receptor;
1049
+ effector;
1050
+ claudeEffector;
1051
+ constructor(config) {
1052
+ const claudeReceptor = new ClaudeReceptor();
1053
+ const claudeEffector = new ClaudeEffector(config, claudeReceptor);
1054
+ this.receptor = claudeReceptor;
1055
+ this.effector = claudeEffector;
1056
+ this.claudeEffector = claudeEffector;
1057
+ }
1058
+ /**
1059
+ * Dispose environment resources
1060
+ */
1061
+ dispose() {
1062
+ this.claudeEffector.dispose();
1063
+ }
1064
+ };
1065
+
1066
+ // src/internal/RuntimeAgent.ts
1067
+ var logger6 = createLogger6("runtime/RuntimeAgent");
1068
+ var BusPresenter = class {
1069
+ constructor(producer, session, agentId, imageId, containerId) {
1070
+ this.producer = producer;
1071
+ this.session = session;
1072
+ this.agentId = agentId;
1073
+ this.imageId = imageId;
1074
+ this.containerId = containerId;
1075
+ }
1076
+ name = "BusPresenter";
1077
+ description = "Forwards AgentOutput to SystemBus and collects messages";
1078
+ present(_agentId, output) {
1079
+ const category = this.getCategoryForOutput(output);
1080
+ if (output.type === "user_message") {
1081
+ return;
1082
+ }
1083
+ let data = output.data;
1084
+ if (category === "message") {
1085
+ data = this.convertToMessage(output);
1086
+ }
1087
+ const systemEvent = {
1088
+ type: output.type,
1089
+ timestamp: output.timestamp,
1090
+ data,
1091
+ source: "agent",
1092
+ category,
1093
+ intent: "notification",
1094
+ context: {
1095
+ containerId: this.containerId,
1096
+ imageId: this.imageId,
1097
+ agentId: this.agentId,
1098
+ sessionId: this.session.sessionId
1099
+ }
1100
+ };
1101
+ this.producer.emit(systemEvent);
1102
+ if (category === "message") {
1103
+ this.session.addMessage(data).catch((err) => {
1104
+ logger6.error("Failed to persist message", { error: err, messageType: output.type });
1105
+ });
1106
+ }
1107
+ }
1108
+ /**
1109
+ * Convert AgentOutput to proper Message type for persistence
1110
+ */
1111
+ convertToMessage(output) {
1112
+ const eventData = output.data;
1113
+ const messageId = eventData.messageId ?? eventData.id;
1114
+ const timestamp = eventData.timestamp || output.timestamp;
1115
+ switch (output.type) {
1116
+ case "assistant_message": {
1117
+ const content = eventData.content;
1118
+ return {
1119
+ id: messageId,
1120
+ role: "assistant",
1121
+ subtype: "assistant",
1122
+ content,
1123
+ timestamp
1124
+ };
1125
+ }
1126
+ case "tool_call_message": {
1127
+ const toolCalls = eventData.toolCalls;
1128
+ const toolCall = toolCalls[0];
1129
+ return {
1130
+ id: messageId,
1131
+ role: "assistant",
1132
+ subtype: "tool-call",
1133
+ toolCall,
1134
+ timestamp
1135
+ };
1136
+ }
1137
+ case "tool_result_message": {
1138
+ const results = eventData.results;
1139
+ const toolResult = results[0];
1140
+ return {
1141
+ id: messageId,
1142
+ role: "tool",
1143
+ subtype: "tool-result",
1144
+ toolCallId: toolResult.id,
1145
+ toolResult,
1146
+ timestamp
1147
+ };
1148
+ }
1149
+ case "error_message": {
1150
+ const content = eventData.content;
1151
+ const errorCode = eventData.errorCode;
1152
+ return {
1153
+ id: messageId,
1154
+ role: "error",
1155
+ subtype: "error",
1156
+ content,
1157
+ errorCode,
1158
+ timestamp
1159
+ };
1160
+ }
1161
+ default:
1162
+ logger6.warn("Unknown message type, passing through", { type: output.type });
1163
+ return eventData;
1164
+ }
1165
+ }
1166
+ /**
1167
+ * Determine event category from output type
1168
+ */
1169
+ getCategoryForOutput(output) {
1170
+ const type = output.type;
1171
+ if (type === "message_start" || type === "message_delta" || type === "message_stop" || type === "text_delta" || type === "tool_use_start" || type === "input_json_delta" || type === "tool_use_stop" || type === "tool_result") {
1172
+ return "stream";
1173
+ }
1174
+ if (type === "user_message" || type === "assistant_message" || type === "tool_call_message" || type === "tool_result_message" || type === "error_message") {
1175
+ return "message";
1176
+ }
1177
+ if (type === "turn_request" || type === "turn_response") {
1178
+ return "turn";
1179
+ }
1180
+ return "state";
1181
+ }
1182
+ };
1183
+ var RuntimeAgent = class {
1184
+ agentId;
1185
+ imageId;
1186
+ name;
1187
+ containerId;
1188
+ createdAt;
1189
+ _lifecycle = "running";
1190
+ interactor;
1191
+ driver;
1192
+ engine;
1193
+ producer;
1194
+ environment;
1195
+ imageRepository;
1196
+ session;
1197
+ config;
1198
+ constructor(config) {
1199
+ this.agentId = config.agentId;
1200
+ this.imageId = config.imageId;
1201
+ this.name = config.config.name ?? `agent-${config.agentId}`;
1202
+ this.containerId = config.containerId;
1203
+ this.createdAt = Date.now();
1204
+ this.producer = config.bus.asProducer();
1205
+ this.session = config.session;
1206
+ this.config = config.config;
1207
+ this.imageRepository = config.imageRepository;
1208
+ const resumeSessionId = config.image.metadata?.claudeSdkSessionId;
1209
+ this.environment = new ClaudeEnvironment({
1210
+ agentId: this.agentId,
1211
+ apiKey: config.llmConfig.apiKey,
1212
+ baseUrl: config.llmConfig.baseUrl,
1213
+ model: config.llmConfig.model,
1214
+ systemPrompt: config.config.systemPrompt,
1215
+ resumeSessionId,
1216
+ onSessionIdCaptured: (sdkSessionId) => {
1217
+ this.saveSessionId(sdkSessionId);
1218
+ }
1219
+ });
1220
+ this.environment.receptor.connect(config.bus.asProducer());
1221
+ this.environment.effector.connect(config.bus.asConsumer());
1222
+ logger6.info("ClaudeEnvironment created for agent", {
1223
+ agentId: this.agentId,
1224
+ imageId: this.imageId,
1225
+ resumeSessionId: resumeSessionId ?? "none",
1226
+ isResume: !!resumeSessionId,
1227
+ imageMetadata: config.image.metadata
1228
+ });
1229
+ const presenter = new BusPresenter(
1230
+ this.producer,
1231
+ config.session,
1232
+ this.agentId,
1233
+ this.imageId,
1234
+ this.containerId
1235
+ );
1236
+ this.engine = createAgent({
1237
+ driver: {
1238
+ name: "DummyDriver",
1239
+ description: "Placeholder driver for push-based event handling",
1240
+ receive: async function* () {
1241
+ },
1242
+ interrupt: () => {
1243
+ }
1244
+ },
1245
+ presenter
1246
+ });
1247
+ this.interactor = new AgentInteractor(this.producer, config.session, {
1248
+ agentId: this.agentId,
1249
+ imageId: this.imageId,
1250
+ containerId: this.containerId,
1251
+ sessionId: config.session.sessionId
1252
+ });
1253
+ this.driver = new BusDriver(config.bus.asConsumer(), {
1254
+ agentId: this.agentId,
1255
+ onStreamEvent: (event) => {
1256
+ logger6.debug("BusDriver \u2192 Engine.handleStreamEvent", { type: event.type });
1257
+ this.engine.handleStreamEvent(event);
1258
+ },
1259
+ onStreamComplete: (reason) => {
1260
+ logger6.debug("Stream completed", { reason, agentId: this.agentId });
1261
+ }
1262
+ });
1263
+ logger6.debug("RuntimeAgent created", {
1264
+ agentId: this.agentId,
1265
+ imageId: this.imageId
1266
+ });
1267
+ }
1268
+ /**
1269
+ * Save SDK session ID to image metadata for future resume
1270
+ */
1271
+ saveSessionId(sdkSessionId) {
1272
+ logger6.info("Saving SDK session ID to image metadata", {
1273
+ agentId: this.agentId,
1274
+ imageId: this.imageId,
1275
+ sdkSessionId
1276
+ });
1277
+ this.imageRepository.updateMetadata(this.imageId, { claudeSdkSessionId: sdkSessionId }).catch((err) => {
1278
+ logger6.error("Failed to save SDK session ID", { error: err, imageId: this.imageId });
1279
+ });
1280
+ }
1281
+ get lifecycle() {
1282
+ return this._lifecycle;
1283
+ }
1284
+ /**
1285
+ * Receive a message from user
1286
+ *
1287
+ * @param content - Message content
1288
+ * @param requestId - Request ID for correlation
1289
+ */
1290
+ async receive(content, requestId) {
1291
+ logger6.debug("RuntimeAgent.receive called", {
1292
+ agentId: this.agentId,
1293
+ contentPreview: content.substring(0, 50),
1294
+ requestId
1295
+ });
1296
+ if (this._lifecycle !== "running") {
1297
+ throw new Error(`Cannot send message to ${this._lifecycle} agent`);
1298
+ }
1299
+ await this.interactor.receive(content, requestId || `req_${Date.now()}`);
1300
+ logger6.debug("RuntimeAgent.receive completed", { agentId: this.agentId });
1301
+ }
1302
+ /**
1303
+ * Interrupt current operation
1304
+ */
1305
+ interrupt(requestId) {
1306
+ logger6.debug("RuntimeAgent.interrupt called", { agentId: this.agentId, requestId });
1307
+ this.interactor.interrupt(requestId);
1308
+ this.producer.emit({
1309
+ type: "interrupted",
1310
+ timestamp: Date.now(),
1311
+ source: "agent",
1312
+ category: "lifecycle",
1313
+ intent: "notification",
1314
+ data: {
1315
+ agentId: this.agentId,
1316
+ containerId: this.containerId
1317
+ },
1318
+ context: {
1319
+ containerId: this.containerId,
1320
+ imageId: this.imageId,
1321
+ agentId: this.agentId,
1322
+ sessionId: this.session.sessionId
1323
+ }
1324
+ });
1325
+ }
1326
+ async stop() {
1327
+ if (this._lifecycle === "destroyed") {
1328
+ throw new Error("Cannot stop destroyed agent");
1329
+ }
1330
+ this._lifecycle = "stopped";
1331
+ }
1332
+ async resume() {
1333
+ if (this._lifecycle === "destroyed") {
1334
+ throw new Error("Cannot resume destroyed agent");
1335
+ }
1336
+ this._lifecycle = "running";
1337
+ this.producer.emit({
1338
+ type: "session_resumed",
1339
+ timestamp: Date.now(),
1340
+ source: "session",
1341
+ category: "lifecycle",
1342
+ intent: "notification",
1343
+ data: {
1344
+ sessionId: this.session.sessionId,
1345
+ agentId: this.agentId,
1346
+ containerId: this.containerId
1347
+ },
1348
+ context: {
1349
+ containerId: this.containerId,
1350
+ imageId: this.imageId,
1351
+ agentId: this.agentId,
1352
+ sessionId: this.session.sessionId
1353
+ }
1354
+ });
1355
+ }
1356
+ async destroy() {
1357
+ if (this._lifecycle !== "destroyed") {
1358
+ this.driver.dispose();
1359
+ this.environment.dispose();
1360
+ await this.engine.destroy();
1361
+ this._lifecycle = "destroyed";
1362
+ this.producer.emit({
1363
+ type: "session_destroyed",
1364
+ timestamp: Date.now(),
1365
+ source: "session",
1366
+ category: "lifecycle",
1367
+ intent: "notification",
1368
+ data: {
1369
+ sessionId: this.session.sessionId,
1370
+ agentId: this.agentId,
1371
+ containerId: this.containerId
1372
+ },
1373
+ context: {
1374
+ containerId: this.containerId,
1375
+ imageId: this.imageId,
1376
+ agentId: this.agentId,
1377
+ sessionId: this.session.sessionId
1378
+ }
1379
+ });
1380
+ }
1381
+ }
1382
+ };
1383
+
1384
+ // src/internal/RuntimeSession.ts
1385
+ var RuntimeSession = class {
1386
+ sessionId;
1387
+ imageId;
1388
+ containerId;
1389
+ createdAt;
1390
+ repository;
1391
+ producer;
1392
+ constructor(config) {
1393
+ this.sessionId = config.sessionId;
1394
+ this.imageId = config.imageId;
1395
+ this.containerId = config.containerId;
1396
+ this.createdAt = Date.now();
1397
+ this.repository = config.repository;
1398
+ this.producer = config.producer;
1399
+ }
1400
+ /**
1401
+ * Initialize session in storage
1402
+ */
1403
+ async initialize() {
1404
+ const record = {
1405
+ sessionId: this.sessionId,
1406
+ imageId: this.imageId,
1407
+ containerId: this.containerId,
1408
+ createdAt: this.createdAt,
1409
+ updatedAt: this.createdAt
1410
+ };
1411
+ await this.repository.saveSession(record);
1412
+ this.producer.emit({
1413
+ type: "session_created",
1414
+ timestamp: this.createdAt,
1415
+ source: "session",
1416
+ category: "lifecycle",
1417
+ intent: "notification",
1418
+ data: {
1419
+ sessionId: this.sessionId,
1420
+ imageId: this.imageId,
1421
+ containerId: this.containerId,
1422
+ createdAt: this.createdAt
1423
+ },
1424
+ context: {
1425
+ containerId: this.containerId,
1426
+ sessionId: this.sessionId
1427
+ }
1428
+ });
1429
+ }
1430
+ async addMessage(message) {
1431
+ await this.repository.addMessage(this.sessionId, message);
1432
+ this.producer.emit({
1433
+ type: "message_persisted",
1434
+ timestamp: Date.now(),
1435
+ source: "session",
1436
+ category: "persist",
1437
+ intent: "result",
1438
+ data: {
1439
+ sessionId: this.sessionId,
1440
+ messageId: message.id,
1441
+ savedAt: Date.now()
1442
+ },
1443
+ context: {
1444
+ containerId: this.containerId,
1445
+ sessionId: this.sessionId
1446
+ }
1447
+ });
1448
+ }
1449
+ async getMessages() {
1450
+ return this.repository.getMessages(this.sessionId);
1451
+ }
1452
+ async clear() {
1453
+ await this.repository.clearMessages(this.sessionId);
1454
+ }
1455
+ };
1456
+
1457
+ // src/internal/RuntimeSandbox.ts
1458
+ import { mkdir } from "fs/promises";
1459
+ import { join } from "path";
1460
+ var RuntimeWorkdir = class {
1461
+ id;
1462
+ name;
1463
+ path;
1464
+ constructor(agentId, path) {
1465
+ this.id = agentId;
1466
+ this.name = `workdir_${agentId}`;
1467
+ this.path = path;
1468
+ }
1469
+ };
1470
+ var RuntimeSandbox = class {
1471
+ name;
1472
+ workdir;
1473
+ initialized = false;
1474
+ constructor(config) {
1475
+ this.name = `sandbox_${config.agentId}`;
1476
+ const workdirPath = join(
1477
+ config.basePath,
1478
+ "containers",
1479
+ config.containerId,
1480
+ "workdirs",
1481
+ config.agentId
1482
+ );
1483
+ this.workdir = new RuntimeWorkdir(config.agentId, workdirPath);
1484
+ }
1485
+ /**
1486
+ * Initialize sandbox - create directories
1487
+ */
1488
+ async initialize() {
1489
+ if (this.initialized) return;
1490
+ await mkdir(this.workdir.path, { recursive: true });
1491
+ this.initialized = true;
1492
+ }
1493
+ /**
1494
+ * Cleanup sandbox - remove directories (optional)
1495
+ */
1496
+ async cleanup() {
1497
+ }
1498
+ };
1499
+
1500
+ // src/internal/RuntimeImage.ts
1501
+ import { createLogger as createLogger7 } from "@agentxjs/common";
1502
+ var logger7 = createLogger7("runtime/RuntimeImage");
1503
+ var RuntimeImage = class _RuntimeImage {
1504
+ constructor(record, context) {
1505
+ this.record = record;
1506
+ this.context = context;
1507
+ }
1508
+ // ==================== Getters ====================
1509
+ get imageId() {
1510
+ return this.record.imageId;
1511
+ }
1512
+ get containerId() {
1513
+ return this.record.containerId;
1514
+ }
1515
+ get sessionId() {
1516
+ return this.record.sessionId;
1517
+ }
1518
+ get name() {
1519
+ return this.record.name;
1520
+ }
1521
+ get description() {
1522
+ return this.record.description;
1523
+ }
1524
+ get systemPrompt() {
1525
+ return this.record.systemPrompt;
1526
+ }
1527
+ get createdAt() {
1528
+ return this.record.createdAt;
1529
+ }
1530
+ get updatedAt() {
1531
+ return this.record.updatedAt;
1532
+ }
1533
+ // ==================== Static Factory Methods ====================
1534
+ /**
1535
+ * Create a new image (conversation)
1536
+ */
1537
+ static async create(config, context) {
1538
+ const now = Date.now();
1539
+ const imageId = _RuntimeImage.generateImageId();
1540
+ const sessionId = _RuntimeImage.generateSessionId();
1541
+ const record = {
1542
+ imageId,
1543
+ containerId: config.containerId,
1544
+ sessionId,
1545
+ name: config.name ?? "New Conversation",
1546
+ description: config.description,
1547
+ systemPrompt: config.systemPrompt,
1548
+ createdAt: now,
1549
+ updatedAt: now
1550
+ };
1551
+ await context.imageRepository.saveImage(record);
1552
+ await context.sessionRepository.saveSession({
1553
+ sessionId,
1554
+ imageId,
1555
+ containerId: config.containerId,
1556
+ createdAt: now,
1557
+ updatedAt: now
1558
+ });
1559
+ logger7.info("Image created", {
1560
+ imageId,
1561
+ sessionId,
1562
+ containerId: config.containerId,
1563
+ name: record.name
1564
+ });
1565
+ return new _RuntimeImage(record, context);
1566
+ }
1567
+ /**
1568
+ * Load an existing image from storage
1569
+ */
1570
+ static async load(imageId, context) {
1571
+ const record = await context.imageRepository.findImageById(imageId);
1572
+ if (!record) {
1573
+ logger7.debug("Image not found", { imageId });
1574
+ return null;
1575
+ }
1576
+ logger7.debug("Image loaded", { imageId, name: record.name });
1577
+ return new _RuntimeImage(record, context);
1578
+ }
1579
+ /**
1580
+ * List all images in a container
1581
+ */
1582
+ static async listByContainer(containerId, context) {
1583
+ return context.imageRepository.findImagesByContainerId(containerId);
1584
+ }
1585
+ /**
1586
+ * List all images
1587
+ */
1588
+ static async listAll(context) {
1589
+ return context.imageRepository.findAllImages();
1590
+ }
1591
+ // ==================== Instance Methods ====================
1592
+ /**
1593
+ * Get messages for this conversation
1594
+ */
1595
+ async getMessages() {
1596
+ return this.context.sessionRepository.getMessages(this.sessionId);
1597
+ }
1598
+ /**
1599
+ * Update image metadata
1600
+ */
1601
+ async update(updates) {
1602
+ const now = Date.now();
1603
+ const updatedRecord = {
1604
+ ...this.record,
1605
+ name: updates.name ?? this.record.name,
1606
+ description: updates.description ?? this.record.description,
1607
+ updatedAt: now
1608
+ };
1609
+ await this.context.imageRepository.saveImage(updatedRecord);
1610
+ logger7.info("Image updated", { imageId: this.imageId, updates });
1611
+ return new _RuntimeImage(updatedRecord, this.context);
1612
+ }
1613
+ /**
1614
+ * Delete this image and its session
1615
+ */
1616
+ async delete() {
1617
+ await this.context.sessionRepository.deleteSession(this.sessionId);
1618
+ await this.context.imageRepository.deleteImage(this.imageId);
1619
+ logger7.info("Image deleted", { imageId: this.imageId, sessionId: this.sessionId });
1620
+ }
1621
+ /**
1622
+ * Get the underlying record
1623
+ */
1624
+ toRecord() {
1625
+ return { ...this.record };
1626
+ }
1627
+ // ==================== Private Helpers ====================
1628
+ static generateImageId() {
1629
+ const timestamp = Date.now().toString(36);
1630
+ const random = Math.random().toString(36).substring(2, 8);
1631
+ return `img_${timestamp}_${random}`;
1632
+ }
1633
+ static generateSessionId() {
1634
+ const timestamp = Date.now().toString(36);
1635
+ const random = Math.random().toString(36).substring(2, 8);
1636
+ return `sess_${timestamp}_${random}`;
1637
+ }
1638
+ };
1639
+
1640
+ // src/internal/RuntimeContainer.ts
1641
+ import { createLogger as createLogger8 } from "@agentxjs/common";
1642
+ var logger8 = createLogger8("runtime/RuntimeContainer");
1643
+ var RuntimeContainer = class _RuntimeContainer {
1644
+ containerId;
1645
+ createdAt;
1646
+ /** Map of agentId → RuntimeAgent */
1647
+ agents = /* @__PURE__ */ new Map();
1648
+ /** Map of imageId → agentId (for quick lookup) */
1649
+ imageToAgent = /* @__PURE__ */ new Map();
1650
+ context;
1651
+ constructor(containerId, createdAt, context) {
1652
+ this.containerId = containerId;
1653
+ this.createdAt = createdAt;
1654
+ this.context = context;
1655
+ }
1656
+ /**
1657
+ * Create a new container and persist it
1658
+ */
1659
+ static async create(containerId, context) {
1660
+ const now = Date.now();
1661
+ const record = {
1662
+ containerId,
1663
+ createdAt: now,
1664
+ updatedAt: now
1665
+ };
1666
+ await context.persistence.containers.saveContainer(record);
1667
+ const container = new _RuntimeContainer(containerId, now, context);
1668
+ context.bus.emit({
1669
+ type: "container_created",
1670
+ timestamp: now,
1671
+ source: "container",
1672
+ category: "lifecycle",
1673
+ intent: "notification",
1674
+ data: {
1675
+ containerId,
1676
+ createdAt: now
1677
+ },
1678
+ context: {
1679
+ containerId
1680
+ }
1681
+ });
1682
+ logger8.info("Container created", { containerId });
1683
+ return container;
1684
+ }
1685
+ /**
1686
+ * Load an existing container from persistence
1687
+ */
1688
+ static async load(containerId, context) {
1689
+ const record = await context.persistence.containers.findContainerById(containerId);
1690
+ if (!record) return null;
1691
+ logger8.info("Container loaded", { containerId });
1692
+ return new _RuntimeContainer(containerId, record.createdAt, context);
1693
+ }
1694
+ // ==================== Image → Agent Lifecycle ====================
1695
+ /**
1696
+ * Run an image - create or reuse an Agent for the given Image
1697
+ * @returns { agent, reused } - the agent and whether it was reused
1698
+ */
1699
+ async runImage(image) {
1700
+ const existingAgentId = this.imageToAgent.get(image.imageId);
1701
+ if (existingAgentId) {
1702
+ const existingAgent = this.agents.get(existingAgentId);
1703
+ if (existingAgent) {
1704
+ logger8.info("Reusing existing agent for image", {
1705
+ containerId: this.containerId,
1706
+ imageId: image.imageId,
1707
+ agentId: existingAgentId
1708
+ });
1709
+ return { agent: existingAgent, reused: true };
1710
+ }
1711
+ this.imageToAgent.delete(image.imageId);
1712
+ }
1713
+ const agentId = this.generateAgentId();
1714
+ const sandbox = new RuntimeSandbox({
1715
+ agentId,
1716
+ containerId: this.containerId,
1717
+ basePath: this.context.basePath
1718
+ });
1719
+ await sandbox.initialize();
1720
+ const session = new RuntimeSession({
1721
+ sessionId: image.sessionId,
1722
+ imageId: image.imageId,
1723
+ containerId: this.containerId,
1724
+ repository: this.context.persistence.sessions,
1725
+ producer: this.context.bus.asProducer()
1726
+ });
1727
+ const agent = new RuntimeAgent({
1728
+ agentId,
1729
+ imageId: image.imageId,
1730
+ containerId: this.containerId,
1731
+ config: {
1732
+ name: image.name,
1733
+ description: image.description,
1734
+ systemPrompt: image.systemPrompt
1735
+ },
1736
+ bus: this.context.bus,
1737
+ sandbox,
1738
+ session,
1739
+ llmConfig: this.context.llmConfig,
1740
+ image,
1741
+ // Pass full image record for metadata access
1742
+ imageRepository: this.context.persistence.images
1743
+ });
1744
+ this.agents.set(agentId, agent);
1745
+ this.imageToAgent.set(image.imageId, agentId);
1746
+ this.context.bus.emit({
1747
+ type: "agent_registered",
1748
+ timestamp: Date.now(),
1749
+ source: "container",
1750
+ category: "lifecycle",
1751
+ intent: "notification",
1752
+ data: {
1753
+ containerId: this.containerId,
1754
+ agentId,
1755
+ definitionName: image.name,
1756
+ registeredAt: Date.now()
1757
+ },
1758
+ context: {
1759
+ containerId: this.containerId,
1760
+ agentId
1761
+ }
1762
+ });
1763
+ logger8.info("Agent created for image", {
1764
+ containerId: this.containerId,
1765
+ imageId: image.imageId,
1766
+ agentId
1767
+ });
1768
+ return { agent, reused: false };
1769
+ }
1770
+ /**
1771
+ * Stop an image - destroy the Agent but keep the Image
1772
+ */
1773
+ async stopImage(imageId) {
1774
+ const agentId = this.imageToAgent.get(imageId);
1775
+ if (!agentId) {
1776
+ logger8.debug("Image not running, nothing to stop", {
1777
+ imageId,
1778
+ containerId: this.containerId
1779
+ });
1780
+ return false;
1781
+ }
1782
+ logger8.info("Stopping image", { imageId, agentId, containerId: this.containerId });
1783
+ const success = await this.destroyAgent(agentId);
1784
+ if (success) {
1785
+ this.imageToAgent.delete(imageId);
1786
+ logger8.info("Image stopped", { imageId, agentId, containerId: this.containerId });
1787
+ }
1788
+ return success;
1789
+ }
1790
+ /**
1791
+ * Get agent ID for an image (if running)
1792
+ */
1793
+ getAgentIdForImage(imageId) {
1794
+ return this.imageToAgent.get(imageId);
1795
+ }
1796
+ /**
1797
+ * Check if an image has a running agent
1798
+ */
1799
+ isImageOnline(imageId) {
1800
+ const agentId = this.imageToAgent.get(imageId);
1801
+ return agentId !== void 0 && this.agents.has(agentId);
1802
+ }
1803
+ /**
1804
+ * Get imageId for an agent (reverse lookup)
1805
+ */
1806
+ getImageIdForAgent(agentId) {
1807
+ for (const [imageId, mappedAgentId] of this.imageToAgent.entries()) {
1808
+ if (mappedAgentId === agentId) {
1809
+ return imageId;
1810
+ }
1811
+ }
1812
+ return void 0;
1813
+ }
1814
+ getAgent(agentId) {
1815
+ return this.agents.get(agentId);
1816
+ }
1817
+ listAgents() {
1818
+ return Array.from(this.agents.values());
1819
+ }
1820
+ get agentCount() {
1821
+ return this.agents.size;
1822
+ }
1823
+ async destroyAgent(agentId) {
1824
+ const agent = this.agents.get(agentId);
1825
+ if (!agent) return false;
1826
+ await agent.destroy();
1827
+ this.agents.delete(agentId);
1828
+ this.context.bus.emit({
1829
+ type: "agent_unregistered",
1830
+ timestamp: Date.now(),
1831
+ source: "container",
1832
+ category: "lifecycle",
1833
+ intent: "notification",
1834
+ data: {
1835
+ containerId: this.containerId,
1836
+ agentId
1837
+ },
1838
+ context: {
1839
+ containerId: this.containerId,
1840
+ agentId
1841
+ }
1842
+ });
1843
+ logger8.info("Agent destroyed", { containerId: this.containerId, agentId });
1844
+ return true;
1845
+ }
1846
+ async destroyAllAgents() {
1847
+ const agentIds = Array.from(this.agents.keys());
1848
+ for (const agentId of agentIds) {
1849
+ await this.destroyAgent(agentId);
1850
+ }
1851
+ }
1852
+ // ==================== Container Lifecycle ====================
1853
+ async dispose() {
1854
+ const agentCount = this.agents.size;
1855
+ await this.destroyAllAgents();
1856
+ this.context.bus.emit({
1857
+ type: "container_destroyed",
1858
+ timestamp: Date.now(),
1859
+ source: "container",
1860
+ category: "lifecycle",
1861
+ intent: "notification",
1862
+ data: {
1863
+ containerId: this.containerId,
1864
+ agentCount
1865
+ },
1866
+ context: {
1867
+ containerId: this.containerId
1868
+ }
1869
+ });
1870
+ this.context.onDisposed?.(this.containerId);
1871
+ logger8.info("Container disposed", { containerId: this.containerId, agentCount });
1872
+ }
1873
+ // ==================== Private Helpers ====================
1874
+ generateAgentId() {
1875
+ const timestamp = Date.now().toString(36);
1876
+ const random = Math.random().toString(36).substring(2, 8);
1877
+ return `agent_${timestamp}_${random}`;
1878
+ }
1879
+ };
1880
+
1881
+ // src/internal/BaseEventHandler.ts
1882
+ import { createLogger as createLogger9 } from "@agentxjs/common";
1883
+ var logger9 = createLogger9("runtime/BaseEventHandler");
1884
+ var BaseEventHandler = class {
1885
+ bus;
1886
+ unsubscribes = [];
1887
+ constructor(bus) {
1888
+ this.bus = bus;
1889
+ }
1890
+ /**
1891
+ * Safe execution wrapper for synchronous handlers
1892
+ *
1893
+ * Automatically catches errors and emits ErrorEvent.
1894
+ */
1895
+ safeHandle(handler, context) {
1896
+ try {
1897
+ return handler();
1898
+ } catch (err) {
1899
+ this.handleError(err, context);
1900
+ return void 0;
1901
+ }
1902
+ }
1903
+ /**
1904
+ * Safe execution wrapper for asynchronous handlers
1905
+ *
1906
+ * Automatically catches errors and emits ErrorEvent.
1907
+ */
1908
+ async safeHandleAsync(handler, context) {
1909
+ try {
1910
+ return await handler();
1911
+ } catch (err) {
1912
+ this.handleError(err, context);
1913
+ return void 0;
1914
+ }
1915
+ }
1916
+ /**
1917
+ * Handle error: log + emit ErrorEvent + optional callback
1918
+ */
1919
+ handleError(err, context) {
1920
+ const message = err instanceof Error ? err.message : String(err);
1921
+ const stack = err instanceof Error ? err.stack : void 0;
1922
+ logger9.error(`Error in ${context.operation || "handler"}`, {
1923
+ message,
1924
+ requestId: context.requestId,
1925
+ details: context.details
1926
+ });
1927
+ const errorEvent = {
1928
+ type: "system_error",
1929
+ timestamp: Date.now(),
1930
+ source: context.source || "command",
1931
+ category: "error",
1932
+ intent: "notification",
1933
+ data: {
1934
+ message,
1935
+ requestId: context.requestId,
1936
+ severity: context.severity || "error",
1937
+ details: {
1938
+ operation: context.operation,
1939
+ stack,
1940
+ ...context.details
1941
+ }
1942
+ }
1943
+ };
1944
+ this.bus.emit(errorEvent);
1945
+ if (context.onError) {
1946
+ try {
1947
+ context.onError(err);
1948
+ } catch (callbackErr) {
1949
+ logger9.error("Error in onError callback", { error: callbackErr });
1950
+ }
1951
+ }
1952
+ }
1953
+ /**
1954
+ * Register subscription and track for cleanup
1955
+ */
1956
+ subscribe(unsubscribe) {
1957
+ this.unsubscribes.push(unsubscribe);
1958
+ }
1959
+ /**
1960
+ * Dispose handler and cleanup all subscriptions
1961
+ */
1962
+ dispose() {
1963
+ for (const unsubscribe of this.unsubscribes) {
1964
+ unsubscribe();
1965
+ }
1966
+ this.unsubscribes = [];
1967
+ logger9.debug(`${this.constructor.name} disposed`);
1968
+ }
1969
+ };
1970
+
1971
+ // src/internal/CommandHandler.ts
1972
+ import { createLogger as createLogger10 } from "@agentxjs/common";
1973
+ var logger10 = createLogger10("runtime/CommandHandler");
1974
+ function createResponse(type, data) {
1975
+ return {
1976
+ type,
1977
+ timestamp: Date.now(),
1978
+ data,
1979
+ source: "command",
1980
+ category: "response",
1981
+ intent: "result"
1982
+ };
1983
+ }
1984
+ function createSystemError(message, requestId, context, stack) {
1985
+ return {
1986
+ type: "system_error",
1987
+ timestamp: Date.now(),
1988
+ source: "command",
1989
+ category: "error",
1990
+ intent: "notification",
1991
+ data: {
1992
+ message,
1993
+ requestId,
1994
+ severity: "error",
1995
+ details: stack
1996
+ },
1997
+ context
1998
+ };
1999
+ }
2000
+ var CommandHandler = class extends BaseEventHandler {
2001
+ ops;
2002
+ constructor(bus, operations) {
2003
+ super(bus);
2004
+ this.ops = operations;
2005
+ this.bindHandlers();
2006
+ logger10.debug("CommandHandler created");
2007
+ }
2008
+ /**
2009
+ * Log error and emit system_error event
2010
+ */
2011
+ emitError(operation, err, requestId, context) {
2012
+ const errorMessage = err instanceof Error ? err.message : String(err);
2013
+ const stack = err instanceof Error ? err.stack : void 0;
2014
+ logger10.error(operation, {
2015
+ requestId,
2016
+ ...context,
2017
+ error: errorMessage,
2018
+ stack
2019
+ });
2020
+ this.bus.emit(createSystemError(errorMessage, requestId, context, stack));
2021
+ }
2022
+ /**
2023
+ * Bind all command handlers to the bus
2024
+ */
2025
+ bindHandlers() {
2026
+ this.subscribe(
2027
+ this.bus.onCommand("container_create_request", (event) => this.handleContainerCreate(event))
2028
+ );
2029
+ this.subscribe(
2030
+ this.bus.onCommand("container_get_request", (event) => this.handleContainerGet(event))
2031
+ );
2032
+ this.subscribe(
2033
+ this.bus.onCommand("container_list_request", (event) => this.handleContainerList(event))
2034
+ );
2035
+ this.subscribe(this.bus.onCommand("agent_get_request", (event) => this.handleAgentGet(event)));
2036
+ this.subscribe(
2037
+ this.bus.onCommand("agent_list_request", (event) => this.handleAgentList(event))
2038
+ );
2039
+ this.subscribe(
2040
+ this.bus.onCommand("agent_destroy_request", (event) => this.handleAgentDestroy(event))
2041
+ );
2042
+ this.subscribe(
2043
+ this.bus.onCommand("agent_destroy_all_request", (event) => this.handleAgentDestroyAll(event))
2044
+ );
2045
+ this.subscribe(
2046
+ this.bus.onCommand("message_send_request", (event) => this.handleMessageSend(event))
2047
+ );
2048
+ this.subscribe(
2049
+ this.bus.onCommand("agent_interrupt_request", (event) => this.handleAgentInterrupt(event))
2050
+ );
2051
+ this.subscribe(
2052
+ this.bus.onCommand("image_create_request", (event) => this.handleImageCreate(event))
2053
+ );
2054
+ this.subscribe(this.bus.onCommand("image_run_request", (event) => this.handleImageRun(event)));
2055
+ this.subscribe(
2056
+ this.bus.onCommand("image_stop_request", (event) => this.handleImageStop(event))
2057
+ );
2058
+ this.subscribe(
2059
+ this.bus.onCommand("image_update_request", (event) => this.handleImageUpdate(event))
2060
+ );
2061
+ this.subscribe(
2062
+ this.bus.onCommand("image_list_request", (event) => this.handleImageList(event))
2063
+ );
2064
+ this.subscribe(this.bus.onCommand("image_get_request", (event) => this.handleImageGet(event)));
2065
+ this.subscribe(
2066
+ this.bus.onCommand("image_delete_request", (event) => this.handleImageDelete(event))
2067
+ );
2068
+ this.subscribe(
2069
+ this.bus.onCommand("image_messages_request", (event) => this.handleImageMessages(event))
2070
+ );
2071
+ logger10.debug("Command handlers bound");
2072
+ }
2073
+ // ==================== Container Handlers ====================
2074
+ async handleContainerCreate(event) {
2075
+ const { requestId, containerId } = event.data;
2076
+ logger10.debug("Handling container_create_request", { requestId, containerId });
2077
+ try {
2078
+ await this.ops.createContainer(containerId);
2079
+ this.bus.emit(
2080
+ createResponse("container_create_response", {
2081
+ requestId,
2082
+ containerId
2083
+ })
2084
+ );
2085
+ } catch (err) {
2086
+ this.emitError("Failed to create container", err, requestId, { containerId });
2087
+ this.bus.emit(
2088
+ createResponse("container_create_response", {
2089
+ requestId,
2090
+ containerId,
2091
+ error: err instanceof Error ? err.message : String(err)
2092
+ })
2093
+ );
2094
+ }
2095
+ }
2096
+ handleContainerGet(event) {
2097
+ const { requestId, containerId } = event.data;
2098
+ logger10.debug("Handling container_get_request", { requestId, containerId });
2099
+ const container = this.ops.getContainer(containerId);
2100
+ this.bus.emit(
2101
+ createResponse("container_get_response", {
2102
+ requestId,
2103
+ containerId: container?.containerId,
2104
+ exists: !!container
2105
+ })
2106
+ );
2107
+ }
2108
+ handleContainerList(event) {
2109
+ const { requestId } = event.data;
2110
+ logger10.debug("Handling container_list_request", { requestId });
2111
+ const containers = this.ops.listContainers();
2112
+ this.bus.emit(
2113
+ createResponse("container_list_response", {
2114
+ requestId,
2115
+ containerIds: containers.map((c) => c.containerId)
2116
+ })
2117
+ );
2118
+ }
2119
+ // ==================== Agent Handlers ====================
2120
+ handleAgentGet(event) {
2121
+ const { requestId, agentId } = event.data;
2122
+ logger10.debug("Handling agent_get_request", { requestId, agentId });
2123
+ const agent = this.ops.getAgent(agentId);
2124
+ this.bus.emit(
2125
+ createResponse("agent_get_response", {
2126
+ requestId,
2127
+ agentId: agent?.agentId,
2128
+ containerId: agent?.containerId,
2129
+ exists: !!agent
2130
+ })
2131
+ );
2132
+ }
2133
+ handleAgentList(event) {
2134
+ const { requestId, containerId } = event.data;
2135
+ logger10.debug("Handling agent_list_request", { requestId, containerId });
2136
+ const agents = this.ops.listAgents(containerId);
2137
+ this.bus.emit(
2138
+ createResponse("agent_list_response", {
2139
+ requestId,
2140
+ agents: agents.map((a) => ({
2141
+ agentId: a.agentId,
2142
+ containerId: a.containerId,
2143
+ imageId: a.imageId
2144
+ }))
2145
+ })
2146
+ );
2147
+ }
2148
+ async handleAgentDestroy(event) {
2149
+ const { requestId, agentId } = event.data;
2150
+ logger10.debug("Handling agent_destroy_request", { requestId, agentId });
2151
+ try {
2152
+ const success = await this.ops.destroyAgent(agentId);
2153
+ this.bus.emit(
2154
+ createResponse("agent_destroy_response", {
2155
+ requestId,
2156
+ agentId,
2157
+ success
2158
+ })
2159
+ );
2160
+ } catch (err) {
2161
+ this.emitError("Failed to destroy agent", err, requestId, { agentId });
2162
+ this.bus.emit(
2163
+ createResponse("agent_destroy_response", {
2164
+ requestId,
2165
+ agentId,
2166
+ success: false,
2167
+ error: err instanceof Error ? err.message : String(err)
2168
+ })
2169
+ );
2170
+ }
2171
+ }
2172
+ async handleAgentDestroyAll(event) {
2173
+ const { requestId, containerId } = event.data;
2174
+ logger10.debug("Handling agent_destroy_all_request", { requestId, containerId });
2175
+ try {
2176
+ await this.ops.destroyAllAgents(containerId);
2177
+ this.bus.emit(
2178
+ createResponse("agent_destroy_all_response", {
2179
+ requestId,
2180
+ containerId
2181
+ })
2182
+ );
2183
+ } catch (err) {
2184
+ this.emitError("Failed to destroy all agents", err, requestId, { containerId });
2185
+ this.bus.emit(
2186
+ createResponse("agent_destroy_all_response", {
2187
+ requestId,
2188
+ containerId,
2189
+ error: err instanceof Error ? err.message : String(err)
2190
+ })
2191
+ );
2192
+ }
2193
+ }
2194
+ async handleMessageSend(event) {
2195
+ const { requestId, imageId, agentId, content } = event.data;
2196
+ logger10.debug("Handling message_send_request", { requestId, imageId, agentId });
2197
+ try {
2198
+ const result = await this.ops.receiveMessage(imageId, agentId, content, requestId);
2199
+ this.bus.emit(
2200
+ createResponse("message_send_response", {
2201
+ requestId,
2202
+ imageId: result.imageId,
2203
+ agentId: result.agentId
2204
+ })
2205
+ );
2206
+ } catch (err) {
2207
+ this.emitError("Failed to send message", err, requestId, { imageId, agentId });
2208
+ this.bus.emit(
2209
+ createResponse("message_send_response", {
2210
+ requestId,
2211
+ imageId,
2212
+ agentId: agentId ?? "",
2213
+ error: err instanceof Error ? err.message : String(err)
2214
+ })
2215
+ );
2216
+ }
2217
+ }
2218
+ handleAgentInterrupt(event) {
2219
+ const { requestId, imageId, agentId } = event.data;
2220
+ logger10.debug("Handling agent_interrupt_request", { requestId, imageId, agentId });
2221
+ try {
2222
+ const result = this.ops.interruptAgent(imageId, agentId, requestId);
2223
+ this.bus.emit(
2224
+ createResponse("agent_interrupt_response", {
2225
+ requestId,
2226
+ imageId: result.imageId,
2227
+ agentId: result.agentId
2228
+ })
2229
+ );
2230
+ } catch (err) {
2231
+ this.emitError("Failed to interrupt agent", err, requestId, { imageId, agentId });
2232
+ this.bus.emit(
2233
+ createResponse("agent_interrupt_response", {
2234
+ requestId,
2235
+ imageId,
2236
+ agentId,
2237
+ error: err instanceof Error ? err.message : String(err)
2238
+ })
2239
+ );
2240
+ }
2241
+ }
2242
+ // ==================== Image Handlers ====================
2243
+ async handleImageCreate(event) {
2244
+ const { requestId, containerId, config } = event.data;
2245
+ logger10.debug("Handling image_create_request", { requestId, containerId });
2246
+ try {
2247
+ const record = await this.ops.createImage(containerId, config);
2248
+ this.bus.emit(
2249
+ createResponse("image_create_response", {
2250
+ requestId,
2251
+ record
2252
+ })
2253
+ );
2254
+ } catch (err) {
2255
+ this.emitError("Failed to create image", err, requestId, { containerId });
2256
+ this.bus.emit(
2257
+ createResponse("image_create_response", {
2258
+ requestId,
2259
+ record: null,
2260
+ error: err instanceof Error ? err.message : String(err)
2261
+ })
2262
+ );
2263
+ }
2264
+ }
2265
+ async handleImageRun(event) {
2266
+ const { requestId, imageId } = event.data;
2267
+ logger10.debug("Handling image_run_request", { requestId, imageId });
2268
+ try {
2269
+ const result = await this.ops.runImage(imageId);
2270
+ this.bus.emit(
2271
+ createResponse("image_run_response", {
2272
+ requestId,
2273
+ imageId: result.imageId,
2274
+ agentId: result.agentId,
2275
+ reused: result.reused
2276
+ })
2277
+ );
2278
+ } catch (err) {
2279
+ this.emitError("Failed to run image", err, requestId, { imageId });
2280
+ this.bus.emit(
2281
+ createResponse("image_run_response", {
2282
+ requestId,
2283
+ imageId,
2284
+ agentId: "",
2285
+ reused: false,
2286
+ error: err instanceof Error ? err.message : String(err)
2287
+ })
2288
+ );
2289
+ }
2290
+ }
2291
+ async handleImageStop(event) {
2292
+ const { requestId, imageId } = event.data;
2293
+ logger10.debug("Handling image_stop_request", { requestId, imageId });
2294
+ try {
2295
+ await this.ops.stopImage(imageId);
2296
+ this.bus.emit(
2297
+ createResponse("image_stop_response", {
2298
+ requestId,
2299
+ imageId
2300
+ })
2301
+ );
2302
+ } catch (err) {
2303
+ this.emitError("Failed to stop image", err, requestId, { imageId });
2304
+ this.bus.emit(
2305
+ createResponse("image_stop_response", {
2306
+ requestId,
2307
+ imageId,
2308
+ error: err instanceof Error ? err.message : String(err)
2309
+ })
2310
+ );
2311
+ }
2312
+ }
2313
+ async handleImageUpdate(event) {
2314
+ const { requestId, imageId, updates } = event.data;
2315
+ logger10.debug("Handling image_update_request", { requestId, imageId });
2316
+ try {
2317
+ const record = await this.ops.updateImage(imageId, updates);
2318
+ this.bus.emit(
2319
+ createResponse("image_update_response", {
2320
+ requestId,
2321
+ record
2322
+ })
2323
+ );
2324
+ } catch (err) {
2325
+ this.emitError("Failed to update image", err, requestId, { imageId });
2326
+ this.bus.emit(
2327
+ createResponse("image_update_response", {
2328
+ requestId,
2329
+ record: null,
2330
+ error: err instanceof Error ? err.message : String(err)
2331
+ })
2332
+ );
2333
+ }
2334
+ }
2335
+ async handleImageList(event) {
2336
+ const { requestId, containerId } = event.data;
2337
+ logger10.debug("Handling image_list_request", { requestId, containerId });
2338
+ try {
2339
+ const images = await this.ops.listImages(containerId);
2340
+ this.bus.emit(
2341
+ createResponse("image_list_response", {
2342
+ requestId,
2343
+ records: images
2344
+ })
2345
+ );
2346
+ } catch (err) {
2347
+ this.emitError("Failed to list images", err, requestId, { containerId });
2348
+ this.bus.emit(
2349
+ createResponse("image_list_response", {
2350
+ requestId,
2351
+ records: [],
2352
+ error: err instanceof Error ? err.message : String(err)
2353
+ })
2354
+ );
2355
+ }
2356
+ }
2357
+ async handleImageGet(event) {
2358
+ const { requestId, imageId } = event.data;
2359
+ logger10.debug("Handling image_get_request", { requestId, imageId });
2360
+ try {
2361
+ const image = await this.ops.getImage(imageId);
2362
+ this.bus.emit(
2363
+ createResponse("image_get_response", {
2364
+ requestId,
2365
+ record: image
2366
+ })
2367
+ );
2368
+ } catch (err) {
2369
+ this.emitError("Failed to get image", err, requestId, { imageId });
2370
+ this.bus.emit(
2371
+ createResponse("image_get_response", {
2372
+ requestId,
2373
+ error: err instanceof Error ? err.message : String(err)
2374
+ })
2375
+ );
2376
+ }
2377
+ }
2378
+ async handleImageDelete(event) {
2379
+ const { requestId, imageId } = event.data;
2380
+ logger10.debug("Handling image_delete_request", { requestId, imageId });
2381
+ try {
2382
+ await this.ops.deleteImage(imageId);
2383
+ this.bus.emit(
2384
+ createResponse("image_delete_response", {
2385
+ requestId,
2386
+ imageId
2387
+ })
2388
+ );
2389
+ } catch (err) {
2390
+ this.emitError("Failed to delete image", err, requestId, { imageId });
2391
+ this.bus.emit(
2392
+ createResponse("image_delete_response", {
2393
+ requestId,
2394
+ imageId,
2395
+ error: err instanceof Error ? err.message : String(err)
2396
+ })
2397
+ );
2398
+ }
2399
+ }
2400
+ async handleImageMessages(event) {
2401
+ const { requestId, imageId } = event.data;
2402
+ logger10.info("Handling image_messages_request", { requestId, imageId });
2403
+ try {
2404
+ const messages = await this.ops.getImageMessages(imageId);
2405
+ logger10.info("Got messages for image", { imageId, count: messages.length });
2406
+ this.bus.emit(
2407
+ createResponse("image_messages_response", {
2408
+ requestId,
2409
+ imageId,
2410
+ messages
2411
+ })
2412
+ );
2413
+ logger10.info("Emitted image_messages_response", { requestId, imageId });
2414
+ } catch (err) {
2415
+ this.emitError("Failed to get image messages", err, requestId, { imageId });
2416
+ this.bus.emit(
2417
+ createResponse("image_messages_response", {
2418
+ requestId,
2419
+ imageId,
2420
+ messages: [],
2421
+ error: err instanceof Error ? err.message : String(err)
2422
+ })
2423
+ );
2424
+ }
2425
+ }
2426
+ // Lifecycle is handled by BaseEventHandler.dispose()
2427
+ };
2428
+
2429
+ // src/RuntimeImpl.ts
2430
+ import { createLogger as createLogger11 } from "@agentxjs/common";
2431
+ import { homedir } from "os";
2432
+ import { join as join2 } from "path";
2433
+ var logger11 = createLogger11("runtime/RuntimeImpl");
2434
+ var RuntimeImpl = class {
2435
+ persistence;
2436
+ llmProvider;
2437
+ bus;
2438
+ llmConfig;
2439
+ basePath;
2440
+ commandHandler;
2441
+ /** Container registry: containerId -> RuntimeContainer */
2442
+ containerRegistry = /* @__PURE__ */ new Map();
2443
+ constructor(config) {
2444
+ logger11.info("RuntimeImpl constructor start");
2445
+ this.persistence = config.persistence;
2446
+ this.llmProvider = config.llmProvider;
2447
+ this.basePath = join2(homedir(), ".agentx");
2448
+ logger11.info("Creating SystemBus");
2449
+ this.bus = new SystemBusImpl();
2450
+ this.llmConfig = this.llmProvider.provide();
2451
+ logger11.info("LLM config loaded", {
2452
+ hasApiKey: !!this.llmConfig.apiKey,
2453
+ model: this.llmConfig.model
2454
+ });
2455
+ logger11.info("Creating CommandHandler");
2456
+ this.commandHandler = new CommandHandler(this.bus, this.createRuntimeOperations());
2457
+ logger11.info("RuntimeImpl constructor done");
2458
+ }
2459
+ // ==================== SystemBus delegation ====================
2460
+ emit(event) {
2461
+ this.bus.emit(event);
2462
+ }
2463
+ emitBatch(events) {
2464
+ this.bus.emitBatch(events);
2465
+ }
2466
+ on(typeOrTypes, handler, options) {
2467
+ return this.bus.on(typeOrTypes, handler, options);
2468
+ }
2469
+ onAny(handler, options) {
2470
+ return this.bus.onAny(handler, options);
2471
+ }
2472
+ once(type, handler) {
2473
+ return this.bus.once(type, handler);
2474
+ }
2475
+ onCommand(type, handler) {
2476
+ return this.bus.onCommand(type, handler);
2477
+ }
2478
+ emitCommand(type, data) {
2479
+ this.bus.emitCommand(type, data);
2480
+ }
2481
+ request(type, data, timeout) {
2482
+ return this.bus.request(type, data, timeout);
2483
+ }
2484
+ asConsumer() {
2485
+ return this.bus.asConsumer();
2486
+ }
2487
+ asProducer() {
2488
+ return this.bus.asProducer();
2489
+ }
2490
+ destroy() {
2491
+ this.bus.destroy();
2492
+ }
2493
+ // ==================== Runtime Operations (for CommandHandler) ====================
2494
+ createRuntimeOperations() {
2495
+ return {
2496
+ // Container operations
2497
+ createContainer: async (containerId) => {
2498
+ const container = await this.getOrCreateContainer(containerId);
2499
+ return { containerId: container.containerId };
2500
+ },
2501
+ getContainer: (containerId) => {
2502
+ const container = this.containerRegistry.get(containerId);
2503
+ return container ? { containerId: container.containerId } : void 0;
2504
+ },
2505
+ listContainers: () => {
2506
+ return Array.from(this.containerRegistry.values()).map((c) => ({
2507
+ containerId: c.containerId
2508
+ }));
2509
+ },
2510
+ // Agent operations (by agentId)
2511
+ getAgent: (agentId) => {
2512
+ const agent = this.findAgent(agentId);
2513
+ if (!agent) return void 0;
2514
+ const imageId = this.findImageIdForAgent(agentId);
2515
+ return { agentId: agent.agentId, containerId: agent.containerId, imageId: imageId ?? "" };
2516
+ },
2517
+ listAgents: (containerId) => {
2518
+ const container = this.containerRegistry.get(containerId);
2519
+ if (!container) return [];
2520
+ return container.listAgents().map((a) => {
2521
+ const imageId = this.findImageIdForAgent(a.agentId);
2522
+ return { agentId: a.agentId, containerId: a.containerId, imageId: imageId ?? "" };
2523
+ });
2524
+ },
2525
+ destroyAgent: async (agentId) => {
2526
+ for (const container of this.containerRegistry.values()) {
2527
+ if (container.getAgent(agentId)) {
2528
+ return container.destroyAgent(agentId);
2529
+ }
2530
+ }
2531
+ return false;
2532
+ },
2533
+ destroyAllAgents: async (containerId) => {
2534
+ const container = this.containerRegistry.get(containerId);
2535
+ await container?.destroyAllAgents();
2536
+ },
2537
+ // Agent operations (by imageId - with auto-activation)
2538
+ receiveMessage: async (imageId, agentId, content, requestId) => {
2539
+ if (imageId) {
2540
+ logger11.debug("Receiving message by imageId", {
2541
+ imageId,
2542
+ contentLength: content.length,
2543
+ requestId
2544
+ });
2545
+ const record = await this.persistence.images.findImageById(imageId);
2546
+ if (!record) throw new Error(`Image not found: ${imageId}`);
2547
+ const container = await this.getOrCreateContainer(record.containerId);
2548
+ const { agent, reused } = await container.runImage(record);
2549
+ logger11.info("Message routed to agent", {
2550
+ imageId,
2551
+ agentId: agent.agentId,
2552
+ reused,
2553
+ requestId
2554
+ });
2555
+ await agent.receive(content, requestId);
2556
+ return { agentId: agent.agentId, imageId };
2557
+ }
2558
+ if (agentId) {
2559
+ logger11.debug("Receiving message by agentId (legacy)", {
2560
+ agentId,
2561
+ contentLength: content.length,
2562
+ requestId
2563
+ });
2564
+ const agent = this.findAgent(agentId);
2565
+ if (!agent) throw new Error(`Agent not found: ${agentId}`);
2566
+ await agent.receive(content, requestId);
2567
+ const foundImageId = this.findImageIdForAgent(agentId);
2568
+ return { agentId, imageId: foundImageId };
2569
+ }
2570
+ throw new Error("Either imageId or agentId must be provided");
2571
+ },
2572
+ interruptAgent: (imageId, agentId, requestId) => {
2573
+ if (imageId) {
2574
+ const foundAgentId = this.findAgentIdForImage(imageId);
2575
+ if (!foundAgentId) {
2576
+ logger11.debug("Image is offline, nothing to interrupt", { imageId });
2577
+ return { imageId, agentId: void 0 };
2578
+ }
2579
+ const agent = this.findAgent(foundAgentId);
2580
+ if (agent) {
2581
+ logger11.info("Interrupting agent by imageId", {
2582
+ imageId,
2583
+ agentId: foundAgentId,
2584
+ requestId
2585
+ });
2586
+ agent.interrupt(requestId);
2587
+ }
2588
+ return { imageId, agentId: foundAgentId };
2589
+ }
2590
+ if (agentId) {
2591
+ const agent = this.findAgent(agentId);
2592
+ if (!agent) throw new Error(`Agent not found: ${agentId}`);
2593
+ logger11.info("Interrupting agent by agentId (legacy)", { agentId, requestId });
2594
+ agent.interrupt(requestId);
2595
+ const foundImageId = this.findImageIdForAgent(agentId);
2596
+ return { agentId, imageId: foundImageId };
2597
+ }
2598
+ throw new Error("Either imageId or agentId must be provided");
2599
+ },
2600
+ // Image operations (new model)
2601
+ createImage: async (containerId, config) => {
2602
+ logger11.debug("Creating image", { containerId, name: config.name });
2603
+ await this.getOrCreateContainer(containerId);
2604
+ const image = await RuntimeImage.create(
2605
+ { containerId, ...config },
2606
+ this.createImageContext()
2607
+ );
2608
+ logger11.info("Image created via RuntimeOps", { imageId: image.imageId, containerId });
2609
+ return this.toImageListItemResult(image.toRecord(), false);
2610
+ },
2611
+ runImage: async (imageId) => {
2612
+ logger11.debug("Running image", { imageId });
2613
+ const record = await this.persistence.images.findImageById(imageId);
2614
+ if (!record) throw new Error(`Image not found: ${imageId}`);
2615
+ const container = await this.getOrCreateContainer(record.containerId);
2616
+ const { agent, reused } = await container.runImage(record);
2617
+ logger11.info("Image running", { imageId, agentId: agent.agentId, reused });
2618
+ return { imageId, agentId: agent.agentId, reused };
2619
+ },
2620
+ stopImage: async (imageId) => {
2621
+ logger11.debug("Stopping image", { imageId });
2622
+ const record = await this.persistence.images.findImageById(imageId);
2623
+ if (!record) throw new Error(`Image not found: ${imageId}`);
2624
+ const container = this.containerRegistry.get(record.containerId);
2625
+ if (container) {
2626
+ await container.stopImage(imageId);
2627
+ logger11.info("Image stopped via RuntimeOps", { imageId });
2628
+ }
2629
+ },
2630
+ updateImage: async (imageId, updates) => {
2631
+ const image = await RuntimeImage.load(imageId, this.createImageContext());
2632
+ if (!image) throw new Error(`Image not found: ${imageId}`);
2633
+ const updatedImage = await image.update(updates);
2634
+ const online = this.isImageOnline(imageId);
2635
+ return this.toImageListItemResult(updatedImage.toRecord(), online);
2636
+ },
2637
+ listImages: async (containerId) => {
2638
+ const records = containerId ? await RuntimeImage.listByContainer(containerId, this.createImageContext()) : await RuntimeImage.listAll(this.createImageContext());
2639
+ return records.map((r) => {
2640
+ const online = this.isImageOnline(r.imageId);
2641
+ return this.toImageListItemResult(r, online);
2642
+ });
2643
+ },
2644
+ getImage: async (imageId) => {
2645
+ const record = await this.persistence.images.findImageById(imageId);
2646
+ if (!record) return null;
2647
+ const online = this.isImageOnline(imageId);
2648
+ return this.toImageListItemResult(record, online);
2649
+ },
2650
+ deleteImage: async (imageId) => {
2651
+ logger11.debug("Deleting image", { imageId });
2652
+ const agentId = this.findAgentIdForImage(imageId);
2653
+ if (agentId) {
2654
+ logger11.debug("Stopping running agent before delete", { imageId, agentId });
2655
+ for (const container of this.containerRegistry.values()) {
2656
+ if (container.getAgent(agentId)) {
2657
+ await container.destroyAgent(agentId);
2658
+ break;
2659
+ }
2660
+ }
2661
+ }
2662
+ const image = await RuntimeImage.load(imageId, this.createImageContext());
2663
+ if (image) {
2664
+ await image.delete();
2665
+ logger11.info("Image deleted via RuntimeOps", { imageId });
2666
+ }
2667
+ },
2668
+ getImageMessages: async (imageId) => {
2669
+ logger11.debug("Getting messages for image", { imageId });
2670
+ const image = await RuntimeImage.load(imageId, this.createImageContext());
2671
+ if (!image) {
2672
+ throw new Error(`Image not found: ${imageId}`);
2673
+ }
2674
+ const messages = await image.getMessages();
2675
+ logger11.debug("Got messages from storage", { imageId, count: messages.length });
2676
+ return messages.map((m) => {
2677
+ let content;
2678
+ let role = m.role;
2679
+ let errorCode;
2680
+ if (m.subtype === "user" || m.subtype === "assistant") {
2681
+ content = m.content;
2682
+ } else if (m.subtype === "tool-call") {
2683
+ content = m.toolCall;
2684
+ role = "tool_call";
2685
+ } else if (m.subtype === "tool-result") {
2686
+ content = m.toolResult;
2687
+ role = "tool_result";
2688
+ } else if (m.subtype === "error") {
2689
+ content = m.content;
2690
+ role = "error";
2691
+ errorCode = m.errorCode;
2692
+ }
2693
+ return {
2694
+ id: m.id,
2695
+ role,
2696
+ content,
2697
+ timestamp: m.timestamp,
2698
+ errorCode
2699
+ };
2700
+ });
2701
+ }
2702
+ };
2703
+ }
2704
+ // ==================== Internal Helpers ====================
2705
+ async getOrCreateContainer(containerId) {
2706
+ const existing = this.containerRegistry.get(containerId);
2707
+ if (existing) return existing;
2708
+ const loaded = await RuntimeContainer.load(containerId, this.createContainerContext());
2709
+ if (loaded) {
2710
+ this.containerRegistry.set(containerId, loaded);
2711
+ return loaded;
2712
+ }
2713
+ const container = await RuntimeContainer.create(containerId, this.createContainerContext());
2714
+ this.containerRegistry.set(containerId, container);
2715
+ return container;
2716
+ }
2717
+ findAgent(agentId) {
2718
+ for (const container of this.containerRegistry.values()) {
2719
+ const agent = container.getAgent(agentId);
2720
+ if (agent) return agent;
2721
+ }
2722
+ return void 0;
2723
+ }
2724
+ /**
2725
+ * Find imageId for a given agentId (reverse lookup)
2726
+ */
2727
+ findImageIdForAgent(agentId) {
2728
+ for (const container of this.containerRegistry.values()) {
2729
+ const imageId = container.getImageIdForAgent(agentId);
2730
+ if (imageId) return imageId;
2731
+ }
2732
+ return void 0;
2733
+ }
2734
+ /**
2735
+ * Find agentId for a given imageId
2736
+ */
2737
+ findAgentIdForImage(imageId) {
2738
+ for (const container of this.containerRegistry.values()) {
2739
+ const agentId = container.getAgentIdForImage(imageId);
2740
+ if (agentId) return agentId;
2741
+ }
2742
+ return void 0;
2743
+ }
2744
+ /**
2745
+ * Check if an image has a running agent
2746
+ */
2747
+ isImageOnline(imageId) {
2748
+ for (const container of this.containerRegistry.values()) {
2749
+ if (container.isImageOnline(imageId)) {
2750
+ return true;
2751
+ }
2752
+ }
2753
+ return false;
2754
+ }
2755
+ /**
2756
+ * Convert ImageRecord to ImageListItemResult
2757
+ */
2758
+ toImageListItemResult(record, online) {
2759
+ const agentId = online ? this.findAgentIdForImage(record.imageId) : void 0;
2760
+ return {
2761
+ imageId: record.imageId,
2762
+ containerId: record.containerId,
2763
+ sessionId: record.sessionId,
2764
+ name: record.name,
2765
+ description: record.description,
2766
+ systemPrompt: record.systemPrompt,
2767
+ createdAt: record.createdAt,
2768
+ updatedAt: record.updatedAt,
2769
+ online,
2770
+ agentId
2771
+ };
2772
+ }
2773
+ createContainerContext() {
2774
+ return {
2775
+ persistence: this.persistence,
2776
+ bus: this.bus,
2777
+ llmConfig: this.llmConfig,
2778
+ basePath: this.basePath,
2779
+ onDisposed: (containerId) => {
2780
+ this.containerRegistry.delete(containerId);
2781
+ }
2782
+ };
2783
+ }
2784
+ createImageContext() {
2785
+ return {
2786
+ imageRepository: this.persistence.images,
2787
+ sessionRepository: this.persistence.sessions
2788
+ };
2789
+ }
2790
+ // ==================== Lifecycle ====================
2791
+ async dispose() {
2792
+ logger11.info("Disposing RuntimeImpl");
2793
+ this.commandHandler.dispose();
2794
+ for (const container of this.containerRegistry.values()) {
2795
+ await container.dispose();
2796
+ }
2797
+ this.bus.destroy();
2798
+ this.containerRegistry.clear();
2799
+ logger11.info("RuntimeImpl disposed");
2800
+ }
2801
+ };
2802
+
2803
+ // src/createRuntime.ts
2804
+ function createRuntime(config) {
2805
+ return new RuntimeImpl(config);
2806
+ }
2807
+
2808
+ // src/internal/persistence/PersistenceImpl.ts
2809
+ import { createStorage } from "unstorage";
2810
+ import { createLogger as createLogger15 } from "@agentxjs/common";
2811
+
2812
+ // src/internal/persistence/repository/StorageImageRepository.ts
2813
+ import { createLogger as createLogger12 } from "@agentxjs/common";
2814
+ var logger12 = createLogger12("persistence/ImageRepository");
2815
+ var PREFIX = "images";
2816
+ var INDEX_BY_NAME = "idx:images:name";
2817
+ var INDEX_BY_CONTAINER = "idx:images:container";
2818
+ var StorageImageRepository = class {
2819
+ constructor(storage) {
2820
+ this.storage = storage;
2821
+ }
2822
+ key(imageId) {
2823
+ return `${PREFIX}:${imageId}`;
2824
+ }
2825
+ nameIndexKey(name, imageId) {
2826
+ return `${INDEX_BY_NAME}:${name}:${imageId}`;
2827
+ }
2828
+ containerIndexKey(containerId, imageId) {
2829
+ return `${INDEX_BY_CONTAINER}:${containerId}:${imageId}`;
2830
+ }
2831
+ async saveImage(record) {
2832
+ await this.storage.setItem(this.key(record.imageId), record);
2833
+ await this.storage.setItem(this.nameIndexKey(record.name, record.imageId), record.imageId);
2834
+ await this.storage.setItem(
2835
+ this.containerIndexKey(record.containerId, record.imageId),
2836
+ record.imageId
2837
+ );
2838
+ logger12.debug("Image saved", { imageId: record.imageId });
2839
+ }
2840
+ async findImageById(imageId) {
2841
+ const record = await this.storage.getItem(this.key(imageId));
2842
+ return record ?? null;
2843
+ }
2844
+ async findAllImages() {
2845
+ const keys = await this.storage.getKeys(PREFIX);
2846
+ const records = [];
2847
+ for (const key of keys) {
2848
+ if (key.startsWith("idx:")) continue;
2849
+ const record = await this.storage.getItem(key);
2850
+ if (record) {
2851
+ records.push(record);
2852
+ }
2853
+ }
2854
+ return records.sort((a, b) => b.createdAt - a.createdAt);
2855
+ }
2856
+ async findImagesByName(name) {
2857
+ const indexPrefix = `${INDEX_BY_NAME}:${name}`;
2858
+ const keys = await this.storage.getKeys(indexPrefix);
2859
+ const records = [];
2860
+ for (const key of keys) {
2861
+ const imageId = await this.storage.getItem(key);
2862
+ if (imageId) {
2863
+ const record = await this.findImageById(imageId);
2864
+ if (record) {
2865
+ records.push(record);
2866
+ }
2867
+ }
2868
+ }
2869
+ return records.sort((a, b) => b.createdAt - a.createdAt);
2870
+ }
2871
+ async findImagesByContainerId(containerId) {
2872
+ const indexPrefix = `${INDEX_BY_CONTAINER}:${containerId}`;
2873
+ const keys = await this.storage.getKeys(indexPrefix);
2874
+ const records = [];
2875
+ for (const key of keys) {
2876
+ const imageId = await this.storage.getItem(key);
2877
+ if (imageId) {
2878
+ const record = await this.findImageById(imageId);
2879
+ if (record) {
2880
+ records.push(record);
2881
+ }
2882
+ }
2883
+ }
2884
+ return records.sort((a, b) => b.createdAt - a.createdAt);
2885
+ }
2886
+ async deleteImage(imageId) {
2887
+ const record = await this.findImageById(imageId);
2888
+ await this.storage.removeItem(this.key(imageId));
2889
+ if (record) {
2890
+ await this.storage.removeItem(this.nameIndexKey(record.name, imageId));
2891
+ await this.storage.removeItem(this.containerIndexKey(record.containerId, imageId));
2892
+ }
2893
+ logger12.debug("Image deleted", { imageId });
2894
+ }
2895
+ async imageExists(imageId) {
2896
+ return await this.storage.hasItem(this.key(imageId));
2897
+ }
2898
+ async updateMetadata(imageId, metadata) {
2899
+ const record = await this.findImageById(imageId);
2900
+ if (!record) {
2901
+ throw new Error(`Image not found: ${imageId}`);
2902
+ }
2903
+ const updatedRecord = {
2904
+ ...record,
2905
+ metadata: {
2906
+ ...record.metadata,
2907
+ ...metadata
2908
+ },
2909
+ updatedAt: Date.now()
2910
+ };
2911
+ await this.storage.setItem(this.key(imageId), updatedRecord);
2912
+ logger12.debug("Image metadata updated", { imageId, metadata });
2913
+ }
2914
+ };
2915
+
2916
+ // src/internal/persistence/repository/StorageContainerRepository.ts
2917
+ import { createLogger as createLogger13 } from "@agentxjs/common";
2918
+ var logger13 = createLogger13("persistence/ContainerRepository");
2919
+ var PREFIX2 = "containers";
2920
+ var StorageContainerRepository = class {
2921
+ constructor(storage) {
2922
+ this.storage = storage;
2923
+ }
2924
+ key(containerId) {
2925
+ return `${PREFIX2}:${containerId}`;
2926
+ }
2927
+ async saveContainer(record) {
2928
+ await this.storage.setItem(this.key(record.containerId), record);
2929
+ logger13.debug("Container saved", { containerId: record.containerId });
2930
+ }
2931
+ async findContainerById(containerId) {
2932
+ const record = await this.storage.getItem(this.key(containerId));
2933
+ return record ?? null;
2934
+ }
2935
+ async findAllContainers() {
2936
+ const keys = await this.storage.getKeys(PREFIX2);
2937
+ const records = [];
2938
+ for (const key of keys) {
2939
+ const record = await this.storage.getItem(key);
2940
+ if (record) {
2941
+ records.push(record);
2942
+ }
2943
+ }
2944
+ return records.sort((a, b) => b.createdAt - a.createdAt);
2945
+ }
2946
+ async deleteContainer(containerId) {
2947
+ await this.storage.removeItem(this.key(containerId));
2948
+ logger13.debug("Container deleted", { containerId });
2949
+ }
2950
+ async containerExists(containerId) {
2951
+ return await this.storage.hasItem(this.key(containerId));
2952
+ }
2953
+ };
2954
+
2955
+ // src/internal/persistence/repository/StorageSessionRepository.ts
2956
+ import { createLogger as createLogger14 } from "@agentxjs/common";
2957
+ var logger14 = createLogger14("persistence/SessionRepository");
2958
+ var PREFIX3 = "sessions";
2959
+ var MESSAGES_PREFIX = "messages";
2960
+ var INDEX_BY_IMAGE = "idx:sessions:image";
2961
+ var INDEX_BY_CONTAINER2 = "idx:sessions:container";
2962
+ var StorageSessionRepository = class {
2963
+ constructor(storage) {
2964
+ this.storage = storage;
2965
+ }
2966
+ key(sessionId) {
2967
+ return `${PREFIX3}:${sessionId}`;
2968
+ }
2969
+ messagesKey(sessionId) {
2970
+ return `${MESSAGES_PREFIX}:${sessionId}`;
2971
+ }
2972
+ imageIndexKey(imageId, sessionId) {
2973
+ return `${INDEX_BY_IMAGE}:${imageId}:${sessionId}`;
2974
+ }
2975
+ containerIndexKey(containerId, sessionId) {
2976
+ return `${INDEX_BY_CONTAINER2}:${containerId}:${sessionId}`;
2977
+ }
2978
+ async saveSession(record) {
2979
+ await this.storage.setItem(this.key(record.sessionId), record);
2980
+ await this.storage.setItem(
2981
+ this.imageIndexKey(record.imageId, record.sessionId),
2982
+ record.sessionId
2983
+ );
2984
+ await this.storage.setItem(
2985
+ this.containerIndexKey(record.containerId, record.sessionId),
2986
+ record.sessionId
2987
+ );
2988
+ logger14.debug("Session saved", { sessionId: record.sessionId });
2989
+ }
2990
+ async findSessionById(sessionId) {
2991
+ const record = await this.storage.getItem(this.key(sessionId));
2992
+ return record ?? null;
2993
+ }
2994
+ async findSessionByImageId(imageId) {
2995
+ const indexPrefix = `${INDEX_BY_IMAGE}:${imageId}`;
2996
+ const keys = await this.storage.getKeys(indexPrefix);
2997
+ if (keys.length === 0) return null;
2998
+ const sessionId = await this.storage.getItem(keys[0]);
2999
+ if (!sessionId) return null;
3000
+ return this.findSessionById(sessionId);
3001
+ }
3002
+ async findSessionsByContainerId(containerId) {
3003
+ const indexPrefix = `${INDEX_BY_CONTAINER2}:${containerId}`;
3004
+ const keys = await this.storage.getKeys(indexPrefix);
3005
+ const records = [];
3006
+ for (const key of keys) {
3007
+ const sessionId = await this.storage.getItem(key);
3008
+ if (sessionId) {
3009
+ const record = await this.findSessionById(sessionId);
3010
+ if (record) {
3011
+ records.push(record);
3012
+ }
3013
+ }
3014
+ }
3015
+ return records.sort((a, b) => b.createdAt - a.createdAt);
3016
+ }
3017
+ async findAllSessions() {
3018
+ const keys = await this.storage.getKeys(PREFIX3);
3019
+ const records = [];
3020
+ for (const key of keys) {
3021
+ if (key.startsWith("idx:")) continue;
3022
+ const record = await this.storage.getItem(key);
3023
+ if (record) {
3024
+ records.push(record);
3025
+ }
3026
+ }
3027
+ return records.sort((a, b) => b.createdAt - a.createdAt);
3028
+ }
3029
+ async deleteSession(sessionId) {
3030
+ const record = await this.findSessionById(sessionId);
3031
+ await this.storage.removeItem(this.key(sessionId));
3032
+ await this.storage.removeItem(this.messagesKey(sessionId));
3033
+ if (record) {
3034
+ await this.storage.removeItem(this.imageIndexKey(record.imageId, sessionId));
3035
+ await this.storage.removeItem(this.containerIndexKey(record.containerId, sessionId));
3036
+ }
3037
+ logger14.debug("Session deleted", { sessionId });
3038
+ }
3039
+ async sessionExists(sessionId) {
3040
+ return await this.storage.hasItem(this.key(sessionId));
3041
+ }
3042
+ // ==================== Message Operations ====================
3043
+ async addMessage(sessionId, message) {
3044
+ const messages = await this.getMessages(sessionId);
3045
+ messages.push(message);
3046
+ await this.storage.setItem(this.messagesKey(sessionId), messages);
3047
+ logger14.debug("Message added to session", { sessionId, subtype: message.subtype });
3048
+ }
3049
+ async getMessages(sessionId) {
3050
+ const messages = await this.storage.getItem(this.messagesKey(sessionId));
3051
+ if (!messages || !Array.isArray(messages)) {
3052
+ if (messages) {
3053
+ logger14.warn("Messages data is not an array, resetting", {
3054
+ sessionId,
3055
+ type: typeof messages
3056
+ });
3057
+ }
3058
+ return [];
3059
+ }
3060
+ return messages;
3061
+ }
3062
+ async clearMessages(sessionId) {
3063
+ await this.storage.removeItem(this.messagesKey(sessionId));
3064
+ logger14.debug("Messages cleared for session", { sessionId });
3065
+ }
3066
+ };
3067
+
3068
+ // src/internal/persistence/PersistenceImpl.ts
3069
+ var logger15 = createLogger15("persistence/Persistence");
3070
+ var PersistenceImpl = class _PersistenceImpl {
3071
+ images;
3072
+ containers;
3073
+ sessions;
3074
+ storage;
3075
+ /**
3076
+ * Private constructor - use createPersistence() factory function
3077
+ */
3078
+ constructor(storage, driverName) {
3079
+ this.storage = storage;
3080
+ this.images = new StorageImageRepository(this.storage);
3081
+ this.containers = new StorageContainerRepository(this.storage);
3082
+ this.sessions = new StorageSessionRepository(this.storage);
3083
+ logger15.info("Persistence created", { driver: driverName });
3084
+ }
3085
+ /**
3086
+ * Create a PersistenceImpl instance (async factory)
3087
+ */
3088
+ static async create(config = {}) {
3089
+ const driverName = config.driver ?? "memory";
3090
+ const storage = config.storage ?? await createStorageFromConfig(config);
3091
+ return new _PersistenceImpl(storage, driverName);
3092
+ }
3093
+ /**
3094
+ * Get the underlying storage instance
3095
+ */
3096
+ getStorage() {
3097
+ return this.storage;
3098
+ }
3099
+ /**
3100
+ * Dispose and cleanup resources
3101
+ */
3102
+ async dispose() {
3103
+ await this.storage.dispose();
3104
+ logger15.info("Persistence disposed");
3105
+ }
3106
+ };
3107
+ async function createStorageFromConfig(config) {
3108
+ const driver = config.driver ?? "memory";
3109
+ switch (driver) {
3110
+ case "memory":
3111
+ return createStorage();
3112
+ case "fs": {
3113
+ const { default: fsDriver } = await import("unstorage/drivers/fs");
3114
+ return createStorage({
3115
+ driver: fsDriver({ base: config.path ?? "./data" })
3116
+ });
3117
+ }
3118
+ case "redis": {
3119
+ const { default: redisDriver } = await import("unstorage/drivers/redis");
3120
+ return createStorage({
3121
+ driver: redisDriver({ url: config.url ?? "redis://localhost:6379" })
3122
+ });
3123
+ }
3124
+ case "mongodb": {
3125
+ const { default: mongoDriver } = await import("unstorage/drivers/mongodb");
3126
+ return createStorage({
3127
+ driver: mongoDriver({
3128
+ connectionString: config.url ?? "mongodb://localhost:27017",
3129
+ databaseName: "agentx",
3130
+ collectionName: "storage"
3131
+ })
3132
+ });
3133
+ }
3134
+ case "sqlite": {
3135
+ const { default: db0Driver } = await import("unstorage/drivers/db0");
3136
+ const { createDatabase } = await import("db0");
3137
+ const { default: sqliteConnector } = await import("db0/connectors/better-sqlite3");
3138
+ const database = createDatabase(sqliteConnector({ path: config.path ?? "./data.db" }));
3139
+ return createStorage({
3140
+ driver: db0Driver({ database })
3141
+ });
3142
+ }
3143
+ case "mysql": {
3144
+ const { default: db0Driver } = await import("unstorage/drivers/db0");
3145
+ const { createDatabase } = await import("db0");
3146
+ const { default: mysqlConnector } = await import("db0/connectors/mysql2");
3147
+ const database = createDatabase(
3148
+ mysqlConnector({ uri: config.url ?? "mysql://localhost:3306/agentx" })
3149
+ );
3150
+ return createStorage({
3151
+ driver: db0Driver({ database })
3152
+ });
3153
+ }
3154
+ case "postgresql": {
3155
+ const { default: db0Driver } = await import("unstorage/drivers/db0");
3156
+ const { createDatabase } = await import("db0");
3157
+ const { default: pgConnector } = await import("db0/connectors/postgresql");
3158
+ const database = createDatabase(
3159
+ pgConnector({ connectionString: config.url ?? "postgres://localhost:5432/agentx" })
3160
+ );
3161
+ return createStorage({
3162
+ driver: db0Driver({ database })
3163
+ });
3164
+ }
3165
+ default:
3166
+ throw new Error(`Unknown storage driver: ${driver}`);
3167
+ }
3168
+ }
3169
+ async function createPersistence(config) {
3170
+ return PersistenceImpl.create(config);
3171
+ }
3172
+ export {
3173
+ createPersistence,
3174
+ createRuntime
3175
+ };
3176
+ //# sourceMappingURL=index.js.map