Merge pull request #1191 from yileicn/master

instruct log optimize
This commit is contained in:
Nick Yi 2025-10-16 11:03:43 +08:00 committed by GitHub
commit cf22033e30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 75 additions and 0 deletions

View file

@ -9,4 +9,5 @@ public class InstructResponseModel
public string UserMessage { get; set; } = default!; public string UserMessage { get; set; } = default!;
public string? SystemInstruction { get; set; } public string? SystemInstruction { get; set; }
public string CompletionText { get; set; } = default!; public string CompletionText { get; set; } = default!;
public string LogId { get; set; } = default!;
} }

View file

@ -10,4 +10,7 @@ public class InstructResult : ITrackableMessage
[JsonPropertyName("template")] [JsonPropertyName("template")]
public string? Template { get; set; } public string? Template { get; set; }
public Dictionary<string, string>? States { get; set; } = new(); public Dictionary<string, string>? States { get; set; } = new();
[JsonPropertyName("log_id")]
public string LogId { get; set; } = default!;
} }

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
namespace BotSharp.Abstraction.Loggers.Services; namespace BotSharp.Abstraction.Loggers.Services;
@ -13,5 +14,6 @@ public interface ILoggerService
#region Instruction #region Instruction
Task<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter); Task<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter);
Task<List<string>> GetInstructionLogSearchKeys(InstructLogKeysFilter filter); Task<List<string>> GetInstructionLogSearchKeys(InstructLogKeysFilter filter);
Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesModel request);
#endregion #endregion
} }

View file

@ -197,6 +197,9 @@ public interface IBotSharpRepository : IHaveServiceProvider
List<string> GetInstructionLogSearchKeys(InstructLogKeysFilter filter) List<string> GetInstructionLogSearchKeys(InstructLogKeysFilter filter)
=> throw new NotImplementedException(); => throw new NotImplementedException();
Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesModel updateInstructionStates)
=> throw new NotImplementedException();
#endregion #endregion
#region Statistics #region Statistics

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Abstraction.Repositories.Models
{
public class UpdateInstructionLogStatesModel
{
public string LogId { get; set; }
public string StateKeyPrefix { get; set; } = "new_";
public Dictionary<string, string> States { get; set; }
}
}

View file

@ -133,12 +133,14 @@ public partial class InstructService
response.Text = result; response.Text = result;
} }
response.LogId = Guid.NewGuid().ToString();
// After completion hooks // After completion hooks
foreach (var hook in hooks) foreach (var hook in hooks)
{ {
await hook.AfterCompletion(agent, response); await hook.AfterCompletion(agent, response);
await hook.OnResponseGenerated(new InstructResponseModel await hook.OnResponseGenerated(new InstructResponseModel
{ {
LogId = response.LogId,
AgentId = agentId, AgentId = agentId,
Provider = provider, Provider = provider,
Model = model, Model = model,

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Abstraction.Users.Models; using BotSharp.Abstraction.Users.Models;
namespace BotSharp.Core.Loggers.Services; namespace BotSharp.Core.Loggers.Services;
@ -77,4 +78,13 @@ public partial class LoggerService
keys = filter.PreLoad ? keys : keys.Where(x => x.Contains(filter.Query ?? string.Empty, StringComparison.OrdinalIgnoreCase)).ToList(); keys = filter.PreLoad ? keys : keys.Where(x => x.Contains(filter.Query ?? string.Empty, StringComparison.OrdinalIgnoreCase)).ToList();
return keys.OrderBy(x => x).Take(filter.KeyLimit).ToList(); return keys.OrderBy(x => x).Take(filter.KeyLimit).ToList();
} }
public async Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesModel request)
{
if(request == null) return false;
if (string.IsNullOrEmpty(request.LogId) || request.States.IsNullOrEmpty()) return false;
var db = _services.GetRequiredService<IBotSharpRepository>();
return await db.UpdateInstructionLogStates(request);
}
} }

View file

@ -48,6 +48,7 @@ public class InstructionLogHook : InstructHookBase
{ {
new InstructionLogModel new InstructionLogModel
{ {
Id = response.LogId,
AgentId = response.AgentId, AgentId = response.AgentId,
Provider = response.Provider, Provider = response.Provider,
Model = response.Model, Model = response.Model,

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Loggers.Services; using BotSharp.Abstraction.Loggers.Services;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.OpenAPI.ViewModels.Instructs; using BotSharp.OpenAPI.ViewModels.Instructs;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -76,5 +77,13 @@ public class LoggerController : ControllerBase
var keys = await logging.GetInstructionLogSearchKeys(request); var keys = await logging.GetInstructionLogSearchKeys(request);
return keys; return keys;
} }
[HttpPost("/logger/instruction/log/{logId}/states")]
public async Task<bool> UpdateInstructionLogStates([FromRoute] string logId, UpdateInstructionLogStatesModel request)
{
request.LogId = logId;
var logging = _services.GetRequiredService<ILoggerService>();
return await logging.UpdateInstructionLogStates(request);
}
#endregion #endregion
} }

View file

@ -19,6 +19,7 @@ public class InstructionLogDocument : MongoBase
{ {
return new InstructionLogDocument return new InstructionLogDocument
{ {
Id = log.Id,
AgentId = log.AgentId, AgentId = log.AgentId,
Provider = log.Provider, Provider = log.Provider,
Model = log.Model, Model = log.Model,

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using MongoDB.Driver; using MongoDB.Driver;
using System.Text.Json; using System.Text.Json;
@ -169,6 +170,33 @@ public partial class MongoRepository
return true; return true;
} }
public async Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesModel updateInstructionStates)
{
var id = updateInstructionStates.LogId;
var logDoc = await _dc.InstructionLogs.Find(p => p.Id == id).FirstOrDefaultAsync();
if (logDoc == null) return false;
foreach (var pair in updateInstructionStates.States)
{
var key = updateInstructionStates.StateKeyPrefix + pair.Key;
try
{
var jsonStr = JsonSerializer.Serialize(new { Data = JsonDocument.Parse(pair.Value) }, _botSharpOptions.JsonSerializerOptions);
var json = BsonDocument.Parse(jsonStr);
logDoc.States[key] = json;
}
catch
{
var jsonStr = JsonSerializer.Serialize(new { Data = pair.Value }, _botSharpOptions.JsonSerializerOptions);
var json = BsonDocument.Parse(jsonStr);
logDoc.States[key] = json;
}
}
await _dc.InstructionLogs.ReplaceOneAsync(p => p.Id == id, logDoc);
return true;
}
public async ValueTask<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter) public async ValueTask<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter)
{ {
if (filter == null) if (filter == null)