a digital person for bluesky
at 074e0ad1ebdc47496fbc576bd30e04f891fac2ce 243 lines 9.3 kB view raw
1#!/usr/bin/env python3 2"""Register all Void tools with a Letta agent.""" 3import os 4import sys 5import logging 6from typing import List 7from letta_client import Letta 8from rich.console import Console 9from rich.table import Table 10from config_loader import get_config, get_letta_config, get_agent_config 11 12# Import standalone functions and their schemas 13from tools.search import search_bluesky_posts, SearchArgs 14from tools.post import create_new_bluesky_post, PostArgs 15from tools.feed import get_bluesky_feed, FeedArgs 16from tools.blocks import attach_user_blocks, detach_user_blocks, user_note_append, user_note_replace, user_note_set, user_note_view, AttachUserBlocksArgs, DetachUserBlocksArgs, UserNoteAppendArgs, UserNoteReplaceArgs, UserNoteSetArgs, UserNoteViewArgs 17from tools.halt import halt_activity, HaltArgs 18from tools.thread import add_post_to_bluesky_reply_thread, ReplyThreadPostArgs 19from tools.ignore import ignore_notification, IgnoreNotificationArgs 20from tools.whitewind import create_whitewind_blog_post, WhitewindPostArgs 21from tools.ack import annotate_ack, AnnotateAckArgs 22from tools.webpage import fetch_webpage, WebpageArgs 23 24config = get_config() 25letta_config = get_letta_config() 26agent_config = get_agent_config() 27logging.basicConfig(level=logging.INFO) 28logger = logging.getLogger(__name__) 29console = Console() 30 31 32# Tool configurations: function paired with its args_schema and metadata 33TOOL_CONFIGS = [ 34 { 35 "func": search_bluesky_posts, 36 "args_schema": SearchArgs, 37 "description": "Search for posts on Bluesky matching the given criteria", 38 "tags": ["bluesky", "search", "posts"] 39 }, 40 { 41 "func": create_new_bluesky_post, 42 "args_schema": PostArgs, 43 "description": "Create a new Bluesky post or thread", 44 "tags": ["bluesky", "post", "create", "thread"] 45 }, 46 { 47 "func": get_bluesky_feed, 48 "args_schema": FeedArgs, 49 "description": "Retrieve a Bluesky feed (home timeline or custom feed)", 50 "tags": ["bluesky", "feed", "timeline"] 51 }, 52 { 53 "func": attach_user_blocks, 54 "args_schema": AttachUserBlocksArgs, 55 "description": "Attach user-specific memory blocks to the agent. Creates blocks if they don't exist.", 56 "tags": ["memory", "blocks", "user"] 57 }, 58 { 59 "func": detach_user_blocks, 60 "args_schema": DetachUserBlocksArgs, 61 "description": "Detach user-specific memory blocks from the agent. Blocks are preserved for later use.", 62 "tags": ["memory", "blocks", "user"] 63 }, 64 { 65 "func": user_note_append, 66 "args_schema": UserNoteAppendArgs, 67 "description": "Append a note to a user's memory block. Creates the block if it doesn't exist.", 68 "tags": ["memory", "blocks", "user", "append"] 69 }, 70 { 71 "func": user_note_replace, 72 "args_schema": UserNoteReplaceArgs, 73 "description": "Replace text in a user's memory block.", 74 "tags": ["memory", "blocks", "user", "replace"] 75 }, 76 { 77 "func": user_note_set, 78 "args_schema": UserNoteSetArgs, 79 "description": "Set the complete content of a user's memory block.", 80 "tags": ["memory", "blocks", "user", "set"] 81 }, 82 { 83 "func": user_note_view, 84 "args_schema": UserNoteViewArgs, 85 "description": "View the content of a user's memory block.", 86 "tags": ["memory", "blocks", "user", "view"] 87 }, 88 { 89 "func": halt_activity, 90 "args_schema": HaltArgs, 91 "description": "Signal to halt all bot activity and terminate bsky.py", 92 "tags": ["control", "halt", "terminate"] 93 }, 94 { 95 "func": add_post_to_bluesky_reply_thread, 96 "args_schema": ReplyThreadPostArgs, 97 "description": "Add a single post to the current Bluesky reply thread atomically", 98 "tags": ["bluesky", "reply", "thread", "atomic"] 99 }, 100 { 101 "func": ignore_notification, 102 "args_schema": IgnoreNotificationArgs, 103 "description": "Explicitly ignore a notification without replying (useful for ignoring bot interactions)", 104 "tags": ["notification", "ignore", "control", "bot"] 105 }, 106 { 107 "func": create_whitewind_blog_post, 108 "args_schema": WhitewindPostArgs, 109 "description": "Create a blog post on Whitewind with markdown support", 110 "tags": ["whitewind", "blog", "post", "markdown"] 111 }, 112 { 113 "func": annotate_ack, 114 "args_schema": AnnotateAckArgs, 115 "description": "Add a note to the acknowledgment record for the current post interaction", 116 "tags": ["acknowledgment", "note", "annotation", "metadata"] 117 }, 118 { 119 "func": fetch_webpage, 120 "args_schema": WebpageArgs, 121 "description": "Fetch a webpage and convert it to markdown/text format using Jina AI reader", 122 "tags": ["web", "fetch", "webpage", "markdown", "jina"] 123 }, 124] 125 126 127def register_tools(agent_name: str = None, tools: List[str] = None): 128 """Register tools with a Letta agent. 129 130 Args: 131 agent_name: Name of the agent to attach tools to. If None, uses config default. 132 tools: List of tool names to register. If None, registers all tools. 133 """ 134 # Use agent name from config if not provided 135 if agent_name is None: 136 agent_name = agent_config['name'] 137 138 try: 139 # Initialize Letta client with API key from config 140 client = Letta(token=letta_config['api_key']) 141 142 # Find the agent 143 agents = client.agents.list() 144 agent = None 145 for a in agents: 146 if a.name == agent_name: 147 agent = a 148 break 149 150 if not agent: 151 console.print(f"[red]Error: Agent '{agent_name}' not found[/red]") 152 console.print("\nAvailable agents:") 153 for a in agents: 154 console.print(f" - {a.name}") 155 return 156 157 # Filter tools if specific ones requested 158 tools_to_register = TOOL_CONFIGS 159 if tools: 160 tools_to_register = [t for t in TOOL_CONFIGS if t["func"].__name__ in tools] 161 if len(tools_to_register) != len(tools): 162 missing = set(tools) - {t["func"].__name__ for t in tools_to_register} 163 console.print(f"[yellow]Warning: Unknown tools: {missing}[/yellow]") 164 165 # Create results table 166 table = Table(title=f"Tool Registration for Agent '{agent_name}'") 167 table.add_column("Tool", style="cyan") 168 table.add_column("Status", style="green") 169 table.add_column("Description") 170 171 # Register each tool 172 for tool_config in tools_to_register: 173 func = tool_config["func"] 174 tool_name = func.__name__ 175 176 try: 177 # Create or update the tool using the standalone function 178 created_tool = client.tools.upsert_from_function( 179 func=func, 180 args_schema=tool_config["args_schema"], 181 tags=tool_config["tags"] 182 ) 183 184 # Get current agent tools 185 current_tools = client.agents.tools.list(agent_id=str(agent.id)) 186 tool_names = [t.name for t in current_tools] 187 188 # Check if already attached 189 if created_tool.name in tool_names: 190 table.add_row(tool_name, "Already Attached", tool_config["description"]) 191 else: 192 # Attach to agent 193 client.agents.tools.attach( 194 agent_id=str(agent.id), 195 tool_id=str(created_tool.id) 196 ) 197 table.add_row(tool_name, "✓ Attached", tool_config["description"]) 198 199 except Exception as e: 200 table.add_row(tool_name, f"✗ Error: {str(e)}", tool_config["description"]) 201 logger.error(f"Error registering tool {tool_name}: {e}") 202 203 console.print(table) 204 205 except Exception as e: 206 console.print(f"[red]Error: {str(e)}[/red]") 207 logger.error(f"Fatal error: {e}") 208 209 210def list_available_tools(): 211 """List all available tools.""" 212 table = Table(title="Available Void Tools") 213 table.add_column("Tool Name", style="cyan") 214 table.add_column("Description") 215 table.add_column("Tags", style="dim") 216 217 for tool_config in TOOL_CONFIGS: 218 table.add_row( 219 tool_config["func"].__name__, 220 tool_config["description"], 221 ", ".join(tool_config["tags"]) 222 ) 223 224 console.print(table) 225 226 227if __name__ == "__main__": 228 import argparse 229 230 parser = argparse.ArgumentParser(description="Register Void tools with a Letta agent") 231 parser.add_argument("agent", nargs="?", default=None, help=f"Agent name (default: {agent_config['name']})") 232 parser.add_argument("--tools", nargs="+", help="Specific tools to register (default: all)") 233 parser.add_argument("--list", action="store_true", help="List available tools") 234 235 args = parser.parse_args() 236 237 if args.list: 238 list_available_tools() 239 else: 240 # Use config default if no agent specified 241 agent_name = args.agent if args.agent is not None else agent_config['name'] 242 console.print(f"\n[bold]Registering tools for agent: {agent_name}[/bold]\n") 243 register_tools(args.agent, args.tools)