-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.c
More file actions
501 lines (448 loc) · 21.9 KB
/
interactive.c
File metadata and controls
501 lines (448 loc) · 21.9 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
#include "deepshell.h"
// Logo functions for interactive mode
void print_interactive_logo() {
// Clear screen and move to top
printf("\033[2J\033[H");
// Add some spacing
printf("\n\n");
// DeepShell ASCII Art Logo - actually spelling out "DeepShell"
printf("%s", COLOR_CYAN);
printf(" ██████╗ ███████╗███████╗██████╗ ███████╗██╗ ██╗███████╗██╗ ██╗ \n");
printf(" ██╔══██╗██╔════╝██╔════╝██╔══██╗██╔════╝██║ ██║██╔════╝██║ ██║ \n");
printf(" ██║ ██║█████╗ █████╗ ██████╔╝███████╗███████║█████╗ ██║ ██║ \n");
printf(" ██║ ██║██╔══╝ ██╔══╝ ██╔═══╝ ╚════██║██╔══██║██╔══╝ ██║ ██║ \n");
printf(" ██████╔╝███████╗███████╗██║ ███████║██║ ██║███████╗███████╗███████╗\n");
printf(" ╚═════╝ ╚══════╝╚══════╝╚═╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝\n");
printf("%s", COLOR_RESET);
printf("\n");
// Subtitle with version info - PERFECT RECTANGLE
printf("%s", COLOR_BLUE);
printf(" ╔══════════════════════════════════════════════════════════════╗\n");
printf(" ║ AI-Powered Shell Interface ║\n");
printf(" ║ Version %s ║\n", DEEPSHELL_VERSION);
printf(" ╚══════════════════════════════════════════════════════════════╝\n");
printf("%s", COLOR_RESET);
printf("\n");
// Feature highlights
printf("%s", COLOR_GREEN);
printf(" ✨ Multi-LLM Support (Ollama, Gemini, and OpenRouter) 🔧 Interactive Mode 📝 Markdown Rendering\n");
printf(" 🚀 Streaming Responses ⚙️ Easy Configuration 💾 Conversation History\n");
printf("%s", COLOR_RESET);
printf("\n");
// Status indicator
printf("%s", COLOR_YELLOW);
printf(" [*] DeepShell is ready to assist you with AI-powered interactions!\n");
printf("%s", COLOR_RESET);
printf("\n");
}
bool start_interactive_session(config_t *config) {
if (!config) {
return false;
}
// Display the DeepShell logo for interactive mode
print_interactive_logo();
display_message("\n--- DeepShell Interactive Mode ---", COLOR_GREEN);
display_message("Type 'help' to see all available commands.", COLOR_YELLOW);
// Check if we have a valid active service
if (strlen(config->active_llm_service) == 0) {
display_message("No active LLM service configured.", COLOR_RED);
return false;
}
// Validate service configuration
if (strcmp(config->active_llm_service, LLM_SERVICE_OLLAMA) == 0) {
if (strlen(config->ollama.server_address) == 0) {
display_message("Ollama server address not configured.", COLOR_RED);
return false;
}
} 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) {
display_message("No active Gemini API key found.", COLOR_RED);
return false;
}
} 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 found.", COLOR_RED);
return false;
}
} else {
display_message("Unknown active LLM service.", COLOR_RED);
return false;
}
// Initialize conversation history
conversation_message_t *history = malloc(config->interactive_history_limit * 2 * sizeof(conversation_message_t));
if (!history) {
display_message("Failed to allocate conversation history.", COLOR_RED);
return false;
}
int history_count = 0;
int max_history_items = config->interactive_history_limit * 2;
while (true) {
printf("%s> %s", COLOR_BLUE, COLOR_RESET);
fflush(stdout);
char *user_input = read_line();
if (!user_input) {
display_message("Failed to read input.", COLOR_RED);
break;
}
// Check for exit commands
if (strcmp(user_input, "exit") == 0 || strcmp(user_input, "quit") == 0) {
display_message("Exiting interactive mode.", COLOR_BLUE);
free(user_input);
break;
}
// Check for save command
if (strncmp(user_input, "save", 4) == 0) {
// Check if there's any conversation history (at least one model response)
bool has_model_response = false;
for (int i = 0; i < history_count; i++) {
if (strcmp(history[i].role, "model") == 0) {
has_model_response = true;
break;
}
}
if (!has_model_response) {
display_message("Please search before saving. No previous response found.", COLOR_YELLOW);
free(user_input);
continue;
}
// Parse filename from command
char *filename = NULL;
if (strlen(user_input) > 4) {
// Skip "save" and any following spaces
char *filename_start = user_input + 4;
while (*filename_start == ' ') {
filename_start++;
}
if (strlen(filename_start) > 0) {
filename = strdup_safe(filename_start);
}
}
// If no filename provided, ask for it
if (!filename) {
display_message("Enter filename (.md extension will be added automatically): ", COLOR_YELLOW);
filename = read_line();
if (!filename || strlen(filename) == 0) {
display_message("Save cancelled.", COLOR_YELLOW);
if (filename) free(filename);
free(user_input);
continue;
}
}
// Auto-append .md extension if not provided
if (filename && strlen(filename) > 0) {
// Check if filename already ends with .md
size_t len = strlen(filename);
if (len < 3 || strcmp(filename + len - 3, ".md") != 0) {
// Append .md extension
char *new_filename = malloc(len + 4);
if (new_filename) {
strcpy(new_filename, filename);
strcat(new_filename, ".md");
free(filename);
filename = new_filename;
}
}
}
// Validate filename
if (!is_valid_filename(filename)) {
display_message("Invalid filename. Please enter a valid filename: ", COLOR_RED);
free(filename);
filename = read_line();
if (!filename || strlen(filename) == 0) {
display_message("Save cancelled.", COLOR_YELLOW);
if (filename) free(filename);
free(user_input);
continue;
}
// Auto-append .md extension for the new filename too
size_t len = strlen(filename);
if (len < 3 || strcmp(filename + len - 3, ".md") != 0) {
char *new_filename = malloc(len + 4);
if (new_filename) {
strcpy(new_filename, filename);
strcat(new_filename, ".md");
free(filename);
filename = new_filename;
}
}
if (!is_valid_filename(filename)) {
display_message("Save cancelled.", COLOR_YELLOW);
if (filename) free(filename);
free(user_input);
continue;
}
}
// Find the last model response
char *last_response = NULL;
for (int i = history_count - 1; i >= 0; i--) {
if (strcmp(history[i].role, "model") == 0) {
last_response = history[i].content;
break;
}
}
if (last_response) {
if (save_response_to_file(last_response, filename)) {
// Success message already printed by save_response_to_file
} else {
display_message("Save failed.", COLOR_RED);
}
} else {
display_message("No model response found to save.", COLOR_RED);
}
free(filename);
free(user_input);
continue;
}
// Check for open command
if (strncmp(user_input, "open", 4) == 0) {
// Parse filepath from command
char *filepath = NULL;
if (strlen(user_input) > 4) {
// Skip "open" and any following spaces
char *filepath_start = user_input + 4;
while (*filepath_start == ' ') {
filepath_start++;
}
if (strlen(filepath_start) > 0) {
filepath = strdup_safe(filepath_start);
}
}
// If no filepath provided, ask for it
if (!filepath) {
display_message("Enter file path: ", COLOR_YELLOW);
filepath = read_line();
if (!filepath || strlen(filepath) == 0) {
display_message("Open cancelled.", COLOR_YELLOW);
if (filepath) free(filepath);
free(user_input);
continue;
}
}
// Read file content first to check if it exists and is readable
char *file_content = read_file_content(filepath);
if (!file_content) {
free(filepath);
free(user_input);
continue;
}
// Check if file is text-based (only after successful read)
if (!is_text_file(filepath)) {
display_message("Error: File is not a text-based file (binary detected).", COLOR_RED);
free(file_content);
free(filepath);
free(user_input);
continue;
}
// Prompt user for instructions
display_message("Enter instructions for the LLM about this data:", COLOR_YELLOW);
char *user_instructions = read_line();
if (!user_instructions || strlen(user_instructions) == 0) {
// Use default instructions if none provided
user_instructions = strdup("Please analyze this data");
}
// Combine user instructions with file content
size_t total_len = strlen(user_instructions) + strlen(file_content) + 10; // Extra space for separators
char *combined_prompt = malloc(total_len);
if (!combined_prompt) {
display_message("Error: Not enough memory to process file.", COLOR_RED);
free(user_instructions);
free(file_content);
free(filepath);
free(user_input);
continue;
}
// Format: [User instructions]\n\n[File content]
strcpy(combined_prompt, user_instructions);
strcat(combined_prompt, "\n\n");
strcat(combined_prompt, file_content);
// Add user instructions as separate history entry
if (history_count < max_history_items) {
strcpy(history[history_count].role, "user");
strncpy(history[history_count].content, user_instructions, MAX_RESPONSE_LEN - 1);
history[history_count].content[MAX_RESPONSE_LEN - 1] = '\0';
history_count++;
}
// Add file content as separate history entry
if (history_count < max_history_items) {
strcpy(history[history_count].role, "user");
strncpy(history[history_count].content, file_content, MAX_RESPONSE_LEN - 1);
history[history_count].content[MAX_RESPONSE_LEN - 1] = '\0';
history_count++;
}
// Process the combined prompt as a query
char *response_text = NULL;
if (strcmp(config->active_llm_service, LLM_SERVICE_OLLAMA) == 0) {
response_text = send_ollama_query(config->ollama.server_address,
config->ollama.model, combined_prompt,
history, history_count, config);
} 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) {
response_text = send_gemini_query(api_key, config->gemini.model,
combined_prompt, history, history_count, config);
} else {
display_message("No active Gemini 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) {
response_text = send_openrouter_query(api_key,
config->openrouter.model, combined_prompt,
history, history_count, config);
} else {
display_message("No active OpenRouter API key found.", COLOR_RED);
}
} else {
display_message("No active LLM service configured.", COLOR_RED);
}
// Display response
if (response_text) {
if (config->enable_streaming) {
display_message("Response:", COLOR_GREEN);
printf("%s\n", response_text);
} else {
display_message("Response:", COLOR_GREEN);
print_markdown(response_text);
}
// Add model response to history
if (history_count < max_history_items) {
strcpy(history[history_count].role, "model");
strncpy(history[history_count].content, response_text, MAX_RESPONSE_LEN - 1);
history[history_count].content[MAX_RESPONSE_LEN - 1] = '\0';
history_count++;
}
free(response_text);
}
free(combined_prompt);
free(user_instructions);
free(file_content);
free(filepath);
free(user_input);
continue;
}
// Check for help command
if (strncmp(user_input, "help", 4) == 0) {
display_interactive_help();
free(user_input);
continue;
}
// Skip empty input
if (strlen(user_input) == 0) {
free(user_input);
continue;
}
// Trim history if it's too long
if (max_history_items > 0 && history_count >= max_history_items) {
// Keep the last N-1 pairs to make room for the new one
for (int i = 0; i < history_count - 2; i++) {
strcpy(history[i].role, history[i + 2].role);
strcpy(history[i].content, history[i + 2].content);
}
history_count -= 2;
}
// Send query to appropriate service
bool query_success = false;
if (strcmp(config->active_llm_service, LLM_SERVICE_OLLAMA) == 0) {
char *response_text = send_ollama_query(config->ollama.server_address,
config->ollama.model, user_input,
history, history_count, config);
query_success = (response_text != NULL);
if (query_success) {
// Print response
display_message("--- Ollama Response ---", COLOR_GREEN);
if (config->ollama.render_markdown) {
print_markdown(response_text);
} else {
printf("%s\n", response_text);
}
display_message("-----------------------", COLOR_GREEN);
}
if (response_text) {
// Add model response to history
if (history_count < max_history_items) {
strcpy(history[history_count].role, "model");
strncpy(history[history_count].content, response_text, MAX_RESPONSE_LEN - 1);
history[history_count].content[MAX_RESPONSE_LEN - 1] = '\0';
history_count++;
}
free(response_text);
}
} 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 *response_text = send_gemini_query(api_key, config->gemini.model,
user_input, history, history_count, config);
query_success = (response_text != NULL);
if (query_success) {
display_message("--- Gemini Response ---", COLOR_GREEN);
if (config->gemini.render_markdown) {
print_markdown(response_text);
} else {
printf("%s\n", response_text);
}
display_message("------------------------", COLOR_GREEN);
}
if (response_text) {
// Add model response to history
if (history_count < max_history_items) {
strcpy(history[history_count].role, "model");
strncpy(history[history_count].content, response_text, MAX_RESPONSE_LEN - 1);
history[history_count].content[MAX_RESPONSE_LEN - 1] = '\0';
history_count++;
}
free(response_text);
}
}
} 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) {
char *response_text = send_openrouter_query(api_key,
config->openrouter.model, user_input,
history, history_count, config);
query_success = (response_text != NULL);
if (query_success) {
// Print response
display_message("--- OpenRouter Response ---", COLOR_GREEN);
if (config->openrouter.render_markdown) {
print_markdown(response_text);
} else {
printf("%s\n", response_text);
}
display_message("---------------------------", COLOR_GREEN);
}
if (response_text) {
// Add model response to history
if (history_count < max_history_items) {
strcpy(history[history_count].role, "model");
strncpy(history[history_count].content, response_text, MAX_RESPONSE_LEN - 1);
history[history_count].content[MAX_RESPONSE_LEN - 1] = '\0';
history_count++;
}
free(response_text);
}
}
}
if (query_success) {
// Add to history
if (history_count < max_history_items) {
strcpy(history[history_count].role, "user");
strncpy(history[history_count].content, user_input, MAX_RESPONSE_LEN - 1);
history[history_count].content[MAX_RESPONSE_LEN - 1] = '\0';
history_count++;
}
} else {
display_message("Failed to get response from LLM.", COLOR_RED);
}
free(user_input);
}
free(history);
return true;
}