Skip to content
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

Implement Channel Callback Interface #263

Merged

Conversation

davidvonthenen
Copy link
Contributor

@davidvonthenen davidvonthenen commented Aug 22, 2024

Proposed changes

Addresses: #215

This implements a mechanism for using Go channels to get callbacks to events. This has been requested numerous times (example). This was deferred work since the project began, but there have been enough requests to make this a reality. This feature is required for upcoming STS work, so we need to get it in early to shake out any bugs.

Advantages of using the channel method:

  • you can have N number of channels subscribed to messages
  • you can manage the subscription of messages on your own (dynamically add and remove subscribers as needed)

This preserves the existing callback using the functions method.

This implements two examples:

  • examples/speech-to-text/http_channel
  • examples/speech-to-text/microphone_channel

Types of changes

What types of changes does your code introduce to the community Go SDK?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update or tests (if none of the other choices apply)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • I have lint'ed all of my code using repo standards
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

NA

Summary by CodeRabbit

  • New Features

    • Introduced new WebSocket client implementation for real-time audio transcription.
    • Added various functions for managing WebSocket connections, enhancing flexibility and control.
    • Created comprehensive documentation for new features and examples.
  • Bug Fixes

    • Improved error handling and messaging for user callbacks and channels.
  • Refactor

    • Renamed and consolidated various client functions to improve clarity and usability.
    • Enhanced callback and channel handling logic in the API.
  • Documentation

    • Added README files and enhanced existing documentation for the WebSocket functionalities.

Copy link

coderabbitai bot commented Aug 22, 2024

Walkthrough

The changes encompass modifications to configuration files, client implementations, and various handler structures across the Deepgram SDK. Key adjustments include the introduction of new WebSocket and REST client functionalities, restructuring of callback and channel handling mechanisms, and updates to error messaging constants. These alterations collectively enhance the SDK's ability to manage real-time audio processing and transcription while improving code organization and maintainability.

Changes

Files Change Summary
.golangci.yaml Updated linter paths and added new paths for websocket files; reinstated linter configuration for a specific file.
examples/speech-to-text/websocket/... Modified Deepgram client instantiation method in main.go files; newly introduced simple streaming application in http_channel/main.go; adjustments in microphone handling for WebSocket connections.
pkg/api/listen/v1/websocket/... Enhanced DefaultCallbackHandler with new debug fields; renamed MessageRouter to CallbackRouter with updated methods; introduced new DefaultChanHandler and ChanRouter for WebSocket events.
pkg/client/analyze/v1/... Changed client instantiation from general to REST-specific client; modified client structure to utilize REST methods.
pkg/client/common/v1/... Added constants for socket operations and error handling; introduced WebSocketHandler interface for managing WebSocket connections; restructured Client type to RESTClient for clarity.
pkg/client/listen/... Deprecated older client aliases and introduced new functions with clearer naming conventions for WebSocket and REST clients.
pkg/client/speak/v1/... Updated client instantiation logic to transition to REST-specific methods.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant WSClient
    participant DeepgramAPI

    User->>+WSClient: Connect()
    WSClient->>DeepgramAPI: Establish WebSocket Connection
    DeepgramAPI-->>-WSClient: Connection Established
    WSClient->>User: Connection Successful
    User->>WSClient: Send Audio Data
    WSClient->>DeepgramAPI: Transmit Audio
    DeepgramAPI-->>WSClient: Transcription Results
    WSClient->>User: Display Transcription
    User->>WSClient: Disconnect()
    WSClient->>DeepgramAPI: Close Connection
    DeepgramAPI-->>WSClient: Connection Closed
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Outside diff range, codebase verification and nitpick comments (7)
examples/speech-to-text/websocket/microphone_channel/README.md (2)

3-3: Consider rephrasing for conciseness.

The phrase "in order to" can be shortened to "to" for conciseness.

Apply this diff to improve the sentence:

-This example uses the Microphone as input in order to detect conversation insights in what is being said.
+This example uses the Microphone as input to detect conversation insights in what is being said.
Tools
LanguageTool

[style] ~3-~3: Consider a shorter alternative to avoid wordiness.
Context: ...is example uses the Microphone as input in order to detect conversation insights in what is...

(IN_ORDER_TO_PREMIUM)


17-17: Consider rephrasing to avoid repetition.

The phrase "makes use of" is repeated. Consider rephrasing to avoid wordiness.

Apply this diff to improve the sentence:

