Skip to content

Commit

Permalink
fix: ActionBarSpeak / StopSpeaking handlers (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonom authored Oct 10, 2024
1 parent 31702b2 commit 708d027
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/react/src/api/MessageRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export type MessageRuntime = {
getState(): MessageState;
reload(): void;
speak(): void;
stopSpeaking(): void;
submitFeedback({ type }: { type: "positive" | "negative" }): void;
switchToBranch({
position,
Expand Down
35 changes: 10 additions & 25 deletions packages/react/src/primitive-hooks/actionBar/useActionBarSpeak.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import { useCallback } from "react";

import { useCombinedStore } from "../../utils/combined/useCombinedStore";
import {
useEditComposerStore,
useMessageRuntime,
useMessageStore,
useMessageUtilsStore,
} from "../../context";
import { useMessage, useMessageRuntime } from "../../context";

export const useActionBarSpeak = () => {
const messageStore = useMessageStore();
const editComposerStore = useEditComposerStore();
const messageRunime = useMessageRuntime();
const messageUtilsStore = useMessageUtilsStore();

const hasSpeakableContent = useCombinedStore(
[messageStore, editComposerStore],
(message, c) => {
return (
!c.isEditing &&
(message.role !== "assistant" || message.status.type !== "running") &&
message.content.some((c) => c.type === "text" && c.text.length > 0)
);
},
);

const callback = useCallback(async () => {
const utt = messageRunime.speak();
messageUtilsStore.getState().addUtterance(utt);
}, [messageRunime, messageUtilsStore]);
messageRunime.speak();
}, [messageRunime]);

const hasSpeakableContent = useMessage((m) => {
return (
(m.role !== "assistant" || m.status.type !== "running") &&
m.content.some((c) => c.type === "text" && c.text.length > 0)
);
});

if (!hasSpeakableContent) return null;
return callback;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { useCallback } from "react";
import {
useMessageUtils,
useMessageUtilsStore,
useMessage,
useMessageRuntime,
} from "../../context/react/MessageContext";

export const useActionBarStopSpeaking = () => {
const messageUtilsStore = useMessageUtilsStore();
const isSpeaking = useMessageUtils((u) => u.isSpeaking);
const messageRuntime = useMessageRuntime();
const isSpeaking = useMessage((u) => u.speech != null);

const callback = useCallback(async () => {
messageUtilsStore.getState().stopSpeaking();
}, [messageUtilsStore]);
messageRuntime.stopSpeaking();
}, [messageRuntime]);

if (!isSpeaking) return null;

Expand Down

0 comments on commit 708d027

Please sign in to comment.