|
84 | 84 | from collections.abc import Callable |
85 | 85 | from pathlib import Path |
86 | 86 | from typing import Any |
| 87 | +from urllib.parse import urlparse |
87 | 88 |
|
88 | 89 | import jsonschema |
89 | 90 | import rich.json |
@@ -156,36 +157,38 @@ async def add_agent( |
156 | 157 | ) -> None: |
157 | 158 | """Install discovered agent or add public docker image or github repository [aliases: install]""" |
158 | 159 | agent_card = None |
159 | | - # Try extracting manifest locally for local images |
160 | 160 | with verbosity(verbose): |
161 | | - process = await run_command(["docker", "inspect", location], check=False, message="Inspecting docker images.") |
162 | | - from subprocess import CalledProcessError |
163 | | - |
164 | | - errors = [] |
| 161 | + if ( |
| 162 | + process := await run_command( |
| 163 | + ["docker", "inspect", location], check=False, message="Inspecting docker images" |
| 164 | + ) |
| 165 | + ).returncode == 0: |
| 166 | + console.success(f"Found local image [bold]{location}[/bold]") |
| 167 | + manifest = base64.b64decode( |
| 168 | + json.loads(process.stdout)[0]["Config"]["Labels"]["beeai.dev.agent.json"] |
| 169 | + ).decode() |
| 170 | + agent_card = json.loads(manifest) |
| 171 | + elif ( |
| 172 | + Path(location).expanduser().exists() |
| 173 | + or location.startswith("git@") |
| 174 | + or location.startswith("github.com/") |
| 175 | + or location.startswith("www.github.com/") |
| 176 | + or location.endswith(".git") |
| 177 | + or ((u := urlparse(location)).scheme.startswith("http") and u.netloc.endswith("github.com")) |
| 178 | + or u.scheme in {"ssh", "git", "git+ssh"} |
| 179 | + ): |
| 180 | + console.info(f"Assuming build context, attempting to build agent from [bold]{location}[/bold]") |
| 181 | + location, agent_card = await build(location, dockerfile, tag=None, vm_name=vm_name, import_image=True) |
| 182 | + else: |
| 183 | + console.info(f"Assuming public docker image, attempting to pull {location}") |
165 | 184 |
|
166 | | - try: |
167 | | - if process.returncode: |
168 | | - # If the image was not found locally, try building image |
169 | | - location, agent_card = await build(location, dockerfile, tag=None, vm_name=vm_name, import_image=True) |
170 | | - else: |
171 | | - manifest = base64.b64decode( |
172 | | - json.loads(process.stdout)[0]["Config"]["Labels"]["beeai.dev.agent.json"] |
173 | | - ).decode() |
174 | | - agent_card = json.loads(manifest) |
175 | | - # If all build and inspect succeeded, use the local image, else use the original; maybe it exists remotely |
176 | | - except CalledProcessError as e: |
177 | | - errors.append(e) |
178 | | - console.print("Attempting to use remote image...") |
179 | | - try: |
180 | | - with status("Registering agent to platform"): |
181 | | - async with configuration.use_platform_client(): |
182 | | - await Provider.create( |
183 | | - location=location, |
184 | | - agent_card=AgentCard.model_validate(agent_card) if agent_card else None, |
185 | | - ) |
186 | | - console.print("Registering agent to platform [[green]DONE[/green]]") |
187 | | - except Exception as e: |
188 | | - raise ExceptionGroup("Error occured", [*errors, e]) from e |
| 185 | + with status("Registering agent to platform"): |
| 186 | + async with configuration.use_platform_client(): |
| 187 | + await Provider.create( |
| 188 | + location=location, |
| 189 | + agent_card=AgentCard.model_validate(agent_card) if agent_card else None, |
| 190 | + ) |
| 191 | + console.success(f"Agent [bold]{location}[/bold] added to platform") |
189 | 192 | await list_agents() |
190 | 193 |
|
191 | 194 |
|
|
0 commit comments