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

creating speak streaming client and api #239

Merged
merged 1 commit into from
Jun 21, 2024

Conversation

SandraRodgers
Copy link
Contributor

@SandraRodgers SandraRodgers commented Jun 17, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a default callback handler for live transcription and text-to-speech API.
    • Added various response types and constants for text-to-speech streaming.
    • Implemented a new MessageRouter for handling different message types.
  • Enhancements

    • Updated SpeakOptions to include Callback and CallbackMethod.
    • Improved error handling with new error constants for connection and validation issues.
  • Examples

    • Added an interactive text-to-speech example using the Deepgram SDK.

These changes enhance the text-to-speech functionality, provide better error handling, and introduce examples for easier implementation.

Copy link

coderabbitai bot commented Jun 17, 2024

Walkthrough

The overall changes introduce significant enhancements to the Deepgram SDK for text-to-speech streaming. New constants, error-handling mechanisms, struct types, and response types are added. The updates include default callback handlers, message routing logic, and a refined client streaming interface, thus providing a more robust foundation for implementing voice and text-based processing solutions.

Changes

Files Change Summary
pkg/api/speak/v1/constants.go Added error constants ErrInvalidMessageType, ErrUserCallbackNotDefined.
pkg/api/speak/v1/default.go Introduced default callback handler for live transcription, including various event handling functions.
pkg/api/speak/v1/interfaces/constants.go Added message type constants for the text-to-speech streaming API.
pkg/api/speak/v1/interfaces/interfaces.go Defined SpeakMessageCallback interface with methods for handling text-to-speech response types.
pkg/api/speak/v1/interfaces/types.go Added several struct types, including SpeakStreamResponse and different response types.
pkg/api/speak/v1/router.go Introduced MessageRouter struct for handling various message types and routing messages to appropriate callback methods.
pkg/api/version/constants.go Added APITypeSpeakStream and updated APIPathMap to include new API type "speak-stream".
pkg/api/version/speakstream-version.go New file introducing GetSpeakStreamAPI function for managing versioning and customization of the text-to-speech API.
pkg/client/interfaces/types-speak.go Updated SpeakOptions struct to include Callback and CallbackMethod fields.
pkg/client/speak/constants.go Added constants for retry mechanisms, error constants, and a new error related to connection validity.
pkg/client/speak/client_stream.go Enhanced streaming client implementation with functions for websocket connection, retries, event handling, and connection lifecycle management.
examples/speak/stream/http/main.go Renamed Transcription options to TTS options in the main function.
examples/speak/stream/interactive/main.go New file implementing an example for text-to-speech using the Deepgram SDK, with user interaction and callback handling.
pkg/api/speak-stream/v1/constants.go Added error constants for input validation and callback definition in the Deepgram SDK.
pkg/api/speak-stream/v1/default.go Introduced a default callback handler for text-to-speech connections, handling various events with debug logging.
pkg/api/speak-stream/v1/interfaces/constants.go Added message type constants for the text-to-speech streaming API.
pkg/api/speak-stream/v1/interfaces/interfaces.go Defined SpeakMessageCallback interface with methods for handling text-to-speech response types.
pkg/api/speak-stream/v1/interfaces/types.go Added request and response structs for the speech streaming API, including SpeakStreamResponse and various response types.

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 Configration 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.

@dvonthenen dvonthenen marked this pull request as draft June 17, 2024 19:43
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: 1

Outside diff range and nitpick comments (1)
pkg/api/speak/v1/constants.go (1)

Line range hint 1-1: Resolve the import cycle involving pkg/api/speak/v1 and other related packages.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5868b91 and e1158c1.

Files selected for processing (9)
  • pkg/api/speak/v1/constants.go (1 hunks)
  • pkg/api/speak/v1/default.go (1 hunks)
  • pkg/api/speak/v1/interfaces/constants.go (1 hunks)
  • pkg/api/speak/v1/interfaces/interfaces.go (1 hunks)
  • pkg/api/speak/v1/interfaces/types.go (1 hunks)
  • pkg/api/speak/v1/router.go (1 hunks)
  • pkg/client/speak/client_stream.go (1 hunks)
  • pkg/client/speak/constants.go (1 hunks)
  • pkg/client/speak/types_stream.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • pkg/api/speak/v1/interfaces/constants.go
Additional context used
golangci-lint
pkg/api/speak/v1/constants.go

