a digital person for bluesky
at main 2.3 kB view raw
1#!/usr/bin/env python3 2""" 3Helper script to create and attach user-specific memory blocks to the profile researcher agent. 4Usage: python attach_user_block.py @cameron.pfiffer.org 5""" 6 7import sys 8import os 9import logging 10from letta_client import Letta 11from create_profile_researcher import create_user_block_for_handle 12 13# Configure logging 14logging.basicConfig( 15 level=logging.INFO, 16 format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" 17) 18logger = logging.getLogger("attach_user_block") 19 20def attach_user_block_to_agent(agent_name: str, handle: str): 21 """Create and attach a user block to an agent.""" 22 23 # Create client 24 client = Letta(token=os.environ["LETTA_API_KEY"]) 25 26 # Find the agent 27 agents = client.agents.list(name=agent_name) 28 if not agents: 29 print(f"❌ Agent '{agent_name}' not found") 30 return False 31 32 agent = agents[0] 33 print(f"📝 Found agent: {agent.name} (ID: {agent.id})") 34 35 # Create the user block 36 print(f"🔍 Creating user block for {handle}...") 37 user_block = create_user_block_for_handle(client, handle) 38 39 # Check if already attached 40 agent_blocks = client.agents.blocks.list(agent_id=agent.id) 41 for block in agent_blocks: 42 if block.id == user_block.id: 43 print(f"✅ User block for {handle} is already attached to {agent.name}") 44 return True 45 46 # Attach the block to the agent 47 print(f"🔗 Attaching user block to agent...") 48 client.agents.blocks.attach(agent_id=agent.id, block_id=user_block.id) 49 50 print(f"✅ Successfully attached user block for {handle} to {agent.name}") 51 print(f" Block ID: {user_block.id}") 52 print(f" Block Label: {user_block.label}") 53 return True 54 55def main(): 56 """Main function.""" 57 if len(sys.argv) != 2: 58 print("Usage: python attach_user_block.py <handle>") 59 print("Example: python attach_user_block.py @cameron.pfiffer.org") 60 sys.exit(1) 61 62 handle = sys.argv[1] 63 agent_name = "profile-researcher" 64 65 try: 66 success = attach_user_block_to_agent(agent_name, handle) 67 if not success: 68 sys.exit(1) 69 except Exception as e: 70 logger.error(f"Error: {e}") 71 print(f"❌ Error: {e}") 72 sys.exit(1) 73 74if __name__ == "__main__": 75 main()