This commit is contained in:
2025-12-28 01:15:58 +08:00
parent 4ef61320ce
commit 42427f28df
3 changed files with 142 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -248,7 +248,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
return;
}
// 附加选中对象的上下文信息
// 附加选中对象的上下文信息
string messageWithContext = BuildUserMessageWithContext(text);
_history.Add(("user", messageWithContext));
PersistHistory();
@@ -323,7 +323,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
_tools.Add(new Tool_SearchThingDef());
_tools.Add(new Tool_SearchPawnKind());
// Agent 工具 - 纯视觉操作 (移除了 GetGameState, DesignateMine, DraftPawn)
// Agent 工具 - 纯视觉操作 (移除了 GetGameState, DesignateMine, DraftPawn)
if (WulaFallenEmpireMod.settings?.enableVlmFeatures == true)
{
_tools.Add(new Tool_AnalyzeScreen());
@@ -498,7 +498,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
string actionWhitelist = phase == RequestPhase.ActionTools
? "ACTION PHASE VALID TAGS ONLY:\n" +
"<spawn_resources>, <send_reinforcement>, <call_bombardment>, <modify_goodwill>, <visual_click>, <visual_scroll>, <visual_type_text>, <visual_drag>, <visual_hotkey>, <visual_wait>, <visual_delete_text>, <no_action/>\n" +
"INVALID EXAMPLES (do NOT use now): <get_map_resources/>, <search_thing_def/>, <search_pawn_kind/>\n"
"INVALID EXAMPLES (do NOT use now): <get_map_resources/>, JSON, Markdown Code Blocks\n"
: string.Empty;
return string.Join("\n\n", new[]
@@ -843,7 +843,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
{
if (string.IsNullOrEmpty(message)) return false;
string[] keywords = new string[] {
"屏幕", "画面", "截图", "看", "找", "显示", // CN
"屏幕", "画面", "截图", "看", "找", "显示", // CN
"screen", "screenshot", "image", "view", "look", "see", "find", "visual", "scan" // EN
};
return keywords.Any(k => message.IndexOf(k, StringComparison.OrdinalIgnoreCase) >= 0);
@@ -880,7 +880,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
if (settings.enableVlmFeatures && settings.showThinkingProcess)
{
// Optional: We can still say "Analyzing data link..."
AddAssistantMessage("<i>[P.I.A] 正在分析数据链路...</i>");
AddAssistantMessage("<i>[P.I.A] 正在分析数据链路...</i>");
}
var queryPhase = RequestPhase.QueryTools;
@@ -914,7 +914,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
base64Image = queryResult.CapturedImage;
if (settings.showThinkingProcess)
{
AddAssistantMessage("<i>[P.I.A] 视觉传感器已激活,图像已捕获...</i>");
AddAssistantMessage("<i>[P.I.A] 视觉传感器已激活,图像已捕获...</i>");
}
}
@@ -965,7 +965,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
if (settings.showThinkingProcess)
{
AddAssistantMessage("<i>[P.I.A] 正在计算最优战术方案...</i>");
AddAssistantMessage("<i>[P.I.A] 正在计算最优战术方案...</i>");
}
var actionPhase = RequestPhase.ActionTools;
@@ -998,7 +998,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
"Preserve the intent of the previous output.\n" +
"If the previous output indicates no action is needed or refuses action, output exactly: <no_action/>.\n" +
"Do NOT invent new actions.\n" +
"Output VALID XML tool calls only. No natural language, no commentary.\n" +
"Output VALID XML tool calls only. No natural language, no commentary.\nIf the previous output contains JSON (including JSON code blocks or inline {\"point\": [x,y]}), convert it to XML.\n- For a point coordinate, output: <visual_click><x>...</x><y>...</y></visual_click>.\n- If coordinates are larger than 1 (e.g., 0-1000), normalize by dividing by 1000.\nIgnore any non-XML text.\n" +
"Allowed tags: <spawn_resources>, <send_reinforcement>, <call_bombardment>, <modify_goodwill>, <visual_click>, <visual_scroll>, <visual_type_text>, <no_action/>.\n" +
"\nAction tool XML formats:\n" +
"- <spawn_resources><items><item><name>DefName</name><count>Int</count></item></items></spawn_resources>\n" +
@@ -1073,7 +1073,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
"Preserve the intent of the previous output.\n" +
"If the previous output indicates no action is needed or refuses action, output exactly: <no_action/>.\n" +
"Do NOT invent new actions.\n" +
"Output VALID XML tool calls only. No natural language, no commentary.\n" +
"Output VALID XML tool calls only. No natural language, no commentary.\nIf the previous output contains JSON (including JSON code blocks or inline {\"point\": [x,y]}), convert it to XML.\n- For a point coordinate, output: <visual_click><x>...</x><y>...</y></visual_click>.\n- If coordinates are larger than 1 (e.g., 0-1000), normalize by dividing by 1000.\nIgnore any non-XML text.\n" +
"Allowed tags: <spawn_resources>, <send_reinforcement>, <call_bombardment>, <modify_goodwill>, <visual_click>, <visual_scroll>, <visual_type_text>, <no_action/>.\n" +
"\nAction tool XML formats:\n" +
"- <spawn_resources><items><item><name>DefName</name><count>Int</count></item></items></spawn_resources>\n" +
@@ -1151,7 +1151,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
if (settings.showThinkingProcess)
{
AddAssistantMessage("<i>[P.I.A] 正在汇总战报并建立通讯记录...</i>");
AddAssistantMessage("<i>[P.I.A] 正在汇总战报并建立通讯记录...</i>");
}
// VISUAL CONTEXT FOR REPLY: Pass the image so the AI can describe what it sees.
@@ -1212,6 +1212,53 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
string guidance = "ToolRunner Guidance: Reply to the player in natural language only. Do NOT output any XML. You may include [EXPR:n] to set expression (n=1-6).";
var matches = Regex.Matches(xml ?? "", @"<([a-zA-Z0-9_]+)(?:>.*?</\1>|/>)", RegexOptions.Singleline);
// GEMINI 3 JSON FALLBACK (Restored & Fixed)
// If no XML found, try to parse JSON markdown block commonly output by Gemini 3 Flash Preview
// Uses a 0-1000 coordinate system typically.
if (matches.Count == 0 && (xml ?? "").Contains("```json"))
{
try
{
var jsonMatch = Regex.Match(xml, @"```json\s*(\[.*?\])\s*```", RegexOptions.Singleline);
if (jsonMatch.Success)
{
string jsonArr = jsonMatch.Groups[1].Value;
// Regex to extract objects with "point" (and optional "action")
// Matches: { "point": [123, 456] }
// Note: In verbatim string @"""", double quotes are escaped as "". Not \".
var pointMatches = Regex.Matches(jsonArr, @"\{.*?\""point\""\s*:\s*\[\s*([\d\.]+)\s*,\s*([\d\.]+)\s*\].*?\}", RegexOptions.Singleline);
if (pointMatches.Count > 0)
{
StringBuilder synthesizedXml = new StringBuilder();
foreach (Match pm in pointMatches)
{
if (float.TryParse(pm.Groups[1].Value, out float xVal) && float.TryParse(pm.Groups[2].Value, out float yVal))
{
// Gemini uses 0-1000 scale usually
if (xVal > 1 || yVal > 1)
{
xVal /= 1000.0f;
yVal /= 1000.0f;
}
synthesizedXml.Append($"<visual_click><x>{xVal}</x><y>{yVal}</y></visual_click>");
}
}
if (synthesizedXml.Length > 0)
{
xml = synthesizedXml.ToString() + "\n" + xml; // Prepend to process downstream
matches = Regex.Matches(xml, @"<([a-zA-Z0-9_]+)(?:>.*?</\1>|/>)", RegexOptions.Singleline);
}
}
}
}
catch (Exception ex)
{
WulaLog.Debug($"[JSON Fallback Error] {ex.Message}");
}
}
if (matches.Count == 0 || (matches.Count == 1 && matches[0].Groups[1].Value.Equals("no_action", StringComparison.OrdinalIgnoreCase)))
{
UpdatePhaseToolLedger(phase, false, new List<string>());
@@ -1455,3 +1502,4 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
}
}
}