1-1: : import cycle not allowed: import stack: [github.com/deepgram/deepgram-go-sdk/examples/speak/file/hello-world github.com/deepgram/deepgram-go-sdk/pkg/api/speak/v1 github.com/deepgram/deepgram-go-sdk/pkg/client/speak github.com/deepgram/deepgram-go-sdk/pkg/api/speak/v1] (typecheck)

pkg/client/speak/client_stream.go

24-24: could not import github.com/deepgram/deepgram-go-sdk/pkg/api/speak/v1 (-: import cycle not allowed: import stack: [github.com/deepgram/deepgram-go-sdk/examples/speak/file/hello-world github.com/deepgram/deepgram-go-sdk/pkg/api/speak/v1 github.com/deepgram/deepgram-go-sdk/pkg/client/speak github.com/deepgram/deepgram-go-sdk/pkg/api/speak/v1]) (typecheck)

GitHub Check: Lint
pkg/client/speak/client_stream.go

[failure] 24-24:
could not import github.com/deepgram/deepgram-go-sdk/pkg/api/speak/v1 (-: import cycle not allowed: import stack: [github.com/deepgram/deepgram-go-sdk/examples/speak/file/hello-world github.com/deepgram/deepgram-go-sdk/pkg/api/speak/v1 github.com/deepgram/deepgram-go-sdk/pkg/client/speak github.com/deepgram/deepgram-go-sdk/pkg/api/speak/v1]) (typecheck)

Additional comments not posted (25)
pkg/api/speak/v1/interfaces/interfaces.go (1)

8-15: The SpeakMessageCallback interface is well-defined, covering all necessary message types for the streaming API.

pkg/api/speak/v1/constants.go (1)

16-20: The new error constants are clearly defined and appropriate for the functionalities being added.

pkg/client/speak/types_stream.go (1)

19-39: The StreamClient struct is well-designed, encapsulating all necessary fields and mechanisms for managing a websocket client connection efficiently.

pkg/client/speak/constants.go (1)

12-43: The defined constants for retry mechanisms, timing, and socket error handling are appropriately set and crucial for the robust operation of the websocket client.

pkg/api/speak/v1/interfaces/types.go (6)

30-36: The SpeakStreamResponse struct is well-defined with appropriate JSON tags. This struct will be essential for handling streaming responses.


50-52: The MessageType struct is crucial for identifying the type of message being processed. Ensure that this type is used consistently across the application to avoid type mismatches.


54-58: The MetadataResponse struct is defined correctly. It's good to see the use of optional JSON fields which can provide flexibility in response handling.


67-72: The ErrorResponse struct is comprehensive and correctly annotated with JSON tags. It includes all necessary fields to describe an error.


74-77: The OpenResponse struct is minimal and appropriate for its purpose, which likely involves signaling the opening of a connection.


79-82: The CloseResponse struct is similarly minimal and well-suited for signaling the closure of a connection.

pkg/api/speak/v1/router.go (8)

17-21: The MessageRouter struct is well-defined with fields for a callback interface and a debug flag. This structure is crucial for routing messages based on their types.


23-26: NewWithDefault function is a convenient way to create a MessageRouter with a default handler. This is good for quick setups or default configurations.


28-35: NewStream function correctly initializes a MessageRouter with a custom callback. The use of environment variables for debug settings is a practical choice for runtime configuration.


37-40: Helper functions (OpenHelper, CloseHelper, ErrorHelper) are implemented correctly to delegate the handling of different message types to the callback. This modular approach enhances maintainability.

Also applies to: 42-45, 47-50


52-67: The processGeneric method is a well-thought-out implementation that generalizes message processing. This reduces duplication and simplifies the handling of different message types. The detailed logging is beneficial for debugging.


69-79: Methods processFlushed, processMetadata, and processErrorResponse are correctly implemented to handle specific message types. These methods ensure that messages are processed according to their type, maintaining a clean separation of concerns.

Also applies to: 82-92, 95-105


108-142: The Message method effectively routes messages based on their type using a switch statement. This central routing logic is crucial for the flexibility and scalability of the message handling system.


144-151: UnhandledMessage method provides a robust way to deal with unexpected message types, logging an appropriate error and returning an ErrInvalidMessageType. This is an essential safety net for catching unanticipated or malformed messages.

pkg/api/speak/v1/default.go (2)

