diff --git a/src/components/Accordion.tsx b/src/components/Accordion.tsx index 2e221b6284..badcffc181 100644 --- a/src/components/Accordion.tsx +++ b/src/components/Accordion.tsx @@ -11,7 +11,7 @@ const Accordion = ({ child, name }: AccordionProps) => { {({ open }) => ( <> - + {name} { const [t] = useTranslation(); const [hasUserScrolled, setHasUserScrolled] = useState(false); @@ -99,7 +97,7 @@ const ChatWindow = ({ return ( - + ); })} @@ -259,12 +257,10 @@ const MacWindowHeader = (props: HeaderProps) => { }; const ChatMessage = ({ message, - isAgentStopped, className, }: { message: Message; className?: string; - isAgentStopped?: boolean; }) => { const [t] = useTranslation(); const [showCopy, setShowCopy] = useState(false); @@ -299,7 +295,7 @@ const ChatMessage = ({ // Avoid for system messages as they do not have an icon and will cause a weird space <>
- {getTaskStatusIcon(message, { isAgentStopped })} + {getTaskStatusIcon(message, {})}
{getMessagePrefix(message, t)} diff --git a/src/components/TaskWindow.tsx b/src/components/TaskWindow.tsx index ed5ef59b89..ceeb489df0 100644 --- a/src/components/TaskWindow.tsx +++ b/src/components/TaskWindow.tsx @@ -3,11 +3,12 @@ import FadeIn from "./motions/FadeIn"; import Expand from "./motions/expand"; import { Task } from "../types/agentTypes"; import { getMessageContainerStyle, getTaskStatusIcon } from "./utils/helpers"; -import { useMessageStore } from "../components/store"; +import { useMessageStore } from "../components/stores"; import { FaListAlt } from "react-icons/fa"; import { useTranslation } from "react-i18next"; +import { useAgentStore } from "../components/stores"; -export const TaskWindow = ({ isAgentStopped }: { isAgentStopped: boolean }) => { +export const TaskWindow = () => { const tasks = useMessageStore.use.tasks(); const [t] = useTranslation(); return ( @@ -18,7 +19,7 @@ export const TaskWindow = ({ isAgentStopped }: { isAgentStopped: boolean }) => {
{tasks.map((task, i) => ( - + ))}
@@ -26,13 +27,8 @@ export const TaskWindow = ({ isAgentStopped }: { isAgentStopped: boolean }) => { ); }; -const Task = ({ - task, - isAgentStopped, -}: { - task: Task; - isAgentStopped: boolean; -}) => { +const Task = ({ task }: { task: Task }) => { + const isAgentStopped = useAgentStore.use.isAgentStopped(); return (
void; + setAgent: (newAgent: AutonomousAgent | null) => void; +} + +const createAgentSlice: StateCreator = (set, get) => { + return { + ...initialAgentState, + updateIsAgentStopped: () => { + set((state) => ({ + isAgentStopped: !state.agent?.isRunning, + })); + }, + setAgent: (newAgent) => { + set(() => ({ + agent: newAgent, + })); + }, + }; +}; + +export const useAgentStore = createSelectors( + create()((...a) => ({ + ...createAgentSlice(...a), + })) +); diff --git a/src/components/store/helpers.ts b/src/components/stores/helpers.ts similarity index 100% rename from src/components/store/helpers.ts rename to src/components/stores/helpers.ts diff --git a/src/components/store/index.ts b/src/components/stores/index.ts similarity index 51% rename from src/components/store/index.ts rename to src/components/stores/index.ts index e6930964f5..790c98888e 100644 --- a/src/components/store/index.ts +++ b/src/components/stores/index.ts @@ -1 +1,2 @@ export * from "./messageStore"; +export * from "./agentStore"; diff --git a/src/components/store/messageStore.ts b/src/components/stores/messageStore.ts similarity index 96% rename from src/components/store/messageStore.ts rename to src/components/stores/messageStore.ts index 2985225ab4..f3596f936e 100644 --- a/src/components/store/messageStore.ts +++ b/src/components/stores/messageStore.ts @@ -103,4 +103,5 @@ export const useMessageStore = createSelectors( })) ); -export const resetAllSlices = () => resetters.forEach((resetter) => resetter()); +export const resetAllMessageSlices = () => + resetters.forEach((resetter) => resetter()); diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 2e68de9090..8dc1f81cae 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -19,25 +19,33 @@ import { useAuth } from "../hooks/useAuth"; import type { Message } from "../types/agentTypes"; import { useAgent } from "../hooks/useAgent"; import { isEmptyOrBlank } from "../utils/whitespace"; -import { useMessageStore, resetAllSlices } from "../components/store"; +import { + useMessageStore, + useAgentStore, + resetAllMessageSlices, +} from "../components/stores"; import { isTask } from "../types/agentTypes"; import { serverSideTranslations } from "next-i18next/serverSideTranslations"; import { useSettings } from "../hooks/useSettings"; const Home: NextPage = () => { const [t] = useTranslation(); - // zustand states + // zustand states with state dependencies + const addMessage = useMessageStore.use.addMessage(); const messages = useMessageStore.use.messages(); const tasks = useMessageStore.use.tasks(); - const addMessage = useMessageStore.use.addMessage(); const updateTaskStatus = useMessageStore.use.updateTaskStatus(); + const setAgent = useAgentStore.use.setAgent(); + const isAgentStopped = useAgentStore.use.isAgentStopped(); + const updateIsAgentStopped = useAgentStore.use.updateIsAgentStopped(); + const agent = useAgentStore.use.agent(); + const { session, status } = useAuth(); const [name, setName] = React.useState(""); const [goalInput, setGoalInput] = React.useState(""); - const [agent, setAgent] = React.useState(null); const settingsModel = useSettings(); - const [shouldAgentStop, setShouldAgentStop] = React.useState(false); + const [showHelpDialog, setShowHelpDialog] = React.useState(false); const [showSettingsDialog, setShowSettingsDialog] = React.useState(false); const [hasSaved, setHasSaved] = React.useState(false); @@ -62,10 +70,8 @@ const Home: NextPage = () => { }, []); useEffect(() => { - if (agent == null) { - setShouldAgentStop(false); - } - }, [agent]); + updateIsAgentStopped(); + }, [agent, updateIsAgentStopped]); const handleAddMessage = (message: Message) => { if (isTask(message)) { @@ -78,10 +84,8 @@ const Home: NextPage = () => { const disableDeployAgent = agent != null || isEmptyOrBlank(name) || isEmptyOrBlank(goalInput); - const isAgentStopped = () => !agent?.isRunning || agent === null; - const handleNewGoal = () => { - const agent = new AutonomousAgent( + const newAgent = new AutonomousAgent( name.trim(), goalInput.trim(), handleAddMessage, @@ -89,10 +93,10 @@ const Home: NextPage = () => { settingsModel.settings, session ?? undefined ); - setAgent(agent); + setAgent(newAgent); setHasSaved(false); - resetAllSlices(); - agent.run().then(console.log).catch(console.error); + resetAllMessageSlices(); + newAgent?.run().then(console.log).catch(console.error); }; const handleKeyPress = ( @@ -109,7 +113,6 @@ const Home: NextPage = () => { }; const handleStopAgent = () => { - setShouldAgentStop(true); agent?.stopAgent(); }; @@ -121,7 +124,7 @@ const Home: NextPage = () => { const shouldShowSave = status === "authenticated" && - !agent?.isRunning && + isAgentStopped && messages.length && !hasSaved; @@ -194,11 +197,8 @@ const Home: NextPage = () => { : undefined } scrollToBottom - isAgentStopped={isAgentStopped()} /> - {tasks.length > 0 && ( - - )} + {tasks.length > 0 && }
@@ -248,11 +248,11 @@ const Home: NextPage = () => { )}