@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
|
@@ -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
|
-
}
|