19-26: DefaultCallbackHandler is a simple struct with no fields, serving as a placeholder for default handling logic. The NewDefaultCallbackHandler function correctly instantiates it.


28-57: All callback methods (Open, Metadata, Flushed, Close, Error, UnhandledEvent) in DefaultCallbackHandler are implemented with a consistent pattern. They check for a debug environment variable and, if set, log detailed information about the message. This implementation is clear and maintains consistency across methods.

Also applies to: 59-88, 90-119, 121-150, 152-185, 187-211

pkg/client/speak/client_stream.go (5)

30-38: The functions NewStreamForDemo, NewStreamWithDefaults, NewStream, and NewStreamWithCancel are well-implemented for creating new streaming clients with various configurations. These functions provide flexibility in setting up connections with different parameters.

Also applies to: 40-49, 51-64, 66-115


117-143: Connection management functions (Connect, ConnectWithCancel, AttemptReconnect, AttemptReconnectWithCancel) are correctly handling retries and context management. This robust handling is crucial for maintaining a stable connection.


149-277: The internalConnectWithCancel method is a key component of the connection management system. It handles the complexities of establishing a websocket connection, including retries, error handling, and thread safety. The detailed logging and error handling enhance the robustness of the connection process.


279-395: The listen method is central to the functionality of the StreamClient, handling incoming messages and maintaining the connection. The implementation is comprehensive, covering various scenarios such as graceful and fatal closures, and message inspection.


397-429: Write operations (WriteBinary, WriteJSON, Write, Finalize, Stop, closeWs) are implemented with careful consideration of thread safety and error handling. These methods are crucial for sending data to and managing the state of the websocket connection.

Also applies to: 431-472, 474-492, 494-517, 521-576

pkg/api/speak/v1/interfaces/types.go Outdated Show resolved Hide resolved
@dvonthenen dvonthenen marked this pull request as ready for review June 17, 2024 20:04
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: 8

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e1158c1 and 39aa306.

Files selected for processing (9)
  • pkg/api/speak-stream/v1/constants.go (1 hunks)
  • pkg/api/speak-stream/v1/default.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/constants.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/interfaces.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/types.go (1 hunks)
  • pkg/api/speak-stream/v1/router.go (1 hunks)
  • pkg/api/speak/v1/interfaces/types.go (1 hunks)
  • pkg/client/speak/client_stream.go (1 hunks)
  • pkg/client/speak/types_stream.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • pkg/api/speak-stream/v1/interfaces/constants.go
Files skipped from review as they are similar to previous changes (1)
  • pkg/client/speak/types_stream.go
Additional context used
GitHub Check: Lint
pkg/client/speak/client_stream.go

[failure] 279-279:
cyclomatic complexity 21 of func (*StreamClient).listen is high (> 20) (gocyclo)

Additional comments not posted (12)
pkg/api/speak-stream/v1/interfaces/interfaces.go (1)

8-15: The SpeakMessageCallback interface is well-defined, covering all essential message types for the streaming API.

pkg/api/speak-stream/v1/constants.go (1)

11-21: The error constants are clearly defined and appropriately encapsulated within their respective error messages, enhancing maintainability and readability.

pkg/api/speak/v1/interfaces/types.go (1)

Line range hint 11-29: The types are well-structured and align with the expected JSON format for API communication. Ensure the JSON tags are consistent with the property names to avoid serialization issues.

pkg/api/speak-stream/v1/router.go (3)

23-26: The method NewWithDefault() uses a good factory pattern to encapsulate the creation of a MessageRouter with a default handler.


153-163: The method printDebugMessages uses prettyjson to format JSON for logging. Ensure that this does not introduce performance issues in production environments, especially with large message sizes.

Verification successful

The printDebugMessages method is called in pkg/api/speak-stream/v1/router.go and pkg/api/live/v1/router.go. To determine if these calls are in performance-critical paths, we need to understand the broader context in which these methods are invoked.

Next, we should inspect the functions or methods that call printDebugMessages to assess their context and determine if they are part of performance-critical code.


The printDebugMessages method is used in the context of logging within error handling and unhandled message scenarios. These contexts are not typically performance-critical paths, as they are more related to debugging and error reporting.

Given this context, the usage of prettyjson for formatting JSON for logs should not introduce significant performance issues in production environments.

  • pkg/api/speak-stream/v1/router.go

    • r.printDebugMessages(5, msgType, byMsg)
    • r.printDebugMessages(3, "UnhandledMessage", byMsg)
  • pkg/api/live/v1/router.go

    • r.printDebugMessages(5, msgType, byMsg)
    • r.printDebugMessages(3, "UnhandledMessage", byMsg)

