Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pause and scripts #61

Merged
merged 5 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions apps/browser/src/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export interface ChatProps {
onMessage: (message: string) => void;
}

const Chat: React.FC<ChatProps> = ({evo, onMessage }: ChatProps) => {
const Chat: React.FC<ChatProps> = ({ evo, onMessage }: ChatProps) => {
const [message, setMessage] = useState<string>("");
const [messages, setMessages] = useState<Message[]>([]);
const [evoRunning, setEvoRunning] = useState<boolean>(false);
const [paused, setPaused] = useState<boolean>(false);
const [evoItr, setEvoItr] = useState<ReturnType<Evo["run"]> | undefined>(
undefined
);
Expand All @@ -36,6 +37,11 @@ const Chat: React.FC<ChatProps> = ({evo, onMessage }: ChatProps) => {
let messageLog = messages;

while (evoRunning) {
if (paused) {
return Promise.resolve();
}

console.log("evoooo", paused);
const response = await evoItr.next();

// TODO:
Expand Down Expand Up @@ -63,15 +69,22 @@ const Chat: React.FC<ChatProps> = ({evo, onMessage }: ChatProps) => {
}, [evoRunning, evoItr]);

const handleSend = async () => {
const newMessages = [...messages, {
setMessages(messages => [...messages, {
type: "info",
text: message,
user: 'user'
}];
setMessages(newMessages);
}]);
setEvoRunning(true);
};

const handlePause = async () => {
setPaused(true);
};

const handleContinue = async () => {
setPaused(false);
};

const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
setMessage(event.target.value);
};
Expand Down Expand Up @@ -101,9 +114,30 @@ const Chat: React.FC<ChatProps> = ({evo, onMessage }: ChatProps) => {
placeholder="Enter your main goal here..."
className="Chat__Input"
/>
<button className="Chat__Btn" onClick={handleSend} disabled={evoRunning}>
Send
</button>
{evoRunning && (
<>
{
!paused && (
<button className="Chat__Btn" onClick={handlePause} disabled={!evoRunning || paused}>
Pause
</button>
)
}
{
paused && (
<button className="Chat__Btn" onClick={handleContinue} disabled={evoRunning && !paused}>
Continue
</button>
)
}
</>
)}

{!evoRunning && (
<button className="Chat__Btn" onClick={handleSend} disabled={evoRunning}>
Start
</button>
)}
</div>
</div>
);
Expand Down
19 changes: 14 additions & 5 deletions apps/browser/src/pages/Dojo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import Sidebar from "../components/Sidebar/Sidebar";
import Chat from "../components/Chat/Chat";
import { InMemoryFile } from '../file';
import { updateWorkspaceFiles } from '../updateWorkspaceFiles';
import { Workspace } from '@evo-ninja/core';
import { onGoalAchievedScript, speakScript } from '../scripts';

type Message = {
text: string;
user: string;
};

function addScript(script: {name: string, definition: string, code: string}, scriptsWorkspace: Workspace) {
scriptsWorkspace.writeFileSync(`${script.name}.json`, script.definition);
scriptsWorkspace.writeFileSync(`${script.name}.js`, script.code);
}

function Dojo() {
const [apiKey, setApiKey] = useState<string | null>(
localStorage.getItem("openai-api-key")
Expand Down Expand Up @@ -101,9 +108,13 @@ function Dojo() {
logUserPrompt: () => {}
});
const scriptsWorkspace = new EvoCore.InMemoryWorkspace();
addScript(onGoalAchievedScript, scriptsWorkspace);
addScript(speakScript, scriptsWorkspace);

const scripts = new EvoCore.Scripts(
scriptsWorkspace
);

setScriptsWorkspace(scriptsWorkspace);
const env = new EvoCore.Env(
{
Expand All @@ -130,16 +141,14 @@ function Dojo() {
);
const agentPackage = PluginPackage.from(module => ({
"onGoalAchieved": async (args: any) => {
logger.success("Goal has been achieved!!!!!");
logger.success("Goal has been achieved!");
},
"speak": async (args: any) => {
logger.success("Agent: " + args.message);
logger.success("Evo: " + args.message);
return "User has been informed! If you think you've achieved the goal, execute onGoalAchieved.";
},
"ask": async (args: any) => {
logger.error("Agent: " + args.message);
const response = await prompt("");
return "User: " + response;
throw new Error("Not implemented");
},
}));

Expand Down
31 changes: 31 additions & 0 deletions apps/browser/src/scripts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const onGoalAchievedScript = {
name: "agent.onGoalAchieved",
definition: `{
"name":"agent.onGoalAchieved",
"description":"Informs the user that the goal has been achieved.",
"arguments":"None",
"code":"./agent.onGoalAchieved.js"
}`,
code: `return __wrap_subinvoke(
'plugin/agent',
'onGoalAchieved',
{ }
).value
`
};

export const speakScript = {
name: "agent.speak",
definition: `{
"name":"agent.speak",
"description":"Informs the user by sending a message.",
"arguments":"{ message: string }",
"code":"./agent.speak.js"
}`,
code: `return __wrap_subinvoke(
'plugin/agent',
'speak',
{ message: message }
).value
`
};
4 changes: 4 additions & 0 deletions packages/core/src/agents/evo/agent-functions/executeScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export const executeScript: AgentFunction = {
JSON.stringify(context.client.jsPromiseOutput.result);
}

if (result.ok && !context.client.jsPromiseOutput.result) {
console.log("No result returned from script.", context.client.jsPromiseOutput, result.value);
}

return result.ok
? result.value.error == null
? context.client.jsPromiseOutput.result
Expand Down