Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): stop one providers card undoing the other's edits

Elian Doran4f66c6be0029
diff --git a/apps/client/src/widgets/type_widgets/options/llm.spec.tsx b/apps/client/src/widgets/type_widgets/options/llm.spec.tsx
index a9dae23510..5ac507f86e 100644
--- a/apps/client/src/widgets/type_widgets/options/llm.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/llm.spec.tsx
@@ -30,14 +30,21 @@ vi.mock("../../../services/i18n", () => ({
t: (key: string) => key
}));
-vi.mock("../../react/hooks", async (importOriginal) => ({
- ...(await importOriginal<typeof import("../../react/hooks")>()),
- useTriliumOption: (name: string) => [
- mocks.stored[name] ?? "",
- (value: string) => void mocks.saved.push([ name, value ])
- ],
- useTriliumOptionBool: (name: string) => [ mocks.stored[name] === "true", () => {} ]
-}));
+// Each call keeps its own copy of the value, as the real hook does until `entitiesReloaded` arrives.
+vi.mock("../../react/hooks", async (importOriginal) => {
+ const { useState } = await import("preact/hooks");
+ return {
+ ...(await importOriginal<typeof import("../../react/hooks")>()),
+ useTriliumOption: (name: string) => {
+ const [ value, setValue ] = useState(mocks.stored[name] ?? "");
+ return [ value, (newValue: string) => {
+ setValue(newValue);
+ mocks.saved.push([ name, newValue ]);
+ } ];
+ },
+ useTriliumOptionBool: (name: string) => [ mocks.stored[name] === "true", () => {} ]
+ };
+});
vi.mock("../../../services/dialog", () => ({ default: { confirm: mocks.confirm } }));
@@ -288,6 +295,29 @@ describe("the configured providers", () => {
const written = mocks.saved.find(([ name ]) => name === "llmProviders");
expect(JSON.parse(written?.[1] ?? "[]").map((provider: { id: string }) => provider.id)).toEqual([ "a" ]);
});
+
+ it("keeps an edit made in one card when the other card saves next", async () => {
+ withProviders([
+ { id: "a", name: "First", provider: "openai", apiKey: "sk" },
+ { id: "b", name: "Second", provider: "openai", apiKey: "sk" },
+ { id: "s", name: "Brave Search", provider: "brave", kind: "search", apiKey: "bk" }
+ ]);
+ open();
+
+ const removeButton = (name: string) => providers()
+ .find((option) => option.querySelector(".llm-provider-name")?.textContent === name)
+ ?.querySelectorAll(".tn-card-option-actions button")[1] as HTMLButtonElement | undefined;
+ const removeSearch = removeButton("Brave Search");
+ expect(removeSearch).toBeDefined();
+ await act(async () => removeSearch?.click());
+ const removeModel = removeButton("First");
+ expect(removeModel).toBeDefined();
+ await act(async () => removeModel?.click());
+
+ const writes = mocks.saved.filter(([ name ]) => name === "llmProviders");
+ expect(writes).toHaveLength(2);
+ expect(JSON.parse(writes[1][1]).map((provider: { id: string }) => provider.id)).toEqual([ "b" ]);
+ });
});
describe("MCP client configuration samples", () => {
diff --git a/apps/client/src/widgets/type_widgets/options/llm.tsx b/apps/client/src/widgets/type_widgets/options/llm.tsx
index b4335ade06..008f108618 100644
--- a/apps/client/src/widgets/type_widgets/options/llm.tsx
+++ b/apps/client/src/widgets/type_widgets/options/llm.tsx
@@ -23,6 +23,19 @@ import AddProviderModal, { findProviderType, type LlmProviderConfig } from "./ll
export default function LlmSettings() {
const [aiEnabled, setAiEnabled] = useTriliumOptionBool("aiEnabled");
+ // Read once for both cards: each saves the whole list, so a card holding its own copy would
+ // write back the other card's providers as they were before its last edit.
+ const [providersJson, setProvidersJson] = useTriliumOption("llmProviders");
+ const allProviders = useMemo<LlmProviderConfig[]>(() => {
+ try {
+ return providersJson ? JSON.parse(providersJson) : [];
+ } catch {
+ return [];
+ }
+ }, [providersJson]);
+ const setAllProviders = useCallback((newProviders: LlmProviderConfig[]) => {
+ setProvidersJson(JSON.stringify(newProviders));
+ }, [setProvidersJson]);
return (
<>
@@ -44,8 +57,8 @@ export default function LlmSettings() {
{aiEnabled && (
<>
- <ProviderSettings kind="llm" />
- <ProviderSettings kind="search" />
+ <ProviderSettings kind="llm" allProviders={allProviders} setAllProviders={setAllProviders} />
+ <ProviderSettings kind="search" allProviders={allProviders} setAllProviders={setAllProviders} />
<McpSettings />
</>
)}
@@ -54,19 +67,13 @@ export default function LlmSettings() {
}
/** The configured providers of one kind. Both kinds are stored in the `llmProviders` option. */
-function ProviderSettings({ kind }: { kind: LlmProviderKind }) {
- const [providersJson, setProvidersJson] = useTriliumOption("llmProviders");
- const allProviders = useMemo<LlmProviderConfig[]>(() => {
- try {
- return providersJson ? JSON.parse(providersJson) : [];
- } catch {
- return [];
- }
- }, [providersJson]);
+function ProviderSettings({ kind, allProviders, setAllProviders }: {
+ kind: LlmProviderKind;
+ /** Every configured provider, of both kinds. */
+ allProviders: LlmProviderConfig[];
+ setAllProviders: (providers: LlmProviderConfig[]) => void;
+}) {
const providers = useMemo(() => allProviders.filter(p => isProviderOfKind(p, kind)), [allProviders, kind]);
- const setAllProviders = useCallback((newProviders: LlmProviderConfig[]) => {
- setProvidersJson(JSON.stringify(newProviders));
- }, [setProvidersJson]);
// `undefined` while closed; the edited provider (or a fresh marker) while open.
// The bumping token keys the modal so it re-initializes its wizard on every open.
const [modalProvider, setModalProvider] = useState<LlmProviderConfig | undefined>();