These calls are part of logging mechanisms, which are generally not in the critical path of application performance.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check for any performance issues related to prettyjson usage in production.
grep -R 'prettyjson' . --include=*.go

Length of output: 8636



Script:

#!/bin/bash
# Search for calls to the printDebugMessages method to understand its usage context.
rg 'printDebugMessages' --glob='*.go'

Length of output: 767



Script:

#!/bin/bash
# Search for functions or methods that call printDebugMessages to understand the context.
rg -A 5 'printDebugMessages' --glob='*.go'

Length of output: 2479


52-67: This method uses a generic approach to process messages by taking an action function as an argument. Ensure that all possible data types are handled correctly in the action function to prevent runtime errors.

pkg/api/speak-stream/v1/default.go (4)

59-88: The Metadata method handles metadata messages similarly to the Open method. As previously noted, ensure that sensitive information is not exposed in logs.


90-119: The Flushed callback method handles flushed messages. It shares the same logging pattern as other methods, which should be checked for potential information exposure.


121-150: The Close method handles close messages and logs detailed information when debugging is enabled. This consistency in handling debug information is good, but the same precautions about sensitive data apply.


28-57: The Open method provides detailed logging when the DEEPGRAM_DEBUG environment variable is set. Ensure that sensitive information is not logged.

Verification successful

The OpenResponse struct in pkg/api/speak-stream/v1/interfaces/types.go only contains a single field, Type, which is a string. This field does not appear to contain sensitive information. Therefore, the logging in the Open method of pkg/api/speak-stream/v1/default.go does not seem to expose any sensitive information.

  • pkg/api/speak-stream/v1/interfaces/types.go
    • type OpenResponse struct { Type string json:"type,omitempty" }
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check for potential logging of sensitive information.
grep -R 'json.Marshal' . --include=*.go

Length of output: 11052



Script:

#!/bin/bash
# Find the definition of the OpenResponse struct to analyze its fields.
ast-grep --lang go --pattern 'type OpenResponse struct { $$$ }'

Length of output: 467

pkg/client/speak/client_stream.go (2)

47-49: The NewStreamWithDefaults function is similar to NewStreamForDemo but allows for a callback. The same security considerations apply.


36-38: The NewStreamForDemo function provides a simplified way to create a stream with default options. Ensure that the API key and other sensitive defaults are securely managed.

Verification successful

The grep output shows several instances where API keys are being set and used. This includes setting API keys in headers and options, as well as error handling for missing API keys.

To ensure secure management of API keys, we should:

  1. Verify that API keys are not hardcoded in the codebase.
  2. Ensure that API keys are not being logged or exposed inappropriately.

Let's run additional checks to gather more information:


The verification confirms that there are no hardcoded API keys in the production code, except for a mock API key used in unit tests (./tests/unit_test/mocks.go). Additionally, there are no instances of API keys being logged inappropriately.

The NewStreamForDemo function appears to manage API keys securely by accepting them as parameters and not hardcoding them. Therefore, the concerns about secure management of API keys and sensitive defaults are addressed.

  • No hardcoded API keys in production code.
  • No inappropriate logging of API keys.
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify secure management of API keys and sensitive defaults.
grep -R 'APIKey' . --include=*.go

Length of output: 3491



Script:

#!/bin/bash
# Check if there are any hardcoded API keys in the Go files
grep -R 'APIKey\s*=\s*"' . --include=*.go

# Check if API keys are being logged
grep -R 'log' . --include=*.go | grep -i 'api'

Length of output: 22664

pkg/api/speak-stream/v1/interfaces/types.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/router.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/router.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/router.go Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 39aa306 and 1cedcbd.

Files selected for processing (1)
  • pkg/api/speak-stream/v1/interfaces/types.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pkg/api/speak-stream/v1/interfaces/types.go

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: 1

Outside diff range and nitpick comments (1)
examples/stream-speak/http/main.go (1)

23-87: Consider adding detailed comments explaining each step in the main function to improve readability and maintainability, especially for new developers or external contributors.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1cedcbd and 9ddbc0b.

