Skip to content

Commit aab468f

Browse files
author
Jicheng Lu
committed
add session logging and crontab settings
1 parent 73c2e9c commit aab468f

File tree

24 files changed

+78
-26
lines changed

24 files changed

+78
-26
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace BotSharp.Abstraction.Crontab.Settings;
2+
3+
public class CrontabSettings
4+
{
5+
public CrontabBaseSetting EventSubscriber { get; set; } = new();
6+
public CrontabBaseSetting Watcher { get; set; } = new();
7+
}
8+
9+
public class CrontabBaseSetting
10+
{
11+
public bool Enabled { get; set; } = true;
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
using Microsoft.Extensions.Logging;
12
using System.Text.Json;
23

34
namespace BotSharp.Abstraction.Realtime.Models.Session;
45

56
public class ChatSessionOptions
67
{
8+
public string Provider { get; set; }
79
public int? BufferSize { get; set; }
810
public JsonSerializerOptions? JsonOptions { get; set; }
11+
public ILogger? Logger { get; set; }
912
}

src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs renamed to src/Infrastructure/BotSharp.Abstraction/Repositories/Settings/BotSharpDatabaseSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace BotSharp.Abstraction.Repositories;
1+
namespace BotSharp.Abstraction.Repositories.Settings;
22

33
public class BotSharpDatabaseSettings : DatabaseBasicSettings
44
{

src/Infrastructure/BotSharp.Core.Crontab/CrontabPlugin.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,22 @@ public class CrontabPlugin : IBotSharpPlugin
3232

3333
public void RegisterDI(IServiceCollection services, IConfiguration config)
3434
{
35+
var settings = new CrontabSettings();
36+
config.Bind("Crontab", settings);
37+
services.AddSingleton(settings);
38+
3539
services.AddScoped<IAgentUtilityHook, CrontabUtilityHook>();
3640
services.AddScoped<ICrontabService, CrontabService>();
3741
services.AddScoped<ITaskFeeder, CrontabService>();
3842

39-
services.AddHostedService<CrontabWatcher>();
40-
services.AddHostedService<CrontabEventSubscription>();
43+
if (settings.Watcher?.Enabled == true)
44+
{
45+
services.AddHostedService<CrontabWatcher>();
46+
}
47+
48+
if (settings.EventSubscriber?.Enabled == true)
49+
{
50+
services.AddHostedService<CrontabEventSubscription>();
51+
}
4152
}
4253
}

src/Infrastructure/BotSharp.Core.Crontab/Using.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
global using BotSharp.Abstraction.Agents.Enums;
77
global using BotSharp.Abstraction.Crontab;
88
global using BotSharp.Abstraction.Crontab.Models;
9+
global using BotSharp.Abstraction.Crontab.Settings;
910
global using BotSharp.Abstraction.Agents;
1011
global using BotSharp.Abstraction.Plugins;
1112
global using BotSharp.Abstraction.Conversations.Models;

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Repositories.Settings;
12
using BotSharp.Abstraction.Tasks.Models;
23
using BotSharp.Abstraction.Users.Enums;
34
using System.IO;

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Repositories.Enums;
2+
using BotSharp.Abstraction.Repositories.Settings;
23
using System.IO;
34

45
namespace BotSharp.Core.Agents.Services;

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Repositories.Enums;
2+
using BotSharp.Abstraction.Repositories.Settings;
23
using BotSharp.Abstraction.Users.Enums;
34
using BotSharp.Abstraction.Users.Models;
45
using System.IO;

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Repositories.Settings;
12
using System.IO;
23
using System.Reflection;
34

src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using BotSharp.Core.Templating;
1717
using BotSharp.Abstraction.Infrastructures.Enums;
1818
using BotSharp.Abstraction.Realtime;
19+
using BotSharp.Abstraction.Repositories.Settings;
1920

2021
namespace BotSharp.Core;
2122

@@ -71,17 +72,6 @@ public static IServiceCollection UsingSqlServer(this IServiceCollection services
7172
return services;
7273
}
7374

74-
//public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config)
75-
//{
76-
// services.AddScoped<IBotSharpRepository>(sp =>
77-
// {
78-
// var myDatabaseSettings = sp.GetRequiredService<BotSharpDatabaseSettings>();
79-
// return new FileRepository(myDatabaseSettings, sp);
80-
// });
81-
82-
// return services;
83-
//}
84-
8575
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
8676
{
8777
if (app == null)

src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Repositories.Settings;
12
using System.IO;
23

34
namespace BotSharp.Core.Files.Services;

src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public async Task<bool> LockAsync(string resource, Func<Task> action, int timeou
2424
var redis = _services.GetService<IConnectionMultiplexer>();
2525
if (redis == null)
2626
{
27+
#if !DEBUG
2728
_logger.LogInformation($"The Redis server is experiencing issues and is not functioning as expected.");
29+
#endif
2830
await action();
2931
return true;
3032
}
@@ -50,7 +52,9 @@ public bool Lock(string resource, Action action, int timeoutInSeconds = 30)
5052
var redis = _services.GetRequiredService<IConnectionMultiplexer>();
5153
if (redis == null)
5254
{
55+
#if !DEBUG
5356
_logger.LogWarning($"The Redis server is experiencing issues and is not functioning as expected.");
57+
#endif
5458
action();
5559
return false;
5660
}

src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataCollectionResult.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BotSharp.Abstraction.Realtime.Models.Session;
21
using System.ClientModel;
32
using System.Net.WebSockets;
43

@@ -7,16 +6,16 @@ namespace BotSharp.Core.Infrastructures.Websocket;
76
internal class AsyncWebsocketDataCollectionResult : AsyncCollectionResult<ClientResult>
87
{
98
private readonly WebSocket _webSocket;
10-
private readonly ChatSessionOptions? _sessionOptions;
9+
private readonly ChatSessionOptions? _options;
1110
private readonly CancellationToken _cancellationToken;
1211

1312
public AsyncWebsocketDataCollectionResult(
1413
WebSocket webSocket,
15-
ChatSessionOptions? sessionOptions,
14+
ChatSessionOptions? options,
1615
CancellationToken cancellationToken)
1716
{
1817
_webSocket = webSocket;
19-
_sessionOptions = sessionOptions;
18+
_options = options;
2019
_cancellationToken = cancellationToken;
2120
}
2221

@@ -27,7 +26,7 @@ public AsyncWebsocketDataCollectionResult(
2726

2827
public override async IAsyncEnumerable<ClientResult> GetRawPagesAsync()
2928
{
30-
await using var enumerator = new AsyncWebsocketDataResultEnumerator(_webSocket, _sessionOptions, _cancellationToken);
29+
await using var enumerator = new AsyncWebsocketDataResultEnumerator(_webSocket, _options, _cancellationToken);
3130
while (await enumerator.MoveNextAsync().ConfigureAwait(false))
3231
{
3332
yield return enumerator.Current;

src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataResultEnumerator.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.AspNetCore.Builder;
12
using System.Buffers;
23
using System.ClientModel;
34
using System.Net.WebSockets;
@@ -7,21 +8,21 @@ namespace BotSharp.Core.Infrastructures.Websocket;
78
internal class AsyncWebsocketDataResultEnumerator : IAsyncEnumerator<ClientResult>
89
{
910
private readonly WebSocket _webSocket;
10-
private readonly ChatSessionOptions? _sessionOptions;
11+
private readonly ChatSessionOptions? _options;
1112
private readonly CancellationToken _cancellationToken;
1213
private readonly byte[] _buffer;
1314

1415
private const int DEFAULT_BUFFER_SIZE = 1024 * 32;
1516

1617
public AsyncWebsocketDataResultEnumerator(
1718
WebSocket webSocket,
18-
ChatSessionOptions? sessionOptions,
19+
ChatSessionOptions? options,
1920
CancellationToken cancellationToken)
2021
{
2122
_webSocket = webSocket;
22-
_sessionOptions = sessionOptions;
23+
_options = options;
2324
_cancellationToken = cancellationToken;
24-
var bufferSize = sessionOptions?.BufferSize > 0 ? sessionOptions.BufferSize.Value : DEFAULT_BUFFER_SIZE;
25+
var bufferSize = options?.BufferSize > 0 ? options.BufferSize.Value : DEFAULT_BUFFER_SIZE;
2526
_buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
2627
}
2728

@@ -44,7 +45,10 @@ public async ValueTask<bool> MoveNextAsync()
4445
if (receivedResult.CloseStatus.HasValue)
4546
{
4647
#if DEBUG
47-
Console.WriteLine($"Websocket close: {receivedResult.CloseStatus} {receivedResult.CloseStatusDescription}");
48+
if (_options?.Logger != null)
49+
{
50+
_options.Logger.LogWarning($"{_options?.Provider} Websocket close: ({receivedResult.CloseStatus}) {receivedResult.CloseStatusDescription}");
51+
}
4852
#endif
4953
Current = null;
5054
return false;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Repositories.Settings;
12
using Microsoft.Data.SqlClient;
23
using System.Data.Common;
34

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using BotSharp.Abstraction.Users.Models;
66
using BotSharp.Abstraction.Plugins.Models;
77
using BotSharp.Abstraction.Tasks.Models;
8+
using BotSharp.Abstraction.Repositories.Settings;
89

910
namespace BotSharp.Core.Repository;
1011

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Repositories.Enums;
2+
using BotSharp.Abstraction.Repositories.Settings;
23
using BotSharp.Abstraction.Settings;
34
using Microsoft.Extensions.Configuration;
45

src/Plugins/BotSharp.Plugin.AudioHandler/Provider/NativeWhisperProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Repositories.Settings;
12
using Whisper.net;
23
using Whisper.net.Ggml;
34

src/Plugins/BotSharp.Plugin.ChatHub/ChatStreamMiddleware.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ private async Task HandleWebSocket(IServiceProvider services, string agentId, st
5454
_session?.Dispose();
5555
_session = new BotSharpRealtimeSession(services, webSocket, new ChatSessionOptions
5656
{
57+
Provider = "BotSharp Chat Stream",
5758
BufferSize = 1024 * 16,
5859
JsonOptions = BotSharpOptions.defaultJsonOptions
5960
});

src/Plugins/BotSharp.Plugin.GoogleAI/Providers/Realtime/RealTimeCompletionProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ public async Task Connect(
9797
_outputStream = new();
9898
_session = new LlmRealtimeSession(_services, new ChatSessionOptions
9999
{
100-
JsonOptions = _jsonOptions
100+
Provider = Provider,
101+
JsonOptions = _jsonOptions,
102+
Logger = _logger
101103
});
102104

103105
var uri = BuildWebsocketUri(modelSettings.ApiKey, "v1beta");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Repositories.Settings;
2+
13
namespace BotSharp.Plugin.MongoStorage;
24

35
public class MongoDbContext

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Repositories.Enums;
2+
using BotSharp.Abstraction.Repositories.Settings;
23
using BotSharp.Plugin.MongoStorage.Repository;
34

45
namespace BotSharp.Plugin.MongoStorage;

src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public async Task Connect(
7272
_session?.Dispose();
7373
_session = new LlmRealtimeSession(_services, new ChatSessionOptions
7474
{
75-
JsonOptions = _botsharpOptions.JsonSerializerOptions
75+
Provider = Provider,
76+
JsonOptions = _botsharpOptions.JsonSerializerOptions,
77+
Logger = _logger
7678
});
7779

7880
await _session.ConnectAsync(

src/WebStarter/appsettings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,15 @@
325325
"Enabled": false
326326
},
327327

328+
"Crontab": {
329+
"Watcher": {
330+
"Enabled": false
331+
},
332+
"EventSubscriber": {
333+
"Enabled": false
334+
}
335+
},
336+
328337
"Instruction": {
329338
"Logging": {
330339
"Enabled": true,
@@ -427,6 +436,7 @@
427436
"BucketName": "",
428437
"Region": ""
429438
},
439+
430440
"Qdrant": {
431441
"Url": "",
432442
"ApiKey": ""
@@ -468,6 +478,7 @@
468478
"ApiSecret": "",
469479
"ModelVersion": "V3_5"
470480
},
481+
471482
"MetaGLM": {
472483
"ApiKey": "6b6c8b3fca3e5da21d633e350980744d.938gruOqrK4BDqW8",
473484
"BaseAddress": "http://localhost:8100/v1/",

0 commit comments

Comments
 (0)