146 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-11-02 00:55:45 +02:00
/**
* @module
*
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-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-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.
*/
import becca from "../becca/becca.js";
import BOption from "../becca/entities/boption.js";
2025-04-18 12:33:50 +03:00
import type { OptionRow } from "@triliumnext/commons";
import type { FilterOptionsByType, OptionDefinitions, OptionMap, OptionNames } from "@triliumnext/commons";
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
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) {
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);
const intVal = parseInt(val);
if (isNaN(intVal)) {
if (defaultValue === undefined) {
throw new Error(`Could not parse '${val}' into integer for option '${name}'`);
} else {
return defaultValue;
}
}
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);
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}'`);
}
2025-01-09 18:07:02 +02:00
return val === "true";
}
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);
if (option) {
2025-01-03 17:54:05 +02:00
option.value = value as string;
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);
}
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}`);
}
});
}
}
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-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) {
new BOption({
2018-01-28 19:30:14 -05:00
name: name,
2025-01-03 17:54:05 +02:00
value: value as string,
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
}
export default {
2017-11-02 20:48:02 -04:00
getOption,
getOptionInt,
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
};