Skip to content

A platform for community discussion. Free, open, simple.

FIX: Fail the AI agent node when an upload never reaches the model

Régis Hanol90d4f508be98
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 98c2f8d93e7..5882417f6f6 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -300,7 +300,7 @@ en:
runner_username: "Run as"
runner_username_tooltip: "User whose permissions are used for upload visibility and AI agent tools. Use system for workflow-owned automation."
upload_ids: "Upload IDs"
- upload_ids_tooltip: "Optional upload IDs to attach to the prompt. Image uploads are included when the agent has vision enabled; document uploads are included when the selected LLM supports their file type."
+ upload_ids_tooltip: "Optional upload IDs to attach to the prompt. Every upload must reach the model or the node fails: images need vision on the agent and the LLM, documents need their file type enabled on the LLM."
upload_ids_placeholder: "={{ $json.post.upload_ids }}"
select_agent: "Select an agent..."
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 2c4c3292e08..5adfe69cde6 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -311,6 +311,14 @@ en:
locked_default_llm_missing: "Agent '%{agent}' is locked to its default LLM, but no valid default LLM is configured"
llm_not_found: "LLM model with id %{llm_model_id} not found"
no_llm_configured: "No LLM is configured for agent '%{agent}'. Select an LLM model or configure an agent/site default LLM."
+ uploads_not_sent: "Agent '%{agent}' did not receive every upload"
+ upload_not_found: "Upload %{upload_id} was not found"
+ upload_not_visible: "%{username} cannot see upload %{upload_id}"
+ agent_cannot_see_images: "Upload %{upload_id} is an image and agent '%{agent}' has vision disabled"
+ llm_cannot_see_images: "Upload %{upload_id} is an image and LLM '%{llm}' has vision disabled"
+ llm_cannot_read_documents: "Upload %{upload_id} is a document and LLM '%{llm}' accepts no attachments"
+ unsupported_image_format: "Upload %{upload_id} is not a supported image format (%{formats})"
+ upload_skipped: "Upload %{upload_id} (%{filename}) was not sent: %{message}"
tools:
custom_name: "%{name} (custom)"
diff --git a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
index 90390446ea9..fdc2ae0549c 100644
--- a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
+++ b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
@@ -209,13 +209,16 @@ if defined?(DiscourseWorkflows)
mode = exec_ctx.get_node_parameter("mode", 0, default: RUN_ONCE_FOR_EACH_ITEM)
validate_mode!(mode)
- return [[run_once_for_all_items(exec_ctx)]] if mode == RUN_ONCE_FOR_ALL_ITEMS
+ agent = resolve_agent(exec_ctx)
+
+ return [[run_once_for_all_items(exec_ctx, agent)]] if mode == RUN_ONCE_FOR_ALL_ITEMS
items =
exec_ctx.input_items.map.with_index do |item, item_index|
result =
run_agent(
- agent_config(exec_ctx, item_index),
+ agent,
+ item_config(exec_ctx, item_index),
exec_ctx.log,
runner(exec_ctx, item_index),
)
@@ -228,8 +231,8 @@ if defined?(DiscourseWorkflows)
private
- def run_once_for_all_items(exec_ctx)
- result = run_agent(agent_config(exec_ctx, 0), exec_ctx.log, runner(exec_ctx, 0))
+ def run_once_for_all_items(exec_ctx, agent)
+ result = run_agent(agent, item_config(exec_ctx, 0), exec_ctx.log, runner(exec_ctx, 0))
wrap(
{ "result" => result },
@@ -237,15 +240,30 @@ if defined?(DiscourseWorkflows)
)
end
- def agent_config(exec_ctx, item_index)
+ def item_config(exec_ctx, item_index)
{
- "agent_id" => exec_ctx.get_node_parameter("agent_id", item_index),
- "llm_model_id" => exec_ctx.get_node_parameter("llm_model_id", item_index),
"prompt" => exec_ctx.get_node_parameter("prompt", item_index),
"upload_ids" => exec_ctx.get_node_parameter("upload_ids", item_index),
}
end
+ def resolve_agent(exec_ctx)
+ agent_id = exec_ctx.get_node_parameter("agent_id", 0)
+ record = ::AiAgent.find_by(id: agent_id)
+ raise_node_error!("AI Agent with id #{agent_id} not found") if record.nil?
+ raise_node_error!("AI Agent '#{record.name}' is disabled") if !record.enabled
+
+ llm_model = resolve_llm_model(record, exec_ctx.get_node_parameter("llm_model_id", 0))
+ bot =
+ DiscourseAi::Agents::Bot.as(
+ Discourse.system_user,
+ agent: record.class_instance.new,
+ model: llm_model,
+ )
+
+ { record: record, llm_model: llm_model, bot: bot }
+ end
+
def runner(exec_ctx, item_index)
exec_ctx.actor_from_parameter("runner_username", item_index, default: "system")
end
@@ -253,9 +271,7 @@ if defined?(DiscourseWorkflows)
def validate_mode!(mode)
return if [RUN_ONCE_FOR_ALL_ITEMS, RUN_ONCE_FOR_EACH_ITEM].include?(mode)
- raise_node_error!(
- I18n.t("discourse_ai.discourse_workflows.ai_agent.errors.invalid_mode", mode: mode),
- )
+ raise_node_error!(error_text("invalid_mode", mode: mode))
end
def resolve_llm_model(agent_record, requested_llm_model_id)
@@ -264,24 +280,14 @@ if defined?(DiscourseWorkflows)
::LlmModel.find_by(id: agent_record.default_llm_id) if agent_record.default_llm_id
return llm_model if llm_model.present?
- raise_node_error!(
- I18n.t(
- "discourse_ai.discourse_workflows.ai_agent.errors.locked_default_llm_missing",
- agent: agent_record.name,
- ),
- )
+ raise_node_error!(error_text("locked_default_llm_missing", agent: agent_record.name))
end
if requested_llm_model_id.present?
llm_model = ::LlmModel.find_by(id: requested_llm_model_id)
return llm_model if llm_model.present?
- raise_node_error!(
- I18n.t(
- "discourse_ai.discourse_workflows.ai_agent.errors.llm_not_found",
- llm_model_id: requested_llm_model_id,
- ),
- )
+ raise_node_error!(error_text("llm_not_found", llm_model_id: requested_llm_model_id))
end
[agent_record.default_llm_id, SiteSetting.ai_default_llm_model].each do |llm_model_id|
@@ -289,33 +295,62 @@ if defined?(DiscourseWorkflows)
return llm_model if llm_model.present?
end
- raise_node_error!(
- I18n.t(
- "discourse_ai.discourse_workflows.ai_agent.errors.no_llm_configured",
- agent: agent_record.name,
- ),
- )
+ raise_node_error!(error_text("no_llm_configured", agent: agent_record.name))
end
- def prompt_content(prompt, upload_ids, agent_record, llm_model, guardian, log)
- upload_ids = filtered_upload_ids(upload_ids, agent_record, llm_model, guardian)
+ def prompt_content(prompt, upload_ids, agent_record, llm_model, runner, log)
+ upload_ids = normalize_upload_ids(upload_ids)
return prompt if upload_ids.blank?
+ uploads_by_id =
+ ::DiscourseAi::Completions::PromptMessagesBuilder.uploads_for_prompt(
+ upload_ids,
+ ).index_by(&:id)
+ guardian = runner.guardian
+ reasons =
+ upload_ids.filter_map do |upload_id|
+ key = drop_reason_key(uploads_by_id[upload_id], agent_record, llm_model, guardian)
+ next if key.nil?
+
+ error_text(
+ key,
+ upload_id: upload_id,
+ agent: agent_record.name,
+ llm: llm_model.display_name,
+ username: runner.username,
+ formats: ::DiscourseAi::Completions::UploadEncoder::SUPPORTED_IMAGE_FORMATS,
+ )
+ end
+ raise_uploads_not_sent!(agent_record, reasons) if reasons.any?
+
log.info("Attachments: #{upload_ids.size} upload(s)")
[prompt, *upload_ids.map { |upload_id| { upload_id: upload_id } }]
end
- def filtered_upload_ids(upload_ids, agent_record, llm_model, guardian)
- upload_ids = normalize_upload_ids(upload_ids)
- return [] if upload_ids.blank?
-
- ::DiscourseAi::Completions::PromptMessagesBuilder.filtered_upload_ids_for_prompt(
- upload_ids,
- include_image_uploads: agent_record.vision_enabled,
- include_document_uploads: llm_model.allowed_attachment_types.present?,
- allowed_attachment_types: llm_model.allowed_attachment_types,
- guardian: guardian,
- ) || []
+ def drop_reason_key(upload, agent_record, llm_model, guardian)
+ return "upload_not_found" if upload.nil?
+ return "upload_not_visible" if !guardian.can_see_upload?(upload)
+
+ encoder = ::DiscourseAi::Completions::UploadEncoder
+ if !encoder.image?(upload)
+ return "llm_cannot_read_documents" if llm_model.allowed_attachment_types.blank?
+
+ return
+ end
+ return "agent_cannot_see_images" if !agent_record.vision_enabled
+ return "llm_cannot_see_images" if !llm_model.agent_image_capable?
+ "unsupported_image_format" if !encoder.supported_image_upload?(upload)
+ end
+
+ def raise_uploads_not_sent!(agent_record, reasons)
+ raise_node_error!(
+ error_text("uploads_not_sent", agent: agent_record.name),
+ description: reasons.join(". "),
+ )
+ end
+
+ def error_text(key, **args)
+ I18n.t("discourse_ai.discourse_workflows.ai_agent.errors.#{key}", **args)
end
def normalize_upload_ids(upload_ids)
@@ -329,10 +364,12 @@ if defined?(DiscourseWorkflows)
upload_ids.flatten
else
Array.wrap(upload_ids)
- end.filter_map do |upload_id|
- id = Integer(upload_id, exception: false)
- id if id&.positive?
end
+ .filter_map do |upload_id|
+ id = Integer(upload_id, exception: false)
+ id if id&.positive?
+ end
+ .uniq
end
def parse_upload_ids_json(upload_ids)
@@ -341,50 +378,39 @@ if defined?(DiscourseWorkflows)
nil
end
- def run_agent(config, log, runner)
- agent_id = config["agent_id"]
+ def run_agent(agent, config, log, runner)
+ agent_record = agent[:record]
+ llm_model = agent[:llm_model]
prompt = config["prompt"].to_s
- agent_record = ::AiAgent.find_by(id: agent_id)
- raise_node_error!("AI Agent with id #{agent_id} not found") if agent_record.nil?
-
- if !agent_record.enabled
- raise_node_error!("AI Agent '#{agent_record.name}' is disabled")
- end
-
- agent_instance = agent_record.class_instance.new
- llm_model = resolve_llm_model(agent_record, config["llm_model_id"])
-
log.info("Agent: #{agent_record.name}")
log.info("Runner: #{runner.username}")
log.info("LLM: #{llm_model.display_name} (#{llm_model.id})")
- log.info("Prompt: #{prompt.to_s[0..200]}")
-
- bot =
- DiscourseAi::Agents::Bot.as(
- Discourse.system_user,
- agent: agent_instance,
- model: llm_model,
- )
+ log.info("Prompt: #{prompt[0..200]}")
content =
- prompt_content(
- prompt,
- config["upload_ids"],
- agent_record,
- llm_model,
- runner.guardian,
- log,
- )
+ prompt_content(prompt, config["upload_ids"], agent_record, llm_model, runner, log)
+ execution_context = DiscourseAi::Completions::ExecutionContext.new
bot_context =
DiscourseAi::Agents::BotContext.new(
user: runner,
guardian: runner.guardian,
messages: [{ type: :user, content: content }],
feature_name: "workflow",
+ execution_context: execution_context,
)
+ result = collect_reply(agent[:bot], bot_context, log)
+
+ skipped =
+ execution_context.upload_skips.map { |skip| error_text("upload_skipped", **skip) }
+ raise_uploads_not_sent!(agent_record, skipped) if skipped.any?
+
+ result
+ end
+
+ def collect_reply(bot, bot_context, log)
result = +""
tool_calls = 0
@@ -393,7 +419,7 @@ if defined?(DiscourseWorkflows)
tool_calls += 1
log.info("Tool call: #{partial}") if partial.is_a?(String)
elsif type == :structured_output
- result = partial.to_s
+ result = partial.to_s.dup
elsif type.blank?
result << partial
end
diff --git a/plugins/discourse-ai/lib/completions/dialects/dialect.rb b/plugins/discourse-ai/lib/completions/dialects/dialect.rb
index e478effd2af..da680840c4a 100644
--- a/plugins/discourse-ai/lib/completions/dialects/dialect.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/dialect.rb
@@ -490,7 +490,15 @@ module DiscourseAi
next if is_image && !allow_images
next if is_document && !allow_documents
- next if upload_filter && !upload_filter.call(encoded)
+
+ if upload_filter && !upload_filter.call(encoded)
+ prompt.record_upload_skip(
+ upload_id: c[:upload_id],
+ filename: encoded[:filename],
+ message: "not accepted by this model",
+ )
+ next
+ end
if !current_string.empty?
result << text_encoder.call(current_string)
diff --git a/plugins/discourse-ai/lib/completions/document_encoder.rb b/plugins/discourse-ai/lib/completions/document_encoder.rb
index 9d7649b9346..090d4801e52 100644
--- a/plugins/discourse-ai/lib/completions/document_encoder.rb
+++ b/plugins/discourse-ai/lib/completions/document_encoder.rb
@@ -175,7 +175,7 @@ module DiscourseAi
end
def log_document_conversion_failure(skips, upload, extension, message)
- record_skip(skips, upload, message)
+ record_upload_skip(skips, upload, message)
Rails.logger.warn(
"Discourse AI: Failed to convert .#{extension} upload to text " \
@@ -184,7 +184,7 @@ module DiscourseAi
end
def log_document_upload_skip(skips, upload, extension, message)
- record_skip(skips, upload, message)
+ record_upload_skip(skips, upload, message)
Rails.logger.warn(
"Discourse AI: Skipping .#{extension} upload " \
diff --git a/plugins/discourse-ai/lib/completions/prompt.rb b/plugins/discourse-ai/lib/completions/prompt.rb
index 7c58770b440..aded49949fd 100644
--- a/plugins/discourse-ai/lib/completions/prompt.rb
+++ b/plugins/discourse-ai/lib/completions/prompt.rb
@@ -3,6 +3,8 @@
module DiscourseAi
module Completions
class Prompt
+ include UploadEncoding
+
INVALID_TURN = Class.new(StandardError)
attr_reader :messages, :tools, :system_message_text
@@ -174,6 +176,10 @@ module DiscourseAi
native_tools.include?(id)
end
+ def record_upload_skip(upload_id:, filename:, message:)
+ record_skip(upload_skips, upload_id: upload_id, filename: filename, message: message)
+ end
+
def encoded_uploads(
message,
allow_images: true,
diff --git a/plugins/discourse-ai/lib/completions/upload_encoder.rb b/plugins/discourse-ai/lib/completions/upload_encoder.rb
index 6df791dd253..1ad8e347a5f 100644
--- a/plugins/discourse-ai/lib/completions/upload_encoder.rb
+++ b/plugins/discourse-ai/lib/completions/upload_encoder.rb
@@ -7,6 +7,7 @@ module DiscourseAi
JPEG_EXTENSIONS = %w[jpg jpeg].freeze
SUPPORTED_IMAGE_EXTENSIONS = (JPEG_EXTENSIONS + %w[png gif webp avif]).freeze
+ SUPPORTED_IMAGE_FORMATS = SUPPORTED_IMAGE_EXTENSIONS.join(", ")
def self.supported_image_upload?(upload)
image_extension?(upload.extension&.downcase)
@@ -44,7 +45,7 @@ module DiscourseAi
log_image_upload_skip(
skips,
upload,
- "unsupported image format, supported formats are: #{SUPPORTED_IMAGE_EXTENSIONS.join(", ")}",
+ "unsupported image format, supported formats are: #{SUPPORTED_IMAGE_FORMATS}",
)
next
end
@@ -57,7 +58,14 @@ module DiscourseAi
"application/octet-stream"
attachment_type = DocumentEncoder.attachment_type_for(upload.extension, mime_type)
- next if allowed_attachment_types&.exclude?(attachment_type)
+ if allowed_attachment_types&.exclude?(attachment_type)
+ record_upload_skip(
+ skips,
+ upload,
+ "#{attachment_type} attachments are not accepted by this model",
+ )
+ next
+ end
next DocumentEncoder.encode(upload, mime_type, attachment_type, skips)
end
@@ -89,7 +97,7 @@ module DiscourseAi
end
def log_image_upload_skip(skips, upload, message)
- record_skip(skips, upload, message)
+ record_upload_skip(skips, upload, message)
Rails.logger.warn(
"Discourse AI: Skipping image upload " \
diff --git a/plugins/discourse-ai/lib/completions/upload_encoding.rb b/plugins/discourse-ai/lib/completions/upload_encoding.rb
index d4279fd9e55..394763ac724 100644
--- a/plugins/discourse-ai/lib/completions/upload_encoding.rb
+++ b/plugins/discourse-ai/lib/completions/upload_encoding.rb
@@ -14,10 +14,20 @@ module DiscourseAi
path
end
- def record_skip(skips, upload, message)
- return if skips.any? { |skip| skip[:upload_id] == upload.id }
+ def record_skip(skips, upload_id:, filename:, message:)
+ return if skips.nil?
+ return if skips.any? { |skip| skip[:upload_id] == upload_id }
- skips << { upload_id: upload.id, filename: upload.original_filename, message: message }
+ skips << { upload_id: upload_id, filename: filename, message: message }
+ end
+
+ def record_upload_skip(skips, upload, message)
+ record_skip(
+ skips,
+ upload_id: upload.id,
+ filename: upload.original_filename,
+ message: message,
+ )
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/completions/dialects/converse_spec.rb b/plugins/discourse-ai/spec/lib/completions/dialects/converse_spec.rb
index 42e7a733803..858841b8086 100644
--- a/plugins/discourse-ai/spec/lib/completions/dialects/converse_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/dialects/converse_spec.rb
@@ -18,6 +18,27 @@ RSpec.describe DiscourseAi::Completions::Dialects::Converse do
expect(translated.messages).to eq([{ role: "user", content: [{ text: "admin: Who am I?" }] }])
end
+ it "records a skip for a document it cannot render as text" do
+ model.update!(allowed_attachment_types: ["pdf"])
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ nil,
+ messages: [{ type: :user, content: ["Read this: ", { upload_id: 123 }] }],
+ )
+ prompt.upload_skips = []
+
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:encode).and_return(
+ [{ kind: :document, filename: "report.pdf", mime_type: "application/pdf", base64: "eA==" }],
+ )
+
+ translated = described_class.new(prompt, model).translate
+
+ expect(translated.messages).to eq([{ role: "user", content: [{ text: "Read this: " }] }])
+ expect(prompt.upload_skips).to eq(
+ [{ upload_id: 123, filename: "report.pdf", message: "not accepted by this model" }],
+ )
+ end
+
it "keeps participant names with text that follows an image" do
model.update!(vision_enabled: true)
prompt =
diff --git a/plugins/discourse-ai/spec/lib/completions/upload_encoder_spec.rb b/plugins/discourse-ai/spec/lib/completions/upload_encoder_spec.rb
index ec7ec092e70..9de44a305aa 100644
--- a/plugins/discourse-ai/spec/lib/completions/upload_encoder_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/upload_encoder_spec.rb
@@ -209,6 +209,25 @@ RSpec.describe DiscourseAi::Completions::UploadEncoder do
expect(skips.first[:message]).to include("unsupported image format")
end
+ it "records documents whose attachment type the caller does not accept" do
+ SiteSetting.authorized_extensions += "|pdf"
+ upload = Fabricate(:upload, original_filename: "report.pdf", extension: "pdf")
+ skips = []
+
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: MAX_PIXELS,
+ allowed_kinds: %i[image document],
+ allowed_attachment_types: ["docx"],
+ skips: skips,
+ )
+
+ expect(skips.first).to include(
+ upload_id: upload.id,
+ message: "pdf attachments are not accepted by this model",
+ )
+ end
+
it "reaches the collector a prompt was handed" do
upload = Fabricate(:upload, original_filename: "avatar.jxl", extension: "jxl")
execution_context = DiscourseAi::Completions::ExecutionContext.new
diff --git a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
index 3d68e43295d..08979020af1 100644
--- a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
@@ -1,10 +1,16 @@
# frozen_string_literal: true
RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
- fab!(:llm_model) { Fabricate(:llm_model, display_name: "Workflow LLM") }
+ fab!(:llm_model) { Fabricate(:llm_model, display_name: "Workflow LLM", vision_enabled: true) }
fab!(:site_llm_model) { Fabricate(:llm_model, display_name: "Site LLM") }
fab!(:agent) do
- Fabricate(:ai_agent, name: "Workflow agent", enabled: true, default_llm_id: llm_model.id)
+ Fabricate(
+ :ai_agent,
+ name: "Workflow agent",
+ enabled: true,
+ vision_enabled: true,
+ default_llm_id: llm_model.id,
+ )
end
let(:bot) { instance_double(DiscourseAi::Agents::Bot) }
@@ -99,13 +105,6 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
end
end
- it "declares an explicit runner actor control" do
- runner_schema = described_class.property_schema[:runner_username]
-
- expect(runner_schema).to include(type: :string, default: "system")
- expect(runner_schema[:ui]).to include(control: :actor)
- end
-
it "runs the agent once per input item with item-specific parameters" do
items =
execute_node_output(
@@ -143,26 +142,11 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
expect(prompts).to eq(["Report about Ada"])
expect(items.length).to eq(1)
expect(items.first["json"]["result"]).to eq("Reply to Report about Ada")
+ expect(items.first["pairedItem"]).to eq([{ "item" => 0 }, { "item" => 1 }, { "item" => 2 }])
expect(bot).to have_received(:reply).once
end
- it "pairs its single output with every input item" do
- items =
- execute_node_output(
- configuration: {
- "agent_id" => agent.id,
- "mode" => "runOnceForAllItems",
- "prompt" => "Hello",
- },
- input_items: [{ "json" => { "name" => "Ada" } }, { "json" => { "name" => "Grace" } }],
- ).first
-
- expect(items.first["pairedItem"]).to eq([{ "item" => 0 }, { "item" => 1 }])
- end
-
it "defaults to per-item so existing workflows keep their behaviour" do
- expect(described_class.property_schema[:mode][:default]).to eq("runOnceForEachItem")
-
execute_node_output(
configuration: {
"agent_id" => agent.id,
@@ -174,18 +158,6 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
expect(bot).to have_received(:reply).twice
end
- it "reports the run scope through the mode parameter" do
- expect(described_class.description[:capabilities][:run_scope]).to eq(
- {
- parameter: "mode",
- values: {
- runOnceForEachItem: "per_item",
- runOnceForAllItems: "all_items",
- },
- },
- )
- end
-
it "raises a node error for an unknown mode" do
expect {
execute_node_output(
@@ -236,7 +208,6 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
end
it "passes configured upload IDs to the agent prompt" do
- agent.update!(vision_enabled: true)
upload = Fabricate(:image_upload)
execute_node_output(
@@ -251,70 +222,102 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
expect(prompts).to eq([["Review the screenshot", { upload_id: upload.id }]])
end
- it "filters image upload IDs when the agent does not have vision enabled" do
- agent.update!(vision_enabled: false)
- upload = Fabricate(:image_upload)
+ describe "uploads that cannot reach the model" do
+ def expect_not_sent(upload_id, reason, runner: "system")
+ expect {
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "runner_username" => runner,
+ "prompt" => "Review",
+ "upload_ids" => [upload_id],
+ },
+ )
+ }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /did not receive every upload.*#{Regexp.escape(reason)}/,
+ )
+ end
- execute_node_output(
- configuration: {
- "agent_id" => agent.id,
- "prompt" => "Review the screenshot",
- "upload_ids" => [upload.id],
- },
- )
+ it "raises before calling the agent when the agent cannot see images" do
+ agent.update!(vision_enabled: false)
+ upload = Fabricate(:image_upload)
- expect(prompts).to eq(["Review the screenshot"])
- end
+ expect_not_sent(upload.id, "Upload #{upload.id} is an image and agent 'Workflow agent'")
+ expect(bot).not_to have_received(:reply)
+ end
- it "filters upload IDs the execution user cannot see" do
- agent.update!(vision_enabled: true)
- visible_upload = Fabricate(:image_upload)
- hidden_upload = Fabricate(:image_upload)
- owner = Fabricate(:user)
- recipient = Fabricate(:user)
- private_topic = Fabricate(:private_message_topic, user: owner, recipient: recipient)
- private_post = Fabricate(:post, topic: private_topic, user: owner)
- hidden_upload.update!(access_control_post_id: private_post.id)
+ it "raises when the LLM cannot see images" do
+ llm_model.update!(vision_enabled: false)
+ upload = Fabricate(:image_upload)
- runner = Fabricate(:user)
+ expect_not_sent(upload.id, "Upload #{upload.id} is an image and LLM 'Workflow LLM'")
+ end
- execute_node_output(
- configuration: {
- "agent_id" => agent.id,
- "runner_username" => runner.username,
- "prompt" => "Review the screenshot",
- "upload_ids" => [visible_upload.id, hidden_upload.id],
- },
- )
+ it "raises when the runner cannot see an upload" do
+ owner = Fabricate(:user)
+ private_topic = Fabricate(:private_message_topic, user: owner, recipient: Fabricate(:user))
+ private_post = Fabricate(:post, topic: private_topic, user: owner)
+ upload = Fabricate(:image_upload, access_control_post_id: private_post.id)
+ runner = Fabricate(:user)
+
+ expect_not_sent(
+ upload.id,
+ "#{runner.username} cannot see upload #{upload.id}",
+ runner: runner.username,
+ )
+ end
+
+ it "raises when an upload does not exist" do
+ missing_id = Upload.maximum(:id).to_i + 1
+
+ expect_not_sent(missing_id, "Upload #{missing_id} was not found")
+ end
+
+ it "raises when the LLM accepts no attachments" do
+ SiteSetting.authorized_extensions += "|pdf"
+ upload = Fabricate(:upload, original_filename: "report.pdf", extension: "pdf")
+
+ expect_not_sent(upload.id, "Upload #{upload.id} is a document and LLM 'Workflow LLM'")
+ end
+
+ it "raises when the image format is not supported" do
+ upload = Fabricate(:image_upload, extension: "heic", original_filename: "photo.heic")
+
+ expect_not_sent(upload.id, "Upload #{upload.id} is not a supported image format")
+ end
- expect(prompts).to eq([["Review the screenshot", { upload_id: visible_upload.id }]])
+ it "raises when encoding drops an upload after the agent ran" do
+ upload = Fabricate(:image_upload)
+ allow(bot).to receive(:reply) do |bot_context, &block|
+ bot_context.execution_context.upload_skips << {
+ upload_id: upload.id,
+ filename: upload.original_filename,
+ message: "could not be converted to png",
+ }
+ block.call("Looks fine", nil, nil)
+ end
+
+ expect_not_sent(upload.id, "was not sent: could not be converted to png")
+ end
end
it "resolves runner expressions per item" do
- agent.update!(vision_enabled: true)
- owner = Fabricate(:user)
runner = Fabricate(:user)
- private_topic = Fabricate(:private_message_topic, user: owner, recipient: runner)
- private_post = Fabricate(:post, topic: private_topic, user: owner)
- upload = Fabricate(:image_upload)
- upload.update!(access_control_post_id: private_post.id)
execute_node_output(
configuration: {
"agent_id" => agent.id,
"runner_username" => "={{ $json.runner_username }}",
"prompt" => "Review the screenshot",
- "upload_ids" => [upload.id],
},
input_items: [{ "json" => { "runner_username" => runner.username } }],
)
expect(bot_contexts.last.user).to eq(runner)
- expect(prompts).to eq([["Review the screenshot", { upload_id: upload.id }]])
end
it "normalizes comma-separated and JSON upload ID values" do
- agent.update!(vision_enabled: true)
first_upload = Fabricate(:image_upload)
second_upload = Fabricate(:image_upload)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
index 0b4eb371433..848178939da 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
@@ -84,7 +84,7 @@ module DiscourseWorkflows
- In create_ai_agent, client_id must be unique among proposed agents and agent must include only name, description, and system_prompt. The server will create a non-system, prompt-only, enabled AI agent when the draft is applied.
- In action:ai_agent nodes that use a proposed agent, set parameters.agent_id to { "$ref": "agent-client-id" } and parameters.agent_name to the same name from create_ai_agent. For existing agents, set parameters.agent_id to the numeric id returned by workflow_ai_agent_catalog.
- For action:ai_agent nodes, set parameters.runner_username when permissions matter. Use "system" for workflow-owned automation, "anonymous" for public anonymous permissions, or a username/expression such as ={{ $json.post.username }} when the agent should only see and use tools as that user.
- - When an action:ai_agent node should analyze images or documents and the current input schema exposes upload IDs, set parameters.upload_ids to a whole-field expression such as ={{ $json.post.upload_ids }}. Do not paste upload URLs into the prompt.
+ - When an action:ai_agent node should analyze images or documents and the current input schema exposes upload IDs, set parameters.upload_ids to a whole-field expression such as ={{ $json.post.upload_ids }}. The node fails when any listed upload cannot reach the model, so only do this when the agent and LLM accept every upload type the input can carry. Do not paste upload URLs into the prompt.
- In add_connection and remove_connection, from/to must be existing node IDs or client_id values introduced by add_node operations in the same proposal.
- Include risk notes for automatic triggers, public content changes, external HTTP requests, credentials, loops, and Code nodes.
- Prefer declarative node configuration over Code nodes when the requested behavior can be expressed without JavaScript.