-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.c
More file actions
402 lines (331 loc) · 16.1 KB
/
config.c
File metadata and controls
402 lines (331 loc) · 16.1 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "deepshell.h"
char* get_config_path(void) {
char *home = get_home_directory();
if (!home) {
return NULL;
}
char *config_path = malloc(strlen(home) + strlen(CONFIG_DIR_NAME) + 2);
if (!config_path) {
free(home);
return NULL;
}
sprintf(config_path, "%s/%s", home, CONFIG_DIR_NAME);
free(home);
return config_path;
}
char* get_config_file_path(void) {
char *config_dir = get_config_path();
if (!config_dir) {
return NULL;
}
char *config_file = malloc(strlen(config_dir) + strlen(CONFIG_FILE_NAME) + 2);
if (!config_file) {
free(config_dir);
return NULL;
}
sprintf(config_file, "%s/%s", config_dir, CONFIG_FILE_NAME);
free(config_dir);
return config_file;
}
config_t* get_default_config(void) {
config_t *config = malloc(sizeof(config_t));
if (!config) {
return NULL;
}
memset(config, 0, sizeof(config_t));
config->interactive_history_limit = 25;
config->enable_streaming = false;
config->show_progress_animation = true;
config->gemini.render_markdown = true;
config->ollama.render_markdown = true;
config->openrouter.render_markdown = true;
return config;
}
config_t* load_config(void) {
char *config_file_path = get_config_file_path();
if (!config_file_path) {
return NULL;
}
FILE *file = fopen(config_file_path, "r");
if (!file) {
free(config_file_path);
return NULL;
}
// Get file size
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
if (file_size <= 0) {
fclose(file);
free(config_file_path);
return NULL;
}
// Read file content
char *json_content = malloc(file_size + 1);
if (!json_content) {
fclose(file);
free(config_file_path);
return NULL;
}
size_t bytes_read = fread(json_content, 1, file_size, file);
json_content[bytes_read] = '\0';
fclose(file);
// Parse JSON
json_object *json_obj = json_tokener_parse(json_content);
free(json_content);
if (!json_obj) {
display_message("Error: Invalid JSON format in config file. Please delete it and run DeepShell again to reconfigure.", COLOR_RED);
free(config_file_path);
return NULL;
}
// Create config structure
config_t *config = get_default_config();
if (!config) {
json_object_put(json_obj);
free(config_file_path);
return NULL;
}
// Parse JSON into config structure
json_object *active_service, *previous_service, *llm_services, *ollama_config, *gemini_config;
json_object *interactive_limit, *enable_streaming;
if (json_object_object_get_ex(json_obj, "active_llm_service", &active_service)) {
strncpy(config->active_llm_service, json_object_get_string(active_service), 31);
config->active_llm_service[31] = '\0';
}
if (json_object_object_get_ex(json_obj, "previous_active_llm_service", &previous_service)) {
strncpy(config->previous_active_llm_service, json_object_get_string(previous_service), 31);
config->previous_active_llm_service[31] = '\0';
}
if (json_object_object_get_ex(json_obj, "interactive_history_limit", &interactive_limit)) {
config->interactive_history_limit = json_object_get_int(interactive_limit);
}
if (json_object_object_get_ex(json_obj, "enable_streaming", &enable_streaming)) {
config->enable_streaming = json_object_get_boolean(enable_streaming);
}
json_object *show_progress_animation;
if (json_object_object_get_ex(json_obj, "show_progress_animation", &show_progress_animation)) {
config->show_progress_animation = json_object_get_boolean(show_progress_animation);
}
if (json_object_object_get_ex(json_obj, "llm_services", &llm_services)) {
// Parse Ollama config
if (json_object_object_get_ex(llm_services, LLM_SERVICE_OLLAMA, &ollama_config)) {
json_object *server_addr, *model, *render_md;
if (json_object_object_get_ex(ollama_config, "server_address", &server_addr)) {
strncpy(config->ollama.server_address, json_object_get_string(server_addr), MAX_SERVER_ADDR_LEN - 1);
config->ollama.server_address[MAX_SERVER_ADDR_LEN - 1] = '\0';
}
if (json_object_object_get_ex(ollama_config, "model", &model)) {
strncpy(config->ollama.model, json_object_get_string(model), MAX_MODEL_NAME_LEN - 1);
config->ollama.model[MAX_MODEL_NAME_LEN - 1] = '\0';
}
if (json_object_object_get_ex(ollama_config, "render_markdown", &render_md)) {
config->ollama.render_markdown = json_object_get_boolean(render_md);
}
}
// Parse Gemini config
if (json_object_object_get_ex(llm_services, LLM_SERVICE_GEMINI, &gemini_config)) {
json_object *api_keys, *active_key, *model, *render_md;
if (json_object_object_get_ex(gemini_config, "api_keys", &api_keys)) {
int key_count = json_object_array_length(api_keys);
config->gemini.api_key_count = key_count > MAX_SERVICES ? MAX_SERVICES : key_count;
for (int i = 0; i < config->gemini.api_key_count; i++) {
json_object *key_obj = json_object_array_get_idx(api_keys, i);
json_object *nickname, *key;
if (json_object_object_get_ex(key_obj, "nickname", &nickname)) {
strncpy(config->gemini.api_keys[i].nickname, json_object_get_string(nickname), MAX_NICKNAME_LEN - 1);
config->gemini.api_keys[i].nickname[MAX_NICKNAME_LEN - 1] = '\0';
}
if (json_object_object_get_ex(key_obj, "key", &key)) {
strncpy(config->gemini.api_keys[i].key, json_object_get_string(key), MAX_API_KEY_LEN - 1);
config->gemini.api_keys[i].key[MAX_API_KEY_LEN - 1] = '\0';
}
}
}
if (json_object_object_get_ex(gemini_config, "active_api_key_nickname", &active_key)) {
strncpy(config->gemini.active_api_key_nickname, json_object_get_string(active_key), MAX_NICKNAME_LEN - 1);
config->gemini.active_api_key_nickname[MAX_NICKNAME_LEN - 1] = '\0';
}
if (json_object_object_get_ex(gemini_config, "model", &model)) {
strncpy(config->gemini.model, json_object_get_string(model), MAX_MODEL_NAME_LEN - 1);
config->gemini.model[MAX_MODEL_NAME_LEN - 1] = '\0';
}
if (json_object_object_get_ex(gemini_config, "render_markdown", &render_md)) {
config->gemini.render_markdown = json_object_get_boolean(render_md);
}
}
// Parse OpenRouter config
json_object *openrouter_config;
if (json_object_object_get_ex(llm_services, LLM_SERVICE_OPENROUTER, &openrouter_config)) {
json_object *api_keys, *active_key, *model, *site_url, *site_name, *render_md;
if (json_object_object_get_ex(openrouter_config, "api_keys", &api_keys)) {
int key_count = json_object_array_length(api_keys);
config->openrouter.api_key_count = key_count > MAX_SERVICES ? MAX_SERVICES : key_count;
for (int i = 0; i < config->openrouter.api_key_count; i++) {
json_object *key_obj = json_object_array_get_idx(api_keys, i);
json_object *nickname, *key;
if (json_object_object_get_ex(key_obj, "nickname", &nickname)) {
strncpy(config->openrouter.api_keys[i].nickname, json_object_get_string(nickname), MAX_NICKNAME_LEN - 1);
config->openrouter.api_keys[i].nickname[MAX_NICKNAME_LEN - 1] = '\0';
}
if (json_object_object_get_ex(key_obj, "key", &key)) {
strncpy(config->openrouter.api_keys[i].key, json_object_get_string(key), MAX_API_KEY_LEN - 1);
config->openrouter.api_keys[i].key[MAX_API_KEY_LEN - 1] = '\0';
}
}
}
if (json_object_object_get_ex(openrouter_config, "active_api_key_nickname", &active_key)) {
strncpy(config->openrouter.active_api_key_nickname, json_object_get_string(active_key), MAX_NICKNAME_LEN - 1);
config->openrouter.active_api_key_nickname[MAX_NICKNAME_LEN - 1] = '\0';
}
if (json_object_object_get_ex(openrouter_config, "model", &model)) {
strncpy(config->openrouter.model, json_object_get_string(model), MAX_MODEL_NAME_LEN - 1);
config->openrouter.model[MAX_MODEL_NAME_LEN - 1] = '\0';
}
if (json_object_object_get_ex(openrouter_config, "site_url", &site_url)) {
strncpy(config->openrouter.site_url, json_object_get_string(site_url), MAX_SERVER_ADDR_LEN - 1);
config->openrouter.site_url[MAX_SERVER_ADDR_LEN - 1] = '\0';
}
if (json_object_object_get_ex(openrouter_config, "site_name", &site_name)) {
strncpy(config->openrouter.site_name, json_object_get_string(site_name), MAX_NICKNAME_LEN - 1);
config->openrouter.site_name[MAX_NICKNAME_LEN - 1] = '\0';
}
if (json_object_object_get_ex(openrouter_config, "render_markdown", &render_md)) {
config->openrouter.render_markdown = json_object_get_boolean(render_md);
}
}
}
json_object_put(json_obj);
free(config_file_path);
return config;
}
bool save_config(config_t *config) {
if (!config) {
return false;
}
char *config_dir = get_config_path();
char *config_file_path = get_config_file_path();
if (!config_dir || !config_file_path) {
free(config_dir);
free(config_file_path);
return false;
}
// Create config directory if it doesn't exist
if (!create_directory_if_not_exists(config_dir)) {
free(config_dir);
free(config_file_path);
return false;
}
// Create JSON object
json_object *json_obj = json_object_new_object();
// Add basic fields
json_object_object_add(json_obj, "active_llm_service",
json_object_new_string(config->active_llm_service));
json_object_object_add(json_obj, "previous_active_llm_service",
json_object_new_string(config->previous_active_llm_service));
json_object_object_add(json_obj, "interactive_history_limit",
json_object_new_int(config->interactive_history_limit));
json_object_object_add(json_obj, "enable_streaming",
json_object_new_boolean(config->enable_streaming));
json_object_object_add(json_obj, "show_progress_animation",
json_object_new_boolean(config->show_progress_animation));
// Create llm_services object
json_object *llm_services = json_object_new_object();
// Add Ollama config
json_object *ollama_config = json_object_new_object();
json_object_object_add(ollama_config, "server_address",
json_object_new_string(config->ollama.server_address));
json_object_object_add(ollama_config, "model",
json_object_new_string(config->ollama.model));
json_object_object_add(ollama_config, "render_markdown",
json_object_new_boolean(config->ollama.render_markdown));
json_object_object_add(llm_services, LLM_SERVICE_OLLAMA, ollama_config);
// Add Gemini config
json_object *gemini_config = json_object_new_object();
json_object_object_add(gemini_config, "active_api_key_nickname",
json_object_new_string(config->gemini.active_api_key_nickname));
json_object_object_add(gemini_config, "model",
json_object_new_string(config->gemini.model));
json_object_object_add(gemini_config, "render_markdown",
json_object_new_boolean(config->gemini.render_markdown));
// Add API keys array
json_object *api_keys = json_object_new_array();
for (int i = 0; i < config->gemini.api_key_count; i++) {
json_object *key_obj = json_object_new_object();
json_object_object_add(key_obj, "nickname",
json_object_new_string(config->gemini.api_keys[i].nickname));
json_object_object_add(key_obj, "key",
json_object_new_string(config->gemini.api_keys[i].key));
json_object_array_add(api_keys, key_obj);
}
json_object_object_add(gemini_config, "api_keys", api_keys);
json_object_object_add(llm_services, LLM_SERVICE_GEMINI, gemini_config);
// Add OpenRouter config
json_object *openrouter_config = json_object_new_object();
json_object_object_add(openrouter_config, "active_api_key_nickname",
json_object_new_string(config->openrouter.active_api_key_nickname));
json_object_object_add(openrouter_config, "model",
json_object_new_string(config->openrouter.model));
json_object_object_add(openrouter_config, "site_url",
json_object_new_string(config->openrouter.site_url));
json_object_object_add(openrouter_config, "site_name",
json_object_new_string(config->openrouter.site_name));
json_object_object_add(openrouter_config, "render_markdown",
json_object_new_boolean(config->openrouter.render_markdown));
// Add API keys array
json_object *openrouter_api_keys = json_object_new_array();
for (int i = 0; i < config->openrouter.api_key_count; i++) {
json_object *key_obj = json_object_new_object();
json_object_object_add(key_obj, "nickname",
json_object_new_string(config->openrouter.api_keys[i].nickname));
json_object_object_add(key_obj, "key",
json_object_new_string(config->openrouter.api_keys[i].key));
json_object_array_add(openrouter_api_keys, key_obj);
}
json_object_object_add(openrouter_config, "api_keys", openrouter_api_keys);
json_object_object_add(llm_services, LLM_SERVICE_OPENROUTER, openrouter_config);
json_object_object_add(json_obj, "llm_services", llm_services);
// Write to file
FILE *file = fopen(config_file_path, "w");
if (!file) {
json_object_put(json_obj);
free(config_dir);
free(config_file_path);
return false;
}
const char *json_string = json_object_to_json_string_ext(json_obj, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE);
fprintf(file, "%s\n", json_string);
fclose(file);
display_message("Configuration saved successfully.", COLOR_GREEN);
json_object_put(json_obj);
free(config_dir);
free(config_file_path);
return true;
}
void free_config(config_t *config) {
if (config) {
free(config);
}
}
bool delete_config_file(void) {
char *config_file_path = get_config_file_path();
if (!config_file_path) {
return false;
}
display_message("Are you sure you want to delete the configuration file? (y/N): ", COLOR_YELLOW);
char *response = read_line();
if (!response) {
free(config_file_path);
return false;
}
bool confirmed = (strlen(response) > 0 &&
(response[0] == 'y' || response[0] == 'Y'));
free(response);
if (!confirmed) {
display_message("Configuration deletion cancelled.", COLOR_YELLOW);
free(config_file_path);
return false;
}
int result = remove(config_file_path);
free(config_file_path);
return result == 0;
}