Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Save steps to db with a separate queue

Tomas Valenta5af79e1cb67d
diff --git a/api-service/agent/base.py b/api-service/agent/base.py
index da64d0aba..edd829179 100644
--- a/api-service/agent/base.py
+++ b/api-service/agent/base.py
@@ -110,7 +110,7 @@ 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(
+ await db.upsert_deployment_state(
run_id=run_id,
project_id=project_id,
state=state,
@@ -154,9 +154,20 @@ class AgentRun:
# -----
# Create log handlers
# Used for sending logs to db/frontend without blocking
- steps_streamer = WorkQueue[List[Step]](
+ steps_ws_streamer = WorkQueue[List[Step]](
on_workload=lambda steps: self._notify("steps", {"steps": steps})
)
+ steps_db_streamer = WorkQueue[List[Step]](
+ on_workload=lambda steps: db.upsert_deployment_steps(
+ run_id=self.run_id,
+ steps=steps,
+ )
+ )
+
+ def stream_steps(steps: List[Step]):
+ steps_db_streamer.schedule(steps)
+ steps_ws_streamer.schedule(steps)
+
# Used for parsing token stream into logs+actions
self._output_parser = OutputStreamParser(tool_names=self.tool_names)
@@ -220,7 +231,7 @@ class AgentRun:
await self._check_interrupt()
# Process logs in a separate task
steps = self._output_parser.ingest_token(token).get_steps()
- steps_streamer.schedule(steps)
+ stream_steps(steps)
current_step = self._output_parser.get_current_step()
@@ -245,7 +256,7 @@ class AgentRun:
steps = self._output_parser.ingest_tool_output(
tool_output
).get_steps()
- steps_streamer.schedule(steps)
+ stream_steps(steps)
except RewriteStepsException:
continue
@@ -271,7 +282,7 @@ class AgentRun:
raise RewriteStepsException()
@abstractmethod
- async def _notify(self, method: str, params: Dict[str, Any] | List[Any]):
+ async def _notify(self, method: str, params: Dict[str, Any]):
pass
@abstractmethod
@@ -280,15 +291,16 @@ class AgentRun:
async def start(self, project_id: str, model_config: Dict[str, Any]):
print("Start agent run")
- run_id = str(uuid.uuid4())
+ self.project_id = project_id
+ self.run_id = str(uuid.uuid4())
asyncio.create_task(
self._run(
- run_id=run_id,
+ run_id=self.run_id,
project_id=project_id,
model_config=ModelConfig(**model_config),
)
)
- return {"run_id": run_id}
+ return {"run_id": self.run_id}
async def pause(self):
print("Pause agent run")
diff --git a/api-service/database/database.py b/api-service/database/database.py
index 7b528c75d..a2c289859 100644
--- a/api-service/database/database.py
+++ b/api-service/database/database.py
@@ -1,5 +1,6 @@
from typing import List
from enum import Enum
+from agent.output.output_stream_parser import Step
from agent.output.parse_output import ThoughtLog, ToolLog
from database.client import Client
@@ -37,7 +38,20 @@ class Database:
}
).eq("id", run_id).execute()
- async def upsert_deployment(
+ async def upsert_deployment_steps(
+ self,
+ run_id: str,
+ steps: List[Step],
+ ) -> None:
+ await self.client.table(TABLE_DEPLOYMENTS).upsert(
+ {
+ "id": run_id,
+ "steps": steps,
+ },
+ on_conflict="id",
+ ).execute()
+
+ async def upsert_deployment_state(
self,
run_id: str,
project_id: str,