0.1.4 • Published 1 day ago

@humeai/voice-react v0.1.4

Weekly downloads
-
License
ISC
Repository
github
Last release
1 day ago

Overview

This is the React SDK for Hume's Empathic Voice Interface, making it easy to integrate the voice API into your own front-end application. The SDK abstracts the complexities of managing websocket connections, capturing user audio via the client's microphone, and handling the playback of the interface's audio responses.

Prerequisites

Before installing this package, please ensure your development environment meets the following requirement:

  • Node.js (v18.0.0 or higher).

To verify your Node.js version, run this command in your terminal:

node --version

If your Node.js version is below 18.0.0, update it to meet the requirement. For updating Node.js, visit Node.js' official site or use a version management tool like nvm for a more seamless upgrade process.

Installation

Add @humeai/voice-react to your project by running this command in your project directory:

npm install @humeai/voice-react

This will download and include the package in your project, making it ready for import and use within your React components.

import { VoiceProvider } from '@humeai/voice-react';

Usage

Quickstart

To use the SDK, wrap your components in the VoiceProvider, which will enable your components to access available voice methods. Here's a simple example to get you started:

import React, { useState } from 'react';
import { EmbeddedVoice } from '@humeai/voice-react';

function App() {
  const apiKey = process.env.HUME_API_KEY || '';
  const [isEmbedOpen, setIsEmbedOpen] = useState(false);

  return (
    <>
      <VoiceProvider
        auth={{ type: 'apiKey', value: apiKey }}
        hostname={process.env.HUME_VOICE_HOSTNAME || 'api.hume.ai'}
      >
        <ExampleComponent />
      </VoiceProvider>
    </>
  );
}

Configuring VoiceProvider

The table below outlines the props accepted by VoiceProvider:

PropRequiredDescription
authyesAuthentication strategy and corresponding value. Authentication is required to establish the web socket connection with Hume's Voice API. See our documentation on obtaining your API key or access token.
hostnamenoHostname of the Hume API. If not provided this value will default to "api.hume.ai".
reconnectAttemptsnoNumber of times to attempt to reconnect to the API. If not provided this value will default to 30.
debugnoEnable debug mode. If not provided this value will default to false.
configIdnoIf you have a configuration ID with voice presets, pass the config ID here.
configVersionnoIf you wish to use a specific version of your config, pass in the version ID here.
onMessagenoCallback function to invoke upon receiving a message through the web socket.
onToolCallnoCallback function to invoke upon receiving a ToolCallMessage through the web socket. It will send the string returned as a the content of a ToolResponseMessage. This is where you should add logic that handles your custom tool calls.
onClosenoCallback function to invoke upon the web socket connection being closed.
clearMessagesOnDisconnectnoBoolean which indicates whether you want to clear message history when the call ends.
messageHistoryLimitnoSet the number of messages that you wish to keep over the course of the conversation. The default value is 100.
sessionSettingsnoOptional settings where you can set custom values for the session.

Using the Voice

After you have set up your voice provider, you will be able to access various properties and methods to use the voice in your application. In any component that is a child of VoiceProvider, access these methods by importing the useVoice custom hook.

// ExampleComponent is nested within VoiceProvider
import { useVoice } from '@humeai/voice-react';

export const ExampleComponent = () => {
  const { connect } = useVoice();
}

Methods

MethodUsage
connect: () => PromiseOpens a socket connection to the voice API and initializes the microphone.
disconnect: () => voidDisconnect from the voice API and microphone.
clearMessages: () => voidClear transcript messages from history.
mute: () => voidMute the microphone
unmute: () => voidUnmute the microphone
sendSessionSettings: (text: string) => voidSend new session settings to the assistant. This overrides any session settings that were passed as props to the VoiceProvider.
sendUserInput: (text: string) => voidSend a user input message.
sendAssistantInput: (text: string) => voidSend a text string for the assistant to read out loud.
sendToolMessage: (toolMessage: ToolResponse | ToolError) => voidSend a tool response or tool error message to the EVI backend.

Properties

PropertyTypeDescription
isMutedbooleanBoolean that describes whether the microphone is muted
isPlayingbooleanDescribes whether the assistant audio is currently playing.
fftnumber[]Audio FFT values for the assistant audio output.
micFftnumber[]Audio FFT values for microphone input.
messagesUserTranscriptMessage, AssistantTranscriptMessage, ConnectionMessage, UserInterruptionMessage, or JSONErrorMessageMessage history of the current conversation.
lastVoiceMessageAssistantTranscriptMessage or nullThe last transcript message received from the assistant.
lastUserMessageUserTranscriptMessage or nullThe last transcript message received from the user.
readyStateVoiceReadyStateThe current readyState of the websocket connection.
readyStateVoiceReadyStateThe current readyState of the websocket connection.
statusVoiceStatusThe current status of the voice connection. Informs you of whether the voice is connected, disconnected, connecting, or error. If the voice is in an error state, it will automatically disconnect from the websocket and microphone.
errorVoiceErrorProvides more detailed error information if the voice is in an error state.
isErrorbooleanIf true, the voice is in an error state.
isAudioErrorbooleanIf true, an audio playback error has occurred.
isMicrophoneErrorbooleanIf true, a microphone error has occurred.
isSocketErrorbooleanIf true, there was an error connecting to the websocket.
callDurationTimestampstring or nullThe length of a call. This value persists after the conversation has ended.
toolStatusStoreRecord<string, { call?: ToolCall; resolved?: ToolResponse | ToolError }>A map of tool call IDs to their associated tool messages.

Support

If you have questions or require assistance pertaining to this package, reach out to us on Discord!

0.1.4

1 day ago

0.1.3

6 days ago

0.1.3-beta.2

8 days ago

0.1.3-beta.1

8 days ago

0.1.2-beta.3

8 days ago

0.1.3-beta.3

8 days ago

0.1.2-beta.2

9 days ago

0.1.2-beta.1

9 days ago

0.1.2

14 days ago

0.1.1

14 days ago

0.1.0

15 days ago

0.0.0-beta.23

15 days ago

0.0.0-beta.22

19 days ago

0.0.0-beta.21

21 days ago

0.0.0-beta.20

1 month ago

0.0.0-beta.19

1 month ago

0.0.0-beta.18

1 month ago

0.0.0-beta.17

1 month ago

0.0.0-beta.16

1 month ago

0.0.0-beta.15

1 month ago

0.0.0-beta.14

1 month ago

0.0.0-beta.13

1 month ago

0.0.0-beta.12

1 month ago

0.0.0-beta.11

2 months ago

0.0.0-beta.10

2 months ago

0.0.0-beta.9

2 months ago

0.0.0-beta.8

2 months ago

0.0.0-beta.7

2 months ago

0.0.0-beta.6

2 months ago

0.0.0-beta.5

2 months ago

0.0.0-beta.4

2 months ago

0.0.0-beta.3

2 months ago

0.0.0-beta.2

2 months ago

0.0.0-beta.1

2 months ago