Coze JS SDKs

repository·main·Indexed 43 days ago

https://github.com/coze-dev/coze-js

A monorepo of SDKs for the Coze platform supporting Node.js, Web, and Mini Program environments (Taro, UniApp), as well as Realtime APIs. Includes examples for streaming chat, real-time voice interaction, transcription, text-to-speech, and various OAuth authentication flows (Device Code, JWT, PKCE, and Web).

Tokens
59.7K
Snippets
195
Records
275
Agent score
53%

What's inside Coze JS

  1. Overview of Coze SDK Packages

    main

    The coze-js monorepo provides several specialized SDKs for interacting with the Coze platform across different environments:

    • @coze/api: The core Coze API SDK for general purpose use.
    • @coze/realtime-api: SDK specifically for Realtime API capabilities.
    • @coze/taro-api: SDK designed for Taro Mini Program environments.
    • @coze/uniapp-api: SDK designed for UniApp Mini Program environments.
  2. Overview of Coze JS Packages

    main

    The coze-js monorepo provides several SDKs for interacting with Coze services across different environments:

    • @coze/api: The core Coze API SDK.
    • @coze/realtime-api: SDK for real-time voice capabilities.
    • @coze/taro-api: Coze API SDK specifically for Taro mini-programs.
    • @coze/uniapp-api: Coze API SDK specifically for UniApp mini-programs.
  3. Overview of Coze JS Web Example

    main

    The coze-js-web example is a React-based web demonstration that showcases the capabilities of the Coze API. It is designed to help developers understand how to implement advanced interaction patterns in a web environment.

    Key Features:

    • Streaming Chat Interface: Supports real-time streaming responses for chat interactions.
    • Voice Interaction: Demonstrates how to implement voice-based capabilities.
    • File Uploads: Shows how to handle file uploads within the Coze ecosystem.
    • Authentication: Supports both OAuth and Personal Access Token (PAT) authentication methods.
  4. Features of the Coze JS Web Demo

    main

    The demo showcases the following Coze API integrations:

    • Chat interface: Supports streaming responses for real-time interaction.
    • Voice interaction: Demonstrates voice-based capabilities.
    • File upload: Shows how to handle file attachments within the chat context.
    • Authentication: Supports both OAuth and Personal Access Token (PAT) flows.
  5. Overview of Coze.js UniApp functional modules

    main

    The example project is organized into several functional modules that demonstrate Coze API usage:

    • Real-time Voice Chat: Uses WebSocket for bidirectional voice dialogue, including microphone/audio mute controls and server/client endpoint detection (判停).
    • Real-time Transcription: Converts speech to text in real-time with pause/resume capabilities.
    • Text-to-Speech (TTS): Converts input text to natural speech with playback controls and speed adjustment.
    • API Examples: Demonstrates basic Coze API calls such as streaming/non-streaming chat, Workflow execution, and file uploads.

    Core Composables (Hooks)

    Logic is encapsulated in src/composables/:

    • use-chat.ts: Chat functionality.
    • use-speech.ts: Text-to-speech.
    • use-transcription.ts: Speech-to-text.
    • use-voice-chat.ts: Voice chat logic.
  6. Understand the Fact Check Extension architecture

    main

    The extension is organized into several functional layers:

    • src/popup/: The user interface for the extension popup, built with React and Ant Design.
    • src/background/: The background service worker that handles extension-level logic.
    • src/content/: Content scripts injected into web pages to enable features like right-click verification.
    • public/manifest.json: The Chrome extension manifest defining permissions and structure.
    • public/icons/: Extension icons.

    Permissions used:

    • activeTab, storage, contextMenus, identity.
    • Content scripts run on <all_urls>.
    • Host permissions: https://*/* and http://*/*.
  7. How WebSocket chat works

    main

    For low-latency, bidirectional communication, use client.websockets.chat.create. This provides a standard WebSocket interface where you can send CHAT_UPDATE events to configure the session and CONVERSATION_MESSAGE_CREATE events to send user messages. You can listen for CONVERSATION_MESSAGE_DELTA for real-time text streaming and CONVERSATION_CHAT_COMPLETED when the session ends.

    import { CozeAPI, RoleType, WebsocketsEventType } from '@coze/api';
    
    async function wsChat() {
      const ws = await client.websockets.chat.create('your_bot_id');
    
      ws.onopen = () => {
        // Configure chat
        ws.send({
          id: 'event_id',
          event_type: WebsocketsEventType.CHAT_UPDATE,
          data: {
            chat_config: {
              auto_save_history: true,
              user_id: 'uuid',
              meta_data: {},
              custom_variables: {},
              extra_params: {},
            },
          },
        });
    
        // Send message
        ws.send({
          id: 'event_id',
          event_type: WebsocketsEventType.CONVERSATION_MESSAGE_CREATE,
          data: {
            role: RoleType.User,
            content: 'tell me a joke',
            content_type: 'text',
          },
        });
      };
    
      ws.onmessage = (data, event) => {
        if (data.event_type === WebsocketsEventType.ERROR) {
          // Handle error codes like 4100 (Unauthorized) or 4101 (Forbidden)
          ws.close();
          return;
        }
    
        if (data.event_type === WebsocketsEventType.CONVERSATION_MESSAGE_DELTA) {
          console.log('on message delta', data.data);
        } else if (data.event_type === WebsocketsEventType.CONVERSATION_CHAT_COMPLETED) {
          console.log('on chat completed', data.data);
        }
      };
    }
  8. How Coze Simultaneous Interpretation works

    main

    Coze Simultaneous Interpretation is a real-time audio translation service based on WebRTC. It operates using two distinct roles:

    1. Publishing Side: Uses the RealtimeClient class to configure and initiate translation tasks.
    2. Subscribing Side: Uses the WebLiveClient class to listen to either the original or the translated audio streams.

    This allows for real-time speech-to-speech translation, suitable for multilingual meetings and cross-language live streaming.