🚨 Add option to manually shutdown

This commit is contained in:
Asim Shrestha
2023-04-09 13:49:32 -07:00
parent 0f39372297
commit 47095012a0
3 changed files with 67 additions and 16 deletions

View File

@@ -62,6 +62,12 @@ class AutonomousAgent {
return; return;
} }
if (!this.isRunning) {
this.sendManualShutdownMessage();
this.shutdown();
return;
}
// Wait before starting // Wait before starting
await new Promise((r) => setTimeout(r, 1000)); await new Promise((r) => setTimeout(r, 1000));
@@ -132,6 +138,10 @@ class AutonomousAgent {
return res.data.response as string; return res.data.response as string;
} }
stopAgent() {
this.isRunning = false;
}
sendGoalMessage() { sendGoalMessage() {
this.sendMessage({ type: "goal", value: this.goal }); this.sendMessage({ type: "goal", value: this.goal });
} }
@@ -143,6 +153,13 @@ class AutonomousAgent {
}); });
} }
sendManualShutdownMessage() {
this.sendMessage({
type: "system",
value: `The agent has been manually shutdown.`,
});
}
sendCompletedMessage() { sendCompletedMessage() {
this.sendMessage({ this.sendMessage({
type: "system", type: "system",

View File

@@ -10,6 +10,7 @@ export interface ButtonProps {
children?: React.ReactNode; children?: React.ReactNode;
loader?: boolean; loader?: boolean;
disabled?: boolean; disabled?: boolean;
enabledClassName?: string;
onClick?: (e: React.MouseEvent<HTMLButtonElement>) => Promise<void> | void; onClick?: (e: React.MouseEvent<HTMLButtonElement>) => Promise<void> | void;
} }
@@ -33,10 +34,12 @@ const Button = forwardRef(
disabled={loading || props.disabled} disabled={loading || props.disabled}
className={clsx( className={clsx(
"text-gray/50 rounded-lg border-[2px] border-white/30 px-10 py-3 font-bold transition-all", "text-gray/50 rounded-lg border-[2px] border-white/30 px-10 py-3 font-bold transition-all",
props.className,
props.disabled props.disabled
? " cursor-not-allowed border-white/10 bg-zinc-900 text-white/30" ? " cursor-not-allowed border-white/10 bg-zinc-900 text-white/30"
: " mou cursor-pointer bg-[#1E88E5]/70 text-white/80 hover:border-white/80 hover:bg-[#0084f7] hover:text-white hover:shadow-2xl" : ` mou cursor-pointer bg-[#1E88E5]/70 text-white/80 hover:border-white/80 hover:bg-[#0084f7] hover:text-white hover:shadow-2xl ${
props.enabledClassName || ""
}`,
props.className
)} )}
onClick={onClick} onClick={onClick}
> >

View File

@@ -18,10 +18,12 @@ const Home: NextPage = () => {
const [name, setName] = React.useState<string>(""); const [name, setName] = React.useState<string>("");
const [goalInput, setGoalInput] = React.useState<string>(""); const [goalInput, setGoalInput] = React.useState<string>("");
const [agent, setAgent] = React.useState<AutonomousAgent | null>(null); const [agent, setAgent] = React.useState<AutonomousAgent | null>(null);
const [stoppingAgent, setStoppingAgent] = React.useState(false);
const [messages, setMessages] = React.useState<Message[]>([]); const [messages, setMessages] = React.useState<Message[]>([]);
const [showModal, setShowModal] = React.useState(false); const [showModal, setShowModal] = React.useState(false);
useEffect(() => { useEffect(() => {
const key = "agentgpt-modal-opened"; const key = "agentgpt-modal-opened";
const savedModalData = localStorage.getItem(key); const savedModalData = localStorage.getItem(key);
@@ -33,6 +35,12 @@ const Home: NextPage = () => {
localStorage.setItem(key, JSON.stringify(true)); localStorage.setItem(key, JSON.stringify(true));
}, []); }, []);
useEffect(() => {
if (agent == null) {
setStoppingAgent(false);
}
}, [agent]);
const handleNewGoal = () => { const handleNewGoal = () => {
const addMessage = (message: Message) => const addMessage = (message: Message) =>
setMessages((prev) => [...prev, message]); setMessages((prev) => [...prev, message]);
@@ -43,6 +51,11 @@ const Home: NextPage = () => {
agent.run().then(console.log).catch(console.error); agent.run().then(console.log).catch(console.error);
}; };
const handleStopAgent = () => {
setStoppingAgent(true);
agent?.stopAgent();
};
return ( return (
<DefaultLayout> <DefaultLayout>
<Dialog showModal={showModal} setShowModal={setShowModal} /> <Dialog showModal={showModal} setShowModal={setShowModal} />
@@ -109,20 +122,38 @@ const Home: NextPage = () => {
/> />
</div> </div>
<Button <div className="flex gap-2">
disabled={agent != null || name === "" || goalInput === ""} <Button
onClick={handleNewGoal} disabled={agent != null || name === "" || goalInput === ""}
className="mt-10" onClick={handleNewGoal}
> className="mt-10"
{agent == null ? ( >
"Deploy Agent" {agent == null ? (
) : ( "Deploy Agent"
<> ) : (
<VscLoading className="animate-spin" size={20} /> <>
<span className="ml-2">Agent running</span> <VscLoading className="animate-spin" size={20} />
</> <span className="ml-2">Agent running</span>
)} </>
</Button> )}
</Button>
<Button
disabled={agent == null}
onClick={handleStopAgent}
className="mt-10"
enabledClassName={"bg-red-600 hover:bg-red-400"}
>
{stoppingAgent ? (
<>
<VscLoading className="animate-spin" size={20} />
<span className="ml-2">Stopping agent</span>
</>
) : (
"Stop agent"
)}
</Button>
</div>
</div> </div>
</div> </div>
</main> </main>