-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.c
More file actions
1441 lines (1266 loc) · 56.8 KB
/
settings.c
File metadata and controls
1441 lines (1266 loc) · 56.8 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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "deepshell.h"
bool setup_config(bool is_direct_flag_call) {
config_t *config = load_config();
bool config_existed_before_load = (config != NULL);
if (!config) {
config = get_default_config();
config_existed_before_load = false;
}
// Handle initial setup
if (!config_existed_before_load && !is_direct_flag_call) {
display_message("\n--- DeepShell Initial Configuration ---", COLOR_GREEN);
display_message("No configuration file found. Let's set up your first LLM service.", COLOR_YELLOW);
// Let user choose which service to configure initially
display_message("\nWhich LLM service would you like to set up initially?", COLOR_BLUE);
display_message("1. Ollama", COLOR_BLUE);
display_message("2. Gemini", COLOR_BLUE);
display_message("3. OpenRouter", COLOR_BLUE);
display_message("4. Cancel", COLOR_BLUE);
display_message("\nEnter your choice (number): ", COLOR_YELLOW);
char *choice = read_line();
if (!choice) {
display_message("Invalid input.", COLOR_RED);
free_config(config);
return false;
}
int service_choice = atoi(choice);
free(choice);
bool setup_success = false;
if (service_choice == 1) {
setup_success = configure_ollama_service(config);
} else if (service_choice == 2) {
setup_success = configure_gemini_service(config);
} else if (service_choice == 3) {
setup_success = configure_openrouter_service(config);
} else if (service_choice == 4) {
display_message("Initial setup cancelled. At least one service must be configured to use DeepShell.", COLOR_YELLOW);
free_config(config);
return false;
} else {
display_message("Invalid choice.", COLOR_RED);
free_config(config);
return false;
}
if (setup_success) {
save_config(config);
display_message("Initial setup complete!", COLOR_GREEN);
free_config(config);
return true;
} else {
display_message("Initial service setup failed. Exiting.", COLOR_RED);
free_config(config);
return false;
}
}
// For direct flag calls (-s), show the full settings menu
if (is_direct_flag_call) {
display_message("\n--- DeepShell Configuration Setup ---", COLOR_GREEN);
display_message("This will guide you through configuring your LLM services.", COLOR_BLUE);
bool setup_completed = false;
while (!setup_completed) {
display_message("\n--- DeepShell Settings Menu ---", COLOR_GREEN);
display_message("Settings Menu:", COLOR_GREEN);
display_message("1. Manage LLM Services (Add/Reconfigure)", COLOR_BLUE);
display_message("2. Switch Active LLM Service", COLOR_BLUE);
display_message("3. Manage API Keys", COLOR_BLUE);
display_message("4. Change Model for Active Service", COLOR_BLUE);
display_message("5. Toggle Markdown Rendering for Active Service", COLOR_BLUE);
display_message("6. Set Interactive History Limit", COLOR_BLUE);
display_message("7. Toggle Response Streaming", COLOR_BLUE);
display_message("8. Toggle Progress Animation", COLOR_BLUE);
display_message("9. View Active Configuration", COLOR_BLUE);
display_message("0. Delete Entire Configuration", COLOR_BLUE);
display_message("X. Exit Settings", COLOR_BLUE);
display_message("\nEnter your choice: ", COLOR_YELLOW);
char *choice = read_line();
if (!choice) {
display_message("Invalid input.", COLOR_RED);
continue;
}
int menu_choice = -1;
bool is_exit = false;
if (strlen(choice) == 1 && (choice[0] == 'x' || choice[0] == 'X')) {
is_exit = true;
} else {
menu_choice = atoi(choice);
}
free(choice);
if (is_exit) {
setup_completed = true;
continue;
}
switch (menu_choice) {
case 1:
// Manage LLM Services (Add/Reconfigure)
{
display_message("\nWhich LLM service would you like to configure/reconfigure?", COLOR_BLUE);
display_message("1. Ollama", COLOR_BLUE);
if (strlen(config->ollama.server_address) > 0) {
display_message(" (already configured)", COLOR_YELLOW);
}
display_message("2. Gemini", COLOR_BLUE);
if (config->gemini.api_key_count > 0) {
display_message(" (already configured)", COLOR_YELLOW);
}
display_message("3. OpenRouter", COLOR_BLUE);
if (config->openrouter.api_key_count > 0) {
display_message(" (already configured)", COLOR_YELLOW);
}
display_message("4. Cancel", COLOR_BLUE);
display_message("\nEnter your choice (number): ", COLOR_YELLOW);
char *service_choice = read_line();
if (!service_choice) {
display_message("Invalid input.", COLOR_RED);
break;
}
int choice = atoi(service_choice);
free(service_choice);
bool setup_success = false;
if (choice == 1) {
setup_success = configure_ollama_service(config);
} else if (choice == 2) {
setup_success = configure_gemini_service(config);
} else if (choice == 3) {
setup_success = configure_openrouter_service(config);
} else if (choice == 4) {
display_message("Service configuration cancelled.", COLOR_YELLOW);
} else {
display_message("Invalid choice.", COLOR_RED);
}
if (setup_success) {
save_config(config);
}
}
break;
case 2:
// Switch Active LLM Service
if (switch_llm_service(config)) {
save_config(config);
}
break;
case 3:
// Manage API Keys
if (manage_api_keys_unified(config)) {
save_config(config);
}
break;
case 4:
// Change Model for Active Service
if (change_active_model(config)) {
save_config(config);
}
break;
case 5:
// Toggle Markdown Rendering for Active Service
if (toggle_markdown_rendering(config)) {
save_config(config);
}
break;
case 6:
// Set Interactive History Limit
if (set_history_limit(config)) {
save_config(config);
}
break;
case 7:
// Toggle Response Streaming
if (toggle_streaming(config)) {
save_config(config);
}
break;
case 8:
// Toggle Progress Animation
if (toggle_progress_animation(config)) {
save_config(config);
}
break;
case 9:
// View Active Configuration
show_active_configuration(config);
break;
case 0:
// Delete Entire Configuration
if (delete_config_file()) {
display_message("Configuration deleted. Exiting settings.", COLOR_GREEN);
free_config(config);
return false;
}
break;
default:
display_message("Invalid choice. Please enter a number between 1 and 0, or X to exit.", COLOR_RED);
break;
}
}
if (save_config(config)) {
display_message("Configuration saved successfully!", COLOR_GREEN);
free_config(config);
return true;
} else {
display_message("Failed to save configuration.", COLOR_RED);
free_config(config);
return false;
}
}
// For programmatic calls (not direct flag), just return success
free_config(config);
return true;
}
bool configure_ollama_service(config_t *config) {
display_message("\n--- Ollama Service Setup ---", COLOR_GREEN);
char server_address[MAX_SERVER_ADDR_LEN];
char current_server[MAX_SERVER_ADDR_LEN];
strcpy(current_server, config->ollama.server_address);
if (strlen(current_server) > 0) {
display_message("Current server address: ", COLOR_BLUE);
printf("%s\n", current_server);
}
display_message("Enter Ollama server address (e.g., http://localhost:11434): ", COLOR_YELLOW);
char *input = read_line();
if (!input) {
return false;
}
if (strlen(input) == 0 && strlen(current_server) > 0) {
strcpy(server_address, current_server);
} else if (strlen(input) > 0) {
strcpy(server_address, input);
// Add http:// if not present
if (strncmp(server_address, "http://", 7) != 0 && strncmp(server_address, "https://", 8) != 0) {
char temp[MAX_SERVER_ADDR_LEN];
if (snprintf(temp, sizeof(temp), "http://%s", server_address) >= (int)sizeof(temp)) {
display_message("Server address too long.", COLOR_RED);
return false;
}
strcpy(server_address, temp);
}
} else {
display_message("Server address cannot be empty.", COLOR_RED);
free(input);
return false;
}
free(input);
// Test connection and fetch models
char **models = NULL;
int model_count = 0;
if (!fetch_ollama_models(server_address, &models, &model_count)) {
display_message("Failed to connect to Ollama server or fetch models.", COLOR_RED);
return false;
}
if (model_count == 0) {
display_message("No models found on the Ollama server.", COLOR_YELLOW);
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
return false;
}
// Display available models
display_message("\nAvailable models:", COLOR_GREEN);
for (int i = 0; i < model_count; i++) {
printf(" %d. %s\n", i + 1, models[i]);
}
// Select model
display_message("\nEnter the number of the model to use: ", COLOR_YELLOW);
char *model_choice = read_line();
if (!model_choice) {
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
return false;
}
int choice = atoi(model_choice);
free(model_choice);
if (choice < 1 || choice > model_count) {
display_message("Invalid model selection.", COLOR_RED);
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
return false;
}
// Update configuration
strcpy(config->ollama.server_address, server_address);
strcpy(config->ollama.model, models[choice - 1]);
config->ollama.render_markdown = true;
// Set as active service if no active service or if it's the only one
if (strlen(config->active_llm_service) == 0) {
strcpy(config->active_llm_service, LLM_SERVICE_OLLAMA);
display_message("Ollama set as active LLM service.", COLOR_GREEN);
}
display_message("Ollama service configured successfully!", COLOR_GREEN);
// Cleanup
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
return true;
}
bool configure_gemini_service(config_t *config) {
display_message("\n--- Gemini Service Setup ---", COLOR_GREEN);
// First, manage API keys
if (!manage_gemini_api_keys(config)) {
display_message("Failed to configure Gemini API keys.", COLOR_RED);
return false;
}
// Get active API key
char nickname[MAX_NICKNAME_LEN];
char *api_key = get_active_gemini_key_value(&config->gemini, nickname);
if (!api_key) {
display_message("No active API key configured.", COLOR_RED);
return false;
}
// Fetch available models
char **models = NULL;
int model_count = 0;
if (!fetch_gemini_models(api_key, &models, &model_count)) {
display_message("Failed to fetch Gemini models.", COLOR_RED);
return false;
}
if (model_count == 0) {
display_message("No models found for Gemini API.", COLOR_YELLOW);
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
return false;
}
// Display available models
display_message("\nAvailable Gemini models:", COLOR_GREEN);
for (int i = 0; i < model_count; i++) {
printf(" %d. %s\n", i + 1, models[i]);
}
// Select model
display_message("\nEnter the number of the model to use: ", COLOR_YELLOW);
char *model_choice = read_line();
if (!model_choice) {
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
return false;
}
int choice = atoi(model_choice);
free(model_choice);
if (choice < 1 || choice > model_count) {
display_message("Invalid model selection.", COLOR_RED);
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
return false;
}
// Update configuration
strcpy(config->gemini.model, models[choice - 1]);
config->gemini.render_markdown = true;
// Set as active service if no active service
if (strlen(config->active_llm_service) == 0) {
strcpy(config->active_llm_service, LLM_SERVICE_GEMINI);
display_message("Gemini set as active LLM service.", COLOR_GREEN);
}
display_message("Gemini service configured successfully!", COLOR_GREEN);
// Cleanup
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
return true;
}
bool manage_gemini_api_keys(config_t *config) {
display_message("\n--- Gemini API Key Management ---", COLOR_GREEN);
while (true) {
// Show active API key details first
if (config->gemini.api_key_count > 0 && strlen(config->gemini.active_api_key_nickname) > 0) {
display_message("\nActive API Key:", COLOR_GREEN);
char *active_key = get_active_gemini_key_value(&config->gemini, config->gemini.active_api_key_nickname);
if (active_key) {
printf(" Nickname: %s\n", config->gemini.active_api_key_nickname);
printf(" API Key : %s\n", active_key);
} else {
display_message(" No active API key set", COLOR_YELLOW);
}
}
display_message("\nAll API keys:", COLOR_BLUE);
if (config->gemini.api_key_count == 0) {
display_message("No API keys configured.", COLOR_YELLOW);
} else {
for (int i = 0; i < config->gemini.api_key_count; i++) {
bool is_active = (strcmp(config->gemini.api_keys[i].nickname,
config->gemini.active_api_key_nickname) == 0);
printf(" %d. %s%s\n", i + 1, config->gemini.api_keys[i].nickname,
is_active ? " (active)" : "");
}
}
display_message("\nOptions:", COLOR_BLUE);
display_message("1. Add new API key", COLOR_BLUE);
if (config->gemini.api_key_count > 0) {
display_message("2. Set API key as active", COLOR_BLUE);
display_message("3. Remove API key", COLOR_BLUE);
display_message("4. Show all API keys & nicknames", COLOR_BLUE);
}
display_message("0. Back to main menu", COLOR_BLUE);
// Add additional options for key management
bool has_active = (config->gemini.api_key_count > 0 && strlen(config->gemini.active_api_key_nickname) > 0);
if (has_active) {
display_message("C. Continue / Confirm selection", COLOR_BLUE);
}
display_message("X. Cancel / Exit key management", COLOR_BLUE);
display_message("\nEnter your choice: ", COLOR_YELLOW);
char *choice = read_line();
if (!choice) {
continue;
}
int menu_choice = -1;
bool is_cancel = false, is_confirm = false;
if (strlen(choice) == 1) {
if (choice[0] == 'x' || choice[0] == 'X') {
is_cancel = true;
} else if ((choice[0] == 'c' || choice[0] == 'C') && has_active) {
is_confirm = true;
} else {
menu_choice = atoi(choice);
}
} else {
menu_choice = atoi(choice);
}
free(choice);
if (is_cancel) {
display_message("Gemini key management cancelled.", COLOR_YELLOW);
return has_active;
}
if (is_confirm) {
return true;
}
switch (menu_choice) {
case 0:
return true;
case 1:
{
display_message("Enter nickname for the API key: ", COLOR_YELLOW);
char *nickname = read_line();
if (!nickname || strlen(nickname) == 0) {
display_message("Nickname cannot be empty.", COLOR_RED);
free(nickname);
break;
}
display_message("Enter the API key: ", COLOR_YELLOW);
char *api_key = read_line();
if (!api_key || strlen(api_key) == 0) {
display_message("API key cannot be empty.", COLOR_RED);
free(nickname);
free(api_key);
break;
}
if (add_gemini_api_key(&config->gemini, nickname, api_key)) {
display_message("API key added successfully.", COLOR_GREEN);
} else {
display_message("Failed to add API key.", COLOR_RED);
}
free(nickname);
free(api_key);
}
break;
case 2:
if (config->gemini.api_key_count > 0) {
display_message("Select API key to activate:", COLOR_YELLOW);
for (int i = 0; i < config->gemini.api_key_count; i++) {
printf(" %d. %s\n", i + 1, config->gemini.api_keys[i].nickname);
}
char *key_choice = read_line();
if (key_choice) {
int key_idx = atoi(key_choice) - 1;
free(key_choice);
if (key_idx >= 0 && key_idx < config->gemini.api_key_count) {
if (set_active_gemini_api_key(&config->gemini, config->gemini.api_keys[key_idx].nickname)) {
display_message("API key activated successfully.", COLOR_GREEN);
} else {
display_message("Failed to activate API key.", COLOR_RED);
}
} else {
display_message("Invalid selection.", COLOR_RED);
}
}
}
break;
case 3:
if (config->gemini.api_key_count > 0) {
display_message("Select API key to remove:", COLOR_YELLOW);
for (int i = 0; i < config->gemini.api_key_count; i++) {
printf(" %d. %s\n", i + 1, config->gemini.api_keys[i].nickname);
}
char *remove_choice = read_line();
if (remove_choice) {
int remove_idx = atoi(remove_choice) - 1;
free(remove_choice);
if (remove_idx >= 0 && remove_idx < config->gemini.api_key_count) {
if (remove_gemini_api_key(&config->gemini, config->gemini.api_keys[remove_idx].nickname)) {
display_message("API key removed successfully.", COLOR_GREEN);
} else {
display_message("Failed to remove API key.", COLOR_RED);
}
} else {
display_message("Invalid selection.", COLOR_RED);
}
}
}
break;
case 4:
if (config->gemini.api_key_count > 0) {
display_message("\n--- All Gemini API Keys ---", COLOR_GREEN);
for (int i = 0; i < config->gemini.api_key_count; i++) {
bool is_active = (strcmp(config->gemini.api_keys[i].nickname,
config->gemini.active_api_key_nickname) == 0);
display_message("", COLOR_BLUE);
printf("Nickname: %s%s\n", config->gemini.api_keys[i].nickname,
is_active ? " (ACTIVE)" : "");
printf("API Key : %s\n", config->gemini.api_keys[i].key);
if (i < config->gemini.api_key_count - 1) {
printf("---\n");
}
}
printf("\n");
} else {
display_message("No API keys configured.", COLOR_YELLOW);
}
break;
default:
display_message("Invalid choice.", COLOR_RED);
break;
}
}
}
bool change_active_model(config_t *config) {
if (strlen(config->active_llm_service) == 0) {
display_message("No active LLM service.", COLOR_YELLOW);
return false;
}
display_message("Change model for active service: ", COLOR_GREEN);
printf("%s\n", config->active_llm_service);
if (strcmp(config->active_llm_service, LLM_SERVICE_OLLAMA) == 0) {
char **models = NULL;
int model_count = 0;
if (fetch_ollama_models(config->ollama.server_address, &models, &model_count)) {
display_message("Available models:", COLOR_GREEN);
for (int i = 0; i < model_count; i++) {
bool is_current = (strcmp(models[i], config->ollama.model) == 0);
printf(" %d. %s%s\n", i + 1, models[i], is_current ? " (current)" : "");
}
display_message("Enter model number: ", COLOR_YELLOW);
char *choice = read_line();
if (choice) {
int model_idx = atoi(choice) - 1;
free(choice);
if (model_idx >= 0 && model_idx < model_count) {
strcpy(config->ollama.model, models[model_idx]);
display_message("Model changed successfully.", COLOR_GREEN);
} else {
display_message("Invalid model selection.", COLOR_RED);
}
}
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
}
} else if (strcmp(config->active_llm_service, LLM_SERVICE_GEMINI) == 0) {
char nickname[MAX_NICKNAME_LEN];
char *api_key = get_active_gemini_key_value(&config->gemini, nickname);
if (api_key) {
char **models = NULL;
int model_count = 0;
if (fetch_gemini_models(api_key, &models, &model_count)) {
display_message("Available models:", COLOR_GREEN);
for (int i = 0; i < model_count; i++) {
bool is_current = (strcmp(models[i], config->gemini.model) == 0);
printf(" %d. %s%s\n", i + 1, models[i], is_current ? " (current)" : "");
}
display_message("Enter model number: ", COLOR_YELLOW);
char *choice = read_line();
if (choice) {
int model_idx = atoi(choice) - 1;
free(choice);
if (model_idx >= 0 && model_idx < model_count) {
strcpy(config->gemini.model, models[model_idx]);
display_message("Model changed successfully.", COLOR_GREEN);
} else {
display_message("Invalid model selection.", COLOR_RED);
}
}
for (int i = 0; i < model_count; i++) {
free(models[i]);
}
free(models);
}
} else {
display_message("No active API key found.", COLOR_RED);
}
} else if (strcmp(config->active_llm_service, LLM_SERVICE_OPENROUTER) == 0) {
char nickname[MAX_NICKNAME_LEN];
char *api_key = get_active_openrouter_key_value(&config->openrouter, nickname);
if (!api_key) {
display_message("No active OpenRouter API key configured.", COLOR_RED);
return false;
}
openrouter_model_t *models = NULL;
int model_count = 0;
if (fetch_openrouter_models_detailed(api_key, &models, &model_count)) {
int current_page = 0;
const int models_per_page = 15;
int total_pages = (model_count + models_per_page - 1) / models_per_page;
while (true) {
// Display header
display_message("\n=== OpenRouter Models (FREE models listed first) ===", COLOR_GREEN);
printf("Page %d of %d (%d total models)\n\n", current_page + 1, total_pages, model_count);
// Calculate display range for current page
int start_idx = current_page * models_per_page;
int end_idx = start_idx + models_per_page;
if (end_idx > model_count) end_idx = model_count;
// Display models for current page
for (int i = start_idx; i < end_idx; i++) {
bool is_current = (strcmp(models[i].id, config->openrouter.model) == 0);
const char *status_icon = models[i].is_free ? "🆓" : "💰";
printf(" %2d. %s %s%s\n",
i + 1,
status_icon,
models[i].id,
is_current ? " (current)" : "");
}
// Display navigation options
printf("\nOptions:\n");
printf(" Enter model number (1-%d) to select\n", model_count);
if (current_page > 0) printf(" 'p' for previous page\n");
if (current_page < total_pages - 1) printf(" 'n' for next page\n");
printf(" 'q' to quit without changing\n");
display_message("\nYour choice: ", COLOR_YELLOW);
char *choice = read_line();
if (!choice) break;
// Handle navigation
if (strcmp(choice, "n") == 0 && current_page < total_pages - 1) {
current_page++;
free(choice);
continue;
} else if (strcmp(choice, "p") == 0 && current_page > 0) {
current_page--;
free(choice);
continue;
} else if (strcmp(choice, "q") == 0) {
free(choice);
break;
}
// Handle model selection
int model_idx = atoi(choice) - 1;
free(choice);
if (model_idx >= 0 && model_idx < model_count) {
strncpy(config->openrouter.model, models[model_idx].id, MAX_MODEL_NAME_LEN - 1);
config->openrouter.model[MAX_MODEL_NAME_LEN - 1] = '\0';
display_message("Model changed successfully to: ", COLOR_GREEN);
printf("%s %s\n",
models[model_idx].is_free ? "🆓" : "💰",
models[model_idx].id);
break;
} else {
display_message("Invalid selection. Try again.", COLOR_RED);
}
}
// Free models
free(models);
} else {
display_message("Failed to fetch models from OpenRouter. Please check your API key.", COLOR_RED);
return false;
}
} else {
display_message("Unknown active LLM service.", COLOR_RED);
return false;
}
return true;
}
bool switch_llm_service(config_t *config) {
display_message("Switch active LLM service:", COLOR_GREEN);
display_message("1. Ollama", COLOR_BLUE);
display_message("2. Gemini", COLOR_BLUE);
display_message("3. OpenRouter", COLOR_BLUE);
display_message("Enter your choice (1-3): ", COLOR_YELLOW);
char *choice = read_line();
if (!choice) {
return false;
}
int service_choice = atoi(choice);
free(choice);
if (service_choice == 1) {
if (strlen(config->ollama.server_address) == 0) {
display_message("Ollama not configured. Please configure it first.", COLOR_YELLOW);
return false;
}
strcpy(config->previous_active_llm_service, config->active_llm_service);
strcpy(config->active_llm_service, LLM_SERVICE_OLLAMA);
display_message("Switched to Ollama.", COLOR_GREEN);
} else if (service_choice == 2) {
if (config->gemini.api_key_count == 0) {
display_message("Gemini not configured. Please configure it first.", COLOR_YELLOW);
return false;
}
strcpy(config->previous_active_llm_service, config->active_llm_service);
strcpy(config->active_llm_service, LLM_SERVICE_GEMINI);
display_message("Switched to Gemini.", COLOR_GREEN);
} else if (service_choice == 3) {
if (config->openrouter.api_key_count == 0) {
display_message("OpenRouter not configured. Please configure it first.", COLOR_YELLOW);
return false;
}
strcpy(config->previous_active_llm_service, config->active_llm_service);
strcpy(config->active_llm_service, LLM_SERVICE_OPENROUTER);
display_message("Switched to OpenRouter.", COLOR_GREEN);
} else {
display_message("Invalid choice.", COLOR_RED);
return false;
}
return true;
}
bool toggle_markdown_rendering(config_t *config) {
if (strlen(config->active_llm_service) == 0) {
display_message("No active LLM service.", COLOR_YELLOW);
return false;
}
if (strcmp(config->active_llm_service, LLM_SERVICE_OLLAMA) == 0) {
config->ollama.render_markdown = !config->ollama.render_markdown;
display_message("Markdown rendering for Ollama: ", COLOR_GREEN);
printf("%s\n", config->ollama.render_markdown ? "Enabled" : "Disabled");
} else if (strcmp(config->active_llm_service, LLM_SERVICE_GEMINI) == 0) {
config->gemini.render_markdown = !config->gemini.render_markdown;
display_message("Markdown rendering for Gemini: ", COLOR_GREEN);
printf("%s\n", config->gemini.render_markdown ? "Enabled" : "Disabled");
}
return true;
}
bool toggle_streaming(config_t *config) {
config->enable_streaming = !config->enable_streaming;
display_message("Response streaming: ", COLOR_GREEN);
printf("%s\n", config->enable_streaming ? "Enabled" : "Disabled");
return true;
}
bool set_history_limit(config_t *config) {
display_message("Current history limit: ", COLOR_BLUE);
printf("%d\n", config->interactive_history_limit);
display_message("Enter new history limit (1-50): ", COLOR_YELLOW);
char *input = read_line();
if (!input) {
return false;
}
int new_limit = atoi(input);
free(input);
if (new_limit >= 1 && new_limit <= 50) {
config->interactive_history_limit = new_limit;
display_message("History limit updated successfully.", COLOR_GREEN);
return true;
} else {
display_message("Invalid history limit. Must be between 1 and 50.", COLOR_RED);
return false;
}
}
void show_active_configuration(config_t *config) {
display_message("\n--- Current Configuration ---", COLOR_GREEN);
display_message("Active LLM Service: ", COLOR_BLUE);
printf("%s\n", strlen(config->active_llm_service) > 0 ? config->active_llm_service : "None");
display_message("Previous LLM Service: ", COLOR_BLUE);
printf("%s\n", strlen(config->previous_active_llm_service) > 0 ? config->previous_active_llm_service : "None");
display_message("Interactive History Limit: ", COLOR_BLUE);
printf("%d\n", config->interactive_history_limit);
display_message("Response Streaming: ", COLOR_BLUE);
printf("%s\n", config->enable_streaming ? "Enabled" : "Disabled");
display_message("Progress Animation: ", COLOR_BLUE);
printf("%s\n", config->show_progress_animation ? "Enabled" : "Disabled");
if (strlen(config->ollama.server_address) > 0) {
display_message("\nOllama Configuration:", COLOR_GREEN);
display_message(" Server Address: ", COLOR_BLUE);
printf("%s\n", config->ollama.server_address);
display_message(" Model: ", COLOR_BLUE);
printf("%s\n", config->ollama.model);
display_message(" Markdown Rendering: ", COLOR_BLUE);
printf("%s\n", config->ollama.render_markdown ? "Enabled" : "Disabled");
}
if (config->gemini.api_key_count > 0) {
display_message("\nGemini Configuration:", COLOR_GREEN);
display_message(" Active API Key: ", COLOR_BLUE);
printf("%s\n", config->gemini.active_api_key_nickname);
display_message(" Model: ", COLOR_BLUE);
printf("%s\n", config->gemini.model);
display_message(" Markdown Rendering: ", COLOR_BLUE);
printf("%s\n", config->gemini.render_markdown ? "Enabled" : "Disabled");
display_message(" API Keys: ", COLOR_BLUE);
printf("%d configured\n", config->gemini.api_key_count);
}
if (strcmp(config->active_llm_service, LLM_SERVICE_OPENROUTER) == 0 || strcmp(config->active_llm_service, "") == 0) {
display_message("\n--- OpenRouter Configuration ---", COLOR_GREEN);
display_message(" Active API Key: ", COLOR_BLUE);
if (config->openrouter.api_key_count > 0) {
printf("%s\n", config->openrouter.active_api_key_nickname);
} else {
printf("Not configured\n");
}
display_message(" Model: ", COLOR_BLUE);
printf("%s\n", config->openrouter.model);
display_message(" Site URL: ", COLOR_BLUE);
printf("%s\n", config->openrouter.site_url);
display_message(" Site Name: ", COLOR_BLUE);
printf("%s\n", config->openrouter.site_name);
display_message(" Markdown Rendering: ", COLOR_BLUE);
printf("%s\n", config->openrouter.render_markdown ? "Enabled" : "Disabled");
}
}
bool jump_to_previous_llm(config_t *config) {
if (strlen(config->previous_active_llm_service) == 0) {
display_message("No previous LLM service to jump to.", COLOR_YELLOW);
return false;
}
// Swap active and previous
char temp[32];
strcpy(temp, config->active_llm_service);
strcpy(config->active_llm_service, config->previous_active_llm_service);
strcpy(config->previous_active_llm_service, temp);
display_message("Jumped to previous LLM service: ", COLOR_GREEN);
printf("%s\n", config->active_llm_service);
return true;
}
bool toggle_progress_animation(config_t *config) {
config->show_progress_animation = !config->show_progress_animation;
display_message("Progress animation: ", COLOR_GREEN);
printf("%s\n", config->show_progress_animation ? "Enabled" : "Disabled");
return true;
}
bool configure_openrouter_service(config_t *config) {
display_message("\n--- OpenRouter Service Setup ---", COLOR_GREEN);
// Get API key
display_message("Enter your OpenRouter API key: ", COLOR_YELLOW);
char *api_key = read_line();
if (!api_key || strlen(api_key) == 0) {
display_message("API key is required for OpenRouter.", COLOR_RED);
if (api_key) free(api_key);
return false;
}
// Trim whitespace and validate
char *trimmed_key = trim_whitespace(api_key);
if (strlen(trimmed_key) == 0) {
display_message("Invalid API key.", COLOR_RED);
free(api_key);
return false;
}
// Add the API key with a default nickname
char default_nickname[MAX_NICKNAME_LEN];
snprintf(default_nickname, sizeof(default_nickname), "default-%d", (int)time(NULL) % 1000);
if (!add_openrouter_api_key(&config->openrouter, default_nickname, trimmed_key)) {
display_message("Failed to add API key.", COLOR_RED);
free(api_key);
return false;
}
free(api_key);
// Get optional site URL for attribution
display_message("Enter your site URL (optional, for rankings): ", COLOR_YELLOW);
char *site_url = read_line();
if (site_url) {
char *trimmed_url = trim_whitespace(site_url);
strncpy(config->openrouter.site_url, trimmed_url, MAX_SERVER_ADDR_LEN - 1);
config->openrouter.site_url[MAX_SERVER_ADDR_LEN - 1] = '\0';
free(site_url);
}
// Get optional site name for attribution
display_message("Enter your site name (optional, for rankings): ", COLOR_YELLOW);
char *site_name = read_line();
if (site_name) {
char *trimmed_name = trim_whitespace(site_name);
strncpy(config->openrouter.site_name, trimmed_name, MAX_NICKNAME_LEN - 1);
config->openrouter.site_name[MAX_NICKNAME_LEN - 1] = '\0';
free(site_name);
}
// Test the API key by fetching models
openrouter_model_t *models = NULL;
int model_count = 0;
char nickname[MAX_NICKNAME_LEN];
char *test_api_key = get_active_openrouter_key_value(&config->openrouter, nickname);
if (!test_api_key || !fetch_openrouter_models_detailed(test_api_key, &models, &model_count)) {
display_message("Failed to validate API key or fetch models. Please check your API key.", COLOR_RED);