LogViewer
A modern log viewer with AI-assisted analysis (Qt 6)
Loading...
Searching...
No Matches
Config.hpp
Go to the documentation of this file.
1// Plugin-local configuration for the AI plugin
2#pragma once
3
4#include <string>
5#include <filesystem>
6#include <nlohmann/json.hpp>
7
8// This must NOT live in `namespace config` with a class named `Config` and a
9// `GetConfig()` free function: the main application defines an unrelated,
10// much larger config::Config/config::GetConfig() in
11// src/application/config/Config.hpp. Since this plugin is a separate dylib
12// (dlopen'd, not linked against application_core) but shares the running
13// process, an identically-named-and-namespaced inline GetConfig() is a One
14// Definition Rule violation — the dynamic linker can bind a call in this
15// plugin to the *main app's* config::GetConfig() (a strong symbol beats this
16// header's weak/inline one), returning a real, large config::Config object
17// while this plugin's compiled code still uses the small local struct's
18// field offsets. Writes like `ollamaBaseUrl = "..."` then land inside
19// whatever real member happens to sit at that byte offset in the actual
20// config::Config object — in practice, FieldTranslator's internal map,
21// corrupting it. Namespaced under `ai` (matching this plugin's other
22// headers) instead, so the symbol can never collide with the app's.
23namespace ai {
24
25struct Config {
26 std::string aiProvider = "ollama";
27 std::string ollamaBaseUrl = "http://localhost:11434";
28 std::string openaiApiKey;
29 std::string anthropicApiKey;
30 std::string googleApiKey;
31 std::string xaiApiKey;
32 std::string ollamaDefaultModel;
34
35 void ApplyJson(const nlohmann::json& j) {
36 if (j.contains("aiTimeoutSeconds") && j["aiTimeoutSeconds"].is_number())
37 aiTimeoutSeconds = j["aiTimeoutSeconds"].get<int>();
38 if (j.contains("provider") && j["provider"].is_string())
39 aiProvider = j["provider"].get<std::string>();
40 if (j.contains("baseUrl") && j["baseUrl"].is_string())
41 ollamaBaseUrl = j["baseUrl"].get<std::string>();
42 if (j.contains("model") && j["model"].is_string())
43 ollamaDefaultModel = j["model"].get<std::string>();
44 if (j.contains("openaiApiKey") && j["openaiApiKey"].is_string())
45 openaiApiKey = j["openaiApiKey"].get<std::string>();
46 if (j.contains("anthropicApiKey") && j["anthropicApiKey"].is_string())
47 anthropicApiKey = j["anthropicApiKey"].get<std::string>();
48 if (j.contains("googleApiKey") && j["googleApiKey"].is_string())
49 googleApiKey = j["googleApiKey"].get<std::string>();
50 if (j.contains("xaiApiKey") && j["xaiApiKey"].is_string())
51 xaiApiKey = j["xaiApiKey"].get<std::string>();
52 }
53
54 nlohmann::json ToJson() const {
55 nlohmann::json j;
56 j["provider"] = aiProvider;
57 j["baseUrl"] = ollamaBaseUrl;
58 j["model"] = ollamaDefaultModel;
59 j["aiTimeoutSeconds"] = aiTimeoutSeconds;
60 j["openaiApiKey"] = openaiApiKey;
61 j["anthropicApiKey"] = anthropicApiKey;
62 j["googleApiKey"] = googleApiKey;
63 j["xaiApiKey"] = xaiApiKey;
64 return j;
65 }
66};
67
68inline Config& GetConfig() {
69 static Config cfg;
70 return cfg;
71}
72
73} // namespace ai
Definition GemmaInferenceEngine.cpp:18
Config & GetConfig()
Definition Config.hpp:68
Definition Config.hpp:25
std::string xaiApiKey
Definition Config.hpp:31
nlohmann::json ToJson() const
Definition Config.hpp:54
std::string openaiApiKey
Definition Config.hpp:28
void ApplyJson(const nlohmann::json &j)
Definition Config.hpp:35
std::string anthropicApiKey
Definition Config.hpp:29
std::string googleApiKey
Definition Config.hpp:30
std::string ollamaDefaultModel
Definition Config.hpp:32
std::string aiProvider
Definition Config.hpp:26
int aiTimeoutSeconds
Definition Config.hpp:33
std::string ollamaBaseUrl
Definition Config.hpp:27