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 <sstream>
17#include <string>
18#include <vector>
19
20#include "rom/rom.h"
22
23namespace yaze {
24namespace cli {
25namespace agent {
26
30struct RomEdit {
31 uint32_t address;
32 std::vector<uint8_t> old_value;
33 std::vector<uint8_t> new_value;
34 std::string description;
35 std::chrono::system_clock::time_point timestamp;
36};
37
43 std::string result;
44 std::chrono::system_clock::time_point timestamp;
46 bool success;
47};
48
54 std::vector<int> visited_rooms;
55 std::map<int, std::string> room_descriptions;
56 std::chrono::system_clock::time_point last_updated;
57
58 bool IsValid() const {
59 auto age = std::chrono::system_clock::now() - last_updated;
60 return std::chrono::duration_cast<std::chrono::minutes>(age).count() < 5;
61 }
62
63 void Invalidate() {
64 current_room_id = -1;
65 visited_rooms.clear();
66 room_descriptions.clear();
67 }
68};
69
75 std::vector<int> visited_maps;
76 std::map<int, std::string> map_descriptions;
77 std::chrono::system_clock::time_point last_updated;
78
79 bool IsValid() const {
80 auto age = std::chrono::system_clock::now() - last_updated;
81 return std::chrono::duration_cast<std::chrono::minutes>(age).count() < 5;
82 }
83
84 void Invalidate() {
85 current_map_id = -1;
86 visited_maps.clear();
87 map_descriptions.clear();
88 }
89};
90
102 public:
103 AgentContext() = default;
104
105 // =========================================================================
106 // ROM State Management
107 // =========================================================================
108
112 void SetRom(Rom* rom) {
113 current_rom_ = rom;
115 }
116
120 Rom* GetRom() const { return current_rom_; }
121
125 bool HasRom() const { return current_rom_ != nullptr; }
126
130 const std::string& GetRomPath() const { return rom_path_; }
131
135 void SetRomPath(const std::string& path) { rom_path_ = path; }
136
137 // =========================================================================
138 // Edit Management
139 // =========================================================================
140
144 void AddPendingEdit(const RomEdit& edit) { pending_edits_.push_back(edit); }
145
149 const std::vector<RomEdit>& GetPendingEdits() const { return pending_edits_; }
150
154 bool HasPendingEdits() const { return !pending_edits_.empty(); }
155
160
164 absl::Status CommitPendingEdits() {
165 if (!current_rom_) {
166 return absl::FailedPreconditionError("No ROM loaded");
167 }
168
169 for (const auto& edit : pending_edits_) {
170 for (size_t i = 0; i < edit.new_value.size(); ++i) {
171 current_rom_->WriteByte(edit.address + i, edit.new_value[i]);
172 }
173 }
174
175 pending_edits_.clear();
177 return absl::OkStatus();
178 }
179
184 // Pending edits haven't been applied yet, just clear them
185 pending_edits_.clear();
186 }
187
188 // =========================================================================
189 // Tool Call History
190 // =========================================================================
191
195 void RecordToolCall(const ToolCallRecord& record) {
196 call_history_.push_back(record);
197
198 // Keep history bounded
199 if (call_history_.size() > max_history_size_) {
200 call_history_.erase(call_history_.begin());
201 }
202 }
203
207 const std::vector<ToolCallRecord>& GetCallHistory() const {
208 return call_history_;
209 }
210
214 std::vector<ToolCallRecord> GetRecentCalls(size_t n) const {
215 if (n >= call_history_.size()) {
216 return call_history_;
217 }
218 return std::vector<ToolCallRecord>(call_history_.end() - n,
219 call_history_.end());
220 }
221
225 void ClearCallHistory() { call_history_.clear(); }
226
227 // =========================================================================
228 // Session Variables
229 // =========================================================================
230
234 void SetVariable(const std::string& key, const std::string& value) {
235 session_vars_[key] = value;
236 }
237
241 std::optional<std::string> GetVariable(const std::string& key) const {
242 auto it = session_vars_.find(key);
243 if (it != session_vars_.end()) {
244 return it->second;
245 }
246 return std::nullopt;
247 }
248
252 bool HasVariable(const std::string& key) const {
253 return session_vars_.find(key) != session_vars_.end();
254 }
255
259 void ClearVariables() { session_vars_.clear(); }
260
264 const std::map<std::string, std::string>& GetAllVariables() const {
265 return session_vars_;
266 }
267
268 // =========================================================================
269 // Caching
270 // =========================================================================
271
276 const DungeonCache& GetDungeonCache() const { return dungeon_cache_; }
277
283
291
292 // =========================================================================
293 // Session Management
294 // =========================================================================
295
299 const std::string& GetSessionId() const { return session_id_; }
300
304 void SetSessionId(const std::string& id) { session_id_ = id; }
305
309 std::chrono::system_clock::time_point GetSessionStartTime() const {
310 return session_start_time_;
311 }
312
317 pending_edits_.clear();
318 call_history_.clear();
319 session_vars_.clear();
321 session_start_time_ = std::chrono::system_clock::now();
322 }
323
327 std::string GenerateSummary() const {
328 std::ostringstream ss;
329 ss << "Agent Context Summary:\n";
330 ss << " ROM loaded: " << (current_rom_ ? "Yes" : "No") << "\n";
331 if (current_rom_) {
332 ss << " ROM path: " << rom_path_ << "\n";
333 }
334 ss << " Pending edits: " << pending_edits_.size() << "\n";
335 ss << " Tool calls in history: " << call_history_.size() << "\n";
336 ss << " Session variables: " << session_vars_.size() << "\n";
337 ss << " Dungeon cache valid: "
338 << (dungeon_cache_.IsValid() ? "Yes" : "No") << "\n";
339 ss << " Overworld cache valid: "
340 << (overworld_cache_.IsValid() ? "Yes" : "No") << "\n";
341 return ss.str();
342 }
343
344 private:
345 // ROM state
346 Rom* current_rom_ = nullptr;
347 std::string rom_path_;
348 std::vector<RomEdit> pending_edits_;
349
350 // Session state
351 std::vector<ToolCallRecord> call_history_;
352 std::map<std::string, std::string> session_vars_;
353 std::string session_id_;
354 std::chrono::system_clock::time_point session_start_time_ =
355 std::chrono::system_clock::now();
356
357 // Caches
360
361 // Configuration
362 size_t max_history_size_ = 100;
363};
364
365} // namespace agent
366} // namespace cli
367} // namespace yaze
368
369#endif // YAZE_SRC_CLI_SERVICE_AGENT_AGENT_CONTEXT_H_
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:290
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