Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Fix merge errors; Cleanup; Update excluded files
Tomas Valentacb1605414963
diff --git a/api-service/agent/smol_agent.py b/api-service/agent/smol_agent.pyindex 8e4f746d7..64b3d84f6 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, Listfrom langchain.callbacks.base import AsyncCallbackManagerfrom langchain.schema import (AIMessage,@@ -26,7 +26,6 @@ from agent.base import (from models.base import ModelConfig, get_modelfrom agent.base import AgentBase, AgentInteractionRequest, GetEnvsfrom session.playground import Playground-from playground_client.exceptions import NotFoundExceptiondefault_openai_api_key = os.environ.get("OPENAI_API_KEY", None)@@ -37,6 +36,38 @@ pricing = {}}+extensions_to_skip = [+ ".png",+ ".jpg",+ ".jpeg",+ ".gif",+ ".bmp",+ ".svg",+ ".ico",+ ".tif",+ ".raw",+ ".webp",+ ".tiff",+ ".mp3",+ "package-lock.json",+ "yarn.lock",+ "pnpm-lock.yaml",+ "Gemfile.lock",+ "Pipfile.lock",+ "composer.lock",+ "Cargo.lock",+ "pubspec.lock",+ "packages.lock.json",+ "go.sum",+ "Package.resolved",+]+++# Helper function for limiting number of concurrent coroutines+async def with_semaphore(semaphore, coro):+ async with semaphore:+ return await coro+class SmolAgent(AgentBase):max_run_time = 60 * 60 # in seconds@@ -197,6 +228,9 @@ Begin generating the code now.git_app_name: str = instructions["GitHubAppName"]git_app_email: str = instructions["GitHubAppEmail"]commit_message: str = instructions["CommitMessage"]+ repo_address = (+ f"https://{git_app_name}:{access_token}@github.com/{owner}/{repo}.git"+ )span.set_attributes({@@ -207,10 +241,6 @@ Begin generating the code now.})- repo_address = (- f"https://{git_app_name}:{access_token}@github.com/{owner}/{repo}.git"- )-await self.on_logs({"message": f"hi its me, 🐣the smol developer🐣!",@@ -224,68 +254,84 @@ Begin generating the code now.playground = Nonetry:playground = Playground(env_id="PPSrlH5TIvFx", get_envs=self.get_envs)- await playground.open()- await self.on_logs(- {- "message": f"Created playground",- "properties": {- "playground": "created",- "id": playground.id,- "run_id": run_id,+ rootdir = "/repo"++ async def initialize_playground():+ await playground.open()+ await self.on_logs(+ {+ "message": f"Created playground",+ "properties": {+ "playground": "created",+ "id": playground.id,+ "run_id": run_id,+ },+ "type": "playground",+ }+ )+ span.add_event(+ "playground-created",+ {+ "playground.id": playground.id,},- "type": "playground",- }- )- span.add_event(- "playground-created",- {- "playground.id": playground.id,- },- )+ )+ await playground.sync_clock()+ await playground.change_rootdir(rootdir)+ await playground.make_dir(rootdir)+ await playground.clone_repo(+ repo_address=repo_address,+ rootdir=rootdir,+ branch=branch,+ )- await asyncio.ensure_future(playground.sync_clock())+ await self.on_logs(+ {+ "message": f"Cloned repository",+ "properties": {+ "tool": "git",+ "repository": f"{owner}/{repo}",+ "run_id": run_id,+ },+ "type": "tool",+ }+ )+ span.add_event(+ "repository-cloned",+ {+ "repository": f"{owner}/{repo}",+ },+ )- rootdir = "/repo"- await playground.change_rootdir(rootdir)- await playground.make_dir(rootdir)+ extensions = " -o ".join(+ [f'-name "*{extension}"' for extension in extensions_to_skip]+ )- await playground.clone_repo(- repo_address=repo_address,- rootdir=rootdir,- branch=branch,- )+ delete_command = f"find . -path ./.git -prune -o ! \\( {extensions} \\) -type f -exec rm -f {{}} +"- await self.on_logs(- {- "message": f"Cloned repository",- "properties": {- "tool": "git",- "repository": f"{owner}/{repo}",- "run_id": run_id,+ print("Delete command: ", delete_command)++ res = await playground.run_command(delete_command, rootdir)+ print("Delete command result: ", res.stdout, res.stderr)++ await self.on_logs(+ {+ "message": f"Cleaned root directory",+ "properties": {+ "tool": "filesystem",+ "run_id": run_id,+ },+ "type": "tool",+ }+ )++ span.add_event(+ "files-deleted",+ {+ "path": rootdir,},- "type": "tool",- }- )- span.add_event(- "repository-cloned",- {- "repository": f"{owner}/{repo}",- },- )+ )- extensions_to_skip = [- ".png",- ".jpg",- ".jpeg",- ".gif",- ".bmp",- ".svg",- ".ico",- ".tif",- ".tiff",- ".mp3",- ".lock",- ]+ initializing_playground = asyncio.ensure_future(initialize_playground())filepaths_string, metadata = await self.generate_response("""You are an AI developer who is trying to write a program that will generate code for the user based on their intent.@@ -323,39 +369,6 @@ Begin generating the code now.# parse the result into a python listlist_actual = ast.literal_eval(filepaths_string)- # if shared_dependencies.md is there, read it in, else set it to None- shared_dependencies: str | None = None- try:- shared_dependencies = await playground.read_file(- os.path.join(rootdir, "shared_dependencies.md")- )- except NotFoundException:- pass-- files = await playground.get_filenames(rootdir, [".git"])- for file in files:- _, extension = os.path.splitext(file.name)- if extension not in extensions_to_skip:- await playground.delete_file(file.name)-- await self.on_logs(- {- "message": f"Cleaned root directory",- "properties": {- "tool": "filesystem",- "run_id": run_id,- },- "type": "tool",- }- )-- span.add_event(- "files-deleted",- {- "path": rootdir,- },- )-(shared_dependencies,metadata,@@ -399,66 +412,34 @@ Begin generating the code now.},)- # write shared dependencies as a md file inside the generated directory- await playground.write_file(- os.path.join(rootdir, "shared_dependencies.md"),- shared_dependencies,- )-- await self.on_logs(- {- "message": f"Saved shared dependencies",- "properties": {- "filename": "shared_dependencies.md",- "content": shared_dependencies,- "tool": "filesystem",- "run_id": run_id,- },- "type": "tool",- }- )+ await initializing_playground- span.add_event(- "file-saved",- {- "filename": "shared_dependencies.md",- "model": "gpt-4",- },- )+ print("Filepaths:", ", ".join(list_actual))# Maximum number of allowed concurrent callssemaphore = asyncio.Semaphore(4)-- async def with_semaphore(coro):- async with semaphore:- return await coro-# execute the file generation in paralell and wait for all of them to finish. Use list comprehension to generate the tasks- coros = [- with_semaphore(- self.generate_file(- name,- run_id=run_id,- filepaths_string=filepaths_string,- shared_dependencies=shared_dependencies,- prompt=user_prompt,+ generated_files = await asyncio.gather(+ *[+ with_semaphore(+ semaphore,+ self.generate_file(+ name,+ run_id=run_id,+ filepaths_string=filepaths_string,+ shared_dependencies=shared_dependencies,+ prompt=user_prompt,+ ),)- )- for name in list_actual- # Filter out files that end with esxtensions we don't want to generate- if not any(- name.endswith(extension) for extension in extensions_to_skip- )- ]-- generated_files = await asyncio.gather(*coros)- print("All files generated")+ for name in list_actual+ # Filter out files that end with extensions we don't want to generate+ if not any(+ name.endswith(extension) for extension in extensions_to_skip+ )+ ]+ )- for (- name,- content,- _, # metadata- ) in generated_files:+ async def save_file(name, content):filepath = os.path.join(rootdir, name)await playground.write_file(filepath, content)await self.on_logs(@@ -477,10 +458,22 @@ Begin generating the code now."file-saved",{"filename": filepath,- "model": "gpt-4",},)+ fs_semaphore = asyncio.Semaphore(5)+ await asyncio.gather(+ *[+ with_semaphore(fs_semaphore, save_file(name, content))+ for name, content, _ in generated_files+ ],+ with_semaphore(+ fs_semaphore,+ save_file("shared_dependencies.md", shared_dependencies),+ ),+ )+ print("All files generated")+await playground.push_repo(rootdir=rootdir,repo_address=repo_address,@@ -488,7 +481,6 @@ Begin generating the code now.git_email=git_app_email,git_name=git_app_name,)-await self.on_logs({"message": f"Pushed repository",@@ -551,15 +543,15 @@ Begin generating the code now."closed-playground",{},)- await self.on_logs(- {- "message": f"Agent run finished",- "properties": {- "run_id": run_id,- },- "type": "info",- }- )+ await self.on_logs(+ {+ "message": f"Agent run finished",+ "properties": {+ "run_id": run_id,+ },+ "type": "info",+ }+ )async def _dev_in_background(self, instructions: Any):print("Start agent run", bool(self._dev_loop))diff --git a/api-service/session/session.py b/api-service/session/session.pyindex ecedd4128..d2e7a3f6b 100644--- a/api-service/session/session.py+++ b/api-service/session/session.py@@ -24,7 +24,7 @@ GetEnvs = Callable[[], Coroutine[Any, Any, List[EnvVar]]]async def get_result(result: ApplyResult[Any]):while not result.ready():- await asyncio.sleep(1) # give other tasks chance to run+ await asyncio.sleep(0.5) # give other tasks chance to runreturn result.get()