This commit is contained in:
2025-12-13 15:06:41 +08:00
parent 3b635580e6
commit 198c4ab26b
2 changed files with 39 additions and 30 deletions

View File

@@ -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));
await GenerateResponse(isContinuation: true);
return;
}
// 3. Execute the tool directly with the XML string var tool = _tools.FirstOrDefault(t => t.Name == toolName);
// We need to pass the INNER XML (parameters) to the tool, stripping the root tool tag. if (tool == null)
// Otherwise, ParseXmlArgs will match the root tag as a parameter. {
string argsXml = xml; string errorMsg = $"Error: Tool '{toolName}' not found.";
var contentMatch = Regex.Match(xml, $@"<{toolName}>(.*?)</{toolName}>", RegexOptions.Singleline); Log.Error($"[WulaAI] {errorMsg}");
if (contentMatch.Success) combinedResults.AppendLine(errorMsg);
{ continue;
argsXml = contentMatch.Groups[1].Value; }
}
Log.Message($"[WulaAI] Executing tool: {toolName} with args: {argsXml}"); // Extract inner XML for arguments
string result = tool.Execute(argsXml).Trim(); string argsXml = toolCallXml;
var contentMatch = Regex.Match(toolCallXml, $@"<{toolName}>(.*?)</{toolName}>", RegexOptions.Singleline);
string toolResultOutput = (toolName == "modify_goodwill") if (contentMatch.Success)
? $"Tool '{toolName}' Result (Invisible): {result}" {
: $"Tool '{toolName}' Result: {result}"; 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}");
}
}
_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)