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,32 +366,35 @@ 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();
foreach (Match match in matches)
{
string toolCallXml = match.Value;
string toolName = match.Groups[1].Value;
var tool = _tools.FirstOrDefault(t => t.Name == toolName); var tool = _tools.FirstOrDefault(t => t.Name == toolName);
if (tool == null) if (tool == null)
{ {
string errorMsg = $"Error: Tool '{toolName}' not found."; string errorMsg = $"Error: Tool '{toolName}' not found.";
Log.Error($"[WulaAI] {errorMsg}"); Log.Error($"[WulaAI] {errorMsg}");
_history.Add(("assistant", xml)); // Log what the AI tried to do combinedResults.AppendLine(errorMsg);
_history.Add(("tool", errorMsg)); continue;
await GenerateResponse(isContinuation: true);
return;
} }
// 3. Execute the tool directly with the XML string // Extract inner XML for arguments
// We need to pass the INNER XML (parameters) to the tool, stripping the root tool tag. string argsXml = toolCallXml;
// Otherwise, ParseXmlArgs will match the root tag as a parameter. var contentMatch = Regex.Match(toolCallXml, $@"<{toolName}>(.*?)</{toolName}>", RegexOptions.Singleline);
string argsXml = xml;
var contentMatch = Regex.Match(xml, $@"<{toolName}>(.*?)</{toolName}>", RegexOptions.Singleline);
if (contentMatch.Success) if (contentMatch.Success)
{ {
argsXml = contentMatch.Groups[1].Value; argsXml = contentMatch.Groups[1].Value;
@@ -400,12 +403,18 @@ When the player requests any form of resources, you MUST follow this multi-turn
Log.Message($"[WulaAI] Executing tool: {toolName} with args: {argsXml}"); Log.Message($"[WulaAI] Executing tool: {toolName} with args: {argsXml}");
string result = tool.Execute(argsXml).Trim(); string result = tool.Execute(argsXml).Trim();
string toolResultOutput = (toolName == "modify_goodwill") if (toolName == "modify_goodwill")
? $"Tool '{toolName}' Result (Invisible): {result}" {
: $"Tool '{toolName}' Result: {result}"; 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)