refactor: centralize runtime config loading

- Move .env parsing and runtime config reload logic into Environment
- Reload runtime config and system prompt when source files change
- Gate unsafe eval and file tools behind explicit environment flags
- Rename datetime tool to get_datetime and improve tool prompts
- Return structured weather tool responses
- Preserve assistant thinking and aggregate tool calls across stream chunks
This commit is contained in:
2026-05-03 19:45:18 +03:00
parent 2fc60806ff
commit 35354a86de
4 changed files with 586 additions and 395 deletions
+1 -135
View File
@@ -1831,141 +1831,7 @@ export async function processNewMessage(msg: Message): Promise<void> {
console.log("New Message", msg);
if (!msg.from) return;
const envFile: string = fs.readFileSync(".env").toString();
const env = new Map(
envFile
.split(/\r?\n/)
.filter(line => line.trim())
.map(line => {
const [key, value = ""] = line.split("=");
return [key.trim(), value] as const;
})
);
const getEnv = (key: string): string | undefined => {
return env.get(key)?.trim();
};
const onlyForCreatorMode = getEnv("ONLY_FOR_CREATOR_MODE");
const defaultAiProvider = getEnv("DEFAULT_AI_PROVIDER");
let systemPrompt: string | null = null;
try {
const promptPath = path.join(Environment.DATA_PATH, "system_prompt.txt");
if (fs.existsSync(promptPath)) {
systemPrompt = fs.readFileSync(promptPath).toString().trim();
} else {
Environment.setSystemPrompt(undefined);
}
} catch (e) {
logError(e);
}
const useNamesInPrompt = getEnv("USE_NAMES_IN_PROMPT");
const useSystemPrompt = getEnv("USE_SYSTEM_PROMPT");
const sendTimeTook = getEnv("SEND_TIME_TOOK");
const ollamaApiKey = getEnv("OLLAMA_API_KEY");
const ollamaAddress = getEnv("OLLAMA_ADDRESS");
const ollamaModel = getEnv("OLLAMA_MODEL");
const ollamaImageModel = getEnv("OLLAMA_IMAGE_MODEL");
const ollamaThinkModel = getEnv("OLLAMA_THINK_MODEL");
const geminiApiKey = getEnv("GEMINI_API_KEY");
const geminiModel = getEnv("GEMINI_MODEL");
const geminiImageModel = getEnv("GEMINI_IMAGE_MODEL");
const mistralApiKey = getEnv("MISTRAL_API_KEY");
const mistralModel = getEnv("MISTRAL_MODEL");
const openAiBaseUrl = getEnv("OPENAI_BASE_URL");
const openAiApiKey = getEnv("OPENAI_API_KEY");
const openAiModel = getEnv("OPENAI_MODEL");
const openAiImageModel = getEnv("OPENAI_IMAGE_MODEL");
if (onlyForCreatorMode) {
Environment.setOnlyForCreatorMode(ifTrue(onlyForCreatorMode));
}
if (defaultAiProvider) {
if (Object.values(AiProvider).includes(defaultAiProvider as AiProvider)) {
Environment.DEFAULT_AI_PROVIDER = defaultAiProvider as AiProvider;
} else {
Environment.DEFAULT_AI_PROVIDER = AiProvider.OLLAMA;
}
}
if (systemPrompt) {
Environment.setSystemPrompt(systemPrompt);
}
if (useNamesInPrompt) {
Environment.setUseNamesInPrompt(ifTrue(useNamesInPrompt))
}
if (useSystemPrompt) {
Environment.setUseSystemPrompt(ifTrue(useSystemPrompt));
}
if (sendTimeTook) {
Environment.setSendTimeTook(ifTrue(sendTimeTook));
}
if (ollamaApiKey) {
Environment.setOllamaApiKey(ollamaApiKey);
}
if (ollamaAddress) {
Environment.setOllamaAddress(ollamaAddress);
}
if (ollamaModel) {
Environment.setOllamaModel(ollamaModel);
}
if (ollamaImageModel) {
Environment.setOllamaImageModel(ollamaImageModel);
}
if (ollamaThinkModel) {
Environment.setOllamaThinkModel(ollamaThinkModel);
}
if (geminiApiKey) {
Environment.setGeminiApiKey(geminiApiKey);
}
if (geminiModel) {
Environment.setGeminiModel(geminiModel);
}
if (geminiImageModel) {
Environment.setGeminiImageModel(geminiImageModel);
}
if (mistralApiKey) {
Environment.setMistralApiKey(mistralApiKey);
}
if (mistralModel) {
Environment.setMistralModel(mistralModel);
}
if (openAiBaseUrl) {
Environment.setOpenAIBaseUrl(openAiBaseUrl);
}
if (openAiApiKey) {
Environment.setOpenAIApiKey(openAiApiKey);
}
if (openAiModel) {
Environment.setOpenAIModel(openAiModel);
}
if (openAiImageModel) {
Environment.setOpenAIImageModel(openAiImageModel);
}
Environment.reloadRuntimeConfigIfChanged();
let storedMsg: StoredMessage | null = null;