Skip to content

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.rb
index 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 DiscourseDataExplorer
FEATURE = :data_explorer_query_authoring
def 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) == :available
end
def self.public_status
@@ -36,18 +33,17 @@ module DiscourseDataExplorer
private_class_method :public_status
def 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 :access
end
end
end
diff --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.rb
index 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 DiscourseDataExplorer
def 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 DiscourseDataExplorer
end
def self.available_for_context?(context)
- context&.user&.admin? &&
- Capability.status(user: context.user, phase: :visibility) == :available
+ Capability.visible?(context&.user)
end
def summary
@@ -135,7 +134,7 @@ module DiscourseDataExplorer
end
def draft
- QueryDraft.read_with_compatibility(parameters[:draft_token])
+ @draft ||= QueryDraft.read_with_compatibility(parameters[:draft_token])
end
def execution_phase
diff --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.rb
index 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 DiscourseDataExplorer
def 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 DiscourseDataExplorer
end
def self.available_for_context?(context)
- context&.user&.admin? &&
- Capability.status(user: context.user, phase: :visibility) == :available
+ Capability.visible?(context&.user)
end
def summary
diff --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.rb
index 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 do
let(: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:)
end
before do
@@ -36,10 +31,10 @@ RSpec.describe DiscourseDataExplorer::AdminAssistant::CreateQuery do
AiToolAction.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:,
)
end
let(:query) do
@@ -203,12 +198,11 @@ RSpec.describe DiscourseDataExplorer::AdminAssistant::GenerateQuery do
it "denies policy before query generation and rejects oversized requests" do
DiscourseDataExplorer::AdminAssistant::Capability.stubs(:status).returns(:restricted)
- allow(DiscourseDataExplorer::QueryGeneration).to receive(:call)
+ DiscourseDataExplorer::QueryGeneration.expects(:call).never
expect(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)
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/capability.rb b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/capability.rb
index 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 DiscourseWorkflows
FEATURE = :workflow_authoring
def 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) == :available
end
def self.public_status
@@ -31,18 +32,17 @@ module DiscourseWorkflows
private_class_method :public_status
def 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 :access
def self.authoring_agent
configured_id = SiteSetting.discourse_workflows_workflow_authoring_agent.to_i
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.rb
index 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 DiscourseWorkflows
def 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 DiscourseWorkflows
end
def 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)
end
def summary
@@ -56,22 +55,18 @@ module DiscourseWorkflows
def details
return 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_i
fence = "`" * [3, longest_run + 1].max
"#{fence}admin-assistant-workflow\n#{preview}\n#{fence}\n"
end
def validation_error
- if !authorized?
+ if !Capability.authorized?(context.user)
return error_response(I18n.t("discourse_workflows.admin_assistant.not_allowed"))
end
if Capability.status(user: context.user, phase: execution_phase) != :available
@@ -81,7 +76,7 @@ module DiscourseWorkflows
if 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 DiscourseWorkflows
error_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)
end
def create_unpublished_workflow!
@@ -161,7 +156,7 @@ module DiscourseWorkflows
workflow = result[:workflow]
patch =
DiscourseWorkflows::Workflow::Action::ApplyPatch.call(
- workflow: workflow,
+ workflow:,
operations: draft[:operations],
persist: true,
user: context.user,
@@ -174,7 +169,7 @@ module DiscourseWorkflows
end
def draft
- WorkflowDraft.read_with_compatibility(parameters[:draft_token])
+ @draft ||= WorkflowDraft.read_with_compatibility(parameters[:draft_token])
end
def execution_phase
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/generate_workflow.rb b/plugins/discourse-workflows/lib/discourse_workflows/admin_assistant/generate_workflow.rb
index 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 DiscourseWorkflows
def 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 DiscourseWorkflows
end
def 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)
end
def summary
@@ -47,7 +46,7 @@ module DiscourseWorkflows
end
def invoke
- if !authorized?
+ if !Capability.authorized?(context.user)
return error_response(I18n.t("discourse_workflows.admin_assistant.not_allowed"))
end
if Capability.status(user: context.user, phase: :generation) != :available
@@ -89,17 +88,17 @@ module DiscourseWorkflows
return generation_error unless validation[:valid]
draft_attributes = {
- name: name,
- operations: operations,
+ name:,
+ operations:,
script_validations: proposal[:script_validations],
}.compact
draft_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:,
}.compact
rescue ActiveRecord::ActiveRecordError, Discourse::InvalidParameters
generation_error
@@ -109,10 +108,6 @@ module DiscourseWorkflows
private
- def authorized?
- context.user&.admin? && context.user.guardian.can_manage_workflows?
- end
-
def clarification_response(session)
response = session.latest_response.symbolize_keys
questions = Array.wrap(response[:questions])
@@ -122,7 +117,7 @@ module DiscourseWorkflows
if 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: }
end
def create_session(description)
@@ -154,7 +149,7 @@ module DiscourseWorkflows
def 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.rb
index 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 DiscourseWorkflows
URL_CREDENTIALS_TEXT = %r{\b[a-z][a-z0-9+.-]*://[^/\s:@]+:[^/\s@]+@}i
def 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 value
diff --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.rb
index 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 do
end
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:)
end
before do
@@ -96,10 +91,10 @@ RSpec.describe DiscourseWorkflows::AdminAssistant::CreateWorkflow do
AiToolAction.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 do
tool.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] = token
freeze_time 2.days.from_now
expect(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 do
context = 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:,
)
end
let(: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 do
expect(result).to eq(
status: "needs_clarification",
message: "I need the topic scope.",
- questions: questions,
+ questions:,
)
expect(DiscourseWorkflows::AiAuthoringSession.count).to eq(0)
end