1111 build_agent ,
1212 run_agent ,
1313)
14+ from agentex .lib .cli .handlers .cleanup_handlers import cleanup_agent_workflows
1415from agentex .lib .cli .handlers .deploy_handlers import (
1516 DeploymentError ,
1617 HelmError ,
@@ -71,6 +72,35 @@ def delete(
7172 logger .info (f"Agent deleted: { agent_name } " )
7273
7374
75+ @agents .command ()
76+ def cleanup_workflows (
77+ agent_name : str = typer .Argument (..., help = "Name of the agent to cleanup workflows for" ),
78+ force : bool = typer .Option (False , help = "Force cleanup using direct Temporal termination (bypasses development check)" ),
79+ ):
80+ """
81+ Clean up all running workflows for an agent.
82+
83+ By default, uses graceful cancellation via agent RPC.
84+ With --force, directly terminates workflows via Temporal client.
85+ This is a convenience command that does the same thing as 'agentex tasks cleanup'.
86+ """
87+ try :
88+ console .print (f"[blue]Cleaning up workflows for agent '{ agent_name } '...[/blue]" )
89+
90+ cleanup_agent_workflows (
91+ agent_name = agent_name ,
92+ force = force ,
93+ development_only = True
94+ )
95+
96+ console .print (f"[green]✓ Workflow cleanup completed for agent '{ agent_name } '[/green]" )
97+
98+ except Exception as e :
99+ console .print (f"[red]Cleanup failed: { str (e )} [/red]" )
100+ logger .exception ("Agent workflow cleanup failed" )
101+ raise typer .Exit (1 ) from e
102+
103+
74104@agents .command ()
75105def build (
76106 manifest : str = typer .Option (..., help = "Path to the manifest you want to use" ),
@@ -80,6 +110,9 @@ def build(
80110 repository_name : str | None = typer .Option (
81111 None , help = "Repository name to use for the built image"
82112 ),
113+ platforms : str | None = typer .Option (
114+ None , help = "Platform to build the image for. Please enter a comma separated list of platforms."
115+ ),
83116 push : bool = typer .Option (False , help = "Whether to push the image to the registry" ),
84117 secret : str | None = typer .Option (
85118 None ,
@@ -98,20 +131,33 @@ def build(
98131 """
99132 typer .echo (f"Building agent image from manifest: { manifest } " )
100133
134+ # Validate required parameters for building
135+ if push and not registry :
136+ typer .echo ("Error: --registry is required when --push is enabled" , err = True )
137+ raise typer .Exit (1 )
138+
139+ # Only proceed with build if we have a registry (for now, to match existing behavior)
140+ if not registry :
141+ typer .echo ("No registry provided, skipping image build" )
142+ return
143+
144+ platform_list = platforms .split ("," ) if platforms else []
145+
101146 try :
102147 image_url = build_agent (
103148 manifest_path = manifest ,
104- registry_url = registry ,
105- repository_name = repository_name ,
149+ registry_url = registry , # Now guaranteed to be non-None
150+ repository_name = repository_name or "default-repo" , # Provide default
151+ platforms = platform_list ,
106152 push = push ,
107- secret = secret ,
108- tag = tag ,
109- build_args = build_arg ,
153+ secret = secret or "" , # Provide default empty string
154+ tag = tag or "latest" , # Provide default
155+ build_args = build_arg or [], # Provide default empty list
110156 )
111157 if image_url :
112158 typer .echo (f"Successfully built image: { image_url } " )
113159 else :
114- typer .echo ("No registry provided, image was not built " )
160+ typer .echo ("Image build completed but no URL returned " )
115161 except Exception as e :
116162 typer .echo (f"Error building agent image: { str (e )} " , err = True )
117163 logger .exception ("Error building agent image" )
@@ -121,11 +167,35 @@ def build(
121167@agents .command ()
122168def run (
123169 manifest : str = typer .Option (..., help = "Path to the manifest you want to use" ),
170+ cleanup_on_start : bool = typer .Option (
171+ False ,
172+ help = "Clean up existing workflows for this agent before starting"
173+ ),
124174):
125175 """
126176 Run an agent locally from the given manifest.
127177 """
128178 typer .echo (f"Running agent from manifest: { manifest } " )
179+
180+ # Optionally cleanup existing workflows before starting
181+ if cleanup_on_start :
182+ try :
183+ # Parse manifest to get agent name
184+ manifest_obj = AgentManifest .from_yaml (file_path = manifest )
185+ agent_name = manifest_obj .agent .name
186+
187+ console .print (f"[yellow]Cleaning up existing workflows for agent '{ agent_name } '...[/yellow]" )
188+ cleanup_agent_workflows (
189+ agent_name = agent_name ,
190+ force = False ,
191+ development_only = True
192+ )
193+ console .print ("[green]✓ Pre-run cleanup completed[/green]" )
194+
195+ except Exception as e :
196+ console .print (f"[yellow]⚠ Pre-run cleanup failed: { str (e )} [/yellow]" )
197+ logger .warning (f"Pre-run cleanup failed: { e } " )
198+
129199 try :
130200 run_agent (manifest_path = manifest )
131201 except Exception as e :
0 commit comments