Pipecat Client Web SDK

repository·main·Indexed 43 days ago

https://github.com/pipecat-ai/pipecat-client-web

The official web SDK for the Pipecat Python framework, providing JavaScript, TypeScript, and React tools to build multimodal AI applications. It includes the PipecatClient for media stream management and bot communication, React bindings like PipecatClientProvider and PipecatClientAudio, and implementation of the UI Worker Protocol (v1) for server-driven UI synchronization and command handling.

Tokens
19.3K
Snippets
60
Records
78
Agent score
55%

What's inside Pipecat Client Web

  1. How UI Worker Protocol (v1) works

    main

    The UI Worker Protocol (v1) allows a server-side Pipecat worker to observe and drive your React application using a structured wire format.

    Key features include:

    • useUISnapshot(): Streams accessibility snapshots so the server knows what is on the screen.
    • UIJobGroupsProvider: Subscribes to ui-job-group envelopes for long-running, fan-out tasks, providing per-worker progress and cancellation.
    • Command Handlers: Use useUICommandHandler to register custom app-specific commands (e.g., toast) and useDefaultScrollToHandler, useDefaultHighlightHandler, etc., to opt-in to standard DOM interactions driven by the server.
    • Form Controls: Follows standard semantics (e.g., set_input_value for inputs/selects, click for checkboxes/radios).
    import { PipecatClient } from "@pipecat-ai/client-js";
    import {
      PipecatClientProvider,
      UIJobGroupsProvider,
      useUISnapshot,
      useDefaultScrollToHandler,
      useDefaultHighlightHandler,
      useUICommandHandler,
    } from "@pipecat-ai/client-react";
    
    const client = new PipecatClient({ transport: myTransport });
    
    function App() {
      useUISnapshot();
      useDefaultScrollToHandler();
      useDefaultHighlightHandler();
      useUICommandHandler("toast", (payload) => showToast(payload));
      return <YourApp />;
    }
    
    render(
      <PipecatClientProvider client={client}>
        <UIJobGroupsProvider>
          <App />
        </UIJobGroupsProvider>
      </PipecatClientProvider>
    );
  2. How the UI Worker Protocol (v1) works

    main

    The UI Worker Protocol (v1) is a structured wire format that allows server-side workers (using the UIWorker class in the Python Pipecat framework) to observe and drive a GUI application.

    client-js capabilities:

    • PipecatClient: Sends ui-event messages, manages accessibility snapshot streaming via startUISnapshotStream(...), and handles RTVIEvent.UICommand / RTVIEvent.UIJobGroup subscriptions.
    • A11ySnapshotStreamer: A low-level implementation for advanced accessibility snapshot streaming.

    client-react capabilities:

    • Providers & Hooks: Provides UIJobGroupsProvider and hooks like useUIEventSender, useUICommandHandler, useUIJobGroups, and useUISnapshot.
    • DOM Defaults: Includes opt-in handlers for common UI interactions like useDefaultClickHandler, useDefaultFocusHandler, useDefaultSetInputValueHandler, etc.
  3. Quickstart: Connect to a bot using RTVI Web

    main

    To start a session, configure the PipecatClient with a transport, media preferences, and lifecycle callbacks. Use startBotAndConnect with a server-side endpoint to initiate the connection.

    Note: It is highly recommended to use your own server-side endpoints to handle authentication and protect bot process secrets (like API keys).

    import { PipecatClient, RTVIEvent, RTVIMessage } from "@pipecat-ai/client-js";
    import { DailyTransport } from "@pipecat-ai/daily-transport";
    
    const pcClient = new PipecatClient({
      transport: new DailyTransport(),
      enableMic: true,
      enableCam: false,
      callbacks: {
        onConnected: () => console.log("[CALLBACK] User connected"),
        onDisconnected: () => console.log("[CALLBACK] User disconnected"),
        onTransportStateChanged: (state: string) => console.log("[CALLBACK] State change:", state),
        onBotConnected: () => console.log("[CALLBACK] Bot connected"),
        onBotDisconnected: () => console.log("[CALLBACK] Bot disconnected"),
        onBotReady: () => console.log("[CALLBACK] Bot ready to chat!"),
      },
    });
    
    try {
      await pcClient.startBotAndConnect({ endpoint: "https://your-server-side-url/connect" });
    } catch (e) {
      console.error(e.message);
    }
    
    // Event Listeners
    pcClient.on(RTVIEvent.TransportStateChanged, (state) => {
      console.log("[EVENT] Transport state change:", state);
    });
    pcClient.on(RTVIEvent.BotReady, () => {
      console.log("[EVENT] Bot is ready");
    });
  4. Install the Pipecat Web SDK packages

    main

    The Pipecat Web client is a monorepo containing several packages. You must install the core JS SDK and a transport implementation to connect to a bot. Optionally, you can install the React library for UI components.

    • @pipecat-ai/client-js: The core JavaScript/TypeScript SDK for communication.
    • @pipecat-ai/client-react: React components and hooks.
    • Transport packages (e.g., @pipecat-ai/daily-transport): Required for media streaming (audio/video).
    # Install the core JS client
    npm install @pipecat-ai/client-js
    
    # Optionally install React support
    npm install @pipecat-ai/client-react
    
    # Install a transport layer (e.g., Daily)
    npm install @pipecat-ai/daily-transport
  5. Quick Start with PipecatClient

    main

    To start a conversation, instantiate a PipecatClient with a transport (such as DailyTransport), configure media settings, and provide lifecycle callbacks. Use startBotAndConnect with your connection endpoint to begin the session.

    import { RTVIEvent, RTVIMessage, PipecatClient } from "@pipecat-ai/client-js";
    import { DailyTransport } from "@pipecat-ai/daily-transport";
    
    const pcClient = new PipecatClient({
      transport: new DailyTransport(),
      enableMic: true,
      enableCam: false,
      callbacks: {
        onConnected: () => {
          console.log("[CALLBACK] User connected");
        },
        onDisconnected: () => {
          console.log("[CALLBACK] User disconnected");
        },
        onTransportStateChanged: (state: string) => {
          console.log("[CALLBACK] State change:", state);
        },
        onBotConnected: () => {
          console.log("[CALLBACK] Bot connected");
        },
        onBotDisconnected: () => {
          console.log("[CALLBACK] Bot disconnected");
        },
        onBotReady: () => {
          console.log("[CALLBACK] Bot ready to chat!");
        },
      },
    });
    
    try {
      await pcClient.startBotAndConnect({ endpoint: "https://your-connect-end-point-here/connect" });
    } catch (e) {
      console.error(e.message);
    }
    
    // Events
    pcClient.on(RTVIEvent.TransportStateChanged, (state) => {
      console.log("[EVENT] Transport state change:", state);
    });
    pcClient.on(RTVIEvent.BotReady, () => {
      console.log("[EVENT] Bot is ready");
    });
    pcClient.on(RTVIEvent.Connected, () => {
      console.log("[EVENT] User connected");
    });
    pcClient.on(RTVIEvent.Disconnected, () => {
      console.log("[EVENT] User disconnected");
    });
  6. Quick Start with Pipecat React

    main

    To get started, instantiate a PipecatClient from @pipecat-ai/client-js, wrap your application in the PipecatClientProvider, and include the <PipecatClientAudio /> component to handle audio output automatically. Use the usePipecatClient hook to access the client instance and trigger actions like .start().

    import { PipecatClient } from "@pipecat-ai/client-js";
    import { PipecatClientAudio, PipecatClientProvider } from "@pipecat-ai/client-react";
    
    const client = new PipecatClient({
      transport: myTransportType.create(),
    });
    
    render(
      <PipecatClientProvider client={client}>
        <MyApp />
        <PipecatClientAudio />
      </PipecatClientProvider>
    );
    
    // Inside your app component
    const MyApp = () => {
      const client = usePipecatClient();
      return <button onClick={() => client.start()}>OK Computer</button>;
    };
  7. Use Pipecat React hooks and components

    main

    The @pipecat-ai/client-react package provides a suite of React hooks and components for building real-time AI interfaces. Key capabilities include:

    • Client Management: usePipecatClient to access the core client instance.
    • Media Control: Hooks like usePipecatClientMicControl, usePipecatClientCamControl, and usePipecatClientScreenShareControl for managing audio, video, and screen sharing.
    • Conversation UI: usePipecatConversation and useConversationContext for managing and displaying chat/transcript history.
    • UI Command Handling: useUICommandHandler and useUIEventSender for interacting with the agent via UI events.
    • Pre-built Components: PipecatClientVideo, PipecatClientMicToggle, PipecatClientCamToggle, and VoiceVisualizer for common UI elements.
    • State & Events: useRTVIClientEvent for listening to client events and usePipecatClientTransportState for monitoring connection status.
  8. Handle RTVI events with PipecatClient

    main

    The PipecatClient extends RTVIEventEmitter, allowing you to subscribe to a wide range of events using .on(event, callback) or by providing a callbacks object in the constructor.

    Common Event Categories:

    • Connection: onConnected, onDisconnected, onBotReady, onBotConnected, onBotDisconnected.
    • Media/Devices: onMediaStateChanged, onDeviceError, onCamUpdated, onMicUpdated, onTrackStarted, onTrackStopped.
    • Audio/Speech: onUserStartedSpeaking, onUserStoppedSpeaking, onBotStartedSpeaking, onBotStoppedSpeaking, onUserTranscript, onBotOutput.
    • LLM/Bot Intelligence: onBotLlmText, onBotTtsText, onLLMFunctionCallStarted, onLLMFunctionCallInProgress, onLLMFunctionCallStopped.
    • UI/System: onUICommand, onUIJobGroup, onUnsupportedFeature, onMessageError.

    Example: Using Event Listeners

    client.on(RTVIEvent.BotReady, (data) => {
      console.log('Bot is ready with data:', data);
    });
    
    client.on(RTVIEvent.UserTranscript, (data) => {
      console.log('User said:', data.text);
    });
  9. How conversation event wiring works in @pipecat-ai/client-react

    main

    The useConversationEventWiring hook is an internal mechanism used by the PipecatConversationProvider to synchronize RTVI (Real-Time Voice Interface) client events with the React application state (managed via Jotai atoms).

    It automates the lifecycle of a conversation by listening to events such as BotOutput, UserTranscript, and LLMFunctionCall events, and translating them into message updates in the conversation history.

    Key behaviors include:

    • Assistant Turn Management: It uses a delayed timer (BOT_STOPPED_FINALIZE_DELAY_MS) to finalize assistant messages. This prevents premature message finalization during brief pauses in speech. The timer is reset if the bot starts speaking again.
    • Protocol Switching: It automatically detects the RTVI protocol version (e.g., v2 vs legacy) from the BotReady event to handle BotOutput data correctly (server-side vs client-side speech progress).
    • User Turn Management: It finalizes user messages when the user starts a new turn or after a period of silence following UserStoppedSpeaking.
    • Function Call Tracking: It wires LLM function call lifecycle events (LLMFunctionCallStarted, LLMFunctionCallInProgress, LLMFunctionCallStopped) to the conversation state to show tool usage in the UI.