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