import asyncio import discord from discord.ui import Button, View from bridge.manager import SessionManager class ApprovalView(View): def __init__(self, agent_wrapper): super().__init__(timeout=None) # Persistent buttons self.agent_wrapper = agent_wrapper @discord.ui.button(label="Approve", style=discord.ButtonStyle.green, custom_id="approve_btn") async def approve(self, interaction: discord.Interaction, button: discord.ui.Button): if interaction.user.id != self.agent_wrapper.bot.authorized_user_id: await interaction.response.send_message("āŒ You are not authorized to approve this.", ephemeral=True) return await interaction.response.defer() # Resolve the future to True (allow tool execution) self.agent_wrapper.resolve_approval(True) # Disable buttons after action for item in self.children: item.disabled = True await interaction.message.edit(content=interaction.message.content + "\n\n🟢 **Approved by User**", view=self) self.stop() @discord.ui.button(label="Deny", style=discord.ButtonStyle.red, custom_id="deny_btn") async def deny(self, interaction: discord.Interaction, button: discord.ui.Button): if interaction.user.id != self.agent_wrapper.bot.authorized_user_id: await interaction.response.send_message("āŒ You are not authorized to deny this.", ephemeral=True) return await interaction.response.defer() # Resolve the future to False (deny tool execution) self.agent_wrapper.resolve_approval(False) # Disable buttons after action for item in self.children: item.disabled = True await interaction.message.edit(content=interaction.message.content + "\n\nšŸ”“ **Denied by User**", view=self) self.stop() class AntigravityBot(discord.Client): def __init__(self, authorized_user_id: int): intents = discord.Intents.default() intents.message_content = True intents.members = True super().__init__(intents=intents) self.authorized_user_id = authorized_user_id self.manager = SessionManager(self) async def on_ready(self): print(f"šŸ¤– Bot logged in as {self.user} (ID: {self.user.id})") print(f"šŸ›”ļø Authorized User ID: {self.authorized_user_id}") async def on_message(self, message: discord.Message): # Ignore messages from bots (including self) if message.author.bot: return # Security check: Only respond to the authorized user if message.author.id != self.authorized_user_id: return # Check if the message is in a Thread is_thread = isinstance(message.channel, discord.Thread) if is_thread: thread_id = message.channel.id channel_id = message.channel.parent_id # Get or create the Antigravity session for this thread session = self.manager.get_or_create_session(thread_id, channel_id) # Run the prompt in the background so the bot remains responsive asyncio.create_task(session.run_prompt(message.content)) else: # If it's a normal channel, guide the user to create a Thread await message.reply( "šŸ’” To start a conversation with Antigravity, please **create a Thread** in this channel " "or reply to an existing Thread. Each Thread represents an isolated coding session." ) async def send_approval_request(self, thread_id: int, content: str): thread = self.get_channel(thread_id) if thread: session = self.manager.active_sessions.get(thread_id) if session: view = ApprovalView(session) await thread.send(content=content, view=view) async def send_to_thread(self, thread_id: int, content: str) -> discord.Message: thread = self.get_channel(thread_id) if thread: return await thread.send(content=content) return None