11import asyncio
22import json
3+ from collections .abc import MutableMapping
34from typing import Annotated , Literal
45
56import typer
1011
1112from agent_engine_cli import __version__
1213from agent_engine_cli .chat import run_chat
13- from agent_engine_cli .client import AgentEngineClient
1414from agent_engine_cli .config import ConfigurationError , resolve_project
15+ from agent_engine_cli .dependencies import get_client
1516
1617console = Console ()
1718
@@ -41,8 +42,8 @@ def list_agents(
4142 raise typer .Exit (code = 1 )
4243
4344 try :
44- client = AgentEngineClient (project = project , location = location )
45- agents = client .list_agents ()
45+ client = get_client (project = project , location = location )
46+ agents = list ( client .list_agents () )
4647
4748 if not agents :
4849 console .print ("No agents found." )
@@ -107,7 +108,7 @@ def get_agent(
107108 raise typer .Exit (code = 1 )
108109
109110 try :
110- client = AgentEngineClient (project = project , location = location )
111+ client = get_client (project = project , location = location )
111112 agent = client .get_agent (agent_id )
112113
113114 # v1beta1 api_resource uses 'name' instead of 'resource_name'
@@ -247,7 +248,7 @@ def create_agent(
247248 raise typer .Exit (code = 1 )
248249
249250 try :
250- client = AgentEngineClient (project = project , location = location )
251+ client = get_client (project = project , location = location )
251252 console .print (f"Creating agent '{ escape (display_name )} '..." )
252253
253254 agent = client .create_agent (
@@ -288,7 +289,7 @@ def delete_agent(
288289 raise typer .Exit ()
289290
290291 try :
291- client = AgentEngineClient (project = project , location = location )
292+ client = get_client (project = project , location = location )
292293 client .delete_agent (agent_id , force = force )
293294 console .print (f"[red]Agent '{ escape (agent_id )} ' deleted.[/red]" )
294295 except Exception as e :
@@ -315,7 +316,7 @@ def list_sessions(
315316 raise typer .Exit (code = 1 )
316317
317318 try :
318- client = AgentEngineClient (project = project , location = location )
319+ client = get_client (project = project , location = location )
319320 sessions = list (client .list_sessions (agent_id ))
320321
321322 if not sessions :
@@ -383,8 +384,12 @@ def list_sandboxes(
383384 raise typer .Exit (code = 1 )
384385
385386 try :
386- client = AgentEngineClient (project = project , location = location )
387- sandboxes = client .list_sandboxes (agent_id )
387+ client = get_client (project = project , location = location )
388+ sandboxes = list (client .list_sandboxes (agent_id ))
389+
390+ if not sandboxes :
391+ console .print ("No sandboxes found." )
392+ return
388393
389394 table = Table (title = "Sandboxes" )
390395 table .add_column ("Sandbox ID" , style = "cyan" )
@@ -393,9 +398,7 @@ def list_sandboxes(
393398 table .add_column ("Created" )
394399 table .add_column ("Expires" )
395400
396- has_items = False
397401 for sandbox in sandboxes :
398- has_items = True
399402 # Extract sandbox ID from full resource name
400403 sandbox_name = getattr (sandbox , "name" , "" ) or ""
401404 sandbox_id = sandbox_name .split ("/" )[- 1 ] if sandbox_name else ""
@@ -430,10 +433,6 @@ def list_sandboxes(
430433 expire_time ,
431434 )
432435
433- if not has_items :
434- console .print ("No sandboxes found." )
435- return
436-
437436 console .print (table )
438437 except Exception as e :
439438 console .print (f"[red]Error listing sandboxes: { escape (str (e ))} [/red]" )
@@ -459,8 +458,8 @@ def list_memories(
459458 raise typer .Exit (code = 1 )
460459
461460 try :
462- client = AgentEngineClient (project = project , location = location )
463- memories = client .list_memories (agent_id )
461+ client = get_client (project = project , location = location )
462+ memories = list ( client .list_memories (agent_id ) )
464463
465464 table = Table (title = "Memories" )
466465 table .add_column ("Memory ID" , style = "cyan" )
@@ -482,7 +481,7 @@ def list_memories(
482481
483482 # Format scope dict as key=value pairs
484483 scope_raw = getattr (memory , "scope" , None )
485- if scope_raw and isinstance (scope_raw , dict ):
484+ if scope_raw and isinstance (scope_raw , ( dict , MutableMapping ) ):
486485 scope = ", " .join (f"{ k } ={ v } " for k , v in scope_raw .items ())
487486 else :
488487 scope = ""
0 commit comments