Add ConversationLog (start/append/end with monotonic per-convo index)
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using backend.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace backend.Conversations;
|
||||
|
||||
public class ConversationLog(AppDbContext db)
|
||||
{
|
||||
public async Task<Conversation> StartAsync(Guid deviceId, CancellationToken ct)
|
||||
{
|
||||
var c = new Conversation
|
||||
{
|
||||
DeviceId = deviceId,
|
||||
StartedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
db.Conversations.Add(c);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return c;
|
||||
}
|
||||
|
||||
public Task AppendUserAsync(Guid conversationId, string text, CancellationToken ct)
|
||||
=> AppendAsync(conversationId, TurnRole.User, text: text, ct: ct);
|
||||
|
||||
public Task AppendAssistantAsync(Guid conversationId, string text, CancellationToken ct)
|
||||
=> AppendAsync(conversationId, TurnRole.Assistant, text: text, ct: ct);
|
||||
|
||||
public Task AppendToolAsync(
|
||||
Guid conversationId, string toolName, string argsJson, string resultJson, CancellationToken ct)
|
||||
=> AppendAsync(conversationId, TurnRole.Tool,
|
||||
toolName: toolName, argsJson: argsJson, resultJson: resultJson, ct: ct);
|
||||
|
||||
public async Task EndAsync(Guid conversationId, EndReason reason, CancellationToken ct)
|
||||
{
|
||||
var row = await db.Conversations.FirstAsync(c => c.Id == conversationId, ct);
|
||||
row.EndedAt = DateTimeOffset.UtcNow;
|
||||
row.EndReason = reason;
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
private async Task AppendAsync(
|
||||
Guid conversationId, TurnRole role,
|
||||
string? text = null, string? toolName = null,
|
||||
string? argsJson = null, string? resultJson = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var next = await db.Turns
|
||||
.Where(t => t.ConversationId == conversationId)
|
||||
.Select(t => (int?)t.Index)
|
||||
.MaxAsync(ct) ?? -1;
|
||||
|
||||
db.Turns.Add(new Turn
|
||||
{
|
||||
ConversationId = conversationId,
|
||||
Index = next + 1,
|
||||
Role = role,
|
||||
Text = text,
|
||||
ToolName = toolName,
|
||||
ToolArgsJson = argsJson,
|
||||
ToolResultJson = resultJson,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
});
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,7 @@ builder.Services.AddAuthorization();
|
||||
builder.Services.AddScoped<backend.Devices.DeviceTokenService>();
|
||||
builder.Services.AddScoped<backend.Pairing.PairingService>();
|
||||
builder.Services.AddSingleton<backend.Install.InstallScriptBuilder>();
|
||||
builder.Services.AddScoped<backend.Conversations.ConversationLog>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user