@adhdev/daemon-core 0.9.76-rc.32 → 0.9.76-rc.33

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 CHANGED
@@ -20525,6 +20525,100 @@ function normalizeExistingPath(filePath) {
20525
20525
  }
20526
20526
  }
20527
20527
 
20528
+ // src/mesh/mesh-events.ts
20529
+ init_mesh_config();
20530
+ init_logger();
20531
+ function readNonEmptyString(value) {
20532
+ return typeof value === "string" && value.trim() ? value.trim() : "";
20533
+ }
20534
+ function formatCompletionMetadata(event) {
20535
+ const parts = [
20536
+ readNonEmptyString(event.targetSessionId) ? `session_id=${readNonEmptyString(event.targetSessionId)}` : "",
20537
+ readNonEmptyString(event.providerType) ? `provider=${readNonEmptyString(event.providerType)}` : "",
20538
+ readNonEmptyString(event.providerSessionId) ? `provider_session_id=${readNonEmptyString(event.providerSessionId)}` : ""
20539
+ ].filter(Boolean);
20540
+ return parts.length > 0 ? ` (${parts.join("; ")})` : "";
20541
+ }
20542
+ function buildMeshSystemMessage(args) {
20543
+ const metadata = formatCompletionMetadata(args.metadataEvent);
20544
+ if (args.event === "agent:generating_completed") {
20545
+ return `[System] ${args.nodeLabel} has completed its task and is now idle${metadata}. You may use mesh_read_chat to review its progress.`;
20546
+ }
20547
+ if (args.event === "agent:waiting_approval") {
20548
+ return `[System] ${args.nodeLabel} is waiting for approval to proceed${metadata}. You may use mesh_read_chat and mesh_approve to handle it.`;
20549
+ }
20550
+ return "";
20551
+ }
20552
+ function injectMeshSystemMessage(components, args) {
20553
+ const coordinatorInstances = components.instanceManager.getByCategory("cli").filter((inst) => {
20554
+ const instState = inst.getState();
20555
+ if (instState.settings?.meshCoordinatorFor !== args.meshId) return false;
20556
+ if (args.sourceInstanceId && instState.instanceId === args.sourceInstanceId) return false;
20557
+ return true;
20558
+ });
20559
+ if (coordinatorInstances.length === 0) return { success: true, forwarded: 0 };
20560
+ const messageText = buildMeshSystemMessage({
20561
+ event: args.event,
20562
+ nodeLabel: args.nodeLabel,
20563
+ metadataEvent: args.metadataEvent
20564
+ });
20565
+ if (!messageText) return { success: false, error: "unsupported mesh event" };
20566
+ for (const coord of coordinatorInstances) {
20567
+ const coordState = coord.getState();
20568
+ LOG.info("MeshEvents", `Forwarding mesh event to coordinator ${coordState.instanceId}`);
20569
+ coord.onEvent("send_message", { input: { text: messageText, textFallback: messageText } });
20570
+ }
20571
+ return { success: true, forwarded: coordinatorInstances.length };
20572
+ }
20573
+ function handleMeshForwardEvent(components, payload) {
20574
+ const eventName = readNonEmptyString(payload.event);
20575
+ if (eventName !== "agent:generating_completed" && eventName !== "agent:waiting_approval") {
20576
+ return { success: false, error: "unsupported mesh event" };
20577
+ }
20578
+ const meshId = readNonEmptyString(payload.meshId);
20579
+ if (!meshId) return { success: false, error: "meshId required" };
20580
+ const nodeId = readNonEmptyString(payload.nodeId);
20581
+ const workspace = readNonEmptyString(payload.workspace);
20582
+ const nodeLabel = nodeId ? `Node '${nodeId}'` : workspace ? `Agent at ${workspace}` : "Remote agent";
20583
+ return injectMeshSystemMessage(components, {
20584
+ meshId,
20585
+ nodeLabel,
20586
+ event: eventName,
20587
+ metadataEvent: {
20588
+ targetSessionId: readNonEmptyString(payload.targetSessionId) || readNonEmptyString(payload.sessionId),
20589
+ providerType: readNonEmptyString(payload.providerType),
20590
+ providerSessionId: readNonEmptyString(payload.providerSessionId)
20591
+ }
20592
+ });
20593
+ }
20594
+ function setupMeshEventForwarding(components) {
20595
+ components.instanceManager.onEvent((event) => {
20596
+ if (event.event !== "agent:generating_completed" && event.event !== "agent:waiting_approval") return;
20597
+ const instanceId = readNonEmptyString(event.instanceId);
20598
+ if (!instanceId) return;
20599
+ const sourceInstance = components.instanceManager.getInstance(instanceId);
20600
+ if (!sourceInstance || sourceInstance.category !== "cli") return;
20601
+ const state = sourceInstance.getState();
20602
+ const workspace = readNonEmptyString(state.workspace);
20603
+ if (!workspace) return;
20604
+ const settings = state.settings && typeof state.settings === "object" ? state.settings : {};
20605
+ const meshIdFromRuntime = readNonEmptyString(settings.meshNodeFor);
20606
+ const mesh = meshIdFromRuntime ? getMesh(meshIdFromRuntime) : getMeshByRepo(workspace);
20607
+ const meshId = meshIdFromRuntime || readNonEmptyString(mesh?.id);
20608
+ if (!meshId) return;
20609
+ const targetNode = mesh?.nodes?.find((n) => n.workspace === workspace);
20610
+ const runtimeNodeId = readNonEmptyString(settings.meshNodeId);
20611
+ const nodeLabel = targetNode ? `Node '${targetNode.id}'` : runtimeNodeId ? `Node '${runtimeNodeId}'` : `Agent at ${workspace}`;
20612
+ injectMeshSystemMessage(components, {
20613
+ meshId,
20614
+ sourceInstanceId: instanceId,
20615
+ nodeLabel,
20616
+ event: event.event,
20617
+ metadataEvent: event
20618
+ });
20619
+ });
20620
+ }
20621
+
20528
20622
  // src/status/snapshot.ts