-The Live API (Real-Time) example makes use of a [microphone package](https://github.com/deepgram/deepgram-go-sdk/tree/main/pkg/audio/microphone) contained within the repository. That package makes use of the [PortAudio library](http://www.portaudio.com/) which is a cross-platform open source audio library.
+The Live API (Real-Time) example uses a [microphone package](https://github.com/deepgram/deepgram-go-sdk/tree/main/pkg/audio/microphone) contained within the repository. This package utilizes the [PortAudio library](http://www.portaudio.com/), a cross-platform open-source audio library.
Tools
LanguageTool

[style] ~17-~17: You have already used this phrasing in nearby sentences. Shortening it will avoid wordiness.
Context: ...ned within the repository. That package makes use of the [PortAudio library](http://www.port...

(REP_MAKE_USE_OF)


[uncategorized] ~17-~17: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...rtaudio.com/) which is a cross-platform open source audio library. If you are on Linux, you...

(EN_COMPOUND_ADJECTIVE_INTERNAL)

pkg/client/speak/v1/rest/client.go (1)

Line range hint 66-68: Reminder: Address the TODO comment.

The TODO comment suggests detecting if the source is JSON and wrapping the text in a JSON object if not. Consider implementing this feature for improved functionality.

Would you like me to help implement this feature or open a GitHub issue to track this task?

pkg/client/common/v1/websocket.go (4)

213-344: Improve error categorization in listen.

Consider categorizing errors more explicitly to enhance error handling and logging.


425-462: Add comments to explain the logic in closeStream.

Consider adding comments to explain the logic behind sending close messages and handling errors.


464-503: Simplify the switch statement in normalClosure.

Consider simplifying the switch statement to improve readability.


515-546: Add comments to explain the logic in closeWs.

Consider adding comments to explain the logic behind closing the WebSocket connection and handling cleanup.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4de178e and 7ef18d7.

Files selected for processing (36)
  • .golangci.yaml (2 hunks)
  • examples/speech-to-text/websocket/http_callback/main.go (2 hunks)
  • examples/speech-to-text/websocket/http_channel/main.go (1 hunks)
  • examples/speech-to-text/websocket/microphone_callback/main.go (1 hunks)
  • examples/speech-to-text/websocket/microphone_channel/README.md (1 hunks)
  • examples/speech-to-text/websocket/microphone_channel/main.go (1 hunks)
  • pkg/api/listen/v1/websocket/callback_default.go (9 hunks)
  • pkg/api/listen/v1/websocket/callback_router.go (10 hunks)
  • pkg/api/listen/v1/websocket/chan_default.go (1 hunks)
  • pkg/api/listen/v1/websocket/chan_router.go (1 hunks)
  • pkg/api/listen/v1/websocket/constants.go (1 hunks)
  • pkg/api/listen/v1/websocket/interfaces/constants.go (1 hunks)
  • pkg/api/listen/v1/websocket/interfaces/interfaces.go (1 hunks)
  • pkg/api/listen/v1/websocket/types.go (1 hunks)
  • pkg/api/manage/v1/manage.go (1 hunks)
  • pkg/client/analyze/v1/client.go (1 hunks)
  • pkg/client/analyze/v1/types.go (1 hunks)
  • pkg/client/common/v1/constants.go (1 hunks)
  • pkg/client/common/v1/interfaces/interfaces.go (1 hunks)
  • pkg/client/common/v1/rest.go (3 hunks)
  • pkg/client/common/v1/types.go (1 hunks)
  • pkg/client/common/v1/websocket.go (1 hunks)
  • pkg/client/listen/client.go (7 hunks)
  • pkg/client/listen/v1/rest/client.go (1 hunks)
  • pkg/client/listen/v1/rest/types.go (1 hunks)
  • pkg/client/listen/v1/websocket/client_callback.go (1 hunks)
  • pkg/client/listen/v1/websocket/client_channel.go (1 hunks)
  • pkg/client/listen/v1/websocket/constants.go (3 hunks)
  • pkg/client/listen/v1/websocket/new_using_callbacks.go (1 hunks)
  • pkg/client/listen/v1/websocket/new_using_chan.go (1 hunks)
  • pkg/client/listen/v1/websocket/types.go (1 hunks)
  • pkg/client/live/legacy.go (2 hunks)
  • pkg/client/manage/v1/client.go (1 hunks)
  • pkg/client/manage/v1/types.go (1 hunks)
  • pkg/client/speak/v1/rest/client.go (1 hunks)
  • pkg/client/speak/v1/rest/types.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • pkg/client/listen/v1/websocket/constants.go
Additional context used
Learnings (1)
examples/speech-to-text/websocket/http_channel/main.go (2)
Learnt from: dvonthenen
PR: deepgram/deepgram-go-sdk#258
File: examples/text-to-speech/websocket/interactive/main.go:35-43
Timestamp: 2024-08-22T03:05:19.486Z
Learning: dvonthenen prefers to exclude example code from automated checks in the Deepgram Go SDK repository.
Learnt from: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: examples/speak/stream/http/main.go:45-65
Timestamp: 2024-06-19T14:40:12.485Z
Learning: The code in `examples/speak/stream/http/main.go` is intended as a straightforward example to aid in understanding and driving the implementation, as per user dvonthenen's clarification.
LanguageTool
examples/speech-to-text/websocket/microphone_channel/README.md

[style] ~3-~3: Consider a shorter alternative to avoid wordiness.
Context: ...is example uses the Microphone as input in order to detect conversation insights in what is...

(IN_ORDER_TO_PREMIUM)


[style] ~17-~17: You have already used this phrasing in nearby sentences. Shortening it will avoid wordiness.
Context: ...ned within the repository. That package makes use of the [PortAudio library](http://www.port...

(REP_MAKE_USE_OF)


[uncategorized] ~17-~17: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...rtaudio.com/) which is a cross-platform open source audio library. If you are on Linux, you...

(EN_COMPOUND_ADJECTIVE_INTERNAL)

Additional comments not posted (140)
pkg/client/speak/v1/rest/types.go (1)

13-13: Transition to RESTClient in Client struct.

The Client struct now embeds *common.RESTClient, indicating a shift towards RESTful communication. Ensure that all methods and functionalities relying on the previous *common.Client structure are compatible with the new *common.RESTClient.

Run the following script to verify the usage of Client struct methods:

pkg/client/analyze/v1/types.go (1)

13-13: Transition to RESTClient in Client struct.

The Client struct now embeds *common.RESTClient, indicating a shift towards RESTful communication. Ensure that all methods and functionalities relying on the previous *common.Client structure are compatible with the new *common.RESTClient.

Run the following script to verify the usage of Client struct methods:

Verification successful

Transition to RESTClient in Client struct verified.

The methods associated with the Client struct, such as DoFile, DoStream, DoText, DoURL, and APIRequest, align with RESTful operations, indicating compatibility with the new *common.RESTClient. No issues found with the transition.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all method calls on `Client` struct instances.

# Test: Search for method calls on `Client` instances. Expect: Compatibility with `RESTClient`.
rg --type go -A 5 $'Client'

Length of output: 148039

pkg/client/listen/v1/rest/types.go (1)

13-13: Transition to RESTClient in Client struct.

The Client struct now embeds *common.RESTClient, indicating a shift towards RESTful communication. Ensure that all methods and functionalities relying on the previous *common.Client structure are compatible with the new *common.RESTClient.

Run the following script to verify the usage of Client struct methods:

Verification successful

Transition to RESTClient in Client struct verified.

The transition to embedding *common.RESTClient in the Client struct appears to be compatible with existing method calls. Methods like DoFile, DoStream, and DoURL are using RESTClient methods internally, indicating that the change has been properly integrated. No further issues were found in the method usage.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all method calls on `Client` struct instances.

# Test: Search for method calls on `Client` instances. Expect: Compatibility with `RESTClient`.
rg --type go -A 5 $'Client'

Length of output: 148039

pkg/client/manage/v1/types.go (1)

14-14: Verify compatibility with common.RESTClient.

The Client struct now embeds a pointer to common.RESTClient. Ensure that RESTClient provides all the necessary methods and properties previously available in common.Client.

Run the following script to verify the methods and properties of common.RESTClient:

pkg/api/listen/v1/websocket/constants.go (2)

17-18: Improved error message clarity.

The error message for ErrUserCallbackNotDefined now clearly indicates that the callback may be undefined or invalid.


20-21: New error constant for user channel issues.

The addition of ErrUserChanNotDefined improves error handling by specifically addressing user channel issues.

pkg/api/listen/v1/websocket/interfaces/constants.go (2)

8-9: Enhanced type safety with TypeResponse.

The introduction of TypeResponse improves type safety and clarity for message type constants.


12-22: Consistent use of TypeResponse for message types.

The use of TypeResponse for all message type constants ensures consistency and enhances code readability.

pkg/api/manage/v1/manage.go (1)

31-32: Verify the RESTClient initialization logic.

The RESTClient is now directly wrapping the client parameter. Ensure this change aligns with the intended design and does not introduce any unintended side effects.

Run the following script to verify the usage and interactions of RESTClient in the codebase:

Verification successful

RESTClient Initialization Logic Verified

The initialization of RESTClient by directly wrapping the client parameter is consistent with the intended design and does not introduce any unintended side effects. The usage across the codebase aligns with this pattern.

  • pkg/api/manage/v1/manage.go: Direct initialization with client.
  • Consistent usage in files like pkg/client/speak/v1/rest/client.go, pkg/client/manage/v1/client.go, etc.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage and interactions of `RESTClient` in the codebase.

# Test: Search for the usage of `RESTClient`. Expect: Consistent usage with the new initialization logic.
rg --type go -A 5 $'RESTClient'

Length of output: 7683

pkg/client/common/v1/interfaces/interfaces.go (1)

8-26: Verify the implementation of WebSocketHandler interface.

Ensure that the WebSocketHandler interface is correctly implemented in the relevant parts of the codebase.

Run the following script to verify the implementation of the WebSocketHandler interface:

Verification successful

WebSocketHandler Interface Implementation Verified

The WebSocketHandler interface is correctly implemented by the WSChannel and WSCallback structs in the codebase. All required methods are present and properly defined.

  • WSChannel and WSCallback implement all methods of the WebSocketHandler interface:
    • ProcessMessage
    • ProcessError
    • Start
    • Finish
    • GetCloseMsg
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `WebSocketHandler` interface.

# Test: Search for the implementation of `WebSocketHandler`. Expect: Correct implementation in relevant parts of the codebase.
ast-grep --lang go --pattern $'type $_ struct { $$$ }\n\nfunc ($_ *$_) GetURL($_, $_) ($_ , $_) {\n  $$$\n}\n\nfunc ($_ *$_) ProcessMessage($_, $_) $_ {\n  $$$\n}\n\nfunc ($_ *$_) ProcessError($_) $_ {\n  $$$\n}\n\nfunc ($_ *$_) Start() {\n  $$$\n}\n\nfunc ($_ *$_) Finish() {\n  $$$\n}\n\nfunc ($_ *$_) GetCloseMsg() $_ {\n  $$$\n}'

Length of output: 1010


Script:

#!/bin/bash
# Description: Verify the implementation of `WebSocketHandler` interface by searching for each method separately.

# Search for structs implementing the GetURL method
ast-grep --lang go --pattern $'func ($_ *$_) GetURL($_, $_) ($_ , $_) { $$$ }'

# Search for structs implementing the ProcessMessage method
ast-grep --lang go --pattern $'func ($_ *$_) ProcessMessage($_, $_) $_ { $$$ }'

# Search for structs implementing the ProcessError method
ast-grep --lang go --pattern $'func ($_ *$_) ProcessError($_) $_ { $$$ }'

# Search for structs implementing the Start method
ast-grep --lang go --pattern $'func ($_ *$_) Start() { $$$ }'

# Search for structs implementing the Finish method
ast-grep --lang go --pattern $'func ($_ *$_) Finish() { $$$ }'

# Search for structs implementing the GetCloseMsg method
ast-grep --lang go --pattern $'func ($_ *$_) GetCloseMsg() $_ { $$$ }'

Length of output: 8626

pkg/client/common/v1/types.go (2)

44-46: Verify the utilization of RESTClient structure.

Ensure that the RESTClient structure is correctly utilized in the codebase, particularly in handling REST functionalities.

Run the following script to verify the utilization of the RESTClient structure:

Verification successful

RESTClient structure is correctly utilized for REST functionalities.

The RESTClient structure is used across various parts of the codebase, including speak, manage, analyze, and listen clients. It provides methods like SetupRequest and HandleResponse, which are utilized to manage REST operations effectively. The usage patterns confirm its role in handling REST functionalities.

  • pkg/client/common/v1/rest.go: Defines methods like SetupRequest and HandleResponse.
  • pkg/client/manage/v1/client.go, pkg/client/analyze/v1/client.go, pkg/client/listen/v1/rest/client.go: Utilize RESTClient methods for REST operations.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the utilization of `RESTClient` structure.

# Test: Search for the usage of `RESTClient`. Expect: Correct utilization in handling REST functionalities.
rg --type go -A 5 $'RESTClient'

Length of output: 7683


25-39: Verify the utilization of WSClient structure.

Ensure that the WSClient structure is correctly utilized in the codebase, particularly in handling WebSocket connections.

Run the following script to verify the utilization of the WSClient structure:

Verification successful

WSClient Structure Utilization Verified

The WSClient structure is correctly utilized across the codebase for handling WebSocket connections. It is instantiated and its methods are invoked in various files, ensuring comprehensive management of WebSocket operations such as connection, reconnection, and data transmission.

  • pkg/client/listen/v1/websocket/types.go
  • pkg/client/listen/v1/websocket/new_using_chan.go
  • pkg/client/listen/v1/websocket/client_channel.go
  • pkg/client/listen/v1/websocket/new_using_callbacks.go
  • pkg/client/listen/v1/websocket/client_callback.go
  • pkg/client/common/v1/websocket.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the utilization of `WSClient` structure.

# Test: Search for the usage of `WSClient`. Expect: Correct utilization in handling WebSocket connections.
rg --type go -A 5 $'WSClient'

Length of output: 10997

pkg/client/common/v1/constants.go (1)

1-44: Well-structured constants and error definitions.

The file provides a centralized location for managing constants and error messages, which enhances maintainability and readability. The definitions are clear and follow best practices.

pkg/api/listen/v1/websocket/interfaces/interfaces.go (1)

8-31: Well-defined interfaces for WebSocket interactions.

The new interfaces provide a structured way to handle WebSocket events and message processing, enhancing modularity and flexibility. The definitions are clear and follow best practices.

pkg/client/listen/v1/websocket/types.go (3)

22-24: Deprecation notice is clear and aliasing is correctly implemented.

The Client type is correctly aliased to WSCallback with a clear deprecation notice.


Line range hint 43-57: Struct modifications for WSChannel align with channel-based design.

The struct modifications are consistent with the channel-oriented approach for handling messages.

Ensure that the chans field is properly initialized and used in the implementation.

Run the following script to verify the usage of chans field:

Verification successful

Initialization and Usage of chans Field in WSChannel is Properly Handled

The chans field in the WSChannel struct is correctly initialized and used across various functions for creating instances of WSChannel. It is initialized as an empty slice, ensuring that it is ready for further operations. No issues were found regarding its usage.

  • The chans field is initialized in the NewUsingChanWithCancel function.
  • It is consistently passed as an argument in several constructor functions.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the initialization and usage of `chans` field in `WSChannel`.

# Test: Search for the usage of `chans` field. Expect: Proper initialization and usage.
rg --type go -A 5 'WSChannel' | rg 'chans'

Length of output: 3264


26-41: Struct definition for WSCallback is well-defined.

The struct is well-structured with fields for context management and callbacks.

Ensure that the context and callback fields are properly initialized and used in the implementation.

Run the following script to verify the usage of ctx and callback fields:

Verification successful

Initialization and Usage of ctx and callback Fields in WSCallback are Properly Managed

The ctx and callback fields in the WSCallback struct are appropriately initialized and used across various functions, ensuring correct context management and callback handling.

  • The ctx field is initialized and used in functions such as NewUsingCallbackWithCancel, Connect, ConnectWithCancel, and AttemptReconnectWithCancel.
  • The callback field is initialized in NewUsingCallbackWithCancel.

These usages confirm that the fields are managed as intended.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the initialization and usage of `ctx` and `callback` fields in `WSCallback`.

# Test: Search for the usage of `ctx` and `callback` fields. Expect: Proper initialization and usage.
rg --type go -A 5 'WSCallback' | rg 'ctx|callback'

Length of output: 14692

examples/speech-to-text/websocket/http_callback/main.go (1)

40-40: Usage of NewWSUsingCallbackForDemo is correctly integrated.

The new method is correctly used to create a Deepgram client.

Ensure that the NewWSUsingCallbackForDemo method is properly implemented and tested.

Run the following script to verify the implementation of NewWSUsingCallbackForDemo:

Verification successful

The implementation of NewWSUsingCallbackForDemo is verified and correct.

The function NewWSUsingCallbackForDemo is properly implemented and calls listenv1ws.NewUsingCallbackForDemo, which sets up the websocket connection using default options. This aligns with its intended use for demonstration purposes. No further issues are found.

  • Implementation is located in pkg/client/listen/v1/websocket/new_using_callbacks.go.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `NewWSUsingCallbackForDemo`.

# Test: Search for the implementation of `NewWSUsingCallbackForDemo`. Expect: Proper implementation.
rg --type go -A 5 'NewWSUsingCallbackForDemo'

Length of output: 2219


Script:

#!/bin/bash
# Description: Verify the implementation of `listenv1ws.NewUsingCallbackForDemo`.

# Test: Search for the implementation of `NewUsingCallbackForDemo`. Expect: Proper implementation.
rg --type go -A 10 'NewUsingCallbackForDemo'

Length of output: 2898

examples/speech-to-text/websocket/http_channel/main.go (1)

24-83: Example code effectively demonstrates channel-based websocket interactions.

The example code is clear and functional, showcasing the use of NewWSUsingChanForDemo.

Ensure that the NewWSUsingChanForDemo method is properly implemented and tested.

Run the following script to verify the implementation of NewWSUsingChanForDemo:

Verification successful

Implementation of NewWSUsingChanForDemo is verified and correct.

The function NewWSUsingChanForDemo is properly implemented in pkg/client/listen/client.go, and it effectively creates a websocket connection for demo purposes. No issues were found with its implementation.

  • pkg/client/listen/client.go: Implementation of NewWSUsingChanForDemo.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `NewWSUsingChanForDemo`.

# Test: Search for the implementation of `NewWSUsingChanForDemo`. Expect: Proper implementation.
rg --type go -A 5 'NewWSUsingChanForDemo'

Length of output: 1873

pkg/api/listen/v1/websocket/types.go (4)

16-28: LGTM! Struct definition is clear and appropriate.

The DefaultChanHandler struct is well-defined for handling different WebSocket events using channels.


31-43: LGTM! Struct definition is clear and appropriate.

The ChanRouter struct is well-defined for routing events to multiple channel subscribers.


51-54: LGTM! Struct definition is clear and appropriate.

The DefaultCallbackHandler struct is straightforward and serves its purpose for handling callbacks.


57-60: LGTM! Struct definition is clear and appropriate.

The CallbackRouter struct is well-defined for routing events using callbacks.

pkg/client/manage/v1/client.go (1)

38-39: LGTM! Change to use common.NewREST is appropriate.

The modification to use common.NewREST for client initialization aligns with the intended RESTful interactions.

.golangci.yaml (1)

109-126: LGTM! Linter path updates are appropriate.

The changes to linter paths and configurations enhance linting coverage for websocket-related files.

pkg/client/speak/v1/rest/client.go (1)

54-57: Verify the implementation of common.NewREST.

The New function now uses common.NewREST for client instantiation. Ensure that this change aligns with the intended REST-specific functionality.

Run the following script to verify the implementation of common.NewREST:

Verification successful

Verified: Implementation of common.NewREST is correct.

The NewREST function properly initializes a RESTClient, handling API key assignment and option parsing with error logging. This aligns with the intended REST-specific functionality. No issues found.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `common.NewREST`.

# Test: Search for the function implementation. Expect: Correct implementation of REST client creation.
ast-grep --lang go --pattern $'func NewREST($_, $_) *RESTClient { $$$ }'

Length of output: 886

pkg/client/live/legacy.go (2)

91-93: Verify the implementation of listenv1ws.NewUsingCallbackWithCancel.

The NewWithCancel function now uses listenv1ws.NewUsingCallbackWithCancel. Ensure that this change aligns with the intended callback management functionality.


72-74: Verify the implementation of listenv1ws.NewUsingCallbackWithCancel.

The New function now uses listenv1ws.NewUsingCallbackWithCancel. Ensure that this change aligns with the intended callback management functionality.

Run the following script to verify the implementation of listenv1ws.NewUsingCallbackWithCancel:

Verification successful

Verified: Implementation of listenv1ws.NewUsingCallbackWithCancel.

The function NewUsingCallbackWithCancel correctly implements context and callback management, aligning with the intended functionality in the New function.

  • Located in pkg/client/listen/v1/websocket/new_using_callbacks.go.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `listenv1ws.NewUsingCallbackWithCancel`.

# Test: Search for the function implementation. Expect: Correct implementation of callback management.
ast-grep --lang go --pattern $'func NewUsingCallbackWithCancel($_, $_, $_, $_, $_, $_) (*Client, error) { $$$ }'

Length of output: 113


Script:

#!/bin/bash
# Search for any occurrences of `NewUsingCallbackWithCancel` in the codebase to verify its existence or find similar functions.
rg 'NewUsingCallbackWithCancel'

Length of output: 996


Script:

#!/bin/bash
# Extract the implementation details of `NewUsingCallbackWithCancel` from its definition file.
ast-grep --lang go --pattern $'func NewUsingCallbackWithCancel($_, $_, $_, $_, $_, $_) (*WSCallback, error) { $$$ }'

Length of output: 3911

pkg/client/common/v1/rest.go (3)

Line range hint 39-59: LGTM!

The SetupRequest method is now associated with RESTClient, enhancing clarity regarding its purpose.


Line range hint 66-100: LGTM!

The HandleResponse method is now associated with RESTClient, enhancing clarity regarding its purpose.


Line range hint 21-34: Verify the implementation of restv1.New.

The NewREST function uses restv1.New for client instantiation. Ensure that this change aligns with the intended REST-specific functionality.

Run the following script to verify the implementation of restv1.New:

pkg/client/listen/v1/websocket/new_using_chan.go (4)

25-27: LGTM!

The function NewUsingChanForDemo correctly delegates to NewUsingChan with default parameters.


36-38: LGTM!

The function NewUsingChanWithDefaults correctly delegates to NewUsingChan with default parameters and a channel handler.


50-53: LGTM!

The function NewUsingChan correctly initializes a context with cancellation and delegates to NewUsingChanWithCancel.


66-111: LGTM!

The function NewUsingChanWithCancel is well-implemented with appropriate error handling and default channel handler setup.

examples/speech-to-text/websocket/microphone_callback/main.go (2)

153-153: LGTM!

The Deepgram client is correctly instantiated using NewWSUsingCallback with the appropriate parameters.


Line range hint 1-152: Well-structured example!

The example effectively demonstrates the usage of the new WebSocket client with a custom callback and microphone input.

Also applies to: 154-223

pkg/api/listen/v1/websocket/callback_router.go (3)

18-42: LGTM!

The creation functions for CallbackRouter are correctly implemented with clear deprecation notices.


Line range hint 48-195: LGTM!

The methods of CallbackRouter are well-implemented with clear routing logic for various message types.


Line range hint 146-192: LGTM!

The Message and UnhandledMessage methods are robust and handle message routing and logging effectively.

pkg/api/listen/v1/websocket/callback_default.go (9)

20-34: Efficient use of environment variables for debugging.

The introduction of debugWebsocket and debugWebsocketVerbose fields to handle debug logging is a good improvement. It reduces redundancy and centralizes the logic for checking debug settings.


Line range hint 38-60: Refactored debug logic in Open method.

The refactoring of the Open method to use debugWebsocket for logging is efficient and improves readability.


Line range hint 64-98: Refactored debug logic in Message method.

The Message method now uses debugWebsocket for logging, which is consistent with the refactoring pattern.


Line range hint 100-125: Refactored debug logic in Metadata method.

The Metadata method now uses debugWebsocket for logging, maintaining consistency across methods.


Line range hint 127-146: Refactored debug logic in SpeechStarted method.

The SpeechStarted method uses both debugWebsocket and debugWebsocketVerbose for logging, which is a good use of the new fields.


161-163: Verbose logging in UtteranceEnd method.

The use of debugWebsocketVerbose for additional logging in the UtteranceEnd method is consistent with the intended functionality.


Line range hint 170-193: Refactored debug logic in Close method.

The Close method now uses debugWebsocket for logging, which aligns with the refactoring strategy.


Line range hint 195-222: Refactored debug logic in Error method.

The Error method uses debugWebsocket for logging, which is consistent with other methods.


Line range hint 224-238: Refactored debug logic in UnhandledEvent method.

The UnhandledEvent method now uses debugWebsocket for logging, maintaining consistency.

pkg/client/listen/v1/websocket/new_using_callbacks.go (7)

19-29: Deprecation notice for NewForDemo.

The function NewForDemo is deprecated in favor of NewUsingCallbackForDemo. Ensure that documentation and usage examples reflect this change.


31-39: Implementation of NewUsingCallbackForDemo.

The function NewUsingCallbackForDemo provides a streamlined way to create websocket connections with default options, which is useful for demonstrations.


41-53: Deprecation notice for NewWithDefaults.

The function NewWithDefaults is deprecated in favor of NewUsingCallbackWithDefaults. Ensure that documentation and usage examples reflect this change.


55-63: Implementation of NewUsingCallbackWithDefaults.

The function NewUsingCallbackWithDefaults offers a clear way to create websocket connections with default options and a specified callback.


65-79: Deprecation notice for New.

The function New is deprecated in favor of NewUsingCallback. Ensure that documentation and usage examples reflect this change.


81-94: Implementation of NewUsingCallback.

The function NewUsingCallback effectively sets up websocket connections with specified options and a callback, utilizing context for cancellation.


96-149: Comprehensive setup in NewUsingCallbackWithCancel.

The function NewUsingCallbackWithCancel provides a thorough setup for websocket connections, including context management and error handling.

pkg/client/listen/v1/rest/client.go (4)

60-62: Transition to common.NewREST.

The use of common.NewREST in the New function indicates a shift to a REST-specific client, which may enhance API interactions. Ensure that this change is reflected in any related documentation or usage examples.


Line range hint 64-102: Robust error handling in DoFile.

The DoFile function includes comprehensive error handling for file operations, which is crucial for reliability.


Line range hint 104-152: Efficient stream handling in DoStream.

The DoStream function efficiently sets up and executes streaming requests, with detailed logging for troubleshooting.


Line range hint 154-203: URL validation in DoURL.

The DoURL function includes robust URL validation and error handling, ensuring reliable operation.

pkg/client/analyze/v1/client.go (1)

62-62: LGTM! The shift to common.NewREST is appropriate.

The change aligns the client initialization with REST-specific functionality, which is consistent with the file's purpose.

examples/speech-to-text/websocket/microphone_channel/main.go (4)

24-33: LGTM! The channel-based struct is well-organized.

The MyHandler struct effectively uses channels to handle asynchronous WebSocket events.


35-52: LGTM! The handler initialization and goroutine setup are appropriate.

The NewMyHandler function correctly sets up the MyHandler struct and starts it in a goroutine for concurrent processing.


96-201: LGTM! The Run function is well-structured for concurrent event handling.

The use of goroutines and a wait group ensures efficient processing of asynchronous events.


203-308: LGTM! The main function demonstrates a clear setup and teardown process.

The function effectively initializes the microphone, sets up the Deepgram client, and handles errors and resource cleanup.

pkg/api/listen/v1/websocket/chan_default.go (3)

20-43: LGTM! The channel-based struct is well-organized.

The DefaultChanHandler struct effectively uses channels to handle asynchronous WebSocket events.


21-53: LGTM! The handler initialization and goroutine setup are appropriate.

The NewDefaultChanHandler function correctly sets up the DefaultChanHandler struct and starts it in a goroutine for concurrent processing.


98-329: LGTM! The Run function is well-structured for concurrent event handling.

The use of goroutines and a wait group ensures efficient processing of asynchronous events.

pkg/api/listen/v1/websocket/chan_router.go (14)

40-73: Verify the usage of DEEPGRAM_DEBUG.

Ensure that the DEEPGRAM_DEBUG environment variable is consistently used across the codebase for debugging purposes.


75-97: LGTM!

The Open function is implemented correctly with appropriate error handling.


99-121: LGTM!

The Close function is implemented correctly with appropriate error handling.


123-145: LGTM!

The Error function is implemented correctly with appropriate error handling.


147-162: LGTM!

The processGeneric function is implemented correctly with appropriate logging and error handling.


164-179: LGTM!

The processMessage function is implemented correctly with appropriate error handling.


181-196: LGTM!

The processMetadata function is implemented correctly with appropriate error handling.


198-213: LGTM!

The processSpeechStartedResponse function is implemented correctly with appropriate error handling.


215-230: LGTM!

The processUtteranceEndResponse function is implemented correctly with appropriate error handling.


232-247: LGTM!

The processErrorResponse function is implemented correctly with appropriate error handling.


249-287: LGTM!

The Message function is implemented correctly with appropriate logging and error handling.


289-301: LGTM!

The UnhandledMessage function is implemented correctly with appropriate logging and error handling.


303-313: LGTM!

The printDebugMessages function is implemented correctly with appropriate error handling for JSON formatting.


18-38: Verify the usage of DEEPGRAM_DEBUG.

Ensure that the DEEPGRAM_DEBUG environment variable is consistently used across the codebase for debugging purposes.

Run the following script to verify the usage of DEEPGRAM_DEBUG:

Verification successful

Consistent usage of DEEPGRAM_DEBUG confirmed.

The DEEPGRAM_DEBUG environment variable is consistently used across the codebase for debugging purposes, particularly within the websocket package. This aligns with standard practices for handling debug configurations.

  • Files using DEEPGRAM_DEBUG:
    • pkg/api/listen/v1/websocket/callback_router.go
    • pkg/api/listen/v1/websocket/chan_router.go
    • pkg/api/listen/v1/websocket/callback_default.go
    • pkg/api/listen/v1/websocket/chan_default.go
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent usage of DEEPGRAM_DEBUG environment variable.

# Test: Search for occurrences of DEEPGRAM_DEBUG. Expect: Consistent usage across the codebase.
rg --type go -A 2 $'DEEPGRAM_DEBUG'

Length of output: 2503

pkg/client/listen/v1/websocket/client_callback.go (17)

25-29: LGTM!

The Connect function is implemented correctly with appropriate context management.


31-37: LGTM!

The ConnectWithCancel function is implemented correctly with appropriate context management.


39-43: LGTM!

The AttemptReconnect function is implemented correctly with appropriate context management.


45-50: LGTM!

The AttemptReconnectWithCancel function is implemented correctly with appropriate context management.


52-61: LGTM!

The GetURL function is implemented correctly with appropriate error handling and logging.


63-71: LGTM!

The Start function is implemented correctly with appropriate checks for configuration options.


73-103: LGTM!

The ProcessMessage function is implemented correctly with appropriate error handling and logging.


105-155: LGTM!

The Stream function is implemented correctly with appropriate error handling and logging.


157-175: LGTM!

The Write function is implemented correctly with appropriate error handling and logging.


177-195: LGTM!

The KeepAlive function is implemented correctly with appropriate error handling and logging.


197-215: LGTM!

The Finalize function is implemented correctly with appropriate error handling and logging.


217-220: LGTM!

The GetCloseMsg function is implemented correctly.


222-225: LGTM!

The Finish function is implemented correctly as a placeholder.


227-236: LGTM!

The ProcessError function is implemented correctly with appropriate error handling and logging.


238-279: LGTM!

The ping function is implemented correctly with appropriate error handling and logging.


281-344: LGTM!

The flush function is implemented correctly with appropriate error handling and logging.


346-372: LGTM!

The errorToResponse function is implemented correctly with appropriate error handling and formatting.

pkg/client/listen/v1/websocket/client_channel.go (17)

25-29: LGTM!

The Connect function is implemented correctly with appropriate context management.


31-37: LGTM!

The ConnectWithCancel function is implemented correctly with appropriate context management.


39-43: LGTM!

The AttemptReconnect function is implemented correctly with appropriate context management.


45-50: LGTM!

The AttemptReconnectWithCancel function is implemented correctly with appropriate context management.


52-61: LGTM!

The GetURL function is implemented correctly with appropriate error handling and logging.


63-71: LGTM!

The Start function is implemented correctly with appropriate checks for configuration options.


73-103: LGTM!

The ProcessMessage function is implemented correctly with appropriate error handling and logging.


105-155: LGTM!

The Stream function is implemented correctly with appropriate error handling and logging.


157-175: LGTM!

The Write function is implemented correctly with appropriate error handling and logging.


177-195: LGTM!

The KeepAlive function is implemented correctly with appropriate error handling and logging.


197-215: LGTM!

The Finalize function is implemented correctly with appropriate error handling and logging.


217-220: LGTM!

The GetCloseMsg function is implemented correctly.


222-225: LGTM!

The Finish function is implemented correctly as a placeholder.


227-236: LGTM!

The ProcessError function is implemented correctly with appropriate error handling and logging.


238-279: LGTM!

The ping function is implemented correctly with appropriate error handling and logging.


281-344: LGTM!

The flush function is implemented correctly with appropriate error handling and logging.


346-372: LGTM!

The errorToResponse function is implemented correctly with appropriate error handling and formatting.

pkg/client/common/v1/websocket.go (4)

80-210: Review the use of InsecureSkipVerify.

The InsecureSkipVerify option in tls.Config is set to true, which can expose the connection to man-in-the-middle attacks. Ensure this is intentional and document the security implications.


347-379: LGTM!

The WriteBinary function is well-implemented and handles locking and error logging appropriately.


381-423: LGTM!

The WriteJSON function is well-implemented and handles JSON marshaling, locking, and error logging appropriately.


505-513: LGTM!

The Stop function is well-implemented and handles stopping the WebSocket connection gracefully.

pkg/client/listen/client.go (20)

73-74: LGTM!

The NewWSUsingCallbackForDemo function is well-implemented and correctly delegates to the underlying implementation.


77-81: LGTM!

The NewWebSocketUsingCallbackForDemo function is correctly implemented as an alias. Note the TODO comment for future deprecation.


95-96: LGTM!

The NewWSUsingCallbackWithDefaults function is well-implemented and correctly delegates to the underlying implementation.


99-103: LGTM!

The NewWebSocketUsingCallbackWithDefaults function is correctly implemented as an alias. Note the TODO comment for future deprecation.


119-121: LGTM!

The NewWSUsingCallback function is well-implemented and correctly delegates to the underlying implementation.


124-128: LGTM!

The NewWebSocketUsingCallback function is correctly implemented as an alias. Note the TODO comment for future deprecation.


145-147: LGTM!

The NewWSUsingCallbackWithCancel function is well-implemented and correctly delegates to the underlying implementation.


149-153: LGTM!

The NewWebSocketUsingCallbackWithCancel function is correctly implemented as an alias. Note the TODO comment for future deprecation.


168-170: LGTM!

The NewWSUsingChanForDemo function is well-implemented and correctly delegates to the underlying implementation.


172-176: LGTM!

The NewWebSocketUsingChanForDemo function is correctly implemented as an alias. Note the TODO comment for future deprecation.


189-191: LGTM!

The NewWSUsingChanWithDefaults function is well-implemented and correctly delegates to the underlying implementation.


193-197: LGTM!

The NewWebSocketUsingChanWithDefaults function is correctly implemented as an alias. Note the TODO comment for future deprecation.


209-211: LGTM!

The NewWSUsingChan function is well-implemented and correctly delegates to the underlying implementation.


214-218: LGTM!

The NewWebSocketUsingChan function is correctly implemented as an alias. Note the TODO comment for future deprecation.


231-233: LGTM!

The NewWSUsingChanWithCancel function is well-implemented and correctly delegates to the underlying implementation.


235-239: LGTM!

The NewWebSocketUsingChanWithCancel function is correctly implemented as an alias. Note the TODO comment for future deprecation.


252-254: LGTM!

The NewWebSocketForDemo function is correctly implemented as an alias. Note the TODO comment for future deprecation.


263-265: LGTM!

The NewWebSocketWithDefaults function is correctly implemented as an alias. Note the TODO comment for future deprecation.


274-276: LGTM!

The NewWebSocket function is correctly implemented as an alias. Note the TODO comment for future deprecation.


285-287: LGTM!

The NewWebSocketWithCancel function is correctly implemented as an alias. Note the TODO comment for future deprecation.

pkg/client/common/v1/websocket.go Show resolved Hide resolved
pkg/client/common/v1/websocket.go Show resolved Hide resolved
pkg/client/common/v1/websocket.go Show resolved Hide resolved
pkg/client/common/v1/websocket.go Show resolved Hide resolved
pkg/client/common/v1/websocket.go Show resolved Hide resolved
@davidvonthenen davidvonthenen changed the title Implement Chan Callback Interface Implement Channel Callback Interface Aug 22, 2024
Copy link
Contributor

@jpvajda jpvajda left a comment

Choose a reason for hiding this comment

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

LGTM

@davidvonthenen davidvonthenen merged commit 0d19615 into deepgram:main Aug 23, 2024
3 checks passed
@davidvonthenen davidvonthenen deleted the implement-chan-transport branch August 23, 2024 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants