auto-reply.js 44.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
/**
 * auto-reply.js — Pixel 手机微信自动回复 Plugin(接入 hermes API)
 *
 * 完整链路:
 *   1. termux-notification-list 监听通知(息屏兜底,定时轮询)
 *   2. 检测到消息后 input keyevent 224 点亮屏幕 + wm dismiss-keyguard 解锁
 *   3. am start LauncherUI 把微信切到前台
 *   4. AndroidWechatTrackerSQLCipher 轮询 EnMicroMsg.db 拉新消息 + 历史上下文
 *   5. callHermesApi POST 到 hermes-agent (127.0.0.1:8642) 生成回复
 *   6. sendTextMessageBroadcast → am broadcast com.hermes.wechat.SEND_MSG
 *      → HermesAccessibilityService UI 自动化点击微信完成发送
 *
 * 运行方式:
 *   cd /data/data/com.termux/files/home/hermes-wechat
 *   ./start-ar.sh   (内部 nohup node auto-reply.js &)
 *
 * 终止:
 *   pkill -f auto-reply.js
 */

const { AndroidWechatTrackerSQLCipher } = require('./android-wechat-tracker-sqlcipher');
const {
  sendTextMessageBroadcast,
  checkAccessibilityServiceEnabled,
  enableAccessibilityService,
  shell,
} = require('./android-wechat-sender');
const fs = require('fs');
const path = require('path');
const http = require('http');

// ═══════════════════════════════════════════════════════════════
//  配置区
// ═══════════════════════════════════════════════════════════════

const CONFIG = {
  // 你的微信信息(用于判断群聊 @/引用)
  myWxid: process.env.MY_WXID || '',
  myNickname: process.env.MY_NICKNAME || '',

  // 主人(授权者):非白名单消息会转给 TA 请示是否授权
  // ownerWxid 是机器人主人的 wxid(如 yibooo),ownerDisplayName 是 A11y 搜索用的昵称
  ownerWxid: process.env.OWNER_WXID || 'yibooo',
  ownerDisplayName: process.env.OWNER_NICKNAME || '火星人',

  // 白名单文件:记录已授权的 wxid / 群 + 待授权的 pending 请求
  whitelistFile: path.join(__dirname, '.whitelist.json'),

  // hermes-agent API (OpenAI-compatible, localhost:8642)
  hermesHost: process.env.HERMES_HOST || '127.0.0.1',
  hermesPort: parseInt(process.env.HERMES_PORT, 10) || 8642,
  hermesTimeoutMs: parseInt(process.env.HERMES_TIMEOUT, 10) || 120000,

  // 回复行为
  replyInPrivateChat: true,
  replyAtInGroup: true,
  replyQuoteInGroup: true,
  maxReplyLength: 500,
  cooldownMs: 3000,
  ignoreSelf: true,

  // 上下文长度:每次调 hermes 时回看多少条最近消息(含本次触发的那条)
  historyContextSize: parseInt(process.env.HISTORY_CONTEXT_SIZE, 10) || 8,

  // 流式行为:模型沉默 agentic 调用时,是否发"🔧 调用 X"进度消息
  // 默认 OFF(避免群里刷屏);开了之后每个 tool.started 都会发一条进度消息
  streamToolProgress: process.env.STREAM_TOOL_PROGRESS === 'true',

  // 是否把 reasoning_content(模型内心独白)作为消息发给用户
  // 默认 OFF — reasoning 是模型自言自语("用户要求..." 第三人称),
  // 直接发给用户极其诡异。开了之后会带 "🧠 " 前缀作为思考进度发。
  streamReasoning: process.env.STREAM_REASONING === 'true',

  // 通知监听(息屏兜底)
  notificationPollIntervalMs: 3000,
  notificationEnabled: process.env.NOTIFICATION_ENABLED !== 'false',

  // 日志
  logFile: path.join(__dirname, 'auto-reply.log'),
  stateFile: path.join(__dirname, '.auto-reply-state.json'),
};

// ═══════════════════════════════════════════════════════════════
//  状态管理
// ═══════════════════════════════════════════════════════════════

class ReplyState {
  constructor(filePath) {
    this.filePath = filePath;
    this.repliedMsgIds = new Set();
    this.lastReplyTime = {};
    this.load();
  }

  load() {
    try {
      if (fs.existsSync(this.filePath)) {
        const data = JSON.parse(fs.readFileSync(this.filePath, 'utf-8'));
        this.repliedMsgIds = new Set(data.repliedMsgIds || []);
        this.lastReplyTime = data.lastReplyTime || {};
      }
    } catch (e) {
      log('State load failed:', e.message);
    }
  }

  save() {
    try {
      fs.writeFileSync(this.filePath, JSON.stringify({
        repliedMsgIds: Array.from(this.repliedMsgIds),
        lastReplyTime: this.lastReplyTime,
      }), 'utf-8');
    } catch (e) {
      log('State save failed:', e.message);
    }
  }

  hasReplied(msgId) {
    return this.repliedMsgIds.has(msgId);
  }

  markReplied(msgId, talker) {
    this.repliedMsgIds.add(msgId);
    this.lastReplyTime[talker] = Date.now();
    if (this.repliedMsgIds.size > 1000) {
      const arr = Array.from(this.repliedMsgIds);
      this.repliedMsgIds = new Set(arr.slice(-500));
    }
    this.save();
  }

  isCooldown(talker) {
    const last = this.lastReplyTime[talker];
    if (!last) return false;
    return Date.now() - last < CONFIG.cooldownMs;
  }
}

