Skip to content

Commit f47ef07

Browse files
JanPokornyedengilbert
authored andcommitted
feat(agentstack-cli): use better heuristics for determining for adding agents (i-am-bee#1603)
Signed-off-by: Eden Gilbert <[email protected]>
1 parent 3803946 commit f47ef07

File tree

1 file changed

+31
-28
lines changed
  • apps/agentstack-cli/src/agentstack_cli/commands

1 file changed

+31
-28
lines changed

apps/agentstack-cli/src/agentstack_cli/commands/agent.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
from collections.abc import Callable
8585
from pathlib import Path
8686
from typing import Any
87+
from urllib.parse import urlparse
8788

8889
import jsonschema
8990
import rich.json
@@ -156,36 +157,38 @@ async def add_agent(
156157
) -> None:
157158
"""Install discovered agent or add public docker image or github repository [aliases: install]"""
158159
agent_card = None
159-
# Try extracting manifest locally for local images
160160
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}")
165184

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")
189192
await list_agents()
190193

191194

0 commit comments

Comments
 (0)