2#include "absl/time/clock.h"
4#if defined(YAZE_BUILD_AGENT_UI)
22 ProposalDrawer* proposal_drawer,
23 RightDrawerManager* right_drawer_manager,
24 WorkspaceWindowManager* window_manager,
25 UserSettings* user_settings) {
26 toast_manager_ = toast_manager;
27 right_drawer_manager_ = right_drawer_manager;
28 window_manager_ = window_manager;
29 user_settings_ = user_settings;
32 session_manager_.CreateSession(
"Agent 1");
36 window_manager->RegisterWindowContent(std::make_unique<OracleRamPanel>());
41 EditorDependencies deps;
42 deps.window_manager = window_manager;
43 deps.toast_manager = toast_manager;
44 deps.user_settings = user_settings_;
45 agent_editor_.SetDependencies(deps);
49 agent_editor_.Initialize();
50 agent_editor_.InitializeWithDependencies(toast_manager, proposal_drawer,
52 agent_editor_.SetContext(&agent_ui_context_);
55 if (right_drawer_manager_) {
56 right_drawer_manager_->SetAgentChat(agent_editor_.GetAgentChat());
57 right_drawer_manager_->SetProposalDrawer(proposal_drawer);
58 right_drawer_manager_->SetToastManager(toast_manager);
63 InitializeKnowledge();
66 agent_editor_.SetKnowledgePanelCallback([
this, toast_manager]() {
67 AgentKnowledgePanel::Callbacks callbacks;
68 callbacks.set_preference = [
this](
const std::string&
key,
69 const std::string& value) {
70 if (knowledge_initialized_) {
71 learned_knowledge_.SetPreference(key, value);
72 learned_knowledge_.SaveAll();
73 SyncKnowledgeToContext();
76 callbacks.remove_preference = [
this](
const std::string&
key) {
77 if (knowledge_initialized_) {
78 learned_knowledge_.RemovePreference(key);
79 learned_knowledge_.SaveAll();
80 SyncKnowledgeToContext();
83 callbacks.clear_all_knowledge = [
this]() {
84 if (knowledge_initialized_) {
85 learned_knowledge_.ClearAll();
86 SyncKnowledgeToContext();
89 callbacks.export_knowledge = [
this, toast_manager]() {
90 if (knowledge_initialized_) {
91 auto json_or = learned_knowledge_.ExportToJSON();
100 callbacks.refresh_knowledge = [
this]() {
101 SyncKnowledgeToContext();
104 knowledge_panel_.Draw(
GetContext(), GetKnowledgeService(), callbacks,
110 SyncStateFromEditor();
114 if (!user_settings_) {
117 agent_editor_.ApplyUserSettingsDefaults(force);
121 agent_editor_.SetRomContext(rom);
122 agent_ui_context_.SetRom(rom);
126 agent_ui_context_.SetProject(project);
129 if (AgentSession* session = session_manager_.GetActiveSession()) {
130 session->context.SetProject(project);
135 agent_ui_context_.SetAsarWrapper(asar_wrapper);
138 if (AgentSession* session = session_manager_.GetActiveSession()) {
139 session->context.SetAsarWrapper(asar_wrapper);
144 const std::map<std::string, core::AsarSymbol>* table) {
145 agent_ui_context_.SetAssemblySymbolTable(table);
146 if (AgentSession* session = session_manager_.GetActiveSession()) {
147 session->context.SetAssemblySymbolTable(table);
153 auto status = agent_editor_.Update();
158void AgentUiController::SyncStateFromEditor() {
160 const auto& profile = agent_editor_.GetCurrentProfile();
161 auto& ctx_config = agent_ui_context_.agent_config();
164 bool changed =
false;
165 if (ctx_config.ai_provider != profile.provider)
167 if (ctx_config.ai_model != profile.model)
169 if (ctx_config.ollama_host != profile.ollama_host)
171 if (ctx_config.gemini_api_key != profile.gemini_api_key)
173 if (ctx_config.anthropic_api_key != profile.anthropic_api_key)
175 if (ctx_config.openai_api_key != profile.openai_api_key)
177 if (ctx_config.openai_base_url != profile.openai_base_url)
179 if (ctx_config.host_id != profile.host_id)
184 ctx_config.ai_provider = profile.provider;
185 ctx_config.ai_model = profile.model;
186 ctx_config.ollama_host = profile.ollama_host;
187 ctx_config.gemini_api_key = profile.gemini_api_key;
188 ctx_config.anthropic_api_key = profile.anthropic_api_key;
189 ctx_config.openai_api_key = profile.openai_api_key;
190 ctx_config.openai_base_url = profile.openai_base_url;
191 ctx_config.host_id = profile.host_id;
194 last_synced_config_ = ctx_config;
196 SyncStateToComponents();
200void AgentUiController::SyncStateToComponents() {
203 if (
auto* chat = agent_editor_.GetAgentChat()) {
204 chat->SetContext(&agent_ui_context_);
209 agent_editor_.set_active(
true);
213 agent_editor_.set_active(
true);
215 if (window_manager_) {
216 const size_t session_id = window_manager_->GetActiveSessionId();
217 window_manager_->OpenWindow(session_id,
"agent.chat");
218 window_manager_->MarkWindowRecentlyUsed(
"agent.chat");
221 if (right_drawer_manager_) {
225 if (
auto* chat = agent_editor_.GetAgentChat()) {
226 chat->set_active(
true);
227 chat->ScrollToBottom();
240 return &agent_editor_;
245 if (AgentSession* session = session_manager_.GetActiveSession()) {
246 return &session->context;
249 return &agent_ui_context_;
254 if (
const AgentSession* session = session_manager_.GetActiveSession()) {
255 return &session->context;
258 return &agent_ui_context_;
262cli::agent::LearnedKnowledgeService* AgentUiController::GetKnowledgeService() {
263 if (!knowledge_initialized_) {
266 return &learned_knowledge_;
269bool AgentUiController::IsKnowledgeServiceAvailable()
const {
270 return knowledge_initialized_;
273void AgentUiController::InitializeKnowledge() {
274 if (knowledge_initialized_) {
278 auto status = learned_knowledge_.Initialize();
280 knowledge_initialized_ =
true;
281 SyncKnowledgeToContext();
283 "LearnedKnowledgeService initialized successfully");
286 "Failed to initialize LearnedKnowledgeService: %s",
287 status.message().data());
291void AgentUiController::SyncKnowledgeToContext() {
292 if (!knowledge_initialized_) {
297 auto stats = learned_knowledge_.GetStats();
298 auto& knowledge_state = agent_ui_context_.knowledge_state();
300 knowledge_state.initialized =
true;
301 knowledge_state.preference_count = stats.preference_count;
302 knowledge_state.pattern_count = stats.pattern_count;
303 knowledge_state.project_count = stats.project_count;
304 knowledge_state.memory_count = stats.memory_count;
305 knowledge_state.last_refresh = absl::Now();
308 if (AgentSession* session = session_manager_.GetActiveSession()) {
309 session->context.knowledge_state() = knowledge_state;
void SetRomContext(Rom *rom)
AgentUIContext * GetContext()
void SetProjectContext(project::YazeProject *project)
void ApplyUserSettingsDefaults(bool force=false)
void SetAssemblySymbolTableContext(const std::map< std::string, core::AsarSymbol > *table)
AgentEditor * GetAgentEditor()
void Initialize(ToastManager *toast_manager, ProposalDrawer *proposal_drawer, RightDrawerManager *right_drawer_manager, WorkspaceWindowManager *window_manager, UserSettings *user_settings)
void SetAsarWrapperContext(core::AsarWrapper *asar_wrapper)
#define LOG_ERROR(category, format,...)
#define LOG_INFO(category, format,...)