dsh-agent-enhancer 0.5.0 → 0.5.2

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.
Files changed (40) hide show
  1. package/README.md +1 -1
  2. package/lib/client/dsh/settings.d.ts.map +1 -1
  3. package/lib/client/index.d.ts.map +1 -1
  4. package/lib/client/locales.d.ts +37 -3
  5. package/lib/client/locales.d.ts.map +1 -1
  6. package/lib/client/remote/panel.d.ts +4 -1
  7. package/lib/client/remote/panel.d.ts.map +1 -1
  8. package/lib/client/remote/status-pill.d.ts +16 -0
  9. package/lib/client/remote/status-pill.d.ts.map +1 -0
  10. package/lib/client/remote.d.ts +4 -1
  11. package/lib/client/remote.d.ts.map +1 -1
  12. package/lib/client.js +460 -150
  13. package/lib/client.js.map +1 -1
  14. package/lib/config.d.ts +4 -0
  15. package/lib/config.d.ts.map +1 -1
  16. package/lib/host/index.d.ts +9 -1
  17. package/lib/host/index.d.ts.map +1 -1
  18. package/lib/host/remote/channels/poller.d.ts +2 -0
  19. package/lib/host/remote/channels/poller.d.ts.map +1 -1
  20. package/lib/host/remote/channels/status.d.ts +2 -0
  21. package/lib/host/remote/channels/status.d.ts.map +1 -1
  22. package/lib/host/remote/controller.d.ts +11 -0
  23. package/lib/host/remote/controller.d.ts.map +1 -1
  24. package/lib/host/remote/feishu/channel.d.ts +2 -0
  25. package/lib/host/remote/feishu/channel.d.ts.map +1 -1
  26. package/lib/host/remote/host-bridge.d.ts +10 -2
  27. package/lib/host/remote/host-bridge.d.ts.map +1 -1
  28. package/lib/host/remote/hub.d.ts +21 -0
  29. package/lib/host/remote/hub.d.ts.map +1 -1
  30. package/lib/host/remote/telegram/channel.d.ts +2 -0
  31. package/lib/host/remote/telegram/channel.d.ts.map +1 -1
  32. package/lib/host/remote/weixin/adapter.d.ts +1 -0
  33. package/lib/host/remote/weixin/adapter.d.ts.map +1 -1
  34. package/lib/host/remote/weixin/channel.d.ts +8 -0
  35. package/lib/host/remote/weixin/channel.d.ts.map +1 -1
  36. package/lib/host/rpc.d.ts +16 -0
  37. package/lib/host/rpc.d.ts.map +1 -1
  38. package/lib/index.js +333 -22
  39. package/lib/index.js.map +3 -3
  40. package/package.json +12 -11
package/lib/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/host/index.ts
2
- import { existsSync as existsSync20, mkdirSync as mkdirSync15, readdirSync as readdirSync7, renameSync as renameSync11, rmdirSync } from "node:fs";
2
+ import { existsSync as existsSync21, mkdirSync as mkdirSync15, readdirSync as readdirSync7, renameSync as renameSync11, rmdirSync } from "node:fs";
3
3
  import { homedir as homedir5 } from "node:os";
4
4
  import { join as join33 } from "node:path";
5
5
  import z5 from "@deepseek-ai/schemastery";
