Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): uploaded images not always shown in chat

Elian Dorana42ae7b3317e
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
index bd8c82c6f1..6b53c7161c 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
@@ -17,6 +17,7 @@ import { FormDropdownDivider, FormDropdownSubmenu, FormListItem, FormListTogglea
import { useLegacyImperativeHandlers } from "../../react/hooks.js";
import AddProviderModal, { type LlmProviderConfig } from "../options/llm/AddProviderModal.js";
import type { FileBlock, ImageBlock, TextFileBlock } from "./llm_chat_types.js";
+import { retryImageOnError } from "./retry_image.js";
import type { AttachmentBlock, UseLlmChatReturn } from "./useLlmChat.js";
/** MIME types we accept for image upload — must match what the providers support as vision input. */
@@ -395,7 +396,7 @@ export default function ChatInputBar({
title={att.title}
>
{att.type === "image" ? (
- <img src={att.url} alt={att.title} />
+ <img src={att.url} alt={att.title} onError={retryImageOnError} />
) : (
<div className="llm-chat-attachment-file">
<span className={`bx ${att.type === "file" ? "bxs-file-pdf" : "bxs-file-blank"} llm-chat-attachment-file-icon`} />
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
index 2b48a6ff25..eef07e7138 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
@@ -12,6 +12,7 @@ import Button from "../../react/Button.js";
import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";
import { ExpandableCard, ExpandableSection } from "./ExpandableCard.js";
import { type ContentBlock, type FileBlock, getMessageText, type ImageBlock, type StoredMessage, type TextBlock, type TextFileBlock, type ToolCallBlock } from "./llm_chat_types.js";
+import { retryImageOnError } from "./retry_image.js";
import ToolCallCard from "./ToolCallCard.js";
function shortenNumber(n: number): string {
@@ -292,7 +293,7 @@ function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {
className="llm-chat-message-image"
title={group.block.title}
>
- <img src={group.block.url} alt={group.block.title} />
+ <img src={group.block.url} alt={group.block.title} onError={retryImageOnError} />
</a>
);
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/retry_image.ts b/apps/client/src/widgets/type_widgets/llm_chat/retry_image.ts
new file mode 100644
index 0000000000..74974ed0bc
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/retry_image.ts
@@ -0,0 +1,27 @@
+/**
+ * `<img onError={retryImageOnError}>` — retry a failed image load up to three
+ * times with exponential backoff and a cache-busting query parameter.
+ *
+ * Why this exists: when the user uploads an image to a chat note, the server's
+ * upload endpoint returns the attachment URL *before* the async image-
+ * processing pipeline has written the actual bytes (see `saveImageToAttachment`
+ * in `packages/trilium-core/src/services/image.ts`). The first `<img>` fetch
+ * 404s for a few hundred milliseconds, the browser caches that 404, and the
+ * preview stays broken even after the image becomes available. Re-requesting
+ * with a fresh query string sidesteps the cached 404.
+ */
+export function retryImageOnError(e: Event) {
+ const img = e.currentTarget as HTMLImageElement | null;
+ if (!img) return;
+ const tries = Number(img.dataset.retries ?? "0");
+ if (tries >= 3) return;
+ // Remember the original URL so successive retries keep cache-busting off
+ // the same base, not stacking `?_retry=N` parameters.
+ const base = img.dataset.retryBase ?? img.src.replace(/[?&]_retry=\d+$/, "");
+ img.dataset.retryBase = base;
+ img.dataset.retries = String(tries + 1);
+ const separator = base.includes("?") ? "&" : "?";
+ setTimeout(() => {
+ img.src = `${base}${separator}_retry=${tries + 1}`;
+ }, 300 * (tries + 1));
+}