// ═══════════════════════════════════════════════════════════════
//  白名单 + 授权 pending(持久化到 .whitelist.json)
// ═══════════════════════════════════════════════════════════════

class WhitelistStore {
  constructor(filePath) {
    this.filePath = filePath;
    this.allowed = new Set();   // 已授权的 wxid / 群 chatroom id
    this.pending = {};          // talker -> { senderName, content, displayName, ts }
    this.load();
  }

  load() {
    try {
      if (fs.existsSync(this.filePath)) {
        const data = JSON.parse(fs.readFileSync(this.filePath, 'utf-8'));
        this.allowed = new Set(data.allowed || []);
        this.pending = data.pending || {};
      }
    } catch (e) {
      log('[WHITELIST] load failed:', e.message);
    }
  }

  save() {
    try {
      fs.writeFileSync(this.filePath, JSON.stringify({
        allowed: Array.from(this.allowed),
        pending: this.pending,
      }, null, 2), 'utf-8');
    } catch (e) {
      log('[WHITELIST] save failed:', e.message);
    }
  }

  isAllowed(talker) { return this.allowed.has(talker); }

  allow(talker) {
    this.allowed.add(talker);
    delete this.pending[talker];
    this.save();
  }

  addPending(talker, info) {
    this.pending[talker] = info;
    this.save();
  }

  hasPending(talker) { return Object.prototype.hasOwnProperty.call(this.pending, talker); }

  removePending(talker) {
    delete this.pending[talker];
    this.save();
  }

  pendingTalkers() { return Object.keys(this.pending); }
}

// ═══════════════════════════════════════════════════════════════
//  日志
// ═══════════════════════════════════════════════════════════════

function log(...args) {
  const line = `[${new Date().toISOString()}] ${args.join(' ')}`;
  // start-ar.sh 已经把 stdout 重定向到 auto-reply.log,console.log 即写入文件。
  // 不再额外 fs.appendFileSync — 同步 I/O 在高频流式场景下会卡死 node。
  console.log(line);
}

// ═══════════════════════════════════════════════════════════════
//  Hermes API 调用
// ═══════════════════════════════════════════════════════════════

/**
 * 调用 hermes-agent 的 /v1/runs 流式 API,按 agent loop 的 tool 边界
 * 把每一段 assistant 文本作为独立"utterance"通过 onUtterance 回调推出。
 *
 * 事件流(SSE on /v1/runs/{id}/events):
 *   message.delta { delta: "字符片段" }     → 累积到 textBuf
 *   tool.started  { tool, preview }          → flush textBuf 作为一条 utterance
 *   tool.completed                           → 下一个 turn 准备开始
 *   reasoning.available                       → 不转发(hermes 思考过程)
 *   run.completed { output, usage }           → 最终 flush
 *   run.failed / run.cancelled                → reject
 *
 * @param {Array} messages       OpenAI 格式(含 system / user / assistant 多轮)
 * @param {string} sessionId     hermes session(每个 talker 独立)
 * @param {Function} onUtterance async (text, index, isFinal) => Promise<void>
 *                               串行调用;返回的 Promise 完成前不会发下一条
 * @returns {Promise<{output?, usage?}>}
 */
