yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
agent_context.h
Go to the documentation of this file.
1
9#ifndef YAZE_SRC_CLI_SERVICE_AGENT_AGENT_CONTEXT_H_
10#define YAZE_SRC_CLI_SERVICE_AGENT_AGENT_CONTEXT_H_
11
12#include <chrono>
13#include <map>
14#include <memory>
15#include <optional>
16#include <string>
17#include <vector>
18
19#include "rom/rom.h"
21
22namespace yaze {
23namespace cli {
24namespace agent {
25
29struct RomEdit {
30 uint32_t address;
31 std::vector<uint8_t> old_value;
32 std::vector<uint8_t> new_value;
33 std::string description;
34 std::chrono::system_clock::time_point timestamp;
35};
36
42 std::string result;
43 std::chrono::system_clock::time_point timestamp;
45 bool success;
46};
47
53 std::vector<int> visited_rooms;
54 std::map<int, std::string> room_descriptions;
55 std::chrono::system_clock::time_point last_updated;
56
57 bool IsValid() const {
58 auto age = std::chrono::system_clock::now() - last_updated;
59 return std::chrono::duration_cast<std::chrono::minutes>(age).count() < 5;
60 }
61
62 void Invalidate() {
63 current_room_id = -1;
64 visited_rooms.clear();
65 room_descriptions.clear();
66 }
67};
68
74 std::vector<int> visited_maps;
75 std::map<int, std::string> map_descriptions;
76 std::chrono::system_clock::time_point last_updated;
77
78 bool IsValid() const {
79 auto age = std::chrono::system_clock::now() - last_updated;
80 return std::chrono::duration_cast<std::chrono::minutes>(age).count() < 5;
81 }
82
83 void Invalidate() {
84 current_map_id = -1;
85 visited_maps.clear();
86 map_descriptions.clear();
87 }
88};
89
101 public:
102 AgentContext() = default;
103
104 // =========================================================================
105 // ROM State Management
106 // =========================================================================
107
111 void SetRom(Rom* rom) {
112 current_rom_ = rom;
114 }
115
119 Rom* GetRom() const { return current_rom_; }
120
124 bool HasRom() const { return current_rom_ != nullptr; }
125
129 const std::string& GetRomPath() const { return rom_path_; }
130
134 void SetRomPath(const std::string& path) { rom_path_ = path; }
135
136 // =========================================================================
137 // Edit Management
138 // =========================================================================
139
143 void AddPendingEdit(const RomEdit& edit) { pending_edits_.push_back(edit); }
144
148 const std::vector<RomEdit>& GetPendingEdits() const { return pending_edits_; }
149
153 bool HasPendingEdits() const { return !pending_edits_.empty(); }
154
159
163 absl::Status CommitPendingEdits() {
164 if (!current_rom_) {
165 return absl::FailedPreconditionError("No ROM loaded");
166 }
167
168 for (const auto& edit : pending_edits_) {
169 for (size_t i = 0; i < edit.new_value.size(); ++i) {
170 current_rom_->WriteByte(edit.address + i, edit.new_value[i]);
171 }
172 }
173
174 pending_edits_.clear();
176 return absl::OkStatus();
177 }
178
183 // Pending edits haven't been applied yet, just clear them
184 pending_edits_.clear();
185 }
186
187 // =========================================================================
188 // Tool Call History
189 // =========================================================================
190
194 void RecordToolCall(const ToolCallRecord& record) {
195 call_history_.push_back(record);
196
197 // Keep history bounded
198 if (call_history_.size() > max_history_size_) {
199 call_history_.erase(call_history_.begin());
200 }
201 }
202
206 const std::vector<ToolCallRecord>& GetCallHistory() const {
207 return call_history_;
208 }
209
213 std::vector<ToolCallRecord> GetRecentCalls(size_t n) const {
214 if (n >= call_history_.size()) {
215 return call_history_;
216 }
217 return std::vector<ToolCallRecord>(call_history_.end() - n,
218 call_history_.end());
219 }
220
224 void ClearCallHistory() { call_history_.clear(); }
225
226 // =========================================================================
227 // Session Variables
228 // =========================================================================
229
233 void SetVariable(const std::string& key, const std::string& value) {
234 session_vars_[key] = value;
235 }
236
240 std::optional<std::string> GetVariable(const std::string& key) const {
241 auto it = session_vars_.find(key);
242 if (it != session_vars_.end()) {
243 return it->second;
244 }
245 return std::nullopt;
246 }
247
251 bool HasVariable(const std::string& key) const {
252 return session_vars_.find(key) != session_vars_.end();
253 }
254
258 void ClearVariables() { session_vars_.clear(); }
259
263 const std::map<std::string, std::string>& GetAllVariables() const {
264 return session_vars_;
265 }
266
267 // =========================================================================
268 // Caching
269 // =========================================================================
270
275 const DungeonCache& GetDungeonCache() const { return dungeon_cache_; }
276
282
290
291 // =========================================================================
292 // Session Management
293 // =========================================================================
294
298 const std::string& GetSessionId() const { return session_id_; }
299
303 void SetSessionId(const std::string& id) { session_id_ = id; }
304
308 std::chrono::system_clock::time_point GetSessionStartTime() const {
309 return session_start_time_;
310 }
311
316 pending_edits_.clear();
317 call_history_.clear();
318 session_vars_.clear();
320 session_start_time_ = std::chrono::system_clock::now();
321 }
322
326 std::string GenerateSummary() const {
327 std::ostringstream ss;
328 ss << "Agent Context Summary:\n";
329 ss << " ROM loaded: " << (current_rom_ ? "Yes" : "No") << "\n";
330 if (current_rom_) {
331 ss << " ROM path: " << rom_path_ << "\n";
332 }
333 ss << " Pending edits: " << pending_edits_.size() << "\n";
334 ss << " Tool calls in history: " << call_history_.size() << "\n";
335 ss << " Session variables: " << session_vars_.size() << "\n";
336 ss << " Dungeon cache valid: "
337 << (dungeon_cache_.IsValid() ? "Yes" : "No") << "\n";
338 ss << " Overworld cache valid: "
339 << (overworld_cache_.IsValid() ? "Yes" : "No") << "\n";
340 return ss.str();
341 }
342
343 private:
344 // ROM state
345 Rom* current_rom_ = nullptr;
346 std::string rom_path_;
347 std::vector<RomEdit> pending_edits_;
348
349 // Session state
350 std::vector<ToolCallRecord> call_history_;
351 std::map<std::string, std::string> session_vars_;
352 std::string session_id_;
353 std::chrono::system_clock::time_point session_start_time_ =
354 std::chrono::system_clock::now();
355
356 // Caches
359
360 // Configuration
361 size_t max_history_size_ = 100;
362};
363
364} // namespace agent
365} // namespace cli
366} // namespace yaze
367
368#endif // YAZE_SRC_CLI_SERVICE_AGENT_AGENT_CONTEXT_H_
369
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
absl::Status WriteByte(int addr, uint8_t value)
Definition rom.cc:286
Agent context for maintaining state across tool calls.
void SetRom(Rom *rom)
Set the current ROM context.
std::vector< ToolCallRecord > call_history_
std::chrono::system_clock::time_point session_start_time_
std::vector< RomEdit > pending_edits_
bool HasRom() const
Check if a ROM is loaded.
bool HasVariable(const std::string &key) const
Check if a session variable exists.
void ClearVariables()
Clear all session variables.
bool HasPendingEdits() const
Check if there are pending edits.
void ResetSession()
Reset the session (clear all state except ROM)
void SetVariable(const std::string &key, const std::string &value)
Set a session variable.
std::map< std::string, std::string > session_vars_
std::chrono::system_clock::time_point GetSessionStartTime() const
Get session start time.
Rom * GetRom() const
Get the current ROM context.
const DungeonCache & GetDungeonCache() const
std::string GenerateSummary() const
Generate a summary of the current context.
void SetRomPath(const std::string &path)
Set the current ROM path.
void InvalidateCaches()
Invalidate all caches.
void ClearPendingEdits()
Clear all pending edits.
OverworldCache & GetOverworldCache()
Get overworld cache.
absl::Status CommitPendingEdits()
Commit all pending edits to the ROM.
const std::vector< ToolCallRecord > & GetCallHistory() const
Get tool call history.
const std::vector< RomEdit > & GetPendingEdits() const
Get all pending edits.
void RollbackPendingEdits()
Rollback all pending edits.
void AddPendingEdit(const RomEdit &edit)
Record a pending ROM edit.
const OverworldCache & GetOverworldCache() const
const std::string & GetSessionId() const
Get session ID.
std::optional< std::string > GetVariable(const std::string &key) const
Get a session variable.
const std::string & GetRomPath() const
Get the current ROM path.
void RecordToolCall(const ToolCallRecord &record)
Record a tool call.
void ClearCallHistory()
Clear tool call history.
std::vector< ToolCallRecord > GetRecentCalls(size_t n) const
Get the last N tool calls.
const std::map< std::string, std::string > & GetAllVariables() const
Get all session variables.
void SetSessionId(const std::string &id)
Set session ID.
DungeonCache & GetDungeonCache()
Get dungeon cache.
Cached dungeon data for efficient access.
std::vector< int > visited_rooms
std::map< int, std::string > room_descriptions
std::chrono::system_clock::time_point last_updated
Cached overworld data for efficient access.
std::map< int, std::string > map_descriptions
std::chrono::system_clock::time_point last_updated
Record of a ROM edit operation.
std::vector< uint8_t > new_value
std::chrono::system_clock::time_point timestamp
std::vector< uint8_t > old_value
Record of a tool call and its result.
std::chrono::system_clock::time_point timestamp