yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
agent_ui_controller.cc
Go to the documentation of this file.
2
3#if defined(YAZE_BUILD_AGENT_UI)
4
10#include "rom/rom.h"
11#include "util/log.h"
12
13namespace yaze {
14namespace editor {
15
16void AgentUiController::Initialize(ToastManager* toast_manager,
17 ProposalDrawer* proposal_drawer,
18 RightPanelManager* right_panel_manager,
19 PanelManager* panel_manager) {
20 toast_manager_ = toast_manager;
21 right_panel_manager_ = right_panel_manager;
22
23 // Create initial agent session
24 session_manager_.CreateSession("Agent 1");
25
26 // Provide minimal dependencies so panels register with the activity bar
27 if (panel_manager) {
28 EditorDependencies deps;
29 deps.panel_manager = panel_manager;
30 deps.toast_manager = toast_manager;
31 agent_editor_.SetDependencies(deps);
32 }
33
34 // Initialize the AgentEditor
35 agent_editor_.Initialize();
36 agent_editor_.InitializeWithDependencies(toast_manager, proposal_drawer,
37 /*rom=*/nullptr);
38
39 // Wire agent/chat into the right sidebar experience
40 if (right_panel_manager_) {
41 right_panel_manager_->SetAgentChat(agent_editor_.GetAgentChat());
42 right_panel_manager_->SetProposalDrawer(proposal_drawer);
43 right_panel_manager_->SetToastManager(toast_manager);
44 }
45
46 // Initialize knowledge service if available
47#if defined(Z3ED_AI)
48 InitializeKnowledge();
49
50 // Set up knowledge panel callback
51 agent_editor_.SetKnowledgePanelCallback([this, toast_manager]() {
52 AgentKnowledgePanel::Callbacks callbacks;
53 callbacks.set_preference = [this](const std::string& key,
54 const std::string& value) {
55 if (knowledge_initialized_) {
56 learned_knowledge_.SetPreference(key, value);
57 learned_knowledge_.SaveAll();
58 SyncKnowledgeToContext();
59 }
60 };
61 callbacks.remove_preference = [this](const std::string& key) {
62 if (knowledge_initialized_) {
63 learned_knowledge_.RemovePreference(key);
64 learned_knowledge_.SaveAll();
65 SyncKnowledgeToContext();
66 }
67 };
68 callbacks.clear_all_knowledge = [this]() {
69 if (knowledge_initialized_) {
70 learned_knowledge_.ClearAll();
71 SyncKnowledgeToContext();
72 }
73 };
74 callbacks.export_knowledge = [this, toast_manager]() {
75 if (knowledge_initialized_) {
76 auto json_or = learned_knowledge_.ExportToJSON();
77 if (json_or.ok()) {
78 // TODO: Save to file or clipboard
79 if (toast_manager) {
80 toast_manager->Show("Knowledge exported", ToastType::kSuccess);
81 }
82 }
83 }
84 };
85 callbacks.refresh_knowledge = [this]() { SyncKnowledgeToContext(); };
86
87 knowledge_panel_.Draw(GetContext(), GetKnowledgeService(), callbacks,
88 toast_manager_);
89 });
90#endif
91
92 // Initial state sync from editor to context
93 SyncStateFromEditor();
94}
95
97 agent_editor_.SetRomContext(rom);
98 agent_ui_context_.SetRom(rom);
99}
100
101void AgentUiController::SetProjectContext(project::YazeProject* project) {
102 agent_ui_context_.SetProject(project);
103
104 // Propagate to active session context
105 if (AgentSession* session = session_manager_.GetActiveSession()) {
106 session->context.SetProject(project);
107 }
108}
109
110void AgentUiController::SetAsarWrapperContext(core::AsarWrapper* asar_wrapper) {
111 agent_ui_context_.SetAsarWrapper(asar_wrapper);
112
113 // Propagate to active session context
114 if (AgentSession* session = session_manager_.GetActiveSession()) {
115 session->context.SetAsarWrapper(asar_wrapper);
116 }
117}
118
119absl::Status AgentUiController::Update() {
120 // Bidirectional sync between AgentEditor and SharedContext
121 SyncStateFromEditor();
122
123 // Update the AgentEditor (draws its cards via PanelManager)
124 auto status = agent_editor_.Update();
125
126 return status;
127}
128
129void AgentUiController::SyncStateFromEditor() {
130 // Pull config from AgentEditor's current profile
131 const auto& profile = agent_editor_.GetCurrentProfile();
132 auto& ctx_config = agent_ui_context_.agent_config();
133
134 // Check for changes between Editor and Context
135 bool changed = false;
136 if (ctx_config.ai_provider != profile.provider) changed = true;
137 if (ctx_config.ai_model != profile.model) changed = true;
138 // ... (Simplified sync logic for now)
139
140 if (changed) {
141 ctx_config.ai_provider = profile.provider;
142 ctx_config.ai_model = profile.model;
143 ctx_config.ollama_host = profile.ollama_host;
144 ctx_config.gemini_api_key = profile.gemini_api_key;
145
146 // Update last synced state
147 last_synced_config_ = ctx_config;
148
149 SyncStateToComponents();
150 }
151}
152
153void AgentUiController::SyncStateToComponents() {
154 // Push context state to chat widget if needed
155 // AgentChat uses context directly, so this might be redundant if it holds a pointer
156 if (auto* chat = agent_editor_.GetAgentChat()) {
157 chat->SetContext(&agent_ui_context_);
158 }
159}
160
162 agent_editor_.set_active(true);
163}
164
166 // Focus the chat panel
167 // TODO: Implement focus logic via PanelManager if needed
168}
169
171 return true;
172}
173
175 // No legacy popups
176}
177
179 return &agent_editor_;
180}
181
182AgentUIContext* AgentUiController::GetContext() {
183 // Return active session's context if available
184 if (AgentSession* session = session_manager_.GetActiveSession()) {
185 return &session->context;
186 }
187 // Fall back to legacy context
188 return &agent_ui_context_;
189}
190
191const AgentUIContext* AgentUiController::GetContext() const {
192 // Return active session's context if available
193 if (const AgentSession* session = session_manager_.GetActiveSession()) {
194 return &session->context;
195 }
196 // Fall back to legacy context
197 return &agent_ui_context_;
198}
199
200#if defined(Z3ED_AI)
201cli::agent::LearnedKnowledgeService* AgentUiController::GetKnowledgeService() {
202 if (!knowledge_initialized_) {
203 return nullptr;
204 }
205 return &learned_knowledge_;
206}
207
208bool AgentUiController::IsKnowledgeServiceAvailable() const {
209 return knowledge_initialized_;
210}
211
212void AgentUiController::InitializeKnowledge() {
213 if (knowledge_initialized_) {
214 return;
215 }
216
217 auto status = learned_knowledge_.Initialize();
218 if (status.ok()) {
219 knowledge_initialized_ = true;
220 SyncKnowledgeToContext();
221 LOG_INFO("AgentUiController", "LearnedKnowledgeService initialized successfully");
222 } else {
223 LOG_ERROR("AgentUiController", "Failed to initialize LearnedKnowledgeService: %s", status.message().data());
224 }
225}
226
227void AgentUiController::SyncKnowledgeToContext() {
228 if (!knowledge_initialized_) {
229 return;
230 }
231
232 // Update knowledge state in context with stats from service
233 auto stats = learned_knowledge_.GetStats();
234 auto& knowledge_state = agent_ui_context_.knowledge_state();
235
236 knowledge_state.initialized = true;
237 knowledge_state.preference_count = stats.preference_count;
238 knowledge_state.pattern_count = stats.pattern_count;
239 knowledge_state.project_count = stats.project_count;
240 knowledge_state.memory_count = stats.memory_count;
241 knowledge_state.last_refresh = absl::Now();
242
243 // Also update active session context
244 if (AgentSession* session = session_manager_.GetActiveSession()) {
245 session->context.knowledge_state() = knowledge_state;
246 }
247}
248#endif // defined(Z3ED_AI)
249
250} // namespace editor
251} // namespace yaze
252
253#endif // defined(YAZE_BUILD_AGENT_UI)
void SetProjectContext(project::YazeProject *project)
void SetAsarWrapperContext(core::AsarWrapper *asar_wrapper)
void Initialize(ToastManager *toast_manager, ProposalDrawer *proposal_drawer, RightPanelManager *right_panel_manager, PanelManager *panel_manager)
EditorContext context() const
Definition editor.h:231
#define LOG_ERROR(category, format,...)
Definition log.h:109
#define LOG_INFO(category, format,...)
Definition log.h:105