yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
agent_editor_internal.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_AGENT_AGENT_EDITOR_INTERNAL_H_
2#define YAZE_APP_EDITOR_AGENT_AGENT_EDITOR_INTERNAL_H_
3
4#include <cstring>
5#include <optional>
6#include <string>
7
12
13#if defined(__APPLE__)
14#include <CoreFoundation/CoreFoundation.h>
15#include <Security/Security.h>
16#include <TargetConditionals.h>
17#endif
18
19namespace yaze {
20namespace editor {
21namespace internal {
22
23template <size_t N>
24void CopyStringToBuffer(const std::string& src, char (&dest)[N]) {
25 std::strncpy(dest, src.c_str(), N - 1);
26 dest[N - 1] = '\0';
27}
28
29inline std::optional<std::string> LoadKeychainValue(const std::string& key) {
30#if defined(__APPLE__)
31 if (key.empty()) {
32 return std::nullopt;
33 }
34 CFStringRef key_ref = CFStringCreateWithCString(
35 kCFAllocatorDefault, key.c_str(), kCFStringEncodingUTF8);
36 const void* keys[] = {kSecClass, kSecAttrAccount, kSecReturnData,
37 kSecMatchLimit};
38 const void* values[] = {kSecClassGenericPassword, key_ref, kCFBooleanTrue,
39 kSecMatchLimitOne};
40 CFDictionaryRef query = CFDictionaryCreate(
41 kCFAllocatorDefault, keys, values,
42 static_cast<CFIndex>(sizeof(keys) / sizeof(keys[0])),
43 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
44 CFTypeRef item = nullptr;
45 OSStatus status = SecItemCopyMatching(query, &item);
46 if (query) {
47 CFRelease(query);
48 }
49 if (key_ref) {
50 CFRelease(key_ref);
51 }
52 if (status == errSecItemNotFound) {
53 return std::nullopt;
54 }
55 if (status != errSecSuccess || !item) {
56 if (item) {
57 CFRelease(item);
58 }
59 return std::nullopt;
60 }
61 CFDataRef data_ref = static_cast<CFDataRef>(item);
62 const UInt8* data_ptr = CFDataGetBytePtr(data_ref);
63 CFIndex data_len = CFDataGetLength(data_ref);
64 std::string value(reinterpret_cast<const char*>(data_ptr),
65 static_cast<size_t>(data_len));
66 CFRelease(item);
67 return value;
68#else
69 (void)key;
70 return std::nullopt;
71#endif
72}
73
74inline std::string ResolveHostApiKey(
75 const UserSettings::Preferences* prefs,
77 if (!host.api_key.empty()) {
78 return host.api_key;
79 }
80 if (!host.credential_id.empty()) {
81 if (auto key = LoadKeychainValue(host.credential_id)) {
82 return *key;
83 }
84 }
85 if (!prefs) {
86 return {};
87 }
88 std::string api_type =
89 host.api_type.empty() ? cli::kProviderOpenAi : host.api_type;
90 if (api_type == cli::kProviderLmStudio) {
91 api_type = cli::kProviderOpenAi;
92 }
93 if (api_type == cli::kProviderOpenAi) {
94 return prefs->openai_api_key;
95 }
96 if (api_type == cli::kProviderGemini) {
97 return prefs->gemini_api_key;
98 }
99 if (api_type == cli::kProviderAnthropic) {
100 return prefs->anthropic_api_key;
101 }
102 return {};
103}
104
108 const UserSettings::Preferences* prefs) {
109 if (!profile) {
110 return;
111 }
112 std::string api_key = ResolveHostApiKey(prefs, host);
113 profile->host_id = host.id;
114 std::string api_type = host.api_type;
115 if (api_type == cli::kProviderLmStudio) {
116 api_type = cli::kProviderOpenAi;
117 }
118 if (api_type == cli::kProviderOpenAi || api_type == cli::kProviderOllama ||
119 api_type == cli::kProviderGemini || api_type == cli::kProviderAnthropic) {
120 profile->provider = api_type;
121 }
122 if (profile->provider == cli::kProviderOpenAi) {
123 if (!host.base_url.empty()) {
125 }
126 if (!api_key.empty()) {
127 profile->openai_api_key = api_key;
128 }
129 } else if (profile->provider == cli::kProviderOllama) {
130 if (!host.base_url.empty()) {
131 profile->ollama_host = host.base_url;
132 }
133 } else if (profile->provider == cli::kProviderGemini) {
134 if (!api_key.empty()) {
135 profile->gemini_api_key = api_key;
136 }
137 } else if (profile->provider == cli::kProviderAnthropic) {
138 if (!api_key.empty()) {
139 profile->anthropic_api_key = api_key;
140 }
141 }
142}
143
144} // namespace internal
145} // namespace editor
146} // namespace yaze
147
148#endif // YAZE_APP_EDITOR_AGENT_AGENT_EDITOR_INTERNAL_H_
constexpr char kProviderGemini[]
Definition provider_ids.h:9
constexpr char kProviderAnthropic[]
std::string NormalizeOpenAiBaseUrl(std::string base)
constexpr char kProviderOpenAi[]
constexpr char kProviderOllama[]
Definition provider_ids.h:8
constexpr char kProviderLmStudio[]
std::optional< std::string > LoadKeychainValue(const std::string &key)
std::string ResolveHostApiKey(const UserSettings::Preferences *prefs, const UserSettings::Preferences::AiHost &host)
void CopyStringToBuffer(const std::string &src, char(&dest)[N])
void ApplyHostPresetToProfile(AgentEditor::BotProfile *profile, const UserSettings::Preferences::AiHost &host, const UserSettings::Preferences *prefs)