Add ToolDispatcher for Enhanced Tool Call Management
- Introduced `ToolDispatcher` class to handle tool calls from the AI agent, allowing for dynamic execution of commands based on user requests. - Updated `ConversationalAgentService` to integrate tool dispatching, enabling the agent to respond to tool calls and manage execution results. - Enhanced `AgentResponse` structure to include a list of tool calls, facilitating communication between the AI and the tool dispatcher. - Modified AI service implementations to parse and include tool calls in responses, improving the agent's interactive capabilities. This commit significantly enhances the z3ed system's ability to manage and execute tool calls, paving the way for more complex interactions in ROM hacking.
This commit is contained in:
@@ -34,6 +34,21 @@ absl::StatusOr<ChatMessage> ConversationalAgentService::SendMessage(
|
||||
response_text += "\n\nCommands:\n" + absl::StrJoin(agent_response.commands, "\n");
|
||||
}
|
||||
|
||||
// If the agent requested a tool call, dispatch it.
|
||||
if (!agent_response.tool_calls.empty()) {
|
||||
for (const auto& tool_call : agent_response.tool_calls) {
|
||||
auto tool_result_or = tool_dispatcher_.Dispatch(tool_call);
|
||||
if (tool_result_or.ok()) {
|
||||
// Add the tool result to the history and send back to the AI.
|
||||
history_.push_back({ChatMessage::Sender::kAgent, tool_result_or.value(), absl::Now()});
|
||||
return SendMessage(""); // Re-prompt the AI with the new context.
|
||||
} else {
|
||||
// Handle tool execution error.
|
||||
return absl::InternalError(absl::StrCat("Tool execution failed: ", tool_result_or.status().message()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ChatMessage chat_response = {ChatMessage::Sender::kAgent, response_text,
|
||||
absl::Now()};
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "cli/service/ai/ai_service.h"
|
||||
#include "cli/service/agent/tool_dispatcher.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace cli {
|
||||
@@ -31,6 +32,7 @@ class ConversationalAgentService {
|
||||
private:
|
||||
std::vector<ChatMessage> history_;
|
||||
std::unique_ptr<AIService> ai_service_;
|
||||
ToolDispatcher tool_dispatcher_;
|
||||
};
|
||||
|
||||
} // namespace agent
|
||||
|
||||
40
src/cli/service/agent/tool_dispatcher.cc
Normal file
40
src/cli/service/agent/tool_dispatcher.cc
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "cli/service/agent/tool_dispatcher.h"
|
||||
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "cli/handlers/agent/commands.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace cli {
|
||||
namespace agent {
|
||||
|
||||
absl::StatusOr<std::string> ToolDispatcher::Dispatch(
|
||||
const ToolCall& tool_call) {
|
||||
std::vector<std::string> args;
|
||||
for (const auto& [key, value] : tool_call.args) {
|
||||
args.push_back(absl::StrFormat("--%s", key));
|
||||
args.push_back(value);
|
||||
}
|
||||
|
||||
if (tool_call.tool_name == "resource-list") {
|
||||
// Note: This is a simplified approach for now. A more robust solution
|
||||
// would capture stdout instead of relying on the handler to return a string.
|
||||
auto status = HandleResourceListCommand(args);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
return "Successfully listed resources.";
|
||||
} else if (tool_call.tool_name == "dungeon-list-sprites") {
|
||||
auto status = HandleDungeonListSpritesCommand(args);
|
||||
if (!status.ok()) {
|
||||
return status;
|
||||
}
|
||||
return "Successfully listed sprites.";
|
||||
}
|
||||
|
||||
return absl::UnimplementedError(
|
||||
absl::StrFormat("Unknown tool: %s", tool_call.tool_name));
|
||||
}
|
||||
|
||||
} // namespace agent
|
||||
} // namespace cli
|
||||
} // namespace yaze
|
||||
24
src/cli/service/agent/tool_dispatcher.h
Normal file
24
src/cli/service/agent/tool_dispatcher.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef YAZE_SRC_CLI_SERVICE_AGENT_TOOL_DISPATCHER_H_
|
||||
#define YAZE_SRC_CLI_SERVICE_AGENT_TOOL_DISPATCHER_H_
|
||||
|
||||
#include <string>
|
||||
#include "absl/status/statusor.h"
|
||||
#include "cli/service/ai/common.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace cli {
|
||||
namespace agent {
|
||||
|
||||
class ToolDispatcher {
|
||||
public:
|
||||
ToolDispatcher() = default;
|
||||
|
||||
// Execute a tool call and return the result as a string.
|
||||
absl::StatusOr<std::string> Dispatch(const ToolCall& tool_call);
|
||||
};
|
||||
|
||||
} // namespace agent
|
||||
} // namespace cli
|
||||
} // namespace yaze
|
||||
|
||||
#endif // YAZE_SRC_CLI_SERVICE_AGENT_TOOL_DISPATCHER_H_
|
||||
Reference in New Issue
Block a user