A platform for community discussion. Free, open, simple.
DEV: Teach the AI workflow author about chat direct messages
Régis Hanol8a171e936d7e
diff --git a/plugins/discourse-workflows/USE_CASES.md b/plugins/discourse-workflows/USE_CASES.mdindex d5364959bfe..543e8977eb2 100644--- a/plugins/discourse-workflows/USE_CASES.md+++ b/plugins/discourse-workflows/USE_CASES.md@@ -30,6 +30,7 @@ For each run, record:| `manual-maintenance-chat` | `create a manual workflow that sends a message to General chat saying Maintenance window is starting now` | `trigger:manual -> action:send_chat_message` | Static message does not need expression syntax; validates without expression errors. || `closed-general-tl1-chat` | `when a topic by a tl1 or lower is closed in the general category message the general channel with a link to the post` | `trigger:topic_closed -> action:topic (get) -> action:group (check_membership) -> condition:if -> action:send_chat_message` | Uses `action:topic` get to hydrate first-post fields (the topic payload has no `trust_level`). Resolves the `trust_level_2` automatic group and uses `action:group` with `operation: check_membership`, resolved `group_id`, and `username: ={{ $json.post.username }}`, then continues on the condition false branch (`group_membership.in_group == false`) for TL1-or-lower because `trust_level_N` is cumulative for TL >= N. Uses `$json.post.post_url`; no Code node; no clarification needed. || `friend-group-post-dm-admin` | `when anyone in the friend group posts send admin a dm with a link to the post` | `trigger:post_created -> action:group (check_membership) -> condition:if -> action:send_personal_message` | Resolves the `friend` group and `admin` user; sets resolved `group_id`, uses `$json.post.username` for membership, branches on `$json.group_membership.in_group`, and uses `$json.post.post_url` in a leading-`=` personal message body; no Code node. |+| `tl1-chat-dm` | `when a user reaches trust level 1, send them a chat DM welcoming them to chat` | `trigger:user_trust_level_changed -> action:send_chat_message` | Sets `new_trust_levels: ["1"]` on the trigger. Uses `target: "user"` with `target_usernames: ={{ $json.user.username }}` and default `actor_username: system`; does not call `search_chat_channels` or set `channel_id`; message template starts with `=`. Messages from `system` skip the recipient's DM preferences. A chat DM on `trigger:user_created` fails with default settings because new signups are inactive and TL0 (not in `chat_allowed_groups`); welcome signups with `action:send_personal_message` instead. |## Additional prompts to rotate into evals@@ -80,7 +81,7 @@ For each run, record:- Deleting a post is `action:post` with `operation: delete`; `action:flag_post` with `review_delete` is for moderation flows that also need a reviewable. Restoring one is `operation: recover`.- Flag post reasons and Flag user notes name the nearest AI Agent on the item's executed input path. Conditions preserve that attribution; merged inputs from different Agents or incomplete item links retain Workflow-only attribution. This applies to new flags and does not change the Workflow filter context.- `trigger:post_destroyed` and `trigger:post_recovered` expose `$json.user` as the acting moderator, not the post author; use `$json.post.username` for the author.-- DM/personal-message notifications should use `action:send_personal_message` instead of chat or topic reply nodes.+- DM/personal-message notifications should use `action:send_personal_message` instead of chat or topic reply nodes. Only an explicit chat DM uses `action:send_chat_message` with `target: "user"` (or `"group"` for one group DM).- Forum `search`/`read` should not be used for node/schema discovery; use `workflow_node_catalog` and `workflow_validate_patch`.## Moderation entrypointsdiff --git a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rbindex 1a66034f7b0..6dc3a312db1 100644--- a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb+++ b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb@@ -129,7 +129,7 @@ module Jobsif ::DiscourseWorkflows::Ai::Tools::SearchChatChannels.available?context_tools[:search_chat_channels- ] = "Call this before asking the admin to choose a chat channel or before setting action:send_chat_message channel_id. Use returned matches; never invent channel names or IDs."+ ] = "Call this before asking the admin to choose a chat channel or before setting action:send_chat_message channel_id with target \"channel\". Use returned matches; never invent channel names or IDs. Not needed for target \"user\", which takes usernames or expressions in target_usernames."endif ::DiscourseWorkflows::Ai::Tools::SearchChatIntegrationChannels.available?context_tools[diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rbindex f29ac2a068b..8b234f712d9 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb@@ -158,7 +158,7 @@ module DiscourseWorkflows],"action:send_personal_message" => [{- name: "DM a post link to an admin",+ name: "Send a personal message with a post link to an admin",parameters: {recipient_usernames: ["admin"],title: "=New post from @{{ $json.post.username }}",@@ -167,6 +167,27 @@ module DiscourseWorkflows},},],+ "action:send_chat_message" => [+ {+ # Signups are inactive and TL0 when `user_created` fires, so they can't chat yet.+ name: "Welcome a user who reached trust level 1 with a chat DM",+ parameters: {+ target: "user",+ target_usernames: "={{ $json.user.username }}",+ message: "=Welcome to the community, @{{ $json.user.username }}!",+ actor_username: "system",+ },+ },+ {+ name: "Ping the moderators in one group chat DM about a flagged post",+ parameters: {+ target: "group",+ target_usernames: %w[alice bob],+ message: "=Please review {{ $json.post.post_url }}",+ actor_username: "system",+ },+ },+ ],"action:send_chat_integration_message" => [{name: "Send a message to an external chat integration channel",@@ -257,6 +278,7 @@ module DiscourseWorkflowsSEARCH_ALIASES = {"action:send_personal_message" => "dm direct message pm personal private message",+ "action:send_chat_message" => "chat channel dm direct message","action:send_chat_integration_message" =>"external chat integration notification slack discord telegram mattermost matrix zulip rocket chat gitter groupme teams power automate webex google chat guilded","action:ai_agent" =>diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rbindex 55b04302ca8..2348b579281 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb@@ -58,7 +58,9 @@ module DiscourseWorkflows- Keep generated AI agent system prompts focused on the workflow task. A simple helpful prompt is acceptable when the task is broad, but prefer concise task-specific instructions when the workflow needs a clear output.- When proposing a new AI agent, include a create_ai_agent operation before the action:ai_agent node that uses it. Reference the proposed agent with agent_id: { "$ref": "agent-client-id" } and set agent_name to the proposed agent name.- Include created AI agents in proposal assumptions or risks so admins know the draft creates a reusable agent record.- - Use search_chat_channels before asking the admin to choose a chat channel or before setting action:send_chat_message channel_id. Never invent chat channel names, IDs, or clarification options. If search_chat_channels returns matches, ask using those exact channel names/IDs or use the named match. If it returns no matches, ask for a custom channel name/ID instead of suggesting made-up channels.+ - action:send_chat_message posts to a chat channel (target "channel", the default), sends each listed user their own chat direct message (target "user"), or sends one group chat direct message to all of them (target "group"). Use target "user" or "group" only when the admin asks for a chat DM; a plain DM or personal message to a user means action:send_personal_message.+ - With action:send_chat_message target "user" or "group", set target_usernames to an array of usernames or an expression such as ={{ $json.user.username }}, and actor_username to the sender (defaults to system). Do not call search_chat_channels or set channel_id in this mode. The recipient must already be able to chat: users from trigger:user_created are usually still inactive and at trust level 0, so welcome new signups with action:send_personal_message instead.+ - With action:send_chat_message target "channel", use search_chat_channels before asking the admin to choose a chat channel or before setting channel_id. Never invent chat channel names, IDs, or clarification options. If search_chat_channels returns matches, ask using those exact channel names/IDs or use the named match. If it returns no matches, ask for a custom channel name/ID instead of suggesting made-up channels.- Use search_chat_integration_channels before asking the admin to choose an external chat integration channel or before setting action:send_chat_integration_message channel_id. Never invent provider names, channel labels, IDs, or clarification options. If search_chat_integration_channels returns matches, set channel_id and channel_name from the exact match or ask using those exact values. If it returns no matches, explain that a matching channel must be configured instead of suggesting made-up channels.- Use workflow_validate_patch as a dry-run planning tool when drafting workflow changes. It does not save anything. For non-trivial workflows, query relevant node types with workflow_node_catalog, then call workflow_validate_patch after adding or connecting candidate nodes to inspect node_fields for exact input/output paths, then continue from those fields.- If workflow_validate_patch returns expression_errors or schema-path errors, repair the operations and call workflow_validate_patch again before returning a final proposal.diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rbindex 458b3eee7ad..927cc034a39 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb@@ -286,6 +286,19 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog doexpect(result[:nodes].map { |node| node[:type] }).to include("trigger:tag_created")end+ it "finds the chat action for DM queries but not for personal message ones",+ :aggregate_failures do+ skip "Chat plugin is not available" if !defined?(::Chat::Channel)+ SiteSetting.chat_enabled = true++ dm_types = invoke_tool(query: "dm")[:nodes].map { |node| node[:type] }+ pm_types = invoke_tool(query: "personal pm")[:nodes].map { |node| node[:type] }++ expect(dm_types).to include("action:send_personal_message", "action:send_chat_message")+ expect(pm_types).to include("action:send_personal_message")+ expect(pm_types).not_to include("action:send_chat_message")+ end+it "matches broad multi-term catalog queries", :aggregate_failures doresult =described_class.new(diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai_workflow_author_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai_workflow_author_spec.rbindex ae3e25e5bcf..f7a5c671715 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai_workflow_author_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai_workflow_author_spec.rb@@ -30,7 +30,7 @@ RSpec.describe DiscourseWorkflows::AiWorkflowAuthor do"Use workflow_ai_agent_catalog before adding action:ai_agent nodes","parameters.runner_username","parameters.upload_ids",- "Use search_chat_channels before asking the admin to choose a chat channel",+ 'With action:send_chat_message target "user" or "group", set target_usernames to an array of usernames or an expression',"Use search_chat_integration_channels before asking the admin to choose an external chat integration channel","call workflow_validate_script with the exact mode and code","Do not add a Code node only to copy trigger fields forward",