Skip to content

Commit cf98805

Browse files
authored
Merge pull request #186 from iceljc/features/add-execution-log
add execution log
2 parents 7fb9440 + 9f6f0e2 commit cf98805

File tree

11 files changed

+96
-16
lines changed

11 files changed

+96
-16
lines changed

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,7 @@ List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRou
3434
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
3535
Conversation GetConversation(string conversationId);
3636
List<Conversation> GetConversations(string userId);
37+
void AddExectionLogs(string conversationId, List<string> logs);
38+
List<string> GetExectionLogs(string conversationId);
3739
#endregion
3840
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public ConversationState Load(string conversationId)
5555
{
5656
_conversationId = conversationId;
5757

58-
_savedStates = _db.GetConversationStates(_conversationId);
58+
_savedStates = _db.GetConversationStates(_conversationId).ToList();
5959

6060
if (!_savedStates.IsNullOrEmpty())
6161
{
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using BotSharp.Abstraction.Evaluations;
22
using BotSharp.Abstraction.Repositories;
3-
using BotSharp.Abstraction.Utilities;
4-
using System.IO;
53

64
namespace BotSharp.Core.Evaluations;
75

@@ -19,13 +17,7 @@ public ExecutionLogger(
1917

2018
public void Append(string conversationId, string content)
2119
{
22-
var file = GetStorageFile(conversationId);
23-
File.AppendAllLines(file, new[] { content });
24-
}
25-
26-
private string GetStorageFile(string conversationId)
27-
{
28-
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
29-
return Path.Combine(dir, "execution.log");
20+
var db = _services.GetRequiredService<IBotSharpRepository>();
21+
db.AddExectionLogs(conversationId, new List<string> { content });
3022
}
3123
}

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public void UpdateConversationStates(string conversationId, List<StateKeyValue>
150150
{
151151
throw new NotImplementedException();
152152
}
153+
154+
public void AddExectionLogs(string conversationId, List<string> logs)
155+
{
156+
throw new NotImplementedException();
157+
}
158+
159+
public List<string> GetExectionLogs(string conversationId)
160+
{
161+
throw new NotImplementedException();
162+
}
153163
#endregion
154164

155165

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,33 @@ public List<Conversation> GetConversations(string userId)
747747

748748
return records;
749749
}
750+
751+
public void AddExectionLogs(string conversationId, List<string> logs)
752+
{
753+
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
754+
755+
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
756+
if (!Directory.Exists(dir))
757+
{
758+
Directory.CreateDirectory(dir);
759+
}
760+
761+
var file = Path.Combine(dir, "execution.log");
762+
File.AppendAllLines(file, logs);
763+
}
764+
765+
public List<string> GetExectionLogs(string conversationId)
766+
{
767+
var logs = new List<string>();
768+
if (string.IsNullOrEmpty(conversationId)) return logs;
769+
770+
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
771+
if (!Directory.Exists(dir)) return logs;
772+
773+
var file = Path.Combine(dir, "execution.log");
774+
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
775+
return logs;
776+
}
750777
#endregion
751778

752779
#region User

src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
1413
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
1514
</ItemGroup>
1615

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Plugin.MongoStorage.Collections;
2+
3+
public class ExectionLogCollection : MongoBase
4+
{
5+
public string ConversationId { get; set; }
6+
public List<string> Logs { get; set; }
7+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using MongoDB.Bson.Serialization.Attributes;
2-
using MongoDB.Bson.Serialization.IdGenerators;
32

43
namespace BotSharp.Plugin.MongoStorage;
54

65
[BsonIgnoreExtraElements(Inherited = true)]
7-
public class MongoBase
6+
public abstract class MongoBase
87
{
9-
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
8+
[BsonId(IdGenerator = typeof(StringGuidIdGenerator))]
109
public string Id { get; set; }
1110
}
11+
12+

src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public IMongoCollection<ConversationCollection> Conversations
3737
public IMongoCollection<ConversationDialogCollection> ConversationDialogs
3838
=> Database.GetCollection<ConversationDialogCollection>($"{_collectionPrefix}_ConversationDialogs");
3939

40+
public IMongoCollection<ExectionLogCollection> ExectionLogs
41+
=> Database.GetCollection<ExectionLogCollection>($"{_collectionPrefix}_ExectionLogs");
42+
4043
public IMongoCollection<UserCollection> Users
4144
=> Database.GetCollection<UserCollection>($"{_collectionPrefix}_Users");
4245

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using BotSharp.Abstraction.Users.Models;
66
using BotSharp.Plugin.MongoStorage.Collections;
77
using BotSharp.Plugin.MongoStorage.Models;
8-
using Aspects.Cache;
98

109
namespace BotSharp.Plugin.MongoStorage.Repository;
1110

@@ -727,6 +726,30 @@ public List<Conversation> GetConversations(string userId)
727726

728727
return records;
729728
}
729+
730+
public void AddExectionLogs(string conversationId, List<string> logs)
731+
{
732+
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
733+
734+
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
735+
var update = Builders<ExectionLogCollection>.Update
736+
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
737+
.PushEach(x => x.Logs, logs);
738+
739+
_dc.ExectionLogs.UpdateOne(filter, update, _options);
740+
}
741+
742+
public List<string> GetExectionLogs(string conversationId)
743+
{
744+
var logs = new List<string>();
745+
if (string.IsNullOrEmpty(conversationId)) return logs;
746+
747+
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
748+
var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault();
749+
750+
logs = logCollection?.Logs ?? new List<string>();
751+
return logs;
752+
}
730753
#endregion
731754

732755
#region User

0 commit comments

Comments
 (0)