Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix rpc connection; Add deploy routes [WIP]

Tomas Valentaa8ad9af96c30
diff --git a/.vscode/settings.json b/.vscode/settings.json
index be917e559..685356c2f 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -57,14 +57,16 @@
"python.formatting.provider": "black",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
-},
+ },
"python.autoComplete.extraPaths": [
"./**",
+ "./api-service/**",
"./api-service/.venv/bin/python",
],
"python.defaultInterpreterPath": "./api-service/.venv/bin/python",
"python.analysis.extraPaths": [
"./**",
+ "./api-service/**",
"./api-service/.venv/bin/python"
],
// Disable Pylint and use just Pylance
diff --git a/api-client/AgentRun.ts b/api-client/AgentConnection.ts
similarity index 67%
rename from api-client/AgentRun.ts
rename to api-client/AgentConnection.ts
index bf0588bc4..90895c0ff 100644
--- a/api-client/AgentRun.ts
+++ b/api-client/AgentConnection.ts
@@ -27,7 +27,7 @@ export interface Opts {
onStateChange: (runState: AgentRunState) => void
}
-export class AgentRun {
+export class AgentConnection {
private readonly rpc = new RpcWebSocketClient()
runID?: string
@@ -43,48 +43,58 @@ export class AgentRun {
async disconnect() {
console.log('closing')
// This is the browser WebSocket way of closing connection
+ // TODO: Test this connection closing
this.rpc.ws?.close()
}
private handleNotification(data: IRpcNotification) {
- if (data.method === 'steps') {
- return this.opts.onSteps(data.params.steps)
- }
- if (data.method === 'state_update') {
- this.opts.onStateChange(data.params.state)
- if (data.params.state === deployment_state.finished) {
- this.disconnect()
+ if (data.method === 'logs') {
+ if (data.params.logs) {
+ this.opts.onSteps(data.params.logs)
+ }
+ // return this.opts.onSteps(data.params.steps)
+ if (data.params.state) {
+ this.opts.onStateChange(data.params.state)
+ if (data.params.state === deployment_state.finished) {
+ this.disconnect()
+ }
}
}
}
- async startRun(projectID: string, modelConfig: ModelConfig) {
- const { run_id } = await this.rpc.call('start', [projectID, modelConfig]) as { run_id: string }
+ async start(projectID: string, modelConfig: ModelConfig) {
+ const { run_id } = await this.rpc.call('start', {
+ 'project_id': projectID,
+ 'model_config': modelConfig,
+ }) as { run_id: string }
+
this.runID = run_id
this.opts.onStateChange(AgentRunState.Running)
}
- async pauseRun() {
+ private async interaction(type: string, data?: any) {
if (!this.runID) return
- await this.rpc.call('pause')
+ await this.rpc.call('interaction', { type, data })
+ }
+
+ async pauseRun() {
+ await this.interaction('pause')
this.opts.onStateChange(AgentRunState.Paused)
}
async resumeRun() {
- if (!this.runID) return
- await this.rpc.call('resume')
+ await this.interaction('resume')
this.opts.onStateChange(AgentRunState.Running)
}
async cancelRun() {
if (!this.runID) return
- await this.rpc.call('cancel')
+ await this.rpc.call('stop')
this.opts.onStateChange(AgentRunState.None)
}
async rewriteRunSteps(steps: Step[]) {
- if (!this.runID) return
- await this.rpc.call('rewrite_steps', [steps])
+ await this.interaction('rewrite_steps', { steps })
this.opts.onStateChange(AgentRunState.Running)
}
diff --git a/api-service/agent/base.py b/api-service/agent/base.py
index 789e6282e..27263f5e5 100644
--- a/api-service/agent/base.py
+++ b/api-service/agent/base.py
@@ -1,5 +1,7 @@
-from abc import ABC, abstractmethod
-from typing import Dict, Literal
+from abc import abstractmethod, ABC
+from typing import Coroutine, Dict, Literal, Optional, Callable
+
+from pydantic import BaseModel, Json
# The schema for agent config and interaction data is serializable and
@@ -10,40 +12,44 @@ from typing import Dict, Literal
# that then be deployed on our platform.
# For local testing there can be a CLI that takes the implementation of AgentBase class
-# and just starts the server and exposes exposes the functionality with a premade web UI where you can test the agent immediately.
+# and just starts the server and exposes the functionality with a premade web UI where you can test the agent immediately.
# Later we can start supporting other ways to generate the server, for example a gRPC or WS server.
# This is orthogonal to the environment where the agent is deployed.
-# The agent can still can be deployed as a Python task, in a Firecracker VM, as a docker container, etc.
+# The agent can still be deployed as a Python task, in a Firecracker VM, as a docker container, etc.
-JsonValue = None | int | str | bool
-JsonType = None | Dict[str, JsonValue | "JsonType"]
+DataType = Dict[str, Json]
-class AgentLog:
- type: Literal["log", "error", "output"]
+class AgentLog(BaseModel):
+ type: Literal["log", "error", "output"] | str
timestamp: str
- data: JsonType
-
+ data: DataType
-AgentConfig = JsonType
-
-class AgentInteraction:
- interaction_id: str
+class AgentInteraction(BaseModel):
+ interaction_id: Optional[str] = None
type: str
- data: JsonType
+ data: DataType
-class AgentInteractionResponse:
- data: JsonType
+class AgentInteractionResponse(BaseModel):
+ data: DataType
-class AgentInteractionRequest:
+class AgentInteractionRequest(BaseModel):
interaction_id: str
type: str
- data: JsonType
+ data: DataType
+
+
+class AgentConfig(BaseModel):
+ data: Optional[DataType] = None
+ on_log: Callable[[AgentLog], Coroutine[None, None, None]]
+ on_interaction_request: Callable[
+ [AgentInteractionRequest], Coroutine[None, None, None]
+ ]
class AgentBase(ABC):
@@ -59,12 +65,12 @@ class AgentBase(ABC):
@classmethod
@abstractmethod
- def create(cls, config: AgentConfig) -> "AgentBase":
+ async def create(cls, config: AgentConfig) -> "AgentBase":
"""Create an agent with the given config without starting it."""
pass
@abstractmethod
- async def start(self):
+ async def start(self, data: DataType | None = None):
"""Start the agent. Started agent can immediately be interacting with the the world."""
pass
@@ -73,17 +79,14 @@ class AgentBase(ABC):
"""Stop the agent and clean used resources. Stopped agent cannot be started again."""
pass
+ @abstractmethod
+ async def get_interactions(self):
+ """Return a list of interactions that the agent can handle and that can be requested."""
+ pass
+
@abstractmethod
async def interaction(
self,
interaction: AgentInteraction,
) -> AgentInteractionResponse:
pass
-
- @abstractmethod
- async def handle_log(self, log: AgentLog):
- pass
-
- @abstractmethod
- async def handle_interaction_request(self, request: AgentInteractionRequest):
- pass
diff --git a/api-service/agent/simple_agent.py b/api-service/agent/basic_agent.py
similarity index 90%
rename from api-service/agent/simple_agent.py
rename to api-service/agent/basic_agent.py
index daa79f1b7..b7e527d15 100644
--- a/api-service/agent/simple_agent.py
+++ b/api-service/agent/basic_agent.py
@@ -3,6 +3,8 @@ import uuid
from abc import abstractmethod
from typing import Any, Dict, List, Literal, cast
+
+from agent.base import AgentInteraction
from agent.output.parse_output import ToolLog
from agent.output.output_stream_parser import OutputStreamParser, Step
from agent.output.token_callback_handler import TokenCallbackHandler
@@ -24,7 +26,7 @@ from langchain.agents.tools import InvalidTool
from agent.tools.base import create_tools
from database.database import DeploymentState
from models.base import ModelConfig, PromptPart, get_model
-from agent.base import AgentBase
+from agent.base import AgentBase, AgentConfig
from database import db
from session import NodeJSPlayground
@@ -33,8 +35,9 @@ class RewriteStepsException(Exception):
pass
-class SimpleAgent(AgentBase):
- def __init__(self, config: ModelConfig):
+class BasicAgent(AgentBase):
+ def __init__(self, config: AgentConfig):
+ super().__init__()
self.config = config
self.should_pause = False
self.canceled = False
@@ -42,6 +45,12 @@ class SimpleAgent(AgentBase):
self.llm_generation = None
self.can_resume = asyncio.Event()
+ @classmethod
+ async def create(cls, config: AgentConfig):
+ c = cls(config=config)
+ print("init")
+ return c
+
@staticmethod
async def _run_tool(
tool_log: ToolLog,
@@ -83,14 +92,14 @@ class SimpleAgent(AgentBase):
def _create_prompt(model_config: ModelConfig, tools: List[BaseTool]):
system_template = "\n\n".join(
[
- SimpleAgent._get_prompt_part(model_config.prompt, "system", "prefix"),
+ BasicAgent._get_prompt_part(model_config.prompt, "system", "prefix"),
"\n".join([f"{tool.name}: {tool.description}" for tool in tools]),
- SimpleAgent._get_prompt_part(model_config.prompt, "system", "suffix"),
+ BasicAgent._get_prompt_part(model_config.prompt, "system", "suffix"),
]
)
human_template = "\n\n".join(
[
- SimpleAgent._get_prompt_part(model_config.prompt, "user", "prefix"),
+ BasicAgent._get_prompt_part(model_config.prompt, "user", "prefix"),
"{agent_scratchpad}",
]
)
@@ -306,25 +315,36 @@ class SimpleAgent(AgentBase):
)
return {"run_id": self.run_id}
- async def pause(self):
+ async def interaction(self, interaction: AgentInteraction):
+ print("Agent interaction")
+ if interaction.type == "pause":
+ await self._pause()
+ elif interaction.type == "resume":
+ await self._resume()
+ elif interaction.type == "rewrite_steps":
+ await self._rewrite_steps(interaction.data["steps"])
+ else:
+ raise Exception(f"Unknown interaction action: {interaction.type}")
+
+ async def _pause(self):
print("Pause agent run")
self.should_pause = True
- async def resume(self):
+ async def _resume(self):
print("Resume agent run")
self.should_pause = False
self.can_resume.set()
- async def cancel(self):
+ async def stop(self):
print("Cancel agent run")
self.canceled = True
if self.llm_generation:
self.llm_generation.cancel()
await self._close()
- async def rewrite_steps(self, steps: List[Step]):
+ async def _rewrite_steps(self, steps: List[Step]):
print("Rewrite agent run steps")
- await self.pause()
+ await self._pause()
self.rewriting_steps = True
if self.llm_generation:
self.llm_generation.cancel()
@@ -335,4 +355,4 @@ class SimpleAgent(AgentBase):
steps=steps[:-1],
buffered_step=steps[-1],
)
- await self.resume()
+ await self._resume()
diff --git a/api-service/agent/json_rpc_connector.py b/api-service/agent/json_rpc_connector.py
new file mode 100644
index 000000000..193514ffa
--- /dev/null
+++ b/api-service/agent/json_rpc_connector.py
@@ -0,0 +1,96 @@
+from typing import Any, AsyncIterator, Callable, Coroutine, Dict
+from pydantic import BaseModel
+from pydantic.fields import PrivateAttr
+
+from agent.base import AgentConfig, AgentInteraction, AgentBase
+from agent.basic_agent import BasicAgent
+
+
+class JsonRpcAgentConnector(BaseModel):
+ jsonrpc_version = "2.0"
+
+ agent: AgentBase | None = None
+ agent_factory: Callable[[AgentConfig], Coroutine[None, None, AgentBase]]
+
+ send_json: Callable[[Any], Coroutine[Any, Any, None]]
+ iter_json: Callable[[], AsyncIterator[Any]]
+
+ 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] | None):
+ async def handle_call():
+ try:
+ print(f"Calling {method} via JSONRPC")
+ match method:
+ case "start":
+ if self.agent is None:
+ raise Exception("Agent not found")
+ await self.agent.start(params and params.get("data"))
+ return {}
+
+ case "stop":
+ if self.agent is None:
+ raise Exception("Agent not found")
+ await self.agent.stop()
+ return {}
+ 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["data"],
+ type=params["type"],
+ )
+ )
+ return {"result": result}
+ case default:
+ 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()
+ 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(self):
+ self.agent = await self.agent_factory(
+ AgentConfig(
+ on_log=lambda log: self._notify("log", {"log": log}),
+ on_interaction_request=lambda request: self._notify(
+ "interaction_request", {"request": request}
+ ),
+ )
+ )
+ print("agenr", self.agent)
+
+ async for data in self.iter_json():
+ await self._call(
+ data["id"],
+ data["method"],
+ data.get("params", {}),
+ )
diff --git a/api-service/agent/ws.py b/api-service/agent/ws.py
deleted file mode 100644
index cb034ad4f..000000000
--- a/api-service/agent/ws.py
+++ /dev/null
@@ -1,65 +0,0 @@
-from typing import Any, Dict, List
-from fastapi import WebSocket
-
-from agent.simple_agent import SimpleAgent
-
-
-class WebsocketAgentRun(SimpleAgent):
- jsonrpc_version = "2.0"
-
- def __init__(self, websocket: WebSocket, **kwargs):
- super().__init__(**kwargs)
- self.websocket = websocket
- 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,
- }
- )
-
- async def _close(self):
- 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():
- try:
- method_handler = getattr(self, method)
- if method_handler is None:
- raise Exception(f"Method not found {method}")
-
- result = await method_handler(*params)
- return {
- "result": result,
- }
- except Exception as e:
- print(f"Error calling {method} via JSONRPC", e)
- return {
- "error": {
- "code": 13,
- "message": str(e),
- }
- }
-
- response = await handle_call()
- await self.websocket.send_json(
- {
- "jsonrpc": self.jsonrpc_version,
- "id": id,
- **response,
- }
- )
-
- async def handle_agent_run(self):
- await self.websocket.accept()
-
- 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/api-service/app.py b/api-service/app.py
index 13d1822f5..b3c84bf01 100644
--- a/api-service/app.py
+++ b/api-service/app.py
@@ -1,12 +1,14 @@
-from fastapi import FastAPI, WebSocket
+from typing import Literal
+from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from models.base import ModelConfig
-
-from deployment.library_deployment import LibraryDeployment
-from agent.ws import WebsocketAgentRun
-from agent.simple_agent import SimpleAgent
+from agent.base import AgentInteraction
+from agent.base import AgentConfig
+from deployment.in_memory import InMemoryDeploymentManager
+from agent.json_rpc_connector import JsonRpcAgentConnector
+from agent.basic_agent import BasicAgent
# TODO: Fix proxying - https://fastapi.tiangolo.com/advanced/behind-a-proxy/
app = FastAPI(title="e2b-api-service")
@@ -18,48 +20,51 @@ app.add_middleware(
allow_headers=["*"],
)
-deployment_manager = LibraryDeployment(agent_factory=SimpleAgent.create)
-debug_agent_manager = LibraryDeployment(agent_factory=SimpleAgent.create)
+deployment_manager = InMemoryDeploymentManager(agent_factory=BasicAgent.create)
+
@app.websocket("/dev/agent")
async def ws_agent_run(websocket: WebSocket):
- run = WebsocketAgentRun(websocket)
- await run.handle_agent_run()
-
-
-async def health():
- return {"status": "ok"}
-
+ print("ws_agent_run")
+ await websocket.accept()
+ connector = JsonRpcAgentConnector(
+ iter_json=websocket.iter_json,
+ send_json=websocket.send_json,
+ agent_factory=BasicAgent.create,
+ )
+ try:
+ await connector.handle()
+ except:
+ await connector.close()
-class DeployConfig(BaseModel):
- pass
-
-class AgentCreateBody(BaseModel):
- model_config: ModelConfig
- deploy_config: DeployConfig
-
-
-class AgentRunBody(BaseModel):
- instructions: str
+async def create_agent(body: AgentConfig):
+ deployment = await deployment_manager.create_deployment(body)
+ return {"id": deployment.id}
-async def create_agent(body: AgentCreateBody):
- await deployment_manager.create_deployed_agent(body.model_config)
+class AgentStatusBody(BaseModel):
+ status: Literal["running", "stopped"]
[email protected]("/agent/{id}/run")
-async def run_agent(id: str, body: AgentRunBody):
- pass
[email protected]("/agent/{id}/status")
+async def change_agent_status(id: str, body: AgentStatusBody):
+ deployment = await deployment_manager.get_deployment(id)
+ if body.status == "running":
+ await deployment.start()
+ elif body.status == "stopped":
+ await deployment.stop()
[email protected]("/agent/{id}/run/{run_id}/status")
-async def get_agent_run_status(id: str, run_id: str):
- pass
[email protected]("/agent/{id}/interaction")
+async def interact_with_agent(id: str, body: AgentInteraction):
+ deployment = await deployment_manager.get_deployment(id)
+ result = await deployment.agent.interaction(body)
+ return result
[email protected]("/agent/{id}/run/{run_id}/result")
-async def get_agent_run_result(id: str, run_id: str):
- pass
[email protected]("/agent/{id}/status")
+async def get_agent_status(id: str):
+ deployment = await deployment_manager.get_deployment(id)
+ return {"status": deployment.status}
diff --git a/api-service/cli.py b/api-service/cli.py
new file mode 100644
index 000000000..a4240272f
--- /dev/null
+++ b/api-service/cli.py
@@ -0,0 +1,81 @@
+import sys
+import importlib
+from fastapi import FastAPI
+from pydantic import BaseModel
+from typing import Callable, Dict, Any
+from uvicorn import run
+
+# Replace the following with the actual implementation of AgentBase class
+from agent.base import (
+ AgentBase,
+ AgentConfig,
+ AgentInteraction,
+ AgentInteractionResponse,
+ DataType,
+)
+
+app = FastAPI()
+
+agent_instance = None
+
+
[email protected]("/agent", status_code=201)
+def create_agent(config: AgentConfig):
+ global agent_instance
+ agent_instance = config.create()
+ return {"status": "Agent created successfully"}
+
+
[email protected]("/agent/start")
+def start_agent(data: DataType):
+ if not agent_instance:
+ return {"error": "Agent not created"}
+ agent_instance.start(data)
+ return {"status": "Agent started successfully"}
+
+
[email protected]("/agent/stop")
+def stop_agent():
+ if not agent_instance:
+ return {"error": "Agent not created"}
+ agent_instance.stop()
+ return {"status": "Agent stopped successfully"}
+
+
[email protected]("/agent/interactions")
+def get_interactions():
+ if not agent_instance:
+ return {"error": "Agent not created"}
+ return agent_instance.get_interactions()
+
+
[email protected]("/agent/interaction")
+def interaction(interaction: AgentInteraction) -> AgentInteractionResponse:
+ if not agent_instance:
+ return {"error": "Agent not created"}
+ return agent_instance.interaction(interaction)
+
+
+def main():
+ if len(sys.argv) != 3:
+ print("Usage: python agent_cli.py <module_name> <class_name>")
+ sys.exit(1)
+
+ module_name = sys.argv[1]
+ class_name = sys.argv[2]
+
+ try:
+ module = importlib.import_module(module_name)
+ agent_class = getattr(module, class_name)
+ if not issubclass(agent_class, AgentBase):
+ raise ValueError(f"{class_name} is not a subclass of AgentBase")
+ except Exception as e:
+ print(f"Error: {e}")
+ sys.exit(1)
+
+ # Start the FastAPI server
+ run(app, host="localhost", port=8080)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/api-service/deployment/adapter.py b/api-service/deployment/adapter.py
deleted file mode 100644
index eef664f50..000000000
--- a/api-service/deployment/adapter.py
+++ /dev/null
@@ -1,33 +0,0 @@
-from typing import List, Tuple
-from abc import ABC, abstractmethod
-
-from agent.base import AgentBase, AgentConfig
-
-
-class DeploymentAdapter(ABC):
- """
- This is an abstract class that defines the interface for a deployment adapter.
-
- With this interface, we can create different deployment adapters that can be used
- to deploy and manage agents in different ways.
-
- For example, we can have a deployment adapter that deploys agents created by our library (without the need to use FCs right now) as Python tasks,
- a deployment adapter that deploys agents with arbiratry code in a Firecracker VM when we need it,
- or a deployment adapter that deploys agents as a docker container later.
- """
-
- @abstractmethod
- async def create_deployed_agent(self, config: AgentConfig) -> Tuple[str, AgentBase]:
- pass
-
- @abstractmethod
- async def remove_deployed_agent(self, deployment_id: str):
- pass
-
- @abstractmethod
- async def get_deployed_agent(self, deployment_id: str) -> AgentBase:
- pass
-
- @abstractmethod
- async def get_deployed_agents(self) -> List[Tuple[str, AgentBase]]:
- pass
diff --git a/api-service/deployment/in_memory.py b/api-service/deployment/in_memory.py
new file mode 100644
index 000000000..6bf4a5c54
--- /dev/null
+++ b/api-service/deployment/in_memory.py
@@ -0,0 +1,35 @@
+import uuid
+
+from typing import Callable, Coroutine, Dict
+
+from pydantic import PrivateAttr
+
+from deployment.manager import AgentDeploymentManager, AgentDeployment
+from agent.base import AgentBase, AgentConfig
+
+
+class InMemoryDeploymentManager(AgentDeploymentManager):
+ agent_factory: Callable[[AgentConfig], Coroutine[None, None, AgentBase]]
+ _deployments: Dict[str, AgentDeployment] = PrivateAttr(default={})
+
+ async def create_deployment(self, config: AgentConfig):
+ agent = await self.agent_factory(config)
+ deployment = AgentDeployment(
+ id=str(uuid.uuid4()),
+ agent=agent,
+ )
+ self._deployments[deployment.id] = deployment
+ return deployment
+
+ async def remove_deployment(self, id: str):
+ deployment = await self.get_deployment(id)
+ if deployment is None:
+ return
+ await deployment.agent.stop()
+ del self._deployments[deployment.id]
+
+ async def get_deployment(self, id: str):
+ return self._deployments[id]
+
+ async def list_deployments(self):
+ return list(self._deployments.values())
diff --git a/api-service/deployment/library_deployment.py b/api-service/deployment/library_deployment.py
deleted file mode 100644
index 409725fa5..000000000
--- a/api-service/deployment/library_deployment.py
+++ /dev/null
@@ -1,31 +0,0 @@
-import uuid
-
-from typing import Callable, Dict, List, Tuple
-
-from deployment.adapter import DeploymentAdapter
-from agent.base import AgentBase, AgentConfig
-
-
-class LibraryDeployment(DeploymentAdapter):
- def __init__(self, agent_factory: Callable[[AgentConfig], AgentBase]) -> None:
- self._agent_factory = agent_factory
- self._deployments: Dict[str, AgentBase] = {}
-
- async def create_deployed_agent(self, config: AgentConfig):
- agent = self._agent_factory(config)
- id = str(uuid.uuid4())
- self._deployments[id] = agent
- return (id, agent)
-
- async def remove_deployed_agent(self, deployment_id: str):
- agent = await self.get_deployed_agent(deployment_id)
- await agent.stop()
- del self._deployments[deployment_id]
-
- async def get_deployed_agent(self, deployment_id: str):
- return self._deployments[deployment_id]
-
- async def get_deployed_agents(self) -> List[Tuple[str, AgentBase]]:
- return [
- (deployment_id, agent) for deployment_id, agent in self._deployments.items()
- ]
diff --git a/api-service/deployment/manager.py b/api-service/deployment/manager.py
new file mode 100644
index 000000000..c17d325c7
--- /dev/null
+++ b/api-service/deployment/manager.py
@@ -0,0 +1,51 @@
+from typing import List, Literal
+from abc import abstractmethod
+from pydantic import BaseModel
+
+from agent.base import AgentBase, AgentConfig, AgentInteractionRequest
+
+
+class AgentDeployment(BaseModel):
+ id: str
+ agent: AgentBase
+ status: Literal["running", "stopped"] = "stopped"
+ interaction_requests: List[AgentInteractionRequest] = []
+
+ async def start(self):
+ await self.agent.start()
+ self.status = "running"
+
+ async def stop(self):
+ await self.agent.stop()
+ self.status = "stopped"
+
+
+
+
+class AgentDeploymentManager(BaseModel):
+ """
+ This is an abstract class that defines the interface for a deployment adapter.
+
+ With this interface, we can create different deployment adapters that can be used
+ to deploy and manage agents in different ways.
+
+ For example, we can have a deployment adapter that deploys agents created by our library (without the need to use FCs right now) as Python tasks,
+ a deployment adapter that deploys agents with arbiratry code in a Firecracker VM when we need it,
+ or a deployment adapter that deploys agents as a docker container later.
+ """
+
+ @abstractmethod
+ async def create_deployment(self, config: AgentConfig) -> AgentDeployment:
+ pass
+
+ @abstractmethod
+ async def remove_deployment(self, id: str):
+ pass
+
+ @abstractmethod
+ async def get_deployment(self, id: str) -> AgentDeployment | None:
+ pass
+
+ @abstractmethod
+ async def list_deployments(self) -> List[AgentDeployment]:
+ pass
diff --git a/components/Editor/Sidebar/Agent/AgentRunControls.tsx b/components/Editor/Sidebar/Agent/AgentRunControls.tsx
index 5f75d6e0a..44c4a6d14 100644
--- a/components/Editor/Sidebar/Agent/AgentRunControls.tsx
+++ b/components/Editor/Sidebar/Agent/AgentRunControls.tsx
@@ -2,13 +2,13 @@ import { ReactNode, memo, useMemo, useCallback } from 'react'
import { Ban, Pause, Play } from 'lucide-react'
import Button from 'components/Button'
-import { AgentRun, AgentRunState } from 'api-client/AgentRun'
+import { AgentConnection, AgentRunState } from 'api-client/AgentConnection'
export interface Props {
agentState?: AgentRunState
run: () => void
- agentRun?: AgentRun
+ agentRun?: AgentConnection
disabled?: boolean
}
diff --git a/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx b/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx
index f7ad64fdd..8fad7bdb6 100644
--- a/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx
+++ b/components/Editor/Sidebar/Agent/StepsStream/StepEditor.tsx
@@ -6,7 +6,7 @@ import { LogType, ToolName, } from 'db/types'
import LogEntry from '../LogEntry'
import Text from 'components/Text'
-import { Step } from 'api-client/AgentRun'
+import { Step } from 'api-client/AgentConnection'
import { notEmpty } from 'utils/notEmpty'
export interface Props {
diff --git a/components/Editor/Sidebar/Agent/StepsStream/index.tsx b/components/Editor/Sidebar/Agent/StepsStream/index.tsx
index c8ddc0d0a..f01a06434 100644
--- a/components/Editor/Sidebar/Agent/StepsStream/index.tsx
+++ b/components/Editor/Sidebar/Agent/StepsStream/index.tsx
@@ -1,7 +1,7 @@
import { useEffect, useRef } from 'react'
import { ToolName, } from 'db/types'
-import { Step, StepEdit } from 'api-client/AgentRun'
+import { Step, StepEdit } from 'api-client/AgentConnection'
import StepEditor from './StepEditor'
diff --git a/components/Editor/Sidebar/Agent/index.tsx b/components/Editor/Sidebar/Agent/index.tsx
index c665e8f68..d1fdaa68a 100644
--- a/components/Editor/Sidebar/Agent/index.tsx
+++ b/components/Editor/Sidebar/Agent/index.tsx
@@ -10,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, AgentRunState, Step, StepEdit } from 'api-client/AgentRun'
+import { AgentConnection, AgentRunState, Step, StepEdit } from 'api-client/AgentConnection'
import StepsStream from './StepsStream'
import AgentRunControls from './AgentRunControls'
@@ -33,7 +33,7 @@ export interface Props {
steps?: Step[]
agentState?: AgentRunState
run: () => void
- agentRun?: AgentRun
+ agentRun?: AgentConnection
}
function Agent({
@@ -76,7 +76,7 @@ function Agent({
async function onEdit(edit: StepEdit) {
if (!steps || !agentRun) return
- const editedSteps = AgentRun.resolveStepsEdit(steps, edit)
+ const editedSteps = AgentConnection.resolveStepsEdit(steps, edit)
if (!editedSteps) return
await agentRun.rewriteRunSteps(editedSteps)
}
diff --git a/components/Editor/Sidebar/index.tsx b/components/Editor/Sidebar/index.tsx
index 45383093f..0762d09af 100644
--- a/components/Editor/Sidebar/index.tsx
+++ b/components/Editor/Sidebar/index.tsx
@@ -5,7 +5,7 @@ import { useStateStore } from 'state/StoreProvider'
import { getModelArgs } from 'state/model'
import useModelProviderArgs from 'hooks/useModelProviderArgs'
import { evaluatePrompt } from 'state/prompt'
-import { Step } from 'api-client/AgentRun'
+import { Step } from 'api-client/AgentConnection'
import useAgentRun from 'hooks/useAgentRun'
import Agent from './Agent'
diff --git a/hooks/useAgentRun.ts b/hooks/useAgentRun.ts
index a54b9c8b6..787c3cd79 100644
--- a/hooks/useAgentRun.ts
+++ b/hooks/useAgentRun.ts
@@ -1,16 +1,16 @@
import { useCallback, useState } from 'react'
-import { AgentRun, AgentRunState, Step } from 'api-client/AgentRun'
+import { AgentConnection, AgentRunState, Step } from 'api-client/AgentConnection'
import { ModelConfig } from 'state/model'
import { baseUrl } from 'api-client/api'
function useAgentRun() {
- const [agentRun, setAgentRun] = useState<AgentRun>()
+ const [agentRun, setAgentRun] = useState<AgentConnection>()
const [steps, setSteps] = useState<Step[]>()
const [agentState, setAgentState] = useState<AgentRunState>()
const start = useCallback(async (projectID: string, modelConfig: ModelConfig) => {
- const run = new AgentRun(`${baseUrl}/dev/agent`, {
+ const run = new AgentConnection(`${baseUrl}/dev/agent`, {
onSteps: setSteps,
onStateChange: setAgentState,
onClose: () => {
@@ -22,7 +22,7 @@ function useAgentRun() {
setSteps([])
setAgentRun(run)
await run.connect()
- await run.startRun(projectID, modelConfig)
+ await run.start(projectID, modelConfig)
}, [setAgentRun, setSteps, setAgentState])
return {