diff --git a/speech-speech/frontend/src/App.tsx b/speech-speech/frontend/src/App.tsx index b573ac6..a97cc8c 100644 --- a/speech-speech/frontend/src/App.tsx +++ b/speech-speech/frontend/src/App.tsx @@ -1,17 +1,7 @@ -import { useEffect, useRef, useState } from "react"; -import { - TbBrandOpenai, - TbMicrophone2, - TbPlayerPlay, - TbPlayerStop, -} from "react-icons/tb"; +import { useState } from "react"; +import { ChatMsg, Controls, Feed, Header } from "./components.tsx"; import "./App.css"; -type ChatMsg = { - role: string; - content: string; -}; - let audioBlobs: Array = []; let streamBeingCaptured: MediaStream | null = null; let mediaRecorder: MediaRecorder | null = null; @@ -69,59 +59,12 @@ function playMsg(msg: ChatMsg) { console.log("loading audio and playing?"); audio.play(); } -function Header() { - return ( -
-
- Speach to Speech AI example -
-
- ); -} - -function Feed(props: { chat: Array; setChatStateFn: any }) { - const bottomRef = useRef(null); - - const scrollToBottom = () => { - if (bottomRef.current) { - bottomRef.current.scrollIntoView({ behavior: "smooth" }); - } - }; - - useEffect(() => { - scrollToBottom(); - console.log("scroll?"); - }); - - return ( -
-
- {props.chat.filter((m: ChatMsg) => m.role != "system").map(( - m: ChatMsg, - i: number, - ) => )} -
-
-
- ); -} - -function Msg(props: { msg: ChatMsg }) { - return ( -
- - {props.msg.role.toUpperCase()}: - -
- - {props.msg.content} - -
- ); -} - -function Controls(props: { setChatStateFn: any, chat: Array }) { +function App() { const [recordState, setRecordState] = useState(false); + const [chatState, setChatState] = useState([{ + role: "system", + content: "You are a helpful assistant.", + }]); function toggleRecord() { if (recordState == false) { @@ -141,19 +84,19 @@ function Controls(props: { setChatStateFn: any, chat: Array }) { "body": formData, }).then((res) => res.json()) .then((res) => { - props.setChatStateFn((curState: Array) => [ + setChatState((curState: Array) => [ ...curState, { "role": "user", "content": res["user-transcript"] }, ]); fetch("http://100.82.51.22:8001/conversation", { "method": "POST", - "body": JSON.stringify([...props.chat, { + "body": JSON.stringify([...chatState, { "role": "user", "content": res["user-transcript"], }]), }).then((res) => res.json()) .then((res) => { - props.setChatStateFn(( + setChatState(( curState: Array, ) => [...curState, res]); console.log("attempting to play result"); @@ -162,41 +105,6 @@ function Controls(props: { setChatStateFn: any, chat: Array }) { }); } - return ( -
- - - - - -
- ); -} - -function App() { - const [chatState, setChatState] = useState([{ - role: "system", - content: "You are a helpful assistant.", - }]); - return ( <>
@@ -205,7 +113,12 @@ function App() {
- +
); diff --git a/speech-speech/frontend/src/components.tsx b/speech-speech/frontend/src/components.tsx new file mode 100644 index 0000000..bc4991f --- /dev/null +++ b/speech-speech/frontend/src/components.tsx @@ -0,0 +1,101 @@ +import { useEffect, useRef } from "react"; +import { + TbBrandOpenai, + TbMicrophone2, + TbPlayerPlay, + TbPlayerStop, +} from "react-icons/tb"; + + +export type ChatMsg = { + role: string; + content: string; +}; + +export function Header() { + return ( +
+
+ Speach to Speech AI example +
+
+ ); +} + +export function Feed(props: { chat: Array; setChatStateFn: any }) { + const bottomRef = useRef(null); + + const scrollToBottom = () => { + if (bottomRef.current) { + bottomRef.current.scrollIntoView({ behavior: "smooth" }); + } + }; + + useEffect(() => { + scrollToBottom(); + console.log("scroll?"); + }); + + return ( +
+
+ {props.chat.filter((m: ChatMsg) => m.role != "system").map(( + m: ChatMsg, + i: number, + ) => )} +
+
+
+ ); +} + +export function Msg(props: { msg: ChatMsg }) { + return ( +
+ + {props.msg.role.toUpperCase()}: + +
+ + {props.msg.content} + +
+ ); +} + +export function Controls( + props: { + recButtonOnClick: Function; + recordState: Boolean; + playButtonOnClick: Function; + sendButtonOnClick: Function; + }, +) { + return ( +
+ + + + + +
+ ); +}