A platform for community discussion. Free, open, simple.
DEV: Simplify Admin Assistant query and workflow tools
Régis Hanol6c7c1a2e077c
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/capability.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/capability.rbindex b6036104458..ed1973b59d2 100644--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/capability.rb+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/capability.rb@@ -6,14 +6,11 @@ module DiscourseDataExplorerFEATURE = :data_explorer_query_authoringdef self.status(user:, phase:)- access =- DiscourseAi::Agents::AdminAssistantAccess.check(- feature: FEATURE,- user: user,- phase: phase,- )- return :restricted if !access.allowed- public_status+ access(user:, phase:).allowed ? public_status : :restricted+ end++ def self.visible?(user)+ user&.admin? && status(user:, phase: :visibility) == :availableenddef self.public_status@@ -36,18 +33,17 @@ module DiscourseDataExplorerprivate_class_method :public_statusdef self.message(user:, phase:)- access =- DiscourseAi::Agents::AdminAssistantAccess.check(- feature: FEATURE,- user: user,- phase: phase,- )+ access = access(user:, phase:)return access.message if !access.allowed && access.message.present?- I18n.t(- "discourse_data_explorer.admin_assistant.availability.#{status(user: user, phase: phase)}",- )+ key = access.allowed ? public_status : :restricted+ I18n.t("discourse_data_explorer.admin_assistant.availability.#{key}")+ end++ def self.access(user:, phase:)+ DiscourseAi::Agents::AdminAssistantAccess.check(feature: FEATURE, user:, phase:)end+ private_class_method :accessendendenddiff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/create_query.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/create_query.rbindex bf7a408ba58..a97f7f117be 100644--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/create_query.rb+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/create_query.rb@@ -9,7 +9,7 @@ module DiscourseDataExplorerdef self.signature{- name: name,+ name:,description:"Request human approval to save an exact generated Data Explorer query draft. Never invent, alter, or reuse the draft token.",parameters: [@@ -40,8 +40,7 @@ module DiscourseDataExplorerenddef self.available_for_context?(context)- context&.user&.admin? &&- Capability.status(user: context.user, phase: :visibility) == :available+ Capability.visible?(context&.user)enddef summary@@ -135,7 +134,7 @@ module DiscourseDataExplorerenddef draft- QueryDraft.read_with_compatibility(parameters[:draft_token])+ @draft ||= QueryDraft.read_with_compatibility(parameters[:draft_token])enddef execution_phasediff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/generate_query.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/generate_query.rbindex 0f1daf819d3..d1c7b58fbe0 100644--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/generate_query.rb+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/admin_assistant/generate_query.rb@@ -11,7 +11,7 @@ module DiscourseDataExplorerdef self.signature{- name: name,+ name:,description:"Generate and test a Data Explorer query draft without saving it. Use create_data_explorer_query with the returned draft token to request approval to save the exact draft. For revisions, describe the complete revised request.",parameters: [@@ -36,8 +36,7 @@ module DiscourseDataExplorerenddef self.available_for_context?(context)- context&.user&.admin? &&- Capability.status(user: context.user, phase: :visibility) == :available+ Capability.visible?(context&.user)enddef summarydiff --git a/plugins/discourse-data-explorer/spec/lib/discourse_data_explorer/admin_assistant/query_tools_spec.rb b/plugins/discourse-data-explorer/spec/lib/discourse_data_explorer/admin_assistant/query_tools_spec.rbindex 04aa9d54f18..95956c6dc1a 100644--- a/plugins/discourse-data-explorer/spec/lib/discourse_data_explorer/admin_assistant/query_tools_spec.rb+++ b/plugins/discourse-data-explorer/spec/lib/discourse_data_explorer/admin_assistant/query_tools_spec.rb@@ -16,12 +16,7 @@ RSpec.describe DiscourseDataExplorer::AdminAssistant::CreateQuery dolet(:token) { DiscourseDataExplorer::AdminAssistant::QueryDraft.sign(query, user: admin) }let(:context) { DiscourseAi::Agents::BotContext.new(user: admin, messages: []) }let(:tool) do- described_class.new(- { draft_token: token },- bot_user: Discourse.system_user,- llm: nil,- context: context,- )+ described_class.new({ draft_token: token }, bot_user: Discourse.system_user, llm: nil, context:)endbefore do@@ -36,10 +31,10 @@ RSpec.describe DiscourseDataExplorer::AdminAssistant::CreateQuery doAiToolAction.create!(tool_name: described_class.name,tool_parameters: {- draft_token: draft_token,- requester_id: requester_id,+ draft_token:,+ requester_id:,},- ai_agent: ai_agent,+ ai_agent:,bot_user_id: Discourse.system_user.id,)ReviewableAiToolAction@@ -174,7 +169,7 @@ RSpec.describe DiscourseDataExplorer::AdminAssistant::GenerateQuery do{ description: "Recent users" },bot_user: Discourse.system_user,llm: nil,- context: context,+ context:,)endlet(:query) do@@ -203,12 +198,11 @@ RSpec.describe DiscourseDataExplorer::AdminAssistant::GenerateQuery doit "denies policy before query generation and rejects oversized requests" doDiscourseDataExplorer::AdminAssistant::Capability.stubs(:status).returns(:restricted)- allow(DiscourseDataExplorer::QueryGeneration).to receive(:call)+ DiscourseDataExplorer::QueryGeneration.expects(:call).neverexpect(tool.invoke[:status]).to eq("error")DiscourseDataExplorer::AdminAssistant::Capability.stubs(:status).returns(:available)- tool.parameters[:description] = "x" * 4001+ tool.parameters[:description] = "x" * (described_class::MAX_DESCRIPTION_LENGTH + 1)expect(tool.invoke[:status]).to eq("error")- expect(DiscourseDataExplorer::QueryGeneration).not_to have_received(:call)endenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/capability.rb b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/capability.rbindex de5d6ceba2d..d835e64751f 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/capability.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/capability.rb@@ -6,14 +6,15 @@ module DiscourseWorkflowsFEATURE = :workflow_authoringdef self.status(user:, phase:)- access =- DiscourseAi::Agents::AdminAssistantAccess.check(- feature: FEATURE,- user: user,- phase: phase,- )- return :restricted if !access.allowed- public_status+ access(user:, phase:).allowed ? public_status : :restricted+ end++ def self.authorized?(user)+ user&.admin? && user.guardian.can_manage_workflows?+ end++ def self.visible?(user)+ authorized?(user) && status(user:, phase: :visibility) == :availableenddef self.public_status@@ -31,18 +32,17 @@ module DiscourseWorkflowsprivate_class_method :public_statusdef self.message(user:, phase:)- access =- DiscourseAi::Agents::AdminAssistantAccess.check(- feature: FEATURE,- user: user,- phase: phase,- )+ access = access(user:, phase:)return access.message if !access.allowed && access.message.present?- I18n.t(- "discourse_workflows.admin_assistant.availability.#{status(user: user, phase: phase)}",- )+ key = access.allowed ? public_status : :restricted+ I18n.t("discourse_workflows.admin_assistant.availability.#{key}")+ end++ def self.access(user:, phase:)+ DiscourseAi::Agents::AdminAssistantAccess.check(feature: FEATURE, user:, phase:)end+ private_class_method :accessdef self.authoring_agentconfigured_id = SiteSetting.discourse_workflows_workflow_authoring_agent.to_idiff --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 763cbf5a9d6..39b09dbe471 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@@ -11,7 +11,7 @@ module DiscourseWorkflowsdef self.signature{- name: name,+ name:,description:"Request human approval to save an exact validated workflow draft as unpublished. Never invent, alter, or reuse the draft token.",parameters: [@@ -42,8 +42,7 @@ module DiscourseWorkflowsenddef self.available_for_context?(context)- context&.user&.admin? && context.user.guardian.can_manage_workflows? &&- Capability.status(user: context.user, phase: :visibility) == :available+ Capability.visible?(context&.user)enddef summary@@ -56,22 +55,18 @@ module DiscourseWorkflowsdef detailsreturn I18n.t("discourse_workflows.admin_assistant.invalid_draft") unless draft- if WorkflowDraft.contains_secret?(draft.slice(:name, :operations, :script_validations))+ if WorkflowDraft.contains_secret?(configuration)return I18n.t("discourse_workflows.admin_assistant.secret_draft")end- preview =- JSON.pretty_generate(- kind: PREVIEW_KIND,- configuration: draft.slice(:name, :operations, :script_validations),- )+ preview = JSON.pretty_generate(kind: PREVIEW_KIND, configuration:)longest_run = preview.scan(/`+/).map(&:length).max.to_ifence = "`" * [3, longest_run + 1].max"#{fence}admin-assistant-workflow\n#{preview}\n#{fence}\n"enddef validation_error- if !authorized?+ if !Capability.authorized?(context.user)return error_response(I18n.t("discourse_workflows.admin_assistant.not_allowed"))endif Capability.status(user: context.user, phase: execution_phase) != :available@@ -81,7 +76,7 @@ module DiscourseWorkflowsif draft[:name].blank? || !draft[:operations].is_a?(Array) || draft[:operations].blank?return error_response(I18n.t("discourse_workflows.admin_assistant.incomplete_draft"))end- if WorkflowDraft.contains_secret?(draft.slice(:name, :operations, :script_validations))+ if WorkflowDraft.contains_secret?(configuration)return error_response(I18n.t("discourse_workflows.admin_assistant.secret_draft"))end@@ -139,8 +134,8 @@ module DiscourseWorkflowserror_response(I18n.t("discourse_workflows.admin_assistant.approval_required"))end- def authorized?- context.user&.admin? && context.user.guardian.can_manage_workflows?+ def configuration+ draft.slice(:name, :operations, :script_validations)enddef create_unpublished_workflow!@@ -161,7 +156,7 @@ module DiscourseWorkflowsworkflow = result[:workflow]patch =DiscourseWorkflows::Workflow::Action::ApplyPatch.call(- workflow: workflow,+ workflow:,operations: draft[:operations],persist: true,user: context.user,@@ -174,7 +169,7 @@ module DiscourseWorkflowsenddef draft- WorkflowDraft.read_with_compatibility(parameters[:draft_token])+ @draft ||= WorkflowDraft.read_with_compatibility(parameters[:draft_token])enddef execution_phasediff --git a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/generate_workflow.rb b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/generate_workflow.rbindex d8f1781623c..1cd6ed0f915 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/generate_workflow.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/generate_workflow.rb@@ -9,7 +9,7 @@ module DiscourseWorkflowsdef self.signature{- name: name,+ name:,description:"Generate and validate an unpublished workflow draft without saving it. Do not include passwords, tokens, API keys, credentials, or other secrets. If clarification is needed, ask the administrator the returned questions and retry with their answers included. Use create_workflow with the returned draft token to request approval to save the exact draft.",parameters: [@@ -34,8 +34,7 @@ module DiscourseWorkflowsenddef self.available_for_context?(context)- context&.user&.admin? && context.user.guardian.can_manage_workflows? &&- Capability.status(user: context.user, phase: :visibility) == :available+ Capability.visible?(context&.user)enddef summary@@ -47,7 +46,7 @@ module DiscourseWorkflowsenddef invoke- if !authorized?+ if !Capability.authorized?(context.user)return error_response(I18n.t("discourse_workflows.admin_assistant.not_allowed"))endif Capability.status(user: context.user, phase: :generation) != :available@@ -89,17 +88,17 @@ module DiscourseWorkflowsreturn generation_error unless validation[:valid]draft_attributes = {- name: name,- operations: operations,+ name:,+ operations:,script_validations: proposal[:script_validations],}.compactdraft_token = WorkflowDraft.sign(draft_attributes, user: context.user){status: "success",- name: name,+ name:,summary: proposal[:summary].to_s,script_validations: proposal[:script_validations],- draft_token: draft_token,+ draft_token:,}.compactrescue ActiveRecord::ActiveRecordError, Discourse::InvalidParametersgeneration_error@@ -109,10 +108,6 @@ module DiscourseWorkflowsprivate- def authorized?- context.user&.admin? && context.user.guardian.can_manage_workflows?- end-def clarification_response(session)response = session.latest_response.symbolize_keysquestions = Array.wrap(response[:questions])@@ -122,7 +117,7 @@ module DiscourseWorkflowsif message.blank? || WorkflowDraft.contains_secret_text?(message)message = I18n.t("discourse_workflows.admin_assistant.clarification_required")end- { status: "needs_clarification", message: message, questions: questions }+ { status: "needs_clarification", message:, questions: }enddef create_session(description)@@ -154,7 +149,7 @@ module DiscourseWorkflowsdef validate_draft(name, operations)DiscourseWorkflows::Ai::Tools::WorkflowValidatePatch.new(- { workflow_name: name, operations: operations },+ { workflow_name: name, operations: },bot_user: Discourse.system_user,llm: nil,context: DiscourseAi::Agents::BotContext.new(messages: [], user: context.user),diff --git a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/workflow_draft.rb b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/workflow_draft.rbindex 6c09cc4d05d..f433ac04118 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/workflow_draft.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/workflow_draft.rb@@ -35,7 +35,6 @@ module DiscourseWorkflowsURL_CREDENTIALS_TEXT = %r{\b[a-z][a-z0-9+.-]*://[^/\s:@]+:[^/\s@]+@}idef self.contains_secret?(value, key: nil)- return true if key.to_s == "credentials" && value.present?return true if SENSITIVE_KEYS.include?(key.to_s.underscore) && value.present?case valuediff --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 a52bbc05230..2a9c34aafe3 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@@ -32,7 +32,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow do{ "op" => "add_connection", "from" => "manual", "to" => "log" },]end- let(:draft_attributes) { { name: "Manual log", operations: operations } }+ let(:draft_attributes) { { name: "Manual log", operations: } }let(:agent_operations) do[{@@ -76,12 +76,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow doendlet(:context) { DiscourseAi::Agents::BotContext.new(user: admin, messages: []) }let(:tool) do- described_class.new(- { draft_token: token },- bot_user: Discourse.system_user,- llm: nil,- context: context,- )+ described_class.new({ draft_token: token }, bot_user: Discourse.system_user, llm: nil, context:)endbefore do@@ -96,10 +91,10 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow doAiToolAction.create!(tool_name: described_class.name,tool_parameters: {- draft_token: draft_token,+ draft_token:,requester_id: admin.id,},- ai_agent: ai_agent,+ ai_agent:,bot_user_id: Discourse.system_user.id,)item =@@ -159,7 +154,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow do{ draft_token: unsafe_token },bot_user: Discourse.system_user,llm: nil,- context: context,+ context:,)cooked = Nokogiri::HTML5.fragment(PrettyText.cook(unsafe_tool.details))@@ -203,7 +198,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow do{ draft_token: agent_token },bot_user: Discourse.system_user,llm: nil,- context: context,+ context:,)expect { initial_tool.validation_error }.not_to change { AiAgent.count }@@ -250,9 +245,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow dotool.parameters[:draft_token] = token + "tampered"expect(tool.validation_error[:status]).to eq("error")- expired = token- tool.parameters[:draft_token] = expired- tool.instance_variable_set(:@draft, nil)+ tool.parameters[:draft_token] = tokenfreeze_time 2.days.from_nowexpect(tool.validation_error[:status]).to eq("error")@@ -267,7 +260,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow do{ draft_token: malformed },bot_user: Discourse.system_user,llm: nil,- context: context.tap { |value| value.user = admin },+ context:,)expect(broken_tool.validation_error[:error]).to eq(I18n.t("discourse_workflows.admin_assistant.invalid_graph"),@@ -299,7 +292,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow do{ draft_token: unsafe },bot_user: Discourse.system_user,llm: nil,- context: context,+ context:,)expect(unsafe_tool.validation_error[:error]).to eq(@@ -365,7 +358,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow docontext = DiscourseAi::Agents::BotContext.new(user: admin, messages: [])assistant = DiscourseAi::Agents::DiscourseAdminAssistant.new- expect(assistant.runtime_tools(context: context)).not_to include(+ expect(assistant.runtime_tools(context:)).not_to include(DiscourseWorkflows::AdminAssistant::GenerateWorkflow,described_class,)@@ -404,7 +397,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::GenerateWorkflow do{ description: "Create a manual workflow that logs Hello" },bot_user: Discourse.system_user,llm: nil,- context: context,+ context:,)endlet(:proposal) do@@ -495,7 +488,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::GenerateWorkflow do{ draft_token: result[:draft_token] },bot_user: Discourse.system_user,llm: nil,- context: context,+ context:,)code = Nokogiri::HTML5.fragment(PrettyText.cook(creator.details)).at_css("pre code")expect(JSON.parse(code.text).dig("configuration", "script_validations")).to eq(validations)@@ -569,7 +562,7 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::GenerateWorkflow doexpect(result).to eq(status: "needs_clarification",message: "I need the topic scope.",- questions: questions,+ questions:,)expect(DiscourseWorkflows::AiAuthoringSession.count).to eq(0)end