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
client/options/AI & LLM: tweak
Adorian Doran40680ca89c91
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 76e635b150..917bcb235a 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -2846,7 +2846,7 @@ describe("Board column rename", () => {.map(element => element.textContent);expect(named).toEqual([ "Text", "Markdown", "Canvas", "Spreadsheet" ]);expect(templates?.querySelectorAll(".tn-sortable-grip").length).toBe(4);- expect(templates?.querySelectorAll(".tn-sortable-adder").length).toBe(2);+ expect(templates?.querySelectorAll(".tn-card-adder").length).toBe(2);// And the attributes the cards show, which the same dialog arranges.expect(dialog?.querySelector(".promoted-attributes-card")).toBeTruthy();diff --git a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsxindex 74d5d7ecd5..3aeaeb9583 100644--- a/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx+++ b/apps/client/src/widgets/react/PromotedAttributesCard.spec.tsx@@ -632,7 +632,7 @@ describe("PromotedAttributesCard", () => {}function adder() {- return container.querySelector<HTMLElement>(".tn-sortable-adder");+ return container.querySelector<HTMLElement>(".tn-card-adder");}/** The editor reporting a definition and being told to save it. */diff --git a/apps/client/src/widgets/react/SortableCard.css b/apps/client/src/widgets/react/SortableCard.cssindex 08a2d55706..c7b6f340be 100644--- a/apps/client/src/widgets/react/SortableCard.css+++ b/apps/client/src/widgets/react/SortableCard.css@@ -114,8 +114,14 @@}}+ .tn-sortable-appearing {+ animation: tn-sortable-appear 400ms ease-out;+ }+}++.tn-card {/* Transparent: each button paints itself, and the gaps show the card through. */- .tn-sortable-adders {+ .tn-card-section.tn-card-adders {display: flex;gap: var(--card-section-gap);border: none;@@ -125,7 +131,7 @@/* Painted as a segment rather than a button, all of one width, undoing what Bootstrap and thetheme give a button. */- button.tn-sortable-adder.btn {+ .tn-card-adders > button.tn-card-adder.btn {display: flex;flex: 1 1 0;justify-content: center;@@ -154,25 +160,21 @@}/* The row is transparent, so its buttons carry the card's corners. */- .tn-sortable-adders:first-of-type button.tn-sortable-adder:first-child {+ .tn-card-adders:first-of-type > button.tn-card-adder:first-child {border-start-start-radius: var(--card-border-radius);}- .tn-sortable-adders:first-of-type button.tn-sortable-adder:last-child {+ .tn-card-adders:first-of-type > button.tn-card-adder:last-child {border-start-end-radius: var(--card-border-radius);}- .tn-sortable-adders:last-of-type button.tn-sortable-adder:first-child {+ .tn-card-adders:last-of-type > button.tn-card-adder:first-child {border-end-start-radius: var(--card-border-radius);}- .tn-sortable-adders:last-of-type button.tn-sortable-adder:last-child {+ .tn-card-adders:last-of-type > button.tn-card-adder:last-child {border-end-end-radius: var(--card-border-radius);}-- .tn-sortable-appearing {- animation: tn-sortable-appear 400ms ease-out;- }}@keyframes tn-sortable-appear {diff --git a/apps/client/src/widgets/react/SortableCard.spec.tsx b/apps/client/src/widgets/react/SortableCard.spec.tsxindex 7779f8c15f..825b0f57a3 100644--- a/apps/client/src/widgets/react/SortableCard.spec.tsx+++ b/apps/client/src/widgets/react/SortableCard.spec.tsx@@ -433,7 +433,7 @@ describe("SortableCard", () => {describe("adding an entry", () => {it("draws no segment for adding unless it is given one", () => {draw();- expect(container.querySelector(".tn-sortable-adders")).toBeNull();+ expect(container.querySelector(".tn-card-adders")).toBeNull();draw({ itemCreationButtons: [ makes("d", "Delta") ] });expect(adder()?.textContent).toContain("Add");@@ -838,16 +838,16 @@ describe("SortableCard", () => {}function row() {- return container.querySelector<HTMLElement>(".tn-sortable-adders");+ return container.querySelector<HTMLElement>(".tn-card-adders");}/** The first of the buttons at the foot of the card, there being at least one in these. */function adder() {- return container.querySelector<HTMLElement>(".tn-sortable-adder");+ return container.querySelector<HTMLElement>(".tn-card-adder");}function adders() {- return [ ...container.querySelectorAll<HTMLElement>(".tn-sortable-adder") ];+ return [ ...container.querySelectorAll<HTMLElement>(".tn-card-adder") ];}/** A way of making one entry, named so a test can tell which button made what. */diff --git a/apps/client/src/widgets/react/SortableCard.tsx b/apps/client/src/widgets/react/SortableCard.tsxindex 1eadd255d7..8b95932a04 100644--- a/apps/client/src/widgets/react/SortableCard.tsx+++ b/apps/client/src/widgets/react/SortableCard.tsx@@ -1,7 +1,7 @@import "./SortableCard.css";import clsx from "clsx";-import { ComponentChildren } from "preact";+import { ComponentChildren, type RefObject } from "preact";import { flushSync } from "preact/compat";import {useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState@@ -57,18 +57,26 @@ export interface SortableItem {icon?: string;}+/** One button of an {@link ItemCreationRow}. */+export interface ItemCreationRowButton {+ label: string;+ /** An icon class, `bx` prefix included. */+ icon?: string;+ /** Turns the button off, for a caller that cannot make an entry yet. */+ disabled?: boolean;+ /** The button's `name` attribute, which tests find it by. */+ name?: string;+ onClick: (event: MouseEvent) => void;+}+/*** One way of creating an entry, rendered as a button at the foot of the card.** `onCreateItem` decides what the entry is, and can open a dialog or a picker first. Returning* nothing creates no entry.*/-export interface ItemCreationButton<T extends SortableItem> {- label: string;- /** An icon class, `bx` prefix included. */- icon?: string;- /** Turns the button off, for a caller that cannot make an entry yet. */- disabled?: boolean;+export interface ItemCreationButton<T extends SortableItem>+ extends Pick<ItemCreationRowButton, "label" | "icon" | "disabled"> {/** Answers with the entry to add, or with nothing where none was made. */onCreateItem: (event?: MouseEvent) => T | undefined | Promise<T | undefined>;}@@ -234,7 +242,7 @@ export function SortableCard<T extends SortableItem>({if (shown.length) {focusEntry(Math.min(focused.index, shown.length - 1));} else {- adderRef.current?.querySelector<HTMLElement>(".tn-sortable-adder")?.focus();+ adderRef.current?.querySelector<HTMLElement>(".tn-card-adder")?.focus();}});@@ -493,7 +501,7 @@ export function SortableCard<T extends SortableItem>({// Ctrl+Down moves nothing there.if (!control && event.key === "ArrowDown" && index === last) {event.preventDefault();- adderRef.current?.querySelector<HTMLElement>(".tn-sortable-adder")?.focus();+ adderRef.current?.querySelector<HTMLElement>(".tn-card-adder")?.focus();}return;@@ -610,36 +618,58 @@ export function SortableCard<T extends SortableItem>({</section>))}- {/* Transparent: each button paints itself, and the gaps show the card through. */}{!!itemCreationButtons?.length && (- <section- ref={adderRef}- className="tn-card-section tn-sortable-adders"+ <ItemCreationRow+ elementRef={adderRef}onKeyDown={onAddKeyDown}- >- {itemCreationButtons.map((button, index) => (- <Button- // The position in the row: two buttons can share a label.- key={index}- // Not a command button: the theme gives those a background, a- // shadow and a minimum width, none of which suit a segment.- kind="lowProfile"- className="tn-sortable-adder"- text={<>- {button.icon && <Icon icon={button.icon} />}- <span>{button.label}</span>- </>}- disabled={button.disabled}- onClick={(event) => add(button.onCreateItem, undefined, event)}- />- ))}- </section>+ buttons={itemCreationButtons.map((button) => ({+ label: button.label,+ icon: button.icon,+ disabled: button.disabled,+ onClick: (event) => add(button.onCreateItem, undefined, event)+ }))}+ />)}</div></Card>);}+interface ItemCreationRowProps {+ buttons: ItemCreationRowButton[];+ elementRef?: RefObject<HTMLElement | null>;+ onKeyDown?: (event: KeyboardEvent) => void;+}++/**+ * The buttons that create an entry, as the last section of a card. Each is painted as a segment of+ * the card rather than as a button, so the row reads as the foot of the list above it.+ */+export function ItemCreationRow({ buttons, elementRef, onKeyDown }: ItemCreationRowProps) {+ return (+ // Transparent: each button paints itself, and the gaps show the card through.+ <section ref={elementRef} className="tn-card-section tn-card-adders" onKeyDown={onKeyDown}>+ {buttons.map((button, index) => (+ <Button+ // The position in the row: two buttons can share a label.+ key={index}+ name={button.name}+ // Not a command button: the theme gives those a background, a shadow and a+ // minimum width, none of which suit a segment.+ kind="lowProfile"+ className="tn-card-adder"+ text={<>+ {button.icon && <Icon icon={button.icon} />}+ <span>{button.label}</span>+ </>}+ disabled={button.disabled}+ onClick={button.onClick}+ />+ ))}+ </section>+ );+}+/** The entry whose segment holds the focus, directly or through a control, and its index. */function focusedEntry(list: HTMLElement | null, shown: SortableItem[]) {const segment = document.activeElement?.closest<HTMLElement>(".tn-sortable-segment");diff --git a/apps/client/src/widgets/react/TemplateSelectionCard.spec.tsx b/apps/client/src/widgets/react/TemplateSelectionCard.spec.tsxindex 492f92b9f6..4bf5485c71 100644--- a/apps/client/src/widgets/react/TemplateSelectionCard.spec.tsx+++ b/apps/client/src/widgets/react/TemplateSelectionCard.spec.tsx@@ -400,7 +400,7 @@ describe("TemplateSelectionCard", () => {}function adders() {- return [ ...container.querySelectorAll<HTMLElement>(".tn-sortable-adder") ];+ return [ ...container.querySelectorAll<HTMLElement>(".tn-card-adder") ];}async function add(label: string) {diff --git a/apps/client/src/widgets/type_widgets/options/code_mime_types_card.spec.tsx b/apps/client/src/widgets/type_widgets/options/code_mime_types_card.spec.tsxindex df65de2222..5d11f4209d 100644--- a/apps/client/src/widgets/type_widgets/options/code_mime_types_card.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/code_mime_types_card.spec.tsx@@ -114,7 +114,7 @@ describe("CodeMimeTypesCard", () => {const picking = vi.spyOn(dialog, "pickSingleItem").mockResolvedValue(null);const container = draw();- await click(container.querySelector(".tn-sortable-adder"));+ await click(container.querySelector(".tn-card-adder"));const items = picking.mock.calls.at(-1)?.[0].items as PickerItem[];const keys = items.map((item) => item.key);@@ -137,19 +137,19 @@ describe("CodeMimeTypesCard", () => {.mockResolvedValue({ key: PYTHON, caption: "Python" });const container = draw();- await click(container.querySelector(".tn-sortable-adder"));+ await click(container.querySelector(".tn-card-adder"));expect(stored()).toEqual([ PLAIN, JAVA, FRONTEND, C, PYTHON ]);expect(captions(container)).toEqual([ "Plain text", "C", "Java", "JavaScript (Trilium frontend)", "Python" ]);- expect(container.querySelector(".tn-sortable-adder")?.textContent?.trim())+ expect(container.querySelector(".tn-card-adder")?.textContent?.trim()).toBe(`code_mime_types.add_another ${MIME_TYPES_DICT.length - 5}`);});it("says how many languages are left to add", () => {const container = draw();- expect(container.querySelector(".tn-sortable-adder")?.textContent?.trim())+ expect(container.querySelector(".tn-card-adder")?.textContent?.trim()).toBe(`code_mime_types.add_another ${MIME_TYPES_DICT.length - 4}`);});});diff --git a/apps/client/src/widgets/type_widgets/options/content_languages_card.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_languages_card.spec.tsxindex 4cd7852aed..45bbcc8c5a 100644--- a/apps/client/src/widgets/type_widgets/options/content_languages_card.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/content_languages_card.spec.tsx@@ -54,7 +54,7 @@ describe("ContentLanguagesCard", () => {const picking = vi.spyOn(dialog, "pickSingleItem").mockResolvedValue(null);const container = draw();- await click(container.querySelector(".tn-sortable-adder"));+ await click(container.querySelector(".tn-card-adder"));const items = picking.mock.calls.at(-1)?.[0].items as PickerItem[];const keys = items.map((item) => item.key);@@ -71,7 +71,7 @@ describe("ContentLanguagesCard", () => {.mockResolvedValue({ key: "it", caption: "Italiano" });const container = draw();- await click(container.querySelector(".tn-sortable-adder"));+ await click(container.querySelector(".tn-card-adder"));expect(stored()).toEqual([ "ro", "de", "fr", "it" ]);expect(names(container)).toEqual([ "Deutsch", "Français", "Italiano", "Română" ]);@@ -83,7 +83,7 @@ describe("ContentLanguagesCard", () => {.mockReturnValue(new Promise((resolve) => { answer = resolve; }));const container = draw();- await click(container.querySelector(".tn-sortable-adder"));+ await click(container.querySelector(".tn-card-adder"));act(() => {options.set("languages", JSON.stringify([ "ro", "de" ]));reloadOption("languages");@@ -96,11 +96,11 @@ describe("ContentLanguagesCard", () => {});it("says how many languages are left to add, as the first one or another", () => {- expect(draw().querySelector(".tn-sortable-adder")?.textContent?.trim())+ expect(draw().querySelector(".tn-card-adder")?.textContent?.trim()).toBe(`content_language.add_another ${LOCALES.length - 3}`);options.set("languages", "[]");- expect(draw().querySelector(".tn-sortable-adder")?.textContent?.trim())+ expect(draw().querySelector(".tn-card-adder")?.textContent?.trim()).toBe(`content_language.add_first ${LOCALES.length}`);});});diff --git a/apps/client/src/widgets/type_widgets/options/llm.css b/apps/client/src/widgets/type_widgets/options/llm.cssindex 4395cdca27..e87b8adb94 100644--- a/apps/client/src/widgets/type_widgets/options/llm.css+++ b/apps/client/src/widgets/type_widgets/options/llm.css@@ -4,10 +4,6 @@gap: 0.5em;}-.llm-add-provider {- text-align: end;-}-/* One line, cut off at the row's width; the full list is in its tooltip. */.llm-provider-models {display: block;diff --git a/apps/client/src/widgets/type_widgets/options/llm.spec.tsx b/apps/client/src/widgets/type_widgets/options/llm.spec.tsxindex 41f07fa916..f673591fc4 100644--- a/apps/client/src/widgets/type_widgets/options/llm.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/llm.spec.tsx@@ -239,6 +239,18 @@ describe("the configured providers", () => {expect(providers()).toHaveLength(0);});+ it("adds from a creation row at the foot of each card, as the other lists do", () => {+ withProviders([ { id: "a", name: "My OpenAI", provider: "openai", apiKey: "sk" } ]);+ open();++ for (const name of [ "add-llm-provider-button", "add-search-provider-button" ]) {+ const row = host.querySelector(`button.tn-card-adder[name='${name}']`)+ ?.closest(".tn-card-adders");+ expect(row).toBeInstanceOf(HTMLElement);+ expect(row?.parentElement?.lastElementChild).toBe(row);+ }+ });+it("survives a stored list that is not readable, rather than taking the page down with it", () => {mocks.stored = { ...mocks.stored, llmProviders: "{ not json" };open();diff --git a/apps/client/src/widgets/type_widgets/options/llm.tsx b/apps/client/src/widgets/type_widgets/options/llm.tsxindex a38ff6a143..90d80500a1 100644--- a/apps/client/src/widgets/type_widgets/options/llm.tsx+++ b/apps/client/src/widgets/type_widgets/options/llm.tsx@@ -8,7 +8,6 @@ import { t } from "../../../services/i18n";import server from "../../../services/server";import { isStandalone } from "../../../services/utils";import ActionButton from "../../react/ActionButton";-import Button from "../../react/Button";import { Card, CardSection, OptionCardSection } from "../../react/Card";import CodeBlock from "../../react/CodeBlock";import Collapsible from "../../react/Collapsible";@@ -17,6 +16,7 @@ import FormToggle from "../../react/FormToggle";import { useTriliumOption, useTriliumOptionBool } from "../../react/hooks";import Icon from "../../react/Icon";import NoItems from "../../react/NoItems";+import { ItemCreationRow } from "../../react/SortableCard";import { shortModelName } from "../llm_chat/model_name";import OptionsPageHeader from "./components/OptionsPageHeader";import AddProviderModal, { findProviderType, type LlmProviderConfig } from "./llm/AddProviderModal";@@ -112,14 +112,12 @@ function ProviderSettings({ kind, allProviders, setAllProviders }: {onDelete={handleDeleteProvider}/>- <CardSection className="llm-add-provider">- <Button- name={isSearch ? "add-search-provider-button" : "add-llm-provider-button"}- size="micro" icon="bx-plus"- text={isSearch ? t("llm.add_search_provider") : t("llm.add_model_provider")}- onClick={() => openModal()}- />- </CardSection>+ <ItemCreationRow buttons={[ {+ name: isSearch ? "add-search-provider-button" : "add-llm-provider-button",+ label: isSearch ? t("llm.add_search_provider") : t("llm.add_model_provider"),+ icon: "bx bx-plus",+ onClick: () => openModal()+ } ]} /></Card><AddProviderModal