Enhance chat command functionality with ROM context integration and improve TUI initialization
This commit is contained in:
@@ -66,7 +66,7 @@ absl::Status Agent::Run(const std::vector<std::string>& arg_vec) {
|
||||
return agent::HandleDungeonListSpritesCommand(subcommand_args);
|
||||
}
|
||||
if (subcommand == "chat") {
|
||||
return agent::HandleChatCommand();
|
||||
return agent::HandleChatCommand(rom_);
|
||||
}
|
||||
|
||||
return absl::InvalidArgumentError(std::string(agent::kUsage));
|
||||
|
||||
@@ -31,7 +31,7 @@ absl::Status HandleResourceListCommand(
|
||||
absl::Status HandleDungeonListSpritesCommand(
|
||||
const std::vector<std::string>& arg_vec,
|
||||
Rom* rom_context = nullptr);
|
||||
absl::Status HandleChatCommand();
|
||||
absl::Status HandleChatCommand(Rom& rom);
|
||||
|
||||
} // namespace agent
|
||||
} // namespace cli
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/str_replace.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "app/zelda3/dungeon/room.h"
|
||||
#include "cli/handlers/agent/common.h"
|
||||
#include "cli/modern_cli.h"
|
||||
@@ -48,6 +49,29 @@ struct DescribeOptions {
|
||||
std::optional<std::string> last_updated;
|
||||
};
|
||||
|
||||
absl::Status EnsureRomLoaded(Rom& rom, absl::string_view command_hint) {
|
||||
if (rom.is_loaded()) {
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
std::string rom_path = absl::GetFlag(FLAGS_rom);
|
||||
if (rom_path.empty()) {
|
||||
return absl::FailedPreconditionError(
|
||||
absl::StrFormat(
|
||||
"No ROM loaded. Pass --rom=<path> when running %s.\n"
|
||||
"Example: z3ed %s --rom=zelda3.sfc",
|
||||
command_hint, command_hint));
|
||||
}
|
||||
|
||||
auto status = rom.LoadFromFile(rom_path);
|
||||
if (!status.ok()) {
|
||||
return absl::FailedPreconditionError(absl::StrFormat(
|
||||
"Failed to load ROM from '%s': %s", rom_path, status.message()));
|
||||
}
|
||||
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::StatusOr<DescribeOptions> ParseDescribeArgs(
|
||||
const std::vector<std::string>& args) {
|
||||
DescribeOptions options;
|
||||
@@ -114,21 +138,7 @@ absl::Status HandleRunCommand(const std::vector<std::string>& arg_vec,
|
||||
}
|
||||
std::string prompt = arg_vec[1];
|
||||
|
||||
if (!rom.is_loaded()) {
|
||||
std::string rom_path = absl::GetFlag(FLAGS_rom);
|
||||
if (rom_path.empty()) {
|
||||
return absl::FailedPreconditionError(
|
||||
"No ROM loaded. Use --rom=<path> to specify ROM file.\n"
|
||||
"Example: z3ed agent run --rom=zelda3.sfc --prompt \"Your prompt "
|
||||
"here\"");
|
||||
}
|
||||
|
||||
auto status = rom.LoadFromFile(rom_path);
|
||||
if (!status.ok()) {
|
||||
return absl::FailedPreconditionError(absl::StrFormat(
|
||||
"Failed to load ROM from '%s': %s", rom_path, status.message()));
|
||||
}
|
||||
}
|
||||
RETURN_IF_ERROR(EnsureRomLoaded(rom, "agent run --prompt \"<prompt>\""));
|
||||
|
||||
// 1. Create a sandbox ROM to apply changes to
|
||||
auto sandbox_or =
|
||||
@@ -470,8 +480,10 @@ absl::Status HandleDescribeCommand(const std::vector<std::string>& arg_vec) {
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status HandleChatCommand() {
|
||||
tui::ChatTUI chat_tui;
|
||||
absl::Status HandleChatCommand(Rom& rom) {
|
||||
RETURN_IF_ERROR(EnsureRomLoaded(rom, "agent chat"));
|
||||
|
||||
tui::ChatTUI chat_tui(&rom);
|
||||
chat_tui.Run();
|
||||
return absl::OkStatus();
|
||||
}
|
||||
@@ -497,11 +509,7 @@ absl::Status HandleAcceptCommand(const std::vector<std::string>& arg_vec,
|
||||
auto proposal = proposal_or.value();
|
||||
|
||||
// 2. Ensure the main ROM is loaded.
|
||||
if (!rom.is_loaded()) {
|
||||
return absl::FailedPreconditionError(
|
||||
"No ROM loaded. Use --rom=<path> to specify the ROM to apply changes "
|
||||
"to.");
|
||||
}
|
||||
RETURN_IF_ERROR(EnsureRomLoaded(rom, "agent accept --proposal-id <id>"));
|
||||
|
||||
// 3. Apply the proposal to the main ROM.
|
||||
auto apply_status = generator.ApplyProposal(proposal, &rom);
|
||||
|
||||
@@ -14,7 +14,16 @@ namespace tui {
|
||||
|
||||
using namespace ftxui;
|
||||
|
||||
ChatTUI::ChatTUI() = default;
|
||||
ChatTUI::ChatTUI(Rom* rom_context) : rom_context_(rom_context) {
|
||||
if (rom_context_ != nullptr) {
|
||||
agent_service_.SetRomContext(rom_context_);
|
||||
}
|
||||
}
|
||||
|
||||
void ChatTUI::SetRomContext(Rom* rom_context) {
|
||||
rom_context_ = rom_context;
|
||||
agent_service_.SetRomContext(rom_context_);
|
||||
}
|
||||
|
||||
void ChatTUI::Run() {
|
||||
auto input = Input(&input_message_, "Enter your message...");
|
||||
|
||||
@@ -6,13 +6,17 @@
|
||||
#include "cli/service/agent/conversational_agent_service.h"
|
||||
|
||||
namespace yaze {
|
||||
|
||||
class Rom;
|
||||
|
||||
namespace cli {
|
||||
namespace tui {
|
||||
|
||||
class ChatTUI {
|
||||
public:
|
||||
ChatTUI();
|
||||
explicit ChatTUI(Rom* rom_context = nullptr);
|
||||
void Run();
|
||||
void SetRomContext(Rom* rom_context);
|
||||
|
||||
private:
|
||||
void Render();
|
||||
@@ -21,6 +25,7 @@ class ChatTUI {
|
||||
ftxui::ScreenInteractive screen_ = ftxui::ScreenInteractive::Fullscreen();
|
||||
std::string input_message_;
|
||||
agent::ConversationalAgentService agent_service_;
|
||||
Rom* rom_context_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace tui
|
||||
|
||||
Reference in New Issue
Block a user