Skip to content

Build your personal knowledge base with Trilium Notes

13.2 KB
Raw Blame
import attributeService from "./attributes.js";
import dateNoteService from "./date_notes.js";
import becca from "../becca/becca.js";
import noteService from "./notes.js";
import dateUtils from "./utils/date.js";
import { getLog } from "./log.js";
import hoistedNoteService from "./hoisted_note.js";
import searchService from "./search/services/search.js";
import SearchContext from "./search/search_context.js";
import { LBTPL_NOTE_LAUNCHER, LBTPL_CUSTOM_WIDGET, LBTPL_SPACER, LBTPL_SCRIPT } from "./hidden_subtree.js";
import { t } from "i18next";
import BNote from '../becca/entities/bnote.js';
import {
    InboxTargetKind, InboxTargetResponse, SaveLlmChatResponse, SaveSearchNoteResponse,
    SaveSqlConsoleResponse
} from "@triliumnext/commons";

function getInboxNote(date: string) {
    const { note } = resolveInboxTarget();

    return note ?? dateNoteService.getDayNote(date);
}

/**
 * Decides where a quickly captured note goes. Creates nothing, unlike `getInboxNote()`, which
 * creates the day note it falls back to. Use this to name the destination without capturing.
 */
function resolveInboxTarget(): { kind: InboxTargetKind; note?: BNote } {
    const workspaceNote = hoistedNoteService.getWorkspaceNote();
    if (!workspaceNote) {
        throw new Error("Unable to find workspace note");
    }

    if (!workspaceNote.isRoot()) {
        const workspaceInbox = workspaceNote.searchNoteInSubtree("#workspaceInbox");
        if (workspaceInbox) {
            return { kind: "workspaceInbox", note: workspaceInbox };
        }

        const inbox = workspaceNote.searchNoteInSubtree("#inbox");
        if (inbox) {
            return { kind: "inbox", note: inbox };
        }

        // Capture into today's note when this workspace has its own journal.
        if (workspaceNote.searchNoteInSubtree("#workspaceCalendarRoot")) {
            return { kind: "dayNote" };
        }

        return { kind: "workspaceRoot", note: workspaceNote };
    }

    const inbox = attributeService.getNoteWithLabel("inbox");
    if (inbox) {
        return { kind: "inbox", note: inbox };
    }

    // Capturing a note is not a request for a journal, so a database without one keeps the note
    // at the top level rather than having a calendar built around it (#11034).
    if (dateNoteService.hasCalendarRoot()) {
        return { kind: "dayNote" };
    }

    return { kind: "root", note: workspaceNote };
}

function getInboxTarget(): InboxTargetResponse {
    const { kind, note } = resolveInboxTarget();

    return { kind, noteId: note?.noteId, title: note?.getTitleOrProtected() };
}

function createSqlConsole() {
    const { note } = noteService.createNewNote({
        parentNoteId: getMonthlyParentNoteId("_sqlConsole", "sqlConsole"),
        title: "SQL Console - " + dateUtils.localNowDate(),
        content: "SELECT title, isDeleted, isProtected FROM notes WHERE noteId = ''\n\n\n\n",
        type: "code",
        mime: "text/x-sqlite;schema=trilium"
    });

    note.setLabel("iconClass", "bx bx-data");
    note.setLabel("keepCurrentHoisting");

    return note;
}

async function saveSqlConsole(sqlConsoleNoteId: string) {
    const sqlConsoleNote = becca.getNote(sqlConsoleNoteId);
    if (!sqlConsoleNote) throw new Error(`Unable to find SQL console note ID: ${sqlConsoleNoteId}`);
    const today = dateUtils.localNowDate();

    const sqlConsoleHome = attributeService.getNoteWithLabel("sqlConsoleHome") || await dateNoteService.getDayNote(today);

    const result = sqlConsoleNote.cloneTo(sqlConsoleHome.noteId);

    for (const parentBranch of sqlConsoleNote.getParentBranches()) {
        if (parentBranch.parentNote?.hasAncestor("_hidden")) {
            parentBranch.markAsDeleted();
        }
    }

    return result satisfies SaveSqlConsoleResponse;
}

function createSearchNote(searchString: string, ancestorNoteId: string) {
    const { note } = noteService.createNewNote({
        parentNoteId: getMonthlyParentNoteId("_search", "search"),
        title: `${t("special_notes.search_prefix")} ${searchString}`,
        content: "",
        type: "search",
        mime: "application/json"
    });

    note.setLabel("searchString", searchString);
    note.setLabel("keepCurrentHoisting");

    if (ancestorNoteId) {
        note.setRelation("ancestor", ancestorNoteId);
    }

    return note;
}

