@chen_hu_hu/pi-qq 1.3.0 → 1.3.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.
@@ -2,14 +2,13 @@ import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-a
2
2
  import type { QQBot, QQBotInboundMessage } from "@tencent-connect/qqbot-nodejs";
3
3
  import { sendReply, type ReplyChannel } from "./reply-sender";
4
4
  import type { ReplyBudget } from "../quota/reply-quota";
5
+ import { formatAttachmentFailures, type AttachmentFailure } from "../attachments/attachment-failure";
5
6
  import {
6
- buildImageMessageContent,
7
- formatImageFailures,
8
- isImageAttachment,
9
- resolveInboundImages,
10
- type ImageContentLike,
11
- } from "../attachments/image-attachments";
12
- import { formatFilePrompt, saveInboundFiles, sessionUploadDir, type SavedFile } from "../attachments/file-attachments";
7
+ formatAttachmentPrompt,
8
+ saveInboundFiles,
9
+ sessionUploadDir,
10
+ type SavedFile,
11
+ } from "../attachments/file-attachments";
13
12
  import { log } from "../infra/file-logger";
14
13
  import { isVoiceAttachment, resolveVoiceTranscript } from "../attachments/voice-transcript";
15
14
 
@@ -18,9 +17,7 @@ export interface InboundDeps {
18
17
  ctx: ExtensionContext;
19
18
  budget: ReplyBudget;
20
19
  getAllowedOpenid: () => string | undefined;
21
- getBoundOpenid: () => string | undefined;
22
20
  getDeliverAs: () => "steer" | "followUp";
23
- getImageMaxSize: () => number;
24
21
  getFileMaxSize: () => number;
25
22
  saveFiles?: typeof saveInboundFiles;
26
23
  getStatusLines: () => string[];
@@ -31,11 +28,11 @@ export interface InboundDeps {
31
28
  }
32
29
 
33
30
  export function createInboundHandler(deps: InboundDeps) {
34
- let pendingFiles: SavedFile[] = [];
35
- const takePendingFiles = (): SavedFile[] => {
36
- const files = pendingFiles;
37
- pendingFiles = [];
38
- return files;
31
+ let pendingAttachments: SavedFile[] = [];
32
+ const takePendingAttachments = (): SavedFile[] => {
33
+ const attachments = pendingAttachments;
34
+ pendingAttachments = [];
35
+ return attachments;
39
36
  };
40
37
 
41
38
  return async (bot: QQBot, msg: QQBotInboundMessage): Promise<void> => {
@@ -78,31 +75,32 @@ export function createInboundHandler(deps: InboundDeps) {
78
75
 
79
76
  const channel: ReplyChannel = {
80
77
  budget: deps.budget,
81
- boundOpenid: deps.getBoundOpenid() ?? msg.senderId,
78
+ boundOpenid: deps.getAllowedOpenid() ?? msg.senderId,
82
79
  onWarn: deps.onWarn,
83
80
  };
84
81
  const reply = (text: string) => sendReply(bot, text, channel);
85
82
  const saveFiles = deps.saveFiles ?? saveInboundFiles;
86
83
 
87
- const text = (msg.content ?? "").trim();
88
- if (text.startsWith("/")) {
89
- const command = text.split(/\s+/)[0];
84
+ const contentText = (msg.content ?? "").trim();
85
+ if (contentText.startsWith("/")) {
86
+ const command = contentText.split(/\s+/)[0];
90
87
  log.info("inbound", "QQ command", { command });
91
- if (command === "/new" && deps.ctx.isIdle() && pendingFiles.length > 0) {
92
- log.info("inbound", "cleared pending files for /new", { count: pendingFiles.length });
93
- pendingFiles = [];
88
+ if (command === "/new" && deps.ctx.isIdle() && pendingAttachments.length > 0) {
89
+ log.info("inbound", "cleared pending attachments for /new", { count: pendingAttachments.length });
90
+ pendingAttachments = [];
94
91
  }
95
- await handleCommand(text, reply, deps);
92
+ await handleCommand(contentText, reply, deps);
96
93
  return;
97
94
  }
98
95
 
99
96
  const attachments = msg.attachments ?? [];
100
- if (!text && attachments.length === 0) {
97
+ if (!contentText && attachments.length === 0) {
101
98
  log.warn("inbound", "unsupported message: no text and no attachments", { messageId: msg.messageId });
102
99
  await reply("Unsupported message type.");
103
100
  return;
104
101
  }
105
102
 
103
+ let text = contentText;
106
104
  const voiceAttachments = attachments.filter(isVoiceAttachment);
107
105
  if (voiceAttachments.length > 0) {
108
106
  const transcript = resolveVoiceTranscript(voiceAttachments);
@@ -112,52 +110,31 @@ export function createInboundHandler(deps: InboundDeps) {
112
110
  asrLength: transcript?.length ?? 0,
113
111
  });
114
112
  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
- });
129
- return;
130
- }
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).`);
113
+ if (!contentText) {
114
+ log.warn("inbound", "voice ASR returned no text", { messageId: msg.messageId });
115
+ await reply("Could not transcribe this voice message; please resend or type it.");
116
+ return;
117
+ }
118
+ log.warn("inbound", "voice ASR returned no text; using text content", { messageId: msg.messageId });
145
119
  } else {
146
- const result = await resolveInboundImages(imageAttachments, deps.getImageMaxSize());
147
- images = result.images;
148
- failures.push(...result.failures);
120
+ log.debug("inbound", "voice transcript", { messageId: msg.messageId, transcript });
121
+ text = contentText ? `${contentText}\n${transcript}` : transcript;
149
122
  }
150
123
  }
151
124
 
125
+ // Voice attachments are handled above; all other attachments are stored as files.
126
+ const attachmentsToSave = attachments.filter((attachment) => !isVoiceAttachment(attachment));
127
+ const failures: AttachmentFailure[] = [];
128
+
152
129
  let files: SavedFile[] = [];
153
- if (fileAttachments.length > 0) {
130
+ if (attachmentsToSave.length > 0) {
154
131
  const uploadDir = sessionUploadDir(deps.ctx.cwd, deps.ctx.sessionManager.getSessionId());
155
- log.debug("inbound", "saving files", {
132
+ log.debug("inbound", "saving attachments", {
156
133
  messageId: msg.messageId,
157
- count: fileAttachments.length,
134
+ count: attachmentsToSave.length,
158
135
  uploadDir,
159
136
  });
160
- const result = await saveFiles(fileAttachments, {
137
+ const result = await saveFiles(attachmentsToSave, {
161
138
  maxSize: deps.getFileMaxSize(),
162
139
  uploadDir,
163
140
  });
@@ -169,11 +146,11 @@ export function createInboundHandler(deps: InboundDeps) {
169
146
  log.warn("inbound", "attachment failure summary", {
170
147
  messageId: msg.messageId,
171
148
  failures,
172
- successCount: images.length + files.length,
149
+ successCount: files.length,
173
150
  });
174
- await reply(formatImageFailures(failures, images.length + files.length));
151
+ await reply(formatAttachmentFailures(failures, files.length));
175
152
  }
176
- if (!text && images.length === 0 && files.length === 0) {
153
+ if (!text && files.length === 0) {
177
154
  log.warn("inbound", "no injectable content after attachment handling", {
178
155
  messageId: msg.messageId,
179
156
  failures: failures.length,
@@ -181,27 +158,27 @@ export function createInboundHandler(deps: InboundDeps) {
181
158
  return;
182
159
  }
183
160
 
184
- if (!text && images.length === 0 && files.length > 0) {
185
- pendingFiles.push(...files);
186
- log.info("inbound", "files pending for instruction", {
161
+ if (!text && files.length > 0) {
162
+ pendingAttachments.push(...files);
163
+ log.info("inbound", "attachments pending for instruction", {
187
164
  messageId: msg.messageId,
188
165
  count: files.length,
189
- pendingCount: pendingFiles.length,
166
+ pendingCount: pendingAttachments.length,
190
167
  });
191
168
  return;
192
169
  }
193
170
 
194
171
  deps.markQQTrigger(msg.senderId, msg.messageId);
195
- const pending = text ? takePendingFiles() : [];
172
+ const pending = text ? takePendingAttachments() : [];
196
173
  const promptFiles = [...pending, ...files];
197
- const promptText = formatFilePrompt(promptFiles, text);
198
- deps.pi.sendUserMessage(buildImageMessageContent(promptText, images), { deliverAs: deps.getDeliverAs() });
174
+ const promptText = formatAttachmentPrompt(promptFiles, text, deps.ctx.cwd);
175
+ deps.pi.sendUserMessage(promptText, { deliverAs: deps.getDeliverAs() });
199
176
  log.info("inbound", "sent to pi", {
200
177
  messageId: msg.messageId,
201
178
  textLength: text.length,
202
- images: images.length,
203
- files: promptFiles.length,
204
- pendingFiles: pending.length,
179
+ voice: voiceAttachments.length > 0,
180
+ attachments: promptFiles.length,
181
+ pendingAttachments: pending.length,
205
182
  deliverAs: deps.getDeliverAs(),
206
183
  });
207
184
  };
@@ -224,10 +201,10 @@ async function handleCommand(text: string, reply: (t: string) => Promise<void>,
224
201
  );
225
202
  break;
226
203
  case "/stop":
204
+ deps.onStop?.();
227
205
  if (deps.ctx.isIdle()) {
228
206
  await reply("No task is running.");
229
207
  } else {
230
- deps.onStop?.();
231
208
  deps.ctx.abort();
232
209
  await deps.waitForEnd?.();
233
210
  await reply("Abort requested.");
@@ -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 await new Promise<QrLoginResult | undefined>((resolve) => {
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
  }
@@ -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
- if (text.length <= limit) return [text];
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 > limit) {
67
- let cut = rest.lastIndexOf("\n", limit);
68
- cut = cut <= 0 ? limit : cut + 1;
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
  }
@@ -58,9 +58,8 @@ const RECOVERY_TTL_MS = 60_000;
58
58
  const MAX_RESUME_ATTEMPTS = 2;
59
59
  const MAX_BUBBLE_CHARS = 12_000;
60
60
 
61
- function appendThinkingDelta(delta: string, run: RunState): string {
61
+ function trackThinkingLineStart(delta: string, run: RunState): void {
62
62
  run.thinkingAtLineStart = delta.endsWith("\n");
63
- return delta;
64
63
  }
65
64
 
66
65
  export class RunFeedback {
@@ -190,14 +189,8 @@ export class RunFeedback {
190
189
  } else {
191
190
  const newBubble = this.startsNewBlock(run, "thinking");
192
191
  const prefix = this.beginBlock(run, "thinking", newBubble);
193
- this.push(
194
- run,
195
- `${prefix}${appendThinkingDelta(delta, run)}`,
196
- prefix.length > 0 || newBubble,
197
- false,
198
- newBubble,
199
- true,
200
- );
192
+ trackThinkingLineStart(delta, run);
193
+ this.push(run, `${prefix}${delta}`, prefix.length > 0 || newBubble, false, newBubble, true);
201
194
  }
202
195
  }
203
196
  }
@@ -284,6 +277,8 @@ export class RunFeedback {
284
277
  this.clearTimers();
285
278
  this.run?.cur?.dispose();
286
279
  this.run = null;
280
+ this.pending = null;
281
+ this.recovery = null;
287
282
  this.finishEnd();
288
283
  }
289
284
 
@@ -341,14 +336,8 @@ export class RunFeedback {
341
336
  run.deferredThinking = "";
342
337
  const newBubble = this.startsNewBlock(run, "thinking");
343
338
  const prefix = this.beginBlock(run, "thinking", newBubble);
344
- this.push(
345
- run,
346
- `${prefix}${appendThinkingDelta(thinking, run)}`,
347
- prefix.length > 0 || newBubble,
348
- false,
349
- newBubble,
350
- true,
351
- );
339
+ trackThinkingLineStart(thinking, run);
340
+ this.push(run, `${prefix}${thinking}`, prefix.length > 0 || newBubble, false, newBubble, true);
352
341
  }
353
342
  if (run.deferredText) {
354
343
  const text = run.deferredText;
@@ -517,6 +506,7 @@ export class RunFeedback {
517
506
  if (run.cur === stream) this.handleStreamError(run, err);
518
507
  });
519
508
  },
509
+ onDebug: (message) => log.debug("stream", message, { msgId: run.msgId?.slice(0, 8) ?? "?" }),
520
510
  });
521
511
  run.cur = stream;
522
512
  if (!resume) this.deps.budget.noteStreamBubble(run.msgId);
@@ -21,9 +21,13 @@ const TOOL_ICONS: Record<string, string> = {
21
21
  test: "🧪",
22
22
  };
23
23
 
24
- function inlineCode(text: string): string {
24
+ function backtickFence(text: string, minimum: number): string {
25
25
  const runs = text.match(/`+/g) ?? [];
26
- const ticks = "`".repeat(Math.max(1, ...runs.map((run) => run.length + 1)));
26
+ return "`".repeat(Math.max(minimum, ...runs.map((run) => run.length + 1)));
27
+ }
28
+
29
+ function inlineCode(text: string): string {
30
+ const ticks = backtickFence(text, 1);
27
31
  const pad = text.startsWith("`") || text.endsWith("`") ? " " : "";
28
32
  return `${ticks}${pad}${text}${pad}${ticks}`;
29
33
  }
@@ -108,7 +112,6 @@ export function formatToolResult(result: unknown, isError: boolean): string {
108
112
  if (!text) text = "(no output)";
109
113
  text = truncateText(text, RESULT_MAX_CHARS, RESULT_MAX_LINES);
110
114
  if (isError) text = `❌ ${text}`;
111
- const runs = text.match(/`+/g) ?? [];
112
- const fence = "`".repeat(Math.max(3, ...runs.map((run) => run.length + 1)));
115
+ const fence = backtickFence(text, 3);
113
116
  return `${fence}text\n${text}\n${fence}\n`;
114
117
  }
@@ -1,142 +0,0 @@
1
- import { downloadLimited, type InboundAttachmentLike } from "./attachment-download";
2
- import { formatBytes } from "../infra/display-format";
3
-
4
- export type { InboundAttachmentLike } from "./attachment-download";
5
-
6
- export interface ImageContentLike {
7
- type: "image";
8
- data: string;
9
- mimeType: string;
10
- }
11
-
12
- export interface TextContentLike {
13
- type: "text";
14
- text: string;
15
- }
16
-
17
- export interface ImageFailure {
18
- filename: string;
19
- reason: string;
20
- }
21
-
22
- export interface ImageResolveResult {
23
- images: ImageContentLike[];
24
- failures: ImageFailure[];
25
- }
26
-
27
- const DOWNLOAD_TIMEOUT_MS = 15_000;
28
-
29
- const MIME_BY_CONTENT_TYPE: Record<string, string> = {
30
- "image/jpeg": "image/jpeg",
31
- "image/jpg": "image/jpeg",
32
- "image/png": "image/png",
33
- "image/webp": "image/webp",
34
- "image/gif": "image/gif",
35
- };
36
-
37
- const MIME_BY_EXTENSION: Record<string, string> = {
38
- jpg: "image/jpeg",
39
- jpeg: "image/jpeg",
40
- png: "image/png",
41
- webp: "image/webp",
42
- gif: "image/gif",
43
- };
44
-
45
- export function resolveImageMime(contentType: string | undefined, filename: string | undefined): string | null {
46
- const type = (contentType ?? "").split(";")[0].trim().toLowerCase();
47
- if (type) {
48
- if (MIME_BY_CONTENT_TYPE[type]) return MIME_BY_CONTENT_TYPE[type];
49
- if (type.startsWith("image/")) return null;
50
- }
51
-
52
- const ext = filename?.split(".").pop()?.toLowerCase();
53
- return ext ? (MIME_BY_EXTENSION[ext] ?? null) : null;
54
- }
55
-
56
- export function isImageAttachment(attachment: InboundAttachmentLike): boolean {
57
- const type = (attachment.content_type ?? "").toLowerCase();
58
- if (type.startsWith("image/")) return true;
59
- return resolveImageMime(attachment.content_type, attachment.filename) !== null;
60
- }
61
-
62
- export function buildImageMessageContent(
63
- text: string,
64
- images: readonly ImageContentLike[],
65
- ): string | (TextContentLike | ImageContentLike)[] {
66
- if (images.length === 0) return text;
67
- const content: (TextContentLike | ImageContentLike)[] = [];
68
- if (text) content.push({ type: "text", text });
69
- content.push(...images);
70
- return content;
71
- }
72
-
73
- export function formatImageFailures(failures: readonly ImageFailure[], successCount: number): string {
74
- const title =
75
- successCount > 0
76
- ? `⚠️ ${failures.length} attachment(s) failed (${successCount} succeeded):`
77
- : "⚠️ Attachment failed:";
78
- return [title, ...failures.map((failure) => `• ${failure.filename}: ${failure.reason}`)].join("\n");
79
- }
80
-
81
- function attachmentLabel(filename: string | undefined, index: number): string {
82
- const name = filename?.trim();
83
- return name || `Image ${index + 1}`;
84
- }
85
-
86
- async function downloadOne(
87
- attachment: InboundAttachmentLike,
88
- index: number,
89
- maxSize: number,
90
- fetchImpl: typeof fetch,
91
- ): Promise<{ image?: ImageContentLike; failure?: ImageFailure }> {
92
- const filename = attachmentLabel(attachment.filename, index);
93
- const mimeType = resolveImageMime(attachment.content_type, attachment.filename);
94
- if (!mimeType) {
95
- return {
96
- failure: {
97
- filename,
98
- reason: `Unsupported attachment type (${attachment.content_type || "unknown"})`,
99
- },
100
- };
101
- }
102
- if (typeof attachment.size === "number" && attachment.size > maxSize) {
103
- return {
104
- failure: {
105
- filename,
106
- reason: `image too large (${formatBytes(attachment.size)} > ${formatBytes(maxSize)})`,
107
- },
108
- };
109
- }
110
-
111
- try {
112
- const body = await downloadLimited({
113
- url: attachment.url,
114
- maxSize,
115
- timeoutMs: DOWNLOAD_TIMEOUT_MS,
116
- kind: "image",
117
- fetchImpl,
118
- });
119
- return { image: { type: "image", data: body.toString("base64"), mimeType } };
120
- } catch (err) {
121
- return { failure: { filename, reason: err instanceof Error ? err.message : "Download failed" } };
122
- }
123
- }
124
-
125
- export async function resolveInboundImages(
126
- attachments: readonly InboundAttachmentLike[],
127
- maxSize: number,
128
- fetchImpl: typeof fetch = fetch,
129
- ): Promise<ImageResolveResult> {
130
- const limit = Math.max(1, maxSize);
131
- const results = await Promise.all(
132
- attachments.map((attachment, index) => downloadOne(attachment, index, limit, fetchImpl)),
133
- );
134
-
135
- const images: ImageContentLike[] = [];
136
- const failures: ImageFailure[] = [];
137
- for (const result of results) {
138
- if (result.image) images.push(result.image);
139
- if (result.failure) failures.push(result.failure);
140
- }
141
- return { images, failures };
142
- }