-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsettings_mutations.cpp
More file actions
347 lines (326 loc) · 12.5 KB
/
Copy pathsettings_mutations.cpp
File metadata and controls
347 lines (326 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "settings_mutations.hpp"
#include "saved_models_revision.hpp"
#include <algorithm>
#include <optional>
#include <utility>
namespace acecode {
namespace {
SettingsMutationResult finish_mutation(
ConfigMutationResult mutation,
const SettingsMutationOptions& options,
bool publish_saved_models_revision = false) {
SettingsMutationResult result;
result.config = mutation.config;
result.changed = mutation.changed;
if (!mutation.ok) {
result.error_kind =
mutation.error_kind == ConfigMutationErrorKind::Validation
? SettingsMutationErrorKind::Validation
: SettingsMutationErrorKind::Persistence;
result.error = std::move(mutation.error);
return result;
}
result.ok = true;
result.persisted = mutation.changed;
if (options.live_config && !publish_saved_models_revision) {
*options.live_config = mutation.config;
}
if (!mutation.changed) {
result.runtime_status = SettingsRuntimeStatus::Unchanged;
return result;
}
if (options.apply_live) {
std::string runtime_error;
if (options.apply_live(mutation.config, runtime_error)) {
result.runtime_status = SettingsRuntimeStatus::AppliedLive;
} else {
result.runtime_status = SettingsRuntimeStatus::RuntimeApplyFailed;
result.error_kind = SettingsMutationErrorKind::RuntimeApply;
result.error = runtime_error.empty()
? "setting was saved but could not be applied live"
: std::move(runtime_error);
return result;
}
}
if (options.live_config && publish_saved_models_revision) {
publish_live_config(*options.live_config, mutation.config, true);
}
if (options.apply_live) return result;
if (options.live_config) {
result.runtime_status = SettingsRuntimeStatus::AppliedLive;
} else if (options.restart_required_without_live_apply) {
result.runtime_status = SettingsRuntimeStatus::RestartRequired;
} else {
result.runtime_status = SettingsRuntimeStatus::AppliedLive;
}
return result;
}
template <typename Mutator>
SettingsMutationResult run_mutation(
Mutator&& mutator,
const SettingsMutationOptions& options,
bool publish_saved_models_revision = false) {
return finish_mutation(
mutate_config(
std::forward<Mutator>(mutator),
options.config_path,
options.live_config),
options,
publish_saved_models_revision);
}
std::string saved_model_error(SavedModelEditError error) {
switch (error) {
case SavedModelEditError::OK:
return {};
case SavedModelEditError::INVALID_NAME:
return "model name is invalid";
case SavedModelEditError::RESERVED_NAME:
return "model name uses a reserved prefix";
case SavedModelEditError::NAME_TAKEN:
return "model name already exists";
case SavedModelEditError::UNKNOWN_PROVIDER:
return "model provider is unknown";
case SavedModelEditError::PROVIDER_DISABLED:
return "model provider is disabled";
case SavedModelEditError::MISSING_MODEL:
return "model identifier is required";
case SavedModelEditError::MISSING_BASE_URL:
return "model base URL is required";
case SavedModelEditError::INVALID_API_KEY:
return "model API key is required";
case SavedModelEditError::INVALID_CONTEXT_WINDOW:
return "model context window is invalid";
case SavedModelEditError::INVALID_STREAM_TIMEOUT:
return "model stream timeout is invalid";
case SavedModelEditError::INVALID_CAPABILITY:
return "model capability list is invalid";
case SavedModelEditError::INVALID_REQUEST_HEADER:
return "model request headers are invalid";
case SavedModelEditError::INVALID_ENDPOINT_MODE:
return "model endpoint mode is invalid";
case SavedModelEditError::INVALID_MAX_OUTPUT_TOKENS:
return "model maximum output token limit is invalid";
case SavedModelEditError::INVALID_CAPABILITIES_SOURCE:
return "model capability source is invalid";
case SavedModelEditError::INVALID_REASONING:
return "model reasoning options are invalid";
case SavedModelEditError::INVALID_CREDENTIAL_SOURCE:
return "saved credential source is missing or incompatible";
case SavedModelEditError::CREDENTIAL_CONFLICT:
return "choose exactly one credential update operation";
case SavedModelEditError::UNSUPPORTED_MODEL_OPTION:
return "model option is not supported by this provider";
case SavedModelEditError::NOT_FOUND:
return "model profile was not found";
case SavedModelEditError::IN_USE_AS_DEFAULT:
return "model profile is used by a busy session";
}
return "model profile update was rejected";
}
} // namespace
SettingsMutationResult set_default_permission_mode(
const std::string& mode,
const SettingsMutationOptions& options) {
return run_mutation(
[mode](AppConfig& cfg, std::string& error) {
if (mode != "default" &&
mode != "accept-edits" &&
mode != "plan" &&
mode != "yolo") {
error = "unsupported default permission mode";
return false;
}
if (cfg.default_permission_mode == mode) return false;
cfg.default_permission_mode = mode;
return true;
},
options);
}
SettingsMutationResult set_native_notifications_enabled(
bool enabled,
const SettingsMutationOptions& options) {
return run_mutation(
[enabled](AppConfig& cfg, std::string&) {
if (cfg.desktop.notifications.enabled == enabled) return false;
cfg.desktop.notifications.enabled = enabled;
return true;
},
options);
}
SettingsMutationResult set_remote_web_enabled(
bool enabled,
const SettingsMutationOptions& options) {
const std::optional<WebConfig> runtime_web = options.live_config
? std::optional<WebConfig>(options.live_config->web)
: std::nullopt;
auto result = run_mutation(
[enabled](AppConfig& cfg, std::string&) {
bool changed = false;
if (cfg.web.bind != "127.0.0.1") {
cfg.web.bind = "127.0.0.1";
changed = true;
}
if (cfg.web.remote_enabled != enabled) {
cfg.web.remote_enabled = enabled;
changed = true;
}
return changed;
},
options);
if (result.ok && runtime_web && options.live_config) {
const bool saved_remote_enabled =
options.live_config->web.remote_enabled;
const int saved_remote_port = options.live_config->web.remote_port;
options.live_config->web = *runtime_web;
options.live_config->web.bind = "127.0.0.1";
options.live_config->web.remote_enabled = saved_remote_enabled;
options.live_config->web.remote_port = saved_remote_port;
}
return result;
}
SettingsMutationResult set_tui_theme(
const std::string& theme,
const SettingsMutationOptions& options) {
return run_mutation(
[theme](AppConfig& cfg, std::string& error) {
if (theme != "auto" && theme != "dark" && theme != "light") {
error = "unsupported TUI theme";
return false;
}
if (cfg.tui.theme == theme) return false;
cfg.tui.theme = theme;
return true;
},
options);
}
SettingsMutationResult set_upgrade_base_url(
const std::string& base_url,
const SettingsMutationOptions& options) {
return run_mutation(
[base_url](AppConfig& cfg, std::string& error) {
const std::string normalized =
normalize_upgrade_base_url(base_url);
if (!is_valid_upgrade_base_url(normalized)) {
error = "upgrade service URL must use http or https";
return false;
}
if (cfg.upgrade.base_url == normalized) return false;
cfg.upgrade.base_url = normalized;
return true;
},
options);
}
SettingsMutationResult set_custom_instructions(
const std::string& text,
const SettingsMutationOptions& options) {
return run_mutation(
[text](AppConfig& cfg, std::string& error) {
if (text.size() > kCustomInstructionsMaxBytes) {
error = "custom instructions exceed the 64 KiB limit";
return false;
}
if (cfg.custom_instructions.text_snapshot() == text) return false;
cfg.custom_instructions.set_text(text);
return true;
},
options);
}
SettingsMutationResult add_saved_model_setting(
const SavedModelDraft& draft,
const SettingsMutationOptions& options) {
SavedModelEditError edit_error = SavedModelEditError::OK;
auto result = run_mutation(
[draft, &edit_error](AppConfig& cfg, std::string& error) {
const auto before = cfg.saved_models;
edit_error = add_saved_model(cfg, draft);
if (edit_error != SavedModelEditError::OK) {
error = saved_model_error(edit_error);
return false;
}
return !saved_model_lists_equal(before, cfg.saved_models);
},
options,
true);
if (edit_error != SavedModelEditError::OK) {
result.error_code = to_string(edit_error);
}
return result;
}
SettingsMutationResult update_saved_model_setting(
const std::string& old_name,
const SavedModelDraft& draft,
const SettingsMutationOptions& options) {
SavedModelEditError edit_error = SavedModelEditError::OK;
auto result = run_mutation(
[old_name, draft, &edit_error](AppConfig& cfg, std::string& error) {
const auto before = cfg.saved_models;
edit_error =
update_saved_model(cfg, old_name, draft);
if (edit_error != SavedModelEditError::OK) {
error = saved_model_error(edit_error);
return false;
}
return !saved_model_lists_equal(before, cfg.saved_models);
},
options,
true);
if (edit_error != SavedModelEditError::OK) {
result.error_code = to_string(edit_error);
}
return result;
}
SettingsMutationResult remove_saved_model_setting(
const std::string& name,
const std::function<bool(const std::string&)>& is_used_by_busy_session,
const SettingsMutationOptions& options) {
if (is_used_by_busy_session && is_used_by_busy_session(name)) {
SettingsMutationResult result;
result.error_kind = SettingsMutationErrorKind::Validation;
result.error_code = "MODEL_IN_USE";
result.error = saved_model_error(SavedModelEditError::IN_USE_AS_DEFAULT);
return result;
}
SavedModelEditError edit_error = SavedModelEditError::OK;
auto result = run_mutation(
[name, &edit_error](AppConfig& cfg, std::string& error) {
const auto before = cfg.saved_models;
edit_error = remove_saved_model(cfg, name);
if (edit_error != SavedModelEditError::OK) {
error = saved_model_error(edit_error);
return false;
}
return !saved_model_lists_equal(before, cfg.saved_models);
},
options,
true);
if (edit_error != SavedModelEditError::OK) {
result.error_code = to_string(edit_error);
}
return result;
}
SettingsMutationResult set_default_model_setting(
const std::string& name,
const SettingsMutationOptions& options) {
bool not_found = false;
auto result = run_mutation(
[name, ¬_found](AppConfig& cfg, std::string& error) {
const auto found = std::find_if(
cfg.saved_models.begin(),
cfg.saved_models.end(),
[&name](const ModelProfile& profile) {
return profile.name == name;
});
if (found == cfg.saved_models.end()) {
not_found = true;
error = "model profile was not found";
return false;
}
if (cfg.default_model_name == name) return false;
cfg.default_model_name = name;
return true;
},
options);
if (not_found) result.error_code = "NOT_FOUND";
return result;
}
} // namespace acecode