async function callHermesRunStream(messages, sessionId, onUtterance) {
  const userMsg = messages[messages.length - 1]?.content || '';
  const systemMsg = messages.find(m => m.role === 'system');
  const history = messages.slice(0, -1)
    .filter(m => m.role !== 'system')
    .map(m => ({ role: m.role, content: m.content }));

  // 1) POST /v1/runs 拿 run_id
  const startBody = JSON.stringify({
    input: userMsg,
    conversation_history: history,
    session_id: sessionId,
    instructions: systemMsg?.content,
  });

  const runId = await new Promise((resolve, reject) => {
    const req = http.request({
      hostname: CONFIG.hermesHost,
      port: CONFIG.hermesPort,
      path: '/v1/runs',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(startBody),
      },
      timeout: 15000,
    }, res => {
      let buf = '';
      res.on('data', c => buf += c.toString());
      res.on('end', () => {
        try {
          const j = JSON.parse(buf);
          if (j.run_id) return resolve(j.run_id);
          reject(new Error('No run_id in response: ' + buf.substring(0, 200)));
        } catch (e) { reject(e); }
      });
    });
    req.on('error', reject);
    req.on('timeout', () => { req.destroy(); reject(new Error('start timeout')); });
    req.write(startBody);
    req.end();
  });

  log('[HERMES] Run started:', runId, 'session:', sessionId);

  // 2) GET /v1/runs/{runId}/events (SSE)
  return new Promise((resolve, reject) => {
    const req = http.get({
      hostname: CONFIG.hermesHost,
      port: CONFIG.hermesPort,
      path: `/v1/runs/${runId}/events`,
      timeout: CONFIG.hermesTimeoutMs,
    }, res => {
      let buf = '';
      let textBuf = '';        // 累积 message.delta(assistant content tokens)
      let reasoningBuf = '';   // 累积 reasoning.delta(Kimi 的 chain-of-thought tokens)
      let utteranceIdx = 0;
      let sendChain = Promise.resolve(); // 串行队列:每个 utterance 在前一个完成后执行
      let finalResult = {};

      // diagnostic counters — message.delta / reasoning.delta 都是高频 token 流,
      // 必须节流否则 600+ events × sync 文件 IO 会卡死 node
      const evCount = {};
      let lastDiagLogAt = 0;
      const HIGH_FREQ_EVENTS = new Set(['message.delta', 'reasoning.delta']);
      const diagLog = (evt) => {
        evCount[evt] = (evCount[evt] || 0) + 1;
        const now = Date.now();
        const isHighFreq = HIGH_FREQ_EVENTS.has(evt);
        if (!isHighFreq || now - lastDiagLogAt > 5000) {
          log('[STREAM EV]', evt, 'counts=', JSON.stringify(evCount),
              'textBuf.len=' + textBuf.length, 'reasoningBuf.len=' + reasoningBuf.length);
          lastDiagLogAt = now;
        }
      };

      // 跟踪上一次实际发出去的文本,避免 reasoning.available 和 run.completed 重复发同样内容
      let lastEmitted = '';
      const emit = (text, isFinal) => {
        const trimmed = (text || '').trim();
        if (!trimmed) return;
        // 简单 dedupe:完全相同 / 完全包含 / 被包含都跳过
        if (trimmed === lastEmitted) return;
        if (lastEmitted && (trimmed.includes(lastEmitted) || lastEmitted.includes(trimmed))) return;
        lastEmitted = trimmed;
        const idx = utteranceIdx++;
        sendChain = sendChain
          .then(() => onUtterance(trimmed, idx, !!isFinal))
          .catch(e => log('[onUtterance error]', e.message));
      };

      // 重新定义 flush 让它走 emit 通道
      const flushTextBuf = (isFinal) => {
        const t = textBuf.trim();
        textBuf = '';
        if (t) emit(t, isFinal);
      };

      // 把累积的 reasoning(Kimi chain-of-thought)作为一段独立 utterance flush
      const flushReasoningBuf = (isFinal) => {
        const t = reasoningBuf.trim();
        reasoningBuf = '';
        if (t) emit(t, isFinal);
      };

      res.setEncoding('utf-8');
      res.on('data', chunk => {
        buf += chunk;
        let sepIdx;
        while ((sepIdx = buf.indexOf('\n\n')) >= 0) {
          const block = buf.substring(0, sepIdx);
          buf = buf.substring(sepIdx + 2);
          for (const line of block.split('\n')) {
            if (!line.startsWith('data: ')) continue;
            let ev;
            try { ev = JSON.parse(line.substring(6)); } catch (_) { continue; }
            diagLog(ev.event);
            switch (ev.event) {
              case 'message.complete':
                // 🎯 主路径:interim_assistant_callback(gateway 平台用的事件)
                // 在每个 turn 末尾、tool 之前 fire,携带模型本轮**给用户看的**
                // 可见 content(已 strip <think> 块)。这是真正的"多步消息"信号。
                // already_streamed=true 表示 content 已通过 message.delta 流过,避免重复
                if (!ev.already_streamed) {
                  emit(ev.text, false);
                  // 同 turn 的 message.delta 已被 interim 覆盖,重置 textBuf
                  textBuf = '';
                }
                // reasoning 同 turn 的 chain-of-thought 已落地,清掉避免泄露给用户
                reasoningBuf = '';
                break;
              case 'message.delta':
                // 模型 content tokens — 主要靠 message.complete 拿干净文本,
                // message.delta 累积仅做兜底(如果 interim 没 fire)
                textBuf += (ev.delta || '');
                break;
              case 'reasoning.delta':
                // Kimi/Moonshot 的 chain-of-thought tokens — 模型内心独白,
                // 默认不发给用户(会出现第三人称"用户要求..."的诡异内容)。
                // STREAM_REASONING=true 时累积,flush 时加 "🧠 " 前缀
                if (CONFIG.streamReasoning) {
                  reasoningBuf += (ev.text || '');
                }
                break;
              case 'reasoning.available':
                // 整段 turn 末尾的 thinking,同样默认不发
                if (CONFIG.streamReasoning && !reasoningBuf.trim() && ev.text) {
                  emit('🧠 ' + ev.text, false);
                }
                break;
              case 'tool.started':
                // turn 边界兜底:interim_assistant_callback 已 emit 过本轮 content,
                // textBuf 仅在 interim 未触发时用
                if (CONFIG.streamReasoning) {
                  const r = reasoningBuf.trim();
                  reasoningBuf = '';
                  if (r) emit('🧠 ' + r, false);
                }
                flushTextBuf(false);
                if (CONFIG.streamToolProgress) {
                  const toolName = ev.tool || '?';
                  const preview = (ev.preview || '').toString().split('\n')[0].substring(0, 60);
                  emit(preview ? `🔧 ${toolName}: ${preview}` : `🔧 ${toolName}`, false);
                }
                break;
              case 'run.completed':
                // 收尾:emit 最终 output(如果跟 last interim 重复会被 dedupe 吃掉)
                if (ev.output) {
                  emit(ev.output, true);
                } else {
                  flushTextBuf(true);
                }
                finalResult = { output: ev.output, usage: ev.usage };
                sendChain.then(() => resolve(finalResult));
                break;
              case 'run.failed':
                reject(new Error(ev.error || 'run failed'));
                break;
              case 'run.cancelled':
                reject(new Error('run cancelled'));
                break;
              // tool.completed / approval.* 忽略
            }
          }
        }
      });
      res.on('end', () => {
        // SSE 流结束兜底:把残留的 reasoning / content flush 掉
        if (CONFIG.streamReasoning) flushReasoningBuf(true);
        flushTextBuf(true);
        sendChain.then(() => resolve(finalResult));
      });
      res.on('error', reject);
    });
    req.on('error', reject);
    req.on('timeout', () => { req.destroy(); reject(new Error('events stream timeout')); });
  });
}

