0.4.1 • Published 5 months ago

@pipecat-ai/client-react v0.4.1

Weekly downloads
-
License
BSD-2-Clause
Repository
github
Last release
5 months ago

Docs NPM Version

Install

npm install @pipecat-ai/client-js @pipecat-ai/client-react

Quick Start

Instantiate an RTVIClient instance and pass it down to the RTVIClientProvider. Render the <RTVIClientAudio> component to have audio output setup automatically.

import { RTVIClient } from "@pipecat-ai/client-js";
import { RTVIClientAudio, RTVIClientProvider } from "@pipecat-ai/client-react";

const client = new RTVIClient({
  baseUrl: "https://rtvi.pipecat.bot",
  enableMic: true,
});

render(
  <RTVIClientProvider client={client}>
    <MyApp />
    <RTVIClientAudio />
  </RTVIClientProvider>
);

We recommend starting the voiceClient from a click of a button, so here's a minimal implementation of <MyApp> to get started:

import { useRTVIClient } from "@pipecat-ai/client-react";

const MyApp = () => {
  const client = useRTVIClient();
  return <button onClick={() => client.start()}>OK Computer</button>;
};

Components

RTVIClientProvider

The root component for providing RTVI client context to your application.

Props

  • client (RTVIClient, required): A singleton instance of RTVIClient.
<RTVIClientProvider client={rtviClient}>
  {/* Child components */}
</RTVIClientProvider>

RTVIClientAudio

Creates a new <audio> element that mounts the bot's audio track.

Props

No props

<RTVIClientAudio />

RTVIClientVideo

Creates a new <video> element that renders either the bot or local participant's video track.

Props

  • participant ("local" | "bot"): Defines which participant's video track is rendered
  • fit ("contain" | "cover", optional): Defines whether the video should be fully contained or cover the box. Default: 'contain'.
  • mirror (boolean, optional): Forces the video to be mirrored, if set.
  • onResize(dimensions: object) (function, optional): Triggered whenever the video's rendered width or height changes. Returns the video's native width, height and aspectRatio.
<RTVIClientVideo
  participant="local"
  fit="cover"
  mirror
  onResize={({ aspectRatio, height, width }) => {
    console.log("Video dimensions changed:", { aspectRatio, height, width });
  }}
/>

RTVIClientCamToggle

This is a stateful headless component and exposes the user's camEnabled state and an onClick handler to toggle the state.

Props

  • onCamEnabledChanged(enabled: boolean) (function, optional): Triggered when the user's camEnabled state changes
  • disabled (boolean, optional): Disables the cam toggle
<RTVIClientCamToggle>
  {({ disabled, isCamEnabled, onClick }) => (
    <button disabled={disabled} onClick={onClick}>
      {isCamEnabled ? "Turn off" : "Turn on"} camera
    </button>
  )}
</RTVIClientCamToggle>

RTVIClientMicToggle

This is a stateful headless component and exposes the user's micEnabled state and an onClick handler to toggle the state.

Props

  • onMicEnabledChanged(enabled: boolean) (function, optional): Triggered when the user's micEnabled state changes
  • disabled (boolean, optional): Disables the mic toggle
<RTVIClientMicToggle>
  {({ disabled, isMicEnabled, onClick }) => (
    <button disabled={disabled} onClick={onClick}>
      {isMicEnabled ? "Mute" : "Unmute"} microphone
    </button>
  )}
</RTVIClientMicToggle>

VoiceVisualizer

Renders a visual representation of audio input levels on a <canvas> element. The visualization consists of vertical bars.

Props

  • participantType (string, required): The participant type to visualize audio for.
  • backgroundColor (string, optional): The background color of the canvas. Default: 'transparent'.
  • barColor (string, optional): The color of the audio level bars. Default: 'black'.
  • barCount (number, optional): The amount of bars to render. Default: 5
  • barGap (number, optional): The gap between bars in pixels. Default: 12.
  • barLineCap ('round' | 'square', optional): The line cap for each bar. Default: 'round'
  • barOrigin ('bottom' | 'center' | 'top', optional): The origin from where the bars grow to full height. Default: 'center'
  • barWidth (number, optional): The width of each bar in pixels. Default: 30.
  • barMaxHeight (number, optional): The maximum height at full volume of each bar in pixels. Default: 120.
