@chen_hu_hu/pi-qq 1.3.0 → 1.3.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 +61 -0
- package/README-zh.md +67 -47
- package/README.md +70 -50
- package/doc/archive/README.md +18 -0
- package/doc/{delivery-v1-zh.md → archive/delivery-v1-zh.md} +9 -8
- package/doc/{delivery-v1.md → archive/delivery-v1.md} +4 -3
- package/doc/{delivery-v2-zh.md → archive/delivery-v2-zh.md} +3 -2
- package/doc/{delivery-v2.md → archive/delivery-v2.md} +3 -2
- package/doc/{delivery-v3-zh.md → archive/delivery-v3-zh.md} +5 -2
- package/doc/{delivery-v3.md → archive/delivery-v3.md} +6 -3
- package/index.ts +70 -156
- package/package.json +5 -1
- package/src/attachments/attachment-failure.ts +12 -0
- package/src/attachments/file-attachments.ts +16 -19
- package/src/infra/file-logger.ts +21 -9
- package/src/infra/settings.ts +62 -8
- package/src/qq/inbound-attachments.ts +101 -0
- package/src/qq/inbound-command.ts +74 -0
- package/src/qq/qq-bot.ts +2 -2
- package/src/qq/qq-diagnostics.ts +129 -0
- package/src/qq/qq-message-handler.ts +42 -156
- package/src/qq/qr-login.ts +4 -1
- package/src/qq/reply-sender.ts +5 -4
- package/src/relay/bubble-writer.ts +338 -0
- package/src/relay/run-stream-relay.ts +50 -348
- package/src/relay/stream-block-format.ts +7 -4
- package/src/attachments/image-attachments.ts +0 -142
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { QQBotInboundMessage } from "@tencent-connect/qqbot-nodejs";
|
|
2
|
+
import type { AttachmentFailure } from "../attachments/attachment-failure";
|
|
3
|
+
import { saveInboundFiles, type SavedFile } from "../attachments/file-attachments";
|
|
4
|
+
import { log } from "../infra/file-logger";
|
|
5
|
+
import { isVoiceAttachment, resolveVoiceTranscript } from "../attachments/voice-transcript";
|
|
6
|
+
|
|
7
|
+
export interface ResolveInboundOptions {
|
|
8
|
+
contentText: string;
|
|
9
|
+
maxSize: number;
|
|
10
|
+
uploadDir: string;
|
|
11
|
+
saveFiles: typeof saveInboundFiles;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type ResolvedInbound =
|
|
15
|
+
| { status: "rejected"; reply: string }
|
|
16
|
+
| {
|
|
17
|
+
status: "resolved";
|
|
18
|
+
text: string;
|
|
19
|
+
files: SavedFile[];
|
|
20
|
+
failures: AttachmentFailure[];
|
|
21
|
+
voiceCount: number;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Turn one inbound message into injectable content: prefer ASR for voice,
|
|
26
|
+
* persist every non-voice attachment, and report per-file failures.
|
|
27
|
+
*/
|
|
28
|
+
export async function resolveInboundContent(
|
|
29
|
+
msg: QQBotInboundMessage,
|
|
30
|
+
options: ResolveInboundOptions,
|
|
31
|
+
): Promise<ResolvedInbound> {
|
|
32
|
+
const attachments = msg.attachments ?? [];
|
|
33
|
+
let text = options.contentText;
|
|
34
|
+
|
|
35
|
+
const voiceAttachments = attachments.filter(isVoiceAttachment);
|
|
36
|
+
if (voiceAttachments.length > 0) {
|
|
37
|
+
const transcript = resolveVoiceTranscript(voiceAttachments);
|
|
38
|
+
log.info("inbound", "voice message", {
|
|
39
|
+
messageId: msg.messageId,
|
|
40
|
+
hasAsr: Boolean(transcript),
|
|
41
|
+
asrLength: transcript?.length ?? 0,
|
|
42
|
+
});
|
|
43
|
+
if (!transcript) {
|
|
44
|
+
if (!options.contentText) {
|
|
45
|
+
log.warn("inbound", "voice ASR returned no text", { messageId: msg.messageId });
|
|
46
|
+
return {
|
|
47
|
+
status: "rejected",
|
|
48
|
+
reply: "Could not transcribe this voice message; please resend or type it.",
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
log.warn("inbound", "voice ASR returned no text; using text content", { messageId: msg.messageId });
|
|
52
|
+
} else {
|
|
53
|
+
log.debug("inbound", "voice transcript", { messageId: msg.messageId, transcript });
|
|
54
|
+
text = options.contentText ? `${options.contentText}\n${transcript}` : transcript;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Voice attachments are handled above; all other attachments are stored as files.
|
|
59
|
+
const attachmentsToSave = attachments.filter((attachment) => !isVoiceAttachment(attachment));
|
|
60
|
+
const failures: AttachmentFailure[] = [];
|
|
61
|
+
let files: SavedFile[] = [];
|
|
62
|
+
|
|
63
|
+
if (attachmentsToSave.length > 0) {
|
|
64
|
+
log.debug("inbound", "saving attachments", {
|
|
65
|
+
messageId: msg.messageId,
|
|
66
|
+
count: attachmentsToSave.length,
|
|
67
|
+
uploadDir: options.uploadDir,
|
|
68
|
+
});
|
|
69
|
+
const result = await options.saveFiles(attachmentsToSave, {
|
|
70
|
+
maxSize: options.maxSize,
|
|
71
|
+
uploadDir: options.uploadDir,
|
|
72
|
+
});
|
|
73
|
+
files = result.files;
|
|
74
|
+
failures.push(...result.failures);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return { status: "resolved", text, files, failures, voiceCount: voiceAttachments.length };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Attachments that arrived without an instruction, consumed by the next text or voice message. */
|
|
81
|
+
export class PendingAttachments {
|
|
82
|
+
private files: SavedFile[] = [];
|
|
83
|
+
|
|
84
|
+
get count(): number {
|
|
85
|
+
return this.files.length;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
add(files: SavedFile[]): void {
|
|
89
|
+
if (files.length > 0) this.files.push(...files);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
take(): SavedFile[] {
|
|
93
|
+
const files = this.files;
|
|
94
|
+
this.files = [];
|
|
95
|
+
return files;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
clear(): void {
|
|
99
|
+
this.files = [];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
/** The slice of ExtensionContext used by QQ inbound handling. */
|
|
4
|
+
export interface InboundContext {
|
|
5
|
+
readonly cwd: string;
|
|
6
|
+
isIdle(): boolean;
|
|
7
|
+
abort(): void;
|
|
8
|
+
compact(): void;
|
|
9
|
+
sessionManager: { getSessionId(): string };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type InboundPi = Pick<ExtensionAPI, "sendUserMessage">;
|
|
13
|
+
|
|
14
|
+
export interface InboundCommandDeps {
|
|
15
|
+
pi: InboundPi;
|
|
16
|
+
ctx: InboundContext;
|
|
17
|
+
getStatusLines: () => string[];
|
|
18
|
+
onStop?: () => void;
|
|
19
|
+
waitForEnd?: () => Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function handleInboundCommand(
|
|
23
|
+
text: string,
|
|
24
|
+
reply: (t: string) => Promise<void>,
|
|
25
|
+
deps: InboundCommandDeps,
|
|
26
|
+
): Promise<void> {
|
|
27
|
+
const cmd = text.split(/\s+/)[0];
|
|
28
|
+
switch (cmd) {
|
|
29
|
+
case "/help":
|
|
30
|
+
await reply(
|
|
31
|
+
[
|
|
32
|
+
"pi-qq commands:",
|
|
33
|
+
"• /stop — abort the current task",
|
|
34
|
+
"• /compact — compact the context",
|
|
35
|
+
"• /status — show status",
|
|
36
|
+
"• /new — reset the session",
|
|
37
|
+
"• /help — this help",
|
|
38
|
+
"Other text is sent to pi.",
|
|
39
|
+
].join("\n"),
|
|
40
|
+
);
|
|
41
|
+
break;
|
|
42
|
+
case "/stop":
|
|
43
|
+
deps.onStop?.();
|
|
44
|
+
if (deps.ctx.isIdle()) {
|
|
45
|
+
await reply("No task is running.");
|
|
46
|
+
} else {
|
|
47
|
+
deps.ctx.abort();
|
|
48
|
+
await deps.waitForEnd?.();
|
|
49
|
+
await reply("Abort requested.");
|
|
50
|
+
}
|
|
51
|
+
break;
|
|
52
|
+
case "/compact":
|
|
53
|
+
if (deps.ctx.isIdle()) {
|
|
54
|
+
deps.ctx.compact();
|
|
55
|
+
await reply("Context compaction triggered.");
|
|
56
|
+
} else {
|
|
57
|
+
await reply("Task is running; use /stop before /compact.");
|
|
58
|
+
}
|
|
59
|
+
break;
|
|
60
|
+
case "/status":
|
|
61
|
+
await reply(deps.getStatusLines().join("\n"));
|
|
62
|
+
break;
|
|
63
|
+
case "/new":
|
|
64
|
+
if (!deps.ctx.isIdle()) {
|
|
65
|
+
await reply("Task is running; use /stop before resetting.");
|
|
66
|
+
} else {
|
|
67
|
+
await reply("Resetting the session…");
|
|
68
|
+
deps.pi.sendUserMessage("/qq-new", { expandPromptTemplates: true });
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
default:
|
|
72
|
+
await reply(`Unknown command ${cmd}; use /help.`);
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/qq/qq-bot.ts
CHANGED
|
@@ -14,7 +14,7 @@ export type BotState = "stopped" | "starting" | "connected" | "reconnecting" | "
|
|
|
14
14
|
|
|
15
15
|
export interface StartBotOptions {
|
|
16
16
|
credentials: Credentials;
|
|
17
|
-
allowedOpenid
|
|
17
|
+
allowedOpenid: string;
|
|
18
18
|
onMessage?: (bot: QQBot, msg: QQBotInboundMessage) => void | Promise<void>;
|
|
19
19
|
onStateChange?: (state: BotState, detail?: string) => void;
|
|
20
20
|
}
|
|
@@ -38,7 +38,7 @@ export function startBot(opts: StartBotOptions): RunningBot {
|
|
|
38
38
|
|
|
39
39
|
bot.use(messageFilter({ skipSelfEcho: true }));
|
|
40
40
|
bot.use(contentSanitizer({ stripBotMention: true }));
|
|
41
|
-
bot.use(accessPolicy(
|
|
41
|
+
bot.use(accessPolicy({ c2c: { mode: "allowlist", allow: [opts.allowedOpenid] } }));
|
|
42
42
|
bot.use(concurrencyGuard());
|
|
43
43
|
|
|
44
44
|
const report = (state: BotState, detail?: string) => {
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { ReplyBudget } from "../quota/reply-quota";
|
|
3
|
+
import { formatBudgetStatus } from "../quota/reply-quota-report";
|
|
4
|
+
import { AppendStream } from "../relay/stream-session";
|
|
5
|
+
import type { RunningBot } from "./qq-bot";
|
|
6
|
+
|
|
7
|
+
export interface QQDiagnosticsDeps {
|
|
8
|
+
getRunning: () => RunningBot | undefined;
|
|
9
|
+
budget: ReplyBudget;
|
|
10
|
+
getBoundOpenid: () => string | undefined;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Terminal-only protocol probes. Kept out of the entrypoint so index.ts stays
|
|
15
|
+
* a composition root and does not construct stream frames directly.
|
|
16
|
+
*/
|
|
17
|
+
export function registerQQDiagnostics(pi: ExtensionAPI, deps: QQDiagnosticsDeps): void {
|
|
18
|
+
const diagTarget = () => {
|
|
19
|
+
const last = deps.budget.lastTarget;
|
|
20
|
+
if (last) return { scope: "c2c" as const, targetId: last.openid, msgId: last.msgId };
|
|
21
|
+
const bound = deps.getBoundOpenid();
|
|
22
|
+
return bound ? { scope: "c2c" as const, targetId: bound } : undefined;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
pi.registerCommand("qq-test-budget", {
|
|
26
|
+
description: "pi-qq: show passive reply budget, stream bubbles, and last inbound target",
|
|
27
|
+
handler: async (_args, ctx) => {
|
|
28
|
+
ctx.ui.notify(formatBudgetStatus(deps.budget.snapshot(), deps.budget.lastTarget).join("\n"), "info");
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
pi.registerCommand("qq-test-md", {
|
|
33
|
+
description: "pi-qq: send a Markdown message to verify QQ C2C Markdown rendering",
|
|
34
|
+
handler: async (args, ctx) => {
|
|
35
|
+
const running = deps.getRunning();
|
|
36
|
+
if (!running) {
|
|
37
|
+
ctx.ui.notify("pi-qq is not connected.", "warning");
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const target = diagTarget();
|
|
41
|
+
if (!target) {
|
|
42
|
+
ctx.ui.notify("pi-qq: no send target. Send a QQ message first.", "warning");
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const content = args?.trim() || "**Markdown test**\n# Heading\n- Item one\n- Item two\n[Link](https://qq.com)";
|
|
46
|
+
try {
|
|
47
|
+
await running.bot.sendMarkdown(target, content);
|
|
48
|
+
ctx.ui.notify("Markdown test sent. Check whether QQ renders it as rich text.", "info");
|
|
49
|
+
} catch (err) {
|
|
50
|
+
ctx.ui.notify(`Markdown send failed: ${(err as Error).message}`, "error");
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
pi.registerCommand("qq-test-stream", {
|
|
56
|
+
description: "pi-qq: run a streamed timeline diagnostic (thinking/tools/text) or resume probe",
|
|
57
|
+
handler: async (args, ctx) => {
|
|
58
|
+
const running = deps.getRunning();
|
|
59
|
+
if (!running) {
|
|
60
|
+
ctx.ui.notify("pi-qq is not connected.", "warning");
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const last = deps.budget.lastTarget;
|
|
64
|
+
if (!last) {
|
|
65
|
+
ctx.ui.notify("pi-qq: send a QQ message first (streaming needs a msgId).", "warning");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
70
|
+
let stream: AppendStream | undefined;
|
|
71
|
+
try {
|
|
72
|
+
if (args?.trim() === "resume") {
|
|
73
|
+
const first = new AppendStream(running.bot.messageApi, running.bot.getCredentials(), last.openid, last.msgId);
|
|
74
|
+
stream = first;
|
|
75
|
+
await first.append("**Resume probe** frame 1\n");
|
|
76
|
+
await first.flush();
|
|
77
|
+
const checkpoint = first.checkpoint();
|
|
78
|
+
first.dispose();
|
|
79
|
+
stream = undefined;
|
|
80
|
+
if (!checkpoint?.streamMsgId) {
|
|
81
|
+
ctx.ui.notify("Resume probe: frame 1 sent but stream_msg_id is missing; cannot resume.", "error");
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
await sleep(800);
|
|
85
|
+
const second = new AppendStream(
|
|
86
|
+
running.bot.messageApi,
|
|
87
|
+
running.bot.getCredentials(),
|
|
88
|
+
last.openid,
|
|
89
|
+
last.msgId,
|
|
90
|
+
{ resume: checkpoint },
|
|
91
|
+
);
|
|
92
|
+
stream = second;
|
|
93
|
+
await second.append("frame 2 after resume\n");
|
|
94
|
+
await second.done();
|
|
95
|
+
second.dispose();
|
|
96
|
+
stream = undefined;
|
|
97
|
+
ctx.ui.notify(
|
|
98
|
+
`Resume probe: frame 2 sent with index ${checkpoint.index} and stream_msg_id ${checkpoint.streamMsgId}. Check whether both frames are in the same bubble.`,
|
|
99
|
+
"info",
|
|
100
|
+
);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
stream = new AppendStream(running.bot.messageApi, running.bot.getCredentials(), last.openid, last.msgId);
|
|
105
|
+
await stream.append(
|
|
106
|
+
"**💭 思考**\n\nprobe thinking\nsecond line\n\n**💻 bash**\n\n`echo probe`\n\n```text\nprobe\n```\n",
|
|
107
|
+
);
|
|
108
|
+
await sleep(1200);
|
|
109
|
+
await stream.append("\n**Intermediate message**: first-turn reply.\n");
|
|
110
|
+
await sleep(1200);
|
|
111
|
+
await stream.append("\n**📖 read**\n\n`src/index.ts`\n\n```text\nprobe file\n```\n");
|
|
112
|
+
await sleep(800);
|
|
113
|
+
await stream.append("\n**Final reply** (streamed append)");
|
|
114
|
+
let doneNote = "DONE succeeded";
|
|
115
|
+
try {
|
|
116
|
+
await stream.done();
|
|
117
|
+
} catch (doneErr) {
|
|
118
|
+
doneNote = `DONE rejected: ${(doneErr as Error).message}`;
|
|
119
|
+
}
|
|
120
|
+
stream.dispose();
|
|
121
|
+
stream = undefined;
|
|
122
|
+
ctx.ui.notify(`Stream timeline test: frames sent, ${doneNote}. Check the QQ activity timeline.`, "info");
|
|
123
|
+
} catch (err) {
|
|
124
|
+
stream?.dispose();
|
|
125
|
+
ctx.ui.notify(`Stream timeline test failed: ${(err as Error).message}`, "error");
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
}
|
|
@@ -1,42 +1,29 @@
|
|
|
1
|
-
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
1
|
import type { QQBot, QQBotInboundMessage } from "@tencent-connect/qqbot-nodejs";
|
|
3
2
|
import { sendReply, type ReplyChannel } from "./reply-sender";
|
|
4
3
|
import type { ReplyBudget } from "../quota/reply-quota";
|
|
4
|
+
import { formatAttachmentFailures } from "../attachments/attachment-failure";
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
} from "../attachments/image-attachments";
|
|
12
|
-
import { formatFilePrompt, saveInboundFiles, sessionUploadDir, type SavedFile } from "../attachments/file-attachments";
|
|
6
|
+
formatAttachmentPrompt,
|
|
7
|
+
saveInboundFiles,
|
|
8
|
+
sessionUploadDir,
|
|
9
|
+
type SavedFile,
|
|
10
|
+
} from "../attachments/file-attachments";
|
|
13
11
|
import { log } from "../infra/file-logger";
|
|
14
|
-
import {
|
|
12
|
+
import { handleInboundCommand, type InboundCommandDeps } from "./inbound-command";
|
|
13
|
+
import { PendingAttachments, resolveInboundContent } from "./inbound-attachments";
|
|
15
14
|
|
|
16
|
-
export interface InboundDeps {
|
|
17
|
-
pi: ExtensionAPI;
|
|
18
|
-
ctx: ExtensionContext;
|
|
15
|
+
export interface InboundDeps extends InboundCommandDeps {
|
|
19
16
|
budget: ReplyBudget;
|
|
20
17
|
getAllowedOpenid: () => string | undefined;
|
|
21
|
-
getBoundOpenid: () => string | undefined;
|
|
22
18
|
getDeliverAs: () => "steer" | "followUp";
|
|
23
|
-
getImageMaxSize: () => number;
|
|
24
19
|
getFileMaxSize: () => number;
|
|
25
20
|
saveFiles?: typeof saveInboundFiles;
|
|
26
|
-
getStatusLines: () => string[];
|
|
27
21
|
onWarn: (msg: string) => void;
|
|
28
22
|
markQQTrigger: (openid: string, msgId: string) => void;
|
|
29
|
-
waitForEnd?: () => Promise<void>;
|
|
30
|
-
onStop?: () => void;
|
|
31
23
|
}
|
|
32
24
|
|
|
33
25
|
export function createInboundHandler(deps: InboundDeps) {
|
|
34
|
-
|
|
35
|
-
const takePendingFiles = (): SavedFile[] => {
|
|
36
|
-
const files = pendingFiles;
|
|
37
|
-
pendingFiles = [];
|
|
38
|
-
return files;
|
|
39
|
-
};
|
|
26
|
+
const pending = new PendingAttachments();
|
|
40
27
|
|
|
41
28
|
return async (bot: QQBot, msg: QQBotInboundMessage): Promise<void> => {
|
|
42
29
|
log.debug("inbound", "message received", {
|
|
@@ -78,102 +65,51 @@ export function createInboundHandler(deps: InboundDeps) {
|
|
|
78
65
|
|
|
79
66
|
const channel: ReplyChannel = {
|
|
80
67
|
budget: deps.budget,
|
|
81
|
-
boundOpenid: deps.
|
|
68
|
+
boundOpenid: deps.getAllowedOpenid() ?? msg.senderId,
|
|
82
69
|
onWarn: deps.onWarn,
|
|
83
70
|
};
|
|
84
71
|
const reply = (text: string) => sendReply(bot, text, channel);
|
|
85
|
-
const saveFiles = deps.saveFiles ?? saveInboundFiles;
|
|
86
72
|
|
|
87
|
-
const
|
|
88
|
-
if (
|
|
89
|
-
const command =
|
|
73
|
+
const contentText = (msg.content ?? "").trim();
|
|
74
|
+
if (contentText.startsWith("/")) {
|
|
75
|
+
const command = contentText.split(/\s+/)[0];
|
|
90
76
|
log.info("inbound", "QQ command", { command });
|
|
91
|
-
if (command === "/new" && deps.ctx.isIdle() &&
|
|
92
|
-
log.info("inbound", "cleared pending
|
|
93
|
-
|
|
77
|
+
if (command === "/new" && deps.ctx.isIdle() && pending.count > 0) {
|
|
78
|
+
log.info("inbound", "cleared pending attachments for /new", { count: pending.count });
|
|
79
|
+
pending.clear();
|
|
94
80
|
}
|
|
95
|
-
await
|
|
81
|
+
await handleInboundCommand(contentText, reply, deps);
|
|
96
82
|
return;
|
|
97
83
|
}
|
|
98
84
|
|
|
99
85
|
const attachments = msg.attachments ?? [];
|
|
100
|
-
if (!
|
|
86
|
+
if (!contentText && attachments.length === 0) {
|
|
101
87
|
log.warn("inbound", "unsupported message: no text and no attachments", { messageId: msg.messageId });
|
|
102
88
|
await reply("Unsupported message type.");
|
|
103
89
|
return;
|
|
104
90
|
}
|
|
105
91
|
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (!transcript) {
|
|
115
|
-
log.warn("inbound", "voice ASR returned no text", { messageId: msg.messageId });
|
|
116
|
-
await reply("Could not transcribe this voice message; please resend or type it.");
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
log.debug("inbound", "voice transcript", { messageId: msg.messageId, transcript });
|
|
120
|
-
const pending = takePendingFiles();
|
|
121
|
-
const promptText = formatFilePrompt(pending, transcript);
|
|
122
|
-
deps.markQQTrigger(msg.senderId, msg.messageId);
|
|
123
|
-
deps.pi.sendUserMessage(promptText, { deliverAs: deps.getDeliverAs() });
|
|
124
|
-
log.info("inbound", "voice sent to pi", {
|
|
125
|
-
messageId: msg.messageId,
|
|
126
|
-
asrLength: transcript.length,
|
|
127
|
-
pendingFiles: pending.length,
|
|
128
|
-
});
|
|
92
|
+
const resolved = await resolveInboundContent(msg, {
|
|
93
|
+
contentText,
|
|
94
|
+
maxSize: deps.getFileMaxSize(),
|
|
95
|
+
uploadDir: sessionUploadDir(deps.ctx.cwd, deps.ctx.sessionManager.getSessionId()),
|
|
96
|
+
saveFiles: deps.saveFiles ?? saveInboundFiles,
|
|
97
|
+
});
|
|
98
|
+
if (resolved.status === "rejected") {
|
|
99
|
+
await reply(resolved.reply);
|
|
129
100
|
return;
|
|
130
101
|
}
|
|
131
|
-
|
|
132
|
-
const imageAttachments = attachments.filter(isImageAttachment);
|
|
133
|
-
const fileAttachments = attachments.filter((attachment) => !isImageAttachment(attachment));
|
|
134
|
-
const failures: Array<{ filename: string; reason: string }> = [];
|
|
135
|
-
|
|
136
|
-
let images: ImageContentLike[] = [];
|
|
137
|
-
if (imageAttachments.length > 0) {
|
|
138
|
-
const supportsImages = deps.ctx.model?.input.includes("image") ?? false;
|
|
139
|
-
if (!supportsImages) {
|
|
140
|
-
log.warn("inbound", "model does not support images; skipped", {
|
|
141
|
-
messageId: msg.messageId,
|
|
142
|
-
count: imageAttachments.length,
|
|
143
|
-
});
|
|
144
|
-
await reply(`Model does not support image input; skipped ${imageAttachments.length} image(s).`);
|
|
145
|
-
} else {
|
|
146
|
-
const result = await resolveInboundImages(imageAttachments, deps.getImageMaxSize());
|
|
147
|
-
images = result.images;
|
|
148
|
-
failures.push(...result.failures);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
let files: SavedFile[] = [];
|
|
153
|
-
if (fileAttachments.length > 0) {
|
|
154
|
-
const uploadDir = sessionUploadDir(deps.ctx.cwd, deps.ctx.sessionManager.getSessionId());
|
|
155
|
-
log.debug("inbound", "saving files", {
|
|
156
|
-
messageId: msg.messageId,
|
|
157
|
-
count: fileAttachments.length,
|
|
158
|
-
uploadDir,
|
|
159
|
-
});
|
|
160
|
-
const result = await saveFiles(fileAttachments, {
|
|
161
|
-
maxSize: deps.getFileMaxSize(),
|
|
162
|
-
uploadDir,
|
|
163
|
-
});
|
|
164
|
-
files = result.files;
|
|
165
|
-
failures.push(...result.failures);
|
|
166
|
-
}
|
|
102
|
+
const { text, files, failures, voiceCount } = resolved;
|
|
167
103
|
|
|
168
104
|
if (failures.length > 0) {
|
|
169
105
|
log.warn("inbound", "attachment failure summary", {
|
|
170
106
|
messageId: msg.messageId,
|
|
171
107
|
failures,
|
|
172
|
-
successCount:
|
|
108
|
+
successCount: files.length,
|
|
173
109
|
});
|
|
174
|
-
await reply(
|
|
110
|
+
await reply(formatAttachmentFailures(failures, files.length));
|
|
175
111
|
}
|
|
176
|
-
if (!text &&
|
|
112
|
+
if (!text && files.length === 0) {
|
|
177
113
|
log.warn("inbound", "no injectable content after attachment handling", {
|
|
178
114
|
messageId: msg.messageId,
|
|
179
115
|
failures: failures.length,
|
|
@@ -181,78 +117,28 @@ export function createInboundHandler(deps: InboundDeps) {
|
|
|
181
117
|
return;
|
|
182
118
|
}
|
|
183
119
|
|
|
184
|
-
if (!text &&
|
|
185
|
-
|
|
186
|
-
log.info("inbound", "
|
|
120
|
+
if (!text && files.length > 0) {
|
|
121
|
+
pending.add(files);
|
|
122
|
+
log.info("inbound", "attachments pending for instruction", {
|
|
187
123
|
messageId: msg.messageId,
|
|
188
124
|
count: files.length,
|
|
189
|
-
pendingCount:
|
|
125
|
+
pendingCount: pending.count,
|
|
190
126
|
});
|
|
191
127
|
return;
|
|
192
128
|
}
|
|
193
129
|
|
|
194
130
|
deps.markQQTrigger(msg.senderId, msg.messageId);
|
|
195
|
-
const
|
|
196
|
-
const promptFiles = [...
|
|
197
|
-
const promptText =
|
|
198
|
-
deps.pi.sendUserMessage(
|
|
131
|
+
const pendingFiles = text ? pending.take() : [];
|
|
132
|
+
const promptFiles: SavedFile[] = [...pendingFiles, ...files];
|
|
133
|
+
const promptText = formatAttachmentPrompt(promptFiles, text, deps.ctx.cwd);
|
|
134
|
+
deps.pi.sendUserMessage(promptText, { deliverAs: deps.getDeliverAs() });
|
|
199
135
|
log.info("inbound", "sent to pi", {
|
|
200
136
|
messageId: msg.messageId,
|
|
201
137
|
textLength: text.length,
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
138
|
+
voice: voiceCount > 0,
|
|
139
|
+
attachments: promptFiles.length,
|
|
140
|
+
pendingAttachments: pendingFiles.length,
|
|
205
141
|
deliverAs: deps.getDeliverAs(),
|
|
206
142
|
});
|
|
207
143
|
};
|
|
208
144
|
}
|
|
209
|
-
|
|
210
|
-
async function handleCommand(text: string, reply: (t: string) => Promise<void>, deps: InboundDeps): Promise<void> {
|
|
211
|
-
const cmd = text.split(/\s+/)[0];
|
|
212
|
-
switch (cmd) {
|
|
213
|
-
case "/help":
|
|
214
|
-
await reply(
|
|
215
|
-
[
|
|
216
|
-
"pi-qq commands:",
|
|
217
|
-
"• /stop — abort the current task",
|
|
218
|
-
"• /compact — compact the context",
|
|
219
|
-
"• /status — show status",
|
|
220
|
-
"• /new — reset the session",
|
|
221
|
-
"• /help — this help",
|
|
222
|
-
"Other text is sent to pi.",
|
|
223
|
-
].join("\n"),
|
|
224
|
-
);
|
|
225
|
-
break;
|
|
226
|
-
case "/stop":
|
|
227
|
-
if (deps.ctx.isIdle()) {
|
|
228
|
-
await reply("No task is running.");
|
|
229
|
-
} else {
|
|
230
|
-
deps.onStop?.();
|
|
231
|
-
deps.ctx.abort();
|
|
232
|
-
await deps.waitForEnd?.();
|
|
233
|
-
await reply("Abort requested.");
|
|
234
|
-
}
|
|
235
|
-
break;
|
|
236
|
-
case "/compact":
|
|
237
|
-
if (deps.ctx.isIdle()) {
|
|
238
|
-
deps.ctx.compact();
|
|
239
|
-
await reply("Context compaction triggered.");
|
|
240
|
-
} else {
|
|
241
|
-
await reply("Task is running; use /stop before /compact.");
|
|
242
|
-
}
|
|
243
|
-
break;
|
|
244
|
-
case "/status":
|
|
245
|
-
await reply(deps.getStatusLines().join("\n"));
|
|
246
|
-
break;
|
|
247
|
-
case "/new":
|
|
248
|
-
if (!deps.ctx.isIdle()) {
|
|
249
|
-
await reply("Task is running; use /stop before resetting.");
|
|
250
|
-
} else {
|
|
251
|
-
await reply("Resetting the session…");
|
|
252
|
-
deps.pi.sendUserMessage("/qq-new", { expandPromptTemplates: true });
|
|
253
|
-
}
|
|
254
|
-
break;
|
|
255
|
-
default:
|
|
256
|
-
await reply(`Unknown command ${cmd}; use /help.`);
|
|
257
|
-
}
|
|
258
|
-
}
|
package/src/qq/qr-login.ts
CHANGED
|
@@ -16,7 +16,7 @@ export async function runQrLogin(ctx: ExtensionContext): Promise<QrLoginResult |
|
|
|
16
16
|
let requestRender: (() => void) | undefined;
|
|
17
17
|
let closeOverlay: (() => void) | undefined;
|
|
18
18
|
|
|
19
|
-
return
|
|
19
|
+
return new Promise<QrLoginResult | undefined>((resolve) => {
|
|
20
20
|
let settled = false;
|
|
21
21
|
const finish = (result?: QrLoginResult) => {
|
|
22
22
|
if (settled) return;
|
|
@@ -86,6 +86,9 @@ export async function runQrLogin(ctx: ExtensionContext): Promise<QrLoginResult |
|
|
|
86
86
|
},
|
|
87
87
|
handleInput(data: string): void {
|
|
88
88
|
if (keybindings.matches(data, "tui.select.cancel") || data === "\x1b") {
|
|
89
|
+
// Mark the login settled before aborting: the connector reports an
|
|
90
|
+
// AbortError through onFailure, which must not look like a failure.
|
|
91
|
+
finish();
|
|
89
92
|
ac.abort();
|
|
90
93
|
done();
|
|
91
94
|
}
|
package/src/qq/reply-sender.ts
CHANGED
|
@@ -60,12 +60,13 @@ export function markdownToPlain(md: string): string {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
export function chunkText(text: string, limit = TEXT_CHUNK_LIMIT): string[] {
|
|
63
|
-
|
|
63
|
+
const max = Number.isFinite(limit) ? Math.max(1, Math.floor(limit)) : TEXT_CHUNK_LIMIT;
|
|
64
|
+
if (text.length <= max) return [text];
|
|
64
65
|
const chunks: string[] = [];
|
|
65
66
|
let rest = text;
|
|
66
|
-
while (rest.length >
|
|
67
|
-
let cut = rest.lastIndexOf("\n",
|
|
68
|
-
cut = cut <= 0 ?
|
|
67
|
+
while (rest.length > max) {
|
|
68
|
+
let cut = rest.lastIndexOf("\n", max);
|
|
69
|
+
cut = cut <= 0 ? max : cut + 1;
|
|
69
70
|
chunks.push(rest.slice(0, cut));
|
|
70
71
|
rest = rest.slice(cut);
|
|
71
72
|
}
|