MCPcopy Create free account
hub / github.com/AQ-MedAI/MedMemoryBench / _command

Method _command

methods/letta/server/server.py:419–535  ·  view source on GitHub ↗

Process a CLI command

(self, user_id: str, agent_id: str, command: str)

Source from the content-addressed store, hash-verified

417 return usage_stats
418
419 def _command(self, user_id: str, agent_id: str, command: str) -> LettaUsageStatistics:
420 """Process a CLI command"""
421 # TODO: Thread actor directly through this function, since the top level caller most likely already retrieved the user
422 actor = self.user_manager.get_user_or_default(user_id=user_id)
423
424 logger.debug(f"Got command: {command}")
425
426 # Get the agent object (loaded in memory)
427 letta_agent = self.load_agent(agent_id=agent_id, actor=actor)
428 usage = None
429
430 if command.lower() == "exit":
431 # exit not supported on server.py
432 raise ValueError(command)
433
434 elif command.lower() == "save" or command.lower() == "savechat":
435 save_agent(letta_agent)
436
437 elif command.lower() == "attach":
438 # Different from CLI, we extract the data source name from the command
439 command = command.strip().split()
440 try:
441 data_source = int(command[1])
442 except:
443 raise ValueError(command)
444
445 # attach data to agent from source
446 letta_agent.attach_source(
447 user=self.user_manager.get_user_by_id(user_id=user_id),
448 source_id=data_source,
449 source_manager=self.source_manager,
450 agent_manager=self.agent_manager,
451 )
452
453 elif command.lower() == "dump" or command.lower().startswith("dump "):
454 # Check if there's an additional argument that's an integer
455 command = command.strip().split()
456 amount = int(command[1]) if len(command) > 1 and command[1].isdigit() else 0
457 if amount == 0:
458 letta_agent.interface.print_messages(letta_agent.messages, dump=True)
459 else:
460 letta_agent.interface.print_messages(letta_agent.messages[-min(amount, len(letta_agent.messages)) :], dump=True)
461
462 elif command.lower() == "dumpraw":
463 letta_agent.interface.print_messages_raw(letta_agent.messages)
464
465 elif command.lower() == "memory":
466 ret_str = f"\nDumping memory contents:\n" + f"\n{str(letta_agent.agent_state.memory)}" + f"\n{str(letta_agent.passage_manager)}"
467 return ret_str
468
469 elif command.lower() == "pop" or command.lower().startswith("pop "):
470 # Check if there's an additional argument that's an integer
471 command = command.strip().split()
472 pop_amount = int(command[1]) if len(command) > 1 and command[1].isdigit() else 3
473 n_messages = len(letta_agent.messages)
474 MIN_MESSAGES = 2
475 if n_messages <= MIN_MESSAGES:
476 logger.debug(f"Agent only has {n_messages} messages in stack, none left to pop")

Callers 1

run_commandMethod · 0.95

Calls 15

load_agentMethod · 0.95
_stepMethod · 0.95
save_agentFunction · 0.90
json_loadsFunction · 0.90
json_dumpsFunction · 0.90
get_user_or_defaultMethod · 0.80
debugMethod · 0.65
attach_sourceMethod · 0.45
get_user_by_idMethod · 0.45
print_messagesMethod · 0.45
print_messages_rawMethod · 0.45

Tested by

no test coverage detected