Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(llm): cover the chat lightbox, attachment chips and tool-call fallbacks
Elian Doran94c2deacbd20
diff --git a/apps/client/src/widgets/dialogs/lightbox.spec.tsx b/apps/client/src/widgets/dialogs/lightbox.spec.tsxnew file mode 100644index 0000000000..3aa40742b5--- /dev/null+++ b/apps/client/src/widgets/dialogs/lightbox.spec.tsx@@ -0,0 +1,65 @@+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import Component from "../../components/component";+import { renderInto } from "../../test/render";+import { ParentComponent } from "../react/react_utils";+import LightboxDialog, { type LightboxOptions } from "./lightbox";++vi.mock("../../services/i18n", () => ({+ t: (key: string) => key+}));+vi.mock("../react/ImageViewer", () => ({+ default: ({ src, alt }: { src: string; alt: string }) => <img className="image-viewer-stub" src={src} alt={alt} />+}));+vi.mock("../type_widgets/file/PdfViewer", () => ({+ default: ({ pdfUrl }: { pdfUrl: string }) => <div className="pdf-viewer-stub" data-url={pdfUrl} />+}));++let host: Component;++beforeEach(() => {+ host = new Component();+ renderInto(+ <ParentComponent.Provider value={host}>+ <LightboxDialog />+ </ParentComponent.Provider>+ );+});++afterEach(() => {+ vi.restoreAllMocks();+});++async function show(opts: LightboxOptions) {+ await act(async () => {+ await host.handleEvent("showLightbox", opts);+ });+ return document.querySelector(".lightbox-dialog");+}++describe("LightboxDialog", () => {+ it("shows an image under its title, and opens the original in a new window", async () => {+ const open = vi.spyOn(window, "open").mockReturnValue(null);+ const dialog = await show({ src: "api/attachments/a1/image/cat.png", title: "cat.png" });++ expect(dialog?.querySelector(".modal-title")?.textContent).toBe("cat.png");+ const image = dialog?.querySelector(".image-viewer-stub");+ expect(image?.getAttribute("src")).toBe("api/attachments/a1/image/cat.png");+ expect(image?.getAttribute("alt")).toBe("cat.png");+ expect(dialog?.querySelector(".pdf-viewer-stub")).toBeNull();++ const openOriginal = dialog?.querySelector<HTMLButtonElement>(".custom-title-bar-button.bx-link-external");+ expect(openOriginal).not.toBeNull();+ act(() => openOriginal?.click());+ expect(open).toHaveBeenCalledExactlyOnceWith("api/attachments/a1/image/cat.png", "_blank", "noopener,noreferrer");+ });++ it("shows a PDF in the viewer, titled by its kind when it has no title", async () => {+ const dialog = await show({ src: "/api/attachments/p1/open", kind: "pdf" });++ expect(dialog?.querySelector(".modal-title")?.textContent).toBe("lightbox.pdf");+ expect(dialog?.querySelector(".pdf-viewer-stub")?.getAttribute("data-url")).toBe("/api/attachments/p1/open");+ expect(dialog?.querySelector(".image-viewer-stub")).toBeNull();+ });+});diff --git a/apps/client/src/widgets/dialogs/tool_call_details.spec.tsx b/apps/client/src/widgets/dialogs/tool_call_details.spec.tsxindex 3a673dcffc..a8ff03d99d 100644--- a/apps/client/src/widgets/dialogs/tool_call_details.spec.tsx+++ b/apps/client/src/widgets/dialogs/tool_call_details.spec.tsx@@ -53,12 +53,11 @@ describe("ToolCallDetailsDialog", () => {expect(sections()[1]).toEqual({ heading: "llm_chat.error", code: "Sunny", empty: undefined });});- it("says so when a call has no result yet", async () => {- await open({ id: "1", toolName: "read_web_page", input: { url: "https://triliumnotes.org" } });- expect(sections()[1]).toEqual({- heading: "llm_chat.result",- code: undefined,- empty: "llm_chat.tool_call_no_result"- });+ it("shows the partial input of a call still streaming as it is, and says it has no result yet", async () => {+ await open({ id: "1", toolName: "read_web_page", input: {}, inputStreaming: "{\"url\":\"https://tri" });+ expect(sections()).toEqual([+ { heading: "llm_chat.input", code: "{\"url\":\"https://tri", empty: undefined },+ { heading: "llm_chat.result", code: undefined, empty: "llm_chat.tool_call_no_result" }+ ]);});});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.spec.tsxnew file mode 100644index 0000000000..4e0b07df56--- /dev/null+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.spec.tsx@@ -0,0 +1,44 @@+import { act } from "preact/test-utils";+import { describe, expect, it, vi } from "vitest";++const mocks = vi.hoisted(() => ({ triggerEvent: vi.fn() }));+vi.mock("../../../components/app_context.js", () => ({ default: { triggerEvent: mocks.triggerEvent } }));++import { renderInto } from "../../../test/render";+import { PendingAttachmentChip } from "./ChatInputBar";+import type { AttachmentBlock } from "./useLlmChat";++const image: AttachmentBlock = { type: "image", attachmentId: "img", mime: "image/png", title: "cat.png", url: "api/attachments/img/image/cat.png" };+const text: AttachmentBlock = { type: "text_file", attachmentId: "txt", mime: "text/plain", title: "notes.txt", url: "#root/n1?attachmentId=txt" };++describe("PendingAttachmentChip", () => {+ it("marks an attachment the model cannot read, and opens a previewable one in the lightbox", () => {+ const onRemove = vi.fn();+ const chip = renderInto(<PendingAttachmentChip att={image} reason="Codex cannot read images" onRemove={onRemove} disabled={false} />)+ .querySelector(".llm-chat-attachment-chip");++ expect(chip?.classList.contains("llm-chat-attachment-chip-unreadable")).toBe(true);+ expect(chip?.getAttribute("title")).toBe("cat.png\nCodex cannot read images");+ expect(chip?.querySelector(".llm-chat-attachment-unreadable-icon")).not.toBeNull();++ const preview = chip?.querySelector<HTMLAnchorElement>("a.llm-chat-attachment-preview-link");+ expect(preview?.querySelector("img")).not.toBeNull();+ act(() => preview?.click());+ expect(mocks.triggerEvent).toHaveBeenCalledExactlyOnceWith("showLightbox", { src: image.url, title: "cat.png" });++ act(() => chip?.querySelector<HTMLButtonElement>(".llm-chat-attachment-remove")?.click());+ expect(onRemove).toHaveBeenCalledOnce();+ });++ it("shows a readable file without a preview link or a warning", () => {+ const chip = renderInto(<PendingAttachmentChip att={text} onRemove={vi.fn()} disabled />)+ .querySelector(".llm-chat-attachment-chip");++ expect(chip?.classList.contains("llm-chat-attachment-chip-unreadable")).toBe(false);+ expect(chip?.getAttribute("title")).toBe("notes.txt");+ expect(chip?.querySelector(".llm-chat-attachment-unreadable-icon")).toBeNull();+ expect(chip?.querySelector("a")).toBeNull();+ expect(chip?.querySelector(".llm-chat-attachment-file-name")?.textContent).toBe("notes.txt");+ expect(chip?.querySelector<HTMLButtonElement>(".llm-chat-attachment-remove")?.disabled).toBe(true);+ });+});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsxindex c25896c19f..b7242f7a6a 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx@@ -25,8 +25,8 @@ import { editorHtmlToMarkdown } from "./chat_input_markdown.js";import { shortModelName } from "./model_name.js";import ReasoningEffortDropdown from "./ReasoningEffortDropdown.js";import { SafeImage } from "./retry_image.js";-import { getAttachmentLightbox, unreadableReason, useChatAttachments } from "./useChatAttachments.js";-import { type ModelOption, nativeAttachmentKind, resolveSelectedModel, unreadableAttachments } from "../../../services/llm_providers.js";+import { getAttachmentLightbox, getUnreadableReasons, useChatAttachments } from "./useChatAttachments.js";+import { type ModelOption, resolveSelectedModel, unreadableAttachments } from "../../../services/llm_providers.js";import { type AttachmentBlock, type UseLlmChatReturn } from "./useLlmChat.js";const READ_ONLY_LOCK = "llm-chat-streaming";@@ -244,14 +244,7 @@ export default function ChatInputBar({// shows as selected is exactly what will be sent (see resolveSelectedModel).const currentModel = resolveSelectedModel(chat.availableModels, chat.selectedModel, chat.selectedProvider, chat.selectedProviderId);const isSelectedModel = (m: ModelOption) => m === currentModel;- const unreadable = unreadableAttachments(currentModel, chat.pendingAttachments);- const unreadableReasons = new Map<string, string>();- for (const att of unreadable) {- const kind = nativeAttachmentKind(att.type, att.mime);- if (currentModel && kind) {- unreadableReasons.set(att.attachmentId, unreadableReason(currentModel, kind));- }- }+ const unreadableReasons = getUnreadableReasons(currentModel, chat.pendingAttachments);// Gemini 2.x cannot combine googleSearch with function tools in a single// request. When note tools are enabled on a Gemini model we silently drop// web search server-side; reflect that here by disabling the toggle so the@@ -554,10 +547,10 @@ export default function ChatInputBar({text={chat.isStreaming? t("llm_chat.stop"): !currentModel ? t("llm_chat.no_model_selected")- : unreadable.length > 0 ? t("llm_chat.remove_unreadable_attachments", { model: currentModel.name })+ : unreadableReasons.size > 0 ? t("llm_chat.remove_unreadable_attachments", { model: currentModel.name }): t("llm_chat.send")}onClick={chat.isStreaming ? chat.stopStreaming : handleSubmit}- disabled={!chat.isStreaming && (!currentModel || unreadable.length > 0 || (!chat.hasInputText && chat.pendingAttachments.length === 0))}+ disabled={!chat.isStreaming && (!currentModel || unreadableReasons.size > 0 || (!chat.hasInputText && chat.pendingAttachments.length === 0))}className={`llm-chat-send-btn ${chat.isStreaming ? "llm-chat-stop-btn" : ""}`}/></div>@@ -599,7 +592,7 @@ export default function ChatInputBar({}/** A pending attachment above the input, marked when the selected model can't read it. */-function PendingAttachmentChip({ att, reason, onRemove, disabled }: {+export function PendingAttachmentChip({ att, reason, onRemove, disabled }: {att: AttachmentBlock;/** Why the selected model can't read it; undefined when it can. */reason?: string;diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.spec.tsxindex 0a7078987e..0457beeae4 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.spec.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.spec.tsx@@ -9,6 +9,8 @@ vi.mock("../../../services/i18n.js", () => ({return key;}}));+const mocks = vi.hoisted(() => ({ triggerEvent: vi.fn() }));+vi.mock("../../../components/app_context.js", () => ({ default: { triggerEvent: mocks.triggerEvent } }));vi.mock("../text/ReadOnlyText.js", () => ({ReadOnlyTextContent: ({ html }: { html: string }) => <div className="markdown-stub">{html}</div>}));@@ -59,15 +61,15 @@ describe("ChatMessage footer", () => {});});-describe("ChatMessage thinking", () => {- function renderMessage(content: StoredMessage["content"], options: { type?: StoredMessage["type"]; isStreaming?: boolean } = {}) {- host ??= document.body.appendChild(document.createElement("div"));- const target = host;- const message: StoredMessage = { id: "m1", role: "assistant", type: options.type, createdAt: "2026-01-01T00:00:00.000Z", content };- act(() => render(<ChatMessage message={message} isStreaming={options.isStreaming} />, target));- return target;- }+function renderMessage(content: StoredMessage["content"], options: { type?: StoredMessage["type"]; isStreaming?: boolean } = {}) {+ host ??= document.body.appendChild(document.createElement("div"));+ const target = host;+ const message: StoredMessage = { id: "m1", role: "assistant", type: options.type, createdAt: "2026-01-01T00:00:00.000Z", content };+ act(() => render(<ChatMessage message={message} isStreaming={options.isStreaming} />, target));+ return target;+}+describe("ChatMessage thinking", () => {it("folds each finished thought to its title or first line, in stream order between the tool calls", () => {const target = renderMessage([{ type: "thinking", content: "**Retrieving PC hostname with command**\n\nI'll read `/etc/hostname`." },@@ -160,3 +162,21 @@ describe("ChatMessage thinking", () => {expect(plain.querySelector(".llm-chat-reply")).toBeNull();});});++describe("ChatMessage attachments", () => {+ it("opens a PDF in the lightbox and any other file through its link", () => {+ const target = renderMessage([+ { type: "file", attachmentId: "pdf1", mime: "application/pdf", title: "report.pdf", url: "#root/n1?attachmentId=pdf1" },+ { type: "text_file", attachmentId: "txt1", mime: "text/plain", title: "notes.txt", url: "#root/n1?attachmentId=txt1" }+ ]);+ const [ pdf, text ] = [ ...target.querySelectorAll<HTMLAnchorElement>("a.llm-chat-message-file") ];+ expect(pdf?.querySelector(".bxs-file-pdf")).not.toBeNull();+ act(() => pdf?.click());+ expect(mocks.triggerEvent).toHaveBeenCalledExactlyOnceWith("showLightbox", { src: "/api/attachments/pdf1/open", kind: "pdf", title: "report.pdf" });++ expect(text?.getAttribute("href")).toBe("#root/n1?attachmentId=txt1");+ expect(text?.querySelector(".bxs-file-blank")).not.toBeNull();+ act(() => text?.click());+ expect(mocks.triggerEvent).toHaveBeenCalledOnce();+ });+});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsxindex afd3f14a6f..16aba53caf 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx@@ -65,10 +65,14 @@ describe("ToolCallCard", () => {const card = renderCard([{ id: "1", toolName: "search_icons", input: { query: "rocket" }, result: "[]" },{ id: "2", toolName: "web_search", input: { query: "weather Sibiu" }, result: "Sunny" },- { id: "3", toolName: "read_web_page", input: { url: "https://triliumnotes.org" }, result: "Fetched" }+ { id: "3", toolName: "read_web_page", input: { url: "https://triliumnotes.org" }, result: "Fetched" },+ // OpenAI's web_search takes no input and names what it did in its result's action.+ { id: "4", toolName: "web_search", input: {}, result: JSON.stringify({ action: { type: "search", queries: [ "a", "b" ] } }) },+ { id: "5", toolName: "get_note", input: { noteId: "x" }, result: "{}" },+ { id: "6", toolName: "web_search", input: {}, result: JSON.stringify({ action: { type: "open_page", url: "https://a.com" } }) }]);expect([ ...card.querySelectorAll(".llm-chat-tool-call-detail") ].map(detail => detail.textContent))- .toEqual([ "rocket", "weather Sibiu", "https://triliumnotes.org" ]);+ .toEqual([ "rocket", "weather Sibiu", "https://triliumnotes.org", "a; b", "https://a.com" ]);});it("lists the calls as bare lines, folding only those with something to show inline", () => {@@ -109,6 +113,9 @@ describe("ToolCallCard", () => {act(() => line?.querySelector<HTMLButtonElement>(".llm-chat-tool-call-debug")?.click());expect(mocks.triggerEvent).toHaveBeenCalledOnce();expect((line as HTMLDetailsElement | null)?.open).toBe(false);++ const plain = renderCard([ { id: "2", toolName: "get_attachment", input: { attachmentId: "a" }, result: "Attachment not found", isError: true } ]);+ expect(plain.querySelector(".llm-chat-tool-call-error-message")?.textContent).toBe("Attachment not found");});it("lists the notes a search found, with their parents and a plain preview", () => {@@ -688,9 +695,11 @@ describe("ToolCallCard", () => {{ id: "4", toolName: "get_note", input: { noteId: "x" }, result: "{}" },search("5", { query: "t" }, "Web search results for query: \"t\"\n\nLinks: [{\"title\":\"T\",\"url\":\"https://t.org/p\"}]\n\nSummary"),{ id: "6", toolName: "get_note", input: { noteId: "x" }, result: "{}" },- search("7", { query: "q" }, "Searching the web")+ search("7", { query: "q" }, "Searching the web"),+ { id: "8", toolName: "get_note", input: { noteId: "x" }, result: "{}" },+ search("9", { query: "u" }, [ { url: "https://bad host/", title: "Bad" } ])]);- const [ anthropic, , openai, , claude, , bare ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];+ const [ anthropic, , openai, , claude, , bare, , badUrl ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];const sources = (line: Element | undefined) => [ ...(line?.querySelectorAll(".llm-chat-note-result") ?? []) ].map(row => ({text: row.querySelector("a.external")?.textContent,href: row.querySelector("a.external")?.getAttribute("href"),@@ -709,6 +718,7 @@ describe("ToolCallCard", () => {expect(sources(claude)).toEqual([ { text: "T", href: "https://t.org/p", domain: "t.org" } ]);expect(bare instanceof HTMLDetailsElement).toBe(false);+ expect(sources(badUrl)).toEqual([ { text: "Bad", href: "https://bad host/", domain: "" } ]);});it("links the page a call read, and previews what it read there", () => {diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.spec.tsxnew file mode 100644index 0000000000..4d5a904a58--- /dev/null+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.spec.tsx@@ -0,0 +1,68 @@+import { describe, expect, it, vi } from "vitest";++vi.mock("../../../services/i18n.js", () => ({+ t: (key: string, options?: Record<string, unknown>) => (options ? `${key}${JSON.stringify(options)}` : key)+}));++import type { ToolCall } from "./llm_chat_types.js";+import { getToolCallView, markdownToPlainPreview } from "./ToolCallViews.js";++function call(toolName: string, result: string | undefined, input: Record<string, unknown> = {}): ToolCall {+ return { id: "1", toolName, input, result };+}++describe("getToolCallView", () => {+ it("falls back to the bare line for a result or input it cannot read", () => {+ const unreadable: ToolCall[] = [+ call("create_note", undefined, { type: 1, content: "x" }),+ call("set_note_content", undefined, { noteId: "n1", content: " " }),+ call("append_to_note", undefined, { noteId: 1, content: "x" }),+ call("set_attribute", "not json", { type: "label" }),+ call("get_attributes", "{}"),+ call("get_attribute", "{\"type\":\"label\"}"),+ call("delete_attribute", "not json"),+ call("search_notes", "{\"totalResults\":\"many\",\"results\":[]}"),+ call("search_notes", "null"),+ call("search_help", "not json"),+ call("search_icons", "{\"totalResults\":1}"),+ call("get_help_toc", "{\"toc\":1}"),+ call("web_search", "[]"),+ call("read_web_page", "{\"status\":\"ok\"}"),+ call("read_web_page", "[12KB - too large to preview]"),+ call("get_child_notes", "not json"),+ call("get_child_notes", "{}"),+ call("rename_note", "{}"),+ call("get_attachment", "{\"title\":\"a.png\"}"),+ call("get_attachment_content", "{}"),+ call("get_note", "not json"),+ call("get_note_content", "not json"),+ call("get_subtree", "not json"),+ call("get_subtree", "[]"),+ call("unknown_tool", "{}")+ ];+ expect(unreadable.map(tc => [ tc.toolName, getToolCallView(tc) ])).toEqual(unreadable.map(tc => [ tc.toolName, null ]));+ });++ it("counts what a result lists when it gives no count of its own, and leaves out an empty body", () => {+ const toc = getToolCallView(call("get_help_toc", JSON.stringify({ toc: "Basics (_help_a)\nNotes (_help_b)" })));+ expect(toc?.summary).toBe("llm_chat.search_help_count{\"count\":2}");++ const subtree = getToolCallView(call("get_subtree", JSON.stringify({ noteId: "root" })));+ expect(subtree).toEqual({ summary: "llm_chat.search_notes_count{\"count\":0}", body: undefined });++ const attachment = getToolCallView(call("get_attachment", JSON.stringify({ title: "a.png", ownerId: "n1" })));+ expect(attachment?.lead).toBeDefined();+ expect(attachment?.summary).toBeUndefined();++ expect(getToolCallView(call("get_attributes", "[]"))?.body).toBeUndefined();+ expect(getToolCallView(call("search_help", JSON.stringify({ totalResults: 0, results: [] })))?.body).toBeUndefined();+ expect(getToolCallView(call("search_icons", JSON.stringify({ totalResults: 0, results: [] })))?.body).toBeUndefined();+ expect(getToolCallView(call("get_child_notes", "[]"))?.body).toBeUndefined();+ });+});++describe("markdownToPlainPreview", () => {+ it("decodes numeric and named entities, and keeps the ones it cannot", () => {+ expect(markdownToPlainPreview("ABC & � &bogus;")).toBe("ABC & � &bogus;");+ });+});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.spec.ts b/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.spec.tsindex 52c9cfa56f..947e759fe6 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.spec.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.spec.ts@@ -1,11 +1,20 @@import type { LlmModelInfo } from "@triliumnext/commons";+import { h } from "preact";+import { act } from "preact/test-utils";import { describe, expect, it, vi } from "vitest";+const mocks = vi.hoisted(() => ({ showError: vi.fn() }));vi.mock("../../../services/i18n.js", () => ({t: (key: string, options?: Record<string, unknown>) => (options ? `${key} ${JSON.stringify(options)}` : key)}));+vi.mock("../../../services/toast.js", () => ({ default: { showError: mocks.showError } }));-import { acceptAttrFor, getAttachmentLightbox, uploadRefusal } from "./useChatAttachments";+import { renderInto } from "../../../test/render";+import {+ acceptAttrFor, getAttachmentLightbox, getUnreadableReasons, uploadRefusal,+ type UseChatAttachmentsReturn, useChatAttachments+} from "./useChatAttachments";+import type { AttachmentBlock, UseLlmChatReturn } from "./useLlmChat";describe("getAttachmentLightbox", () => {it("previews images and PDFs, and nothing else", () => {@@ -49,4 +58,46 @@ describe("attachments a model cannot read", () => {expect(uploadRefusal(imagesOnly, file("cat.png", "image/png"), "image")).toBeUndefined();expect(uploadRefusal(undefined, file("report.pdf", "application/pdf"), "binary_file")).toBeUndefined();});++ it("names why the model cannot read each pending attachment, and not the others", () => {+ const attachments: AttachmentBlock[] = [+ { type: "image", attachmentId: "img", mime: "image/png", title: "c.png", url: "u" },+ { type: "image", attachmentId: "svg", mime: "image/svg+xml", title: "d.svg", url: "u" },+ { type: "file", attachmentId: "pdf", mime: "application/pdf", title: "r.pdf", url: "u" },+ { type: "text_file", attachmentId: "txt", mime: "text/plain", title: "n.txt", url: "u" }+ ];+ expect([ ...getUnreadableReasons(imagesOnly, attachments) ]).toEqual([+ [ "pdf", `llm_chat.attachment_model_cannot_read_file {"model":"Codex"}` ]+ ]);+ expect([ ...getUnreadableReasons(textOnly, attachments).keys() ]).toEqual([ "img", "pdf" ]);+ expect(getUnreadableReasons(undefined, attachments).size).toBe(0);+ });++ it("keeps the picker and uploads to what the selected model reads", async () => {+ const chat = {+ chatNoteId: "chat1",+ availableModels: [ { ...textOnly, provider: "deepseek", providerId: "p1" } ],+ selectedModel: textOnly.id,+ selectedProvider: "deepseek",+ selectedProviderId: "p1",+ addPendingAttachment: vi.fn()+ } as unknown as UseLlmChatReturn;+ let result: UseChatAttachmentsReturn | undefined;+ function Harness() {+ result = useChatAttachments(chat);+ return null;+ }+ renderInto(h(Harness, {}));++ expect(result?.acceptAttr.split(",")).not.toContain("application/pdf");++ const input = document.createElement("input");+ Object.defineProperty(input, "files", { value: [ file("report.pdf", "application/pdf") ] });+ await act(async () => {+ await result?.handleFilePickerChange({ target: input } as unknown as Event);+ });+ expect(mocks.showError)+ .toHaveBeenCalledExactlyOnceWith(expect.stringContaining("attachment_refused"));+ expect(chat.addPendingAttachment).not.toHaveBeenCalled();+ });});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.ts b/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.tsindex 30421413dd..aafa4d7208 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/useChatAttachments.ts@@ -3,7 +3,9 @@ import { RefObject } from "preact";import { useCallback, useRef } from "preact/hooks";import { t } from "../../../services/i18n.js";-import { nativeAttachmentKind, readsAttachmentKind, resolveSelectedModel } from "../../../services/llm_providers.js";+import {+ nativeAttachmentKind, readsAttachmentKind, resolveSelectedModel, unreadableAttachments+} from "../../../services/llm_providers.js";import server from "../../../services/server.js";import toast from "../../../services/toast.js";import type { LightboxOptions } from "../../dialogs/lightbox.js";@@ -247,6 +249,21 @@ export function unreadableReason(model: LlmModelInfo, kind: LlmAttachmentKind):return t(`llm_chat.attachment_model_cannot_read_${kind}`, { model: model.name });}+/** Why `model` can't read each of `attachments`, by attachment ID; readable ones are left out. */+export function getUnreadableReasons(+ model: LlmModelInfo | undefined,+ attachments: AttachmentBlock[]+): Map<string, string> {+ const reasons = new Map<string, string>();+ for (const att of unreadableAttachments(model, attachments)) {+ const kind = nativeAttachmentKind(att.type, att.mime);+ if (model && kind) {+ reasons.set(att.attachmentId, unreadableReason(model, kind));+ }+ }+ return reasons;+}+/** The error for a file `model` can't read, which is then not uploaded; undefined when it can. */export function uploadRefusal(model: LlmModelInfo | undefined, file: File, kind: Exclude<UploadKind, null>): string | undefined {const nativeKind = nativeAttachmentKind(kind === "binary_file" ? "file" : kind, file.type);