Charisma.ai SDK for JavaScript
Usage
yarn add charisma-sdk-js
The script can be pulled straight into the browser.
Charisma.connect
Use this to connect to Charisma and set up a new playthrough.
storyId
(number
): Theid
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): Theversion
of the story that you want to create a new playthrough for. If omitted, it will default to the most recent version. To get the debug story, pass-1
.userToken
(string
, optional): If the story is unpublished, pass auserToken
to be able to access your story.
Returns a promise that resolves once the socket has connected.
const charisma = await Charisma
Events
To interact with the story, events are sent backwards and forwards along the websocket.
Events sent from client
charisma.start({ ... })
"startNodeId": 12 // Optional, default undefined "speech": true // Optional, default false
charisma.reply({ ... })
"message": "Please reply to this!" "speech": true // Optional, default false
Events received by client
charisma.on('reply', (data) => { ... })
"reply": "message": "Greetings and good day." "character": "Ted Baker" "avatar": "https://s3.charisma.ai/..." "speech": "..." // Stringified buffer "metadata": "myMetadata": "someValue" "endStory": false "path": "id": 1 "type": "edge" "id": 2 "type": "node"
charisma.on('start-typing', () => { ... })
This event has no additional data.
charisma.on('stop-typing', () => { ... })
This event has no additional data.
charisma.on('recognise', (message) => { ... })
To be used in conjunction with speech recognition (see below).
charisma.on('recognise-interim', (message) => { ... })
To be used in conjunction with speech recognition (see below).
Utils
To help you with speech-to-text and text-to-speech:
charisma.speak(data)
e.g.
charisma;
charisma.startListening()
Starts browser speech recognition (Google Chrome only). charisma
will then emit recognise-interim
(player hasn't finished speaking, this is the current best guess) and recognise
(player has finished speaking and we're confident about the result) events.
The speech recognition will automatically pause when a character is speaking via charisma.speak
.
charisma.stopListening()
Stops browser speech recognition (Google Chrome only).
Questions
For further details or any questions, feel free to get in touch with ben@charisma.ai