dsh-message-gateway 0.1.3 → 0.1.5
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 +93 -36
- 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,10 @@ var QQBridge = class {
|
|
|
519
579
|
accessToken = null;
|
|
520
580
|
/** access_token 过期时间戳(官方 ≤7200s;实测可能更短,提前 60s 刷新)。 */
|
|
521
581
|
tokenExpiresAt = 0;
|
|
522
|
-
/**
|
|
523
|
-
|
|
582
|
+
/** 单聊流式/群聊定稿发送器。 */
|
|
583
|
+
sender;
|
|
584
|
+
/** 连续重连次数(退避用,READY 后重置)。 */
|
|
585
|
+
reconnectAttempts = 0;
|
|
524
586
|
recentMsgIds = /* @__PURE__ */ new Set();
|
|
525
587
|
status = { state: "idle", detail: "", connectedAt: null };
|
|
526
588
|
setStatus(state, detail = "") {
|
|
@@ -621,16 +683,19 @@ var QQBridge = class {
|
|
|
621
683
|
this.clearHeartbeat();
|
|
622
684
|
this.ws = null;
|
|
623
685
|
if (!this.stopped) {
|
|
624
|
-
|
|
686
|
+
this.setStatus("connecting", "reconnect");
|
|
687
|
+
const delay = Math.min(3e3 * 2 ** this.reconnectAttempts, 6e4);
|
|
688
|
+
this.reconnectAttempts += 1;
|
|
689
|
+
console.warn(`[dsh-message-gateway] qq gateway closed, reconnect in ${delay}ms`);
|
|
625
690
|
setTimeout(() => {
|
|
626
691
|
if (this.stopped) return;
|
|
627
|
-
void this.
|
|
628
|
-
if (this.stopped ||
|
|
692
|
+
void this.getAccessToken().then(async (token) => {
|
|
693
|
+
if (this.stopped || token === null) return;
|
|
629
694
|
const u = await this.getGatewayUrl();
|
|
630
695
|
if (this.stopped || u === null) return;
|
|
631
696
|
this.connect(u);
|
|
632
697
|
});
|
|
633
|
-
},
|
|
698
|
+
}, delay);
|
|
634
699
|
}
|
|
635
700
|
};
|
|
636
701
|
}
|
|
@@ -655,6 +720,7 @@ var QQBridge = class {
|
|
|
655
720
|
}
|
|
656
721
|
case 0: {
|
|
657
722
|
if (payload.t === "READY") {
|
|
723
|
+
this.reconnectAttempts = 0;
|
|
658
724
|
this.setStatus("connected", "qq");
|
|
659
725
|
console.log("[dsh-message-gateway] qq READY");
|
|
660
726
|
} else if (payload.t === "C2C_MESSAGE_CREATE") {
|
|
@@ -665,7 +731,8 @@ var QQBridge = class {
|
|
|
665
731
|
break;
|
|
666
732
|
}
|
|
667
733
|
case 9: {
|
|
668
|
-
console.warn("[dsh-message-gateway] qq identify rejected (INVALID_SESSION)
|
|
734
|
+
console.warn("[dsh-message-gateway] qq identify rejected (INVALID_SESSION), refreshing token");
|
|
735
|
+
this.accessToken = null;
|
|
669
736
|
this.ws?.close();
|
|
670
737
|
break;
|
|
671
738
|
}
|
|
@@ -744,10 +811,10 @@ var QQBridge = class {
|
|
|
744
811
|
if (openid === "") return;
|
|
745
812
|
const frame = { openid, msgId, scene };
|
|
746
813
|
const sink = {
|
|
747
|
-
//
|
|
748
|
-
// 只发一条最终消息,避免「正在处理… + 回复」两条。
|
|
814
|
+
// 单聊:流式(ack 开启,「正在处理…」就地增长);群聊:不支持流式,关闭 ack 只发定稿。
|
|
749
815
|
ack: false,
|
|
750
|
-
|
|
816
|
+
// 单聊流式用 replace 全量正文,不能先发独立回执(前缀不一致会 40007);群聊本就不支持流式。
|
|
817
|
+
stream: (f, sid, content, finish) => void this.sender.send(f, sid, content, finish)
|
|
751
818
|
};
|
|
752
819
|
const identity = {
|
|
753
820
|
key: scene === "c2c" ? `qq:c2c:${openid}` : `qq:group:${openid}`,
|
|
@@ -758,21 +825,11 @@ var QQBridge = class {
|
|
|
758
825
|
console.log("[dsh-message-gateway] qq text", { scene, openid, text: text.slice(0, 60) });
|
|
759
826
|
this.callbacks.onText(text, identity);
|
|
760
827
|
}
|
|
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
828
|
/** 停止网关连接。 */
|
|
772
829
|
stop() {
|
|
773
830
|
this.stopped = true;
|
|
774
831
|
this.clearHeartbeat();
|
|
775
|
-
this.
|
|
832
|
+
this.sender.clear();
|
|
776
833
|
this.recentMsgIds.clear();
|
|
777
834
|
this.ws?.close();
|
|
778
835
|
this.ws = null;
|
|
@@ -787,8 +844,10 @@ var QqWebhookBridge = class {
|
|
|
787
844
|
this.callbackToken = callbackToken;
|
|
788
845
|
this.callbacks = callbacks;
|
|
789
846
|
this.key = qqEd25519Key(callbackToken);
|
|
847
|
+
this.sender = new QqMessageSender((method, path, body) => this.rest(method, path, body));
|
|
790
848
|
}
|
|
791
849
|
key;
|
|
850
|
+
sender;
|
|
792
851
|
accessToken = null;
|
|
793
852
|
tokenExpiresAt = 0;
|
|
794
853
|
recentMsgIds = /* @__PURE__ */ new Set();
|
|
@@ -836,10 +895,10 @@ var QqWebhookBridge = class {
|
|
|
836
895
|
if (openid === "") return true;
|
|
837
896
|
const frame = { openid, msgId, scene };
|
|
838
897
|
const sink = {
|
|
898
|
+
// 单聊:流式(ack 开启);群聊:只发定稿。
|
|
839
899
|
ack: false,
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
}
|
|
900
|
+
// 单聊流式用 replace 全量正文,不能先发独立回执(前缀不一致会 40007);群聊本就不支持流式。
|
|
901
|
+
stream: (f, sid, content, finish) => void this.sender.send(f, sid, content, finish)
|
|
843
902
|
};
|
|
844
903
|
const identity = {
|
|
845
904
|
key: scene === "c2c" ? `qq:c2c:${openid}` : `qq:group:${openid}`,
|
|
@@ -878,27 +937,25 @@ var QqWebhookBridge = class {
|
|
|
878
937
|
clearTimeout(timer);
|
|
879
938
|
}
|
|
880
939
|
}
|
|
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`;
|
|
940
|
+
/** 发送器用的 rest:先确保 token,再打 /v2 接口。 */
|
|
941
|
+
async rest(method, path, body) {
|
|
942
|
+
if (!await this.ensureToken()) return null;
|
|
886
943
|
try {
|
|
887
944
|
const response = await fetch(`${BASE}${path}`, {
|
|
888
|
-
method
|
|
945
|
+
method,
|
|
889
946
|
headers: { authorization: `QQBot ${this.accessToken ?? ""}`, "content-type": "application/json" },
|
|
890
|
-
body:
|
|
947
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
891
948
|
signal: AbortSignal.timeout(15e3)
|
|
892
949
|
});
|
|
893
950
|
if (!response.ok) {
|
|
894
951
|
const err = (await response.text()).slice(0, 120);
|
|
895
|
-
console.warn("[dsh-message-gateway] qq webhook
|
|
896
|
-
return
|
|
952
|
+
console.warn("[dsh-message-gateway] qq webhook rest", method, path, response.status, err);
|
|
953
|
+
return null;
|
|
897
954
|
}
|
|
898
|
-
return
|
|
955
|
+
return await response.json();
|
|
899
956
|
} catch (error) {
|
|
900
|
-
console.warn("[dsh-message-gateway] qq webhook
|
|
901
|
-
return
|
|
957
|
+
console.warn("[dsh-message-gateway] qq webhook rest failed", method, path, String(error));
|
|
958
|
+
return null;
|
|
902
959
|
}
|
|
903
960
|
}
|
|
904
961
|
};
|