Skip to content

Commit

Permalink
Zg/update hume sdk package version (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
zgreathouse authored May 13, 2024
1 parent 2542e2e commit 65b75dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion evi-typescript-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"hume": "^0.5.14"
"hume": "^0.5.15"
},
"devDependencies": {
"typescript": "^5.2.2",
Expand Down
8 changes: 4 additions & 4 deletions evi-typescript-example/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 23 additions & 15 deletions evi-typescript-example/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ function getElementById<T extends HTMLElement>(id: string): T | null {

/**
* captures and records audio stream, and sends audio stream through the socket
*
* API Reference:
* - `audio_input`: https://dev.hume.ai/reference/empathic-voice-interface-evi/chat/chat#send.Audio%20Input.type
*/
async function captureAudio(): Promise<void> {
audioStream = await getAudioStream();
Expand All @@ -137,16 +140,12 @@ function getElementById<T extends HTMLElement>(id: string): T | null {
const encodedAudioData = await convertBlobToBase64(data);

// define the audio_input message JSON
const audioInput: Hume.empathicVoice.AudioInput = {
type: 'audio_input',
const audioInput: Omit<Hume.empathicVoice.AudioInput, 'type'> = {
data: encodedAudioData,
};

// stringify the JSON to be sent over the socket
const json = JSON.stringify(audioInput);


// send audio_input message
socket?.sendAudioInput(json);
socket?.sendAudioInput(audioInput);
};

// capture audio input at a rate of 100ms (recommended)
Expand Down Expand Up @@ -203,26 +202,35 @@ function getElementById<T extends HTMLElement>(id: string): T | null {
}

/**
* handles WebSocket open event
* callback function to handle a WebSocket opened event
*/
async function handleWebSocketOpenEvent(): Promise<void> {
// place logic here which you would like invoked when the socket opens
/* place logic here which you would like invoked when the socket opens */
console.log('Web socket connection opened');
await captureAudio();
}

/**
* handles WebSocket message event
* callback function to handle a WebSocket message event
*
* API Reference:
* - `user_message`: https://dev.hume.ai/reference/empathic-voice-interface-evi/chat/chat#receive.User%20Message.type
* - `assistant_message`: https://dev.hume.ai/reference/empathic-voice-interface-evi/chat/chat#receive.Assistant%20Message.type
* - `audio_output`: https://dev.hume.ai/reference/empathic-voice-interface-evi/chat/chat#receive.Audio%20Output.type
* - `user_interruption`: https://dev.hume.ai/reference/empathic-voice-interface-evi/chat/chat#receive.User%20Interruption.type
*/
function handleWebSocketMessageEvent(
message: Hume.empathicVoice.SubscribeEvent
): void {
// place logic here which you would like to invoke when receiving a message through the socket
/* place logic here which you would like to invoke when receiving a message through the socket */

// handle messages received through the WebSocket (messages are distinguished by their "type" field.)
switch (message.type) {
// append user and assistant messages to UI for chat visibility
case 'user_message':
case 'assistant_message':
const { role, content } = message.message;

appendMessage(role, content ?? '');
break;

Expand All @@ -247,20 +255,20 @@ function getElementById<T extends HTMLElement>(id: string): T | null {
}

/**
* handles WebSocket error event
* callback function to handle a WebSocket error event
*/
function handleWebSocketErrorEvent(
error: Hume.empathicVoice.WebSocketError
): void {
// place logic here which you would like invoked when receiving an error through the socket
/* place logic here which you would like invoked when receiving an error through the socket */
console.error(error);
}

/**
* handles WebSocket close event
* callback function to handle a WebSocket closed event
*/
function handleWebSocketCloseEvent(): void {
// place logic here which you would like invoked when the socket closes
/* place logic here which you would like invoked when the socket closes */
console.log('Web socket connection closed');
}

Expand Down

0 comments on commit 65b75dd

Please sign in to comment.