Files selected for processing (3)
  • examples/stream-speak/http/main.go (1 hunks)
  • pkg/api/version/speakstream-version.go (1 hunks)
  • pkg/client/speak/client_stream.go (1 hunks)
Additional context used
GitHub Check: Lint
pkg/client/speak/client_stream.go

[failure] 279-279:
cyclomatic complexity 21 of func (*StreamClient).listen is high (> 20) (gocyclo)

Additional comments not posted (1)
pkg/api/version/speakstream-version.go (1)

32-34: The implementation of GetSpeakStreamAPI looks clean and follows best practices for constructing service URLs dynamically.

pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/api/speak-stream/v1/router.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
Copy link
Contributor

@dvonthenen dvonthenen left a comment

Choose a reason for hiding this comment

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

This is looking great. Got some feedback for you!

pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
examples/stream-speak/http/main.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/interfaces/constants.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/interfaces/interfaces.go Outdated Show resolved Hide resolved
pkg/api/version/speakstream-version.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/router.go Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
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: 10

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9ddbc0b and cee9a23.

Files selected for processing (12)
  • examples/speak/rest/stream/hello-world/main.go (1 hunks)
  • examples/speak/stream/http/main.go (1 hunks)
  • pkg/api/speak-stream/v1/default.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/constants.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/interfaces.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/types.go (1 hunks)
  • pkg/api/speak-stream/v1/router.go (1 hunks)
  • pkg/api/version/constants.go (2 hunks)
  • pkg/api/version/speakstream-version.go (1 hunks)
  • pkg/api/version/version.go (1 hunks)
  • pkg/client/interfaces/utils.go (1 hunks)
  • pkg/client/speak/client_stream.go (1 hunks)
Files not reviewed due to errors (1)
  • pkg/client/speak/client_stream.go (no review received)
Files skipped from review due to trivial changes (1)
  • examples/speak/rest/stream/hello-world/main.go
Files skipped from review as they are similar to previous changes (5)
  • pkg/api/speak-stream/v1/interfaces/constants.go
  • pkg/api/speak-stream/v1/interfaces/interfaces.go
  • pkg/api/speak-stream/v1/interfaces/types.go
  • pkg/api/speak-stream/v1/router.go
  • pkg/api/version/speakstream-version.go
Additional context used
Learnings (2)
pkg/api/speak-stream/v1/default.go (1)
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/api/speak-stream/v1/default.go:187-211
Timestamp: 2024-06-17T23:10:23.450Z
Learning: The `UnhandledEvent` method in the `DefaultCallbackHandler` from the `pkg/api/speak-stream/v1/default.go` file is intended for demonstration purposes only and not used in production environments.
pkg/client/speak/client_stream.go (1)
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:279-279
Timestamp: 2024-06-17T23:06:01.713Z
Learning: Ignore cyclomatic complexity warnings for the `listen` method in the `StreamClient` class as per dvonthenen's request.
GitHub Check: Lint
pkg/client/speak/client_stream.go

[failure] 385-385:
ifElseChain: rewrite if-else to switch statement (gocritic)

Additional comments not posted (7)
pkg/api/version/constants.go (1)

24-24: The addition of APITypeSpeakStream constant is appropriate for the new feature.

pkg/client/interfaces/utils.go (1)

74-79: Enhancements to the DeepgramError struct with additional fields (Type, Description, Variant) are well-implemented and improve error context.

pkg/api/version/version.go (1)

31-31: Including APITypeSpeakStream in the condition for setting the protocol to WSProtocol correctly supports the new streaming feature.

pkg/api/speak-stream/v1/default.go (4)

19-21: The DefaultCallbackHandler struct is well-defined and serves as a base for handling different types of callbacks in the speak/streaming client. This is a foundational change that enables extensible handling of various events in streaming connections.


22-25: The NewDefaultCallbackHandler function is straightforward and correctly initializes a new instance of DefaultCallbackHandler. This is a typical factory method pattern which is appropriate here.


120-124: The Binary callback method is simple and logs the size of the binary data received. This method is correctly implemented for its intended functionality.


192-216: The UnhandledEvent method is intended for demonstration purposes only and not used in production environments. It correctly logs unhandled events when debugging is enabled. Given the context, no changes are necessary here.

