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_++) : name;
21 session.is_active = sessions_.empty(); // First session is active by default
22
23 sessions_.push_back(std::move(session));
24
25 // If this is the first session, set it as active
26 if (sessions_.size() == 1) {
27 active_session_id_ = sessions_.back().agent_id;
28 }
29
30 LOG_INFO("AgentSessionManager", "Created session '%s' with ID '%s'",
31 sessions_.back().display_name.c_str(),
32 sessions_.back().agent_id.c_str());
33
35 on_session_created_(sessions_.back().agent_id);
36 }
37
38 return sessions_.back().agent_id;
39}
40
41void AgentSessionManager::CloseSession(const std::string& agent_id) {
42 int index = FindSessionIndex(agent_id);
43 if (index < 0) {
44 LOG_WARN("AgentSessionManager", "Attempted to close unknown session: %s",
45 agent_id.c_str());
46 return;
47 }
48
49 // Close card if open
50 if (sessions_[index].has_card_open) {
51 ClosePanelForSession(agent_id);
52 }
53
54 bool was_active = (active_session_id_ == agent_id);
55
56 // Remove the session
57 sessions_.erase(sessions_.begin() + index);
58
59 // If we removed the active session, activate another
60 if (was_active && !sessions_.empty()) {
61 // Activate the previous session, or the first one
62 int new_active_index = std::max(0, index - 1);
63 active_session_id_ = sessions_[new_active_index].agent_id;
64 sessions_[new_active_index].is_active = true;
65 } else if (sessions_.empty()) {
66 active_session_id_.clear();
67 }
68
69 LOG_INFO("AgentSessionManager", "Closed session: %s", agent_id.c_str());
70
72 on_session_closed_(agent_id);
73 }
74}
75
76void AgentSessionManager::RenameSession(const std::string& agent_id,
77 const std::string& new_name) {
78 AgentSession* session = GetSession(agent_id);
79 if (session) {
80 session->display_name = new_name;
81 LOG_INFO("AgentSessionManager", "Renamed session %s to '%s'",
82 agent_id.c_str(), new_name.c_str());
83 }
84}
85
87 if (active_session_id_.empty()) {
88 return nullptr;
89 }
91}
92
94 if (active_session_id_.empty()) {
95 return nullptr;
96 }
98}
99
100void AgentSessionManager::SetActiveSession(const std::string& agent_id) {
101 // Deactivate current
102 if (auto* current = GetSession(active_session_id_)) {
103 current->is_active = false;
104 }
105
106 // Activate new
107 if (auto* new_session = GetSession(agent_id)) {
108 new_session->is_active = true;
109 active_session_id_ = agent_id;
110 LOG_DEBUG("AgentSessionManager", "Switched to session: %s (%s)",
111 new_session->display_name.c_str(), agent_id.c_str());
112 }
113}
114
115AgentSession* AgentSessionManager::GetSession(const std::string& agent_id) {
116 for (auto& session : sessions_) {
117 if (session.agent_id == agent_id) {
118 return &session;
119 }
120 }
121 return nullptr;
122}
123
125 const std::string& agent_id) const {
126 for (const auto& session : sessions_) {
127 if (session.agent_id == agent_id) {
128 return &session;
129 }
130 }
131 return nullptr;
132}
133
134void AgentSessionManager::OpenPanelForSession(const std::string& agent_id) {
135 AgentSession* session = GetSession(agent_id);
136 if (!session) {
137 LOG_WARN("AgentSessionManager",
138 "Attempted to open card for unknown session: %s",
139 agent_id.c_str());
140 return;
141 }
142
143 if (session->has_card_open) {
144 LOG_DEBUG("AgentSessionManager", "Panel already open for session: %s",
145 agent_id.c_str());
146 return;
147 }
148
149 session->has_card_open = true;
150 LOG_INFO("AgentSessionManager", "Opened card for session: %s (%s)",
151 session->display_name.c_str(), agent_id.c_str());
152
153 if (on_card_opened_) {
154 on_card_opened_(agent_id);
155 }
156}
157
158void AgentSessionManager::ClosePanelForSession(const std::string& agent_id) {
159 AgentSession* session = GetSession(agent_id);
160 if (!session) {
161 return;
162 }
163
164 if (!session->has_card_open) {
165 return;
166 }
167
168 session->has_card_open = false;
169 LOG_INFO("AgentSessionManager", "Closed card for session: %s",
170 agent_id.c_str());
171
172 if (on_card_closed_) {
173 on_card_closed_(agent_id);
174 }
175}
176
178 const std::string& agent_id) const {
179 const AgentSession* session = GetSession(agent_id);
180 return session ? session->has_card_open : false;
181}
182
183std::vector<std::string> AgentSessionManager::GetOpenPanelSessionIds() const {
184 std::vector<std::string> result;
185 for (const auto& session : sessions_) {
186 if (session.has_card_open) {
187 result.push_back(session.agent_id);
188 }
189 }
190 return result;
191}
192
194 static int id_counter = 0;
195 return absl::StrFormat("agent_%d", ++id_counter);
196}
197
198int AgentSessionManager::FindSessionIndex(const std::string& agent_id) const {
199 for (size_t i = 0; i < sessions_.size(); ++i) {
200 if (sessions_[i].agent_id == agent_id) {
201 return static_cast<int>(i);
202 }
203 }
204 return -1;
205}
206
207} // namespace editor
208} // 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.