@chen_hu_hu/pi-qq 1.6.0 → 1.6.2
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 +36 -4
- package/README-zh.md +21 -44
- package/README.md +29 -52
- package/doc/archive/{delivery-v1.md → delivery.1.0.0.md} +1 -1
- package/doc/archive/{delivery-v2.md → delivery.1.2.0.md} +1 -1
- package/doc/archive/{delivery-v3.md → delivery.1.3.0.md} +1 -1
- package/doc/archive/{delivery-v4.md → delivery.1.4.0.md} +1 -1
- package/doc/archive/{delivery-v5.md → delivery.1.5.0.md} +2 -2
- package/doc/archive/{delivery-v6.md → delivery.1.6.0.md} +3 -3
- package/doc/archive/delivery.1.6.1.md +150 -0
- package/doc/archive/delivery.1.6.2.md +196 -0
- package/doc/todo.md +2 -2
- package/index.ts +30 -32
- package/package.json +1 -1
- package/src/attachments/attachment-download.ts +2 -1
- package/src/attachments/file-attachments.ts +4 -5
- package/src/infra/file-logger.ts +6 -6
- package/src/infra/i18n.ts +4 -198
- package/src/infra/log-message-keys.ts +66 -0
- package/src/infra/power-status.ts +1 -11
- package/src/infra/reply-type.ts +1 -1
- package/src/infra/settings.ts +0 -6
- package/src/qq/inbound-attachments.ts +2 -2
- package/src/qq/inbound-command.ts +8 -7
- package/src/qq/media-tool.ts +36 -60
- package/src/qq/qq-bot.ts +1 -1
- package/src/qq/qq-diagnostics.ts +1 -1
- package/src/qq/qq-message-handler.ts +7 -6
- package/src/qq/status-report.ts +1 -13
- package/src/quota/reply-quota.ts +2 -2
- package/src/relay/bubble-writer.ts +84 -38
- package/src/relay/reply-policy.ts +0 -13
- package/src/relay/run-stream-relay.ts +129 -203
- package/src/relay/run-timeline.ts +127 -0
- package/src/relay/stream-block-format.ts +1 -1
- package/src/relay/stream-session.ts +25 -8
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { QQBot, QQBotInboundMessage } from "@tencent-connect/qqbot-nodejs";
|
|
2
2
|
import { sendReply, sendStreamReply, type ReplyChannel } from "./reply-sender";
|
|
3
3
|
import type { ReplyBudget } from "../quota/reply-quota";
|
|
4
|
+
import type { RepushResult } from "../relay/run-stream-relay";
|
|
4
5
|
import { formatAttachmentFailures } from "../attachments/attachment-failure";
|
|
5
6
|
import {
|
|
6
7
|
formatAttachmentPrompt,
|
|
@@ -9,20 +10,19 @@ import {
|
|
|
9
10
|
type SavedFile,
|
|
10
11
|
} from "../attachments/file-attachments";
|
|
11
12
|
import { log } from "../infra/file-logger";
|
|
12
|
-
import type { ReplyType } from "../infra/reply-type";
|
|
13
13
|
import { t } from "../infra/i18n";
|
|
14
14
|
import { handleInboundCommand, type InboundCommandDeps } from "./inbound-command";
|
|
15
15
|
import { PendingAttachments, resolveInboundContent } from "./inbound-attachments";
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
interface InboundDeps extends InboundCommandDeps {
|
|
18
18
|
budget: ReplyBudget;
|
|
19
19
|
getAllowedOpenid: () => string | undefined;
|
|
20
|
+
reloadSettings: () => void;
|
|
20
21
|
getDeliverAs: () => "steer" | "followUp";
|
|
21
|
-
getReplyType: () => ReplyType;
|
|
22
22
|
getFileMaxSize: () => number;
|
|
23
23
|
saveFiles?: typeof saveInboundFiles;
|
|
24
24
|
streamReply?: typeof sendStreamReply;
|
|
25
|
-
repushCurrent: (openid: string, msgId: string) =>
|
|
25
|
+
repushCurrent: (openid: string, msgId: string) => RepushResult;
|
|
26
26
|
onWarn: (msg: string) => void;
|
|
27
27
|
markQQTrigger: (openid: string, msgId: string) => void;
|
|
28
28
|
}
|
|
@@ -60,6 +60,7 @@ export function createInboundHandler(deps: InboundDeps) {
|
|
|
60
60
|
return;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
deps.reloadSettings();
|
|
63
64
|
deps.budget.observeInbound(msg.senderId, msg.messageId);
|
|
64
65
|
log.info("inbound", "message received", {
|
|
65
66
|
messageId: msg.messageId,
|
|
@@ -70,7 +71,7 @@ export function createInboundHandler(deps: InboundDeps) {
|
|
|
70
71
|
|
|
71
72
|
const channel: ReplyChannel = {
|
|
72
73
|
budget: deps.budget,
|
|
73
|
-
boundOpenid:
|
|
74
|
+
boundOpenid: allowed,
|
|
74
75
|
onWarn: deps.onWarn,
|
|
75
76
|
};
|
|
76
77
|
const reply = (text: string) => sendReply(bot, text, channel);
|
|
@@ -101,7 +102,7 @@ export function createInboundHandler(deps: InboundDeps) {
|
|
|
101
102
|
}
|
|
102
103
|
},
|
|
103
104
|
restartPush: () => {
|
|
104
|
-
deps.repushCurrent(msg.senderId, msg.messageId);
|
|
105
|
+
return deps.repushCurrent(msg.senderId, msg.messageId);
|
|
105
106
|
},
|
|
106
107
|
});
|
|
107
108
|
return;
|
package/src/qq/status-report.ts
CHANGED
|
@@ -38,19 +38,7 @@ function maskOpenid(openid: string): string {
|
|
|
38
38
|
return `${openid.slice(0, 8)}…`;
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
function label(
|
|
42
|
-
key:
|
|
43
|
-
| "status.agent"
|
|
44
|
-
| "status.context"
|
|
45
|
-
| "status.model"
|
|
46
|
-
| "status.login"
|
|
47
|
-
| "status.connection"
|
|
48
|
-
| "status.qqUser"
|
|
49
|
-
| "status.battery"
|
|
50
|
-
| "status.replyType"
|
|
51
|
-
| "status.settings",
|
|
52
|
-
value: string,
|
|
53
|
-
): string {
|
|
41
|
+
function label(key: TranslationKey, value: string): string {
|
|
54
42
|
return `${t(key)}${t("status.separator")}${value}`;
|
|
55
43
|
}
|
|
56
44
|
|
package/src/quota/reply-quota.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ReplyLimiter } from "@tencent-connect/qqbot-nodejs/protocol";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type ReplyMode = "passive" | "proactive";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
interface AcquireResult {
|
|
6
6
|
mode: ReplyMode;
|
|
7
7
|
openid?: string;
|
|
8
8
|
msgId?: string;
|
|
@@ -16,6 +16,26 @@ type BlockKind = "none" | "thinking" | "tool" | "text";
|
|
|
16
16
|
const MAX_RESUME_ATTEMPTS = 2;
|
|
17
17
|
const MAX_BUBBLE_CHARS = 12_000;
|
|
18
18
|
|
|
19
|
+
function isDuplicateMsgSeqError(err: unknown): boolean {
|
|
20
|
+
const e = err as { code?: number; err_code?: number; bizCode?: number; message?: string };
|
|
21
|
+
return (
|
|
22
|
+
e?.code === 40054005 ||
|
|
23
|
+
e?.err_code === 40054005 ||
|
|
24
|
+
e?.bizCode === 40054005 ||
|
|
25
|
+
/40054005|消息被去重|msgseq/i.test(String(e?.message ?? ""))
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface RepushTool {
|
|
30
|
+
name: string;
|
|
31
|
+
args?: unknown;
|
|
32
|
+
skillName?: string;
|
|
33
|
+
result?: { isError: boolean; result?: unknown };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type RepushBlock =
|
|
37
|
+
{ kind: "thinking"; text: string } | { kind: "tool"; tool: RepushTool } | { kind: "text"; text: string };
|
|
38
|
+
|
|
19
39
|
export interface BubbleWriterDeps {
|
|
20
40
|
bot: () => QQBot | undefined;
|
|
21
41
|
budget: ReplyBudget;
|
|
@@ -89,14 +109,14 @@ export class BubbleWriter {
|
|
|
89
109
|
const newBubble = this.startsNewBlock("thinking");
|
|
90
110
|
const prefix = this.beginBlock("thinking", newBubble);
|
|
91
111
|
this.thinkingAtLineStart = delta.endsWith("\n");
|
|
92
|
-
this.push(`${prefix}${delta}`, prefix.length > 0 || newBubble,
|
|
112
|
+
this.push(`${prefix}${delta}`, { blockBoundary: prefix.length > 0 || newBubble, newBubble, thinking: true });
|
|
93
113
|
}
|
|
94
114
|
|
|
95
115
|
pushText(text: string): void {
|
|
96
116
|
if (this.streamDead || this.disposed) return;
|
|
97
117
|
const newBubble = this.startsNewBlock("text");
|
|
98
118
|
const prefix = this.beginBlock("text", newBubble);
|
|
99
|
-
this.push(`${prefix}${text}`, prefix.length > 0 || newBubble,
|
|
119
|
+
this.push(`${prefix}${text}`, { blockBoundary: prefix.length > 0 || newBubble, newBubble });
|
|
100
120
|
}
|
|
101
121
|
|
|
102
122
|
pushTool(name: string, args: unknown): void {
|
|
@@ -114,14 +134,11 @@ export class BubbleWriter {
|
|
|
114
134
|
budget: this.deps.budget.snapshot(),
|
|
115
135
|
});
|
|
116
136
|
}
|
|
117
|
-
this.push(
|
|
118
|
-
|
|
119
|
-
prefix.length > 0 || newBubble,
|
|
120
|
-
false,
|
|
137
|
+
this.push(`${prefix}${formatToolLine(name, args)}\n\n`, {
|
|
138
|
+
blockBoundary: prefix.length > 0 || newBubble,
|
|
121
139
|
newBubble,
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
);
|
|
140
|
+
trace: "tool",
|
|
141
|
+
});
|
|
125
142
|
}
|
|
126
143
|
|
|
127
144
|
pushSkill(name: string): void {
|
|
@@ -129,25 +146,25 @@ export class BubbleWriter {
|
|
|
129
146
|
this.toolOpen = false;
|
|
130
147
|
const newBubble = true;
|
|
131
148
|
const prefix = this.beginBlock("tool", newBubble);
|
|
132
|
-
this.push(`${prefix}${formatSkillBlock(name)}`,
|
|
149
|
+
this.push(`${prefix}${formatSkillBlock(name)}`, {
|
|
150
|
+
blockBoundary: prefix.length > 0 || newBubble,
|
|
151
|
+
newBubble,
|
|
152
|
+
trace: "skill",
|
|
153
|
+
});
|
|
133
154
|
}
|
|
134
155
|
|
|
135
156
|
pushToolResult(result: unknown, isError: boolean, toolName?: string, args?: unknown): void {
|
|
136
157
|
if (this.streamDead || this.disposed) return;
|
|
137
158
|
const prefix = this.beginBlock("tool");
|
|
138
159
|
const language = toolName ? resultLanguage(toolName, args, isError) : "text";
|
|
139
|
-
this.push(`${prefix}${formatToolResult(result, isError, language)}`,
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
schedule(op: () => void): void {
|
|
143
|
-
void this.enqueue(async () => {
|
|
144
|
-
if (this.streamDead || this.disposed) return;
|
|
145
|
-
op();
|
|
160
|
+
this.push(`${prefix}${formatToolResult(result, isError, language)}`, {
|
|
161
|
+
blockBoundary: prefix.length > 0,
|
|
162
|
+
endTool: true,
|
|
146
163
|
});
|
|
147
164
|
}
|
|
148
165
|
|
|
149
|
-
|
|
150
|
-
if (
|
|
166
|
+
restartSnapshot(blocks: RepushBlock[], target?: { openid: string; msgId: string }): void {
|
|
167
|
+
if (this.disposed || blocks.length === 0) return;
|
|
151
168
|
void this.enqueue(async () => {
|
|
152
169
|
if (this.disposed) return;
|
|
153
170
|
if (this.fenceOpen) await this.closeOpenFence();
|
|
@@ -157,19 +174,21 @@ export class BubbleWriter {
|
|
|
157
174
|
this.deps.openid = target.openid;
|
|
158
175
|
this.deps.msgId = target.msgId;
|
|
159
176
|
}
|
|
160
|
-
this.
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
177
|
+
this.resetForRestart();
|
|
178
|
+
for (const block of blocks) {
|
|
179
|
+
if (block.kind === "thinking") {
|
|
180
|
+
this.pushThinking(block.text);
|
|
181
|
+
} else if (block.kind === "text") {
|
|
182
|
+
this.pushText(block.text);
|
|
183
|
+
} else if (block.tool.skillName) {
|
|
184
|
+
this.pushSkill(block.tool.skillName);
|
|
185
|
+
} else {
|
|
186
|
+
this.pushTool(block.tool.name, block.tool.args);
|
|
187
|
+
if (block.tool.result) {
|
|
188
|
+
this.pushToolResult(block.tool.result.result, block.tool.result.isError, block.tool.name, block.tool.args);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
173
192
|
});
|
|
174
193
|
}
|
|
175
194
|
|
|
@@ -177,6 +196,23 @@ export class BubbleWriter {
|
|
|
177
196
|
await this.drainQueue();
|
|
178
197
|
}
|
|
179
198
|
|
|
199
|
+
private resetForRestart(): void {
|
|
200
|
+
this.cur?.dispose();
|
|
201
|
+
this.cur = null;
|
|
202
|
+
this.streamDead = false;
|
|
203
|
+
this.pendingResume = undefined;
|
|
204
|
+
this.resumeAttempts = 0;
|
|
205
|
+
this.lastKind = "none";
|
|
206
|
+
this.blockClosed = false;
|
|
207
|
+
this.toolOpen = false;
|
|
208
|
+
this.fenceOpen = false;
|
|
209
|
+
this.fenceLen = 0;
|
|
210
|
+
this.fenceQuoted = false;
|
|
211
|
+
this.lastChar = "";
|
|
212
|
+
this.thinkingAtLineStart = true;
|
|
213
|
+
this.bubbleChars = 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
180
216
|
async flush(): Promise<boolean> {
|
|
181
217
|
await this.drainQueue();
|
|
182
218
|
if (this.streamDead || this.disposed) return false;
|
|
@@ -263,12 +299,15 @@ export class BubbleWriter {
|
|
|
263
299
|
|
|
264
300
|
private push(
|
|
265
301
|
text: string,
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
302
|
+
options: {
|
|
303
|
+
blockBoundary?: boolean;
|
|
304
|
+
endTool?: boolean;
|
|
305
|
+
newBubble?: boolean;
|
|
306
|
+
thinking?: boolean;
|
|
307
|
+
trace?: string;
|
|
308
|
+
} = {},
|
|
271
309
|
): void {
|
|
310
|
+
const { blockBoundary = false, endTool = false, newBubble = false, thinking = false, trace } = options;
|
|
272
311
|
if (!text || this.streamDead || this.disposed) return;
|
|
273
312
|
const queuedAt = trace ? Date.now() : 0;
|
|
274
313
|
void this.enqueue(async () => {
|
|
@@ -471,9 +510,11 @@ export class BubbleWriter {
|
|
|
471
510
|
const checkpoint = stream?.checkpoint() ?? null;
|
|
472
511
|
stream?.dispose();
|
|
473
512
|
this.cur = null;
|
|
513
|
+
const resetSeq = isDuplicateMsgSeqError(err);
|
|
474
514
|
log.warn("stream", "stream error", {
|
|
475
515
|
msgId: this.logMsgId(),
|
|
476
516
|
elapsedMs: Date.now() - this.deps.startedAt,
|
|
517
|
+
resetSeq,
|
|
477
518
|
checkpoint: checkpoint
|
|
478
519
|
? {
|
|
479
520
|
index: checkpoint.index,
|
|
@@ -487,7 +528,12 @@ export class BubbleWriter {
|
|
|
487
528
|
});
|
|
488
529
|
if (checkpoint && this.resumeAttempts < MAX_RESUME_ATTEMPTS) {
|
|
489
530
|
this.resumeAttempts += 1;
|
|
490
|
-
this.pendingResume =
|
|
531
|
+
this.pendingResume = resetSeq
|
|
532
|
+
? {
|
|
533
|
+
index: 0,
|
|
534
|
+
...(checkpoint.buffer ? { buffer: checkpoint.buffer } : {}),
|
|
535
|
+
}
|
|
536
|
+
: checkpoint;
|
|
491
537
|
return;
|
|
492
538
|
}
|
|
493
539
|
this.killStream(err.message);
|
|
@@ -3,32 +3,19 @@ import type { ReplyType } from "../infra/reply-type";
|
|
|
3
3
|
export interface ReplyPolicy {
|
|
4
4
|
relayThinking: boolean;
|
|
5
5
|
relayTools: boolean;
|
|
6
|
-
relayTextLive: boolean;
|
|
7
6
|
deferTextWhileToolsPending: boolean;
|
|
8
|
-
deliverLastAtEnd: boolean;
|
|
9
7
|
}
|
|
10
8
|
|
|
11
9
|
const POLICIES: Record<ReplyType, ReplyPolicy> = {
|
|
12
10
|
full: {
|
|
13
11
|
relayThinking: true,
|
|
14
12
|
relayTools: true,
|
|
15
|
-
relayTextLive: true,
|
|
16
13
|
deferTextWhileToolsPending: true,
|
|
17
|
-
deliverLastAtEnd: false,
|
|
18
14
|
},
|
|
19
15
|
simple: {
|
|
20
16
|
relayThinking: false,
|
|
21
17
|
relayTools: false,
|
|
22
|
-
relayTextLive: true,
|
|
23
18
|
deferTextWhileToolsPending: false,
|
|
24
|
-
deliverLastAtEnd: false,
|
|
25
|
-
},
|
|
26
|
-
last: {
|
|
27
|
-
relayThinking: false,
|
|
28
|
-
relayTools: false,
|
|
29
|
-
relayTextLive: false,
|
|
30
|
-
deferTextWhileToolsPending: false,
|
|
31
|
-
deliverLastAtEnd: true,
|
|
32
19
|
},
|
|
33
20
|
};
|
|
34
21
|
|