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" ),
@@ -101,23 +131,33 @@ def build(
101131 """
102132 typer .echo (f"Building agent image from manifest: { manifest } " )
103133
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+
104144 platform_list = platforms .split ("," ) if platforms else []
105145
106146 try :
107147 image_url = build_agent (
108148 manifest_path = manifest ,
109- registry_url = registry ,
110- repository_name = repository_name ,
149+ registry_url = registry , # Now guaranteed to be non-None
150+ repository_name = repository_name or "default-repo" , # Provide default
111151 platforms = platform_list ,
112152 push = push ,
113- secret = secret ,
114- tag = tag ,
115- 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
116156 )
117157 if image_url :
118158 typer .echo (f"Successfully built image: { image_url } " )
119159 else :
120- typer .echo ("No registry provided, image was not built " )
160+ typer .echo ("Image build completed but no URL returned " )
121161 except Exception as e :
122162 typer .echo (f"Error building agent image: { str (e )} " , err = True )
123163 logger .exception ("Error building agent image" )
@@ -127,11 +167,35 @@ def build(
127167@agents .command ()
128168def run (
129169 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+ ),
130174):
131175 """
132176 Run an agent locally from the given manifest.
133177 """
134178 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+
135199 try :
136200 run_agent (manifest_path = manifest )
137201 except Exception as e :
0 commit comments