@chen_hu_hu/pi-qq 1.0.2 → 1.1.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.
- package/README.md +16 -7
- package/doc/features-v1.md +1 -1
- package/doc/features-v2.md +97 -0
- package/doc/plan-v1.md +1 -1
- package/doc/plan-v2.md +245 -0
- package/index.ts +54 -5
- package/package.json +1 -1
- package/src/append-stream.ts +1 -1
- package/src/bot.ts +43 -5
- package/src/config.ts +7 -3
- package/src/download.ts +8 -8
- package/src/files.ts +4 -4
- package/src/images.ts +19 -7
- package/src/inbound.ts +96 -19
- package/src/logger.ts +180 -0
- package/src/outbound.ts +20 -3
- package/src/run-feedback.ts +24 -36
- package/src/voice.ts +25 -0
package/src/outbound.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { QQBot } from "@tencent-connect/qqbot-nodejs";
|
|
2
2
|
import { ReplyBudget } from "./reply-budget";
|
|
3
|
+
import { log } from "./logger";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* pi → QQ text relay (F4 non-streaming path): lightweight Markdown-to-plain
|
|
@@ -115,21 +116,37 @@ export async function sendReply(bot: QQBot, text: string, ch: ReplyChannel): Pro
|
|
|
115
116
|
const replyTarget =
|
|
116
117
|
mode === "passive" && msgId ? { scope: "c2c" as const, targetId, msgId } : { scope: "c2c" as const, targetId };
|
|
117
118
|
try {
|
|
119
|
+
log.debug("outbound", "sending text chunk", {
|
|
120
|
+
mode,
|
|
121
|
+
targetId,
|
|
122
|
+
msgId: msgId ?? null,
|
|
123
|
+
length: chunk.length,
|
|
124
|
+
text: chunk,
|
|
125
|
+
});
|
|
118
126
|
await bot.sendText(replyTarget, chunk);
|
|
119
127
|
if (mode === "passive" && msgId) ch.budget.commitPassive(msgId);
|
|
128
|
+
log.info("outbound", "text chunk sent", { mode, targetId, length: chunk.length });
|
|
120
129
|
} catch (err) {
|
|
130
|
+
log.warn("outbound", "text chunk failed", {
|
|
131
|
+
mode,
|
|
132
|
+
targetId,
|
|
133
|
+
length: chunk.length,
|
|
134
|
+
err,
|
|
135
|
+
});
|
|
121
136
|
if (mode === "passive") {
|
|
122
137
|
// Passive failure (e.g. stale msgId) → retry once proactively.
|
|
123
138
|
try {
|
|
139
|
+
log.warn("outbound", "passive send failed; retrying proactively", { targetId: ch.boundOpenid });
|
|
124
140
|
await bot.sendText({ scope: "c2c", targetId: ch.boundOpenid }, chunk);
|
|
125
|
-
ch.onWarn?.("
|
|
141
|
+
ch.onWarn?.("Passive reply failed; falling back to proactive.");
|
|
126
142
|
continue;
|
|
127
143
|
} catch (retryErr) {
|
|
128
|
-
|
|
144
|
+
log.error("outbound", "proactive retry failed", { targetId: ch.boundOpenid, err: retryErr });
|
|
145
|
+
ch.onWarn?.(`Proactive send failed: ${(retryErr as Error).message}`);
|
|
129
146
|
continue;
|
|
130
147
|
}
|
|
131
148
|
}
|
|
132
|
-
ch.onWarn?.(
|
|
149
|
+
ch.onWarn?.(`Send failed: ${(err as Error).message}`);
|
|
133
150
|
}
|
|
134
151
|
}
|
|
135
152
|
}
|
package/src/run-feedback.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { QQBot } from "@tencent-connect/qqbot-nodejs";
|
|
|
2
2
|
import { AppendStream } from "./append-stream";
|
|
3
3
|
import { sendReply, type ReplyChannel } from "./outbound";
|
|
4
4
|
import type { ReplyBudget } from "./reply-budget";
|
|
5
|
+
import { log } from "./logger";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* F7 long-task feedback: run-level state machine (summary frames + text bubbles).
|
|
@@ -15,7 +16,7 @@ import type { ReplyBudget } from "./reply-budget";
|
|
|
15
16
|
* The final reply is its own text bubble. Frames are sent every `streamFlushMs`
|
|
16
17
|
* (default 10000ms):
|
|
17
18
|
*
|
|
18
|
-
* Activity: [💭] Thinking... / [🔧] [2] bash (event order
|
|
19
|
+
* Activity: [💭] Thinking... / [🔧] [2] bash (event order)
|
|
19
20
|
* Text: message text (streamed increments, rendered as Markdown)
|
|
20
21
|
*
|
|
21
22
|
* Any failed frame discards the bubble; unsent text falls back to the message path.
|
|
@@ -35,8 +36,6 @@ export interface RunFeedbackDeps {
|
|
|
35
36
|
/** Stream summary frame interval in ms (minimum 300). */
|
|
36
37
|
getStreamFlushMs: () => number;
|
|
37
38
|
getStreamRelay: () => boolean;
|
|
38
|
-
/** Debug mode: SDK logs plus timestamps on stream frames. */
|
|
39
|
-
getDebug: () => boolean;
|
|
40
39
|
makeChannel: () => ReplyChannel;
|
|
41
40
|
onWarn: (msg: string) => void;
|
|
42
41
|
}
|
|
@@ -82,14 +81,7 @@ interface RunState {
|
|
|
82
81
|
messageHeartbeats: number;
|
|
83
82
|
}
|
|
84
83
|
|
|
85
|
-
/**
|
|
86
|
-
function nowStamp(): string {
|
|
87
|
-
const d = new Date();
|
|
88
|
-
const p = (n: number) => String(n).padStart(2, "0");
|
|
89
|
-
return `${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Tool count line: `[🔧] [n] name`; debug mode prefixes [HH:MM:SS]. */
|
|
84
|
+
/** Tool count line: `[🔧] [n] name`. */
|
|
93
85
|
function formatToolCount(name: string, count: number): string {
|
|
94
86
|
return `[🔧] [${count}] ${name}`;
|
|
95
87
|
}
|
|
@@ -97,12 +89,12 @@ function formatToolCount(name: string, count: number): string {
|
|
|
97
89
|
/** Heartbeat text: tool totals and elapsed time only (features-v1.md decision #3). */
|
|
98
90
|
export function formatHeartbeat(elapsedMs: number, tools: Map<string, ToolStat2>): string {
|
|
99
91
|
const minutes = Math.floor(elapsedMs / 60_000);
|
|
100
|
-
const elapsedText = minutes < 1 ? "
|
|
101
|
-
if (tools.size === 0) return `⏱
|
|
92
|
+
const elapsedText = minutes < 1 ? "less than 1 minute" : `${minutes} minute${minutes === 1 ? "" : "s"}`;
|
|
93
|
+
if (tools.size === 0) return `⏱ Running for ${elapsedText} (no tool calls yet)`;
|
|
102
94
|
const parts = [...tools.entries()].map(([name, s]) => formatToolCount(name, s.count));
|
|
103
95
|
const errors = [...tools.values()].reduce((n, s) => n + s.errors, 0);
|
|
104
|
-
const suffix = errors > 0 ? ` · ${errors}
|
|
105
|
-
return `⏱
|
|
96
|
+
const suffix = errors > 0 ? ` · ${errors} failed` : "";
|
|
97
|
+
return `⏱ Running for ${elapsedText} · ${parts.join(" · ")}${suffix}`;
|
|
106
98
|
}
|
|
107
99
|
|
|
108
100
|
const STREAM_LIVENESS_MS = 60_000; // Add a liveness row after this much silence
|
|
@@ -119,11 +111,6 @@ export class RunFeedback {
|
|
|
119
111
|
this.deps = deps;
|
|
120
112
|
}
|
|
121
113
|
|
|
122
|
-
private debug(message: string): void {
|
|
123
|
-
if (!this.deps.getDebug()) return;
|
|
124
|
-
console.error(`[pi-qq debug ${nowStamp()}] ${message}`);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
114
|
/** Called by inbound before injecting: mark the next run as QQ-triggered. */
|
|
128
115
|
markQQTrigger(openid: string, msgId: string): void {
|
|
129
116
|
this.pending = { openid, msgId, at: Date.now() };
|
|
@@ -153,7 +140,7 @@ export class RunFeedback {
|
|
|
153
140
|
messageHeartbeats: 0,
|
|
154
141
|
};
|
|
155
142
|
this.run = run;
|
|
156
|
-
|
|
143
|
+
log.debug("run", "started", { msgId: run.msgId?.slice(0, 8) ?? "?" });
|
|
157
144
|
|
|
158
145
|
// Typing keepalive: start immediately, renew every 50s (zero quota).
|
|
159
146
|
const keepTyping = () => {
|
|
@@ -260,7 +247,10 @@ export class RunFeedback {
|
|
|
260
247
|
this.clearTimers();
|
|
261
248
|
this.run = null;
|
|
262
249
|
if (run) {
|
|
263
|
-
|
|
250
|
+
log.debug("run", "ended", {
|
|
251
|
+
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
252
|
+
elapsedMs: Date.now() - run.startedAt,
|
|
253
|
+
});
|
|
264
254
|
// Do not resend on DONE failure; content was already shown.
|
|
265
255
|
try {
|
|
266
256
|
await this.sealBubble(run);
|
|
@@ -334,21 +324,18 @@ export class RunFeedback {
|
|
|
334
324
|
);
|
|
335
325
|
}
|
|
336
326
|
|
|
337
|
-
/** Append activity rows;
|
|
327
|
+
/** Append activity rows; rows continue across frames. */
|
|
338
328
|
private async appendLineGroups(r: RunState, lines?: string[]): Promise<boolean> {
|
|
339
329
|
const group = lines ?? this.collectWindowLines(r);
|
|
340
330
|
if (group.length === 0) return true;
|
|
341
|
-
|
|
342
|
-
const frame = stamp + group.join(`\n${stamp}`) + "\n";
|
|
343
|
-
return this.pushAppend(r, frame, "activity");
|
|
331
|
+
return this.pushAppend(r, `${group.join("\n")}\n`, "activity");
|
|
344
332
|
}
|
|
345
333
|
|
|
346
|
-
/** Append text: text messages own bubbles
|
|
334
|
+
/** Append text: text messages own bubbles. */
|
|
347
335
|
private async appendText(r: RunState, text: string): Promise<boolean> {
|
|
348
336
|
// Previous text message finalized: seal it and start a new bubble.
|
|
349
337
|
if (r.startNewBubble) await this.closeBubble(r);
|
|
350
|
-
|
|
351
|
-
return this.pushAppend(r, stamp + text, "text");
|
|
338
|
+
return this.pushAppend(r, text, "text");
|
|
352
339
|
}
|
|
353
340
|
|
|
354
341
|
/** Heartbeat fallback when streaming is unavailable (max N messages per run). */
|
|
@@ -363,8 +350,7 @@ export class RunFeedback {
|
|
|
363
350
|
const bot = this.deps.bot();
|
|
364
351
|
if (!bot) return;
|
|
365
352
|
run.messageHeartbeats += 1;
|
|
366
|
-
|
|
367
|
-
await sendReply(bot, `${stamp}${formatHeartbeat(Date.now() - run.startedAt, run.tools)}`, this.deps.makeChannel());
|
|
353
|
+
await sendReply(bot, formatHeartbeat(Date.now() - run.startedAt, run.tools), this.deps.makeChannel());
|
|
368
354
|
}
|
|
369
355
|
|
|
370
356
|
/** Append a frame (lazy bubble; close on kind change); discard on failure. */
|
|
@@ -381,7 +367,7 @@ export class RunFeedback {
|
|
|
381
367
|
run.cur = {
|
|
382
368
|
stream: new AppendStream(bot.messageApi, bot.getCredentials(), run.openid, run.msgId, {
|
|
383
369
|
onError: (err) => this.handleStreamError(run, err),
|
|
384
|
-
onDebug: (message) =>
|
|
370
|
+
onDebug: (message) => log.debug("stream", message, { msgId: run.msgId?.slice(0, 8) ?? "?" }),
|
|
385
371
|
}),
|
|
386
372
|
kind,
|
|
387
373
|
};
|
|
@@ -400,10 +386,12 @@ export class RunFeedback {
|
|
|
400
386
|
private handleStreamError(run: RunState, err: Error): void {
|
|
401
387
|
const had = run.cur !== null;
|
|
402
388
|
run.cur = null;
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
389
|
+
log.warn("stream", "stream error", {
|
|
390
|
+
msgId: run.msgId?.slice(0, 8) ?? "?",
|
|
391
|
+
elapsedMs: Date.now() - run.startedAt,
|
|
392
|
+
err,
|
|
393
|
+
});
|
|
394
|
+
if (had) this.deps.onWarn(`Stream unavailable; falling back to text (${err.message}).`);
|
|
407
395
|
const rest = run.finalizedText.slice(run.resultSentLen);
|
|
408
396
|
if (rest) {
|
|
409
397
|
const bound = this.deps.boundOpenid();
|
package/src/voice.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** QQ voice attachments: identify voice messages and extract platform ASR text. */
|
|
2
|
+
import type { InboundAttachmentLike } from "./download";
|
|
3
|
+
|
|
4
|
+
export interface VoiceAttachmentLike extends InboundAttachmentLike {
|
|
5
|
+
asr_refer_text?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** A QQ voice message has content_type "voice"; other media/file types are excluded. */
|
|
9
|
+
export function isVoiceAttachment(attachment: { content_type?: string }): boolean {
|
|
10
|
+
return (attachment.content_type ?? "").toLowerCase() === "voice";
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Collect non-empty ASR text from voice attachments in order.
|
|
15
|
+
* Returns undefined when no attachment has a usable transcript.
|
|
16
|
+
*/
|
|
17
|
+
export function resolveVoiceTranscript(attachments: readonly VoiceAttachmentLike[]): string | undefined {
|
|
18
|
+
const transcripts: string[] = [];
|
|
19
|
+
for (const attachment of attachments) {
|
|
20
|
+
if (!isVoiceAttachment(attachment)) continue;
|
|
21
|
+
const text = attachment.asr_refer_text?.trim();
|
|
22
|
+
if (text) transcripts.push(text);
|
|
23
|
+
}
|
|
24
|
+
return transcripts.length > 0 ? transcripts.join("\n") : undefined;
|
|
25
|
+
}
|