Skip to content

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

FIX: Fail the classify node when an image can't be sent (#44646)

Régis Hanol68c1ba606dc0
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index cc467c45105..864ee17af82 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -332,17 +332,18 @@ en:
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}"
classify:
errors:
decision_model_not_found: "Decision model with id %{id} not found"
classify_failed: "Decision model '%{decision_model}' could not answer: %{error}"
+ uploads:
+ not_found: "Upload %{upload_id} was not found"
+ 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})"
+ 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 f9eb5b4e826..3f2e154b605 100644
--- a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
+++ b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
@@ -325,7 +325,7 @@ if defined?(DiscourseWorkflows)
key = drop_reason_key(uploads_by_id[upload_id], agent_record, llm_model, guardian)
next if key.nil?
- error_text(
+ upload_error_text(
key,
upload_id: upload_id,
agent: agent_record.name,
@@ -341,8 +341,8 @@ if defined?(DiscourseWorkflows)
end
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)
+ return "not_found" if upload.nil?
+ return "not_visible" if !guardian.can_see_upload?(upload)
encoder = ::DiscourseAi::Completions::UploadEncoder
if !encoder.image?(upload)
@@ -366,6 +366,10 @@ if defined?(DiscourseWorkflows)
I18n.t("discourse_ai.discourse_workflows.ai_agent.errors.#{key}", **args)
end
+ def upload_error_text(key, **args)
+ I18n.t("discourse_ai.discourse_workflows.uploads.#{key}", **args)
+ end
+
def run_agent(agent, config, log, runner)
agent_record = agent[:record]
llm_model = agent[:llm_model]
@@ -392,7 +396,7 @@ if defined?(DiscourseWorkflows)
result, structured = collect_reply(agent[:bot], bot_context, log)
skipped =
- execution_context.upload_skips.map { |skip| error_text("upload_skipped", **skip) }
+ execution_context.upload_skips.map { |skip| upload_error_text("skipped", **skip) }
raise_uploads_not_sent!(agent_record, skipped) if skipped.any?
{ "result" => result }.merge(structured_fields(structured, agent_record))
diff --git a/plugins/discourse-ai/discourse_workflows/nodes/classify/v1.rb b/plugins/discourse-ai/discourse_workflows/nodes/classify/v1.rb
index 4c0b2120c81..026593a9358 100644
--- a/plugins/discourse-ai/discourse_workflows/nodes/classify/v1.rb
+++ b/plugins/discourse-ai/discourse_workflows/nodes/classify/v1.rb
@@ -266,16 +266,32 @@ if defined?(DiscourseWorkflows)
upload_ids = encoder.normalize_upload_ids(upload_ids)
return [] if upload_ids.empty?
- encoder
- .encode(upload_ids:, max_pixels: MAX_IMAGE_PIXELS)
- .map { |image| "data:#{image[:mime_type]};base64,#{image[:base64]}" }
- rescue StandardError => e
- raise DiscourseAi::Decisions::InvalidRequest, "Could not prepare images: #{e.message}"
+ missing = upload_ids - ::Upload.where(id: upload_ids).pluck(:id)
+ skips = []
+ images =
+ begin
+ encoder.encode(upload_ids:, max_pixels: MAX_IMAGE_PIXELS, skips:)
+ rescue StandardError => e
+ raise DiscourseAi::Decisions::InvalidRequest,
+ "Could not prepare images: #{e.message}"
+ end
+
+ # Answering without every image would record a confident verdict on partial evidence.
+ reasons =
+ missing.map { |upload_id| upload_error_text("not_found", upload_id:) } +
+ skips.map { |skip| upload_error_text("skipped", **skip) }
+ raise DiscourseAi::Decisions::InvalidRequest, reasons.join(". ") if reasons.any?
+
+ images.map { |image| "data:#{image[:mime_type]};base64,#{image[:base64]}" }
end
def error_text(key, **args)
I18n.t("discourse_ai.discourse_workflows.classify.errors.#{key}", **args)
end
+
+ def upload_error_text(key, **args)
+ I18n.t("discourse_ai.discourse_workflows.uploads.#{key}", **args)
+ end
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb
index ee37d8aa476..8a65859557d 100644
--- a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb
@@ -202,6 +202,37 @@ RSpec.describe DiscourseWorkflows::Nodes::Classify::V1 do
expect(item).to match_node_output_schema(described_class, configuration: config)
end
+ it "fails instead of answering when an image can't be sent" do
+ decision_model.update!(vision_enabled: true)
+ upload = Fabricate(:image_upload, original_filename: "cube.png")
+ missing_id = upload.id + 1000
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:fetch_path).and_return(nil)
+ request = stub_request(:post, decision_model.url)
+
+ expect {
+ execute_node(
+ configuration:
+ configuration(
+ { "name" => "solved", "type" => "yes_no", "instructions" => "Is the cube solved?" },
+ upload_ids: "={{ $json.post.upload_ids }}",
+ ),
+ item: {
+ "json" => {
+ "post" => {
+ "raw" => "Done!",
+ "upload_ids" => [upload.id, missing_id],
+ },
+ },
+ },
+ )
+ }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "Decision model 'Workflow decisions' could not answer: Upload #{missing_id} was not found. " \
+ "Upload #{upload.id} (cube.png) was not sent: file is not available in the store [item 0]",
+ )
+ expect(request).not_to have_been_requested
+ end
+
it "fails when the decision model no longer exists" do
expect {
execute_node(