@chen_hu_hu/pi-qq 1.6.1 → 1.7.0

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.
@@ -1,5 +1,5 @@
1
- import { getNextMsgSeq, StreamContentType, StreamInputState } from "@tencent-connect/qqbot-nodejs/protocol";
2
- import { t } from "../infra/i18n";
1
+ import { StreamContentType, StreamInputState } from "@tencent-connect/qqbot-nodejs/protocol";
2
+ import { allocateMsgSeq } from "./msg-seq";
3
3
  import type {
4
4
  Credentials,
5
5
  MessageApi,
@@ -12,7 +12,7 @@ const RATE_LIMIT_BASE_MS = 1000;
12
12
  const MAX_RETRIES = 3;
13
13
 
14
14
  export interface AppendStreamCheckpoint {
15
- msgSeq: number;
15
+ msgSeq?: number;
16
16
  index: number;
17
17
  streamMsgId?: string;
18
18
  buffer?: string;
@@ -26,7 +26,7 @@ export interface AppendStreamFrameInfo {
26
26
  bufferLength: number;
27
27
  }
28
28
 
29
- export interface AppendStreamOptions {
29
+ interface AppendStreamOptions {
30
30
  throttleMs?: number;
31
31
  retryBaseMs?: number;
32
32
  resume?: AppendStreamCheckpoint;
@@ -54,7 +54,7 @@ export class AppendStream {
54
54
  ) {
55
55
  const resume = this.opts.resume;
56
56
  if (resume) {
57
- this.msgSeq = resume.msgSeq;
57
+ if (resume.msgSeq !== undefined) this.msgSeq = resume.msgSeq;
58
58
  this.index = resume.index;
59
59
  this.streamMsgId = resume.streamMsgId;
60
60
  this.buffer = resume.buffer ?? "";
@@ -146,7 +146,7 @@ export class AppendStream {
146
146
  this.flushing = true;
147
147
  let failed = false;
148
148
  try {
149
- if (this.msgSeq === null) this.msgSeq = getNextMsgSeq(this.msgId);
149
+ if (this.msgSeq === null) this.msgSeq = allocateMsgSeq(this.msgId);
150
150
  const index = this.index++;
151
151
  const req = {
152
152
  input_mode: "append",
@@ -198,18 +198,36 @@ export class AppendStream {
198
198
  ...(this.streamMsgId ? { streamMsgId: this.streamMsgId } : {}),
199
199
  bufferLength: this.buffer.length,
200
200
  });
201
- this.opts.onDebug?.(t("log.streamFrameFailed", { index: req.index, attempts: attempt + 1, reason }));
201
+ this.opts.onDebug?.(`frame index=${req.index} failed after ${attempt + 1} attempt(s): ${reason}`);
202
202
  throw err;
203
203
  }
204
204
  const delay = base * 2 ** attempt;
205
- this.opts.onDebug?.(t("log.streamFrameRateLimited", { index: req.index, attempt: attempt + 1, delay, reason }));
205
+ this.opts.onDebug?.(
206
+ `frame index=${req.index} rate limited (attempt ${attempt + 1}), retry in ${delay}ms: ${reason}`,
207
+ );
206
208
  await new Promise((r) => setTimeout(r, delay));
209
+ req.index = this.index++;
207
210
  }
208
211
  }
209
212
  }
210
213
 
211
214
  private isRateLimitError(err: unknown): boolean {
212
- const e = err as { status?: number; message?: string };
213
- return e?.status === 429 || /429|50002/.test(String(e?.message ?? ""));
215
+ const e = err as {
216
+ status?: number;
217
+ httpStatus?: number;
218
+ code?: number;
219
+ err_code?: number;
220
+ bizCode?: number;
221
+ message?: string;
222
+ };
223
+ return (
224
+ e?.status === 429 ||
225
+ e?.httpStatus === 429 ||
226
+ e?.code === 429 ||
227
+ e?.code === 50002 ||
228
+ e?.err_code === 50002 ||
229
+ e?.bizCode === 50002 ||
230
+ /429|50002/.test(String(e?.message ?? ""))
231
+ );
214
232
  }
215
233
  }