A platform for community discussion. Free, open, simple.
FIX: Keep AI tool approval cards within the chat message limit (#45473)
Keegan George0801565a151d
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.ymlindex 84866cdaf90..d249ef658ff 100644--- a/plugins/discourse-ai/config/locales/server.en.yml+++ b/plugins/discourse-ai/config/locales/server.en.yml@@ -671,6 +671,7 @@ en:rejected: "Rejected by @%{username}."failed: "Could not complete the action: %{error}"unexpected_error: "an unexpected error occurred"+ details_shortened: "These details were shortened to fit in chat."agents:forced_default_llm_required: "Select a default language model before requiring this agent to always use it."bot_user_unsupported: "This agent is used internally by a feature and cannot have a bot user"diff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rbindex a36e1d7f07c..10a45088310 100644--- a/plugins/discourse-ai/lib/agents/bot.rb+++ b/plugins/discourse-ai/lib/agents/bot.rb@@ -624,7 +624,7 @@ module DiscourseAireviewable_id: reviewable.id,summary: tool.approval_title,changes: tool.approval_changes,- details: tool.approval_details,+ details: chat_approval_details(tool),description_label: tool.approval_description_label,show_description: tool.approval_show_description?,question: tool.approval_question,@@ -651,6 +651,16 @@ module DiscourseAi{ status: "pending_approval", message: I18n.t("discourse_ai.ai_bot.tool_pending_approval") }end+ # The approval card is posted as a chat message, so its details must fit+ # within the chat message length limit or the card cannot be created.+ def chat_approval_details(tool)+ details = tool.approval_details.to_s+ max_length = SiteSetting.chat_maximum_message_length+ return details if details.length <= max_length++ tool.compact_approval_details(max_length:).to_s.truncate(max_length)+ end+def process_tool(tool:,raw_context:,diff --git a/plugins/discourse-ai/lib/agents/tools/tool.rb b/plugins/discourse-ai/lib/agents/tools/tool.rbindex a0bb44c114f..b943a3dcafb 100644--- a/plugins/discourse-ai/lib/agents/tools/tool.rb+++ b/plugins/discourse-ai/lib/agents/tools/tool.rb@@ -140,6 +140,17 @@ module DiscourseAidetailsend+ # Used instead of approval_details when they are longer than an approval+ # surface allows, such as a chat message. Tools with large details can+ # override this with a compact version of the same action.+ def compact_approval_details(max_length:)+ notice = I18n.t("discourse_ai.ai_bot.chat_tool_approval.details_shortened")+ available = max_length - notice.length - 2+ return notice.truncate(max_length) if available <= 0++ "#{notice}\n\n#{approval_details.to_s.truncate(available)}"+ end+def approval_questionI18n.t("discourse_ai.ai_bot.chat_tool_approval.question")enddiff --git a/plugins/discourse-ai/spec/lib/agents/tools/tool_spec.rb b/plugins/discourse-ai/spec/lib/agents/tools/tool_spec.rbindex 89ae5b7ae36..258cce693e9 100644--- a/plugins/discourse-ai/spec/lib/agents/tools/tool_spec.rb+++ b/plugins/discourse-ai/spec/lib/agents/tools/tool_spec.rb@@ -43,4 +43,40 @@ RSpec.describe DiscourseAi::Agents::Tools::Tool doexpect(result).to eq("abcabcabca")endend++ describe "#compact_approval_details" do+ fab!(:admin)++ let(:notice) { I18n.t("discourse_ai.ai_bot.chat_tool_approval.details_shortened") }+ let(:tool) do+ Class+ .new(described_class) do+ def self.name+ "long_details"+ end++ def approval_details+ "#{"a" * 200}\n\n#{"b" * 200}"+ end+ end+ .new(+ {},+ bot_user: admin,+ llm: nil,+ context: DiscourseAi::Agents::BotContext.new(user: admin, messages: []),+ )+ end++ it "keeps the start of the details after a notice and fits the limit" do+ details = tool.compact_approval_details(max_length: 120)++ expect(details.length).to be <= 120+ expect(details).to start_with("#{notice}\n\naaa")+ expect(details).not_to include("b")+ end++ it "returns only the notice when there is no room for the details" do+ expect(tool.compact_approval_details(max_length: 20)).to eq(notice.truncate(20))+ end+ endenddiff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rbindex e282a73fb36..b06822cb32f 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb@@ -1237,6 +1237,51 @@ RSpec.describe DiscourseAi::AiBot::Playground do)end+ it "shortens approval details that do not fit in a chat message" do+ target = Fabricate(:user)+ agent.update!(tools: ["SuspendUser"], require_approval: true)+ DiscourseAi::Agents::Agent+ .any_instance+ .stubs(:stop_chain_on_pending_approval?)+ .returns(true)+ DiscourseAi::Agents::Tools::SuspendUser+ .any_instance+ .stubs(:approval_details)+ .returns("Suspension details. " * 50)+ SiteSetting.chat_maximum_message_length = 200++ tool_call =+ DiscourseAi::Completions::ToolCall.new(+ name: "suspend_user",+ id: "suspend",+ parameters: {+ username: target.username,+ duration_days: 3,+ reason: "spam",+ },+ )++ DiscourseAi::Completions::Llm.with_prepared_responses([[tool_call]]) do+ ChatSDK::Message.create(channel_id: dm_channel.id, raw: "Suspend them", guardian:)+ end++ approval_message = Chat::Message.where(chat_channel: dm_channel).where.not(blocks: nil).sole+ reviewable = ReviewableAiToolAction.last++ expect(approval_message.message.length).to be <= 200+ expect(approval_message.message).to start_with(+ I18n.t("discourse_ai.ai_bot.chat_tool_approval.details_shortened"),+ )+ expect(+ approval_message.blocks.first["elements"].map { |element| element["action_id"] },+ ).to eq(+ %W[+ ai_tool_approval::approve::#{reviewable.id}+ ai_tool_approval::reject::#{reviewable.id}+ ],+ )+ end+it "can reply to a chat message" domessage =DiscourseAi::Completions::Llm.with_prepared_responses(["World"]) dodiff --git a/plugins/discourse-workflows/assets/javascripts/discourse/components/admin-assistant/workflow-preview.gjs b/plugins/discourse-workflows/assets/javascripts/discourse/components/admin-assistant/workflow-preview.gjsindex 72cd140e1db..ccaca5a56c8 100644--- a/plugins/discourse-workflows/assets/javascripts/discourse/components/admin-assistant/workflow-preview.gjs+++ b/plugins/discourse-workflows/assets/javascripts/discourse/components/admin-assistant/workflow-preview.gjs@@ -126,6 +126,12 @@ export default class AdminAssistantWorkflowPreview extends Component {</p>{{/if}}+ {{#if this.preview.shortened}}+ <p class="assistant-workflow-preview__notice">+ {{i18n "discourse_workflows.admin_assistant.preview.shortened"}}+ </p>+ {{/if}}+{{#if this.layout}}<div class="assistant-workflow-preview__scroller" tabindex="0"><div@@ -222,9 +228,15 @@ export default class AdminAssistantWorkflowPreview extends Component {{{/if}}<span class="assistant-workflow-preview__details-label">{{dIcon "caret-right"}}- {{i18n- "discourse_workflows.admin_assistant.preview.exact_configuration"- }}+ {{#if this.preview.shortened}}+ {{i18n+ "discourse_workflows.admin_assistant.preview.shortened_configuration"+ }}+ {{else}}+ {{i18n+ "discourse_workflows.admin_assistant.preview.exact_configuration"+ }}+ {{/if}}</span></summary><pre><code>{{this.exactConfiguration}}</code></pre>diff --git a/plugins/discourse-workflows/assets/javascripts/discourse/lib/admin-assistant/workflow-preview.js b/plugins/discourse-workflows/assets/javascripts/discourse/lib/admin-assistant/workflow-preview.jsindex 26f0ad64265..c9121dfa7cd 100644--- a/plugins/discourse-workflows/assets/javascripts/discourse/lib/admin-assistant/workflow-preview.js+++ b/plugins/discourse-workflows/assets/javascripts/discourse/lib/admin-assistant/workflow-preview.js@@ -284,27 +284,27 @@ export function extractWorkflowPreview(cooked) {) {return null;}+ const preview = {+ name: configuration.name,+ configuration,+ shortened: payload.shortened === true,+ };if (configuration.operations.length > MAX_PREVIEW_OPERATIONS) {- return { name: configuration.name, configuration, unavailable: true };+ return { ...preview, unavailable: true };}const graph = graphFromOperations(configuration.operations);if (!graph) {- return { name: configuration.name, configuration, unavailable: true };+ return { ...preview, unavailable: true };}if (graph.nodes.length > MAX_PREVIEW_NODES ||graph.connections.length > MAX_PREVIEW_CONNECTIONS) {- return {- name: configuration.name,- configuration,- nodeCount: graph.nodes.length,- unavailable: true,- };+ return { ...preview, nodeCount: graph.nodes.length, unavailable: true };}- return { name: configuration.name, configuration, ...graph };+ return { ...preview, ...graph };} catch {return null;}diff --git a/plugins/discourse-workflows/assets/stylesheets/common/admin-assistant-workflow-preview.scss b/plugins/discourse-workflows/assets/stylesheets/common/admin-assistant-workflow-preview.scssindex 5806dd8eeb3..5f34b31d75c 100644--- a/plugins/discourse-workflows/assets/stylesheets/common/admin-assistant-workflow-preview.scss+++ b/plugins/discourse-workflows/assets/stylesheets/common/admin-assistant-workflow-preview.scss@@ -159,6 +159,13 @@padding: var(--space-3) var(--space-4);}+ &__notice {+ color: var(--primary-high);+ font-size: var(--font-down-1);+ margin: 0;+ padding: var(--space-3) var(--space-4);+ }+&__fallback {color: var(--primary-high);margin: 0;diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex 84cef69ee06..5fd195394c2 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -35,6 +35,8 @@ en:preview:label: "Workflow preview"exact_configuration: "Exact configuration"+ shortened_configuration: "Shortened configuration"+ shortened: "Some long settings are shortened here to fit in chat. Approving saves the full workflow unpublished, so you can review every setting before publishing it."read_only: "Read-only preview"script_validation_warning: "Some Code nodes failed syntax or mode checks. Review the errors in Exact configuration before approving."connects_to: "Connects to %{nodes}"diff --git a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/create_workflow.rb b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/create_workflow.rbindex 39b09dbe471..d286084fa84 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/create_workflow.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/create_workflow.rb@@ -4,6 +4,21 @@ module DiscourseWorkflowsmodule AdminAssistantclass CreateWorkflow < DiscourseAi::Agents::Tools::ToolPREVIEW_KIND = "discourse_admin_assistant_workflow_preview_v1"+ # Keys the preview needs to rebuild the graph, so they are never shortened.+ PREVIEW_GRAPH_KEYS = %w[+ op+ client_id+ node_id+ from+ to+ from_node_id+ to_node_id+ connection_type+ id+ type+ $ref+ ].freeze+ SHORTENED_VALUE_LENGTHS = [500, 160, 60].freezedef self.name"create_workflow"@@ -59,10 +74,32 @@ module DiscourseWorkflowsreturn I18n.t("discourse_workflows.admin_assistant.secret_draft")end- preview = JSON.pretty_generate(kind: PREVIEW_KIND, configuration:)- longest_run = preview.scan(/`+/).map(&:length).max.to_i- fence = "`" * [3, longest_run + 1].max- "#{fence}admin-assistant-workflow\n#{preview}\n#{fence}\n"+ preview_details(configuration)+ end++ # Large drafts do not fit in a chat message, so the preview keeps the full+ # graph and shortens long setting values, then drops settings entirely.+ def compact_approval_details(max_length:)+ return super if !draft || WorkflowDraft.contains_secret?(configuration)++ candidates =+ Enumerator.new do |yielder|+ yielder << preview_details(configuration, pretty: false)+ SHORTENED_VALUE_LENGTHS.each do |length|+ yielder << preview_details(+ shorten_values(configuration, length),+ pretty: false,+ shortened: true,+ )+ end+ yielder << preview_details(+ shorten_values(without_settings(configuration), SHORTENED_VALUE_LENGTHS.last),+ pretty: false,+ shortened: true,+ )+ end++ candidates.find { |candidate| candidate.length <= max_length } || superenddef validation_error@@ -138,6 +175,58 @@ module DiscourseWorkflowsdraft.slice(:name, :operations, :script_validations)end+ def preview_details(preview_configuration, pretty: true, shortened: false)+ payload = { kind: PREVIEW_KIND, configuration: preview_configuration }+ payload[:shortened] = true if shortened+ preview = pretty ? JSON.pretty_generate(payload) : JSON.generate(payload)+ longest_run = preview.scan(/`+/).map(&:length).max.to_i+ fence = "`" * [3, longest_run + 1].max+ "#{fence}admin-assistant-workflow\n#{preview}\n#{fence}\n"+ end++ def shorten_values(value, length, key = nil)+ case value+ when Hash+ value.to_h do |nested_key, nested_value|+ [nested_key, shorten_values(nested_value, length, nested_key)]+ end+ when Array+ value.map { |nested_value| shorten_values(nested_value, length, key) }+ when String+ PREVIEW_GRAPH_KEYS.include?(key.to_s) ? value : value.truncate(length)+ else+ value+ end+ end++ def without_settings(preview_configuration)+ operations =+ Array+ .wrap(preview_configuration[:operations])+ .map do |operation|+ next operation if !operation.is_a?(Hash)++ case operation["op"]+ when "add_node"+ node = operation["node"]+ if node.is_a?(Hash)+ operation.merge("node" => node.slice("id", "name", "type"))+ else+ operation+ end+ when "create_ai_agent"+ agent = operation["agent"]+ agent.is_a?(Hash) ? operation.merge("agent" => agent.slice("name")) : operation+ when "update_node_parameters"+ operation.except("parameters")+ else+ operation+ end+ end++ preview_configuration.merge(operations:)+ end+def create_unpublished_workflow!workflow = nilDiscourseWorkflows::Workflow.transaction dodiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/admin_assistant/workflow_tools_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/admin_assistant/workflow_tools_spec.rbindex 49b335129d1..3e83e0100ff 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/admin_assistant/workflow_tools_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/admin_assistant/workflow_tools_spec.rb@@ -127,6 +127,81 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow doexpect { tool.invoke }.not_to change { DiscourseWorkflows::Workflow.count }end+ describe "#compact_approval_details" do+ let(:long_operations) do+ operations.map do |operation|+ next operation if operation["op"] != "add_node"++ operation.deep_merge("node" => { "parameters" => { "message" => "Long message. " * 80 } })+ end+ end+ let(:draft_attributes) { { name: "Manual log", operations: long_operations } }++ def preview_payload(details)+ code = Nokogiri::HTML5.fragment(PrettyText.cook(details)).at_css("pre code")+ JSON.parse(code.text)+ end++ def graph(payload)+ payload+ .dig("configuration", "operations")+ .map do |operation|+ operation.slice("op", "client_id", "from", "to").merge(+ operation.fetch("node", {}).slice("name", "type"),+ )+ end+ end++ it "uses compact JSON when the formatted preview does not fit" do+ details = tool.compact_approval_details(max_length: tool.details.length - 1)+ payload = preview_payload(details)++ expect(details.length).to be < tool.details.length+ expect(payload).not_to have_key("shortened")+ expect(payload["configuration"]).to eq(draft_attributes.deep_stringify_keys)+ end++ it "shortens long setting values but keeps the graph" do+ details = tool.compact_approval_details(max_length: 1_200)+ payload = preview_payload(details)+ message = payload.dig("configuration", "operations", 1, "node", "parameters", "message")++ expect(details.length).to be <= 1_200+ expect(payload["shortened"]).to eq(true)+ expect(message).to end_with("...")+ expect(message.length).to be < long_operations.dig(1, "node", "parameters", "message").length+ expect(graph(payload)).to eq(graph("configuration" => draft_attributes.deep_stringify_keys))+ end++ it "drops settings when shortened values still do not fit" do+ details = tool.compact_approval_details(max_length: 500)+ payload = preview_payload(details)++ expect(details.length).to be <= 500+ expect(payload["shortened"]).to eq(true)+ expect(payload.dig("configuration", "operations", 1, "node")).to eq(+ "type" => "action:log",+ "name" => "Log",+ )+ expect(graph(payload)).to eq(graph("configuration" => draft_attributes.deep_stringify_keys))+ end++ it "falls back to a shortened notice when even the graph does not fit" do+ details = tool.compact_approval_details(max_length: 120)++ expect(details.length).to be <= 120+ expect(details).to start_with(+ I18n.t("discourse_ai.ai_bot.chat_tool_approval.details_shortened"),+ )+ end++ it "never includes the draft token" do+ [1_200, 500, 120].each do |max_length|+ expect(tool.compact_approval_details(max_length:)).not_to include(token)+ end+ end+ end+it "rejects secret-bearing script validation metadata in signed drafts" dotool.parameters[:draft_token] = DiscourseWorkflows::AdminAssistant::WorkflowDraft.sign(draft_attributes.merge(diff --git a/plugins/discourse-workflows/test/javascripts/components/admin-assistant/workflow-preview-test.gjs b/plugins/discourse-workflows/test/javascripts/components/admin-assistant/workflow-preview-test.gjsindex 7f920d54723..143978c1856 100644--- a/plugins/discourse-workflows/test/javascripts/components/admin-assistant/workflow-preview-test.gjs+++ b/plugins/discourse-workflows/test/javascripts/components/admin-assistant/workflow-preview-test.gjs@@ -3,6 +3,7 @@ import { click, render, settled } from "@ember/test-helpers";import { module, test } from "qunit";import { setupRenderingTest } from "discourse/tests/helpers/component-test";import DDecoratedHtml from "discourse/ui-kit/d-decorated-html";+import { i18n } from "discourse-i18n";import AdminAssistantWorkflowPreview from "discourse/plugins/discourse-workflows/discourse/components/admin-assistant/workflow-preview";import { decorateWorkflowPreview } from "discourse/plugins/discourse-workflows/discourse/initializers/assistant-workflow-preview";import {@@ -20,8 +21,8 @@ function escapeHtml(value) {.replaceAll(">", ">");}-function cookedPreview(configuration, kind = PREVIEW_KIND) {- return `<pre><code class="lang-admin-assistant-workflow">${escapeHtml(JSON.stringify({ kind, configuration }))}</code></pre>`;+function cookedPreview(configuration, kind = PREVIEW_KIND, extra = {}) {+ return `<pre><code class="lang-admin-assistant-workflow">${escapeHtml(JSON.stringify({ kind, configuration, ...extra }))}</code></pre>`;}function addNode(clientId, name, type = "action:log") {@@ -214,6 +215,46 @@ module(</template>);assert.dom(".assistant-workflow-preview__warning").doesNotExist();+ assert.dom(".assistant-workflow-preview__notice").doesNotExist();+ assert+ .dom(".assistant-workflow-preview__details-label")+ .hasText(+ i18n(+ "discourse_workflows.admin_assistant.preview.exact_configuration"+ )+ );+ });++ test("labels shortened previews and explains where to review settings", async function (assert) {+ this.message = {+ id: 113,+ blocks: [{ type: "confirmation", title: APPROVAL_TITLE }],+ cooked: cookedPreview(+ {+ name: "Large workflow",+ operations: [addNode("start", "Start", "trigger:manual")],+ },+ PREVIEW_KIND,+ { shortened: true }+ ),+ };+ await render(+ <template>+ <AdminAssistantWorkflowPreview @message={{this.message}} />+ </template>+ );++ assert+ .dom(".assistant-workflow-preview__notice")+ .hasText(i18n("discourse_workflows.admin_assistant.preview.shortened"));+ assert+ .dom(".assistant-workflow-preview__details-label")+ .hasText(+ i18n(+ "discourse_workflows.admin_assistant.preview.shortened_configuration"+ )+ );+ assert.dom(".assistant-workflow-preview__node").exists({ count: 1 });});test("lays out cycles deterministically and labels them", async function (assert) {