yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
enhanced_chat_component.cc
Go to the documentation of this file.
2
3#include <ftxui/component/component.hpp>
4#include <ftxui/component/screen_interactive.hpp>
5#include <ftxui/dom/elements.hpp>
6
7#include "absl/strings/str_cat.h"
8#include "absl/strings/str_format.h"
9
10namespace yaze {
11namespace cli {
12
13using namespace ftxui;
14
16 : rom_context_(rom_context) {
17 // Initialize agent service
18 if (rom_context_) {
20 }
21
22 // Initialize autocomplete
23 autocomplete_engine_.RegisterCommand("/help", "Show help message");
24 autocomplete_engine_.RegisterCommand("/exit", "Exit the chat");
25 autocomplete_engine_.RegisterCommand("/clear", "Clear chat history");
26 autocomplete_engine_.RegisterCommand("/rom_info", "Display ROM information");
27 autocomplete_engine_.RegisterCommand("/status", "Show chat status");
28
29 // Create components
33
34 // Set up event handlers
35 input_event_handler_ = [this](const Event& event) {
36 return HandleInputEvents(event);
37 };
38
39 history_event_handler_ = [this](const Event& event) {
40 return HandleHistoryEvents(event);
41 };
42}
43
47
49 rom_context_ = rom_context;
50 if (rom_context_) {
52 }
53}
54
55void EnhancedChatComponent::SendMessage(const std::string& message) {
56 if (message.empty()) return;
57
58 ProcessMessage(message);
59 input_message_.clear();
60}
61
67
72
74 auto input = Input(&input_message_, "Type your message...");
75
76 auto send_button = Button("Send", [this] {
78 });
79
80 auto clear_button = Button("Clear", [this] {
82 });
83
84 auto container = Container::Horizontal({
85 input,
86 send_button,
87 clear_button
88 });
89
90 return CatchEvent(container, input_event_handler_);
91}
92
94 return Renderer([this] {
95 return RenderHistoryArea();
96 });
97}
98
100 auto container = Container::Vertical({
103 });
104
105 return Renderer(container, [this] {
106 return vbox({
107 text("🤖 AI Chat") | bold | center,
108 separator(),
109 history_component_->Render() | flex | frame,
110 separator(),
111 input_component_->Render(),
112 separator(),
113 text("Commands: /help, /exit, /clear, /rom_info, /status") | dim | center
114 }) | border;
115 });
116}
117
119 if (event == Event::Return) {
120 if (input_message_.empty()) return true;
121
123 return true;
124 }
125
126 if (event == Event::Character('q')) {
127 // Exit chat or go back
128 return true;
129 }
130
131 return false;
132}
133
135 if (event == Event::ArrowUp) {
136 if (selected_history_index_ > 0) {
138 }
139 return true;
140 }
141
142 if (event == Event::ArrowDown) {
143 if (selected_history_index_ < static_cast<int>(chat_history_.size()) - 1) {
145 }
146 return true;
147 }
148
149 return false;
150}
151
152void EnhancedChatComponent::ProcessMessage(const std::string& message) {
153 // Handle special commands
154 if (message == "/exit") {
155 // Signal to parent component to exit
156 return;
157 }
158
159 if (message == "/clear") {
160 ClearHistory();
161 return;
162 }
163
164 if (message == "/help") {
165 AddMessageToHistory("System",
166 "Available commands: /help, /exit, /clear, /rom_info, /status");
167 return;
168 }
169
170 if (message == "/rom_info") {
171 if (rom_context_) {
172 AddMessageToHistory("System",
173 absl::StrFormat("ROM: %s | Size: %d bytes",
175 } else {
176 AddMessageToHistory("System", "No ROM loaded");
177 }
178 return;
179 }
180
181 if (message == "/status") {
182 AddMessageToHistory("System",
183 absl::StrFormat("Chat Status: %d messages, %s",
184 chat_history_.size(),
185 focused_ ? "Focused" : "Not focused"));
186 return;
187 }
188
189 // Add user message to history
190 AddMessageToHistory("You", message);
191
192 // Send to agent service
193 auto response = agent_service_.SendMessage(message);
194 if (response.ok()) {
195 // Agent response will be handled by the service
196 // For now, add a placeholder response
197 AddMessageToHistory("Agent", "Response received (integration pending)");
198 } else {
199 AddMessageToHistory("System",
200 absl::StrCat("Error: ", response.status().message()));
201 }
202
204}
205
206void EnhancedChatComponent::AddMessageToHistory(const std::string& sender, const std::string& message) {
207 chat_history_.push_back({sender, message});
208
209 // Limit history size
210 if (static_cast<int>(chat_history_.size()) > max_history_lines_) {
211 chat_history_.erase(chat_history_.begin());
212 }
213
214 // Auto-scroll to bottom
216}
217
219 // Trigger re-render of history component
220 // This is handled automatically by FTXUI
221}
222
223Element EnhancedChatComponent::RenderChatMessage(const std::string& sender, const std::string& message) {
224 Color sender_color;
225 if (sender == "You") {
226 sender_color = Color::Yellow;
227 } else if (sender == "Agent") {
228 sender_color = Color::Green;
229 } else {
230 sender_color = Color::Cyan;
231 }
232
233 return hbox({
234 text(sender) | bold | color(sender_color),
235 text(": "),
236 text(message) | flex
237 });
238}
239
241 return hbox({
242 text("You: ") | bold,
243 input_component_->Render() | flex
244 });
245}
246
248 if (chat_history_.empty()) {
249 return vbox({
250 text("No messages yet. Start chatting!") | dim | center
251 }) | flex | center;
252 }
253
254 std::vector<Element> messages;
255 for (const auto& [sender, message] : chat_history_) {
256 messages.push_back(RenderChatMessage(sender, message));
257 }
258
259 return vbox(messages) | flex;
260}
261
262} // namespace cli
263} // namespace yaze
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:71
auto size() const
Definition rom.h:202
auto title() const
Definition rom.h:201
void RegisterCommand(const std::string &cmd, const std::string &desc, const std::vector< std::string > &examples={})
void SendMessage(const std::string &message)
ftxui::Element RenderChatMessage(const std::string &sender, const std::string &message)
bool HandleHistoryEvents(const ftxui::Event &event)
void ProcessMessage(const std::string &message)
void AddMessageToHistory(const std::string &sender, const std::string &message)
agent::ConversationalAgentService agent_service_
bool HandleInputEvents(const ftxui::Event &event)
std::function< bool(const ftxui::Event &)> input_event_handler_
std::vector< std::pair< std::string, std::string > > chat_history_
std::function< bool(const ftxui::Event &)> history_event_handler_
EnhancedChatComponent(Rom *rom_context=nullptr)
absl::StatusOr< ChatMessage > SendMessage(const std::string &message)
Definition cli.h:21
Main namespace for the application.