2024-11-02 00:55:45 +02:00
|
|
|
/**
|
|
|
|
* @module
|
2024-12-22 15:42:15 +02:00
|
|
|
*
|
2024-11-02 00:55:45 +02:00
|
|
|
* Options are key-value pairs that are used to store information such as user preferences (for example
|
|
|
|
* the current theme, sync server information), but also information about the state of the application.
|
2024-12-22 15:42:15 +02:00
|
|
|
*
|
2024-11-02 00:55:45 +02:00
|
|
|
* Although options internally are represented as strings, their value can be interpreted as a number or
|
|
|
|
* boolean by calling the appropriate methods from this service (e.g. {@link #getOptionInt}).\
|
2024-12-22 15:42:15 +02:00
|
|
|
*
|
2024-11-02 00:55:45 +02:00
|
|
|
* Generally options are shared across multiple instances of the application via the sync mechanism,
|
|
|
|
* however it is possible to have options that are local to an instance. For example, the user can select
|
|
|
|
* a theme on a device and it will not affect other devices.
|
|
|
|
*/
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import becca from "../becca/becca.js";
|
2024-07-18 22:30:16 +03:00
|
|
|
import BOption from "../becca/entities/boption.js";
|
2025-04-18 12:33:50 +03:00
|
|
|
import type { OptionRow } from "@triliumnext/commons";
|
2025-04-18 12:39:52 +03:00
|
|
|
import type { FilterOptionsByType, OptionDefinitions, OptionMap, OptionNames } from "@triliumnext/commons";
|
2024-07-18 21:35:17 +03:00
|
|
|
import sql from "./sql.js";
|
2021-05-01 21:52:22 +02:00
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function getOptionOrNull(name: OptionNames): string | null {
|
2022-01-02 21:20:56 +01:00
|
|
|
let option;
|
|
|
|
|
|
|
|
if (becca.loaded) {
|
|
|
|
option = becca.getOption(name);
|
2022-01-12 19:32:23 +01:00
|
|
|
} else {
|
2022-01-02 21:20:56 +01:00
|
|
|
// e.g. in initial sync becca is not loaded because DB is not initialized
|
2025-03-26 18:41:36 +02:00
|
|
|
try {
|
|
|
|
option = sql.getRow<OptionRow>("SELECT * FROM options WHERE name = ?", [name]);
|
|
|
|
} catch (e: unknown) {
|
|
|
|
// DB is not initialized.
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-02 21:20:56 +01:00
|
|
|
}
|
2023-05-04 22:16:18 +02:00
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
return option ? option.value : null;
|
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function getOption(name: OptionNames) {
|
2022-01-12 19:32:23 +01:00
|
|
|
const val = getOptionOrNull(name);
|
2017-11-02 20:48:02 -04:00
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
if (val === null) {
|
2023-05-05 23:17:23 +02:00
|
|
|
throw new Error(`Option '${name}' doesn't exist`);
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2022-01-12 19:32:23 +01:00
|
|
|
return val;
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function getOptionInt(name: FilterOptionsByType<number>, defaultValue?: number): number {
|
2020-06-20 12:31:38 +02:00
|
|
|
const val = getOption(name);
|
2019-11-03 11:43:04 +01:00
|
|
|
|
|
|
|
const intVal = parseInt(val);
|
|
|
|
|
|
|
|
if (isNaN(intVal)) {
|
2023-05-09 23:32:06 +02:00
|
|
|
if (defaultValue === undefined) {
|
2023-05-20 20:55:57 +02:00
|
|
|
throw new Error(`Could not parse '${val}' into integer for option '${name}'`);
|
2023-05-09 23:32:06 +02:00
|
|
|
} else {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2019-11-03 11:43:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return intVal;
|
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function getOptionBool(name: FilterOptionsByType<boolean>): boolean {
|
2020-06-20 12:31:38 +02:00
|
|
|
const val = getOption(name);
|
2019-11-10 11:25:41 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (typeof val !== "string" || !["true", "false"].includes(val)) {
|
2023-05-04 22:16:18 +02:00
|
|
|
throw new Error(`Could not parse '${val}' into boolean for option '${name}'`);
|
2019-11-10 11:25:41 +01:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return val === "true";
|
2019-11-10 11:25:41 +01:00
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
function setOption<T extends OptionNames>(name: T, value: string | OptionDefinitions[T]) {
|
2022-01-02 21:20:56 +01:00
|
|
|
const option = becca.getOption(name);
|
|
|
|
|
2020-03-01 20:49:11 +01:00
|
|
|
if (option) {
|
2025-01-03 17:54:05 +02:00
|
|
|
option.value = value as string;
|
2018-01-11 22:45:25 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
option.save();
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2020-06-20 12:31:38 +02:00
|
|
|
createOption(name, value, false);
|
2020-03-01 20:49:11 +01:00
|
|
|
}
|
2025-06-07 23:57:35 +00:00
|
|
|
|
|
|
|
// Clear current AI provider when AI-related options change
|
|
|
|
const aiOptions = [
|
|
|
|
'aiSelectedProvider', 'openaiApiKey', 'openaiBaseUrl', 'openaiDefaultModel',
|
|
|
|
'anthropicApiKey', 'anthropicBaseUrl', 'anthropicDefaultModel',
|
|
|
|
'ollamaBaseUrl', 'ollamaDefaultModel'
|
|
|
|
];
|
|
|
|
|
|
|
|
if (aiOptions.includes(name)) {
|
|
|
|
// Import dynamically to avoid circular dependencies
|
|
|
|
setImmediate(async () => {
|
|
|
|
try {
|
|
|
|
const aiServiceManager = (await import('./llm/ai_service_manager.js')).default;
|
|
|
|
aiServiceManager.getInstance().clearCurrentProvider();
|
|
|
|
console.log(`Cleared AI provider after ${name} option changed`);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Could not clear AI provider: ${error}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-01-11 22:45:25 -05:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:55:45 +02:00
|
|
|
/**
|
|
|
|
* Creates a new option in the database, with the given name, value and whether it should be synced.
|
2024-12-22 15:42:15 +02:00
|
|
|
*
|
2024-11-02 00:55:45 +02:00
|
|
|
* @param name the name of the option to be created.
|
|
|
|
* @param value the value of the option, as a string. It can then be interpreted as other types such as a number of boolean.
|
|
|
|
* @param isSynced `true` if the value should be synced across multiple instances (e.g. locale) or `false` if it should be local-only (e.g. theme).
|
|
|
|
*/
|
2025-01-03 17:54:05 +02:00
|
|
|
function createOption<T extends OptionNames>(name: T, value: string | OptionDefinitions[T], isSynced: boolean) {
|
2023-01-03 13:52:37 +01:00
|
|
|
new BOption({
|
2018-01-28 19:30:14 -05:00
|
|
|
name: name,
|
2025-01-03 17:54:05 +02:00
|
|
|
value: value as string,
|
2018-05-22 00:22:43 -04:00
|
|
|
isSynced: isSynced
|
|
|
|
}).save();
|
2017-11-02 20:48:02 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getOptions() {
|
2021-05-01 21:52:22 +02:00
|
|
|
return Object.values(becca.options);
|
2018-09-06 11:54:04 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 22:10:13 +02:00
|
|
|
function getOptionMap() {
|
2025-01-03 17:54:05 +02:00
|
|
|
const map: Record<string, string> = {};
|
2021-05-01 21:52:22 +02:00
|
|
|
|
|
|
|
for (const option of Object.values(becca.options)) {
|
|
|
|
map[option.name] = option.value;
|
|
|
|
}
|
|
|
|
|
2025-01-03 17:54:05 +02:00
|
|
|
return map as OptionMap;
|
2018-09-06 11:54:04 +02:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2017-11-02 20:48:02 -04:00
|
|
|
getOption,
|
2019-11-03 11:43:04 +01:00
|
|
|
getOptionInt,
|
2019-11-10 11:25:41 +01:00
|
|
|
getOptionBool,
|
2017-11-02 20:48:02 -04:00
|
|
|
setOption,
|
2018-09-06 11:54:04 +02:00
|
|
|
createOption,
|
|
|
|
getOptions,
|
2023-06-29 22:10:13 +02:00
|
|
|
getOptionMap,
|
2022-01-12 19:32:23 +01:00
|
|
|
getOptionOrNull
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|