Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Nov 7, 2024
1 parent 0037b0c commit c526ff8
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions app/components/realtime-chat/realtime-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function RealtimeChat({
const chatStore = useChatStore();
const session = chatStore.currentSession();

const [status, setStatus] = useState("");
const [isRecording, setIsRecording] = useState(false);
const [isConnected, setIsConnected] = useState(false);
const [isConnecting, setIsConnecting] = useState(false);
Expand Down Expand Up @@ -94,15 +95,22 @@ export function RealtimeChat({
await clientRef.current.sendItem({
type: "message",
role: role as any,
content: [{ type: "input_text", text: content as string }],
content: [
{
type: (role === "assistant" ? "text" : "input_text") as any,
text: content as string,
},
],
});
}
}
} catch (error) {
console.error("Set message failed:", error);
setStatus("Set message failed");
}
} catch (error) {
console.error("Connection failed:", error);
setStatus("Connection failed");
} finally {
setIsConnecting(false);
}
Expand Down Expand Up @@ -256,7 +264,10 @@ export function RealtimeChat({
await toggleRecording();
};

initAudioHandler().catch(console.error);
initAudioHandler().catch((error) => {
setStatus(error);
console.error(error);
});

return () => {
if (isRecording) {
Expand All @@ -272,9 +283,9 @@ export function RealtimeChat({
const handleClose = async () => {
onClose?.();
if (isRecording) {
toggleRecording();
await toggleRecording();
}
disconnect();
disconnect().catch(console.error);
};

return (
Expand All @@ -289,13 +300,13 @@ export function RealtimeChat({
<div className={styles["bottom-icons"]}>
<div>
<IconButton
icon={!isRecording ? <VoiceOffIcon /> : <VoiceIcon />}
icon={isRecording ? <VoiceOffIcon /> : <VoiceIcon />}
onClick={toggleRecording}
disabled={!isConnected}
type={isConnecting || isConnected ? "danger" : "primary"}
type={isRecording ? "danger" : "primary"}
/>
</div>
<div className={styles["icon-center"]}></div>
<div className={styles["icon-center"]}>{status}</div>
<div>
<IconButton
icon={<PowerIcon />}
Expand Down

0 comments on commit c526ff8

Please sign in to comment.