@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
package/package.json
CHANGED
package/src/infra/file-logger.ts
CHANGED
|
@@ -18,7 +18,7 @@ const DEFAULT_LOG_LEVEL: LogLevel = "silent";
|
|
|
18
18
|
let activeLevel: LogLevel = DEFAULT_LOG_LEVEL;
|
|
19
19
|
let activeDir: string | undefined;
|
|
20
20
|
let writeQueue: Promise<void> = Promise.resolve();
|
|
21
|
-
let logGate: (() => boolean) | undefined;
|
|
21
|
+
let logGate: ((scope: string) => boolean) | undefined;
|
|
22
22
|
|
|
23
23
|
function isLogLevel(value: unknown): value is LogLevel {
|
|
24
24
|
return typeof value === "string" && Object.hasOwn(LEVEL_WEIGHT, value);
|
|
@@ -45,19 +45,61 @@ export function configureLogger(level: LogLevel, dir?: string): void {
|
|
|
45
45
|
activeDir = dir;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export function setLogGate(gate: (() => boolean) | undefined): void {
|
|
48
|
+
export function setLogGate(gate: ((scope: string) => boolean) | undefined): void {
|
|
49
49
|
logGate = gate;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
function isLogAllowed(): boolean {
|
|
52
|
+
function isLogAllowed(scope: string): boolean {
|
|
53
53
|
if (!logGate) return true;
|
|
54
54
|
try {
|
|
55
|
-
return logGate();
|
|
55
|
+
return logGate(scope);
|
|
56
56
|
} catch {
|
|
57
57
|
return false;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
const REDACTED = "<redacted>";
|
|
62
|
+
const SECRET_KEY_RE =
|
|
63
|
+
/^(?:password|passwd|pwd|secret|token|session|sessionId|cookie|authorization|api[_-]?key|client[_-]?secret|app[_-]?secret|access[_-]?token|refresh[_-]?token|private[_-]?key)$/i;
|
|
64
|
+
|
|
65
|
+
/** Redact common credential shapes while keeping the surrounding log text. */
|
|
66
|
+
export function redactLogText(text: string): string {
|
|
67
|
+
return text
|
|
68
|
+
.replace(/(\bAuthorization\s*:\s*(?:Bearer\s+)?)([^\s,;]+)/gi, `$1${REDACTED}`)
|
|
69
|
+
.replace(/(\bCookie\s*:\s*)([^\r\n]+)/gi, `$1${REDACTED}`)
|
|
70
|
+
.replace(
|
|
71
|
+
/(\b["']?(?:password|passwd|pwd|secret|token|session|sessionId|api[_-]?key|client[_-]?secret|app[_-]?secret|access[_-]?token|refresh[_-]?token|private[_-]?key)["']?\s*[:=]\s*)(?:"[^"]*"|'[^']*'|[^\s,;&]+)/gi,
|
|
72
|
+
`$1${REDACTED}`,
|
|
73
|
+
)
|
|
74
|
+
.replace(/(\bBearer\s+)([A-Za-z0-9._~+/=-]+)/gi, `$1${REDACTED}`)
|
|
75
|
+
.replace(/(\bmysql\b[^\r\n]*?\s-p)([^\s]+)/gi, `$1${REDACTED}`)
|
|
76
|
+
.replace(/((?:[A-Za-z0-9_-]+:)+session:)([A-Za-z0-9._:-]{8,})/gi, `$1${REDACTED}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function redactValue(value: unknown, key?: string): unknown {
|
|
80
|
+
if (key && SECRET_KEY_RE.test(key)) return REDACTED;
|
|
81
|
+
if (typeof value === "string") return redactLogText(value);
|
|
82
|
+
if (Array.isArray(value)) return value.map((item) => redactValue(item));
|
|
83
|
+
if (value instanceof Error) {
|
|
84
|
+
return {
|
|
85
|
+
name: value.name,
|
|
86
|
+
message: redactLogText(value.message),
|
|
87
|
+
stack: value.stack ? redactLogText(value.stack) : undefined,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
if (value instanceof Date) return value;
|
|
91
|
+
if (value !== null && typeof value === "object") {
|
|
92
|
+
const prototype = Object.getPrototypeOf(value);
|
|
93
|
+
if (prototype !== Object.prototype && prototype !== null) return value;
|
|
94
|
+
const redacted: LogFields = {};
|
|
95
|
+
for (const [childKey, childValue] of Object.entries(value)) {
|
|
96
|
+
redacted[childKey] = redactValue(childValue, childKey);
|
|
97
|
+
}
|
|
98
|
+
return redacted;
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
|
|
61
103
|
function resolveLogDir(): string {
|
|
62
104
|
return process.env.PI_QQ_LOG_DIR ?? activeDir ?? join(process.cwd(), ".pi", "pi-qq", "logs");
|
|
63
105
|
}
|
|
@@ -91,7 +133,7 @@ function stringifyError(err: unknown): string {
|
|
|
91
133
|
|
|
92
134
|
function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message: string, data?: unknown): void {
|
|
93
135
|
if (!isLevelEnabled(activeLevel, level)) return;
|
|
94
|
-
if (!isLogAllowed()) return;
|
|
136
|
+
if (!isLogAllowed(scope)) return;
|
|
95
137
|
|
|
96
138
|
const msgKey = logMessageKey(message);
|
|
97
139
|
const record: LogFields = {
|
|
@@ -99,12 +141,12 @@ function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message:
|
|
|
99
141
|
level,
|
|
100
142
|
scope,
|
|
101
143
|
...(msgKey ? { msgKey } : {}),
|
|
102
|
-
msg: message,
|
|
144
|
+
msg: redactLogText(message),
|
|
103
145
|
};
|
|
104
146
|
const fields = normalizeData(data);
|
|
105
147
|
if (fields) {
|
|
106
148
|
for (const [key, value] of Object.entries(fields)) {
|
|
107
|
-
if (!(key in record)) record[key] = value;
|
|
149
|
+
if (!(key in record)) record[key] = redactValue(value, key);
|
|
108
150
|
}
|
|
109
151
|
}
|
|
110
152
|
|
|
@@ -123,7 +165,7 @@ function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message:
|
|
|
123
165
|
level,
|
|
124
166
|
scope,
|
|
125
167
|
...(msgKey ? { msgKey } : {}),
|
|
126
|
-
msg: message,
|
|
168
|
+
msg: redactLogText(message),
|
|
127
169
|
serializationError: stringifyError(err),
|
|
128
170
|
});
|
|
129
171
|
}
|
|
@@ -140,6 +182,25 @@ function writeRecord(level: Exclude<LogLevel, "silent">, scope: string, message:
|
|
|
140
182
|
.catch(() => {});
|
|
141
183
|
}
|
|
142
184
|
|
|
185
|
+
export function logValueLength(value: unknown): number {
|
|
186
|
+
if (value === undefined || value === null) return 0;
|
|
187
|
+
if (typeof value === "string") return value.length;
|
|
188
|
+
try {
|
|
189
|
+
const text = JSON.stringify(value);
|
|
190
|
+
return typeof text === "string" ? text.length : 0;
|
|
191
|
+
} catch {
|
|
192
|
+
return 0;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function summarizeSdkMessage(message: string): string {
|
|
197
|
+
const marker = "Body: ";
|
|
198
|
+
const index = message.indexOf(marker);
|
|
199
|
+
if (index < 0) return message;
|
|
200
|
+
const bodyLength = message.length - index - marker.length;
|
|
201
|
+
return `${message.slice(0, index + marker.length)}<${bodyLength} chars>`;
|
|
202
|
+
}
|
|
203
|
+
|
|
143
204
|
export const log = {
|
|
144
205
|
debug(scope: string, message: string, data?: unknown): void {
|
|
145
206
|
writeRecord("debug", scope, message, data);
|
|
@@ -157,32 +218,15 @@ export const log = {
|
|
|
157
218
|
|
|
158
219
|
export const sdkLogger = {
|
|
159
220
|
debug(message: string, meta?: Record<string, unknown>): void {
|
|
160
|
-
log.debug("qqbot", message, meta);
|
|
221
|
+
log.debug("qqbot", summarizeSdkMessage(message), meta);
|
|
161
222
|
},
|
|
162
223
|
info(message: string, meta?: Record<string, unknown>): void {
|
|
163
|
-
log.info("qqbot", message, meta);
|
|
224
|
+
log.info("qqbot", summarizeSdkMessage(message), meta);
|
|
164
225
|
},
|
|
165
226
|
warn(message: string, meta?: Record<string, unknown>): void {
|
|
166
|
-
log.warn("qqbot", message, meta);
|
|
227
|
+
log.warn("qqbot", summarizeSdkMessage(message), meta);
|
|
167
228
|
},
|
|
168
229
|
error(message: string, meta?: Record<string, unknown>): void {
|
|
169
|
-
log.error("qqbot", message, meta);
|
|
230
|
+
log.error("qqbot", summarizeSdkMessage(message), meta);
|
|
170
231
|
},
|
|
171
232
|
};
|
|
172
|
-
|
|
173
|
-
export function previewForLog(value: unknown, maxChars = 2000): string {
|
|
174
|
-
let text: string;
|
|
175
|
-
if (typeof value === "string") {
|
|
176
|
-
text = value;
|
|
177
|
-
} else if (value instanceof Error) {
|
|
178
|
-
text = `${value.name}: ${value.message}`;
|
|
179
|
-
} else {
|
|
180
|
-
try {
|
|
181
|
-
const stringified = JSON.stringify(value);
|
|
182
|
-
text = typeof stringified === "string" ? stringified : String(value);
|
|
183
|
-
} catch {
|
|
184
|
-
text = String(value);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return text.length <= maxChars ? text : `${text.slice(0, maxChars)}…(${text.length} chars)`;
|
|
188
|
-
}
|
|
@@ -60,12 +60,22 @@ export async function handleInboundCommand(
|
|
|
60
60
|
deps.ctx.abort();
|
|
61
61
|
const waitStartedAt = Date.now();
|
|
62
62
|
log.debug("inbound", "stream timing", { phase: "/stop wait start" });
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
const waiting = Promise.resolve()
|
|
64
|
+
.then(() => deps.waitForEnd?.())
|
|
65
|
+
.then(() => {
|
|
66
|
+
log.debug("inbound", "stream timing", {
|
|
67
|
+
phase: "/stop wait done",
|
|
68
|
+
waitedMs: Date.now() - waitStartedAt,
|
|
69
|
+
});
|
|
70
|
+
})
|
|
71
|
+
.catch((err) => {
|
|
72
|
+
log.warn("inbound", "/stop wait failed", {
|
|
73
|
+
waitedMs: Date.now() - waitStartedAt,
|
|
74
|
+
err,
|
|
75
|
+
});
|
|
76
|
+
});
|
|
68
77
|
await reply(t("commands.stop.abort"));
|
|
78
|
+
void waiting;
|
|
69
79
|
}
|
|
70
80
|
break;
|
|
71
81
|
case "/compact":
|
package/src/qq/media-tool.ts
CHANGED
|
@@ -18,6 +18,7 @@ import { log } from "../infra/file-logger";
|
|
|
18
18
|
import { t } from "../infra/i18n";
|
|
19
19
|
import type { ReplyBudget } from "../quota/reply-quota";
|
|
20
20
|
import { TEXT_CHUNK_LIMIT } from "./reply-sender";
|
|
21
|
+
import { sendWithMsgSeq } from "./msg-seq-send";
|
|
21
22
|
|
|
22
23
|
export const MEDIA_TOOL_NAME = "qq_send_media";
|
|
23
24
|
|
|
@@ -298,9 +299,14 @@ function createTool(deps: MediaToolDeps): ToolDefinition<typeof mediaParams, Med
|
|
|
298
299
|
});
|
|
299
300
|
|
|
300
301
|
const sendMedia = (target: string, passiveMsgId?: string) =>
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
302
|
+
sendWithMsgSeq(bot, {
|
|
303
|
+
target: {
|
|
304
|
+
scope: "c2c",
|
|
305
|
+
targetId: target,
|
|
306
|
+
...(passiveMsgId ? { msgId: passiveMsgId } : {}),
|
|
307
|
+
},
|
|
308
|
+
media: { file_info: upload.file_info },
|
|
309
|
+
...(caption ? { content: caption } : {}),
|
|
304
310
|
});
|
|
305
311
|
const buildResult = (
|
|
306
312
|
messageId: string,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { QQBot, SendMessageOptions } from "@tencent-connect/qqbot-nodejs";
|
|
2
|
+
import { allocateMsgSeq } from "../relay/msg-seq";
|
|
3
|
+
|
|
4
|
+
export function sendWithMsgSeq(bot: QQBot, options: SendMessageOptions) {
|
|
5
|
+
const msgId = options.target.msgId;
|
|
6
|
+
if (!msgId) return bot.send(options);
|
|
7
|
+
return bot.send({
|
|
8
|
+
...options,
|
|
9
|
+
extra: { ...options.extra, msg_seq: allocateMsgSeq(msgId) },
|
|
10
|
+
});
|
|
11
|
+
}
|
package/src/qq/qq-bot.ts
CHANGED
|
@@ -65,18 +65,12 @@ export function startBot(opts: StartBotOptions): RunningBot {
|
|
|
65
65
|
rawEventType: msg.rawEventType,
|
|
66
66
|
messageId: msg.messageId,
|
|
67
67
|
senderId: msg.senderId,
|
|
68
|
-
senderName: msg.senderName,
|
|
69
68
|
senderIsBot: msg.senderIsBot,
|
|
70
|
-
content: msg.content,
|
|
71
69
|
msgType: msg.msgType,
|
|
72
70
|
timestamp: msg.timestamp,
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
messageScene: msg.messageScene,
|
|
77
|
-
attachments: msg.attachments,
|
|
78
|
-
msgElements: msg.msgElements,
|
|
79
|
-
raw: msg.raw,
|
|
71
|
+
contentLength: (msg.content ?? "").length,
|
|
72
|
+
attachmentCount: (msg.attachments ?? []).length,
|
|
73
|
+
elementCount: (msg.msgElements ?? []).length,
|
|
80
74
|
});
|
|
81
75
|
try {
|
|
82
76
|
await opts.onMessage?.(bot, msg);
|
package/src/qq/qq-diagnostics.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { AppendStream } from "../relay/stream-session";
|
|
|
5
5
|
import type { RunningBot } from "./qq-bot";
|
|
6
6
|
import { t } from "../infra/i18n";
|
|
7
7
|
import { thinkingHeader } from "../relay/stream-block-format";
|
|
8
|
+
import { sendWithMsgSeq } from "./msg-seq-send";
|
|
8
9
|
|
|
9
10
|
interface QQDiagnosticsDeps {
|
|
10
11
|
getRunning: () => RunningBot | undefined;
|
|
@@ -42,7 +43,7 @@ export function registerQQDiagnostics(pi: ExtensionAPI, deps: QQDiagnosticsDeps)
|
|
|
42
43
|
}
|
|
43
44
|
const content = args?.trim() || "**Markdown test**\n# Heading\n- Item one\n- Item two\n[Link](https://qq.com)";
|
|
44
45
|
try {
|
|
45
|
-
await running.bot
|
|
46
|
+
await sendWithMsgSeq(running.bot, { target, markdown: { content } });
|
|
46
47
|
ctx.ui.notify(t("diagnostics.mdSent"), "info");
|
|
47
48
|
} catch (err) {
|
|
48
49
|
ctx.ui.notify(t("diagnostics.mdFailed", { message: (err as Error).message }), "error");
|
|
@@ -36,17 +36,12 @@ export function createInboundHandler(deps: InboundDeps) {
|
|
|
36
36
|
rawEventType: msg.rawEventType,
|
|
37
37
|
messageId: msg.messageId,
|
|
38
38
|
senderId: msg.senderId,
|
|
39
|
-
senderName: msg.senderName,
|
|
40
39
|
senderIsBot: msg.senderIsBot,
|
|
41
|
-
content: msg.content,
|
|
42
40
|
msgType: msg.msgType,
|
|
43
41
|
timestamp: msg.timestamp,
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
attachments: msg.attachments,
|
|
48
|
-
msgElements: msg.msgElements,
|
|
49
|
-
raw: msg.raw,
|
|
42
|
+
contentLength: (msg.content ?? "").length,
|
|
43
|
+
attachmentCount: (msg.attachments ?? []).length,
|
|
44
|
+
elementCount: (msg.msgElements ?? []).length,
|
|
50
45
|
});
|
|
51
46
|
|
|
52
47
|
if (msg.kind !== "c2c") {
|
package/src/qq/qr-login.ts
CHANGED
|
@@ -47,7 +47,7 @@ export async function runQrLogin(ctx: ExtensionContext): Promise<QrLoginResult |
|
|
|
47
47
|
causeCode: cause && typeof cause === "object" ? (cause as NodeJS.ErrnoException).code : undefined,
|
|
48
48
|
causeMessage: cause instanceof Error ? cause.message : undefined,
|
|
49
49
|
});
|
|
50
|
-
ctx.ui.notify(t("login.failed")
|
|
50
|
+
ctx.ui.notify(`${t("login.failed")}: ${err.message}`, "error");
|
|
51
51
|
finish();
|
|
52
52
|
closeOverlay?.();
|
|
53
53
|
},
|
package/src/qq/reply-sender.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { QQBot } from "@tencent-connect/qqbot-nodejs";
|
|
2
2
|
import { ReplyBudget } from "../quota/reply-quota";
|
|
3
3
|
import { AppendStream } from "../relay/stream-session";
|
|
4
|
+
import { sendWithMsgSeq } from "./msg-seq-send";
|
|
4
5
|
import { log } from "../infra/file-logger";
|
|
5
6
|
import { t } from "../infra/i18n";
|
|
6
7
|
|
|
@@ -120,9 +121,8 @@ export async function sendReply(bot: QQBot, text: string, ch: ReplyChannel): Pro
|
|
|
120
121
|
targetId,
|
|
121
122
|
msgId: msgId ?? null,
|
|
122
123
|
length: chunk.length,
|
|
123
|
-
text: chunk,
|
|
124
124
|
});
|
|
125
|
-
await bot
|
|
125
|
+
await sendWithMsgSeq(bot, { target: replyTarget, content: chunk });
|
|
126
126
|
if (mode === "passive" && msgId) ch.budget.commitPassive(msgId);
|
|
127
127
|
log.info("outbound", "text chunk sent", { mode, targetId, length: chunk.length });
|
|
128
128
|
} catch (err) {
|
|
@@ -135,7 +135,10 @@ export async function sendReply(bot: QQBot, text: string, ch: ReplyChannel): Pro
|
|
|
135
135
|
if (mode === "passive") {
|
|
136
136
|
try {
|
|
137
137
|
log.warn("outbound", "passive send failed; retrying proactively", { targetId: ch.boundOpenid });
|
|
138
|
-
await bot
|
|
138
|
+
await sendWithMsgSeq(bot, {
|
|
139
|
+
target: { scope: "c2c", targetId: ch.boundOpenid },
|
|
140
|
+
content: chunk,
|
|
141
|
+
});
|
|
139
142
|
ch.onWarn?.(t("outbound.passiveFailed"));
|
|
140
143
|
continue;
|
|
141
144
|
} catch (retryErr) {
|
|
@@ -26,6 +26,15 @@ function isDuplicateMsgSeqError(err: unknown): boolean {
|
|
|
26
26
|
);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
function errorCode(err: unknown): number | undefined {
|
|
30
|
+
const e = err as { code?: number; err_code?: number; bizCode?: number };
|
|
31
|
+
return e?.code ?? e?.err_code ?? e?.bizCode;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isExpiredStreamError(err: unknown): boolean {
|
|
35
|
+
return errorCode(err) === 40034020 || /40034020|超过时间限制|time limit/i.test(String((err as Error)?.message ?? ""));
|
|
36
|
+
}
|
|
37
|
+
|
|
29
38
|
export interface RepushTool {
|
|
30
39
|
name: string;
|
|
31
40
|
args?: unknown;
|
|
@@ -63,6 +72,8 @@ export class BubbleWriter {
|
|
|
63
72
|
private toolOpen = false;
|
|
64
73
|
private queue: Promise<void> = Promise.resolve();
|
|
65
74
|
private disposed = false;
|
|
75
|
+
private terminalCode?: number;
|
|
76
|
+
private lastErrorCode?: number;
|
|
66
77
|
|
|
67
78
|
constructor(private readonly deps: BubbleWriterDeps) {}
|
|
68
79
|
|
|
@@ -74,6 +85,14 @@ export class BubbleWriter {
|
|
|
74
85
|
return this.streamDead;
|
|
75
86
|
}
|
|
76
87
|
|
|
88
|
+
get deadCode(): number | undefined {
|
|
89
|
+
return this.terminalCode;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get hasActiveStream(): boolean {
|
|
93
|
+
return Boolean(this.cur) && !this.streamDead && !this.disposed;
|
|
94
|
+
}
|
|
95
|
+
|
|
77
96
|
stats(now = Date.now()): {
|
|
78
97
|
dead: boolean;
|
|
79
98
|
disposed: boolean;
|
|
@@ -202,6 +221,8 @@ export class BubbleWriter {
|
|
|
202
221
|
this.streamDead = false;
|
|
203
222
|
this.pendingResume = undefined;
|
|
204
223
|
this.resumeAttempts = 0;
|
|
224
|
+
this.terminalCode = undefined;
|
|
225
|
+
this.lastErrorCode = undefined;
|
|
205
226
|
this.lastKind = "none";
|
|
206
227
|
this.blockClosed = false;
|
|
207
228
|
this.toolOpen = false;
|
|
@@ -225,13 +246,14 @@ export class BubbleWriter {
|
|
|
225
246
|
await this.queue;
|
|
226
247
|
if (this.streamDead) return false;
|
|
227
248
|
if (this.cur !== stream) continue;
|
|
249
|
+
this.lastErrorCode = undefined;
|
|
228
250
|
return true;
|
|
229
251
|
} catch (err) {
|
|
230
252
|
this.handleStreamError(err as Error);
|
|
231
253
|
if (this.streamDead) return false;
|
|
232
254
|
}
|
|
233
255
|
}
|
|
234
|
-
this.killStream("resume attempts exhausted");
|
|
256
|
+
this.killStream("resume attempts exhausted", this.lastErrorCode);
|
|
235
257
|
return false;
|
|
236
258
|
}
|
|
237
259
|
|
|
@@ -361,6 +383,7 @@ export class BubbleWriter {
|
|
|
361
383
|
const outgoing = opts.thinking && (!this.cur || this.bubbleChars === 0) ? `${thinkingHeader()}${text}` : text;
|
|
362
384
|
try {
|
|
363
385
|
await stream.append(outgoing);
|
|
386
|
+
this.lastErrorCode = undefined;
|
|
364
387
|
this.bubbleChars += outgoing.length;
|
|
365
388
|
this.lastFrameAt = Date.now();
|
|
366
389
|
this.trackFences(outgoing);
|
|
@@ -457,6 +480,7 @@ export class BubbleWriter {
|
|
|
457
480
|
if (!bot || !this.deps.msgId) return null;
|
|
458
481
|
const resume = this.pendingResume;
|
|
459
482
|
this.pendingResume = undefined;
|
|
483
|
+
const fresh = !resume || (resume.msgSeq === undefined && !resume.streamMsgId);
|
|
460
484
|
const stream = new AppendStream(bot.messageApi, bot.getCredentials(), this.deps.openid, this.deps.msgId, {
|
|
461
485
|
resume,
|
|
462
486
|
onError: (err) => {
|
|
@@ -484,7 +508,7 @@ export class BubbleWriter {
|
|
|
484
508
|
onDebug: (message) => log.debug("stream", message, { msgId: this.logMsgId() }),
|
|
485
509
|
});
|
|
486
510
|
this.cur = stream;
|
|
487
|
-
if (
|
|
511
|
+
if (fresh) this.deps.budget.noteStreamBubble(this.deps.msgId);
|
|
488
512
|
if (isLogEnabled("debug")) {
|
|
489
513
|
log.debug("stream", "stream timing", {
|
|
490
514
|
phase: "stream open",
|
|
@@ -510,11 +534,15 @@ export class BubbleWriter {
|
|
|
510
534
|
const checkpoint = stream?.checkpoint() ?? null;
|
|
511
535
|
stream?.dispose();
|
|
512
536
|
this.cur = null;
|
|
537
|
+
const code = errorCode(err);
|
|
513
538
|
const resetSeq = isDuplicateMsgSeqError(err);
|
|
539
|
+
const expired = isExpiredStreamError(err);
|
|
540
|
+
this.lastErrorCode = code;
|
|
514
541
|
log.warn("stream", "stream error", {
|
|
515
542
|
msgId: this.logMsgId(),
|
|
516
543
|
elapsedMs: Date.now() - this.deps.startedAt,
|
|
517
544
|
resetSeq,
|
|
545
|
+
expired,
|
|
518
546
|
checkpoint: checkpoint
|
|
519
547
|
? {
|
|
520
548
|
index: checkpoint.index,
|
|
@@ -528,26 +556,29 @@ export class BubbleWriter {
|
|
|
528
556
|
});
|
|
529
557
|
if (checkpoint && this.resumeAttempts < MAX_RESUME_ATTEMPTS) {
|
|
530
558
|
this.resumeAttempts += 1;
|
|
531
|
-
this.pendingResume =
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
559
|
+
this.pendingResume =
|
|
560
|
+
resetSeq || expired
|
|
561
|
+
? {
|
|
562
|
+
index: 0,
|
|
563
|
+
...(checkpoint.buffer ? { buffer: checkpoint.buffer } : {}),
|
|
564
|
+
}
|
|
565
|
+
: checkpoint;
|
|
537
566
|
return;
|
|
538
567
|
}
|
|
539
|
-
this.killStream(err.message);
|
|
568
|
+
this.killStream(err.message, code);
|
|
540
569
|
}
|
|
541
570
|
|
|
542
|
-
private killStream(reason: string): void {
|
|
571
|
+
private killStream(reason: string, code = this.lastErrorCode): void {
|
|
543
572
|
if (this.streamDead) return;
|
|
544
573
|
this.streamDead = true;
|
|
574
|
+
this.terminalCode = code;
|
|
545
575
|
this.pendingResume = undefined;
|
|
546
576
|
this.cur?.dispose();
|
|
547
577
|
this.cur = null;
|
|
548
578
|
log.warn("stream", "stream unavailable", {
|
|
549
579
|
msgId: this.logMsgId(),
|
|
550
580
|
reason,
|
|
581
|
+
code: code ?? null,
|
|
551
582
|
stats: this.stats(),
|
|
552
583
|
budget: this.deps.budget.snapshot(),
|
|
553
584
|
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const MAX_MSG_SEQ = 0xffff;
|
|
2
|
+
const nextByMsgId = new Map<string, number>();
|
|
3
|
+
|
|
4
|
+
export function allocateMsgSeq(msgId: string): number {
|
|
5
|
+
const next = nextByMsgId.get(msgId) ?? 0;
|
|
6
|
+
if (next > MAX_MSG_SEQ) {
|
|
7
|
+
throw new Error("msg_seq exhausted for message");
|
|
8
|
+
}
|
|
9
|
+
nextByMsgId.set(msgId, next + 1);
|
|
10
|
+
return next;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function resetMsgSeqAllocator(): void {
|
|
14
|
+
nextByMsgId.clear();
|
|
15
|
+
}
|
|
@@ -39,6 +39,7 @@ export type RepushResult =
|
|
|
39
39
|
const TYPING_INTERVAL_MS = 50_000;
|
|
40
40
|
const PENDING_TTL_MS = 15_000;
|
|
41
41
|
const RECOVERY_TTL_MS = 60_000;
|
|
42
|
+
const AUTO_REBIND_TTL_MS = 5 * 60_000;
|
|
42
43
|
|
|
43
44
|
function toRepushTool(tool: TimelineTool): RepushTool {
|
|
44
45
|
return {
|
|
@@ -55,6 +56,7 @@ export class RunFeedback {
|
|
|
55
56
|
private recovery: { openid: string; msgId: string; at: number } | null = null;
|
|
56
57
|
private run: RunState | null = null;
|
|
57
58
|
private lastCompletedText: string | undefined;
|
|
59
|
+
private autoRebinds = 0;
|
|
58
60
|
private endPromise: Promise<void> = Promise.resolve();
|
|
59
61
|
private endResolve?: () => void;
|
|
60
62
|
|
|
@@ -144,6 +146,7 @@ export class RunFeedback {
|
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
onAgentStart(): void {
|
|
149
|
+
this.autoRebinds = 0;
|
|
147
150
|
this.endPromise = new Promise((resolve) => {
|
|
148
151
|
this.endResolve = resolve;
|
|
149
152
|
});
|
|
@@ -205,6 +208,9 @@ export class RunFeedback {
|
|
|
205
208
|
}
|
|
206
209
|
|
|
207
210
|
const keepTyping = () => {
|
|
211
|
+
// A live stream already shows activity; do not compete with it for the
|
|
212
|
+
// serialized API client, especially when the network is slow.
|
|
213
|
+
if (run.writer.hasActiveStream) return;
|
|
208
214
|
void bot.sendTyping({ scope: "c2c", targetId: run.openid }, 60).catch(() => {});
|
|
209
215
|
};
|
|
210
216
|
keepTyping();
|
|
@@ -294,6 +300,7 @@ export class RunFeedback {
|
|
|
294
300
|
run.finalizedText = [run.finalizedText, text].filter((part) => part.trim()).join("\n\n");
|
|
295
301
|
const delta = run.finalizedText.slice(run.resultSentLen);
|
|
296
302
|
run.resultSentLen = run.finalizedText.length;
|
|
303
|
+
if (!isError) this.tryAutoRebind(run);
|
|
297
304
|
if (!isError && text) this.emitTextDelta(run, delta, policy);
|
|
298
305
|
if (!isError && text) run.writer.closeBlock();
|
|
299
306
|
|
|
@@ -303,7 +310,11 @@ export class RunFeedback {
|
|
|
303
310
|
}
|
|
304
311
|
|
|
305
312
|
const hadPendingTools = this.hasPendingTools(run);
|
|
306
|
-
|
|
313
|
+
let ok = await run.writer.flush();
|
|
314
|
+
if (!isError && !ok && run.writer.deadCode === 40034020) {
|
|
315
|
+
this.tryAutoRebind(run);
|
|
316
|
+
if (this.autoRebinds > 0) ok = await run.writer.flush();
|
|
317
|
+
}
|
|
307
318
|
const streamTookOver = !isError && ok && run.writer.hasPushed;
|
|
308
319
|
log.debug("run", "stream takeover decision", {
|
|
309
320
|
phase: "assistant final",
|
|
@@ -387,6 +398,36 @@ export class RunFeedback {
|
|
|
387
398
|
return run.timeline.hasPendingTools();
|
|
388
399
|
}
|
|
389
400
|
|
|
401
|
+
private tryAutoRebind(run: RunState): void {
|
|
402
|
+
if (this.autoRebinds > 0 || run.writer.deadCode !== 40034020) return;
|
|
403
|
+
|
|
404
|
+
let openid = run.openid;
|
|
405
|
+
let msgId = run.msgId;
|
|
406
|
+
let freshTarget = false;
|
|
407
|
+
let targetAgeMs: number | null = null;
|
|
408
|
+
const target = this.deps.budget.lastTarget;
|
|
409
|
+
if (target && Date.now() - target.arrivedAt <= AUTO_REBIND_TTL_MS) {
|
|
410
|
+
openid = target.openid;
|
|
411
|
+
msgId = target.msgId;
|
|
412
|
+
freshTarget = true;
|
|
413
|
+
targetAgeMs = Date.now() - target.arrivedAt;
|
|
414
|
+
}
|
|
415
|
+
if (!msgId) return;
|
|
416
|
+
|
|
417
|
+
const result = this.repushCurrent(openid, msgId);
|
|
418
|
+
if (!result.ok) return;
|
|
419
|
+
this.autoRebinds += 1;
|
|
420
|
+
log.debug("run", "stream timing", {
|
|
421
|
+
phase: "auto rebind",
|
|
422
|
+
msgId: msgId.slice(0, 8),
|
|
423
|
+
freshTarget,
|
|
424
|
+
targetAgeMs,
|
|
425
|
+
textLength: result.textLength,
|
|
426
|
+
thinkingLength: result.thinkingLength,
|
|
427
|
+
toolCount: result.toolCount,
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
|
|
390
431
|
private emitTextDelta(run: RunState, delta: string, policy: ReplyPolicy): void {
|
|
391
432
|
if (!delta || run.writer.dead) return;
|
|
392
433
|
if (policy.deferTextWhileToolsPending && this.hasPendingTools(run)) {
|