20529
20623
  var os18 = __toESM(require("os"));
20530
20624
  init_config();
@@ -21494,6 +21588,9 @@ var DaemonCommandRouter = class {
21494
21588
  async executeDaemonCommand(cmd, args) {
21495
21589
  switch (cmd) {
21496
21590
  // ─── CLI / ACP commands ───
21591
+ case "mesh_forward_event": {
21592
+ return handleMeshForwardEvent({ instanceManager: this.deps.instanceManager }, args);
21593
+ }
21497
21594
  case "launch_cli":
21498
21595
  case "stop_cli":
21499
21596
  case "set_cli_view_mode":
@@ -30116,64 +30213,6 @@ var SessionRegistry = class {
30116
30213
  // src/boot/daemon-lifecycle.ts
30117
30214
  init_logger();
30118
30215
  init_config();
30119
-
30120
- // src/mesh/mesh-events.ts
30121
- init_mesh_config();
30122
- init_logger();
30123
- function readNonEmptyString(value) {
30124
- return typeof value === "string" && value.trim() ? value.trim() : "";
30125
- }
30126
- function formatCompletionMetadata(event) {
30127
- const parts = [
30128
- readNonEmptyString(event.targetSessionId) ? `session_id=${readNonEmptyString(event.targetSessionId)}` : "",
30129
- readNonEmptyString(event.providerType) ? `provider=${readNonEmptyString(event.providerType)}` : "",
30130
- readNonEmptyString(event.providerSessionId) ? `provider_session_id=${readNonEmptyString(event.providerSessionId)}` : ""
30131
- ].filter(Boolean);
30132
- return parts.length > 0 ? ` (${parts.join("; ")})` : "";
30133
- }
30134
- function setupMeshEventForwarding(components) {
30135
- components.instanceManager.onEvent((event) => {
30136
- if (event.event !== "agent:generating_completed" && event.event !== "agent:waiting_approval") return;
30137
- const instanceId = readNonEmptyString(event.instanceId);
30138
- if (!instanceId) return;
30139
- const sourceInstance = components.instanceManager.getInstance(instanceId);
30140
- if (!sourceInstance || sourceInstance.category !== "cli") return;
30141
- const state = sourceInstance.getState();
30142
- const workspace = readNonEmptyString(state.workspace);
30143
- if (!workspace) return;
30144
- const settings = state.settings && typeof state.settings === "object" ? state.settings : {};
30145
- const meshIdFromRuntime = readNonEmptyString(settings.meshNodeFor);
30146
- const mesh = meshIdFromRuntime ? getMesh(meshIdFromRuntime) : getMeshByRepo(workspace);
30147
- const meshId = meshIdFromRuntime || readNonEmptyString(mesh?.id);
30148
- if (!meshId) return;
30149
- const allInstances = components.instanceManager.getByCategory("cli");
30150
- const coordinatorInstances = allInstances.filter((inst) => {
30151
- const instState = inst.getState();
30152
- if (instState.settings?.meshCoordinatorFor !== meshId) return false;
30153
- if (instState.instanceId === instanceId) return false;
30154
- return true;
30155
- });
30156
- if (coordinatorInstances.length === 0) return;
30157
- const targetNode = mesh?.nodes?.find((n) => n.workspace === workspace);
30158
- const runtimeNodeId = readNonEmptyString(settings.meshNodeId);
30159
- const nodeLabel = targetNode ? `Node '${targetNode.id}'` : runtimeNodeId ? `Node '${runtimeNodeId}'` : `Agent at ${workspace}`;
30160
- const metadata = formatCompletionMetadata(event);
30161
- let messageText = "";
30162
- if (event.event === "agent:generating_completed") {
30163
- messageText = `[System] ${nodeLabel} has completed its task and is now idle${metadata}. You may use mesh_read_chat to review its progress.`;
30164
- } else if (event.event === "agent:waiting_approval") {
30165
- messageText = `[System] ${nodeLabel} is waiting for approval to proceed${metadata}. You may use mesh_read_chat and mesh_approve to handle it.`;
30166
- }
30167
- if (!messageText) return;
30168
- for (const coord of coordinatorInstances) {
30169
- const coordState = coord.getState();
30170
- LOG.info("MeshEvents", `Forwarding event from ${workspace} to coordinator ${coordState.instanceId}`);
30171
- coord.onEvent("send_message", { input: { text: messageText, textFallback: messageText } });
30172
- }
30173
- });
30174
- }
30175
-
30176
- // src/boot/daemon-lifecycle.ts
30177
30216
  async function initDaemonComponents(config) {
30178
30217
  installGlobalInterceptor();
30179
30218
  const appConfig = loadConfig();