yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
agent_session.cc
Go to the documentation of this file.
2
3#include <algorithm>
4
5#include "absl/strings/str_format.h"
6#include "util/log.h"
7
8namespace yaze {
9namespace editor {
10
12 // Create a default session on startup
13 CreateSession("Agent 1");
14}
15
16std::string AgentSessionManager::CreateSession(const std::string& name) {
17 AgentSession session;
18 session.agent_id = GenerateAgentId();
19 session.display_name =
20 name.empty() ? absl::StrFormat("Agent %d", next_session_number_++)
21 : name;
22 session.is_active = sessions_.empty(); // First session is active by default
23
24 sessions_.push_back(std::move(session));
25
26 // If this is the first session, set it as active
27 if (sessions_.size() == 1) {
28 active_session_id_ = sessions_.back().agent_id;
29 }
30
31 LOG_INFO("AgentSessionManager", "Created session '%s' with ID '%s'",
32 sessions_.back().display_name.c_str(),
33 sessions_.back().agent_id.c_str());
34
36 on_session_created_(sessions_.back().agent_id);
37 }
38
39 return sessions_.back().agent_id;
40}
41
42void AgentSessionManager::CloseSession(const std::string& agent_id) {
43 int index = FindSessionIndex(agent_id);
44 if (index < 0) {
45 LOG_WARN("AgentSessionManager", "Attempted to close unknown session: %s",
46 agent_id.c_str());
47 return;
48 }
49
50 // Close card if open
51 if (sessions_[index].has_card_open) {
52 ClosePanelForSession(agent_id);
53 }
54
55 bool was_active = (active_session_id_ == agent_id);
56
57 // Remove the session
58 sessions_.erase(sessions_.begin() + index);
59
60 // If we removed the active session, activate another
61 if (was_active && !sessions_.empty()) {
62 // Activate the previous session, or the first one
63 int new_active_index = std::max(0, index - 1);
64 active_session_id_ = sessions_[new_active_index].agent_id;
65 sessions_[new_active_index].is_active = true;
66 } else if (sessions_.empty()) {
67 active_session_id_.clear();
68 }
69
70 LOG_INFO("AgentSessionManager", "Closed session: %s", agent_id.c_str());
71
73 on_session_closed_(agent_id);
74 }
75}
76
77void AgentSessionManager::RenameSession(const std::string& agent_id,
78 const std::string& new_name) {
79 AgentSession* session = GetSession(agent_id);
80 if (session) {
81 session->display_name = new_name;
82 LOG_INFO("AgentSessionManager", "Renamed session %s to '%s'",
83 agent_id.c_str(), new_name.c_str());
84 }
85}
86
88 if (active_session_id_.empty()) {
89 return nullptr;
90 }
92}
93
95 if (active_session_id_.empty()) {
96 return nullptr;
97 }
99}
100
101void AgentSessionManager::SetActiveSession(const std::string& agent_id) {
102 // Deactivate current
103 if (auto* current = GetSession(active_session_id_)) {
104 current->is_active = false;
105 }
106
107 // Activate new
108 if (auto* new_session = GetSession(agent_id)) {
109 new_session->is_active = true;
110 active_session_id_ = agent_id;
111 LOG_DEBUG("AgentSessionManager", "Switched to session: %s (%s)",
112 new_session->display_name.c_str(), agent_id.c_str());
113 }
114}
115
116AgentSession* AgentSessionManager::GetSession(const std::string& agent_id) {
117 for (auto& session : sessions_) {
118 if (session.agent_id == agent_id) {
119 return &session;
120 }
121 }
122 return nullptr;
123}
124
126 const std::string& agent_id) const {
127 for (const auto& session : sessions_) {
128 if (session.agent_id == agent_id) {
129 return &session;
130 }
131 }
132 return nullptr;
133}
134
135void AgentSessionManager::OpenPanelForSession(const std::string& agent_id) {
136 AgentSession* session = GetSession(agent_id);
137 if (!session) {
138 LOG_WARN("AgentSessionManager",
139 "Attempted to open card for unknown session: %s",
140 agent_id.c_str());
141 return;
142 }
143
144 if (session->has_card_open) {
145 LOG_DEBUG("AgentSessionManager", "Panel already open for session: %s",
146 agent_id.c_str());
147 return;
148 }
149
150 session->has_card_open = true;
151 LOG_INFO("AgentSessionManager", "Opened card for session: %s (%s)",
152 session->display_name.c_str(), agent_id.c_str());
153
154 if (on_card_opened_) {
155 on_card_opened_(agent_id);
156 }
157}
158
159void AgentSessionManager::ClosePanelForSession(const std::string& agent_id) {
160 AgentSession* session = GetSession(agent_id);
161 if (!session) {
162 return;
163 }
164
165 if (!session->has_card_open) {
166 return;
167 }
168
169 session->has_card_open = false;
170 LOG_INFO("AgentSessionManager", "Closed card for session: %s",
171 agent_id.c_str());
172
173 if (on_card_closed_) {
174 on_card_closed_(agent_id);
175 }
176}
177
179 const std::string& agent_id) const {
180 const AgentSession* session = GetSession(agent_id);
181 return session ? session->has_card_open : false;
182}
183
184std::vector<std::string> AgentSessionManager::GetOpenPanelSessionIds() const {
185 std::vector<std::string> result;
186 for (const auto& session : sessions_) {
187 if (session.has_card_open) {
188 result.push_back(session.agent_id);
189 }
190 }
191 return result;
192}
193
195 static int id_counter = 0;
196 return absl::StrFormat("agent_%d", ++id_counter);
197}
198
199int AgentSessionManager::FindSessionIndex(const std::string& agent_id) const {
200 for (size_t i = 0; i < sessions_.size(); ++i) {
201 if (sessions_[i].agent_id == agent_id) {
202 return static_cast<int>(i);
203 }
204 }
205 return -1;
206}
207
208} // namespace editor
209} // namespace yaze
AgentSession * GetActiveSession()
Get the currently active session (shown in sidebar)
std::vector< std::string > GetOpenPanelSessionIds() const
Get list of session IDs with open cards.
void SetActiveSession(const std::string &agent_id)
Set the active session by ID.
void CloseSession(const std::string &agent_id)
Close and remove a session.
std::vector< AgentSession > sessions_
bool IsPanelOpenForSession(const std::string &agent_id) const
Check if a session has an open card.
SessionCreatedCallback on_session_created_
PanelClosedCallback on_card_closed_
std::string CreateSession(const std::string &name="")
Create a new agent session.
void RenameSession(const std::string &agent_id, const std::string &new_name)
Rename a session.
int FindSessionIndex(const std::string &agent_id) const
Find session index by ID.
SessionClosedCallback on_session_closed_
std::string GenerateAgentId()
Generate a unique agent ID.
void OpenPanelForSession(const std::string &agent_id)
Open a full dockable card for a session.
AgentSession * GetSession(const std::string &agent_id)
Get a session by ID.
void ClosePanelForSession(const std::string &agent_id)
Close the dockable card for a session.
PanelOpenedCallback on_card_opened_
#define LOG_DEBUG(category, format,...)
Definition log.h:103
#define LOG_WARN(category, format,...)
Definition log.h:107
#define LOG_INFO(category, format,...)
Definition log.h:105
Represents a single agent session with its own chat history and config.