pkg/api/speak-stream/v1/default.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Show resolved Hide resolved
pkg/api/version/constants.go Show resolved Hide resolved
examples/speak/stream/http/main.go Outdated Show resolved Hide resolved
examples/speak/stream/http/main.go Outdated Show resolved Hide resolved
examples/speak/stream/http/main.go Outdated Show resolved Hide resolved
examples/speak/stream/http/main.go Outdated Show resolved Hide resolved
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 and nitpick comments (4)
pkg/client/speak/client_stream.go (4)

36-38: Consider enhancing the documentation to explain what "all default options" means in this context.


126-130: The ConnectWithCancel method is a good addition for managing connections with more control. Ensure proper documentation is added to describe its usage and parameters.


279-408: The listen method is marked to ignore cyclomatic complexity warnings. However, consider adding more detailed logging to help with future debugging and maintenance.

Tools
GitHub Check: Lint

[failure] 385-385:
ifElseChain: rewrite if-else to switch statement (gocritic)


532-554: Reset method is well-implemented with clear error handling. Consider adding more comments to explain the purpose of the reset in the context of the application.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cee9a23 and 8f388b7.

Files selected for processing (2)
  • examples/speak/stream/http/main.go (1 hunks)
  • pkg/client/speak/client_stream.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • examples/speak/stream/http/main.go
Additional context used
Learnings (1)
pkg/client/speak/client_stream.go (1)
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:279-279
Timestamp: 2024-06-17T23:06:01.713Z
Learning: Ignore cyclomatic complexity warnings for the `listen` method in the `StreamClient` class as per dvonthenen's request.
GitHub Check: Lint
pkg/client/speak/client_stream.go

[failure] 385-385:
ifElseChain: rewrite if-else to switch statement (gocritic)

Additional comments not posted (6)
pkg/client/speak/client_stream.go (6)

47-49: This method duplicates the logic of NewStreamForDemo. Consider consolidating these methods to reduce redundancy.
[REFACTOR_SUGGESTion]

- func NewStreamWithDefaults(ctx context.Context, options *interfaces.SpeakOptions, callback msginterfaces.SpeakMessageCallback) (*StreamClient, error) {
- 	return NewStream(ctx, "", &interfaces.ClientOptions{}, options, callback)
- }

61-64: The method NewStream is well-documented and neatly sets up a new stream with cancellation. Good use of context management.


132-143: The retry logic in AttemptReconnectWithCancel is clear and concise. Good implementation of managing reconnection attempts.


410-442: The WriteBinary method correctly handles connection checks and locks. Ensure that error handling is consistent and that all errors are logged at an appropriate level.


491-505: The Write method is a straightforward implementation of the io.Writer interface. Good job on keeping it simple and to the point.


556-611: The Stop and closeWs methods handle connection teardown effectively. Ensure that all resources are properly released and that the connection closure is logged for audit purposes.

pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
Copy link
Contributor

@dvonthenen dvonthenen left a comment

Choose a reason for hiding this comment

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

This is amazing! Great job here!

I just left some minor feedback and dismissed a bunch of coderabbit reviews that didn't add value.

Just a clarifying Q, for your MyCallback's Binary(), did you test to make sure you could play the file?

Overall, this is EXCELLENT! 🤘

examples/speak/stream/http/main.go Outdated Show resolved Hide resolved
examples/speak/stream/http/main.go Outdated Show resolved Hide resolved
examples/speak/stream/http/main.go Outdated Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Show resolved Hide resolved
pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/client/speak/constants.go Show resolved Hide resolved
@dvonthenen
Copy link
Contributor

Just in case this got lost:
#239 (review)

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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8f388b7 and 8819005.

Files selected for processing (4)
  • examples/speak/stream/http/main.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/interfaces.go (1 hunks)
  • pkg/client/speak/client_stream.go (1 hunks)
  • pkg/client/speak/constants.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • examples/speak/stream/http/main.go
  • pkg/api/speak-stream/v1/interfaces/interfaces.go
  • pkg/client/speak/constants.go
Additional context used
Learnings (1)
pkg/client/speak/client_stream.go (4)
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:117-124
Timestamp: 2024-06-19T14:40:24.878Z
Learning: Connection issues in the `Connect` method of the `StreamClient` class are communicated through the callback mechanism, not through the method's return type.
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:279-279
Timestamp: 2024-06-17T23:06:01.713Z
Learning: Ignore cyclomatic complexity warnings for the `listen` method in the `StreamClient` class as per dvonthenen's request.
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:150-277
Timestamp: 2024-06-19T14:40:11.197Z
Learning: The `internalConnectWithCancel` method in `pkg/client/speak/client_stream.go` is complex and is planned to be refactored in the future. This is tracked in issue https://github.com/deepgram/deepgram-go-sdk/issues/242.
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: examples/speak/stream/http/main.go:124-127
Timestamp: 2024-06-19T14:42:20.980Z
Learning: The `Connect` method in the `pkg/client/speak/client_stream.go` returns a boolean, and any errors during the connection are handled through the `Error` method in the `MyCallback` struct.
GitHub Check: Lint
pkg/client/speak/client_stream.go

