2024-07-05 14:36:50 +08:00
|
|
|
import fs from "fs";
|
|
|
|
|
import path from "path";
|
|
|
|
|
import toml from "@iarna/toml";
|
2024-04-20 09:32:19 +05:30
|
|
|
|
2024-07-05 14:36:50 +08:00
|
|
|
const configFileName = "config.toml";
|
2024-04-20 09:32:19 +05:30
|
|
|
|
|
|
|
|
interface Config {
|
|
|
|
|
GENERAL: {
|
|
|
|
|
PORT: number;
|
|
|
|
|
SIMILARITY_MEASURE: string;
|
|
|
|
|
};
|
|
|
|
|
API_KEYS: {
|
|
|
|
|
OPENAI: string;
|
2024-05-01 19:43:06 +05:30
|
|
|
GROQ: string;
|
2024-04-20 09:32:19 +05:30
|
|
|
};
|
|
|
|
|
API_ENDPOINTS: {
|
|
|
|
|
SEARXNG: string;
|
2024-04-20 11:18:52 +05:30
|
|
|
OLLAMA: string;
|
2024-04-20 09:32:19 +05:30
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-23 16:45:14 +05:30
|
|
|
type RecursivePartial<T> = {
|
|
|
|
|
[P in keyof T]?: RecursivePartial<T[P]>;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-20 09:32:19 +05:30
|
|
|
const loadConfig = () =>
|
2024-07-05 14:36:50 +08:00
|
|
|
toml.parse(fs.readFileSync(path.join(__dirname, `../${configFileName}`), "utf-8")) as unknown as Config;
|
2024-04-20 09:32:19 +05:30
|
|
|
|
|
|
|
|
export const getPort = () => loadConfig().GENERAL.PORT;
|
|
|
|
|
|
2024-07-05 14:36:50 +08:00
|
|
|
export const getSimilarityMeasure = () => loadConfig().GENERAL.SIMILARITY_MEASURE;
|
2024-04-20 09:32:19 +05:30
|
|
|
|
|
|
|
|
export const getOpenaiApiKey = () => loadConfig().API_KEYS.OPENAI;
|
|
|
|
|
|
2024-05-01 19:43:06 +05:30
|
|
|
export const getGroqApiKey = () => loadConfig().API_KEYS.GROQ;
|
|
|
|
|
|
2024-04-20 09:32:19 +05:30
|
|
|
export const getSearxngApiEndpoint = () => loadConfig().API_ENDPOINTS.SEARXNG;
|
2024-04-20 11:18:52 +05:30
|
|
|
|
|
|
|
|
export const getOllamaApiEndpoint = () => loadConfig().API_ENDPOINTS.OLLAMA;
|
2024-04-23 16:45:14 +05:30
|
|
|
|
|
|
|
|
export const updateConfig = (config: RecursivePartial<Config>) => {
|
|
|
|
|
const currentConfig = loadConfig();
|
|
|
|
|
|
|
|
|
|
for (const key in currentConfig) {
|
2024-05-02 12:14:26 +05:30
|
|
|
if (!config[key]) config[key] = {};
|
2024-04-23 16:45:14 +05:30
|
|
|
|
2024-07-05 14:36:50 +08:00
|
|
|
if (typeof currentConfig[key] === "object" && currentConfig[key] !== null) {
|
2024-04-23 16:45:14 +05:30
|
|
|
for (const nestedKey in currentConfig[key]) {
|
2024-07-05 14:36:50 +08:00
|
|
|
if (!config[key][nestedKey] && currentConfig[key][nestedKey] && config[key][nestedKey] !== "") {
|
2024-04-23 16:45:14 +05:30
|
|
|
config[key][nestedKey] = currentConfig[key][nestedKey];
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-05 14:36:50 +08:00
|
|
|
} else if (currentConfig[key] && config[key] !== "") {
|
2024-04-23 16:45:14 +05:30
|
|
|
config[key] = currentConfig[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 14:36:50 +08:00
|
|
|
fs.writeFileSync(path.join(__dirname, `../${configFileName}`), toml.stringify(config));
|
2024-04-23 16:45:14 +05:30
|
|
|
};
|