Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Fix steps accumulating; Change pause state handling; Improve editing UI
Tomas Valenta9fcfcc10a530
diff --git a/api-client/AgentRun.ts b/api-client/AgentRun.tsindex 5c930b226..bf0588bc4 100644--- a/api-client/AgentRun.ts+++ b/api-client/AgentRun.ts@@ -15,10 +15,16 @@ export interface StepEdit {output: string}+export enum AgentRunState {+ None,+ Running,+ Paused,+}+export interface Opts {onSteps: (steps: Step[]) => voidonClose: () => void- onStateChange: (runState: deployment_state) => void+ onStateChange: (runState: AgentRunState) => void}export class AgentRun {@@ -55,27 +61,31 @@ export class AgentRun {async startRun(projectID: string, modelConfig: ModelConfig) {const { run_id } = await this.rpc.call('start', [projectID, modelConfig]) as { run_id: string }this.runID = run_id- this.opts.onStateChange(deployment_state.generating)+ this.opts.onStateChange(AgentRunState.Running)}async pauseRun() {if (!this.runID) returnawait this.rpc.call('pause')+ this.opts.onStateChange(AgentRunState.Paused)}async resumeRun() {if (!this.runID) returnawait this.rpc.call('resume')+ this.opts.onStateChange(AgentRunState.Running)}async cancelRun() {if (!this.runID) returnawait this.rpc.call('cancel')+ this.opts.onStateChange(AgentRunState.None)}async rewriteRunSteps(steps: Step[]) {if (!this.runID) returnawait this.rpc.call('rewrite_steps', [steps])+ this.opts.onStateChange(AgentRunState.Running)}static resolveStepsEdit(steps: Step[], edit: StepEdit): Step[] | undefined {diff --git a/api-service/agent/base.py b/api-service/agent/base.pyindex 9cdafed6b..2dba01685 100644--- a/api-service/agent/base.py+++ b/api-service/agent/base.py@@ -109,13 +109,11 @@ class AgentRun:):async def change_state(state: DeploymentState) -> None:print(f"Run '{run_id}'", state.value, flush=True)- await self._notify("state_update", {"state": state.value})await db.upsert_deployment_state(run_id=run_id,project_id=project_id,state=state,)- print("Changed state")playground = Noneawait change_state(DeploymentState.Generating)@@ -315,14 +313,16 @@ class AgentRun:async def cancel(self):print("Cancel agent run")self.canceled = True- self.llm_generation.cancel()+ if self.llm_generation:+ self.llm_generation.cancel()await self._close()async def rewrite_steps(self, steps: List[Step]):print("Rewrite agent run steps")await self.pause()self.rewriting_steps = True- self.llm_generation.cancel()+ if self.llm_generation:+ self.llm_generation.cancel()self._output_parser = OutputStreamParser(tool_names=self.tool_names,steps=steps[:-1],diff --git a/api-service/agent/output/output_stream_parser.py b/api-service/agent/output/output_stream_parser.pyindex 681b84f9b..8481131a6 100644--- a/api-service/agent/output/output_stream_parser.py+++ b/api-service/agent/output/output_stream_parser.py@@ -73,12 +73,12 @@ class OutputStreamParser:self,tool_names: List[str],steps: List[Step] = [],- buffered_step: Step = Step(output="", logs=[]),+ buffered_step=Step(output="", logs=[]),) -> None:self._tool_names = tool_names-+ self.id = uuid.uuid4()# All "finished" logs that the parser saved.- self._steps = steps+ self._steps = steps[:]self._token_buffer: str = buffered_step["output"]diff --git a/api-service/agent/ws.py b/api-service/agent/ws.pyindex 4f9ee0796..11aad6975 100644--- a/api-service/agent/ws.py+++ b/api-service/agent/ws.py@@ -13,18 +13,18 @@ class WebsocketAgentRun(AgentRun):self.is_closed = Falseasync def _notify(self, method: str, params: Dict[str, Any]):- if not self.is_closed:- await self.websocket.send_json(- {- "jsonrpc": self.jsonrpc_version,- "method": method,- "params": params,- }- )+ await self.websocket.send_json(+ {+ "jsonrpc": self.jsonrpc_version,+ "method": method,+ "params": params,+ }+ )async def _close(self):- self.is_closed = True- await self.websocket.close()+ if not self.is_closed:+ self.is_closed = True+ await self.websocket.close()async def _call(self, id: str, method: str, params: List[Any]):async def handle_call():@@ -47,24 +47,19 @@ class WebsocketAgentRun(AgentRun):}response = await handle_call()- if not self.is_closed:- await self.websocket.send_json(- {- "jsonrpc": self.jsonrpc_version,- "id": id,- **response,- }- )+ await self.websocket.send_json(+ {+ "jsonrpc": self.jsonrpc_version,+ "id": id,+ **response,+ }+ )async def handle_agent_run(self):await self.websocket.accept()- try:- async for data in self.websocket.iter_json():- await self._call(data["id"], data["method"], data.get("params", []))- except RuntimeError as e:- if not self.is_closed:- raise+ async for data in self.websocket.iter_json():+ await self._call(data["id"], data["method"], data.get("params", []))print("closed")await self.cancel()diff --git a/components/Editor/Sidebar/Agent/AgentRunControls.tsx b/components/Editor/Sidebar/Agent/AgentRunControls.tsxindex 9de11469d..5f75d6e0a 100644--- a/components/Editor/Sidebar/Agent/AgentRunControls.tsx+++ b/components/Editor/Sidebar/Agent/AgentRunControls.tsx@@ -1,13 +1,12 @@-import { deployment_state } from '@prisma/client'-import { ReactNode, memo, useState, useMemo, useCallback, useEffect } from 'react'+import { ReactNode, memo, useMemo, useCallback } from 'react'import { Ban, Pause, Play } from 'lucide-react'import Button from 'components/Button'-import { AgentRun } from 'api-client/AgentRun'+import { AgentRun, AgentRunState } from 'api-client/AgentRun'export interface Props {- agentState?: deployment_state+ agentState?: AgentRunStaterun: () => voidagentRun?: AgentRundisabled?: boolean@@ -19,61 +18,44 @@ function AgentRunControls({agentState,run,}: Props) {- const [isPaused, setIsPaused] = useState(false)-- useEffect(function resetRun() {- if (agentRun) {- setIsPaused(false)- }- }, [agentRun])-const cancel = useCallback(async () => {await agentRun?.cancelRun()- setIsPaused(false)}, [agentRun])const pause = useCallback(async () => {await agentRun?.pauseRun()- setIsPaused(true)- }, [agentRun, setIsPaused])+ }, [agentRun])const resume = useCallback(async () => {await agentRun?.resumeRun()- setIsPaused(false)- }, [agentRun, setIsPaused])+ }, [agentRun])const { action, icon, text } = useMemo<{text: stringicon: ReactNode | nullaction: () => void}>(() => {- if (agentRun) {- switch (agentState) {- case deployment_state.generating:- if (isPaused) {- return {- text: 'Resume',- icon: <Play size="16px" />,- action: resume,- }- } else {- return {- text: 'Running',- icon: <Pause size="16px" />,- action: pause,- }- }- case deployment_state.error:- case deployment_state.finished:- default:- }- }- return {- icon: <Play size="16px" />,- text: 'Run',- action: run,+ switch (agentState) {+ case AgentRunState.Running:+ return {+ text: 'Running',+ icon: <Pause size="16px" />,+ action: pause,+ }+ case AgentRunState.Paused:+ return {+ text: 'Resume',+ icon: <Play size="16px" />,+ action: resume,+ }+ default:+ return {+ icon: <Play size="16px" />,+ text: 'Run',+ action: run,+ }}- }, [agentState, agentRun, isPaused, pause, run, resume])+ }, [agentState, pause, run, resume])return (<>diff --git a/components/Editor/Sidebar/Agent/LogEntry.tsx b/components/Editor/Sidebar/Agent/LogEntry.tsxindex e77712c26..653575aa6 100644--- a/components/Editor/Sidebar/Agent/LogEntry.tsx+++ b/components/Editor/Sidebar/Agent/LogEntry.tsx@@ -9,10 +9,11 @@ import Tool from './Tool'export interface Props {log: Log+ isRunning?: booleanonAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => void}-function LogEntry({ log, onAnswer }: Props) {+function LogEntry({ isRunning, log, onAnswer }: Props) {return (<div className={`p-3@@ -32,6 +33,7 @@ function LogEntry({ log, onAnswer }: Props) {}{log.type === LogType.Tool &&<Tool+ isRunning={isRunning}log={log}onAnswer={onAnswer}/>diff --git a/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx b/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsxindex 13e229b27..f7ad64fdd 100644--- a/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx+++ b/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx@@ -1,6 +1,5 @@import { useEffect, useMemo, useState, MouseEvent } from 'react'import clsx from 'clsx'-import TextareaAutosize from 'react-textarea-autosize'import { Check, X } from 'lucide-react'import { LogType, ToolName, } from 'db/types'@@ -10,11 +9,11 @@ import Text from 'components/Text'import { Step } from 'api-client/AgentRun'import { notEmpty } from 'utils/notEmpty'-export interface Props {step: SteponAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => voidstepIdx: number+ isRunning?: booleanonEditFinish: (output: string) => void}@@ -22,6 +21,7 @@ function StepEditor({step,onAnswer,stepIdx,+ isRunning,onEditFinish,}: Props) {const [isEditing, setIsEditing] = useState(false)@@ -68,7 +68,6 @@ function StepEditor({return (<div- onClick={() => setIsEditing(e => true)}className="flexflex-col@@ -139,13 +138,13 @@ function StepEditor({className="flexflex-col- cursor-textrelative- transition-all+ h-full+ w-full">{isEditing &&- <TextareaAutosize+ <textareaautoCapitalize="off"autoComplete="off"autoCorrect="off"@@ -164,38 +163,44 @@ function StepEditor({z-40borderleading-[16px]+ transition-alltracking-normalfont-sans- bg-slate-50rounded+ resize-none+ border-dashedborder-slate-300- border-l-4- p-3+ pl-3+ py-4+ pr-2`,'text-slate-500',- 'no-scroller',+ 'scroller','outline-none',)}/>}<divclassName={clsx(`- flex- flex-col- space-y-4- `,+ flex+ flex-col+ space-y-4+ `,{ 'invisible': isEditing },)}>{trimmedLogs.map((l, i) =><div+ onClick={() => setIsEditing(true)}key={i}className="- flex- flex-col+ cursor-text+ flex+ flex-col"><LogEntry+ isRunning={isRunning}onAnswer={onAnswer}log={l}/>diff --git a/components/Editor/Sidebar/Agent/StepsStream/index.tsx b/components/Editor/Sidebar/Agent/StepsStream/index.tsxindex 059b17e6f..340466111 100644--- a/components/Editor/Sidebar/Agent/StepsStream/index.tsx+++ b/components/Editor/Sidebar/Agent/StepsStream/index.tsx@@ -7,32 +7,34 @@ import StepEditor from './StepEditor'export interface Props {steps?: Step[]+ isRunning?: booleanonAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => void- isDeployRequestRunning?: booleanonEdit: (edit: StepEdit) => void}function StepsStream({steps,onAnswer,- isDeployRequestRunning,+ isRunning,onEdit,}: Props) {const ref = useRef<HTMLDivElement>(null)useEffect(function scrollLogs() {if (!ref.current) return- if (!isDeployRequestRunning) return+ if (!isRunning) returnref.current.scrollIntoView({ behavior: 'smooth' })- }, [steps, isDeployRequestRunning])+ }, [steps, isRunning])useEffect(function scrollLogs() {if (!ref.current) return- if (isDeployRequestRunning) return+ if (isRunning) returnref.current.scrollIntoView({ behavior: 'auto' })- }, [steps, isDeployRequestRunning])+ }, [steps, isRunning])++ console.log('running', isRunning)return (<div@@ -55,6 +57,7 @@ function StepsStream({<StepEditoronEditFinish={output => onEdit({ stepIdx: i, output })}step={s}+ isRunning={isRunning}stepIdx={i}key={i}onAnswer={onAnswer}diff --git a/components/Editor/Sidebar/Agent/Tool/AskHuman.tsx b/components/Editor/Sidebar/Agent/Tool/AskHuman.tsxindex 74976357e..b77594219 100644--- a/components/Editor/Sidebar/Agent/Tool/AskHuman.tsx+++ b/components/Editor/Sidebar/Agent/Tool/AskHuman.tsx@@ -16,11 +16,13 @@ import Base from './Base'export interface Props {log: ToolLog+ isRunning?: booleanonAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => void}function AskHuman({log,+ isRunning,onAnswer,}: Props) {if (log.tool_name !== ToolName.AskHuman) throw new Error(`'${log.tool_name}': This component supports only logs for '${ToolName.AskHuman}' tool`)@@ -66,6 +68,7 @@ function AskHuman({return (<Base+ isRunning={isRunning}log={log}icon={<HelpCircle size="16px" />}body={body}diff --git a/components/Editor/Sidebar/Agent/Tool/Base.tsx b/components/Editor/Sidebar/Agent/Tool/Base.tsxindex f9d716315..16e2fccd4 100644--- a/components/Editor/Sidebar/Agent/Tool/Base.tsx+++ b/components/Editor/Sidebar/Agent/Tool/Base.tsx@@ -10,12 +10,14 @@ export interface Props {log: ToolLogicon: ReactNodebody: ReactNode+ isRunning?: boolean}function BaseTool({icon,log,body,+ isRunning,}: Props) {if (log.type !== LogType.Tool) throw new Error(`'${log.type}': This component supports only logs of type '${log.type}'`)@@ -42,7 +44,7 @@ function BaseTool({{log.tool_name}</div></div>- {log.tool_output === undefined &&+ {log.tool_output === undefined && isRunning &&<LoaderclassName="text-slate-400 animate-spin"size="16px"diff --git a/components/Editor/Sidebar/Agent/Tool/Curl.tsx b/components/Editor/Sidebar/Agent/Tool/Curl.tsxindex 9080cb22b..5f48882f9 100644--- a/components/Editor/Sidebar/Agent/Tool/Curl.tsx+++ b/components/Editor/Sidebar/Agent/Tool/Curl.tsx@@ -6,13 +6,14 @@ import { ToolName, ToolLog } from 'db/types'import Base from './Base'-export interface Props {log: ToolLog+ isRunning?: boolean}function Curl({log,+ isRunning,}: Props) {if (log.tool_name !== ToolName.Curl) throw new Error(`'${log.tool_name}': This component supports only logs for '${ToolName.Curl}' tool`)@@ -28,6 +29,7 @@ function Curl({return (<Base+ isRunning={isRunning}log={log}icon={<Terminal size="16px" />}body={body}diff --git a/components/Editor/Sidebar/Agent/Tool/InstallNPMDeps.tsx b/components/Editor/Sidebar/Agent/Tool/InstallNPMDeps.tsxindex 0721d192f..80e3dc214 100644--- a/components/Editor/Sidebar/Agent/Tool/InstallNPMDeps.tsx+++ b/components/Editor/Sidebar/Agent/Tool/InstallNPMDeps.tsx@@ -9,10 +9,12 @@ import Base from './Base'export interface Props {log: ToolLog+ isRunning?: boolean}function InstallNPMDeps({log,+ isRunning,}: Props) {if (log.tool_name !== ToolName.InstallNPMDependencies) throw new Error(`'${log.tool_name}': This component supports only logs for '${ToolName.InstallNPMDependencies}' tool`)@@ -31,6 +33,7 @@ function InstallNPMDeps({return (<Base+ isRunning={isRunning}log={log}icon={<Package size="16px" />}body={body}diff --git a/components/Editor/Sidebar/Agent/Tool/LetHumanChoose.tsx b/components/Editor/Sidebar/Agent/Tool/LetHumanChoose.tsxindex 2196007f7..ac0ae5324 100644--- a/components/Editor/Sidebar/Agent/Tool/LetHumanChoose.tsx+++ b/components/Editor/Sidebar/Agent/Tool/LetHumanChoose.tsx@@ -13,11 +13,13 @@ import Base from './Base'export interface Props {log: ToolLog+ isRunning?: booleanonAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => void}function LetHumanChoose({log,+ isRunning,onAnswer,}: Props) {if (log.tool_name !== ToolName.LetHumanChoose) throw new Error(`'${log.tool_name}': This component supports only logs for '${ToolName.LetHumanChoose}' tool`)@@ -104,6 +106,7 @@ function LetHumanChoose({return (<Base+ isRunning={isRunning}log={log}icon={<HelpCircle size="16px" />}body={body}diff --git a/components/Editor/Sidebar/Agent/Tool/RunSavedCode.tsx b/components/Editor/Sidebar/Agent/Tool/RunSavedCode.tsxindex 332d42821..2308b7462 100644--- a/components/Editor/Sidebar/Agent/Tool/RunSavedCode.tsx+++ b/components/Editor/Sidebar/Agent/Tool/RunSavedCode.tsx@@ -9,10 +9,12 @@ import Base from './Base'export interface Props {log: ToolLog+ isRunning?: boolean}function RunSavedCode({log,+ isRunning,}: Props) {if (log.tool_name !== ToolName.RunSavedCode) throw new Error(`'${log.tool_name}': This component supports only logs for '${ToolName.WriteJavaScriptCode}' tool`)@@ -30,6 +32,7 @@ function RunSavedCode({return (<Base+ isRunning={isRunning}log={log}icon={<Code2 size="16px" />}body={body}diff --git a/components/Editor/Sidebar/Agent/Tool/WriteCode.tsx b/components/Editor/Sidebar/Agent/Tool/WriteCode.tsxindex 3eaf96b40..74cdf38ce 100644--- a/components/Editor/Sidebar/Agent/Tool/WriteCode.tsx+++ b/components/Editor/Sidebar/Agent/Tool/WriteCode.tsx@@ -8,10 +8,12 @@ import Base from './Base'export interface Props {log: ToolLog+ isRunning?: boolean}function WriteCode({log,+ isRunning,}: Props) {if (log.tool_name !== ToolName.WriteJavaScriptCode) throw new Error(`'${log.tool_name}': This component supports only logs for '${ToolName.WriteJavaScriptCode}' tool`)@@ -27,6 +29,7 @@ function WriteCode({return (<Base+ isRunning={isRunning}log={log}icon={<FileEdit size="16px" />}body={body}diff --git a/components/Editor/Sidebar/Agent/Tool/index.tsx b/components/Editor/Sidebar/Agent/Tool/index.tsxindex 7a0cd91ab..4723af8cb 100644--- a/components/Editor/Sidebar/Agent/Tool/index.tsx+++ b/components/Editor/Sidebar/Agent/Tool/index.tsx@@ -9,26 +9,28 @@ import LetHumanChoose from './LetHumanChoose'export interface Props {log: ToolLog+ isRunning?: booleanonAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => void}function Tool({log,+ isRunning,onAnswer,}: Props) {switch (log.tool_name) {case ToolName.Curl:- return <Curl log={log} />+ return <Curl log={log} isRunning={isRunning} />case ToolName.InstallNPMDependencies:- return <InstallNPMDeps log={log} />+ return <InstallNPMDeps log={log} isRunning={isRunning} />case ToolName.WriteJavaScriptCode:- return <WriteCode log={log} />+ return <WriteCode log={log} isRunning={isRunning} />case ToolName.RunSavedCode:- return <RunSavedCode log={log} />+ return <RunSavedCode log={log} isRunning={isRunning} />case ToolName.AskHuman:- return <AskHuman log={log} onAnswer={onAnswer} />+ return <AskHuman log={log} onAnswer={onAnswer} isRunning={isRunning} />case ToolName.LetHumanChoose:- return <LetHumanChoose log={log} onAnswer={onAnswer} />+ return <LetHumanChoose log={log} onAnswer={onAnswer} isRunning={isRunning} />default:return <div>Cannot render frontend component - unknown tol {log.tool_name}</div>}diff --git a/components/Editor/Sidebar/Agent/index.tsx b/components/Editor/Sidebar/Agent/index.tsxindex 2d61f9c22..c665e8f68 100644--- a/components/Editor/Sidebar/Agent/index.tsx+++ b/components/Editor/Sidebar/Agent/index.tsx@@ -1,7 +1,6 @@import { useState } from 'react'// import { useSupabaseClient } from '@supabase/auth-helpers-react'import { useRouter } from 'next/router'-import { deployment_state } from '@prisma/client'import Text from 'components/Text'import { useTabs } from 'components/Tabs/useTabs'@@ -11,7 +10,7 @@ import useModelProviderArgs from 'hooks/useModelProviderArgs'import { getMissingCreds } from 'state/model'import { useStateStore } from 'state/StoreProvider'import Button from 'components/Button'-import { AgentRun, Step, StepEdit } from 'api-client/AgentRun'+import { AgentRun, AgentRunState, Step, StepEdit } from 'api-client/AgentRun'import StepsStream from './StepsStream'import AgentRunControls from './AgentRunControls'@@ -32,7 +31,7 @@ const tabsProps = {export interface Props {steps?: Step[]- agentState?: deployment_state+ agentState?: AgentRunStaterun: () => voidagentRun?: AgentRun}@@ -166,7 +165,7 @@ function Agent({<StepsStreamonEdit={onEdit}steps={steps}- isDeployRequestRunning={agentState === deployment_state.generating}+ isRunning={agentState === AgentRunState.Running}/>}{selectedTab === 1 && steps &&diff --git a/hooks/useAgentRun.ts b/hooks/useAgentRun.tsindex 26f0b0857..a54b9c8b6 100644--- a/hooks/useAgentRun.ts+++ b/hooks/useAgentRun.ts@@ -1,31 +1,28 @@import { useCallback, useState } from 'react'-import { deployment_state } from '@prisma/client'-import { AgentRun, Step } from 'api-client/AgentRun'+import { AgentRun, AgentRunState, Step } from 'api-client/AgentRun'import { ModelConfig } from 'state/model'import { baseUrl } from 'api-client/api'function useAgentRun() {const [agentRun, setAgentRun] = useState<AgentRun>()const [steps, setSteps] = useState<Step[]>()- const [agentState, setAgentState] = useState<deployment_state>()+ const [agentState, setAgentState] = useState<AgentRunState>()const start = useCallback(async (projectID: string, modelConfig: ModelConfig) => {const run = new AgentRun(`${baseUrl}/dev/agent`, {onSteps: setSteps,onStateChange: setAgentState,- onClose: () => setAgentRun(undefined),+ onClose: () => {+ setAgentRun(undefined)+ setAgentState(undefined)+ },})setSteps([])setAgentRun(run)await run.connect()- try {- await run.startRun(projectID, modelConfig)- } catch (error) {- setAgentState(deployment_state.error)- console.error(error)- }+ await run.startRun(projectID, modelConfig)}, [setAgentRun, setSteps, setAgentState])return {