Skip to content

Test/realtime test #1010

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

Merged
merged 10 commits into from
Apr 16, 2025
Merged

Test/realtime test #1010

merged 10 commits into from
Apr 16, 2025

Conversation

iceljc
Copy link
Collaborator

@iceljc iceljc commented Apr 14, 2025

PR Type

Enhancement, Tests


Description

  • Refactored WebSocket handling to use RealtimeChatSession.

    • Replaced ClientWebSocket with RealtimeChatSession for better modularity.
    • Added new classes for WebSocket data handling and session management.
  • Introduced new models and utilities for real-time updates.

    • Added SessionConversationUpdate for structured real-time responses.
    • Implemented AiWebsocketPipelineResponse for WebSocket response handling.
  • Enhanced test cases for real-time voice functionality.

    • Updated Program.cs to align with new WebSocket handling.
    • Adjusted appsettings.json configuration for real-time model settings.

Changes walkthrough 📝

Relevant files
Enhancement
7 files
WaveStreamChannel.cs
Adjusted buffer duration and WaveOut initialization           
+6/-3     
SessionConversationUpdate.cs
Added `SessionConversationUpdate` model for real-time responses
+11/-0   
RealTimeCompletionProvider.cs
Refactored to use `RealtimeChatSession` for WebSocket handling
+19/-66 
AiWebsocketPipelineResponse.cs
Implemented WebSocket response handling with
AiWebsocketPipelineResponse
+116/-0 
AsyncWebsocketDataCollectionResult.cs
Added async WebSocket data collection result handling       
+38/-0   
AsyncWebsocketDataResultEnumerator.cs
Implemented enumerator for WebSocket data results               
+52/-0   
RealtimeChatSession.cs
Added `RealtimeChatSession` for managing WebSocket sessions
+109/-0 
Tests
1 files
Program.cs
Updated test program to align with new WebSocket handling
+2/-0     
Configuration changes
1 files
appsettings.json
Adjusted real-time model configuration settings                   
+1/-3     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @GGHansome
    Copy link

    Auto Review Result:

    Code Review Summary

    Change Overview: The changes introduce a new real-time chat session management feature using websockets for the BotSharp application, replacing the old ClientWebSocket solution with an enhanced session management approach and adding several helper classes supporting WebSocket operations.

    Identified Issues

    Issue 1: Unhandled Exceptions

    • Description: In methods like HandleReceivedResult, there is a lack of proper handling for exceptions, such as the NotImplementedException for non-text messages.
    • Suggestion: Implement more robust error handling to manage unexpected data formats gracefully.
    • Example:
      try {
          // Current logic
      } catch (NotImplementedException ex) {
          _logger.LogError($"Unsupported message received: {ex.Message}");
          // Handle gracefully or terminate the session
      }

    Issue 2: Lack of Logging

    • Description: Many critical actions (e.g., message processing, connection establishment) do not have sufficient logging, which can hinder debugging and monitoring.
    • Suggestion: Introduce logging statements at each significant step, particularly when connections are established or closed and when data is processed.
    • Example:
      _logger.LogInformation("WebSocket connection established.");

    Issue 3: Resource Management

    • Description: Objects like MemoryStream and _webSocket need proper disposal to avoid resource leaks.
    • Suggestion: Ensure all disposable resources are properly disposed using using statements or explicitly call .Dispose().

    Issue 4: Thread Safety

    • Description: Concurrent operations on resources like _receivedCollectionResult and _webSocket could lead to race conditions.
    • Suggestion: Utilize proper synchronization mechanisms such as lock statements judiciously or consider using concurrent collections.

    Overall Assessment

    The implementation effectively introduces real-time chat functionality with comprehensive support for asynchronous operations. However, enhancements in error handling, logging, and resource management are necessary to ensure reliability and maintainability. The code can be significantly improved by addressing the concurrency and resource management concerns as well as increasing the verbosity of the log output for better traceability.

    Copy link

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Duplicate Code

    The onModelAudioDeltaReceived method is called twice with the same parameters in the response.audio.delta handler - once in the if block and again in the else block.

    {
        _logger.LogDebug($"{response.Type}: {receivedText}");
        onModelAudioDeltaReceived(audio.Delta, audio.ItemId);
    }
    else
    {
        _logger.LogDebug($"{response.Type}: {receivedText}");
        onModelAudioDeltaReceived(audio.Delta, audio.ItemId);
    }
    Memory Leak

    The buffer rented from ArrayPool is never returned to the pool, which could lead to memory leaks. The buffer should be returned in the DisposeAsync method.

        _buffer = ArrayPool<byte>.Shared.Rent(1024 * 32);
    }
    
    public ClientResult Current { get; private set; }
    
    public ValueTask DisposeAsync()
    {
        _webSocket?.Dispose();
        return new ValueTask(Task.CompletedTask);
    }

    Copy link

    qodo-merge-pro bot commented Apr 14, 2025

    Qodo Merge was enabled for this repository. To continue using it, please link your Git account with your Qodo account here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix potential null reference

    The else block is redundant and contains the same code as the if block, but it's
    trying to use audio.Delta which could be null since the if-condition already
    checks for non-null delta. This could cause a NullReferenceException.

    src/Plugins/BotSharp.Plugin.OpenAI/Providers/Realtime/RealTimeCompletionProvider.cs [181-185]

     else
     {
         _logger.LogDebug($"{response.Type}: {receivedText}");
    -    onModelAudioDeltaReceived(audio.Delta, audio.ItemId);
    +    // Don't call onModelAudioDeltaReceived with null delta
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    __

    Why: The suggestion correctly identifies a critical issue where the else block attempts to use audio.Delta which would be null (since the if condition checks for non-null delta). This could cause a NullReferenceException at runtime, potentially crashing the application.

    High
    • Update

    @iceljc iceljc requested a review from Oceania2018 April 14, 2025 15:16
    @GGHansome
    Copy link

    Auto Review Result:

    Code Review Summary

    Change Overview: The provided code changes replace the usage of ClientWebSocket with a new RealtimeChatSession structure for handling WebSocket connections. These changes also introduce new classes and methods to remodel the handling of WebSocket-based real-time communication, thereby promoting extensibility and improving the separation of concerns.

    Identified Issues

    Issue 1: [Code Consistency/ clarity]

    • Description: The use of Nullable Reference Types (! and ?) is inconsistent across the codebase. Nullable context is not always enforced consistently.
    • Suggestion: Ensure consistent use of nullable reference types by enabling nullable context and reviewing each variable for proper nullability handling. You might consider using [MaybeNull] or [NotNull] annotations where appropriate.
    • Example:
      // Before
      public Func<string, string> OnModelMessageReceived { get; set; } = null!;
      // After
      public Func<string, string> OnModelMessageReceived { get; set; } = null!;  // Ensure non-null before usage

    Issue 2: [Performance]

    • Description: The WebSocket buffer was resized from a specific length to a duration-based buffer. This change could result in excessive memory allocation if not monitored.
    • Suggestion: Profile memory usage with this change and consider implementing buffer segment management or advisory warnings for buffer overflow situations.
    • Example:
      // Consider adding monitoring or limiting extremities of buffer duration
      _bufferedWaveProvider.BufferDuration = TimeSpan.FromMinutes(10);

    Issue 3: [Usability]

    • Description: The addition of the OnUserSpeechDetected handler is conceptually clear but could benefit from in-code documentation or comments for future maintainers.
    • Suggestion: Add comments explaining the role and expected behavior of each new delegate.
    • Example:
      // Before
      public Func<string> OnUserSpeechDetected { get; set; } = () => string.Empty;
      // Add a comment explaining its trigger and influence

    Overall Evaluation

    This code update improves the architectural pattern for handling real-time WebSocket interaction by decoupling specific WebSocket logic into more manageable components. Some adjustments, like using a duration-based buffer, should be carefully reviewed for performance implications. More consistent use of nullable types and additional commenting would enhance maintainability.

    @GGHansome
    Copy link

    Auto Review Result:

    Code Review Summary

    Change Overview: The code changes aim to improve the system's ability to handle real-time audio and text streaming, particularly by enhancing the WebSocket session handling, integrating new response event functionality, and refactoring existing code for improved maintainability and performance.

    Issues Identified

    Issue 1: [Code Consistency]

    • Description: The logging statements in the AttachEvents method of RealTimeCompletionProvider.cs were inconsistent in terms of punctuation.
    • Suggestion: Ensure all log messages end with punctuation for consistency.
    • Example:
      // Before
      _logger.LogInformation("Google Realtime Client disconnected");
      // After
      _logger.LogInformation("Google Realtime Client disconnected.");
      

    Issue 2: [Buffer Management]

    • Description: The buffer length was set directly instead of buffer duration which can lead to inefficiencies.
    • Suggestion: Transition to using BufferDuration for better control and possibly improved performance.
    • Example:
      // Before
      _bufferedWaveProvider.BufferLength = 1024 * 1024;
      // After
      _bufferedWaveProvider.BufferDuration = TimeSpan.FromMinutes(10);
      

    Issue 3: [Null Reference Check]

    • Description: There are null reference risks, such as when accessing _session without checking if it is null before operations in SendEventToModel.
    • Suggestion: Implement null checks or use null-conditional operators to improve stability.
    • Example:
      if (_session == null) return;
      

    Issue 4: [Code Readability]

    • Description: Complex procedures like ReceiveMessage in RealTimeCompletionProvider.cs could become hard to maintain over time.
    • Suggestion: Break down into smaller methods or improve documentation and comments within the method for clarity.

    Overall Evaluation

    The code changes reflect a move towards more robust and maintainable real-time processing. However, there are areas for improvement in logging consistency, buffer management, and the handling of potentially null objects. Breaking down complex methods into smaller functions and ensuring all logging is consistent and informative can further enhance code quality and maintainability.

    @GGHansome
    Copy link

    Auto Review Result:

    Code Review Summary

    Change Overview: The code changes introduce a new functionality to detect user speech and refactor existing code for enhanced session management in a real-time web socket communication context. Furthermore, it provides improved logging and modifies the audio buffer management.

    Identified Issues

    Issue 1: Missing Null Check for WebSocket or Session

    • Description: In the RealtimeChatSession and RealTimeCompletionProvider classes, there are no null checks before operating on the _webSocket or _session objects. This could lead to a NullReferenceException if these objects are not properly initialized.
    • Suggestion: Implement null checks and handle cases where _webSocket or _session might be null.
    • Example:
      // Before modification
      if (_webSocket.State == WebSocketState.Open) { /*...*/ }
      
      // After modification
      if (_webSocket != null && _webSocket.State == WebSocketState.Open) { /*...*/ }

    Issue 2: Exception Handling on Dispose Operations

    • Description: In the Dispose methods, _webSocket.Dispose and ContentStream?.Dispose() are called without any exception handling. If these resources are already disposed or encounter an issue during disposal, it could lead to runtime exceptions.
    • Suggestion: Wrap dispose calls in a try-catch block to handle potential exceptions gracefully.
    • Example:
      public void Dispose()
      {
          try { _webSocket?.Dispose(); } catch (Exception ex) { /* Log error */ }
      }

    Issue 3: Logging Consistency and Level

    • Description: The logging statements have been improved but should ensure they are consistent in terms of detail level and verbosity across various events.
    • Suggestion: Review log levels and ensure consistency, especially between information and debug statements for similar types of activities.
    • Example:
      _logger.LogInformation("Google Realtime Client connected.");
      _logger.LogDebug("User message received.");
      // Consider whether both should be info or debug based on required verbosity.

    Issue 4: Legacy Code Integration

    • Description: There is evidence of mixing legacy and new implementations, such as using both ClientWebSocket and the new RealtimeChatSession. This dual approach may introduce maintenance challenges.
    • Suggestion: Consider completely migrating to the new session handling where feasible, to reduce complexity and potential inconsistencies.

    Overall Evaluation

    The updated code introduces valuable new functionality but needs improvements in exception handling, logging consistency, and ensuring complete migration to new implementations for simplicity and maintainability. Ensuring robustness through proper checks and streamlined operations will enhance maintainability and reduce runtime errors.

    @GGHansome
    Copy link

    Auto Review Result:

    Summary of Code Changes

    Purpose and Impact: The changes introduce several new features and optimizations in handling real-time communication and session management within the BotSharp system, including refactoring of WebSocket handling, adding logging for various system events, and improving the session handling logic.

    Identified Issues

    Issue 1: [Code Readability]

    • Description: The variable _session?.Dispose(); is used without a preceding null check, increasing the risk of a null reference exception.
    • Suggestion: Add a null check before disposing the object.
    • Example:
      if (_session != null) 
      {
          _session.Dispose();
      }

    Issue 2: [Resource Management]

    • Description: The AiWebsocketPipelineResponse.ContentStream property does not correctly handle the potential of setting the stream, which could lead to unexpected behaviors.
    • Suggestion: Implement the set method or throw an InvalidOperationException to prevent misuse.
    • Example:
      set => throw new InvalidOperationException("Setting the content stream is not allowed.");

    Issue 3: [Code Duplication]

    • Description: The handling for logging events and error cases is duplicated across methods which can lead to maintenance issues.
    • Suggestion: Extract duplicated logic into separate helper methods to reduce code repetition.
    • Example:
      private void LogEvent(string message)
      {
          _logger.LogInformation(message);
      }

    Issue 4: [Inconsistent Input Validation]

    • Description: Methods like CalculateAudioLevel assume inputs are well-formed but do not validate input buffers for null or out-of-range data.
    • Suggestion: Implement input validation and make the method safer from unexpected input.
    • Example:
      if (buffer == null || bytesRecorded <= 0)
      {
          throw new ArgumentException("Invalid buffer or bytesRecorded value.");
      }

    Overall Evaluation

    The code introduces significant changes and improvements but suffers from lack of proper input handling and duplicated code blocks which can become maintenance overheads. Adding robust error-handling measures, consistent null-check practices, and deduplication of repeated logic can enhance the maintainability and robustness of the code.

    @GGHansome
    Copy link

    Auto Review Result:

    Code Review Summary

    Change Overview: The code changes aim to refactor the server transport configuration system by splitting the configuration settings into two distinct classes for SSE and Stdio transports. Additionally, improvements are made to the real-time communication features, particularly in handling websockets and real-time events, as well as refactoring and optimization of certain service configurations and message handling.

    Issues Found

    Issue 1: Code Clarity and Maintainability

    • Description: The refactoring of transport type configurations into separate classes (McpSseServerConfig and McpStdioServerConfig) improves code organization but lacks clear documentation on how these classes should be used in the broader system.

    • Suggestion: Add XML comments or documentation indicating how these configuration classes should be initialized and used within the system.

      // Before: Combined configuration logic
      // After: Separate classes with unclear integration instructions
      

    Issue 2: Error Handling

    • Description: The function RegisterFunctionCall throws ArgumentNullException when both config types are null, which is misleading because ArgumentNullException is intended for missing arguments in method calls.

    • Suggestion: Consider using a custom exception type or InvalidOperationException for invalid configuration states.

      throw new ArgumentNullException("Invalid MCP server configuration!");
      

      Improvement:

      throw new InvalidOperationException("Both SseConfig and StdioConfig cannot be null.");
      

    Issue 3: Bug in Conditional Logic

    • Description: The Append method in ConversationStorage potentially causes recursion, as it calls itself incorrectly.

    • Suggestion: Ensure the method calls are appropriately guarded or modified to prevent stack overflow due to recursive calls.

      // Before
      Append(conversationId, [dialog]);
      // Suspected error — review call to self with an array
      

    Issue 4: WebSocket Usage

    • Description: The transition from using a WebSocket directly to using a RealtimeChatSession is not fully explained in the context of class responsibilities and usage patterns.
    • Suggestion: Provide a class-level comment or README update to clarify why this abstraction is beneficial.

    Overall Assessment

    The refactoring efforts present improvements in modularity and testability of server configurations and real-time communication. However, additional documentation is required to ensure that future developers can easily understand and extend these systems. Error handling needs adjustments to prevent confusion, and method logic should be carefully reviewed to prevent recursion and unintended behaviors.

    @Oceania2018
    Copy link
    Member

    Please fix the compile issue.

    @GGHansome
    Copy link

    Auto Review Result:

    Code Review Summary

    Summary of Changes: The code changes are aimed at refactoring the BotSharp infrastructure to improve clarity, maintainability, and feature expansion. Notable changes include the introduction of new configuration models for server settings, replacing WebSocket management for real-time message handling with a more robust session management mechanism, and removing unused or outdated pieces of code.

    Issues Identified

    Issue 1: Code Style & Clarity

    • Description: The addition of new interface properties and classes, such as Provider in IFunctionCallback and new configuration models like McpSseServerConfig and McpStdioServerConfig, improve the modularization of the code. However, some initialization patterns could benefit from clearer formatting, especially in constructor usage and property initialization.
    • Suggestion: Be consistent in initialization and ensure that default values are explicitly documented where applicable.
    • Example:
      public class McpStdioServerConfig
      {
          public string Command { get; set; } = null!;
          // Consider providing more context
      }

    Issue 2: Potential Nullability Concerns

    • Description: Usage of nullable types and unchecked constructor parameters could lead to runtime exceptions. For instance, GetMcpClientAsync uses configurations that might be null without explicit checks.
    • Suggestion: Implement null-checks or utilize nullability annotations to prevent potential issues.
    • Example:
      var config = _mcpSettings.McpServerConfigs.FirstOrDefault(x => x.Id == serverId);
      if (config == null) throw new ArgumentNullException("Server config not found");

    Issue 3: Lack of Documentation or Comments

    • Description: New additions like the session management classes lack comments that explain their purpose and usage in detail.
    • Suggestion: Add comments to elaborate on the logic and purpose behind critical sections of the code, especially for async and real-time data handling.

    Overall Evaluation

    The refactored code significantly enhances the modular design of the BotSharp framework by segregating configurations and enhancing real-time processing capabilities. However, emphasis on comprehensive documentation and addressing potential nullability issues would further bolster code reliability and readability. The existing and new functionalities are well-integrated, but consideration should be given to ensure the robustness and fault tolerance of added features.

    @GGHansome
    Copy link

    Auto Review Result:

    Code Review Summary

    Change Summary: This code update primarily involves refining the server configuration and communication strategies within the BotSharp framework. The changes include modifying the data structures for server configurations, restructuring server options, refining message handling for better flexibility, and optimizing real-time communication mechanisms.

    Issues Identified

    Issue 1: [Code Maintainability]

    • Description: The McpServerConfigModel class has been fragmented into specific transport configurations (McpSseServerConfig and McpStdioServerConfig), improving clarity on server options.
    • Suggestion: Ensure comprehensive comments are included in these classes, explaining the purpose and expected content for each configuration parameter.
    • Example:
      // Before
      public string TransportType { get; set; } = null!;
      
      // After
      // McpSseServerConfig and McpStdioServerConfig are distinct classes with responsibilities clearly defined.

    Issue 2: [Code Clarity]

    • Description: The introduction of real-time session management for OpenAI communication enhances communication stability. However, components like RealtimeChatSession and AsyncWebsocketDataCollectionResult lack extensive documentation.
    • Recommendation: Provide detailed comments on class responsibilities and the flow of data through these real-time communication classes.
    • Example:
      // Add descriptive comments explaining the role of the buffer's size and functionality.
      byte[] _buffer = ArrayPool<byte>.Shared.Rent(1024 * 32);

    Issue 3: [Possible Logic Bug]

    • Description: In RealtimeChatSession, the web socket connection is re-established without disposing properly if _webSocket is already initialized.
    • Suggestion: Ensure the previous connection is disposed of before establishing a new one to avoid resource leakage.
    • Example:
      _webSocket?.Dispose();
      This line is correct but ensure _webSocket instances from previous tasks are correctly terminated.

    Overall Assessment

    The changes bring significant enhancements to the BotSharp infrastructure, focusing on flexibility and real-time communication advances. However, ensuring comprehensive documentation and addressing potential resource management issues will improve code maintainability and reliability.

    @@ -13,6 +13,7 @@ public class RealtimeHubConnection
    public Func<string, string> OnModelMessageReceived { get; set; } = null!;
    public Func<string> OnModelAudioResponseDone { get; set; } = null!;
    public Func<string> OnModelUserInterrupted { get; set; } = null!;
    public Func<string> OnUserSpeechDetected { get; set; } = () => string.Empty;
    Copy link
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    What's the difference with OnModelUserInterrupted?

    @@ -150,6 +150,9 @@ await _completer.Connect(_conn,
    var data = _conn.OnModelUserInterrupted();
    await (responseToUser?.Invoke(data) ?? Task.CompletedTask);
    }

    Copy link
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    image

    @Oceania2018 Oceania2018 merged commit 7ea6a62 into SciSharp:master Apr 16, 2025
    4 checks passed
    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.

    3 participants