0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/plugins/discourse-ai/spec/lib
Gabriel Grubba 9c09e988b3
FEATURE: Approve AI moderation actions inline in bot chat DMs (#41565)
> Was stacked on #41497, which has since merged. This PR is now rebased
onto `main` and contains only the chat-approval work.

### What

Lets a moderator approve or reject an AI-bot moderation tool action
(`suspend_user` / `silence_user`) **inside a Chat direct message with
the bot**, instead of leaving for the `/review` queue — the chat
counterpart to the base PR's inline PM/topic card.

When the bot queues one of these actions in a DM, it posts a message
with **Approve / Reject** buttons rendered via the Chat plugin's native
interactive **blocks**. Clicking performs the queued action through the
existing `ReviewableAiToolAction` backend (credited to the approving
moderator) and rewrites the message to its resolved state, removing the
buttons.

### How

- `bot.rb` — `enqueue_tool_for_approval` branches on chat context: in
chat it emits a `:chat_approval` signal; in PM/topic it keeps the
existing inline card.
- `playground.rb` — `reply_to_chat_message` posts a bot chat message
carrying the Approve/Reject blocks, in the **same DM thread as the bot's
reply** (AI-bot DM replies are threaded by design). DM channels only.
- `chat_tool_approval.rb` — builds/parses the button `action_id`s,
builds the blocks, and handles the `chat_message_interaction` event:
performs the reviewable and rewrites the message. Runs synchronously so
the buttons clear before the request returns.
- `entry_point.rb` — registers the `:chat_message_interaction` listener.

### Authorization

`Chat::CreateMessageInteraction` only checks channel visibility, so
staff-gating is enforced here: the handler requires
`Reviewable.viewable_by(user)` **and** `Reviewable#perform` re-checks
(`ensure_performed_by_is_a_real_person!` + the approver's guardian).
Non-staff clicks are ignored. A crafted `action_id` can't target another
reviewable — core only matches `action_id`s present in that message's
own blocks.

### Core-chat changes (3 lines)

The blocks system was built for **create-time-only** blocks; nothing had
ever mutated a message's `blocks` after creation. Clearing the buttons
on approve/reject is the first such case, which required:

- `chat-message.js` — make `blocks` a `@tracked` property (so
reassigning it re-renders).
- `chat-channel-subscription-manager.js` +
`chat-channel-thread-subscription-manager.js` — refresh `message.blocks`
in `handleEditMessage` (so the block-clearing edit reaches the client,
in both the channel and thread views).

All are no-ops for the only other block user (category blocks, which are
never edited after creation).

### Testing

`plugins/discourse-ai/spec/lib/ai_bot/chat_tool_approval_spec.rb` —
action-id round-trip, block shape, staff gating, foreign/stale
action-ids, approve/reject, failure surfacing, and an end-to-end run
through the real `Chat::CreateMessageInteraction` service. The existing
`playground_spec.rb` chat-DM tests (threaded conversation + context)
continue to pass. Verified manually in a bot DM.
2026-07-13 17:08:35 -03:00
..
admin_dashboard FEATURE: Scoped AI highlights to user-selected categories (#41506) 2026-07-09 11:38:30 +08:00
agents FEATURE: Add AI agent suspend/silence tools with inline approval review (#41497) 2026-07-10 10:59:51 -03:00
ai_bot FEATURE: Approve AI moderation actions inline in bot chat DMs (#41565) 2026-07-13 17:08:35 -03:00
automation FEATURE: extract text from document uploads for LLM prompts (#39634) 2026-05-05 08:16:23 +10:00
completions FEATURE: add AI agent thinking effort (#41280) 2026-07-01 17:03:19 -03:00
discord/bot FIX: Run Discord agents as their associated user (#40914) 2026-06-15 18:00:30 -03:00
discourse_automation FIX: Avoid duplicate AI triage reviewables for spam flags (#41043) 2026-06-22 13:01:42 +08:00
discourse_workflows/nodes FEATURE: Support AI agent workflow uploads and runner (#41042) 2026-06-19 18:06:17 +02:00
inference
inferred_concepts DEV: AI persona to agent migration (#38319) 2026-03-10 15:59:45 +11:00
mcp DEV: Support other OAuth token auth methods (#41219) 2026-06-26 20:30:53 +08:00
modules FEATURE: Approve AI moderation actions inline in bot chat DMs (#41565) 2026-07-13 17:08:35 -03:00
translation FEATURE: Add category scope for AI translations (#41585) 2026-07-10 09:39:20 +08:00
utils SECURITY: Harden imagemagick execution 2026-06-30 16:10:52 +02:00
admin_dashboard_spec.rb FEATURE: AI highlights for new dashboard (#40740) 2026-06-10 23:00:35 +08:00
ai_api_audit_log_cleanup_spec.rb FEATURE: Also delete api logs when user / user content is deleted (#41122) 2026-06-24 15:21:00 +08:00
guardian_extensions_spec.rb SECURITY: Force regeneration for edit-outdated summaries and block stale fallback 2026-03-31 15:12:45 +01:00