@agentxjs/runtime 1.8.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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: {
@@ -732,6 +719,7 @@ import { Subject as Subject2 } from "rxjs";
732
719
  import { createLogger as createLogger6 } from "@agentxjs/common";
733
720
 
734
721
  // src/environment/buildOptions.ts
722
+ import { spawn } from "child_process";
735
723
  import { createLogger as createLogger5 } from "@agentxjs/common";
736
724
 
737
725
  // src/RuntimeEnvironment.ts
@@ -791,6 +779,17 @@ function buildOptions(context, abortController) {
791
779
  options.stderr = (data) => {
792
780
  logger5.info("SDK stderr", { data: data.trim() });
793
781
  };
782
+ options.spawnClaudeCodeProcess = (spawnOptions) => {
783
+ const childProcess = spawn(process.execPath, spawnOptions.args, {
784
+ cwd: spawnOptions.cwd,
785
+ env: {
786
+ ...spawnOptions.env,
787
+ ELECTRON_RUN_AS_NODE: "1"
788
+ },
789
+ stdio: ["pipe", "pipe", "pipe"]
790
+ });
791
+ return childProcess;
792
+ };
794
793
  const claudeCodePath = RuntimeEnvironment.getClaudeCodePath();
795
794
  if (claudeCodePath) {
796
795
  options.pathToClaudeCodeExecutable = claudeCodePath;
@@ -821,6 +820,7 @@ function buildOptions(context, abortController) {
821
820
  options.permissionMode = "bypassPermissions";
822
821
  options.allowDangerouslySkipPermissions = true;
823
822
  }
823
+ options.settingSources = ["project"];
824
824
  logger5.info("SDK Options built", {
825
825
  model: options.model,
826
826
  systemPrompt: options.systemPrompt,
@@ -907,6 +907,7 @@ class SDKQueryLifecycle {
907
907
  claudeQuery = null;
908
908
  isInitialized = false;
909
909
  abortController = null;
910
+ capturedSessionId = null;
910
911
  constructor(config, callbacks = {}) {
911
912
  this.config = config;
912
913
  this.callbacks = callbacks;
@@ -914,6 +915,11 @@ class SDKQueryLifecycle {
914
915
  get initialized() {
915
916
  return this.isInitialized;
916
917
  }
918
+ async warmup() {
919
+ logger6.info("Warming up SDKQueryLifecycle");
920
+ await this.initialize();
921
+ logger6.info("SDKQueryLifecycle warmup complete");
922
+ }
917
923
  async initialize() {
918
924
  if (this.isInitialized)
919
925
  return;
@@ -987,7 +993,8 @@ class SDKQueryLifecycle {
987
993
  subtype: sdkMsg.subtype,
988
994
  sessionId: sdkMsg.session_id
989
995
  });
990
- if (sdkMsg.session_id && this.callbacks.onSessionIdCaptured) {
996
+ if (sdkMsg.session_id && this.callbacks.onSessionIdCaptured && this.capturedSessionId !== sdkMsg.session_id) {
997
+ this.capturedSessionId = sdkMsg.session_id;
991
998
  this.callbacks.onSessionIdCaptured(sdkMsg.session_id);
992
999
  }
993
1000
  if (sdkMsg.type === "stream_event") {
@@ -1063,6 +1070,9 @@ class ClaudeEffector {
1063
1070
  onListenerExit: (reason) => this.handleListenerExit(reason)
1064
1071
  });
1065
1072
  }
1073
+ async warmup() {
1074
+ await this.queryLifecycle.warmup();
1075
+ }
1066
1076
  connect(consumer) {
1067
1077
  logger7.debug("ClaudeEffector connected to SystemBusConsumer", {
1068
1078
  agentId: this.config.agentId
@@ -1215,10 +1225,31 @@ class ClaudeEnvironment {
1215
1225
  this.effector = claudeEffector;
1216
1226
  this.claudeEffector = claudeEffector;
1217
1227
  }
1228
+ async warmup() {
1229
+ await this.claudeEffector.warmup();
1230
+ }
1218
1231
  dispose() {
1219
1232
  this.claudeEffector.dispose();
1220
1233
  }
1221
1234
  }
1235
+
1236
+ // src/environment/DefaultEnvironmentFactory.ts
1237
+ var defaultEnvironmentFactory = {
1238
+ create(config) {
1239
+ return new ClaudeEnvironment({
1240
+ agentId: config.agentId,
1241
+ apiKey: config.llmConfig.apiKey,
1242
+ baseUrl: config.llmConfig.baseUrl,
1243
+ model: config.llmConfig.model,
1244
+ systemPrompt: config.systemPrompt,
1245
+ cwd: config.cwd,
1246
+ resumeSessionId: config.resumeSessionId,
1247
+ mcpServers: config.mcpServers,
1248
+ onSessionIdCaptured: config.onSessionIdCaptured
1249
+ });
1250
+ }
1251
+ };
1252
+
1222
1253
  // src/internal/RuntimeAgent.ts
1223
1254
  var logger8 = createLogger8("runtime/RuntimeAgent");
1224
1255
 
@@ -1261,11 +1292,6 @@ class BusPresenter {
1261
1292
  }
1262
1293
  };
1263
1294
  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
1295
  }
1270
1296
  convertToMessage(output) {
1271
1297
  return output.data;
@@ -1311,11 +1337,10 @@ class RuntimeAgent {
1311
1337
  this.config = config.config;
1312
1338
  this.imageRepository = config.imageRepository;
1313
1339
  const resumeSessionId = config.image.metadata?.claudeSdkSessionId;
1314
- this.environment = new ClaudeEnvironment({
1340
+ const factory = config.environmentFactory ?? defaultEnvironmentFactory;
1341
+ this.environment = factory.create({
1315
1342
  agentId: this.agentId,
1316
- apiKey: config.llmConfig.apiKey,
1317
- baseUrl: config.llmConfig.baseUrl,
1318
- model: config.llmConfig.model,
1343
+ llmConfig: config.llmConfig,
1319
1344
  systemPrompt: config.image.systemPrompt,
1320
1345
  cwd: config.sandbox.workdir.path,
1321
1346
  resumeSessionId,
@@ -1326,8 +1351,14 @@ class RuntimeAgent {
1326
1351
  });
1327
1352
  this.environment.receptor.connect(config.bus.asProducer());
1328
1353
  this.environment.effector.connect(config.bus.asConsumer());
1329
- logger8.info("ClaudeEnvironment created for agent", {
1354
+ if (this.environment.warmup) {
1355
+ this.environment.warmup().catch((err) => {
1356
+ logger8.warn("Environment warmup failed (non-fatal)", { error: err, agentId: this.agentId });
1357
+ });
1358
+ }
1359
+ logger8.info("Environment created for agent", {
1330
1360
  agentId: this.agentId,
1361
+ environmentName: this.environment.name,
1331
1362
  imageId: this.imageId,
1332
1363
  cwd: config.sandbox.workdir.path,
1333
1364
  resumeSessionId: resumeSessionId ?? "none",
@@ -1388,7 +1419,7 @@ class RuntimeAgent {
1388
1419
  if (this._lifecycle !== "running") {
1389
1420
  throw new Error(`Cannot send message to ${this._lifecycle} agent`);
1390
1421
  }
1391
- await this.interactor.receive(content, requestId || `req_${Date.now()}`);
1422
+ await this.interactor.receive(content, requestId || generateRequestId2());
1392
1423
  logger8.debug("RuntimeAgent.receive completed", { agentId: this.agentId });
1393
1424
  }
1394
1425
  interrupt(requestId) {
@@ -1775,7 +1806,8 @@ class RuntimeContainer {
1775
1806
  session,
1776
1807
  llmConfig: this.context.llmConfig,
1777
1808
  image,
1778
- imageRepository: this.context.persistence.images
1809
+ imageRepository: this.context.persistence.images,
1810
+ environmentFactory: this.context.environmentFactory
1779
1811
  });
1780
1812
  this.agents.set(agentId, agent);
1781
1813
  this.imageToAgent.set(image.imageId, agentId);
@@ -2187,13 +2219,13 @@ class CommandHandler extends BaseEventHandler {
2187
2219
  const record = await this.ops.createImage(containerId, config);
2188
2220
  this.bus.emit(createResponse("image_create_response", {
2189
2221
  requestId,
2190
- record
2222
+ record,
2223
+ __subscriptions: [record.sessionId]
2191
2224
  }));
2192
2225
  } catch (err) {
2193
2226
  this.emitError("Failed to create image", err, requestId, { containerId });
2194
2227
  this.bus.emit(createResponse("image_create_response", {
2195
2228
  requestId,
2196
- record: null,
2197
2229
  error: err instanceof Error ? err.message : String(err)
2198
2230
  }));
2199
2231
  }
@@ -2251,7 +2283,6 @@ class CommandHandler extends BaseEventHandler {
2251
2283
  this.emitError("Failed to update image", err, requestId, { imageId });
2252
2284
  this.bus.emit(createResponse("image_update_response", {
2253
2285
  requestId,
2254
- record: null,
2255
2286
  error: err instanceof Error ? err.message : String(err)
2256
2287
  }));
2257
2288
  }
@@ -2263,7 +2294,8 @@ class CommandHandler extends BaseEventHandler {
2263
2294
  const images = await this.ops.listImages(containerId);
2264
2295
  this.bus.emit(createResponse("image_list_response", {
2265
2296
  requestId,
2266
- records: images
2297
+ records: images,
2298
+ __subscriptions: images.map((img) => img.sessionId)
2267
2299
  }));
2268
2300
  } catch (err) {
2269
2301
  this.emitError("Failed to list images", err, requestId, { containerId });
@@ -2281,7 +2313,8 @@ class CommandHandler extends BaseEventHandler {
2281
2313
  const image = await this.ops.getImage(imageId);
2282
2314
  this.bus.emit(createResponse("image_get_response", {
2283
2315
  requestId,
2284
- record: image
2316
+ record: image,
2317
+ __subscriptions: image?.sessionId ? [image.sessionId] : undefined
2285
2318
  }));
2286
2319
  } catch (err) {
2287
2320
  this.emitError("Failed to get image", err, requestId, { imageId });
@@ -2344,6 +2377,7 @@ class RuntimeImpl {
2344
2377
  basePath;
2345
2378
  commandHandler;
2346
2379
  defaultAgent;
2380
+ environmentFactory;
2347
2381
  containerRegistry = new Map;
2348
2382
  constructor(config) {
2349
2383
  logger13.info("RuntimeImpl constructor start");
@@ -2351,6 +2385,7 @@ class RuntimeImpl {
2351
2385
  this.llmProvider = config.llmProvider;
2352
2386
  this.basePath = config.basePath;
2353
2387
  this.defaultAgent = config.defaultAgent;
2388
+ this.environmentFactory = config.environmentFactory;
2354
2389
  logger13.info("Creating SystemBus");
2355
2390
  this.bus = new SystemBusImpl;
2356
2391
  this.llmConfig = this.llmProvider.provide();
@@ -2662,6 +2697,7 @@ class RuntimeImpl {
2662
2697
  bus: this.bus,
2663
2698
  llmConfig: this.llmConfig,
2664
2699
  basePath: this.basePath,
2700
+ environmentFactory: this.environmentFactory,
2665
2701
  onDisposed: (containerId) => {
2666
2702
  this.containerRegistry.delete(containerId);
2667
2703
  }
@@ -2699,4 +2735,4 @@ export {
2699
2735
  RuntimeEnvironment
2700
2736
  };
2701
2737
 
2702
- //# debugId=C63126293F6B51AD64756E2164756E21
2738
+ //# debugId=D9D9B299D6CF92E064756E2164756E21