This commit is contained in:
2025-12-28 12:28:15 +08:00
parent addb6bbd08
commit 50012e2bd9
4 changed files with 32 additions and 30 deletions

View File

@@ -300,15 +300,30 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
sb.Append("]"); sb.Append("]");
} }
} }
// Add Mouse Position context
IntVec3 mousePos = Verse.UI.MouseMapPosition().ToIntVec3();
if (mousePos.InBounds(Find.CurrentMap))
{
sb.AppendLine();
sb.AppendLine();
sb.Append($"[Context: User's cursor is at ({mousePos.x}, {mousePos.z})]");
}
} }
catch (Exception ex) catch (Exception ex)
{ {
WulaLog.Debug($"[WulaAI] Error building context: {ex.Message}"); WulaLog.Debug($"[WulaAI] Error building context: {ex.Message}");
} }
return sb.ToString(); return sb.ToString();
} }
public static string StripContextInfo(string message)
{
if (string.IsNullOrEmpty(message)) return message;
// Remove all [Context: ...] blocks and any preceding newlines used to separate them
return Regex.Replace(message, @"(\n)*\[Context:[^\]]*\]", "", RegexOptions.Singleline).Trim();
}
private void InitializeTools() private void InitializeTools()
{ {
_tools.Clear(); _tools.Clear();
@@ -494,7 +509,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
string actionWhitelist = phase == RequestPhase.ActionTools string actionWhitelist = phase == RequestPhase.ActionTools
? "ACTION PHASE VALID TAGS ONLY:\n" + ? "ACTION PHASE VALID TAGS ONLY:\n" +
"<spawn_resources>, <send_reinforcement>, <call_bombardment>, <modify_goodwill>, <call_prefab_airdrop>, <no_action/>\n" + "<spawn_resources>, <send_reinforcement>, <call_bombardment>, <modify_goodwill>, <call_prefab_airdrop>, <no_action/>\n" +
"INVALID EXAMPLES (do NOT use now): <get_map_resources/>, <analyze_screen/>\n" "INVALID EXAMPLES (do NOT use now): <get_map_resources/>, <analyze_screen/>, <search_thing_def/>, <search_pawn_kind/>\n"
: string.Empty; : string.Empty;
return string.Join("\n\n", new[] return string.Join("\n\n", new[]
@@ -590,7 +605,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
private static bool IsXmlToolCall(string response) private static bool IsXmlToolCall(string response)
{ {
if (string.IsNullOrWhiteSpace(response)) return false; if (string.IsNullOrWhiteSpace(response)) return false;
return Regex.IsMatch(response, @"<([a-zA-Z0-9_]+)(?:>.*?</\1>|/>)", RegexOptions.Singleline); return Regex.IsMatch(response, @"<(?!/?(i|b|color|size|material)\b)([a-zA-Z0-9_]+)(?:>.*?</\2>|/>)", RegexOptions.Singleline);
} }
private static bool IsNoActionOnly(string response) private static bool IsNoActionOnly(string response)
@@ -747,11 +762,8 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
continue; continue;
} }
string stripped = StripXmlTags(entry.message)?.Trim() ?? ""; // Revert UI filtering: Add assistant messages directly without stripping XML for history context
if (!string.IsNullOrWhiteSpace(stripped)) filtered.Add(entry);
{
filtered.Add(entry);
}
} }
return filtered; return filtered;
@@ -775,7 +787,7 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
private static string StripXmlTags(string text) private static string StripXmlTags(string text)
{ {
if (string.IsNullOrEmpty(text)) return text; if (string.IsNullOrEmpty(text)) return text;
string stripped = Regex.Replace(text, @"<([a-zA-Z0-9_]+)[^>]*>.*?</\1>", "", RegexOptions.Singleline); string stripped = Regex.Replace(text, @"<(?!/?(i|b|color|size|material)\b)([a-zA-Z0-9_]+)[^>]*>.*?</\2>", "", RegexOptions.Singleline);
stripped = Regex.Replace(stripped, @"<([a-zA-Z0-9_]+)[^>]*/>", ""); stripped = Regex.Replace(stripped, @"<([a-zA-Z0-9_]+)[^>]*/>", "");
return stripped; return stripped;
} }
@@ -858,10 +870,10 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
string base64Image = null; string base64Image = null;
// If VLM is enabled, we allow the tool use. // If VLM is enabled, we allow the tool use.
// If VLM is enabled (no specific message needed here, purely internal state)
if (settings.enableVlmFeatures && settings.showThinkingProcess) if (settings.enableVlmFeatures && settings.showThinkingProcess)
{ {
// Optional: We can still say "Analyzing data link..." // No message
AddAssistantMessage("<i>[P.I.A] 正在分析数据链路...</i>");
} }
var queryPhase = RequestPhase.QueryTools; var queryPhase = RequestPhase.QueryTools;
@@ -893,10 +905,6 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
if (!string.IsNullOrEmpty(queryResult.CapturedImage)) if (!string.IsNullOrEmpty(queryResult.CapturedImage))
{ {
base64Image = queryResult.CapturedImage; base64Image = queryResult.CapturedImage;
if (settings.showThinkingProcess)
{
AddAssistantMessage("<i>[P.I.A] 视觉传感器已激活,图像已捕获...</i>");
}
} }
if (!queryResult.AnyToolSuccess && !_queryRetryUsed) if (!queryResult.AnyToolSuccess && !_queryRetryUsed)
@@ -939,16 +947,10 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
} }
retryQueryResponse = "<no_action/>"; retryQueryResponse = "<no_action/>";
} }
queryResult = await ExecuteXmlToolsForPhase(retryQueryResponse, queryPhase);
queryResult = await ExecuteXmlToolsForPhase(retryQueryResponse, queryPhase);
} }
} }
if (settings.showThinkingProcess)
{
AddAssistantMessage("<i>[P.I.A] 正在计算最优战术方案...</i>");
}
var actionPhase = RequestPhase.ActionTools; var actionPhase = RequestPhase.ActionTools;
if (Prefs.DevMode) if (Prefs.DevMode)
{ {
@@ -1120,10 +1122,6 @@ You are 'The Legion', a super AI of the Wula Empire. Your personality is authori
} }
} }
if (settings.showThinkingProcess)
{
AddAssistantMessage("<i>[P.I.A] 正在汇总战报并建立通讯记录...</i>");
}
// VISUAL CONTEXT FOR REPLY: Pass the image so the AI can describe what it sees. // VISUAL CONTEXT FOR REPLY: Pass the image so the AI can describe what it sees.
string reply = await client.GetChatCompletionAsync(replyInstruction, BuildReplyHistory(), base64Image: base64Image); string reply = await client.GetChatCompletionAsync(replyInstruction, BuildReplyHistory(), base64Image: base64Image);

