Skip to content

Build your personal knowledge base with Trilium Notes

client/AI chat: better describe file-only messages in ToC

Adorian Doran2e257d086382
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 2e015c3f5a..ed20cf5e87 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2070,6 +2070,7 @@
"stop": "Stop",
"retry": "Retry",
"scroll_to_bottom": "Scroll to latest",
+ "toc_file": "File: {{name}}",
"attach_file": "Attach image, PDF, or text file",
"remove_attachment": "Remove attachment",
"attachment_upload_failed": "Failed to upload \"{{name}}\".",
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.spec.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.spec.ts
index 118b624238..67edf478a0 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.spec.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.spec.ts
@@ -1,8 +1,15 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import { buildChatHeadings, pickActiveHeadingId, truncateForToc } from "./chat_toc.js";
import type { ContentBlock, StoredMessage } from "./llm_chat_types.js";
+// i18next isn't initialized in unit tests, so stub `t` with a minimal interpolating
+// implementation to make the localized "File: " prefix deterministic.
+vi.mock("../../../services/i18n.js", () => ({
+ t: (key: string, opts?: Record<string, unknown>) =>
+ key === "llm_chat.toc_file" ? `File: ${opts?.name}` : key
+}));
+
describe("truncateForToc", () => {
it("returns short text unchanged", () => {
expect(truncateForToc("Hello world")).toBe("Hello world");
@@ -132,12 +139,28 @@ describe("buildChatHeadings", () => {
expect(heading.text).toBe("Describe this image");
});
- it("falls back to an attachment title for image-only messages", () => {
+ it("prefixes the localized \"File: \" label for image-only messages", () => {
const content: ContentBlock[] = [
{ type: "image", attachmentId: "att1", mime: "image/png", title: "diagram.png", url: "/x" }
];
const [heading] = buildChatHeadings([userMessage("u1", content)]);
- expect(heading.text).toBe("diagram.png");
+ expect(heading.text).toBe("File: diagram.png");
+ });
+
+ it("prefixes a localized \"File: \" label for file-only messages", () => {
+ const content: ContentBlock[] = [
+ { type: "file", attachmentId: "att1", mime: "application/pdf", title: "report.pdf", url: "/x" }
+ ];
+ const [heading] = buildChatHeadings([userMessage("u1", content)]);
+ expect(heading.text).toBe("File: report.pdf");
+ });
+
+ it("prefixes the \"File: \" label for text-file-only messages too", () => {
+ const content: ContentBlock[] = [
+ { type: "text_file", attachmentId: "att1", mime: "text/markdown", title: "notes.md", url: "/x" }
+ ];
+ const [heading] = buildChatHeadings([userMessage("u1", content)]);
+ expect(heading.text).toBe("File: notes.md");
});
});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts
index a8992a0476..4d552b93fa 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";
import type NoteContext from "../../../components/note_context.js";
+import { t } from "../../../services/i18n.js";
import { escapeHtml } from "../../../services/utils.js";
import type { HeadingContext } from "../../sidebar/TableOfContents.js";
import { type ContentBlock, getMessageText, type StoredMessage } from "./llm_chat_types.js";
@@ -129,8 +130,10 @@ function getMessagePreviewText(content: string | ContentBlock[]): string {
if (text.trim()) return text;
if (Array.isArray(content)) {
for (const block of content) {
+ // Attachment-only messages get a localized "File: " prefix so a bare filename
+ // in the list isn't mistaken for the message's prose.
if ((block.type === "image" || block.type === "file" || block.type === "text_file") && block.title) {
- return block.title;
+ return t("llm_chat.toc_file", { name: block.title });
}
}
}