dsh-message-gateway 0.1.17 → 0.1.18
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/lib/index.js +47 -72
- package/lib/index.js.map +3 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
|
-
}) : x)(function(x) {
|
|
6
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
-
});
|
|
9
3
|
var __esm = (fn, res) => function __init() {
|
|
10
4
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
11
5
|
};
|
|
@@ -4379,18 +4373,18 @@ var ImapClient = class {
|
|
|
4379
4373
|
return match[1].trim().split(/\s+/).filter((s) => s !== "").map(Number);
|
|
4380
4374
|
}
|
|
4381
4375
|
/** FETCH 头部与正文(BODY.PEEK 不置已读)。 */
|
|
4382
|
-
async fetchMessage(
|
|
4376
|
+
async fetchMessage(id2) {
|
|
4383
4377
|
const res = await this.command(
|
|
4384
|
-
`FETCH ${
|
|
4378
|
+
`FETCH ${id2} (UID BODY.PEEK[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)] BODY.PEEK[TEXT])`,
|
|
4385
4379
|
3e4
|
|
4386
4380
|
);
|
|
4387
|
-
const uid = res.match(/UID (\d+)/)?.[1] ?? String(
|
|
4381
|
+
const uid = res.match(/UID (\d+)/)?.[1] ?? String(id2);
|
|
4388
4382
|
const header = literalAt(res, "BODY[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)]")?.value ?? "";
|
|
4389
4383
|
const text = literalAt(res, "BODY[TEXT]")?.value ?? "";
|
|
4390
4384
|
return { uid, header, text };
|
|
4391
4385
|
}
|
|
4392
|
-
async markSeen(
|
|
4393
|
-
await this.command(`STORE ${
|
|
4386
|
+
async markSeen(id2) {
|
|
4387
|
+
await this.command(`STORE ${id2} +FLAGS (\\Seen)`);
|
|
4394
4388
|
}
|
|
4395
4389
|
close() {
|
|
4396
4390
|
try {
|
|
@@ -4653,9 +4647,9 @@ var EmailBridge = class {
|
|
|
4653
4647
|
this.setStatus("connected", "email");
|
|
4654
4648
|
}
|
|
4655
4649
|
const ids = await this.imap.searchUnseen();
|
|
4656
|
-
for (const
|
|
4650
|
+
for (const id2 of ids) {
|
|
4657
4651
|
if (this.stopped) break;
|
|
4658
|
-
await this.processMessage(
|
|
4652
|
+
await this.processMessage(id2);
|
|
4659
4653
|
}
|
|
4660
4654
|
await sleep(POLL_MS);
|
|
4661
4655
|
} catch (error) {
|
|
@@ -4667,16 +4661,16 @@ var EmailBridge = class {
|
|
|
4667
4661
|
}
|
|
4668
4662
|
}
|
|
4669
4663
|
}
|
|
4670
|
-
async processMessage(
|
|
4664
|
+
async processMessage(id2) {
|
|
4671
4665
|
if (this.imap === null) return;
|
|
4672
4666
|
let fetched;
|
|
4673
4667
|
try {
|
|
4674
|
-
fetched = await this.imap.fetchMessage(
|
|
4668
|
+
fetched = await this.imap.fetchMessage(id2);
|
|
4675
4669
|
} catch {
|
|
4676
4670
|
return;
|
|
4677
4671
|
}
|
|
4678
4672
|
if (this.seen.has(fetched.uid)) {
|
|
4679
|
-
void this.imap.markSeen(
|
|
4673
|
+
void this.imap.markSeen(id2).catch(() => {
|
|
4680
4674
|
});
|
|
4681
4675
|
return;
|
|
4682
4676
|
}
|
|
@@ -4688,7 +4682,7 @@ var EmailBridge = class {
|
|
|
4688
4682
|
const from = parseAddress(headerField(fetched.header, "From"));
|
|
4689
4683
|
const subject = headerField(fetched.header, "Subject");
|
|
4690
4684
|
const text = cleanBody(fetched.text);
|
|
4691
|
-
void this.imap.markSeen(
|
|
4685
|
+
void this.imap.markSeen(id2).catch(() => {
|
|
4692
4686
|
});
|
|
4693
4687
|
if (from === "" || text.trim() === "") return;
|
|
4694
4688
|
const frame = { from, subject, threadKey };
|
|
@@ -4799,10 +4793,14 @@ var BridgeManager = class {
|
|
|
4799
4793
|
return;
|
|
4800
4794
|
}
|
|
4801
4795
|
if (event.type === "turn/end") {
|
|
4802
|
-
if (p.buffer !== ""
|
|
4796
|
+
if (p.buffer !== "") {
|
|
4803
4797
|
void this.pushStream(p, true);
|
|
4804
4798
|
this.finishPending(key);
|
|
4805
4799
|
return;
|
|
4800
|
+
} else {
|
|
4801
|
+
id.sink.stream(id.frame, p.streamId, "\u2705 \u5904\u7406\u5B8C\u6210", true);
|
|
4802
|
+
this.finishPending(key);
|
|
4803
|
+
return;
|
|
4806
4804
|
}
|
|
4807
4805
|
}
|
|
4808
4806
|
}
|
|
@@ -4980,26 +4978,26 @@ var BridgeManager = class {
|
|
|
4980
4978
|
* 外部平台消息统一入口:命令优先,否则注入该聊天独立 agent 会话,
|
|
4981
4979
|
* 回复经 sink 流式回发。所有已打通的平台共用此管线。
|
|
4982
4980
|
*/
|
|
4983
|
-
async handleExternalMessage(
|
|
4981
|
+
async handleExternalMessage(id2, rawText) {
|
|
4984
4982
|
if (this.disposed) return;
|
|
4985
4983
|
const text = stripMention(rawText);
|
|
4986
4984
|
const reply = (content) => {
|
|
4987
|
-
|
|
4985
|
+
id2.sink.stream(id2.frame, `cmd-${Date.now().toString(36)}`, content, true);
|
|
4988
4986
|
};
|
|
4989
4987
|
try {
|
|
4990
|
-
if (
|
|
4991
|
-
if (await this.handleCommand(text,
|
|
4988
|
+
if (id2.chatType === "group" && !this.config.groupReply) return;
|
|
4989
|
+
if (await this.handleCommand(text, id2.key, reply)) return;
|
|
4992
4990
|
if (this.awaiting !== null) {
|
|
4993
4991
|
console.warn("[dsh-message-gateway] busy: webhook in flight, drop message");
|
|
4994
4992
|
return;
|
|
4995
4993
|
}
|
|
4996
|
-
if (this.pendingMap.has(
|
|
4997
|
-
|
|
4994
|
+
if (this.pendingMap.has(id2.key)) {
|
|
4995
|
+
id2.sink.stream(id2.frame, `busy-${Date.now().toString(36)}`, this.t("busy"), true);
|
|
4998
4996
|
return;
|
|
4999
4997
|
}
|
|
5000
|
-
const route = this.matchRoute(
|
|
4998
|
+
const route = this.matchRoute(id2.key, text);
|
|
5001
4999
|
const routedText = route !== null && route.prefix !== "" ? text.slice(route.prefix.length).trim() : text;
|
|
5002
|
-
const agent = await this.ensureAgentForKey(
|
|
5000
|
+
const agent = await this.ensureAgentForKey(id2.key, route?.rule);
|
|
5003
5001
|
if (agent === null) {
|
|
5004
5002
|
console.warn("[dsh-message-gateway] no dedicated agent available");
|
|
5005
5003
|
return;
|
|
@@ -5012,8 +5010,8 @@ var BridgeManager = class {
|
|
|
5012
5010
|
};
|
|
5013
5011
|
const session = agent.session;
|
|
5014
5012
|
const p = {
|
|
5015
|
-
frame:
|
|
5016
|
-
sink:
|
|
5013
|
+
frame: id2.frame,
|
|
5014
|
+
sink: id2.sink,
|
|
5017
5015
|
streamId: `gw-${Date.now().toString(36)}`,
|
|
5018
5016
|
buffer: "",
|
|
5019
5017
|
cursor: session.seq,
|
|
@@ -5026,26 +5024,26 @@ var BridgeManager = class {
|
|
|
5026
5024
|
startedAt: Date.now(),
|
|
5027
5025
|
heartbeat: null
|
|
5028
5026
|
};
|
|
5029
|
-
this.pendingMap.set(
|
|
5030
|
-
if (
|
|
5027
|
+
this.pendingMap.set(id2.key, p);
|
|
5028
|
+
if (id2.sink.ack !== false) {
|
|
5031
5029
|
p.ackSent = true;
|
|
5032
|
-
|
|
5030
|
+
id2.sink.stream(id2.frame, p.streamId, this.t("ack"), false);
|
|
5033
5031
|
p.heartbeat = setInterval(() => {
|
|
5034
|
-
if (this.pendingMap.get(
|
|
5032
|
+
if (this.pendingMap.get(id2.key) !== p || p.pushed) return;
|
|
5035
5033
|
const elapsed = Math.round((Date.now() - p.startedAt) / 1e3);
|
|
5036
|
-
|
|
5034
|
+
id2.sink.stream(id2.frame, p.streamId, `${this.t("ack")}\uFF08${elapsed}s\uFF09`, false);
|
|
5037
5035
|
}, ACK_HEARTBEAT_INTERVAL);
|
|
5038
5036
|
p.heartbeat.unref?.();
|
|
5039
5037
|
}
|
|
5040
5038
|
agent.send(message, "next-turn", true);
|
|
5041
|
-
console.log("[dsh-message-gateway] sent to agent", { key:
|
|
5039
|
+
console.log("[dsh-message-gateway] sent to agent", { key: id2.key, text: sanitizeSecrets(text.slice(0, 60)), baseSeq: p.cursor });
|
|
5042
5040
|
p.timer = setInterval(() => {
|
|
5043
|
-
if (this.pendingMap.get(
|
|
5044
|
-
this.pollPending(
|
|
5041
|
+
if (this.pendingMap.get(id2.key) !== p) return;
|
|
5042
|
+
this.pollPending(id2.key, p, session);
|
|
5045
5043
|
}, 400);
|
|
5046
5044
|
p.fallback = setTimeout(() => {
|
|
5047
|
-
if (this.pendingMap.get(
|
|
5048
|
-
},
|
|
5045
|
+
if (this.pendingMap.get(id2.key) === p) this.finishPending(id2.key);
|
|
5046
|
+
}, 2 * 60 * 1e3);
|
|
5049
5047
|
p.fallback.unref?.();
|
|
5050
5048
|
} catch (error) {
|
|
5051
5049
|
console.error("[dsh-message-gateway] handleExternalMessage failed", error);
|
|
@@ -5118,16 +5116,6 @@ var BridgeManager = class {
|
|
|
5118
5116
|
const userid = body.from?.userid ?? "";
|
|
5119
5117
|
const chatid = body.chatid ?? "";
|
|
5120
5118
|
const key = chatid !== "" ? `wecom:group:${chatid}` : `wecom:user:${userid}`;
|
|
5121
|
-
try {
|
|
5122
|
-
const fs = __require("node:fs");
|
|
5123
|
-
const path = __require("node:path");
|
|
5124
|
-
const os = __require("node:os");
|
|
5125
|
-
const target = chatid !== "" ? chatid : userid;
|
|
5126
|
-
if (target) {
|
|
5127
|
-
fs.writeFileSync(path.join(os.homedir(), ".dsh", "latest_wecom_target.txt"), target, "utf8");
|
|
5128
|
-
}
|
|
5129
|
-
} catch {
|
|
5130
|
-
}
|
|
5131
5119
|
const sink = {
|
|
5132
5120
|
stream: (f, sid, content, finish) => void this.wecom?.streamReply(f, sid, content, finish)
|
|
5133
5121
|
// 企业微信走单一流式消息(ack → 内容 → 定稿同一条消息就地更新);
|
|
@@ -5495,16 +5483,16 @@ ${secret}`;
|
|
|
5495
5483
|
this.email = null;
|
|
5496
5484
|
}
|
|
5497
5485
|
/** 任意桥接平台的状态(telegram/discord/qq/email)。 */
|
|
5498
|
-
bridgeStatus(
|
|
5499
|
-
if (
|
|
5500
|
-
if (
|
|
5501
|
-
if (
|
|
5502
|
-
if (
|
|
5486
|
+
bridgeStatus(id2) {
|
|
5487
|
+
if (id2 === "telegram") return this.telegram?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5488
|
+
if (id2 === "discord") return this.discord?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5489
|
+
if (id2 === "qq") return this.qq?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5490
|
+
if (id2 === "email") return this.email?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5503
5491
|
return { state: "idle", detail: "", connectedAt: null };
|
|
5504
5492
|
}
|
|
5505
5493
|
/** 将桥状态并入存储状态。 */
|
|
5506
|
-
mergeStatus(stored,
|
|
5507
|
-
const live =
|
|
5494
|
+
mergeStatus(stored, id2 = "wecom-aibot") {
|
|
5495
|
+
const live = id2 === "wecom-aibot" ? this.wecomStatus() : this.bridgeStatus(id2);
|
|
5508
5496
|
if (live.state === "connected") {
|
|
5509
5497
|
return { state: "connected", detail: live.detail, testedAt: live.connectedAt ?? stored?.testedAt ?? null };
|
|
5510
5498
|
}
|
|
@@ -5776,8 +5764,8 @@ var PLATFORMS = [
|
|
|
5776
5764
|
fields: [{ key: "secret", labelKey: "field.webhookSecret", placeholderKey: "field.secret.ph", kind: "secret" }]
|
|
5777
5765
|
}
|
|
5778
5766
|
];
|
|
5779
|
-
function platformDef(
|
|
5780
|
-
return PLATFORMS.find((p) => p.id ===
|
|
5767
|
+
function platformDef(id2) {
|
|
5768
|
+
return PLATFORMS.find((p) => p.id === id2);
|
|
5781
5769
|
}
|
|
5782
5770
|
async function httpJson(url, init) {
|
|
5783
5771
|
const controller = new AbortController();
|
|
@@ -5815,8 +5803,8 @@ function tcpBanner(host, port, timeoutMs = 8e3) {
|
|
|
5815
5803
|
});
|
|
5816
5804
|
});
|
|
5817
5805
|
}
|
|
5818
|
-
async function testPlatform(
|
|
5819
|
-
switch (
|
|
5806
|
+
async function testPlatform(id2, cred) {
|
|
5807
|
+
switch (id2) {
|
|
5820
5808
|
case "telegram": {
|
|
5821
5809
|
const { status, body } = await httpJson(`https://api.telegram.org/bot${cred.token ?? ""}/getMe`);
|
|
5822
5810
|
const r = body;
|
|
@@ -6605,19 +6593,6 @@ function registerGatewayRoutes(ctx, manager) {
|
|
|
6605
6593
|
json(res, { ok: true, value: { sent: true } });
|
|
6606
6594
|
return;
|
|
6607
6595
|
}
|
|
6608
|
-
if (path === "/gateway/recent-target") {
|
|
6609
|
-
const fs = await import("node:fs");
|
|
6610
|
-
const pathModule = await import("node:path");
|
|
6611
|
-
const os = await import("node:os");
|
|
6612
|
-
const targetFile = pathModule.join(os.homedir(), ".dsh", "latest_wecom_target.txt");
|
|
6613
|
-
let target = "";
|
|
6614
|
-
try {
|
|
6615
|
-
target = fs.readFileSync(targetFile, "utf8").trim();
|
|
6616
|
-
} catch {
|
|
6617
|
-
}
|
|
6618
|
-
json(res, { ok: true, value: { target } });
|
|
6619
|
-
return;
|
|
6620
|
-
}
|
|
6621
6596
|
if (path === "/gateway/send") {
|
|
6622
6597
|
const body = payload;
|
|
6623
6598
|
const chatid = typeof body?.chatid === "string" ? body.chatid.trim() : "";
|