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