Skip to content

Commit

Permalink
Merge pull request #70 from polywrap/nerfzael-evo-fixes
Browse files Browse the repository at this point in the history
Stopping the agent
  • Loading branch information
dOrgJelli authored Aug 21, 2023
2 parents e898f71 + 096c43c commit e91d7a7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
50 changes: 42 additions & 8 deletions apps/browser/src/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, ChangeEvent, KeyboardEvent } from "react";
import React, { useState, useEffect, ChangeEvent, KeyboardEvent, useRef } from "react";
import { Evo } from "@evo-ninja/core";
import ReactMarkdown from "react-markdown";

Expand All @@ -14,16 +14,34 @@ export interface ChatProps {
evo: Evo;
onMessage: (message: ChatMessage) => void;
messages: ChatMessage[];
goalAchieved: boolean
}

const Chat: React.FC<ChatProps> = ({ evo, onMessage, messages }: ChatProps) => {
const Chat: React.FC<ChatProps> = ({ evo, onMessage, messages, goalAchieved }: ChatProps) => {
const [message, setMessage] = useState<string>("");
const [evoRunning, setEvoRunning] = useState<boolean>(false);
const [paused, setPaused] = useState<boolean>(false);
const [sending, setSending] = useState<boolean>(false);
const [evoItr, setEvoItr] = useState<ReturnType<Evo["run"]> | undefined>(
undefined
);
const [stopped, setStopped] = useState<boolean>(false);

const pausedRef = useRef(paused);
useEffect(() => {
pausedRef.current = paused;
}, [paused]);

const goalAchievedRef = useRef(paused);
useEffect(() => {
goalAchievedRef.current = goalAchieved;
}, [goalAchieved]);

useEffect(() => {
if (goalAchieved) {
setPaused(true);
}
}, [goalAchieved]);

useEffect(() => {
const runEvo = async () => {
Expand All @@ -41,10 +59,13 @@ const Chat: React.FC<ChatProps> = ({ evo, onMessage, messages }: ChatProps) => {
let messageLog = messages;

while (evoRunning) {
if (paused) {
if (pausedRef.current || goalAchievedRef.current) {
setStopped(true);
return Promise.resolve();
}

setStopped(false);

const response = await evoItr.next();

if (response.value && response.value.message) {
Expand All @@ -53,7 +74,9 @@ const Chat: React.FC<ChatProps> = ({ evo, onMessage, messages }: ChatProps) => {
user: "evo"
};
messageLog = [...messageLog, evoMessage];
onMessage(evoMessage);
if (!goalAchievedRef.current) {
onMessage(evoMessage);
}
}

if (response.done) {
Expand All @@ -65,7 +88,8 @@ const Chat: React.FC<ChatProps> = ({ evo, onMessage, messages }: ChatProps) => {
return Promise.resolve();
}

runEvo();
const timer = setTimeout(runEvo, 200);
return () => clearTimeout(timer);
}, [evoRunning, evoItr]);

const handleSend = async () => {
Expand Down Expand Up @@ -129,9 +153,19 @@ const Chat: React.FC<ChatProps> = ({ evo, onMessage, messages }: ChatProps) => {
}
{
paused && (
<button className="Chat__Btn" onClick={handleContinue} disabled={evoRunning && !paused}>
Continue
</button>
<>
{!stopped && (
<button className="Chat__Btn" disabled={true}>
Pausing
</button>
)}

{stopped && (
<button className="Chat__Btn" onClick={handleContinue} disabled={evoRunning && !paused}>
Paused
</button>
)}
</>
)
}
</>
Expand Down
4 changes: 3 additions & 1 deletion apps/browser/src/pages/Dojo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function Dojo() {
const [userWorkspace, setUserWorkspace] = useState<EvoCore.InMemoryWorkspace | undefined>(undefined);
const [scriptsWorkspace, setScriptsWorkspace] = useState<EvoCore.InMemoryWorkspace | undefined>(undefined);
const [messages, setMessages] = useState<ChatMessage[]>([]);
const [goalAchieved, setGoalAchieved] = useState<boolean>(false);

useEffect(() => {
if (!evo || !scriptsWorkspace) {
Expand Down Expand Up @@ -158,6 +159,7 @@ function Dojo() {
const agentPackage = PluginPackage.from(module => ({
"onGoalAchieved": async (args: any) => {
logger.success("Goal has been achieved!");
setGoalAchieved(true);
},
"speak": async (args: any) => {
logger.success("Evo: " + args.message);
Expand Down Expand Up @@ -191,7 +193,7 @@ function Dojo() {
}
<Sidebar onSettingsClick={() => setConfigOpen(true)} scripts={scripts} userFiles={userFiles} uploadUserFiles={setUploadedFiles} />
<>
{evo && <Chat evo={evo} onMessage={onMessage} messages={messages} />}
{evo && <Chat evo={evo} onMessage={onMessage} messages={messages} goalAchieved={goalAchieved} />}
{dojoError && <DojoError error={dojoError} />}
</>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/agents/evo/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const INITIAL_PROMP = `You are an agent that executes scripts to accompli
`When creating scripts be sure to declare all the require statements. The code of the scripts should be written as if in a function.\n` +
`Do not use async/await or promises.\n` +
`Use the speak script to inform the user.\n` +
`Once you have achieved the goal, execute the onGoalAchieved script in the agent namespace.`;
`Once you have achieved the goal, use executeScript function to execute agent.onGoalAchieved script.`;

export const GOAL_PROMPT = (goal: string) => `The user has the following goal: ${goal}.`
export const LOOP_PREVENTION_PROMPT = "Assistant, are you trying to inform the user? If so, Try calling findScript with the agent namespace.";

0 comments on commit e91d7a7

Please sign in to comment.