<VoiceVisualizer
  participantType="local"
  backgroundColor="white"
  barColor="black"
  barGap={1}
  barWidth={4}
  barMaxHeight={24}
/>

Hooks

useRTVIClient

Provides access to the RTVIClient instance originally passed to RTVIClientProvider.

import { useRTVIClient } from "@pipecat-ai/client-react";

function MyComponent() {
  const rtviClient = useRTVIClient();
}

useRTVIClientEvent

Allows subscribing to RTVI client events. It is advised to wrap handlers with useCallback.

Arguments

  • event (RTVIEvent, required)
  • handler (function, required)
import { useCallback } from "react";
import { RTVIEvent, TransportState } from "@pipecat-ai/client-js";
import { useRTVIClientEvent } from "@pipecat-ai/client-react";

function EventListener() {
  useRTVIClientEvent(
    RTVIEvent.TransportStateChanged,
    useCallback((transportState: TransportState) => {
      console.log("Transport state changed to", transportState);
    }, [])
  );
}

useRTVIClientCamControl

Allows to control the user's camera state.

import { useRTVIClientCamControl } from "@pipecat-ai/client-react";

function CustomCamToggle() {
  const { enableCam, isCamEnabled } = useRTVIClientCamControl();
}

useRTVIClientMicControl

Allows to control the user's microphone state.

import { useRTVIClientMicControl } from "@pipecat-ai/client-react";

function CustomMicToggle() {
  const { enableMic, isMicEnabled } = useRTVIClientMicControl();
}

useRTVIClientMediaDevices

Manage and list available media devices.

import { useRTVIClientMediaDevices } from "@pipecat-ai/client-react";

function DeviceSelector() {
  const {
    availableCams,
    availableMics,
    selectedCam,
    selectedMic,
    updateCam,
    updateMic,
  } = useRTVIClientMediaDevices();

  return (
    <>
      <select
        name="cam"
        onChange={(ev) => updateCam(ev.target.value)}
        value={selectedCam?.deviceId}
      >
        {availableCams.map((cam) => (
          <option key={cam.deviceId} value={cam.deviceId}>
            {cam.label}
          </option>
        ))}
      </select>
      <select
        name="mic"
        onChange={(ev) => updateMic(ev.target.value)}
        value={selectedMic?.deviceId}
      >
        {availableMics.map((mic) => (
          <option key={mic.deviceId} value={mic.deviceId}>
            {mic.label}
          </option>
        ))}
      </select>
    </>
  );
}

useRTVIClientMediaTrack

Access audio and video tracks.

Arguments

  • trackType ("audio" | "video", required)
  • participantType ("bot" | "local", required)
import { useRTVIClientMediaTrack } from "@pipecat-ai/client-react";

function MyTracks() {
  const localAudioTrack = useRTVIClientMediaTrack("audio", "local");
  const botAudioTrack = useRTVIClientMediaTrack("audio", "bot");
}

useRTVIClientTransportState

Returns the current transport state.

import { useRTVIClientTransportState } from "@pipecat-ai/client-react";

function ConnectionStatus() {
  const transportState = useRTVIClientTransportState();
}
0.4.1

5 months ago

0.4.0

5 months ago

0.3.5

7 months ago

0.3.4

8 months ago

0.3.3

8 months ago

0.3.2

10 months ago

0.3.2-rc.2

10 months ago

0.3.2-rc.1

10 months ago

0.3.1

11 months ago

0.3.0

11 months ago

0.3.0-rc.3

11 months ago

0.3.0-rc.2

11 months ago

0.3.0-rc.1

11 months ago