@@ -117,6 +117,9 @@ var DEFAULT_SETTINGS = {
117
117
  instructionsSkillsEnabled: true,
118
118
  mcpConnectorsEnabled: true,
119
119
  remoteControlEnabled: true,
120
+ remoteWeixinEnabled: true,
121
+ remoteTelegramEnabled: true,
122
+ remoteFeishuEnabled: true,
120
123
  optimizeProvider: "",
121
124
  optimizeModel: "",
122
125
  optimizeReasoningEffort: "",
@@ -168,6 +171,9 @@ function resolveSettings(value) {
168
171
  instructionsSkillsEnabled: value?.instructionsSkillsEnabled ?? true,
169
172
  mcpConnectorsEnabled: value?.mcpConnectorsEnabled ?? true,
170
173
  remoteControlEnabled: value?.remoteControlEnabled ?? true,
174
+ remoteWeixinEnabled: value?.remoteWeixinEnabled ?? true,
175
+ remoteTelegramEnabled: value?.remoteTelegramEnabled ?? true,
176
+ remoteFeishuEnabled: value?.remoteFeishuEnabled ?? true,
171
177
  optimizeProvider: text(value?.optimizeProvider),
172
178
  optimizeModel: text(value?.optimizeModel),
173
179
  optimizeReasoningEffort: text(value?.optimizeReasoningEffort),
@@ -18320,6 +18326,7 @@ var remoteChannelStatusSchema = z4.strictObject({
18320
18326
  login: remoteChannelLoginSchema,
18321
18327
  devices: z4.array(remoteChannelDeviceSchema),
18322
18328
  handle: z4.string().optional(),
18329
+ enabled: z4.boolean().optional(),
18323
18330
  error: z4.string().optional()
18324
18331
  });
18325
18332
  var remoteStatusSchema = z4.strictObject({
@@ -18368,6 +18375,14 @@ var remoteTelegramConnectResultSchema = z4.strictObject({
18368
18375
  username: z4.string().optional(),
18369
18376
  error: z4.string().optional()
18370
18377
  });
18378
+ var remoteSetChannelEnabledRequestSchema = z4.strictObject({
18379
+ channel: remoteChannelIdSchema,
18380
+ enabled: z4.boolean()
18381
+ });
18382
+ var remoteSetChannelEnabledResultSchema = z4.strictObject({
18383
+ ok: z4.boolean(),
18384
+ error: z4.string().optional()
18385
+ });
18371
18386
  var remoteFeishuConnectRequestSchema = z4.strictObject({
18372
18387
  appId: z4.string().min(1).max(100),
18373
18388
  appSecret: z4.string().min(1).max(200)
@@ -18415,6 +18430,14 @@ var remoteFeishuConnectResultCodec = codec(
18415
18430
  "dsh-agent-enhancer#RemoteFeishuConnectResult",
18416
18431
  remoteFeishuConnectResultSchema
18417
18432
  );
18433
+ var remoteSetChannelEnabledRequestCodec = codec(
18434
+ "dsh-agent-enhancer#RemoteSetChannelEnabledRequest",
18435
+ remoteSetChannelEnabledRequestSchema
18436
+ );
18437
+ var remoteSetChannelEnabledResultCodec = codec(
18438
+ "dsh-agent-enhancer#RemoteSetChannelEnabledResult",
18439
+ remoteSetChannelEnabledResultSchema
18440
+ );
18418
18441
  var remoteOkResultCodec = codec("dsh-agent-enhancer#RemoteOkResult", remoteOkResultSchema);
18419
18442
  var showOsNotificationRequestSchema = z4.strictObject({
18420
18443
  title: z4.string().min(1).max(120),
@@ -19555,6 +19578,18 @@ var remoteInfo = createTypertRemoteInfo({
19555
19578
  }],
19556
19579
  result: remoteFeishuConnectResultCodec
19557
19580
  },
19581
+ "agentEnhancer/remoteSetChannelEnabled": {
19582
+ service: "agentEnhancer",
19583
+ implementation: "remoteSetChannelEnabled",
19584
+ invocation: { kind: "direct" },
19585
+ parameters: [{
19586
+ name: "request",
19587
+ source: "json",
19588
+ codec: remoteSetChannelEnabledRequestCodec,
19589
+ strictCodec: remoteSetChannelEnabledRequestCodec
19590
+ }],
19591
+ result: remoteSetChannelEnabledResultCodec
19592
+ },
19558
19593
  "agentEnhancer/showOsNotification": {
19559
19594
  service: "agentEnhancer",
19560
19595
  implementation: "showOsNotification",
@@ -20352,6 +20387,7 @@ async function showOsNotificationNow(input) {
20352
20387
  }
20353
20388
 
20354
20389
  // src/host/remote/hub.ts
20390
+ import { appendFileSync, existsSync as existsSync20 } from "node:fs";
20355
20391
  import { join as join32 } from "node:path";
20356
20392
 
20357
20393
  // src/host/remote/bindings.ts
@@ -21056,6 +21092,19 @@ var RemoteController = class {
21056
21092
  }
21057
21093
  return false;
21058
21094
  }
21095
+ /**
21096
+ * Another surface (the desktop card) resolved this ask: drop its remote card
21097
+ * and waiter so the remote chat cannot approve an already-finished decision.
21098
+ */
21099
+ dropPending(code) {
21100
+ this.approvalWaiters.delete(code);
21101
+ this.questionWaiters.delete(code);
21102
+ for (const [approvalId, pendingCode] of this.codeByApprovalId) {
21103
+ if (pendingCode === code) this.codeByApprovalId.delete(approvalId);
21104
+ }
21105
+ const task = this.board.list().find((entry) => entry.pending?.code === code);
21106
+ if (task !== void 0) this.board.clearPending(task.id);
21107
+ }
21059
21108
  /** Drop expired codes so the board does not keep stale cards. */
21060
21109
  expire(at = this.now()) {
21061
21110
  const dropped = this.board.expire(at);
@@ -21122,7 +21171,7 @@ var RemoteController = class {
21122
21171
  return task === void 0 ? { text: "\u4EFB\u52A1\u5DF2\u4E0D\u5B58\u5728\u3002\u56DE\u300C\u4EFB\u52A1\u300D\u770B\u5217\u8868\u3002" } : this.focusedReply(task, capabilities, at);
21123
21172
  }
21124
21173
  if (command.kind === "approve" || command.kind === "reject") {
21125
- return this.decide(command.kind, command.code, at);
21174
+ return this.decide(command.kind, command.code, at, chatId);
21126
21175
  }
21127
21176
  if (command.kind === "answer") {
21128
21177
  const route = routeAnswer(this.board.list(), command, at);
@@ -21132,6 +21181,7 @@ var RemoteController = class {
21132
21181
  const waiter = this.questionWaiters.get(route.pending.code);
21133
21182
  this.questionWaiters.delete(route.pending.code);
21134
21183
  waiter?.(route.text);
21184
+ this.options.onDecision?.({ chatId, sessionId: route.taskId, decision: "answered" });
21135
21185
  return { text: `\u5DF2\u56DE\u590D\u3010${this.numbers.get(route.taskId) ?? 0}\u3011${this.taskTitle(route.taskId)}` };
21136
21186
  }
21137
21187
  if (command.kind === "pause" || command.kind === "resume") {
@@ -21192,7 +21242,7 @@ var RemoteController = class {
21192
21242
  const buttons = taskActionButtons(task);
21193
21243
  return buttons.length === 0 ? { text: text3 } : { text: text3, buttons };
21194
21244
  }
21195
- decide(kind, code, at) {
21245
+ decide(kind, code, at, chatId) {
21196
21246
  const tasks = this.board.list();
21197
21247
  const route = kind === "approve" ? routeDecision(tasks, code === void 0 ? { kind: "approve" } : { kind: "approve", code }, at) : routeDecision(tasks, code === void 0 ? { kind: "reject" } : { kind: "reject", code }, at);
21198
21248
  if (!route.ok) return { text: this.routeFailureText(route.reason, route.codes) };
@@ -21201,6 +21251,7 @@ var RemoteController = class {
21201
21251
  const waiter = this.approvalWaiters.get(route.pending.code);
21202
21252
  this.approvalWaiters.delete(route.pending.code);
21203
21253
  if (route.pending.kind === "approval") waiter?.(route.decision);
21254
+ this.options.onDecision?.({ chatId, sessionId: route.taskId, decision: route.decision });
21204
21255
  const label = route.decision === "approve" ? "\u5DF2\u6279\u51C6" : "\u5DF2\u62D2\u7EDD";
21205
21256
  return { text: `${label}\u3010${this.numbers.get(route.taskId) ?? 0}\u3011${this.taskTitle(route.taskId)}` };
21206
21257
  }
@@ -21323,25 +21374,57 @@ function pickAnswer(labels, text3) {
21323
21374
  const match = labels.find((label) => label.trim() === text3.trim());
21324
21375
  return match === void 0 ? { selected: [], custom: text3 } : { selected: [match] };
21325
21376
  }
21377
+ function never() {
21378
+ return new Promise(() => {
21379
+ });
21380
+ }
21381
+ function hasAnswer(value) {
21382
+ if (value === null || typeof value !== "object") return false;
21383
+ const answers = value.answers;
21384
+ if (!Array.isArray(answers)) return false;
21385
+ return answers.some((item) => {
21386
+ if (item === null || typeof item !== "object") return false;
21387
+ const record2 = item;
21388
+ return Array.isArray(record2.selected) && record2.selected.length > 0 || typeof record2.custom === "string" && record2.custom.trim() !== "";
21389
+ });
21390
+ }
21391
+ function callNext(next) {
21392
+ let result;
21393
+ try {
21394
+ result = next();
21395
+ } catch {
21396
+ return void 0;
21397
+ }
21398
+ if (result === null || typeof result !== "object") return void 0;
21399
+ const then = result.then;
21400
+ return typeof then === "function" ? result : void 0;
21401
+ }
21326
21402
  function installRemoteHostBridge(ctx, options) {
21403
+ const notify = (notice) => {
21404
+ options.debugLog?.(JSON.stringify({ kind: notice.kind, sessionId: notice.sessionId }));
21405
+ try {
21406
+ options.ports.notify?.(notice);
21407
+ } catch {
21408
+ }
21409
+ };
21327
21410
  const controller = new RemoteController({
21328
21411
  actions: {
21329
21412
  sendPrompt: (sessionId, text3) => options.ports.prompt(sessionId, text3, "queue"),
21330
21413
  setPaused: (sessionId, paused) => options.ports.setPaused(sessionId, paused),
21331
21414
  stop: (sessionId) => options.ports.cancel(sessionId)
21332
21415
  },
21416
+ onDecision: (info) => notify({
21417
+ kind: "decided",
21418
+ sessionId: info.sessionId,
21419
+ decision: info.decision,
21420
+ chatId: info.chatId
21421
+ }),
21333
21422
  ...options.ports.readAnswer === void 0 ? {} : { readAnswer: options.ports.readAnswer },
21334
21423
  ...options.now === void 0 ? {} : { now: options.now },
21335
21424
  ...options.ttlMs === void 0 ? {} : { ttlMs: options.ttlMs }
21336
21425
  });
21337
21426
  const known = (sessionId) => controller.tasks().some((task) => task.id === sessionId);
21338
21427
  const lastAssistant = /* @__PURE__ */ new Map();
21339
- const notify = (notice) => {
21340
- try {
21341
- options.ports.notify?.(notice);
21342
- } catch {
21343
- }
21344
- };
21345
21428
  let refreshing;
21346
21429
  let lastEventRefresh = 0;
21347
21430
  const refreshSessions = async () => {
@@ -21422,10 +21505,14 @@ function installRemoteHostBridge(ctx, options) {
21422
21505
  }
21423
21506
  };
21424
21507
  const canAnswer = () => options.shouldHold?.() !== false;
21425
- const onApprovalRequest = (request, next) => {
21508
+ const onApprovalRequest = async (request, next) => {
21426
21509
  if (!canAnswer()) return next();
21427
21510
  const sessionId = sessionIdOf2(request?.agent);
21428
- if (sessionId === void 0 || !known(sessionId)) return next();
21511
+ if (sessionId === void 0) return next();
21512
+ if (!known(sessionId)) {
21513
+ await refreshSessions().catch(() => 0);
21514
+ if (!known(sessionId)) return next();
21515
+ }
21429
21516
  try {
21430
21517
  const handle = controller.askApproval(sessionId, {
21431
21518
  prompt: describeApproval(request),
@@ -21433,15 +21520,32 @@ function installRemoteHostBridge(ctx, options) {
21433
21520
  risk: "other"
21434
21521
  });
21435
21522
  notify({ kind: "approval", sessionId, code: handle.code });
21436
- return handle.wait;
21523
+ const remote = handle.wait.then((decision) => decision === "approve" ? "allowed-once" : "rejected");
21524
+ const desktop = callNext(next);
21525
+ if (desktop === void 0) return remote;
21526
+ const fromDesktop = Promise.resolve(desktop).then(
21527
+ (outcome) => {
21528
+ if (outcome !== "allowed-once" && outcome !== "rejected") return never();
21529
+ controller.dropPending(handle.code);
21530
+ const decision = outcome === "allowed-once" ? "approve" : "reject";
21531
+ notify({ kind: "decided", sessionId, decision });
21532
+ return outcome;
21533
+ },
21534
+ () => never()
21535
+ );
21536
+ return Promise.race([remote, fromDesktop]);
21437
21537
  } catch {
21438
21538
  return next();
21439
21539
  }
21440
21540
  };
21441
- const onQuestionRequest = (request, next) => {
21541
+ const onQuestionRequest = async (request, next) => {
21442
21542
  if (!canAnswer()) return next();
21443
21543
  const sessionId = sessionIdOf2(request?.agent) ?? (typeof request?.sessionId === "string" ? request.sessionId : void 0);
21444
- if (sessionId === void 0 || !known(sessionId)) return next();
21544
+ if (sessionId === void 0) return next();
21545
+ if (!known(sessionId)) {
21546
+ await refreshSessions().catch(() => 0);
21547
+ if (!known(sessionId)) return next();
21548
+ }
21445
21549
  const questions = Array.isArray(request.questions) ? request.questions : [];
21446
21550
  if (questions.length !== 1) return next();
21447
21551
  const question = questions[0];
@@ -21456,9 +21560,21 @@ function installRemoteHostBridge(ctx, options) {
21456
21560
  ...labels.length === 0 ? {} : { options: labels }
21457
21561
  });
21458
21562
  notify({ kind: "question", sessionId, code: handle.code });
21459
- return handle.wait.then((text3) => ({
21563
+ const remote = handle.wait.then((text3) => ({
21460
21564
  answers: [{ id: questionId, ...pickAnswer(labels, text3) }]
21461
21565
  }));
21566
+ const desktop = callNext(next);
21567
+ if (desktop === void 0) return remote;
21568
+ const fromDesktop = Promise.resolve(desktop).then(
21569
+ (answer) => {
21570
+ if (!hasAnswer(answer)) return never();
21571
+ controller.dropPending(handle.code);
21572
+ notify({ kind: "decided", sessionId, decision: "answered" });
21573
+ return answer;
21574
+ },
21575
+ () => never()
21576
+ );
21577
+ return Promise.race([remote, fromDesktop]);
21462
21578
  } catch {
21463
21579
  return next();
21464
21580
  }
@@ -21705,6 +21821,7 @@ var ChannelPoller = class {
21705
21821
  }
21706
21822
  }
21707
21823
  async handle(message) {
21824
+ this.options.onInbound?.();
21708
21825
  const chatId = message.fromUserId;
21709
21826
  if (message.contextToken !== void 0 && message.contextToken !== "") {
21710
21827
  this.contexts.set(chatId, message.contextToken);
@@ -22099,6 +22216,10 @@ var TelegramChannel = class {
22099
22216
  this.credentialsValue = void 0;
22100
22217
  this.phaseValue = "off";
22101
22218
  }
22219
+ /** A proactive push failed: inbound can still work, but the UI must say so. */
22220
+ reportSendError(error) {
22221
+ this.errorValue = error.code === "network-error" ? "\u63A8\u9001\u53D7\u9650\uFF1A\u6682\u65F6\u8FDE\u4E0D\u4E0A Telegram API\uFF0C\u6D88\u606F\u672A\u53D1\u51FA\u3002" : `\u63A8\u9001\u5931\u8D25\uFF1A${messageOf(error)}`;
22222
+ }
22102
22223
  async send(chatId, text3, buttons) {
22103
22224
  const credentials = this.credentialsValue;
22104
22225
  if (this.phaseValue !== "running" || credentials === void 0) return;
@@ -22675,6 +22796,10 @@ var FeishuChannel = class {
22675
22796
  this.credentialsValue = void 0;
22676
22797
  this.phaseValue = "off";
22677
22798
  }
22799
+ /** A proactive push failed: keep the connection, surface the reason. */
22800
+ reportSendError(error) {
22801
+ this.errorValue = `\u63A8\u9001\u5931\u8D25\uFF1A${messageOf2(error)}`;
22802
+ }
22678
22803
  async send(chatId, text3, buttons) {
22679
22804
  const credentials = this.credentialsValue;
22680
22805
  if (this.phaseValue !== "running" || credentials === void 0) return;
@@ -23134,6 +23259,7 @@ var WeixinAdapter = class {
23134
23259
  ...options.refresh === void 0 ? {} : { refresh: options.refresh },
23135
23260
  ...options.emptyPageDelayMs === void 0 ? {} : { emptyPageDelayMs: options.emptyPageDelayMs },
23136
23261
  isFatal: (error) => error instanceof WeixinTransportError && error.code === "stale-token",
23262
+ ...options.onInbound === void 0 ? {} : { onInbound: options.onInbound },
23137
23263
  hints: WEIXIN_HINTS
23138
23264
  });
23139
23265
  }
@@ -23283,11 +23409,26 @@ var WeixinChannel = class {
23283
23409
  credentialsValue;
23284
23410
  phaseValue = "off";
23285
23411
  errorValue;
23412
+ /** Timestamp of the last inbound message: WeChat push rights come from it. */
23413
+ lastInboundAt = 0;
23286
23414
  /** Bumped by stop/logout so an in-flight scan result cannot reconnect. */
23287
23415
  generation = 0;
23288
23416
  get running() {
23289
23417
  return this.phaseValue === "running";
23290
23418
  }
23419
+ /** Timestamp of the last inbound message that renewed WeChat push rights. */
23420
+ get lastInbound() {
23421
+ return this.lastInboundAt;
23422
+ }
23423
+ /** Warn before the user walks away; a fresh inbound message clears it. */
23424
+ reportStale() {
23425
+ this.errorValue = "\u63A8\u9001\u53EF\u80FD\u5DF2\u8FC7\u671F\uFF1A\u79BB\u5F00\u7535\u8111\u524D\u7ED9\u673A\u5668\u4EBA\u53D1\u4E00\u6761\u6D88\u606F\uFF0C\u5426\u5219\u5BA1\u6279 / \u56DE\u7B54\u63A8\u4E0D\u5230\u5FAE\u4FE1\u3002";
23426
+ }
23427
+ /** A proactive push failed: keep inbound alive but make the reason visible. */
23428
+ reportSendError(error) {
23429
+ const providerCode = error.detail?.providerCode;
23430
+ this.errorValue = providerCode === "-2" ? "\u63A8\u9001\u53D7\u9650\uFF1A\u5FAE\u4FE1\u4F1A\u8BDD\u5DF2\u8FC7\u671F\u3002\u5728\u5FAE\u4FE1\u91CC\u7ED9\u673A\u5668\u4EBA\u53D1\u4E00\u6761\u6D88\u606F\u5373\u53EF\u6062\u590D\u3002" : `\u63A8\u9001\u5931\u8D25\uFF1A${messageOf3(error)}`;
23431
+ }
23291
23432
  status() {
23292
23433
  const devices = this.options.bindings.list().filter((binding) => binding.channel === this.id).map((binding) => ({ chatId: binding.chatId, boundAt: binding.boundAt }));
23293
23434
  return {
@@ -23362,6 +23503,9 @@ var WeixinChannel = class {
23362
23503
  contextToken === void 0 ? {} : { contextToken }
23363
23504
  );
23364
23505
  }
23506
+ if (this.errorValue?.startsWith("\u63A8\u9001\u53D7\u9650") === true || this.errorValue?.startsWith("\u63A8\u9001\u53EF\u80FD\u5DF2\u8FC7\u671F") === true) {
23507
+ this.errorValue = void 0;
23508
+ }
23365
23509
  }
23366
23510
  contextFor(chatId) {
23367
23511
  return this.adapter?.contextFor(chatId);
@@ -23386,6 +23530,12 @@ var WeixinChannel = class {
23386
23530
  capabilities: this.options.capabilities,
23387
23531
  cursor,
23388
23532
  refresh: this.options.refresh,
23533
+ onInbound: () => {
23534
+ this.lastInboundAt = Date.now();
23535
+ if (this.errorValue?.startsWith("\u63A8\u9001\u53D7\u9650") === true || this.errorValue?.startsWith("\u63A8\u9001\u53EF\u80FD\u5DF2\u8FC7\u671F") === true) {
23536
+ this.errorValue = void 0;
23537
+ }
23538
+ },
23389
23539
  ...this.options.emptyPageDelayMs === void 0 ? {} : { emptyPageDelayMs: this.options.emptyPageDelayMs },
23390
23540
  onError: (error) => this.options.onError?.(error),
23391
23541
  onFatal: (error) => {
@@ -23521,6 +23671,8 @@ var RemoteHub = class {
23521
23671
  retryTimer;
23522
23672
  retries = 0;
23523
23673
  generation = 0;
23674
+ lastWechatWarnAt = 0;
23675
+ channelEnabled = /* @__PURE__ */ new Map();
23524
23676
  weixinCapabilities;
23525
23677
  telegramCapabilities;
23526
23678
  feishuCapabilities;
@@ -23534,11 +23686,18 @@ var RemoteHub = class {
23534
23686
  return this.weixin?.running === true || this.telegram?.running === true || this.feishu?.running === true;
23535
23687
  }
23536
23688
  status() {
23689
+ this.checkWechatFreshness();
23537
23690
  const weixin = this.weixin;
23538
23691
  const telegram = this.telegram;
23539
23692
  const feishu = this.feishu;
23540
23693
  const started = weixin !== void 0 && telegram !== void 0 && feishu !== void 0;
23541
- const channels = started ? [weixin.status(), telegram.status(), feishu.status()] : [offStatus("weixin"), offStatus("telegram"), offStatus("feishu")];
23694
+ const channels = started ? [weixin, telegram, feishu].map((channel) => ({
23695
+ ...channel.status(),
23696
+ enabled: this.channelEnabled.get(channel.id) !== false
23697
+ })) : [offStatus("weixin"), offStatus("telegram"), offStatus("feishu")].map((channel) => ({
23698
+ ...channel,
23699
+ enabled: this.channelEnabled.get(channel.id) !== false
23700
+ }));
23542
23701
  let phase;
23543
23702
  if (!started) {
23544
23703
  phase = this.phaseValue === "failed" ? "failed" : this.phaseValue;
@@ -23584,7 +23743,13 @@ var RemoteHub = class {
23584
23743
  this.bridge = installRemoteHostBridge(this.options.ctx, {
23585
23744
  ports,
23586
23745
  capabilities: this.weixinCapabilities,
23587
- shouldHold: () => bindings.list().length > 0 && this.anyRunning()
23746
+ shouldHold: () => bindings.list().length > 0 && this.anyRunning(),
23747
+ debugLog: (line) => {
23748
+ try {
23749
+ this.debugNotice({ stage: "bridge", detail: JSON.parse(line) });
23750
+ } catch {
23751
+ }
23752
+ }
23588
23753
  });
23589
23754
  this.controller = this.bridge.controller;
23590
23755
  try {
@@ -23638,7 +23803,13 @@ var RemoteHub = class {
23638
23803
  ...this.options.onError === void 0 ? {} : { onError: this.options.onError },
23639
23804
  ...this.options.emptyPageDelayMs === void 0 ? {} : { emptyPageDelayMs: this.options.emptyPageDelayMs }
23640
23805
  });
23641
- await Promise.all([this.weixin.start(), this.telegram.start(), this.feishu.start()]);
23806
+ await Promise.all([this.weixin, this.telegram, this.feishu].map(async (channel) => {
23807
+ if (this.channelEnabled.get(channel.id) === false) {
23808
+ this.debugNotice({ stage: "startSkip", channel: channel.id });
23809
+ return;
23810
+ }
23811
+ await channel.start();
23812
+ }));
23642
23813
  this.refreshPhase();
23643
23814
  return this.status();
23644
23815
  }
@@ -23684,6 +23855,42 @@ var RemoteHub = class {
23684
23855
  for (const binding of targets) this.bindings.unbind(binding.chatId);
23685
23856
  return targets.length;
23686
23857
  }
23858
+ /** Apply persisted per-channel switches; safe before the hub has started. */
23859
+ applyChannelSettings(enabled) {
23860
+ this.debugNotice({ stage: "flags", enabled, phase: this.phaseValue });
23861
+ for (const channel of REMOTE_CHANNELS) {
23862
+ const before = this.channelEnabled.get(channel) !== false;
23863
+ const wanted = enabled[channel] !== false;
23864
+ this.channelEnabled.set(channel, wanted);
23865
+ if (before === wanted) continue;
23866
+ const runtime = channel === "weixin" ? this.weixin : channel === "telegram" ? this.telegram : this.feishu;
23867
+ if (runtime === void 0) continue;
23868
+ if (wanted) {
23869
+ if (this.phaseValue !== "stopped") void runtime.start().catch((error) => this.options.onError?.(error));
23870
+ } else {
23871
+ runtime.stop();
23872
+ }
23873
+ }
23874
+ if (this.weixin !== void 0) {
23875
+ if (!this.anyRunning()) this.controller?.releasePending();
23876
+ this.refreshPhase();
23877
+ }
23878
+ }
23879
+ /** Pause one channel without touching its credentials or pairing codes. */
23880
+ setChannelEnabled(channel, enabled) {
23881
+ this.debugNotice({ stage: "setChannel", channel, enabled, phase: this.phaseValue });
23882
+ const runtime = channel === "weixin" ? this.weixin : channel === "telegram" ? this.telegram : this.feishu;
23883
+ if (runtime === void 0) return false;
23884
+ this.channelEnabled.set(channel, enabled);
23885
+ if (!enabled) {
23886
+ runtime.stop();
23887
+ if (!this.anyRunning()) this.controller?.releasePending();
23888
+ } else if (this.phaseValue !== "stopped") {
23889
+ void runtime.start().catch((error) => this.options.onError?.(error));
23890
+ }
23891
+ this.refreshPhase();
23892
+ return true;
23893
+ }
23687
23894
  async logout(channel) {
23688
23895
  if (channel === void 0 || channel === "weixin") await this.weixin?.logout();
23689
23896
  if (channel === void 0 || channel === "telegram") await this.telegram?.logout();
@@ -23728,6 +23935,42 @@ var RemoteHub = class {
23728
23935
  }
23729
23936
  this.phaseValue = channels.length > 0 && channels.every((channel) => channel.status().phase === "failed") ? "failed" : "unbound";
23730
23937
  }
23938
+ /**
23939
+ * WeChat only allows proactive sends while a recent inbound message has
23940
+ * renewed the session. Warn while the user is still at the desktop instead
23941
+ * of failing silently after they walk away.
23942
+ */
23943
+ checkWechatFreshness() {
23944
+ const weixin = this.weixin;
23945
+ if (weixin === void 0 || !weixin.running) return;
23946
+ if (!(this.bindings?.list().some((binding) => binding.channel === "weixin") ?? false)) return;
23947
+ const last = weixin.lastInbound;
23948
+ if (last === 0 || Date.now() - last < 2 * 60 * 6e4) return;
23949
+ weixin.reportStale();
23950
+ this.warnWechatStale("idle");
23951
+ }
23952
+ warnWechatStale(reason) {
23953
+ const now = Date.now();
23954
+ if (now - this.lastWechatWarnAt < 12 * 60 * 6e4) return;
23955
+ this.lastWechatWarnAt = now;
23956
+ this.options.desktopNotify?.({
23957
+ title: "\u5FAE\u4FE1\u8FDC\u7A0B\u63A8\u9001\u53EF\u80FD\u6536\u4E0D\u5230",
23958
+ body: reason === "expired" ? "\u5FAE\u4FE1\u4F1A\u8BDD\u5DF2\u8FC7\u671F\u3002\u79BB\u5F00\u7535\u8111\u524D\u7ED9\u673A\u5668\u4EBA\u53D1\u4E00\u6761\u6D88\u606F\uFF0C\u5426\u5219\u5BA1\u6279\u548C\u56DE\u7B54\u63A8\u4E0D\u5230\u5FAE\u4FE1\u3002" : "\u8D85\u8FC7 2 \u5C0F\u65F6\u6CA1\u6709\u5FAE\u4FE1\u6D88\u606F\u3002\u79BB\u5F00\u7535\u8111\u524D\u7ED9\u673A\u5668\u4EBA\u53D1\u4E00\u6761\u6D88\u606F\uFF0C\u786E\u4FDD\u8FD8\u80FD\u6536\u5230\u5BA1\u6279\u3002",
23959
+ tag: "remote-wechat-stale"
23960
+ });
23961
+ }
23962
+ /** Opt-in diagnostics: create `remote/debug-notices.enabled` in plugin-data. */
23963
+ debugNotice(entry) {
23964
+ try {
23965
+ if (!existsSync20(join32(this.options.rootDir, "debug-notices.enabled"))) return;
23966
+ appendFileSync(
23967
+ join32(this.options.rootDir, "notice-log.jsonl"),
23968
+ `${JSON.stringify({ at: Date.now(), ...entry })}
23969
+ `
23970
+ );
23971
+ } catch {
23972
+ }
23973
+ }
23731
23974
  /** Send one notice to every paired chat on a channel that can answer. */
23732
23975
  pushNotice(notice) {
23733
23976
  const controller = this.controller;
@@ -23747,6 +23990,14 @@ ${clip2(notice.error, 300)}`;
23747
23990
  if (answer !== "") text3 += `
23748
23991
 
23749
23992
  ${answer}`;
23993
+ }
23994
+ if (notice.kind === "decided") {
23995
+ const channel2 = notice.chatId === void 0 ? void 0 : this.bindings?.binding(notice.chatId)?.channel;
23996
+ const where = channel2 === void 0 ? "\u684C\u9762" : channel2 === "weixin" ? "\u5FAE\u4FE1" : channel2 === "telegram" ? "Telegram" : "\u98DE\u4E66";
23997
+ const verb = notice.decision === "approve" ? "\u6279\u51C6" : notice.decision === "reject" ? "\u62D2\u7EDD" : "\u56DE\u7B54";
23998
+ text3 += `
23999
+
24000
+ \u5DF2\u7531${where}${verb}`;
23750
24001
  }
23751
24002
  const task = controller.tasks().find((entry) => entry.id === notice.sessionId);
23752
24003
  const pending = task?.pending;
@@ -23766,11 +24017,42 @@ ${answer}`;
23766
24017
  command: `\u56DE\u7B54 ${pending.code} ${option}`
23767
24018
  }));
23768
24019
  }
23769
- } else if (notice.kind === "done" || notice.kind === "failed") {
24020
+ } else if (notice.kind === "done" || notice.kind === "failed" || notice.kind === "decided") {
23770
24021
  buttons = [{ label: "\u8BE6\u60C5", command: number === void 0 ? "\u8BE6\u60C5" : `\u8BE6\u60C5 ${number}` }];
23771
24022
  }
23772
24023
  }
23773
- void channel.send(binding.chatId, text3, buttons).catch((error) => this.options.onError?.(error));
24024
+ this.debugNotice({
24025
+ stage: "queued",
24026
+ kind: notice.kind,
24027
+ channel: binding.channel,
24028
+ chatId: binding.chatId,
24029
+ sessionId: notice.sessionId
24030
+ });
24031
+ void channel.send(binding.chatId, text3, buttons).then(() => {
24032
+ this.debugNotice({
24033
+ stage: "sent",
24034
+ kind: notice.kind,
24035
+ channel: binding.channel,
24036
+ chatId: binding.chatId,
24037
+ sessionId: notice.sessionId
24038
+ });
24039
+ }).catch((error) => {
24040
+ this.debugNotice({
24041
+ stage: "error",
24042
+ kind: notice.kind,
24043
+ channel: binding.channel,
24044
+ chatId: binding.chatId,
24045
+ sessionId: notice.sessionId,
24046
+ error: messageOf4(error),
24047
+ code: error.code,
24048
+ detail: error.detail
24049
+ });
24050
+ channel.reportSendError(error);
24051
+ if (binding.channel === "weixin" && error.detail?.providerCode === "-2") {
24052
+ this.warnWechatStale("expired");
24053
+ }
24054
+ this.options.onError?.(error);
24055
+ });
23774
24056
  }
23775
24057
  }
23776
24058
  scheduleRetry() {
@@ -23848,6 +24130,9 @@ var Config = z5.object({
23848
24130
  webChatBridgeEnabled: z5.boolean().default(true).volatile(),
23849
24131
  /** WeChat + Telegram remote control; on by default, inert until a chat is paired. */
23850
24132
  remoteControlEnabled: z5.boolean().default(true).volatile(),
24133
+ remoteWeixinEnabled: z5.boolean().default(true).volatile(),
24134
+ remoteTelegramEnabled: z5.boolean().default(true).volatile(),
24135
+ remoteFeishuEnabled: z5.boolean().default(true).volatile(),
23851
24136
  instructionsSkillsEnabled: z5.boolean().default(true).volatile(),
23852
24137
  mcpConnectorsEnabled: z5.boolean().default(true).volatile(),
23853
24138
  // Empty provider/model = follow the current Desktop session model.
@@ -24687,6 +24972,16 @@ var AgentEnhancerService = class extends TypertRemoteService {
24687
24972
  this.assertOwner();
24688
24973
  return await showOsNotificationNow(request);
24689
24974
  }
24975
+ /** Pause or resume one channel without clearing its credentials. */
24976
+ async remoteSetChannelEnabled(request) {
24977
+ this.assertOwner();
24978
+ try {
24979
+ const ok = this.requireRemoteControl().setChannelEnabled(request.channel, request.enabled);
24980
+ return ok ? { ok: true } : { ok: false, error: "\u8FDC\u7A0B\u63A7\u5236\u8FD8\u6CA1\u6709\u542F\u52A8" };
24981
+ } catch (error) {
24982
+ return { ok: false, error: error instanceof Error ? error.message : String(error) };
24983
+ }
24984
+ }
24690
24985
  async browserAgentSnapshot() {
24691
24986
  this.assertOwner();
24692
24987
  return await this.browserSnapshotNow();
@@ -24884,12 +25179,12 @@ function resolveCheckpointRoot(dshHome2) {
24884
25179
  const nextPkg = join33(base, PACKAGE_NAME);
24885
25180
  const next = join33(nextPkg, "checkpoint");
24886
25181
  const prev = join33(base, LEGACY_PACKAGE_NAME, "checkpoint");
24887
- if (!existsSync20(next) && existsSync20(prev)) {
25182
+ if (!existsSync21(next) && existsSync21(prev)) {
24888
25183
  mkdirSync15(nextPkg, { recursive: true });
24889
25184
  renameSync11(prev, next);
24890
25185
  const prevPkg = join33(base, LEGACY_PACKAGE_NAME);
24891
25186
  try {
24892
- if (existsSync20(prevPkg) && readdirSync7(prevPkg).length === 0) rmdirSync(prevPkg);
25187
+ if (existsSync21(prevPkg) && readdirSync7(prevPkg).length === 0) rmdirSync(prevPkg);
24893
25188
  } catch {
24894
25189
  }
24895
25190
  }
@@ -24968,6 +25263,9 @@ function readSettings(config) {
24968
25263
  browserAgentUnattendedActions: config.browserAgentUnattendedActions.get(),
24969
25264
  webChatBridgeEnabled: config.webChatBridgeEnabled.get(),
24970
25265
  remoteControlEnabled: config.remoteControlEnabled.get(),
25266
+ remoteWeixinEnabled: config.remoteWeixinEnabled.get(),
25267
+ remoteTelegramEnabled: config.remoteTelegramEnabled.get(),
25268
+ remoteFeishuEnabled: config.remoteFeishuEnabled.get(),
24971
25269
  instructionsSkillsEnabled: config.instructionsSkillsEnabled.get(),
24972
25270
  mcpConnectorsEnabled: config.mcpConnectorsEnabled.get(),
24973
25271
  optimizeProvider: config.optimizeProvider.get(),
@@ -24994,6 +25292,13 @@ function readSettings(config) {
24994
25292
  autoClassifierReasoningEffort: config.autoClassifierReasoningEffort.get()
24995
25293
  });
24996
25294
  }
25295
+ function remoteChannelFlags(settings) {
25296
+ return {
25297
+ weixin: settings.remoteWeixinEnabled,
25298
+ telegram: settings.remoteTelegramEnabled,
25299
+ feishu: settings.remoteFeishuEnabled
25300
+ };
25301
+ }
24997
25302
  function apply(ctx, config) {
24998
25303
  const settings = () => readSettings(config);
24999
25304
  const models = new AuxiliaryModels(ctx, settings);
@@ -25095,6 +25400,10 @@ function apply(ctx, config) {
25095
25400
  onError: (error) => {
25096
25401
  const detail = error instanceof Error ? error.message : String(error);
25097
25402
  ctx.logger.warn(`agent-enhancer: remote control: ${detail}`);
25403
+ },
25404
+ desktopNotify: (input) => {
25405
+ void showOsNotificationNow(input).catch(() => {
25406
+ });
25098
25407
  }
25099
25408
  });
25100
25409
  let remoteRunning = false;
@@ -25110,6 +25419,7 @@ function apply(ctx, config) {
25110
25419
  ctx.logger.warn(`agent-enhancer: remote control start failed: ${detail}`);
25111
25420
  }
25112
25421
  };
25422
+ remoteHub.applyChannelSettings(remoteChannelFlags(settings()));
25113
25423
  void syncRemoteControl(settings().remoteControlEnabled);
25114
25424
  sessionCleanup.register({
25115
25425
  id: "remote-control",
@@ -25166,6 +25476,7 @@ function apply(ctx, config) {
25166
25476
  browserAgentLive.enabled = cfg.browserAgentEnabled.get();
25167
25477
  browserAgentLive.browser = hostBrowserPreference(cfg.browserAgentBrowser.get());
25168
25478
  browserAgentLive.demonstrate = cfg.browserAgentDemonstrate.get();
25479
+ remoteHub.applyChannelSettings(remoteChannelFlags(readSettings(cfg)));
25169
25480
  void syncRemoteControl(cfg.remoteControlEnabled.get());
25170
25481
  browserAgent.refresh();
25171
25482
  if (browserAgentLive.enabled && prevBrowser !== browserAgentLive.browser && browserAgent.driverStatus()?.ready === true) {