Skip to content

Conversation

@jackshen310
Copy link
Collaborator

@jackshen310 jackshen310 commented Mar 12, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Patched speech functionality for improved stability by ensuring a single client instance and enhanced disconnection handling.
  • Documentation
    • Updated API usage examples with standardized greeting messages in both English and Chinese.
  • Chores
    • Upgraded the package version to 1.1.0-beta.4, reflecting the latest improvements.
  • New Features
    • Introduced new JSON files documenting patches for speech functionality, enhancing change tracking.
    • Added a delay in event handling for speech completion to improve timing accuracy.

@coderabbitai
Copy link

coderabbitai bot commented Mar 12, 2025

Walkthrough

This pull request introduces several updates related to the speech functionality in the @coze/api package. It includes new JSON files documenting patch fixes, enhances the management of the WsSpeechClient instance by reusing it, and refines event listener setup. The message handling has been modified to use static strings in both English and Chinese, and the disconnection process has been revised. Additionally, the package version has been updated to reflect these changes.

Changes

File(s) Summary
common/changes/@coze/api/fix-speech_2025-03-12-13-51.json, common/changes/@coze/api/fix-speech_2025-03-14-03-48.json, common/changes/@coze/api/fix-speech_2025-03-14-04-06.json, common/changes/@coze/api/fix-speech_2025-03-14-04-11.json Added new JSON files documenting patch fixes for the speech functionality bug in the @coze/api package.
examples/coze-js-web/src/pages/chat-x/use-ws-api.ts Modified the useWsAPI function: now reuses an existing WsSpeechClient instance, adds a condition to prevent duplicate instantiation, sets up event listeners (including a new 'completed' listener), and updates the disconnection method from interrupt() to disconnect().
packages/coze-js/README.md, packages/coze-js/README.zh-CN.md Updated message sending logic to use hardcoded strings instead of dynamic variables; modified the order of operations by repositioning the disconnection call after other checks; localized content changes in the Chinese README.
packages/coze-js/package.json Updated the package version from "1.1.0-beta.2" to "1.1.0-beta.4" to reflect a new beta release.
packages/coze-js/src/ws-tools/speech/index.ts Enhanced the disconnect method in the WsSpeechClient class to clear a playback timeout if set, use wavStreamPlayer.interrupt() instead of a generic interrupt method, and ensure proper WebSocket closure via closeWs().
packages/coze-js/test/ws-tools/speech.spec.ts Introduced a delay of 50 milliseconds before invoking the onmessage handler for the SPEECH_AUDIO_COMPLETED event in the WsSpeechClient class.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant useWsAPI
    participant WsSpeechClient

    User->>useWsAPI: Call startSpeech()
    alt No existing client
        useWsAPI->>WsSpeechClient: Instantiate new client
        WsSpeechClient-->>useWsAPI: Client instance created
        useWsAPI->>WsSpeechClient: Attach event listeners ('data', 'error', 'completed')
    else Existing client
        useWsAPI->>WsSpeechClient: Reuse existing client
    end
    useWsAPI->>WsSpeechClient: Call appendAndComplete()
    User->>useWsAPI: Call stopSpeech()
    useWsAPI->>WsSpeechClient: Call disconnect()
Loading
sequenceDiagram
    participant Client as WsSpeechClient
    participant Player as wavStreamPlayer
    participant WS as WebSocket

    Client->>Client: Check if playbackTimeout exists
    alt Timeout is set
        Client->>Client: Clear playbackTimeout
    end
    Client->>Player: Call interrupt() on wavStreamPlayer
    Client->>WS: Call closeWs() to close the connection
Loading

Possibly related PRs

  • feat(coze-js-web): Add text-to-speech support #97: The changes in the main PR, which document a bug fix related to speech functionality in the @coze/api package, are related to the modifications in the retrieved PR that enhance the WebSocket API for speech processing, as both involve improvements to speech-related features.

Suggested reviewers

  • DingGao-Devin

Poem

Oh, what a tweak, a hop so light,
I’m a bunny with code delight! 🐇
Static strings now pave the way,
Reused clients save the day.
With clean disconnects and bug fixes in sight,
My whiskers twitch in the soft moonlight!
Hip-hip-hooray for our smooth new flight!

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 399bd91 and 7c8b5a3.

📒 Files selected for processing (6)
  • common/changes/@coze/api/fix-speech_2025-03-14-04-06.json (1 hunks)
  • common/changes/@coze/api/fix-speech_2025-03-14-04-11.json (1 hunks)
  • packages/coze-js/README.md (3 hunks)
  • packages/coze-js/README.zh-CN.md (3 hunks)
  • packages/coze-js/package.json (1 hunks)
  • packages/coze-js/test/ws-tools/speech.spec.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • common/changes/@coze/api/fix-speech_2025-03-14-04-11.json
🚧 Files skipped from review as they are similar to previous changes (5)
  • common/changes/@coze/api/fix-speech_2025-03-14-04-06.json
  • packages/coze-js/package.json
  • packages/coze-js/README.md
  • packages/coze-js/test/ws-tools/speech.spec.ts
  • packages/coze-js/README.zh-CN.md
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Node.js v20 (ubuntu-latest)

🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

🧹 Nitpick comments (3)
examples/coze-js-web/src/pages/chat-x/use-ws-api.ts (3)

330-330: Consider removing redundant optional chaining

