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
feat(resources): list the resources in the options
Elian Doran7db9be1050d5
diff --git a/apps/client/src/widgets/dialogs/resource_download.spec.tsx b/apps/client/src/widgets/dialogs/resource_download.spec.tsxindex 88a814310c..6d7efbd560 100644--- a/apps/client/src/widgets/dialogs/resource_download.spec.tsx+++ b/apps/client/src/widgets/dialogs/resource_download.spec.tsx@@ -36,7 +36,8 @@ const STATUS: ResourcesStatusResponse = {size: 700,downloadedSize: 200,sources: [ "huggingface.co", "github.com" ],- installed: false+ installed: false,+ directory: null} ],freeBytes: 10_000,download: nulldiff --git a/apps/client/src/widgets/type_widgets/ContentWidget.tsx b/apps/client/src/widgets/type_widgets/ContentWidget.tsxindex 5f0cb6bc37..26150978a5 100644--- a/apps/client/src/widgets/type_widgets/ContentWidget.tsx+++ b/apps/client/src/widgets/type_widgets/ContentWidget.tsx@@ -18,13 +18,14 @@ import InternationalizationOptions from "./options/i18n";import AdvancedSettings from "./options/advanced";import SecuritySettings from "./options/security";import TranscriptionSettings from "./options/transcription";+import ResourcesSettings from "./options/resources";import LlmSettings from "./options/llm";import "./ContentWidget.css";import { t } from "../../services/i18n";import BackendLog from "./code/BackendLog";import SpaceUsage from "./space_usage";-export type OptionPages = "_optionsAppearance" | "_optionsShortcuts" | "_optionsTextNotes" | "_optionsCodeNotes" | "_optionsContentManager" | "_optionsMedia" | "_optionsSpellcheck" | "_optionsTranscription" | "_optionsPassword" | "_optionsEtapi" | "_optionsBackup" | "_optionsDatabase" | "_optionsSync" | "_optionsDesktop" | "_optionsOther" | "_optionsLocalization" | "_optionsSecurity" | "_optionsAdvanced" | "_optionsLlm";+export type OptionPages = "_optionsAppearance" | "_optionsShortcuts" | "_optionsTextNotes" | "_optionsCodeNotes" | "_optionsContentManager" | "_optionsMedia" | "_optionsSpellcheck" | "_optionsTranscription" | "_optionsPassword" | "_optionsEtapi" | "_optionsBackup" | "_optionsDatabase" | "_optionsResources" | "_optionsSync" | "_optionsDesktop" | "_optionsOther" | "_optionsLocalization" | "_optionsSecurity" | "_optionsAdvanced" | "_optionsLlm";/** The page behind each of these notes. Exported for the options search, which renders them all. */export const CONTENT_WIDGETS: Record<OptionPages | "_backendLog" | "_spaceUsage", (props: TypeWidgetProps) => JSX.Element> = {@@ -40,6 +41,7 @@ export const CONTENT_WIDGETS: Record<OptionPages | "_backendLog" | "_spaceUsage"_optionsEtapi: EtapiSettings,_optionsBackup: BackupSettings,_optionsDatabase: DatabaseSettings,+ _optionsResources: ResourcesSettings,_optionsSync: SyncOptions,_optionsDesktop: DesktopSettings,_optionsOther: OtherSettings,diff --git a/apps/client/src/widgets/type_widgets/options/resources.css b/apps/client/src/widgets/type_widgets/options/resources.cssnew file mode 100644index 0000000000..9cd4a91ce1--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/resources.css@@ -0,0 +1,35 @@+.resources-list {+ .resources-item {+ gap: 12px;+ }++ .resources-icon {+ align-self: flex-start;+ font-size: 1.6em;+ color: var(--muted-text-color);+ }++ .resources-directory {+ word-break: break-all;+ }++ .resources-status {+ display: flex;+ flex-direction: column;+ flex-shrink: 0;+ align-items: flex-end;+ text-align: end;+ }++ .resources-size {+ font-variant-numeric: tabular-nums;+ white-space: nowrap;+ }++ .resources-total {+ .tn-card-option-title,+ .resources-size {+ font-weight: 600;+ }+ }+}diff --git a/apps/client/src/widgets/type_widgets/options/resources.spec.tsx b/apps/client/src/widgets/type_widgets/options/resources.spec.tsxnew file mode 100644index 0000000000..852b0f76c7--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/resources.spec.tsx@@ -0,0 +1,129 @@+import type { ResourceInfo, WebSocketMessage } from "@triliumnext/commons";+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const mocks = vi.hoisted(() => ({+ resources: [] as ResourceInfo[],+ requests: 0,+ handlers: [] as ((message: WebSocketMessage) => void)[]+}));++vi.mock("../../../services/server", () => ({+ default: {+ get: async (url: string) => {+ if (url !== "resources") {+ return [];+ }+ mocks.requests++;+ return { resources: mocks.resources, freeBytes: null, download: null };+ }+ }+}));++vi.mock("../../../services/ws", () => {+ const ws = {+ subscribeToMessages: (handler: (message: WebSocketMessage) => void) => {+ mocks.handlers.push(handler);+ },+ unsubscribeToMessage: (handler: (message: WebSocketMessage) => void) => {+ mocks.handlers = mocks.handlers.filter((h) => h !== handler);+ }+ };+ return { ...ws, default: ws };+});++vi.mock("../../../services/i18n", () => ({+ t: (key: string, args?: Record<string, string>) => args ? `${key} ${JSON.stringify(args)}` : key+}));++vi.mock("../../react/DirectoryLink", () => ({+ default: ({ directory }: { directory: string }) => <a className="directory-stub">{directory}</a>+}));++vi.mock("./components/OptionsPageHeader", () => ({ default: () => <div className="header-stub" /> }));++import ResourcesSettings from "./resources";++const MIB = 1024 * 1024;+let host: HTMLElement;++beforeEach(() => {+ mocks.resources = [+ resource({ id: "test.installed", installed: true, downloadedSize: 10 * MIB, directory: "/data/resources/test.installed-abc" }),+ resource({ id: "test.partial", downloadedSize: 4 * MIB }),+ resource({ id: "test.missing" })+ ];+ mocks.requests = 0;+ mocks.handlers = [];+ host = document.body.appendChild(document.createElement("div"));+});++afterEach(() => {+ act(() => render(null, host));+ document.body.innerHTML = "";+});++async function open() {+ await act(async () => {+ render(<ResourcesSettings />, host);+ });+}++describe("ResourcesSettings", () => {+ it("lists each resource with its status, the folder of an installed one, and the space used", async () => {+ await open();++ const rows = [ ...host.querySelectorAll(".resources-item") ];+ expect(rows.map((row) => row.querySelector(".tn-card-option-title")?.textContent))+ .toEqual([ "test.installed", "test.partial", "test.missing" ]);+ expect(rows.map((row) => row.querySelector(".resources-status small")?.textContent)).toEqual([+ "resources_options.installed",+ `resources_options.partially_downloaded {"downloaded":"4.00 MiB","size":"10 MiB"}`,+ "resources_options.not_downloaded"+ ]);+ expect(rows.map((row) => row.querySelector(".directory-stub")?.textContent ?? null))+ .toEqual([ "/data/resources/test.installed-abc", null, null ]);+ expect(host.querySelector(".resources-total .resources-size")?.textContent).toBe("14 MiB");+ });++ it("shows an empty state when no feature registers a resource", async () => {+ mocks.resources = [];+ await open();++ expect(host.querySelector(".resources-item")).toBeNull();+ expect(host.textContent).toContain("resources_options.no_resources");+ });++ it("reads the resources again once a download ends, but not while it runs", async () => {+ await open();+ expect(mocks.requests).toBe(1);++ const send = async (status: "downloading" | "succeeded") => {+ await act(async () => {+ for (const handler of mocks.handlers) {+ handler({ type: "resource-download", download: { status } } as WebSocketMessage);+ }+ });+ };+ await send("downloading");+ expect(mocks.requests).toBe(1);+ await send("succeeded");+ expect(mocks.requests).toBe(2);+ });+});++function resource(overrides: Partial<ResourceInfo> & { id: string }): ResourceInfo {+ return {+ title: overrides.id,+ description: "For tests",+ icon: "bx bx-test-tube",+ license: "MIT",+ size: 10 * MIB,+ downloadedSize: 0,+ sources: [ "example.com" ],+ installed: false,+ directory: null,+ ...overrides+ };+}diff --git a/apps/client/src/widgets/type_widgets/options/resources.tsx b/apps/client/src/widgets/type_widgets/options/resources.tsxnew file mode 100644index 0000000000..af10dc9de5--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/resources.tsx@@ -0,0 +1,118 @@+import "./resources.css";++import type { ResourceInfo, ResourcesStatusResponse, WebSocketMessage } from "@triliumnext/commons";+import { useEffect, useState } from "preact/hooks";++import { t } from "../../../services/i18n";+import server from "../../../services/server";+import { formatSize } from "../../../services/utils";+import { subscribeToMessages, unsubscribeToMessage } from "../../../services/ws";+import { Card, CardSection } from "../../react/Card";+import DirectoryLink from "../../react/DirectoryLink";+import Icon from "../../react/Icon";+import NoItems from "../../react/NoItems";+import OptionsPageHeader from "./components/OptionsPageHeader";++/** Lists the resources the features of this device can download, and which of them are installed. */+export default function ResourcesSettings() {+ const resources = useResources();++ return (+ <>+ <OptionsPageHeader />+ {resources && (+ <Card+ className="resources-list"+ heading={t("resources_options.list_title")}+ description={t("resources_options.list_description")}+ >+ {resources.length > 0 ? (+ <ResourceRows resources={resources} />+ ) : (+ <CardSection>+ <NoItems icon="bx bx-download" text={t("resources_options.no_resources")} size="small" />+ </CardSection>+ )}+ </Card>+ )}+ </>+ );+}++/** The registered resources, fetched again whenever a download of any of them ends. */+function useResources() {+ const [ resources, setResources ] = useState<ResourceInfo[]>();++ useEffect(() => {+ const refresh = () => {+ void server.get<ResourcesStatusResponse>("resources")+ .then((status) => setResources(status.resources));+ };+ const handler = (message: WebSocketMessage) => {+ if (message.type === "resource-download" && message.download.status !== "downloading") {+ refresh();+ }+ };+ refresh();+ subscribeToMessages(handler);+ return () => unsubscribeToMessage(handler);+ }, []);++ return resources;+}++function ResourceRows({ resources }: { resources: ResourceInfo[] }) {+ const used = resources.reduce((total, { downloadedSize }) => total + downloadedSize, 0);+ return (+ <>+ {resources.map((resource) => (+ <ResourceRow key={resource.id} resource={resource} />+ ))}+ <CardSection className="tn-card-option resources-total">+ <div className="tn-card-option-label">+ <span className="tn-card-option-title">{t("resources_options.space_used")}</span>+ </div>+ <span className="tn-card-option-value resources-size">{formatSize(used)}</span>+ </CardSection>+ </>+ );+}++function ResourceRow({ resource }: { resource: ResourceInfo }) {+ return (+ <CardSection className="tn-card-option resources-item">+ <Icon icon={resource.icon} className="resources-icon" />+ <div className="tn-card-option-label">+ <span className="tn-card-option-title">{resource.title}</span>+ <small className="tn-card-option-description">{resource.description}</small>+ <small className="tn-card-option-description">+ {t("resource_download.sources", { sources: resource.sources.join(", ") })}+ {" · "}+ {t("resource_download.license", { license: resource.license })}+ </small>+ {resource.directory && (+ <small className="tn-card-option-description resources-directory">+ <DirectoryLink directory={resource.directory} />+ </small>+ )}+ </div>+ <div className="tn-card-option-value resources-status">+ <span className="resources-size">{formatSize(resource.size)}</span>+ <small className="tn-card-option-description">{getStatusText(resource)}</small>+ </div>+ </CardSection>+ );+}++function getStatusText({ installed, size, downloadedSize }: ResourceInfo) {+ if (installed) {+ return t("resources_options.installed");+ }+ if (downloadedSize > 0) {+ return t("resources_options.partially_downloaded", {+ downloaded: formatSize(downloadedSize, 2, size),+ size: formatSize(size)+ });+ }+ return t("resources_options.not_downloaded");+}diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.jsonindex 2670b1a5ea..8f40fb6c2d 100644--- a/apps/server/src/assets/translations/en/server.json+++ b/apps/server/src/assets/translations/en/server.json@@ -331,7 +331,8 @@"tab-switcher-title": "Tab Switcher","sidebar-chat-title": "AI Chat","record-meeting-title": "Transcribe meeting",- "transcription-title": "Transcription"+ "transcription-title": "Transcription",+ "resources-title": "Downloaded resources"},"notes": {"new-note": "New note",diff --git a/apps/server/src/routes/api/resources.ts b/apps/server/src/routes/api/resources.tsindex af6be6faaf..75388981d7 100644--- a/apps/server/src/routes/api/resources.ts+++ b/apps/server/src/routes/api/resources.ts@@ -4,9 +4,12 @@ import type { Request } from "express";import resources from "../../services/resources.js";-/** `GET /api/resources?ids=a,b`: whether each resource is installed, and the download that runs. */+/**+ * `GET /api/resources?ids=a,b`: whether each resource is installed, and the download that runs.+ * Without `ids`, reports every registered resource.+ */function getStatus(req: Request): Promise<ResourcesStatusResponse> {- return resources.getStatus(parseIds(req.query.ids));+ return resources.getStatus(req.query.ids === undefined ? undefined : parseIds(req.query.ids));}/** `POST /api/resources/download` with `{ ids }`: starts downloading the resources not installed. */diff --git a/apps/server/src/services/resources.spec.ts b/apps/server/src/services/resources.spec.tsindex b3d6f72641..d6acc2b65d 100644--- a/apps/server/src/services/resources.spec.ts+++ b/apps/server/src/services/resources.spec.ts@@ -117,7 +117,8 @@ describe("ResourceManager", () => {size: MODEL.length + TOKENS.length,downloadedSize: MODEL.length + TOKENS.length,sources: [ "127.0.0.1" ],- installed: true+ installed: true,+ directory} ]);expect(download).toBeNull();expect(manager.startDownload([ "test.model" ]).status).toBe("succeeded");@@ -287,6 +288,18 @@ describe("ResourceManager", () => {expect(ranges[0]).toBeUndefined();});+ it("lists every registered resource, with the directory of the installed ones", async () => {+ manager.register(defineModel(), defineModel({ id: "test.other" }));+ manager.startDownload([ "test.model" ]);+ expect((await waitForEnd()).status).toBe("succeeded");++ const { resources } = await manager.getStatus();+ expect(resources.map(({ id, directory }) => ({ id, directory }))).toEqual([+ { id: "test.model", directory: manager.getPath("test.model") },+ { id: "test.other", directory: null }+ ]);+ });+it("prefers a local copy, and refuses invalid or unknown resources", () => {manager.register({ ...defineModel(), findLocalCopy: () => "/hand/installed" });expect(manager.getPath("test.model")).toBe("/hand/installed");diff --git a/apps/server/src/services/resources.ts b/apps/server/src/services/resources.tsindex 8783163e5a..debca32302 100644--- a/apps/server/src/services/resources.ts+++ b/apps/server/src/services/resources.ts@@ -97,12 +97,15 @@ export class ResourceManager {return isInstalled ? directory : null;}- async getStatus(ids: string[]): Promise<ResourcesStatusResponse> {- const definitions = ids.map((id) => this.getDefinition(id));+ /** Reports the resources `ids`, or every registered one when `ids` is left out. */+ async getStatus(ids?: string[]): Promise<ResourcesStatusResponse> {+ const definitions = ids+ ? ids.map((id) => this.getDefinition(id))+ : [ ...this.definitions.values() ];return {resources: definitions.map((definition) => this.getInfo(definition)),freeBytes: await this.getFreeBytes(),- download: this.findDownload(ids)?.progress ?? null+ download: this.findDownload(definitions.map(({ id }) => id))?.progress ?? null};}@@ -229,6 +232,7 @@ export class ResourceManager {? file.size: getPartialSize(destination, file));}, 0);+ const installedDirectory = this.getPath(definition.id);return {id: definition.id,title: definition.title(),@@ -238,7 +242,8 @@ export class ResourceManager {size: sumSizes(definition.files),downloadedSize,sources: [ ...new Set(definition.files.map(({ url }) => new URL(url).hostname)) ],- installed: this.getPath(definition.id) !== null+ installed: installedDirectory !== null,+ directory: installedDirectory};}diff --git a/packages/commons/src/lib/resources.ts b/packages/commons/src/lib/resources.tsindex a006c330d1..e297286af6 100644--- a/packages/commons/src/lib/resources.ts+++ b/packages/commons/src/lib/resources.ts@@ -20,6 +20,8 @@ export interface ResourceInfo {/** The host names its files are downloaded from. */sources: string[];installed: boolean;+ /** The directory holding its files while it is installed, else `null`. */+ directory: string | null;}/** How far a download has got with one of the resources it fetches. */diff --git a/packages/trilium-core/src/services/hidden_subtree.ts b/packages/trilium-core/src/services/hidden_subtree.tsindex 6b25c6b11c..59e430651a 100644--- a/packages/trilium-core/src/services/hidden_subtree.ts+++ b/packages/trilium-core/src/services/hidden_subtree.ts@@ -340,6 +340,7 @@ function buildHiddenSubtreeDefinition(helpSubtree: HiddenSubtreeItem[]): HiddenS{ id: "_optionsEtapi", title: t("hidden-subtree.etapi-title"), type: "contentWidget", icon: "bx-extension", attributes: [{ type: "label", name: "notInStandalone" }] },{ id: "_optionsBackup", title: t("hidden-subtree.backup-title"), type: "contentWidget", icon: "bx-data" },{ id: "_optionsDatabase", title: t("hidden-subtree.database-title"), type: "contentWidget", icon: "bx-hdd" },+ { id: "_optionsResources", title: t("hidden-subtree.resources-title"), type: "contentWidget", icon: "bx-download", attributes: [{ type: "label", name: "notInStandalone" }] },{ id: "_optionsSecurity", title: t("hidden-subtree.security-title"), type: "contentWidget", icon: "bx-shield" },{ id: "_optionsMFA", title: t("hidden-subtree.multi-factor-authentication-title"), type: "contentWidget", enforceDeleted: true },{ id: "_optionsPassword", title: t("hidden-subtree.password-title"), type: "contentWidget", icon: "bx-lock", attributes: [{ type: "label", name: "notInStandalone" }] },