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())
57 return;
58
59 ProcessMessage(message);
60 input_message_.clear();
61}
62
68
73
75 auto input = Input(&input_message_, "Type your message...");
76
77 auto send_button = Button("Send", [this] { SendMessage(input_message_); });
78
79 auto clear_button = Button("Clear", [this] { ClearHistory(); });
80
81 auto container = Container::Horizontal({input, send_button, clear_button});
82
83 return CatchEvent(container, input_event_handler_);
84}
85
87 return Renderer([this] { return RenderHistoryArea(); });
88}
89
91 auto container = Container::Vertical({history_component_, input_component_});
92
93 return Renderer(container, [this] {
94 return vbox({text("🤖 AI Chat") | bold | center, separator(),
95 history_component_->Render() | flex | frame, separator(),
96 input_component_->Render(), separator(),
97 text("Commands: /help, /exit, /clear, /rom_info, /status") |
98 dim | center}) |
99 border;
100 });
101}
102
104 if (event == Event::Return) {
105 if (input_message_.empty())
106 return true;
107
109 return true;
110 }
111
112 if (event == Event::Character('q')) {
113 // Exit chat or go back
114 return true;
115 }
116
117 return false;
118}
119
121 if (event == Event::ArrowUp) {
122 if (selected_history_index_ > 0) {
124 }
125 return true;
126 }
127
128 if (event == Event::ArrowDown) {
129 if (selected_history_index_ < static_cast<int>(chat_history_.size()) - 1) {
131 }
132 return true;
133 }
134
135 return false;
136}
137
138void EnhancedChatComponent::ProcessMessage(const std::string& message) {
139 // Handle special commands
140 if (message == "/exit") {
141 // Signal to parent component to exit
142 return;
143 }
144
145 if (message == "/clear") {
146 ClearHistory();
147 return;
148 }
149
150 if (message == "/help") {
152 "System",
153 "Available commands: /help, /exit, /clear, /rom_info, /status");
154 return;
155 }
156
157 if (message == "/rom_info") {
158 if (rom_context_) {
159 AddMessageToHistory("System", absl::StrFormat("ROM: %s | Size: %d bytes",
161 rom_context_->size()));
162 } else {
163 AddMessageToHistory("System", "No ROM loaded");
164 }
165 return;
166 }
167
168 if (message == "/status") {
170 "System",
171 absl::StrFormat("Chat Status: %d messages, %s", chat_history_.size(),
172 focused_ ? "Focused" : "Not focused"));
173 return;
174 }
175
176 // Add user message to history
177 AddMessageToHistory("You", message);
178
179 // Send to agent service
180 auto response = agent_service_.SendMessage(message);
181 if (response.ok()) {
182 // Agent response will be handled by the service
183 // For now, add a placeholder response
184 AddMessageToHistory("Agent", "Response received (integration pending)");
185 } else {
186 AddMessageToHistory("System",
187 absl::StrCat("Error: ", response.status().message()));
188 }
189
191}
192
193void EnhancedChatComponent::AddMessageToHistory(const std::string& sender,
194 const std::string& message) {
195 chat_history_.push_back({sender, message});
196
197 // Limit history size
198 if (static_cast<int>(chat_history_.size()) > max_history_lines_) {
199 chat_history_.erase(chat_history_.begin());
200 }
201
202 // Auto-scroll to bottom
204}
205
207 // Trigger re-render of history component
208 // This is handled automatically by FTXUI
209}
210
211Element EnhancedChatComponent::RenderChatMessage(const std::string& sender,
212 const std::string& message) {
213 Color sender_color;
214 if (sender == "You") {
215 sender_color = Color::Yellow;
216 } else if (sender == "Agent") {
217 sender_color = Color::Green;
218 } else {
219 sender_color = Color::Cyan;
220 }
221
222 return hbox({text(sender) | bold | color(sender_color), text(": "),
223 text(message) | flex});
224}
225
227 return hbox({text("You: ") | bold, input_component_->Render() | flex});
228}
229
231 if (chat_history_.empty()) {
232 return vbox({text("No messages yet. Start chatting!") | dim | center}) |
233 flex | center;
234 }
235
236 std::vector<Element> messages;
237 for (const auto& [sender, message] : chat_history_) {
238 messages.push_back(RenderChatMessage(sender, message));
239 }
240
241 return vbox(messages) | flex;
242}
243
244} // namespace cli
245} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
auto size() const
Definition rom.h:134
auto title() const
Definition rom.h:133
void RegisterCommand(const std::string &cmd, const std::string &desc, const std::vector< std::string > &examples={})
void SendMessage(const std::string &message)
std::function< bool(const ftxui::Event &) input_event_handler_)
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 &) history_event_handler_)
std::vector< std::pair< std::string, std::string > > chat_history_
EnhancedChatComponent(Rom *rom_context=nullptr)
absl::StatusOr< ChatMessage > SendMessage(const std::string &message)
Definition cli.h:17