dsh-message-gateway 0.1.13 → 0.1.15

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 CHANGED
@@ -4635,6 +4635,7 @@ var BridgeManager = class {
4635
4635
  awaiting = null;
4636
4636
  /** 各聊天的独立 agent 会话(key = 平台:chatKey;webhook 固定 'webhook')。 */
4637
4637
  agents = /* @__PURE__ */ new Map();
4638
+ disposed = false;
4638
4639
  /** 当前语言的文案。 */
4639
4640
  t(key) {
4640
4641
  return botText(this.config.botLocale, key);
@@ -4812,14 +4813,32 @@ var BridgeManager = class {
4812
4813
  console.log("[dsh-message-gateway] chat session reset", { key });
4813
4814
  return true;
4814
4815
  }
4815
- /** 插件卸载时释放全部聊天会话。 */
4816
+ /** 插件卸载时释放全部聊天会话与长连接(带超时脱钩保护,防死锁)。 */
4816
4817
  async dispose() {
4818
+ if (this.disposed) return;
4819
+ this.disposed = true;
4820
+ this.wecom?.stop();
4821
+ this.wecom = null;
4822
+ this.telegram?.stop();
4823
+ this.telegram = null;
4824
+ this.discord?.stop();
4825
+ this.discord = null;
4826
+ this.qq?.stop();
4827
+ this.qq = null;
4828
+ this.email?.stop();
4829
+ this.email = null;
4817
4830
  this.finishAllPending();
4818
4831
  const entries = [...this.agents.values()];
4819
4832
  this.agents.clear();
4820
- await Promise.allSettled(entries.map((entry) => entry.dispose()));
4821
- this.wecom?.stop();
4822
- this.wecom = null;
4833
+ const disposePromises = entries.map(
4834
+ (entry) => entry.dispose().catch((error) => {
4835
+ console.warn("[dsh-message-gateway] agent dispose warning", String(error));
4836
+ })
4837
+ );
4838
+ await Promise.race([
4839
+ Promise.allSettled(disposePromises),
4840
+ new Promise((resolve) => setTimeout(resolve, 1500))
4841
+ ]);
4823
4842
  }
4824
4843
  finishAllPending() {
4825
4844
  for (const key of [...this.pendingMap.keys()]) this.finishPending(key);
@@ -4829,6 +4848,7 @@ var BridgeManager = class {
4829
4848
  * 回复经 sink 流式回发。所有已打通的平台共用此管线。
4830
4849
  */
4831
4850
  async handleExternalMessage(id, rawText) {
4851
+ if (this.disposed) return;
4832
4852
  const text = stripMention(rawText);
4833
4853
  const reply = (content) => {
4834
4854
  id.sink.stream(id.frame, `cmd-${Date.now().toString(36)}`, content, true);
@@ -5043,6 +5063,30 @@ ${content}` : content;
5043
5063
  return { ok: false, detail: `platform "${platform}" does not support push` };
5044
5064
  }
5045
5065
  }
5066
+ /**
5067
+ * 向已配置的 Outbound Webhooks 广播事件(异步投递,失败不阻塞)。
5068
+ */
5069
+ async broadcastEvent(event, payload) {
5070
+ const webhooks = this.config.outboundWebhooks ?? [];
5071
+ if (webhooks.length === 0) return;
5072
+ const { createHmac: createHmac2 } = await import("node:crypto");
5073
+ const bodyStr = JSON.stringify({ event, timestamp: Date.now(), ...payload });
5074
+ for (const hook of webhooks) {
5075
+ const allowed = hook.events ?? ["*"];
5076
+ if (!allowed.includes("*") && !allowed.includes(event)) continue;
5077
+ void (async () => {
5078
+ try {
5079
+ const headers = { "content-type": "application/json" };
5080
+ if (hook.secret) {
5081
+ headers["x-gateway-signature"] = createHmac2("sha256", hook.secret).update(bodyStr, "utf8").digest("hex");
5082
+ }
5083
+ await fetch(hook.url, { method: "POST", headers, body: bodyStr, signal: AbortSignal.timeout(1e4) });
5084
+ } catch (error) {
5085
+ console.warn("[dsh-message-gateway] outbound webhook post failed", hook.url, String(error));
5086
+ }
5087
+ })();
5088
+ }
5089
+ }
5046
5090
  // ==================== Telegram / Discord / QQ / Email ====================
5047
5091
  telegram = null;
5048
5092
  discord = null;
@@ -6224,6 +6268,11 @@ var Config = Schema.object({
6224
6268
  autoStartEmail: Schema.boolean().default(true).description("\u542F\u52A8\u65F6\u81EA\u52A8\u7528\u5DF2\u4FDD\u5B58\u7684 Email \u51ED\u636E\u5EFA\u7ACB\u5E38\u9A7B\u8FDE\u63A5\uFF08IMAP \u8F6E\u8BE2\uFF09"),
6225
6269
  groupReply: Schema.boolean().default(true).description("\u662F\u5426\u56DE\u590D\u7FA4\u804A\u6D88\u606F\uFF08false \u65F6\u53EA\u5904\u7406\u5355\u804A\uFF09"),
6226
6270
  welcomeReply: Schema.boolean().default(false).description("\u662F\u5426\u5728\u7528\u6237\u5F53\u5929\u9996\u6B21\u8FDB\u5165\u5355\u804A\u65F6\u81EA\u52A8\u53D1\u9001\u6B22\u8FCE\u8BED\uFF08\u9ED8\u8BA4 false \u4FDD\u6301\u514D\u6253\u6270\uFF0C\u8BBE\u4E3A true \u5F00\u542F\uFF09"),
6271
+ outboundWebhooks: Schema.array(Schema.object({
6272
+ url: Schema.string().required().description("\u76EE\u6807 Webhook \u63A5\u6536 URL"),
6273
+ secret: Schema.string().description("\u53EF\u9009 HMAC-SHA256 \u7B7E\u540D\u5BC6\u94A5\uFF08\u5C06\u5728 X-Gateway-Signature \u5934\u9644\u5E26 hex \u7B7E\u540D\uFF09"),
6274
+ events: Schema.array(Schema.string()).default(["*"]).description('\u8BA2\u9605\u7684\u4E8B\u4EF6\u7C7B\u578B\uFF08\u5982 ["message.in", "message.out", "agent.done"]\uFF0C\u9ED8\u8BA4 ["*"]\uFF09')
6275
+ })).default([]).description("Outbound Webhooks \u5916\u90E8\u4E8B\u4EF6\u8BA2\u9605\u5E7F\u64AD\u5217\u8868"),
6227
6276
  routes: Schema.array(Schema.object({
6228
6277
  id: Schema.string().required().description("\u89C4\u5219 id\uFF08\u552F\u4E00\u6807\u8BC6\uFF09"),
6229
6278
  matchPlatform: Schema.string().description("\u5339\u914D\u5E73\u53F0 id\uFF08telegram/discord/qq/wecom-aibot/email/webhook \u7B49\uFF09\uFF1B\u7F3A\u7701=\u5168\u90E8\u5E73\u53F0"),