@agent-wechat/wechat 0.2.4 → 0.3.1

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 (2) hide show
  1. package/dist/index.js +62 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -5542,11 +5542,22 @@ function isMessageAllowed(account, isGroup, senderId) {
5542
5542
  }
5543
5543
  return true;
5544
5544
  }
5545
+ function enqueueWeChatSystemEvent(text, contextKey) {
5546
+ try {
5547
+ const core = getWeChatRuntime();
5548
+ core.system.enqueueSystemEvent(text, {
5549
+ sessionKey: "agent:main:main",
5550
+ contextKey
5551
+ });
5552
+ } catch {
5553
+ }
5554
+ }
5545
5555
  async function startWeChatMonitor(opts) {
5546
5556
  const { account, abortSignal, runtime: runtime2, setStatus, log } = opts;
5547
5557
  const client = new WeChatClient({ baseUrl: account.serverUrl, token: account.token });
5548
5558
  const lastSeenId = /* @__PURE__ */ new Map();
5549
5559
  let lastAuthCheck = 0;
5560
+ let prevStatus = void 0;
5550
5561
  setStatus({
5551
5562
  accountId: account.accountId,
5552
5563
  running: true,
@@ -5561,14 +5572,26 @@ async function startWeChatMonitor(opts) {
5561
5572
  lastAuthCheck = now;
5562
5573
  try {
5563
5574
  const auth = await client.authStatus();
5575
+ const isLinked = auth.status === "logged_in";
5564
5576
  setStatus({
5565
5577
  accountId: account.accountId,
5566
5578
  running: true,
5567
5579
  connected: true,
5568
- linked: auth.isLoggedIn
5580
+ linked: isLinked,
5581
+ authStatus: auth.status
5569
5582
  });
5570
- if (!auth.isLoggedIn) {
5571
- log?.info?.(`[wechat:${account.accountId}] Not authenticated`);
5583
+ if (prevStatus === "logged_in" && !isLinked) {
5584
+ const msg = auth.status === "app_not_running" ? "[WeChat] Application stopped. It will restart automatically \u2014 you may need to log in again." : "[WeChat] Session lost. Please log in again using the wechat_login tool.";
5585
+ enqueueWeChatSystemEvent(msg, "wechat:auth_lost");
5586
+ } else if (prevStatus === void 0 && !isLinked) {
5587
+ enqueueWeChatSystemEvent(
5588
+ "[WeChat] Not logged in. Use the wechat_login tool to authenticate.",
5589
+ "wechat:auth_required"
5590
+ );
5591
+ }
5592
+ prevStatus = auth.status;
5593
+ if (!isLinked) {
5594
+ log?.info?.(`[wechat:${account.accountId}] Not authenticated (status: ${auth.status})`);
5572
5595
  await sleep(account.pollIntervalMs, abortSignal);
5573
5596
  continue;
5574
5597
  }
@@ -5580,6 +5603,13 @@ async function startWeChatMonitor(opts) {
5580
5603
  linked: false,
5581
5604
  lastError: String(err)
5582
5605
  });
5606
+ if (prevStatus === "logged_in") {
5607
+ enqueueWeChatSystemEvent(
5608
+ "[WeChat] Cannot reach agent-wechat server. The container may have stopped.",
5609
+ "wechat:server_unreachable"
5610
+ );
5611
+ }
5612
+ prevStatus = void 0;
5583
5613
  log?.error?.(
5584
5614
  `[wechat:${account.accountId}] Auth check failed: ${err}`
5585
5615
  );
@@ -5738,7 +5768,8 @@ ${replyBlock}` : replyBlock;
5738
5768
  senderId,
5739
5769
  isGroup,
5740
5770
  timestamp,
5741
- hasMedia
5771
+ hasMedia,
5772
+ isMentioned: isGroup && msg.isMentioned === true
5742
5773
  };
5743
5774
  }
5744
5775
  function buildSegments(processed) {
@@ -5770,6 +5801,18 @@ async function dispatchSegment(segment, client, chatId, chat, liveAccount, cfg,
5770
5801
  log?.info?.(
5771
5802
  `[wechat:${liveAccount.accountId}] Dispatching segment: ${segment.length} msg(s), last=${msg.localId}${mediaPath ? ` media=${mediaPath}` : ""}`
5772
5803
  );
5804
+ if (isGroup) {
5805
+ const wechatCfg = cfg?.channels?.wechat;
5806
+ const groupEntry = wechatCfg?.groups?.[chatId];
5807
+ const defaultEntry = wechatCfg?.groups?.["*"];
5808
+ const requireMention = groupEntry?.requireMention ?? defaultEntry?.requireMention ?? true;
5809
+ if (requireMention && !lastMsg.isMentioned) {
5810
+ log?.info?.(
5811
+ `[wechat:${liveAccount.accountId}] Skipping group message (mention required, not mentioned) in ${chatId}`
5812
+ );
5813
+ return;
5814
+ }
5815
+ }
5773
5816
  try {
5774
5817
  const route = core.channel.routing.resolveAgentRoute({
5775
5818
  cfg,
@@ -5856,6 +5899,7 @@ async function dispatchSegment(segment, client, chatId, chat, liveAccount, cfg,
5856
5899
  Provider: "wechat",
5857
5900
  Surface: "wechat",
5858
5901
  MessageSid: `wechat:${chatId}:${msg.localId}`,
5902
+ WasMentioned: isGroup ? lastMsg.isMentioned : void 0,
5859
5903
  OriginatingChannel: "wechat",
5860
5904
  OriginatingTo: `wechat:${chatId}`,
5861
5905
  ...mediaPath ? { MediaPath: mediaPath, MediaUrl: mediaPath, MediaType: mediaMime } : {},
@@ -6284,7 +6328,7 @@ var wechatOnboardingAdapter = {
6284
6328
  }
6285
6329
  try {
6286
6330
  const auth = await client.authStatus();
6287
- if (auth.isLoggedIn) {
6331
+ if (auth.status === "logged_in") {
6288
6332
  lines.push(
6289
6333
  `Logged in${auth.loggedInUser ? ` as ${auth.loggedInUser}` : ""}`
6290
6334
  );
@@ -6330,7 +6374,7 @@ var wechatOnboardingAdapter = {
6330
6374
  }
6331
6375
  try {
6332
6376
  const auth = await client.authStatus();
6333
- if (!auth.isLoggedIn) {
6377
+ if (auth.status !== "logged_in") {
6334
6378
  const wantsLink = await prompter.confirm({
6335
6379
  message: "WeChat not logged in. Link now?",
6336
6380
  default: true
@@ -6458,6 +6502,14 @@ async function collectWeChatStatusIssues(accounts) {
6458
6502
  message: snapshot.lastError ? `Cannot reach agent-wechat server: ${snapshot.lastError}` : "Cannot reach agent-wechat server.",
6459
6503
  fix: "Ensure the agent-wechat container is running (pnpm cli up)"
6460
6504
  });
6505
+ } else if (snapshot.authStatus === "app_not_running") {
6506
+ issues.push({
6507
+ channel: "wechat",
6508
+ accountId: snapshot.accountId,
6509
+ kind: "runtime",
6510
+ message: "WeChat application is not running. It should restart automatically.",
6511
+ fix: "If it doesn't restart, try: wx down && wx up"
6512
+ });
6461
6513
  } else if (snapshot.linked === false) {
6462
6514
  issues.push({
6463
6515
  channel: "wechat",
@@ -6504,7 +6556,7 @@ function createWeChatLoginTool(account) {
6504
6556
  case "status": {
6505
6557
  try {
6506
6558
  const auth = await client.authStatus();
6507
- const text = auth.isLoggedIn ? `WeChat is logged in${auth.loggedInUser ? ` as ${auth.loggedInUser}` : ""}.` : "WeChat is not logged in.";
6559
+ const text = auth.status === "logged_in" ? `WeChat is logged in${auth.loggedInUser ? ` as ${auth.loggedInUser}` : ""}.` : `WeChat status: ${auth.status.replace(/_/g, " ")}.`;
6508
6560
  return {
6509
6561
  content: [{ type: "text", text }],
6510
6562
  details: auth
@@ -6882,7 +6934,7 @@ var wechatPlugin = {
6882
6934
  const client = new WeChatClient({ baseUrl: account.serverUrl, token: account.token });
6883
6935
  try {
6884
6936
  const auth = await client.authStatus();
6885
- if (auth.isLoggedIn) {
6937
+ if (auth.status === "logged_in") {
6886
6938
  runtime2.log(
6887
6939
  `Already logged in${auth.loggedInUser ? ` as ${auth.loggedInUser}` : ""}`
6888
6940
  );
@@ -6960,7 +7012,7 @@ Error: ${event.message}`);
6960
7012
  const client = new WeChatClient({ baseUrl: account.serverUrl, token: account.token });
6961
7013
  try {
6962
7014
  const auth = await client.authStatus();
6963
- if (!auth.isLoggedIn || !auth.loggedInUser) return null;
7015
+ if (auth.status !== "logged_in" || !auth.loggedInUser) return null;
6964
7016
  return { kind: "user", id: auth.loggedInUser };
6965
7017
  } catch {
6966
7018
  return null;
@@ -7007,7 +7059,7 @@ Error: ${event.message}`);
7007
7059
  const client = new WeChatClient({ baseUrl: account.serverUrl, token: account.token });
7008
7060
  try {
7009
7061
  const auth = await client.authStatus();
7010
- if (!auth.isLoggedIn) {
7062
+ if (auth.status !== "logged_in") {
7011
7063
  return { ok: false, reason: "wechat-not-logged-in" };
7012
7064
  }
7013
7065
  return { ok: true, reason: "ok" };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-wechat/wechat",
3
- "version": "0.2.4",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",