Skip to content

Commit

Permalink
clear player queue when assistant is paused (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
yinishi authored Sep 6, 2024
1 parent 696087b commit ea5c245
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 19 deletions.
10 changes: 5 additions & 5 deletions examples/next-app/components/ExampleComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const ExampleComponent = () => {
callDurationTimestamp,
sendUserInput,
sendAssistantInput,
sendResumeAssistantMessage,
sendPauseAssistantMessage,
pauseAssistant,
resumeAssistant,
chatMetadata,
playerQueueLength,
} = useVoice();
Expand All @@ -57,13 +57,13 @@ export const ExampleComponent = () => {

const togglePaused = useCallback(() => {
if (paused) {
sendResumeAssistantMessage({});
resumeAssistant();
setPaused(false);
} else {
sendPauseAssistantMessage({});
pauseAssistant();
setPaused(true);
}
}, [paused, sendPauseAssistantMessage, sendResumeAssistantMessage]);
}, [paused, resumeAssistant, pauseAssistant]);
const pausedText = paused ? 'Resume' : 'Pause';

const assistantMessages = useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/embed-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@humeai/voice-embed-react",
"version": "0.1.12",
"version": "0.1.13",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/embed/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@humeai/voice-embed",
"version": "0.1.12",
"version": "0.1.13",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
4 changes: 2 additions & 2 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export const ExampleComponent = () => {
| `sendUserInput: (text: string) => void` | Send a user input message. |
| `sendAssistantInput: (text: string) => void` | Send a text string for the assistant to read out loud. |
| `sendToolMessage: (toolMessage: ToolResponse \| ToolError) => void` | Send a tool response or tool error message to the EVI backend. |
| `sendPauseAssistantMessage: () => void` | Send pause assistant message to the websocket. This pauses responses from EVI. Chat history is still saved and sent after resuming. |
| `sendResumeAssistantMessage: () => void` | Send resume assistant message to the websocket. This resumes responses from EVI. Chat history sent while paused will now be sent. |
| `pauseAssistant: () => void` | Pauses responses from EVI. Chat history is still saved and sent after resuming. |
| `resumeAssistant: () => void` | Resumes responses from EVI. Chat history sent while paused will now be sent. |

### Properties

Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@humeai/voice-react",
"version": "0.1.12",
"version": "0.1.13",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
27 changes: 18 additions & 9 deletions packages/react/src/lib/VoiceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export type VoiceContextType = {
| Hume.empathicVoice.ToolResponseMessage
| Hume.empathicVoice.ToolErrorMessage,
) => void;
sendPauseAssistantMessage: Hume.empathicVoice.chat.ChatSocket['pauseAssistant'];
sendResumeAssistantMessage: Hume.empathicVoice.chat.ChatSocket['resumeAssistant'];
pauseAssistant: () => void;
resumeAssistant: () => void;
status: VoiceStatus;
micFft: number[];
error: VoiceError | null;
Expand Down Expand Up @@ -247,6 +247,15 @@ export const VoiceProvider: FC<VoiceProviderProps> = ({
),
});

const pauseAssistant = useCallback(() => {
client.sendPauseAssistantMessage();
player.clearQueue();
}, [client, player]);

const resumeAssistant = useCallback(() => {
client.sendResumeAssistantMessage();
}, [client]);

const connect = useCallback(async () => {
updateError(null);
setStatus({ value: 'connecting' });
Expand Down Expand Up @@ -383,8 +392,8 @@ export const VoiceProvider: FC<VoiceProviderProps> = ({
sendUserInput: client.sendUserInput,
sendAssistantInput: client.sendAssistantInput,
sendSessionSettings: client.sendSessionSettings,
sendPauseAssistantMessage: client.sendPauseAssistantMessage,
sendResumeAssistantMessage: client.sendResumeAssistantMessage,
pauseAssistant,
resumeAssistant,
sendToolMessage: client.sendToolMessage,
status,
unmute: mic.unmute,
Expand All @@ -403,8 +412,8 @@ export const VoiceProvider: FC<VoiceProviderProps> = ({
connect,
disconnect,
player.fft,
player.isPlaying,
player.isAudioMuted,
player.isPlaying,
player.muteAudio,
player.unmuteAudio,
player.queueLength,
Expand All @@ -416,22 +425,22 @@ export const VoiceProvider: FC<VoiceProviderProps> = ({
messageStore.lastVoiceMessage,
messageStore.lastUserMessage,
messageStore.clearMessages,
messageStore.chatMetadata,
client.readyState,
client.sendUserInput,
client.sendAssistantInput,
client.sendSessionSettings,
client.sendToolMessage,
client.sendPauseAssistantMessage,
client.sendResumeAssistantMessage,
pauseAssistant,
resumeAssistant,
status,
error,
isAudioError,
isError,
isMicrophoneError,
isSocketError,
callDurationTimestamp,
toolStatus,
messageStore.chatMetadata,
toolStatus.store,
],
);

Expand Down

0 comments on commit ea5c245

Please sign in to comment.