@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.
- package/README-zh.md +67 -47
- package/README.md +70 -50
- package/doc/delivery-v1-zh.md +9 -9
- package/doc/delivery-v1.md +4 -4
- package/doc/delivery-v2-zh.md +3 -3
- package/doc/delivery-v2.md +3 -3
- package/doc/delivery-v3-zh.md +4 -2
- package/doc/delivery-v3.md +4 -2
- package/index.ts +36 -131
- package/package.json +4 -1
- package/src/attachments/attachment-failure.ts +12 -0
- package/src/attachments/file-attachments.ts +16 -19
- package/src/infra/file-logger.ts +0 -3
- package/src/infra/settings.ts +62 -8
- package/src/qq/qq-bot.ts +2 -2
- package/src/qq/qq-diagnostics.ts +129 -0
- package/src/qq/qq-message-handler.ts +51 -74
- package/src/qq/qr-login.ts +4 -1
- package/src/qq/reply-sender.ts +5 -4
- package/src/relay/run-stream-relay.ts +8 -18
- package/src/relay/stream-block-format.ts +7 -4
- package/src/attachments/image-attachments.ts +0 -142
|
@@ -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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
return
|
|
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.
|
|
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
|
|
88
|
-
if (
|
|
89
|
-
const command =
|
|
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() &&
|
|
92
|
-
log.info("inbound", "cleared pending
|
|
93
|
-
|
|
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(
|
|
92
|
+
await handleCommand(contentText, reply, deps);
|
|
96
93
|
return;
|
|
97
94
|
}
|
|
98
95
|
|
|
99
96
|
const attachments = msg.attachments ?? [];
|
|
100
|
-
if (!
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
-
|
|
147
|
-
|
|
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 (
|
|
130
|
+
if (attachmentsToSave.length > 0) {
|
|
154
131
|
const uploadDir = sessionUploadDir(deps.ctx.cwd, deps.ctx.sessionManager.getSessionId());
|
|
155
|
-
log.debug("inbound", "saving
|
|
132
|
+
log.debug("inbound", "saving attachments", {
|
|
156
133
|
messageId: msg.messageId,
|
|
157
|
-
count:
|
|
134
|
+
count: attachmentsToSave.length,
|
|
158
135
|
uploadDir,
|
|
159
136
|
});
|
|
160
|
-
const result = await saveFiles(
|
|
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:
|
|
149
|
+
successCount: files.length,
|
|
173
150
|
});
|
|
174
|
-
await reply(
|
|
151
|
+
await reply(formatAttachmentFailures(failures, files.length));
|
|
175
152
|
}
|
|
176
|
-
if (!text &&
|
|
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 &&
|
|
185
|
-
|
|
186
|
-
log.info("inbound", "
|
|
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:
|
|
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 ?
|
|
172
|
+
const pending = text ? takePendingAttachments() : [];
|
|
196
173
|
const promptFiles = [...pending, ...files];
|
|
197
|
-
const promptText =
|
|
198
|
-
deps.pi.sendUserMessage(
|
|
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
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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.");
|
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
|
}
|
|
@@ -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
|
|
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
|
-
|
|
194
|
-
|
|
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
|
-
|
|
345
|
-
|
|
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
|
|
24
|
+
function backtickFence(text: string, minimum: number): string {
|
|
25
25
|
const runs = text.match(/`+/g) ?? [];
|
|
26
|
-
|
|
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
|
|
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
|
-
}
|