dsh-message-gateway 0.1.18 → 0.1.19
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 +41 -45
- package/lib/index.js.map +3 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -4373,18 +4373,18 @@ var ImapClient = class {
|
|
|
4373
4373
|
return match[1].trim().split(/\s+/).filter((s) => s !== "").map(Number);
|
|
4374
4374
|
}
|
|
4375
4375
|
/** FETCH 头部与正文(BODY.PEEK 不置已读)。 */
|
|
4376
|
-
async fetchMessage(
|
|
4376
|
+
async fetchMessage(id) {
|
|
4377
4377
|
const res = await this.command(
|
|
4378
|
-
`FETCH ${
|
|
4378
|
+
`FETCH ${id} (UID BODY.PEEK[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)] BODY.PEEK[TEXT])`,
|
|
4379
4379
|
3e4
|
|
4380
4380
|
);
|
|
4381
|
-
const uid = res.match(/UID (\d+)/)?.[1] ?? String(
|
|
4381
|
+
const uid = res.match(/UID (\d+)/)?.[1] ?? String(id);
|
|
4382
4382
|
const header = literalAt(res, "BODY[HEADER.FIELDS (FROM SUBJECT MESSAGE-ID REFERENCES IN-REPLY-TO)]")?.value ?? "";
|
|
4383
4383
|
const text = literalAt(res, "BODY[TEXT]")?.value ?? "";
|
|
4384
4384
|
return { uid, header, text };
|
|
4385
4385
|
}
|
|
4386
|
-
async markSeen(
|
|
4387
|
-
await this.command(`STORE ${
|
|
4386
|
+
async markSeen(id) {
|
|
4387
|
+
await this.command(`STORE ${id} +FLAGS (\\Seen)`);
|
|
4388
4388
|
}
|
|
4389
4389
|
close() {
|
|
4390
4390
|
try {
|
|
@@ -4647,9 +4647,9 @@ var EmailBridge = class {
|
|
|
4647
4647
|
this.setStatus("connected", "email");
|
|
4648
4648
|
}
|
|
4649
4649
|
const ids = await this.imap.searchUnseen();
|
|
4650
|
-
for (const
|
|
4650
|
+
for (const id of ids) {
|
|
4651
4651
|
if (this.stopped) break;
|
|
4652
|
-
await this.processMessage(
|
|
4652
|
+
await this.processMessage(id);
|
|
4653
4653
|
}
|
|
4654
4654
|
await sleep(POLL_MS);
|
|
4655
4655
|
} catch (error) {
|
|
@@ -4661,16 +4661,16 @@ var EmailBridge = class {
|
|
|
4661
4661
|
}
|
|
4662
4662
|
}
|
|
4663
4663
|
}
|
|
4664
|
-
async processMessage(
|
|
4664
|
+
async processMessage(id) {
|
|
4665
4665
|
if (this.imap === null) return;
|
|
4666
4666
|
let fetched;
|
|
4667
4667
|
try {
|
|
4668
|
-
fetched = await this.imap.fetchMessage(
|
|
4668
|
+
fetched = await this.imap.fetchMessage(id);
|
|
4669
4669
|
} catch {
|
|
4670
4670
|
return;
|
|
4671
4671
|
}
|
|
4672
4672
|
if (this.seen.has(fetched.uid)) {
|
|
4673
|
-
void this.imap.markSeen(
|
|
4673
|
+
void this.imap.markSeen(id).catch(() => {
|
|
4674
4674
|
});
|
|
4675
4675
|
return;
|
|
4676
4676
|
}
|
|
@@ -4682,7 +4682,7 @@ var EmailBridge = class {
|
|
|
4682
4682
|
const from = parseAddress(headerField(fetched.header, "From"));
|
|
4683
4683
|
const subject = headerField(fetched.header, "Subject");
|
|
4684
4684
|
const text = cleanBody(fetched.text);
|
|
4685
|
-
void this.imap.markSeen(
|
|
4685
|
+
void this.imap.markSeen(id).catch(() => {
|
|
4686
4686
|
});
|
|
4687
4687
|
if (from === "" || text.trim() === "") return;
|
|
4688
4688
|
const frame = { from, subject, threadKey };
|
|
@@ -4797,10 +4797,6 @@ var BridgeManager = class {
|
|
|
4797
4797
|
void this.pushStream(p, true);
|
|
4798
4798
|
this.finishPending(key);
|
|
4799
4799
|
return;
|
|
4800
|
-
} else {
|
|
4801
|
-
id.sink.stream(id.frame, p.streamId, "\u2705 \u5904\u7406\u5B8C\u6210", true);
|
|
4802
|
-
this.finishPending(key);
|
|
4803
|
-
return;
|
|
4804
4800
|
}
|
|
4805
4801
|
}
|
|
4806
4802
|
}
|
|
@@ -4978,26 +4974,26 @@ var BridgeManager = class {
|
|
|
4978
4974
|
* 外部平台消息统一入口:命令优先,否则注入该聊天独立 agent 会话,
|
|
4979
4975
|
* 回复经 sink 流式回发。所有已打通的平台共用此管线。
|
|
4980
4976
|
*/
|
|
4981
|
-
async handleExternalMessage(
|
|
4977
|
+
async handleExternalMessage(id, rawText) {
|
|
4982
4978
|
if (this.disposed) return;
|
|
4983
4979
|
const text = stripMention(rawText);
|
|
4984
4980
|
const reply = (content) => {
|
|
4985
|
-
|
|
4981
|
+
id.sink.stream(id.frame, `cmd-${Date.now().toString(36)}`, content, true);
|
|
4986
4982
|
};
|
|
4987
4983
|
try {
|
|
4988
|
-
if (
|
|
4989
|
-
if (await this.handleCommand(text,
|
|
4984
|
+
if (id.chatType === "group" && !this.config.groupReply) return;
|
|
4985
|
+
if (await this.handleCommand(text, id.key, reply)) return;
|
|
4990
4986
|
if (this.awaiting !== null) {
|
|
4991
4987
|
console.warn("[dsh-message-gateway] busy: webhook in flight, drop message");
|
|
4992
4988
|
return;
|
|
4993
4989
|
}
|
|
4994
|
-
if (this.pendingMap.has(
|
|
4995
|
-
|
|
4990
|
+
if (this.pendingMap.has(id.key)) {
|
|
4991
|
+
id.sink.stream(id.frame, `busy-${Date.now().toString(36)}`, this.t("busy"), true);
|
|
4996
4992
|
return;
|
|
4997
4993
|
}
|
|
4998
|
-
const route = this.matchRoute(
|
|
4994
|
+
const route = this.matchRoute(id.key, text);
|
|
4999
4995
|
const routedText = route !== null && route.prefix !== "" ? text.slice(route.prefix.length).trim() : text;
|
|
5000
|
-
const agent = await this.ensureAgentForKey(
|
|
4996
|
+
const agent = await this.ensureAgentForKey(id.key, route?.rule);
|
|
5001
4997
|
if (agent === null) {
|
|
5002
4998
|
console.warn("[dsh-message-gateway] no dedicated agent available");
|
|
5003
4999
|
return;
|
|
@@ -5010,8 +5006,8 @@ var BridgeManager = class {
|
|
|
5010
5006
|
};
|
|
5011
5007
|
const session = agent.session;
|
|
5012
5008
|
const p = {
|
|
5013
|
-
frame:
|
|
5014
|
-
sink:
|
|
5009
|
+
frame: id.frame,
|
|
5010
|
+
sink: id.sink,
|
|
5015
5011
|
streamId: `gw-${Date.now().toString(36)}`,
|
|
5016
5012
|
buffer: "",
|
|
5017
5013
|
cursor: session.seq,
|
|
@@ -5024,25 +5020,25 @@ var BridgeManager = class {
|
|
|
5024
5020
|
startedAt: Date.now(),
|
|
5025
5021
|
heartbeat: null
|
|
5026
5022
|
};
|
|
5027
|
-
this.pendingMap.set(
|
|
5028
|
-
if (
|
|
5023
|
+
this.pendingMap.set(id.key, p);
|
|
5024
|
+
if (id.sink.ack !== false) {
|
|
5029
5025
|
p.ackSent = true;
|
|
5030
|
-
|
|
5026
|
+
id.sink.stream(id.frame, p.streamId, this.t("ack"), false);
|
|
5031
5027
|
p.heartbeat = setInterval(() => {
|
|
5032
|
-
if (this.pendingMap.get(
|
|
5028
|
+
if (this.pendingMap.get(id.key) !== p || p.pushed) return;
|
|
5033
5029
|
const elapsed = Math.round((Date.now() - p.startedAt) / 1e3);
|
|
5034
|
-
|
|
5030
|
+
id.sink.stream(id.frame, p.streamId, `${this.t("ack")}\uFF08${elapsed}s\uFF09`, false);
|
|
5035
5031
|
}, ACK_HEARTBEAT_INTERVAL);
|
|
5036
5032
|
p.heartbeat.unref?.();
|
|
5037
5033
|
}
|
|
5038
5034
|
agent.send(message, "next-turn", true);
|
|
5039
|
-
console.log("[dsh-message-gateway] sent to agent", { key:
|
|
5035
|
+
console.log("[dsh-message-gateway] sent to agent", { key: id.key, text: sanitizeSecrets(text.slice(0, 60)), baseSeq: p.cursor });
|
|
5040
5036
|
p.timer = setInterval(() => {
|
|
5041
|
-
if (this.pendingMap.get(
|
|
5042
|
-
this.pollPending(
|
|
5037
|
+
if (this.pendingMap.get(id.key) !== p) return;
|
|
5038
|
+
this.pollPending(id.key, p, session);
|
|
5043
5039
|
}, 400);
|
|
5044
5040
|
p.fallback = setTimeout(() => {
|
|
5045
|
-
if (this.pendingMap.get(
|
|
5041
|
+
if (this.pendingMap.get(id.key) === p) this.finishPending(id.key);
|
|
5046
5042
|
}, 2 * 60 * 1e3);
|
|
5047
5043
|
p.fallback.unref?.();
|
|
5048
5044
|
} catch (error) {
|
|
@@ -5483,16 +5479,16 @@ ${secret}`;
|
|
|
5483
5479
|
this.email = null;
|
|
5484
5480
|
}
|
|
5485
5481
|
/** 任意桥接平台的状态(telegram/discord/qq/email)。 */
|
|
5486
|
-
bridgeStatus(
|
|
5487
|
-
if (
|
|
5488
|
-
if (
|
|
5489
|
-
if (
|
|
5490
|
-
if (
|
|
5482
|
+
bridgeStatus(id) {
|
|
5483
|
+
if (id === "telegram") return this.telegram?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5484
|
+
if (id === "discord") return this.discord?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5485
|
+
if (id === "qq") return this.qq?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5486
|
+
if (id === "email") return this.email?.status ?? { state: "idle", detail: "", connectedAt: null };
|
|
5491
5487
|
return { state: "idle", detail: "", connectedAt: null };
|
|
5492
5488
|
}
|
|
5493
5489
|
/** 将桥状态并入存储状态。 */
|
|
5494
|
-
mergeStatus(stored,
|
|
5495
|
-
const live =
|
|
5490
|
+
mergeStatus(stored, id = "wecom-aibot") {
|
|
5491
|
+
const live = id === "wecom-aibot" ? this.wecomStatus() : this.bridgeStatus(id);
|
|
5496
5492
|
if (live.state === "connected") {
|
|
5497
5493
|
return { state: "connected", detail: live.detail, testedAt: live.connectedAt ?? stored?.testedAt ?? null };
|
|
5498
5494
|
}
|
|
@@ -5764,8 +5760,8 @@ var PLATFORMS = [
|
|
|
5764
5760
|
fields: [{ key: "secret", labelKey: "field.webhookSecret", placeholderKey: "field.secret.ph", kind: "secret" }]
|
|
5765
5761
|
}
|
|
5766
5762
|
];
|
|
5767
|
-
function platformDef(
|
|
5768
|
-
return PLATFORMS.find((p) => p.id ===
|
|
5763
|
+
function platformDef(id) {
|
|
5764
|
+
return PLATFORMS.find((p) => p.id === id);
|
|
5769
5765
|
}
|
|
5770
5766
|
async function httpJson(url, init) {
|
|
5771
5767
|
const controller = new AbortController();
|
|
@@ -5803,8 +5799,8 @@ function tcpBanner(host, port, timeoutMs = 8e3) {
|
|
|
5803
5799
|
});
|
|
5804
5800
|
});
|
|
5805
5801
|
}
|
|
5806
|
-
async function testPlatform(
|
|
5807
|
-
switch (
|
|
5802
|
+
async function testPlatform(id, cred) {
|
|
5803
|
+
switch (id) {
|
|
5808
5804
|
case "telegram": {
|
|
5809
5805
|
const { status, body } = await httpJson(`https://api.telegram.org/bot${cred.token ?? ""}/getMe`);
|
|
5810
5806
|
const r = body;
|