Skip to content

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

FEATURE: extract text from document uploads for LLM prompts (#39634)

Samfa54f62348a7
diff --git a/Gemfile b/Gemfile
index 1a24cd992a6..771a093ce31 100644
--- a/Gemfile
+++ b/Gemfile
@@ -227,6 +227,10 @@ gem "rqrcode"
gem "rubyzip", require: false
+install_if -> { RUBY_PLATFORM.include?("linux") } do
+ gem "landlock", require: false
+end
+
gem "sshkey", require: false
gem "rchardet", require: false
diff --git a/Gemfile.lock b/Gemfile.lock
index 9e7ddb59466..22e3f28c4f9 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -256,6 +256,7 @@ GEM
simpleidn (~> 0.2)
jwt (2.10.1)
base64
+ landlock (0.2)
language_server-protocol (3.17.0.5)
libv8-node (24.12.0.1)
libv8-node (24.12.0.1-aarch64-linux)
@@ -831,6 +832,7 @@ DEPENDENCIES
iso8601
json
json_schemer
+ landlock
listen
lograge
logstash-event
@@ -1052,6 +1054,7 @@ CHECKSUMS
json-schema (6.2.0) sha256=e8bff46ed845a22c1ab2bd0d7eccf831c01fe23bb3920caa4c74db4306813666
json_schemer (2.5.0) sha256=2f01fb4cce721a4e08dd068fc2030cffd0702a7f333f1ea2be6e8991f00ae396
jwt (2.10.1) sha256=e6424ae1d813f63e761a04d6284e10e7ec531d6f701917fadcd0d9b2deaf1cc5
+ landlock (0.2) sha256=9bb86d7d12b16485b278ba575d49f55b730089c7a3d0f45a769fe4d7384d5e55
language_server-protocol (3.17.0.5) sha256=fd1e39a51a28bf3eec959379985a72e296e9f9acfce46f6a79d31ca8760803cc
libv8-node (24.12.0.1) sha256=d93d23b861bfe5de3ba829e34a142402fb83b4c3592dd58d9ac4e027588d739a
libv8-node (24.12.0.1-aarch64-linux) sha256=22bd0246cde85d70c88cf772727ac3677a20ac1d169105f82efe5f1f7c39f755
diff --git a/lib/compression/safe_zip_reader.rb b/lib/compression/safe_zip_reader.rb
new file mode 100644
index 00000000000..bf3855ac4db
--- /dev/null
+++ b/lib/compression/safe_zip_reader.rb
@@ -0,0 +1,127 @@
+# frozen_string_literal: true
+
+require "zip"
+
+module Compression
+ class SafeZipReader
+ Error = Class.new(StandardError)
+ EntryTooLargeError = Class.new(Error)
+ MissingEntryError = Class.new(Error)
+ TooManyEntriesError = Class.new(Error)
+ SuspiciousEntryError = Class.new(Error)
+
+ DEFAULT_READ_CHUNK_BYTES = 16 * 1024
+
+ attr_reader :zip_file, :remaining_total_bytes
+
+ def self.open(path, **kwargs)
+ ::Zip::File.open(path) do |zip_file|
+ reader = new(zip_file, **kwargs)
+ reader.validate!
+
+ yield(reader)
+ end
+ end
+
+ def initialize(
+ zip_file,
+ max_entries: nil,
+ max_total_bytes: nil,
+ max_compression_ratio: nil,
+ read_chunk_bytes: DEFAULT_READ_CHUNK_BYTES
+ )
+ @zip_file = zip_file
+ @max_entries = max_entries
+ @remaining_total_bytes = max_total_bytes
+ @max_compression_ratio = max_compression_ratio
+ @read_chunk_bytes = read_chunk_bytes
+ end
+
+ def validate!
+ if @max_entries && entries.size > @max_entries
+ raise TooManyEntriesError, "Zip archive has too many entries"
+ end
+
+ self
+ end
+
+ def entries
+ zip_file.entries
+ end
+
+ def find_entry(name)
+ zip_file.find_entry(name)
+ end
+
+ def read_entry(entry_or_name, max_bytes:, required: false)
+ entry = resolve_entry(entry_or_name)
+ if required && (entry.nil? || entry.directory?)
+ raise MissingEntryError, "Zip entry #{entry_name(entry_or_name)} is missing"
+ end
+ return if entry.nil? || entry.directory?
+
+ data = +""
+ stream_entry(entry, max_bytes: max_bytes) { |chunk| data << chunk }
+ data
+ end
+
+ def stream_entry_to_file(entry_or_name, file, max_bytes:, required: false)
+ entry = resolve_entry(entry_or_name)
+ if required && (entry.nil? || entry.directory?)
+ raise MissingEntryError, "Zip entry #{entry_name(entry_or_name)} is missing"
+ end
+ return if entry.nil? || entry.directory?
+
+ stream_entry(entry, max_bytes: max_bytes) { |chunk| file.write(chunk) }
+ end
+
+ private
+
+ def resolve_entry(entry_or_name)
+ entry_or_name.respond_to?(:get_input_stream) ? entry_or_name : find_entry(entry_or_name)
+ end
+
+ def entry_name(entry_or_name)
+ entry_or_name.respond_to?(:name) ? entry_or_name.name : entry_or_name
+ end
+
+ def stream_entry(entry, max_bytes:)
+ limit = entry_limit(max_bytes)
+ validate_entry_metadata!(entry, limit)
+
+ bytes_read = 0
+ entry.get_input_stream do |stream|
+ while (chunk = stream.read(@read_chunk_bytes))
+ bytes_read += chunk.bytesize
+ raise EntryTooLargeError, "Zip entry #{entry.name} is too large" if bytes_read > limit
+
+ yield chunk
+ end
+ end
+
+ @remaining_total_bytes -= bytes_read if @remaining_total_bytes
+ bytes_read
+ end
+
+ def entry_limit(max_bytes)
+ limit = [max_bytes, @remaining_total_bytes].compact.min
+ raise EntryTooLargeError, "Zip content is too large" if limit.nil? || limit <= 0
+
+ limit
+ end
+
+ def validate_entry_metadata!(entry, limit)
+ if entry.size && entry.size > limit
+ raise EntryTooLargeError, "Zip entry #{entry.name} is too large"
+ end
+
+ return if @max_compression_ratio.nil?
+ return if entry.compressed_size.nil? || entry.compressed_size <= 0
+ return if entry.size.nil?
+
+ if (entry.size.to_f / entry.compressed_size) > @max_compression_ratio
+ raise SuspiciousEntryError, "Zip entry #{entry.name} has a suspicious compression ratio"
+ end
+ end
+ end
+end
diff --git a/lib/compression/zip.rb b/lib/compression/zip.rb
index ccfe4096d34..672fd532edc 100644
--- a/lib/compression/zip.rb
+++ b/lib/compression/zip.rb
@@ -1,9 +1,12 @@
# frozen_string_literal: true
require "zip"
+require "compression/safe_zip_reader"
module Compression
class Zip < Strategy
+ MAX_ZIP_ENTRIES = 10_000
+
def extension
".zip"
end
@@ -32,6 +35,7 @@ module Compression
def get_compressed_file_stream(compressed_file_path)
zip_file = ::Zip::File.open(compressed_file_path)
+ Compression::SafeZipReader.new(zip_file, max_entries: MAX_ZIP_ENTRIES).validate!
yield(zip_file)
end
diff --git a/lib/discourse/safe_exec.rb b/lib/discourse/safe_exec.rb
new file mode 100644
index 00000000000..6c4fda6ac54
--- /dev/null
+++ b/lib/discourse/safe_exec.rb
@@ -0,0 +1,121 @@
+# frozen_string_literal: true
+
+begin
+ require "landlock"
+rescue LoadError
+end
+
+module Discourse
+ class SafeExec
+ DEFAULT_READ_PATHS = %w[/bin /etc /lib /lib64 /usr].freeze
+ DEFAULT_EXECUTE_PATHS = %w[/bin /lib /lib64 /usr].freeze
+ LANDLOCK_COMMAND_ERROR =
+ if defined?(::Landlock::SafeExec::CommandError)
+ ::Landlock::SafeExec::CommandError
+ else
+ Class.new(StandardError)
+ end
+
+ def self.capture(
+ *command,
+ read: [],
+ write: [],
+ execute: [],
+ timeout: nil,
+ failure_message: "",
+ success_status_codes: [0],
+ env: nil,
+ unsetenv_others: false,
+ chdir: nil,
+ connect_tcp: nil,
+ bind_tcp: [],
+ rlimits: {},
+ seccomp_deny_network: false,
+ max_output_bytes: nil,
+ truncate_output: false
+ )
+ if defined?(::Landlock::SafeExec)
+ result =
+ ::Landlock::SafeExec.capture(
+ *command,
+ read: read,
+ write: write,
+ execute: execute,
+ timeout: timeout,
+ failure_message: failure_message,
+ success_status_codes: success_status_codes,
+ env: env || {},
+ inherit_env: !unsetenv_others,
+ chdir: chdir,
+ connect_tcp: connect_tcp,
+ bind_tcp: bind_tcp,
+ rlimits: rlimits,
+ seccomp_deny_network: seccomp_deny_network,
+ max_output_bytes: max_output_bytes,
+ truncate_output: truncate_output,
+ )
+
+ return result.stdout if result.output_truncated? && truncate_output
+
+ if !result.status.exited? || !success_status_codes.include?(result.status.exitstatus)
+ raise_command_error(command, failure_message, result)
+ end
+
+ result.stdout
+ else
+ fallback_command = env ? [env, *command] : command
+ Discourse::Utils.execute_command(
+ *fallback_command,
+ timeout: timeout,
+ failure_message: failure_message,
+ success_status_codes: success_status_codes,
+ chdir: chdir || ".",
+ )
+ end
+ rescue LANDLOCK_COMMAND_ERROR => e
+ raise Discourse::Utils::CommandError.new(
+ e.message,
+ stdout: e.stdout,
+ stderr: e.stderr,
+ status: e.status,
+ )
+ end
+
+ def self.landlock_supported?
+ defined?(::Landlock::SafeExec) && ::Landlock::SafeExec.supported?
+ end
+
+ def self.default_read_paths
+ if defined?(::Landlock::SafeExec)
+ ::Landlock::SafeExec.default_read_paths
+ else
+ existing_paths(DEFAULT_READ_PATHS)
+ end
+ end
+
+ def self.default_execute_paths
+ if defined?(::Landlock::SafeExec)
+ ::Landlock::SafeExec.default_execute_paths
+ else
+ existing_paths(DEFAULT_EXECUTE_PATHS)
+ end
+ end
+
+ def self.existing_paths(paths)
+ Array(paths).filter { |path| path.to_s != "" && File.exist?(path) }.uniq
+ end
+
+ def self.raise_command_error(command, failure_message, result)
+ message =
+ [command.join(" "), failure_message, result.stderr].filter { |part| part.to_s != "" }
+ .join("\n")
+ raise Discourse::Utils::CommandError.new(
+ message,
+ stdout: result.stdout,
+ stderr: result.stderr,
+ status: result.status,
+ )
+ end
+ private_class_method :raise_command_error
+ end
+end
diff --git a/lib/tasks/site.rake b/lib/tasks/site.rake
index dec1d428afe..2f17c5f0230 100644
--- a/lib/tasks/site.rake
+++ b/lib/tasks/site.rake
@@ -2,8 +2,13 @@
require "yaml"
require "zip"
+require "compression/safe_zip_reader"
class ZippedSiteStructure
+ MAX_ZIP_ENTRIES = 10_000
+ MAX_JSON_ENTRY_BYTES = 50 * 1024 * 1024
+ MAX_UPLOAD_ENTRY_BYTES = 1024 * 1024 * 1024
+
attr_reader :zip
def initialize(path, create: false)
@@ -20,7 +25,7 @@ class ZippedSiteStructure
end
def get(name)
- data = @zip.get_input_stream("#{name}.json").read
+ data = read_entry("#{name}.json", max_bytes: MAX_JSON_ENTRY_BYTES)
JSON.parse(data)
end
@@ -71,7 +76,7 @@ class ZippedSiteStructure
puts " - Importing upload #{upload["filename"]} from #{upload["path"]}"
tempfile = Tempfile.new(upload["filename"], binmode: true)
- tempfile.write(@zip.get_input_stream(upload["path"]).read)
+ stream_entry_to_file(upload["path"], tempfile, max_bytes: MAX_UPLOAD_ENTRY_BYTES)
tempfile.rewind
@uploads[upload["path"]] ||= UploadCreator.new(tempfile, upload["filename"], opts).create_for(
@@ -79,8 +84,21 @@ class ZippedSiteStructure
)
end
+ def read_entry(path, max_bytes:)
+ safe_zip_reader.read_entry(path, max_bytes: max_bytes, required: true)
+ end
+
+ def stream_entry_to_file(path, file, max_bytes:)
+ safe_zip_reader.stream_entry_to_file(path, file, max_bytes: max_bytes, required: true)
+ end
+
private
+ def safe_zip_reader
+ @safe_zip_reader ||=
+ Compression::SafeZipReader.new(@zip, max_entries: MAX_ZIP_ENTRIES).validate!
+ end
+
def get_unique_path(path)
return path if @zip.find_entry(path).blank?
@@ -498,7 +516,11 @@ task "site:import_structure", [:zip_path] => :environment do |task, args|
)
elsif t["filename"].present?
tempfile = Tempfile.new(t["filename"], binmode: true)
- tempfile.write(data.zip.get_input_stream(t["path"]).read)
+ data.stream_entry_to_file(
+ t["path"],
+ tempfile,
+ max_bytes: ZippedSiteStructure::MAX_UPLOAD_ENTRY_BYTES,
+ )
tempfile.flush
RemoteTheme.update_zipped_theme(
diff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rb
index 5c8d39dc0b7..4c43b8f2945 100644
--- a/plugins/discourse-ai/app/models/llm_model.rb
+++ b/plugins/discourse-ai/app/models/llm_model.rb
@@ -10,8 +10,8 @@ class LlmModel < ActiveRecord::Base
BEDROCK_CONVERSE_PROVIDER_NAME = "aws_bedrock_converse"
DEFAULT_ALLOWED_ATTACHMENT_TYPES = [].freeze
ATTACHMENT_TYPE_ALIASES = {
- "md" => "markdown",
- "markdown" => "markdown",
+ "markdown" => "md",
+ "md" => "md",
"htm" => "html",
"text" => "txt",
}.freeze
@@ -389,11 +389,7 @@ class LlmModel < ActiveRecord::Base
tokenizer.constantize
end
- def allowed_attachment_types
- (self[:allowed_attachment_types].presence || DEFAULT_ALLOWED_ATTACHMENT_TYPES).map(&:downcase)
- end
-
- def allowed_attachment_types=(value)
+ def self.normalize_attachment_types(value)
normalized =
Array(value)
.map { |v| v.to_s.downcase.strip }
@@ -401,7 +397,17 @@ class LlmModel < ActiveRecord::Base
.reject(&:blank?)
.uniq
normalized = DEFAULT_ALLOWED_ATTACHMENT_TYPES if normalized.empty?
- self[:allowed_attachment_types] = normalized
+ normalized
+ end
+
+ def allowed_attachment_types
+ self.class.normalize_attachment_types(
+ self[:allowed_attachment_types].presence || DEFAULT_ALLOWED_ATTACHMENT_TYPES,
+ )
+ end
+
+ def allowed_attachment_types=(value)
+ self[:allowed_attachment_types] = self.class.normalize_attachment_types(value)
end
def lookup_custom_param(key)
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-attachment-types.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-attachment-types.gjs
index e26975795f7..4c5af18de5a 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-attachment-types.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-llm-attachment-types.gjs
@@ -11,9 +11,12 @@ const DEFAULT_CHOICES = [
"txt",
"doc",
"docx",
+ "csv",
+ "xls",
+ "xlsx",
"rtf",
"html",
- "markdown",
+ "md",
];
/**
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 993941834fc..c8b1fc25260 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -556,9 +556,9 @@ en:
max_turn_tokens_help: "Maximum total tokens (input + output) consumed across all LLM calls during a single bot reply. When set, replaces the fixed completion/tool limits with a token budget, enabling more agentic behavior. Leave empty for default limits."
compression_threshold: "Compression Threshold"
compression_threshold_help: "Percentage of context usage (20–99) at which automatic compression kicks in. Lower values trigger compression earlier, preserving more headroom."
- vision_enabled: Vision enabled
- vision_enabled_help: If enabled, the AI will attempt to understand images users post in the topic, depends on the model being used supporting vision. Supported by latest models from Anthropic, Google, and OpenAI.
- vision_max_pixels: Supported image size
+ vision_enabled: Include image uploads
+ vision_enabled_help: If enabled, this agent can include images from posts and chats when the selected model supports image input.
+ vision_max_pixels: Maximum image size
vision_max_pixel_sizes:
low: Low quality - cheapest (256x256)
medium: Medium quality (512x512)
@@ -864,9 +864,9 @@ en:
max_output_tokens: "Max output tokens"
url: "URL of the service hosting the model"
api_key: "API Key of the service hosting the model"
- vision_enabled: "Vision enabled"
- allowed_attachment_types: "Allowed attachments"
- attachment_types_placeholder: "Select or type extensions (e.g., pdf, txt, docx)"
+ vision_enabled: "Supports image input"
+ allowed_attachment_types: "Allowed document attachments"
+ attachment_types_placeholder: "Select or type extensions (e.g., pdf, txt, csv, docx, xlsx)"
ai_bot_user: "AI bot User"
cost_input: "Input cost"
cost_cached_input: "Cached input cost"
@@ -960,8 +960,8 @@ en:
max_output_tokens: "The maximum number of tokens the model can generate in a single request"
display_name: "The name used to reference this model across your site's interface."
name: "We include this in the API call to specify which model we'll use"
- vision_enabled: "If enabled, the AI will attempt to understand images. It depends on the model being used supporting vision. Supported by latest models from Anthropic, Google, and OpenAI."
- allowed_attachment_types: "Choose which attachment types are forwarded to the model. Leave blank to disallow attachments. Images are controlled via vision."
+ vision_enabled: "Whether this model/provider can receive and understand images."
+ allowed_attachment_types: "Choose which document attachment types can be converted or forwarded. Leave blank to disallow document attachments. Images are controlled separately."
cost_input: "The input cost per 1M tokens for this model"
cost_cached_input: "The cached input cost per 1M tokens for this model (when reading from prompt cache)"
cost_cache_write: "The cache write cost per 1M tokens for this model (when writing to prompt cache)"
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index b81ed34bd1f..968380d2c80 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -232,7 +232,9 @@ module DiscourseAi
post,
max_posts: 5,
bot_usernames: available_bot_usernames,
- include_uploads: bot.agent.class.vision_enabled,
+ include_image_uploads: include_image_uploads?,
+ include_document_uploads: include_document_uploads?,
+ allowed_attachment_types: bot.model.allowed_attachment_types,
)
# conversation context may contain tool calls, and confusing user names
@@ -325,7 +327,9 @@ module DiscourseAi
message,
channel: channel,
context_post_ids: context_post_ids,
- include_uploads: bot.agent.class.vision_enabled,
+ include_image_uploads: include_image_uploads?,
+ include_document_uploads: include_document_uploads?,
+ allowed_attachment_types: bot.model.allowed_attachment_types,
max_messages: max_chat_messages,
bot_user_ids: available_bot_user_ids,
instruction_message: instruction_message,
@@ -460,7 +464,9 @@ module DiscourseAi
post,
style: context_style,
max_posts: max_context_posts,
- include_uploads: bot.agent.class.vision_enabled,
+ include_image_uploads: include_image_uploads?,
+ include_document_uploads: include_document_uploads?,
+ allowed_attachment_types: bot.model.allowed_attachment_types,
bot_usernames: available_bot_usernames,
),
)
@@ -694,6 +700,14 @@ module DiscourseAi
@bot_ids ||= AiAgent.joins(:user).pluck("users.id").concat(available_bot_users.map(&:id))
end
+ def include_image_uploads?
+ bot.agent.class.vision_enabled
+ end
+
+ def include_document_uploads?
+ bot.model.allowed_attachment_types.present?
+ end
+
private
def should_stop_thinking?(partial:, context:, type:, started_thinking:, placeholder:)
diff --git a/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb b/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb
index c6e65dc5deb..d9c87a200b2 100644
--- a/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb
+++ b/plugins/discourse-ai/lib/ai_bot/stream_reply_custom_tools_session.rb
@@ -113,7 +113,9 @@ module DiscourseAi
DiscourseAi::Completions::PromptMessagesBuilder.messages_from_post(
@source_post,
max_posts: max_context_posts,
- include_uploads: @bot.agent.class.vision_enabled,
+ include_image_uploads: @bot.agent.class.vision_enabled,
+ include_document_uploads: @bot.model.allowed_attachment_types.present?,
+ allowed_attachment_types: @bot.model.allowed_attachment_types,
bot_usernames: available_bot_usernames,
),
)
diff --git a/plugins/discourse-ai/lib/automation/llm_tagger.rb b/plugins/discourse-ai/lib/automation/llm_tagger.rb
index f69679272de..964b718a87f 100644
--- a/plugins/discourse-ai/lib/automation/llm_tagger.rb
+++ b/plugins/discourse-ai/lib/automation/llm_tagger.rb
@@ -47,9 +47,16 @@ module DiscourseAi
end
all_upload_ids = topic_posts.flat_map(&:upload_ids).compact.uniq
- if all_upload_ids.present?
+ upload_ids =
+ DiscourseAi::Completions::PromptMessagesBuilder.filtered_upload_ids_for_prompt(
+ all_upload_ids,
+ include_image_uploads: tagger_agent.vision_enabled,
+ include_document_uploads: model.allowed_attachment_types.present?,
+ allowed_attachment_types: model.allowed_attachment_types,
+ )
+ if upload_ids.present?
input = [input]
- input.concat(all_upload_ids.map { |upload_id| { upload_id: upload_id } })
+ input.concat(upload_ids.map { |upload_id| { upload_id: upload_id } })
end
bot =
diff --git a/plugins/discourse-ai/lib/automation/llm_triage.rb b/plugins/discourse-ai/lib/automation/llm_triage.rb
index 7568f5e131f..f6d27280e3a 100644
--- a/plugins/discourse-ai/lib/automation/llm_triage.rb
+++ b/plugins/discourse-ai/lib/automation/llm_triage.rb
@@ -89,9 +89,17 @@ module DiscourseAi
strict: SiteSetting.ai_strict_token_counting,
) if max_post_tokens.present?
- if post.upload_ids.present? && triage_agent.vision_enabled
+ upload_ids =
+ DiscourseAi::Completions::PromptMessagesBuilder.filtered_upload_ids_for_prompt(
+ post.upload_ids,
+ include_image_uploads: triage_agent.vision_enabled,
+ include_document_uploads: model.allowed_attachment_types.present?,
+ allowed_attachment_types: model.allowed_attachment_types,
+ )
+
+ if upload_ids.present?
input = [input]
- input.concat(post.upload_ids.map { |upload_id| { upload_id: upload_id } })
+ input.concat(upload_ids.map { |upload_id| { upload_id: upload_id } })
end
bot_ctx =
diff --git a/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb b/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb
index cc50024faaf..0b27c3e0cd6 100644
--- a/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb
@@ -129,7 +129,7 @@ module DiscourseAi
content_array =
to_encoded_content_array(
content: content_array.flatten,
- upload_encoder: ->(details) { upload_node(details) },
+ upload_encoder: ->(details) { upload_node(details, role) },
text_encoder: ->(text) { text_node(text, role) },
other_encoder: ->(hash) { hash },
allow_images:,
@@ -154,7 +154,9 @@ module DiscourseAi
{ type: "text", text: text }
end
- def upload_node(details)
+ def upload_node(details, role)
+ return text_node(details[:text], role) if details[:text].present?
+
if details[:mime_type] == "application/pdf" || details[:kind] == :document
file_node(details)
else
diff --git a/plugins/discourse-ai/lib/completions/dialects/claude.rb b/plugins/discourse-ai/lib/completions/dialects/claude.rb
index 7166e151a3a..818b8f66ef4 100644
--- a/plugins/discourse-ai/lib/completions/dialects/claude.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/claude.rb
@@ -175,6 +175,7 @@ module DiscourseAi
def upload_node(details)
return if details.blank?
+ return { type: "text", text: details[:text] } if details[:text].present?
if details[:kind] == :document || details[:mime_type] == "application/pdf"
{
diff --git a/plugins/discourse-ai/lib/completions/dialects/converse.rb b/plugins/discourse-ai/lib/completions/dialects/converse.rb
index 2492cedc76c..33752bd73eb 100644
--- a/plugins/discourse-ai/lib/completions/dialects/converse.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/converse.rb
@@ -107,23 +107,40 @@ module DiscourseAi
end
def user_msg(msg)
- images = nil
- if vision_support?
- encoded_uploads = prompt.encoded_uploads(msg)
- encoded_uploads&.each do |upload|
- images ||= []
- images << {
- image: {
- format: upload[:format] || detect_format(upload[:mime_type]),
- source: {
- bytes: upload[:base64],
- },
- },
- }
- end
- end
+ content_array = [msg[:content]].flatten
+
+ content_array =
+ to_encoded_content_array(
+ content: content_array,
+ upload_encoder: ->(details) { upload_node(details) },
+ text_encoder: ->(text) { { text: text } },
+ allow_images: vision_support?,
+ allow_documents: true,
+ allowed_attachment_types: llm_model.allowed_attachment_types,
+ upload_filter: ->(encoded) { converse_upload_allowed?(encoded) },
+ )
+
+ { role: "user", content: content_array }
+ end
+
+ def converse_upload_allowed?(encoded)
+ return true if encoded[:kind] == :image
+
+ document_allowed?(encoded) && encoded[:text].present?
+ end
- { role: "user", content: DiscourseAi::Completions::Prompt.text_only(msg), images: images }
+ def upload_node(details)
+ return { text: details[:text] } if details[:text].present?
+ return if details[:kind] != :image
+
+ {
+ image: {
+ format: details[:format] || detect_format(details[:mime_type]),
+ source: {
+ bytes: details[:base64],
+ },
+ },
+ }
end
def model_msg(msg)
diff --git a/plugins/discourse-ai/lib/completions/dialects/dialect.rb b/plugins/discourse-ai/lib/completions/dialects/dialect.rb
index d0cadc06509..db5c085fd42 100644
--- a/plugins/discourse-ai/lib/completions/dialects/dialect.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/dialect.rb
@@ -136,6 +136,7 @@ module DiscourseAi
if strip_upload_markdown_mode != :none
messages = strip_upload_markdown(messages, strip_mode: strip_upload_markdown_mode)
end
+ messages = expand_text_document_uploads(messages)
messages = trim_messages(messages)
last_message = messages.last
inject_done_on_last_tool_call = false
@@ -204,7 +205,6 @@ module DiscourseAi
prompt_limit = max_prompt_tokens
current_token_count = 0
- message_step_size = (prompt_limit / 25).to_i * -1
trimmed_messages = []
@@ -246,13 +246,16 @@ module DiscourseAi
end
# Trimming content to make sure we respect token limit.
- while dupped_msg[:content].present? &&
- message_tokens + current_token_count + per_message_overhead > prompt_limit
- dupped_msg[:content] = dupped_msg[:content][0..message_step_size] || ""
+ available_tokens = prompt_limit - current_token_count - per_message_overhead
+ if message_tokens > available_tokens
+ dupped_msg[:content] = truncate_content_to_token_budget(
+ dupped_msg[:content],
+ available_tokens,
+ )
message_tokens = calculate_message_token(dupped_msg)
end
- next if dupped_msg[:content].blank?
+ next if content_blank?(dupped_msg[:content])
current_token_count += message_tokens + per_message_overhead
@@ -268,8 +271,117 @@ module DiscourseAi
0
end
+ def expand_text_document_uploads(messages)
+ messages.map do |message|
+ content = message[:content]
+ next message if !content.is_a?(Array)
+
+ expanded_content =
+ content.map do |part|
+ next part if !part.is_a?(Hash) || !part.key?(:upload_id)
+
+ encoded =
+ prompt.encode_upload(
+ part[:upload_id],
+ allow_images: false,
+ allow_documents: true,
+ allowed_attachment_types: llm_model.allowed_attachment_types,
+ )
+
+ if encoded&.dig(:kind) == :document && encoded[:text].present? &&
+ document_allowed?(encoded)
+ { encoded_upload: encoded }
+ else
+ part
+ end
+ end
+
+ message.merge(content: expanded_content)
+ end
+ end
+
+ def truncate_content_to_token_budget(content, token_budget)
+ return "" if token_budget <= 0
+
+ case content
+ when Array
+ truncate_array_content_to_token_budget(content, token_budget)
+ when Hash
+ truncate_hash_content_to_token_budget(content, token_budget)
+ else
+ tokenizer.truncate(
+ content.to_s,
+ token_budget,
+ strict: SiteSetting.ai_strict_token_counting,
+ )
+ end
+ end
+
+ def truncate_array_content_to_token_budget(content, token_budget)
+ remaining_tokens = token_budget
+ truncated = []
+
+ content.each do |part|
+ part_tokens = calculate_content_token(part)
+ if part_tokens <= remaining_tokens
+ truncated << part
+ remaining_tokens -= part_tokens
+ elsif part.is_a?(String) || (part.is_a?(Hash) && part.key?(:encoded_upload))
+ truncated_part = truncate_content_to_token_budget(part, remaining_tokens)
+ truncated << truncated_part if !content_blank?(truncated_part)
+ break
+ else
+ break
+ end
+ end
+
+ truncated
+ end
+
+ def truncate_hash_content_to_token_budget(content, token_budget)
+ return "" if !content.key?(:encoded_upload)
+
+ encoded = content[:encoded_upload].dup
+ encoded[:text] = tokenizer.truncate(
+ encoded[:text].to_s,
+ token_budget,
+ strict: SiteSetting.ai_strict_token_counting,
+ )
+ encoded[:text].present? ? { encoded_upload: encoded } : ""
+ end
+
+ def content_blank?(content)
+ case content
+ when Array
+ content.all? { |part| content_blank?(part) }
+ when Hash
+ content.key?(:encoded_upload) ? content[:encoded_upload][:text].blank? : content.blank?
+ else
+ content.blank?
+ end
+ end
+
def calculate_message_token(msg)
- llm_model.tokenizer_class.size(msg[:content].to_s)
+ calculate_content_token(msg[:content])
+ end
+
+ def calculate_content_token(content)
+ case content
+ when Array
+ content.sum { |part| calculate_content_token(part) }
+ when Hash
+ if content.key?(:encoded_upload)
+ calculate_content_token(content[:encoded_upload][:text].to_s)
+ else
+ tokenizer.size(content.to_s)
+ end
+ else
+ tokenizer.size(content.to_s)
+ end
+ end
+
+ def tokenizer
+ llm_model.tokenizer_class
end
def tools_dialect
@@ -318,15 +430,20 @@ module DiscourseAi
content.each do |c|
if c.is_a?(String)
current_string << c
- elsif c.is_a?(Hash) && c.key?(:upload_id)
+ elsif c.is_a?(Hash) && (c.key?(:upload_id) || c.key?(:encoded_upload))
next if !allow_images && !allow_documents
encoded =
- prompt.encode_upload(
- c[:upload_id],
- allow_documents: allow_documents,
- allowed_attachment_types: allowed_attachment_types,
- )
+ if c.key?(:encoded_upload)
+ c[:encoded_upload]
+ else
+ prompt.encode_upload(
+ c[:upload_id],
+ allow_images: allow_images,
+ allow_documents: allow_documents,
+ allowed_attachment_types: allowed_attachment_types,
+ )
+ end
next if encoded.blank?
is_image = encoded[:kind] == :image
@@ -341,7 +458,8 @@ module DiscourseAi
current_string = +""
end
- result << upload_encoder.call(encoded)
+ encoded_upload = upload_encoder.call(encoded)
+ result << encoded_upload if encoded_upload
elsif other_encoder
encoded = other_encoder.call(c)
result << encoded if encoded
@@ -352,28 +470,16 @@ module DiscourseAi
result
end
- def attachment_type_for(encoded)
- ext = File.extname(encoded[:filename].to_s).delete_prefix(".").downcase
- mime = encoded[:mime_type].to_s
-
- return "pdf" if ext == "pdf" || mime.include?("pdf")
- return "docx" if ext == "docx"
- return "doc" if ext == "doc"
- return "txt" if ext == "txt" || mime.include?("text/plain")
- return "rtf" if ext == "rtf"
- return "html" if %w[html htm].include?(ext) || mime.include?("html")
- return "markdown" if %w[md markdown].include?(ext) || mime.include?("markdown")
-
- "file"
- end
-
def document_allowed?(encoded)
return true if encoded[:kind] != :document
allowed_types = llm_model.allowed_attachment_types
return false if allowed_types.blank?
- allowed_types.include?(attachment_type_for(encoded))
+ ext = File.extname(encoded[:filename].to_s).delete_prefix(".")
+ allowed_types.include?(
+ DiscourseAi::Completions::UploadEncoder.attachment_type_for(ext, encoded[:mime_type]),
+ )
end
end
end
diff --git a/plugins/discourse-ai/lib/completions/dialects/gemini.rb b/plugins/discourse-ai/lib/completions/dialects/gemini.rb
index 31c694706d3..469eb4c60ca 100644
--- a/plugins/discourse-ai/lib/completions/dialects/gemini.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/gemini.rb
@@ -133,6 +133,8 @@ module DiscourseAi
end
def upload_node(details)
+ return { text: details[:text] } if details[:text].present?
+
image_node(details)
end
diff --git a/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb b/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
index 24194688027..afee6c5d107 100644
--- a/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
@@ -97,7 +97,7 @@ module DiscourseAi
content_array =
to_encoded_content_array(
content: content_array.flatten,
- upload_encoder: ->(details) { upload_node(details) },
+ upload_encoder: ->(details) { upload_node(details, role) },
text_encoder: ->(text) { text_node(text, role) },
other_encoder: ->(hash) { thinking_signature_node(hash) },
allow_images:,
@@ -145,7 +145,9 @@ module DiscourseAi
{ type: role == "user" ? "input_text" : "output_text", text: text }
end
- def upload_node(details)
+ def upload_node(details, role)
+ return text_node(details[:text], role) if details[:text].present?
+
if details[:mime_type] == "application/pdf" || details[:kind] == :document
file_node(details)
else
diff --git a/plugins/discourse-ai/lib/completions/doc_to_text.rb b/plugins/discourse-ai/lib/completions/doc_to_text.rb
new file mode 100644
index 00000000000..abce4a203a3
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/doc_to_text.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+require "discourse/safe_exec"
+
+module DiscourseAi
+ module Completions
+ class DocToText
+ ANTIWORD_TIMEOUT_SECONDS = 5
+ MAX_CONVERSION_OUTPUT_BYTES = 4 * 100_001
+ SAFE_EXEC_ENV = { "PATH" => ENV["PATH"].to_s }.freeze
+ ANTIWORD_RLIMITS = {
+ cpu_seconds: ANTIWORD_TIMEOUT_SECONDS,
+ memory_bytes: 256 * 1024 * 1024,
+ file_size_bytes: 1 * 1024 * 1024,
+ open_files: 64,
+ processes: 0,
+ }
+
+ def self.convert(path)
+ return if !antiword_installed?
+
+ Discourse::SafeExec.capture(
+ "antiword",
+ "-w",
+ "0",
+ path,
+ read: sandbox_read_paths(path),
+ execute: Discourse::SafeExec.default_execute_paths,
+ timeout: ANTIWORD_TIMEOUT_SECONDS,
+ env: SAFE_EXEC_ENV,
+ unsetenv_others: true,
+ rlimits: ANTIWORD_RLIMITS,
+ seccomp_deny_network: true,
+ max_output_bytes: MAX_CONVERSION_OUTPUT_BYTES,
+ truncate_output: true,
+ failure_message: "Failed to convert .doc upload to text",
+ )
+ end
+
+ def self.antiword_installed?
+ return @antiword_installed if defined?(@antiword_installed)
+
+ @antiword_installed =
+ begin
+ Discourse::Utils.execute_command("which", "antiword")
+ true
+ rescue Discourse::Utils::CommandError
+ false
+ end
+ end
+
+ def self.sandbox_read_paths(path)
+ Discourse::SafeExec.default_read_paths + [File.realpath(path)]
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/completions/docx_to_text.rb b/plugins/discourse-ai/lib/completions/docx_to_text.rb
new file mode 100644
index 00000000000..d5d699f8d52
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/docx_to_text.rb
@@ -0,0 +1,444 @@
+# frozen_string_literal: true
+
+require "nokogiri"
+require "compression/safe_zip_reader"
+
+module DiscourseAi
+ module Completions
+ class DocxToText
+ MAX_ENTRY_XML_BYTES = 10 * 1024 * 1024
+ MAX_TOTAL_XML_BYTES = 30 * 1024 * 1024
+ MAX_ZIP_ENTRIES = 1_000
+ MAX_EXTRACTED_TEXT_CHARS = 100_001
+ MAX_PARAGRAPHS = 20_000
+
+ SUPPORTED_PARTS = %w[word/document.xml word/footnotes.xml word/endnotes.xml word/comments.xml]
+ IMAGE_ALT_ATTRIBUTES = %w[descr title alt]
+ IMAGE_ALT_NODES = %w[docPr cNvPr shape imagedata]
+
+ class EntryTooLargeError < StandardError
+ end
+
+ class Numbering
+ Level = Struct.new(:num_format, :level_text, :start, keyword_init: true)
+
+ BULLET_FORMAT = "bullet"
+ DEFAULT_START = 1
+ DEFAULT_NUM_FORMAT = "decimal"
+ ROMAN_NUMERALS = {
+ 1000 => "M",
+ 900 => "CM",
+ 500 => "D",
+ 400 => "CD",
+ 100 => "C",
+ 90 => "XC",
+ 50 => "L",
+ 40 => "XL",
+ 10 => "X",
+ 9 => "IX",
+ 5 => "V",
+ 4 => "IV",
+ 1 => "I",
+ }
+
+ def self.from_xml(xml)
+ new.tap { |numbering| numbering.parse(xml) }
+ end
+
+ def initialize
+ @abstract_levels = Hash.new { |hash, key| hash[key] = {} }
+ @num_abstract_ids = {}
+ @num_level_overrides = Hash.new { |hash, key| hash[key] = {} }
+ @counters = Hash.new { |hash, key| hash[key] = {} }
+ end
+
+ def parse(xml)
+ doc = Nokogiri.XML(force_utf8(xml)) { |config| config.recover.nonet }
+ doc.remove_namespaces!
+
+ parse_abstract_levels(doc)
+ parse_numbering_instances(doc)
+ end
+
+ def prefix_for(paragraph)
+ num_pr = paragraph.at_xpath("./pPr/numPr")
+ return if num_pr.blank?
+
+ num_id = num_pr.at_xpath("./numId")&.[]("val")
+ return if num_id.blank?
+
+ ilvl = integer_value(num_pr.at_xpath("./ilvl")&.[]("val"))
+ level = level_for(num_id, ilvl)
+ return if level.blank?
+
+ indent = " " * ilvl
+ if bullet?(level)
+ reset_deeper_counters(num_id, ilvl)
+ "#{indent}#{bullet_for(level)} "
+ else
+ increment_counter(num_id, ilvl, level)
+ "#{indent}#{render_level_text(num_id, ilvl, level)} "
+ end
+ end
+
+ private
+
+ attr_reader :abstract_levels, :num_abstract_ids, :num_level_overrides, :counters
+
+ def parse_abstract_levels(doc)
+ doc
+ .xpath("//abstractNum")
+ .each do |abstract_num|
+ abstract_id = abstract_num["abstractNumId"]
+ next if abstract_id.blank?
+
+ abstract_num
+ .xpath("./lvl")
+ .each do |level_node|
+ ilvl = integer_value(level_node["ilvl"])
+ abstract_levels[abstract_id][ilvl] = level_from_node(level_node)
+ end
+ end
+ end
+
+ def parse_numbering_instances(doc)
+ doc
+ .xpath("//num")
+ .each do |num|
+ num_id = num["numId"]
+ next if num_id.blank?
+
+ abstract_id = num.at_xpath("./abstractNumId")&.[]("val")
+ num_abstract_ids[num_id] = abstract_id if abstract_id.present?
+
+ num
+ .xpath("./lvlOverride")
+ .each do |override_node|
+ ilvl = integer_value(override_node["ilvl"])
+ num_level_overrides[num_id][ilvl] = override_level(num_id, ilvl, override_node)
+ end
+ end
+ end
+
+ def override_level(num_id, ilvl, override_node)
+ start_override = integer_value(override_node.at_xpath("./startOverride")&.[]("val"), nil)
+ override_level_node = override_node.at_xpath("./lvl")
+ level =
+ (
+ if override_level_node
+ level_from_node(override_level_node)
+ else
+ level_for(num_id, ilvl)&.dup
+ end
+ )
+ level ||=
+ Level.new(
+ num_format: DEFAULT_NUM_FORMAT,
+ level_text: "%#{ilvl + 1}.",
+ start: DEFAULT_START,
+ )
+ level.start = start_override if start_override.present?
+ level
+ end
+
+ def level_from_node(level_node)
+ Level.new(
+ num_format: level_node.at_xpath("./numFmt")&.[]("val") || DEFAULT_NUM_FORMAT,
+ level_text: level_node.at_xpath("./lvlText")&.[]("val"),
+ start: integer_value(level_node.at_xpath("./start")&.[]("val"), DEFAULT_START),
+ )
+ end
+
+ def level_for(num_id, ilvl)
+ num_level_overrides.dig(num_id, ilvl) ||
+ abstract_levels.dig(num_abstract_ids[num_id], ilvl)
+ end
+
+ def bullet?(level)
+ level.num_format == BULLET_FORMAT
+ end
+
+ def bullet_for(level)
+ bullet = level.level_text.to_s.strip
+ if bullet.blank? || bullet.match?(/%\d/) || bullet.match?(/\A[\u{F000}-\u{F8FF}]\z/)
+ return "•"
+ end
+
+ bullet
+ end
+
+ def increment_counter(num_id, ilvl, level)
+ list_counters = counters[num_id]
+
+ (0...ilvl).each do |parent_ilvl|
+ parent_level = level_for(num_id, parent_ilvl)
+ next if parent_level.blank? || bullet?(parent_level)
+
+ list_counters[parent_ilvl] ||= parent_level.start || DEFAULT_START
+ end
+
+ list_counters[ilvl] = list_counters.key?(ilvl) ? list_counters[ilvl] + 1 : level.start
+ reset_deeper_counters(num_id, ilvl)
+ end
+
+ def reset_deeper_counters(num_id, ilvl)
+ counters[num_id].keys.each do |counter_ilvl|
+ counters[num_id].delete(counter_ilvl) if counter_ilvl > ilvl
+ end
+ end
+
+ def render_level_text(num_id, ilvl, level)
+ level_text = level.level_text.presence || "%#{ilvl + 1}."
+
+ level_text.gsub(/%(\d+)/) do
+ referenced_ilvl = Regexp.last_match(1).to_i - 1
+ referenced_level = level_for(num_id, referenced_ilvl)
+ value = counters[num_id][referenced_ilvl] || referenced_level&.start || DEFAULT_START
+ format_counter(value, referenced_level&.num_format || DEFAULT_NUM_FORMAT)
+ end
+ end
+
+ def format_counter(value, num_format)
+ case num_format
+ when "decimal"
+ value.to_s
+ when "decimalZero"
+ format("%02d", value)
+ when "lowerLetter"
+ alphabetic_counter(value)
+ when "upperLetter"
+ alphabetic_counter(value).upcase
+ when "lowerRoman"
+ roman_counter(value).downcase
+ when "upperRoman"
+ roman_counter(value)
+ when "ordinal"
+ ordinal_counter(value)
+ else
+ value.to_s
+ end
+ end
+
+ def alphabetic_counter(value)
+ value = value.to_i
+ return value.to_s if value < 1
+
+ text = +""
+ while value.positive?
+ value -= 1
+ text.prepend(("a".ord + (value % 26)).chr)
+ value /= 26
+ end
+ text
+ end
+
+ def roman_counter(value)
+ value = value.to_i
+ return value.to_s if value < 1
+
+ text = +""
+ ROMAN_NUMERALS.each do |number, numeral|
+ while value >= number
+ text << numeral
+ value -= number
+ end
+ end
+ text
+ end
+
+ def ordinal_counter(value)
+ suffix =
+ if (11..13).include?(value % 100)
+ "th"
+ else
+ case value % 10
+ when 1
+ "st"
+ when 2
+ "nd"
+ when 3
+ "rd"
+ else
+ "th"
+ end
+ end
+
+ "#{value}#{suffix}"
+ end
+
+ def integer_value(value, default = 0)
+ value.present? ? value.to_i : default
+ end
+
+ def force_utf8(text)
+ text.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
+ end
+ end
+
+ def self.convert(path)
+ new(path).convert
+ end
+
+ def initialize(path)
+ @path = path
+ end
+
+ def convert
+ part_texts = []
+ extracted_chars = 0
+
+ Compression::SafeZipReader.open(
+ path,
+ max_entries: MAX_ZIP_ENTRIES,
+ max_total_bytes: MAX_TOTAL_XML_BYTES,
+ ) do |zip_file|
+ numbering = numbering_for(zip_file)
+
+ text_entries(zip_file).each do |entry|
+ xml = read_xml_entry(zip_file, entry)
+
+ text = extract_text_from_xml(xml, numbering)
+ next if text.blank?
+
+ part_texts << text
+ extracted_chars += text.length + 2
+ break if extracted_chars > MAX_EXTRACTED_TEXT_CHARS
+ end
+ end
+
+ normalize_document_text(part_texts.join("\n\n"))
+ end
+
+ private
+
+ attr_reader :path
+
+ def numbering_for(zip_file)
+ entry = zip_file.find_entry("word/numbering.xml")
+ return Numbering.new if entry.blank? || entry.directory?
+
+ Numbering.from_xml(read_xml_entry(zip_file, entry))
+ end
+
+ def text_entries(zip_file)
+ zip_file
+ .entries
+ .select { |entry| text_part_name?(entry.name) && !entry.directory? }
+ .sort_by { |entry| part_sort_key(entry.name) }
+ end
+
+ def text_part_name?(name)
+ SUPPORTED_PARTS.include?(name) || name.match?(%r{\Aword/(?:header|footer)\d+\.xml\z})
+ end
+
+ def part_sort_key(name)
+ case name
+ when "word/document.xml"
+ [0, 0]
+ when %r{\Aword/header(\d+)\.xml\z}
+ [1, Regexp.last_match(1).to_i]
+ when %r{\Aword/footer(\d+)\.xml\z}
+ [2, Regexp.last_match(1).to_i]
+ when "word/footnotes.xml"
+ [3, 0]
+ when "word/endnotes.xml"
+ [4, 0]
+ when "word/comments.xml"
+ [5, 0]
+ else
+ [99, name]
+ end
+ end
+
+ def read_xml_entry(zip_file, entry)
+ zip_file.read_entry(entry, max_bytes: MAX_ENTRY_XML_BYTES)
+ rescue Compression::SafeZipReader::EntryTooLargeError => e
+ raise EntryTooLargeError, e.message
+ end
+
+ def extract_text_from_xml(xml, numbering)
+ doc = Nokogiri.XML(force_utf8(xml)) { |config| config.recover.nonet }
+ doc.remove_namespaces!
+
+ paragraphs = doc.xpath("//p")
+ nodes = paragraphs.presence || [doc.root].compact
+
+ text_parts = []
+ extracted_chars = 0
+
+ nodes
+ .first(MAX_PARAGRAPHS)
+ .each do |node|
+ text = normalize_paragraph_text(text_for_node(node))
+ next if text.blank?
+
+ text = "#{numbering.prefix_for(node)}#{text}"
+ text_parts << text
+ extracted_chars += text.length + 1
+ break if extracted_chars > MAX_EXTRACTED_TEXT_CHARS
+ end
+
+ text_parts.join("\n")
+ end
+
+ def text_for_node(node)
+ text = +""
+
+ node.traverse do |child|
+ next if !child.element?
+
+ case child.name
+ when "t"
+ text << child.text
+ when "tab"
+ text << "\t"
+ when "br", "cr"
+ text << "\n"
+ when "drawing", "pict"
+ alt_text = image_alt_text(child)
+ text << "[Image: #{alt_text}]" if alt_text.present?
+ end
+ end
+
+ text
+ end
+
+ def image_alt_text(node)
+ node
+ .xpath(".//*")
+ .select { |child| IMAGE_ALT_NODES.include?(child.name) }
+ .flat_map { |child| IMAGE_ALT_ATTRIBUTES.filter_map { |attribute| child[attribute] } }
+ .map { |text| normalize_inline_text(text) }
+ .reject(&:blank?)
+ .uniq
+ .join(" - ")
+ end
+
+ def normalize_inline_text(text)
+ force_utf8(text).gsub("\u00A0", " ").gsub(/\s+/, " ").strip
+ end
+
+ def normalize_paragraph_text(text)
+ force_utf8(text)
+ .gsub("\u00A0", " ")
+ .gsub(/\r\n?/, "\n")
+ .gsub(/[ \t]+\n/, "\n")
+ .gsub(/\n[ \t]+/, "\n")
+ .rstrip
+ end
+
+ def normalize_document_text(text)
+ force_utf8(text)
+ .gsub("\u00A0", " ")
+ .gsub(/\r\n?/, "\n")
+ .gsub(/[ \t]+\n/, "\n")
+ .gsub(/\n{3,}/, "\n\n")
+ .strip
+ end
+
+ def force_utf8(text)
+ text.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/completions/prompt.rb b/plugins/discourse-ai/lib/completions/prompt.rb
index 3466863e19c..c6d3a3145eb 100644
--- a/plugins/discourse-ai/lib/completions/prompt.rb
+++ b/plugins/discourse-ai/lib/completions/prompt.rb
@@ -151,7 +151,12 @@ module DiscourseAi
tools.present?
end
- def encoded_uploads(message, allow_documents: false, allowed_attachment_types: nil)
+ def encoded_uploads(
+ message,
+ allow_images: true,
+ allow_documents: false,
+ allowed_attachment_types: nil
+ )
if message[:content].is_a?(Array)
upload_ids =
message[:content]
@@ -160,7 +165,10 @@ module DiscourseAi
end
.compact
if !upload_ids.empty?
- allowed_kinds = allow_documents ? %i[image document] : %i[image]
+ allowed_kinds =
+ allowed_upload_kinds(allow_images: allow_images, allow_documents: allow_documents)
+ return [] if allowed_kinds.empty?
+
return(
UploadEncoder.encode(
upload_ids: upload_ids,
@@ -175,8 +183,16 @@ module DiscourseAi
[]
end
- def encode_upload(upload_id, allow_documents: false, allowed_attachment_types: nil)
- allowed_kinds = allow_documents ? %i[image document] : %i[image]
+ def encode_upload(
+ upload_id,
+ allow_images: true,
+ allow_documents: false,
+ allowed_attachment_types: nil
+ )
+ allowed_kinds =
+ allowed_upload_kinds(allow_images: allow_images, allow_documents: allow_documents)
+ return if allowed_kinds.empty?
+
UploadEncoder.encode(
upload_ids: [upload_id],
max_pixels: max_pixels,
@@ -187,6 +203,7 @@ module DiscourseAi
def content_with_encoded_uploads(
content,
+ allow_images: true,
allow_documents: false,
allowed_attachment_types: nil
)
@@ -196,6 +213,7 @@ module DiscourseAi
if c.is_a?(Hash) && c.key?(:upload_id)
encode_upload(
c[:upload_id],
+ allow_images: allow_images,
allow_documents: allow_documents,
allowed_attachment_types: allowed_attachment_types,
)
@@ -222,6 +240,13 @@ module DiscourseAi
private
+ def allowed_upload_kinds(allow_images:, allow_documents:)
+ allowed_kinds = []
+ allowed_kinds << :image if allow_images
+ allowed_kinds << :document if allow_documents
+ allowed_kinds
+ end
+
def validate_message(message)
valid_types = %i[system user model tool tool_call]
if !valid_types.include?(message[:type])
diff --git a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
index ffe4e48d728..1f34fff8357 100644
--- a/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
+++ b/plugins/discourse-ai/lib/completions/prompt_messages_builder.rb
@@ -5,6 +5,7 @@ module DiscourseAi
class PromptMessagesBuilder
MAX_CHAT_UPLOADS = 5
MAX_TOPIC_UPLOADS = 5
+ IMAGE_UPLOAD_EXTENSIONS = %w[jpg jpeg png gif webp].freeze
attr_reader :chat_context_posts
attr_accessor :topic
@@ -13,10 +14,19 @@ module DiscourseAi
channel:,
context_post_ids:,
max_messages:,
- include_uploads:,
+ include_uploads: nil,
+ include_image_uploads: nil,
+ include_document_uploads: nil,
+ allowed_attachment_types: nil,
bot_user_ids:,
instruction_message: nil
)
+ include_image_uploads, include_document_uploads =
+ normalize_upload_inclusion(
+ include_uploads,
+ include_image_uploads,
+ include_document_uploads,
+ )
include_thread_titles = !channel.direct_message_channel? && !message.thread_id
current_id = message.id
@@ -52,7 +62,9 @@ module DiscourseAi
builder.set_chat_context_posts(
context_post_ids,
guardian,
- include_uploads: include_uploads,
+ include_image_uploads: include_image_uploads,
+ include_document_uploads: include_document_uploads,
+ allowed_attachment_types: allowed_attachment_types,
)
end
@@ -63,8 +75,13 @@ module DiscourseAi
if bot_user_ids.include?(m.user_id)
builder.push(type: :model, content: m.message)
else
- upload_ids = nil
- upload_ids = m.uploads.map(&:id) if include_uploads && m.uploads.present?
+ upload_ids =
+ filtered_upload_ids_from_uploads(
+ m.uploads,
+ include_image_uploads: include_image_uploads,
+ include_document_uploads: include_document_uploads,
+ allowed_attachment_types: allowed_attachment_types,
+ )
mapped_message = m.message
thread_title = nil
@@ -91,7 +108,23 @@ module DiscourseAi
)
end
- def self.messages_from_post(post, style: nil, max_posts:, bot_usernames:, include_uploads:)
+ def self.messages_from_post(
+ post,
+ style: nil,
+ max_posts:,
+ bot_usernames:,
+ include_uploads: nil,
+ include_image_uploads: nil,
+ include_document_uploads: nil,
+ allowed_attachment_types: nil
+ )
+ include_image_uploads, include_document_uploads =
+ normalize_upload_inclusion(
+ include_uploads,
+ include_image_uploads,
+ include_document_uploads,
+ )
+
# Pay attention to the `post_number <= ?` here.
# We want to inject the last post as context because they are translated differently.
@@ -155,7 +188,12 @@ module DiscourseAi
context[:id] = username if context[:type] == :user
- context[:upload_ids] = upload_ids.compact if upload_ids.present? && include_uploads
+ context[:upload_ids] = filtered_upload_ids_for_prompt(
+ upload_ids,
+ include_image_uploads: include_image_uploads,
+ include_document_uploads: include_document_uploads,
+ allowed_attachment_types: allowed_attachment_types,
+ )
context[:created_at] = created_at
builder.push(**context)
@@ -165,12 +203,122 @@ module DiscourseAi
builder.to_a(style: style || (post.topic.private_message? ? :bot : :topic))
end
+ def self.normalize_upload_inclusion(
+ include_uploads,
+ include_image_uploads,
+ include_document_uploads
+ )
+ include_image_uploads = include_uploads if include_image_uploads.nil?
+ include_document_uploads = include_uploads if include_document_uploads.nil?
+
+ [!!include_image_uploads, !!include_document_uploads]
+ end
+
+ def self.filtered_upload_ids_for_prompt(
+ upload_ids,
+ include_image_uploads:,
+ include_document_uploads:,
+ allowed_attachment_types: nil
+ )
+ return if !include_image_uploads && !include_document_uploads
+ return if upload_ids.blank?
+
+ upload_ids = Array(upload_ids).compact.map(&:to_i)
+ uploads_by_id = Upload.where(id: upload_ids).index_by(&:id)
+
+ filtered_upload_ids =
+ upload_ids.select do |upload_id|
+ upload = uploads_by_id[upload_id]
+ upload &&
+ upload_allowed_for_prompt?(
+ upload,
+ include_image_uploads: include_image_uploads,
+ include_document_uploads: include_document_uploads,
+ allowed_attachment_types: allowed_attachment_types,
+ )
+ end
+
+ filtered_upload_ids.presence
+ end
+
+ def self.filtered_upload_ids_from_uploads(
+ uploads,
+ include_image_uploads:,
+ include_document_uploads:,
+ allowed_attachment_types: nil
+ )
+ return if !include_image_uploads && !include_document_uploads
+ return if uploads.blank?
+
+ uploads
+ .select do |upload|
+ upload_allowed_for_prompt?(
+ upload,
+ include_image_uploads: include_image_uploads,
+ include_document_uploads: include_document_uploads,
+ allowed_attachment_types: allowed_attachment_types,
+ )
+ end
+ .map(&:id)
+ .presence
+ end
+
+ def self.upload_allowed_for_prompt?(
+ upload,
+ include_image_uploads:,
+ include_document_uploads:,
+ allowed_attachment_types: nil
+ )
+ if image_upload?(upload)
+ include_image_uploads
+ else
+ include_document_uploads &&
+ document_upload_allowed_for_prompt?(upload, allowed_attachment_types)
+ end
+ end
+
+ def self.document_upload_allowed_for_prompt?(upload, allowed_attachment_types)
+ allowed_attachment_types = LlmModel.normalize_attachment_types(allowed_attachment_types)
+ return false if allowed_attachment_types.blank?
+
+ mime_type =
+ MiniMime.lookup_by_filename(upload.original_filename)&.content_type ||
+ "application/octet-stream"
+ attachment_type =
+ DiscourseAi::Completions::UploadEncoder.attachment_type_for(upload.extension, mime_type)
+ allowed_attachment_types.include?(attachment_type)
+ end
+
+ def self.image_upload?(upload)
+ extension = upload.extension.to_s.delete_prefix(".").downcase
+ return true if IMAGE_UPLOAD_EXTENSIONS.include?(extension)
+
+ filename = upload.original_filename.to_s
+ filename = "upload.#{extension}" if File.extname(filename).blank? && extension.present?
+
+ MiniMime.lookup_by_filename(filename)&.content_type.to_s.start_with?("image/")
+ end
+
def initialize
@raw_messages = []
@timestamps = {}
end
- def set_chat_context_posts(post_ids, guardian, include_uploads:)
+ def set_chat_context_posts(
+ post_ids,
+ guardian,
+ include_uploads: nil,
+ include_image_uploads: nil,
+ include_document_uploads: nil,
+ allowed_attachment_types: nil
+ )
+ include_image_uploads, include_document_uploads =
+ self.class.normalize_upload_inclusion(
+ include_uploads,
+ include_image_uploads,
+ include_document_uploads,
+ )
+
posts = []
Post
.where(id: post_ids)
@@ -186,9 +334,14 @@ module DiscourseAi
posts.each do |post|
posts_context << "url: #{post.url}\n"
posts_context << "#{post.username}: #{post.raw}\n\n"
- if include_uploads
- post.uploads.each { |upload| posts_context << { upload_id: upload.id } }
- end
+ upload_ids =
+ self.class.filtered_upload_ids_from_uploads(
+ post.uploads,
+ include_image_uploads: include_image_uploads,
+ include_document_uploads: include_document_uploads,
+ allowed_attachment_types: allowed_attachment_types,
+ )
+ upload_ids&.each { |upload_id| posts_context << { upload_id: upload_id } }
end
posts_context << "}}}"
@chat_context_posts = posts_context
diff --git a/plugins/discourse-ai/lib/completions/rtf_to_text.rb b/plugins/discourse-ai/lib/completions/rtf_to_text.rb
new file mode 100644
index 00000000000..49d390cf2d3
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/rtf_to_text.rb
@@ -0,0 +1,404 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Completions
+ class RtfToText
+ MAX_INPUT_BYTES = 2 * 1024 * 1024
+ MAX_EXTRACTED_TEXT_CHARS = 100_001
+ MAX_GROUP_DEPTH = 100
+ MAX_CONTROL_WORD_CHARS = 64
+
+ GroupState = Struct.new(:skip, :uc, keyword_init: true)
+
+ DESTINATION_CONTROL_WORDS = %w[
+ annotation
+ atnauthor
+ atntime
+ author
+ buptim
+ category
+ colorschememapping
+ colortbl
+ comment
+ company
+ creatim
+ datafield
+ datastore
+ doccomm
+ docvar
+ factoidname
+ falt
+ filetbl
+ fldinst
+ fonttbl
+ footer
+ footerf
+ footerl
+ footerr
+ footnote
+ formfield
+ generator
+ header
+ headerf
+ headerl
+ headerr
+ info
+ keywords
+ latentstyles
+ listoverridetable
+ listtable
+ manager
+ nextfile
+ nonshppict
+ object
+ objdata
+ operator
+ pict
+ pn
+ pnseclvl
+ private
+ protusertbl
+ revtbl
+ rsidtbl
+ shp
+ shpgrp
+ shpinst
+ stylesheet
+ subject
+ template
+ title
+ txe
+ userprops
+ xmlnstbl
+ ].freeze
+
+ CONTROL_TEXT = {
+ "bullet" => "•",
+ "cell" => "\t",
+ "column" => "\t",
+ "emdash" => "—",
+ "emspace" => " ",
+ "endash" => "–",
+ "enspace" => " ",
+ "ldblquote" => "“",
+ "line" => "\n",
+ "lquote" => "‘",
+ "page" => "\n",
+ "par" => "\n",
+ "qmspace" => " ",
+ "rdblquote" => "”",
+ "row" => "\n",
+ "rquote" => "’",
+ "sect" => "\n",
+ "tab" => "\t",
+ }.freeze
+
+ ENCODING_BY_CODEPAGE = {
+ 437 => "IBM437",
+ 850 => "IBM850",
+ 852 => "IBM852",
+ 855 => "IBM855",
+ 857 => "IBM857",
+ 860 => "IBM860",
+ 861 => "IBM861",
+ 862 => "IBM862",
+ 863 => "IBM863",
+ 864 => "IBM864",
+ 865 => "IBM865",
+ 866 => "IBM866",
+ 869 => "IBM869",
+ 874 => "Windows-874",
+ 932 => "Windows-31J",
+ 936 => "GBK",
+ 949 => "Windows-949",
+ 950 => "Big5",
+ 1250 => "Windows-1250",
+ 1251 => "Windows-1251",
+ 1252 => "Windows-1252",
+ 1253 => "Windows-1253",
+ 1254 => "Windows-1254",
+ 1255 => "Windows-1255",
+ 1256 => "Windows-1256",
+ 1257 => "Windows-1257",
+ 1258 => "Windows-1258",
+ 65_001 => "UTF-8",
+ 20_127 => "US-ASCII",
+ 20_850 => "IBM850",
+ 28_591 => "ISO-8859-1",
+ }.freeze
+
+ class ParseLimitError < StandardError
+ end
+
+ def self.convert(path)
+ new(path).convert
+ end
+
+ def initialize(path)
+ @path = path
+ end
+
+ def convert
+ @input = read_input
+ @index = 0
+ @output = +""
+ @group_stack = [GroupState.new(skip: false, uc: 1)]
+ @source_encoding = find_encoding("Windows-1252")
+ @fallback_chars_to_skip = 0
+
+ parse
+ normalize_document_text(@output)
+ end
+
+ private
+
+ attr_reader :path
+
+ def read_input
+ input = File.binread(path, MAX_INPUT_BYTES + 1).to_s
+ input = input.byteslice(0, MAX_INPUT_BYTES) if input.bytesize > MAX_INPUT_BYTES
+ input.force_encoding(Encoding::BINARY)
+ end
+
+ def parse
+ while @index < @input.bytesize && @output.length <= MAX_EXTRACTED_TEXT_CHARS
+ if @fallback_chars_to_skip.positive?
+ skip_fallback_char
+ @fallback_chars_to_skip -= 1
+ next
+ end
+
+ byte = current_byte
+
+ case byte
+ when 123 # {
+ push_group
+ when 125 # }
+ pop_group
+ when 92 # \
+ parse_control
+ when 10, 13
+ @index += 1
+ else
+ append_encoded_byte(byte) if !current_group.skip
+ @index += 1
+ end
+ end
+ end
+
+ def push_group
+ if @group_stack.length >= MAX_GROUP_DEPTH
+ raise ParseLimitError, "RTF group nesting is too deep"
+ end
+
+ @group_stack << GroupState.new(skip: current_group.skip, uc: current_group.uc)
+ @index += 1
+ end
+
+ def pop_group
+ @group_stack.pop if @group_stack.length > 1
+ @index += 1
+ end
+
+ def current_group
+ @group_stack.last
+ end
+
+ def current_byte
+ @input.getbyte(@index)
+ end
+
+ def parse_control
+ @index += 1
+ return if @index >= @input.bytesize
+
+ byte = current_byte
+
+ if letter?(byte)
+ parse_control_word
+ elsif byte == 39 # '
+ parse_hex_escape
+ else
+ parse_control_symbol(byte)
+ end
+ end
+
+ def parse_control_word
+ word = read_control_word
+ param = read_control_parameter
+ skip_control_space
+
+ handle_control_word(word, param)
+ end
+
+ def read_control_word
+ stored_length = 0
+ word = +""
+
+ while @index < @input.bytesize && letter?(current_byte)
+ word << current_byte.chr if stored_length < MAX_CONTROL_WORD_CHARS
+ stored_length += 1
+ @index += 1
+ end
+
+ word.downcase
+ end
+
+ def read_control_parameter
+ sign = 1
+ if current_byte == 45 # -
+ sign = -1
+ @index += 1
+ end
+
+ start = @index
+ @index += 1 while @index < @input.bytesize && digit?(current_byte)
+ return if start == @index
+
+ @input.byteslice(start, @index - start).to_i * sign
+ end
+
+ def skip_control_space
+ @index += 1 if current_byte == 32
+ end
+
+ def handle_control_word(word, param)
+ if DESTINATION_CONTROL_WORDS.include?(word)
+ current_group.skip = true
+ return
+ end
+
+ case word
+ when "ansi"
+ @source_encoding = find_encoding("Windows-1252")
+ when "mac"
+ @source_encoding = find_encoding("MacRoman")
+ when "pc"
+ @source_encoding = find_encoding("IBM437")
+ when "pca"
+ @source_encoding = find_encoding("IBM850")
+ when "ansicpg"
+ @source_encoding = encoding_for_codepage(param) if param
+ when "uc"
+ current_group.uc = param.clamp(0, 10) if param
+ when "bin"
+ @index += [param.to_i, @input.bytesize - @index].min if param.to_i.positive?
+ end
+
+ return if current_group.skip
+
+ if word == "u"
+ append_unicode(param)
+ @fallback_chars_to_skip = current_group.uc.to_i
+ elsif (text = CONTROL_TEXT[word])
+ append_text(text)
+ end
+ end
+
+ def parse_hex_escape
+ @index += 1
+ hex = @input.byteslice(@index, 2)
+ if hex&.match?(/\A[0-9a-fA-F]{2}\z/)
+ append_encoded_byte(hex.to_i(16)) if !current_group.skip
+ @index += 2
+ end
+ end
+
+ def parse_control_symbol(byte)
+ @index += 1
+
+ case byte
+ when 42 # *
+ current_group.skip = true
+ when 45,
+ 95 # -, _
+ append_text("-") if !current_group.skip
+ when 92,
+ 123,
+ 125 # \, {, }
+ append_text(byte.chr) if !current_group.skip
+ when 126 # ~
+ append_text(" ") if !current_group.skip
+ end
+ end
+
+ def skip_fallback_char
+ return if @index >= @input.bytesize
+
+ if current_byte == 92 # \
+ @index += 1
+ return if @index >= @input.bytesize
+
+ if current_byte == 39 # '
+ @index += 1
+ @index += 2 if @input.byteslice(@index, 2)&.match?(/\A[0-9a-fA-F]{2}\z/)
+ elsif letter?(current_byte)
+ read_control_word
+ read_control_parameter
+ skip_control_space
+ else
+ @index += 1
+ end
+ else
+ @index += 1
+ end
+ end
+
+ def append_unicode(param)
+ return if param.nil?
+
+ codepoint = param.negative? ? param + 65_536 : param
+ append_text([codepoint].pack("U")) if codepoint.between?(0, 0x10FFFF)
+ rescue RangeError
+ nil
+ end
+
+ def append_encoded_byte(byte)
+ return if byte.nil? || byte == 0
+
+ if byte < 128
+ append_text(byte.chr)
+ else
+ text = byte.chr.b.force_encoding(@source_encoding || Encoding::Windows_1252)
+ append_text(text.encode("UTF-8", invalid: :replace, undef: :replace, replace: ""))
+ end
+ end
+
+ def append_text(text)
+ return if text.nil? || text.empty? || @output.length > MAX_EXTRACTED_TEXT_CHARS
+
+ @output << text
+ end
+
+ def encoding_for_codepage(codepage)
+ find_encoding(ENCODING_BY_CODEPAGE[codepage.to_i]) || @source_encoding
+ end
+
+ def find_encoding(name)
+ Encoding.find(name)
+ rescue ArgumentError
+ nil
+ end
+
+ def letter?(byte)
+ (byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122)
+ end
+
+ def digit?(byte)
+ byte >= 48 && byte <= 57
+ end
+
+ def normalize_document_text(text)
+ text
+ .to_s
+ .encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
+ .gsub("\u00A0", " ")
+ .gsub(/\r\n?/, "\n")
+ .gsub(/[ \t]+\n/, "\n")
+ .gsub(/\n[ \t]+/, "\n")
+ .gsub(/\n{3,}/, "\n\n")
+ .strip
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/completions/upload_encoder.rb b/plugins/discourse-ai/lib/completions/upload_encoder.rb
index 528f343fb22..c4b64b525b2 100644
--- a/plugins/discourse-ai/lib/completions/upload_encoder.rb
+++ b/plugins/discourse-ai/lib/completions/upload_encoder.rb
@@ -28,7 +28,7 @@ module DiscourseAi
attachment_type = attachment_type_for(upload.extension, mime_type)
next if disallowed_attachment?(allowed_attachment_types, attachment_type)
- payload = encode_document(upload, mime_type)
+ payload = encode_document(upload, mime_type, attachment_type)
uploads << payload if payload
next
end
@@ -49,17 +49,25 @@ module DiscourseAi
uploads
end
+ MAX_EXTRACTED_DOCUMENT_TEXT_CHARS = 100_000
+ MAX_TEXT_FILE_BYTES = 1 * 1024 * 1024
+ MAX_RAW_DOCUMENT_BYTES = 10 * 1024 * 1024
+ RAW_DOCUMENT_ATTACHMENT_TYPES = %w[pdf]
+
def self.attachment_type_for(extension, mime_type)
ext = extension.to_s.delete_prefix(".").downcase
- mime = mime_type.to_s
+ mime = mime_type.to_s.downcase
return "pdf" if ext == "pdf" || mime.include?("pdf")
- return "docx" if ext == "docx"
- return "doc" if ext == "doc"
+ return "docx" if ext == "docx" || mime.include?("wordprocessingml.document")
+ return "doc" if ext == "doc" || mime == "application/msword"
+ return "xlsx" if ext == "xlsx" || mime.include?("spreadsheetml.sheet")
+ return "xls" if ext == "xls" || mime == "application/vnd.ms-excel"
+ return "csv" if ext == "csv" || mime.include?("text/csv") || mime.include?("csv")
return "txt" if ext == "txt" || mime.include?("text/plain")
- return "rtf" if ext == "rtf"
+ return "rtf" if ext == "rtf" || mime.include?("rtf")
return "html" if %w[html htm].include?(ext) || mime.include?("html")
- return "markdown" if %w[md markdown].include?(ext) || mime.include?("markdown")
+ return "md" if %w[md markdown].include?(ext) || mime.include?("markdown")
"file"
end
@@ -70,7 +78,7 @@ module DiscourseAi
def normalize_attachment_types(types)
return nil if types.nil?
- Array(types).map(&:downcase)
+ LlmModel.normalize_attachment_types(types)
end
def disallowed_attachment?(allowed_types, attachment_type)
@@ -81,18 +89,227 @@ module DiscourseAi
%w[jpg jpeg png gif webp].include?(ext)
end
- def encode_document(upload, mime_type)
+ def encode_document(upload, mime_type, attachment_type)
path = fetch_path(upload)
return if path.blank?
- encoded = Base64.strict_encode64(File.binread(path))
+ if attachment_type == "doc"
+ text_payload = doc_to_text_payload(upload, path)
+ return text_payload if text_payload
+ elsif attachment_type == "docx"
+ text_payload = docx_to_text_payload(upload, path)
+ return text_payload if text_payload
+ elsif attachment_type == "xls"
+ text_payload = xls_to_text_payload(upload, path)
+ return text_payload if text_payload
+ elsif attachment_type == "xlsx"
+ text_payload = xlsx_to_text_payload(upload, path)
+ return text_payload if text_payload
+ elsif attachment_type == "rtf"
+ text_payload = rtf_to_text_payload(upload, path)
+ return text_payload if text_payload
+ elsif %w[csv md txt].include?(attachment_type)
+ text_payload = text_file_payload(upload, path, attachment_type)
+ return text_payload if text_payload
+ end
+
+ raw_document_payload(upload, path, mime_type, attachment_type)
+ end
+
+ def raw_document_payload(upload, path, mime_type, attachment_type)
+ if RAW_DOCUMENT_ATTACHMENT_TYPES.exclude?(attachment_type)
+ log_document_upload_skip(
+ upload,
+ attachment_type,
+ "raw upload is not supported for this attachment type; it must be converted to text",
+ )
+ return
+ end
+
+ bytesize = File.size(path)
+ if bytesize > MAX_RAW_DOCUMENT_BYTES
+ log_document_upload_skip(
+ upload,
+ attachment_type,
+ "raw upload size #{human_filesize(bytesize)} exceeds the #{human_filesize(MAX_RAW_DOCUMENT_BYTES)} limit",
+ )
+ return
+ end
{
- base64: encoded,
+ base64: Base64.strict_encode64(File.binread(path)),
mime_type: mime_type,
kind: :document,
filename: upload.original_filename,
}
+ rescue SystemCallError => e
+ log_document_upload_skip(upload, attachment_type, "#{e.class}: #{e.message}")
+ nil
+ end
+
+ def doc_to_text_payload(upload, path)
+ text = normalize_extracted_text(DiscourseAi::Completions::DocToText.convert(path))
+
+ if text.blank?
+ log_document_conversion_failure(upload, "doc", "DOC converter returned blank output")
+ return
+ end
+
+ text_document_payload(upload, path, text, converted_from: "doc")
+ rescue StandardError => e
+ log_document_conversion_failure(upload, "doc", "#{e.class}: #{e.message}")
+ nil
+ end
+
+ def docx_to_text_payload(upload, path)
+ text = normalize_extracted_text(DiscourseAi::Completions::DocxToText.convert(path))
+
+ if text.blank?
+ log_document_conversion_failure(upload, "docx", "DOCX converter returned blank output")
+ return
+ end
+
+ text_document_payload(upload, path, text, converted_from: "docx")
+ rescue StandardError => e
+ log_document_conversion_failure(upload, "docx", "#{e.class}: #{e.message}")
+ nil
+ end
+
+ def xls_to_text_payload(upload, path)
+ text = normalize_extracted_text(DiscourseAi::Completions::XlsToText.convert(path))
+
+ if text.blank?
+ log_document_conversion_failure(upload, "xls", "XLS converter returned blank output")
+ return
+ end
+
+ text_document_payload(upload, path, text, converted_from: "xls")
+ rescue StandardError => e
+ log_document_conversion_failure(upload, "xls", "#{e.class}: #{e.message}")
+ nil
+ end
+
+ def xlsx_to_text_payload(upload, path)
+ text = normalize_extracted_text(DiscourseAi::Completions::XlsxToText.convert(path))
+
+ if text.blank?
+ log_document_conversion_failure(upload, "xlsx", "XLSX converter returned blank output")
+ return
+ end
+
+ text_document_payload(upload, path, text, converted_from: "xlsx")
+ rescue StandardError => e
+ log_document_conversion_failure(upload, "xlsx", "#{e.class}: #{e.message}")
+ nil
+ end
+
+ def text_file_payload(upload, path, attachment_type)
+ text = normalize_extracted_text(read_utf8_text_file(path))
+
+ if text.blank?
+ log_document_conversion_failure(upload, attachment_type, "text file was blank")
+ return
+ end
+
+ text_document_payload(upload, path, text, converted_from: attachment_type)
+ rescue SystemCallError => e
+ log_document_conversion_failure(upload, attachment_type, "#{e.class}: #{e.message}")
+ nil
+ end
+
+ def rtf_to_text_payload(upload, path)
+ text = normalize_extracted_text(DiscourseAi::Completions::RtfToText.convert(path))
+
+ if text.blank?
+ log_document_conversion_failure(upload, "rtf", "RTF converter returned blank output")
+ return
+ end
+
+ text_document_payload(upload, path, text, converted_from: "rtf")
+ rescue StandardError => e
+ log_document_conversion_failure(upload, "rtf", "#{e.class}: #{e.message}")
+ nil
+ end
+
+ def read_utf8_text_file(path)
+ text = +""
+ truncated = false
+
+ File.open(path, "rb") do |file|
+ text = file.read(MAX_TEXT_FILE_BYTES + 1).to_s
+ if text.bytesize > MAX_TEXT_FILE_BYTES
+ text = text.byteslice(0, MAX_TEXT_FILE_BYTES)
+ truncated = true
+ end
+ end
+
+ text = text.delete_prefix("\xEF\xBB\xBF".b)
+ text.force_encoding("UTF-8")
+ text = text.encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
+
+ if truncated
+ text << "\n\n[Document text truncated after #{human_filesize(MAX_TEXT_FILE_BYTES)}.]"
+ end
+
+ text
+ end
+
+ def normalize_extracted_text(output)
+ output.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: "").strip
+ end
+
+ def truncate_extracted_text(text)
+ return text if text.length <= MAX_EXTRACTED_DOCUMENT_TEXT_CHARS
+
+ text.first(MAX_EXTRACTED_DOCUMENT_TEXT_CHARS) +
+ "\n\n[Document text truncated after #{MAX_EXTRACTED_DOCUMENT_TEXT_CHARS} characters.]"
+ end
+
+ def text_document_payload(upload, path, text, converted_from:)
+ {
+ kind: :document,
+ filename: upload.original_filename,
+ mime_type: "text/plain",
+ text: document_text_preamble(upload, path) + truncate_extracted_text(text),
+ converted_from: converted_from,
+ }
+ end
+
+ def document_text_preamble(upload, path)
+ filename = upload.original_filename.presence || "document"
+ filesize = upload.filesize || File.size(path)
+ "Uploaded document: #{filename} (#{human_filesize(filesize)})\n\n"
+ end
+
+ def human_filesize(bytes)
+ bytes = bytes.to_i
+ units = %w[Bytes KB MB GB TB]
+ size = bytes.to_f
+ unit = units.shift
+
+ while size >= 1024 && units.any?
+ size /= 1024.0
+ unit = units.shift
+ end
+
+ return "#{bytes} #{bytes == 1 ? "Byte" : "Bytes"}" if unit == "Bytes"
+
+ formatted_size = size >= 10 ? size.round.to_s : format("%.1f", size).sub(/\.0\z/, "")
+ "#{formatted_size} #{unit}"
+ end
+
+ def log_document_conversion_failure(upload, extension, message)
+ Rails.logger.warn(
+ "Discourse AI: Failed to convert .#{extension} upload to text " \
+ "(upload_id=#{upload.id}, filename=#{upload.original_filename.inspect}): #{message}",
+ )
+ end
+
+ def log_document_upload_skip(upload, extension, message)
+ Rails.logger.warn(
+ "Discourse AI: Skipping .#{extension} upload " \
+ "(upload_id=#{upload.id}, filename=#{upload.original_filename.inspect}): #{message}",
+ )
end
def encode_image(upload, desired_extension, max_pixels)
diff --git a/plugins/discourse-ai/lib/completions/xls_to_text.rb b/plugins/discourse-ai/lib/completions/xls_to_text.rb
new file mode 100644
index 00000000000..282f9c5b151
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/xls_to_text.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+require "discourse/safe_exec"
+
+module DiscourseAi
+ module Completions
+ class XlsToText
+ XLS2CSV_TIMEOUT_SECONDS = 5
+ MAX_CONVERSION_OUTPUT_BYTES = 4 * 100_001
+ SAFE_EXEC_ENV = { "PATH" => ENV["PATH"].to_s }.freeze
+ XLS2CSV_RLIMITS = {
+ cpu_seconds: XLS2CSV_TIMEOUT_SECONDS,
+ memory_bytes: 256 * 1024 * 1024,
+ file_size_bytes: 1 * 1024 * 1024,
+ open_files: 64,
+ processes: 0,
+ }
+
+ def self.convert(path)
+ return if !xls2csv_installed?
+
+ Discourse::SafeExec.capture(
+ "xls2csv",
+ path,
+ read: sandbox_read_paths(path),
+ execute: Discourse::SafeExec.default_execute_paths,
+ timeout: XLS2CSV_TIMEOUT_SECONDS,
+ env: SAFE_EXEC_ENV,
+ unsetenv_others: true,
+ rlimits: XLS2CSV_RLIMITS,
+ seccomp_deny_network: true,
+ max_output_bytes: MAX_CONVERSION_OUTPUT_BYTES,
+ truncate_output: true,
+ failure_message: "Failed to convert .xls upload to text",
+ )
+ end
+
+ def self.xls2csv_installed?
+ return @xls2csv_installed if defined?(@xls2csv_installed)
+
+ @xls2csv_installed =
+ begin
+ Discourse::Utils.execute_command("which", "xls2csv")
+ true
+ rescue Discourse::Utils::CommandError
+ false
+ end
+ end
+
+ def self.sandbox_read_paths(path)
+ Discourse::SafeExec.default_read_paths + [File.realpath(path)]
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/completions/xlsx_to_text.rb b/plugins/discourse-ai/lib/completions/xlsx_to_text.rb
new file mode 100644
index 00000000000..69aad720533
--- /dev/null
+++ b/plugins/discourse-ai/lib/completions/xlsx_to_text.rb
@@ -0,0 +1,264 @@
+# frozen_string_literal: true
+
+require "nokogiri"
+require "pathname"
+require "compression/safe_zip_reader"
+
+module DiscourseAi
+ module Completions
+ class XlsxToText
+ MAX_ENTRY_XML_BYTES = 10 * 1024 * 1024
+ MAX_TOTAL_XML_BYTES = 30 * 1024 * 1024
+ MAX_ZIP_ENTRIES = 1_000
+ MAX_EXTRACTED_TEXT_CHARS = 100_001
+ MAX_SHEETS = 50
+ MAX_ROWS_PER_SHEET = 10_000
+ MAX_COLUMNS = 200
+
+ WORKBOOK_PATH = "xl/workbook.xml"
+ WORKBOOK_RELS_PATH = "xl/_rels/workbook.xml.rels"
+ SHARED_STRINGS_PATH = "xl/sharedStrings.xml"
+
+ Sheet = Struct.new(:name, :path, keyword_init: true)
+
+ class EntryTooLargeError < StandardError
+ end
+
+ def self.convert(path)
+ new(path).convert
+ end
+
+ def initialize(path)
+ @path = path
+ end
+
+ def convert
+ Compression::SafeZipReader.open(
+ path,
+ max_entries: MAX_ZIP_ENTRIES,
+ max_total_bytes: MAX_TOTAL_XML_BYTES,
+ ) do |zip_file|
+ shared_strings = shared_strings_for(zip_file)
+ sheets = sheets_for(zip_file)
+
+ sheet_texts = []
+ extracted_chars = 0
+
+ sheets
+ .first(MAX_SHEETS)
+ .each do |sheet|
+ entry = zip_file.find_entry(sheet.path)
+ next if entry.blank? || entry.directory?
+
+ xml = read_xml_entry(zip_file, entry)
+
+ text = sheet_text(xml, shared_strings)
+ next if text.blank?
+
+ sheet_text = "Sheet: #{sheet.name}\n\n#{text}"
+ sheet_texts << sheet_text
+ extracted_chars += sheet_text.length + 2
+ break if extracted_chars > MAX_EXTRACTED_TEXT_CHARS
+ end
+
+ normalize_document_text(sheet_texts.join("\n\n"))
+ end
+ end
+
+ private
+
+ attr_reader :path
+
+ def shared_strings_for(zip_file)
+ entry = zip_file.find_entry(SHARED_STRINGS_PATH)
+ return [] if entry.blank? || entry.directory?
+
+ parse_shared_strings(read_xml_entry(zip_file, entry))
+ end
+
+ def parse_shared_strings(xml)
+ doc = Nokogiri.XML(force_utf8(xml)) { |config| config.recover.nonet }
+ doc.remove_namespaces!
+
+ doc.xpath("//sst/si").map { |si| normalize_inline_text(string_item_text(si)) }
+ end
+
+ def sheets_for(zip_file)
+ workbook_entry = zip_file.find_entry(WORKBOOK_PATH)
+ return fallback_sheets(zip_file) if workbook_entry.blank? || workbook_entry.directory?
+
+ rels = {}
+ rels_entry = zip_file.find_entry(WORKBOOK_RELS_PATH)
+ if rels_entry.present? && !rels_entry.directory?
+ rels = workbook_relationships(read_xml_entry(zip_file, rels_entry))
+ end
+
+ workbook_xml = read_xml_entry(zip_file, workbook_entry)
+
+ sheets = parse_workbook_sheets(workbook_xml, rels)
+ sheets = fallback_sheets(zip_file) if sheets.empty?
+ sheets
+ end
+
+ def workbook_relationships(xml)
+ doc = Nokogiri.XML(force_utf8(xml)) { |config| config.recover.nonet }
+ doc.remove_namespaces!
+
+ doc
+ .xpath("//Relationship")
+ .each_with_object({}) do |relationship, memo|
+ id = relationship["Id"]
+ target = relationship["Target"]
+ memo[id] = workbook_target_path(target) if id.present? && target.present?
+ end
+ end
+
+ def parse_workbook_sheets(xml, rels)
+ doc = Nokogiri.XML(force_utf8(xml)) { |config| config.recover.nonet }
+ doc.remove_namespaces!
+
+ doc
+ .xpath("//sheets/sheet")
+ .filter_map do |sheet|
+ rel_id = sheet["id"]
+ sheet_path = rels[rel_id]
+ next if sheet_path.blank?
+
+ Sheet.new(
+ name: sheet["name"].presence || File.basename(sheet_path, ".xml"),
+ path: sheet_path,
+ )
+ end
+ end
+
+ def fallback_sheets(zip_file)
+ zip_file
+ .entries
+ .select do |entry|
+ entry.name.match?(%r{\Axl/worksheets/sheet\d+\.xml\z}) && !entry.directory?
+ end
+ .sort_by { |entry| entry.name.scan(/\d+/).last.to_i }
+ .map
+ .with_index { |entry, index| Sheet.new(name: "Sheet#{index + 1}", path: entry.name) }
+ end
+
+ def workbook_target_path(target)
+ normalized_target = target.delete_prefix("/")
+ normalized_target = File.join("xl", normalized_target) if !normalized_target.start_with?(
+ "xl/",
+ )
+ Pathname.new(normalized_target).cleanpath.to_s
+ end
+
+ def read_xml_entry(zip_file, entry)
+ zip_file.read_entry(entry, max_bytes: MAX_ENTRY_XML_BYTES)
+ rescue Compression::SafeZipReader::EntryTooLargeError => e
+ raise EntryTooLargeError, e.message
+ end
+
+ def sheet_text(xml, shared_strings)
+ doc = Nokogiri.XML(force_utf8(xml)) { |config| config.recover.nonet }
+ doc.remove_namespaces!
+
+ rows = doc.xpath("//sheetData/row")
+ return if rows.blank?
+
+ row_texts = []
+ extracted_chars = 0
+
+ rows
+ .first(MAX_ROWS_PER_SHEET)
+ .each do |row|
+ text = row_text(row, shared_strings)
+ next if text.blank?
+
+ row_texts << text
+ extracted_chars += text.length + 1
+ break if extracted_chars > MAX_EXTRACTED_TEXT_CHARS
+ end
+
+ row_texts.join("\n")
+ end
+
+ def row_text(row, shared_strings)
+ cells = row.xpath("./c")
+ return if cells.blank?
+
+ values = []
+ next_column_index = 0
+
+ cells.each do |cell|
+ column_index = column_index_for(cell["r"]) || next_column_index
+ next_column_index = column_index + 1
+ next if column_index >= MAX_COLUMNS
+
+ values.concat([""] * (column_index - values.length)) if column_index > values.length
+ values[column_index] = cell_text(cell, shared_strings)
+ end
+
+ values.pop while values.last.blank?
+ values.join("\t")
+ end
+
+ def cell_text(cell, shared_strings)
+ type = cell["t"]
+ value = cell.at_xpath("./v")&.text
+
+ text =
+ case type
+ when "s"
+ shared_strings[value.to_i] if value.present?
+ when "inlineStr"
+ string_item_text(cell.at_xpath("./is"))
+ when "b"
+ value.to_s == "1" ? "TRUE" : "FALSE" if value.present?
+ when "str", "e"
+ value
+ else
+ value.presence || formula_text(cell)
+ end
+
+ normalize_inline_text(text)
+ end
+
+ def formula_text(cell)
+ formula = cell.at_xpath("./f")&.text
+ formula.present? ? "=#{formula}" : nil
+ end
+
+ def string_item_text(node)
+ return if node.blank?
+
+ direct_text = node.xpath("./t").map(&:text).join
+ return direct_text if direct_text.present?
+
+ node.xpath("./r/t").map(&:text).join
+ end
+
+ def column_index_for(reference)
+ column_name = reference.to_s[/\A[A-Z]+/i]
+ return if column_name.blank?
+
+ column_name.upcase.each_byte.reduce(0) { |index, char| (index * 26) + char - "A".ord + 1 } -
+ 1
+ end
+
+ def normalize_inline_text(text)
+ force_utf8(text).gsub("\u00A0", " ").gsub(/[ \t\r\n]+/, " ").strip
+ end
+
+ def normalize_document_text(text)
+ force_utf8(text)
+ .gsub("\u00A0", " ")
+ .gsub(/\r\n?/, "\n")
+ .gsub(/[ \t]+\n/, "\n")
+ .gsub(/\n{3,}/, "\n\n")
+ .strip
+ end
+
+ def force_utf8(text)
+ text.to_s.encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb b/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb
index 83c7a2a2bf7..0fb4ba246e4 100644
--- a/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb
+++ b/plugins/discourse-ai/spec/lib/automation/llm_tagger_spec.rb
@@ -85,6 +85,36 @@ RSpec.describe DiscourseAi::Automation::LlmTagger do
expect(topic.reload.tags.map(&:name)).to include("bug")
end
+ it "includes document uploads independently from image uploads" do
+ ai_agent.update!(vision_enabled: false)
+ llm_model.update!(allowed_attachment_types: ["txt"])
+ SiteSetting.authorized_extensions = "*"
+ image_upload = Fabricate(:image_upload, posts: [post])
+ document_upload = Fabricate(:upload, original_filename: "notes.txt", extension: "txt")
+ UploadReference.create!(target: post, upload: document_upload)
+
+ mock_response = { "tags" => ["bug"], "confidence" => 90 }.to_json
+
+ DiscourseAi::Completions::Llm.with_prepared_responses([mock_response]) do
+ described_class.handle(
+ post: post.reload,
+ tagger_agent_id: ai_agent.id,
+ available_tags: available_tags,
+ confidence_threshold: 70,
+ max_tags: 3,
+ max_post_tokens: 4000,
+ allow_restricted_tags: false,
+ max_posts_for_context: 5,
+ )
+
+ tagger_prompt = DiscourseAi::Completions::Llm.prompts.last
+ content = tagger_prompt.messages.last[:content]
+
+ expect(content).to include({ upload_id: document_upload.id })
+ expect(content).not_to include({ upload_id: image_upload.id })
+ end
+ end
+
it "respects confidence threshold" do
mock_response = { "tags" => ["bug"], "confidence" => 50 }.to_json
diff --git a/plugins/discourse-ai/spec/lib/completions/dialects/chat_gpt_spec.rb b/plugins/discourse-ai/spec/lib/completions/dialects/chat_gpt_spec.rb
index 981369c5d41..bdb24d5084c 100644
--- a/plugins/discourse-ai/spec/lib/completions/dialects/chat_gpt_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/dialects/chat_gpt_spec.rb
@@ -95,6 +95,69 @@ RSpec.describe DiscourseAi::Completions::Dialects::ChatGpt do
expect(translated.last[:role]).to eq("user")
expect(translated.last[:content].length).to be < (8000 * 4)
end
+ it "renders converted document uploads as text content parts" do
+ llm_model.update!(allowed_attachment_types: ["docx"])
+ converted_text = "Uploaded document: sample.docx (13 Bytes)\n\nConverted text"
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ nil,
+ messages: [{ type: :user, content: ["Read this: ", { upload_id: 123 }] }],
+ )
+
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:encode).and_return(
+ [
+ {
+ kind: :document,
+ filename: "sample.docx",
+ mime_type: "text/plain",
+ text: converted_text,
+ converted_from: "docx",
+ },
+ ],
+ )
+
+ translated = described_class.new(prompt, llm_model).translate
+ user_message = translated.find { |msg| msg[:role] == "user" }
+
+ expect(user_message[:content]).to eq(
+ [{ type: "text", text: "Read this: " }, { type: "text", text: converted_text }],
+ )
+ expect(user_message[:content]).not_to include(hash_including(type: "file"))
+ end
+
+ it "trims converted document text before building the provider payload" do
+ llm_model.update!(allowed_attachment_types: ["docx"], max_prompt_tokens: 2700)
+ converted_text = "Uploaded document: sample.docx (13 Bytes)\n\n#{"document text " * 1000}"
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ "You are a bot",
+ messages: [{ type: :user, content: ["Read this: ", { upload_id: 123 }] }],
+ )
+
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:encode).and_return(
+ [
+ {
+ kind: :document,
+ filename: "sample.docx",
+ mime_type: "text/plain",
+ text: converted_text,
+ converted_from: "docx",
+ },
+ ],
+ )
+
+ dialect = described_class.new(prompt, llm_model)
+ translated = dialect.translate
+ translated_text =
+ translated.sum do |message|
+ content = message[:content]
+ text = content.is_a?(Array) ? content.pluck(:text).compact.join : content.to_s
+ llm_model.tokenizer_class.size(text)
+ end
+
+ expect(translated_text).to be <= dialect.max_prompt_tokens
+ expect(translated.last[:content].last[:text].length).to be < converted_text.length
+ end
end
describe "#tools" do
diff --git a/plugins/discourse-ai/spec/lib/completions/dialects/claude_spec.rb b/plugins/discourse-ai/spec/lib/completions/dialects/claude_spec.rb
index 527351dbdaf..52e44e5538b 100644
--- a/plugins/discourse-ai/spec/lib/completions/dialects/claude_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/dialects/claude_spec.rb
@@ -137,5 +137,35 @@ RSpec.describe DiscourseAi::Completions::Dialects::Claude do
]
expect(translated.messages).to eq(expected)
end
+
+ it "renders converted document uploads as text" do
+ llm_model.update!(allowed_attachment_types: ["docx"])
+ converted_text = "Uploaded document: sample.docx (13 Bytes)\n\nConverted text"
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ nil,
+ messages: [{ type: :user, content: ["Read this: ", { upload_id: 123 }] }],
+ )
+
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:encode).and_return(
+ [
+ {
+ kind: :document,
+ filename: "sample.docx",
+ mime_type: "text/plain",
+ text: converted_text,
+ converted_from: "docx",
+ },
+ ],
+ )
+
+ translated = described_class.new(prompt, llm_model).translate
+ content = translated.messages.first[:content]
+
+ expect(content).to eq(
+ [{ type: "text", text: "Read this: " }, { type: "text", text: converted_text }],
+ )
+ expect(content).not_to include(hash_including(type: "document"))
+ 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
new file mode 100644
index 00000000000..be03fa2e56c
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/completions/dialects/converse_spec.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Completions::Dialects::Converse do
+ fab!(:model, :bedrock_converse_model)
+
+ before { enable_current_plugin }
+
+ describe "#translate" do
+ it "renders converted document uploads as text content blocks" do
+ model.update!(allowed_attachment_types: ["docx"])
+ converted_text = "Uploaded document: sample.docx (13 Bytes)\n\nConverted text"
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ nil,
+ messages: [{ type: :user, content: ["Read this: ", { upload_id: 123 }] }],
+ )
+
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:encode).and_return(
+ [
+ {
+ kind: :document,
+ filename: "sample.docx",
+ mime_type: "text/plain",
+ text: converted_text,
+ converted_from: "docx",
+ },
+ ],
+ )
+
+ translated = described_class.new(prompt, model).translate
+ user_message = translated.messages.find { |msg| msg[:role] == "user" }
+
+ expect(user_message[:content]).to eq([{ text: "Read this: " }, { text: converted_text }])
+ end
+
+ it "skips raw document uploads because Converse raw document support is not enabled" do
+ model.update!(allowed_attachment_types: ["doc"])
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ nil,
+ messages: [{ type: :user, content: ["Read this: ", { upload_id: 123 }] }],
+ )
+
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:encode).and_return(
+ [
+ {
+ kind: :document,
+ filename: "sample.doc",
+ mime_type: "application/msword",
+ base64: "cmF3IGRvYw==",
+ },
+ ],
+ )
+
+ translated = described_class.new(prompt, model).translate
+ user_message = translated.messages.find { |msg| msg[:role] == "user" }
+
+ expect(user_message[:content]).to eq([{ text: "Read this: " }])
+ expect(user_message[:content]).not_to include(hash_including(image: anything))
+ expect(user_message[:content]).not_to include(hash_including(document: anything))
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/completions/dialects/gemini_spec.rb b/plugins/discourse-ai/spec/lib/completions/dialects/gemini_spec.rb
index 4e254054a51..4e82b91be6c 100644
--- a/plugins/discourse-ai/spec/lib/completions/dialects/gemini_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/dialects/gemini_spec.rb
@@ -213,6 +213,34 @@ RSpec.describe DiscourseAi::Completions::Dialects::Gemini do
expect(translated[:messages].last.dig(:parts, 0, :text).length).to be <
context.long_message_text(length: 5_000).length
end
+
+ it "renders converted document uploads as text parts" do
+ model.update!(allowed_attachment_types: ["docx"])
+ converted_text = "Uploaded document: sample.docx (13 Bytes)\n\nConverted text"
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ nil,
+ messages: [{ type: :user, content: ["Read this: ", { upload_id: 123 }] }],
+ )
+
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:encode).and_return(
+ [
+ {
+ kind: :document,
+ filename: "sample.docx",
+ mime_type: "text/plain",
+ text: converted_text,
+ converted_from: "docx",
+ },
+ ],
+ )
+
+ translated = described_class.new(prompt, model).translate
+ user_message = translated[:messages].find { |msg| msg[:role] == "user" }
+
+ expect(user_message[:parts]).to eq([{ text: "Read this: " }, { text: converted_text }])
+ expect(user_message[:parts]).not_to include(hash_including(inlineData: anything))
+ end
end
describe "#tools" do
diff --git a/plugins/discourse-ai/spec/lib/completions/dialects/open_ai_responses_spec.rb b/plugins/discourse-ai/spec/lib/completions/dialects/open_ai_responses_spec.rb
index f92fd8e2391..088a89d7b3a 100644
--- a/plugins/discourse-ai/spec/lib/completions/dialects/open_ai_responses_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/dialects/open_ai_responses_spec.rb
@@ -65,5 +65,35 @@ RSpec.describe DiscourseAi::Completions::Dialects::OpenAiResponses do
name: "echo",
)
end
+
+ it "renders converted document uploads as input_text parts" do
+ llm_model.update!(allowed_attachment_types: ["docx"])
+ converted_text = "Uploaded document: sample.docx (13 Bytes)\n\nConverted text"
+ prompt =
+ DiscourseAi::Completions::Prompt.new(
+ nil,
+ messages: [{ type: :user, content: ["Read this: ", { upload_id: 123 }] }],
+ )
+
+ allow(DiscourseAi::Completions::UploadEncoder).to receive(:encode).and_return(
+ [
+ {
+ kind: :document,
+ filename: "sample.docx",
+ mime_type: "text/plain",
+ text: converted_text,
+ converted_from: "docx",
+ },
+ ],
+ )
+
+ translated = described_class.new(prompt, llm_model).translate
+ user_message = translated.find { |msg| msg[:role] == "user" }
+
+ expect(user_message[:content]).to eq(
+ [{ type: "input_text", text: "Read this: " }, { type: "input_text", text: converted_text }],
+ )
+ expect(user_message[:content]).not_to include(hash_including(type: "input_file"))
+ end
end
end
diff --git a/plugins/discourse-ai/spec/lib/completions/doc_to_text_spec.rb b/plugins/discourse-ai/spec/lib/completions/doc_to_text_spec.rb
new file mode 100644
index 00000000000..83785b4147a
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/completions/doc_to_text_spec.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Completions::DocToText do
+ after do
+ if described_class.instance_variable_defined?(:@antiword_installed)
+ described_class.remove_instance_variable(:@antiword_installed)
+ end
+ end
+
+ it "returns nil when antiword is not installed" do
+ allow(Discourse::Utils).to receive(:execute_command).with("which", "antiword").and_raise(
+ Discourse::Utils::CommandError.new("missing antiword"),
+ )
+
+ allow(Discourse::SafeExec).to receive(:capture)
+
+ expect(described_class.convert("/tmp/sample.doc")).to be_nil
+ expect(Discourse::SafeExec).not_to have_received(:capture).with("antiword", any_args)
+ end
+
+ it "converts doc files with antiword" do
+ tempfile = Tempfile.new(%w[sample .doc])
+ tempfile.close
+
+ allow(Discourse::Utils).to receive(:execute_command).with("which", "antiword").and_return(
+ "/usr/bin/antiword\n",
+ )
+ allow(Discourse::SafeExec).to receive(:capture).with(
+ "antiword",
+ "-w",
+ "0",
+ tempfile.path,
+ read: described_class.sandbox_read_paths(tempfile.path),
+ execute: Discourse::SafeExec.default_execute_paths,
+ timeout: described_class::ANTIWORD_TIMEOUT_SECONDS,
+ env: described_class::SAFE_EXEC_ENV,
+ unsetenv_others: true,
+ rlimits: described_class::ANTIWORD_RLIMITS,
+ seccomp_deny_network: true,
+ max_output_bytes: described_class::MAX_CONVERSION_OUTPUT_BYTES,
+ truncate_output: true,
+ failure_message: "Failed to convert .doc upload to text",
+ ).and_return("Converted document text\n")
+
+ expect(described_class.convert(tempfile.path)).to eq("Converted document text\n")
+ ensure
+ tempfile&.close!
+ end
+
+ it "uses the real antiword binary when it is available" do
+ skip "antiword is not installed" if !system("which antiword", out: File::NULL)
+
+ tempfile = Tempfile.new(%w[invalid .doc])
+ tempfile.write("not a real Word document")
+ tempfile.close
+
+ expect { described_class.convert(tempfile.path) }.to raise_error(Discourse::Utils::CommandError)
+ ensure
+ tempfile&.close!
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/completions/docx_to_text_spec.rb b/plugins/discourse-ai/spec/lib/completions/docx_to_text_spec.rb
new file mode 100644
index 00000000000..b58353661bf
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/completions/docx_to_text_spec.rb
@@ -0,0 +1,158 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Completions::DocxToText do
+ def with_docx(entries)
+ tempfile = Tempfile.new(%w[document .docx])
+ path = tempfile.path
+ tempfile.close
+ FileUtils.rm_f(path)
+
+ ::Zip::File.open(path, create: true) do |zip_file|
+ entries.each do |name, content|
+ zip_file.get_output_stream(name) { |stream| stream.write(content) }
+ end
+ end
+
+ yield path
+ ensure
+ tempfile&.close
+ FileUtils.rm_f(path) if path
+ end
+
+ def word_xml(body)
+ <<~XML
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
+ #{body}
+ </w:document>
+ XML
+ end
+
+ def numbered_paragraph(text, num_id:, ilvl: 0)
+ <<~XML
+ <w:p>
+ <w:pPr>
+ <w:numPr>
+ <w:ilvl w:val="#{ilvl}"/>
+ <w:numId w:val="#{num_id}"/>
+ </w:numPr>
+ </w:pPr>
+ <w:r><w:t>#{text}</w:t></w:r>
+ </w:p>
+ XML
+ end
+
+ def numbering_xml
+ <<~XML
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
+ <w:abstractNum w:abstractNumId="1">
+ <w:lvl w:ilvl="0">
+ <w:start w:val="1"/>
+ <w:numFmt w:val="decimal"/>
+ <w:lvlText w:val="%1."/>
+ </w:lvl>
+ <w:lvl w:ilvl="1">
+ <w:start w:val="1"/>
+ <w:numFmt w:val="lowerLetter"/>
+ <w:lvlText w:val="%2)"/>
+ </w:lvl>
+ </w:abstractNum>
+ <w:num w:numId="7">
+ <w:abstractNumId w:val="1"/>
+ </w:num>
+ <w:abstractNum w:abstractNumId="2">
+ <w:lvl w:ilvl="0">
+ <w:numFmt w:val="bullet"/>
+ <w:lvlText w:val="•"/>
+ </w:lvl>
+ </w:abstractNum>
+ <w:num w:numId="8">
+ <w:abstractNumId w:val="2"/>
+ </w:num>
+ </w:numbering>
+ XML
+ end
+
+ it "extracts paragraph text from the main document" do
+ with_docx("word/document.xml" => word_xml(<<~XML)) do |path|
+ <w:body>
+ <w:p>
+ <w:r><w:t>Hello</w:t></w:r>
+ <w:r><w:tab/></w:r>
+ <w:r><w:t>world</w:t></w:r>
+ <w:r><w:br/></w:r>
+ <w:r><w:t>with a break</w:t></w:r>
+ </w:p>
+ <w:p><w:r><w:t>Second paragraph</w:t></w:r></w:p>
+ </w:body>
+ XML
+ expect(described_class.convert(path)).to eq("Hello\tworld\nwith a break\nSecond paragraph")
+ end
+ end
+
+ it "extracts supported text parts in a stable order" do
+ part = ->(text) { word_xml("<w:p><w:r><w:t>#{text}</w:t></w:r></w:p>") }
+
+ with_docx(
+ "word/footer1.xml" => part.call("Footer"),
+ "word/header2.xml" => part.call("Header two"),
+ "word/document.xml" => part.call("Body"),
+ "word/comments.xml" => part.call("Comment"),
+ "word/header1.xml" => part.call("Header one"),
+ "word/endnotes.xml" => part.call("Endnote"),
+ "word/footnotes.xml" => part.call("Footnote"),
+ ) do |path|
+ expect(described_class.convert(path)).to eq(
+ "Body\n\nHeader one\n\nHeader two\n\nFooter\n\nFootnote\n\nEndnote\n\nComment",
+ )
+ end
+ end
+
+ it "extracts image alt text inline" do
+ with_docx("word/document.xml" => word_xml(<<~XML)) do |path|
+ <w:body>
+ <w:p>
+ <w:r><w:t>See </w:t></w:r>
+ <w:r>
+ <w:drawing>
+ <wp:inline xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
+ <wp:docPr id="1" name="Picture 1" descr="a diagram of the upload flow" title="Upload flow"/>
+ </wp:inline>
+ </w:drawing>
+ </w:r>
+ <w:r><w:t> for details</w:t></w:r>
+ </w:p>
+ </w:body>
+ XML
+ expect(described_class.convert(path)).to eq(
+ "See [Image: a diagram of the upload flow - Upload flow] for details",
+ )
+ end
+ end
+
+ it "adds prefixes for numbered and bulleted lists" do
+ with_docx(
+ "word/numbering.xml" => numbering_xml,
+ "word/document.xml" => word_xml(<<~XML),
+ <w:body>
+ #{numbered_paragraph("First", num_id: 7)}
+ #{numbered_paragraph("Second", num_id: 7)}
+ #{numbered_paragraph("Nested", num_id: 7, ilvl: 1)}
+ #{numbered_paragraph("Third", num_id: 7)}
+ #{numbered_paragraph("Bullet", num_id: 8)}
+ </w:body>
+ XML
+ ) do |path|
+ expect(described_class.convert(path)).to eq(
+ "1. First\n2. Second\n a) Nested\n3. Third\n• Bullet",
+ )
+ end
+ end
+
+ it "returns blank text when the docx has no supported text parts" do
+ with_docx("docProps/core.xml" => "<properties />") do |path|
+ expect(described_class.convert(path)).to eq("")
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb b/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
index 75c1c248ce4..4f900897c83 100644
--- a/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/prompt_messages_builder_spec.rb
@@ -13,8 +13,14 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
fab!(:image_upload2) do
Fabricate(:upload, user: user, original_filename: "image.png", extension: "png")
end
+ let(:document_upload) do
+ Fabricate(:upload, user: user, original_filename: "notes.txt", extension: "txt")
+ end
- before { enable_current_plugin }
+ before do
+ enable_current_plugin
+ SiteSetting.authorized_extensions = "*"
+ end
it "correctly merges user messages with uploads" do
builder.push(type: :user, content: "Hello", id: "Alice", upload_ids: [1])
@@ -142,7 +148,7 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
end
it "includes uploads from context posts when include_uploads is true" do
- upload = Fabricate(:upload, user: user)
+ upload = Fabricate(:upload, user: user, original_filename: "image.png", extension: "png")
UploadReference.create!(target: post1, upload: upload)
context =
@@ -273,6 +279,67 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
expect(message[:upload_ids]).to be_nil
end
+ it "can include document uploads while excluding image uploads" do
+ message_with_mixed_uploads =
+ Fabricate(
+ :chat_message,
+ chat_channel: dm_channel,
+ user: user,
+ message: "Check these files",
+ upload_ids: [image_upload1.id, document_upload.id],
+ )
+
+ context =
+ described_class.messages_from_chat(
+ message_with_mixed_uploads,
+ channel: dm_channel,
+ context_post_ids: nil,
+ max_messages: 10,
+ include_image_uploads: false,
+ include_document_uploads: true,
+ allowed_attachment_types: ["txt"],
+ bot_user_ids: [bot_user.id],
+ instruction_message: nil,
+ )
+
+ expect(context.first[:content]).to eq(
+ [
+ "Check these files -- uploaded(#{image_upload1.short_url}, #{document_upload.short_url})",
+ { upload_id: document_upload.id },
+ ],
+ )
+ end
+
+ it "can include image uploads while excluding document uploads" do
+ message_with_mixed_uploads =
+ Fabricate(
+ :chat_message,
+ chat_channel: dm_channel,
+ user: user,
+ message: "Check these files",
+ upload_ids: [image_upload1.id, document_upload.id],
+ )
+
+ context =
+ described_class.messages_from_chat(
+ message_with_mixed_uploads,
+ channel: dm_channel,
+ context_post_ids: nil,
+ max_messages: 10,
+ include_image_uploads: true,
+ include_document_uploads: false,
+ bot_user_ids: [bot_user.id],
+ instruction_message: nil,
+ )
+
+ expect(context.first[:content]).to eq(
+ [
+ "Check these files -- uploaded(#{image_upload1.short_url}, #{document_upload.short_url})",
+ { upload_id: image_upload1.id },
+ ],
+ )
+ end
+
it "properly handles uploads in public channels with multiple users" do
_first_message =
Fabricate(:chat_message, chat_channel: public_channel, user: user, message: "First message")
@@ -373,6 +440,51 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
actual_upload_ids = upload_hashes.map { |h| h[:upload_id] }
expect(actual_upload_ids).to match_array(expected_upload_ids)
end
+
+ it "filters disallowed documents before applying upload limits" do
+ allowed_upload =
+ Fabricate(:upload, user: test_user, original_filename: "notes.txt", extension: "txt")
+ disallowed_uploads =
+ described_class::MAX_CHAT_UPLOADS.times.map do |i|
+ Fabricate(
+ :upload,
+ user: test_user,
+ original_filename: "archive#{i}.zip",
+ extension: "zip",
+ )
+ end
+ mixed_uploads = [allowed_upload, *disallowed_uploads]
+
+ messages =
+ mixed_uploads.map do |upload|
+ Fabricate(
+ :chat_message,
+ chat_channel: test_channel,
+ user: test_user,
+ message: "Message with upload #{upload.id}",
+ ).tap do |msg|
+ UploadReference.create!(target: msg, upload: upload)
+ msg.update!(upload_ids: [upload.id])
+ end
+ end
+
+ context =
+ described_class.messages_from_chat(
+ messages.last,
+ channel: test_channel,
+ context_post_ids: nil,
+ max_messages: messages.size,
+ include_image_uploads: false,
+ include_document_uploads: true,
+ allowed_attachment_types: ["txt"],
+ bot_user_ids: [],
+ instruction_message: nil,
+ )
+
+ upload_hashes = context.first[:content].select { |item| item.is_a?(Hash) && item[:upload_id] }
+
+ expect(upload_hashes).to eq([{ upload_id: allowed_upload.id }])
+ end
end
describe ".messages_from_post" do
@@ -439,6 +551,51 @@ describe DiscourseAi::Completions::PromptMessagesBuilder do
# will be brittle, but open to changing this
end
+ it "includes document post uploads independently from image uploads" do
+ UploadReference.create!(target: third_post, upload: image_upload1)
+ UploadReference.create!(target: third_post, upload: document_upload)
+
+ context =
+ described_class.messages_from_post(
+ third_post,
+ max_posts: 1,
+ bot_usernames: [bot_user.username],
+ include_image_uploads: false,
+ include_document_uploads: true,
+ allowed_attachment_types: ["txt"],
+ )
+
+ expect(context).to contain_exactly(
+ {
+ type: :user,
+ id: user.username,
+ content: [third_post.raw, { upload_id: document_upload.id }],
+ },
+ )
+ end
+
+ it "includes image post uploads independently from document uploads" do
+ UploadReference.create!(target: third_post, upload: image_upload1)
+ UploadReference.create!(target: third_post, upload: document_upload)
+
+ context =
+ described_class.messages_from_post(
+ third_post,
+ max_posts: 1,
+ bot_usernames: [bot_user.username],
+ include_image_uploads: true,
+ include_document_uploads: false,
+ )
+
+ expect(context).to contain_exactly(
+ {
+ type: :user,
+ id: user.username,
+ content: [third_post.raw, { upload_id: image_upload1.id }],
+ },
+ )
+ end
+
it "handles uploads correctly in topic style messages (and times)" do
freeze_time 32.days.ago
diff --git a/plugins/discourse-ai/spec/lib/completions/prompt_spec.rb b/plugins/discourse-ai/spec/lib/completions/prompt_spec.rb
index 71bad6c1a02..5ac5008689a 100644
--- a/plugins/discourse-ai/spec/lib/completions/prompt_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/prompt_spec.rb
@@ -85,6 +85,22 @@ RSpec.describe DiscourseAi::Completions::Prompt do
expect(encoded.first[:mime_type]).to eq("application/pdf")
expect(encoded.first[:kind]).to eq(:document)
end
+
+ it "can encode documents without encoding images" do
+ image_upload =
+ UploadCreator.new(image100x100, "image.jpg").create_for(Discourse.system_user.id)
+ prompt.push(
+ type: :user,
+ content: ["mixed uploads", { upload_id: image_upload.id }, { upload_id: pdf_upload.id }],
+ )
+
+ encoded =
+ prompt.encoded_uploads(prompt.messages.last, allow_images: false, allow_documents: true)
+
+ expect(encoded.length).to eq(1)
+ expect(encoded.first[:kind]).to eq(:document)
+ expect(encoded.first[:filename]).to eq("document.pdf")
+ end
end
describe "#push" do
diff --git a/plugins/discourse-ai/spec/lib/completions/rtf_to_text_spec.rb b/plugins/discourse-ai/spec/lib/completions/rtf_to_text_spec.rb
new file mode 100644
index 00000000000..4afa84d919b
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/completions/rtf_to_text_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Completions::RtfToText do
+ def with_rtf(contents)
+ tempfile = Tempfile.new(%w[document .rtf])
+ tempfile.binmode
+ tempfile.write(contents)
+ tempfile.rewind
+
+ yield tempfile.path
+ ensure
+ tempfile&.close!
+ end
+
+ it "extracts formatted body text" do
+ with_rtf(<<~'RTF') do |path|
+ {\rtf1\ansi\ansicpg1252 This is {\b bold}\par Caf\'e9\par Unicode \u8212? dash\tab done}
+ RTF
+ expect(described_class.convert(path)).to eq("This is bold\nCafé\nUnicode — dash\tdone")
+ end
+ end
+
+ it "ignores formatting tables and embedded binary destinations" do
+ with_rtf(<<~'RTF') { |path| expect(described_class.convert(path)).to eq("Visible\nMore text") }
+ {\rtf1\ansi{\fonttbl{\f0 Arial;}}{\colortbl;\red255\green0\blue0;}{\pict\pngblip abcdef}Visible\par More text}
+ RTF
+ end
+
+ it "preserves escaped literal braces and backslashes" do
+ with_rtf(<<~'RTF') do |path|
+ {\rtf1\ansi literal \{brace\} and \\slash}
+ RTF
+ expect(described_class.convert(path)).to eq("literal {brace} and \\slash")
+ end
+ end
+end
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 be1c9f9b328..8614397694d 100644
--- a/plugins/discourse-ai/spec/lib/completions/upload_encoder_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/upload_encoder_spec.rb
@@ -1,5 +1,7 @@
# frozen_string_literal: true
+require "zip"
+
RSpec.describe DiscourseAi::Completions::UploadEncoder do
let(:gif) { plugin_file_from_fixtures("1x1.gif") }
let(:jpg) { plugin_file_from_fixtures("1x1.jpg") }
@@ -7,6 +9,55 @@ RSpec.describe DiscourseAi::Completions::UploadEncoder do
before { enable_current_plugin }
+ def create_doc_upload(contents: "raw doc bytes", filename: "sample.doc")
+ extension = File.extname(filename)
+ tempfile = Tempfile.new([File.basename(filename, extension), extension.presence || ".upload"])
+ tempfile.binmode
+ tempfile.write(contents)
+ tempfile.rewind
+
+ UploadCreator.new(tempfile, filename).create_for(Discourse.system_user.id)
+ ensure
+ tempfile&.close!
+ end
+
+ def zip_bytes(entries, extension)
+ tempfile = Tempfile.new(["sample", extension])
+ path = tempfile.path
+ tempfile.close
+ FileUtils.rm_f(path)
+
+ ::Zip::File.open(path, create: true) do |zip_file|
+ entries.each do |name, content|
+ zip_file.get_output_stream(name) { |stream| stream.write(content) }
+ end
+ end
+
+ File.binread(path)
+ ensure
+ tempfile&.close
+ FileUtils.rm_f(path) if path
+ end
+
+ def docx_bytes(entries)
+ zip_bytes(entries, ".docx")
+ end
+
+ def xlsx_bytes(entries)
+ zip_bytes(entries, ".xlsx")
+ end
+
+ def docx_document_xml(text)
+ <<~XML
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
+ <w:body>
+ <w:p><w:r><w:t>#{text}</w:t></w:r></w:p>
+ </w:body>
+ </w:document>
+ XML
+ end
+
it "automatically converts gifs to pngs" do
upload = UploadCreator.new(gif, "1x1.gif").create_for(Discourse.system_user.id)
encoded = described_class.encode(upload_ids: [upload.id], max_pixels: 1_048_576)
@@ -30,4 +81,458 @@ RSpec.describe DiscourseAi::Completions::UploadEncoder do
expect(encoded[0][:base64]).to be_present
expect(encoded[0][:mime_type]).to eq("image/jpeg")
end
+
+ describe ".doc, .docx, .xls, and .xlsx uploads" do
+ before { SiteSetting.authorized_extensions = "*" }
+
+ it "converts .doc files to text" do
+ upload = create_doc_upload(contents: "raw doc bytes", filename: "sample.doc")
+
+ allow(DiscourseAi::Completions::DocToText).to receive(:convert).and_return(
+ "Converted document text\n",
+ )
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["doc"],
+ )
+
+ expect(DiscourseAi::Completions::DocToText).to have_received(:convert).with(
+ a_string_matching(/\.doc\z/),
+ )
+ expect(encoded.length).to eq(1)
+ expect(encoded.first).to include(
+ kind: :document,
+ filename: "sample.doc",
+ mime_type: "text/plain",
+ converted_from: "doc",
+ )
+ expect(encoded.first[:text]).to start_with("Uploaded document: sample.doc (")
+ expect(encoded.first[:text]).to include("\n\nConverted document text")
+ expect(encoded.first).not_to have_key(:base64)
+ end
+
+ it "skips .doc files when conversion is unavailable" do
+ upload = create_doc_upload(contents: "raw doc bytes", filename: "sample.doc")
+
+ allow(DiscourseAi::Completions::DocToText).to receive(:convert).and_return(nil)
+ allow(Rails.logger).to receive(:warn)
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["doc"],
+ )
+
+ expect(encoded).to be_empty
+ expect(Rails.logger).to have_received(:warn).with(
+ a_string_including(
+ "Skipping .doc upload",
+ "raw upload is not supported for this attachment type; it must be converted to text",
+ ),
+ )
+ expect(DiscourseAi::Completions::DocToText).to have_received(:convert).with(
+ a_string_matching(/\.doc\z/),
+ )
+ end
+
+ it "converts .docx files to text" do
+ upload =
+ create_doc_upload(
+ contents:
+ docx_bytes("word/document.xml" => docx_document_xml("Converted DOCX document text")),
+ filename: "sample.docx",
+ )
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["docx"],
+ )
+
+ expect(encoded.length).to eq(1)
+ expect(encoded.first).to include(
+ kind: :document,
+ filename: "sample.docx",
+ mime_type: "text/plain",
+ converted_from: "docx",
+ )
+ expect(encoded.first[:text]).to start_with("Uploaded document: sample.docx (")
+ expect(encoded.first[:text]).to include("\n\nConverted DOCX document text")
+ expect(encoded.first).not_to have_key(:base64)
+ end
+
+ it "logs docx conversion failures and skips the upload" do
+ upload = create_doc_upload(contents: "raw docx bytes", filename: "sample.docx")
+
+ allow(Rails.logger).to receive(:warn)
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["docx"],
+ )
+
+ expect(Rails.logger).to have_received(:warn).with(
+ a_string_including(
+ "Failed to convert .docx upload to text",
+ "upload_id=#{upload.id}",
+ "sample.docx",
+ "Zip",
+ ),
+ )
+ expect(encoded).to be_empty
+ end
+
+ it "converts .xls files to text" do
+ upload = create_doc_upload(contents: "raw xls bytes", filename: "sample.xls")
+
+ allow(DiscourseAi::Completions::XlsToText).to receive(:convert).and_return(
+ "Name,Value\nAlice,1\n",
+ )
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["xls"],
+ )
+
+ expect(DiscourseAi::Completions::XlsToText).to have_received(:convert).with(
+ a_string_matching(/\.xls\z/),
+ )
+ expect(encoded.length).to eq(1)
+ expect(encoded.first).to include(
+ kind: :document,
+ filename: "sample.xls",
+ mime_type: "text/plain",
+ converted_from: "xls",
+ )
+ expect(encoded.first[:text]).to start_with("Uploaded document: sample.xls (")
+ expect(encoded.first[:text]).to include("\n\nName,Value\nAlice,1")
+ expect(encoded.first).not_to have_key(:base64)
+ end
+
+ it "skips .xls files when conversion is unavailable" do
+ upload = create_doc_upload(contents: "raw xls bytes", filename: "sample.xls")
+
+ allow(DiscourseAi::Completions::XlsToText).to receive(:convert).and_return(nil)
+ allow(Rails.logger).to receive(:warn)
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["xls"],
+ )
+
+ expect(encoded).to be_empty
+ expect(Rails.logger).to have_received(:warn).with(
+ a_string_including(
+ "Skipping .xls upload",
+ "raw upload is not supported for this attachment type; it must be converted to text",
+ ),
+ )
+ expect(DiscourseAi::Completions::XlsToText).to have_received(:convert).with(
+ a_string_matching(/\.xls\z/),
+ )
+ end
+
+ it "converts .xlsx files to text" do
+ upload =
+ create_doc_upload(
+ contents: xlsx_bytes("xl/worksheets/sheet1.xml" => <<~XML),
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
+ <sheetData>
+ <row><c t="inlineStr"><is><t>Converted XLSX spreadsheet text</t></is></c></row>
+ </sheetData>
+ </worksheet>
+ XML
+ filename: "sample.xlsx",
+ )
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["xlsx"],
+ )
+
+ expect(encoded.length).to eq(1)
+ expect(encoded.first).to include(
+ kind: :document,
+ filename: "sample.xlsx",
+ mime_type: "text/plain",
+ converted_from: "xlsx",
+ )
+ expect(encoded.first[:text]).to start_with("Uploaded document: sample.xlsx (")
+ expect(encoded.first[:text]).to include(
+ "\n\nSheet: Sheet1\n\nConverted XLSX spreadsheet text",
+ )
+ expect(encoded.first).not_to have_key(:base64)
+ end
+
+ it "logs xlsx conversion failures and skips the upload" do
+ upload = create_doc_upload(contents: "raw xlsx bytes", filename: "sample.xlsx")
+
+ allow(Rails.logger).to receive(:warn)
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["xlsx"],
+ )
+
+ expect(Rails.logger).to have_received(:warn).with(
+ a_string_including(
+ "Failed to convert .xlsx upload to text",
+ "upload_id=#{upload.id}",
+ "sample.xlsx",
+ "Zip",
+ ),
+ )
+ expect(encoded).to be_empty
+ end
+
+ it "converts .rtf files to text" do
+ upload =
+ create_doc_upload(
+ contents: "{\\rtf1\\ansi RTF {\\b document}\\par text}",
+ filename: "sample.rtf",
+ )
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["rtf"],
+ )
+
+ expect(encoded.length).to eq(1)
+ expect(encoded.first).to include(
+ kind: :document,
+ filename: "sample.rtf",
+ mime_type: "text/plain",
+ converted_from: "rtf",
+ )
+ expect(encoded.first[:text]).to include("\n\nRTF document\ntext")
+ expect(encoded.first).not_to have_key(:base64)
+ end
+
+ it "embeds text, markdown, and csv uploads as text" do
+ uploads = [
+ create_doc_upload(contents: "plain text with unicode café", filename: "notes.txt"),
+ create_doc_upload(
+ contents:
+ "# Heading
+
+markdown body",
+ filename: "guide.md",
+ ),
+ create_doc_upload(
+ contents:
+ "name,value
+Alice,1",
+ filename: "data.csv",
+ ),
+ ]
+
+ encoded =
+ described_class.encode(
+ upload_ids: uploads.map(&:id),
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: %w[txt md csv],
+ )
+
+ expect(encoded.map { |payload| payload[:converted_from] }).to eq(%w[txt md csv])
+ expect(encoded).to all(include(kind: :document, mime_type: "text/plain"))
+ expect(encoded).to all(satisfy { |payload| !payload.key?(:base64) })
+ expect(encoded[0][:text]).to include(
+ "
+
+plain text with unicode café",
+ )
+ expect(encoded[1][:text]).to include(
+ "
+
+# Heading
+
+markdown body",
+ )
+ expect(encoded[2][:text]).to include(
+ "
+
+name,value
+Alice,1",
+ )
+ end
+
+ it "accepts legacy aliases for attachment types" do
+ md_upload = create_doc_upload(contents: "# Heading", filename: "guide.md")
+ txt_upload = create_doc_upload(contents: "plain text", filename: "notes.txt")
+
+ encoded =
+ described_class.encode(
+ upload_ids: [md_upload.id, txt_upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: %w[markdown text],
+ )
+
+ expect(encoded.map { |payload| payload[:converted_from] }).to eq(%w[md txt])
+ expect(encoded.first[:text]).to include("\n\n# Heading")
+ expect(encoded.second[:text]).to include("\n\nplain text")
+ end
+
+ it "limits text upload reads before embedding" do
+ upload = create_doc_upload(contents: "0123456789abcdef", filename: "large.txt")
+
+ encoded =
+ stub_const(described_class, :MAX_TEXT_FILE_BYTES, 10) do
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["txt"],
+ )
+ end
+
+ expect(encoded.length).to eq(1)
+ expect(encoded.first[:text]).to include(
+ "\n\n0123456789\n\n[Document text truncated after 10 Bytes.]",
+ )
+ expect(encoded.first).not_to have_key(:base64)
+ end
+
+ it "logs blank text uploads and skips the upload" do
+ blank_text = " \n\n"
+ upload = create_doc_upload(contents: blank_text, filename: "blank.txt")
+
+ allow(Rails.logger).to receive(:warn)
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["txt"],
+ )
+
+ expect(Rails.logger).to have_received(:warn).with(
+ a_string_including(
+ "Failed to convert .txt upload to text",
+ "upload_id=#{upload.id}",
+ "blank.txt",
+ "blank",
+ ),
+ ).at_least(:once)
+ expect(encoded).to be_empty
+ end
+
+ it "logs conversion failures and skips the upload" do
+ upload = create_doc_upload(contents: "raw doc bytes", filename: "sample.doc")
+ error = StandardError.new("converter failed")
+
+ allow(DiscourseAi::Completions::DocToText).to receive(:convert).and_raise(error)
+ allow(Rails.logger).to receive(:warn)
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["doc"],
+ )
+
+ expect(Rails.logger).to have_received(:warn).with(
+ a_string_including(
+ "Failed to convert .doc upload to text",
+ "upload_id=#{upload.id}",
+ "sample.doc",
+ "StandardError",
+ "converter failed",
+ ),
+ )
+ expect(encoded).to be_empty
+ end
+
+ it "logs blank doc conversion output and skips the upload" do
+ upload = create_doc_upload(contents: "raw doc bytes", filename: "sample.doc")
+
+ allow(DiscourseAi::Completions::DocToText).to receive(:convert).and_return("\n \n")
+ allow(Rails.logger).to receive(:warn)
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["doc"],
+ )
+
+ expect(Rails.logger).to have_received(:warn).with(
+ a_string_including(
+ "Failed to convert .doc upload to text",
+ "upload_id=#{upload.id}",
+ "sample.doc",
+ "DOC converter returned blank output",
+ ),
+ )
+ expect(encoded).to be_empty
+ end
+
+ it "sends allowed raw PDF documents when they are within the byte limit" do
+ upload = create_doc_upload(contents: "%PDF raw bytes", filename: "sample.pdf")
+
+ encoded =
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["pdf"],
+ )
+
+ expect(encoded.length).to eq(1)
+ expect(encoded.first[:kind]).to eq(:document)
+ expect(encoded.first[:filename]).to eq("sample.pdf")
+ expect(Base64.strict_decode64(encoded.first[:base64])).to eq("%PDF raw bytes")
+ end
+
+ it "skips raw PDF documents that exceed the byte limit" do
+ upload = create_doc_upload(contents: "%PDF raw bytes", filename: "sample.pdf")
+
+ allow(Rails.logger).to receive(:warn)
+
+ encoded =
+ stub_const(described_class, :MAX_RAW_DOCUMENT_BYTES, 4) do
+ described_class.encode(
+ upload_ids: [upload.id],
+ max_pixels: 1_048_576,
+ allowed_kinds: %i[document],
+ allowed_attachment_types: ["pdf"],
+ )
+ end
+
+ expect(encoded).to be_empty
+ expect(Rails.logger).to have_received(:warn).with(
+ a_string_including("Skipping .pdf upload", "exceeds the 4 Bytes limit"),
+ )
+ end
+ end
end
diff --git a/plugins/discourse-ai/spec/lib/completions/xls_to_text_spec.rb b/plugins/discourse-ai/spec/lib/completions/xls_to_text_spec.rb
new file mode 100644
index 00000000000..d0e5fc3827e
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/completions/xls_to_text_spec.rb
@@ -0,0 +1,60 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Completions::XlsToText do
+ after do
+ if described_class.instance_variable_defined?(:@xls2csv_installed)
+ described_class.remove_instance_variable(:@xls2csv_installed)
+ end
+ end
+
+ it "returns nil when xls2csv is not installed" do
+ allow(Discourse::Utils).to receive(:execute_command).with("which", "xls2csv").and_raise(
+ Discourse::Utils::CommandError.new("missing xls2csv"),
+ )
+
+ allow(Discourse::SafeExec).to receive(:capture)
+
+ expect(described_class.convert("/tmp/sample.xls")).to be_nil
+ expect(Discourse::SafeExec).not_to have_received(:capture).with("xls2csv", any_args)
+ end
+
+ it "converts xls files with xls2csv" do
+ tempfile = Tempfile.new(%w[sample .xls])
+ tempfile.close
+
+ allow(Discourse::Utils).to receive(:execute_command).with("which", "xls2csv").and_return(
+ "/usr/bin/xls2csv\n",
+ )
+ allow(Discourse::SafeExec).to receive(:capture).with(
+ "xls2csv",
+ tempfile.path,
+ read: described_class.sandbox_read_paths(tempfile.path),
+ execute: Discourse::SafeExec.default_execute_paths,
+ timeout: described_class::XLS2CSV_TIMEOUT_SECONDS,
+ env: described_class::SAFE_EXEC_ENV,
+ unsetenv_others: true,
+ rlimits: described_class::XLS2CSV_RLIMITS,
+ seccomp_deny_network: true,
+ max_output_bytes: described_class::MAX_CONVERSION_OUTPUT_BYTES,
+ truncate_output: true,
+ failure_message: "Failed to convert .xls upload to text",
+ ).and_return("Name,Value\nAlice,1\n")
+
+ expect(described_class.convert(tempfile.path)).to eq("Name,Value\nAlice,1\n")
+ ensure
+ tempfile&.close!
+ end
+
+ it "uses the real xls2csv binary when it is available" do
+ skip "xls2csv is not installed" if !system("which xls2csv", out: File::NULL)
+
+ tempfile = Tempfile.new(%w[invalid .xls])
+ tempfile.binmode
+ tempfile.write("not a real Excel document")
+ tempfile.close
+
+ expect(described_class.convert(tempfile.path)).to eq("")
+ ensure
+ tempfile&.close!
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/completions/xlsx_to_text_spec.rb b/plugins/discourse-ai/spec/lib/completions/xlsx_to_text_spec.rb
new file mode 100644
index 00000000000..6c3452d494f
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/completions/xlsx_to_text_spec.rb
@@ -0,0 +1,117 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Completions::XlsxToText do
+ def with_xlsx(entries)
+ tempfile = Tempfile.new(%w[workbook .xlsx])
+ path = tempfile.path
+ tempfile.close
+ FileUtils.rm_f(path)
+
+ ::Zip::File.open(path, create: true) do |zip_file|
+ entries.each do |name, content|
+ zip_file.get_output_stream(name) { |stream| stream.write(content) }
+ end
+ end
+
+ yield path
+ ensure
+ tempfile&.close
+ FileUtils.rm_f(path) if path
+ end
+
+ def workbook_xml
+ <<~XML
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
+ <sheets>
+ <sheet name="People" sheetId="1" r:id="rId1"/>
+ <sheet name="Summary" sheetId="2" r:id="rId2"/>
+ </sheets>
+ </workbook>
+ XML
+ end
+
+ def workbook_rels_xml
+ <<~XML
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/>
+ <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet2.xml"/>
+ </Relationships>
+ XML
+ end
+
+ def shared_strings_xml
+ <<~XML
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ <sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
+ <si><t>Name</t></si>
+ <si><t>Alice</t></si>
+ <si><r><t>Rich</t></r><r><t> text</t></r></si>
+ <si><t>Total</t></si>
+ </sst>
+ XML
+ end
+
+ it "extracts sheets in workbook order as tab-delimited text" do
+ with_xlsx(
+ "xl/workbook.xml" => workbook_xml,
+ "xl/_rels/workbook.xml.rels" => workbook_rels_xml,
+ "xl/sharedStrings.xml" => shared_strings_xml,
+ "xl/worksheets/sheet1.xml" => <<~XML,
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
+ <sheetData>
+ <row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1"><v>Age</v></c></row>
+ <row r="2"><c r="A2" t="s"><v>1</v></c><c r="C2" t="inlineStr"><is><t>Engineer</t></is></c></row>
+ <row r="3"><c r="A3" t="s"><v>2</v></c><c r="B3" t="b"><v>1</v></c></row>
+ </sheetData>
+ </worksheet>
+ XML
+ "xl/worksheets/sheet2.xml" => <<~XML,
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
+ <sheetData>
+ <row r="1"><c r="A1" t="s"><v>3</v></c><c r="B1"><v>42</v></c></row>
+ </sheetData>
+ </worksheet>
+ XML
+ ) do |path|
+ expect(described_class.convert(path)).to eq(
+ "Sheet: People\n\nName\tAge\nAlice\t\tEngineer\nRich text\tTRUE\n\nSheet: Summary\n\nTotal\t42",
+ )
+ end
+ end
+
+ it "falls back to worksheet entries when workbook metadata is missing" do
+ with_xlsx("xl/worksheets/sheet2.xml" => <<~XML, "xl/worksheets/sheet1.xml" => <<~XML) do |path|
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
+ <sheetData><row><c t="inlineStr"><is><t>Second</t></is></c></row></sheetData>
+ </worksheet>
+ XML
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
+ <sheetData><row><c t="inlineStr"><is><t>First</t></is></c></row></sheetData>
+ </worksheet>
+ XML
+ expect(described_class.convert(path)).to eq(
+ "Sheet: Sheet1\n\nFirst\n\nSheet: Sheet2\n\nSecond",
+ )
+ end
+ end
+
+ it "limits very wide sparse rows" do
+ with_xlsx("xl/worksheets/sheet1.xml" => <<~XML) do |path|
+ <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
+ <sheetData>
+ <row><c r="A1" t="inlineStr"><is><t>Visible</t></is></c><c r="ZZ1" t="inlineStr"><is><t>Too far away</t></is></c></row>
+ </sheetData>
+ </worksheet>
+ XML
+ expect(described_class.convert(path)).to eq("Sheet: Sheet1\n\nVisible")
+ end
+ end
+
+ it "returns blank text when the workbook has no readable sheets" do
+ with_xlsx("docProps/core.xml" => "<properties />") do |path|
+ expect(described_class.convert(path)).to eq("")
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb b/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb
index f8c4e5ff701..55f59cfd137 100644
--- a/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/automation/llm_triage_spec.rb
@@ -361,6 +361,31 @@ describe DiscourseAi::Automation::LlmTriage do
end
end
+ it "includes document uploads when triaging even if image uploads are disabled" do
+ ai_agent.update!(vision_enabled: false)
+ llm_model.update!(allowed_attachment_types: ["txt"])
+ SiteSetting.authorized_extensions = "*"
+ image_upload = Fabricate(:image_upload, posts: [post])
+ document_upload = Fabricate(:upload, original_filename: "notes.txt", extension: "txt")
+ UploadReference.create!(target: post, upload: document_upload)
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["bad"]) do
+ triage(
+ post: post.reload,
+ triage_agent_id: ai_agent.id,
+ search_for_text: "bad",
+ flag_post: true,
+ automation: nil,
+ )
+
+ triage_prompt = DiscourseAi::Completions::Llm.prompts.last
+ content = triage_prompt.messages.last[:content]
+
+ expect(content).to include({ upload_id: document_upload.id })
+ expect(content).not_to include({ upload_id: image_upload.id })
+ end
+ end
+
it "includes stop_sequences in the completion call" do
sequences = %w[GOOD BAD]
diff --git a/plugins/discourse-ai/spec/models/llm_model_spec.rb b/plugins/discourse-ai/spec/models/llm_model_spec.rb
index f8ed1e69c13..3cc2e82982c 100644
--- a/plugins/discourse-ai/spec/models/llm_model_spec.rb
+++ b/plugins/discourse-ai/spec/models/llm_model_spec.rb
@@ -56,4 +56,13 @@ RSpec.describe LlmModel do
expect(bedrock_model.valid?).to be true
end
end
+
+ describe "allowed_attachment_types" do
+ it "normalizes markdown attachments to md" do
+ model = Fabricate.build(:llm_model)
+ model.allowed_attachment_types = %w[pdf markdown md htm text]
+
+ expect(model.allowed_attachment_types).to eq(%w[pdf md html txt])
+ end
+ end
end
diff --git a/spec/lib/compression/safe_zip_reader_spec.rb b/spec/lib/compression/safe_zip_reader_spec.rb
new file mode 100644
index 00000000000..99ea1f651c2
--- /dev/null
+++ b/spec/lib/compression/safe_zip_reader_spec.rb
@@ -0,0 +1,92 @@
+# frozen_string_literal: true
+
+require "compression/safe_zip_reader"
+
+RSpec.describe Compression::SafeZipReader do
+ let(:temp_folder) do
+ path = "#{Pathname.new(Dir.tmpdir).realpath}/#{SecureRandom.hex}"
+ FileUtils.mkdir(path)
+ path
+ end
+
+ let(:zip_path) { File.join(temp_folder, "archive.zip") }
+
+ after { FileUtils.rm_rf(temp_folder) }
+
+ def create_zip(entries)
+ Zip::File.open(zip_path, create: true) do |zip_file|
+ entries.each do |name, content|
+ zip_file.get_output_stream(name) { |stream| stream.write(content) }
+ end
+ end
+ end
+
+ it "reads an entry within the configured limits" do
+ create_zip("document.xml" => "hello")
+
+ described_class.open(zip_path, max_entries: 10, max_total_bytes: 100) do |zip|
+ expect(zip.read_entry("document.xml", max_bytes: 10)).to eq("hello")
+ expect(zip.remaining_total_bytes).to eq(95)
+ end
+ end
+
+ it "returns nil for missing entries" do
+ create_zip("document.xml" => "hello")
+
+ described_class.open(zip_path) do |zip|
+ expect(zip.read_entry("missing.xml", max_bytes: 10)).to be_nil
+ end
+ end
+
+ it "raises for missing required entries" do
+ create_zip("document.xml" => "hello")
+
+ described_class.open(zip_path) do |zip|
+ expect { zip.read_entry("missing.xml", max_bytes: 10, required: true) }.to raise_error(
+ described_class::MissingEntryError,
+ )
+ end
+ end
+
+ it "raises when an entry exceeds its per-entry limit" do
+ create_zip("document.xml" => "hello")
+
+ described_class.open(zip_path) do |zip|
+ expect { zip.read_entry("document.xml", max_bytes: 4) }.to raise_error(
+ described_class::EntryTooLargeError,
+ )
+ end
+ end
+
+ it "raises when reads exceed the total inflated byte budget" do
+ create_zip("a.xml" => "hello", "b.xml" => "world")
+
+ described_class.open(zip_path, max_total_bytes: 6) do |zip|
+ expect(zip.read_entry("a.xml", max_bytes: 10)).to eq("hello")
+ expect { zip.read_entry("b.xml", max_bytes: 10) }.to raise_error(
+ described_class::EntryTooLargeError,
+ )
+ end
+ end
+
+ it "raises when the archive has too many entries" do
+ create_zip("a.xml" => "a", "b.xml" => "b")
+
+ expect { described_class.open(zip_path, max_entries: 1) { nil } }.to raise_error(
+ described_class::TooManyEntriesError,
+ )
+ end
+
+ it "streams entries to files within the configured limits" do
+ create_zip("document.xml" => "hello")
+
+ Tempfile.create("safe-zip-reader") do |tempfile|
+ described_class.open(zip_path) do |zip|
+ zip.stream_entry_to_file("document.xml", tempfile, max_bytes: 10)
+ end
+
+ tempfile.rewind
+ expect(tempfile.read).to eq("hello")
+ end
+ end
+end
diff --git a/spec/lib/discourse/safe_exec_spec.rb b/spec/lib/discourse/safe_exec_spec.rb
new file mode 100644
index 00000000000..8e1e5235303
--- /dev/null
+++ b/spec/lib/discourse/safe_exec_spec.rb
@@ -0,0 +1,202 @@
+# frozen_string_literal: true
+
+require "discourse/safe_exec"
+
+RSpec.describe Discourse::SafeExec do
+ describe ".capture" do
+ it "delegates sandboxed execution to Landlock::SafeExec" do
+ status = instance_double(Process::Status, exited?: true, exitstatus: 0)
+ result =
+ instance_double(
+ Landlock::SafeExec::Result,
+ stdout: "hello\n",
+ stderr: "",
+ status: status,
+ output_truncated?: false,
+ )
+
+ allow(Landlock::SafeExec).to receive(:capture).and_return(result)
+
+ expect(
+ described_class.capture(
+ "echo",
+ "hello",
+ read: ["/tmp/input"],
+ execute: described_class.default_execute_paths,
+ timeout: 1,
+ env: {
+ "PATH" => ENV["PATH"].to_s,
+ },
+ unsetenv_others: true,
+ rlimits: {
+ cpu_seconds: 1,
+ },
+ seccomp_deny_network: true,
+ max_output_bytes: 1024,
+ truncate_output: true,
+ ),
+ ).to eq("hello\n")
+
+ expect(Landlock::SafeExec).to have_received(:capture).with(
+ "echo",
+ "hello",
+ read: ["/tmp/input"],
+ write: [],
+ execute: described_class.default_execute_paths,
+ timeout: 1,
+ failure_message: "",
+ success_status_codes: [0],
+ env: {
+ "PATH" => ENV["PATH"].to_s,
+ },
+ inherit_env: false,
+ chdir: nil,
+ connect_tcp: nil,
+ bind_tcp: [],
+ rlimits: {
+ cpu_seconds: 1,
+ },
+ seccomp_deny_network: true,
+ max_output_bytes: 1024,
+ truncate_output: true,
+ )
+ end
+
+ it "converts Landlock command failures to Discourse command errors" do
+ status = instance_double(Process::Status, exited?: true, exitstatus: 1)
+ result =
+ instance_double(
+ Landlock::SafeExec::Result,
+ stdout: "",
+ stderr: "nope",
+ status: status,
+ output_truncated?: false,
+ )
+
+ allow(Landlock::SafeExec).to receive(:capture).and_return(result)
+
+ expect { described_class.capture("false", failure_message: "failed") }.to raise_error(
+ Discourse::Utils::CommandError,
+ /failed\nnope/,
+ )
+ end
+
+ it "returns truncated stdout without checking the terminated status" do
+ status = instance_double(Process::Status, exited?: false, exitstatus: nil)
+ result =
+ instance_double(
+ Landlock::SafeExec::Result,
+ stdout: "x" * 1024,
+ stderr: "",
+ status: status,
+ output_truncated?: true,
+ )
+
+ allow(Landlock::SafeExec).to receive(:capture).and_return(result)
+
+ expect(described_class.capture("tool", truncate_output: true)).to eq("x" * 1024)
+ end
+
+ it "captures stdout from a landlocked subprocess" do
+ skip "Landlock SafeExec is not supported" if !described_class.landlock_supported?
+
+ output =
+ described_class.capture(
+ "echo",
+ "hello",
+ read: described_class.default_read_paths,
+ execute: described_class.default_execute_paths,
+ )
+
+ expect(output).to eq("hello\n")
+ end
+
+ it "denies access to paths outside of the Landlock policy" do
+ skip "Landlock SafeExec is not supported" if !described_class.landlock_supported?
+
+ Tempfile.create("safe-exec") do |tempfile|
+ tempfile.write("secret")
+ tempfile.close
+
+ expect {
+ described_class.capture(
+ "cat",
+ tempfile.path,
+ read: described_class.default_read_paths,
+ execute: described_class.default_execute_paths,
+ )
+ }.to raise_error(Discourse::Utils::CommandError)
+ end
+ end
+
+ it "can clear the child environment" do
+ previous_secret = ENV["SAFE_EXEC_SECRET"]
+ ENV["SAFE_EXEC_SECRET"] = "hidden"
+ output =
+ described_class.capture(
+ "sh",
+ "-c",
+ "printf '%s' \"${SAFE_EXEC_SECRET-unset}\"",
+ read: described_class.default_read_paths,
+ execute: described_class.default_execute_paths,
+ env: {
+ "PATH" => ENV["PATH"].to_s,
+ },
+ unsetenv_others: true,
+ )
+
+ expect(output).to eq("unset")
+ ensure
+ if previous_secret.nil?
+ ENV.delete("SAFE_EXEC_SECRET")
+ else
+ ENV["SAFE_EXEC_SECRET"] = previous_secret
+ end
+ end
+
+ it "denies network syscalls when requested" do
+ skip "Landlock SafeExec is not supported" if !described_class.landlock_supported?
+
+ expect {
+ described_class.capture(
+ RbConfig.ruby,
+ "-rsocket",
+ "-e",
+ "UDPSocket.new",
+ read: described_class.default_read_paths,
+ execute: described_class.default_execute_paths,
+ env: {
+ "PATH" => ENV["PATH"].to_s,
+ },
+ unsetenv_others: true,
+ seccomp_deny_network: true,
+ )
+ }.to raise_error(Discourse::Utils::CommandError)
+ end
+
+ it "terminates commands that exceed the output limit" do
+ expect {
+ described_class.capture(
+ RbConfig.ruby,
+ "-e",
+ "STDOUT.write('x' * 2048)",
+ max_output_bytes: 1024,
+ )
+ }.to raise_error(Discourse::Utils::CommandError, /output exceeded 1024 bytes/)
+ end
+
+ it "truncates commands that exceed the output limit when requested" do
+ output =
+ described_class.capture(
+ RbConfig.ruby,
+ "-e",
+ "STDOUT.write('x' * 2048)",
+ max_output_bytes: 1024,
+ truncate_output: true,
+ )
+
+ expect(output.bytesize).to eq(1024)
+ expect(output).to eq("x" * 1024)
+ end
+ end
+end