@adhdev/daemon-core 0.7.4 → 0.7.6

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.mts CHANGED
@@ -631,6 +631,7 @@ declare class ProviderInstanceManager {
631
631
  onEvent(listener: (event: ProviderEvent & {
632
632
  providerType: string;
633
633
  }) => void): void;
634
+ private emitPendingEvents;
634
635
  /**
635
636
  * Forward event to specific Instance
636
637
  */
@@ -965,6 +966,8 @@ declare class DaemonCommandHandler implements CommandHelpers {
965
966
  private resolveRoute;
966
967
  /** Extract CDP scope key from target session or explicit ideType */
967
968
  private extractIdeType;
969
+ private logCommandStart;
970
+ private logCommandEnd;
968
971
  setAgentStreamManager(manager: DaemonAgentStreamManager): void;
969
972
  handle(cmd: string, args: any): Promise<CommandResult$1>;
970
973
  private dispatch;
@@ -1050,6 +1053,10 @@ declare class ExtensionProviderInstance implements ProviderInstance {
1050
1053
  private monitor;
1051
1054
  private instanceId;
1052
1055
  private ideType;
1056
+ private chatId;
1057
+ private chatTitle;
1058
+ private agentName;
1059
+ private extensionId;
1053
1060
  constructor(provider: ProviderModule);
1054
1061
  init(context: InstanceContext): Promise<void>;
1055
1062
  onTick(): Promise<void>;
@@ -1061,6 +1068,7 @@ declare class ExtensionProviderInstance implements ProviderInstance {
1061
1068
  private detectTransition;
1062
1069
  private pushEvent;
1063
1070
  private flushEvents;
1071
+ private resolveChatTitle;
1064
1072
  }
1065
1073
 
1066
1074
  /**
package/dist/index.d.ts CHANGED
@@ -631,6 +631,7 @@ declare class ProviderInstanceManager {
631
631
  onEvent(listener: (event: ProviderEvent & {
632
632
  providerType: string;
633
633
  }) => void): void;
634
+ private emitPendingEvents;
634
635
  /**
635
636
  * Forward event to specific Instance
636
637
  */
@@ -965,6 +966,8 @@ declare class DaemonCommandHandler implements CommandHelpers {
965
966
  private resolveRoute;
966
967
  /** Extract CDP scope key from target session or explicit ideType */
967
968
  private extractIdeType;
969
+ private logCommandStart;
970
+ private logCommandEnd;
968
971
  setAgentStreamManager(manager: DaemonAgentStreamManager): void;
969
972
  handle(cmd: string, args: any): Promise<CommandResult$1>;
970
973
  private dispatch;
@@ -1050,6 +1053,10 @@ declare class ExtensionProviderInstance implements ProviderInstance {
1050
1053
  private monitor;
1051
1054
  private instanceId;
1052
1055
  private ideType;
1056
+ private chatId;
1057
+ private chatTitle;
1058
+ private agentName;
1059
+ private extensionId;
1053
1060
  constructor(provider: ProviderModule);
1054
1061
  init(context: InstanceContext): Promise<void>;
1055
1062
  onTick(): Promise<void>;
@@ -1061,6 +1068,7 @@ declare class ExtensionProviderInstance implements ProviderInstance {
1061
1068
  private detectTransition;
1062
1069
  private pushEvent;
1063
1070
  private flushEvents;
1071
+ private resolveChatTitle;
1064
1072
  }
1065
1073
 
1066
1074
  /**
package/dist/index.js CHANGED
@@ -3486,6 +3486,10 @@ var ExtensionProviderInstance = class {
3486
3486
  // meta
3487
3487
  instanceId;
3488
3488
  ideType = "";
3489
+ chatId = null;
3490
+ chatTitle = null;
3491
+ agentName = "";
3492
+ extensionId = "";
3489
3493
  constructor(provider) {
3490
3494
  this.type = provider.type;
3491
3495
  this.provider = provider;
@@ -3512,8 +3516,8 @@ var ExtensionProviderInstance = class {
3512
3516
  category: "extension",
3513
3517
  status: this.currentStatus,
3514
3518
  activeChat: this.messages.length > 0 ? {
3515
- id: `${this.type}_session`,
3516
- title: this.provider.name,
3519
+ id: this.chatId || this.instanceId,
3520
+ title: this.chatTitle || this.agentName || this.provider.name,
3517
3521
  status: this.currentStatus,
3518
3522
  messages: this.messages,
3519
3523
  activeModal: this.activeModal,
@@ -3535,6 +3539,10 @@ var ExtensionProviderInstance = class {
3535
3539
  if (data?.activeModal !== void 0) this.activeModal = data.activeModal;
3536
3540
  if (data?.model) this.currentModel = data.model;
3537
3541
  if (data?.mode) this.currentMode = data.mode;
3542
+ if (typeof data?.sessionId === "string" && data.sessionId.trim()) this.chatId = data.sessionId;
3543
+ if (typeof data?.title === "string" && data.title.trim()) this.chatTitle = data.title;
3544
+ if (typeof data?.agentName === "string" && data.agentName.trim()) this.agentName = data.agentName;
3545
+ if (typeof data?.extensionId === "string" && data.extensionId.trim()) this.extensionId = data.extensionId;
3538
3546
  if (data?.status) {
3539
3547
  const newStatus = data.status;
3540
3548
  this.detectTransition(newStatus, data);
@@ -3554,11 +3562,6 @@ var ExtensionProviderInstance = class {
3554
3562
  return this.instanceId;
3555
3563
  }
3556
3564
  // ─── status transition detect ──────────────────────────────
3557
- // NOTE: Extension transitions are TRACKED but NOT emitted as events.
3558
- // The parent IdeProviderInstance already emits identical events
3559
- // (generating_started, generating_completed, waiting_approval)
3560
- // via its own detectAgentTransitions(). Emitting here would cause
3561
- // duplicate toasts with slightly different content.
3562
3565
  detectTransition(newStatus, data) {
3563
3566
  const now = Date.now();
3564
3567
  const agentStatus = newStatus === "streaming" || newStatus === "generating" ? "generating" : newStatus === "waiting_approval" ? "waiting_approval" : "idle";
@@ -3567,7 +3570,40 @@ var ExtensionProviderInstance = class {
3567
3570
  if (agentStatus !== this.lastAgentStatus) {
3568
3571
  if (this.lastAgentStatus === "idle" && agentStatus === "generating") {
3569
3572
  this.generatingStartedAt = now;
3573
+ this.pushEvent({
3574
+ event: "agent:generating_started",
3575
+ chatTitle: this.resolveChatTitle(data),
3576
+ timestamp: now,
3577
+ ideType: this.ideType || this.type,
3578
+ agentType: this.type,
3579
+ agentName: this.agentName || this.provider.name,
3580
+ extensionId: this.extensionId || this.type
3581
+ });
3582
+ } else if (agentStatus === "waiting_approval") {
3583
+ if (!this.generatingStartedAt) this.generatingStartedAt = now;
3584
+ this.pushEvent({
3585
+ event: "agent:waiting_approval",
3586
+ chatTitle: this.resolveChatTitle(data),
3587
+ timestamp: now,
3588
+ ideType: this.ideType || this.type,
3589
+ agentType: this.type,
3590
+ agentName: this.agentName || this.provider.name,
3591
+ extensionId: this.extensionId || this.type,
3592
+ modalMessage: data?.activeModal?.message,
3593
+ modalButtons: data?.activeModal?.buttons
3594
+ });
3570
3595
  } else if (agentStatus === "idle" && (this.lastAgentStatus === "generating" || this.lastAgentStatus === "waiting_approval")) {
3596
+ const duration = this.generatingStartedAt ? Math.round((now - this.generatingStartedAt) / 1e3) : 0;
3597
+ this.pushEvent({
3598
+ event: "agent:generating_completed",
3599
+ chatTitle: this.resolveChatTitle(data),
3600
+ duration,
3601
+ timestamp: now,
3602
+ ideType: this.ideType || this.type,
3603
+ agentType: this.type,
3604
+ agentName: this.agentName || this.provider.name,
3605
+ extensionId: this.extensionId || this.type
3606
+ });
3571
3607
  this.generatingStartedAt = 0;
3572
3608
  }
3573
3609
  this.lastAgentStatus = agentStatus;
@@ -3587,6 +3623,10 @@ var ExtensionProviderInstance = class {
3587
3623
  this.events = [];
3588
3624
  return events;
3589
3625
  }
3626
+ resolveChatTitle(data) {
3627
+ const title = typeof data?.title === "string" && data.title.trim() ? data.title.trim() : this.chatTitle;
3628
+ return title || this.agentName || this.provider.name;
3629
+ }
3590
3630
  };
3591
3631
 
3592
3632
  // src/config/chat-history.ts
@@ -4314,7 +4354,7 @@ var DaemonCdpScanner = class {
4314
4354
  if (!manager) return;
4315
4355
  registerExtensionProviders(this.ctx.providerLoader, manager, ide);
4316
4356
  this.ctx.cdpManagers.set(ide, manager);
4317
- LOG.info("CDP", `Connected: ${ide} (port ${port})`);
4357
+ LOG.info("IDE", `Attached: ${ide} (port ${port})`);
4318
4358
  await setupIdeInstance(this.ctx, { ideType: ide, manager });
4319
4359
  this.opts.onConnected?.(ide, ide, manager);
4320
4360
  }
@@ -4347,7 +4387,7 @@ var DaemonCdpScanner = class {
4347
4387
  );
4348
4388
  if (!manager) continue;
4349
4389
  this.ctx.cdpManagers.set(managerKey, manager);
4350
- LOG.info("CDP", `Connected: ${managerKey} (port ${port}, page "${target.title}")`);
4390
+ LOG.info("IDE", `Attached window: ${managerKey} (port ${port}, page "${target.title}")`);
4351
4391
  await setupIdeInstance(this.ctx, {
4352
4392
  ideType: ide,
4353
4393
  manager,
@@ -4392,9 +4432,9 @@ var DaemonCdpInitializer = class {
4392
4432
  await this.connectIdePort(port, ide);
4393
4433
  }
4394
4434
  if (cdpManagers.size > 0) {
4395
- LOG.info("CDP", `${cdpManagers.size} IDE(s) connected: ${[...cdpManagers.entries()].map(([k, m]) => `${k}:${m.getPort()}`).join(", ")}`);
4435
+ LOG.info("IDE", `${cdpManagers.size} IDE window(s) attached: ${[...cdpManagers.entries()].map(([k, m]) => `${k}:${m.getPort()}`).join(", ")}`);
4396
4436
  } else {
4397
- LOG.warn("CDP", `No IDEs connected \u2014 tried: ${filtered.map((p) => `${p.ide}:${p.port}`).join(", ")}`);
4437
+ LOG.warn("IDE", `No IDE windows attached \u2014 tried: ${filtered.map((p) => `${p.ide}:${p.port}`).join(", ")}`);
4398
4438
  }
4399
4439
  }
4400
4440
  // ─── Per-port connection (multi-window aware) ───
@@ -4420,7 +4460,7 @@ var DaemonCdpInitializer = class {
4420
4460
  if (connected) {
4421
4461
  registerExtensionProviders(providerLoader, manager, ide);
4422
4462
  cdpManagers.set(ide, manager);
4423
- LOG.info("CDP", `Connected: ${ide} (port ${port})`);
4463
+ LOG.info("IDE", `Attached: ${ide} (port ${port})`);
4424
4464
  await this.config.onConnected?.(ide, manager, ide);
4425
4465
  }
4426
4466
  return;
@@ -4453,7 +4493,7 @@ var DaemonCdpInitializer = class {
4453
4493
  if (connected) {
4454
4494
  registerExtensionProviders(providerLoader, manager, ide);
4455
4495
  cdpManagers.set(managerKey, manager);
4456
- LOG.info("CDP", `Connected: ${managerKey} (port ${port}${targets.length > 1 ? `, page "${target.title}"` : ""})`);
4496
+ LOG.info("IDE", `Attached window: ${managerKey} (port ${port}${targets.length > 1 ? `, page "${target.title}"` : ""})`);
4457
4497
  await this.config.onConnected?.(ide, manager, managerKey);
4458
4498
  }
4459
4499
  }
@@ -4482,7 +4522,7 @@ var DaemonCdpInitializer = class {
4482
4522
  } catch {
4483
4523
  }
4484
4524
  this.config.cdpManagers.delete(key);
4485
- LOG.info("CDP", `Removed stale manager: ${key} (${reason})`);
4525
+ LOG.info("IDE", `Detached window: ${key} (${reason})`);
4486
4526
  await this.config.onDisconnected?.(ide, manager, key, reason);
4487
4527
  }
4488
4528
  }
@@ -5878,7 +5918,6 @@ function handleSetProviderSetting(h, args) {
5878
5918
  }
5879
5919
  async function handleExtensionScript(h, args, scriptName) {
5880
5920
  const { agentType, ideType } = args || {};
5881
- LOG.info("Command", `[ExtScript] ${scriptName} agentType=${agentType} ideType=${ideType} session=${h.currentSession?.sessionId || ""}`);
5882
5921
  if (!agentType) return { success: false, error: "agentType is required" };
5883
5922
  const loader = h.ctx.providerLoader;
5884
5923
  if (!loader) return { success: false, error: "ProviderLoader not initialized" };
@@ -6092,6 +6131,83 @@ function handleWorkspaceSetDefault(args) {
6092
6131
 
6093
6132
  // src/commands/handler.ts
6094
6133
  init_workspaces();
6134
+ var COMMAND_DEBUG_LEVELS = /* @__PURE__ */ new Set([
6135
+ "pty_input",
6136
+ "pty_resize",
6137
+ "cdp_eval",
6138
+ "cdp_batch",
6139
+ "cdp_dom_query",
6140
+ "cdp_dom_dump",
6141
+ "cdp_dom_debug"
6142
+ ]);
6143
+ function logAtLevel(level, category, message) {
6144
+ switch (level) {
6145
+ case "debug":
6146
+ LOG.debug(category, message);
6147
+ return;
6148
+ case "warn":
6149
+ LOG.warn(category, message);
6150
+ return;
6151
+ case "error":
6152
+ LOG.error(category, message);
6153
+ return;
6154
+ default:
6155
+ LOG.info(category, message);
6156
+ }
6157
+ }
6158
+ function getCommandLogLevel(cmd) {
6159
+ return COMMAND_DEBUG_LEVELS.has(cmd) ? "debug" : "info";
6160
+ }
6161
+ function summarizeLogValue(value) {
6162
+ if (value === null) return "null";
6163
+ if (value === void 0) return "undefined";
6164
+ if (typeof value === "string") {
6165
+ const normalized = value.replace(/\s+/g, " ").trim();
6166
+ if (!normalized) return '""';
6167
+ if (normalized.length <= 80) return JSON.stringify(normalized);
6168
+ return `${JSON.stringify(normalized.slice(0, 80))}\u2026(${normalized.length} chars)`;
6169
+ }
6170
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
6171
+ if (Array.isArray(value)) return `[${value.length} items]`;
6172
+ if (typeof value === "object") return "{...}";
6173
+ return String(value);
6174
+ }
6175
+ function summarizeCommandArgs(args) {
6176
+ if (!args || typeof args !== "object") return "-";
6177
+ const preferredKeys = [
6178
+ "targetSessionId",
6179
+ "providerType",
6180
+ "agentType",
6181
+ "ideType",
6182
+ "model",
6183
+ "mode",
6184
+ "action",
6185
+ "button",
6186
+ "key",
6187
+ "force",
6188
+ "offset",
6189
+ "limit",
6190
+ "cols",
6191
+ "rows",
6192
+ "path",
6193
+ "command",
6194
+ "commandId",
6195
+ "workspace",
6196
+ "dir",
6197
+ "url",
6198
+ "text",
6199
+ "message",
6200
+ "data",
6201
+ "value"
6202
+ ];
6203
+ const entries = [];
6204
+ for (const key of preferredKeys) {
6205
+ if (!(key in args) || args[key] === void 0) continue;
6206
+ const value = key === "text" || key === "message" ? `${String(args[key] || "").length} chars` : key === "data" ? `${String(args[key] || "").length} chars` : summarizeLogValue(args[key]);
6207
+ entries.push(`${key}=${value}`);
6208
+ }
6209
+ return entries.length ? entries.join(" ") : "{...}";
6210
+ }
6095
6211
  var DaemonCommandHandler = class {
6096
6212
  _ctx;
6097
6213
  _agentStream = null;
@@ -6239,23 +6355,54 @@ var DaemonCommandHandler = class {
6239
6355
  }
6240
6356
  return void 0;
6241
6357
  }
6358
+ logCommandStart(cmd, args) {
6359
+ const routeBits = [
6360
+ this._currentRoute.session?.sessionId ? `session=${this._currentRoute.session.sessionId}` : "",
6361
+ this._currentRoute.managerKey ? `manager=${this._currentRoute.managerKey}` : "",
6362
+ this._currentRoute.providerType ? `provider=${this._currentRoute.providerType}` : ""
6363
+ ].filter(Boolean).join(" ");
6364
+ const summary = summarizeCommandArgs(args);
6365
+ logAtLevel(
6366
+ getCommandLogLevel(cmd),
6367
+ "Command",
6368
+ `[${cmd}] start${routeBits ? ` ${routeBits}` : ""} args=${summary}`
6369
+ );
6370
+ }
6371
+ logCommandEnd(cmd, result, startedAt) {
6372
+ const durationMs = Date.now() - startedAt;
6373
+ const parts = [`[${cmd}] end`, `success=${result.success}`, `duration=${durationMs}ms`];
6374
+ if (typeof result.error === "string" && result.error) {
6375
+ parts.push(`error=${JSON.stringify(result.error)}`);
6376
+ }
6377
+ const level = result.success ? getCommandLogLevel(cmd) : "warn";
6378
+ logAtLevel(level, "Command", parts.join(" "));
6379
+ }
6242
6380
  setAgentStreamManager(manager) {
6243
6381
  this._agentStream = manager;
6244
6382
  }
6245
6383
  // ─── Command Dispatcher ──────────────────────────
6246
6384
  async handle(cmd, args) {
6247
6385
  this._currentRoute = this.resolveRoute(args);
6386
+ const startedAt = Date.now();
6387
+ this.logCommandStart(cmd, args);
6388
+ let result;
6248
6389
  if (!this._currentRoute.session && !this._currentRoute.managerKey && !this._currentRoute.providerType) {
6249
6390
  const cdpCommands = ["send_chat", "read_chat", "list_chats", "new_chat", "switch_chat", "set_mode", "change_model", "set_thought_level", "resolve_action"];
6250
6391
  if (cdpCommands.includes(cmd)) {
6251
- return { success: false, error: "No targetSessionId specified \u2014 cannot route command" };
6392
+ result = { success: false, error: "No targetSessionId specified \u2014 cannot route command" };
6393
+ this.logCommandEnd(cmd, result, startedAt);
6394
+ return result;
6252
6395
  }
6253
6396
  }
6254
6397
  try {
6255
- return await this.dispatch(cmd, args);
6398
+ result = await this.dispatch(cmd, args);
6399
+ this.logCommandEnd(cmd, result, startedAt);
6400
+ return result;
6256
6401
  } catch (e) {
6257
6402
  LOG.error("Command", `[${cmd}] Unhandled error: ${e?.message || e}`);
6258
- return { success: false, error: `Internal error: ${e?.message || "unknown"}` };
6403
+ result = { success: false, error: `Internal error: ${e?.message || "unknown"}` };
6404
+ this.logCommandEnd(cmd, result, startedAt);
6405
+ return result;
6259
6406
  }
6260
6407
  }
6261
6408
  async dispatch(cmd, args) {
@@ -10579,7 +10726,13 @@ function forwardAgentStreamsToIdeInstance(instanceManager, ideType, streams) {
10579
10726
  status: stream.status || "idle",
10580
10727
  activeModal: stream.activeModal || null,
10581
10728
  model: stream.model || void 0,
10582
- mode: stream.mode || void 0
10729
+ mode: stream.mode || void 0,
10730
+ sessionId: stream.sessionId || stream.instanceId || void 0,
10731
+ title: stream.title || stream.agentName || void 0,
10732
+ agentType: stream.agentType || void 0,
10733
+ agentName: stream.agentName || void 0,
10734
+ extensionId: stream.extensionId || void 0,
10735
+ inputContent: stream.inputContent || ""
10583
10736
  });
10584
10737
  }
10585
10738
  }
@@ -10643,14 +10796,13 @@ var ProviderInstanceManager = class {
10643
10796
  try {
10644
10797
  const state = instance.getState();
10645
10798
  states.push(state);
10646
- for (const event of state.pendingEvents) {
10647
- for (const listener of this.eventListeners) {
10648
- listener({
10649
- ...event,
10650
- providerType: instance.type,
10651
- instanceId: state.instanceId,
10652
- targetSessionId: state.instanceId,
10653
- providerCategory: state.category
10799
+ this.emitPendingEvents(instance.type, state);
10800
+ if (state.category === "ide") {
10801
+ for (const childState of state.extensions) {
10802
+ this.emitPendingEvents(childState.type, childState, {
10803
+ targetSessionId: childState.instanceId,
10804
+ workspaceName: state.workspace || void 0,
10805
+ parentSessionId: state.instanceId
10654
10806
  });
10655
10807
  }
10656
10808
  }
@@ -10699,6 +10851,21 @@ var ProviderInstanceManager = class {
10699
10851
  onEvent(listener) {
10700
10852
  this.eventListeners.push(listener);
10701
10853
  }
10854
+ emitPendingEvents(providerType, state, extra = {}) {
10855
+ for (const event of state.pendingEvents) {
10856
+ for (const listener of this.eventListeners) {
10857
+ listener({
10858
+ ...event,
10859
+ providerType,
10860
+ instanceId: state.instanceId,
10861
+ targetSessionId: state.instanceId,
10862
+ providerCategory: state.category,
10863
+ workspaceName: state.workspace || void 0,
10864
+ ...extra
10865
+ });
10866
+ }
10867
+ }
10868
+ }
10702
10869
  /**
10703
10870
  * Forward event to specific Instance
10704
10871
  */
@@ -14589,7 +14756,7 @@ async function initDaemonComponents(config) {
14589
14756
  const ideInstance = instanceManager.getInstance(instanceKey);
14590
14757
  if (ideInstance) {
14591
14758
  instanceManager.removeInstance(instanceKey);
14592
- LOG.info("CDP", `Instance removed after disconnect: ${instanceKey}`);
14759
+ LOG.info("IDE", `Instance removed after detach: ${instanceKey}`);
14593
14760
  }
14594
14761
  if (ideInstance?.getInstanceId) {
14595
14762
  agentStreamManager?.resetParentSession(ideInstance.getInstanceId());