Persistenz: SQLite + Dapper, chains/messages Tabellen, History-Endpoint, Frontend History-Load
This commit is contained in:
1 parent
5f210323fd
commit
9f2f3fee1f
9 files changed
+370
-6
No files matched your search
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
@@ -19,7 +21,7 @@ public static class ChatEndpoint
|
||||
app.MapPost("/api/chat", HandleChat);
|
||||
}
|
||||
|
||||
private static async Task HandleChat(HttpRequest req, HttpResponse res, IHttpClientFactory httpFactory, IConfiguration config, ToolService toolService)
|
||||
private static async Task HandleChat(HttpRequest req, HttpResponse res, IHttpClientFactory httpFactory, IConfiguration config, ToolService toolService, DatabaseService db)
|
||||
{
|
||||
var body = await JsonSerializer.DeserializeAsync<ChatRequest>(req.Body);
|
||||
if (body == null || body.Messages == null || body.Messages.Count == 0)
|
||||
@@ -48,10 +50,34 @@ public static class ChatEndpoint
|
||||
res.Headers["Cache-Control"] = "no-cache";
|
||||
res.Headers["Connection"] = "keep-alive";
|
||||
|
||||
var chainId = Guid.NewGuid().ToString("N");
|
||||
|
||||
// Chain zuerst anlegen (FK für messages)
|
||||
await db.CreateChain(new ChainRecord
|
||||
{
|
||||
ChainId = chainId,
|
||||
Model = modelId,
|
||||
SystemPrompt = body.SystemPrompt
|
||||
});
|
||||
|
||||
var messages = new List<object>();
|
||||
foreach (var msg in body.Messages)
|
||||
messages.Add(new { role = msg.Role, content = msg.Content });
|
||||
|
||||
// Nur die letzte User-Message speichern (= die neue)
|
||||
var lastUserMsg = body.Messages.LastOrDefault(m => m.Role == "user");
|
||||
if (lastUserMsg != null)
|
||||
{
|
||||
await db.SaveMessage(new MessageRecord
|
||||
{
|
||||
ChainId = chainId,
|
||||
Iteration = 0,
|
||||
Role = "user",
|
||||
Type = "text",
|
||||
Content = lastUserMsg.Content
|
||||
});
|
||||
}
|
||||
|
||||
var systemPrompt = new List<object>
|
||||
{
|
||||
new { type = "text", text = "You are Claude Code, Anthropic's official CLI for Claude." }
|
||||
@@ -59,7 +85,7 @@ public static class ChatEndpoint
|
||||
if (!string.IsNullOrWhiteSpace(body.SystemPrompt))
|
||||
systemPrompt.Add(new { type = "text", text = body.SystemPrompt });
|
||||
|
||||
var anthropic = new AnthropicService(httpFactory, token, toolService);
|
||||
await anthropic.StreamChat(res, modelId, systemPrompt, messages);
|
||||
var anthropic = new AnthropicService(httpFactory, token, toolService, db);
|
||||
await anthropic.StreamChat(res, modelId, systemPrompt, messages, chainId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Gott.Models;
|
||||
using Gott.Services;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
namespace Gott.Endpoints;
|
||||
|
||||
public static class HistoryEndpoint
|
||||
{
|
||||
public static void MapHistoryEndpoint(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.MapGet("/api/history", HandleHistory);
|
||||
}
|
||||
|
||||
private static async Task HandleHistory(HttpContext ctx, DatabaseService db)
|
||||
{
|
||||
var sessionId = ctx.Request.Query["sessionId"].FirstOrDefault();
|
||||
var limitStr = ctx.Request.Query["limit"].FirstOrDefault();
|
||||
var limit = int.TryParse(limitStr, out var l) ? l : 50;
|
||||
|
||||
IEnumerable<ChainRecord> chains;
|
||||
if (string.IsNullOrEmpty(sessionId) || sessionId == "null")
|
||||
chains = await db.GetRecentChains(limit);
|
||||
else
|
||||
chains = await db.GetSessionChains(sessionId, limit);
|
||||
|
||||
var result = new List<object>();
|
||||
foreach (var chain in chains)
|
||||
{
|
||||
var messages = await db.GetChainMessages(chain.ChainId);
|
||||
result.Add(new { chain, messages });
|
||||
}
|
||||
|
||||
await ctx.Response.WriteAsJsonAsync(result);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user