diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6089870 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.bak +*.db +bin +obj diff --git a/AISApp/AISApp.csproj b/AISApp/AISApp.csproj new file mode 100644 index 0000000..eb93366 --- /dev/null +++ b/AISApp/AISApp.csproj @@ -0,0 +1,14 @@ + + + + Exe + net9.0 + enable + enable + + + + + + + diff --git a/api_client.cs b/AISApp/Program.cs similarity index 95% rename from api_client.cs rename to AISApp/Program.cs index 081fce0..e2e2ecd 100644 --- a/api_client.cs +++ b/AISApp/Program.cs @@ -92,13 +92,15 @@ namespace AISApp double backoff = 1, string? name = null, double temperature = 0.0, - string dbName = ":memory:", + string dbName = "tuna_ais_client.db", bool safeMode = true, string baseUrl = "openrouter.ai", string rel = "/api", - string? apiKey = null + string? apiKey = null + ) { + Console.WriteLine($"[INFO] Initializing AIS name={name} model={model} db={dbName} api_key={apiKey}"); Loaded = dbName != ":memory:" && File.Exists(dbName); DbName = dbName; @@ -395,8 +397,8 @@ namespace AISApp if (firstColon >= 0 && rest.IndexOf(':', firstColon + 1) < 0) { var user = rest[..firstColon]; - var assistant = rest[(firstColon + 1)..]; - Append(user, assistant); + var assistantMessage = rest[(firstColon + 1)..]; + Append(user, assistantMessage); return "true"; } } @@ -419,7 +421,7 @@ namespace AISApp Console.WriteLine(url); int attempt = 0; - string assistant = "ERROR"; + string assistantResponse = "ERROR"; double backoff = Backoff; while (attempt < MaxRetries) @@ -435,15 +437,27 @@ namespace AISApp using var resp = await Http.SendAsync(req, ct); var text = await resp.Content.ReadAsStringAsync(ct); + //var parsed = JsonSerializer.Deserialize(text, JsonOpts); + //assistantResponse = parsed?.Choices?[0]?.Message?.Content ?? "ERROR"; var parsed = JsonSerializer.Deserialize(text, JsonOpts); - assistant = parsed?.Choices?[0]?.Message?.Content ?? "ERROR"; + if (parsed?.Choices != null && parsed.Choices.Count > 0) + { + var msg = parsed.Choices[0]?.Message?.Content; + assistantResponse = string.IsNullOrEmpty(msg) ? "ERROR" : msg!; + } + else + { + assistantResponse = text; + // assistantResponse = "ERROR"; + } + SaveSettings(); - _messages.Add(new ChatMessage { Role = "assistant", Content = assistant }); - SaveMessage("assistant", assistant); + _messages.Add(new ChatMessage { Role = "assistant", Content = assistantResponse }); + SaveMessage("assistant", assistantResponse); - return TryJson(assistant); + return TryJson(assistantResponse); } catch (Exception ex) { @@ -624,3 +638,4 @@ namespace AISApp } } } +