5.0.2 • Published 8 months ago

@charisma-ai/react v5.0.2

Weekly downloads
80
License
MIT
Repository
github
Last release
8 months ago

Charisma for React

Charisma.ai chat component for React.

pnpm i @charisma-ai/react

This package is a react wrapper for the Charisma AI Javascript SDK. It provides a simple way to embed a chat in your React app along with accessing conversation data. It also handles audio playback and speech to text.

Setup in Charisma

After you have signed up at Charisma.ai, and logged in, make sure you have a valid licence (a free trial licence is available).

Then, on your My Stories page, click to Create a new game story.

In your story add a subplot, and a few character nodes, and player nodes.

If using a Pro story, you will need the startGraphReferenceId which is a uuid. From the story editor left navigation bar, click ... next to your subplot name, select `Edit details, and copy the reference id. If you are using a Web comic story, the story will start at the first scene automatically.

On the story overview page scroll down to Play API Access and generate and/or copy the API key. This will enable access to start playthroughs of your story.

At the top of the navbar on the left, underneath the title, your version number, or 'draft' will be displayed. You can use -1 for your most recent draft, undefined for the promoted (usually most recent) version release, or the version number for an exact version of your story.

Finally the story ID can be found in the url, eg. https://charisma.ai/stories/12345 gives you ID 12345.

Try the Demo App

From the root of this project, run pnpm install and pnpm build to install dependencies in node_modules, and create the dist directory with the compiled package respectively.

Navigate to ./demos/chat and run pnpm install to install the demos dependencies. You can now start the demo app which has a form to input your story parameters. You can launch the demo app by running pnpm run dev.

In your browser, go to localhost:3000. Once the page loads, enter your conversation parameters, and click the Submit button. This opens the chat, where you can click start, and would expect to see the first character nodes from your story start to appear on the page.

SDK Documentation

The best way to get started with this SDK is to use the demo project located in demos/chat.

Playthrough

The <Playthrough> component handles the connection to charisma.ai. This component is a context provider so anything inside has access to the usePlaythroughContext hook.

Props

PropTypeDescription
playthroughTokenstring or undefinedIf your story is not published, this is required.
autoconnectbooleanIf true, the component will automatically connect to charisma.ai
onConnectionStatus(connectionStatus: ConnectionStatus) => voidIf provided, this function will be called when the connection status changes.
onError(error: any) => voidIf provided, this function will be called when an error occurs.
onProblem(problem: { code: string, error: string}) => voidIf provided, this function will be called when a problem occurs.

usePlaythroughContext

Hook that provides access to the Playthrough context.

const { connectionStatus, playthrough, playthroughToken, playerSessionId } = usePlaythroughContext();

Conversation

The <Conversation> component handles the conversation. This component is a context provider so anything inside has access to the useConversationContext hook.

Props

PropTypeDescription
conversationUuidstring or undefinedReferences the current conversation. This is returned from createConversation function.
onMessage(event: MessageEvent) => Promise<void> | voidIf provided, this function will be called when a message is received.
onStartTyping(event: StartTypingEvent) => voidIf provided, this function will be called when the character starts typing.
onStopTyping(event: StopTypingEvent) => voidIf provided, this function will be called when the character stops typing.
onProblem(event: ProblemEvent) => voidIf provided, this function will be called when a problem occurs.
onStart(event: StartEvent) => voidIf provided, this function will be called when the conversation starts.
onReply(event: ReplyEvent) => voidIf provided, this function will be called when a reply is received from the player.
onResume() => voidIf provided, this function will be called when the conversation resumes.
onTap() => voidIf provided, this function will be called when the player taps to continue the conversation.
onAction(event: ActionEvent) => voidIf provided, this function will be called when an action node is executed.
initialStateConversationStateIf provided, this will be used as the initial state of the conversation.
onStateChange(newState: ConversationState) => voidIf provided, this function will be called when the conversation state changes.
shouldResumeOnConnectbooleanIf true, when the conversation reconnects from a disconnection event, it will resume automatically.
shouldStartOnConnectbooleanIf true, when the conversation reconnects from a disconnection event, it will start from the beginning automatically.
speechConfigSpeechConfigDefines the parameters for character speech. Required if you want character speech audio.

useConversationContext

Hook that provides access to the Conversation context.

const {isRestarting, isTyping, messages, mode, start, reply, tap, action, resume, restart} = useConversationContext()

Values

ValueTypeDescription
isRestartingbooleanIs true when restart is called.
isTypingbooleanIs true when the character is typing.
messagesStoredMessage[]Contains all the messages for the current conversation.
modeChatModeDefines the current chat mode.
start(event?: StartEvent) => voidStarts the conversation
reply(event: ReplyEvent) => voidSends a reply to the conversation from the player.
tap() => voidTaps to continue the conversation.
action(event: ActionEvent) => voidExecutes an action node.
resume() => voidResumes the conversation.
restart(eventId: string) => Promise<void>Restarts the conversation from the specified event.

AudioManagerProvider

Provides audio. This component is a context provider and must wrap <Playthrough> and <Conversation> on order to be setup correctly. See the chat demo for a suggested structure.

<AudioManagerProvider options={options}>
...
</AudioManagerProvider>

Options

OptionTypeDefaultDescription
duckVolumeLevelnumber0Volume level when ducking (0 to 1)
normalVolumeLevelnumber1Regular volume level (0 to 1)
sttService"charisma/deepgram" or "browser""charisma/deepgram"Speech-to-text service to use (see below).
streamTimeslicenumber100The number of milliseconds to record into each Blob. See https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/start#timeslice
handleError(error: string) => voidconsole.error(error)Callback to handle errors.
handleDisconnect(message: string) => voidconsole.error(message)Callback to handle when the transcription service disconnects.
handleConnect(message: string) => voidconsole.log(message)Callback to handle when the transcription service connects.

useAudioManager

Hook that provides access to the AudioManagerProvider context.

Values

ValueTypeDescription
isListeningbooleanIs true when microphone is listening for STT.
isBrowserSupportedbooleanIs true when the current browser supports local STT.
initialise() => voidInitialises the AudioManager. This must be called via a user interaction, such as a button click. See the chat demo for an example.
transcriptstringThe current transcript from STT.
recordingStatusRecordingStatusThe current recording status. Can be "OFF", "STARTING" or "RECORDING".
startListening() => voidStarts listening for STT.
stopListening() => voidStops listening for STT.
connect(token: string, playerSessionId: string) => voidConnects to the AudioManager. See StartButton.tsx in the chat demo for an example.
resetTimeout(timeout: number) => voidResets the STT timeout before the microphone automatically stops listening for STT.
playOutput(audio: ArrayBuffer, playOptions: AudioOutputsServicePlayOptions) => Promise<void> or undefinedPlays character speech from a MessageEvent.
setOutputVolume(volume: number) => voidSets the volume of character speech. Must be between 0 and 1.
playMediaAudio(audioTracks: AudioTrack[]) => voidPlays media audio from a MessageEvent. This is used for playing background music.
setMediaVolume(volume: number) => voidSets the volume of media audio. Must be between 0 and 1.
toggleMediaMute() => voidToggles the mute state of media audio.
stopAllMedia() => voidStops all media audio.

createPlaythroughToken

Use this to set up a new playthrough.

  • storyId (number): The id of the story that you want to create a new playthrough for. The story must be published, unless a Charisma.ai user token has been passed and the user matches the owner of the story.
  • version (number, optional): The version of the story that you want to create a new playthrough for. If omitted, it will default to the most recent published version. To get the draft version of a story, pass -1 and an apiKey.
  • apiKey (string, optional): To access draft, test or unpublished versions of your story, pass an apiKey. The API key can be found on the story overview page.
  • languageCode (string, optional): To play a story in a language other than English (en, the default), pass a BCP-47 languageCode. For example, to play in Italian, use it.

Returns a promise that resolves with the token.

const { token } = await createPlaythroughToken({
  storyId: 12,
  version: 4,
  apiKey: '...',
  languageCode: 'en',
});

createConversation

A playthrough can have many simultaneous conversations. In order to start interacting, a conversation needs to be created, which can then be joined.

  • playthroughToken (string): The token generated with createPlaythroughToken.

Note: The @charisma-ai/sdk package is a peer dependency of @charisma-ai/react all of its exports are re-exported in this package. View the SDK documentation for more information.

5.0.2

8 months ago

5.0.0-beta.3

9 months ago

5.0.1

9 months ago

5.0.0

9 months ago

5.0.0-beta.2

9 months ago

5.0.0-beta.1

9 months ago

4.0.3

1 year ago

4.0.3-alpha.2

1 year ago

4.0.3-alpha

1 year ago

4.0.2-alpha

1 year ago

4.0.1

1 year ago

4.0.0-alpha.5

2 years ago

4.0.0-alpha.4

2 years ago

4.0.0

2 years ago

4.0.0-alpha.3

2 years ago

4.0.0-alpha.2

2 years ago

4.0.0-alpha.1

3 years ago

3.5.0

3 years ago

3.4.0

3 years ago

3.4.2

3 years ago

3.4.1

3 years ago

3.3.0

4 years ago

3.3.0-alpha.1

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.1.0-alpha.1

4 years ago

3.0.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

2.0.0-alpha.10

4 years ago

2.0.0-alpha.8

4 years ago

2.0.0-alpha.9

4 years ago

2.0.0-alpha.7

5 years ago

2.0.0-alpha.6

5 years ago

2.0.0-alpha.5

5 years ago

2.0.0-alpha.3

5 years ago

2.0.0-alpha.4

5 years ago

2.0.0-alpha.2

5 years ago

2.0.0-alpha.1

5 years ago

1.10.0-alpha.1

5 years ago

1.10.0-alpha.2

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.8.0-alpha.1

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.0

6 years ago

1.6.0-alpha.1

6 years ago

1.5.0

6 years ago

1.5.0-alpha.3

6 years ago

1.5.0-alpha.2

6 years ago

1.5.0-alpha.1

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.3.0-alpha.1

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.2.0-alpha.4

6 years ago

1.2.0-alpha.3

6 years ago

1.2.0-alpha.2

6 years ago

1.2.0-alpha.1

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-alpha.16

6 years ago

1.0.0-alpha.15

6 years ago

1.0.0-alpha.14

6 years ago

1.0.0-alpha.13

6 years ago

1.0.0-alpha.12

6 years ago

1.0.0-alpha.11

6 years ago

1.0.0-alpha.10

6 years ago

1.0.0-alpha.9

6 years ago

1.0.0-alpha.8

6 years ago

1.0.0-alpha.7

6 years ago

1.0.0-alpha.6

6 years ago

1.0.0-alpha.5

6 years ago

1.0.0-alpha.4

6 years ago

1.0.0-alpha.3

6 years ago

1.0.0-alpha.2

6 years ago

1.0.0-alpha.1

6 years ago

0.9.2

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.0

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.5.0-next.6

6 years ago

0.5.0-next.5

6 years ago

0.5.0-next.4

6 years ago

0.5.0-next.3

6 years ago

0.5.0-next.2

6 years ago

0.5.0-next.1

6 years ago

0.5.0-next.0

6 years ago

0.4.0

6 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago