@chen_hu_hu/pi-qq 1.6.2 → 1.7.1
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/CHANGELOG.md +25 -1
- package/doc/archive/delivery.1.7.0.md +96 -0
- package/doc/archive/delivery.1.7.1.md +215 -0
- package/doc/todo.md +31 -119
- package/index.ts +17 -15
- package/package.json +1 -1
- package/src/infra/file-logger.ts +73 -29
- package/src/qq/inbound-command.ts +15 -5
- package/src/qq/media-tool.ts +9 -3
- package/src/qq/msg-seq-send.ts +11 -0
- package/src/qq/qq-bot.ts +3 -9
- package/src/qq/qq-diagnostics.ts +2 -1
- package/src/qq/qq-message-handler.ts +3 -8
- package/src/qq/qr-login.ts +1 -1
- package/src/qq/reply-sender.ts +6 -3
- package/src/relay/bubble-writer.ts +41 -10
- package/src/relay/msg-seq.ts +15 -0
- package/src/relay/run-stream-relay.ts +42 -1
- package/src/relay/stream-session.ts +74 -16
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StreamContentType, StreamInputState } from "@tencent-connect/qqbot-nodejs/protocol";
|
|
2
|
+
import { allocateMsgSeq } from "./msg-seq";
|
|
2
3
|
import type {
|
|
3
4
|
Credentials,
|
|
4
5
|
MessageApi,
|
|
@@ -9,6 +10,10 @@ import type {
|
|
|
9
10
|
const MIN_INTERVAL_MS = 300;
|
|
10
11
|
const RATE_LIMIT_BASE_MS = 1000;
|
|
11
12
|
const MAX_RETRIES = 3;
|
|
13
|
+
// QQ rejects oversized markdown shards with 40034017. The platform appears to
|
|
14
|
+
// split frames larger than roughly 4k and then requires every shard to end at
|
|
15
|
+
// a newline; 3500 bytes leaves room for that limit and for multi-byte text.
|
|
16
|
+
export const MAX_FRAME_BYTES = 3500;
|
|
12
17
|
|
|
13
18
|
export interface AppendStreamCheckpoint {
|
|
14
19
|
msgSeq?: number;
|
|
@@ -42,6 +47,7 @@ export class AppendStream {
|
|
|
42
47
|
private buffer = "";
|
|
43
48
|
private flushTimer: ReturnType<typeof setTimeout> | null = null;
|
|
44
49
|
private flushing = false;
|
|
50
|
+
private flushingAppends = 0;
|
|
45
51
|
private lastFlushAt = 0;
|
|
46
52
|
|
|
47
53
|
constructor(
|
|
@@ -72,15 +78,16 @@ export class AppendStream {
|
|
|
72
78
|
|
|
73
79
|
async flush(): Promise<void> {
|
|
74
80
|
if (this.doneSent) return;
|
|
75
|
-
|
|
76
|
-
clearTimeout(this.flushTimer);
|
|
77
|
-
this.flushTimer = null;
|
|
78
|
-
}
|
|
81
|
+
this.clearFlushTimer();
|
|
79
82
|
while (this.flushing) {
|
|
80
83
|
await new Promise((r) => setTimeout(r, 20));
|
|
81
84
|
}
|
|
82
85
|
if (this.doneSent) return;
|
|
83
|
-
|
|
86
|
+
while (this.buffer) {
|
|
87
|
+
await this.doFlush("generating");
|
|
88
|
+
this.clearFlushTimer();
|
|
89
|
+
if (this.buffer) await this.waitForThrottle();
|
|
90
|
+
}
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
private throttleMs(): number {
|
|
@@ -93,7 +100,10 @@ export class AppendStream {
|
|
|
93
100
|
const elapsed = Date.now() - this.lastFlushAt;
|
|
94
101
|
const throttle = this.throttleMs();
|
|
95
102
|
if (this.flushing) {
|
|
96
|
-
this.
|
|
103
|
+
this.flushingAppends += 1;
|
|
104
|
+
if (this.flushingAppends === 1 || this.flushingAppends % 50 === 0) {
|
|
105
|
+
this.opts.onDebug?.(`append: while flushing, count=${this.flushingAppends}, buffer=${this.buffer.length}`);
|
|
106
|
+
}
|
|
97
107
|
return;
|
|
98
108
|
}
|
|
99
109
|
if (elapsed >= throttle) {
|
|
@@ -114,38 +124,86 @@ export class AppendStream {
|
|
|
114
124
|
|
|
115
125
|
async done(): Promise<void> {
|
|
116
126
|
if (this.doneSent) return;
|
|
117
|
-
|
|
118
|
-
clearTimeout(this.flushTimer);
|
|
119
|
-
this.flushTimer = null;
|
|
120
|
-
}
|
|
127
|
+
this.clearFlushTimer();
|
|
121
128
|
while (this.flushing) {
|
|
122
129
|
await new Promise((r) => setTimeout(r, 20));
|
|
123
130
|
}
|
|
124
131
|
if (this.doneSent) return;
|
|
125
|
-
|
|
132
|
+
while (this.buffer) {
|
|
133
|
+
await this.doFlush("generating");
|
|
134
|
+
this.clearFlushTimer();
|
|
135
|
+
if (this.buffer) await this.waitForThrottle();
|
|
136
|
+
}
|
|
126
137
|
await this.doFlush("done");
|
|
127
138
|
this.doneSent = true;
|
|
128
139
|
}
|
|
129
140
|
|
|
130
141
|
dispose(): void {
|
|
142
|
+
this.clearFlushTimer();
|
|
143
|
+
this.doneSent = true;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private clearFlushTimer(): void {
|
|
131
147
|
if (this.flushTimer) {
|
|
132
148
|
clearTimeout(this.flushTimer);
|
|
133
149
|
this.flushTimer = null;
|
|
134
150
|
}
|
|
135
|
-
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private async waitForThrottle(): Promise<void> {
|
|
154
|
+
const waitMs = this.throttleMs() - (Date.now() - this.lastFlushAt);
|
|
155
|
+
if (waitMs > 0) await new Promise((r) => setTimeout(r, waitMs));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private takeFrame(): { text: string; consumed: number } {
|
|
159
|
+
const buffer = this.buffer;
|
|
160
|
+
if (!buffer) return { text: "", consumed: 0 };
|
|
161
|
+
if (Buffer.byteLength(buffer, "utf8") <= MAX_FRAME_BYTES) {
|
|
162
|
+
return { text: buffer, consumed: buffer.length };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let bytes = 0;
|
|
166
|
+
let consumed = 0;
|
|
167
|
+
let lastNewline = -1;
|
|
168
|
+
// Reserve one byte for the newline added when no line break is in range.
|
|
169
|
+
const scanLimit = MAX_FRAME_BYTES - 1;
|
|
170
|
+
for (const char of buffer) {
|
|
171
|
+
const size = Buffer.byteLength(char, "utf8");
|
|
172
|
+
if (bytes + size > scanLimit) break;
|
|
173
|
+
bytes += size;
|
|
174
|
+
consumed += char.length;
|
|
175
|
+
if (char === "\n") lastNewline = consumed;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (lastNewline > 0) {
|
|
179
|
+
return { text: buffer.slice(0, lastNewline), consumed: lastNewline };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// A single line longer than the platform shard limit: hard-cut on a code
|
|
183
|
+
// point boundary and terminate the shard with a newline. This is the only
|
|
184
|
+
// way to satisfy the API without dropping content.
|
|
185
|
+
if (consumed === 0) {
|
|
186
|
+
const first = buffer.codePointAt(0) ?? 0;
|
|
187
|
+
const char = String.fromCodePoint(first);
|
|
188
|
+
return { text: `${char}\n`, consumed: char.length };
|
|
189
|
+
}
|
|
190
|
+
return { text: `${buffer.slice(0, consumed)}\n`, consumed };
|
|
136
191
|
}
|
|
137
192
|
|
|
138
193
|
private async doFlush(state: "generating" | "done"): Promise<void> {
|
|
139
194
|
if (this.flushing) return;
|
|
140
|
-
|
|
195
|
+
this.clearFlushTimer();
|
|
196
|
+
const frame = state === "done" ? { text: "", consumed: 0 } : this.takeFrame();
|
|
197
|
+
const content = frame.text;
|
|
141
198
|
if (!content && state === "generating") return;
|
|
142
199
|
this.opts.onDebug?.(
|
|
143
200
|
`flush: start state=${state}, content=${content.length}, index=${this.index}, msgSeq=${this.msgSeq ?? "none"}, streamMsgId=${this.streamMsgId ?? "none"}`,
|
|
144
201
|
);
|
|
145
202
|
this.flushing = true;
|
|
203
|
+
this.flushingAppends = 0;
|
|
146
204
|
let failed = false;
|
|
147
205
|
try {
|
|
148
|
-
if (this.msgSeq === null) this.msgSeq =
|
|
206
|
+
if (this.msgSeq === null) this.msgSeq = allocateMsgSeq(this.msgId);
|
|
149
207
|
const index = this.index++;
|
|
150
208
|
const req = {
|
|
151
209
|
input_mode: "append",
|
|
@@ -160,7 +218,7 @@ export class AppendStream {
|
|
|
160
218
|
} as unknown as StreamMessageRequest;
|
|
161
219
|
const resp = await this.sendWithRetry(req, state);
|
|
162
220
|
if (resp?.id && !this.streamMsgId) this.streamMsgId = resp.id;
|
|
163
|
-
if (state === "generating") this.buffer = this.buffer.slice(
|
|
221
|
+
if (state === "generating") this.buffer = this.buffer.slice(frame.consumed);
|
|
164
222
|
this.lastFlushAt = Date.now();
|
|
165
223
|
this.opts.onDebug?.(
|
|
166
224
|
`flush: done state=${state}, remaining=${this.buffer.length}, streamMsgId=${this.streamMsgId ?? "none"}`,
|