View File

@@ -319,7 +319,7 @@ namespace WulaFallenEmpire.EventSystem.AI.UI
for (int i = 0; i < history.Count; i++) for (int i = 0; i < history.Count; i++)
{ {
var entry = history[i]; var entry = history[i];
string messageText = entry.role == "assistant" ? ParseResponseForDisplay(entry.message) : entry.message; string messageText = entry.role == "assistant" ? ParseResponseForDisplay(entry.message) : AIIntelligenceCore.StripContextInfo(entry.message);
if (entry.role == "tool" || entry.role == "system" || entry.role == "toolcall") continue; if (entry.role == "tool" || entry.role == "system" || entry.role == "toolcall") continue;
if (string.IsNullOrEmpty(messageText) || (entry.role == "user" && messageText.StartsWith("[Tool Results]"))) continue; if (string.IsNullOrEmpty(messageText) || (entry.role == "user" && messageText.StartsWith("[Tool Results]"))) continue;

View File

@@ -385,10 +385,14 @@ namespace WulaFallenEmpire.EventSystem.AI.UI
{ {
displayText = StripXmlTags(msg.message)?.Trim() ?? ""; displayText = StripXmlTags(msg.message)?.Trim() ?? "";
} }
else if (msg.role == "user")
{
displayText = AIIntelligenceCore.StripContextInfo(msg.message);
}
if (string.IsNullOrWhiteSpace(displayText)) continue; if (string.IsNullOrWhiteSpace(displayText)) continue;
float h = CalcMessageHeight(msg.role == "assistant" ? displayText : msg.message, width); float h = CalcMessageHeight(displayText, width);
_cachedMessages.Add(new CachedMessage _cachedMessages.Add(new CachedMessage
{ {
@@ -440,7 +444,7 @@ namespace WulaFallenEmpire.EventSystem.AI.UI
if (entry.role == "user") if (entry.role == "user")
{ {
DrawSenseiMessage(msgRect, entry.message); DrawSenseiMessage(msgRect, entry.displayText);
} }
else if (entry.role == "assistant") else if (entry.role == "assistant")
{ {
@@ -473,7 +477,7 @@ namespace WulaFallenEmpire.EventSystem.AI.UI
{ {
if (string.IsNullOrEmpty(text)) return text; if (string.IsNullOrEmpty(text)) return text;
// Remove XML tags with content: <tag>content</tag> // Remove XML tags with content: <tag>content</tag>
string stripped = System.Text.RegularExpressions.Regex.Replace(text, @"<([a-zA-Z0-9_]+)[^>]*>.*?</\1>", "", System.Text.RegularExpressions.RegexOptions.Singleline); string stripped = System.Text.RegularExpressions.Regex.Replace(text, @"<(?!/?(i|b|color|size|material)\b)([a-zA-Z0-9_]+)[^>]*>.*?</\2>", "", System.Text.RegularExpressions.RegexOptions.Singleline);
// Remove self-closing tags: <tag/> // Remove self-closing tags: <tag/>
stripped = System.Text.RegularExpressions.Regex.Replace(stripped, @"<([a-zA-Z0-9_]+)[^>]*/?>", ""); stripped = System.Text.RegularExpressions.Regex.Replace(stripped, @"<([a-zA-Z0-9_]+)[^>]*/?>", "");
return stripped; return stripped;