Skip to content

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

FEATURE: Add custom headers to System One models (#44618)

Samb5366651ea0c
diff --git a/db/structure.sql b/db/structure.sql
index 5f5ce93aa6c..92adc5f272e 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -4113,7 +4113,8 @@ CREATE TABLE public.decision_models (
ai_secret_id bigint,
vision_enabled boolean DEFAULT false NOT NULL,
created_at timestamp(6) without time zone NOT NULL,
- updated_at timestamp(6) without time zone NOT NULL
+ updated_at timestamp(6) without time zone NOT NULL,
+ custom_headers jsonb DEFAULT '[]'::jsonb NOT NULL
);
@@ -25919,6 +25920,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groups
SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
+('20261014224810'),
('20261014145458'),
('20261014065735'),
('20261013222431'),
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-decision-model-editor-form.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-decision-model-editor-form.gjs
index 34f1a641fc5..babad351dec 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-decision-model-editor-form.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-decision-model-editor-form.gjs
@@ -1,6 +1,6 @@
import Component from "@glimmer/component";
import { cached, tracked } from "@glimmer/tracking";
-import { concat, fn } from "@ember/helper";
+import { concat, fn, hash } from "@ember/helper";
import { action } from "@ember/object";
import { service } from "@ember/service";
import BackButton from "discourse/components/back-button";
@@ -10,6 +10,7 @@ import {
addUniqueValueToArray,
removeValueFromArray,
} from "discourse/lib/array-tools";
+import { eq, gte, or } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
import AiSecretSelector from "./ai-secret-selector";
@@ -32,9 +33,14 @@ export default class AiDecisionModelEditorForm extends Component {
url: model.url,
ai_secret_id: model.ai_secret_id,
vision_enabled: model.vision_enabled ?? false,
+ custom_headers: model.custom_headers ?? [],
};
}
+ get customHeaderLimits() {
+ return this.args.decisionModels.resultSetMeta.custom_header_limits;
+ }
+
get presets() {
return this.args.decisionModels.resultSetMeta.presets;
}
@@ -104,15 +110,23 @@ export default class AiDecisionModelEditorForm extends Component {
await set("preset", value);
const preset = this.presets.find((item) => item.preset_id === value);
if (preset) {
- for (const name of [
- "display_name",
- "name",
- "provider",
- "url",
- "vision_enabled",
- ]) {
+ for (const name of ["display_name", "name", "url", "vision_enabled"]) {
await set(name, preset[name]);
}
+ await this.selectProvider(preset.provider, { set });
+ await set(
+ "custom_headers",
+ (preset.custom_headers ?? []).map((header) => ({ ...header }))
+ );
+ }
+ this.testResult = null;
+ }
+
+ @action
+ async selectProvider(value, { set }) {
+ await set("provider", value);
+ if (!["system_one", "typesafe"].includes(value)) {
+ await set("custom_headers", []);
}
this.testResult = null;
}
@@ -175,6 +189,7 @@ export default class AiDecisionModelEditorForm extends Component {
<form.Field
@format="large"
@name="provider"
+ @onSet={{this.selectProvider}}
@title={{i18n "discourse_ai.decision_models.provider"}}
@type="select"
@validation="required"
@@ -227,6 +242,74 @@ export default class AiDecisionModelEditorForm extends Component {
</field.Control>
</form.Field>
+ {{#if (or (eq data.provider "system_one") (eq data.provider "typesafe"))}}
+ <form.Container @direction="column" @format="large">
+ <form.Section
+ @title={{i18n "discourse_ai.decision_models.custom_headers.title"}}
+ >
+ <form.Collection @name="custom_headers" as |collection index|>
+ <form.Fieldset>
+ <collection.Field
+ @format="large"
+ @name="name"
+ @title={{i18n
+ "discourse_ai.decision_models.custom_headers.name"
+ }}
+ @type="input"
+ @validation={{concat
+ "required|length:1,"
+ this.customHeaderLimits.name_length
+ }}
+ as |field|
+ >
+ <field.Control />
+ </collection.Field>
+
+ <collection.Field
+ @format="large"
+ @name="value"
+ @title={{i18n
+ "discourse_ai.decision_models.custom_headers.value"
+ }}
+ @type="input"
+ @validation={{concat
+ "length:0,"
+ this.customHeaderLimits.value_length
+ }}
+ as |field|
+ >
+ <field.Control />
+ </collection.Field>
+ <form.Actions>
+ <form.Button
+ class="btn-default ai-decision-model-editor__remove-header"
+ @action={{fn collection.remove index}}
+ @icon="trash-can"
+ @title="discourse_ai.decision_models.custom_headers.remove"
+ />
+ </form.Actions>
+ </form.Fieldset>
+ </form.Collection>
+ <form.Actions>
+ <form.Button
+ class="btn-default ai-decision-model-editor__add-header"
+ @action={{fn
+ form.addItemToCollection
+ "custom_headers"
+ (hash name="" value="")
+ }}
+ @disabled={{gte
+ data.custom_headers.length
+ this.customHeaderLimits.count
+ }}
+ @icon="plus"
+ @label="discourse_ai.decision_models.custom_headers.add"
+ />
+ </form.Actions>
+ </form.Section>
+ </form.Container>
+ {{/if}}
+
<form.Field
@format="large"
@name="vision_enabled"
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_decision_models_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_decision_models_controller.rb
index da9ff2ff657..b5084fd8e4f 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_decision_models_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_decision_models_controller.rb
@@ -5,9 +5,9 @@ module DiscourseAi
class AiDecisionModelsController < ::Admin::AdminController
requires_plugin PLUGIN_NAME
- # A 16x16 solid red PNG: models that claim image input must recognise it.
+ # The red square meets minimum image-size requirements; tiny placeholders may be rejected.
TEST_IMAGE =
- "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEX/AAAZ4gk3AAAADElEQVQI12NgIA0AAAAwAAHHqoWOAAAAAElFTkSuQmCC"
+ "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARgAAAEYCAMAAACwUBm+AAAAA1BMVEX/AAAZ4gk3AAAAYklEQVR42u3BAQEAAACCIP+vbkhAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHwaM2cAAalku0EAAAAASUVORK5CYII="
def index
decision_models = DecisionModel.order(:display_name)
@@ -17,11 +17,13 @@ module DiscourseAi
ActiveModel::ArraySerializer.new(
decision_models,
each_serializer: AiDecisionModelSerializer,
+ scope: guardian,
root: false,
).as_json,
meta: {
providers: DecisionModel::PROVIDERS,
presets: DecisionModel.presets,
+ custom_header_limits: DecisionModel::CUSTOM_HEADER_LIMITS,
ai_secrets: AiSecret.order(:name).select(:id, :name).as_json(only: %i[id name]),
},
}
@@ -32,7 +34,7 @@ module DiscourseAi
def edit
decision_model = DecisionModel.find(params[:id])
- render json: AiDecisionModelSerializer.new(decision_model)
+ render json: AiDecisionModelSerializer.new(decision_model, scope: guardian)
end
def create
@@ -45,7 +47,8 @@ module DiscourseAi
logger_fields,
{ decision_model_id: decision_model.id, subject: decision_model.display_name },
)
- render json: AiDecisionModelSerializer.new(decision_model), status: :created
+ render json: AiDecisionModelSerializer.new(decision_model, scope: guardian),
+ status: :created
else
render_json_error decision_model
end
@@ -63,7 +66,7 @@ module DiscourseAi
logger_fields,
{ decision_model_id: decision_model.id, subject: decision_model.display_name },
)
- render json: AiDecisionModelSerializer.new(decision_model)
+ render json: AiDecisionModelSerializer.new(decision_model, scope: guardian)
else
render_json_error decision_model
end
@@ -79,6 +82,8 @@ module DiscourseAi
.slice(*logger_fields.keys)
.merge(decision_model_id: decision_model.id, subject: decision_model.display_name)
+ details[:custom_headers] = "[FILTERED]" if decision_model.custom_headers.present?
+
if decision_model.destroy
DiscourseAi::Utils::AiStaffActionLogger.new(current_user).log_deletion(
"decision_model",
@@ -149,18 +154,20 @@ module DiscourseAi
private
def decision_model_params
- params.require(:ai_decision_model).permit(
- :display_name,
- :name,
- :provider,
- :url,
- :ai_secret_id,
- :vision_enabled,
+ input = params.require(:ai_decision_model)
+ permitted =
+ input.permit(:display_name, :name, :provider, :url, :ai_secret_id, :vision_enabled)
+ # Preserve malformed JSON shapes so validation rejects them rather than silently dropping them.
+ permitted[:custom_headers] = input.to_unsafe_h[:custom_headers] if input.key?(
+ :custom_headers,
)
+ permitted
end
def logger_fields
- { display_name: {}, name: {}, provider: {}, url: {}, ai_secret_id: {}, vision_enabled: {} }
+ %i[display_name name provider url ai_secret_id vision_enabled]
+ .index_with { {} }
+ .merge(custom_headers: { type: :sensitive })
end
end
end
diff --git a/plugins/discourse-ai/app/models/decision_model.rb b/plugins/discourse-ai/app/models/decision_model.rb
index 2a0741e7ed7..15263443a8e 100644
--- a/plugins/discourse-ai/app/models/decision_model.rb
+++ b/plugins/discourse-ai/app/models/decision_model.rb
@@ -1,7 +1,39 @@
# frozen_string_literal: true
class DecisionModel < ActiveRecord::Base
- PROVIDERS = %w[system_one open_ai].freeze
+ PROVIDERS = %w[system_one typesafe open_ai].freeze
+ MAX_CUSTOM_HEADERS = 20
+ MAX_CUSTOM_HEADER_NAME_LENGTH = 128
+ MAX_CUSTOM_HEADER_VALUE_LENGTH = 4000
+ CUSTOM_HEADER_LIMITS = {
+ count: MAX_CUSTOM_HEADERS,
+ name_length: MAX_CUSTOM_HEADER_NAME_LENGTH,
+ value_length: MAX_CUSTOM_HEADER_VALUE_LENGTH,
+ }.freeze
+ CUSTOM_HEADER_NAME = /\A[!\#$%&'*+\-.^_`|~0-9A-Za-z]+\z/
+ CUSTOM_HEADER_VALUE = /\A[\x20-\x7E]*\z/
+ RESERVED_CUSTOM_HEADERS = %w[
+ authorization
+ content-type
+ host
+ content-length
+ transfer-encoding
+ connection
+ cookie
+ cookie2
+ set-cookie
+ proxy-authorization
+ proxy-authenticate
+ proxy-connection
+ www-authenticate
+ keep-alive
+ te
+ trailer
+ upgrade
+ expect
+ accept-encoding
+ content-encoding
+ ].freeze
belongs_to :ai_secret, optional: true
@@ -11,6 +43,7 @@ class DecisionModel < ActiveRecord::Base
validates :url, presence: true, length: { maximum: 1000 }
validate :valid_endpoint_url
validate :secret_exists
+ validate :valid_custom_headers
def self.presets
@presets ||= [
@@ -19,9 +52,18 @@ class DecisionModel < ActiveRecord::Base
display_name: "TypeSafe Jev",
name: "jev-latest",
url: "https://api.typesafe.ai/v1/systemone",
- provider: "system_one",
+ provider: "typesafe",
vision_enabled: false,
}.freeze,
+ {
+ preset_id: "jev-latest-vision",
+ display_name: "TypeSafe Jev with images (experimental)",
+ name: "jev-latest",
+ url: "https://api.typesafe.ai/v1/systemone",
+ provider: "typesafe",
+ vision_enabled: true,
+ custom_headers: [{ name: "TypeSafe-Danger-Zone-Images", value: "true" }.freeze].freeze,
+ }.freeze,
{
preset_id: "vllm-sr-d3",
display_name: "d3",
@@ -83,8 +125,53 @@ class DecisionModel < ActiveRecord::Base
"https://#{service.target}:#{service.port}#{uri.path}#{"?#{uri.query}" if uri.query}"
end
+ def supports_custom_headers?
+ %w[system_one typesafe].include?(provider)
+ end
+
+ def custom_headers_for_request
+ # Stored configuration can bypass validations, so check it again before transport.
+ return {} if !supports_custom_headers? || custom_headers_error
+
+ custom_headers.to_h { |header| [header["name"], header["value"]] }
+ end
+
private
+ def valid_custom_headers
+ error = custom_headers_error
+ if error
+ errors.add(
+ :custom_headers,
+ I18n.t("discourse_ai.decision_models.custom_headers.#{error}", **CUSTOM_HEADER_LIMITS),
+ )
+ end
+ end
+
+ def custom_headers_error
+ return :invalid_format if !custom_headers.is_a?(Array)
+ return :too_many if custom_headers.size > MAX_CUSTOM_HEADERS
+ return :unsupported_provider if custom_headers.present? && !supports_custom_headers?
+
+ names = Set.new
+ custom_headers.each do |header|
+ return :invalid_format if !header.is_a?(Hash) || header.keys.sort != %w[name value]
+ name, value = header.values_at("name", "value")
+ if !name.is_a?(String) || name.length > MAX_CUSTOM_HEADER_NAME_LENGTH ||
+ !CUSTOM_HEADER_NAME.match?(name)
+ return :invalid_name
+ end
+ if !value.is_a?(String) || value.length > MAX_CUSTOM_HEADER_VALUE_LENGTH ||
+ !CUSTOM_HEADER_VALUE.match?(value)
+ return :invalid_value
+ end
+ return :reserved_name if RESERVED_CUSTOM_HEADERS.include?(name.downcase)
+ return :duplicate_name if !names.add?(name.downcase)
+ end
+
+ nil
+ end
+
def valid_endpoint_url
return if url.blank?
uri = URI(url)
@@ -108,6 +195,7 @@ end
# Table name: decision_models
#
# id :bigint not null, primary key
+# custom_headers :jsonb not null
# display_name :string(100) not null
# name :string(255) not null
# provider :string(50) not null
diff --git a/plugins/discourse-ai/app/serializers/ai_decision_model_serializer.rb b/plugins/discourse-ai/app/serializers/ai_decision_model_serializer.rb
index 1ea58701938..73ee0848faf 100644
--- a/plugins/discourse-ai/app/serializers/ai_decision_model_serializer.rb
+++ b/plugins/discourse-ai/app/serializers/ai_decision_model_serializer.rb
@@ -3,5 +3,16 @@
class AiDecisionModelSerializer < ApplicationSerializer
root "ai_decision_model"
- attributes :id, :display_name, :name, :provider, :url, :ai_secret_id, :vision_enabled
+ attributes :id,
+ :display_name,
+ :name,
+ :provider,
+ :url,
+ :ai_secret_id,
+ :vision_enabled,
+ :custom_headers
+
+ def include_custom_headers?
+ scope&.is_admin?
+ end
end
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-decision-model.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-decision-model.js
index d162704ea9d..be1adc41c42 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-decision-model.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-decision-model.js
@@ -9,7 +9,8 @@ export default class AiDecisionModel extends RestModel {
"provider",
"url",
"ai_secret_id",
- "vision_enabled"
+ "vision_enabled",
+ "custom_headers"
);
}
@@ -20,7 +21,8 @@ export default class AiDecisionModel extends RestModel {
testConfig(config) {
return ajax("/admin/plugins/discourse-ai/ai-decision-models/test.json", {
type: "POST",
- data: { ai_decision_model: config },
+ contentType: "application/json",
+ data: JSON.stringify({ ai_decision_model: config }),
});
}
}
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 62a6b35c699..26148f2ffc2 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -1184,6 +1184,12 @@ en:
url: "Endpoint URL"
api_key: "Credential"
vision_enabled: "Supports image input"
+ custom_headers:
+ title: "Custom headers"
+ name: "Header name"
+ value: "Header value"
+ add: "Add header"
+ remove: "Remove header"
edit: "Edit"
delete: "Delete"
confirm_delete: "Are you sure you want to delete this decision model? Workflows that use it will stop working."
@@ -1192,6 +1198,7 @@ en:
test_success: "The decision model answered the test question (yes: %{percentage}%)."
providers:
system_one: "System One"
+ typesafe: "TypeSafe"
open_ai: "OpenAI Decisions"
mcp_servers:
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index b9fa63a7324..cc467c45105 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -1394,6 +1394,14 @@ en:
model_test_failed: "We failed to generate a test embedding with this model. Check it's correctly configured."
decision_models:
+ custom_headers:
+ invalid_format: "must be an array of header name and value pairs"
+ too_many: "cannot contain more than %{count} entries"
+ unsupported_provider: "are only supported for System One and TypeSafe models"
+ invalid_name: "must have valid HTTP names of at most %{name_length} characters"
+ invalid_value: "must have printable ASCII values of at most %{value_length} characters"
+ reserved_name: "cannot override reserved transport or authentication headers"
+ duplicate_name: "must have unique names (case-insensitive)"
secret_not_found: "The selected credential does not exist"
invalid_url: "must be an http:// or https:// URL, or an srv:// URL without a port"
test_refused: "The decision model declined to answer the test question."
diff --git a/plugins/discourse-ai/db/migrate/20261014224810_add_custom_headers_to_decision_models.rb b/plugins/discourse-ai/db/migrate/20261014224810_add_custom_headers_to_decision_models.rb
new file mode 100644
index 00000000000..63f214e66e2
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20261014224810_add_custom_headers_to_decision_models.rb
@@ -0,0 +1,6 @@
+# frozen_string_literal: true
+class AddCustomHeadersToDecisionModels < ActiveRecord::Migration[8.1]
+ def change
+ add_column :decision_models, :custom_headers, :jsonb, default: [], null: false
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/client.rb b/plugins/discourse-ai/lib/decisions/client.rb
index 466aa23b9fd..9cf17d36e66 100644
--- a/plugins/discourse-ai/lib/decisions/client.rb
+++ b/plugins/discourse-ai/lib/decisions/client.rb
@@ -18,19 +18,15 @@ module DiscourseAi
end
uri = URI(@decision_model.endpoint_url)
- body = @endpoint.payload(state:, questions:, images:).to_json
+ payload = @endpoint.payload(state:, questions:, images:)
+ body = payload.to_json
log =
AiApiAuditLog.new(
provider_id: @endpoint.provider_id,
user_id: user&.id,
feature_name:,
language_model: @decision_model.name,
- raw_request_payload:
- @endpoint.payload(
- state:,
- questions:,
- images: images.map { |image| redacted_image(image) },
- ).to_json,
+ raw_request_payload: @endpoint.audit_payload(payload).to_json,
)
started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
begin
@@ -89,14 +85,6 @@ module DiscourseAi
end
end
- # Base64 images would bloat the audit log, so it only records what was sent.
- def redacted_image(image)
- header, data = image.split(",", 2)
- mime = header.delete_prefix("data:").delete_suffix(";base64")
- size = ActiveSupport::NumberHelper.number_to_human_size(data.length * 3 / 4)
- "[redacted #{mime}, #{size}]"
- end
-
def retryable_network_error?(exception)
return false if exception.is_a?(FinalDestination::SSRFError)
# FinalDestination raises a bare Timeout::Error when DNS resolution is slow.
@@ -110,6 +98,8 @@ module DiscourseAi
# @return [String] the body of the successful response; the audit log only keeps a
# redacted copy
def post(uri, headers, body, log)
+ api_key = @decision_model.api_key
+ header_values = @decision_model.custom_headers_for_request.values
attempts = []
loop do
begin
@@ -125,13 +115,14 @@ module DiscourseAi
write_timeout: @timeout,
) { |http| http.request(request) }
log.response_status = response.code.to_i
- log.raw_response_payload = RequestFailed.redact(response.body, @decision_model.api_key)
+ log.raw_response_payload =
+ RequestFailed.redact_response(response.body, api_key, header_values:)
error = RequestFailed.from_response(@decision_model, response) if !response.is_a?(
Net::HTTPSuccess,
)
rescue StandardError => exception
log.response_status = 0
- message = RequestFailed.sanitize(exception.message, @decision_model.api_key)
+ message = RequestFailed.sanitize(exception.message, api_key, header_values:)
error =
RequestFailed.new(
"Request failed: #{message}",
diff --git a/plugins/discourse-ai/lib/decisions/endpoints/base.rb b/plugins/discourse-ai/lib/decisions/endpoints/base.rb
index 4babc63ab75..efee01a93ef 100644
--- a/plugins/discourse-ai/lib/decisions/endpoints/base.rb
+++ b/plugins/discourse-ai/lib/decisions/endpoints/base.rb
@@ -11,6 +11,8 @@ module DiscourseAi
case model.provider
when "system_one"
SystemOne.new(model)
+ when "typesafe"
+ Typesafe.new(model)
when "open_ai"
OpenAi.new(model)
else
@@ -31,6 +33,10 @@ module DiscourseAi
headers
end
+ def audit_payload(payload)
+ payload
+ end
+
# Token counts only feed usage stats, so unusable values become nil rather than
# discarding valid answers.
# @return [Array(Integer, Integer)] input and output tokens, either may be nil
@@ -47,6 +53,11 @@ module DiscourseAi
private
+ def redacted_image(data, mime_type:)
+ size = ActiveSupport::NumberHelper.number_to_human_size(data.length * 3 / 4)
+ "[redacted #{mime_type}, #{size}]"
+ end
+
def validate_names!(answers, questions)
if answers.keys.to_set != questions.map(&:name).to_set
malformed!("every question must be answered exactly once")
diff --git a/plugins/discourse-ai/lib/decisions/endpoints/open_ai.rb b/plugins/discourse-ai/lib/decisions/endpoints/open_ai.rb
index b1aa2a8ed77..182e1740faa 100644
--- a/plugins/discourse-ai/lib/decisions/endpoints/open_ai.rb
+++ b/plugins/discourse-ai/lib/decisions/endpoints/open_ai.rb
@@ -24,6 +24,22 @@ module DiscourseAi
}
end
+ def audit_payload(payload)
+ payload = payload.deep_dup
+ if payload[:input].is_a?(Array)
+ payload[:input].each do |message|
+ message[:content].each do |content|
+ next if content[:type] != "input_image"
+
+ header, data = content[:image_url].split(",", 2)
+ mime_type = header.delete_prefix("data:").delete_suffix(";base64")
+ content[:image_url] = redacted_image(data, mime_type:)
+ end
+ end
+ end
+ payload
+ end
+
def parse(body, questions)
if !body.is_a?(Hash) || !body["answers"].is_a?(Array) || !body["answers"].all?(Hash)
malformed!("answers must be a list of objects")
diff --git a/plugins/discourse-ai/lib/decisions/endpoints/system_one.rb b/plugins/discourse-ai/lib/decisions/endpoints/system_one.rb
index 45fa0472586..7b8426035d2 100644
--- a/plugins/discourse-ai/lib/decisions/endpoints/system_one.rb
+++ b/plugins/discourse-ai/lib/decisions/endpoints/system_one.rb
@@ -20,7 +20,14 @@ module DiscourseAi
has_images:,
streaming: false,
)
- super.merge(RequestHeaders.for(context))
+ headers = decision_model.custom_headers_for_request
+ [super, RequestHeaders.for(context)].each do |authoritative_headers|
+ authoritative_headers.each do |name, value|
+ headers.delete_if { |existing_name, _| existing_name.casecmp?(name) }
+ headers[name] = value
+ end
+ end
+ headers
end
def payload(state:, questions:, images:)
@@ -33,6 +40,16 @@ module DiscourseAi
payload
end
+ def audit_payload(payload)
+ payload = payload.deep_dup
+ payload[:images]&.map! do |image|
+ header, data = image.split(",", 2)
+ mime_type = header.delete_prefix("data:").delete_suffix(";base64")
+ redacted_image(data, mime_type:)
+ end
+ payload
+ end
+
def parse(body, questions)
if !body.is_a?(Hash) || !body["answers"].is_a?(Hash)
malformed!("answers must be an object")
diff --git a/plugins/discourse-ai/lib/decisions/endpoints/typesafe.rb b/plugins/discourse-ai/lib/decisions/endpoints/typesafe.rb
new file mode 100644
index 00000000000..9a009aee887
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/endpoints/typesafe.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ module Endpoints
+ class Typesafe < SystemOne
+ def payload(state:, questions:, images:)
+ payload = super(state:, questions:, images: [])
+ if images.present?
+ payload[:assets] = images.to_h do |image|
+ match = image.match(%r{\Adata:image/(png|jpeg|jpg);base64,(.+)\z})
+ raise InvalidRequest, "Images must be PNG or JPEG base64 data URLs" if !match
+
+ format = match[1] == "jpg" ? "jpeg" : match[1]
+ [SecureRandom.uuid, { type: "image", format:, data_base64: match[2] }]
+ end
+ # Every asset must be referenced without changing the original state.
+ payload[:state] = { text: state, images: payload[:assets].keys }
+ end
+ payload
+ end
+
+ def audit_payload(payload)
+ payload = payload.deep_dup
+ payload[:assets]&.each_value do |asset|
+ asset[:data_base64] = redacted_image(
+ asset[:data_base64],
+ mime_type: "image/#{asset[:format]}",
+ )
+ end
+ payload
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/request_failed.rb b/plugins/discourse-ai/lib/decisions/request_failed.rb
index 6c2f6560307..bcf5bbef489 100644
--- a/plugins/discourse-ai/lib/decisions/request_failed.rb
+++ b/plugins/discourse-ai/lib/decisions/request_failed.rb
@@ -18,17 +18,60 @@ module DiscourseAi
@retryable
end
- def self.redact(text, api_key)
- text = text.to_s
- text = text.gsub(api_key, "[redacted]") if api_key.present?
- text.gsub(/Bearer\s+[^\s"',;]+/i, "Bearer [redacted]").gsub(
- /\b(?:sk|key)-[A-Za-z0-9_-]+/i,
- "[redacted]",
- )
+ def self.redact(text, api_key, header_values: [])
+ patterns = []
+ variants =
+ [api_key, *header_values].reject(&:blank?)
+ .uniq
+ .flat_map do |value|
+ [value, value.to_json[1...-1]].uniq.map { |variant| [variant, value] }
+ end
+ .sort_by { |variant, _value| -variant.length }
+ variants.each do |variant, value|
+ pattern = Regexp.escape(variant)
+ # Short configuration values must not redact parts of ordinary words or identifiers.
+ if value != api_key && value.length < 8
+ pattern =
+ "(?<![[:alnum:]_-])(?<![[:alnum:]_]\\.)#{pattern}(?![[:alnum:]_-]|\\.[[:alnum:]_])"
+ end
+ patterns << Regexp.new(pattern)
+ end
+ patterns << /(?<bearer>Bearer\s+)[^\s"',;]+/i
+ patterns << /\b(?:sk|key)-[A-Za-z0-9_-]+/i
+
+ # One pass prevents overlapping values from redacting the replacement itself.
+ text
+ .to_s
+ .gsub(Regexp.union(patterns)) do
+ Regexp.last_match[:bearer] ? "Bearer [redacted]" : "[redacted]"
+ end
end
- def self.sanitize(message, api_key)
- redact(message, api_key)[0, 500]
+ def self.redact_response(text, api_key, header_values: [])
+ parsed = JSON.parse(text.to_s)
+ redact_json =
+ lambda do |value|
+ case value
+ when Hash
+ value.to_h do |key, child|
+ # Header configuration values must not change response field names.
+ [redact(key, api_key), redact_json.call(child)]
+ end
+ when Array
+ value.map { |child| redact_json.call(child) }
+ when String
+ redact(value, api_key, header_values:)
+ else
+ value
+ end
+ end
+ redact_json.call(parsed).to_json
+ rescue JSON::ParserError
+ redact(text, api_key, header_values:)
+ end
+
+ def self.sanitize(message, api_key, header_values: [])
+ redact(message, api_key, header_values:)[0, 500]
end
def self.from_response(model, response)
@@ -46,7 +89,12 @@ module DiscourseAi
detail = detail.is_a?(Hash) ? detail["message"] || detail.to_json : detail
detail&.to_s&.gsub("AiError: ", "")
end
- message = sanitize(message || response.message, model.api_key)
+ message =
+ sanitize(
+ message || response.message,
+ model.api_key,
+ header_values: model.custom_headers_for_request.values,
+ )
delay = Float(response["Retry-After"], exception: false)
delay = nil if delay && !delay.finite?
new(
diff --git a/plugins/discourse-ai/spec/lib/decisions/client_spec.rb b/plugins/discourse-ai/spec/lib/decisions/client_spec.rb
index 14bf7b8f6cb..e12e9df07fb 100644
--- a/plugins/discourse-ai/spec/lib/decisions/client_spec.rb
+++ b/plugins/discourse-ai/spec/lib/decisions/client_spec.rb
@@ -305,6 +305,49 @@ RSpec.describe DiscourseAi::Decisions::Client do
)
end
+ it "redacts echoed custom-header values from provider errors, network errors and audit logs" do
+ header_value = 'private"header\\value'
+ decision_model.update!(custom_headers: [{ name: "X-Custom-Key", value: header_value }])
+ request =
+ stub_request(:post, decision_model.url)
+ .to_return(status: 401, body: { error: { message: "Rejected #{header_value}" } }.to_json)
+ .then
+ .to_raise(IOError.new("Echoed #{header_value}"))
+
+ 2.times do
+ expect {
+ described_class.new(decision_model, max_attempts: 1).decide(
+ "Hello",
+ questions:,
+ feature_name: "test",
+ )
+ }.to raise_error(DiscourseAi::Decisions::RequestFailed) do |error|
+ expect(error.message).to include("[redacted]")
+ expect(error.message).not_to include(header_value)
+ end
+ end
+
+ expect(request).to have_been_requested.twice
+ log = AiApiAuditLog.find_by!(response_status: 401)
+ expect(JSON.parse(log.raw_response_payload).dig("error", "message")).to eq(
+ "Rejected [redacted]",
+ )
+ end
+
+ it "redacts short custom-header echoes in stored successful responses without changing answers" do
+ header_value = "xy"
+ decision_model.update!(custom_headers: [{ name: "X-Custom-Key", value: header_value }])
+ response_body[:answers][:urgent] = { type: "refusal", reason: header_value }
+ stub_request(:post, decision_model.url).to_return(body: response_body.to_json)
+
+ result = client.decide("Hello", questions:, feature_name: "test")
+
+ expect(result["urgent"].reason).to eq(header_value)
+ expect(AiApiAuditLog.find(result.audit_log_id).raw_response_payload).not_to include(
+ header_value,
+ )
+ end
+
it "redacts only the stored response, never the answers" do
key_questions = [
{ name: "team", type: "choice", instructions: "Who?", choices: %w[key-account other] },
@@ -355,6 +398,50 @@ RSpec.describe DiscourseAi::Decisions::Client do
)
end
+ it "preserves Cloudflare flags and numeric probabilities in response audits" do
+ decision_model.update!(
+ custom_headers: [
+ { name: "X-Images", value: "true" },
+ { name: "X-Other-Flag", value: "false" },
+ { name: "X-Count", value: "1" },
+ ],
+ )
+ envelope = { result: response_body, success: true, errors: [], messages: [] }
+ stub_request(:post, decision_model.url).to_return(body: envelope.to_json)
+
+ result = client.decide("Hello", questions:, feature_name: "test")
+
+ expect(result["urgent"].probability).to eq(0.95)
+ expect(JSON.parse(AiApiAuditLog.find(result.audit_log_id).raw_response_payload)).to eq(
+ JSON.parse(envelope.to_json),
+ )
+ end
+
+ it "keeps words containing flag values intact in HTTP and network diagnostics" do
+ decision_model.update!(custom_headers: [{ name: "X-Images", value: "true" }])
+ message = "image configuration is untrue"
+ request =
+ stub_request(:post, decision_model.url)
+ .to_return(status: 422, body: { error: message }.to_json)
+ .then
+ .to_raise(IOError.new(message))
+
+ [
+ "Request failed with status 422: #{message}",
+ "Request failed: #{message}",
+ ].each do |expected|
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ expected,
+ )
+ end
+
+ expect(request).to have_been_requested.twice
+ expect(JSON.parse(AiApiAuditLog.find_by!(response_status: 422).raw_response_payload)).to eq(
+ "error" => message,
+ )
+ end
+
it "keeps valid answers when usage counts are unusable" do
body =
response_body.deep_dup.tap do |payload|
@@ -368,17 +455,48 @@ RSpec.describe DiscourseAi::Decisions::Client do
expect(AiApiAuditLog.last).to have_attributes(request_tokens: 296, response_tokens: nil)
end
- it "replaces only image inputs with placeholders in the audit payload, without truncating state" do
+ it "sends legacy images separately from unchanged string and structured state and redacts image audits" do
decision_model.update!(vision_enabled: true)
- image = "data:image/png;base64,AAAA"
- state = image + "x" * 20_001
+ images = %w[data:image/png;base64,AAAA data:image/jpeg;base64,BBBB]
+ [images.first + "x" * 20_001, { posts: [{ text: "Hello" }] }].each do |state|
+ sent = nil
+ request =
+ stub_request(:post, decision_model.url)
+ .with do |req|
+ sent = JSON.parse(req.body)
+ sent["state"] == JSON.parse(state.to_json) && sent["images"] == images &&
+ !sent.key?("assets")
+ end
+ .to_return(body: response_body.to_json)
+
+ result = client.decide(state, questions:, images:, feature_name: "test")
+
+ payload = JSON.parse(AiApiAuditLog.find(result.audit_log_id).raw_request_payload)
+ expect(payload).to eq(
+ sent.merge(
+ "images" => ["[redacted image/png, 3 Bytes]", "[redacted image/jpeg, 3 Bytes]"],
+ ),
+ )
+ expect(images).to eq(%w[data:image/png;base64,AAAA data:image/jpeg;base64,BBBB])
+ expect(request).to have_been_requested
+ end
+ end
+
+ it "serializes Time values in structured state consistently on the wire and in audits" do
+ state = { posts: [{ text: "Hello", created_at: Time.utc(2026, 10, 14, 12, 34, 56) }] }
+ sent = nil
request =
stub_request(:post, decision_model.url)
- .with { |req| JSON.parse(req.body).values_at("state", "images") == [state, [image]] }
+ .with do |req|
+ sent = JSON.parse(req.body)
+ sent["state"] == JSON.parse(state.to_json)
+ end
.to_return(body: response_body.to_json)
- result = client.decide(state, questions:, images: [image], feature_name: "test")
- payload = JSON.parse(AiApiAuditLog.find(result.audit_log_id).raw_request_payload)
- expect(payload).to include("state" => state, "images" => ["[redacted image/png, 3 Bytes]"])
+
+ result = client.decide(state, questions:, feature_name: "test")
+
+ expect(sent.dig("state", "posts", 0, "created_at")).to eq("2026-10-14T12:34:56.000Z")
+ expect(JSON.parse(AiApiAuditLog.find(result.audit_log_id).raw_request_payload)).to eq(sent)
expect(request).to have_been_requested.once
end
@@ -451,6 +569,127 @@ RSpec.describe DiscourseAi::Decisions::Client do
end
end
+ describe "#decide with TypeSafe" do
+ before { decision_model.update!(provider: "typesafe", vision_enabled: true) }
+
+ after { DiscourseAi::RequestHeaders.reset! }
+
+ it "sends PNG and JPEG assets with stable references and redacts only their binary audit data" do
+ image = "data:image/png;base64,AAAA"
+ jpeg = "data:image/jpeg;base64,BBBB"
+ state = image + "x" * 20_001
+ sent = nil
+ request =
+ stub_request(:post, decision_model.url)
+ .with do |req|
+ sent = JSON.parse(req.body)
+ references = sent.dig("state", "images")
+ sent.dig("state", "text") == state && references.is_a?(Array) && references.size == 2 &&
+ references.uniq.size == 2 &&
+ references.all? { |reference| reference.match?(/\A[0-9a-f-]{36}\z/) } &&
+ sent["assets"].keys == references && !sent.key?("images") &&
+ sent["assets"].values ==
+ [
+ { "type" => "image", "format" => "png", "data_base64" => "AAAA" },
+ { "type" => "image", "format" => "jpeg", "data_base64" => "BBBB" },
+ ]
+ end
+ .to_return(body: response_body.to_json)
+
+ result = client.decide(state, questions:, images: [image, jpeg], feature_name: "test")
+
+ payload = JSON.parse(AiApiAuditLog.find(result.audit_log_id).raw_request_payload)
+ expect(payload["state"]).to eq(sent["state"])
+ expect(payload["assets"].keys).to eq(sent["assets"].keys)
+ expect(payload["assets"].values).to eq(
+ [
+ {
+ "type" => "image",
+ "format" => "png",
+ "data_base64" => "[redacted image/png, 3 Bytes]",
+ },
+ {
+ "type" => "image",
+ "format" => "jpeg",
+ "data_base64" => "[redacted image/jpeg, 3 Bytes]",
+ },
+ ],
+ )
+ expect(payload["questions"]).to eq(sent["questions"])
+ expect(payload).not_to have_key("images")
+ expect(request).to have_been_requested.once
+ end
+
+ it "preserves structured state and normalizes the JPEG alias for image assets" do
+ state = { posts: [{ text: "Hello" }] }
+ request =
+ stub_request(:post, decision_model.url)
+ .with do |req|
+ payload = JSON.parse(req.body)
+ reference = payload.dig("state", "images", 0)
+ payload.dig("state", "text") == state.deep_stringify_keys &&
+ payload["assets"] ==
+ { reference => { "type" => "image", "format" => "jpeg", "data_base64" => "AAAA" } }
+ end
+ .to_return(body: response_body.to_json)
+
+ client.decide(state, questions:, images: ["data:image/jpg;base64,AAAA"], feature_name: "test")
+
+ expect(request).to have_been_requested.once
+ end
+
+ it "rejects unsupported image formats before transport" do
+ expect {
+ client.decide(
+ "Hello",
+ questions:,
+ images: ["data:image/webp;base64,AAAA"],
+ feature_name: "test",
+ )
+ }.to raise_error(DiscourseAi::Decisions::InvalidRequest, /PNG or JPEG/)
+ expect(AiApiAuditLog.count).to eq(0)
+ end
+
+ it "preserves text-only string and structured state without adding image fields" do
+ ["Hello", { posts: [{ text: "Hello" }] }].each do |state|
+ sent = nil
+ request =
+ stub_request(:post, decision_model.url)
+ .with do |req|
+ sent = JSON.parse(req.body)
+ sent["state"] == JSON.parse(state.to_json) && (sent.keys & %w[images assets]).empty?
+ end
+ .to_return(body: response_body.to_json)
+
+ result = client.decide(state, questions:, feature_name: "test")
+
+ expect(result["urgent"].probability).to eq(0.95)
+ log = AiApiAuditLog.find(result.audit_log_id)
+ expect(log.provider_id).to eq(AiApiAuditLog::Provider::SystemOne)
+ expect(JSON.parse(log.raw_request_payload)).to eq(sent)
+ expect(request).to have_been_requested
+ end
+ end
+
+ it "shares custom and registered routing headers with System One" do
+ decision_model.update!(custom_headers: [{ name: "X-Custom-Key", value: "sensitive" }])
+ DiscourseAi::RequestHeaders.register { |_context| { "X-Test-Feature" => "workflow" } }
+ request =
+ stub_request(:post, decision_model.url).with(
+ headers: {
+ "X-Custom-Key" => "sensitive",
+ "X-Test-Feature" => "workflow",
+ "Content-Type" => "application/json",
+ "Authorization" => "Bearer #{decision_model.api_key}",
+ },
+ ).to_return(body: response_body.to_json)
+
+ client.decide("Hello", questions:, feature_name: "workflow")
+
+ expect(request).to have_been_requested.once
+ end
+ end
+
describe "#decide with OpenAI" do
let(:open_ai_body) do
{
@@ -551,6 +790,33 @@ RSpec.describe DiscourseAi::Decisions::Client do
).to match(%r{\A\[redacted image/\w+, [\d.]+ \w+\]\z})
end
+ it "redacts only OpenAI image content and preserves matching image text in the state" do
+ image = "data:image/png;base64,AAAA"
+ state = image + "x" * 20_001
+ request =
+ stub_request(:post, decision_model.url)
+ .with do |req|
+ payload = JSON.parse(req.body)
+ payload.dig("input", 0, "content") ==
+ [
+ { "type" => "input_text", "text" => state },
+ { "type" => "input_image", "image_url" => image },
+ ] && !payload.key?("assets")
+ end
+ .to_return(body: open_ai_body.to_json)
+
+ result = client.decide(state, questions:, images: [image], feature_name: "test")
+
+ payload = JSON.parse(AiApiAuditLog.find(result.audit_log_id).raw_request_payload)
+ expect(payload.dig("input", 0, "content")).to eq(
+ [
+ { "type" => "input_text", "text" => state },
+ { "type" => "input_image", "image_url" => "[redacted image/png, 3 Bytes]" },
+ ],
+ )
+ expect(request).to have_been_requested.once
+ end
+
it "returns refusal text" do
open_ai_body[:answers][0] = { name: "urgent", type: "refusal", refusal: "Cannot answer" }
stub_request(:post, decision_model.url).to_return(body: open_ai_body.to_json)
@@ -587,6 +853,75 @@ RSpec.describe DiscourseAi::Decisions::Client do
describe "request header providers" do
after { DiscourseAi::RequestHeaders.reset! }
+ it "sends persisted custom headers with System One requests without including them in audit payloads" do
+ headers = [{ "name" => "X-Custom-Key", "value" => "sensitive-header-value" }]
+ decision_model.update!(custom_headers: headers)
+ decision_model.reload
+ request =
+ stub_request(:post, decision_model.url).with(
+ headers: {
+ "X-Custom-Key" => headers.first["value"],
+ "Content-Type" => "application/json",
+ "Authorization" => "Bearer #{decision_model.api_key}",
+ },
+ ).to_return(body: response_body.to_json)
+
+ result = client.decide("Hello", questions:, feature_name: "workflow")
+
+ expect(request).to have_been_requested.once
+ expect(AiApiAuditLog.find(result.audit_log_id).raw_request_payload).not_to include(
+ headers.first["value"],
+ )
+ end
+
+ it "keeps registered routing headers authoritative across case variants" do
+ decision_model.update!(
+ custom_headers: [
+ { name: "x-test-feature", value: "custom override" },
+ { name: "X-Unrelated", value: "custom value" },
+ ],
+ )
+ DiscourseAi::RequestHeaders.register { |_context| { "X-Test-Feature" => "registered" } }
+ request =
+ stub_request(:post, decision_model.url).with(
+ headers: {
+ "X-Test-Feature" => "registered",
+ "X-Unrelated" => "custom value",
+ },
+ ).to_return(body: response_body.to_json)
+
+ client.decide("Hello", questions:, feature_name: "workflow")
+
+ expect(request).to have_been_requested.once
+ end
+
+ it "ignores unsafe custom headers stored without validation" do
+ decision_model.update_columns(
+ custom_headers: [
+ { name: "aUtHoRiZaTiOn", value: "attacker" },
+ { name: "content-TYPE", value: "text/plain" },
+ { name: "Host", value: "attacker.example.com" },
+ { name: "X-Unsafe", value: "bad\r\nInjected: value" },
+ ],
+ )
+ request =
+ stub_request(:post, decision_model.url)
+ .with(
+ headers: {
+ "Authorization" => "Bearer #{decision_model.api_key}",
+ "Content-Type" => "application/json",
+ },
+ )
+ .with do |req|
+ !req.headers.key?("X-Unsafe") && req.headers["Host"] != "attacker.example.com"
+ end
+ .to_return(body: response_body.to_json)
+
+ client.decide("Hello", questions:, feature_name: "workflow")
+
+ expect(request).to have_been_requested.once
+ end
+
it "sends registered routing headers with System One decisions" do
captured = nil
DiscourseAi::RequestHeaders.register do |context|
@@ -613,12 +948,13 @@ RSpec.describe DiscourseAi::Decisions::Client do
expect(captured).to have_attributes(model: decision_model, has_images: true, streaming: false)
end
- it "does not send them to OpenAI" do
+ it "does not send registered or validation-bypassing custom headers to OpenAI" do
DiscourseAi::RequestHeaders.register { |_context| { "X-Test-Feature" => "x" } }
decision_model.update!(provider: "open_ai", url: "https://api.openai.com/v1/decisions")
+ decision_model.update_columns(custom_headers: [{ name: "X-Custom-Key", value: "secret" }])
request =
stub_request(:post, decision_model.url)
- .with { |req| !req.headers.key?("X-Test-Feature") }
+ .with { |req| (req.headers.keys & %w[X-Test-Feature X-Custom-Key]).empty? }
.to_return(status: 400, body: "{}")
expect { client.decide("Hello", questions:, feature_name: "workflow") }.to raise_error(
diff --git a/plugins/discourse-ai/spec/lib/decisions/endpoints/base_spec.rb b/plugins/discourse-ai/spec/lib/decisions/endpoints/base_spec.rb
new file mode 100644
index 00000000000..eb6e6124100
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/decisions/endpoints/base_spec.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Decisions::Endpoints::Base do
+ before { enable_current_plugin }
+
+ describe ".for" do
+ it "dispatches each provider to its dedicated endpoint" do
+ {
+ "system_one" => DiscourseAi::Decisions::Endpoints::SystemOne,
+ "typesafe" => DiscourseAi::Decisions::Endpoints::Typesafe,
+ "open_ai" => DiscourseAi::Decisions::Endpoints::OpenAi,
+ }.each do |provider, endpoint_class|
+ model = Fabricate.build(:decision_model, provider:)
+ expect(described_class.for(model)).to be_an_instance_of(endpoint_class)
+ end
+ end
+
+ it "rejects unknown providers" do
+ model = Fabricate.build(:decision_model, provider: "unknown")
+ expect { described_class.for(model) }.to raise_error(DiscourseAi::Decisions::InvalidRequest)
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/decisions/request_failed_spec.rb b/plugins/discourse-ai/spec/lib/decisions/request_failed_spec.rb
new file mode 100644
index 00000000000..85997a07026
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/decisions/request_failed_spec.rb
@@ -0,0 +1,122 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Decisions::RequestFailed do
+ describe ".redact" do
+ it "redacts short header tokens without changing words containing configuration flags" do
+ expect(
+ described_class.redact(
+ "untrue falsehood: true false; rejected 'abc' and 7",
+ nil,
+ header_values: %w[true false abc 7],
+ ),
+ ).to eq("untrue falsehood: [redacted] [redacted]; rejected '[redacted]' and [redacted]")
+ end
+
+ it "preserves short header values within identifiers and versions while scrubbing standalone echoes" do
+ expect(
+ described_class.redact(
+ "version0.1 jev-1.13.0 prefix-xy-suffix: '1' and 'xy'",
+ nil,
+ header_values: %w[1 xy],
+ ),
+ ).to eq("version0.1 jev-1.13.0 prefix-xy-suffix: '[redacted]' and '[redacted]'")
+ end
+
+ it "redacts short credentials next to sentence punctuation" do
+ expect(
+ described_class.redact(
+ "Invalid token ab12. Also rejected (ab12), ab12! and ab12...",
+ nil,
+ header_values: ["ab12"],
+ ),
+ ).to eq("Invalid token [redacted]. Also rejected ([redacted]), [redacted]! and [redacted]...")
+ end
+
+ it "redacts API keys as substrings even when they are short or match configuration flags" do
+ expect(described_class.redact("untrue true", "true", header_values: ["true"])).to eq(
+ "un[redacted] [redacted]",
+ )
+ expect(described_class.redact("prefix-xs-suffix", "xs")).to eq("prefix-[redacted]-suffix")
+ end
+
+ it "replaces overlapping values once without redacting the replacement" do
+ expect(
+ described_class.redact(
+ "private-token token redacted acted Bearer private-token key-example",
+ nil,
+ header_values: %w[token private-token redacted acted],
+ ),
+ ).to eq("[redacted] [redacted] [redacted] [redacted] Bearer [redacted] [redacted]")
+ end
+ end
+
+ describe ".redact_response" do
+ it "preserves JSON booleans, numbers and field names while redacting string echoes" do
+ body = {
+ success: true,
+ errors: false,
+ result: {
+ probabilities: {
+ "0" => 0.1,
+ "1" => 0.9,
+ },
+ count: 1,
+ },
+ echoes: ["true", "false", "1", "xy", nil],
+ }
+
+ redacted =
+ described_class.redact_response(body.to_json, nil, header_values: %w[true false 1 xy])
+
+ expect(JSON.parse(redacted)).to eq(
+ "success" => true,
+ "errors" => false,
+ "result" => {
+ "probabilities" => {
+ "0" => 0.1,
+ "1" => 0.9,
+ },
+ "count" => 1,
+ },
+ "echoes" => ["[redacted]", "[redacted]", "[redacted]", "[redacted]", nil],
+ )
+ end
+
+ it "preserves scalar JSON responses" do
+ [true, false, 1, 0.1, nil].each do |scalar|
+ expect(
+ JSON.parse(
+ described_class.redact_response(scalar.to_json, "1", header_values: %w[true false]),
+ ),
+ ).to eq(scalar)
+ end
+ end
+
+ it "redacts escaped credentials after decoding JSON and keeps the stored JSON valid" do
+ header_value = 'a"b\\c'
+ body = { message: "Rejected #{header_value}", nested: [header_value] }.to_json
+
+ expect(
+ JSON.parse(described_class.redact_response(body, nil, header_values: [header_value])),
+ ).to eq("message" => "Rejected [redacted]", "nested" => ["[redacted]"])
+ expect(JSON.parse(described_class.redact_response('"\\u0061\\u0062"', "ab"))).to eq(
+ "[redacted]",
+ )
+ end
+
+ it "scrubs API keys in JSON field names" do
+ expect(JSON.parse(described_class.redact_response('{"xs": "value"}', "xs"))).to eq(
+ "[redacted]" => "value",
+ )
+ end
+
+ it "scrubs non-JSON diagnostics including JSON-escaped header values" do
+ header_value = 'a"b\\c'
+ text = "untrue: rejected #{header_value.to_json} and xy"
+
+ expect(
+ described_class.redact_response(text, nil, header_values: ["true", header_value, "xy"]),
+ ).to eq('untrue: rejected "[redacted]" and [redacted]')
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb
index 6fd1de0400c..ee37d8aa476 100644
--- a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb
@@ -165,11 +165,20 @@ RSpec.describe DiscourseWorkflows::Nodes::Classify::V1 do
end
it "sends image uploads to a vision-enabled model" do
- decision_model.update!(vision_enabled: true)
+ decision_model.update!(
+ vision_enabled: true,
+ custom_headers: [{ name: "X-Image-Input", value: "true" }],
+ )
upload = Fabricate(:image_upload)
request =
stub_request(:post, decision_model.url)
- .with { |req| JSON.parse(req.body)["images"].sole.start_with?("data:image/png;base64,") }
+ .with(headers: { "X-Image-Input" => "true" }) do |req|
+ payload = JSON.parse(req.body)
+ image = payload.fetch("images").sole
+ payload.fetch("state") == "Lunch" && !payload.key?("assets") &&
+ image.start_with?("data:image/png;base64,") &&
+ Base64.strict_decode64(image.split(",", 2).last).start_with?("\x89PNG".b)
+ end
.to_return(body: { answers: { is_hotdog: { type: "noul", noul: 0.97 } } }.to_json)
config =
configuration(
diff --git a/plugins/discourse-ai/spec/models/decision_model_spec.rb b/plugins/discourse-ai/spec/models/decision_model_spec.rb
index 2deb7427b00..2332207ca65 100644
--- a/plugins/discourse-ai/spec/models/decision_model_spec.rb
+++ b/plugins/discourse-ai/spec/models/decision_model_spec.rb
@@ -36,10 +36,124 @@ RSpec.describe DecisionModel do
end
end
+ describe "custom headers" do
+ it "persists header rows, accepts boundary lengths and allows clearing them" do
+ headers = [
+ { "name" => "X-Token", "value" => "sensitive token" },
+ { "name" => "X" * 128, "value" => "x" * 4000 },
+ { "name" => "X-Empty", "value" => "" },
+ { "name" => "X-!#$%&'*+-.^_`|~", "value" => " !~" },
+ ]
+ model = Fabricate(:decision_model, custom_headers: headers)
+ expect(model.reload.custom_headers).to eq(headers)
+ expect(Fabricate.build(:decision_model).custom_headers).to eq([])
+ model.update!(custom_headers: [])
+ expect(model.reload.custom_headers).to eq([])
+
+ model.custom_headers = 20.times.map { |index| { name: "X-#{index}", value: "ok" } }
+ expect(model).to be_valid
+ end
+
+ it "rejects invalid shapes, names, values, duplicate names and too many rows" do
+ invalid_headers = [
+ nil,
+ {},
+ "X-Token: secret",
+ ["X-Token"],
+ [{ name: "X-Token" }],
+ [{ name: "X-Token", value: "ok", extra: "unexpected" }],
+ [{ name: "", value: "ok" }],
+ [{ name: "X" * 129, value: "ok" }],
+ [{ name: "X Token", value: "ok" }],
+ [{ name: "X:Token", value: "ok" }],
+ [{ name: "X-Token\r\nInjected", value: "ok" }],
+ [{ name: "X-☃", value: "ok" }],
+ [{ name: 123, value: "ok" }],
+ [{ name: "X-Token", value: nil }],
+ [{ name: "X-Token", value: 123 }],
+ [{ name: "X-Token", value: { secret: "bad" } }],
+ [{ name: "X-Token", value: "x" * 4001 }],
+ [{ name: "X-Token", value: "☃" }],
+ [{ name: "X-Token", value: "first\r\nInjected: header" }],
+ [{ name: "X-Token", value: "first\nsecond" }],
+ [{ name: "X-Token", value: "first\tsecond" }],
+ [{ name: "X-Token", value: "\u0000" }],
+ [{ name: "X-Token", value: "\u007F" }],
+ [{ name: "X-Token", value: "one" }, { name: "x-token", value: "two" }],
+ 21.times.map { |index| { name: "X-#{index}", value: "ok" } },
+ ]
+ %w[system_one typesafe].each do |provider|
+ invalid_headers.each do |headers|
+ model = Fabricate.build(:decision_model, provider:, custom_headers: headers)
+ expect(model).not_to be_valid
+ expect(model.errors[:custom_headers]).to be_present
+ expect(model.custom_headers_for_request).to eq({})
+ end
+ end
+ end
+
+ it "rejects reserved headers regardless of case" do
+ DecisionModel::RESERVED_CUSTOM_HEADERS.each do |name|
+ model =
+ Fabricate.build(:decision_model, custom_headers: [{ name: name.upcase, value: "x" }])
+ expect(model).not_to be_valid
+ expect(model.errors[:custom_headers]).to include(
+ I18n.t("discourse_ai.decision_models.custom_headers.reserved_name"),
+ )
+ end
+ end
+
+ it "only permits nonempty custom headers for System One and TypeSafe" do
+ model = Fabricate.build(:decision_model, custom_headers: [{ name: "X-Token", value: "x" }])
+ %w[system_one typesafe].each do |provider|
+ model.provider = provider
+ expect(model).to be_valid
+ expect(model.custom_headers_for_request).to eq("X-Token" => "x")
+ end
+ model.provider = "open_ai"
+ expect(model).not_to be_valid
+ expect(model.errors[:custom_headers]).to include(
+ I18n.t("discourse_ai.decision_models.custom_headers.unsupported_provider"),
+ )
+ model.custom_headers = []
+ expect(model).to be_valid
+ end
+ end
+
describe ".presets" do
+ it "provides a separate image-enabled Jev configuration" do
+ preset = described_class.presets.find { |item| item[:preset_id] == "jev-latest-vision" }
+ expect(preset).to include(
+ name: "jev-latest",
+ provider: "typesafe",
+ url: "https://api.typesafe.ai/v1/systemone",
+ vision_enabled: true,
+ custom_headers: [{ name: "TypeSafe-Danger-Zone-Images", value: "true" }],
+ )
+ expect(preset[:custom_headers]).to be_frozen
+ expect(preset[:custom_headers]).to all(be_frozen)
+ expect(described_class.new(preset.except(:preset_id))).to be_valid
+ text_preset = described_class.presets.find { |item| item[:preset_id] == "jev-latest" }
+ expect(text_preset).to include(provider: "typesafe", vision_enabled: false)
+ expect(described_class.new(text_preset.except(:preset_id))).to be_valid
+ expect(
+ described_class
+ .presets
+ .select { |item| item[:provider] == "system_one" }
+ .map { |item| item[:preset_id] },
+ ).to eq(%w[vllm-sr-d3 cloudflare-clef cloudflare-clef-flash])
+ end
+
it "provides immutable preset metadata" do
- expect(described_class.presets.map { |preset| preset[:name] }).to eq(
- %w[jev-latest vllm-sr/d3 clef clef-flash gpt-6-luna],
+ expect(described_class.presets.map { |preset| preset[:preset_id] }).to eq(
+ %w[
+ jev-latest
+ jev-latest-vision
+ vllm-sr-d3
+ cloudflare-clef
+ cloudflare-clef-flash
+ gpt-6-luna
+ ],
)
expect(described_class.presets).to be_frozen
expect(described_class.presets).to all(be_frozen)
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_decision_models_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_decision_models_controller_spec.rb
index 8d20c023c01..910098065ab 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_decision_models_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_decision_models_controller_spec.rb
@@ -12,6 +12,7 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
url: "https://api.typesafe.ai/v1/systemone",
ai_secret_id: ai_secret.id,
vision_enabled: false,
+ custom_headers: [{ "name" => "X-Custom-Key", "value" => "sensitive-header-value" }],
}
end
@@ -24,13 +25,20 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
fab!(:decision_model)
it "lists decision models with providers, presets and secrets" do
+ decision_model.update!(custom_headers: valid_attrs[:custom_headers])
get "/admin/plugins/discourse-ai/ai-decision-models.json"
expect(response.status).to eq(200)
expect(
response.parsed_body["ai_decision_models"].map { |model| model["id"] },
).to contain_exactly(decision_model.id)
- expect(response.parsed_body.dig("meta", "providers")).to eq(%w[system_one open_ai])
+ expect(response.parsed_body["ai_decision_models"].first["custom_headers"]).to eq(
+ valid_attrs[:custom_headers],
+ )
+ expect(response.parsed_body.dig("meta", "providers")).to eq(%w[system_one typesafe open_ai])
+ expect(response.parsed_body.dig("meta", "custom_header_limits")).to eq(
+ DecisionModel::CUSTOM_HEADER_LIMITS.stringify_keys,
+ )
expect(
response.parsed_body.dig("meta", "presets").map { |preset| preset["preset_id"] },
).to eq(DecisionModel.presets.map { |preset| preset[:preset_id] })
@@ -74,9 +82,16 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
expect(response.status).to eq(201)
expect(DecisionModel.last).to have_attributes(valid_attrs)
+ expect(response.parsed_body.dig("ai_decision_model", "custom_headers")).to eq(
+ valid_attrs[:custom_headers],
+ )
log = UserHistory.find_by!(custom_type: "create_ai_decision_model")
expect(log.details).to include(ai_secret.id.to_s)
- expect(log.details).not_to include(ai_secret.secret)
+ expect(log.details).to include("[FILTERED]")
+ expect(log.details).not_to include(
+ ai_secret.secret,
+ valid_attrs[:custom_headers].first["value"],
+ )
expect(
UserHistory.where(
action: UserHistory.actions[:custom_staff],
@@ -85,6 +100,28 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
).to eq(1)
end
+ it "rejects malformed header JSON, reserved headers and nonempty OpenAI headers" do
+ [
+ valid_attrs.merge(custom_headers: "invalid"),
+ valid_attrs.merge(custom_headers: { name: "X-Token", value: "secret" }),
+ valid_attrs.merge(custom_headers: { "0" => { name: "X-Token", value: "secret" } }),
+ valid_attrs.merge(custom_headers: [{ name: "X-Token", value: { secret: "bad" } }]),
+ valid_attrs.merge(custom_headers: [{ name: "Authorization", value: "secret" }]),
+ valid_attrs.merge(provider: "open_ai"),
+ ].each do |attrs|
+ post "/admin/plugins/discourse-ai/ai-decision-models.json",
+ params: {
+ ai_decision_model: attrs,
+ },
+ as: :json
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to be_present
+ end
+ expect(DecisionModel.count).to eq(0)
+ expect(UserHistory.where(custom_type: "create_ai_decision_model").count).to eq(0)
+ end
+
it "returns an error for an unknown provider" do
post "/admin/plugins/discourse-ai/ai-decision-models.json",
params: {
@@ -99,6 +136,59 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
describe "#update" do
fab!(:decision_model)
+ it "persists, serializes and clears header updates without leaking values in staff logs" do
+ headers = valid_attrs[:custom_headers]
+ path = "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}.json"
+ put path, params: { ai_decision_model: { custom_headers: headers } }, as: :json
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body.dig("ai_decision_model", "custom_headers")).to eq(headers)
+ expect(decision_model.reload.custom_headers).to eq(headers)
+ expect(UserHistory.where(custom_type: "update_ai_decision_model").last.details).to include(
+ I18n.t("discourse_ai.ai_staff_action_logger.set"),
+ )
+
+ get "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}/edit.json"
+ expect(response.parsed_body.dig("ai_decision_model", "custom_headers")).to eq(headers)
+
+ replacement = [{ "name" => "x-custom-key", "value" => "replacement-secret" }]
+ put path, params: { ai_decision_model: { custom_headers: replacement } }, as: :json
+ expect(response.status).to eq(200)
+ expect(decision_model.reload.custom_headers).to eq(replacement)
+ expect(UserHistory.where(custom_type: "update_ai_decision_model").last.details).to include(
+ I18n.t("discourse_ai.ai_staff_action_logger.updated"),
+ )
+
+ put path, params: { ai_decision_model: { display_name: "Still configured" } }, as: :json
+ expect(decision_model.reload.custom_headers).to eq(replacement)
+
+ put path, params: { ai_decision_model: { custom_headers: [] } }, as: :json
+ expect(response.status).to eq(200)
+ expect(response.parsed_body.dig("ai_decision_model", "custom_headers")).to eq([])
+ expect(decision_model.reload.custom_headers).to eq([])
+ logs = UserHistory.where(custom_type: "update_ai_decision_model").order(:id)
+ expect(logs.last.details).to include(I18n.t("discourse_ai.ai_staff_action_logger.removed"))
+ expect(logs.map(&:details).join).not_to include(
+ headers.first["value"],
+ replacement.first["value"],
+ )
+ end
+
+ it "keeps persisted headers when an invalid update is rejected" do
+ decision_model.update!(custom_headers: valid_attrs[:custom_headers])
+ put "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}.json",
+ params: {
+ ai_decision_model: {
+ custom_headers: [{ name: "Host", value: "evil" }],
+ },
+ },
+ as: :json
+
+ expect(response.status).to eq(422)
+ expect(decision_model.reload.custom_headers).to eq(valid_attrs[:custom_headers])
+ expect(UserHistory.where(custom_type: "update_ai_decision_model").count).to eq(0)
+ end
+
it "updates the decision model and logs the staff action" do
put "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}.json",
params: {
@@ -133,6 +223,7 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
fab!(:decision_model)
it "deletes the decision model and logs the staff action" do
+ decision_model.update!(custom_headers: valid_attrs[:custom_headers])
delete "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}.json"
expect(response.status).to eq(204)
@@ -143,7 +234,11 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
decision_model.name,
decision_model.ai_secret_id.to_s,
)
- expect(log.details).not_to include(decision_model.api_key)
+ expect(log.details).to include("[FILTERED]")
+ expect(log.details).not_to include(
+ decision_model.api_key,
+ valid_attrs[:custom_headers].first["value"],
+ )
expect(
UserHistory.where(
action: UserHistory.actions[:custom_staff],
@@ -172,6 +267,25 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
expect(AiApiAuditLog.count).to eq(0)
end
+ it "sends unsaved custom headers when testing a model" do
+ request =
+ stub_request(:post, valid_attrs[:url]).with(
+ headers: {
+ "X-Custom-Key" => valid_attrs[:custom_headers].first["value"],
+ },
+ ).to_return(body: { answers: { greeting: { type: "noul", noul: 0.98 } } }.to_json)
+
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: valid_attrs,
+ },
+ as: :json
+
+ expect(response.parsed_body).to eq("success" => true, "probability" => 0.98)
+ expect(request).to have_been_requested.once
+ expect(DecisionModel.count).to eq(0)
+ end
+
it "reports refusals and validation errors" do
DiscourseAi::Decisions.with_canned_answers(greeting: :refusal) do
post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
@@ -190,6 +304,49 @@ RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
expect(response.parsed_body).to include("success" => false, "error" => include("URL"))
end
+ it "sends a 280×280 probe image as a referenced PNG asset" do
+ sent = nil
+ request =
+ stub_request(:post, valid_attrs[:url])
+ .with do |req|
+ sent = JSON.parse(req.body)
+ reference = sent.dig("state", "images", 0)
+ asset = sent.dig("assets", reference)
+ asset && asset["type"] == "image" && asset["format"] == "png" &&
+ "data:image/png;base64,#{asset["data_base64"]}" == described_class::TEST_IMAGE &&
+ FastImage.size(StringIO.new(Base64.strict_decode64(asset["data_base64"]))) ==
+ [280, 280] && !sent.key?("images")
+ end
+ .to_return(
+ body: {
+ answers: {
+ greeting: {
+ type: "noul",
+ noul: 0.98,
+ },
+ red_image: {
+ type: "noul",
+ noul: 0.99,
+ },
+ },
+ }.to_json,
+ )
+
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: valid_attrs.merge(provider: "typesafe", vision_enabled: true),
+ },
+ as: :json
+
+ expect(response.parsed_body).to eq("success" => true, "probability" => 0.98)
+ expect(request).to have_been_requested.once
+ payload = JSON.parse(AiApiAuditLog.last.raw_request_payload)
+ expect(payload["state"]).to eq(sent["state"])
+ expect(payload["assets"].keys).to eq(sent["assets"].keys)
+ expect(payload["assets"].values.first["data_base64"]).to start_with("[redacted image/png,")
+ expect(DecisionModel.count).to eq(0)
+ end
+
it "checks that vision-enabled models read a test image" do
attrs = valid_attrs.merge(vision_enabled: true)
diff --git a/plugins/discourse-ai/spec/serializers/ai_decision_model_serializer_spec.rb b/plugins/discourse-ai/spec/serializers/ai_decision_model_serializer_spec.rb
new file mode 100644
index 00000000000..1d5771e8f0c
--- /dev/null
+++ b/plugins/discourse-ai/spec/serializers/ai_decision_model_serializer_spec.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+RSpec.describe AiDecisionModelSerializer do
+ fab!(:admin)
+ fab!(:user)
+ fab!(:moderator)
+ fab!(:decision_model) do
+ Fabricate(:decision_model, custom_headers: [{ name: "X-Token", value: "sensitive-token" }])
+ end
+
+ before { enable_current_plugin }
+
+ it "only includes custom headers with an administrator scope" do
+ serialized = described_class.new(decision_model, scope: admin.guardian, root: false).as_json
+ expect(serialized[:custom_headers]).to eq(decision_model.custom_headers)
+
+ [nil, user.guardian, moderator.guardian].each do |scope|
+ serialized = described_class.new(decision_model, scope:, root: false).as_json
+ expect(serialized).not_to have_key(:custom_headers)
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb b/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb
index 3aeb9f02dcc..b13b0b1699b 100644
--- a/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb
+++ b/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb
@@ -10,6 +10,31 @@ RSpec.describe "Managing decision models" do
sign_in(admin)
end
+ it "keeps header fields aligned with the rest of the form at every layout size" do
+ visit "/admin/plugins/discourse-ai/ai-decision-models/new"
+ form.field("preset").select("jev-latest-vision")
+ expect(form.field("provider")).to have_value("typesafe")
+
+ [390, 1000, 1440].each do |width|
+ resize_window(width: width) do
+ try_until_success do
+ name_rect = find('[data-name="display_name"] input').rect
+ header_name_rect = find('[data-name="custom_headers.0.name"] input').rect
+ header_value_rect = find('[data-name="custom_headers.0.value"] input').rect
+
+ aggregate_failures("at #{width}px") do
+ [header_name_rect, header_value_rect].each do |header_rect|
+ expect(header_rect["x"]).to be_within(1).of(name_rect["x"])
+ expect(header_rect["width"]).to be_within(1).of(name_rect["width"])
+ end
+ expect(header_value_rect["y"]).to be >
+ header_name_rect["y"] + header_name_rect["height"]
+ end
+ end
+ end
+ end
+ end
+
it "creates from a preset and tests the model" do
DiscourseAi::Decisions.with_canned_answers(greeting: 0.97, red_image: 0.99) do
visit "/admin/plugins/discourse-ai/ai-decision-models"
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-decision-model-editor-form-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-decision-model-editor-form-test.gjs
new file mode 100644
index 00000000000..e902d9f9eae
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-decision-model-editor-form-test.gjs
@@ -0,0 +1,304 @@
+import { click, render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import formKit from "discourse/tests/helpers/form-kit-helper";
+import { i18n } from "discourse-i18n";
+import AiDecisionModelEditorForm from "discourse/plugins/discourse-ai/discourse/components/ai-decision-model-editor-form";
+
+module("Integration | Component | AiDecisionModelEditorForm", function (hooks) {
+ setupRenderingTest(hooks, { stubRouter: true });
+
+ hooks.beforeEach(function () {
+ const router = this.owner.lookup("service:router");
+ router.replaceWith = async () => {};
+ router.transitionTo = async () => {};
+
+ this.decisionModels = {
+ resultSetMeta: {
+ providers: ["system_one", "typesafe", "open_ai"],
+ presets: [],
+ ai_secrets: [],
+ custom_header_limits: {
+ count: 20,
+ name_length: 128,
+ value_length: 4000,
+ },
+ },
+ };
+ this.savedPayload = null;
+ this.model = {
+ id: 1,
+ isNew: false,
+ display_name: "Decisions",
+ name: "test-model",
+ provider: "system_one",
+ url: "https://example.com/decisions",
+ custom_headers: [{ name: "X-Image-Input", value: "true" }],
+ save: async (payload) => {
+ this.savedPayload = payload;
+ },
+ };
+ });
+
+ test("limits header rows using the server-provided maximum", async function (assert) {
+ this.decisionModels.resultSetMeta.custom_header_limits.count = 2;
+ await render(
+ <template>
+ <AiDecisionModelEditorForm
+ @decisionModels={{this.decisionModels}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ assert
+ .dom(".ai-decision-model-editor__add-header")
+ .isEnabled("allows another header below the limit");
+ await click(".ai-decision-model-editor__add-header");
+ assert
+ .dom(".ai-decision-model-editor__add-header")
+ .isDisabled("prevents adding more than the configured limit");
+ await click(".ai-decision-model-editor__remove-header");
+ assert
+ .dom(".ai-decision-model-editor__add-header")
+ .isEnabled("allows adding a header again after removing a row");
+ });
+
+ test("edits, adds and removes custom headers", async function (assert) {
+ await render(
+ <template>
+ <AiDecisionModelEditorForm
+ @decisionModels={{this.decisionModels}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+
+ assert
+ .dom(".ai-decision-model-editor__remove-header")
+ .hasAttribute(
+ "title",
+ i18n("discourse_ai.decision_models.custom_headers.remove"),
+ "the icon-only remove button has an accessible name"
+ );
+ assert
+ .form()
+ .field("custom_headers.0.name")
+ .hasValue("X-Image-Input", "loads the saved header name");
+ assert
+ .form()
+ .field("custom_headers.0.value")
+ .hasValue("true", "loads the saved header value");
+ await formKit().field("custom_headers.0.value").fillIn("false");
+ await click(".ai-decision-model-editor__add-header");
+ await formKit().field("custom_headers.1.name").fillIn("X-Mode");
+ await formKit().field("custom_headers.1.value").fillIn("images");
+ await formKit().submit();
+ assert.deepEqual(
+ this.savedPayload.custom_headers,
+ [
+ { name: "X-Image-Input", value: "false" },
+ { name: "X-Mode", value: "images" },
+ ],
+ "submits all header rows"
+ );
+ assert.strictEqual(
+ this.model.custom_headers[0].value,
+ "true",
+ "edits do not mutate the model before saving"
+ );
+
+ await click(".ai-decision-model-editor__remove-header");
+ await click(".ai-decision-model-editor__remove-header");
+ assert
+ .dom(".ai-decision-model-editor .form-kit__collection")
+ .doesNotExist("the empty header collection leaves no layout wrapper");
+ assert
+ .dom(".ai-decision-model-editor__add-header")
+ .isEnabled("the empty state still offers adding a header");
+ await formKit().submit();
+ assert.deepEqual(
+ this.savedPayload.custom_headers,
+ [],
+ "submits an empty array to clear headers"
+ );
+ });
+
+ test("image presets populate headers and text presets clear them", async function (assert) {
+ this.model.isNew = true;
+ this.decisionModels.content = [];
+ const imageHeader = { name: "TypeSafe-Danger-Zone-Images", value: "true" };
+ const textPreset = {
+ preset_id: "jev-latest",
+ display_name: "TypeSafe Jev",
+ name: "jev-latest",
+ provider: "typesafe",
+ url: "https://api.typesafe.ai/v1/systemone",
+ vision_enabled: false,
+ };
+ this.decisionModels.resultSetMeta.presets = [
+ textPreset,
+ {
+ ...textPreset,
+ preset_id: "jev-latest-vision",
+ display_name: "TypeSafe Jev with images (experimental)",
+ vision_enabled: true,
+ custom_headers: [imageHeader],
+ },
+ ];
+
+ await render(
+ <template>
+ <AiDecisionModelEditorForm
+ @decisionModels={{this.decisionModels}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ await formKit().field("preset").select("jev-latest-vision");
+ assert
+ .form()
+ .field("provider")
+ .hasValue("typesafe", "uses the dedicated TypeSafe endpoint");
+ assert.form().field("vision_enabled").hasValue(true, "enables image input");
+ assert
+ .form()
+ .field("custom_headers.0.name")
+ .hasValue(imageHeader.name, "populates the image header");
+ assert
+ .form()
+ .field("custom_headers.0.value")
+ .hasValue(imageHeader.value, "populates the header value");
+ await formKit().submit();
+ assert.deepEqual(
+ this.savedPayload.custom_headers,
+ [imageHeader],
+ "saves the image opt-in header"
+ );
+
+ await formKit().field("custom_headers.0.value").fillIn("false");
+ assert.strictEqual(
+ imageHeader.value,
+ "true",
+ "editing the draft leaves preset metadata unchanged"
+ );
+ await formKit().field("preset").select("jev-latest");
+ assert
+ .form()
+ .field("vision_enabled")
+ .hasValue(false, "disables image input for the text preset");
+ await formKit().submit();
+ assert.deepEqual(
+ this.savedPayload.custom_headers,
+ [],
+ "clears the opt-in header for the text preset"
+ );
+ });
+
+ test("selecting a preset clears headers unsupported by its provider", async function (assert) {
+ this.model.isNew = true;
+ this.decisionModels.content = [];
+ this.decisionModels.resultSetMeta.presets = [
+ {
+ preset_id: "open-ai-test",
+ display_name: "OpenAI test",
+ name: "test-model",
+ provider: "open_ai",
+ url: "https://example.com/decisions",
+ vision_enabled: true,
+ },
+ ];
+ let testedPayload;
+ this.model.testConfig = async (payload) => {
+ testedPayload = payload;
+ return { success: true, probability: 0.99 };
+ };
+
+ await render(
+ <template>
+ <AiDecisionModelEditorForm
+ @decisionModels={{this.decisionModels}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ await formKit().field("preset").select("open-ai-test");
+ assert
+ .dom(".ai-decision-model-editor__add-header")
+ .doesNotExist("hides unsupported headers after selecting the preset");
+ await click(".ai-decision-model-editor__test-btn");
+ assert.deepEqual(
+ testedPayload.custom_headers,
+ [],
+ "tests the preset without stale headers"
+ );
+ await formKit().submit();
+ assert.deepEqual(
+ this.savedPayload.custom_headers,
+ [],
+ "saves the preset without stale headers"
+ );
+ });
+
+ test("switching between System One and TypeSafe preserves custom headers", async function (assert) {
+ await render(
+ <template>
+ <AiDecisionModelEditorForm
+ @decisionModels={{this.decisionModels}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+
+ for (const provider of ["typesafe", "system_one"]) {
+ await formKit().field("provider").select(provider);
+ assert
+ .form()
+ .field("custom_headers.0.name")
+ .hasValue(
+ "X-Image-Input",
+ `preserves headers when switching to ${provider}`
+ );
+ await formKit().submit();
+ assert.strictEqual(
+ this.savedPayload.provider,
+ provider,
+ "submits the selected provider"
+ );
+ assert.deepEqual(
+ this.savedPayload.custom_headers,
+ [{ name: "X-Image-Input", value: "true" }],
+ "submits the preserved headers"
+ );
+ }
+ });
+
+ test("switching providers clears unsupported custom headers", async function (assert) {
+ this.model.provider = "typesafe";
+ await render(
+ <template>
+ <AiDecisionModelEditorForm
+ @decisionModels={{this.decisionModels}}
+ @model={{this.model}}
+ />
+ </template>
+ );
+ await formKit().field("provider").select("open_ai");
+ assert
+ .dom(".ai-decision-model-editor__add-header")
+ .doesNotExist("hides custom headers for other providers");
+ await formKit().submit();
+ assert.deepEqual(
+ this.savedPayload.custom_headers,
+ [],
+ "does not submit System One headers to another provider"
+ );
+ await formKit().field("provider").select("system_one");
+ assert
+ .dom(".ai-decision-model-editor__add-header")
+ .exists("shows custom headers for System One");
+ assert.false(
+ formKit().hasField("custom_headers.0.name"),
+ "does not restore stale headers"
+ );
+ });
+});
diff --git a/plugins/discourse-ai/test/javascripts/unit/models/ai-decision-model-test.js b/plugins/discourse-ai/test/javascripts/unit/models/ai-decision-model-test.js
new file mode 100644
index 00000000000..1df229dde27
--- /dev/null
+++ b/plugins/discourse-ai/test/javascripts/unit/models/ai-decision-model-test.js
@@ -0,0 +1,33 @@
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import pretender, { response } from "discourse/tests/helpers/create-pretender";
+import AiDecisionModel from "discourse/plugins/discourse-ai/discourse/admin/models/ai-decision-model";
+
+module("Unit | Model | ai-decision-model", function (hooks) {
+ setupTest(hooks);
+
+ test("tests the draft configuration as JSON to preserve header arrays", async function (assert) {
+ const config = {
+ name: "test-model",
+ custom_headers: [{ name: "X-Image-Input", value: "true" }],
+ };
+ pretender.post(
+ "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ (request) => {
+ assert.true(
+ request.requestHeaders["Content-Type"].startsWith("application/json"),
+ "sends JSON rather than indexed form parameters"
+ );
+ assert.deepEqual(
+ JSON.parse(request.requestBody),
+ { ai_decision_model: config },
+ "preserves the custom header array"
+ );
+ return response({ success: true, probability: 0.99 });
+ }
+ );
+
+ const result = await AiDecisionModel.create().testConfig(config);
+ assert.true(result.success, "returns the test response");
+ });
+});