@adhdev/daemon-core 0.9.77-rc.21 → 0.9.77-rc.23

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.mjs CHANGED
@@ -1315,13 +1315,31 @@ function tryAssignQueueTask(components, meshId, nodeId, sessionId, providerType)
1315
1315
  return false;
1316
1316
  }
1317
1317
  LOG.info("MeshQueue", `Node ${nodeId} (${sessionId}) pulled task ${task.id}`);
1318
+ const mesh = getMesh(meshId);
1319
+ const node = mesh?.nodes.find((n) => n.id === nodeId);
1320
+ if (node?.daemonId && components.dispatchMeshCommand) {
1321
+ const isLocalNode = components.cliManager.adapters.has(sessionId);
1322
+ if (!isLocalNode) {
1323
+ components.dispatchMeshCommand(node.daemonId, "agent_command", {
1324
+ targetSessionId: sessionId,
1325
+ cliType: providerType,
1326
+ action: "send_chat",
1327
+ message: task.message
1328
+ }).catch((e) => {
1329
+ LOG.error("MeshQueue", `Failed to dispatch task via P2P to remote node ${nodeId}: ${e?.message}`);
1330
+ updateTaskStatus(meshId, task.id, "failed");
1331
+ });
1332
+ return true;
1333
+ }
1334
+ }
1318
1335
  components.cliManager.handleCliCommand("agent_command", {
1319
1336
  targetSessionId: sessionId,
1320
1337
  cliType: providerType,
1321
1338
  action: "send_chat",
1322
1339
  message: task.message
1323
1340
  }).catch((e) => {
1324
- LOG.error("MeshQueue", `Failed to dispatch task to node ${nodeId}: ${e?.message}`);
1341
+ LOG.error("MeshQueue", `Failed to dispatch task locally to node ${nodeId}: ${e?.message}`);
1342
+ updateTaskStatus(meshId, task.id, "failed");
1325
1343
  });
1326
1344
  return true;
1327
1345
  }
@@ -1343,6 +1361,15 @@ function triggerMeshQueue(components, meshId) {
1343
1361
  tryAssignQueueTask(components, meshId, nodeId, sessionId, providerType);
1344
1362
  }
1345
1363
  }
1364
+ for (const [key, idle] of remoteIdleSessions.entries()) {
1365
+ const node = mesh.nodes.find((n) => n.id === idle.nodeId);
1366
+ if (node) {
1367
+ const assigned = tryAssignQueueTask(components, meshId, idle.nodeId, idle.sessionId, idle.providerType);
1368
+ if (assigned) {
1369
+ remoteIdleSessions.delete(key);
1370
+ }
1371
+ }
1372
+ }
1346
1373
  }
1347
1374
  function buildMeshSystemMessage(args) {
1348
1375
  const metadata = formatCompletionMetadata(args.metadataEvent);
@@ -1406,12 +1433,26 @@ function injectMeshSystemMessage(components, args) {
1406
1433
  const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1407
1434
  const providerType = readNonEmptyString(args.metadataEvent.providerType);
1408
1435
  if (sessionId && nodeId && providerType) {
1436
+ remoteIdleSessions.set(`${nodeId}:${sessionId}`, { nodeId, sessionId, providerType });
1409
1437
  setTimeout(() => {
1410
- tryAssignQueueTask(components, args.meshId, nodeId, sessionId, providerType);
1438
+ const assigned = tryAssignQueueTask(components, args.meshId, nodeId, sessionId, providerType);
1439
+ if (assigned) {
1440
+ remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
1441
+ }
1411
1442
  }, 500);
1412
1443
  }
1444
+ } else if (args.event === "agent:generating_started") {
1445
+ const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1446
+ const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1447
+ if (sessionId && nodeId) {
1448
+ remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
1449
+ }
1413
1450
  } else if (args.event === "agent:stopped") {
1414
1451
  const sessionId = readNonEmptyString(args.metadataEvent.targetSessionId);
1452
+ const nodeId = readNonEmptyString(args.nodeId) || readNonEmptyString(args.metadataEvent.meshNodeId);
1453
+ if (sessionId && nodeId) {
1454
+ remoteIdleSessions.delete(`${nodeId}:${sessionId}`);
1455
+ }
1415
1456
  if (sessionId) {
1416
1457
  updateSessionTaskStatus(args.meshId, sessionId, "failed");
1417
1458
  }
@@ -1578,7 +1619,7 @@ function setupMeshEventForwarding(components) {
1578
1619
  });
1579
1620
  });
1580
1621
  }
1581
- var MAX_PENDING_EVENTS, pendingMeshCoordinatorEvents, MESH_COORDINATOR_EVENTS, EVENT_TO_LEDGER_KIND;
1622
+ var remoteIdleSessions, MAX_PENDING_EVENTS, pendingMeshCoordinatorEvents, MESH_COORDINATOR_EVENTS, EVENT_TO_LEDGER_KIND;
1582
1623
  var init_mesh_events = __esm({
1583
1624
  "src/mesh/mesh-events.ts"() {
1584
1625
  "use strict";
@@ -1586,6 +1627,7 @@ var init_mesh_events = __esm({
1586
1627
  init_logger();
1587
1628
  init_mesh_ledger();
1588
1629
  init_mesh_work_queue();
1630
+ remoteIdleSessions = /* @__PURE__ */ new Map();
1589
1631
  MAX_PENDING_EVENTS = 50;
1590
1632
  pendingMeshCoordinatorEvents = [];
1591
1633
  MESH_COORDINATOR_EVENTS = /* @__PURE__ */ new Set([
@@ -32117,7 +32159,8 @@ async function initDaemonComponents(config) {
32117
32159
  cdpManagers,
32118
32160
  sessionRegistry,
32119
32161
  detectedIdes: detectedIdesRef,
32120
- refreshProviderAvailability
32162
+ refreshProviderAvailability,
32163
+ dispatchMeshCommand: config.dispatchMeshCommand
32121
32164
  };
32122
32165
  setupMeshEventForwarding(components);
32123
32166
  return components;