Initial commit working version.

This commit is contained in:
retoor 2025-09-10 11:49:38 +02:00
parent 985eb20a38
commit b56011d3cc
3 changed files with 42 additions and 9 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.bak
*.db
bin
obj

14
AISApp/AISApp.csproj Normal file
View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.9" />
</ItemGroup>
</Project>

View File

@ -92,13 +92,15 @@ namespace AISApp
double backoff = 1, double backoff = 1,
string? name = null, string? name = null,
double temperature = 0.0, double temperature = 0.0,
string dbName = ":memory:", string dbName = "tuna_ais_client.db",
bool safeMode = true, bool safeMode = true,
string baseUrl = "openrouter.ai", string baseUrl = "openrouter.ai",
string rel = "/api", 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); Loaded = dbName != ":memory:" && File.Exists(dbName);
DbName = dbName; DbName = dbName;
@ -395,8 +397,8 @@ namespace AISApp
if (firstColon >= 0 && rest.IndexOf(':', firstColon + 1) < 0) if (firstColon >= 0 && rest.IndexOf(':', firstColon + 1) < 0)
{ {
var user = rest[..firstColon]; var user = rest[..firstColon];
var assistant = rest[(firstColon + 1)..]; var assistantMessage = rest[(firstColon + 1)..];
Append(user, assistant); Append(user, assistantMessage);
return "true"; return "true";
} }
} }
@ -419,7 +421,7 @@ namespace AISApp
Console.WriteLine(url); Console.WriteLine(url);
int attempt = 0; int attempt = 0;
string assistant = "ERROR"; string assistantResponse = "ERROR";
double backoff = Backoff; double backoff = Backoff;
while (attempt < MaxRetries) while (attempt < MaxRetries)
@ -435,15 +437,27 @@ namespace AISApp
using var resp = await Http.SendAsync(req, ct); using var resp = await Http.SendAsync(req, ct);
var text = await resp.Content.ReadAsStringAsync(ct); var text = await resp.Content.ReadAsStringAsync(ct);
//var parsed = JsonSerializer.Deserialize<ChatResponse>(text, JsonOpts);
//assistantResponse = parsed?.Choices?[0]?.Message?.Content ?? "ERROR";
var parsed = JsonSerializer.Deserialize<ChatResponse>(text, JsonOpts); var parsed = JsonSerializer.Deserialize<ChatResponse>(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(); SaveSettings();
_messages.Add(new ChatMessage { Role = "assistant", Content = assistant }); _messages.Add(new ChatMessage { Role = "assistant", Content = assistantResponse });
SaveMessage("assistant", assistant); SaveMessage("assistant", assistantResponse);
return TryJson(assistant); return TryJson(assistantResponse);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -624,3 +638,4 @@ namespace AISApp
} }
} }
} }