/**
 * 调用 hermes-agent API (OpenAI-compatible) 生成回复 — 老的非流式同步 API
 *
 * @param {Array<{role:string,content:string}>} messages - OpenAI messages 格式
 * @param {string} sessionId - 会话 ID(每个 talker 独立,作为 X-Hermes-Session-Id)
 * @returns {Promise<string|null>} 回复文本
 */
async function callHermesApi(messages, sessionId) {
  return new Promise((resolve) => {
    const payload = JSON.stringify({
      model: 'hermes-agent',
      messages,
      stream: false,
    });
    const req = http.request({
      hostname: CONFIG.hermesHost,
      port: CONFIG.hermesPort,
      path: '/v1/chat/completions',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(payload),
      },
      timeout: CONFIG.hermesTimeoutMs,
    }, (res) => {
      let buffer = '';
      res.on('data', chunk => { buffer += chunk; });
      res.on('end', () => {
        log('[HERMES] HTTP status:', res.statusCode, 'body:', buffer.substring(0, 300));
        try {
          const data = JSON.parse(buffer);
          if (data.error) {
            log('[HERMES] API error:', JSON.stringify(data.error));
            resolve(null);
            return;
          }
          const content = data.choices?.[0]?.message?.content;
          resolve(content || null);
        } catch (e) {
          log('[HERMES] JSON parse error:', e.message);
          resolve(null);
        }
      });
    });

    req.on('error', (err) => {
      log('[HERMES] HTTP error:', err.message);
      resolve(null);
    });
    req.on('timeout', () => {
      req.destroy();
      log('[HERMES] API timeout');
      resolve(null);
    });

    req.write(payload);
    req.end();
  });
}

// ═══════════════════════════════════════════════════════════════
//  回复生成器 — 接入 hermes
// ═══════════════════════════════════════════════════════════════

/**
 * 构造 OpenAI messages 数组发给 hermes-agent
 *
 * 把 talker 的最近 N 条聊天记录作为多轮 messages(自己发的为 assistant、对方发的为 user)。
 * 再把本次触发消息作为最后一个 user turn,并在 system 中说明回复要求。
 */
function buildHermesMessages(ctx) {
  const { type, talker, displayName, content, senderName, history } = ctx;

  let role = '';
  if (type === 'private') {
    role = `这是和「${displayName || talker}」的微信私聊。`;
  } else if (type === 'group-at') {
    role = `这是一个微信群聊,刚才有人 @ 我("${senderName}"),群名 ${displayName || talker}。`;
  } else if (type === 'group-quote') {
    role = `这是一个微信群聊,有人引用了我的消息(发送者 "${senderName}"),群名 ${displayName || talker}。`;
  } else {
    role = `这是来自「${displayName || talker}」的消息。`;
  }

  const systemMsg = {
    role: 'system',
    content: `你是 lemonAI 助手,正在帮我回复微信消息。${role}\n`
      + '回复要自然、简短、得体,使用中文。只输出回复内容,不要加任何解释、不要带前缀如"回复:"。',
  };

  // history 是 [{ isSend, content }] 旧→新;本次消息已经在最后一条
  const msgs = [systemMsg];
  if (Array.isArray(history) && history.length > 0) {
    for (const h of history) {
      const r = h.isSend === 1 ? 'assistant' : 'user';
      // 群聊里对方发的 content 形如 "wxid_x:\n实际内容",去掉前缀
      let txt = h.content || '';
      if (r === 'user' && txt.includes(':\n')) {
        txt = txt.substring(txt.indexOf(':\n') + 2);
      }
      msgs.push({ role: r, content: txt });
    }
  } else {
    // 没有历史时,直接把当前内容作为 user turn
    msgs.push({ role: 'user', content });
  }
  return msgs;
}

/**
 * 接入 hermes-agent 的回复生成器
 */
async function hermesReplyGenerator(ctx) {
  const { talker } = ctx;
  const sessionId = `wx-${talker}`;
  const messages = buildHermesMessages(ctx);

  log('[HERMES] Calling API for', talker, 'session:', sessionId, 'turns=' + messages.length);
  const reply = await callHermesApi(messages, sessionId);

  if (reply) {
    log('[HERMES] Reply:', reply.substring(0, 80));
  } else {
    log('[HERMES] No reply generated');
  }

  return reply;
}

// ═══════════════════════════════════════════════════════════════
//  消息判断
// ═══════════════════════════════════════════════════════════════

function isGroupChat(talker) {
  return talker && talker.endsWith('@chatroom');
}

function isAtMe(content, nickname) {
  if (!nickname) return false;
  const pattern = new RegExp('@' + escapeRegex(nickname) + '(\\s|\\n|$)');
  return pattern.test(content);
}

function isQuoteMe(content, nickname) {
  if (!nickname) return false;
  if (!content.includes('<refermsg')) return false;
  const match = content.match(/<displayname>([^<]+)<\/displayname>/);
  if (match) return match[1] === nickname;
  return false;
}

