Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Fix smol dev log DB updating
Tomas Valentaaee5fa1cf9be
diff --git a/api-service/agent/base.py b/api-service/agent/base.pyindex 619b6b32e..e2f5a56fa 100644--- a/api-service/agent/base.py+++ b/api-service/agent/base.py@@ -32,6 +32,7 @@ class AgentInteractionRequest(BaseModel):OnLogs = Callable[[Any], Coroutine[None, None, None]]+SetRun = Callable[[str], None]OnInteractionRequest = Callable[[AgentInteractionRequest], Coroutine[None, None, None]]GetEnvs = Callable[[], Coroutine[Any, Any, List[EnvVar]]]diff --git a/api-service/agent/basic_agent.py b/api-service/agent/basic_agent.pydeleted file mode 100644index bd37c805d..000000000--- a/api-service/agent/basic_agent.py+++ /dev/null@@ -1,375 +0,0 @@-import asyncio-import uuid-import chevron--# Monkey patch chevron to not escape html-chevron.render.__globals__["_html_escape"] = lambda string: string--from typing import Any, Dict, List, Literal, cast-from langchain.agents.tools import InvalidTool-from langchain.callbacks.base import AsyncCallbackManager-from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler-from langchain.chains.llm import LLMChain-from langchain.schema import BaseMessage, SystemMessage, HumanMessage-from langchain.tools import BaseTool-from langchain.prompts.chat import (- AIMessagePromptTemplate,- BaseMessagePromptTemplate,- ChatPromptTemplate,-)--from agent.base import (- AgentInteraction,- OnLogs,- OnInteractionRequest,-)-from agent.output.parse_output import 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 agent.tools.base import create_tools-from models.base import ModelConfig, PromptPart, get_model-from agent.base import AgentBase, AgentInteractionRequest, GetEnvs-from session import NodeJSPlayground---class RewriteStepsException(Exception):- pass---class BasicAgent(AgentBase):- max_run_time = 60 * 60 * 24 # in seconds-- def __init__(- self,- config: Any,- get_envs: GetEnvs,- on_logs: OnLogs,- on_interaction_request: OnInteractionRequest,- ):- super().__init__()- self._start_loop: asyncio.Task | None = None- self.get_envs = get_envs- self.config = ModelConfig(**config)- self.on_interaction_request = on_interaction_request- self.on_logs = on_logs- self.should_pause = False- self.canceled = False- self.rewriting_steps = False- self.llm_generation = None- self.can_resume = asyncio.Event()-- @classmethod- async def create(- cls,- config: Any,- get_envs: GetEnvs,- on_logs: OnLogs,- on_interaction_request: OnInteractionRequest,- ):- return cls(- config,- get_envs,- on_logs,- on_interaction_request,- )-- @staticmethod- async def _run_tool(- tool_log: ToolLog,- name_to_tool_map: Dict[str, BaseTool],- ) -> str:- # Otherwise we lookup the tool- 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_log["tool_input"],- verbose=True,- )- else:- observation = await InvalidTool().arun( # type: ignore- tool_name=tool_log["tool_name"],- tool_input=tool_log["tool_input"],- verbose=True,- )- return observation-- @staticmethod- def _get_prompt_part(- prompts: List[PromptPart],- role: Literal["user", "system"],- type: str,- ):- return (- next(- prompt- for prompt in prompts- if prompt.role == role and prompt.type == type- )- # Double the parenthesses to escape them because the string will be prompt templated.- .content- )-- @staticmethod- def _create_prompt(- config: ModelConfig,- tools: List[BaseTool],- instructions: Any,- ):- system_template = chevron.render(- "\n\n".join(- [- BasicAgent._get_prompt_part(config.prompt, "system", "prefix"),- "\n".join([f"{tool.name}: {tool.description}" for tool in tools]),- BasicAgent._get_prompt_part(config.prompt, "system", "suffix"),- ]- ),- instructions,- )-- human_template = chevron.render(- "\n\n".join(- [- BasicAgent._get_prompt_part(config.prompt, "user", "prefix"),- ]- ),- instructions,- )-- messages: List[- BaseMessage | BaseMessagePromptTemplate | SystemMessage | HumanMessage- ] = [- SystemMessage(content=system_template),- HumanMessage(content=human_template),- AIMessagePromptTemplate.from_template("{agent_scratchpad}"),- ]- return ChatPromptTemplate(- input_variables=["agent_scratchpad"],- messages=messages,- )-- async def _run(self, instructions: Any):- playground = None- try:- # ------ # Initialize callback manager and handler for controlling token stream- callback_manager = AsyncCallbackManager([StreamingStdOutCallbackHandler()])- self.token_handler = TokenCallbackHandler()- callback_manager.add_handler(self.token_handler)-- # ------ # Create tools- # Create playground for code tools- playground = NodeJSPlayground(get_envs=self.get_envs)- await playground.open()-- tools = list(create_tools(playground=playground))- 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 LLM from specified models- prompt = self._create_prompt(self.config, tools, instructions)- llm_chain = LLMChain(- llm=get_model(self.config, callback_manager),- prompt=prompt,- callback_manager=callback_manager,- verbose=True,- )-- # ------ # Create log handlers- # Used for sending logs to db/frontend without blocking- steps_streamer = WorkQueue[List[Step]](- on_workload=lambda steps: self.on_logs(steps),- )-- def stream_steps(steps: List[Step]):- steps_streamer.schedule(steps)-- # Used for parsing token stream into logs+actions- self._output_parser = OutputStreamParser(tool_names=self.tool_names)-- # This function is used to inject information from previous steps to the current prompt- # TODO: Improve the scratchpad handling and prompts for templates- def get_agent_scratchpad():- steps = self._output_parser.get_steps()-- if (- len(steps) == 0- or len(steps) == 1- and not self._output_parser._token_buffer- ):- return ""-- agent_scratchpad = ""- 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 is my previous work (I will continue where I left off):\n{agent_scratchpad}"-- print("Generating...", flush=True)- while True:- try:- await self._check_interrupt()- # Query the LLM in background- scratchpad = get_agent_scratchpad()- self.llm_generation = asyncio.create_task(- llm_chain.agenerate(- [- {- "agent_scratchpad": scratchpad,- "stop": "Observation:",- }- ]- )- )- while True:- await self._check_interrupt()- # Consume the tokens from LLM- token = await self.token_handler.retrieve_token()-- if token is None:- break-- await self._check_interrupt()- self._output_parser.ingest_token(token)- stream_steps(self._output_parser.get_steps())-- current_step = self._output_parser.get_current_step()-- # Check if the run is finished- if any(- keyword in current_step["output"]- for keyword in ("Final Answer", "FinalAnswer", "Finished")- ):- break-- for tool in (- cast(ToolLog, action)- for action in current_step["logs"]- if action["type"] == "tool"- ):- await self._check_interrupt()- tool_output = await self._run_tool(- tool,- name_to_tool_map=tool_map,- )- await self._check_interrupt()- self._output_parser.ingest_tool_output(tool_output)- stream_steps(self._output_parser.get_steps())-- except RewriteStepsException:- print("REWRITING")- continue-- except:- raise- finally:- if playground is not None:- await playground.close()- await self.on_interaction_request(- AgentInteractionRequest(- interaction_id=str(uuid.uuid4()),- type="done",- )- )-- async def _check_interrupt(self):- if self.canceled:- raise Exception("Canceled")-- if self.should_pause:- await self.can_resume.wait()- self.can_resume.clear()-- if self.rewriting_steps:- self.rewriting_steps = False- raise RewriteStepsException()-- async def _start(self, instructions: Any):- print("Start agent run")-- async def start_with_timeout():- try:- self._start_loop = asyncio.create_task(- self._run(instructions=instructions),- )-- await asyncio.wait_for(- self._start_loop,- timeout=self.max_run_time,- )- except asyncio.TimeoutError:- await self.stop()- print("Timeout")-- asyncio.create_task(- start_with_timeout(),- )-- async def _pause(self):- print("Pause agent run")- self.should_pause = True-- async def _resume(self):- print("Resume agent run")- self.should_pause = False- self.can_resume.set()-- async def _rewrite_steps(self, steps: List[Step]):- print("Rewrite agent run steps")- await self._pause()- self.rewriting_steps = True- if self.llm_generation:- self.llm_generation.cancel()- # TODO: Communicate the rewriting in model prompt - inform about what was rewritten.- # TODO: Handle token that is generated after a rewrite - if the current step seems "finished" the model still outputs string (usually something like . or :).- self._output_parser = OutputStreamParser(- tool_names=self.tool_names,- steps=steps[:-1],- buffered_step=steps[-1],- )- await self._resume()-- async def interaction(self, interaction: AgentInteraction):- print("Agent interaction")- match interaction.type:- case "pause":- await self._pause()- case "resume":- await self._resume()- case "start":- await self._start(interaction.data["instructions"])- case "rewrite_steps":- await self._rewrite_steps(interaction.data["steps"])- case _:- raise Exception(f"Unknown interaction action: {interaction.type}")-- async def stop(self):- print("Cancel agent run")- self.canceled = True- if self.llm_generation:- self.llm_generation.cancel()- if self._start_loop:- self._start_loop.cancel()diff --git a/api-service/agent/from_template.py b/api-service/agent/from_template.pyindex 363ebc871..e88824dc3 100644--- a/api-service/agent/from_template.py+++ b/api-service/agent/from_template.py@@ -1,4 +1,3 @@-from agent.basic_agent import BasicAgentfrom agent.smol_agent import SmolAgent@@ -7,4 +6,4 @@ def get_agent_factory_from_template(template_id: str):case "SmolDeveloper":return SmolAgent.createcase _:- return BasicAgent.create+ raise ValueError(f"Unknown template {template_id}")diff --git a/api-service/agent/smol_agent.py b/api-service/agent/smol_agent.pyindex 46f068170..1f9edf0a0 100644--- a/api-service/agent/smol_agent.py+++ b/api-service/agent/smol_agent.py@@ -3,7 +3,7 @@ import uuidimport osimport ast-from typing import Any, Coroutine, List+from typing import Any, Callable, Coroutine, Listfrom langchain.callbacks.base import AsyncCallbackManagerfrom langchain.schema import (AIMessage,@@ -45,6 +45,7 @@ class SmolAgent(AgentBase):self,config: Any,get_envs: GetEnvs,+ set_run_id: Callable[[str], None],on_logs: OnLogs,on_interaction_request: OnInteractionRequest,model: BaseLanguageModel,@@ -55,6 +56,7 @@ class SmolAgent(AgentBase):self.config = configself.on_interaction_request = on_interaction_requestself.on_logs = on_logs+ self.set_run_id = set_run_idself.model = model@classmethod@@ -62,6 +64,7 @@ class SmolAgent(AgentBase):cls,config: Any,get_envs: GetEnvs,+ set_run_id: Callable[[str], None],on_logs: OnLogs,on_interaction_request: OnInteractionRequest,):@@ -76,6 +79,7 @@ class SmolAgent(AgentBase):return cls(new_config,get_envs,+ set_run_id,on_logs,on_interaction_request,model,@@ -84,7 +88,6 @@ class SmolAgent(AgentBase):async def generate_file(self,filename: str,- run_id: str,filepaths_string=None,shared_dependencies=None,prompt=None,@@ -132,10 +135,9 @@ Begin generating the code now.**metadata,"result": filecode,"model": "gpt-4",- "run_id": run_id,},"type": "model",- }+ },)return filename, filecode, metadata@@ -186,7 +188,7 @@ Begin generating the code now."cost": cost,}- async def _dev(self, run_id: str, instructions: Any):+ async def _dev(self, instructions: Any):with tracer.start_as_current_span("agent-run") as span:user_prompt: str = instructions["Prompt"]access_token: str = instructions["AccessToken"]@@ -199,7 +201,7 @@ Begin generating the code now.span.set_attributes({- "run_id": run_id,+ "run_id": self.run_id,"prompt": user_prompt,"prompt.length": len(user_prompt),"repository": f"{owner}/{repo}",@@ -215,10 +217,9 @@ Begin generating the code now."message": f"hi its me, 🐣the smol developer🐣!","type": "info","properties": {- "run_id": run_id,"prompt": user_prompt,},- }+ },)playground = Nonetry:@@ -230,10 +231,9 @@ Begin generating the code now."properties": {"playground": "created","id": playground.id,- "run_id": run_id,},"type": "playground",- }+ },)span.add_event("playground-created",@@ -260,10 +260,9 @@ Begin generating the code now."properties": {"tool": "git","repository": f"{owner}/{repo}",- "run_id": run_id,},"type": "tool",- }+ },)span.add_event("repository-cloned",@@ -302,10 +301,9 @@ Begin generating the code now.**metadata,"result": filepaths_string,"model": "gpt-4",- "run_id": run_id,},"type": "model",- }+ },)span.add_event(@@ -340,10 +338,9 @@ Begin generating the code now."message": f"Cleaned root directory","properties": {"tool": "filesystem",- "run_id": run_id,},"type": "tool",- }+ },)span.add_event(@@ -381,10 +378,9 @@ Begin generating the code now.**metadata,"result": shared_dependencies,"model": "gpt-4",- "run_id": run_id,},"type": "model",- }+ },)span.add_event(@@ -409,10 +405,9 @@ Begin generating the code now."filename": "shared_dependencies.md","content": shared_dependencies,"tool": "filesystem",- "run_id": run_id,},"type": "tool",- }+ },)span.add_event(@@ -435,7 +430,6 @@ Begin generating the code now.with_semaphore(self.generate_file(name,- run_id=run_id,filepaths_string=filepaths_string,shared_dependencies=shared_dependencies,prompt=user_prompt,@@ -464,10 +458,9 @@ Begin generating the code now."filename": filepath,"content": content,"tool": "filesystem",- "run_id": run_id,},"type": "tool",- }+ },)span.add_event("file-saved",@@ -493,10 +486,9 @@ Begin generating the code now."branch": branch,"commit_message": commit_message,"tool": "git",- "run_id": run_id,},"type": "tool",- }+ },)span.add_event("pushed-repository",@@ -521,11 +513,10 @@ Begin generating the code now.{"message": f"Agent run failed","properties": {- "run_id": run_id,"error": str(e),},"type": "error",- }+ },)span.set_status(Status(StatusCode.ERROR))span.record_exception(e)@@ -538,10 +529,9 @@ Begin generating the code now."message": f"Closed playground","properties": {"playground": "closed",- "run_id": run_id,},"type": "playground",- }+ },)span.add_event("closed-playground",@@ -550,11 +540,9 @@ Begin generating the code now.await self.on_logs({"message": f"Agent run finished",- "properties": {- "run_id": run_id,- },+ "properties": {},"type": "info",- }+ },)async def _dev_in_background(self, instructions: Any):@@ -565,11 +553,12 @@ Begin generating the code now.await self.stop()self.run_id = str(uuid.uuid4())+ self.set_run_id(self.run_id)async def start_with_timeout():try:self._dev_loop = asyncio.create_task(- self._dev(run_id=self.run_id, instructions=instructions),+ self._dev(instructions=instructions),)await asyncio.wait_for(self._dev_loop,diff --git a/api-service/app.py b/api-service/app.pyindex d11cac08d..00c8d6901 100644--- a/api-service/app.py+++ b/api-service/app.py@@ -63,7 +63,6 @@ async def create_agent_deployment(db_deployment["id"],project_id,body.config,- db_deployment.get("logs") or [],)current_span = trace.get_current_span()current_span.set_attributes(@@ -88,7 +87,6 @@ async def create_agent_deployment(id,project_id,body.config,- [],)current_span = trace.get_current_span()current_span.set_attributes(@@ -156,7 +154,6 @@ async def interact_with_agent_deployment(db_deployment["id"],db_deployment["project_id"],db_deployment["config"],- db_deployment.get("logs") or [],)else:raise HTTPException(status_code=404, detail="Deployment not found")diff --git a/api-service/database/database.py b/api-service/database/database.pyindex 1d2332646..d23f49d93 100644--- a/api-service/database/database.py+++ b/api-service/database/database.py@@ -1,3 +1,4 @@+import jsonfrom typing import Any, Listfrom agent.output.output_stream_parser import Step@@ -5,6 +6,7 @@ from database.client import Clientfrom session.env import EnvVarTABLE_DEPLOYMENTS = "deployments"+TABLE_LOGS = "log_files"TABLE_PROJECTS = "projects"@@ -20,7 +22,6 @@ class Database:.limit(1).execute())-return None if len(response.data) == 0 else response.data[0]async def get_deployment_by_id(self, id: str):@@ -45,14 +46,23 @@ class Database:async def update_deployment_logs(self,- id: str,+ deployment_id: str,+ run_id: str | None,+ project_id: str,logs: List[Step],):- await self.client.table(TABLE_DEPLOYMENTS).update(+ if run_id is None:+ return++ await self.client.table(TABLE_LOGS).upsert({- "logs": logs,- }- ).eq("id", id).execute()+ "id": run_id,+ "project_id": project_id,+ "deployment_id": deployment_id,+ "content": json.dumps(logs),+ },+ on_conflict="id",+ ).execute()async def update_deployment(self, id: str, enabled: bool) -> None:await self.client.table(TABLE_DEPLOYMENTS).update(diff --git a/api-service/deployment/in_memory.py b/api-service/deployment/in_memory.pyindex 15d90b3d1..21cd60158 100644--- a/api-service/deployment/in_memory.py+++ b/api-service/deployment/in_memory.py@@ -1,4 +1,4 @@-from typing import Any, Dict, List+from typing import Any, Dictfrom opentelemetry.metrics import Observationfrom database.base import db@@ -46,15 +46,13 @@ class InMemoryDeploymentManager(AgentDeploymentManager):id: str,project_id: str,config: Any,- logs: List[Any],):- print("Creating deployment", config, logs)+ print("Creating deployment", config)deployment = await AgentDeployment.from_factory(id,get_agent_factory_from_template(config["templateID"]),project_id,config,- logs,)try:await db.create_deployment(deployment.id, project_id, config)@@ -70,9 +68,8 @@ class InMemoryDeploymentManager(AgentDeploymentManager):id: str,project_id: str,config: Any,- logs: List[Any],):- print(">>>> Updating deployment", id, config, logs)+ print(">>>> Updating deployment", id, config)try:await self.remove_deployment(id)except:@@ -82,7 +79,6 @@ class InMemoryDeploymentManager(AgentDeploymentManager):id,project_id,config,- logs,)async def remove_deployment(self, id: str):diff --git a/api-service/deployment/manager.py b/api-service/deployment/manager.pyindex 40e093db1..1294cb8d0 100644--- a/api-service/deployment/manager.py+++ b/api-service/deployment/manager.py@@ -14,13 +14,14 @@ from agent.base import (AgentBase,AgentInteractionRequest,OnLogs,+ SetRun,OnInteractionRequest,GetEnvs,)-AgentFactory = Callable[- [Any, GetEnvs, OnLogs, OnInteractionRequest], Coroutine[None, None, AgentBase]+ [Any, GetEnvs, SetRun, OnLogs, OnInteractionRequest],+ Coroutine[None, None, AgentBase],]app_url = os.environ.get("APP_URL", "http://localhost:3000")@@ -63,14 +64,25 @@ async def agent_run_cancelled(deployment_id: str):class AgentEvents:- def __init__(self, id: str, logs: List[Any]) -> None:- self.id = id- self.logs: List[Any] = logs+ def __init__(self, deployment_id: str, project_id: str) -> None:+ self.deployment_id = deployment_id+ self.logs: List[Any] = []self.interaction_requests: List[AgentInteractionRequest] = []+ self.run_id: str | None+self.log_que = WorkQueue[List[Any]](- lambda logs: db.update_deployment_logs(self.id, logs)+ lambda logs: db.update_deployment_logs(+ deployment_id=self.deployment_id,+ run_id=self.run_id,+ project_id=project_id,+ logs=logs,+ ))+ def set_run_id(self, run_id: str):+ self.logs = []+ self.run_id = run_id+async def add_log(self, log: Any):date = datetime.now()log["timestamp"] = str(date)@@ -85,9 +97,11 @@ class AgentEvents:self.interaction_requests.append(interaction_request)match interaction_request.type:case "done":- await agent_run_done(self.id, interaction_request.data["prompt"])+ await agent_run_done(+ self.deployment_id, interaction_request.data["prompt"]+ )case "cancelled":- await agent_run_cancelled(self.id)+ await agent_run_cancelled(self.deployment_id)# case "failed":# # TODO: Save agent/interactions/states/run# await agent_run_failed(self.id)@@ -113,12 +127,12 @@ class AgentDeployment:factory: AgentFactory,project_id: str,config: Any,- logs: List[Any],):- event_handler = AgentEvents(deployment_id, logs)+ event_handler = AgentEvents(deployment_id=deployment_id, project_id=project_id)agent = await factory(config,lambda: db.get_env_vars(project_id),+ event_handler.set_run_id,event_handler.add_log,event_handler.add_interaction_request,)