Repository navigation
Expand file tree
/
Copy pathbolt_terminal_ai.cpp
More file actions
180 lines (154 loc) Β· 5.5 KB
/
Copy pathbolt_terminal_ai.cpp
File metadata and controls
180 lines (154 loc) Β· 5.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
#include <cstdio>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include "include/bolt/ai/enhanced_ai_manager.hpp"
#ifdef _WIN32
#include <io.h> // For _isatty / _fileno
#define isatty _isatty
#define fileno _fileno
#else
#include <unistd.h> // For isatty
#endif
class BoltTerminalAI {
private:
std::unique_ptr<bolt::ai::EnhancedAIManager> ai_manager_;
std::string session_id_;
public:
BoltTerminalAI() {
ai_manager_ = std::make_unique<bolt::ai::EnhancedAIManager>();
session_id_ = "terminal_session";
ai_manager_->create_session(session_id_);
}
void show_status() {
std::cout << "\nπ AI Status:\n";
std::cout << " Current Provider: " << ai_manager_->get_current_provider() << "\n";
std::cout << " Ready: " << (ai_manager_->is_ready() ? "β
" : "β") << "\n";
std::cout << " Direct Model: "
<< (ai_manager_->has_direct_model() ? "β
" + ai_manager_->get_model_info() : "β Using fallback")
<< "\n";
auto stats = ai_manager_->get_statistics();
std::cout << " Total Requests: " << stats.total_requests << "\n";
std::cout << " Successful: " << stats.successful_requests << "\n";
std::cout << " Failed: " << stats.failed_requests << "\n\n";
}
void chat_loop() {
std::cout << "π€ Bolt AI Terminal Chat\n";
std::cout << "========================\n\n";
show_status();
// Check if input is from a pipe or terminal
bool is_piped = !isatty(fileno(stdin));
if (!is_piped) {
std::cout << "Commands:\n";
std::cout << " /status - Show AI status\n";
std::cout << " /history - Show chat history\n";
std::cout << " /clear - Clear chat history\n";
std::cout << " /load - Try to load a GGUF model\n";
std::cout << " /quit - Exit the application\n\n";
std::cout << "π¬ Start chatting (type your message and press Enter):\n\n";
}
std::string input;
while (std::getline(std::cin, input)) {
if (input.empty()) {
if (is_piped)
break; // Exit on empty line when piped
continue;
}
if (!is_piped) {
std::cout << "You: " << input << "\n";
}
// Handle commands
if (input == "/quit" || input == "/exit") {
std::cout << "\nπ Goodbye!\n";
break;
}
if (input == "/status") {
show_status();
if (!is_piped)
continue;
else
break;
}
if (input == "/history") {
show_history();
if (!is_piped)
continue;
else
break;
}
if (input == "/clear") {
ai_manager_->clear_session_history(session_id_);
std::cout << "ποΈ Chat history cleared.\n\n";
if (!is_piped)
continue;
else
break;
}
if (input == "/load") {
try_load_model();
if (!is_piped)
continue;
else
break;
}
// Send to AI
std::cout << "AI: ";
auto response = ai_manager_->chat(input, session_id_);
if (response.success) {
std::cout << response.response << "\n";
if (response.inference_time_ms > 0) {
std::cout << " β±οΈ " << response.inference_time_ms << "ms";
if (response.tokens_generated > 0) {
std::cout << " | π€ " << response.tokens_generated << " tokens";
}
std::cout << "\n";
}
} else {
std::cout << "β Error: " << response.error << "\n";
}
std::cout << "\n";
// For piped input, process one question and exit
if (is_piped) {
break;
}
}
}
private:
void show_history() {
auto history = ai_manager_->get_session_history(session_id_);
if (history.empty()) {
std::cout << "π No chat history yet.\n\n";
return;
}
std::cout << "\nπ Chat History:\n";
std::cout << "================\n";
for (const auto& entry : history) {
std::cout << entry << "\n";
}
std::cout << "\n";
}
void try_load_model() {
std::cout << "π Attempting to auto-detect GGUF models...\n";
if (ai_manager_->auto_detect_models()) {
std::cout << "β
Successfully loaded a GGUF model!\n";
show_status();
} else {
std::cout << "β No GGUF models found. You can:\n";
std::cout << " 1. Download a small model:\n";
std::cout
<< " wget -P ./models/ https://huggingface.co/microsoft/DialoGPT-small/resolve/main/model.gguf\n";
std::cout << " 2. Or continue using intelligent fallback responses\n\n";
}
}
};
int main() {
try {
BoltTerminalAI app;
app.chat_loop();
} catch (const std::exception& e) {
std::cerr << "β Error: " << e.what() << std::endl;
return 1;
}
return 0;
}