Notes/apps/server/src/services/options_init.ts

277 lines
13 KiB
TypeScript
Raw Normal View History

2024-11-02 01:42:25 +02:00
import optionService from "./options.js";
import appInfo from "./app_info.js";
import { randomSecureToken, isWindows } from "./utils.js";
import log from "./log.js";
import dateUtils from "./date_utils.js";
import keyboardActions from "./keyboard_actions.js";
import type { KeyboardShortcutWithRequiredActionName, OptionMap, OptionNames } from "@triliumnext/commons";
import { DEFAULT_ALLOWED_TAGS } from "./html_sanitizer.js";
2020-06-20 12:31:38 +02:00
function initDocumentOptions() {
2025-01-09 18:07:02 +02:00
optionService.createOption("documentId", randomSecureToken(16), false);
optionService.createOption("documentSecret", randomSecureToken(16), false);
}
/**
* Contains additional options to be initialized for a new database, containing the information entered by the user.
*/
2024-02-18 13:32:00 +02:00
interface NotSyncedOpts {
syncServerHost?: string;
syncProxy?: string;
}
/**
* Represents a correspondence between an option and its default value, to be initialized when the database is missing that particular option (after a migration from an older version, or when creating a new database).
*/
2024-08-11 07:36:00 +03:00
interface DefaultOption {
2025-01-03 17:54:05 +02:00
name: OptionNames;
/**
2025-01-09 18:07:02 +02:00
* The value to initialize the option with, if the option is not already present in the database.
*
* If a function is passed Gin instead, the function is called if the option does not exist (with access to the current options) and the return value is used instead. Useful to migrate a new option with a value depending on some other option that might be initialized.
*/
value: string | ((options: OptionMap) => string);
2024-08-11 07:36:00 +03:00
isSynced: boolean;
}
/**
* Initializes the default options for new databases only.
*
* @param initialized `true` if the database has been fully initialized (i.e. a new database was created), or `false` if the database is created for sync.
* @param opts additional options to be initialized, for example the sync configuration.
*/
async function initNotSyncedOptions(initialized: boolean, opts: NotSyncedOpts = {}) {
2025-01-09 18:07:02 +02:00
optionService.createOption(
"openNoteContexts",
JSON.stringify([
{
notePath: "root",
active: true
}
]),
false
);
2025-01-09 18:07:02 +02:00
optionService.createOption("lastDailyBackupDate", dateUtils.utcNowDateTime(), false);
optionService.createOption("lastWeeklyBackupDate", dateUtils.utcNowDateTime(), false);
optionService.createOption("lastMonthlyBackupDate", dateUtils.utcNowDateTime(), false);
optionService.createOption("dbVersion", appInfo.dbVersion.toString(), false);
2025-01-09 18:07:02 +02:00
optionService.createOption("initialized", initialized ? "true" : "false", false);
2025-01-09 18:07:02 +02:00
optionService.createOption("lastSyncedPull", "0", false);
optionService.createOption("lastSyncedPush", "0", false);
2025-01-09 18:07:02 +02:00
optionService.createOption("theme", "next", false);
optionService.createOption("textNoteEditorType", "ckeditor-classic", true);
2025-01-09 18:07:02 +02:00
optionService.createOption("syncServerHost", opts.syncServerHost || "", false);
optionService.createOption("syncServerTimeout", "120000", false);
optionService.createOption("syncProxy", opts.syncProxy || "", false);
}
2024-11-02 00:55:45 +02:00
/**
* Contains all the default options that must be initialized on new and existing databases (at startup). The value can also be determined based on other options, provided they have already been initialized.
*/
2024-08-11 07:36:00 +03:00
const defaultOptions: DefaultOption[] = [
2025-01-09 18:07:02 +02:00
{ name: "revisionSnapshotTimeInterval", value: "600", isSynced: true },
{ name: "revisionSnapshotTimeIntervalTimeScale", value: "60", isSynced: true }, // default to Minutes
2025-01-09 18:07:02 +02:00
{ name: "revisionSnapshotNumberLimit", value: "-1", isSynced: true },
{ name: "protectedSessionTimeout", value: "600", isSynced: true },
{ name: "protectedSessionTimeoutTimeScale", value: "60", isSynced: true },
{ name: "zoomFactor", value: isWindows ? "0.9" : "1.0", isSynced: false },
2025-01-09 18:07:02 +02:00
{ name: "overrideThemeFonts", value: "false", isSynced: false },
{ name: "mainFontFamily", value: "theme", isSynced: false },
{ name: "mainFontSize", value: "100", isSynced: false },
{ name: "treeFontFamily", value: "theme", isSynced: false },
{ name: "treeFontSize", value: "100", isSynced: false },
{ name: "detailFontFamily", value: "theme", isSynced: false },
{ name: "detailFontSize", value: "110", isSynced: false },
{ name: "monospaceFontFamily", value: "theme", isSynced: false },
{ name: "monospaceFontSize", value: "110", isSynced: false },
{ name: "spellCheckEnabled", value: "true", isSynced: false },
{ name: "spellCheckLanguageCode", value: "en-US", isSynced: false },
{ name: "imageMaxWidthHeight", value: "2000", isSynced: true },
{ name: "imageJpegQuality", value: "75", isSynced: true },
{ name: "autoFixConsistencyIssues", value: "true", isSynced: false },
{ name: "vimKeymapEnabled", value: "false", isSynced: false },
{ name: "codeLineWrapEnabled", value: "true", isSynced: false },
{
name: "codeNotesMimeTypes",
value: '["text/x-csrc","text/x-c++src","text/x-csharp","text/css","text/x-go","text/x-groovy","text/x-haskell","text/html","message/http","text/x-java","application/javascript;env=frontend","application/javascript;env=backend","application/json","text/x-kotlin","text/x-markdown","text/x-perl","text/x-php","text/x-python","text/x-ruby",null,"text/x-sql","text/x-sqlite;schema=trilium","text/x-swift","text/xml","text/x-yaml","text/x-sh","application/typescript"]',
2025-01-09 18:07:02 +02:00
isSynced: true
},
{ name: "leftPaneWidth", value: "25", isSynced: false },
{ name: "leftPaneVisible", value: "true", isSynced: false },
{ name: "rightPaneWidth", value: "25", isSynced: false },
{ name: "rightPaneVisible", value: "true", isSynced: false },
{ name: "nativeTitleBarVisible", value: "false", isSynced: false },
{ name: "eraseEntitiesAfterTimeInSeconds", value: "604800", isSynced: true }, // default is 7 days
{ name: "eraseEntitiesAfterTimeScale", value: "86400", isSynced: true }, // default 86400 seconds = Day
2025-01-09 18:07:02 +02:00
{ name: "hideArchivedNotes_main", value: "false", isSynced: false },
{ name: "debugModeEnabled", value: "false", isSynced: false },
{ name: "headingStyle", value: "underline", isSynced: true },
{ name: "autoCollapseNoteTree", value: "true", isSynced: true },
{ name: "autoReadonlySizeText", value: "10000", isSynced: false },
{ name: "autoReadonlySizeCode", value: "30000", isSynced: false },
{ name: "dailyBackupEnabled", value: "true", isSynced: false },
{ name: "weeklyBackupEnabled", value: "true", isSynced: false },
{ name: "monthlyBackupEnabled", value: "true", isSynced: false },
{ name: "maxContentWidth", value: "1200", isSynced: false },
{ name: "compressImages", value: "true", isSynced: true },
{ name: "downloadImagesAutomatically", value: "true", isSynced: true },
{ name: "minTocHeadings", value: "5", isSynced: true },
{ name: "highlightsList", value: '["bold","italic","underline","color","bgColor"]', isSynced: true },
{ name: "checkForUpdates", value: "true", isSynced: true },
{ name: "disableTray", value: "false", isSynced: false },
{ name: "eraseUnusedAttachmentsAfterSeconds", value: "2592000", isSynced: true }, // default 30 days
{ name: "eraseUnusedAttachmentsAfterTimeScale", value: "86400", isSynced: true }, // default 86400 seconds = Day
2025-01-09 18:07:02 +02:00
{ name: "customSearchEngineName", value: "DuckDuckGo", isSynced: true },
{ name: "customSearchEngineUrl", value: "https://duckduckgo.com/?q={keyword}", isSynced: true },
{ name: "promotedAttributesOpenInRibbon", value: "true", isSynced: true },
{ name: "editedNotesOpenInRibbon", value: "true", isSynced: true },
{ name: "mfaEnabled", value: "false", isSynced: false },
2025-03-28 02:37:12 +01:00
{ name: "mfaMethod", value: "totp", isSynced: false },
{ name: "encryptedRecoveryCodes", value: "false", isSynced: false },
{ name: "userSubjectIdentifierSaved", value: "false", isSynced: false },
// Appearance
{ name: "splitEditorOrientation", value: "horizontal", isSynced: true },
{
name: "codeNoteTheme",
value: (optionsMap) => {
switch (optionsMap.theme) {
case "light":
case "next-light":
return "default:vs-code-light";
case "dark":
case "next-dark":
default:
return "default:vs-code-dark";
}
},
isSynced: false
},
// Internationalization
2025-01-09 18:07:02 +02:00
{ name: "locale", value: "en", isSynced: true },
{ name: "formattingLocale", value: "en", isSynced: true },
2025-01-09 18:07:02 +02:00
{ name: "firstDayOfWeek", value: "1", isSynced: true },
{ name: "firstWeekOfYear", value: "0", isSynced: true },
{ name: "minDaysInFirstWeek", value: "4", isSynced: true },
{ name: "languages", value: "[]", isSynced: true },
2024-11-02 00:55:45 +02:00
// Code block configuration
2025-01-09 18:07:02 +02:00
{
name: "codeBlockTheme",
value: (optionsMap) => {
if (optionsMap.theme === "light") {
return "default:stackoverflow-light";
} else {
return "default:stackoverflow-dark";
}
},
isSynced: false
},
2024-11-09 14:33:14 +02:00
{ name: "codeBlockWordWrap", value: "false", isSynced: true },
// Text note configuration
{ name: "textNoteEditorType", value: "ckeditor-balloon", isSynced: true },
{ name: "textNoteEditorMultilineToolbar", value: "false", isSynced: true },
// HTML import configuration
{ name: "layoutOrientation", value: "vertical", isSynced: false },
{ name: "backgroundEffects", value: "false", isSynced: false },
2025-01-09 18:07:02 +02:00
{
name: "allowedHtmlTags",
value: JSON.stringify(DEFAULT_ALLOWED_TAGS),
2025-01-09 18:07:02 +02:00
isSynced: true
},
// Share settings
2025-02-17 13:19:55 -07:00
{ name: "redirectBareDomain", value: "false", isSynced: true },
{ name: "showLoginInShareTheme", value: "false", isSynced: true },
// AI Options
{ name: "aiEnabled", value: "false", isSynced: true },
{ name: "openaiApiKey", value: "", isSynced: false },
2025-03-16 18:53:01 +00:00
{ name: "openaiDefaultModel", value: "gpt-4o", isSynced: true },
{ name: "openaiEmbeddingModel", value: "text-embedding-3-small", isSynced: true },
{ name: "openaiBaseUrl", value: "https://api.openai.com/v1", isSynced: true },
{ name: "anthropicApiKey", value: "", isSynced: false },
2025-03-16 18:53:01 +00:00
{ name: "anthropicDefaultModel", value: "claude-3-opus-20240229", isSynced: true },
2025-03-17 22:32:00 +00:00
{ name: "voyageEmbeddingModel", value: "voyage-2", isSynced: true },
{ name: "voyageApiKey", value: "", isSynced: false },
{ name: "anthropicBaseUrl", value: "https://api.anthropic.com/v1", isSynced: true },
{ name: "ollamaEnabled", value: "false", isSynced: true },
{ name: "ollamaDefaultModel", value: "llama3", isSynced: true },
2025-04-01 19:41:30 +00:00
{ name: "ollamaBaseUrl", value: "", isSynced: true },
{ name: "ollamaEmbeddingModel", value: "nomic-embed-text", isSynced: true },
2025-03-16 20:49:55 +00:00
{ name: "embeddingAutoUpdateEnabled", value: "true", isSynced: true },
// Adding missing AI options
{ name: "aiTemperature", value: "0.7", isSynced: true },
{ name: "aiSystemPrompt", value: "", isSynced: true },
{ name: "aiProviderPrecedence", value: "openai,anthropic,ollama", isSynced: true },
2025-04-01 20:38:03 +00:00
{ name: "embeddingDimensionStrategy", value: "auto", isSynced: true },
{ name: "embeddingProviderPrecedence", value: "openai,voyage,ollama,local", isSynced: true },
{ name: "embeddingSimilarityThreshold", value: "0.75", isSynced: true },
2025-03-16 18:53:01 +00:00
{ name: "enableAutomaticIndexing", value: "true", isSynced: true },
2025-04-01 20:38:03 +00:00
{ name: "maxNotesPerLlmQuery", value: "3", isSynced: true },
2025-03-16 18:53:01 +00:00
{ name: "embeddingBatchSize", value: "10", isSynced: true },
{ name: "embeddingUpdateInterval", value: "5000", isSynced: true },
{ name: "embeddingDefaultDimension", value: "1536", isSynced: true },
{ name: "embeddingGenerationLocation", value: "client", isSynced: true },
];
2024-11-02 00:55:45 +02:00
/**
* Initializes the options, by checking which options from {@link #allDefaultOptions()} are missing and registering them. It will also check some environment variables such as safe mode, to make any necessary adjustments.
*
2024-11-02 00:55:45 +02:00
* This method is called regardless of whether a new database is created, or an existing database is used.
*/
2020-06-20 12:31:38 +02:00
function initStartupOptions() {
2023-06-29 22:10:13 +02:00
const optionsMap = optionService.getOptionMap();
2019-11-18 23:01:31 +01:00
const allDefaultOptions = defaultOptions.concat(getKeyboardDefaultOptions());
2025-01-09 18:07:02 +02:00
for (const { name, value, isSynced } of allDefaultOptions) {
if (!(name in optionsMap)) {
let resolvedValue;
if (typeof value === "function") {
resolvedValue = value(optionsMap);
} else {
resolvedValue = value;
}
optionService.createOption(name, resolvedValue, isSynced);
log.info(`Created option "${name}" with default value "${resolvedValue}"`);
}
}
if (process.env.TRILIUM_START_NOTE_ID || process.env.TRILIUM_SAFE_MODE) {
2025-01-09 18:07:02 +02:00
optionService.setOption(
"openNoteContexts",
JSON.stringify([
{
notePath: process.env.TRILIUM_START_NOTE_ID || "root",
active: true
}
])
);
}
}
2019-11-18 23:01:31 +01:00
function getKeyboardDefaultOptions() {
2025-01-09 18:07:02 +02:00
return (keyboardActions.getDefaultKeyboardActions().filter((ka) => !!ka.actionName) as KeyboardShortcutWithRequiredActionName[]).map((ka) => ({
name: `keyboardShortcuts${ka.actionName.charAt(0).toUpperCase()}${ka.actionName.slice(1)}`,
value: JSON.stringify(ka.defaultShortcuts),
isSynced: false
})) as DefaultOption[];
2019-11-18 23:01:31 +01:00
}
export default {
initDocumentOptions,
initNotSyncedOptions,
initStartupOptions
2020-06-20 12:31:38 +02:00
};