zc'
This commit is contained in:
@@ -12,6 +12,7 @@ namespace WulaFallenEmpire.EventSystem.AI
|
||||
private readonly string _apiKey;
|
||||
private readonly string _baseUrl;
|
||||
private readonly string _model;
|
||||
private const int MaxLogChars = 2000;
|
||||
|
||||
public SimpleAIClient(string apiKey, string baseUrl, string model)
|
||||
{
|
||||
@@ -41,28 +42,36 @@ namespace WulaFallenEmpire.EventSystem.AI
|
||||
jsonBuilder.Append("\"messages\": [");
|
||||
|
||||
// System instruction
|
||||
bool firstMessage = true;
|
||||
if (!string.IsNullOrEmpty(instruction))
|
||||
{
|
||||
jsonBuilder.Append($"{{\"role\": \"system\", \"content\": \"{EscapeJson(instruction)}\"}},");
|
||||
jsonBuilder.Append($"{{\"role\": \"system\", \"content\": \"{EscapeJson(instruction)}\"}}");
|
||||
firstMessage = false;
|
||||
}
|
||||
|
||||
// Messages
|
||||
for (int i = 0; i < messages.Count; i++)
|
||||
{
|
||||
var msg = messages[i];
|
||||
string role = msg.role.ToLower();
|
||||
string role = (msg.role ?? "user").ToLowerInvariant();
|
||||
if (role == "ai") role = "assistant";
|
||||
// Map other roles if needed
|
||||
else if (role == "tool") role = "system"; // Internal-only role; map to supported role for Chat Completions APIs.
|
||||
else if (role != "system" && role != "user" && role != "assistant") role = "user";
|
||||
|
||||
if (!firstMessage) jsonBuilder.Append(",");
|
||||
jsonBuilder.Append($"{{\"role\": \"{role}\", \"content\": \"{EscapeJson(msg.message)}\"}}");
|
||||
if (i < messages.Count - 1) jsonBuilder.Append(",");
|
||||
firstMessage = false;
|
||||
}
|
||||
|
||||
jsonBuilder.Append("]");
|
||||
jsonBuilder.Append("}");
|
||||
|
||||
string jsonBody = jsonBuilder.ToString();
|
||||
Log.Message($"[WulaAI] Sending request to {endpoint}:\n{jsonBody}");
|
||||
if (Prefs.DevMode)
|
||||
{
|
||||
Log.Message($"[WulaAI] Sending request to {endpoint} (model={_model}, messages={messages?.Count ?? 0})");
|
||||
Log.Message($"[WulaAI] Request body (truncated):\n{TruncateForLog(jsonBody)}");
|
||||
}
|
||||
|
||||
using (UnityWebRequest request = new UnityWebRequest(endpoint, "POST"))
|
||||
{
|
||||
@@ -84,24 +93,57 @@ namespace WulaFallenEmpire.EventSystem.AI
|
||||
|
||||
if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
|
||||
{
|
||||
Log.Error($"[WulaAI] API Error: {request.error}\nResponse: {request.downloadHandler.text}");
|
||||
Log.Error($"[WulaAI] API Error: {request.error}\nResponse (truncated): {TruncateForLog(request.downloadHandler.text)}");
|
||||
return null;
|
||||
}
|
||||
|
||||
string responseText = request.downloadHandler.text;
|
||||
Log.Message($"[WulaAI] Raw Response: {responseText}");
|
||||
if (Prefs.DevMode)
|
||||
{
|
||||
Log.Message($"[WulaAI] Raw Response (truncated): {TruncateForLog(responseText)}");
|
||||
}
|
||||
return ExtractContent(responseText);
|
||||
}
|
||||
}
|
||||
|
||||
private static string TruncateForLog(string s)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s)) return s;
|
||||
if (s.Length <= MaxLogChars) return s;
|
||||
return s.Substring(0, MaxLogChars) + $"... (truncated, total {s.Length} chars)";
|
||||
}
|
||||
|
||||
private string EscapeJson(string s)
|
||||
{
|
||||
if (s == null) return "";
|
||||
return s.Replace("\\", "\\\\")
|
||||
.Replace("\"", "\\\"")
|
||||
.Replace("\n", "\\n")
|
||||
.Replace("\r", "\\r")
|
||||
.Replace("\t", "\\t");
|
||||
|
||||
StringBuilder sb = new StringBuilder(s.Length + 16);
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
char c = s[i];
|
||||
switch (c)
|
||||
{
|
||||
case '\\': sb.Append("\\\\"); break;
|
||||
case '"': sb.Append("\\\""); break;
|
||||
case '\n': sb.Append("\\n"); break;
|
||||
case '\r': sb.Append("\\r"); break;
|
||||
case '\t': sb.Append("\\t"); break;
|
||||
case '\b': sb.Append("\\b"); break;
|
||||
case '\f': sb.Append("\\f"); break;
|
||||
default:
|
||||
if (c < 0x20)
|
||||
{
|
||||
sb.Append("\\u");
|
||||
sb.Append(((int)c).ToString("x4"));
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(c);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private string ExtractContent(string json)
|
||||
@@ -116,13 +158,13 @@ namespace WulaFallenEmpire.EventSystem.AI
|
||||
string[] lines = json.Split(new[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (string line in lines)
|
||||
{
|
||||
string trimmedLine = line.Trim();
|
||||
if (trimmedLine == "data: [DONE]") continue;
|
||||
if (trimmedLine.StartsWith("data: "))
|
||||
{
|
||||
string dataJson = trimmedLine.Substring(6);
|
||||
string trimmedLine = line.Trim();
|
||||
if (trimmedLine == "data: [DONE]") continue;
|
||||
if (trimmedLine.StartsWith("data: "))
|
||||
{
|
||||
string dataJson = trimmedLine.Substring(6);
|
||||
// Extract content from this chunk
|
||||
string chunkContent = ExtractJsonValue(dataJson, "content");
|
||||
string chunkContent = TryExtractAssistantContent(dataJson) ?? ExtractJsonValue(dataJson, "content");
|
||||
if (!string.IsNullOrEmpty(chunkContent))
|
||||
{
|
||||
fullContent.Append(chunkContent);
|
||||
@@ -134,7 +176,7 @@ namespace WulaFallenEmpire.EventSystem.AI
|
||||
else
|
||||
{
|
||||
// Standard non-stream format
|
||||
return ExtractJsonValue(json, "content");
|
||||
return TryExtractAssistantContent(json) ?? ExtractJsonValue(json, "content");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -144,12 +186,109 @@ namespace WulaFallenEmpire.EventSystem.AI
|
||||
return null;
|
||||
}
|
||||
|
||||
private string ExtractJsonValue(string json, string key)
|
||||
private static string TryExtractAssistantContent(string json)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(json)) return null;
|
||||
|
||||
int choicesIndex = json.IndexOf("\"choices\"", StringComparison.Ordinal);
|
||||
if (choicesIndex == -1) return null;
|
||||
|
||||
string firstChoiceJson = TryExtractFirstChoiceObject(json, choicesIndex);
|
||||
if (string.IsNullOrEmpty(firstChoiceJson)) return null;
|
||||
|
||||
int messageIndex = firstChoiceJson.IndexOf("\"message\"", StringComparison.Ordinal);
|
||||
if (messageIndex != -1)
|
||||
{
|
||||
return ExtractJsonValue(firstChoiceJson, "content", messageIndex);
|
||||
}
|
||||
|
||||
int deltaIndex = firstChoiceJson.IndexOf("\"delta\"", StringComparison.Ordinal);
|
||||
if (deltaIndex != -1)
|
||||
{
|
||||
return ExtractJsonValue(firstChoiceJson, "content", deltaIndex);
|
||||
}
|
||||
|
||||
return ExtractJsonValue(firstChoiceJson, "text", 0);
|
||||
}
|
||||
|
||||
private static string TryExtractFirstChoiceObject(string json, int choicesKeyIndex)
|
||||
{
|
||||
int arrayStart = json.IndexOf('[', choicesKeyIndex);
|
||||
if (arrayStart == -1) return null;
|
||||
|
||||
int objStart = json.IndexOf('{', arrayStart);
|
||||
if (objStart == -1) return null;
|
||||
|
||||
int objEnd = FindMatchingBrace(json, objStart);
|
||||
if (objEnd == -1) return null;
|
||||
|
||||
return json.Substring(objStart, objEnd - objStart + 1);
|
||||
}
|
||||
|
||||
private static int FindMatchingBrace(string json, int startIndex)
|
||||
{
|
||||
int depth = 0;
|
||||
bool inString = false;
|
||||
bool escaped = false;
|
||||
|
||||
for (int i = startIndex; i < json.Length; i++)
|
||||
{
|
||||
char c = json[i];
|
||||
if (inString)
|
||||
{
|
||||
if (escaped)
|
||||
{
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '\\')
|
||||
{
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '"')
|
||||
{
|
||||
inString = false;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '"')
|
||||
{
|
||||
inString = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '{')
|
||||
{
|
||||
depth++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '}')
|
||||
{
|
||||
depth--;
|
||||
if (depth == 0) return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static string ExtractJsonValue(string json, string key)
|
||||
{
|
||||
// Simple parser to find "key": "value"
|
||||
// This is not a full JSON parser and assumes standard formatting
|
||||
return ExtractJsonValue(json, key, 0);
|
||||
}
|
||||
|
||||
private static string ExtractJsonValue(string json, string key, int startIndex)
|
||||
{
|
||||
string keyPattern = $"\"{key}\"";
|
||||
int keyIndex = json.IndexOf(keyPattern);
|
||||
int keyIndex = json.IndexOf(keyPattern, startIndex, StringComparison.Ordinal);
|
||||
if (keyIndex == -1) return null;
|
||||
|
||||
// Find the colon after the key
|
||||
@@ -196,4 +335,4 @@ namespace WulaFallenEmpire.EventSystem.AI
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user