dsh-message-gateway 0.1.35 → 0.1.37
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/client.js +6 -0
- package/lib/client.js.map +2 -2
- package/lib/index.js +153 -16
- package/lib/index.js.map +2 -2
- package/package.json +10 -9
package/lib/index.js
CHANGED
|
@@ -474,6 +474,7 @@ var init_gateway_store = __esm({
|
|
|
474
474
|
});
|
|
475
475
|
|
|
476
476
|
// src/index.ts
|
|
477
|
+
import { Service } from "@deepseek-ai/cordis";
|
|
477
478
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
478
479
|
|
|
479
480
|
// src/host/bridge-manager.ts
|
|
@@ -950,6 +951,10 @@ import { ProxyAgent, fetch as undiciFetch } from "undici";
|
|
|
950
951
|
var cachedProxyUrl = void 0;
|
|
951
952
|
async function getProxyUrl() {
|
|
952
953
|
if (cachedProxyUrl !== void 0) return cachedProxyUrl;
|
|
954
|
+
if (process.env.DSH_GATEWAY_DISABLE_PROXY === "1") {
|
|
955
|
+
cachedProxyUrl = null;
|
|
956
|
+
return null;
|
|
957
|
+
}
|
|
953
958
|
const envProxy = process.env.https_proxy || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.HTTP_PROXY || process.env.all_proxy || process.env.ALL_PROXY;
|
|
954
959
|
if (envProxy) {
|
|
955
960
|
cachedProxyUrl = envProxy;
|
|
@@ -2345,6 +2350,11 @@ function decodeBody(raw, encoding) {
|
|
|
2345
2350
|
}
|
|
2346
2351
|
|
|
2347
2352
|
// src/host/email-bridge.ts
|
|
2353
|
+
function isSenderAllowed(from, allowed) {
|
|
2354
|
+
const list = (allowed ?? "").split(",").map((s) => s.trim().toLowerCase()).filter((s) => s !== "");
|
|
2355
|
+
if (list.length === 0) return true;
|
|
2356
|
+
return list.includes((from ?? "").trim().toLowerCase());
|
|
2357
|
+
}
|
|
2348
2358
|
var EMAIL_ATTACH_MAX_BYTES = 25 * 1024 * 1024;
|
|
2349
2359
|
var POLL_MS = 3e4;
|
|
2350
2360
|
var RETRY_MS = 5e3;
|
|
@@ -2896,6 +2906,14 @@ var EmailBridge = class {
|
|
|
2896
2906
|
if (notes.length > 0) text = text === "" ? notes.join("\n") : `${text}
|
|
2897
2907
|
${notes.join("\n")}`;
|
|
2898
2908
|
if (from === "" || text.trim() === "" && attachments.length === 0) return;
|
|
2909
|
+
if (!isSenderAllowed(from, this.cred.allowedSenders)) {
|
|
2910
|
+
console.warn("[dsh-message-gateway] email dropped by sender allowlist", {
|
|
2911
|
+
from,
|
|
2912
|
+
subject: subject.slice(0, 40),
|
|
2913
|
+
hint: "\u82E5\u975E\u9884\u671F\uFF0C\u8BF7\u68C0\u67E5\u8BE5\u90AE\u7BB1\u51ED\u636E\u7684 allowedSenders \u914D\u7F6E"
|
|
2914
|
+
});
|
|
2915
|
+
return;
|
|
2916
|
+
}
|
|
2899
2917
|
const frame = { from, subject, threadKey };
|
|
2900
2918
|
const sink = {
|
|
2901
2919
|
ack: false,
|
|
@@ -3368,16 +3386,62 @@ var BridgeManager = class {
|
|
|
3368
3386
|
constructor(ctx, config) {
|
|
3369
3387
|
this.ctx = ctx;
|
|
3370
3388
|
this.config = config;
|
|
3389
|
+
if (typeof this.ctx.on === "function") {
|
|
3390
|
+
try {
|
|
3391
|
+
this.disposeStreamSub = this.ctx.on(
|
|
3392
|
+
"agent/assistant-stream",
|
|
3393
|
+
(payload) => this.handleAssistantStream(payload),
|
|
3394
|
+
{ global: true }
|
|
3395
|
+
);
|
|
3396
|
+
} catch (error) {
|
|
3397
|
+
console.warn("[dsh-message-gateway] assistant stream subscribe skipped", error);
|
|
3398
|
+
}
|
|
3399
|
+
}
|
|
3371
3400
|
}
|
|
3372
3401
|
wecom = null;
|
|
3373
3402
|
onStatusCallback = null;
|
|
3374
3403
|
/** 各聊天在途回复(key = 平台:chatKey)。 */
|
|
3375
3404
|
pendingMap = /* @__PURE__ */ new Map();
|
|
3405
|
+
/** 会话 id → 在途回复(0.2 流式帧按会话路由到对应平台聊天)。 */
|
|
3406
|
+
pendingBySession = /* @__PURE__ */ new Map();
|
|
3407
|
+
/** 全局流式帧订阅的 disposer。 */
|
|
3408
|
+
disposeStreamSub = void 0;
|
|
3376
3409
|
/** Webhook 在途请求(同步等待完整回复;全局单槽位)。 */
|
|
3377
3410
|
awaiting = null;
|
|
3378
3411
|
/** 各聊天的独立 agent 会话(key = 平台:chatKey;webhook 固定 'webhook')。 */
|
|
3379
3412
|
agents = /* @__PURE__ */ new Map();
|
|
3380
3413
|
disposed = false;
|
|
3414
|
+
/** 0.2 流式帧:按会话 id 找到在途平台回复,把文本增量接到现有节流推送管线。 */
|
|
3415
|
+
handleAssistantStream(payload) {
|
|
3416
|
+
const p = this.pendingBySession.get(payload.agent.session.id);
|
|
3417
|
+
if (p === void 0) return;
|
|
3418
|
+
const frame = payload.frame;
|
|
3419
|
+
if (frame.type === "start") {
|
|
3420
|
+
p.currentStepBuffer = "";
|
|
3421
|
+
return;
|
|
3422
|
+
}
|
|
3423
|
+
if (frame.type === "chunk") {
|
|
3424
|
+
if (frame.chunk.type !== "text-delta" || frame.chunk.text === "") return;
|
|
3425
|
+
p.currentStepBuffer += frame.chunk.text;
|
|
3426
|
+
p.buffer = [...p.stepMessages, p.currentStepBuffer].filter(Boolean).join("\n\n");
|
|
3427
|
+
this.keepAlive(p.key, p);
|
|
3428
|
+
this.scheduleStream(p);
|
|
3429
|
+
return;
|
|
3430
|
+
}
|
|
3431
|
+
this.keepAlive(p.key, p);
|
|
3432
|
+
}
|
|
3433
|
+
/** 任意新事件或流式 chunk 都滑动续期兜底超时。 */
|
|
3434
|
+
keepAlive(key, p) {
|
|
3435
|
+
if (p.fallback === null) return;
|
|
3436
|
+
clearTimeout(p.fallback);
|
|
3437
|
+
p.fallback = setTimeout(() => {
|
|
3438
|
+
if (this.pendingMap.get(key) === p) {
|
|
3439
|
+
console.warn("[dsh-message-gateway] idle timeout (no events for 90s)", { key });
|
|
3440
|
+
this.finishPending(key);
|
|
3441
|
+
}
|
|
3442
|
+
}, 90 * 1e3);
|
|
3443
|
+
p.fallback.unref?.();
|
|
3444
|
+
}
|
|
3381
3445
|
/** 当前语言的文案。 */
|
|
3382
3446
|
t(key) {
|
|
3383
3447
|
return botText(this.config.botLocale, key);
|
|
@@ -3392,16 +3456,7 @@ var BridgeManager = class {
|
|
|
3392
3456
|
p.cursor = i + 1;
|
|
3393
3457
|
const event = events[i];
|
|
3394
3458
|
if (!event) continue;
|
|
3395
|
-
|
|
3396
|
-
clearTimeout(p.fallback);
|
|
3397
|
-
p.fallback = setTimeout(() => {
|
|
3398
|
-
if (this.pendingMap.get(key) === p) {
|
|
3399
|
-
console.warn("[dsh-message-gateway] idle timeout (no events for 90s)", { key });
|
|
3400
|
-
this.finishPending(key);
|
|
3401
|
-
}
|
|
3402
|
-
}, 90 * 1e3);
|
|
3403
|
-
p.fallback.unref?.();
|
|
3404
|
-
}
|
|
3459
|
+
this.keepAlive(key, p);
|
|
3405
3460
|
if (event.type === "assistant/chunk") {
|
|
3406
3461
|
const chunk = event.data?.chunk;
|
|
3407
3462
|
if (chunk && chunk.type === "text-delta" && typeof chunk.text === "string") {
|
|
@@ -3489,6 +3544,8 @@ var BridgeManager = class {
|
|
|
3489
3544
|
p.heartbeat = null;
|
|
3490
3545
|
}
|
|
3491
3546
|
this.pendingMap.delete(key);
|
|
3547
|
+
const pending = this.pendingBySession.get(p.sessionId);
|
|
3548
|
+
if (pending === p) this.pendingBySession.delete(p.sessionId);
|
|
3492
3549
|
if (p.ackSent && !p.pushed) {
|
|
3493
3550
|
void p.sink.stream(p.frame, p.streamId, this.t("timeout"), true);
|
|
3494
3551
|
}
|
|
@@ -3513,7 +3570,8 @@ var BridgeManager = class {
|
|
|
3513
3570
|
let provider = selection?.provider ?? "";
|
|
3514
3571
|
let model = selection?.model ?? "";
|
|
3515
3572
|
if (provider === "" || model === "") {
|
|
3516
|
-
const
|
|
3573
|
+
const roots = this.ctx.agents.roots?.() ?? [];
|
|
3574
|
+
const root = roots[0] ?? this.ctx.agents.list()[0];
|
|
3517
3575
|
provider = root?.options.provider ?? "";
|
|
3518
3576
|
model = root?.options.model ?? "";
|
|
3519
3577
|
}
|
|
@@ -3562,8 +3620,11 @@ var BridgeManager = class {
|
|
|
3562
3620
|
}
|
|
3563
3621
|
}
|
|
3564
3622
|
}
|
|
3565
|
-
|
|
3566
|
-
|
|
3623
|
+
const installModel = installModelSelection;
|
|
3624
|
+
if (installModelSelection.length >= 2) {
|
|
3625
|
+
installModel(agentCtx, { current: selection, assembled: void 0 });
|
|
3626
|
+
} else {
|
|
3627
|
+
installModel(agentCtx);
|
|
3567
3628
|
}
|
|
3568
3629
|
}
|
|
3569
3630
|
});
|
|
@@ -3598,6 +3659,9 @@ var BridgeManager = class {
|
|
|
3598
3659
|
async dispose() {
|
|
3599
3660
|
if (this.disposed) return;
|
|
3600
3661
|
this.disposed = true;
|
|
3662
|
+
this.disposeStreamSub?.();
|
|
3663
|
+
this.disposeStreamSub = void 0;
|
|
3664
|
+
this.pendingBySession.clear();
|
|
3601
3665
|
this.wecom?.stop();
|
|
3602
3666
|
this.wecom = null;
|
|
3603
3667
|
this.telegram?.stop();
|
|
@@ -3721,6 +3785,8 @@ var BridgeManager = class {
|
|
|
3721
3785
|
};
|
|
3722
3786
|
const session = agent.session;
|
|
3723
3787
|
const p = {
|
|
3788
|
+
key: id.key,
|
|
3789
|
+
sessionId: session.id,
|
|
3724
3790
|
frame: id.frame,
|
|
3725
3791
|
sink: id.sink,
|
|
3726
3792
|
streamId: `gw-${Date.now().toString(36)}`,
|
|
@@ -3738,6 +3804,7 @@ var BridgeManager = class {
|
|
|
3738
3804
|
heartbeat: null
|
|
3739
3805
|
};
|
|
3740
3806
|
this.pendingMap.set(id.key, p);
|
|
3807
|
+
this.pendingBySession.set(session.id, p);
|
|
3741
3808
|
if (id.sink.ack !== false) {
|
|
3742
3809
|
p.ackSent = true;
|
|
3743
3810
|
id.sink.stream(id.frame, p.streamId, this.t("ack"), false);
|
|
@@ -4281,15 +4348,66 @@ ${secret}`;
|
|
|
4281
4348
|
smtpHost: cred.smtpHost ?? void 0,
|
|
4282
4349
|
smtpPort: cred.smtpPort ?? void 0,
|
|
4283
4350
|
smtpUser: cred.smtpUser ?? void 0,
|
|
4284
|
-
smtpPass: cred.smtpPass ?? void 0
|
|
4351
|
+
smtpPass: cred.smtpPass ?? void 0,
|
|
4352
|
+
// 安全:发件人白名单(空 = 不限制,保持向后兼容)。
|
|
4353
|
+
allowedSenders: cred.allowedSenders ?? void 0
|
|
4285
4354
|
};
|
|
4286
4355
|
const bridge = new EmailBridge(emailCred, {
|
|
4287
4356
|
onStatus: (status) => this.onStatusCallback?.(status),
|
|
4288
|
-
onText: (text, identity, attachments) => void this.
|
|
4357
|
+
onText: (text, identity, attachments) => void this.handleInboundEmail(identity, text, attachments)
|
|
4289
4358
|
});
|
|
4290
4359
|
this.email = bridge;
|
|
4291
4360
|
bridge.start();
|
|
4292
4361
|
}
|
|
4362
|
+
/**
|
|
4363
|
+
* 邮件入站分流:先问审批闸门要不要这条消息,再决定是否交给 agent。
|
|
4364
|
+
*
|
|
4365
|
+
* 为什么需要这一层:审批回信("approve")**不能**被当成一句闲聊喂给模型。
|
|
4366
|
+
* 若直接把回信交给 agent,模型会看到"approve"并自由发挥,而真正的审批
|
|
4367
|
+
* 状态却仍挂在原地 —— 用户以为批准了,实际什么也没发生。
|
|
4368
|
+
*
|
|
4369
|
+
* 因此顺序是:审批闸门先认领 → 认领了就地回执并**终止**;不认领才进 agent。
|
|
4370
|
+
*
|
|
4371
|
+
* 审批闸门未安装或未配置时,这个调用会快速失败(连接被拒/404),
|
|
4372
|
+
* 邮件照常进入 agent —— 即"没装审批闸门"的行为与从前完全一致。
|
|
4373
|
+
*/
|
|
4374
|
+
async handleInboundEmail(identity, text, attachments) {
|
|
4375
|
+
const claimed = await this.tryApprovalGate(identity, text);
|
|
4376
|
+
if (claimed) return;
|
|
4377
|
+
await this.handleExternalMessage(identity, text, attachments);
|
|
4378
|
+
}
|
|
4379
|
+
/**
|
|
4380
|
+
* 尝试把一条入站消息交给 dsh-approval-gate 处理。
|
|
4381
|
+
*
|
|
4382
|
+
* @param identity - 入站身份(含 frame.from 作为发件人)。
|
|
4383
|
+
* @param text - 消息正文。
|
|
4384
|
+
* @returns 是否被审批闸门认领(true = 已处理,不应再进 agent)。
|
|
4385
|
+
*/
|
|
4386
|
+
async tryApprovalGate(identity, text) {
|
|
4387
|
+
const frame = identity.frame;
|
|
4388
|
+
const sender = frame?.from ?? "";
|
|
4389
|
+
if (sender === "") return false;
|
|
4390
|
+
const port = process.env.DSH_WEB_PORT ?? process.env.PORT ?? "3080";
|
|
4391
|
+
const url = `http://127.0.0.1:${port}/approval/reply`;
|
|
4392
|
+
try {
|
|
4393
|
+
const res = await fetch(url, {
|
|
4394
|
+
method: "POST",
|
|
4395
|
+
headers: { "Content-Type": "application/json" },
|
|
4396
|
+
body: JSON.stringify({ sender, text }),
|
|
4397
|
+
signal: AbortSignal.timeout(5e3)
|
|
4398
|
+
});
|
|
4399
|
+
if (!res.ok) return false;
|
|
4400
|
+
const body = await res.json();
|
|
4401
|
+
if (body.handled !== true) return false;
|
|
4402
|
+
if (typeof body.reply === "string" && body.reply !== "") {
|
|
4403
|
+
identity.sink.stream(identity.frame, `approval-${Date.now().toString(36)}`, body.reply, true);
|
|
4404
|
+
}
|
|
4405
|
+
console.log("[dsh-message-gateway] inbound message claimed by approval gate", { sender, len: text.length });
|
|
4406
|
+
return true;
|
|
4407
|
+
} catch {
|
|
4408
|
+
return false;
|
|
4409
|
+
}
|
|
4410
|
+
}
|
|
4293
4411
|
/** 停止 Email 桥。 */
|
|
4294
4412
|
stopEmail() {
|
|
4295
4413
|
this.email?.stop();
|
|
@@ -4525,7 +4643,9 @@ var PLATFORMS = [
|
|
|
4525
4643
|
{ key: "smtpHost", labelKey: "field.smtpHost", kind: "text" },
|
|
4526
4644
|
{ key: "smtpPort", labelKey: "field.smtpPort", kind: "number" },
|
|
4527
4645
|
{ key: "smtpUser", labelKey: "field.smtpUser", kind: "text" },
|
|
4528
|
-
{ key: "smtpPass", labelKey: "field.smtpPass", placeholderKey: "field.secret.ph", kind: "secret" }
|
|
4646
|
+
{ key: "smtpPass", labelKey: "field.smtpPass", placeholderKey: "field.secret.ph", kind: "secret" },
|
|
4647
|
+
// 安全:发件人白名单。留空 = 不限制(旧行为);填写后只有名单内地址能进入 agent。
|
|
4648
|
+
{ key: "allowedSenders", labelKey: "field.allowedSenders", placeholderKey: "field.allowedSenders.ph", kind: "text" }
|
|
4529
4649
|
]
|
|
4530
4650
|
},
|
|
4531
4651
|
{
|
|
@@ -5626,8 +5746,23 @@ var Config = Schema.object({
|
|
|
5626
5746
|
|
|
5627
5747
|
// src/index.ts
|
|
5628
5748
|
var inject = ["webServer", "sessions", "agents", "agentDefaultModel", "tools"];
|
|
5749
|
+
var MessageGatewayService = class extends Service {
|
|
5750
|
+
constructor(ctx, manager) {
|
|
5751
|
+
super(ctx, "messageGateway");
|
|
5752
|
+
this.manager = manager;
|
|
5753
|
+
}
|
|
5754
|
+
/** 向已配置的平台推送文本(平台 id / 目标与 send_chat_message 一致)。 */
|
|
5755
|
+
push(platform, target, message, opts = {}) {
|
|
5756
|
+
return this.manager.pushMessage(platform, target, message, opts);
|
|
5757
|
+
}
|
|
5758
|
+
/** 把一次提示交给网关的专用 agent,等待完整回复。 */
|
|
5759
|
+
ask(prompt, timeoutMs) {
|
|
5760
|
+
return this.manager.sendAndWait(prompt, timeoutMs);
|
|
5761
|
+
}
|
|
5762
|
+
};
|
|
5629
5763
|
function apply(ctx, config = Config({})) {
|
|
5630
5764
|
const manager = new BridgeManager(ctx, config);
|
|
5765
|
+
new MessageGatewayService(ctx, manager);
|
|
5631
5766
|
ctx.effect(() => {
|
|
5632
5767
|
void loadStore().then((store) => {
|
|
5633
5768
|
const wecom = store.platforms["wecom-aibot"];
|
|
@@ -5841,6 +5976,7 @@ export {
|
|
|
5841
5976
|
DiscordBridge,
|
|
5842
5977
|
EmailBridge,
|
|
5843
5978
|
ImapClient,
|
|
5979
|
+
MessageGatewayService,
|
|
5844
5980
|
PLATFORMS,
|
|
5845
5981
|
QQBridge,
|
|
5846
5982
|
QqWebhookBridge,
|
|
@@ -5856,6 +5992,7 @@ export {
|
|
|
5856
5992
|
index_default as default,
|
|
5857
5993
|
headerField,
|
|
5858
5994
|
inject,
|
|
5995
|
+
isSenderAllowed,
|
|
5859
5996
|
parseAddress,
|
|
5860
5997
|
platformDef,
|
|
5861
5998
|
sha1Sorted,
|