zc
This commit is contained in:
Binary file not shown.
@@ -37,7 +37,7 @@ namespace WulaFallenEmpire.EventSystem.AI
|
|||||||
StringBuilder jsonBuilder = new StringBuilder();
|
StringBuilder jsonBuilder = new StringBuilder();
|
||||||
jsonBuilder.Append("{");
|
jsonBuilder.Append("{");
|
||||||
jsonBuilder.Append($"\"model\": \"{_model}\",");
|
jsonBuilder.Append($"\"model\": \"{_model}\",");
|
||||||
jsonBuilder.Append("\"stream\": false,");
|
jsonBuilder.Append("\"stream\": false,"); // We request non-stream, but handle stream if returned
|
||||||
jsonBuilder.Append("\"messages\": [");
|
jsonBuilder.Append("\"messages\": [");
|
||||||
|
|
||||||
// System instruction
|
// System instruction
|
||||||
@@ -108,55 +108,33 @@ namespace WulaFallenEmpire.EventSystem.AI
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Robust parsing for "content": "..." allowing for whitespace variations
|
// Check for stream format (SSE)
|
||||||
int contentIndex = json.IndexOf("\"content\"");
|
// SSE lines start with "data: "
|
||||||
if (contentIndex == -1) return null;
|
if (json.TrimStart().StartsWith("data:"))
|
||||||
|
|
||||||
// Find the opening quote after "content"
|
|
||||||
int openQuoteIndex = -1;
|
|
||||||
for (int i = contentIndex + 9; i < json.Length; i++)
|
|
||||||
{
|
{
|
||||||
if (json[i] == '"')
|
StringBuilder fullContent = new StringBuilder();
|
||||||
|
string[] lines = json.Split(new[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
foreach (string line in lines)
|
||||||
{
|
{
|
||||||
openQuoteIndex = i;
|
string trimmedLine = line.Trim();
|
||||||
break;
|
if (trimmedLine == "data: [DONE]") continue;
|
||||||
|
if (trimmedLine.StartsWith("data: "))
|
||||||
|
{
|
||||||
|
string dataJson = trimmedLine.Substring(6);
|
||||||
|
// Extract content from this chunk
|
||||||
|
string chunkContent = ExtractJsonValue(dataJson, "content");
|
||||||
|
if (!string.IsNullOrEmpty(chunkContent))
|
||||||
|
{
|
||||||
|
fullContent.Append(chunkContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return fullContent.ToString();
|
||||||
}
|
}
|
||||||
if (openQuoteIndex == -1) return null;
|
else
|
||||||
|
|
||||||
int startIndex = openQuoteIndex + 1;
|
|
||||||
StringBuilder content = new StringBuilder();
|
|
||||||
bool escaped = false;
|
|
||||||
|
|
||||||
for (int i = startIndex; i < json.Length; i++)
|
|
||||||
{
|
{
|
||||||
char c = json[i];
|
// Standard non-stream format
|
||||||
if (escaped)
|
return ExtractJsonValue(json, "content");
|
||||||
{
|
|
||||||
if (c == 'n') content.Append('\n');
|
|
||||||
else if (c == 'r') content.Append('\r');
|
|
||||||
else if (c == 't') content.Append('\t');
|
|
||||||
else if (c == '"') content.Append('"');
|
|
||||||
else if (c == '\\') content.Append('\\');
|
|
||||||
else content.Append(c); // Literal
|
|
||||||
escaped = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (c == '\\')
|
|
||||||
{
|
|
||||||
escaped = true;
|
|
||||||
}
|
|
||||||
else if (c == '"')
|
|
||||||
{
|
|
||||||
// End of string
|
|
||||||
return content.ToString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
content.Append(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -165,5 +143,57 @@ namespace WulaFallenEmpire.EventSystem.AI
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private string ExtractJsonValue(string json, string key)
|
||||||
|
{
|
||||||
|
// Simple parser to find "key": "value"
|
||||||
|
// This is not a full JSON parser and assumes standard formatting
|
||||||
|
string keyPattern = $"\"{key}\"";
|
||||||
|
int keyIndex = json.IndexOf(keyPattern);
|
||||||
|
if (keyIndex == -1) return null;
|
||||||
|
|
||||||
|
// Find the colon after the key
|
||||||
|
int colonIndex = json.IndexOf(':', keyIndex + keyPattern.Length);
|
||||||
|
if (colonIndex == -1) return null;
|
||||||
|
|
||||||
|
// Find the opening quote of the value
|
||||||
|
int valueStart = json.IndexOf('"', colonIndex);
|
||||||
|
if (valueStart == -1) return null;
|
||||||
|
|
||||||
|
// Extract string with escape handling
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
bool escaped = false;
|
||||||
|
for (int i = valueStart + 1; i < json.Length; i++)
|
||||||
|
{
|
||||||
|
char c = json[i];
|
||||||
|
if (escaped)
|
||||||
|
{
|
||||||
|
if (c == 'n') sb.Append('\n');
|
||||||
|
else if (c == 'r') sb.Append('\r');
|
||||||
|
else if (c == 't') sb.Append('\t');
|
||||||
|
else if (c == '"') sb.Append('"');
|
||||||
|
else if (c == '\\') sb.Append('\\');
|
||||||
|
else sb.Append(c); // Literal
|
||||||
|
escaped = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (c == '\\')
|
||||||
|
{
|
||||||
|
escaped = true;
|
||||||
|
}
|
||||||
|
else if (c == '"')
|
||||||
|
{
|
||||||
|
// End of string
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sb.Append(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ namespace WulaFallenEmpire.EventSystem.AI.Tools
|
|||||||
Faction faction = Find.FactionManager.FirstFactionOfDef(FactionDef.Named("Wula_PIA_Legion_Faction"));
|
Faction faction = Find.FactionManager.FirstFactionOfDef(FactionDef.Named("Wula_PIA_Legion_Faction"));
|
||||||
if (faction != null)
|
if (faction != null)
|
||||||
{
|
{
|
||||||
Messages.Message("Wula_ResourceDrop".Translate(faction.Name), new LookTargets(dropSpot, map), MessageTypeDefOf.PositiveEvent);
|
Messages.Message("Wula_ResourceDrop".Translate(faction.Name.Named("FACTION_name")), new LookTargets(dropSpot, map), MessageTypeDefOf.PositiveEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
resultLog.Length -= 2; // Remove trailing comma
|
resultLog.Length -= 2; // Remove trailing comma
|
||||||
|
|||||||
@@ -506,7 +506,9 @@ When the player requests any form of resources, you MUST follow this multi-turn
|
|||||||
bool isLastMessage = i == filteredHistory.Count - 1;
|
bool isLastMessage = i == filteredHistory.Count - 1;
|
||||||
Text.Font = (isLastMessage && entry.role == "assistant") ? GameFont.Medium : GameFont.Small;
|
Text.Font = (isLastMessage && entry.role == "assistant") ? GameFont.Medium : GameFont.Small;
|
||||||
|
|
||||||
viewHeight += Text.CalcHeight(text, rect.width - 16f) + 15f; // Add padding
|
// Increase padding significantly for Medium font to prevent clipping
|
||||||
|
float padding = (isLastMessage && entry.role == "assistant") ? 30f : 15f;
|
||||||
|
viewHeight += Text.CalcHeight(text, rect.width - 16f) + padding;
|
||||||
}
|
}
|
||||||
Rect viewRect = new Rect(0f, 0f, rect.width - 16f, viewHeight);
|
Rect viewRect = new Rect(0f, 0f, rect.width - 16f, viewHeight);
|
||||||
Widgets.BeginScrollView(rect, ref _scrollPosition, viewRect);
|
Widgets.BeginScrollView(rect, ref _scrollPosition, viewRect);
|
||||||
@@ -520,7 +522,9 @@ When the player requests any form of resources, you MUST follow this multi-turn
|
|||||||
|
|
||||||
bool isLastMessage = i == filteredHistory.Count - 1;
|
bool isLastMessage = i == filteredHistory.Count - 1;
|
||||||
Text.Font = (isLastMessage && entry.role == "assistant") ? GameFont.Medium : GameFont.Small;
|
Text.Font = (isLastMessage && entry.role == "assistant") ? GameFont.Medium : GameFont.Small;
|
||||||
float height = Text.CalcHeight(text, viewRect.width) + 10f; // Increased padding
|
|
||||||
|
float padding = (isLastMessage && entry.role == "assistant") ? 30f : 15f;
|
||||||
|
float height = Text.CalcHeight(text, viewRect.width) + padding;
|
||||||
Rect labelRect = new Rect(0f, curY, viewRect.width, height);
|
Rect labelRect = new Rect(0f, curY, viewRect.width, height);
|
||||||
if (entry.role == "user")
|
if (entry.role == "user")
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user