Skip to content

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

FEATURE: Add decision models and classify workflow node (#44580)

Same12c1ce7acbc
diff --git a/db/structure.sql b/db/structure.sql
index c1453661736..1ac359839ce 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -4100,6 +4100,42 @@ CREATE SEQUENCE public.data_explorer_tags_id_seq
ALTER SEQUENCE public.data_explorer_tags_id_seq OWNED BY public.data_explorer_tags.id;
+--
+-- Name: decision_models; Type: TABLE; Schema: public; Owner: -
+--
+
+CREATE TABLE public.decision_models (
+ id bigint NOT NULL,
+ display_name character varying(100) NOT NULL,
+ name character varying(255) NOT NULL,
+ provider character varying(50) NOT NULL,
+ url character varying(1000) NOT NULL,
+ 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
+);
+
+
+--
+-- Name: decision_models_id_seq; Type: SEQUENCE; Schema: public; Owner: -
+--
+
+CREATE SEQUENCE public.decision_models_id_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+
+
+--
+-- Name: decision_models_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
+--
+
+ALTER SEQUENCE public.decision_models_id_seq OWNED BY public.decision_models.id;
+
+
--
-- Name: developers; Type: TABLE; Schema: public; Owner: -
--
@@ -14575,6 +14611,13 @@ ALTER TABLE ONLY public.data_explorer_query_tags ALTER COLUMN id SET DEFAULT nex
ALTER TABLE ONLY public.data_explorer_tags ALTER COLUMN id SET DEFAULT nextval('public.data_explorer_tags_id_seq'::regclass);
+--
+-- Name: decision_models id; Type: DEFAULT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.decision_models ALTER COLUMN id SET DEFAULT nextval('public.decision_models_id_seq'::regclass);
+
+
--
-- Name: developers id; Type: DEFAULT; Schema: public; Owner: -
--
@@ -17195,6 +17238,14 @@ ALTER TABLE ONLY public.data_explorer_tags
ADD CONSTRAINT data_explorer_tags_pkey PRIMARY KEY (id);
+--
+-- Name: decision_models decision_models_pkey; Type: CONSTRAINT; Schema: public; Owner: -
+--
+
+ALTER TABLE ONLY public.decision_models
+ ADD CONSTRAINT decision_models_pkey PRIMARY KEY (id);
+
+
--
-- Name: developers developers_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
@@ -21650,6 +21701,13 @@ CREATE UNIQUE INDEX index_data_explorer_query_groups_on_query_id_and_group_id ON
CREATE UNIQUE INDEX index_data_explorer_query_stats_on_query_id_and_date ON public.data_explorer_query_stats USING btree (query_id, date);
+--
+-- Name: index_decision_models_on_ai_secret_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE INDEX index_decision_models_on_ai_secret_id ON public.decision_models USING btree (ai_secret_id);
+
+
--
-- Name: index_developers_on_user_id; Type: INDEX; Schema: public; Owner: -
--
@@ -25855,6 +25913,7 @@ SET search_path TO "$user", public;
INSERT INTO "schema_migrations" (version) VALUES
('20261014145458'),
+('20261013222431'),
('20261013183208'),
('20261013010026'),
('20261013010022'),
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
new file mode 100644
index 00000000000..34f1a641fc5
--- /dev/null
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-decision-model-editor-form.gjs
@@ -0,0 +1,282 @@
+import Component from "@glimmer/component";
+import { cached, tracked } from "@glimmer/tracking";
+import { concat, fn } from "@ember/helper";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import BackButton from "discourse/components/back-button";
+import Form from "discourse/components/form";
+import { popupAjaxError } from "discourse/lib/ajax-error";
+import {
+ addUniqueValueToArray,
+ removeValueFromArray,
+} from "discourse/lib/array-tools";
+import { i18n } from "discourse-i18n";
+import AiSecretSelector from "./ai-secret-selector";
+
+export default class AiDecisionModelEditorForm extends Component {
+ @service dialog;
+ @service router;
+ @service toasts;
+
+ @tracked testResult = null;
+ @tracked testRunning = false;
+
+ @cached
+ get formData() {
+ const { model } = this.args;
+
+ return {
+ display_name: model.display_name,
+ name: model.name,
+ provider: model.provider || this.providers[0],
+ url: model.url,
+ ai_secret_id: model.ai_secret_id,
+ vision_enabled: model.vision_enabled ?? false,
+ };
+ }
+
+ get presets() {
+ return this.args.decisionModels.resultSetMeta.presets;
+ }
+
+ get providers() {
+ return this.args.decisionModels.resultSetMeta.providers;
+ }
+
+ get secrets() {
+ return this.args.decisionModels.resultSetMeta.ai_secrets;
+ }
+
+ get testPercentage() {
+ return Math.round(this.testResult.probability * 100);
+ }
+
+ @action
+ delete() {
+ return this.dialog.confirm({
+ message: i18n("discourse_ai.decision_models.confirm_delete"),
+ didConfirm: async () => {
+ try {
+ await this.args.model.destroyRecord();
+ removeValueFromArray(
+ this.args.decisionModels.content,
+ this.args.model
+ );
+ this.router.transitionTo(
+ "adminPlugins.show.discourse-ai-decision-models.index"
+ );
+ } catch (e) {
+ popupAjaxError(e);
+ }
+ },
+ });
+ }
+
+ @action
+ async save(data) {
+ const isNew = this.args.model.isNew;
+
+ try {
+ await this.args.model.save(data);
+
+ if (isNew) {
+ addUniqueValueToArray(
+ this.args.decisionModels.content,
+ this.args.model
+ );
+ this.router.replaceWith(
+ "adminPlugins.show.discourse-ai-decision-models.edit",
+ this.args.model.id
+ );
+ }
+
+ this.toasts.success({
+ data: { message: i18n("discourse_ai.decision_models.saved") },
+ duration: "short",
+ });
+ } catch (e) {
+ popupAjaxError(e);
+ }
+ }
+
+ @action
+ async selectPreset(value, { set }) {
+ 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",
+ ]) {
+ await set(name, preset[name]);
+ }
+ }
+ this.testResult = null;
+ }
+
+ @action
+ async test(data) {
+ this.testRunning = true;
+
+ try {
+ this.testResult = await this.args.model.testConfig(data);
+ } catch (e) {
+ popupAjaxError(e);
+ } finally {
+ this.testRunning = false;
+ }
+ }
+
+ <template>
+ <BackButton
+ @label="discourse_ai.decision_models.back"
+ @route="adminPlugins.show.discourse-ai-decision-models"
+ />
+ <Form
+ class="ai-decision-model-editor"
+ @data={{this.formData}}
+ @onSubmit={{this.save}}
+ as |form data|
+ >
+ {{#if @model.isNew}}
+ <form.Field
+ @name="preset"
+ @onSet={{this.selectPreset}}
+ @title={{i18n "discourse_ai.decision_models.start_from"}}
+ @type="select"
+ as |field|
+ >
+ <field.Control
+ @nonePlaceholder={{i18n "discourse_ai.decision_models.no_preset"}}
+ as |select|
+ >
+ {{#each this.presets as |preset|}}
+ <select.Option
+ @value={{preset.preset_id}}
+ >{{preset.display_name}}</select.Option>
+ {{/each}}
+ </field.Control>
+ </form.Field>
+ {{/if}}
+ <form.Field
+ @format="large"
+ @name="display_name"
+ @title={{i18n "discourse_ai.decision_models.display_name"}}
+ @type="input"
+ @validation="required|length:1,100"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+
+ <form.Field
+ @format="large"
+ @name="provider"
+ @title={{i18n "discourse_ai.decision_models.provider"}}
+ @type="select"
+ @validation="required"
+ as |field|
+ >
+ <field.Control @includeNone={{false}} as |select|>
+ {{#each this.providers as |option|}}
+ <select.Option @value={{option}}>
+ {{i18n (concat "discourse_ai.decision_models.providers." option)}}
+ </select.Option>
+ {{/each}}
+ </field.Control>
+ </form.Field>
+
+ <form.Field
+ @format="large"
+ @name="name"
+ @title={{i18n "discourse_ai.decision_models.model"}}
+ @type="input"
+ @validation="required|length:1,255"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+
+ <form.Field
+ @format="large"
+ @name="url"
+ @title={{i18n "discourse_ai.decision_models.url"}}
+ @type="input"
+ @validation="required|length:1,1000"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+
+ <form.Field
+ @format="large"
+ @name="ai_secret_id"
+ @title={{i18n "discourse_ai.decision_models.api_key"}}
+ @type="custom"
+ as |field|
+ >
+ <field.Control>
+ <AiSecretSelector
+ @onChange={{field.set}}
+ @secrets={{this.secrets}}
+ @value={{field.value}}
+ />
+ </field.Control>
+ </form.Field>
+
+ <form.Field
+ @format="large"
+ @name="vision_enabled"
+ @showTitle={{false}}
+ @title={{i18n "discourse_ai.decision_models.vision_enabled"}}
+ @type="checkbox"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+
+ <form.Actions>
+ <form.Submit />
+ <form.Button
+ class="btn-default ai-decision-model-editor__test-btn"
+ @action={{fn this.test data}}
+ @isLoading={{this.testRunning}}
+ @label="discourse_ai.decision_models.test"
+ />
+ {{#unless @model.isNew}}
+ <form.Button
+ class="btn-danger ai-decision-model-editor__delete-btn"
+ @action={{this.delete}}
+ @label="discourse_ai.decision_models.delete"
+ />
+ {{/unless}}
+ </form.Actions>
+
+ {{#if this.testResult}}
+ {{#if this.testResult.success}}
+ <form.Alert
+ class="ai-decision-model-editor__test-result"
+ @icon="check"
+ @type="success"
+ >
+ {{i18n
+ "discourse_ai.decision_models.test_success"
+ percentage=this.testPercentage
+ }}
+ </form.Alert>
+ {{else}}
+ <form.Alert
+ class="ai-decision-model-editor__test-result"
+ @icon="xmark"
+ @type="error"
+ >
+ {{this.testResult.error}}
+ </form.Alert>
+ {{/if}}
+ {{/if}}
+ </Form>
+ </template>
+}
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-decision-models-list-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-decision-models-list-editor.gjs
new file mode 100644
index 00000000000..4b1de814648
--- /dev/null
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-decision-models-list-editor.gjs
@@ -0,0 +1,85 @@
+import { concat } from "@ember/helper";
+import AdminConfigAreaEmptyList from "discourse/admin/components/admin-config-area-empty-list";
+import DBreadcrumbsItem from "discourse/ui-kit/d-breadcrumbs-item";
+import DButton from "discourse/ui-kit/d-button";
+import DPageSubheader from "discourse/ui-kit/d-page-subheader";
+import { i18n } from "discourse-i18n";
+
+export default <template>
+ <DBreadcrumbsItem
+ @label={{i18n "discourse_ai.decision_models.short_title"}}
+ @route="adminPlugins.show.discourse-ai-decision-models"
+ />
+ <section class="ai-decision-models-list-editor admin-detail">
+ <DPageSubheader
+ @descriptionLabel={{i18n "discourse_ai.decision_models.description"}}
+ @titleLabel={{i18n "discourse_ai.decision_models.short_title"}}
+ >
+ <:actions as |actions|>
+ <actions.Primary
+ class="ai-decision-models-list-editor__new-btn"
+ @icon="plus"
+ @label="discourse_ai.decision_models.create_new"
+ @route="adminPlugins.show.discourse-ai-decision-models.new"
+ />
+ </:actions>
+ </DPageSubheader>
+
+ {{#if @decisionModels.content.length}}
+ <table class="d-table">
+ <thead class="d-table__header">
+ <tr>
+ <th>{{i18n "discourse_ai.decision_models.display_name"}}</th>
+ <th>{{i18n "discourse_ai.decision_models.model"}}</th>
+ <th>{{i18n "discourse_ai.decision_models.provider"}}</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ {{#each @decisionModels.content as |decisionModel|}}
+ <tr
+ class="ai-decision-models-list-editor__row d-table__row"
+ data-decision-model-id={{decisionModel.id}}
+ >
+ <td class="d-table__cell --overview">
+ <strong>{{decisionModel.display_name}}</strong>
+ </td>
+ <td class="d-table__cell --detail">
+ <div class="d-table__mobile-label">
+ {{i18n "discourse_ai.decision_models.model"}}
+ </div>
+ {{decisionModel.name}}
+ </td>
+ <td class="d-table__cell --detail">
+ <div class="d-table__mobile-label">
+ {{i18n "discourse_ai.decision_models.provider"}}
+ </div>
+ {{i18n
+ (concat
+ "discourse_ai.decision_models.providers."
+ decisionModel.provider
+ )
+ }}
+ </td>
+ <td class="d-table__cell --controls">
+ <DButton
+ class="btn-default btn-small ai-decision-models-list-editor__edit-btn"
+ @label="discourse_ai.decision_models.edit"
+ @route="adminPlugins.show.discourse-ai-decision-models.edit"
+ @routeModels={{decisionModel.id}}
+ />
+ </td>
+ </tr>
+ {{/each}}
+ </tbody>
+ </table>
+ {{else}}
+ <AdminConfigAreaEmptyList
+ @ctaClass="ai-decision-models-list-editor__empty-new-btn"
+ @ctaLabel="discourse_ai.decision_models.create_new"
+ @ctaRoute="adminPlugins.show.discourse-ai-decision-models.new"
+ @emptyLabel="discourse_ai.decision_models.no_decision_models"
+ />
+ {{/if}}
+ </section>
+</template>
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-secrets-list-editor.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-secrets-list-editor.gjs
index fa48c21062a..76e45169ba0 100644
--- a/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-secrets-list-editor.gjs
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/components/ai-secrets-list-editor.gjs
@@ -73,6 +73,14 @@ export default class AiSecretsListEditor extends Component {
{{usage.name}}
</LinkTo>
({{i18n "discourse_ai.secrets.embedding"}})
+ {{else if (eq usage.type "decision_model")}}
+ <LinkTo
+ @model={{usage.id}}
+ @route="adminPlugins.show.discourse-ai-decision-models.edit"
+ >
+ {{usage.name}}
+ </LinkTo>
+ ({{i18n "discourse_ai.secrets.decision_model"}})
{{else if (eq usage.type "mcp_server")}}
<LinkTo
@model={{usage.id}}
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models.js b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models.js
new file mode 100644
index 00000000000..cde422a33ab
--- /dev/null
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models.js
@@ -0,0 +1,7 @@
+import DiscourseRoute from "discourse/routes/discourse";
+
+export default class DiscourseAiDecisionModelsRoute extends DiscourseRoute {
+ model() {
+ return this.store.findAll("ai-decision-model");
+ }
+}
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models/edit.js b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models/edit.js
new file mode 100644
index 00000000000..fe6569aba01
--- /dev/null
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models/edit.js
@@ -0,0 +1,18 @@
+import DiscourseRoute from "discourse/routes/discourse";
+
+export default class AdminPluginsShowDiscourseAiDecisionModelsEdit extends DiscourseRoute {
+ model(params) {
+ const id = parseInt(params.id, 10);
+ return this.modelFor(
+ "adminPlugins.show.discourse-ai-decision-models"
+ ).content.find((decisionModel) => decisionModel.id === id);
+ }
+
+ setupController(controller, model) {
+ super.setupController(controller, model);
+ controller.set(
+ "allDecisionModels",
+ this.modelFor("adminPlugins.show.discourse-ai-decision-models")
+ );
+ }
+}
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models/new.js b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models/new.js
new file mode 100644
index 00000000000..48bfa954ae2
--- /dev/null
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/routes/admin-plugins/show/discourse-ai-decision-models/new.js
@@ -0,0 +1,15 @@
+import DiscourseRoute from "discourse/routes/discourse";
+
+export default class AdminPluginsShowDiscourseAiDecisionModelsNew extends DiscourseRoute {
+ model() {
+ return this.store.createRecord("ai-decision-model");
+ }
+
+ setupController(controller, model) {
+ super.setupController(controller, model);
+ controller.set(
+ "allDecisionModels",
+ this.modelFor("adminPlugins.show.discourse-ai-decision-models")
+ );
+ }
+}
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/edit.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/edit.gjs
new file mode 100644
index 00000000000..b17b5d2a811
--- /dev/null
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/edit.gjs
@@ -0,0 +1,17 @@
+import AiDecisionModelEditorForm from "../../../../components/ai-decision-model-editor-form";
+import AiDecisionModelsListEditor from "../../../../components/ai-decision-models-list-editor";
+
+export default <template>
+ {{#if @controller.model}}
+ <section class="admin-detail">
+ <AiDecisionModelEditorForm
+ @decisionModels={{@controller.allDecisionModels}}
+ @model={{@controller.model}}
+ />
+ </section>
+ {{else}}
+ <AiDecisionModelsListEditor
+ @decisionModels={{@controller.allDecisionModels}}
+ />
+ {{/if}}
+</template>
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/index.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/index.gjs
new file mode 100644
index 00000000000..4f5f774ac1a
--- /dev/null
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/index.gjs
@@ -0,0 +1,5 @@
+import AiDecisionModelsListEditor from "../../../../components/ai-decision-models-list-editor";
+
+export default <template>
+ <AiDecisionModelsListEditor @decisionModels={{@controller.model}} />
+</template>
diff --git a/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/new.gjs b/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/new.gjs
new file mode 100644
index 00000000000..99b3c5a1e58
--- /dev/null
+++ b/plugins/discourse-ai/admin/assets/javascripts/discourse/templates/admin-plugins/show/discourse-ai-decision-models/new.gjs
@@ -0,0 +1,10 @@
+import AiDecisionModelEditorForm from "../../../../components/ai-decision-model-editor-form";
+
+export default <template>
+ <section class="admin-detail">
+ <AiDecisionModelEditorForm
+ @decisionModels={{@controller.allDecisionModels}}
+ @model={{@controller.model}}
+ />
+ </section>
+</template>
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
new file mode 100644
index 00000000000..da9ff2ff657
--- /dev/null
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_decision_models_controller.rb
@@ -0,0 +1,167 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Admin
+ class AiDecisionModelsController < ::Admin::AdminController
+ requires_plugin PLUGIN_NAME
+
+ # A 16x16 solid red PNG: models that claim image input must recognise it.
+ TEST_IMAGE =
+ "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEX/AAAZ4gk3AAAADElEQVQI12NgIA0AAAAwAAHHqoWOAAAAAElFTkSuQmCC"
+
+ def index
+ decision_models = DecisionModel.order(:display_name)
+
+ render json: {
+ ai_decision_models:
+ ActiveModel::ArraySerializer.new(
+ decision_models,
+ each_serializer: AiDecisionModelSerializer,
+ root: false,
+ ).as_json,
+ meta: {
+ providers: DecisionModel::PROVIDERS,
+ presets: DecisionModel.presets,
+ ai_secrets: AiSecret.order(:name).select(:id, :name).as_json(only: %i[id name]),
+ },
+ }
+ end
+
+ def new
+ end
+
+ def edit
+ decision_model = DecisionModel.find(params[:id])
+ render json: AiDecisionModelSerializer.new(decision_model)
+ end
+
+ def create
+ decision_model = DecisionModel.new(decision_model_params)
+
+ if decision_model.save
+ DiscourseAi::Utils::AiStaffActionLogger.new(current_user).log_creation(
+ "decision_model",
+ decision_model,
+ logger_fields,
+ { decision_model_id: decision_model.id, subject: decision_model.display_name },
+ )
+ render json: AiDecisionModelSerializer.new(decision_model), status: :created
+ else
+ render_json_error decision_model
+ end
+ end
+
+ def update
+ decision_model = DecisionModel.find(params[:id])
+ initial_attributes = decision_model.attributes.dup
+
+ if decision_model.update(decision_model_params)
+ DiscourseAi::Utils::AiStaffActionLogger.new(current_user).log_update(
+ "decision_model",
+ decision_model,
+ initial_attributes,
+ logger_fields,
+ { decision_model_id: decision_model.id, subject: decision_model.display_name },
+ )
+ render json: AiDecisionModelSerializer.new(decision_model)
+ else
+ render_json_error decision_model
+ end
+ end
+
+ def destroy
+ decision_model = DecisionModel.find(params[:id])
+
+ details =
+ decision_model
+ .attributes
+ .symbolize_keys
+ .slice(*logger_fields.keys)
+ .merge(decision_model_id: decision_model.id, subject: decision_model.display_name)
+
+ if decision_model.destroy
+ DiscourseAi::Utils::AiStaffActionLogger.new(current_user).log_deletion(
+ "decision_model",
+ details,
+ )
+ head :no_content
+ else
+ render_json_error decision_model
+ end
+ end
+
+ def test
+ RateLimiter.new(
+ current_user,
+ "ai_decision_models_test_#{current_user.id}",
+ 6,
+ 1.minute,
+ apply_limit_to_staff: true,
+ ).performed!
+
+ decision_model = DecisionModel.new(decision_model_params)
+ decision_model.display_name = "Test" if decision_model.display_name.blank?
+ if !decision_model.valid?
+ return(
+ render json: { success: false, error: decision_model.errors.full_messages.join(", ") }
+ )
+ end
+
+ questions = [
+ { type: "yes_no", name: "greeting", instructions: "Is this message a greeting?" },
+ ]
+ images = []
+ if decision_model.vision_enabled
+ questions << {
+ type: "yes_no",
+ name: "red_image",
+ instructions: "Is the attached image a solid red square?",
+ }
+ images << TEST_IMAGE
+ end
+
+ result =
+ DiscourseAi::Decisions::Client.new(decision_model, timeout: 10, max_attempts: 1).decide(
+ "Hello! How are you doing today?",
+ questions:,
+ images:,
+ user: current_user,
+ feature_name: "decision_model_test",
+ )
+
+ if result.refusals.present?
+ render json: {
+ success: false,
+ error: I18n.t("discourse_ai.decision_models.test_refused"),
+ }
+ elsif decision_model.vision_enabled && result["red_image"].probability < 0.5
+ render json: {
+ success: false,
+ error: I18n.t("discourse_ai.decision_models.test_image_not_read"),
+ }
+ else
+ render json: { success: true, probability: result["greeting"].probability }
+ end
+ rescue DiscourseAi::Decisions::Error => e
+ render json: { success: false, error: e.message }
+ end
+
+ private
+
+ def decision_model_params
+ params.require(:ai_decision_model).permit(
+ :display_name,
+ :name,
+ :provider,
+ :url,
+ :ai_secret_id,
+ :vision_enabled,
+ )
+ end
+
+ def logger_fields
+ { display_name: {}, name: {}, provider: {}, url: {}, ai_secret_id: {}, vision_enabled: {} }
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_secrets_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_secrets_controller.rb
index 91b13c3444b..b458c65fb09 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_secrets_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/admin/ai_secrets_controller.rb
@@ -11,6 +11,7 @@ module DiscourseAi
.all
.includes(
:llm_models,
+ :decision_models,
:embedding_definitions,
:ai_mcp_servers,
ai_tool_secret_bindings: :ai_tool,
@@ -31,6 +32,7 @@ module DiscourseAi
secret =
AiSecret.includes(
:llm_models,
+ :decision_models,
:embedding_definitions,
:ai_mcp_servers,
ai_tool_secret_bindings: :ai_tool,
diff --git a/plugins/discourse-ai/app/models/ai_api_audit_log.rb b/plugins/discourse-ai/app/models/ai_api_audit_log.rb
index 3a48d147bb3..f55b7706827 100644
--- a/plugins/discourse-ai/app/models/ai_api_audit_log.rb
+++ b/plugins/discourse-ai/app/models/ai_api_audit_log.rb
@@ -42,6 +42,8 @@ class AiApiAuditLog < ActiveRecord::Base
Mistral = 9
OpenRouter = 10
BedrockConverse = 11
+ SystemOne = 12
+ OpenAiDecisions = 13
end
def self.token_and_spending_stats(scope)
diff --git a/plugins/discourse-ai/app/models/ai_secret.rb b/plugins/discourse-ai/app/models/ai_secret.rb
index 0db1c709c2a..b71b08948e8 100644
--- a/plugins/discourse-ai/app/models/ai_secret.rb
+++ b/plugins/discourse-ai/app/models/ai_secret.rb
@@ -7,6 +7,7 @@ class AiSecret < ActiveRecord::Base
validates :secret, presence: true, length: { maximum: 10_000 }
has_many :llm_models, dependent: :nullify
+ has_many :decision_models, dependent: :nullify
has_many :embedding_definitions, dependent: :nullify
has_many :ai_tool_secret_bindings, dependent: :destroy
has_many :ai_mcp_servers, dependent: :nullify
@@ -16,14 +17,17 @@ class AiSecret < ActiveRecord::Base
dependent: :nullify
def in_use?
- llm_models.exists? || embedding_definitions.exists? || used_by_provider_params? ||
- ai_tool_secret_bindings.exists? || ai_mcp_servers.exists? ||
+ llm_models.exists? || decision_models.exists? || embedding_definitions.exists? ||
+ used_by_provider_params? || ai_tool_secret_bindings.exists? || ai_mcp_servers.exists? ||
oauth_client_secret_mcp_servers.exists?
end
def used_by
usage = []
llm_models.each { |llm| usage << { type: "llm", name: llm.display_name, id: llm.id } }
+ decision_models.each do |decision_model|
+ usage << { type: "decision_model", name: decision_model.display_name, id: decision_model.id }
+ end
embedding_definitions.each do |ed|
usage << { type: "embedding", name: ed.display_name, id: ed.id }
end
diff --git a/plugins/discourse-ai/app/models/decision_model.rb b/plugins/discourse-ai/app/models/decision_model.rb
new file mode 100644
index 00000000000..2a0741e7ed7
--- /dev/null
+++ b/plugins/discourse-ai/app/models/decision_model.rb
@@ -0,0 +1,123 @@
+# frozen_string_literal: true
+
+class DecisionModel < ActiveRecord::Base
+ PROVIDERS = %w[system_one open_ai].freeze
+
+ belongs_to :ai_secret, optional: true
+
+ validates :display_name, presence: true, length: { maximum: 100 }
+ validates :name, presence: true, length: { maximum: 255 }
+ validates :provider, presence: true, inclusion: { in: PROVIDERS }
+ validates :url, presence: true, length: { maximum: 1000 }
+ validate :valid_endpoint_url
+ validate :secret_exists
+
+ def self.presets
+ @presets ||= [
+ {
+ preset_id: "jev-latest",
+ display_name: "TypeSafe Jev",
+ name: "jev-latest",
+ url: "https://api.typesafe.ai/v1/systemone",
+ provider: "system_one",
+ vision_enabled: false,
+ }.freeze,
+ {
+ preset_id: "vllm-sr-d3",
+ display_name: "d3",
+ name: "vllm-sr/d3",
+ url: "",
+ provider: "system_one",
+ vision_enabled: true,
+ }.freeze,
+ {
+ preset_id: "cloudflare-clef",
+ display_name: "Cloudflare Clef",
+ name: "clef",
+ url: "https://api.cloudflare.com/client/v4/accounts/ACCOUNT_ID/ai/run/@cf/cloudflare/clef",
+ provider: "system_one",
+ vision_enabled: true,
+ }.freeze,
+ {
+ preset_id: "cloudflare-clef-flash",
+ display_name: "Cloudflare Clef Flash",
+ name: "clef-flash",
+ url:
+ "https://api.cloudflare.com/client/v4/accounts/ACCOUNT_ID/ai/run/@cf/cloudflare/clef-flash",
+ provider: "system_one",
+ vision_enabled: true,
+ }.freeze,
+ {
+ preset_id: "gpt-6-luna",
+ display_name: "GPT-6 Luna",
+ name: "gpt-6-luna",
+ url: "https://api.openai.com/v1/decisions",
+ provider: "open_ai",
+ vision_enabled: true,
+ }.freeze,
+ ].freeze
+ end
+
+ def decide(state, **)
+ DiscourseAi::Decisions::Client.new(self).decide(state, **)
+ end
+
+ def api_key
+ ai_secret&.secret
+ end
+
+ def endpoint_url
+ uri = URI(url)
+ return url if uri.scheme != "srv"
+
+ service =
+ begin
+ DiscourseAi::Utils::DnsSrv.lookup(uri.host)
+ rescue Resolv::ResolvError, Resolv::ResolvTimeout => e
+ raise DiscourseAi::Decisions::RequestFailed,
+ "Request failed: SRV lookup for #{uri.host} failed (#{e.class})"
+ end
+ if !service
+ raise DiscourseAi::Decisions::RequestFailed, "Request failed: no SRV record for #{uri.host}"
+ end
+ "https://#{service.target}:#{service.port}#{uri.path}#{"?#{uri.query}" if uri.query}"
+ end
+
+ private
+
+ def valid_endpoint_url
+ return if url.blank?
+ uri = URI(url)
+ # SRV records supply the port, so one in the URL would be silently ignored.
+ if !%w[http https srv].include?(uri.scheme) || uri.host.blank? ||
+ (uri.scheme == "srv" && uri.port)
+ errors.add(:url, I18n.t("discourse_ai.decision_models.invalid_url"))
+ end
+ rescue URI::InvalidURIError
+ errors.add(:url, I18n.t("discourse_ai.decision_models.invalid_url"))
+ end
+
+ def secret_exists
+ return if ai_secret_id.blank? || AiSecret.exists?(ai_secret_id)
+ errors.add(:ai_secret_id, I18n.t("discourse_ai.decision_models.secret_not_found"))
+ end
+end
+
+# == Schema Information
+#
+# Table name: decision_models
+#
+# id :bigint not null, primary key
+# display_name :string(100) not null
+# name :string(255) not null
+# provider :string(50) not null
+# url :string(1000) not null
+# vision_enabled :boolean default(FALSE), not null
+# created_at :datetime not null
+# updated_at :datetime not null
+# ai_secret_id :bigint
+#
+# Indexes
+#
+# index_decision_models_on_ai_secret_id (ai_secret_id)
+#
diff --git a/plugins/discourse-ai/app/serializers/ai_decision_model_serializer.rb b/plugins/discourse-ai/app/serializers/ai_decision_model_serializer.rb
new file mode 100644
index 00000000000..1ea58701938
--- /dev/null
+++ b/plugins/discourse-ai/app/serializers/ai_decision_model_serializer.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AiDecisionModelSerializer < ApplicationSerializer
+ root "ai_decision_model"
+
+ attributes :id, :display_name, :name, :provider, :url, :ai_secret_id, :vision_enabled
+end
diff --git a/plugins/discourse-ai/app/serializers/discourse_ai/admin/ai_log_serializer_helpers.rb b/plugins/discourse-ai/app/serializers/discourse_ai/admin/ai_log_serializer_helpers.rb
index 605d555b575..b55823a65db 100644
--- a/plugins/discourse-ai/app/serializers/discourse_ai/admin/ai_log_serializer_helpers.rb
+++ b/plugins/discourse-ai/app/serializers/discourse_ai/admin/ai_log_serializer_helpers.rb
@@ -15,6 +15,8 @@ module DiscourseAi
AiApiAuditLog::Provider::Mistral => "Mistral",
AiApiAuditLog::Provider::OpenRouter => "OpenRouter",
AiApiAuditLog::Provider::BedrockConverse => "Amazon Bedrock",
+ AiApiAuditLog::Provider::SystemOne => "System One",
+ AiApiAuditLog::Provider::OpenAiDecisions => "OpenAI Decisions",
}.freeze
def self.outcome(log)
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin-discourse-ai-plugin-route-map.js b/plugins/discourse-ai/assets/javascripts/discourse/admin-discourse-ai-plugin-route-map.js
index ef836b425c7..cf99aff2864 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/admin-discourse-ai-plugin-route-map.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin-discourse-ai-plugin-route-map.js
@@ -15,6 +15,15 @@ export default {
this.route("edit", { path: "/:id/edit" });
});
+ this.route(
+ "discourse-ai-decision-models",
+ { path: "ai-decision-models" },
+ function () {
+ this.route("new");
+ this.route("edit", { path: "/:id/edit" });
+ }
+ );
+
this.route("discourse-ai-secrets", { path: "ai-secrets" }, function () {
this.route("new");
this.route("edit", { path: "/:id/edit" });
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/admin/adapters/ai-decision-model.js b/plugins/discourse-ai/assets/javascripts/discourse/admin/adapters/ai-decision-model.js
new file mode 100644
index 00000000000..3333b6c6e61
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/adapters/ai-decision-model.js
@@ -0,0 +1,21 @@
+import RestAdapter from "discourse/adapters/rest";
+
+export default class Adapter extends RestAdapter {
+ jsonMode = true;
+
+ basePath() {
+ return "/admin/plugins/discourse-ai/";
+ }
+
+ pathFor(store, type, findArgs) {
+ // Preserve the hyphenated route instead of the base adapter's underscore conversion.
+ const path =
+ this.basePath(store, type, findArgs) +
+ store.pluralize(this.apiNameFor(type));
+ return this.appendQueryParams(path, findArgs);
+ }
+
+ apiNameFor() {
+ return "ai-decision-model";
+ }
+}
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
new file mode 100644
index 00000000000..d162704ea9d
--- /dev/null
+++ b/plugins/discourse-ai/assets/javascripts/discourse/admin/models/ai-decision-model.js
@@ -0,0 +1,26 @@
+import { ajax } from "discourse/lib/ajax";
+import RestModel from "discourse/models/rest";
+
+export default class AiDecisionModel extends RestModel {
+ createProperties() {
+ return this.getProperties(
+ "display_name",
+ "name",
+ "provider",
+ "url",
+ "ai_secret_id",
+ "vision_enabled"
+ );
+ }
+
+ updateProperties() {
+ return this.createProperties();
+ }
+
+ testConfig(config) {
+ return ajax("/admin/plugins/discourse-ai/ai-decision-models/test.json", {
+ type: "POST",
+ data: { ai_decision_model: config },
+ });
+ }
+}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-secret-editor-form.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-secret-editor-form.gjs
index 86320bba307..5f4a63bceef 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-secret-editor-form.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-secret-editor-form.gjs
@@ -136,6 +136,11 @@ export default class AiSecretEditorForm extends Component {
@model={{usage.id}}
@route="adminPlugins.show.discourse-ai-embeddings.edit"
>{{usage.name}}</LinkTo>
+ {{~else if (eq usage.type "decision_model")~}}
+ <LinkTo
+ @model={{usage.id}}
+ @route="adminPlugins.show.discourse-ai-decision-models.edit"
+ >{{usage.name}}</LinkTo>
{{~else if (eq usage.type "tool")~}}
<LinkTo
@model={{usage.id}}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/admin-plugin-configuration-nav.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/admin-plugin-configuration-nav.js
index 1be1e19109d..e294763cfed 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/admin-plugin-configuration-nav.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/admin-plugin-configuration-nav.js
@@ -11,6 +11,8 @@ export default {
return;
}
+ const siteSettings = container.lookup("service:site-settings");
+
withPluginApi((api) => {
api.setAdminPluginIcon(PLUGIN_ID, "discourse-sparkles");
api.addAdminPluginConfigurationNav(PLUGIN_ID, [
@@ -29,6 +31,15 @@ export default {
route: "adminPlugins.show.discourse-ai-llms",
description: "discourse_ai.llms.preconfigured.description",
},
+ ...(siteSettings.enable_discourse_workflows
+ ? [
+ {
+ label: "discourse_ai.decision_models.short_title",
+ route: "adminPlugins.show.discourse-ai-decision-models",
+ description: "discourse_ai.decision_models.description",
+ },
+ ]
+ : []),
{
label: "discourse_ai.secrets.short_title",
route: "adminPlugins.show.discourse-ai-secrets",
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index 7db850e308b..62a6b35c699 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -103,6 +103,9 @@ en:
create_ai_embedding: "Create AI embedding"
update_ai_embedding: "Update AI embedding"
delete_ai_embedding: "Delete AI embedding"
+ create_ai_decision_model: "Create AI decision model"
+ update_ai_decision_model: "Update AI decision model"
+ delete_ai_decision_model: "Delete AI decision model"
create_ai_secret: "Create AI secret"
update_ai_secret: "Update AI secret"
delete_ai_secret: "Delete AI secret"
@@ -118,10 +121,13 @@ en:
discourse_workflows:
node_unavailable:
requires_ai: "Requires the AI plugin to be enabled"
+ requires_decision_model: "Requires a decision model"
nodes:
"action:ai_agent": "AI Agent"
+ "action:classify": "Classify"
node_descriptions:
"action:ai_agent": "Run an AI agent to process data or generate content"
+ "action:classify": "Ask a decision model questions about each item"
discourse_automation:
scriptables:
llm_report:
@@ -339,6 +345,24 @@ en:
upload_ids_tooltip: "Optional upload IDs to attach to the prompt. Every upload must reach the model or the node fails: images need vision on the agent and the LLM, documents need their file type enabled on the LLM."
upload_ids_placeholder: "={{ $json.post.upload_ids }}"
select_agent: "Select an agent..."
+ classify:
+ decision_model_id: "Decision model"
+ text: "Text"
+ text_description: "What the decision model reads for each item."
+ upload_ids: "Images"
+ upload_ids_description: "Upload IDs of images the decision model looks at, such as the post's upload_ids. Other uploads are skipped. Needs a decision model that reads images."
+ questions: "Questions"
+ questions_description: "Each answer is added to the item as classification.NAME, where NAME is the question name."
+ add_question: "Add question"
+ name: "Name"
+ type: "Type"
+ type_yes_no: "Yes/no"
+ type_choice: "Choice"
+ type_score: "Score"
+ instructions: "Question"
+ choices: "Choices"
+ levels: "Levels"
+ levels_description: "From lowest to highest. The answer has the most likely level and a weighted score from 0 (first level) upwards."
bot_chats:
tab_label: "Bot chats"
@@ -1118,7 +1142,7 @@ en:
secrets:
short_title: "Credentials"
- description: "Manage authentication credentials shared across LLMs, embeddings, and tools."
+ description: "Manage authentication credentials shared across LLMs, embeddings, decision models, and tools."
name: "Name"
secret: "Value"
select_secret: "Select a credential..."
@@ -1141,9 +1165,35 @@ en:
tool: "Tool"
mcp_server: "MCP server"
mcp_server_oauth_client_secret: "MCP server OAuth client secret"
+ decision_model: "Decision model"
modal:
title: "Create new credential"
+ decision_models:
+ short_title: "Decision models"
+ description: "Models that answer typed questions about content (yes/no, choice, score) with probabilities, faster and cheaper than an LLM."
+ create_new: "New decision model"
+ no_decision_models: "No decision models yet"
+ manage: "Manage decision models"
+ back: "Back to decision models"
+ start_from: "Start from"
+ no_preset: "None"
+ display_name: "Name"
+ model: "Model"
+ provider: "Provider"
+ url: "Endpoint URL"
+ api_key: "Credential"
+ vision_enabled: "Supports image input"
+ edit: "Edit"
+ delete: "Delete"
+ confirm_delete: "Are you sure you want to delete this decision model? Workflows that use it will stop working."
+ saved: "Decision model saved"
+ test: "Run test"
+ test_success: "The decision model answered the test question (yes: %{percentage}%)."
+ providers:
+ system_one: "System One"
+ open_ai: "OpenAI Decisions"
+
mcp_servers:
short_title: "MCP servers"
new: "New MCP server"
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 5bb583b4516..b9fa63a7324 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -339,6 +339,10 @@ en:
llm_cannot_read_documents: "Upload %{upload_id} is a document and LLM '%{llm}' accepts no attachments"
unsupported_image_format: "Upload %{upload_id} is not a supported image format (%{formats})"
upload_skipped: "Upload %{upload_id} (%{filename}) was not sent: %{message}"
+ classify:
+ errors:
+ decision_model_not_found: "Decision model with id %{id} not found"
+ classify_failed: "Decision model '%{decision_model}' could not answer: %{error}"
tools:
custom_name: "%{name} (custom)"
@@ -1389,8 +1393,14 @@ en:
choose_model: "Set 'ai embeddings selected model' first."
model_test_failed: "We failed to generate a test embedding with this model. Check it's correctly configured."
+ decision_models:
+ 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."
+ test_image_not_read: "The decision model did not recognise the test image. Untick \"Supports image input\" if it cannot read images."
+
secrets:
- delete_failed_in_use: "This credential is currently in use by one or more LLMs, embeddings, or tools and cannot be deleted."
+ delete_failed_in_use: "This credential is currently in use by one or more LLMs, embeddings, decision models, or tools and cannot be deleted."
llm_models:
missing_provider_param: "%{param} can't be blank"
diff --git a/plugins/discourse-ai/config/routes.rb b/plugins/discourse-ai/config/routes.rb
index 9f74c7e2344..63f312ae202 100644
--- a/plugins/discourse-ai/config/routes.rb
+++ b/plugins/discourse-ai/config/routes.rb
@@ -214,6 +214,13 @@ Discourse::Application.routes.draw do
collection { post :test }
end
+ resources :ai_decision_models,
+ only: %i[index new create edit update destroy],
+ path: "ai-decision-models",
+ controller: "discourse_ai/admin/ai_decision_models" do
+ collection { post :test }
+ end
+
resources :ai_llm_quotas,
controller: "discourse_ai/admin/ai_llm_quotas",
path: "quotas",
diff --git a/plugins/discourse-ai/db/migrate/20261013222431_create_decision_models.rb b/plugins/discourse-ai/db/migrate/20261013222431_create_decision_models.rb
new file mode 100644
index 00000000000..75f7092bb3d
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20261013222431_create_decision_models.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+class CreateDecisionModels < ActiveRecord::Migration[8.1]
+ def change
+ create_table :decision_models do |t|
+ t.string :display_name, null: false, limit: 100
+ t.string :name, null: false, limit: 255
+ t.string :provider, null: false, limit: 50
+ t.string :url, null: false, limit: 1000
+ t.bigint :ai_secret_id
+ t.boolean :vision_enabled, null: false, default: false
+ t.timestamps
+ end
+
+ add_index :decision_models, :ai_secret_id
+ end
+end
diff --git a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
index dde3d33ed72..f9eb5b4e826 100644
--- a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
+++ b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
@@ -312,7 +312,7 @@ if defined?(DiscourseWorkflows)
end
def prompt_content(prompt, upload_ids, agent_record, llm_model, runner, log)
- upload_ids = normalize_upload_ids(upload_ids)
+ upload_ids = ::DiscourseAi::Completions::UploadEncoder.normalize_upload_ids(upload_ids)
return prompt if upload_ids.blank?
uploads_by_id =
@@ -366,31 +366,6 @@ if defined?(DiscourseWorkflows)
I18n.t("discourse_ai.discourse_workflows.ai_agent.errors.#{key}", **args)
end
- def normalize_upload_ids(upload_ids)
- case upload_ids
- when String
- parsed = parse_upload_ids_json(upload_ids)
- return normalize_upload_ids(parsed) if parsed
-
- upload_ids.split(",")
- when Array
- upload_ids.flatten
- else
- Array.wrap(upload_ids)
- end
- .filter_map do |upload_id|
- id = Integer(upload_id, exception: false)
- id if id&.positive?
- end
- .uniq
- end
-
- def parse_upload_ids_json(upload_ids)
- JSON.parse(upload_ids)
- rescue JSON::ParserError, TypeError
- nil
- end
-
def run_agent(agent, config, log, runner)
agent_record = agent[:record]
llm_model = agent[:llm_model]
diff --git a/plugins/discourse-ai/discourse_workflows/nodes/classify/v1.rb b/plugins/discourse-ai/discourse_workflows/nodes/classify/v1.rb
new file mode 100644
index 00000000000..4c0b2120c81
--- /dev/null
+++ b/plugins/discourse-ai/discourse_workflows/nodes/classify/v1.rb
@@ -0,0 +1,283 @@
+# frozen_string_literal: true
+
+if defined?(DiscourseWorkflows)
+ module DiscourseWorkflows
+ module Nodes
+ module Classify
+ class V1 < DiscourseWorkflows::NodeType
+ OUTPUT_KEY = "classification"
+ MAX_IMAGE_PIXELS = 1_600_000
+
+ description(
+ name: "action:classify",
+ version: "1.0",
+ defaults: {
+ icon: "chart-simple",
+ color: "violet",
+ },
+ group: "ai",
+ available: -> { SiteSetting.discourse_ai_enabled && ::DecisionModel.exists? },
+ unavailable_reason_key: "discourse_workflows.node_unavailable.requires_decision_model",
+ i18n_prefix: "discourse_ai.discourse_workflows",
+ output_schema_resolver: "classify",
+ capabilities: {
+ run_scope: "per_item",
+ },
+ output_contracts: [
+ {
+ mode: :merge,
+ schema: DiscourseWorkflows::Schema.document(OUTPUT_KEY => { "type" => "object" }),
+ },
+ ],
+ properties: {
+ decision_model_id: {
+ type: :integer,
+ required: true,
+ type_options: {
+ load_options_method: "decision_models",
+ },
+ no_data_expression: true,
+ ui: {
+ control: :combo_box,
+ },
+ control_options: {
+ action_icon: "chart-simple",
+ action_label: "discourse_ai.decision_models.manage",
+ action_route: "adminPlugins.show.discourse-ai-decision-models",
+ action_route_models: ["discourse-ai"],
+ filterable: true,
+ value_property: :id,
+ name_property: :name,
+ },
+ },
+ text: {
+ type: :string,
+ required: true,
+ default: "={{ $json.post.raw }}",
+ ui: {
+ control: :textarea,
+ },
+ },
+ upload_ids: {
+ type: :array,
+ default: [],
+ ui: {
+ control: :multi_input,
+ expression: true,
+ },
+ },
+ questions: {
+ type: :fixed_collection,
+ required: true,
+ type_options: {
+ multiple_values: true,
+ },
+ options: [
+ {
+ name: "values",
+ values: {
+ name: {
+ type: :string,
+ required: true,
+ no_data_expression: true,
+ },
+ type: {
+ type: :options,
+ required: true,
+ options: DiscourseAi::Decisions::Question::TYPES.map(&:to_s),
+ default: "yes_no",
+ no_data_expression: true,
+ },
+ instructions: {
+ type: :string,
+ required: true,
+ ui: {
+ control: :textarea,
+ },
+ },
+ choices: {
+ type: :array,
+ default: [],
+ no_data_expression: true,
+ display_options: {
+ show: {
+ type: %w[choice],
+ },
+ },
+ ui: {
+ control: :multi_input,
+ },
+ },
+ levels: {
+ type: :array,
+ default: [],
+ no_data_expression: true,
+ display_options: {
+ show: {
+ type: %w[score],
+ },
+ },
+ ui: {
+ control: :multi_input,
+ },
+ },
+ },
+ },
+ ],
+ },
+ },
+ )
+
+ # Answer keys come from the configured questions, so the exact shape is
+ # resolved from the configuration rather than declared as a contract.
+ def self.output_schemas(configuration = {}, input_schemas: [])
+ rows =
+ DiscourseWorkflows::CollectionParameters.rows(
+ (configuration || {}).deep_stringify_keys,
+ "questions",
+ )
+
+ answers =
+ rows
+ .grep(Hash)
+ .each_with_object({}) do |row, properties|
+ key = row["name"].to_s.strip
+ next if key.blank?
+
+ properties[key] = answer_schema(row["type"].to_s)
+ end
+
+ schema =
+ DiscourseWorkflows::Schema.document(
+ OUTPUT_KEY => {
+ "type" => "object",
+ "properties" => answers,
+ },
+ )
+ input_schema = DiscourseWorkflows::Schema.union(*Array(input_schemas).compact)
+ [DiscourseWorkflows::Schema.resolve(schema, mode: :merge, input_schema:)]
+ end
+
+ def self.answer_schema(type)
+ properties = {
+ "type" => {
+ "type" => "string",
+ },
+ "reason" => {
+ "type" => %w[string null],
+ },
+ }
+ case type
+ when "yes_no"
+ properties["probability"] = { "type" => "number" }
+ when "choice", "score"
+ properties["probabilities"] = { "type" => "object" }
+ properties["confidence"] = { "type" => %w[number null] }
+ if type == "choice"
+ properties["choice"] = { "type" => "string" }
+ else
+ properties["score"] = { "type" => "number" }
+ properties["level"] = { "type" => "string" }
+ end
+ end
+ { "type" => "object", "properties" => properties }
+ end
+
+ def self.group_definition
+ { icon: "robot", label_key: "discourse_workflows.add_node.categories.ai", order: 40 }
+ end
+
+ def self.load_options_context(context)
+ return if context.method_name != "decision_models"
+
+ ::DecisionModel
+ .order(:display_name)
+ .pluck(:id, :display_name)
+ .map { |id, display_name| { id: id, name: display_name } }
+ .select { |decision_model| context.matches_filter?(decision_model[:name]) }
+ end
+
+ def execute(exec_ctx)
+ decision_model = find_decision_model(exec_ctx)
+
+ [
+ exec_ctx.each_item do |item, item_index|
+ answers = classify(exec_ctx, decision_model, item_index)
+ wrap(item.fetch("json") { {} }.merge(OUTPUT_KEY => answers))
+ end,
+ ]
+ end
+
+ private
+
+ def find_decision_model(exec_ctx)
+ id = exec_ctx.get_node_parameter("decision_model_id", 0)
+ decision_model = ::DecisionModel.find_by(id: id)
+ raise_node_error!(error_text("decision_model_not_found", id: id)) if decision_model.nil?
+
+ decision_model
+ end
+
+ def classify(exec_ctx, decision_model, item_index)
+ text = exec_ctx.get_node_parameter("text", item_index)
+ questions = exec_ctx.get_node_parameter("questions.values", item_index, default: [])
+ images = images_for(exec_ctx.get_node_parameter("upload_ids", item_index, default: []))
+ exec_ctx.log.info(
+ "Decision model: #{decision_model.display_name}, images: #{images.size}",
+ )
+
+ questions =
+ questions.map do |question|
+ next question if !question.is_a?(Hash)
+
+ question = question.symbolize_keys
+ question[:choices] = nil if question[:type] != "choice"
+ question[:levels] = nil if question[:type] != "score"
+ question
+ end
+ result =
+ decision_model.decide(
+ text,
+ questions:,
+ images:,
+ feature_name: "workflow",
+ user: Discourse.system_user,
+ )
+ refusals = result.refusals
+ if refusals.present?
+ exec_ctx.log.warn(
+ "Decision model refused questions: #{refusals.map(&:name).join(", ")}",
+ )
+ end
+ result.answers.transform_values(&:to_h)
+ rescue DiscourseAi::Decisions::Error => e
+ raise_node_error!(
+ error_text(
+ "classify_failed",
+ decision_model: decision_model.display_name,
+ error: e.message,
+ ),
+ item_index: item_index,
+ )
+ end
+
+ def images_for(upload_ids)
+ encoder = DiscourseAi::Completions::UploadEncoder
+ upload_ids = encoder.normalize_upload_ids(upload_ids)
+ return [] if upload_ids.empty?
+
+ encoder
+ .encode(upload_ids:, max_pixels: MAX_IMAGE_PIXELS)
+ .map { |image| "data:#{image[:mime_type]};base64,#{image[:base64]}" }
+ rescue StandardError => e
+ raise DiscourseAi::Decisions::InvalidRequest, "Could not prepare images: #{e.message}"
+ end
+
+ def error_text(key, **args)
+ I18n.t("discourse_ai.discourse_workflows.classify.errors.#{key}", **args)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/completions/endpoints/base.rb b/plugins/discourse-ai/lib/completions/endpoints/base.rb
index f227d50992b..c3488216eda 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/base.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/base.rb
@@ -33,22 +33,6 @@ module DiscourseAi
OpenSSL::SSL::SSLError,
]
- # Read-only context handed to request header providers. Lets plugins
- # contribute provider-agnostic, per-request HTTP headers (e.g. routing
- # hints for a proxy) without reaching into endpoint internals.
- RequestHeaderContext =
- Struct.new(
- :llm_model,
- :feature_name,
- :feature_context,
- :has_images,
- :streaming,
- keyword_init: true,
- )
-
- # Mutated in place (<<, clear) but never reassigned, so the single array
- # is shared across Base and all endpoint subclasses via constant lookup.
- REQUEST_HEADERS_PROVIDERS = []
# 6 minutes
# Reasoning LLMs can take a very long time to respond, generally it will be under 5 minutes
# The alternative is to have per LLM timeouts but that would make it extra confusing for people
@@ -95,20 +79,10 @@ module DiscourseAi
true
end
- # Register a block that returns a Hash of extra HTTP headers to send
- # with each completion request. The block receives a
- # RequestHeaderContext and must not raise (errors are swallowed and
- # logged so a header bug can never break inference).
+ # Plugins registered header providers here before RequestHeaders also
+ # served decision models; kept so they work across versions.
def register_request_headers_provider(&block)
- REQUEST_HEADERS_PROVIDERS << block
- end
-
- def request_headers_providers
- REQUEST_HEADERS_PROVIDERS
- end
-
- def reset_request_headers_providers!
- REQUEST_HEADERS_PROVIDERS.clear
+ RequestHeaders.register(&block)
end
end
@@ -237,30 +211,17 @@ module DiscourseAi
# Extra HTTP headers contributed by registered providers for this
# request. Endpoints that want them merge the result into their request
- # headers. Provider failures are isolated so they can never break a
- # completion.
+ # headers.
def extra_request_headers
- providers = self.class.request_headers_providers
- return {} if providers.blank?
-
- context =
- RequestHeaderContext.new(
- llm_model: llm_model,
+ RequestHeaders.for(
+ RequestHeaders::Context.new(
+ model: llm_model,
feature_name: @feature_name,
feature_context: @feature_context,
has_images: @request_has_images,
streaming: @streaming_mode,
- )
-
- providers.each_with_object({}) do |provider, headers|
- result = provider.call(context)
- headers.merge!(result.stringify_keys) if result.is_a?(Hash)
- rescue StandardError => e
- Discourse.warn_exception(
- e,
- message: "Discourse AI request header provider raised an error; skipping it",
- )
- end
+ ),
+ )
end
protected
diff --git a/plugins/discourse-ai/lib/completions/upload_encoder.rb b/plugins/discourse-ai/lib/completions/upload_encoder.rb
index 1ad8e347a5f..166c4eba1b8 100644
--- a/plugins/discourse-ai/lib/completions/upload_encoder.rb
+++ b/plugins/discourse-ai/lib/completions/upload_encoder.rb
@@ -9,6 +9,35 @@ module DiscourseAi
SUPPORTED_IMAGE_EXTENSIONS = (JPEG_EXTENSIONS + %w[png gif webp avif]).freeze
SUPPORTED_IMAGE_FORMATS = SUPPORTED_IMAGE_EXTENSIONS.join(", ")
+ # Accepts the forms workflow expressions produce: arrays, "1,2" or "[1,2]".
+ def self.normalize_upload_ids(upload_ids)
+ if upload_ids.is_a?(String)
+ parsed =
+ begin
+ JSON.parse(upload_ids)
+ rescue JSON::ParserError
+ nil
+ end
+ return normalize_upload_ids(parsed) if parsed.is_a?(Array) || parsed.is_a?(Integer)
+
+ upload_ids = upload_ids.split(",")
+ end
+
+ Array
+ .wrap(upload_ids)
+ .flatten
+ .filter_map do |upload_id|
+ id =
+ if upload_id.is_a?(String)
+ Integer(upload_id.strip, 10, exception: false)
+ else
+ Integer(upload_id, exception: false)
+ end
+ id if id&.positive?
+ end
+ .uniq
+ end
+
def self.supported_image_upload?(upload)
image_extension?(upload.extension&.downcase)
end
diff --git a/plugins/discourse-ai/lib/decisions.rb b/plugins/discourse-ai/lib/decisions.rb
new file mode 100644
index 00000000000..8fd9547183c
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ def self.with_canned_answers(**answers)
+ raise ArgumentError, "Canned answers cannot be nested" if CannedAnswers.current
+
+ canned = CannedAnswers.new(answers)
+ begin
+ CannedAnswers.current = canned
+ yield canned
+ ensure
+ CannedAnswers.current = nil
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/answers/choice.rb b/plugins/discourse-ai/lib/decisions/answers/choice.rb
new file mode 100644
index 00000000000..186297302d7
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/answers/choice.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ module Answers
+ Choice =
+ Data.define(:name, :choice, :probabilities, :confidence) do
+ def initialize(**attributes)
+ super(**attributes.merge(probabilities: attributes.fetch(:probabilities).dup.freeze))
+ end
+
+ def refusal? = false
+
+ def to_h
+ { type: "choice", choice:, probabilities:, confidence: }
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/answers/refusal.rb b/plugins/discourse-ai/lib/decisions/answers/refusal.rb
new file mode 100644
index 00000000000..68828ff16a2
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/answers/refusal.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ module Answers
+ Refusal =
+ Data.define(:name, :reason) do
+ def initialize(name:, reason: nil)
+ super(name:, reason:)
+ end
+
+ def refusal? = true
+
+ def to_h
+ { type: "refusal", reason: }
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/answers/score.rb b/plugins/discourse-ai/lib/decisions/answers/score.rb
new file mode 100644
index 00000000000..a818391e7a7
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/answers/score.rb
@@ -0,0 +1,20 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ module Answers
+ Score =
+ Data.define(:name, :score, :level, :probabilities, :confidence) do
+ def initialize(**attributes)
+ super(**attributes.merge(probabilities: attributes.fetch(:probabilities).dup.freeze))
+ end
+
+ def refusal? = false
+
+ def to_h
+ { type: "score", score:, level:, probabilities:, confidence: }
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/answers/yes_no.rb b/plugins/discourse-ai/lib/decisions/answers/yes_no.rb
new file mode 100644
index 00000000000..3324bbc0164
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/answers/yes_no.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ module Answers
+ YesNo =
+ Data.define(:name, :probability) do
+ def refusal? = false
+
+ def to_h
+ { type: "yes_no", probability: }
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/canned_answers.rb b/plugins/discourse-ai/lib/decisions/canned_answers.rb
new file mode 100644
index 00000000000..2f516dc81ee
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/canned_answers.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ class CannedAnswers
+ class << self
+ attr_accessor :current
+ end
+
+ attr_reader :calls
+
+ def initialize(answers)
+ @answers = answers.transform_keys(&:to_s)
+ @calls = []
+ end
+
+ def decide(state:, questions:, images:, user:, feature_name:)
+ @calls << { state:, questions:, images:, user:, feature_name: }
+ answers =
+ questions.map do |question|
+ value =
+ @answers.fetch(question.name) do
+ raise ArgumentError, "No canned answer for question #{question.name.inspect}"
+ end
+ next Answers::Refusal.new(name: question.name) if value == :refusal
+
+ case question.type
+ when :yes_no
+ probability = value == true ? 1.0 : value == false ? 0.0 : value
+ if !probability.is_a?(Numeric) || !probability.real? || !probability.finite? ||
+ !probability.between?(0, 1)
+ raise ArgumentError, "Invalid canned probability for #{question.name}"
+ end
+ Answers::YesNo.new(name: question.name, probability: probability.to_f)
+ when :choice, :score
+ probabilities = distribution(question.choices || question.levels, value)
+ label, confidence = probabilities.max_by(&:last)
+ attributes = { name: question.name, probabilities:, confidence: }
+ if question.type == :choice
+ Answers::Choice.new(**attributes, choice: label)
+ else
+ score =
+ probabilities.values.each_with_index.sum do |level_probability, index|
+ level_probability * index
+ end
+ Answers::Score.new(**attributes, score:, level: label)
+ end
+ end
+ end
+ Result.new(answers:, provider_model: "canned", input_tokens: 0, output_tokens: 0)
+ end
+
+ private
+
+ def distribution(labels, value)
+ if value.is_a?(String) && labels.include?(value)
+ return labels.index_with { |label| label == value ? 1.0 : 0.0 }
+ end
+ value = value.transform_keys(&:to_s) if value.is_a?(Hash)
+ if !value.is_a?(Hash) || (value.keys - labels).any? || value.empty?
+ raise ArgumentError, "Invalid canned distribution"
+ end
+ probabilities = labels.index_with { |label| value.fetch(label, 0.0) }
+ if probabilities.values.any? { |probability|
+ !probability.is_a?(Numeric) || !probability.real? || !probability.finite? ||
+ !probability.between?(0, 1)
+ }
+ raise ArgumentError, "Invalid canned probability"
+ end
+ probabilities
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/client.rb b/plugins/discourse-ai/lib/decisions/client.rb
new file mode 100644
index 00000000000..466aa23b9fd
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/client.rb
@@ -0,0 +1,152 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ class Client
+ def initialize(decision_model, timeout: 30, max_attempts: 3)
+ @decision_model = decision_model
+ @endpoint = Endpoints::Base.for(decision_model)
+ @timeout = timeout
+ @max_attempts = max_attempts
+ end
+
+ def decide(state, questions:, feature_name:, user: nil, images: [])
+ questions = Array.wrap(questions).map { |question| Question.wrap(question) }
+ validate!(state, questions, feature_name, images)
+ if (canned = CannedAnswers.current)
+ return canned.decide(state:, questions:, feature_name:, user:, images:)
+ end
+
+ uri = URI(@decision_model.endpoint_url)
+ body = @endpoint.payload(state:, questions:, images:).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,
+ )
+ started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
+ begin
+ headers = @endpoint.headers(feature_name:, has_images: images.present?)
+ response_body = post(uri, headers, body, log)
+ begin
+ parsed = JSON.parse(response_body)
+ # Cloudflare Workers AI wraps answers in {result, success, errors}.
+ parsed = parsed["result"] if parsed.is_a?(Hash) && parsed["success"] == true
+ rescue JSON::ParserError
+ raise RequestFailed.new(
+ "Request failed: invalid JSON",
+ http_status: log.response_status,
+ )
+ end
+ answers = @endpoint.parse(parsed, questions)
+ input_tokens, output_tokens = @endpoint.usage(parsed)
+ log.request_tokens = input_tokens
+ log.response_tokens = output_tokens
+ ensure
+ log.duration_msecs =
+ ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at) * 1000).round
+ log.save!
+ AiApiRequestStat.record_from_audit_log(log, llm_model: nil)
+ end
+ Result.new(
+ answers:,
+ provider_model: parsed["model"],
+ input_tokens:,
+ output_tokens:,
+ audit_log_id: log.id,
+ )
+ end
+
+ private
+
+ def validate!(state, questions, feature_name, images)
+ raise InvalidRequest, "feature_name is required" if feature_name.blank?
+ raise InvalidRequest, "At least one question is required" if questions.empty?
+ if questions.map(&:name).uniq.size != questions.size
+ raise InvalidRequest, "Question names must be unique"
+ end
+ raise InvalidRequest, "Images must be a list of base64 data URLs" if !images.is_a?(Array)
+ if images.any? { |image|
+ !image.is_a?(String) || !image.start_with?("data:image/") ||
+ !image.include?(";base64,")
+ }
+ raise InvalidRequest, "Images must be base64 data URLs"
+ end
+ if images.present? && !@decision_model.vision_enabled
+ raise InvalidRequest, "Images require a decision model with image input enabled"
+ end
+ raise InvalidRequest, "State must be present" if state.blank?
+ if !state.is_a?(String) && !state.is_a?(Hash) && !state.is_a?(Array)
+ raise InvalidRequest, "State must be a string, hash or array"
+ 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.
+ return true if exception.is_a?(Timeout::Error)
+
+ DiscourseAi::Completions::Endpoints::Base::RETRIABLE_NETWORK_ERRORS.any? do |klass|
+ exception.is_a?(klass)
+ end
+ end
+
+ # @return [String] the body of the successful response; the audit log only keeps a
+ # redacted copy
+ def post(uri, headers, body, log)
+ attempts = []
+ loop do
+ begin
+ request = FinalDestination::HTTP::Post.new(uri.request_uri, headers)
+ request.body = body
+ response =
+ FinalDestination::HTTP.start(
+ uri.host,
+ uri.port,
+ use_ssl: uri.scheme == "https",
+ open_timeout: @timeout,
+ read_timeout: @timeout,
+ 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)
+ 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)
+ error =
+ RequestFailed.new(
+ "Request failed: #{message}",
+ retryable: retryable_network_error?(exception),
+ )
+ end
+ return response.body.to_s if error.nil?
+ raise error if !error.retryable? || attempts.size + 1 >= @max_attempts
+
+ delay = (error.retry_after || 2**attempts.size).clamp(0, 5)
+ attempts << { "status" => error.http_status || 0, "delay_ms" => (delay * 1000).round }
+ log.request_attempts = attempts
+ sleep(delay)
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/endpoints/base.rb b/plugins/discourse-ai/lib/decisions/endpoints/base.rb
new file mode 100644
index 00000000000..4babc63ab75
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/endpoints/base.rb
@@ -0,0 +1,130 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ module Endpoints
+ class Base
+ # Token columns in the audit log and usage stats are 4-byte integers.
+ MAX_TOKENS = 2_147_483_647
+
+ def self.for(model)
+ case model.provider
+ when "system_one"
+ SystemOne.new(model)
+ when "open_ai"
+ OpenAi.new(model)
+ else
+ raise InvalidRequest, "Unknown decision model provider #{model.provider.inspect}"
+ end
+ end
+
+ attr_reader :decision_model
+
+ def initialize(decision_model)
+ @decision_model = decision_model
+ end
+
+ def headers(feature_name:, has_images:)
+ headers = { "Content-Type" => "application/json" }
+ api_key = decision_model.api_key
+ headers["Authorization"] = "Bearer #{api_key}" if api_key.present?
+ headers
+ 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
+ def usage(body)
+ usage = body["usage"].is_a?(Hash) ? body["usage"] : {}
+ usage
+ .values_at("input_tokens", "output_tokens")
+ .map do |count|
+ if finite_number?(count) && count.between?(0, MAX_TOKENS) && count == count.to_i
+ count.to_i
+ end
+ end
+ end
+
+ private
+
+ def validate_names!(answers, questions)
+ if answers.keys.to_set != questions.map(&:name).to_set
+ malformed!("every question must be answered exactly once")
+ end
+ end
+
+ def parse_answer(question, raw, wire_type, probability:, probabilities:)
+ if raw["type"] == "refusal"
+ reason = raw["reason"] || raw["refusal"]
+ if !reason.nil? && !reason.is_a?(String)
+ malformed!("invalid refusal reason for #{question.name}")
+ end
+ return Answers::Refusal.new(name: question.name, reason:)
+ end
+ malformed!("unexpected type for #{question.name}") if raw["type"] != wire_type
+ case question.type
+ when :yes_no
+ Answers::YesNo.new(name: question.name, probability: probability!(probability))
+ when :choice
+ choice_answer(question, raw["choice"], probabilities, raw["confidence"])
+ when :score
+ score_answer(question, raw["score"], probabilities, raw["confidence"])
+ end
+ end
+
+ def choice_answer(question, choice, probabilities, confidence)
+ malformed!("unknown choice for #{question.name}") if !question.choices.include?(choice)
+ if !probabilities.is_a?(Hash) || probabilities.keys.sort != question.choices.sort
+ malformed!("expected one probability per choice for #{question.name}")
+ end
+ Answers::Choice.new(
+ name: question.name,
+ choice:,
+ probabilities:
+ question.choices.index_with { |value| probability!(probabilities[value]) },
+ confidence: confidence!(confidence),
+ )
+ end
+
+ def score_answer(question, score, probabilities, confidence)
+ if !probabilities.is_a?(Array) || probabilities.size != question.levels.size
+ malformed!("expected one probability per level for #{question.name}")
+ end
+ probabilities = probabilities.map { |value| probability!(value) }
+ if !finite_number?(score) || !score.between?(0, question.levels.size - 1)
+ malformed!("invalid score for #{question.name}")
+ end
+ Answers::Score.new(
+ name: question.name,
+ score:,
+ level: question.levels[probabilities.index(probabilities.max)],
+ probabilities: question.levels.zip(probabilities).to_h,
+ confidence: confidence!(confidence),
+ )
+ end
+
+ def confidence!(value)
+ value.nil? ? nil : probability!(value)
+ end
+
+ def probability!(value)
+ if !finite_number?(value) || !value.between?(0, 1)
+ malformed!("probability must be finite and in [0, 1]")
+ end
+ value
+ end
+
+ def finite_number?(value)
+ value.is_a?(Numeric) && value.real? && value.finite?
+ end
+
+ def malformed!(detail)
+ raise RequestFailed.new(
+ "Request failed: malformed response (#{detail})",
+ http_status: 200,
+ )
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/endpoints/open_ai.rb b/plugins/discourse-ai/lib/decisions/endpoints/open_ai.rb
new file mode 100644
index 00000000000..b1aa2a8ed77
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/endpoints/open_ai.rb
@@ -0,0 +1,80 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ module Endpoints
+ class OpenAi < Base
+ WIRE_TYPES = { yes_no: "predicate", choice: "choice", score: "score" }.freeze
+
+ def provider_id
+ AiApiAuditLog::Provider::OpenAiDecisions
+ end
+
+ def payload(state:, questions:, images:)
+ input = state.is_a?(String) ? state : state.to_json
+ if images.present?
+ content = [{ type: "input_text", text: input }]
+ images.each { |image| content << { type: "input_image", image_url: image } }
+ input = [{ role: "user", content: }]
+ end
+ {
+ model: decision_model.name,
+ input:,
+ questions: questions.map { |question| wire_question(question) },
+ }
+ 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")
+ end
+ raw_answers = body["answers"]
+ if raw_answers.map { |answer| answer["name"] }.uniq.size != raw_answers.size
+ malformed!("duplicate answers")
+ end
+ raw_answers = raw_answers.index_by { |answer| answer["name"] }
+ validate_names!(raw_answers, questions)
+ questions.map do |question|
+ raw = raw_answers.fetch(question.name)
+ probabilities = nil
+ if question.type != :yes_no && raw["type"] != "refusal"
+ entries = raw["probabilities"]
+ if !entries.is_a?(Array) || !entries.all?(Hash)
+ malformed!("probabilities must be a list of objects")
+ end
+ values = entries.map { |entry| entry["value"] }
+ expected =
+ question.type == :choice ? question.choices : question.levels.each_index.to_a
+ if values.uniq.size != values.size || values.to_set != expected.to_set
+ malformed!("expected one probability per choice or level")
+ end
+ probabilities = entries.to_h { |entry| [entry["value"], entry["probability"]] }
+ probabilities = expected.map { |value| probabilities[value] } if question.type ==
+ :score
+ end
+ parse_answer(
+ question,
+ raw,
+ WIRE_TYPES.fetch(question.type),
+ probability: raw["probability"],
+ probabilities:,
+ )
+ end
+ end
+
+ private
+
+ def wire_question(question)
+ wire = {
+ type: WIRE_TYPES.fetch(question.type),
+ name: question.name,
+ instructions: question.instructions,
+ }
+ wire[:choices] = question.choices.map { |value| { value: } } if question.type == :choice
+ wire[:levels] = question.levels.map { |label| { label: } } if question.type == :score
+ wire
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/endpoints/system_one.rb b/plugins/discourse-ai/lib/decisions/endpoints/system_one.rb
new file mode 100644
index 00000000000..45fa0472586
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/endpoints/system_one.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ module Endpoints
+ class SystemOne < Base
+ WIRE_TYPES = { yes_no: "noul", choice: "choice", score: "score" }.freeze
+
+ def provider_id
+ AiApiAuditLog::Provider::SystemOne
+ end
+
+ # Self-hosted System One engines sit behind the same routing proxy as
+ # self-hosted LLMs, so they get the same registered routing headers.
+ def headers(feature_name:, has_images:)
+ context =
+ RequestHeaders::Context.new(
+ model: decision_model,
+ feature_name:,
+ has_images:,
+ streaming: false,
+ )
+ super.merge(RequestHeaders.for(context))
+ end
+
+ def payload(state:, questions:, images:)
+ payload = {
+ model: decision_model.name,
+ state:,
+ questions: questions.to_h { |question| [question.name, wire_question(question)] },
+ }
+ payload[:images] = images if images.present?
+ payload
+ end
+
+ def parse(body, questions)
+ if !body.is_a?(Hash) || !body["answers"].is_a?(Hash)
+ malformed!("answers must be an object")
+ end
+ validate_names!(body["answers"], questions)
+ questions.map do |question|
+ raw = body["answers"][question.name]
+ malformed!("invalid answer for #{question.name}") if !raw.is_a?(Hash)
+ probabilities = raw["probabilities"]
+ if question.type == :score && raw["type"] != "refusal"
+ keys = question.levels.each_index.map(&:to_s)
+ if !probabilities.is_a?(Hash) || probabilities.keys.sort != keys.sort
+ malformed!("expected one probability per level for #{question.name}")
+ end
+ probabilities = keys.map { |key| probabilities[key] }
+ end
+ parse_answer(
+ question,
+ raw,
+ WIRE_TYPES.fetch(question.type),
+ probability: raw["noul"],
+ probabilities:,
+ )
+ end
+ end
+
+ private
+
+ def wire_question(question)
+ wire = { type: WIRE_TYPES.fetch(question.type), instructions: question.instructions }
+ wire[:criteria] = question.choices.index_with(&:itself) if question.type == :choice
+ wire[:criteria] = question.levels if question.type == :score
+ wire
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/error.rb b/plugins/discourse-ai/lib/decisions/error.rb
new file mode 100644
index 00000000000..ff135470f17
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/error.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ class Error < StandardError
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/invalid_request.rb b/plugins/discourse-ai/lib/decisions/invalid_request.rb
new file mode 100644
index 00000000000..89adc76ea90
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/invalid_request.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ class InvalidRequest < Error
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/question.rb b/plugins/discourse-ai/lib/decisions/question.rb
new file mode 100644
index 00000000000..ba39f8f6ded
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/question.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ class Question
+ TYPES = %i[yes_no choice score].freeze
+ NAME_FORMAT = /\A[A-Za-z_][A-Za-z0-9_]{0,63}\z/
+
+ attr_reader :name, :type, :instructions, :choices, :levels
+
+ def self.wrap(question)
+ return question if question.is_a?(self)
+ raise InvalidRequest, "Questions must be hashes" if !question.is_a?(Hash)
+
+ new(**question.symbolize_keys.slice(:name, :type, :instructions, :choices, :levels))
+ end
+
+ def initialize(name: nil, type: nil, instructions: nil, choices: nil, levels: nil)
+ @name = name.to_s.strip
+ if [email protected]?(NAME_FORMAT)
+ invalid!(:name, "must be an identifier of at most 64 characters")
+ end
+ @type = type.to_s.to_sym
+ invalid!(:type, "must be yes_no, choice or score") if !TYPES.include?(@type)
+ if !instructions.is_a?(String) || instructions.blank?
+ invalid!(:instructions, "must be present")
+ end
+ @instructions = instructions.strip
+ if !choices.nil? && @type != :choice
+ invalid!(:choices, "is only allowed for choice questions")
+ end
+ invalid!(:levels, "is only allowed for score questions") if !levels.nil? && @type != :score
+ @choices = labels(choices, :choices, 2..255) if @type == :choice
+ @levels = labels(levels, :levels, 2..10) if @type == :score
+ end
+
+ private
+
+ def labels(values, field, range)
+ if !values.is_a?(Array) || !range.cover?(values.size)
+ invalid!(field, "must contain #{range.first}..#{range.last} strings")
+ end
+ invalid!(field, "must contain strings") if !values.all?(String)
+ values = values.map(&:strip)
+ if values.any?(&:blank?) || values.uniq.size != values.size
+ invalid!(field, "must be non-blank and unique")
+ end
+ values.freeze
+ end
+
+ def invalid!(field, message)
+ raise InvalidRequest, "Question #{name.inspect} #{field}: #{message}"
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/request_failed.rb b/plugins/discourse-ai/lib/decisions/request_failed.rb
new file mode 100644
index 00000000000..6c2f6560307
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/request_failed.rb
@@ -0,0 +1,61 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ class RequestFailed < Error
+ RETRYABLE_STATUSES = [408, 429, 500, 502, 503, 504, 529].freeze
+
+ attr_reader :http_status, :retry_after
+
+ def initialize(message, http_status: nil, retry_after: nil, retryable: false)
+ super(message.to_s[0, 500])
+ @http_status = http_status
+ @retry_after = retry_after
+ @retryable = retryable
+ end
+
+ def retryable?
+ @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]",
+ )
+ end
+
+ def self.sanitize(message, api_key)
+ redact(message, api_key)[0, 500]
+ end
+
+ def self.from_response(model, response)
+ status = response.code.to_i
+ body =
+ begin
+ JSON.parse(response.body.to_s)
+ rescue JSON::ParserError
+ nil
+ end
+ message =
+ if body.is_a?(Hash)
+ detail =
+ body["error"] || body["detail"] || body["message"] || Array.wrap(body["errors"]).first
+ 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)
+ delay = Float(response["Retry-After"], exception: false)
+ delay = nil if delay && !delay.finite?
+ new(
+ "Request failed with status #{status}: #{message}",
+ http_status: status,
+ retry_after: delay,
+ retryable: RETRYABLE_STATUSES.include?(status),
+ )
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/decisions/result.rb b/plugins/discourse-ai/lib/decisions/result.rb
new file mode 100644
index 00000000000..9f166a1ac70
--- /dev/null
+++ b/plugins/discourse-ai/lib/decisions/result.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ module Decisions
+ class Result
+ attr_reader :answers, :provider_model, :input_tokens, :output_tokens, :audit_log_id
+
+ def initialize(
+ answers:,
+ provider_model: nil,
+ input_tokens: nil,
+ output_tokens: nil,
+ audit_log_id: nil
+ )
+ @answers = answers.index_by(&:name).freeze
+ @provider_model = provider_model
+ @input_tokens = input_tokens
+ @output_tokens = output_tokens
+ @audit_log_id = audit_log_id
+ end
+
+ def [](name)
+ answers.fetch(name.to_s)
+ end
+
+ def refusals
+ answers.values.select(&:refusal?)
+ end
+
+ def to_h
+ {
+ provider_model:,
+ input_tokens:,
+ output_tokens:,
+ audit_log_id:,
+ answers: answers.transform_values(&:to_h),
+ }
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/lib/request_headers.rb b/plugins/discourse-ai/lib/request_headers.rb
new file mode 100644
index 00000000000..84c296622e7
--- /dev/null
+++ b/plugins/discourse-ai/lib/request_headers.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+module DiscourseAi
+ # Lets plugins add per-request HTTP headers (e.g. routing hints for a proxy) to
+ # outbound model requests without reaching into endpoint internals.
+ module RequestHeaders
+ # `model` is the LlmModel or DecisionModel the request is for.
+ Context =
+ Struct.new(
+ :model,
+ :feature_name,
+ :feature_context,
+ :has_images,
+ :streaming,
+ keyword_init: true,
+ )
+
+ # Mutated in place (<<, clear) but never reassigned.
+ PROVIDERS = []
+
+ class << self
+ # The block receives a Context and returns a Hash of headers.
+ def register(&block)
+ PROVIDERS << block
+ end
+
+ # Provider failures are logged and skipped so a header bug can never break
+ # a request.
+ # @param context [Context]
+ # @return [Hash{String => String}]
+ def for(context)
+ PROVIDERS.each_with_object({}) do |provider, headers|
+ result = provider.call(context)
+ headers.merge!(result.stringify_keys) if result.is_a?(Hash)
+ rescue StandardError => e
+ Discourse.warn_exception(
+ e,
+ message: "Discourse AI request header provider raised an error; skipping it",
+ )
+ end
+ end
+
+ def reset!
+ PROVIDERS.clear
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rb
index 0ce4f5073a0..c27b2e5f4e8 100644
--- a/plugins/discourse-ai/plugin.rb
+++ b/plugins/discourse-ai/plugin.rb
@@ -124,6 +124,7 @@ after_initialize do
if respond_to?(:register_discourse_workflows_node)
register_discourse_workflows_node { DiscourseWorkflows::Nodes::AiAgent::V1 }
+ register_discourse_workflows_node { DiscourseWorkflows::Nodes::Classify::V1 }
end
add_admin_route("discourse_ai.title", "discourse-ai", { use_new_show_route: true })
@@ -267,6 +268,7 @@ after_initialize do
plugin_icons = %w[
chart-column
+ chart-simple
spell-check
language
images
diff --git a/plugins/discourse-ai/spec/fabricators/decision_model_fabricator.rb b/plugins/discourse-ai/spec/fabricators/decision_model_fabricator.rb
new file mode 100644
index 00000000000..e30f50c3c72
--- /dev/null
+++ b/plugins/discourse-ai/spec/fabricators/decision_model_fabricator.rb
@@ -0,0 +1,9 @@
+# frozen_string_literal: true
+
+Fabricator(:decision_model) do
+ display_name "TypeSafe Jev"
+ name "jev-latest"
+ provider "system_one"
+ url "https://api.typesafe.ai/v1/systemone"
+ ai_secret
+end
diff --git a/plugins/discourse-ai/spec/lib/completions/endpoints/vllm_spec.rb b/plugins/discourse-ai/spec/lib/completions/endpoints/vllm_spec.rb
index b8f7d48dd72..2695bfb6f2c 100644
--- a/plugins/discourse-ai/spec/lib/completions/endpoints/vllm_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/endpoints/vllm_spec.rb
@@ -998,7 +998,7 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Vllm do
end
describe "request header providers" do
- after { described_class.reset_request_headers_providers! }
+ after { DiscourseAi::RequestHeaders.reset! }
def capture_headers(feature_name: nil)
captured = nil
@@ -1026,24 +1026,24 @@ RSpec.describe DiscourseAi::Completions::Endpoints::Vllm do
expect(headers["X-Test-Feature"]).to eq("summarize")
end
- it "passes request context (llm_model, streaming, vision) to the provider" do
+ it "passes request context (model, streaming, vision) to the provider" do
captured_context = nil
- described_class.register_request_headers_provider do |context|
+ DiscourseAi::RequestHeaders.register do |context|
captured_context = context
{}
end
capture_headers(feature_name: "ai_helper")
- expect(captured_context.llm_model).to eq(llm_model)
+ expect(captured_context.model).to eq(llm_model)
expect(captured_context.feature_name).to eq("ai_helper")
expect(captured_context.streaming).to eq(false)
expect(captured_context.has_images).to eq(false)
end
it "isolates provider failures so the completion still succeeds" do
- described_class.register_request_headers_provider { |_context| raise "boom" }
- described_class.register_request_headers_provider { |_context| { "X-Test-Survivor" => "1" } }
+ DiscourseAi::RequestHeaders.register { |_context| raise "boom" }
+ DiscourseAi::RequestHeaders.register { |_context| { "X-Test-Survivor" => "1" } }
headers = nil
expect { headers = capture_headers }.not_to raise_error
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 9de44a305aa..0b1ed454b8e 100644
--- a/plugins/discourse-ai/spec/lib/completions/upload_encoder_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/upload_encoder_spec.rb
@@ -81,6 +81,16 @@ RSpec.describe DiscourseAi::Completions::UploadEncoder do
XML
end
+ describe ".normalize_upload_ids" do
+ it "accepts arrays, JSON and comma separated decimal ids" do
+ expect(
+ [[3, "3", [4]], "[5, 5]", "6, 7", "010", "0x1a", nil].map do |ids|
+ described_class.normalize_upload_ids(ids)
+ end,
+ ).to eq([[3, 4], [5], [6, 7], [10], [], []])
+ end
+ 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: MAX_PIXELS)
diff --git a/plugins/discourse-ai/spec/lib/decisions/client_spec.rb b/plugins/discourse-ai/spec/lib/decisions/client_spec.rb
new file mode 100644
index 00000000000..14bf7b8f6cb
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/decisions/client_spec.rb
@@ -0,0 +1,630 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Decisions::Client do
+ fab!(:decision_model)
+ fab!(:user)
+
+ let(:questions) do
+ [
+ { name: "urgent", type: "yes_no", instructions: "Urgent?" },
+ { name: "team", type: "choice", instructions: "Who?", choices: %w[billing returns] },
+ { name: "severity", type: "score", instructions: "How bad?", levels: %w[low medium high] },
+ ]
+ end
+ let(:response_body) do
+ {
+ model: "jev-1.13.0",
+ answers: {
+ urgent: {
+ type: "noul",
+ noul: 0.95,
+ },
+ team: {
+ type: "choice",
+ choice: "billing",
+ probabilities: {
+ billing: 0.9,
+ returns: 0.1,
+ },
+ confidence: 0.8,
+ },
+ severity: {
+ type: "score",
+ score: 1.6,
+ probabilities: {
+ "0" => 0.1,
+ "1" => 0.1,
+ "2" => 0.8,
+ },
+ confidence: nil,
+ },
+ },
+ usage: {
+ input_tokens: 296,
+ output_tokens: 0,
+ },
+ }
+ end
+ let(:client) { described_class.new(decision_model) }
+
+ before do
+ enable_current_plugin
+ described_class.any_instance.stubs(:sleep)
+ end
+
+ describe "#decide" do
+ it "sends System One questions and returns typed answers, with one audit and usage row" do
+ request =
+ stub_request(:post, decision_model.url).with(
+ headers: {
+ "Authorization" => "Bearer #{decision_model.api_key}",
+ },
+ body: {
+ model: "jev-latest",
+ state: {
+ text: "Help!",
+ },
+ questions: {
+ urgent: {
+ type: "noul",
+ instructions: "Urgent?",
+ },
+ team: {
+ type: "choice",
+ instructions: "Who?",
+ criteria: {
+ billing: "billing",
+ returns: "returns",
+ },
+ },
+ severity: {
+ type: "score",
+ instructions: "How bad?",
+ criteria: %w[low medium high],
+ },
+ },
+ }.to_json,
+ ).to_return(body: response_body.to_json)
+
+ expect {
+ @result = client.decide({ text: "Help!" }, questions:, feature_name: "workflow", user:)
+ }.to change { AiApiAuditLog.count }.by(1).and change { AiApiRequestStat.count }.by(1)
+ result = @result
+ expect(request).to have_been_requested.once
+ expect(result["urgent"]).to be_a(DiscourseAi::Decisions::Answers::YesNo)
+ expect(result["urgent"].to_h).to eq(type: "yes_no", probability: 0.95)
+ expect(result["team"]).to have_attributes(
+ choice: "billing",
+ probabilities: {
+ "billing" => 0.9,
+ "returns" => 0.1,
+ },
+ confidence: 0.8,
+ )
+ expect(result["severity"]).to have_attributes(
+ score: 1.6,
+ level: "high",
+ probabilities: {
+ "low" => 0.1,
+ "medium" => 0.1,
+ "high" => 0.8,
+ },
+ confidence: nil,
+ )
+ expect(result["team"].probabilities).to be_frozen
+ expect(result["severity"].probabilities).to be_frozen
+ expect(result.provider_model).to eq("jev-1.13.0")
+ log = AiApiAuditLog.find(result.audit_log_id)
+ expect(log).to have_attributes(
+ provider_id: AiApiAuditLog::Provider::SystemOne,
+ user_id: user.id,
+ feature_name: "workflow",
+ language_model: "jev-latest",
+ request_tokens: 296,
+ response_tokens: 0,
+ response_status: 200,
+ request_attempts: nil,
+ )
+ expect(AiApiRequestStat.last).to have_attributes(
+ usage_count: 1,
+ llm_id: nil,
+ request_tokens: 296,
+ provider_id: AiApiAuditLog::Provider::SystemOne,
+ )
+ end
+
+ it "omits authorization when the endpoint has no credential" do
+ decision_model.update!(ai_secret: nil)
+ request =
+ stub_request(:post, decision_model.url)
+ .with { |req| !req.headers.key?("Authorization") }
+ .to_return(body: response_body.to_json)
+ client.decide("Hello", questions:, feature_name: "test")
+ expect(request).to have_been_requested.once
+ end
+
+ it "returns explicit refusals without discarding other answers" do
+ response_body[:answers][:urgent] = { type: "refusal", reason: "Cannot answer" }
+ stub_request(:post, decision_model.url).to_return(body: response_body.to_json)
+ result = client.decide("Hello", questions:, feature_name: "test")
+ expect(result.refusals.map(&:name)).to eq(["urgent"])
+ expect(result["urgent"].to_h).to eq(type: "refusal", reason: "Cannot answer")
+ expect(result["team"].choice).to eq("billing")
+ end
+
+ it "rejects malformed answers, probabilities and confidence without retrying" do
+ malformed = [
+ [],
+ response_body.deep_dup.tap { |body| body[:answers] = [] },
+ response_body.deep_dup.tap { |body| body[:answers][:urgent] = nil },
+ response_body.deep_dup.tap do |body|
+ body[:answers][:urgent] = { type: "refusal", reason: 1 }
+ end,
+ response_body.deep_dup.tap { |body| body[:answers].delete(:urgent) },
+ response_body.deep_dup.tap { |body| body[:answers][:extra] = body[:answers][:urgent] },
+ response_body.deep_dup.tap { |body| body[:answers][:urgent][:type] = "predicate" },
+ response_body.deep_dup.tap { |body| body[:answers][:urgent][:noul] = 1.1 },
+ response_body.deep_dup.tap { |body| body[:answers][:team][:choice] = "other" },
+ response_body.deep_dup.tap do |body|
+ body[:answers][:team][:probabilities].delete(:returns)
+ end,
+ response_body.deep_dup.tap do |body|
+ body[:answers][:severity][:probabilities]["0"] = "0.1"
+ end,
+ response_body.deep_dup.tap { |body| body[:answers][:severity][:probabilities]["3"] = 0.0 },
+ response_body.deep_dup.tap { |body| body[:answers][:team][:confidence] = -0.1 },
+ response_body.deep_dup.tap { |body| body[:answers][:severity][:score] = 3 },
+ ]
+ request =
+ stub_request(:post, decision_model.url).to_return(
+ *malformed.map { |body| { body: body.to_json } },
+ )
+ malformed.each do
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ ) do |error|
+ expect(error).to have_attributes(http_status: 200, retryable?: false)
+ end
+ end
+ expect(request).to have_been_requested.times(malformed.size)
+ expect(AiApiAuditLog.count).to eq(malformed.size)
+ expect(AiApiRequestStat.count).to eq(malformed.size)
+ end
+
+ it "rejects invalid JSON in successful responses without retrying" do
+ request = stub_request(:post, decision_model.url).to_return(body: "not JSON")
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ /invalid JSON/,
+ )
+ expect(request).to have_been_requested.once
+ expect(AiApiAuditLog.last.request_attempts).to be_nil
+ end
+
+ it "retains the last response body when the final attempt fails on the network" do
+ body = { message: "Busy" }.to_json
+ request =
+ stub_request(:post, decision_model.url)
+ .to_return(status: 503, body:)
+ .then
+ .to_raise(EOFError)
+ expect {
+ described_class.new(decision_model, max_attempts: 2).decide(
+ "Hello",
+ questions:,
+ feature_name: "test",
+ )
+ }.to raise_error(DiscourseAi::Decisions::RequestFailed)
+ expect(request).to have_been_requested.times(2)
+ expect(AiApiAuditLog.last).to have_attributes(
+ response_status: 0,
+ raw_response_payload: body,
+ request_attempts: [{ "status" => 503, "delay_ms" => 1000 }],
+ )
+ expect(AiApiRequestStat.count).to eq(1)
+ end
+
+ it "retries HTTP and network errors with capped delays and records only retried attempts" do
+ request =
+ stub_request(:post, decision_model.url)
+ .to_return(status: 429, headers: { "Retry-After" => "20" })
+ .then
+ .to_raise(Timeout::Error)
+ .then
+ .to_return(body: response_body.to_json)
+ expect { @result = client.decide("Hello", questions:, feature_name: "test") }.to change {
+ AiApiRequestStat.count
+ }.by(1)
+ expect(request).to have_been_requested.times(3)
+ log = AiApiAuditLog.find(@result.audit_log_id)
+ expect(log.request_attempts).to eq(
+ [{ "status" => 429, "delay_ms" => 5000 }, { "status" => 0, "delay_ms" => 2000 }],
+ )
+ expect(log.response_status).to eq(200)
+ expect(log.raw_response_payload).to eq(response_body.to_json)
+ end
+
+ it "stops after max_attempts and records failures once" do
+ request =
+ stub_request(:post, decision_model.url).to_return(
+ status: 503,
+ body: { detail: "Busy" }.to_json,
+ )
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ ) { |error| expect(error).to have_attributes(http_status: 503, retryable?: true) }
+ expect(request).to have_been_requested.times(3)
+ expect(AiApiAuditLog.last.request_attempts).to eq(
+ [{ "status" => 503, "delay_ms" => 1000 }, { "status" => 503, "delay_ms" => 2000 }],
+ )
+ expect(AiApiRequestStat.count).to eq(1)
+ end
+
+ it "does not retry SSRF failures" do
+ WebMock.enable!(except: [:final_destination])
+ FinalDestination::SSRFDetector.allow_ip_lookups_in_test!
+ stub_ip_lookup(URI(decision_model.url).host, ["169.254.169.254"])
+
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ /all resolved IPs were disallowed/,
+ ) { |error| expect(error).not_to be_retryable }
+ expect(AiApiAuditLog.last).to have_attributes(response_status: 0, request_attempts: nil)
+ expect(AiApiRequestStat.count).to eq(1)
+ ensure
+ WebMock.enable!
+ FinalDestination::SSRFDetector.disallow_ip_lookups_in_test!
+ end
+
+ it "sanitizes provider errors and does not retry rejected requests" do
+ leaked = "#{decision_model.api_key} Bearer xyz-secret sk-provider-secret key-other-secret"
+ request =
+ stub_request(:post, decision_model.url).to_return(
+ status: 401,
+ body: { error: { message: "Rejected #{leaked} #{"x" * 600}" } }.to_json,
+ )
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ ) do |error|
+ expect(error.message).to start_with("Request failed with status 401: Rejected")
+ expect(error.message).not_to include(
+ decision_model.api_key,
+ "xyz-secret",
+ "sk-provider-secret",
+ "key-other-secret",
+ )
+ expect(error.message.size).to be <= 500
+ expect(error).to have_attributes(http_status: 401, retryable?: false)
+ end
+ expect(request).to have_been_requested.once
+ expect(AiApiAuditLog.last).to have_attributes(response_status: 401, request_attempts: nil)
+ expect(AiApiAuditLog.last.raw_response_payload).not_to include(
+ decision_model.api_key,
+ "xyz-secret",
+ "sk-provider-secret",
+ )
+ end
+
+ it "redacts only the stored response, never the answers" do
+ key_questions = [
+ { name: "team", type: "choice", instructions: "Who?", choices: %w[key-account other] },
+ ]
+ body = {
+ model: "jev",
+ answers: {
+ team: {
+ type: "choice",
+ choice: "key-account",
+ probabilities: {
+ "key-account" => 0.9,
+ "other" => 0.1,
+ },
+ confidence: 0.8,
+ },
+ },
+ }
+ stub_request(:post, decision_model.url).to_return(body: body.to_json)
+
+ result = client.decide("Hello", questions: key_questions, feature_name: "test")
+
+ expect(result["team"].choice).to eq("key-account")
+ expect(AiApiAuditLog.last.raw_response_payload).not_to include("key-account")
+ end
+
+ it "unwraps Cloudflare Workers AI responses and errors" do
+ envelope = { result: response_body, success: true, errors: [], messages: [] }
+ cloudflare_error = {
+ errors: [
+ { message: "AiError: AiError: image must be an embedded base64 data URI", code: 5012 },
+ ],
+ success: false,
+ result: {
+ },
+ }
+ stub_request(:post, decision_model.url).to_return(
+ { body: envelope.to_json },
+ { status: 422, body: cloudflare_error.to_json },
+ )
+
+ expect(client.decide("Hello", questions:, feature_name: "test")["urgent"].probability).to eq(
+ 0.95,
+ )
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ "Request failed with status 422: image must be an embedded base64 data URI",
+ )
+ end
+
+ it "keeps valid answers when usage counts are unusable" do
+ body =
+ response_body.deep_dup.tap do |payload|
+ payload[:usage] = { input_tokens: 296.0, output_tokens: 3_000_000_000 }
+ end
+ stub_request(:post, decision_model.url).to_return(body: body.to_json)
+
+ result = client.decide("Hello", questions:, feature_name: "test")
+
+ expect([result.input_tokens, result.output_tokens]).to eq([296, nil])
+ 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
+ decision_model.update!(vision_enabled: true)
+ image = "data:image/png;base64,AAAA"
+ state = image + "x" * 20_001
+ request =
+ stub_request(:post, decision_model.url)
+ .with { |req| JSON.parse(req.body).values_at("state", "images") == [state, [image]] }
+ .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]"])
+ expect(request).to have_been_requested.once
+ end
+
+ it "resolves SRV endpoints and preserves the path" do
+ decision_model.update!(url: "srv://_decisions._tcp.example.com/v1/decide?region=eu")
+ DiscourseAi::Utils::DnsSrv
+ .stubs(:lookup)
+ .with("_decisions._tcp.example.com")
+ .returns(Resolv::DNS::Resource::IN::SRV.new(0, 0, 8443, "engine.example.com"))
+ request =
+ stub_request(:post, "https://engine.example.com:8443/v1/decide?region=eu").to_return(
+ body: response_body.to_json,
+ )
+ client.decide("Hello", questions:, feature_name: "test")
+ expect(request).to have_been_requested.once
+ end
+
+ it "does not log when SRV discovery fails before transport" do
+ decision_model.update!(url: "srv://missing.example.com/path")
+ DiscourseAi::Utils::DnsSrv.stubs(:lookup).returns(nil)
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ ) { |error| expect(error).not_to be_retryable }
+ expect(AiApiAuditLog.count).to eq(0)
+
+ DiscourseAi::Utils::DnsSrv.stubs(:lookup).raises(Resolv::ResolvTimeout)
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ /SRV lookup/,
+ ) { |error| expect(error).not_to be_retryable }
+ expect(AiApiAuditLog.count).to eq(0)
+ end
+
+ it "validates requests before transport or audit logging" do
+ [
+ { feature_name: "" },
+ { questions: [] },
+ { questions: questions + [questions.first] },
+ { images: ["https://example.com/image.png"] },
+ { images: ["data:image/png,AAAA"] },
+ { images: ["data:image/png;base64,AAAA"] },
+ { images: [123] },
+ { images: nil },
+ { questions: [{ name: "invalid-name", type: "yes_no", instructions: "Question?" }] },
+ ].each do |overrides|
+ expect {
+ client.decide("Hello", **{ questions:, feature_name: "test" }.merge(overrides))
+ }.to raise_error(DiscourseAi::Decisions::InvalidRequest)
+ end
+ ["", nil, {}, [], 123].each do |state|
+ expect { client.decide(state, questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::InvalidRequest,
+ )
+ end
+ expect(AiApiAuditLog.count).to eq(0)
+ expect(AiApiRequestStat.count).to eq(0)
+ end
+
+ it "uses a single attempt when configured" do
+ request = stub_request(:post, decision_model.url).to_return(status: 529)
+ expect {
+ described_class.new(decision_model, timeout: 10, max_attempts: 1).decide(
+ "Hello",
+ questions:,
+ feature_name: "test",
+ )
+ }.to raise_error(DiscourseAi::Decisions::RequestFailed)
+ expect(request).to have_been_requested.once
+ expect(AiApiAuditLog.last.request_attempts).to be_nil
+ end
+ end
+
+ describe "#decide with OpenAI" do
+ let(:open_ai_body) do
+ {
+ model: "gpt-6-luna-version",
+ answers: [
+ { type: "predicate", name: "urgent", probability: 0.2 },
+ {
+ type: "choice",
+ name: "team",
+ choice: "returns",
+ probabilities: [
+ { value: "returns", probability: 0.7 },
+ { value: "billing", probability: 0.3 },
+ ],
+ confidence: 0.6,
+ },
+ {
+ type: "score",
+ name: "severity",
+ score: 0.1,
+ probabilities: [
+ { value: 2, label: "high", probability: 0.0 },
+ { value: 0, probability: 0.9 },
+ { value: 1, probability: 0.1 },
+ ],
+ confidence: 0.85,
+ },
+ ],
+ }
+ end
+
+ before do
+ decision_model.update!(
+ provider: "open_ai",
+ name: "gpt-6-luna",
+ url: "https://api.openai.com/v1/decisions",
+ vision_enabled: true,
+ )
+ end
+
+ it "sends typed questions and images, normalizes score order and redacts audit images" do
+ image = "data:image/png;base64,AAAA"
+ request =
+ stub_request(:post, decision_model.url).with(
+ body: {
+ model: "gpt-6-luna",
+ input: [
+ {
+ role: "user",
+ content: [
+ { type: "input_text", text: '["Help!"]' },
+ { type: "input_image", image_url: image },
+ ],
+ },
+ ],
+ questions: [
+ { type: "predicate", name: "urgent", instructions: "Urgent?" },
+ {
+ type: "choice",
+ name: "team",
+ instructions: "Who?",
+ choices: [{ value: "billing" }, { value: "returns" }],
+ },
+ {
+ type: "score",
+ name: "severity",
+ instructions: "How bad?",
+ levels: [{ label: "low" }, { label: "medium" }, { label: "high" }],
+ },
+ ],
+ }.to_json,
+ ).to_return(body: open_ai_body.to_json)
+ result = client.decide(["Help!"], questions:, images: [image], feature_name: "test")
+ expect(request).to have_been_requested.once
+ expect(result["urgent"].probability).to eq(0.2)
+ expect(result["team"]).to have_attributes(
+ choice: "returns",
+ probabilities: {
+ "billing" => 0.3,
+ "returns" => 0.7,
+ },
+ confidence: 0.6,
+ )
+ expect(result["severity"]).to have_attributes(
+ score: 0.1,
+ level: "low",
+ probabilities: {
+ "low" => 0.9,
+ "medium" => 0.1,
+ "high" => 0.0,
+ },
+ )
+ log = AiApiAuditLog.find(result.audit_log_id)
+ expect(log.provider_id).to eq(AiApiAuditLog::Provider::OpenAiDecisions)
+ expect(log.raw_request_payload).not_to include(image)
+ expect(
+ JSON.parse(log.raw_request_payload).dig("input", 0, "content", 1, "image_url"),
+ ).to match(%r{\A\[redacted image/\w+, [\d.]+ \w+\]\z})
+ 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)
+ expect(client.decide("Hello", questions:, feature_name: "test")["urgent"].to_h).to eq(
+ type: "refusal",
+ reason: "Cannot answer",
+ )
+ end
+
+ it "rejects duplicate answers and missing or duplicate level probabilities" do
+ malformed = [
+ open_ai_body.deep_dup.tap { |body| body[:answers] = {} },
+ open_ai_body.deep_dup.tap { |body| body[:answers][1][:probabilities] = {} },
+ open_ai_body.deep_dup.tap { |body| body[:answers] << body[:answers].first },
+ open_ai_body.deep_dup.tap { |body| body[:answers][2][:probabilities].pop },
+ open_ai_body.deep_dup.tap do |body|
+ body[:answers][2][:probabilities] << body[:answers][2][:probabilities].first
+ end,
+ open_ai_body.deep_dup.tap { |body| body[:answers][0][:probability] = nil },
+ ]
+ request =
+ stub_request(:post, decision_model.url).to_return(
+ *malformed.map { |body| { body: body.to_json } },
+ )
+ malformed.each do
+ expect { client.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ ) { |error| expect(error).not_to be_retryable }
+ end
+ expect(request).to have_been_requested.times(malformed.size)
+ end
+ end
+
+ describe "request header providers" do
+ after { DiscourseAi::RequestHeaders.reset! }
+
+ it "sends registered routing headers with System One decisions" do
+ captured = nil
+ DiscourseAi::RequestHeaders.register do |context|
+ captured = context
+ { "X-Test-Feature" => context.feature_name }
+ end
+ decision_model.update!(vision_enabled: true)
+ request =
+ stub_request(:post, decision_model.url).with(
+ headers: {
+ "X-Test-Feature" => "workflow",
+ "Authorization" => "Bearer #{decision_model.api_key}",
+ },
+ ).to_return(body: response_body.to_json)
+
+ client.decide(
+ "Hello",
+ questions:,
+ images: ["data:image/png;base64,AAAA"],
+ feature_name: "workflow",
+ )
+
+ expect(request).to have_been_requested.once
+ expect(captured).to have_attributes(model: decision_model, has_images: true, streaming: false)
+ end
+
+ it "does not send them to OpenAI" do
+ DiscourseAi::RequestHeaders.register { |_context| { "X-Test-Feature" => "x" } }
+ decision_model.update!(provider: "open_ai", url: "https://api.openai.com/v1/decisions")
+ request =
+ stub_request(:post, decision_model.url)
+ .with { |req| !req.headers.key?("X-Test-Feature") }
+ .to_return(status: 400, body: "{}")
+
+ expect { client.decide("Hello", questions:, feature_name: "workflow") }.to raise_error(
+ DiscourseAi::Decisions::RequestFailed,
+ )
+ expect(request).to have_been_requested.once
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/decisions/question_spec.rb b/plugins/discourse-ai/spec/lib/decisions/question_spec.rb
new file mode 100644
index 00000000000..b24e363e166
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/decisions/question_spec.rb
@@ -0,0 +1,91 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Decisions::Question do
+ describe ".wrap" do
+ it "builds typed questions with stripped strings and preserves wrapped questions" do
+ question =
+ described_class.wrap(
+ "name" => " team ",
+ "type" => "choice",
+ "instructions" => " Who? ",
+ "choices" => [" billing ", "returns"],
+ )
+ expect(question).to have_attributes(
+ name: "team",
+ type: :choice,
+ instructions: "Who?",
+ choices: %w[billing returns],
+ levels: nil,
+ )
+ expect(described_class.wrap(question)).to equal(question)
+ expect(
+ described_class.new(
+ name: "severity",
+ type: :score,
+ instructions: "How bad?",
+ levels: [" low ", "high"],
+ ).levels,
+ ).to eq(%w[low high])
+ end
+
+ it "identifies the question and invalid field" do
+ base = { name: "team", type: "choice", instructions: "Who?", choices: %w[billing returns] }
+ [
+ [:name, "1team"],
+ [:name, "a" * 65],
+ [:type, "unknown"],
+ [:instructions, " "],
+ [:levels, %w[low high]],
+ [:choices, ["only"]],
+ [:choices, Array.new(256) { |index| index.to_s }],
+ [:choices, ["same", " same "]],
+ [:choices, ["", "valid"]],
+ [:choices, [1, "valid"]],
+ [:choices, { "a" => "a", "b" => "b" }],
+ ].each do |field, value|
+ expect { described_class.wrap(base.merge(field => value)) }.to raise_error(
+ DiscourseAi::Decisions::InvalidRequest,
+ /Question .*#{field}:/,
+ )
+ end
+ [nil, ["only"], Array.new(11) { |index| index.to_s }, %w[a a], ["a", " "]].each do |levels|
+ expect {
+ described_class.new(name: "severity", type: :score, instructions: "How bad?", levels:)
+ }.to raise_error(DiscourseAi::Decisions::InvalidRequest, /severity.*levels/)
+ end
+ expect { described_class.wrap({}) }.to raise_error(
+ DiscourseAi::Decisions::InvalidRequest,
+ /name:/,
+ )
+ expect { described_class.wrap(base.merge(type: "yes_no")) }.to raise_error(
+ DiscourseAi::Decisions::InvalidRequest,
+ /choices:/,
+ )
+ end
+
+ it "accepts the name and collection limits" do
+ expect(
+ described_class
+ .new(
+ name: "_" + "a" * 63,
+ type: :choice,
+ instructions: "Who?",
+ choices: Array.new(255) { |index| index.to_s },
+ )
+ .choices
+ .size,
+ ).to eq(255)
+ expect(
+ described_class
+ .new(
+ name: "severity",
+ type: :score,
+ instructions: "How bad?",
+ levels: Array.new(10) { |index| index.to_s },
+ )
+ .levels
+ .size,
+ ).to eq(10)
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/decisions_spec.rb b/plugins/discourse-ai/spec/lib/decisions_spec.rb
new file mode 100644
index 00000000000..6db120797e6
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/decisions_spec.rb
@@ -0,0 +1,115 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Decisions do
+ fab!(:decision_model)
+ fab!(:user)
+
+ let(:questions) do
+ [
+ { name: "spam", type: "yes_no", instructions: "Spam?" },
+ { name: "team", type: "choice", instructions: "Who?", choices: %w[billing returns] },
+ { name: "severity", type: "score", instructions: "How bad?", levels: %w[low high] },
+ ]
+ end
+
+ before { enable_current_plugin }
+
+ describe ".with_canned_answers" do
+ it "returns value objects, records calls across threads, and creates no audit or usage rows" do
+ described_class.with_canned_answers(
+ spam: 1,
+ team: "billing",
+ severity: {
+ "low" => 0.25,
+ "high" => 0.75,
+ },
+ unused: true,
+ ) do |canned|
+ result =
+ Thread
+ .new { decision_model.decide("Buy now!", questions:, user:, feature_name: "test") }
+ .value
+ expect(result).to be_a(DiscourseAi::Decisions::Result)
+ expect(result["spam"].to_h).to eq(type: "yes_no", probability: 1.0)
+ expect(result[:team]).to have_attributes(choice: "billing", confidence: 1.0)
+ expect(result["severity"]).to have_attributes(
+ score: 0.75,
+ level: "high",
+ probabilities: {
+ "low" => 0.25,
+ "high" => 0.75,
+ },
+ )
+ expect(result.to_h[:answers]["team"]).to eq(result["team"].to_h)
+ expect { result["unknown"] }.to raise_error(KeyError)
+ expect(canned.calls).to contain_exactly(
+ state: "Buy now!",
+ questions: all(be_a(DiscourseAi::Decisions::Question)),
+ user:,
+ feature_name: "test",
+ images: [],
+ )
+ expect(AiApiAuditLog.count).to eq(0)
+ expect(AiApiRequestStat.count).to eq(0)
+ end
+ end
+
+ it "supports boolean predicates, distributions, labels and refusals" do
+ [true, false].each do |value|
+ described_class.with_canned_answers(
+ spam: value,
+ team: {
+ "returns" => 0.8,
+ "billing" => 0.2,
+ },
+ severity: "low",
+ ) do
+ result = decision_model.decide("Hello", questions:, feature_name: "test")
+ expect(result["spam"].probability).to eq(value ? 1.0 : 0.0)
+ expect(result["team"].choice).to eq("returns")
+ expect(result["severity"]).to have_attributes(score: 0.0, level: "low")
+ end
+ end
+ described_class.with_canned_answers(spam: :refusal, team: :refusal, severity: :refusal) do
+ result = decision_model.decide("Hello", questions:, feature_name: "test")
+ expect(result.refusals.map(&:name)).to eq(%w[spam team severity])
+ expect(result["spam"].to_h).to eq(type: "refusal", reason: nil)
+ end
+ end
+
+ it "resets after exceptions and rejects nesting without clearing the outer block" do
+ expect { described_class.with_canned_answers { raise "failed" } }.to raise_error("failed")
+ expect(DiscourseAi::Decisions::CannedAnswers.current).to be_nil
+ described_class.with_canned_answers(spam: 0.1) do |outer|
+ expect { described_class.with_canned_answers {} }.to raise_error(ArgumentError, /nested/)
+ expect(DiscourseAi::Decisions::CannedAnswers.current).to equal(outer)
+ expect { decision_model.decide("Hello", questions:, feature_name: "test") }.to raise_error(
+ ArgumentError,
+ /No canned answer for question "team"/,
+ )
+ expect { decision_model.decide("", questions:, feature_name: "test") }.to raise_error(
+ DiscourseAi::Decisions::InvalidRequest,
+ )
+ end
+ expect(DiscourseAi::Decisions::CannedAnswers.current).to be_nil
+ end
+
+ it "rejects invalid canned probabilities and distributions" do
+ [
+ { spam: -0.1 },
+ { team: "other" },
+ { team: {} },
+ { team: { billing: 2 } },
+ { severity: { low: Float::INFINITY } },
+ ].each do |overrides|
+ described_class.with_canned_answers(
+ **{ spam: true, team: "billing", severity: "low" }.merge(overrides),
+ ) do
+ expect {
+ decision_model.decide("Hello", questions:, feature_name: "test")
+ }.to raise_error(ArgumentError, /Invalid canned/)
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb
new file mode 100644
index 00000000000..6fd1de0400c
--- /dev/null
+++ b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/classify_spec.rb
@@ -0,0 +1,205 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::Classify::V1 do
+ fab!(:decision_model) { Fabricate(:decision_model, display_name: "Workflow decisions") }
+
+ let(:spam_question) do
+ { "name" => "is_spam", "type" => "yes_no", "instructions" => "Is this post spam?" }
+ end
+ let(:team_question) do
+ {
+ "name" => "team",
+ "type" => "choice",
+ "instructions" => "Which team?",
+ "choices" => %w[billing returns],
+ }
+ end
+ let(:score_question) do
+ {
+ "name" => "severity",
+ "type" => "score",
+ "instructions" => "How bad?",
+ "levels" => %w[low high],
+ }
+ end
+
+ before { enable_current_plugin }
+
+ def configuration(*questions, **overrides)
+ {
+ "decision_model_id" => decision_model.id,
+ "text" => "={{ $json.post.raw }}",
+ "questions" => {
+ "values" => questions,
+ },
+ }.merge(overrides.stringify_keys)
+ end
+
+ def post_item(raw)
+ { "json" => { "post" => { "raw" => raw } } }
+ end
+
+ it "passes the framework's property schema validation" do
+ expect(
+ DiscourseWorkflows::PropertySchemaValidator.call(
+ "action:classify",
+ described_class.property_schema,
+ ),
+ ).to be_empty
+ end
+
+ describe ".available?" do
+ it "requires the AI plugin and a decision model" do
+ expect(described_class.available?).to eq(true)
+ SiteSetting.discourse_ai_enabled = false
+ expect(described_class.available?).to eq(false)
+ SiteSetting.discourse_ai_enabled = true
+ DecisionModel.delete_all
+ expect(described_class.available?).to eq(false)
+ end
+ end
+
+ describe ".load_options_context" do
+ it "lists decision models matching the filter" do
+ Fabricate(:decision_model, display_name: "Another one")
+ context =
+ DiscourseWorkflows::LoadOptionsContext.new(
+ method_name: "decision_models",
+ filter: "Workflow",
+ )
+ expect(described_class.load_options_context(context)).to eq(
+ [{ id: decision_model.id, name: "Workflow decisions" }],
+ )
+ end
+ end
+
+ describe "#execute" do
+ it "adds typed answers to each item and matches the resolved output schema" do
+ config = configuration(spam_question, team_question, score_question)
+ DiscourseAi::Decisions.with_canned_answers(
+ is_spam: 0.95,
+ team: "billing",
+ severity: {
+ "low" => 0.25,
+ "high" => 0.75,
+ },
+ ) do |canned|
+ items =
+ execute_node_output(
+ configuration: config,
+ input_items: [post_item("Buy now!"), post_item("Hello!")],
+ ).first
+ expected = {
+ "is_spam" => {
+ "type" => "yes_no",
+ "probability" => 0.95,
+ },
+ "team" => {
+ "type" => "choice",
+ "choice" => "billing",
+ "probabilities" => {
+ "billing" => 1.0,
+ "returns" => 0.0,
+ },
+ "confidence" => 1.0,
+ },
+ "severity" => {
+ "type" => "score",
+ "score" => 0.75,
+ "level" => "high",
+ "probabilities" => {
+ "low" => 0.25,
+ "high" => 0.75,
+ },
+ "confidence" => 0.75,
+ },
+ }
+ expect(items.map { |item| item.dig("json", "classification") }).to eq([expected, expected])
+ expect(items.first.dig("json", "post", "raw")).to eq("Buy now!")
+ expect(items.first["json"]).to match_node_output_schema(
+ described_class,
+ configuration: config,
+ )
+ expect(canned.calls.map { |call| call[:state] }).to eq(["Buy now!", "Hello!"])
+ expect(canned.calls.map { |call| call[:feature_name] }.uniq).to eq(["workflow"])
+ expect(canned.calls.map { |call| call[:user] }.uniq).to eq([Discourse.system_user])
+ end
+ end
+
+ it "keeps refusals in the output without failing the item" do
+ config = configuration(spam_question, team_question)
+ DiscourseAi::Decisions.with_canned_answers(is_spam: :refusal, team: "returns") do
+ item = execute_node(configuration: config, item: post_item("Hello"))
+ expect(item.dig("classification", "is_spam")).to eq("type" => "refusal", "reason" => nil)
+ expect(item.dig("classification", "team", "choice")).to eq("returns")
+ expect(item).to match_node_output_schema(described_class, configuration: config)
+ end
+ end
+
+ it "fails the item when questions are invalid" do
+ expect {
+ execute_node(
+ configuration: configuration(team_question.merge("choices" => ["only"])),
+ item: post_item("Hello"),
+ )
+ }.to raise_error(DiscourseWorkflows::NodeError, /Workflow decisions.*team.*choices/)
+
+ expect {
+ execute_node(configuration: configuration("not a question"), item: post_item("Hello"))
+ }.to raise_error(DiscourseWorkflows::NodeError, /Questions must be hashes/)
+ end
+
+ it "names the decision model in provider failures" do
+ request =
+ stub_request(:post, decision_model.url).to_return(
+ status: 401,
+ body: { message: "Rejected" }.to_json,
+ )
+ expect {
+ execute_node(configuration: configuration(spam_question), item: post_item("Hello"))
+ }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "Decision model 'Workflow decisions' could not answer: Request failed with status 401: Rejected [item 0]",
+ )
+ expect(request).to have_been_requested.once
+ end
+
+ it "sends image uploads to a vision-enabled model" do
+ decision_model.update!(vision_enabled: 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,") }
+ .to_return(body: { answers: { is_hotdog: { type: "noul", noul: 0.97 } } }.to_json)
+ config =
+ configuration(
+ { "name" => "is_hotdog", "type" => "yes_no", "instructions" => "Is this a hot dog?" },
+ upload_ids: "={{ $json.post.upload_ids }}",
+ )
+ item =
+ execute_node(
+ configuration: config,
+ item: {
+ "json" => {
+ "post" => {
+ "raw" => "Lunch",
+ "upload_ids" => "[#{upload.id}, #{upload.id}]",
+ },
+ },
+ },
+ )
+ expect(request).to have_been_requested.once
+ expect(item.dig("classification", "is_hotdog", "probability")).to eq(0.97)
+ expect(item).to match_node_output_schema(described_class, configuration: config)
+ end
+
+ it "fails when the decision model no longer exists" do
+ expect {
+ execute_node(
+ configuration: configuration(spam_question, decision_model_id: -1),
+ item: post_item("Hello"),
+ )
+ }.to raise_error(DiscourseWorkflows::NodeError, "Decision model with id -1 not found")
+ end
+ end
+end
diff --git a/plugins/discourse-ai/spec/models/ai_secret_spec.rb b/plugins/discourse-ai/spec/models/ai_secret_spec.rb
index dcc794688ee..f62861fa37b 100644
--- a/plugins/discourse-ai/spec/models/ai_secret_spec.rb
+++ b/plugins/discourse-ai/spec/models/ai_secret_spec.rb
@@ -94,6 +94,12 @@ RSpec.describe AiSecret do
expect(ai_secret.in_use?).to eq(true)
end
+ it "returns true when used by a decision_model" do
+ Fabricate(:decision_model, ai_secret: ai_secret)
+
+ expect(ai_secret.in_use?).to eq(true)
+ end
+
it "returns true when used by an mcp server" do
Fabricate(:ai_mcp_server, ai_secret: ai_secret)
@@ -158,6 +164,14 @@ RSpec.describe AiSecret do
expect(tool_usage[:name]).to include("Weather Tool")
end
+ it "lists decision_models using this secret" do
+ decision_model = Fabricate(:decision_model, display_name: "Jev", ai_secret: ai_secret)
+
+ expect(ai_secret.used_by).to contain_exactly(
+ { type: "decision_model", name: "Jev", id: decision_model.id },
+ )
+ end
+
it "lists mcp servers using this secret" do
server = Fabricate(:ai_mcp_server, name: "Jira", ai_secret: ai_secret)
diff --git a/plugins/discourse-ai/spec/models/decision_model_spec.rb b/plugins/discourse-ai/spec/models/decision_model_spec.rb
new file mode 100644
index 00000000000..2deb7427b00
--- /dev/null
+++ b/plugins/discourse-ai/spec/models/decision_model_spec.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+RSpec.describe DecisionModel do
+ before { enable_current_plugin }
+
+ describe "validations" do
+ it "requires a supported provider, URL and an existing credential when selected" do
+ [
+ nil,
+ "ftp://example.com",
+ "garbage https://example.com",
+ "https:///missing-host",
+ "srv://_decisions._tcp.example.com:8443/path",
+ ].each do |url|
+ model = Fabricate.build(:decision_model, url:)
+ expect(model).not_to be_valid
+ expect(model.errors[:url]).to be_present
+ end
+ expect(Fabricate.build(:decision_model, provider: "unknown")).not_to be_valid
+ expect(
+ DecisionModel.new(
+ display_name: "Test",
+ name: "test",
+ provider: "system_one",
+ url: "https://example.com",
+ ai_secret_id: -1,
+ ),
+ ).not_to be_valid
+ expect(
+ Fabricate.build(
+ :decision_model,
+ ai_secret: nil,
+ url: "srv://_decisions._tcp.example.com/path",
+ ),
+ ).to be_valid
+ end
+ end
+
+ describe ".presets" do
+ 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).to be_frozen
+ expect(described_class.presets).to all(be_frozen)
+ end
+ end
+end
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
new file mode 100644
index 00000000000..8d20c023c01
--- /dev/null
+++ b/plugins/discourse-ai/spec/requests/admin/ai_decision_models_controller_spec.rb
@@ -0,0 +1,255 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseAi::Admin::AiDecisionModelsController do
+ fab!(:admin)
+ fab!(:ai_secret)
+
+ let(:valid_attrs) do
+ {
+ display_name: "Jev",
+ name: "jev-latest",
+ provider: "system_one",
+ url: "https://api.typesafe.ai/v1/systemone",
+ ai_secret_id: ai_secret.id,
+ vision_enabled: false,
+ }
+ end
+
+ before do
+ enable_current_plugin
+ sign_in(admin)
+ end
+
+ describe "#index" do
+ fab!(:decision_model)
+
+ it "lists decision models with providers, presets and secrets" do
+ 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.dig("meta", "presets").map { |preset| preset["preset_id"] },
+ ).to eq(DecisionModel.presets.map { |preset| preset[:preset_id] })
+ expect(
+ response.parsed_body.dig("meta", "ai_secrets").map { |secret| secret["id"] },
+ ).to include(ai_secret.id)
+ expect(response.body).not_to include(ai_secret.secret, decision_model.api_key)
+ end
+ end
+
+ describe "authorization" do
+ fab!(:decision_model)
+
+ it "restricts every action to administrators" do
+ sign_in(Fabricate(:moderator))
+ base_path = "/admin/plugins/discourse-ai/ai-decision-models"
+ [
+ [:get, ""],
+ [:get, "/new"],
+ [:get, "/#{decision_model.id}/edit"],
+ [:post, ""],
+ [:put, "/#{decision_model.id}"],
+ [:delete, "/#{decision_model.id}"],
+ [:post, "/test"],
+ ].each do |method, path|
+ public_send(method, "#{base_path}#{path}.json", params: { ai_decision_model: valid_attrs })
+ expect(response.status).to eq(404)
+ end
+ expect(decision_model.reload.display_name).to eq("TypeSafe Jev")
+ expect(DecisionModel.count).to eq(1)
+ expect(AiApiAuditLog.count).to eq(0)
+ end
+ end
+
+ describe "#create" do
+ it "creates a decision model and logs the staff action" do
+ post "/admin/plugins/discourse-ai/ai-decision-models.json",
+ params: {
+ ai_decision_model: valid_attrs,
+ }
+
+ expect(response.status).to eq(201)
+ expect(DecisionModel.last).to have_attributes(valid_attrs)
+ 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(
+ UserHistory.where(
+ action: UserHistory.actions[:custom_staff],
+ custom_type: "create_ai_decision_model",
+ ).count,
+ ).to eq(1)
+ end
+
+ it "returns an error for an unknown provider" do
+ post "/admin/plugins/discourse-ai/ai-decision-models.json",
+ params: {
+ ai_decision_model: valid_attrs.merge(provider: "nope"),
+ }
+
+ expect(response.status).to eq(422)
+ expect(DecisionModel.count).to eq(0)
+ end
+ end
+
+ describe "#update" do
+ fab!(:decision_model)
+
+ it "updates the decision model and logs the staff action" do
+ put "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}.json",
+ params: {
+ ai_decision_model: {
+ display_name: "Jev (prod)",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(decision_model.reload.display_name).to eq("Jev (prod)")
+ expect(UserHistory.find_by!(custom_type: "update_ai_decision_model").details).to include(
+ "TypeSafe Jev → Jev (prod)",
+ )
+ expect(
+ UserHistory.where(
+ action: UserHistory.actions[:custom_staff],
+ custom_type: "update_ai_decision_model",
+ ).count,
+ ).to eq(1)
+ put "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}.json",
+ params: {
+ ai_decision_model: {
+ provider: "unknown",
+ },
+ }
+ expect(response.status).to eq(422)
+ expect(decision_model.reload.provider).to eq("system_one")
+ end
+ end
+
+ describe "#destroy" do
+ fab!(:decision_model)
+
+ it "deletes the decision model and logs the staff action" do
+ delete "/admin/plugins/discourse-ai/ai-decision-models/#{decision_model.id}.json"
+
+ expect(response.status).to eq(204)
+ expect(DecisionModel.exists?(decision_model.id)).to eq(false)
+ log = UserHistory.find_by!(custom_type: "delete_ai_decision_model")
+ expect(log.details).to include(
+ decision_model.url,
+ decision_model.name,
+ decision_model.ai_secret_id.to_s,
+ )
+ expect(log.details).not_to include(decision_model.api_key)
+ expect(
+ UserHistory.where(
+ action: UserHistory.actions[:custom_staff],
+ custom_type: "delete_ai_decision_model",
+ ).count,
+ ).to eq(1)
+ end
+ end
+
+ describe "#test" do
+ it "asks the model a test question without saving it" do
+ DiscourseAi::Decisions.with_canned_answers(greeting: 0.98) do |canned|
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: valid_attrs.merge(display_name: ""),
+ }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body).to eq("success" => true, "probability" => 0.98)
+ expect(canned.calls.first).to include(
+ state: "Hello! How are you doing today?",
+ user: admin,
+ feature_name: "decision_model_test",
+ )
+ end
+ expect(DecisionModel.count).to eq(0)
+ expect(AiApiAuditLog.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",
+ params: {
+ ai_decision_model: valid_attrs,
+ }
+ expect(response.parsed_body).to eq(
+ "success" => false,
+ "error" => I18n.t("discourse_ai.decision_models.test_refused"),
+ )
+ end
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: valid_attrs.merge(url: "ftp://example.com"),
+ }
+ expect(response.parsed_body).to include("success" => false, "error" => include("URL"))
+ end
+
+ it "checks that vision-enabled models read a test image" do
+ attrs = valid_attrs.merge(vision_enabled: true)
+
+ DiscourseAi::Decisions.with_canned_answers(greeting: 0.98, red_image: 0.97) do |canned|
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: attrs,
+ }
+ expect(response.parsed_body).to eq("success" => true, "probability" => 0.98)
+ expect(canned.calls.first[:images]).to eq([described_class::TEST_IMAGE])
+ end
+
+ DiscourseAi::Decisions.with_canned_answers(greeting: 0.98, red_image: 0.02) do
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: attrs,
+ }
+ expect(response.parsed_body).to eq(
+ "success" => false,
+ "error" => I18n.t("discourse_ai.decision_models.test_image_not_read"),
+ )
+ end
+ end
+
+ it "rate limits staff after six tests" do
+ RateLimiter.enable
+ DiscourseAi::Decisions.with_canned_answers(greeting: true) do
+ 6.times do
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: valid_attrs,
+ }
+ expect(response.status).to eq(200)
+ end
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: valid_attrs,
+ }
+ expect(response.status).to eq(429)
+ end
+ end
+
+ it "reports provider errors for text and image requests" do
+ [[401, false], [400, true]].each do |status, vision_enabled|
+ stub_request(:post, valid_attrs[:url]).to_return(
+ status:,
+ body: { detail: { error_type: "api_usage_error", message: "Invalid request." } }.to_json,
+ )
+
+ post "/admin/plugins/discourse-ai/ai-decision-models/test.json",
+ params: {
+ ai_decision_model: valid_attrs.merge(vision_enabled:),
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body).to eq(
+ "success" => false,
+ "error" => "Request failed with status #{status}: Invalid request.",
+ )
+ end
+ 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
new file mode 100644
index 00000000000..3aeb9f02dcc
--- /dev/null
+++ b/plugins/discourse-ai/spec/system/admin_ai_decision_models_spec.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+RSpec.describe "Managing decision models" do
+ fab!(:admin)
+
+ let(:form) { PageObjects::Components::FormKit.new(".ai-decision-model-editor") }
+
+ before do
+ enable_current_plugin
+ sign_in(admin)
+ 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"
+ find(".ai-decision-models-list-editor__empty-new-btn").click
+ form.field("preset").select("vllm-sr-d3")
+ form.field("url").fill_in("https://d3.example.com/v1/systemone")
+ form.submit
+
+ expect(page).to have_current_path(
+ %r{\A/admin/plugins/discourse-ai/ai-decision-models/\d+/edit\z},
+ )
+ page.refresh
+ expect(form.field("display_name")).to have_value("d3")
+ expect(form.field("name")).to have_value("vllm-sr/d3")
+ expect(form.field("provider")).to have_value("system_one")
+ expect(form.field("url")).to have_value("https://d3.example.com/v1/systemone")
+ expect(form.field("vision_enabled")).to be_checked
+ find(".ai-decision-model-editor__test-btn").click
+ expect(page).to have_css(
+ ".ai-decision-model-editor__test-result.alert-success",
+ text: "yes: 97%",
+ )
+ find(".back-button").click
+ expect(page).to have_css(".ai-decision-models-list-editor__row", text: "d3")
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/combo-box.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/combo-box.gjs
index 9b21023a484..c612bf3edeb 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/combo-box.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/combo-box.gjs
@@ -306,8 +306,9 @@ export default class ComboBoxField extends Component {
<DButton
class="btn-default"
@action={{this.performAction}}
+ @ariaLabel={{this.actionLabel}}
@icon={{this.actionIcon}}
- @label={{this.actionLabel}}
+ @title={{this.actionLabel}}
/>
</div>
{{else}}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/node-output-schemas.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/node-output-schemas.js
index b8d1c4539d6..cd6a85f8e70 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/node-output-schemas.js
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/node-output-schemas.js
@@ -1,8 +1,9 @@
+import classify from "./output-schemas/classify";
import summarize from "./output-schemas/summarize";
// Nodes whose output keys come from what the author typed cannot declare them as a contract, so
// they compute them in Ruby and name an editor-side counterpart through `output_schema_resolver`.
-const RESOLVERS = { summarize };
+const RESOLVERS = { classify, summarize };
export function outputSchemasFromResolver(resolver, configuration = {}) {
if (!resolver) {
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/output-schemas/classify.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/output-schemas/classify.js
new file mode 100644
index 00000000000..ba48d5e7536
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/output-schemas/classify.js
@@ -0,0 +1,50 @@
+import { fixedCollectionRows } from "../property-engine";
+
+function answerSchema(type) {
+ const properties = {
+ type: { type: "string" },
+ reason: { type: ["string", "null"] },
+ };
+
+ if (type === "yes_no") {
+ properties.probability = { type: "number" };
+ } else if (type === "choice" || type === "score") {
+ properties.probabilities = { type: "object" };
+ properties.confidence = { type: ["number", "null"] };
+ if (type === "choice") {
+ properties.choice = { type: "string" };
+ } else {
+ properties.score = { type: "number" };
+ properties.level = { type: "string" };
+ }
+ }
+
+ return { type: "object", properties };
+}
+
+export default function classifyOutputSchemas(configuration = {}) {
+ const answers = new Map();
+ for (const row of fixedCollectionRows(configuration.questions)) {
+ if (!row || typeof row !== "object" || Array.isArray(row)) {
+ continue;
+ }
+
+ const name = String(row.name ?? "").trim();
+ if (name) {
+ answers.set(name, answerSchema(row.type));
+ }
+ }
+
+ return [
+ {
+ $schema: "https://json-schema.org/draft/2020-12/schema",
+ type: "object",
+ properties: {
+ classification: {
+ type: "object",
+ properties: Object.fromEntries(answers),
+ },
+ },
+ },
+ ];
+}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/schema-graph.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/schema-graph.js
index 44faaf2e797..d470a315c81 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/schema-graph.js
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/schema-graph.js
@@ -229,7 +229,12 @@ function ownOutputResolution(node, graph, configuration) {
);
if (resolved) {
return {
- contracts: resolved.map((schema) => [{ mode: "replace", schema }]),
+ contracts: resolved.map((schema, index) => [
+ {
+ mode: definition.output_contracts?.[index]?.mode || "replace",
+ schema,
+ },
+ ]),
extensions: resolved.map(() => []),
};
}
diff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
index 153c978867c..2090f108def 100644
--- a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs
@@ -1778,7 +1778,8 @@ module("Integration | Component | workflows property engine", function (hooks) {
assert
.dom(".workflows-property-engine__select-with-action > .btn")
- .hasText("Open workflow");
+ .hasAttribute("title", "Open workflow")
+ .hasAria("label", "Open workflow");
await click(".workflows-property-engine__select-with-action > .btn");
diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-schema-graph-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-schema-graph-test.js
index 45cce276233..0d8f1e901a6 100644
--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-schema-graph-test.js
+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-schema-graph-test.js
@@ -417,6 +417,80 @@ module("Unit | lib | discourse-workflows | schema-graph", function () {
);
});
+ test("classification answers resolve their types while preserving input fields", function (assert) {
+ const classify = node("classify", "action:classify");
+ const graph = {
+ nodes: [node("source", "trigger:source"), classify],
+ connections: [conn("source", "classify")],
+ nodeTypes: [
+ triggerType("trigger:source", prop("post", "object")),
+ {
+ name: "action:classify",
+ versions: {
+ "1.0": {
+ output_schema_resolver: "classify",
+ output_contracts: [{ mode: "merge" }],
+ },
+ },
+ },
+ ],
+ };
+ const common = {
+ type: { type: "string" },
+ reason: { type: ["string", "null"] },
+ };
+ const distribution = {
+ probabilities: { type: "object" },
+ confidence: { type: ["number", "null"] },
+ };
+ const configuration = {
+ questions: {
+ values: [
+ { name: " urgent ", type: "yes_no" },
+ { name: "team", type: "choice" },
+ { name: "severity", type: "score" },
+ { name: "__proto__", type: "yes_no" },
+ { name: "draft", type: "" },
+ { name: " " },
+ null,
+ "invalid",
+ ],
+ },
+ };
+
+ assert.deepEqual(
+ outputSchemaForNode(classify, graph, { configuration }).properties,
+ {
+ post: { type: "object" },
+ classification: objectSchema({
+ urgent: objectSchema({ ...common, probability: { type: "number" } }),
+ team: objectSchema({
+ ...common,
+ ...distribution,
+ choice: { type: "string" },
+ }),
+ severity: objectSchema({
+ ...common,
+ ...distribution,
+ score: { type: "number" },
+ level: { type: "string" },
+ }),
+ ["__proto__"]: objectSchema({
+ ...common,
+ probability: { type: "number" },
+ }),
+ draft: objectSchema(common),
+ }),
+ },
+ "downstream expressions can select each answer and the original input"
+ );
+ assert.deepEqual(
+ outputSchemaForNode(classify, graph, { configuration: {} }).properties,
+ { post: { type: "object" }, classification: objectSchema({}) },
+ "an unconfigured classifier still preserves the input"
+ );
+ });
+
test("joins, union contracts and cycles resolve across the graph", function (assert) {
const str = prop("v", "string");
const num = prop("v", "integer");