From 9e6afbc1bca755b88b50de6703550684f6c48128 Mon Sep 17 00:00:00 2001 From: Cameron Pfiffer Date: Wed, 20 Aug 2025 15:46:14 -0700 Subject: [PATCH] Fix X mention username resolution and add user ID mapping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Fixes the '@unknown (unknown)' issue:** - Added fallback username resolution when thread_data['users'] lookup fails - Scans thread tweets to find author info when primary lookup returns 'unknown' - Logs successful fallback resolutions **Added User ID Mapping Key:** - Builds comprehensive user_id → @username mapping from thread data - Appends USER_ID_KEY section to prompts showing all participants - Helps void understand who is who when user IDs appear without handles **Example output:** ``` USER_ID_KEY: 1232326955652931584: @cameron_pfiffer 1188460292998602752: @scholzmx 1950680610282094592: @void_comind ``` This resolves cases where the X API doesn't consistently provide usernames in mention data, using the thread context as a reliable fallback source. 🤖 Generated with Claude Code Co-Authored-By: Claude --- x.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/x.py b/x.py index d893b11..b493a85 100644 --- a/x.py +++ b/x.py @@ -1517,6 +1517,41 @@ def process_x_mention(void_agent, x_client, mention_data, queue_filepath=None, t author_username = author_info.get('username', 'unknown') author_name = author_info.get('name', author_username) + # Fallback: if username is unknown, try to find it in the thread tweets + if author_username == 'unknown' and 'tweets' in thread_data: + for tweet in thread_data['tweets']: + if tweet.get('author_id') == author_id and 'author' in tweet: + tweet_author = tweet['author'] + if tweet_author.get('username'): + author_username = tweet_author['username'] + author_name = tweet_author.get('name', author_username) + logger.info(f"Resolved unknown author via thread fallback: @{author_username} ({author_name})") + break + + # Build user ID mapping from thread data + user_id_mapping = {} + if 'tweets' in thread_data: + for tweet in thread_data['tweets']: + author_id_tweet = tweet.get('author_id') + if author_id_tweet and 'author' in tweet: + tweet_author = tweet['author'] + username = tweet_author.get('username') + if username and author_id_tweet not in user_id_mapping: + user_id_mapping[author_id_tweet] = f"@{username}" + + # Also add users from the users dict if available + if 'users' in thread_data: + for user_id, user_data in thread_data['users'].items(): + username = user_data.get('username') + if username and user_id not in user_id_mapping: + user_id_mapping[user_id] = f"@{username}" + + # Format user ID mapping for prompt + id_mapping_text = "" + if user_id_mapping: + id_mapping_lines = [f" {user_id}: {handle}" for user_id, handle in user_id_mapping.items()] + id_mapping_text = f"\n\nUSER_ID_KEY:\n" + "\n".join(id_mapping_lines) + prompt = f"""You received a mention on X (Twitter) from @{author_username} ({author_name}). MOST RECENT POST (the mention you're responding to): @@ -1527,7 +1562,7 @@ FULL THREAD CONTEXT: {thread_context} ``` -The YAML above shows the complete conversation thread. The most recent post is the one mentioned above that you should respond to, but use the full thread context to understand the conversation flow. +The YAML above shows the complete conversation thread. The most recent post is the one mentioned above that you should respond to, but use the full thread context to understand the conversation flow.{id_mapping_text} If you need to update user information, use the x_user_* tools. -- 2.51.2