Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Cleanup API

Tomas Valenta184defaa14ec
diff --git a/api-service/app.py b/api-service/app.py
index d386d1819..b60e14250 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -2,7 +2,7 @@ import uuid
import os
from typing import Annotated, Any
-from fastapi import Depends, FastAPI, HTTPException, WebSocket
+from fastapi import Depends, FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from pydantic import BaseModel
@@ -11,7 +11,6 @@ from fastapi.security import OAuth2PasswordBearer
from agent.base import AgentInteraction
from deployment.in_memory import InMemoryDeploymentManager
-from json_rpc import JsonRpcAgentConnection
from database.base import db
deployment_manager = InMemoryDeploymentManager()
@@ -66,23 +65,6 @@ async def health():
return {"Status": "Ok"}
[email protected]("/dev/agent")
-async def ws_agent_run(websocket: WebSocket, project_id: str):
- await websocket.accept()
-
- connection = JsonRpcAgentConnection(
- project_id=project_id,
- iter_json=websocket.iter_json,
- send_json=websocket.send_json,
- )
- try:
- await connection.handle()
- except:
- await connection.close()
- finally:
- print("Closing websocket")
-
-
class CreateDeploymentBody(BaseModel):
config: Any
diff --git a/api-service/json_rpc.py b/api-service/json_rpc.py
deleted file mode 100644
index fa1e5a839..000000000
--- a/api-service/json_rpc.py
+++ /dev/null
@@ -1,114 +0,0 @@
-import asyncio
-
-from typing import Any, AsyncIterator, Callable, Coroutine, Dict, List
-
-from agent.base import AgentBase
-from agent.from_template import get_agent_factory_from_template
-from database import db
-from agent.base import AgentInteraction, AgentInteractionRequest
-
-
-class JsonRpcAgentConnection:
- jsonrpc_version = "2.0"
-
- def __init__(
- self,
- send_json: Callable[[Any], Coroutine[Any, Any, None]],
- iter_json: Callable[[], AsyncIterator[Any]],
- project_id: str,
- ) -> None:
- self._project_id = project_id
- self._agent: AgentBase | None = None
- self._send_json = send_json
- self._iter_json = iter_json
-
- async def _notify(self, method: str, params: Dict[str, Any]):
- await self._send_json(
- {
- "jsonrpc": self.jsonrpc_version,
- "method": method,
- "params": params,
- }
- )
-
- async def _call(self, id: str, method: str, params: Dict[str, Any] = {}):
- async def handle_call():
- try:
- print(f"Calling {method} via JSONRPC")
- match method:
- case "start":
- agent_factory = get_agent_factory_from_template(
- params["config"]["templateID"]
- )
- self._agent = await agent_factory(
- params["config"],
- lambda: db.get_env_vars(self._project_id),
- self._handle_logs,
- self._handle_interaction_request,
- )
- print("config", params["config"])
- await self._agent.interaction(
- AgentInteraction(
- type="start",
- data={"instructions": params["instructions"]},
- )
- )
- case "stop":
- if self._agent is None:
- raise Exception("Agent not found")
- await self._agent.stop()
- case "interaction":
- if self._agent is None:
- raise Exception("Agent not found")
- if params is None:
- raise Exception("Interaction params not found")
- result = await self._agent.interaction(
- AgentInteraction(
- interaction_id=id,
- data=params.get("data", None),
- type=params["type"],
- )
- )
- return {"result": result}
- case _:
- raise Exception(f"Method {method} not found")
- except Exception as e:
- print(f"Error calling {method} via JSONRPC", e)
- return {
- "error": {
- "code": 13,
- "message": str(e),
- }
- }
-
- response = await handle_call() or {"result": None}
- print(f"Sending response for {method} via JSONRPC", response)
- await self._send_json(
- {
- "jsonrpc": self.jsonrpc_version,
- "id": id,
- **response,
- }
- )
-
- async def close(self):
- if self._agent is not None:
- await self._agent.stop()
-
- async def _handle_logs(self, logs: List[Any]):
- await self._notify("logs", {"logs": logs})
- asyncio.create_task(db.update_project_developent_logs(self._project_id, logs))
-
- async def _handle_interaction_request(self, request: AgentInteractionRequest):
- await self._notify(
- "interaction_request",
- request.dict(),
- )
-
- async def handle(self):
- async for data in self._iter_json():
- await self._call(
- data["id"],
- data["method"],
- data.get("params", {}),
- )