a digital person for bluesky
1#!/usr/bin/env python3
2"""Platform-specific tool management for Void agent."""
3import logging
4from typing import List, Set
5from letta_client import Letta
6from config_loader import get_letta_config, get_agent_config
7
8logger = logging.getLogger(__name__)
9
10# Define platform-specific tool sets
11BLUESKY_TOOLS = {
12 'search_bluesky_posts',
13 'create_new_bluesky_post',
14 'get_bluesky_feed',
15 'add_post_to_bluesky_reply_thread',
16 'attach_user_blocks',
17 'detach_user_blocks',
18 'user_note_append',
19 'user_note_replace',
20 'user_note_set',
21 'user_note_view',
22}
23
24X_TOOLS = {
25 'add_post_to_x_thread',
26 'search_x_posts',
27 'attach_x_user_blocks',
28 'detach_x_user_blocks',
29 'x_user_note_append',
30 'x_user_note_replace',
31 'x_user_note_set',
32 'x_user_note_view',
33}
34
35# Common tools shared across platforms
36COMMON_TOOLS = {
37 'halt_activity',
38 'ignore_notification',
39 'annotate_ack',
40 'create_whitewind_blog_post',
41 'fetch_webpage',
42}
43
44
45def ensure_platform_tools(platform: str, agent_id: str = None) -> None:
46 """
47 Ensure the correct tools are attached for the specified platform.
48
49 This function will:
50 1. Detach tools that belong to other platforms
51 2. Keep common tools attached
52 3. Ensure platform-specific tools are attached
53
54 Args:
55 platform: Either 'bluesky' or 'x'
56 agent_id: Agent ID to manage tools for (uses config default if None)
57 """
58 if platform not in ['bluesky', 'x']:
59 raise ValueError(f"Platform must be 'bluesky' or 'x', got '{platform}'")
60
61 letta_config = get_letta_config()
62 agent_config = get_agent_config()
63
64 # Use agent ID from config if not provided
65 if agent_id is None:
66 agent_id = letta_config.get('agent_id', agent_config.get('id'))
67
68 try:
69 # Initialize Letta client
70 client = Letta(token=letta_config['api_key'])
71
72 # Get the agent
73 try:
74 agent = client.agents.retrieve(agent_id=agent_id)
75 logger.info(f"Managing tools for agent '{agent.name}' ({agent_id}) for platform '{platform}'")
76 except Exception as e:
77 logger.error(f"Could not retrieve agent {agent_id}: {e}")
78 return
79
80 # Get current attached tools
81 current_tools = client.agents.tools.list(agent_id=str(agent.id))
82 current_tool_names = {tool.name for tool in current_tools}
83 current_tool_mapping = {tool.name: tool for tool in current_tools}
84
85 # Determine which tools to keep and which to remove
86 if platform == 'bluesky':
87 tools_to_keep = BLUESKY_TOOLS | COMMON_TOOLS
88 tools_to_remove = X_TOOLS
89 required_tools = BLUESKY_TOOLS
90 else: # platform == 'x'
91 tools_to_keep = X_TOOLS | COMMON_TOOLS
92 tools_to_remove = BLUESKY_TOOLS
93 required_tools = X_TOOLS
94
95 # Detach tools that shouldn't be on this platform
96 tools_to_detach = tools_to_remove & current_tool_names
97 for tool_name in tools_to_detach:
98 try:
99 tool = current_tool_mapping[tool_name]
100 client.agents.tools.detach(
101 agent_id=str(agent.id),
102 tool_id=str(tool.id)
103 )
104 logger.info(f"Detached {tool_name} (not needed for {platform})")
105 except Exception as e:
106 logger.error(f"Failed to detach {tool_name}: {e}")
107
108 # Check which required tools are missing
109 missing_tools = required_tools - current_tool_names
110
111 if missing_tools:
112 logger.info(f"Missing {len(missing_tools)} {platform} tools: {missing_tools}")
113 logger.info(f"Please run the appropriate registration script:")
114 if platform == 'bluesky':
115 logger.info(" python register_tools.py")
116 else:
117 logger.info(" python register_x_tools.py")
118 else:
119 logger.info(f"All required {platform} tools are already attached")
120
121 # Log final state
122 remaining_tools = (current_tool_names - tools_to_detach) & tools_to_keep
123 logger.info(f"Tools configured for {platform}: {len(remaining_tools)} tools active")
124
125 except Exception as e:
126 logger.error(f"Error managing platform tools: {e}")
127 raise
128
129
130def get_attached_tools(agent_id: str = None) -> Set[str]:
131 """
132 Get the currently attached tools for an agent.
133
134 Args:
135 agent_id: Agent ID to check (uses config default if None)
136
137 Returns:
138 Set of tool names currently attached
139 """
140 letta_config = get_letta_config()
141 agent_config = get_agent_config()
142
143 # Use agent ID from config if not provided
144 if agent_id is None:
145 agent_id = letta_config.get('agent_id', agent_config.get('id'))
146
147 try:
148 client = Letta(token=letta_config['api_key'])
149 agent = client.agents.retrieve(agent_id=agent_id)
150 current_tools = client.agents.tools.list(agent_id=str(agent.id))
151 return {tool.name for tool in current_tools}
152 except Exception as e:
153 logger.error(f"Error getting attached tools: {e}")
154 return set()
155
156
157if __name__ == "__main__":
158 import argparse
159
160 parser = argparse.ArgumentParser(description="Manage platform-specific tools for Void agent")
161 parser.add_argument("platform", choices=['bluesky', 'x'], nargs='?', help="Platform to configure tools for")
162 parser.add_argument("--agent-id", help="Agent ID (default: from config)")
163 parser.add_argument("--list", action="store_true", help="List current tools without making changes")
164
165 args = parser.parse_args()
166
167 if args.list:
168 tools = get_attached_tools(args.agent_id)
169 print(f"\nCurrently attached tools ({len(tools)}):")
170 for tool in sorted(tools):
171 platform_indicator = ""
172 if tool in BLUESKY_TOOLS:
173 platform_indicator = " [Bluesky]"
174 elif tool in X_TOOLS:
175 platform_indicator = " [X]"
176 elif tool in COMMON_TOOLS:
177 platform_indicator = " [Common]"
178 print(f" - {tool}{platform_indicator}")
179 else:
180 if not args.platform:
181 parser.error("platform is required when not using --list")
182 ensure_platform_tools(args.platform, args.agent_id)