zc
This commit is contained in:
Binary file not shown.
@@ -366,46 +366,55 @@ When the player requests any form of resources, you MUST follow this multi-turn
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 1. Extract tool name (the root tag)
|
// Match all top-level XML tags to support multiple tool calls in one response
|
||||||
var toolNameMatch = Regex.Match(xml, @"<([a-zA-Z0-9_]+)");
|
// Regex: <TagName>...</TagName> or <TagName/>
|
||||||
if (!toolNameMatch.Success)
|
var matches = Regex.Matches(xml, @"<([a-zA-Z0-9_]+)(?:>.*?</\1>|/>)", RegexOptions.Singleline);
|
||||||
|
|
||||||
|
if (matches.Count == 0)
|
||||||
{
|
{
|
||||||
ParseResponse(xml); // Invalid XML format, treat as conversational
|
ParseResponse(xml); // Invalid XML format, treat as conversational
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
string toolName = toolNameMatch.Groups[1].Value;
|
|
||||||
|
|
||||||
// 2. Find the tool
|
StringBuilder combinedResults = new StringBuilder();
|
||||||
var tool = _tools.FirstOrDefault(t => t.Name == toolName);
|
|
||||||
if (tool == null)
|
foreach (Match match in matches)
|
||||||
{
|
{
|
||||||
string errorMsg = $"Error: Tool '{toolName}' not found.";
|
string toolCallXml = match.Value;
|
||||||
Log.Error($"[WulaAI] {errorMsg}");
|
string toolName = match.Groups[1].Value;
|
||||||
_history.Add(("assistant", xml)); // Log what the AI tried to do
|
|
||||||
_history.Add(("tool", errorMsg));
|
var tool = _tools.FirstOrDefault(t => t.Name == toolName);
|
||||||
await GenerateResponse(isContinuation: true);
|
if (tool == null)
|
||||||
return;
|
{
|
||||||
|
string errorMsg = $"Error: Tool '{toolName}' not found.";
|
||||||
|
Log.Error($"[WulaAI] {errorMsg}");
|
||||||
|
combinedResults.AppendLine(errorMsg);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract inner XML for arguments
|
||||||
|
string argsXml = toolCallXml;
|
||||||
|
var contentMatch = Regex.Match(toolCallXml, $@"<{toolName}>(.*?)</{toolName}>", RegexOptions.Singleline);
|
||||||
|
if (contentMatch.Success)
|
||||||
|
{
|
||||||
|
argsXml = contentMatch.Groups[1].Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.Message($"[WulaAI] Executing tool: {toolName} with args: {argsXml}");
|
||||||
|
string result = tool.Execute(argsXml).Trim();
|
||||||
|
|
||||||
|
if (toolName == "modify_goodwill")
|
||||||
|
{
|
||||||
|
combinedResults.AppendLine($"Tool '{toolName}' Result (Invisible): {result}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
combinedResults.AppendLine($"Tool '{toolName}' Result: {result}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Execute the tool directly with the XML string
|
|
||||||
// We need to pass the INNER XML (parameters) to the tool, stripping the root tool tag.
|
|
||||||
// Otherwise, ParseXmlArgs will match the root tag as a parameter.
|
|
||||||
string argsXml = xml;
|
|
||||||
var contentMatch = Regex.Match(xml, $@"<{toolName}>(.*?)</{toolName}>", RegexOptions.Singleline);
|
|
||||||
if (contentMatch.Success)
|
|
||||||
{
|
|
||||||
argsXml = contentMatch.Groups[1].Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.Message($"[WulaAI] Executing tool: {toolName} with args: {argsXml}");
|
|
||||||
string result = tool.Execute(argsXml).Trim();
|
|
||||||
|
|
||||||
string toolResultOutput = (toolName == "modify_goodwill")
|
|
||||||
? $"Tool '{toolName}' Result (Invisible): {result}"
|
|
||||||
: $"Tool '{toolName}' Result: {result}";
|
|
||||||
|
|
||||||
_history.Add(("assistant", xml));
|
_history.Add(("assistant", xml));
|
||||||
_history.Add(("tool", toolResultOutput));
|
_history.Add(("tool", combinedResults.ToString().Trim()));
|
||||||
await GenerateResponse(isContinuation: true);
|
await GenerateResponse(isContinuation: true);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
Reference in New Issue
Block a user