@agentxjs/runtime 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Persistence } from "@agentxjs/types";
2
2
  import { Runtime, LLMProvider, ClaudeLLMConfig } from "@agentxjs/types/runtime";
3
- import { Environment } from "@agentxjs/types/runtime/internal";
3
+ import { EnvironmentFactory } from "@agentxjs/types/runtime/internal";
4
4
  import { AgentDefinition } from "@agentxjs/types/agentx";
5
5
  /**
6
6
  * Runtime configuration
@@ -20,10 +20,10 @@ interface RuntimeConfig {
20
20
  */
21
21
  basePath: string;
22
22
  /**
23
- * Optional custom environment (for testing)
24
- * If not provided, ClaudeEnvironment will be created from llmProvider
23
+ * Optional environment factory for dependency injection (e.g., mock for testing)
24
+ * If not provided, ClaudeEnvironment will be created by default
25
25
  */
26
- environment?: Environment;
26
+ environmentFactory?: EnvironmentFactory;
27
27
  /**
28
28
  * Default agent definition
29
29
  * Used as base configuration when creating new images
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/internal/SystemBusImpl.ts
2
2
  import { Subject } from "rxjs";
3
- import { createLogger } from "@agentxjs/common";
3
+ import { createLogger, generateRequestId } from "@agentxjs/common";
4
4
  var logger = createLogger("runtime/SystemBusImpl");
5
5
 
6
6
  class SystemBusImpl {
@@ -73,7 +73,7 @@ class SystemBusImpl {
73
73
  }
74
74
  request(type, data, timeout = 30000) {
75
75
  return new Promise((resolve, reject) => {
76
- const requestId = `req_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
76
+ const requestId = generateRequestId();
77
77
  const responseType = type.replace("_request", "_response");
78
78
  const timer = setTimeout(() => {
79
79
  unsubscribe();
@@ -373,8 +373,7 @@ class AgentInteractor {
373
373
  category: "message",
374
374
  intent: "request",
375
375
  requestId,
376
- context: eventContext,
377
- broadcastable: false
376
+ context: eventContext
378
377
  });
379
378
  logger3.info("user_message event emitted to bus", {
380
379
  messageId: userMessage.id,
@@ -403,14 +402,13 @@ class AgentInteractor {
403
402
  category: "action",
404
403
  intent: "request",
405
404
  requestId,
406
- context: eventContext,
407
- broadcastable: false
405
+ context: eventContext
408
406
  });
409
407
  }
410
408
  }
411
409
  // src/internal/RuntimeAgent.ts
412
410
  import { createAgent } from "@agentxjs/agent";
413
- import { createLogger as createLogger8 } from "@agentxjs/common";
411
+ import { createLogger as createLogger8, generateRequestId as generateRequestId2 } from "@agentxjs/common";
414
412
 
415
413
  // src/environment/ClaudeReceptor.ts
416
414
  import { createLogger as createLogger4 } from "@agentxjs/common";
@@ -443,7 +441,6 @@ class ClaudeReceptor {
443
441
  source: "environment",
444
442
  category: "stream",
445
443
  intent: "notification",
446
- broadcastable: false,
447
444
  requestId: eventMeta?.requestId,
448
445
  context: eventMeta?.context,
449
446
  data: { reason }
@@ -457,7 +454,6 @@ class ClaudeReceptor {
457
454
  source: "environment",
458
455
  category: "stream",
459
456
  intent: "notification",
460
- broadcastable: false,
461
457
  requestId: eventMeta?.requestId,
462
458
  context: eventMeta?.context,
463
459
  data: { message, errorCode }
@@ -478,7 +474,6 @@ class ClaudeReceptor {
478
474
  source: "environment",
479
475
  category: "stream",
480
476
  intent: "notification",
481
- broadcastable: false,
482
477
  requestId,
483
478
  context,
484
479
  data: {
@@ -509,7 +504,6 @@ class ClaudeReceptor {
509
504
  source: "environment",
510
505
  category: "stream",
511
506
  intent: "notification",
512
- broadcastable: false,
513
507
  requestId,
514
508
  context,
515
509
  data: {
@@ -532,7 +526,6 @@ class ClaudeReceptor {
532
526
  source: "environment",
533
527
  category: "stream",
534
528
  intent: "notification",
535
- broadcastable: false,
536
529
  index: event.index,
537
530
  requestId,
538
531
  context,
@@ -548,7 +541,6 @@ class ClaudeReceptor {
548
541
  source: "environment",
549
542
  category: "stream",
550
543
  intent: "notification",
551
- broadcastable: false,
552
544
  index: event.index,
553
545
  requestId,
554
546
  context,
@@ -569,7 +561,6 @@ class ClaudeReceptor {
569
561
  source: "environment",
570
562
  category: "stream",
571
563
  intent: "notification",
572
- broadcastable: false,
573
564
  requestId,
574
565
  context,
575
566
  data: { text: delta.text || "" }
@@ -581,7 +572,6 @@ class ClaudeReceptor {
581
572
  source: "environment",
582
573
  category: "stream",
583
574
  intent: "notification",
584
- broadcastable: false,
585
575
  index: this.blockContext.currentBlockIndex,
586
576
  requestId,
587
577
  context,
@@ -598,7 +588,6 @@ class ClaudeReceptor {
598
588
  source: "environment",
599
589
  category: "stream",
600
590
  intent: "notification",
601
- broadcastable: false,
602
591
  index: this.blockContext.currentBlockIndex,
603
592
  requestId,
604
593
  context,
@@ -611,7 +600,6 @@ class ClaudeReceptor {
611
600
  source: "environment",
612
601
  category: "stream",
613
602
  intent: "notification",
614
- broadcastable: false,
615
603
  index: this.blockContext.currentBlockIndex,
616
604
  requestId,
617
605
  context,
@@ -637,7 +625,6 @@ class ClaudeReceptor {
637
625
  source: "environment",
638
626
  category: "stream",
639
627
  intent: "notification",
640
- broadcastable: false,
641
628
  requestId,
642
629
  context,
643
630
  data: {
@@ -907,6 +894,7 @@ class SDKQueryLifecycle {
907
894
  claudeQuery = null;
908
895
  isInitialized = false;
909
896
  abortController = null;
897
+ capturedSessionId = null;
910
898
  constructor(config, callbacks = {}) {
911
899
  this.config = config;
912
900
  this.callbacks = callbacks;
@@ -914,6 +902,11 @@ class SDKQueryLifecycle {
914
902
  get initialized() {
915
903
  return this.isInitialized;
916
904
  }
905
+ async warmup() {
906
+ logger6.info("Warming up SDKQueryLifecycle");
907
+ await this.initialize();
908
+ logger6.info("SDKQueryLifecycle warmup complete");
909
+ }
917
910
  async initialize() {
918
911
  if (this.isInitialized)
919
912
  return;
@@ -987,7 +980,8 @@ class SDKQueryLifecycle {
987
980
  subtype: sdkMsg.subtype,
988
981
  sessionId: sdkMsg.session_id
989
982
  });
990
- if (sdkMsg.session_id && this.callbacks.onSessionIdCaptured) {
983
+ if (sdkMsg.session_id && this.callbacks.onSessionIdCaptured && this.capturedSessionId !== sdkMsg.session_id) {
984
+ this.capturedSessionId = sdkMsg.session_id;
991
985
  this.callbacks.onSessionIdCaptured(sdkMsg.session_id);
992
986
  }
993
987
  if (sdkMsg.type === "stream_event") {
@@ -1063,6 +1057,9 @@ class ClaudeEffector {
1063
1057
  onListenerExit: (reason) => this.handleListenerExit(reason)
1064
1058
  });
1065
1059
  }
1060
+ async warmup() {
1061
+ await this.queryLifecycle.warmup();
1062
+ }
1066
1063
  connect(consumer) {
1067
1064
  logger7.debug("ClaudeEffector connected to SystemBusConsumer", {
1068
1065
  agentId: this.config.agentId
@@ -1215,10 +1212,31 @@ class ClaudeEnvironment {
1215
1212
  this.effector = claudeEffector;
1216
1213
  this.claudeEffector = claudeEffector;
1217
1214
  }
1215
+ async warmup() {
1216
+ await this.claudeEffector.warmup();
1217
+ }
1218
1218
  dispose() {
1219
1219
  this.claudeEffector.dispose();
1220
1220
  }
1221
1221
  }
1222
+
1223
+ // src/environment/DefaultEnvironmentFactory.ts
1224
+ var defaultEnvironmentFactory = {
1225
+ create(config) {
1226
+ return new ClaudeEnvironment({
1227
+ agentId: config.agentId,
1228
+ apiKey: config.llmConfig.apiKey,
1229
+ baseUrl: config.llmConfig.baseUrl,
1230
+ model: config.llmConfig.model,
1231
+ systemPrompt: config.systemPrompt,
1232
+ cwd: config.cwd,
1233
+ resumeSessionId: config.resumeSessionId,
1234
+ mcpServers: config.mcpServers,
1235
+ onSessionIdCaptured: config.onSessionIdCaptured
1236
+ });
1237
+ }
1238
+ };
1239
+
1222
1240
  // src/internal/RuntimeAgent.ts
1223
1241
  var logger8 = createLogger8("runtime/RuntimeAgent");
1224
1242
 
@@ -1261,11 +1279,6 @@ class BusPresenter {
1261
1279
  }
1262
1280
  };
1263
1281
  this.producer.emit(systemEvent);
1264
- if (category === "message") {
1265
- this.session.addMessage(data).catch((err) => {
1266
- logger8.error("Failed to persist message", { error: err, messageType: output.type });
1267
- });
1268
- }
1269
1282
  }
1270
1283
  convertToMessage(output) {
1271
1284
  return output.data;
@@ -1311,11 +1324,10 @@ class RuntimeAgent {
1311
1324
  this.config = config.config;
1312
1325
  this.imageRepository = config.imageRepository;
1313
1326
  const resumeSessionId = config.image.metadata?.claudeSdkSessionId;
1314
- this.environment = new ClaudeEnvironment({
1327
+ const factory = config.environmentFactory ?? defaultEnvironmentFactory;
1328
+ this.environment = factory.create({
1315
1329
  agentId: this.agentId,
1316
- apiKey: config.llmConfig.apiKey,
1317
- baseUrl: config.llmConfig.baseUrl,
1318
- model: config.llmConfig.model,
1330
+ llmConfig: config.llmConfig,
1319
1331
  systemPrompt: config.image.systemPrompt,
1320
1332
  cwd: config.sandbox.workdir.path,
1321
1333
  resumeSessionId,
@@ -1326,8 +1338,14 @@ class RuntimeAgent {
1326
1338
  });
1327
1339
  this.environment.receptor.connect(config.bus.asProducer());
1328
1340
  this.environment.effector.connect(config.bus.asConsumer());
1329
- logger8.info("ClaudeEnvironment created for agent", {
1341
+ if (this.environment.warmup) {
1342
+ this.environment.warmup().catch((err) => {
1343
+ logger8.warn("Environment warmup failed (non-fatal)", { error: err, agentId: this.agentId });
1344
+ });
1345
+ }
1346
+ logger8.info("Environment created for agent", {
1330
1347
  agentId: this.agentId,
1348
+ environmentName: this.environment.name,
1331
1349
  imageId: this.imageId,
1332
1350
  cwd: config.sandbox.workdir.path,
1333
1351
  resumeSessionId: resumeSessionId ?? "none",
@@ -1388,7 +1406,7 @@ class RuntimeAgent {
1388
1406
  if (this._lifecycle !== "running") {
1389
1407
  throw new Error(`Cannot send message to ${this._lifecycle} agent`);
1390
1408
  }
1391
- await this.interactor.receive(content, requestId || `req_${Date.now()}`);
1409
+ await this.interactor.receive(content, requestId || generateRequestId2());
1392
1410
  logger8.debug("RuntimeAgent.receive completed", { agentId: this.agentId });
1393
1411
  }
1394
1412
  interrupt(requestId) {
@@ -1775,7 +1793,8 @@ class RuntimeContainer {
1775
1793
  session,
1776
1794
  llmConfig: this.context.llmConfig,
1777
1795
  image,
1778
- imageRepository: this.context.persistence.images
1796
+ imageRepository: this.context.persistence.images,
1797
+ environmentFactory: this.context.environmentFactory
1779
1798
  });
1780
1799
  this.agents.set(agentId, agent);
1781
1800
  this.imageToAgent.set(image.imageId, agentId);
@@ -2187,13 +2206,13 @@ class CommandHandler extends BaseEventHandler {
2187
2206
  const record = await this.ops.createImage(containerId, config);
2188
2207
  this.bus.emit(createResponse("image_create_response", {
2189
2208
  requestId,
2190
- record
2209
+ record,
2210
+ __subscriptions: [record.sessionId]
2191
2211
  }));
2192
2212
  } catch (err) {
2193
2213
  this.emitError("Failed to create image", err, requestId, { containerId });
2194
2214
  this.bus.emit(createResponse("image_create_response", {
2195
2215
  requestId,
2196
- record: null,
2197
2216
  error: err instanceof Error ? err.message : String(err)
2198
2217
  }));
2199
2218
  }
@@ -2251,7 +2270,6 @@ class CommandHandler extends BaseEventHandler {
2251
2270
  this.emitError("Failed to update image", err, requestId, { imageId });
2252
2271
  this.bus.emit(createResponse("image_update_response", {
2253
2272
  requestId,
2254
- record: null,
2255
2273
  error: err instanceof Error ? err.message : String(err)
2256
2274
  }));
2257
2275
  }
@@ -2263,7 +2281,8 @@ class CommandHandler extends BaseEventHandler {
2263
2281
  const images = await this.ops.listImages(containerId);
2264
2282
  this.bus.emit(createResponse("image_list_response", {
2265
2283
  requestId,
2266
- records: images
2284
+ records: images,
2285
+ __subscriptions: images.map((img) => img.sessionId)
2267
2286
  }));
2268
2287
  } catch (err) {
2269
2288
  this.emitError("Failed to list images", err, requestId, { containerId });
@@ -2281,7 +2300,8 @@ class CommandHandler extends BaseEventHandler {
2281
2300
  const image = await this.ops.getImage(imageId);
2282
2301
  this.bus.emit(createResponse("image_get_response", {
2283
2302
  requestId,
2284
- record: image
2303
+ record: image,
2304
+ __subscriptions: image?.sessionId ? [image.sessionId] : undefined
2285
2305
  }));
2286
2306
  } catch (err) {
2287
2307
  this.emitError("Failed to get image", err, requestId, { imageId });
@@ -2344,6 +2364,7 @@ class RuntimeImpl {
2344
2364
  basePath;
2345
2365
  commandHandler;
2346
2366
  defaultAgent;
2367
+ environmentFactory;
2347
2368
  containerRegistry = new Map;
2348
2369
  constructor(config) {
2349
2370
  logger13.info("RuntimeImpl constructor start");
@@ -2351,6 +2372,7 @@ class RuntimeImpl {
2351
2372
  this.llmProvider = config.llmProvider;
2352
2373
  this.basePath = config.basePath;
2353
2374
  this.defaultAgent = config.defaultAgent;
2375
+ this.environmentFactory = config.environmentFactory;
2354
2376
  logger13.info("Creating SystemBus");
2355
2377
  this.bus = new SystemBusImpl;
2356
2378
  this.llmConfig = this.llmProvider.provide();
@@ -2662,6 +2684,7 @@ class RuntimeImpl {
2662
2684
  bus: this.bus,
2663
2685
  llmConfig: this.llmConfig,
2664
2686
  basePath: this.basePath,
2687
+ environmentFactory: this.environmentFactory,
2665
2688
  onDisposed: (containerId) => {
2666
2689
  this.containerRegistry.delete(containerId);
2667
2690
  }
@@ -2699,4 +2722,4 @@ export {
2699
2722
  RuntimeEnvironment
2700
2723
  };
2701
2724
 
2702
- //# debugId=C63126293F6B51AD64756E2164756E21
2725
+ //# debugId=D5CF113E4C388AEF64756E2164756E21