Skip to content

Build your personal knowledge base with Trilium Notes

client/AI chat: fix some issues

Adorian Doran2975976d4731
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_message_jump.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_message_jump.ts
index 9cde9f51f4..ce064a8a0e 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_message_jump.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_message_jump.ts
@@ -19,6 +19,8 @@ export function useChatMessageJumps(scrollContainerRef: RefObject<HTMLElement>)
const container = scrollContainerRef.current;
if (!container) return;
+ let flashTimer: number | undefined;
+
const onClick = (e: MouseEvent) => {
const link = (e.target as HTMLElement | null)?.closest<HTMLElement>(`.${MESSAGE_JUMP_CLASS}`);
const targetId = link?.dataset.messageId;
@@ -34,14 +36,26 @@ export function useChatMessageJumps(scrollContainerRef: RefObject<HTMLElement>)
if (!wrapper) return; // referenced message is gone — nothing to jump to
wrapper.scrollIntoView({ block: "center", behavior: "smooth" });
- // Re-trigger the flash on repeat clicks: remove, force a reflow, then re-add.
- wrapper.classList.remove(FLASH_CLASS);
+
+ // Flash the target. Cancel any pending removal and clear a lingering flash from an earlier
+ // rapid jump (which its own timer would otherwise remove, cutting this one short or leaving
+ // the old one stuck), then force a reflow so re-adding the class replays the animation.
+ if (flashTimer !== undefined) window.clearTimeout(flashTimer);
+ for (const flashed of container.querySelectorAll(`.${FLASH_CLASS}`)) {
+ flashed.classList.remove(FLASH_CLASS);
+ }
void wrapper.offsetWidth;
wrapper.classList.add(FLASH_CLASS);
- window.setTimeout(() => wrapper.classList.remove(FLASH_CLASS), FLASH_DURATION_MS);
+ flashTimer = window.setTimeout(() => {
+ wrapper.classList.remove(FLASH_CLASS);
+ flashTimer = undefined;
+ }, FLASH_DURATION_MS);
};
container.addEventListener("click", onClick);
- return () => container.removeEventListener("click", onClick);
+ return () => {
+ container.removeEventListener("click", onClick);
+ if (flashTimer !== undefined) window.clearTimeout(flashTimer);
+ };
}, [scrollContainerRef]);
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_quote.spec.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_quote.spec.ts
index 2fd73345ca..f634d8d1fc 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_quote.spec.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_quote.spec.ts
@@ -64,6 +64,11 @@ describe("stripQuoteSources", () => {
it("leaves text with no token untouched", () => {
expect(stripQuoteSources("> a normal quote\n\ntext")).toBe("> a normal quote\n\ntext");
});
+
+ it("keeps quoted content that merely contains a token but isn't an attribution footer", () => {
+ const md = "> the api uses <<mid:abc123>> as an anchor\n> (Quoted from <<mid:def456>>)\n\nq";
+ expect(stripQuoteSources(md)).toBe("> the api uses <<mid:abc123>> as an anchor\n\nq");
+ });
});
describe("renderQuoteSourceLinks", () => {
@@ -97,6 +102,11 @@ describe("renderQuoteSourceLinks", () => {
);
});
+ it("does not treat a quoted line that merely contains a token as a source", () => {
+ const md = "> mentions <<mid:abc123>> mid-sentence\n\ntext";
+ expect(renderQuoteSourceLinks(md, label)).toBe(md);
+ });
+
it("leaves surrounding non-quote text in place", () => {
const md = "before\n\n> q\n> (Quoted from <<mid:abc>>)\n\nafter";
expect(renderQuoteSourceLinks(md, label)).toBe(
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_quote.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_quote.ts
index a3e725bbfc..6069345289 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_quote.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_quote.ts
@@ -23,20 +23,22 @@ export const MESSAGE_JUMP_CLASS = "chat-message-jump";
/** Href scheme for a quote-source link; the referenced message id follows the prefix. */
export const QUOTE_SOURCE_HREF_PREFIX = "#mid-";
-// The message-id anchor embedded in a quote's attribution line. Locale-independent, so the transforms
-// below never depend on the translated "Quoted from" wrapper. Ids are `[A-Za-z0-9]` (from randomString).
-const MID_TOKEN = /<<mid:([A-Za-z0-9]+)>>/;
-const MID_TOKEN_GLOBAL = /<<mid:([A-Za-z0-9]+)>>/g;
+// A quote's attribution footer, exactly as `buildQuoteMarkdown` emits it: a blockquote line of the
+// shape `> (<label> <<mid:id>>)`. Anchoring to the whole line (not the bare `<<mid:…>>` token) keeps
+// quoted *content* that merely contains such a sequence from being mistaken for an attribution line.
+// The `<<mid:…>>` anchor is locale-independent, so the match never depends on the translated wrapper.
+// Ids are `[A-Za-z0-9]` (from randomString).
+const ATTRIBUTION_LINE = /^\s*>\s*\(.*<<mid:([A-Za-z0-9]+)>>\)\s*$/;
/**
- * Remove quote attribution lines (those carrying a `<<mid:…>>` token) from `markdown`, keeping the
- * quoted text. Used for anything that leaves the chat timeline — the LLM payload and saved notes —
- * where the message-id anchor is meaningless (and, to the LLM, distracting noise).
+ * Remove quote attribution lines from `markdown`, keeping the quoted text. Used for anything that
+ * leaves the chat timeline — the LLM payload and saved notes — where the message-id anchor is
+ * meaningless (and, to the LLM, distracting noise).
*/
export function stripQuoteSources(markdown: string): string {
return markdown
.split("\n")
- .filter(line => !MID_TOKEN.test(line))
+ .filter(line => !ATTRIBUTION_LINE.test(line))
.join("\n");
}
@@ -72,11 +74,16 @@ function isQuoteLine(line: string): boolean {
/** Apply the one/none/many rule to a single contiguous blockquote block. */
function rewriteQuoteBlock(block: string[], label: string): string[] {
- const ids = block.flatMap(line => [...line.matchAll(MID_TOKEN_GLOBAL)].map(match => match[1]));
- if (ids.length === 0) return block;
- if (ids.length > 1) {
- return block.filter(line => !MID_TOKEN.test(line)); // ambiguous → drop token lines, no link
+ const sources: { index: number; id: string }[] = [];
+ block.forEach((line, index) => {
+ const match = ATTRIBUTION_LINE.exec(line);
+ if (match) sources.push({ index, id: match[1] });
+ });
+ if (sources.length === 0) return block;
+ if (sources.length > 1) {
+ return block.filter(line => !ATTRIBUTION_LINE.test(line)); // ambiguous → drop, no link
}
- const link = `> [${label}](${QUOTE_SOURCE_HREF_PREFIX}${ids[0]})`;
- return block.map(line => (MID_TOKEN.test(line) ? link : line));
+ const { index, id } = sources[0];
+ const link = `> [${label}](${QUOTE_SOURCE_HREF_PREFIX}${id})`;
+ return block.map((line, i) => (i === index ? link : line));
}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
index 5d0080bb1a..6512ef68c4 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts
@@ -786,7 +786,9 @@ export function useLlmChat(
/** Regenerate the last reply: re-run from the last user message, dropping the reply that followed. */
const regenerateLastReply = useCallback(async () => {
- if (isStreaming) return;
+ // Re-check against the current state, not the snapshot from when the menu opened: only act while
+ // idle and while the last message is still an assistant reply to regenerate.
+ if (isStreaming || messages[messages.length - 1]?.role !== "assistant") return;
const conversation = conversationForRegenerate(messages);
if (!conversation) return;
pendingScrollRef.current = "anchor";