[failure] 385-385:
ifElseChain: rewrite if-else to switch statement (gocritic)


[failure] 463-463:
marshalling is a misspelling of marshaling (misspell)

Additional comments not posted (3)
pkg/client/speak/client_stream.go (3)

150-277: The complexity of internalConnectWithCancel is acknowledged and planned for future refactoring.


279-408: The complexity of the listen method is acknowledged but will be ignored as per previous discussions.

Tools
GitHub Check: Lint

[failure] 385-385:
ifElseChain: rewrite if-else to switch statement (gocritic)


617-625: The error handling in sendError is well-implemented, ensuring that all errors are communicated back to the callback handler efficiently.

pkg/client/speak/client_stream.go Outdated Show resolved Hide resolved
pkg/client/speak/client_stream.go Show resolved Hide resolved
@SandraRodgers SandraRodgers force-pushed the implement-tts-streaming branch 2 times, most recently from fcc0c11 to b4c8aed Compare June 20, 2024 19:16
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: 4

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8819005 and bf2ed42.

Files selected for processing (16)
  • examples/speak/rest/stream/hello-world/main.go (1 hunks)
  • examples/speak/stream/interactive/main.go (1 hunks)
  • pkg/api/speak-stream/v1/constants.go (1 hunks)
  • pkg/api/speak-stream/v1/default.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/constants.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/interfaces.go (1 hunks)
  • pkg/api/speak-stream/v1/interfaces/types.go (1 hunks)
  • pkg/api/speak-stream/v1/router.go (1 hunks)
  • pkg/api/version/constants.go (2 hunks)
  • pkg/api/version/speakstream-version.go (1 hunks)
  • pkg/api/version/version.go (1 hunks)
  • pkg/client/interfaces/types-speak.go (1 hunks)
  • pkg/client/interfaces/utils.go (1 hunks)
  • pkg/client/speak/client_stream.go (1 hunks)
  • pkg/client/speak/constants.go (1 hunks)
  • pkg/client/speak/types_stream.go (1 hunks)
Files skipped from review as they are similar to previous changes (12)
  • examples/speak/rest/stream/hello-world/main.go
  • pkg/api/speak-stream/v1/constants.go
  • pkg/api/speak-stream/v1/interfaces/constants.go
  • pkg/api/speak-stream/v1/interfaces/interfaces.go
  • pkg/api/speak-stream/v1/interfaces/types.go
  • pkg/api/speak-stream/v1/router.go
  • pkg/api/version/constants.go
  • pkg/api/version/speakstream-version.go
  • pkg/api/version/version.go
  • pkg/client/interfaces/utils.go
  • pkg/client/speak/constants.go
  • pkg/client/speak/types_stream.go
Additional context used
Learnings (3)
examples/speak/stream/interactive/main.go (1)
User: 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.
pkg/api/speak-stream/v1/default.go (1)
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/api/speak-stream/v1/default.go:187-211
Timestamp: 2024-06-17T23:10:23.450Z
Learning: The `UnhandledEvent` method in the `DefaultCallbackHandler` from the `pkg/api/speak-stream/v1/default.go` file is intended for demonstration purposes only and not used in production environments.
pkg/client/speak/client_stream.go (4)
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:117-124
Timestamp: 2024-06-19T14:40:24.878Z
Learning: Connection issues in the `Connect` method of the `StreamClient` class are communicated through the callback mechanism, not through the method's return type.
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:279-279
Timestamp: 2024-06-17T23:06:01.713Z
Learning: Ignore cyclomatic complexity warnings for the `listen` method in the `StreamClient` class as per dvonthenen's request.
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: pkg/client/speak/client_stream.go:150-277
Timestamp: 2024-06-19T14:40:11.197Z
Learning: The `internalConnectWithCancel` method in `pkg/client/speak/client_stream.go` is complex and is planned to be refactored in the future. This is tracked in issue https://github.com/deepgram/deepgram-go-sdk/issues/242.
User: dvonthenen
PR: deepgram/deepgram-go-sdk#239
File: examples/speak/stream/http/main.go:124-127
Timestamp: 2024-06-19T14:42:20.980Z
Learning: The `Connect` method in the `pkg/client/speak/client_stream.go` returns a boolean, and any errors during the connection are handled through the `Error` method in the `MyCallback` struct.
GitHub Check: Lint
pkg/client/speak/client_stream.go