function escapeRegex(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

/**
 * 解析 owner 的授权指令。返回 { approve, target } 或 null(不是指令)。
 * 例:"同意" → {approve:true, target:''}
 *     "同意 火星人" → {approve:true, target:'火星人'}
 *     "拒绝" → {approve:false, target:''}
 */
function parseApprovalCommand(content) {
  if (!content) return null;
  let replyText = content;
  let quotedContext = '';
  // 引用回复(type 57 appmsg):真正的回复文本在 <title> 里,
  // 被引用的原消息内容在 <refermsg> 里(用来判断"同意哪条")
  if (content.includes('<appmsg') || content.includes('<refermsg>')) {
    const tm = content.match(/<title>([\s\S]*?)<\/title>/);
    if (tm) replyText = tm[1];
    const rm = content.match(/<refermsg>([\s\S]*?)<\/refermsg>/);
    if (rm) quotedContext = rm[1];
  }
  // 群消息前缀剥离(owner 私聊一般没有,保险起见)
  let t = replyText;
  const ci = t.indexOf(':\n');
  if (ci > 0 && ci < 30) t = t.substring(ci + 2);
  t = t.trim();
  const m = t.match(/^(同意|授权|批准|approve|allow|yes|拒绝|忽略|不同意|deny|no)\s*(.*)$/i);
  if (!m) return null;
  const approve = /同意|授权|批准|approve|allow|yes/i.test(m[1]);
  return { approve, target: (m[2] || '').trim(), quotedContext };
}

// ═══════════════════════════════════════════════════════════════
//  屏幕状态检测与微信唤醒(息屏专用)
// ═══════════════════════════════════════════════════════════════

async function isScreenOn() {
  try {
    const out = await shell('dumpsys power | grep "mWakefulness"', 3000);
    return out.includes('mWakefulness=Awake');
  } catch {
    return false;
  }
}

async function isKeyguardLocked() {
  try {
    const out = await shell('dumpsys window | grep "mInputRestricted"', 3000);
    return out.includes('mInputRestricted=true');
  } catch {
    return false;
  }
}

async function isWechatForeground() {
  try {
    // Android 15 上 mCurrentFocus 已废弃,改用 mFocusedApp
    const out = await shell('dumpsys activity activities | grep "mFocusedApp"', 5000);
    return out.includes('com.tencent.mm');
  } catch {
    return false;
  }
}

/**
 * 息屏唤醒完整流程:点亮 → 解锁(无密码时滑动) → 启动微信 → 确认前台
 */
async function wakeWechat() {
  try {
    // 1. 如果屏幕是黑的,用 KEYCODE_WAKEUP(224) 点亮,不会 toggle
    const screenOn = await isScreenOn();
    if (!screenOn) {
      await shell('input keyevent 224', 3000);
      log('[WAKE] Screen woke up');
      await sleep(800);
    }

    // 2. 无密码时,wm dismiss-keyguard 可直接解除锁屏,比滑动更可靠
    try {
      await shell('wm dismiss-keyguard', 3000);
      log('[WAKE] Keyguard dismissed');
    } catch (e) {
      log('[WAKE] dismiss-keyguard failed (may already unlocked):', e.message);
    }
    await sleep(500);

    // 3. 启动微信到前台
    await shell('/system/bin/am start -n com.tencent.mm/.ui.LauncherUI', 5000);
    log('[WAKE] WeChat LauncherUI started');
    await sleep(1000);

    return true;
  } catch (e) {
    log('[WAKE] Failed:', e.message);
    return false;
  }
}

/**
 * 确保微信在前台且屏幕已解锁,轮询最多 20 秒
 */
async function ensureWechatForeground(maxWaitMs = 20000) {
  const start = Date.now();
  while (Date.now() - start < maxWaitMs) {
    const screenOn = await isScreenOn();
    const locked = await isKeyguardLocked();
    const fg = await isWechatForeground();

    if (screenOn && !locked && fg) {
      // 模拟一次点击保持活跃
      try { await shell('input keyevent 62', 1000); } catch (_) {}
      log('[WAKE] Confirmed: screen on + unlocked + WeChat foreground');
      return true;
    }

    if (!screenOn) {
      log('[WAKE] Screen still off, retrying keyevent 224...');
      try { await shell('input keyevent 224', 3000); } catch (_) {}
    } else if (locked) {
      log('[WAKE] Still locked, retrying dismiss-keyguard...');
      try { await shell('wm dismiss-keyguard', 3000); } catch (_) {}
    } else if (!fg) {
      log('[WAKE] WeChat not foreground, retrying start...');
      try { await shell('am start -n com.tencent.mm/.ui.LauncherUI', 5000); } catch (_) {}
    }

    await sleep(1500);
  }
  log('[WAKE] Foreground check timeout');
  return false;
}

// ═══════════════════════════════════════════════════════════════
//  通知监听(息屏兜底)
// ═══════════════════════════════════════════════════════════════

async function pollNotifications(bot) {
  const termuxBin = '/data/data/com.termux/files/usr/bin/termux-notification-list';

  if (!fs.existsSync(termuxBin)) {
    log('[NOTIF] termux-notification-list not found, notification watcher disabled');
    return;
  }

  log('[NOTIF] Notification watcher started');
  const knownKeys = new Set();

  while (bot.running) {
    try {
      const out = await shell(termuxBin, 10000);
      const notifs = JSON.parse(out);
      const wechatNotifs = notifs.filter(n => n.packageName === 'com.tencent.mm');

      for (const n of wechatNotifs) {
        const key = `${n.title}:${n.content}`;
        if (knownKeys.has(key)) continue;
        knownKeys.add(key);

        // 限制 set 大小
        if (knownKeys.size > 200) {
          const arr = Array.from(knownKeys);
          knownKeys.clear();
          arr.slice(-100).forEach(k => knownKeys.add(k));
        }

        log('[NOTIF] WeChat notification:', n.title, n.content.substring(0, 60));

        // 息屏时微信可能不在前台,先点亮+启动微信
        // 剩下的检测和发送交给 sqlcipher 轮询 + handleMessage
        const screenOn = await isScreenOn();
        if (!screenOn) {
          log('[NOTIF] Screen off, waking WeChat...');
          const ok = await wakeWechat();
          if (ok) {
            await ensureWechatForeground(20000);
            log('[NOTIF] WeChat woken up, tracker will catch the message');
          }
        }
      }
    } catch (e) {
      // notification-list 可能返回空或失败,忽略
    }

    await sleep(CONFIG.notificationPollIntervalMs);
  }
}

function sleep(ms) {
  return new Promise(r => setTimeout(r, ms));
}

// ═══════════════════════════════════════════════════════════════
//  主逻辑
// ═══════════════════════════════════════════════════════════════

class AutoReplyBot {
  constructor(replyGenerator = hermesReplyGenerator) {
    // 优先使用 sqlcipher DB 轮询(LSPosed hook 在 WeChat 8.0.72 上不稳定)
    this.tracker = new AndroidWechatTrackerSQLCipher();
    this.state = new ReplyState(CONFIG.stateFile);
    this.whitelist = new WhitelistStore(CONFIG.whitelistFile);
    // owner 永远在白名单里
    if (CONFIG.ownerWxid) this.whitelist.allowed.add(CONFIG.ownerWxid);
    this.replyGenerator = replyGenerator;
    this.running = false;
  }

  async handleMessage(msg) {
    if (msg.type !== 'recv') return;

    const { talker, msgId } = msg;
    let content = msg.content;
    const rawContent = msg.content; // 原始(含 appmsg XML),授权指令解析要用 refermsg
    if (!talker || !content) return;

    // 引用回复(appmsg XML,type=822083633):真正的文本在 <title> 里,
    // @小火子 也在 title 里且后面跟 </title>(不是空格)→ isAtMe 匹配不上。
    // 这里规范化成 "senderPrefix:\n<title 文本>",让下游 @ 检测 / quote / hermes 都用干净文本。
    if (content.includes('<appmsg') || content.includes('<refermsg>')) {
      let prefix = '';
      const pm = content.match(/^([^:\n]+):\n/);
      if (pm) prefix = pm[0];
      const tm = content.match(/<title>([\s\S]*?)<\/title>/);
      if (tm) {
        const title = tm[1].replace(/&#x20;/g, ' ').replace(/&amp;/g, '&').trim();
        content = prefix + title;
        log('[NORMALIZE] quote-reply →', content.substring(0, 60));
      }
    }

    if (CONFIG.ignoreSelf && talker === CONFIG.myWxid) return;
    if (this.state.hasReplied(msgId)) return;
    if (this.state.isCooldown(talker)) {
      log('[COOLDOWN] Skip', talker);
      return;
    }

    let replyType = null;
    let senderName = '';

    if (!isGroupChat(talker)) {
      if (CONFIG.replyInPrivateChat) replyType = 'private';
    } else {
      const nameMatch = content.match(/^([^:\n]+):\n/);
      if (nameMatch) senderName = nameMatch[1];

      if (CONFIG.replyAtInGroup && isAtMe(content, CONFIG.myNickname)) {
        replyType = 'group-at';
      } else if (CONFIG.replyQuoteInGroup && isQuoteMe(content, CONFIG.myNickname)) {
        replyType = 'group-quote';
      }
    }

    if (!replyType) return;

    // ═══ 白名单门禁 + owner 授权流程 ═══
    // 发送者身份:私聊就是 talker,群聊是 content 前缀里的 senderWxid(即 senderName 变量)
    const senderWxid = isGroupChat(talker) ? senderName : talker;
    const isFromOwner = (senderWxid === CONFIG.ownerWxid) || (talker === CONFIG.ownerWxid);

    if (isFromOwner) {
      // owner 自己发的(私聊 / 群里 @):先看是不是授权指令
      // 用 rawContent(含 refermsg)让 parseApprovalCommand 能判断同意哪条
      const cmd = parseApprovalCommand(rawContent);
      if (cmd) {
        this.state.markReplied(msgId, talker);
        await this.handleApproval(cmd);
        return;
      }
      // 否则放行(owner 永远白名单)
    } else if (!this.whitelist.isAllowed(talker)) {
      // 群 / 私聊不在白名单 → 请示 owner,不直接回复
      this.state.markReplied(msgId, talker);
      await this.requestAuthorization(talker, senderName, content);
      return;
    }

    log(`[${replyType.toUpperCase()}] ${talker}: ${content.substring(0, 80)}`);
    await this.generateAndSendReply({ talker, content, msgId, replyType, senderName, raw: msg.raw });
  }

  // 生成并发送回复(白名单通过后 / 授权后调用)
  async generateAndSendReply({ talker, content, msgId, replyType, senderName, raw }) {
    // 发送前确保微信在前台(息屏时通知监听会先唤醒)
    const fg = await isWechatForeground();
    if (!fg) {
      log('[WECHAT] Not foreground, waking...');
      await wakeWechat();
      const ok = await ensureWechatForeground();
      if (!ok) {
        log('[WECHAT] Could not bring WeChat to foreground, skip sending');
        return;
      }
      await sleep(2000); // 额外等待网络初始化
    }

    // 查 displayName(A11y 用它来在搜索框里搜联系人)
    let displayName;
    try {
      displayName = await this.tracker.getContactNickname(talker);
    } catch (e) {
      displayName = talker;
    }
    if (!displayName || displayName === talker) {
      log('[WARN] No displayName resolved for', talker, '— A11y 搜索可能失败');
    }

    // 拉历史消息作为 hermes 上下文
    let history = [];
    try {
      history = await this.tracker.fetchHistory(talker, CONFIG.historyContextSize);
    } catch (e) {
      log('[HISTORY] fetch failed:', e.message);
    }

    // 流式 hermes 调用直接在 onUtterance 里发送,不再先生成完整文本
    // 群里被 @ 时,第一条回复带 quote + atSender;多步流式回复后续条仅文本
    const baseSendOpts = { toUserDisplayName: displayName };
    let firstSendOpts = { ...baseSendOpts };
    if (replyType === 'group-at' || replyType === 'group-quote') {
      let bubbleText = content;
      const colonIdx = bubbleText.indexOf(':\n');
      if (colonIdx > 0) bubbleText = bubbleText.substring(colonIdx + 2);
      const trimmed = bubbleText.replace(/<[^>]+>/g, '').trim();
      if (trimmed) firstSendOpts.quoteText = trimmed.substring(0, 30);
      if (senderName) {
        try {
          const senderNick = await this.tracker.getContactNickname(senderName);
          firstSendOpts.atSender = senderNick || senderName;
        } catch (_) {
          firstSendOpts.atSender = senderName;
        }
      }
      log('[GROUP] first-reply quote=' + (firstSendOpts.quoteText || '-')
          + ' atSender=' + (firstSendOpts.atSender || '-'));
    }

    // 走流式:每个 hermes turn → 一条独立微信消息
    // 第一条带 quote + atSender,后续条仅文本(避免重复引用 / @)
    const onUtterance = async (text, idx, isFinal) => {
      // 单条限长保护
      let toSend = text;
      if (toSend.length > CONFIG.maxReplyLength) {
        toSend = toSend.substring(0, CONFIG.maxReplyLength) + '...';
      }
      const opts = (idx === 0) ? firstSendOpts : baseSendOpts;
      log('[UTTER]', idx, isFinal ? '(final)' : '', toSend.substring(0, 60));
      try {
        const r = await sendTextMessageBroadcast(talker, toSend, opts);
        if (r.ok) {
          log('[SENT]', talker, `#${idx}`, toSend.substring(0, 60));
        } else {
          log('[SEND FAILED]', talker, r.error);
        }
      } catch (e) {
        log('[SEND ERROR]', e.message);
      }
      // 等 A11y 跑完 chat-list-click → quote → @ → type → send 完整一轮
      // 不等的话下一条 broadcast 会打断 A11y 当前状态
      await sleep(idx === 0 ? 14000 : 9000); // 第一条带 quote+@ 多花时间
    };

    try {
      const sessionId = `wx-${talker}`;
      const messages = buildHermesMessages({
        type: replyType, talker, displayName, content, senderName, history, raw,
      });
      log('[HERMES] Streaming for', talker, 'turns=' + messages.length);
      const result = await callHermesRunStream(messages, sessionId, onUtterance);
      log('[HERMES] Run done. usage=' + JSON.stringify(result.usage || {}));
      if (msgId != null) this.state.markReplied(msgId, talker);
    } catch (e) {
      log('[STREAM ERROR]', e.message);
    }
  }

  // 给 owner 发一条纯文本消息(授权请求 / 确认),不走 hermes
  async sendPlainTo(toUser, displayName, text) {
    const fg = await isWechatForeground();
    if (!fg) {
      await wakeWechat();
      await ensureWechatForeground();
      await sleep(2000);
    }
    try {
      const r = await sendTextMessageBroadcast(toUser, text, { toUserDisplayName: displayName });
      log('[AUTH SEND]', toUser, r.ok ? 'OK' : r.error);
      await sleep(10000); // 等 A11y 跑完
    } catch (e) {
      log('[AUTH SEND ERROR]', e.message);
    }
  }

  // 非白名单消息 → 请示 owner
  async requestAuthorization(talker, senderName, content) {
    if (this.whitelist.hasPending(talker)) {
      log('[AUTH] Already pending for', talker, '— skip duplicate ask');
      return;
    }
    let dn = talker;
    try { dn = await this.tracker.getContactNickname(talker); } catch (_) {}

    // 群消息 content 形如 "senderWxid:\n实际内容",剥掉前缀
    let cleanContent = content;
    const ci = cleanContent.indexOf(':\n');
    if (ci > 0) cleanContent = cleanContent.substring(ci + 2);
    cleanContent = cleanContent.replace(/<[^>]+>/g, '').trim().substring(0, 100);

    const who = isGroupChat(talker)
      ? `群「${dn}」里的 ${senderName || '某人'}`
      : `${dn}`;

    this.whitelist.addPending(talker, {
      senderName, content, displayName: dn, ts: Date.now(),
    });
    log('[AUTH] Pending authorization for', talker, '(' + dn + ')');

    const ask = `【授权请求】${who} 给我发消息了:\n「${cleanContent}」\n\n`
      + `回复「同意」我就自动回复 TA;回复「拒绝」忽略。`
      + `(多个待授权时用「同意 ${dn}」指定)`;
    await this.sendPlainTo(CONFIG.ownerWxid, CONFIG.ownerDisplayName, ask);
  }

  // 处理 owner 的授权指令
  async handleApproval(cmd) {
    const { approve, target, quotedContext } = cmd;
    const pendings = this.whitelist.pendingTalkers();
    if (pendings.length === 0) {
      await this.sendPlainTo(CONFIG.ownerWxid, CONFIG.ownerDisplayName, '当前没有待授权的请求。');
      return;
    }

    // 选定目标,优先级:
    //   1. 显式 target("同意 宜创AI全员群")按 wxid/昵称/senderName 匹配
    //   2. 引用回复的被引用内容里出现哪个 pending 的 displayName("同意"引用了某条授权请求)
    //   3. 都没有 → 取最近一个 pending
    let talker = null;
    if (target) {
      talker = pendings.find(t => {
        const p = this.whitelist.pending[t];
        return t === target
          || p.displayName === target
          || p.senderName === target
          || (p.displayName && (p.displayName.includes(target) || target.includes(p.displayName)));
      });
    }
    if (!talker && quotedContext) {
      talker = pendings.find(t => {
        const dn = this.whitelist.pending[t].displayName;
        return dn && quotedContext.includes(dn);
      });
    }
    if (!talker) talker = pendings[pendings.length - 1];

    const p = this.whitelist.pending[talker];
    if (approve) {
      this.whitelist.allow(talker);
      log('[AUTH] Approved', talker, '(' + p.displayName + ')');
      await this.sendPlainTo(CONFIG.ownerWxid, CONFIG.ownerDisplayName,
        `已授权「${p.displayName}」,正在回复 TA 刚才的消息,之后也会自动回复。`);
      // 回复 TA 刚才那条 pending 消息
      let replyType = 'private';
      let senderName = p.senderName || '';
      if (isGroupChat(talker)) {
        replyType = isAtMe(p.content, CONFIG.myNickname) ? 'group-at' : 'group-quote';
      }
      await this.generateAndSendReply({
        talker, content: p.content, msgId: null, replyType, senderName, raw: '',
      });
    } else {
      this.whitelist.removePending(talker);
      log('[AUTH] Denied', talker, '(' + p.displayName + ')');
      await this.sendPlainTo(CONFIG.ownerWxid, CONFIG.ownerDisplayName,
        `已忽略「${p.displayName}」的请求。`);
    }
  }

  async start() {
    if (this.running) return;
    this.running = true;

    log('=== Auto-Reply Bot Started ===');
    log('Hermes:', `${CONFIG.hermesHost}:${CONFIG.hermesPort}`);
    log('Config:', JSON.stringify({
      myWxid: CONFIG.myWxid ? '***' : '(not set)',
      myNickname: CONFIG.myNickname || '(not set)',
      replyInPrivateChat: CONFIG.replyInPrivateChat,
      replyAtInGroup: CONFIG.replyAtInGroup,
      replyQuoteInGroup: CONFIG.replyQuoteInGroup,
      historyContextSize: CONFIG.historyContextSize,
      notificationEnabled: CONFIG.notificationEnabled,
      ownerWxid: CONFIG.ownerWxid,
      ownerDisplayName: CONFIG.ownerDisplayName,
    }));
    log('[WHITELIST] allowed:', JSON.stringify(Array.from(this.whitelist.allowed)),
        'pending:', JSON.stringify(this.whitelist.pendingTalkers()));

    // A11y service 健康检查 — 发送 broadcast 必须依赖它
    const a11y = await checkAccessibilityServiceEnabled();
    if (!a11y.enabled) {
      log('[A11Y] NOT enabled (raw:', a11y.raw, '), trying to enable...');
      const en = await enableAccessibilityService();
      if (en.ok) {
        log('[A11Y] Enabled OK');
      } else {
        log('[A11Y] FAILED to enable:', en.error || '(unknown)');
        log('[A11Y] Manual fix: adb shell "su -c \'settings put secure enabled_accessibility_services com.hermes.wechat.xposed/com.hermes.wechat.xposed.accessibility.HermesAccessibilityService\'"');
      }
    } else {
      log('[A11Y] HermesAccessibilityService is enabled');
    }

    // 启动 tracker(sqlcipher DB 轮询,唯一稳定的消息检测方式)
    this.tracker.on('message', msg => this.handleMessage(msg));
    this.tracker.on('error', err => log('[TRACKER ERROR]', err.message));
    await this.tracker.init();
    this.tracker.startPolling(3000);
    log('[TRACKER] DB polling started (sqlcipher)');

    // 启动通知监听(息屏兜底:检测到通知时点亮屏幕+启动微信)
    if (CONFIG.notificationEnabled) {
      this.notifLoop = pollNotifications(this);
    }

    process.on('SIGINT', () => this.stop());
    process.on('SIGTERM', () => this.stop());
  }

  stop() {
    if (!this.running) return;
    this.running = false;
    this.tracker.stopPolling();
    log('=== Auto-Reply Bot Stopped ===');
    process.exit(0);
  }
}

// ═══════════════════════════════════════════════════════════════
//  入口
// ═══════════════════════════════════════════════════════════════

if (require.main === module) {
  // 全局兜底:Node 默认遇 unhandledRejection / uncaughtException 会退出进程,
  // 流式 + sendChain Promise 链路某处漏接 reject 会导致 node 崩 → keepalive 重启
  // → 打断正在跑的 hermes run。这里捕获并记日志,保持进程存活。
  process.on('unhandledRejection', (reason) => {
    log('[UNHANDLED REJECTION]', (reason && reason.stack) || String(reason));
  });
  process.on('uncaughtException', (err) => {
    log('[UNCAUGHT EXCEPTION]', (err && err.stack) || String(err));
  });

  if (process.env.MY_WXID) CONFIG.myWxid = process.env.MY_WXID;
  if (process.env.MY_NICKNAME) CONFIG.myNickname = process.env.MY_NICKNAME;

  if (!CONFIG.myWxid || !CONFIG.myNickname) {
    console.error('');
    console.error('ERROR: 请设置 MY_WXID 和 MY_NICKNAME 环境变量');
    console.error('');
    console.error('用法示例:');
    console.error('  MY_WXID=wxid_xxxxxx MY_NICKNAME=小明 nohup node auto-reply.js &');
    console.error('');
    process.exit(1);
  }

  const bot = new AutoReplyBot();
  bot.start();
}

module.exports = { AutoReplyBot, hermesReplyGenerator, callHermesApi, CONFIG };