Skip to content

use session in twilio stream #1110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

iceljc
Copy link
Collaborator

@iceljc iceljc commented Jul 25, 2025

PR Type

Enhancement


Description

  • Refactor Twilio stream to use BotSharpRealtimeSession

  • Add error handling for JSON deserialization

  • Increase buffer size and add debug logging

  • Improve session lifecycle management


Diagram Walkthrough

flowchart LR
  A["WebSocket Connection"] --> B["BotSharpRealtimeSession"]
  B --> C["Event Processing"]
  C --> D["Error Handling"]
  D --> E["Session Cleanup"]
Loading

File Walkthrough

Relevant files
Enhancement
ChatStreamMiddleware.cs
Enhance ChatHub stream with logging and error handling     

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

  • Increase buffer size from 16KB to 32KB
  • Add logger to session options
  • Add debug logging for connection events
  • Enhance error handling in JSON deserialization
+26/-9   
TwilioStreamMiddleware.cs
Refactor to use BotSharpRealtimeSession with enhanced lifecycle

src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs

  • Replace manual WebSocket handling with BotSharpRealtimeSession
  • Add proper session lifecycle management with disposal
  • Implement error handling for JSON deserialization
  • Add debug logging for connection events
  • Save conversation states on disconnect
+46/-15 

Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Resource Leak

The _session field is a class-level instance variable that could lead to resource leaks if multiple concurrent requests access the same middleware instance. Each request disposes and recreates the session, but concurrent access could cause issues with session lifecycle management.

private BotSharpRealtimeSession _session;
Debug Logging

Using LogCritical for debug connection events is inappropriate as it indicates system failure rather than informational messages. This could trigger unnecessary alerts in production monitoring systems.

                _logger.LogCritical($"Start chat stream connection for conversation ({conversationId})");
#endif
Exception Handling

The JSON deserialization error handling creates a new empty StreamEventResponse object but continues processing with potentially null response data, which could cause null reference exceptions in subsequent switch statement.

StreamEventResponse? response = new();

try
{
    response = JsonSerializer.Deserialize<StreamEventResponse>(receivedText);
}
catch (Exception ex)
{
    _logger.LogError(ex, $"Error when deserializing twilio stream event response for conversation ({conversationId}) (response: {receivedText?.SubstringMax(30)})");
}

@iceljc iceljc requested a review from Oceania2018 July 25, 2025 19:52
Copy link

qodo-merge-pro bot commented Jul 25, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Add safe session cleanup

The session disposal should be wrapped in a try-catch block to prevent
exceptions during cleanup from masking the original error. Also consider using a
finally block to ensure disposal always occurs.

src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs [142-144]

 convService.SaveStates();
-await _session.DisconnectAsync();
-_session.Dispose();
+try
+{
+    await _session.DisconnectAsync();
+}
+finally
+{
+    _session?.Dispose();
+    _session = null;
+}
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: Using a try-finally block correctly ensures that _session.Dispose() is always called, even if _session.DisconnectAsync() throws an exception, which is a significant improvement in resource management.

Medium
Fix misleading response initialization

Initializing response to a new empty object and then potentially overwriting it
is misleading. Initialize to null and handle the null case properly in the
switch statement below.

src/Plugins/BotSharp.Plugin.ChatHub/ChatStreamMiddleware.cs [127-136]

-ChatStreamEventResponse? response = new();
+ChatStreamEventResponse? response = null;
 
 try
 {
     response = JsonSerializer.Deserialize<ChatStreamEventResponse>(receivedText);
 }
 catch (Exception ex)
 {
     _logger.LogError(ex, $"Error when deserializing chat stream event response for conversation ({conversationId}) (response: {receivedText?.SubstringMax(30)})");
 }
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why: Initializing response to null instead of new() more accurately reflects its state before deserialization and improves code clarity, as the subsequent code already handles the null case.

Low
Ensure proper session disposal

The session field should be disposed and nullified in a finally block or using
proper disposal patterns. Currently, if an exception occurs during session
creation, the old session may not be properly disposed.

src/Plugins/BotSharp.Plugin.Twilio/TwilioStreamMiddleware.cs [67-74]

 _session?.Dispose();
+_session = null;
 _session = new BotSharpRealtimeSession(services, webSocket, new ChatSessionOptions
 {
     Provider = "BotSharp Twilio Stream",
     BufferSize = 1024 * 32,
     JsonOptions = BotSharpOptions.defaultJsonOptions,
     Logger = _logger
 });
  • Apply / Chat
Suggestion importance[1-10]: 5

__

Why: Setting _session to null after disposal is a good defensive practice to prevent issues if the subsequent session creation fails, improving the code's robustness.

Low
  • Update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant