dsh-message-gateway 0.1.3 → 0.1.4
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 +81 -31
- package/lib/index.js.map +2 -2
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -506,11 +506,71 @@ function qqEd25519Key(secret) {
|
|
|
506
506
|
const der = Buffer.concat([Buffer.from([48, body.length]), body]);
|
|
507
507
|
return createPrivateKey({ key: der, format: "der", type: "pkcs8" });
|
|
508
508
|
}
|
|
509
|
+
var QqMessageSender = class {
|
|
510
|
+
constructor(rest) {
|
|
511
|
+
this.rest = rest;
|
|
512
|
+
}
|
|
513
|
+
c2cStreams = /* @__PURE__ */ new Map();
|
|
514
|
+
sent = /* @__PURE__ */ new Set();
|
|
515
|
+
/** 发送一帧;finish=false 为中间帧(单聊流式推进,群聊忽略),finish=true 定稿。 */
|
|
516
|
+
async send(frame, streamId, content, finish) {
|
|
517
|
+
if (frame.scene === "c2c") {
|
|
518
|
+
await this.streamC2c(frame, streamId, content, finish);
|
|
519
|
+
} else if (finish) {
|
|
520
|
+
await this.sendGroupOnce(frame, streamId, content);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
async streamC2c(frame, streamId, content, finish) {
|
|
524
|
+
if (this.sent.has(streamId)) return;
|
|
525
|
+
const state = this.c2cStreams.get(streamId) ?? { index: 0, streamMsgId: "" };
|
|
526
|
+
const text = content.trim() === "" ? "\u2026" : content.slice(0, 2e3);
|
|
527
|
+
const body = {
|
|
528
|
+
input_mode: "replace",
|
|
529
|
+
input_state: finish ? 10 : 1,
|
|
530
|
+
index: state.index,
|
|
531
|
+
content_type: "markdown",
|
|
532
|
+
content_raw: text,
|
|
533
|
+
msg_id: frame.msgId,
|
|
534
|
+
msg_seq: 1
|
|
535
|
+
};
|
|
536
|
+
if (state.streamMsgId !== "") body.stream_msg_id = state.streamMsgId;
|
|
537
|
+
const result = await this.rest(
|
|
538
|
+
"POST",
|
|
539
|
+
`/v2/users/${encodeURIComponent(frame.openid)}/stream_messages`,
|
|
540
|
+
body
|
|
541
|
+
);
|
|
542
|
+
if (result?.id !== void 0) {
|
|
543
|
+
state.streamMsgId = result.id;
|
|
544
|
+
state.index += 1;
|
|
545
|
+
this.c2cStreams.set(streamId, state);
|
|
546
|
+
if (finish) this.sent.add(streamId);
|
|
547
|
+
} else {
|
|
548
|
+
this.c2cStreams.delete(streamId);
|
|
549
|
+
if (finish) this.sent.add(streamId);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
async sendGroupOnce(frame, streamId, content) {
|
|
553
|
+
if (this.sent.has(streamId)) return;
|
|
554
|
+
this.sent.add(streamId);
|
|
555
|
+
const text = content.trim() === "" ? "\uFF08\u7A7A\u56DE\u590D\uFF09" : content.slice(0, 2e3);
|
|
556
|
+
const result = await this.rest(
|
|
557
|
+
"POST",
|
|
558
|
+
`/v2/groups/${encodeURIComponent(frame.openid)}/messages`,
|
|
559
|
+
{ content: text, msg_type: 0, msg_id: frame.msgId }
|
|
560
|
+
);
|
|
561
|
+
if (result === null) this.sent.delete(streamId);
|
|
562
|
+
}
|
|
563
|
+
clear() {
|
|
564
|
+
this.c2cStreams.clear();
|
|
565
|
+
this.sent.clear();
|
|
566
|
+
}
|
|
567
|
+
};
|
|
509
568
|
var QQBridge = class {
|
|
510
569
|
constructor(appId, secret, callbacks) {
|
|
511
570
|
this.appId = appId;
|
|
512
571
|
this.secret = secret;
|
|
513
572
|
this.callbacks = callbacks;
|
|
573
|
+
this.sender = new QqMessageSender((method, path, body) => this.rest(method, path, body));
|
|
514
574
|
}
|
|
515
575
|
ws = null;
|
|
516
576
|
stopped = false;
|
|
@@ -519,8 +579,8 @@ var QQBridge = class {
|
|
|
519
579
|
accessToken = null;
|
|
520
580
|
/** access_token 过期时间戳(官方 ≤7200s;实测可能更短,提前 60s 刷新)。 */
|
|
521
581
|
tokenExpiresAt = 0;
|
|
522
|
-
/**
|
|
523
|
-
|
|
582
|
+
/** 单聊流式/群聊定稿发送器。 */
|
|
583
|
+
sender;
|
|
524
584
|
recentMsgIds = /* @__PURE__ */ new Set();
|
|
525
585
|
status = { state: "idle", detail: "", connectedAt: null };
|
|
526
586
|
setStatus(state, detail = "") {
|
|
@@ -744,10 +804,10 @@ var QQBridge = class {
|
|
|
744
804
|
if (openid === "") return;
|
|
745
805
|
const frame = { openid, msgId, scene };
|
|
746
806
|
const sink = {
|
|
747
|
-
//
|
|
748
|
-
// 只发一条最终消息,避免「正在处理… + 回复」两条。
|
|
807
|
+
// 单聊:流式(ack 开启,「正在处理…」就地增长);群聊:不支持流式,关闭 ack 只发定稿。
|
|
749
808
|
ack: false,
|
|
750
|
-
|
|
809
|
+
// 单聊流式用 replace 全量正文,不能先发独立回执(前缀不一致会 40007);群聊本就不支持流式。
|
|
810
|
+
stream: (f, sid, content, finish) => void this.sender.send(f, sid, content, finish)
|
|
751
811
|
};
|
|
752
812
|
const identity = {
|
|
753
813
|
key: scene === "c2c" ? `qq:c2c:${openid}` : `qq:group:${openid}`,
|
|
@@ -758,21 +818,11 @@ var QQBridge = class {
|
|
|
758
818
|
console.log("[dsh-message-gateway] qq text", { scene, openid, text: text.slice(0, 60) });
|
|
759
819
|
this.callbacks.onText(text, identity);
|
|
760
820
|
}
|
|
761
|
-
/** 回复:finish 时一次发出(带 msg_id 被动回复;幂等防重发)。 */
|
|
762
|
-
async streamReply(frame, streamId, content, finish) {
|
|
763
|
-
if (!finish) return;
|
|
764
|
-
if (this.sent.has(streamId)) return;
|
|
765
|
-
this.sent.add(streamId);
|
|
766
|
-
const text = content.trim() === "" ? "\uFF08\u7A7A\u56DE\u590D\uFF09" : content.slice(0, 2e3);
|
|
767
|
-
const path = frame.scene === "c2c" ? `/v2/users/${encodeURIComponent(frame.openid)}/messages` : `/v2/groups/${encodeURIComponent(frame.openid)}/messages`;
|
|
768
|
-
const result = await this.rest("POST", path, { content: text, msg_type: 0, msg_id: frame.msgId });
|
|
769
|
-
if (result === null) this.sent.delete(streamId);
|
|
770
|
-
}
|
|
771
821
|
/** 停止网关连接。 */
|
|
772
822
|
stop() {
|
|
773
823
|
this.stopped = true;
|
|
774
824
|
this.clearHeartbeat();
|
|
775
|
-
this.
|
|
825
|
+
this.sender.clear();
|
|
776
826
|
this.recentMsgIds.clear();
|
|
777
827
|
this.ws?.close();
|
|
778
828
|
this.ws = null;
|
|
@@ -787,8 +837,10 @@ var QqWebhookBridge = class {
|
|
|
787
837
|
this.callbackToken = callbackToken;
|
|
788
838
|
this.callbacks = callbacks;
|
|
789
839
|
this.key = qqEd25519Key(callbackToken);
|
|
840
|
+
this.sender = new QqMessageSender((method, path, body) => this.rest(method, path, body));
|
|
790
841
|
}
|
|
791
842
|
key;
|
|
843
|
+
sender;
|
|
792
844
|
accessToken = null;
|
|
793
845
|
tokenExpiresAt = 0;
|
|
794
846
|
recentMsgIds = /* @__PURE__ */ new Set();
|
|
@@ -836,10 +888,10 @@ var QqWebhookBridge = class {
|
|
|
836
888
|
if (openid === "") return true;
|
|
837
889
|
const frame = { openid, msgId, scene };
|
|
838
890
|
const sink = {
|
|
891
|
+
// 单聊:流式(ack 开启);群聊:只发定稿。
|
|
839
892
|
ack: false,
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
}
|
|
893
|
+
// 单聊流式用 replace 全量正文,不能先发独立回执(前缀不一致会 40007);群聊本就不支持流式。
|
|
894
|
+
stream: (f, sid, content, finish) => void this.sender.send(f, sid, content, finish)
|
|
843
895
|
};
|
|
844
896
|
const identity = {
|
|
845
897
|
key: scene === "c2c" ? `qq:c2c:${openid}` : `qq:group:${openid}`,
|
|
@@ -878,27 +930,25 @@ var QqWebhookBridge = class {
|
|
|
878
930
|
clearTimeout(timer);
|
|
879
931
|
}
|
|
880
932
|
}
|
|
881
|
-
/**
|
|
882
|
-
async
|
|
883
|
-
if (!await this.ensureToken()) return
|
|
884
|
-
const text = content.trim() === "" ? "\uFF08\u7A7A\u56DE\u590D\uFF09" : content.slice(0, 2e3);
|
|
885
|
-
const path = frame.scene === "c2c" ? `/v2/users/${encodeURIComponent(frame.openid)}/messages` : `/v2/groups/${encodeURIComponent(frame.openid)}/messages`;
|
|
933
|
+
/** 发送器用的 rest:先确保 token,再打 /v2 接口。 */
|
|
934
|
+
async rest(method, path, body) {
|
|
935
|
+
if (!await this.ensureToken()) return null;
|
|
886
936
|
try {
|
|
887
937
|
const response = await fetch(`${BASE}${path}`, {
|
|
888
|
-
method
|
|
938
|
+
method,
|
|
889
939
|
headers: { authorization: `QQBot ${this.accessToken ?? ""}`, "content-type": "application/json" },
|
|
890
|
-
body:
|
|
940
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
891
941
|
signal: AbortSignal.timeout(15e3)
|
|
892
942
|
});
|
|
893
943
|
if (!response.ok) {
|
|
894
944
|
const err = (await response.text()).slice(0, 120);
|
|
895
|
-
console.warn("[dsh-message-gateway] qq webhook
|
|
896
|
-
return
|
|
945
|
+
console.warn("[dsh-message-gateway] qq webhook rest", method, path, response.status, err);
|
|
946
|
+
return null;
|
|
897
947
|
}
|
|
898
|
-
return
|
|
948
|
+
return await response.json();
|
|
899
949
|
} catch (error) {
|
|
900
|
-
console.warn("[dsh-message-gateway] qq webhook
|
|
901
|
-
return
|
|
950
|
+
console.warn("[dsh-message-gateway] qq webhook rest failed", method, path, String(error));
|
|
951
|
+
return null;
|
|
902
952
|
}
|
|
903
953
|
}
|
|
904
954
|
};
|