@agent-wechat/wechat 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +47 -9
- 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:
|
|
5580
|
+
linked: isLinked,
|
|
5581
|
+
authStatus: auth.status
|
|
5569
5582
|
});
|
|
5570
|
-
if (!
|
|
5571
|
-
log
|
|
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
|
);
|
|
@@ -6284,7 +6314,7 @@ var wechatOnboardingAdapter = {
|
|
|
6284
6314
|
}
|
|
6285
6315
|
try {
|
|
6286
6316
|
const auth = await client.authStatus();
|
|
6287
|
-
if (auth.
|
|
6317
|
+
if (auth.status === "logged_in") {
|
|
6288
6318
|
lines.push(
|
|
6289
6319
|
`Logged in${auth.loggedInUser ? ` as ${auth.loggedInUser}` : ""}`
|
|
6290
6320
|
);
|
|
@@ -6330,7 +6360,7 @@ var wechatOnboardingAdapter = {
|
|
|
6330
6360
|
}
|
|
6331
6361
|
try {
|
|
6332
6362
|
const auth = await client.authStatus();
|
|
6333
|
-
if (
|
|
6363
|
+
if (auth.status !== "logged_in") {
|
|
6334
6364
|
const wantsLink = await prompter.confirm({
|
|
6335
6365
|
message: "WeChat not logged in. Link now?",
|
|
6336
6366
|
default: true
|
|
@@ -6458,6 +6488,14 @@ async function collectWeChatStatusIssues(accounts) {
|
|
|
6458
6488
|
message: snapshot.lastError ? `Cannot reach agent-wechat server: ${snapshot.lastError}` : "Cannot reach agent-wechat server.",
|
|
6459
6489
|
fix: "Ensure the agent-wechat container is running (pnpm cli up)"
|
|
6460
6490
|
});
|
|
6491
|
+
} else if (snapshot.authStatus === "app_not_running") {
|
|
6492
|
+
issues.push({
|
|
6493
|
+
channel: "wechat",
|
|
6494
|
+
accountId: snapshot.accountId,
|
|
6495
|
+
kind: "runtime",
|
|
6496
|
+
message: "WeChat application is not running. It should restart automatically.",
|
|
6497
|
+
fix: "If it doesn't restart, try: wx down && wx up"
|
|
6498
|
+
});
|
|
6461
6499
|
} else if (snapshot.linked === false) {
|
|
6462
6500
|
issues.push({
|
|
6463
6501
|
channel: "wechat",
|
|
@@ -6504,7 +6542,7 @@ function createWeChatLoginTool(account) {
|
|
|
6504
6542
|
case "status": {
|
|
6505
6543
|
try {
|
|
6506
6544
|
const auth = await client.authStatus();
|
|
6507
|
-
const text = auth.
|
|
6545
|
+
const text = auth.status === "logged_in" ? `WeChat is logged in${auth.loggedInUser ? ` as ${auth.loggedInUser}` : ""}.` : `WeChat status: ${auth.status.replace(/_/g, " ")}.`;
|
|
6508
6546
|
return {
|
|
6509
6547
|
content: [{ type: "text", text }],
|
|
6510
6548
|
details: auth
|
|
@@ -6882,7 +6920,7 @@ var wechatPlugin = {
|
|
|
6882
6920
|
const client = new WeChatClient({ baseUrl: account.serverUrl, token: account.token });
|
|
6883
6921
|
try {
|
|
6884
6922
|
const auth = await client.authStatus();
|
|
6885
|
-
if (auth.
|
|
6923
|
+
if (auth.status === "logged_in") {
|
|
6886
6924
|
runtime2.log(
|
|
6887
6925
|
`Already logged in${auth.loggedInUser ? ` as ${auth.loggedInUser}` : ""}`
|
|
6888
6926
|
);
|
|
@@ -6960,7 +6998,7 @@ Error: ${event.message}`);
|
|
|
6960
6998
|
const client = new WeChatClient({ baseUrl: account.serverUrl, token: account.token });
|
|
6961
6999
|
try {
|
|
6962
7000
|
const auth = await client.authStatus();
|
|
6963
|
-
if (
|
|
7001
|
+
if (auth.status !== "logged_in" || !auth.loggedInUser) return null;
|
|
6964
7002
|
return { kind: "user", id: auth.loggedInUser };
|
|
6965
7003
|
} catch {
|
|
6966
7004
|
return null;
|
|
@@ -7007,7 +7045,7 @@ Error: ${event.message}`);
|
|
|
7007
7045
|
const client = new WeChatClient({ baseUrl: account.serverUrl, token: account.token });
|
|
7008
7046
|
try {
|
|
7009
7047
|
const auth = await client.authStatus();
|
|
7010
|
-
if (
|
|
7048
|
+
if (auth.status !== "logged_in") {
|
|
7011
7049
|
return { ok: false, reason: "wechat-not-logged-in" };
|
|
7012
7050
|
}
|
|
7013
7051
|
return { ok: true, reason: "ok" };
|