+ >({});
const attachmentIds = Array.from(
content.matchAll(
/!\[[^\]]*\]\(astrbot-attachment:\/\/([A-Za-z0-9._-]+)\)/g,
@@ -95,6 +119,7 @@ export function MessageContent({ content, slot }: MessageContentProps) {
let cancelled = false;
const createdUrls: string[] = [];
setImageUrls({});
+ setAttachmentErrors({});
const ids = attachmentKey ? attachmentKey.split("|") : [];
if (!ids.length) return undefined;
@@ -110,7 +135,11 @@ export function MessageContent({ content, slot }: MessageContentProps) {
payload.bytes instanceof Uint8Array
? payload.bytes
: Array.isArray(payload.bytes)
- ? new Uint8Array(payload.bytes.filter((item): item is number => typeof item === "number"))
+ ? new Uint8Array(
+ payload.bytes.filter(
+ (item): item is number => typeof item === "number",
+ ),
+ )
: null;
if (!bytes || cancelled) return;
const blob = new Blob([bytes.buffer as ArrayBuffer], {
@@ -124,8 +153,15 @@ export function MessageContent({ content, slot }: MessageContentProps) {
if (!cancelled) {
setImageUrls((current) => ({ ...current, [attachmentId]: url }));
}
- } catch {
- // Keep an inline placeholder when the attachment is no longer available.
+ } catch (reason) {
+ // Used to be swallowed entirely, which left a broken image icon with
+ // no way to tell a failed download from an undecodable payload.
+ if (!cancelled) {
+ setAttachmentErrors((current) => ({
+ ...current,
+ [attachmentId]: errorMessage(reason),
+ }));
+ }
}
}),
);
@@ -136,9 +172,17 @@ export function MessageContent({ content, slot }: MessageContentProps) {
};
}, [attachmentKey, slot]);
- const lines = normalizeContent(content)
- .replaceAll("\r\n", "\n")
- .split("\n");
+ const attachments: AttachmentView = {
+ urls: imageUrls,
+ errors: attachmentErrors,
+ onDecodeFailure: (attachmentId) =>
+ setAttachmentErrors((current) => ({
+ ...current,
+ [attachmentId]: "数据已下载,但不是浏览器能解码的图片格式。",
+ })),
+ };
+
+ const lines = normalizeContent(content).replaceAll("\r\n", "\n").split("\n");
const blocks: ReactNode[] = [];
let codeLines: string[] | null = null;
let codeLanguage = "";
@@ -149,7 +193,11 @@ export function MessageContent({ content, slot }: MessageContentProps) {
if (!paragraph.length) return;
blocks.push(
- {renderInline(paragraph.join(" "), `paragraph-${blocks.length}`, imageUrls)}
+ {renderInline(
+ paragraph.join(" "),
+ `paragraph-${blocks.length}`,
+ attachments,
+ )}
,
);
paragraph = [];
@@ -160,7 +208,7 @@ export function MessageContent({ content, slot }: MessageContentProps) {
{list.map((item, index) => (
-
- {renderInline(item, `list-${blocks.length}-${index}`, imageUrls)}
+ {renderInline(item, `list-${blocks.length}-${index}`, attachments)}
))}
,
@@ -203,7 +251,7 @@ export function MessageContent({ content, slot }: MessageContentProps) {
const Heading = `h${level}` as "h1" | "h2" | "h3";
blocks.push(
- {renderInline(heading[2], `heading-${index}`, imageUrls)}
+ {renderInline(heading[2], `heading-${index}`, attachments)}
,
);
return;
diff --git a/src/styles.css b/src/styles.css
index 22a0c3c..807855c 100644
--- a/src/styles.css
+++ b/src/styles.css
@@ -3399,6 +3399,18 @@ button:disabled {
background: var(--ui-surface-soft);
}
+/* Why an attachment did not render, rather than a bare broken-image icon. */
+.message-attachment-failed {
+ display: inline-flex;
+ align-items: center;
+ min-height: 2rem;
+ padding: 0.35rem 0.55rem;
+ border: 1px dashed #d88d8d;
+ border-radius: var(--ui-radius-sm, 0.5rem);
+ color: #e2a5a5;
+ font-size: 0.85rem;
+}
+
.message-attachment-placeholder {
display: inline-flex;
align-items: center;