Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Finish canceling, pausing and resuming; Add editing [WIP]

Tomas Valenta3121ad490a35
diff --git a/api-client/AgentDebugRun.ts b/api-client/AgentRun.ts
similarity index 62%
rename from api-client/AgentDebugRun.ts
rename to api-client/AgentRun.ts
index 7d3781239..ab691788d 100644
--- a/api-client/AgentDebugRun.ts
+++ b/api-client/AgentRun.ts
@@ -4,8 +4,20 @@ import { deployment_state } from '@prisma/client'
import { Log } from 'db/types'
import { ModelConfig } from 'state/model'
+
+export interface Step {
+ output: string
+ logs: Log[]
+}
+
+export interface StepEdit {
+ stepIdx: number
+ output: string
+}
+
export interface Opts {
- onLogs: (logs: Log[]) => void
+ onSteps: (steps: Step[]) => void
+ onClose: () => void
onStateChange: (runState: deployment_state) => void
}
@@ -15,6 +27,7 @@ export class AgentRun {
constructor(private readonly url: string, private readonly opts: Opts) {
this.rpc.onNotification.push(this.handleNotification.bind(this))
+ this.rpc.onClose(opts.onClose)
}
async connect() {
@@ -28,8 +41,8 @@ export class AgentRun {
}
private handleNotification(data: IRpcNotification) {
- if (data.method === 'logs') {
- return this.opts.onLogs(data.params.logs)
+ if (data.method === 'steps') {
+ return this.opts.onSteps(data.params.steps)
}
if (data.method === 'state_update') {
this.opts.onStateChange(data.params.state)
@@ -46,18 +59,35 @@ export class AgentRun {
}
async pauseRun() {
+ if (!this.runID) return
await this.rpc.call('pause')
}
async resumeRun() {
+ if (!this.runID) return
await this.rpc.call('resume')
}
async cancelRun() {
+ if (!this.runID) return
await this.rpc.call('cancel')
}
- async rewriteRunLogs(logs: Log[]) {
- await this.rpc.call('rewrite_logs', [logs])
+ async rewriteRunSteps(steps: Step[]) {
+ if (!this.runID) return
+ await this.rpc.call('rewrite_steps', [steps])
+ }
+
+ static resolveStepsEdit(steps: Step[], edit: StepEdit): Step[] | undefined {
+ const step = steps[edit.stepIdx]
+ if (!step) {
+ throw new Error('Step does not exist')
+ }
+ if (step.output === edit.output) return
+ step.output = edit.output
+ step.logs = []
+
+ // return steps without steps after edited step
+ return steps.slice(0, edit.stepIdx + 1)
}
}
diff --git a/api-service/agent/base.py b/api-service/agent/base.py
index a931eac7c..da64d0aba 100644
--- a/api-service/agent/base.py
+++ b/api-service/agent/base.py
@@ -3,72 +3,63 @@ import uuid
from abc import abstractmethod
from typing import Any, Dict, List, Literal, Tuple, cast
-from agent.tokens.parsing import ThoughtLog, ToolLog
-from agent.tokens.log_parser import LogStreamParser
-from agent.tokens.logs import LogsCallbackHandler
+from agent.output.parse_output import Log, ThoughtLog, ToolLog
+from agent.output.output_stream_parser import OutputStreamParser, Step
+from agent.output.token_callback_handler import TokenCallbackHandler
+from agent.output.work_queue import WorkQueue
from langchain.callbacks.base import AsyncCallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain.chains.llm import LLMChain
-from langchain.schema import AgentAction, BaseMessage
+from langchain.schema import BaseMessage
from langchain.tools import BaseTool
-from pydantic import BaseModel
from langchain.prompts.chat import (
BaseMessagePromptTemplate,
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
+from langchain.agents.tools import InvalidTool
-from agent.tokens.token_streamer import TokenStreamer
-from agent.tokens.log_processor import LogProcessor
from agent.tools.base import create_tools
from database.database import DeploymentState
from models.base import ModelConfig, PromptPart, get_model
from database import db
from session import NodeJSPlayground
-FINAL_ANSWERS = ("Final Answer", "FinalAnswer")
-
-class RewriteHistoryException(Exception):
+class RewriteStepsException(Exception):
pass
-class AgentRun(BaseModel):
- should_pause = False
- canceled = False
- rewriting_history = False
-
+class AgentRun:
def __init__(self, **kwargs):
super().__init__(**kwargs)
+ self.should_pause = False
+ self.canceled = False
+ self.rewriting_steps = False
self.can_resume = asyncio.Event()
- async def _arun_tool(
- self,
- tool_name: str,
- tool_input: str,
+ @staticmethod
+ async def _run_tool(
+ tool_log: ToolLog,
name_to_tool_map: Dict[str, BaseTool],
) -> str:
# Otherwise we lookup the tool
- if tool_name in name_to_tool_map:
- tool = name_to_tool_map[tool_name]
+ if tool_log["tool_name"] in name_to_tool_map:
+ tool = name_to_tool_map[tool_log["tool_name"]]
# We then call the tool on the tool input to get an observation
observation = await tool.arun(
- tool_input,
+ tool_log["tool_input"],
verbose=True,
)
else:
observation = await InvalidTool().arun( # type: ignore
- tool_name=tool_name,
- tool_input=tool_input,
+ tool_name=tool_log["tool_name"],
+ tool_input=tool_log["tool_input"],
verbose=True,
)
return observation
- @abstractmethod
- async def _notify(self, method: str, params: Dict[str, Any] | List[Any]):
- pass
-
@staticmethod
def _get_prompt_part(
prompts: List[PromptPart], role: Literal["user", "system"], type: str
@@ -85,6 +76,31 @@ class AgentRun(BaseModel):
.replace("}", "}}")
)
+ @staticmethod
+ def _create_prompt(model_config: ModelConfig, tools: List[BaseTool]):
+ system_template = "\n\n".join(
+ [
+ AgentRun._get_prompt_part(model_config.prompt, "system", "prefix"),
+ "\n".join([f"{tool.name}: {tool.description}" for tool in tools]),
+ AgentRun._get_prompt_part(model_config.prompt, "system", "suffix"),
+ ]
+ )
+ human_template = "\n\n".join(
+ [
+ AgentRun._get_prompt_part(model_config.prompt, "user", "prefix"),
+ "{agent_scratchpad}",
+ ]
+ )
+
+ messages: List[BaseMessage | BaseMessagePromptTemplate] = [
+ SystemMessagePromptTemplate.from_template(system_template),
+ HumanMessagePromptTemplate.from_template(human_template),
+ ]
+ return ChatPromptTemplate(
+ input_variables=["agent_scratchpad"],
+ messages=messages,
+ )
+
async def _run(
self,
run_id: str,
@@ -105,10 +121,10 @@ class AgentRun(BaseModel):
await change_state(DeploymentState.Generating)
try:
# -----
- # Initialize callback manager for controlling token stream
- streamer = TokenStreamer()
+ # Initialize callback manager and handler for controlling token stream
callback_manager = AsyncCallbackManager([StreamingStdOutCallbackHandler()])
- callback_manager.add_handler(LogsCallbackHandler(streamer=streamer))
+ self.token_handler = TokenCallbackHandler()
+ callback_manager.add_handler(self.token_handler)
# -----
# Create tools
@@ -120,67 +136,57 @@ class AgentRun(BaseModel):
playground=playground,
)
)
- tool_names = [tool.name for tool in tools]
+ self.tool_names = [tool.name for tool in tools]
tool_map = {tool.name: tool for tool in tools}
# Assign callback manager to tools
for tool in tools:
tool.callback_manager = callback_manager
- # -----
- # Create prompt
- system_template = "\n\n".join(
- [
- self._get_prompt_part(model_config.prompt, "system", "prefix"),
- "\n".join([f"{tool.name}: {tool.description}" for tool in tools]),
- self._get_prompt_part(model_config.prompt, "system", "suffix"),
- ]
- )
- human_template = "\n\n".join(
- [
- self._get_prompt_part(model_config.prompt, "user", "prefix"),
- "{agent_scratchpad}",
- ]
- )
-
- messages: List[BaseMessage | BaseMessagePromptTemplate] = [
- SystemMessagePromptTemplate.from_template(system_template),
- HumanMessagePromptTemplate.from_template(human_template),
- ]
- prompt = ChatPromptTemplate(
- input_variables=["agent_scratchpad"],
- messages=messages,
- )
-
# -----
# Create LLM from specified model
llm_chain = LLMChain(
llm=get_model(model_config, callback_manager),
- prompt=prompt,
+ prompt=self._create_prompt(model_config, tools),
callback_manager=callback_manager,
)
# -----
# Create log handlers
- # TODO: Keep track of logs generations - we may need to purge them on rewrite
- self.logs: List[ThoughtLog | ToolLog] = []
# Used for sending logs to db/frontend without blocking
- log_processor = LogProcessor(
- on_logs=lambda logs: self._notify("logs", {"logs": logs})
+ steps_streamer = WorkQueue[List[Step]](
+ on_workload=lambda steps: self._notify("steps", {"steps": steps})
)
# Used for parsing token stream into logs+actions
- log_parser = LogStreamParser(tool_names=tool_names)
+ self._output_parser = OutputStreamParser(tool_names=self.tool_names)
# -----
- # Initialize intermediate_steps that keeps state between LLM calls
- self.intermediate_steps: List[Tuple[AgentAction, str]] = []
+ # List of (LLM output, parsed logs)
+ steps: List[Step] = []
- # This function is used to inject information from previous runs to the prompt
+ # This function is used to inject information from previous steps to the current prompt
def get_agent_scratchpad():
+ if len(steps) == 0:
+ return ""
+
agent_scratchpad = ""
- for action, observation in self.intermediate_steps:
- agent_scratchpad += action.log
- agent_scratchpad += f"\nObservation: {observation}\nThought:"
+ for step in steps:
+ agent_scratchpad += step["output"]
+
+ tool_logs = (
+ cast(ToolLog, action)
+ for action in step["logs"]
+ if action["type"] == "tool"
+ )
+
+ tool_outputs = "\n".join(
+ log.get("tool_output", "")
+ for log in tool_logs
+ if log.get("tool_output")
+ )
+
+ if tool_outputs:
+ agent_scratchpad += f"\nObservation: {tool_outputs}\nThought:"
return (
f"This was your previous work "
@@ -191,6 +197,7 @@ class AgentRun(BaseModel):
print("Generating...", flush=True)
while True:
try:
+ await self._check_interrupt()
# Query the LLM in background
self.llm_generation = asyncio.create_task(
llm_chain.agenerate(
@@ -205,47 +212,42 @@ class AgentRun(BaseModel):
while True:
await self._check_interrupt()
# Consume the tokens from LLM
- token = await streamer.retrieve()
+ token = await self.token_handler.retrieve_token()
if token is None:
break
- self.logs = log_parser.ingest_token(token).get_logs()
- log_processor.ingest(self.logs)
+ await self._check_interrupt()
+ # Process logs in a separate task
+ steps = self._output_parser.ingest_token(token).get_steps()
+ steps_streamer.schedule(steps)
+
+ current_step = self._output_parser.get_current_step()
- # Check if the the run is finished
+ # Check if the run is finished
if any(
- final in log_parser._token_buffer for final in FINAL_ANSWERS
+ keyword in current_step["output"]
+ for keyword in ("Final Answer", "FinalAnswer")
):
break
- observations: List[str] = []
-
for tool in (
cast(ToolLog, action)
- for action in self.logs
+ for action in current_step["logs"]
if action["type"] == "tool"
):
await self._check_interrupt()
- output = await self._arun_tool(
- tool_name=tool["tool_name"],
- tool_input=tool["tool_input"],
+ tool_output = await self._run_tool(
+ tool,
name_to_tool_map=tool_map,
)
- self.logs = log_parser.ingest_tool_output(output).get_logs()
- log_processor.ingest(self.logs)
-
- observations.append(output)
-
- self.intermediate_steps.append(
- (
- AgentAction("Action", "", log_parser._token_buffer),
- "\n".join(observations),
- )
- )
+ await self._check_interrupt()
+ steps = self._output_parser.ingest_tool_output(
+ tool_output
+ ).get_steps()
+ steps_streamer.schedule(steps)
- except RewriteHistoryException:
- self.logs = log_parser.ingest_complete_llm_output("").get_logs()
+ except RewriteStepsException:
continue
await change_state(DeploymentState.Finished)
@@ -264,11 +266,20 @@ class AgentRun(BaseModel):
await self.can_resume.wait()
self.can_resume.clear()
- if self.rewriting_history:
- self.rewriting_history = False
- raise RewriteHistoryException()
+ if self.rewriting_steps:
+ self.rewriting_steps = False
+ raise RewriteStepsException()
+
+ @abstractmethod
+ async def _notify(self, method: str, params: Dict[str, Any] | List[Any]):
+ pass
+
+ @abstractmethod
+ async def _close():
+ pass
async def start(self, project_id: str, model_config: Dict[str, Any]):
+ print("Start agent run")
run_id = str(uuid.uuid4())
asyncio.create_task(
self._run(
@@ -279,21 +290,29 @@ class AgentRun(BaseModel):
)
return {"run_id": run_id}
- def pause(self):
+ async def pause(self):
+ print("Pause agent run")
self.should_pause = True
- def resume(self):
+ async def resume(self):
+ print("Resume agent run")
+ self.should_pause = False
self.can_resume.set()
- def cancel(self):
+ async def cancel(self):
+ print("Cancel agent run")
self.canceled = True
- # Ensure that the agent run is not stuck on paused
- self.resume()
+ self.llm_generation.cancel()
+ await self._close()
- def rewrite_history(self):
- self.pause()
- # TODO: Check if there could be any unflushed tokens after cancel
+ async def rewrite_steps(self, steps: List[Step], buffered_step: Step):
+ print("Rewrite agent run steps")
+ await self.pause()
+ self.rewriting_steps = True
self.llm_generation.cancel()
- # TODO: Change self.intermediate_steps to reflect the modified history
- self.rewriting_history = True
- self.resume()
+ self._output_parser = OutputStreamParser(
+ tool_names=self.tool_names,
+ steps=steps,
+ buffered_step=buffered_step,
+ )
+ await self.resume()
diff --git a/api-service/agent/tokens/__init__.py b/api-service/agent/output/__init__.py
similarity index 100%
rename from api-service/agent/tokens/__init__.py
rename to api-service/agent/output/__init__.py
diff --git a/api-service/agent/tokens/log_parser.py b/api-service/agent/output/output_stream_parser.py
similarity index 79%
rename from api-service/agent/tokens/log_parser.py
rename to api-service/agent/output/output_stream_parser.py
index 101f9c6e7..681b84f9b 100644
--- a/api-service/agent/tokens/log_parser.py
+++ b/api-service/agent/output/output_stream_parser.py
@@ -3,15 +3,20 @@ import uuid
from typing import List, TypedDict
-from agent.tokens.parsing import (
+from agent.output.parse_output import (
Log,
ThoughtLog,
ToolLog,
merge_logs,
- parse_thoughts_and_actions,
+ parse_output,
)
+class Step(TypedDict):
+ output: str
+ logs: List[ToolLog | ThoughtLog]
+
+
class ToolOutput(TypedDict):
finish_at: str
tool_output: str
@@ -22,7 +27,7 @@ class LogMeta(TypedDict):
created_at: str
-class LogStreamParser:
+class OutputStreamParser:
"""
We receive a stream of tokens and want to parse this stream of tokens to thought and tool logs
even if we haven't yet received all tokens we need to execute the action.
@@ -64,22 +69,37 @@ class LogStreamParser:
and the buffered can be flushed to finished logs when we receive output for the last tool in the buffered logs.
"""
- def __init__(self, tool_names: List[str]) -> None:
+ def __init__(
+ self,
+ tool_names: List[str],
+ steps: List[Step] = [],
+ buffered_step: Step = Step(output="", logs=[]),
+ ) -> None:
self._tool_names = tool_names
# All "finished" logs that the parser saved.
- self._logs: List[ToolLog | ThoughtLog] = []
+ self._steps = steps
- self._token_buffer: str = ""
+ self._token_buffer: str = buffered_step["output"]
# Logs have both `thought` logs and `tool` logs.
# These logs can be partially parsed or they can have missing outputs.
# They are flushed to self._logs after outputs for all tools in self._logs.buffer are ingested.
- self._logs_buffer: List[ToolLog | ThoughtLog] = []
- self._logs_meta_buffer: List[LogMeta] = []
+ self._logs_buffer: List[ToolLog | ThoughtLog] = buffered_step["logs"]
+ self._logs_meta_buffer: List[LogMeta] = [
+ LogMeta(created_at=log.get("created_at", ""), id=log.get("id", ""))
+ for log in self._logs_buffer
+ ]
# This is a list of tools' outputs that corresponds to the tool logs in the self._logs.buffer.
- self._tools_output_buffer: List[ToolOutput] = []
+ self._tools_output_buffer: List[ToolOutput] = [
+ ToolOutput(
+ finish_at=log.get("finish_at", ""),
+ tool_output=log.get("tool_output", ""),
+ )
+ for log in self._logs_buffer
+ if log.get("tool_output")
+ ]
def _parse(self):
"""This function should be called only after ingesting new token or tool output to update the buffered logs."""
@@ -87,7 +107,7 @@ class LogStreamParser:
# If you ingested token or tool output this will save the change so you can call self.get_logs and get the new logs.
self._logs_buffer = [
log
- for log in parse_thoughts_and_actions(self._token_buffer)
+ for log in parse_output(self._token_buffer)
if log["type"] == "thought"
or log["type"] == "tool"
and log.get("tool_name") in self._tool_names
@@ -137,7 +157,7 @@ class LogStreamParser:
if len(self._tools_output_buffer) == len(
[log for log in self._logs_buffer if log["type"] == "tool"]
):
- self._logs.extend(self._logs_buffer)
+ self._steps.append(self.get_current_step())
self._token_buffer = ""
self._logs_buffer = []
self._tools_output_buffer = []
@@ -145,5 +165,8 @@ class LogStreamParser:
return self
- def get_logs(self) -> List[ThoughtLog | ToolLog]:
- return [*self._logs, *self._logs_buffer]
+ def get_steps(self) -> List[Step]:
+ return [*self._steps, self.get_current_step()]
+
+ def get_current_step(self):
+ return Step(output=self._token_buffer, logs=self._logs_buffer)
diff --git a/api-service/agent/tokens/parsing.py b/api-service/agent/output/parse_output.py
similarity index 98%
rename from api-service/agent/tokens/parsing.py
rename to api-service/agent/output/parse_output.py
index 46eff0a9e..cb299796c 100644
--- a/api-service/agent/tokens/parsing.py
+++ b/api-service/agent/output/parse_output.py
@@ -41,7 +41,7 @@ action_tag_close = "</\\s*action\\s*>?"
action_tag_split_pattern = re.compile(f"({action_tag_open})|({action_tag_close})|(.+?)")
action_tag_check_pattern = re.compile(f"{action_tag_open}|{action_tag_close}")
-def parse_thoughts_and_actions(text: str):
+def parse_output(text: str):
escaped = "".join(
# If the text part is not action tag escape it.
part if action_tag_check_pattern.match(part) else xml_escape(part)
diff --git a/api-service/agent/output/token_callback_handler.py b/api-service/agent/output/token_callback_handler.py
new file mode 100644
index 000000000..7b3092f44
--- /dev/null
+++ b/api-service/agent/output/token_callback_handler.py
@@ -0,0 +1,28 @@
+from typing import Any
+from langchain.callbacks.base import AsyncCallbackHandler
+from langchain.schema import LLMResult
+from asyncio import Queue
+
+
+class TokenCallbackHandler(AsyncCallbackHandler):
+ def __init__(self, **kwargs) -> None:
+ super().__init__(**kwargs)
+ self._queue: Queue[str | None] = Queue(1)
+
+ async def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
+ """Run on new LLM token. Only available when streaming is enabled."""
+ await self._ingest_token(token)
+
+ async def on_llm_end(self, response: LLMResult, **kwargs: Any):
+ await self._close_token_stream()
+
+ async def retrieve_token(self):
+ token = await self._queue.get()
+ self._queue.task_done()
+ return token
+
+ async def _ingest_token(self, token: str):
+ await self._queue.put(token)
+
+ async def _close_token_stream(self):
+ await self._queue.put(None)
diff --git a/api-service/agent/tokens/log_queue.py b/api-service/agent/output/work_queue.py
similarity index 68%
rename from api-service/agent/tokens/log_queue.py
rename to api-service/agent/output/work_queue.py
index a9a88b37e..29ed47a12 100644
--- a/api-service/agent/tokens/log_queue.py
+++ b/api-service/agent/output/work_queue.py
@@ -1,10 +1,17 @@
-from asyncio import Queue, ensure_future, sleep
+from asyncio import Queue, ensure_future
+from typing import Any, Callable, Coroutine, Generic, TypeVar
+
from typing import Coroutine
+T = TypeVar("T")
+
+
+class WorkQueue(Generic[T]):
+ """Queue that tries to always process only the most recently scheduled workload."""
-class LogQueue:
- def __init__(self) -> None:
+ def __init__(self, on_workload: Callable[[T], Coroutine[Any, Any, Any]]) -> None:
self._queue: Queue[Coroutine] = Queue()
+ self._on_workload = on_workload
# Start the worker that saves logs from queue to the db.
self._worker = ensure_future(self._start())
@@ -35,8 +42,9 @@ class LogQueue:
async def flush(self):
await self._queue.join()
- def add(self, log: Coroutine):
- self._queue.put_nowait(log)
+ def schedule(self, workload: T):
+ task = self._on_workload(workload)
+ self._queue.put_nowait(task)
def close(self):
self._worker.cancel()
diff --git a/api-service/agent/tokens/log_processor.py b/api-service/agent/tokens/log_processor.py
deleted file mode 100644
index be1ad64af..000000000
--- a/api-service/agent/tokens/log_processor.py
+++ /dev/null
@@ -1,24 +0,0 @@
-from typing import Any, Callable, Coroutine, Generic, TypeVar
-from pydantic import BaseModel, PrivateAttr
-
-from agent.tokens.log_queue import LogQueue
-
-T = TypeVar("T")
-OnLogs = Callable[[T], Coroutine[Any, Any, Any]]
-
-
-class LogProcessor(Generic[T]):
- def __init__(self, on_logs: OnLogs[T]) -> None:
- super().__init__()
- self._on_logs = on_logs
- self.log_queue = LogQueue()
-
- def ingest(self, logs: T):
- task = self._on_logs(logs)
- self.log_queue.add(task)
-
- async def flush(self):
- await self.log_queue.flush()
-
- def close(self):
- self.log_queue.close()
diff --git a/api-service/agent/tokens/logs.py b/api-service/agent/tokens/logs.py
deleted file mode 100644
index 7c77b2b08..000000000
--- a/api-service/agent/tokens/logs.py
+++ /dev/null
@@ -1,19 +0,0 @@
-from typing import Any
-from langchain.callbacks.base import AsyncCallbackHandler
-from langchain.schema import LLMResult
-from pydantic import PrivateAttr
-
-from agent.tokens.token_streamer import TokenStreamer
-
-
-class LogsCallbackHandler(AsyncCallbackHandler):
- def __init__(self, streamer: TokenStreamer) -> None:
- super().__init__()
- self.streamer = streamer
-
- async def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
- """Run on new LLM token. Only available when streaming is enabled."""
- await self.streamer.ingest(token)
-
- async def on_llm_end(self, response: LLMResult, **kwargs: Any):
- await self.streamer.close()
diff --git a/api-service/agent/tokens/token_streamer.py b/api-service/agent/tokens/token_streamer.py
deleted file mode 100644
index 812bde49a..000000000
--- a/api-service/agent/tokens/token_streamer.py
+++ /dev/null
@@ -1,17 +0,0 @@
-from asyncio import Queue
-
-
-class TokenStreamer:
- def __init__(self) -> None:
- self._queue: Queue[str | None] = Queue(1)
-
- async def retrieve(self):
- token = await self._queue.get()
- self._queue.task_done()
- return token
-
- async def ingest(self, token: str):
- await self._queue.put(token)
-
- async def close(self):
- await self._queue.put(None)
diff --git a/api-service/agent/tools/async_tool.py b/api-service/agent/tools/async_tool.py
index 5aab6cce2..e46db78fb 100644
--- a/api-service/agent/tools/async_tool.py
+++ b/api-service/agent/tools/async_tool.py
@@ -2,7 +2,7 @@ from typing import Awaitable, Callable, Optional, cast
from langchain.agents import Tool
from langchain.tools.base import BaseTool
-from agent.tokens.parsing import ToolLog, parse_thoughts_and_actions
+from agent.output.parse_output import ToolLog, parse_output
def func(_: str):
@@ -23,7 +23,7 @@ def async_tool(name: str | None = None) -> Callable:
tool_example = next(
(
cast(ToolLog, tool)
- for tool in parse_thoughts_and_actions(arun.__doc__.strip())
+ for tool in parse_output(arun.__doc__.strip())
if tool["type"] == "tool"
),
None,
diff --git a/api-service/agent/tools/human/tools.py b/api-service/agent/tools/human/tools.py
index d0ca87654..db621ae3e 100644
--- a/api-service/agent/tools/human/tools.py
+++ b/api-service/agent/tools/human/tools.py
@@ -49,4 +49,4 @@ def create_human_tools(
response: ToolsLogOutput = thread.get()
return response.response
- yield ask_human
+ # yield ask_human
diff --git a/api-service/agent/ws.py b/api-service/agent/ws.py
index 02bb3a591..4f9ee0796 100644
--- a/api-service/agent/ws.py
+++ b/api-service/agent/ws.py
@@ -1,5 +1,3 @@
-import pydantic
-
from typing import Any, Dict, List
from fastapi import WebSocket
@@ -12,24 +10,27 @@ class WebsocketAgentRun(AgentRun):
def __init__(self, websocket: WebSocket, **kwargs):
super().__init__(**kwargs)
self.websocket = websocket
-
- class Config:
- extra = pydantic.Extra.allow
+ self.is_closed = False
async def _notify(self, method: str, params: Dict[str, Any]):
- await self.websocket.send_json(
- {
- "jsonrpc": self.jsonrpc_version,
- "method": method,
- "params": params,
- }
- )
+ if not self.is_closed:
+ 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()
async def _call(self, id: str, method: str, params: List[Any]):
async def handle_call():
try:
method_handler = getattr(self, method)
- if not method_handler:
+ if method_handler is None:
raise Exception(f"Method not found {method}")
result = await method_handler(*params)
@@ -37,6 +38,7 @@ class WebsocketAgentRun(AgentRun):
"result": result,
}
except Exception as e:
+ print(f"Error calling {method} via JSONRPC", e)
return {
"error": {
"code": 13,
@@ -45,20 +47,24 @@ class WebsocketAgentRun(AgentRun):
}
response = await handle_call()
- await self.websocket.send_json(
- {
- "jsonrpc": self.jsonrpc_version,
- "id": id,
- **response,
- }
- )
+ if not self.is_closed:
+ await self.websocket.send_json(
+ {
+ "jsonrpc": self.jsonrpc_version,
+ "id": id,
+ **response,
+ }
+ )
- @classmethod
- async def handle_agent_run(cls, websocket: WebSocket):
- await websocket.accept()
- run = cls(websocket=websocket)
+ async def handle_agent_run(self):
+ await self.websocket.accept()
- async for data in websocket.iter_json():
- await run._call(data["id"], data["method"], data["params"])
+ 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
print("closed")
+ await self.cancel()
diff --git a/api-service/app.py b/api-service/app.py
index db3af3915..9c5262d2e 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -15,4 +15,5 @@ app.add_middleware(
@app.websocket("/dev/agent")
async def ws_agent_run(websocket: WebSocket):
- await WebsocketAgentRun.handle_agent_run(websocket)
+ run = WebsocketAgentRun(websocket)
+ await run.handle_agent_run()
diff --git a/api-service/database/database.py b/api-service/database/database.py
index 3eec5d506..7b528c75d 100644
--- a/api-service/database/database.py
+++ b/api-service/database/database.py
@@ -1,7 +1,7 @@
from typing import List
from enum import Enum
-from agent.tokens.parsing import ThoughtLog, ToolLog
+from agent.output.parse_output import ThoughtLog, ToolLog
from database.client import Client
from session.env import EnvVar
diff --git a/api-service/test/conftest.py b/api-service/test/conftest.py
index f7264873d..16c7d6cdf 100644
--- a/api-service/test/conftest.py
+++ b/api-service/test/conftest.py
@@ -1,7 +1,7 @@
import pytest
from typing import Dict, List, Any, cast
-from agent.tokens.parsing import ToolLog, parse_thoughts_and_actions
+from agent.output.parse_output import ToolLog, parse_output
class Helpers:
@@ -12,7 +12,7 @@ class Helpers:
for output in llm_outputs:
actions = [
cast(ToolLog, action)
- for action in parse_thoughts_and_actions(output["llm_output"])
+ for action in parse_output(output["llm_output"])
if action["type"] == "tool"
]
diff --git a/api-service/test/test_action_parsing.py b/api-service/test/test_action_parsing.py
index b089604dd..859b9cb55 100644
--- a/api-service/test/test_action_parsing.py
+++ b/api-service/test/test_action_parsing.py
@@ -2,7 +2,7 @@
from typing import Dict, List, Any
-from agent.tokens.parsing import ToolLog
+from agent.output.parse_output import ToolLog
llm_outputs: List[Dict[str, Any]] = [
diff --git a/api-service/test/test_incomplete_action_parsing.py b/api-service/test/test_incomplete_action_parsing.py
index fec7197a9..47f6e37c8 100644
--- a/api-service/test/test_incomplete_action_parsing.py
+++ b/api-service/test/test_incomplete_action_parsing.py
@@ -2,7 +2,7 @@
from typing import Dict, List, Any
-from agent.tokens.parsing import ToolLog
+from agent.output.parse_output import ToolLog
llm_outputs: List[Dict[str, Any]] = [
{
diff --git a/components/Editor/Sidebar/Agent/AgentRunControls.tsx b/components/Editor/Sidebar/Agent/AgentRunControls.tsx
new file mode 100644
index 000000000..9de11469d
--- /dev/null
+++ b/components/Editor/Sidebar/Agent/AgentRunControls.tsx
@@ -0,0 +1,98 @@
+import { deployment_state } from '@prisma/client'
+import { ReactNode, memo, useState, useMemo, useCallback, useEffect } from 'react'
+import { Ban, Pause, Play } from 'lucide-react'
+
+import Button from 'components/Button'
+import { AgentRun } from 'api-client/AgentRun'
+
+
+export interface Props {
+ agentState?: deployment_state
+ run: () => void
+ agentRun?: AgentRun
+ disabled?: boolean
+}
+
+function AgentRunControls({
+ agentRun,
+ disabled,
+ 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])
+
+ const resume = useCallback(async () => {
+ await agentRun?.resumeRun()
+ setIsPaused(false)
+ }, [agentRun, setIsPaused])
+
+ const { action, icon, text } = useMemo<{
+ text: string
+ icon: ReactNode | null
+ action: () => 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,
+ }
+ }, [agentState, agentRun, isPaused, pause, run, resume])
+
+ return (
+ <>
+ {agentRun && agentState &&
+ <Button
+ onClick={cancel}
+ variant={Button.variant.Outline}
+ icon={<Ban size="16px" />}
+ />
+ }
+ <Button
+ text={text}
+ onClick={action}
+ isDisabled={disabled}
+ variant={Button.variant.Full}
+ icon={icon}
+ />
+ </>
+ )
+}
+
+export default memo(AgentRunControls)
diff --git a/components/Editor/Sidebar/Agent/LogEntry.tsx b/components/Editor/Sidebar/Agent/LogEntry.tsx
index 7b96277f1..e77712c26 100644
--- a/components/Editor/Sidebar/Agent/LogEntry.tsx
+++ b/components/Editor/Sidebar/Agent/LogEntry.tsx
@@ -19,6 +19,9 @@ function LogEntry({ log, onAnswer }: Props) {
border-l-4
rounded
shadow-sm
+ border-y-transparent
+ border-r-transparent
+ border
bg-white
${log.type === LogType.Tool ? 'border-green-300' : 'border-blue-300'}
`}>
diff --git a/components/Editor/Sidebar/Agent/LogStream.tsx b/components/Editor/Sidebar/Agent/LogStream.tsx
deleted file mode 100644
index a95d017b0..000000000
--- a/components/Editor/Sidebar/Agent/LogStream.tsx
+++ /dev/null
@@ -1,105 +0,0 @@
-import { useEffect, useMemo, useRef } from 'react'
-import ReactMarkdown from 'react-markdown'
-
-import { Log, LogType, ToolName, } from 'db/types'
-import { notEmpty } from 'utils/notEmpty'
-
-import LogEntry from './LogEntry'
-import Text from 'components/Text'
-
-export interface Props {
- logs?: Log[]
- rawLogs?: string | null
- onAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => void
- isDeployRequestRunning?: boolean
-}
-
-function LogStream({
- logs,
- rawLogs,
- onAnswer,
- isDeployRequestRunning,
-}: Props) {
- const ref = useRef<HTMLDivElement>(null)
-
- useEffect(function scrollLogs() {
- if (!ref.current) return
- if (!isDeployRequestRunning) return
-
- ref.current.scrollIntoView({ behavior: 'smooth' })
- }, [logs, isDeployRequestRunning])
-
- useEffect(function scrollLogs() {
- if (!ref.current) return
- if (isDeployRequestRunning) return
-
- ref.current.scrollIntoView({ behavior: 'auto' })
- }, [logs, isDeployRequestRunning])
-
- const trimmedLogs = useMemo(() => {
- return logs
- ?.map(l => {
- switch (l.type) {
- case LogType.Thought:
- const content = l.content
- .replaceAll('\nAction:', '')
- .replaceAll('Action:', '')
- .replaceAll('Thought:', '')
- .trim()
- return content
- ? {
- ...l,
- content,
- }
- : undefined
- default:
- return l
- }
- })
- .filter(notEmpty)
- }, [logs])
-
- return (
- <div
- className="
- flex-1
- overflow-auto
- text-xs
- tracking-wide
- font-sans
- scroller
- whitespace-pre-wrap
- py-4
- flex
- flex-col
- bg-slate-50
- space-y-4
- px-2
- ">
- {rawLogs &&
- <ReactMarkdown>
- {rawLogs}
- </ReactMarkdown>
- }
- {trimmedLogs && trimmedLogs.map((l, i) =>
- <div
- key={i}
- className="flex flex-col space-y-1"
- >
- <Text
- size={Text.size.S3}
- className="text-slate-400"
- text={`Step ${i + 1}`}
- />
- <LogEntry
- onAnswer={onAnswer}
- log={l}
- />
- </div>
- )}
- <div ref={ref} />
- </div>
- )
-}
-
-export default LogStream
diff --git a/components/Editor/Sidebar/Agent/RawStepsStream.tsx b/components/Editor/Sidebar/Agent/RawStepsStream.tsx
new file mode 100644
index 000000000..98014f579
--- /dev/null
+++ b/components/Editor/Sidebar/Agent/RawStepsStream.tsx
@@ -0,0 +1,36 @@
+import ReactMarkdown from 'react-markdown'
+
+export interface Props {
+ rawOutput?: string | null
+}
+
+function RawStepsStream({
+ rawOutput,
+}: Props) {
+ return (
+ <div
+ className="
+ flex-1
+ overflow-auto
+ text-xs
+ tracking-wide
+ font-sans
+ scroller
+ whitespace-pre-wrap
+ py-4
+ flex
+ flex-col
+ bg-slate-50
+ space-y-4
+ px-2
+ ">
+ {rawOutput &&
+ <ReactMarkdown>
+ {rawOutput}
+ </ReactMarkdown>
+ }
+ </div>
+ )
+}
+
+export default RawStepsStream
diff --git a/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx b/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx
new file mode 100644
index 000000000..13e229b27
--- /dev/null
+++ b/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx
@@ -0,0 +1,210 @@
+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'
+
+import LogEntry from '../LogEntry'
+import Text from 'components/Text'
+import { Step } from 'api-client/AgentRun'
+import { notEmpty } from 'utils/notEmpty'
+
+
+export interface Props {
+ step: Step
+ onAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => void
+ stepIdx: number
+ onEditFinish: (output: string) => void
+}
+
+function StepEditor({
+ step,
+ onAnswer,
+ stepIdx,
+ onEditFinish,
+}: Props) {
+ const [isEditing, setIsEditing] = useState(false)
+ const [editedOutput, setEditedOutput] = useState(step.output)
+
+ useEffect(function resetEditedOutput() {
+ setEditedOutput(step.output)
+ }, [step])
+
+ function handleEditFinish(e: MouseEvent<HTMLElement>) {
+ onEditFinish(editedOutput)
+ setIsEditing(false)
+ e.stopPropagation()
+ }
+
+ function cancelEdit(e: MouseEvent<HTMLElement>) {
+ setIsEditing(false)
+ setEditedOutput(step.output)
+ e.stopPropagation()
+ }
+
+ const trimmedLogs = useMemo(() => {
+ return step.logs
+ ?.map(l => {
+ switch (l.type) {
+ case LogType.Thought:
+ const content = l.content
+ .replaceAll('\nAction:', '')
+ .replaceAll('Action:', '')
+ .replaceAll('Thought:', '')
+ .trim()
+ return content
+ ? {
+ ...l,
+ content,
+ }
+ : undefined
+ default:
+ return l
+ }
+ })
+ .filter(notEmpty)
+ }, [step.logs])
+
+ return (
+ <div
+ onClick={() => setIsEditing(e => true)}
+ className="
+ flex
+ flex-col
+ space-y-2
+ "
+ >
+ <div
+ className="
+ flex
+ space-x-2
+ justify-between
+ "
+ >
+ <Text
+ size={Text.size.S3}
+ className="
+ text-slate-400
+ py-[1px]
+ "
+ text={`Step ${stepIdx + 1}`}
+ />
+ {isEditing &&
+ <Text
+ size={Text.size.S3}
+ className="
+ text-slate-400
+ py-[1px]
+ italic
+ "
+ text="editing"
+ />
+ }
+ {isEditing &&
+ <div
+ className="
+ flex
+ space-x-2
+ "
+ >
+ <div
+ onClick={handleEditFinish}
+ className="
+ cursor-pointer
+ transition-all
+ hover:text-slate-600
+ text-slate-500
+ z-50
+ "
+ >
+ <Check size="18px" />
+ </div>
+ <div
+ onClick={cancelEdit}
+ className="
+ cursor-pointer
+ transition-all
+ hover:text-slate-600
+ text-slate-500
+ z-50
+ "
+ >
+ <X size="18px" />
+ </div>
+ </div>
+ }
+ </div>
+ <div
+ className="
+ flex
+ flex-col
+ cursor-text
+ relative
+ transition-all
+ "
+ >
+ {isEditing &&
+ <TextareaAutosize
+ autoCapitalize="off"
+ autoComplete="off"
+ autoCorrect="off"
+ spellCheck="false"
+ value={editedOutput}
+ onChange={e => setEditedOutput(e.target.value)}
+ className={clsx(
+ `
+ whitespace-pre-wrap
+ flex
+ w-full
+ h-full
+ absolute
+ flex-1
+ text-sm
+ z-40
+ border
+ leading-[16px]
+ tracking-normal
+ font-sans
+ bg-slate-50
+ rounded
+ border-slate-300
+ border-l-4
+ p-3
+ `,
+ 'text-slate-500',
+ 'no-scroller',
+ 'outline-none',
+ )}
+ />
+ }
+ <div
+ className={clsx(`
+ flex
+ flex-col
+ space-y-4
+ `,
+ { 'invisible': isEditing },
+ )}
+ >
+ {trimmedLogs.map((l, i) =>
+ <div
+ key={i}
+ className="
+ flex
+ flex-col
+ "
+ >
+ <LogEntry
+ onAnswer={onAnswer}
+ log={l}
+ />
+ </div>
+ )}
+ </div>
+ </div>
+ </div >
+ )
+}
+
+export default StepEditor
diff --git a/components/Editor/Sidebar/Agent/StepsStream/index.tsx b/components/Editor/Sidebar/Agent/StepsStream/index.tsx
new file mode 100644
index 000000000..059b17e6f
--- /dev/null
+++ b/components/Editor/Sidebar/Agent/StepsStream/index.tsx
@@ -0,0 +1,68 @@
+import { useEffect, useRef } from 'react'
+
+import { ToolName, } from 'db/types'
+import { Step, StepEdit } from 'api-client/AgentRun'
+
+import StepEditor from './StepEditor'
+
+export interface Props {
+ steps?: Step[]
+ onAnswer?: (args: { logID: string, answer: string, toolName: ToolName }) => void
+ isDeployRequestRunning?: boolean
+ onEdit: (edit: StepEdit) => void
+}
+
+function StepsStream({
+ steps,
+ onAnswer,
+ isDeployRequestRunning,
+ onEdit,
+}: Props) {
+ const ref = useRef<HTMLDivElement>(null)
+
+ useEffect(function scrollLogs() {
+ if (!ref.current) return
+ if (!isDeployRequestRunning) return
+
+ ref.current.scrollIntoView({ behavior: 'smooth' })
+ }, [steps, isDeployRequestRunning])
+
+ useEffect(function scrollLogs() {
+ if (!ref.current) return
+ if (isDeployRequestRunning) return
+
+ ref.current.scrollIntoView({ behavior: 'auto' })
+ }, [steps, isDeployRequestRunning])
+
+ return (
+ <div
+ className="
+ flex-1
+ overflow-auto
+ text-xs
+ tracking-wide
+ font-sans
+ scroller
+ whitespace-pre-wrap
+ py-4
+ flex
+ flex-col
+ bg-slate-50
+ space-y-4
+ px-2
+ ">
+ {steps?.map((s, i) =>
+ <StepEditor
+ onEditFinish={output => onEdit({ stepIdx: i, output })}
+ step={s}
+ stepIdx={i}
+ key={i}
+ onAnswer={onAnswer}
+ />
+ )}
+ <div ref={ref} />
+ </div>
+ )
+}
+
+export default StepsStream
diff --git a/components/Editor/Sidebar/Agent/Thought.tsx b/components/Editor/Sidebar/Agent/Thought.tsx
index e958d4879..0751ba07a 100644
--- a/components/Editor/Sidebar/Agent/Thought.tsx
+++ b/components/Editor/Sidebar/Agent/Thought.tsx
@@ -9,7 +9,7 @@ function Thought({
}: Props) {
return (
<div className="
- leading-[24px]
+ leading-[22px]
tracking-normal
font-sans
text-sm
diff --git a/components/Editor/Sidebar/Agent/index.tsx b/components/Editor/Sidebar/Agent/index.tsx
index a36b7b3bb..3a5699770 100644
--- a/components/Editor/Sidebar/Agent/index.tsx
+++ b/components/Editor/Sidebar/Agent/index.tsx
@@ -1,23 +1,21 @@
import { useState } from 'react'
-import { useSupabaseClient } from '@supabase/auth-helpers-react'
+// import { useSupabaseClient } from '@supabase/auth-helpers-react'
import { useRouter } from 'next/router'
-import produce from 'immer'
import { deployment_state } from '@prisma/client'
import Text from 'components/Text'
import { useTabs } from 'components/Tabs/useTabs'
import Tabs from 'components/Tabs'
-import { Log, LogType, ToolName } from 'db/types'
-import { Database } from 'db/supabase'
+// import { Database } from 'db/supabase'
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 LogStream from './LogStream'
-import RunButton from '../RunButton'
-import { AgentRun } from 'api-client/AgentDebugRun'
-import { deploymentsTable } from 'db/tables'
+import StepsStream from './StepsStream'
+import AgentRunControls from './AgentRunControls'
+import RawStepsStream from './RawStepsStream'
const tabsProps = {
tabs: [
@@ -33,7 +31,7 @@ const tabsProps = {
}
export interface Props {
- logs?: Log[]
+ steps?: Step[]
agentState?: deployment_state
run: () => void
agentRun?: AgentRun
@@ -42,11 +40,11 @@ export interface Props {
function Agent({
agentState,
agentRun,
- logs,
+ steps,
run,
}: Props) {
const [selectedTab, setSelectedTab] = useState(0)
- const client = useSupabaseClient<Database>()
+ // const client = useSupabaseClient<Database>()
const tabsCss = useTabs(tabsProps)
const [selectors] = useStateStore()
const router = useRouter()
@@ -55,26 +53,33 @@ function Agent({
const modelConfig = selectors.use.getSelectedModelConfig()()
const missingCreds = modelConfig ? getMissingCreds(modelConfig?.provider, creds) : []
- async function saveAnswer({
- logID,
- answer,
- toolName,
- }: { logID: string, answer: string, toolName: ToolName }) {
- if (!agentRun?.runID) return
- if (!logs) return
+ // async function saveAnswer({
+ // logID,
+ // answer,
+ // toolName,
+ // }: { logID: string, answer: string, toolName: ToolName }) {
+ // if (!agentRun?.runID) return
+ // if (!logs) return
- const modifiedLogs = produce(logs, ls => {
- const log = ls.find(l => l.id === logID)
- if (log && log.type === LogType.Tool && log.tool_name === toolName) {
- log.tool_output = answer
- }
- })
+ // const modifiedLogs = produce(logs, ls => {
+ // const log = ls.find(l => l.id === logID)
+ // if (log && log.type === LogType.Tool && log.tool_name === toolName) {
+ // log.tool_output = answer
+ // }
+ // })
+
+ // await client
+ // .from(deploymentsTable)
+ // .update({ logs: modifiedLogs as any })
+ // .eq('id', agentRun.runID)
+ // .single()
+ // }
- await client
- .from(deploymentsTable)
- .update({ logs: modifiedLogs as any })
- .eq('id', agentRun.runID)
- .single()
+ async function onEdit(edit: StepEdit) {
+ if (!steps || !agentRun) return
+ const editedSteps = AgentRun.resolveStepsEdit(steps, edit)
+ if (!editedSteps) return
+ await agentRun.rewriteRunSteps(editedSteps)
}
return (
@@ -135,10 +140,11 @@ function Agent({
/>
</>
}
- <RunButton
+ <AgentRunControls
disabled={missingCreds.length !== 0}
- deploy={run}
- deployStatus={agentState}
+ run={run}
+ agentRun={agentRun}
+ agentState={agentState}
/>
</div>
</div>
@@ -156,19 +162,18 @@ function Agent({
/>
</div>
}
- {selectedTab === 0 && logs &&
- <LogStream
- logs={logs}
- onAnswer={saveAnswer}
+ {selectedTab === 0 && steps &&
+ <StepsStream
+ onEdit={onEdit}
+ steps={steps}
isDeployRequestRunning={agentState === deployment_state.generating}
/>
}
- {/* {selectedTab === 1 && deployment &&
- <LogStream
- rawLogs={deployment?.logs_raw}
- isDeployRequestRunning={isDeployRequestRunning}
+ {selectedTab === 1 && steps &&
+ <RawStepsStream
+ rawOutput={steps.map(s => s.output).join('\n')}
/>
- } */}
+ }
</div>
)
}
diff --git a/components/Editor/Sidebar/RunButton.tsx b/components/Editor/Sidebar/RunButton.tsx
deleted file mode 100644
index 9c2410fb8..000000000
--- a/components/Editor/Sidebar/RunButton.tsx
+++ /dev/null
@@ -1,69 +0,0 @@
-import { deployment_state } from '@prisma/client'
-import { ReactNode, memo } from 'react'
-
-import Button from 'components/Button'
-import Spinner from 'components/Spinner'
-
-export interface Props {
- deploy: () => void
- deployStatus?: deployment_state | null
- isInitializingDeploy?: boolean
- isDeployRequestRunning?: boolean
- disabled?: boolean
-}
-
-interface DeploymentRepresentation {
- text: string
- icon: ReactNode | null
-}
-
-function getProgressRepresentation(deployStatus: deployment_state | null | undefined, isInitializingDeploy: boolean | undefined): DeploymentRepresentation {
- if (isInitializingDeploy) {
- return {
- text: 'Initializing...',
- icon: <Spinner />,
- }
- }
-
- switch (deployStatus) {
- case deployment_state.generating:
- return {
- text: 'Generating...',
- icon: <Spinner />,
- }
- case deployment_state.deploying:
- return {
- text: 'Deploying...',
- icon: <Spinner />,
- }
- case deployment_state.error:
- case deployment_state.finished:
- default:
- return {
- text: 'Run',
- icon: null,
- }
- }
-}
-
-function DeployButton({
- deploy,
- disabled,
- deployStatus,
- isDeployRequestRunning,
- isInitializingDeploy,
-}: Props) {
- const representation = getProgressRepresentation(deployStatus, isInitializingDeploy)
-
- return (
- <Button
- isDisabled={isDeployRequestRunning || disabled}
- text={representation.text}
- onClick={deploy}
- variant={Button.variant.Full}
- icon={representation.icon}
- />
- )
-}
-
-export default memo(DeployButton)
diff --git a/components/Editor/Sidebar/index.tsx b/components/Editor/Sidebar/index.tsx
index b4fd5a986..590335d06 100644
--- a/components/Editor/Sidebar/index.tsx
+++ b/components/Editor/Sidebar/index.tsx
@@ -27,10 +27,11 @@ function Sidebar({
}: Props) {
const {
agentRun,
- logs: newLogs,
+ steps: newSteps,
start,
agentState,
} = useAgentRun()
+ console.log('new', newSteps)
const [selectors] = useStateStore()
const modelConfig = selectors.use.getSelectedModelConfig()()
const instructions = selectors.use.instructions()
@@ -38,8 +39,9 @@ function Sidebar({
const [creds] = useModelProviderArgs()
+ // TODO: Change logs to steps in db
const lastRunLogs = project.deployments.length === 1 ? project.deployments[0] : undefined
- const logs = newLogs || lastRunLogs
+ const steps = newSteps || lastRunLogs?.logs
async function run() {
if (!modelConfig) {
@@ -82,7 +84,7 @@ function Sidebar({
<Agent
run={run}
agentState={agentState}
- logs={logs}
+ steps={steps}
agentRun={agentRun}
/>
}
diff --git a/hooks/useAgentRun.ts b/hooks/useAgentRun.ts
index 5e3a25fa5..3cf9dc92a 100644
--- a/hooks/useAgentRun.ts
+++ b/hooks/useAgentRun.ts
@@ -1,23 +1,23 @@
import { useCallback, useState } from 'react'
import { deployment_state } from '@prisma/client'
-import { AgentRun } from 'api-client/AgentDebugRun'
-import { Log } from 'db/types'
+import { AgentRun, Step } from 'api-client/AgentRun'
import { ModelConfig } from 'state/model'
import { baseUrl } from 'api-client/api'
function useAgentRun() {
const [agentRun, setAgentRun] = useState<AgentRun>()
- const [logs, setLogs] = useState<Log[]>([])
+ const [steps, setSteps] = useState<Step[]>([])
const [agentState, setAgentState] = useState<deployment_state>()
const start = useCallback(async (projectID: string, modelConfig: ModelConfig) => {
const run = new AgentRun(`${baseUrl}/dev/agent`, {
- onLogs: setLogs,
+ onSteps: setSteps,
onStateChange: setAgentState,
+ onClose: () => setAgentRun(undefined),
})
- setLogs([])
+ setSteps([])
setAgentRun(run)
await run.connect()
try {
@@ -26,12 +26,12 @@ function useAgentRun() {
setAgentState(deployment_state.error)
console.error(error)
}
- }, [setAgentRun, setLogs, setAgentState])
+ }, [setAgentRun, setSteps, setAgentState])
return {
start,
agentRun,
- logs,
+ steps,
agentState,
}
}