Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public class InstructResponseModel
public string UserMessage { get; set; } = default!;
public string? SystemInstruction { get; set; }
public string CompletionText { get; set; } = default!;
public string LogId { get; set; } = default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ public class InstructResult : ITrackableMessage
[JsonPropertyName("template")]
public string? Template { get; set; }
public Dictionary<string, string>? States { get; set; } = new();

[JsonPropertyName("log_id")]
public string LogId { get; set; } = default!;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;

namespace BotSharp.Abstraction.Loggers.Services;

Expand All @@ -13,5 +14,6 @@ public interface ILoggerService
#region Instruction
Task<PagedItems<InstructionLogModel>> GetInstructionLogs(InstructLogFilter filter);
Task<List<string>> GetInstructionLogSearchKeys(InstructLogKeysFilter filter);
Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesModel request);
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();
void CreateUser(User user) => throw new NotImplementedException();
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();

Check warning on line 49 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 49 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.
void UpdateUserVerified(string userId) => throw new NotImplementedException();
void AddDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
void RemoveDashboardConversation(string userId, string conversationId) => throw new NotImplementedException();
Expand Down Expand Up @@ -195,7 +195,10 @@

List<string> GetInstructionLogSearchKeys(InstructLogKeysFilter filter)
=> throw new NotImplementedException();

Task<bool> UpdateInstructionLogStates(UpdateInstructionLogStatesModel updateInstructionStates)
=> throw new NotImplementedException();
#endregion

Check failure on line 201 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

The type or namespace name 'UpdateInstructionLogStatesModel' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 201 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

The type or namespace name 'UpdateInstructionLogStatesModel' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 201 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'UpdateInstructionLogStatesModel' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 201 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'UpdateInstructionLogStatesModel' could not be found (are you missing a using directive or an assembly reference?)

#region Statistics
BotSharpStats? GetGlobalStats(string agentId, DateTime recordTime, StatsInterval interval)
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ public async Task<InstructResult> Execute(
response.Text = result;
}

response.LogId = Guid.NewGuid().ToString();
// After completion hooks
foreach (var hook in hooks)
{
await hook.AfterCompletion(agent, response);
await hook.OnResponseGenerated(new InstructResponseModel
{
LogId = response.LogId,
AgentId = agentId,
Provider = provider,
Model = model,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Abstraction.Users.Models;

namespace BotSharp.Core.Loggers.Services;
Expand Down Expand Up @@ -77,4 +78,13 @@ public async Task<List<string>> GetInstructionLogSearchKeys(InstructLogKeysFilte
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();
}

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public override async Task OnResponseGenerated(InstructResponseModel response)
{
new InstructionLogModel
{
Id = response.LogId,
AgentId = response.AgentId,
Provider = response.Provider,
Model = response.Model,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Loggers.Services;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.OpenAPI.ViewModels.Instructs;
using Microsoft.AspNetCore.Hosting;

Expand Down Expand Up @@ -76,5 +77,13 @@ public async Task<List<string>> GetInstructionLogKeys([FromQuery] InstructLogKey
var keys = await logging.GetInstructionLogSearchKeys(request);
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static InstructionLogDocument ToMongoModel(InstructionLogModel log)
{
return new InstructionLogDocument
{
Id = log.Id,
AgentId = log.AgentId,
Provider = log.Provider,
Model = log.Model,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using MongoDB.Driver;
using System.Text.Json;

Expand Down Expand Up @@ -169,6 +170,33 @@ public bool SaveInstructionLogs(IEnumerable<InstructionLogModel> logs)
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)
{
if (filter == null)
Expand Down
Loading