A platform for community discussion. Free, open, simple.
FEATURE: Add native support for /llms.txt (#36939)
Régis Hanolc668871f46f6
diff --git a/app/controllers/static_controller.rb b/app/controllers/static_controller.rbindex 35f2f498e56..7426c9b316f 100644--- a/app/controllers/static_controller.rb+++ b/app/controllers/static_controller.rb@@ -3,9 +3,9 @@class StaticController < ApplicationControllerskip_before_action :check_xhr, :redirect_to_login_if_required, :redirect_to_profile_if_requiredskip_before_action :verify_authenticity_token,- only: %i[cdn_asset enter favicon service_worker_asset]- skip_before_action :preload_json, only: %i[cdn_asset enter favicon service_worker_asset]- skip_before_action :handle_theme, only: %i[cdn_asset enter favicon service_worker_asset]+ only: %i[cdn_asset enter favicon llms_txt service_worker_asset]+ skip_before_action :preload_json, only: %i[cdn_asset enter favicon llms_txt service_worker_asset]+ skip_before_action :handle_theme, only: %i[cdn_asset enter favicon llms_txt service_worker_asset]before_action :apply_cdn_headers, only: %i[cdn_asset enter favicon service_worker_asset]@@ -224,6 +224,28 @@ class StaticController < ApplicationControllerendend+ def llms_txt+ upload = SiteSetting.llms_txt+ return head(:not_found) if upload.blank?++ if Discourse.store.external?+ content =+ Discourse+ .cache+ .fetch("llms_txt_content:#{upload.sha1}") do+ Discourse.store.download_safe(upload)&.path&.then { |path| File.read(path) }+ end+ return head(:not_found) if content.blank?++ render plain: content, content_type: "text/plain"+ else+ path = Discourse.store.path_for(upload)+ return head(:not_found) if path.blank? || !File.exist?(path)++ send_file(path, type: "text/plain", disposition: "inline")+ end+ end+def cdn_assetis_asset_pathdiff --git a/config/routes.rb b/config/routes.rbindex 0b12af46d3a..9e8a7a77df2 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -1648,6 +1648,7 @@ Discourse::Application.routes.draw doget "robots.txt" => "robots_txt#index"get "robots-builder.json" => "robots_txt#builder"+ get "llms.txt" => "static#llms_txt"get "offline.html" => "offline#index"get "manifest.webmanifest" => "metadata#manifest", :as => :manifestget "manifest.json" => "metadata#manifest"diff --git a/config/site_settings.yml b/config/site_settings.ymlindex fca1b66053e..e3dac13b98f 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -2722,7 +2722,6 @@ security:type: uploadauthorized_extensions: "txt|md"max_file_size_kb: 512- hidden: truecontent_security_policy:default: truecontent_security_policy_report_only:diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rbindex 1d8a67db6a0..cb92da97769 100644--- a/lib/middleware/anonymous_cache.rb+++ b/lib/middleware/anonymous_cache.rb@@ -85,12 +85,16 @@ module Middlewareenddef blocked_crawler?- @request.get? && [email protected]? && [email protected]_with?("robots.txt") &&- [email protected]_with?("srv/status") &&- @request[Auth::DefaultCurrentUserProvider::API_KEY].nil? &&- @env[Auth::DefaultCurrentUserProvider::USER_API_KEY].nil? &&- @env[Auth::DefaultCurrentUserProvider::HEADER_API_KEY].nil? &&- CrawlerDetection.is_blocked_crawler?(crawler_identifier)+ return false if [email protected]?+ return false if @request.xhr?+ return false if @request.path.ends_with?("robots.txt")+ return false if @request.path.ends_with?("llms.txt")+ return false if @request.path.ends_with?("srv/status")+ return false if @request[Auth::DefaultCurrentUserProvider::API_KEY]+ return false if @env[Auth::DefaultCurrentUserProvider::USER_API_KEY]+ return false if @env[Auth::DefaultCurrentUserProvider::HEADER_API_KEY]++ CrawlerDetection.is_blocked_crawler?(crawler_identifier)end# rubocop:disable Lint/BooleanSymboldiff --git a/spec/requests/static_controller_spec.rb b/spec/requests/static_controller_spec.rbindex a1ee06bda07..aba63e42f95 100644--- a/spec/requests/static_controller_spec.rb+++ b/spec/requests/static_controller_spec.rb@@ -431,4 +431,33 @@ RSpec.describe StaticController doexpect(response.body).to include("addEventListener")endend++ describe "#llms_txt" do+ it "returns 404 when no upload is set" do+ get "/llms.txt"+ expect(response.status).to eq(404)+ end++ context "with local store" do+ it "returns content as plain text" do+ SiteSetting.authorized_extensions = "txt"++ file = Tempfile.new(%w[llms .txt])+ file.write("# Test LLMs Content")+ file.rewind++ upload = UploadCreator.new(file, "llms.txt").create_for(Discourse.system_user.id)+ SiteSetting.llms_txt = upload++ get "/llms.txt"++ expect(response.status).to eq(200)+ expect(response.content_type).to start_with("text/plain")+ expect(response.body).to eq("# Test LLMs Content")+ ensure+ file.close+ file.unlink+ end+ end+ endend