Since you've already checked that speechClientRef.current exists in the if condition above, the optional chaining operator here is redundant.

-  await speechClientRef.current?.connect();
+  await speechClientRef.current.connect();

337-337: Consider removing redundant optional chaining

Similar to the connect call, the optional chaining is redundant here since we've already verified the client exists.

-  speechClientRef.current?.appendAndComplete(message);
+  speechClientRef.current.appendAndComplete(message);

302-338: Consider adding cleanup for speech client

The component doesn't clean up the speech client when unmounted, which could lead to resource leaks. Consider adding a cleanup function in a useEffect hook.

+ useEffect(() => {
+   // Cleanup function to disconnect speech client when component unmounts
+   return () => {
+     if (speechClientRef.current) {
+       speechClientRef.current.disconnect();
+       speechClientRef.current = null;
+     }
+   };
+ }, []);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4f04b93 and 946325a.

📒 Files selected for processing (6)
  • common/changes/@coze/api/fix-speech_2025-03-12-13-51.json (1 hunks)
  • examples/coze-js-web/src/pages/chat-x/use-ws-api.ts (1 hunks)
  • packages/coze-js/README.md (2 hunks)
  • packages/coze-js/README.zh-CN.md (2 hunks)
  • packages/coze-js/package.json (1 hunks)
  • packages/coze-js/src/ws-tools/speech/index.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Node.js v20 (ubuntu-latest)
🔇 Additional comments (11)
packages/coze-js/package.json (1)

3-3: Version update aligns with speech functionality patch

The version bump from "1.1.0-beta.2" to "1.1.0-beta.3" correctly follows semantic versioning for a patch release that addresses the speech bug fix.

common/changes/@coze/api/fix-speech_2025-03-12-13-51.json (1)

1-11: Well-structured change log entry

The change log properly documents the speech bug fix as a patch-level update, which aligns with the version bump in package.json and the actual code changes.

packages/coze-js/src/ws-tools/speech/index.ts (1)

137-142: Improved resource cleanup in disconnect method

The enhanced disconnect method now properly clears the playbackTimeout and directly interrupts the wavStreamPlayer, which prevents potential memory leaks and ensures cleaner resource management compared to the previous implementation.

This approach is better than the previous implementation because:

  1. It properly handles the timeout cleanup
  2. It directly calls the wavStreamPlayer's interrupt method rather than going through the internal interrupt method that would emit events and reset tracking information
packages/coze-js/README.zh-CN.md (3)

217-217: Updated example with explicit Chinese text

Replacing the variable with the actual Chinese text makes the example more concrete and easier to understand.


235-237: Improved documentation structure

Moving the disconnect comment and method call to this position in the example improves the logical flow of the code example.


239-240: Clearer example with explicit text segments

Breaking the append operation into two explicit text segments better demonstrates how the append method can be used for partial text synthesis.

packages/coze-js/README.md (3)

218-218: Updated example to use static string instead of variable

The example has been updated to use a static string 'Hello, Coze!' instead of a variable message. This change aligns with the simplified examples throughout the documentation and makes the intent clearer.


236-238: Improved client lifecycle management

The README now properly demonstrates the disconnection step before the append operations, which aligns with the intended usage pattern. This change helps developers understand the correct sequence of operations when working with the speech client.


240-241: Improved example for append operations

The example now shows how to send multiple text fragments separately with append(), which better demonstrates the incremental nature of the API compared to the previous example.

examples/coze-js-web/src/pages/chat-x/use-ws-api.ts (2)

309-327: Improved client instance management

The code now reuses the existing speech client instance instead of recreating it on each call to startSpeech(). This change improves resource management and prevents potential memory leaks.

Additionally, the code now properly sets up event listeners for completed speech events, which improves the feedback mechanism.


341-342: Updated disconnection method

Changed from using interrupt() to disconnect(), which properly cleans up resources. This aligns with the changes in the documentation and ensures a complete disconnection of the client.

@jackshen310 jackshen310 changed the title Fix/speech fix(coze/api): fix speech bug Mar 14, 2025
@codecov
Copy link

codecov bot commented Mar 14, 2025

Codecov Report

Attention: Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
packages/coze-js/src/ws-tools/speech/index.ts 87.50% 2 Missing ⚠️
@@            Coverage Diff             @@
##             main     #141      +/-   ##
==========================================
- Coverage   91.82%   90.69%   -1.13%     
==========================================
  Files         117       76      -41     
  Lines        5469     3752    -1717     
  Branches     1106      684     -422     
==========================================
- Hits         5022     3403    -1619     
+ Misses        447      347     -100     
- Partials        0        2       +2     
Components Coverage Δ
coze-js 92.73% <ø> (-0.22%) ⬇️
realtime-api 90.67% <ø> (-1.19%) ⬇️
chat-sdk ∅ <ø> (∅)
Files with missing lines Coverage Δ
packages/coze-js/src/ws-tools/speech/index.ts 93.10% <87.50%> (ø)

... and 72 files with indirect coverage changes

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jackshen310 jackshen310 added this pull request to the merge queue Mar 14, 2025
Merged via the queue into main with commit c8efa55 Mar 14, 2025
13 checks passed
@jackshen310 jackshen310 deleted the fix/speech branch March 14, 2025 05:14
@coderabbitai coderabbitai bot mentioned this pull request Sep 4, 2025
@coderabbitai coderabbitai bot mentioned this pull request Oct 28, 2025
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.

3 participants