-
Notifications
You must be signed in to change notification settings - Fork 46k
feat(backend): let AutoPilot edit its own chat-platform messages #14436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
2ed37b1
c2e083e
5711e02
144d0ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| from ..base import ( | ||
| ChannelInfo, | ||
| ChannelType, | ||
| EditOutcome, | ||
| FileAttachment, | ||
| InboundAttachment, | ||
| MessageCallback, | ||
|
|
@@ -146,12 +147,22 @@ async def _resolve_channel(self, channel_id: str): | |
| ``Client.get_channel`` only reads the in-memory cache, so it misses | ||
| threads the bot hasn't seen since its last restart. Fall back to | ||
| ``fetch_channel`` (REST) so long-lived threads keep working. | ||
|
|
||
| ``channel_id`` reaches here as a caller-supplied string (a model-chosen | ||
| edit target, a raw proactive-post ID) that was never guaranteed to look | ||
| like a snowflake, so the ``int()`` conversion is inside the guarded | ||
| block rather than raising ``ValueError`` straight out to the RPC layer. | ||
| """ | ||
| channel = self._client.get_channel(int(channel_id)) | ||
| try: | ||
| numeric_id = int(channel_id) | ||
| except ValueError: | ||
| logger.warning("Channel id %r is not a valid snowflake", channel_id) | ||
| return None | ||
| channel = self._client.get_channel(numeric_id) | ||
| if channel is not None: | ||
| return channel | ||
| try: | ||
| return await self._client.fetch_channel(int(channel_id)) | ||
| return await self._client.fetch_channel(numeric_id) | ||
| except (discord.NotFound, discord.Forbidden, discord.HTTPException): | ||
| logger.warning("Channel %s not found or inaccessible", channel_id) | ||
| return None | ||
|
|
@@ -355,6 +366,33 @@ async def create_channel_thread( | |
| ) | ||
| return PostedRef(id=str(thread.id), url=thread.jump_url) | ||
|
|
||
| async def edit_channel_message( | ||
| self, channel_id: str, ref_id: str, text: str | ||
| ) -> EditOutcome: | ||
| channel = await self._resolve_channel(channel_id) | ||
| if channel is None or not isinstance(channel, discord.abc.Messageable): | ||
| return EditOutcome.NOT_FOUND | ||
| try: | ||
| message = await channel.fetch_message(int(ref_id)) | ||
| except ValueError: | ||
| return EditOutcome.NOT_FOUND | ||
| except discord.NotFound: | ||
| return EditOutcome.NOT_FOUND | ||
| except discord.HTTPException: | ||
| logger.exception("Failed to fetch message %s for edit", ref_id) | ||
| return EditOutcome.FAILED | ||
| rendered, allowed = _resolve_mentions( | ||
| text, await self._mentionables_for(channel, text, ()) | ||
| ) | ||
| try: | ||
| await message.edit(content=rendered, allowed_mentions=allowed) | ||
|
Comment on lines
+376
to
+388
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Return editable message references for Discord and Telegram thread posts. When 🤖 Prompt for AI Agents |
||
| except discord.HTTPException: | ||
| # Covers both a rejected edit (message too old/foreign author) and | ||
| # a body over Discord's cap — either way the edit did not land. | ||
| logger.exception("Failed to edit message %s", ref_id) | ||
| return EditOutcome.FAILED | ||
| return EditOutcome.OK | ||
|
|
||
| async def _send_chunked( | ||
| self, channel: discord.abc.Messageable, text: str | ||
| ) -> Optional[discord.Message]: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| from backend.copilot.bot.adapters.base import ( | ||
| ChannelInfo, | ||
| ChannelType, | ||
| EditOutcome, | ||
| FileAttachment, | ||
| MessageCallback, | ||
| MessageContext, | ||
|
|
@@ -428,6 +429,25 @@ async def create_channel_thread( | |
| body = f"**{name}**\n\n{text}" if name else text | ||
| return await self.post_channel_message(channel_id, body) | ||
|
|
||
| async def edit_channel_message( | ||
| self, channel_id: str, ref_id: str, text: str | ||
| ) -> EditOutcome: | ||
| activity = { | ||
| "type": "message", | ||
| "text": self.localize_markup(text), | ||
| "textFormat": "markdown", | ||
| } | ||
| try: | ||
| await self._client.update_activity( | ||
| self._service_url_for(channel_id), channel_id, ref_id, activity | ||
| ) | ||
| except TeamsApiError as e: | ||
| logger.exception("Failed to edit Teams activity %s", ref_id) | ||
| if e.status_code == 404: | ||
| return EditOutcome.NOT_FOUND | ||
| return EditOutcome.FAILED | ||
| return EditOutcome.OK | ||
|
|
||
|
Comment on lines
+432
to
+450
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Map Teams 404 responses to When the Connector returns 404 for the 🤖 Prompt for AI Agents |
||
| async def open_dm_channel(self, platform_user_id: str) -> Optional[str]: | ||
| """Create (or fetch) the bot's 1:1 conversation with a user. | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.