[failure] 385-385:
ifElseChain: rewrite if-else to switch statement (gocritic)


[failure] 279-279:
cyclomatic complexity 22 of func (*StreamClient).listen is high (> 20) (gocyclo)

Additional comments not posted (6)
pkg/client/interfaces/types-speak.go (1)

20-21: The addition of Callback and CallbackMethod to the SpeakOptions struct is a good enhancement for customizing callback behaviors in TTS operations.

examples/speak/stream/interactive/main.go (3)

13-16: The import statements are correctly organized and the new import msginterfaces is appropriately used to access message interface types specific to the speak-stream API.


28-42: The callback methods are well-implemented, handling different types of responses and events effectively. The error handling within these methods is robust, ensuring that any issues during the callback operations are properly logged and handled.

Also applies to: 44-69, 71-86, 88-91


93-178: The main function sets up and demonstrates a typical usage scenario of the SDK. It includes initialization, context setup, user interaction, and proper cleanup. The structure and flow of the function are clear, making it a good example for users.

pkg/client/speak/client_stream.go (2)

385-402: Convert if-else chain to a switch statement for clarity.
[REFACTOR_SUGGESTion]
The if-else chain in the listen method could be more cleanly expressed using a switch statement. This would make the code more readable and maintainable.

- if msgType == websocket.TextMessage {
-     ...
- } else if msgType == websocket.BinaryMessage {
-     ...
- } else {
-     ...
- }
+ switch msgType {
+ case websocket.TextMessage:
+     ...
+ case websocket.BinaryMessage:
+     ...
+ default:
+     ...
+ }
Tools
GitHub Check: Lint

[failure] 385-385:
ifElseChain: rewrite if-else to switch statement (gocritic)


117-124: Review the use of a boolean return type for the Connect method.

The Connect method returns a boolean which might not convey enough information about the connection status. Consider using more expressive types or structures that can provide detailed error information or connection status.

Verification successful

The Connect method's boolean return value is used in various files to determine the success or failure of the connection. The handling is uniform, with a check on the boolean value followed by error messages or program termination if the connection fails. This indicates that the boolean return type is interpreted correctly across the codebase.

  • tests/edge_cases/timeout/main.go
  • tests/edge_cases/keepalive/main.go
  • tests/edge_cases/failed_retry/main.go
  • tests/edge_cases/cancel/main.go
  • examples/streaming/test/main.go
  • examples/streaming/replay/main.go
  • examples/streaming/http/main.go
  • examples/streaming/microphone/main.go
  • examples/speak/stream/interactive/main.go

These files handle the boolean result appropriately by checking if the connection was successful and taking necessary actions (e.g., logging an error, exiting the program) if it was not.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check if there are any places in the code where the boolean return from `Connect` might be misinterpreted or lead to issues.

# Test: Search for usages of `Connect` method. Expect: Proper handling of the boolean result.
rg --type go $'\\.Connect\\('

Length of output: 1476



Script:

#!/bin/bash
# Description: Check the context around the usage of `Connect` method to see how its boolean return value is being handled.

# Test: Extract lines around the usage of `Connect` method to understand the handling of its boolean return type.
rg --type go -A 5 -B 5 $'\\.Connect\\('

Length of output: 11868

pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/client/speak/client_stream.go Show resolved Hide resolved
pkg/api/speak-stream/v1/default.go Show resolved Hide resolved
@dvonthenen
Copy link
Contributor

This is ready to go, but we need to merge this PR #245 and cut a release before merging this one.

@dvonthenen
Copy link
Contributor

dvonthenen commented Jun 21, 2024

Hold this for right now, but I think we are in the clear.

I am going to need to port/code some things over from these PRs: #243 and #245

Will do that when tackling this:
#241

We should be good for the release then.

@dvonthenen dvonthenen merged commit d56e144 into deepgram:main Jun 21, 2024
2 checks passed
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