function getSearchHome() {
    const workspaceNote = hoistedNoteService.getWorkspaceNote();
    if (!workspaceNote) {
        throw new Error("Unable to find workspace note");
    }

    if (!workspaceNote.isRoot()) {
        return workspaceNote.searchNoteInSubtree("#workspaceSearchHome") || workspaceNote.searchNoteInSubtree("#searchHome") || workspaceNote;
    } else {
        const today = dateUtils.localNowDate();

        return workspaceNote.searchNoteInSubtree("#searchHome") || dateNoteService.getDayNote(today);
    }
}

function saveSearchNote(searchNoteId: string) {
    const searchNote = becca.getNote(searchNoteId);
    if (!searchNote) {
        throw new Error("Unable to find search note");
    }

    const searchHome = getSearchHome();

    const result = searchNote.cloneTo(searchHome.noteId);

    for (const parentBranch of searchNote.getParentBranches()) {
        if (parentBranch.parentNote?.hasAncestor("_hidden")) {
            parentBranch.markAsDeleted();
        }
    }

    return result satisfies SaveSearchNoteResponse;
}

function getMonthlyParentNoteId(rootNoteId: string, prefix: string) {
    const month = dateUtils.localNowDate().substring(0, 7);
    const labelName = `${prefix}MonthNote`;

    let monthNote = searchService.findFirstNoteWithQuery(`#${labelName}="${month}"`, new SearchContext({ ancestorNoteId: rootNoteId }));

    if (!monthNote) {
        monthNote = noteService.createNewNote({
            parentNoteId: rootNoteId,
            title: month,
            content: "",
            isProtected: false,
            type: "book"
        }).note;

        monthNote.addLabel(labelName, month);
    }

    return monthNote.noteId;
}

function createScriptLauncher(parentNoteId: string, forceNoteId?: string) {
    const note = noteService.createNewNote({
        noteId: forceNoteId,
        title: "Script Launcher",
        type: "launcher",
        content: "",
        parentNoteId: parentNoteId
    }).note;

    note.addRelation("template", LBTPL_SCRIPT);
    return note;
}

export type LauncherType = "launcher" | "note" | "script" | "customWidget" | "spacer";

interface LauncherConfig {
    parentNoteId: string;
    launcherType: LauncherType;
    noteId?: string;
}

function createLauncher({ parentNoteId, launcherType, noteId }: LauncherConfig) {
    let note;

    if (launcherType === "note") {
        note = noteService.createNewNote({
            noteId: noteId,
            title: "Note Launcher",
            type: "launcher",
            content: "",
            parentNoteId: parentNoteId
        }).note;

        note.addRelation("template", LBTPL_NOTE_LAUNCHER);
    } else if (launcherType === "script") {
        note = createScriptLauncher(parentNoteId, noteId);
    } else if (launcherType === "customWidget") {
        note = noteService.createNewNote({
            noteId: noteId,
            title: "Widget Launcher",
            type: "launcher",
            content: "",
            parentNoteId: parentNoteId
        }).note;

        note.addRelation("template", LBTPL_CUSTOM_WIDGET);
    } else if (launcherType === "spacer") {
        note = noteService.createNewNote({
            noteId: noteId,
            branchId: noteId,
            title: "Spacer",
            type: "launcher",
            content: "",
            parentNoteId: parentNoteId
        }).note;

        note.addRelation("template", LBTPL_SPACER);
    } else {
        throw new Error(`Unrecognized launcher type '${launcherType}'`);
    }

    return {
        success: true,
        note
    };
}

function resetLauncher(noteId: string) {
    const note = becca.getNote(noteId);

    if (note?.isLaunchBarConfig()) {
        if (note) {
            if (noteId === "_lbRoot" || noteId === "_lbMobileRoot") {
                // deleting hoisted notes are not allowed, so we just reset the children
                for (const childNote of note.getChildNotes()) {
                    childNote.deleteNote();
                }
            } else {
                note.deleteNote();
            }
        } else {
            getLog().info(`Note ${noteId} has not been found and cannot be reset.`);
        }
    } else {
        getLog().info(`Note ${noteId} is not a resettable launcher note.`);
    }

    // the re-building deleted launchers will be done in handlers
}

/**
 * This exists to ease transition into the new launchbar, but it's not meant to be a permanent functionality.
 * Previously, the launchbar was fixed and the only way to add buttons was through this API, so a lot of buttons have been
 * created just to fill this user hole.
 *
 * Another use case was for script-packages (e.g. demo Task manager) which could this way register automatically/easily
 * into the launchbar - for this it's recommended to use backend API's createOrUpdateLauncher()
 */
