Skip to content

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.rb
index 35f2f498e56..7426c9b316f 100644
--- a/app/controllers/static_controller.rb
+++ b/app/controllers/static_controller.rb
@@ -3,9 +3,9 @@
class StaticController < ApplicationController
skip_before_action :check_xhr, :redirect_to_login_if_required, :redirect_to_profile_if_required
skip_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 < ApplicationController
end
end
+ 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_asset
is_asset_path
diff --git a/config/routes.rb b/config/routes.rb
index 0b12af46d3a..9e8a7a77df2 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1648,6 +1648,7 @@ Discourse::Application.routes.draw do
get "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 => :manifest
get "manifest.json" => "metadata#manifest"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index fca1b66053e..e3dac13b98f 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2722,7 +2722,6 @@ security:
type: upload
authorized_extensions: "txt|md"
max_file_size_kb: 512
- hidden: true
content_security_policy:
default: true
content_security_policy_report_only:
diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rb
index 1d8a67db6a0..cb92da97769 100644
--- a/lib/middleware/anonymous_cache.rb
+++ b/lib/middleware/anonymous_cache.rb
@@ -85,12 +85,16 @@ module Middleware
end
def 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/BooleanSymbol
diff --git a/spec/requests/static_controller_spec.rb b/spec/requests/static_controller_spec.rb
index a1ee06bda07..aba63e42f95 100644
--- a/spec/requests/static_controller_spec.rb
+++ b/spec/requests/static_controller_spec.rb
@@ -431,4 +431,33 @@ RSpec.describe StaticController do
expect(response.body).to include("addEventListener")
end
end
+
+ 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
+ end
end