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