function createOrUpdateScriptLauncherFromApi(opts: { id: string; title: string; action: string; icon?: string; shortcut?: string }) {
    if (opts.id && !/^[a-z0-9]+$/i.test(opts.id)) {
        throw new Error(`Launcher ID can be alphanumeric only, '${opts.id}' given`);
    }

    const launcherId = opts.id || `tb_${opts.title.toLowerCase().replace(/[^[a-z0-9]/gi, "")}`;

    if (!opts.title) {
        throw new Error("Title is mandatory property to create or update a launcher.");
    }

    const launcherNote = becca.getNote(launcherId) || createScriptLauncher("_lbVisibleLaunchers", launcherId);

    launcherNote.title = opts.title;
    launcherNote.setContent(`(${opts.action})()`);
    launcherNote.setLabel("scriptInLauncherContent"); // there's no target note, the script is in the launcher's content
    launcherNote.mime = "application/javascript;env=frontend";
    launcherNote.save();

    if (opts.shortcut) {
        launcherNote.setLabel("keyboardShortcut", opts.shortcut);
    } else {
        launcherNote.removeLabel("keyboardShortcut");
    }

    if (opts.icon) {
        launcherNote.setLabel("iconClass", `bx bx-${opts.icon}`);
    } else {
        launcherNote.removeLabel("iconClass");
    }

    return launcherNote;
}

/**
 * Gets the most recently modified LLM chat note.
 * Used by sidebar chat to persist conversation across page refreshes.
 * Returns null if no chat exists.
 */
function getMostRecentLlmChat() {
    // Search for all llmChat notes and return the most recently modified
    const results = searchService.searchNotes(
        "note.type = llmChat",
        new SearchContext({
            ancestorNoteId: "_llmChat",
            limit: 1,
            orderBy: "utcDateModified",
            orderDirection: "desc"
        })
    );

    return results.length > 0 ? results[0] : null;
}

/**
 * Gets a list of recent LLM chat notes.
 * Used by sidebar chat history popup.
 */
function getRecentLlmChats(limit: number = 10) {
    const results = searchService.searchNotes(
        "note.type = llmChat",
        new SearchContext({
            ancestorNoteId: "_llmChat",
            limit,
            orderBy: "utcDateModified",
            orderDirection: "desc"
        })
    );

    return results.map(note => ({
        noteId: note.noteId,
        title: note.title,
        dateModified: note.utcDateModified
    }));
}

function createLlmChat() {
    const { note } = noteService.createNewNote({
        parentNoteId: getMonthlyParentNoteId("_llmChat", "llmChat"),
        title: `${t("special_notes.llm_chat_prefix")} ${dateUtils.localNowDateTime()}`,
        content: JSON.stringify({
            version: 1,
            messages: []
        }),
        type: "llmChat",
        mime: "application/json"
    });

    note.setLabel("iconClass", "bx bx-message-square-dots");
    note.setLabel("keepCurrentHoisting");

    return note;
}

function getLlmChatHome() {
    const workspaceNote = hoistedNoteService.getWorkspaceNote();
    if (!workspaceNote) {
        throw new Error("Unable to find workspace note");
    }

    if (!workspaceNote.isRoot()) {
        return workspaceNote.searchNoteInSubtree("#workspaceLlmChatHome") || workspaceNote.searchNoteInSubtree("#llmChatHome") || workspaceNote;
    }
    const today = dateUtils.localNowDate();

    return workspaceNote.searchNoteInSubtree("#llmChatHome") || dateNoteService.getDayNote(today);
}

function saveLlmChat(llmChatNoteId: string | null) {
    if (!llmChatNoteId) {
        throw new Error(`Missing chat note ID`);
    }

    const llmChatNote = becca.getNote(llmChatNoteId);
    if (!llmChatNote) {
        throw new Error(`Unable to find LLM chat note ID: ${llmChatNoteId}`);
    }

    const llmChatHome = getLlmChatHome();

    const result = llmChatNote.cloneTo(llmChatHome.noteId);

    for (const parentBranch of llmChatNote.getParentBranches()) {
        if (parentBranch.parentNote?.hasAncestor("_hidden")) {
            parentBranch.markAsDeleted();
        }
    }

    return result satisfies SaveLlmChatResponse;
}

export default {
    getInboxNote,
    getInboxTarget,
    createSqlConsole,
    saveSqlConsole,
    createSearchNote,
    saveSearchNote,
    createLauncher,
    resetLauncher,
    createOrUpdateScriptLauncherFromApi,
    createLlmChat,
    getMostRecentLlmChat,
    getRecentLlmChats,
    saveLlmChat
};