Skip to content

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

FEATURE: better summarize primitives for workflows (#42297)

Joffrey JAFFEUXdfe770d0403f
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index cc19b706eab..f565644ef3d 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -245,6 +245,10 @@ en:
llm_model_id: "LLM model"
llm_model_default: "Use agent default"
llm_model_default_with_name: "Use agent default (%{value})"
+ mode: "Mode"
+ mode_description: "Choose whether the agent runs once per input item or once for all of them. Each run is a separate request to the LLM."
+ mode_run_once_for_each_item: "Run once for each item"
+ mode_run_once_for_all_items: "Run once for all items"
prompt: "Prompt"
runner_username: "Run as"
runner_username_tooltip: "User whose permissions are used for upload visibility and AI agent tools. Use system for workflow-owned automation."
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 66d11b59ea3..8d82fa886b3 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -292,6 +292,7 @@ en:
discourse_workflows:
ai_agent:
errors:
+ invalid_mode: "'%{mode}' is not a supported AI agent execution mode."
locked_default_llm_missing: "Agent '%{agent}' is locked to its default LLM, but no valid default LLM is configured"
llm_not_found: "LLM model with id %{llm_model_id} not found"
no_llm_configured: "No LLM is configured for agent '%{agent}'. Select an LLM model or configure an agent/site default LLM."
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 f6c5231c984..90390446ea9 100644
--- a/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
+++ b/plugins/discourse-ai/discourse_workflows/nodes/ai_agent/v1.rb
@@ -5,6 +5,9 @@ if defined?(DiscourseWorkflows)
module Nodes
module AiAgent
class V1 < DiscourseWorkflows::NodeType
+ RUN_ONCE_FOR_ALL_ITEMS = "runOnceForAllItems"
+ RUN_ONCE_FOR_EACH_ITEM = "runOnceForEachItem"
+
description(
name: "action:ai_agent",
version: "1.0",
@@ -17,7 +20,13 @@ if defined?(DiscourseWorkflows)
unavailable_reason_key: "discourse_workflows.node_unavailable.requires_ai",
i18n_prefix: "discourse_ai.discourse_workflows",
capabilities: {
- run_scope: "per_item",
+ run_scope: {
+ parameter: "mode",
+ values: {
+ RUN_ONCE_FOR_EACH_ITEM => "per_item",
+ RUN_ONCE_FOR_ALL_ITEMS => "all_items",
+ },
+ },
},
output_contracts: [
{
@@ -118,6 +127,13 @@ if defined?(DiscourseWorkflows)
control: :actor,
},
},
+ mode: {
+ type: :options,
+ required: true,
+ options: [RUN_ONCE_FOR_EACH_ITEM, RUN_ONCE_FOR_ALL_ITEMS],
+ default: RUN_ONCE_FOR_EACH_ITEM,
+ no_data_expression: true,
+ },
prompt: {
type: :string,
ui: {
@@ -190,17 +206,19 @@ if defined?(DiscourseWorkflows)
end
def execute(exec_ctx)
+ mode = exec_ctx.get_node_parameter("mode", 0, default: RUN_ONCE_FOR_EACH_ITEM)
+ validate_mode!(mode)
+
+ return [[run_once_for_all_items(exec_ctx)]] if mode == RUN_ONCE_FOR_ALL_ITEMS
+
items =
exec_ctx.input_items.map.with_index do |item, item_index|
- config = {
- "agent_id" => exec_ctx.get_node_parameter("agent_id", item_index),
- "llm_model_id" => exec_ctx.get_node_parameter("llm_model_id", item_index),
- "prompt" => exec_ctx.get_node_parameter("prompt", item_index),
- "upload_ids" => exec_ctx.get_node_parameter("upload_ids", item_index),
- }
- runner =
- exec_ctx.actor_from_parameter("runner_username", item_index, default: "system")
- result = run_agent(config, exec_ctx.log, runner)
+ result =
+ run_agent(
+ agent_config(exec_ctx, item_index),
+ exec_ctx.log,
+ runner(exec_ctx, item_index),
+ )
wrap({ "result" => result }, paired_item: exec_ctx.paired_item_for(item))
end
@@ -210,6 +228,36 @@ if defined?(DiscourseWorkflows)
private
+ def run_once_for_all_items(exec_ctx)
+ result = run_agent(agent_config(exec_ctx, 0), exec_ctx.log, runner(exec_ctx, 0))
+
+ wrap(
+ { "result" => result },
+ paired_item: exec_ctx.input_items.map { |item| exec_ctx.paired_item_for(item) },
+ )
+ end
+
+ def agent_config(exec_ctx, item_index)
+ {
+ "agent_id" => exec_ctx.get_node_parameter("agent_id", item_index),
+ "llm_model_id" => exec_ctx.get_node_parameter("llm_model_id", item_index),
+ "prompt" => exec_ctx.get_node_parameter("prompt", item_index),
+ "upload_ids" => exec_ctx.get_node_parameter("upload_ids", item_index),
+ }
+ end
+
+ def runner(exec_ctx, item_index)
+ exec_ctx.actor_from_parameter("runner_username", item_index, default: "system")
+ end
+
+ def validate_mode!(mode)
+ return if [RUN_ONCE_FOR_ALL_ITEMS, RUN_ONCE_FOR_EACH_ITEM].include?(mode)
+
+ raise_node_error!(
+ I18n.t("discourse_ai.discourse_workflows.ai_agent.errors.invalid_mode", mode: mode),
+ )
+ end
+
def resolve_llm_model(agent_record, requested_llm_model_id)
if agent_record.force_default_llm?
llm_model =
diff --git a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
index d5ee361e8c7..3d68e43295d 100644
--- a/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
+++ b/plugins/discourse-ai/spec/lib/discourse_workflows/nodes/ai_agent_spec.rb
@@ -124,6 +124,81 @@ RSpec.describe DiscourseWorkflows::Nodes::AiAgent::V1 do
expect(bot).to have_received(:reply).twice
end
+ describe "run once for all items" do
+ it "makes a single request no matter how many items arrive" do
+ items =
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "mode" => "runOnceForAllItems",
+ "prompt" => "={{ 'Report about ' + $json.name }}",
+ },
+ input_items: [
+ { "json" => { "name" => "Ada" } },
+ { "json" => { "name" => "Grace" } },
+ { "json" => { "name" => "Alan" } },
+ ],
+ ).first
+
+ expect(prompts).to eq(["Report about Ada"])
+ expect(items.length).to eq(1)
+ expect(items.first["json"]["result"]).to eq("Reply to Report about Ada")
+ expect(bot).to have_received(:reply).once
+ end
+
+ it "pairs its single output with every input item" do
+ items =
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "mode" => "runOnceForAllItems",
+ "prompt" => "Hello",
+ },
+ input_items: [{ "json" => { "name" => "Ada" } }, { "json" => { "name" => "Grace" } }],
+ ).first
+
+ expect(items.first["pairedItem"]).to eq([{ "item" => 0 }, { "item" => 1 }])
+ end
+
+ it "defaults to per-item so existing workflows keep their behaviour" do
+ expect(described_class.property_schema[:mode][:default]).to eq("runOnceForEachItem")
+
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "prompt" => "Hello",
+ },
+ input_items: [{ "json" => {} }, { "json" => {} }],
+ )
+
+ expect(bot).to have_received(:reply).twice
+ end
+
+ it "reports the run scope through the mode parameter" do
+ expect(described_class.description[:capabilities][:run_scope]).to eq(
+ {
+ parameter: "mode",
+ values: {
+ runOnceForEachItem: "per_item",
+ runOnceForAllItems: "all_items",
+ },
+ },
+ )
+ end
+
+ it "raises a node error for an unknown mode" do
+ expect {
+ execute_node_output(
+ configuration: {
+ "agent_id" => agent.id,
+ "mode" => "whenever",
+ "prompt" => "Hello",
+ },
+ )
+ }.to raise_error(DiscourseWorkflows::NodeError, /not a supported AI agent execution mode/)
+ end
+ end
+
it "uses the system user as the default runner" do
execute_node_output(configuration: { "agent_id" => agent.id, "prompt" => "Hello" })
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field-path-control.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field-path-control.gjs
new file mode 100644
index 00000000000..535bbf67ef4
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field-path-control.gjs
@@ -0,0 +1,80 @@
+import Component from "@glimmer/component";
+import { hash } from "@ember/helper";
+import { action } from "@ember/object";
+import { makeArray } from "discourse/lib/helpers";
+import ComboBox from "discourse/select-kit/components/combo-box";
+import MultiSelect from "discourse/select-kit/components/multi-select";
+import { inputFieldPathsForNode } from "../../../lib/workflows/input-fields";
+
+export default class FieldPathControl extends Component {
+ get multiple() {
+ return Boolean(this.args.schema?.ui?.multiple);
+ }
+
+ get selected() {
+ return String(this.args.field?.value ?? "").trim();
+ }
+
+ get selectedPaths() {
+ return String(this.args.field?.value ?? "")
+ .split(",")
+ .map((path) => path.trim())
+ .filter(Boolean);
+ }
+
+ get availablePaths() {
+ return inputFieldPathsForNode(this.args.node, {
+ nodes: this.args.nodes,
+ connections: this.args.connections,
+ nodeTypes: this.args.nodeTypes,
+ session: this.args.session,
+ });
+ }
+
+ get content() {
+ const paths = this.availablePaths.map((entry) => entry.path);
+
+ const missing = (
+ this.multiple ? this.selectedPaths : [this.selected]
+ ).filter((path) => path && !paths.includes(path));
+
+ return [...paths, ...missing].map((path) => ({ id: path, name: path }));
+ }
+
+ get noneLabel() {
+ return this.args.schema?.control_options?.none_label_i18n_key;
+ }
+
+ @action
+ handleChange(value) {
+ if (this.multiple) {
+ this.args.field.set(makeArray(value).join(", "));
+ } else {
+ this.args.field.set(value ?? "");
+ }
+ }
+
+ <template>
+ {{#if this.multiple}}
+ <MultiSelect
+ class="workflows-field-path"
+ @content={{this.content}}
+ @value={{this.selectedPaths}}
+ @nameProperty="name"
+ @valueProperty="id"
+ @onChange={{this.handleChange}}
+ @options={{hash allowAny=true filterable=true none=this.noneLabel}}
+ />
+ {{else}}
+ <ComboBox
+ class="workflows-field-path"
+ @content={{this.content}}
+ @value={{this.selected}}
+ @nameProperty="name"
+ @valueProperty="id"
+ @onChange={{this.handleChange}}
+ @options={{hash allowAny=true filterable=true clearable=true}}
+ />
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
index e0623d8693f..c19986a20dd 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
@@ -218,6 +218,8 @@ export default class Field extends Component {
@nodeDefinition={{this.nodeDefinition}}
@nodeParameters={{@nodeParameters}}
@nodes={{@nodes}}
+ @nodeTypes={{@nodeTypes}}
+ @connections={{@connections}}
@formApi={{@formApi}}
@session={{@session}}
@supportsExpression={{this.supportsExpression}}
@@ -238,6 +240,8 @@ export default class Field extends Component {
@nodeDefinition={{this.nodeDefinition}}
@nodeParameters={{@nodeParameters}}
@nodes={{@nodes}}
+ @nodeTypes={{@nodeTypes}}
+ @connections={{@connections}}
@formApi={{@formApi}}
@session={{@session}}
@supportsExpression={{this.supportsExpression}}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/fixed-collection.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/fixed-collection.gjs
index b42c486e259..30341f95575 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/fixed-collection.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/fixed-collection.gjs
@@ -84,6 +84,10 @@ export default class FixedCollection extends Component {
}
get label() {
+ if (this.args.showLabel === false) {
+ return null;
+ }
+
return (
this.args.label || propertyLabel(this.nodeDefinition, this.args.fieldName)
);
@@ -113,6 +117,10 @@ export default class FixedCollection extends Component {
}
get maxItemsReachedTitle() {
+ if (this.maxItems === null) {
+ return null;
+ }
+
return i18n("discourse_workflows.property_engine.max_items_reached", {
count: this.maxItems,
});
@@ -229,6 +237,17 @@ export default class FixedCollection extends Component {
return propertyLabel(this.nodeDefinition, fieldName);
}
+ @action
+ removeItemLabel(item) {
+ if (!item?.name) {
+ return i18n("discourse_workflows.property_engine.remove_item");
+ }
+
+ return i18n("discourse_workflows.property_engine.remove_assignment", {
+ name: item.name,
+ });
+ }
+
@action
removeItem(group, removeFn, index) {
this.args.onRemove?.(index);
@@ -336,14 +355,8 @@ export default class FixedCollection extends Component {
@action={{fn this.removeItem group collection.remove index}}
@icon="xmark"
class="workflows-property-engine__collection-delete"
- @translatedAriaLabel={{i18n
- "discourse_workflows.property_engine.remove_assignment"
- name=item.name
- }}
- @translatedTitle={{i18n
- "discourse_workflows.property_engine.remove_assignment"
- name=item.name
- }}
+ @translatedAriaLabel={{this.removeItemLabel item}}
+ @translatedTitle={{this.removeItemLabel item}}
/>
<collection.Object
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/summarize-aggregations.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/summarize-aggregations.gjs
new file mode 100644
index 00000000000..55632400eb9
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/summarize-aggregations.gjs
@@ -0,0 +1,200 @@
+import Component from "@glimmer/component";
+import { i18n } from "discourse-i18n";
+import {
+ fieldVisible,
+ normalizeOptions,
+ normalizeSchema,
+ propertyLabel,
+ propertyOptionLabel,
+} from "../../../lib/workflows/property-engine";
+import {
+ summarizeOutputKey,
+ summarizeOutputKeyIsDerived,
+} from "../../../lib/workflows/summarize-output-key";
+import FieldPathControl from "./field-path-control";
+import FixedCollection from "./fixed-collection";
+
+function isVisible(field, item) {
+ return field ? fieldVisible(field, item) : false;
+}
+
+function isDerived(item) {
+ return summarizeOutputKeyIsDerived(item || {});
+}
+
+function derivedPlaceholder(item) {
+ return summarizeOutputKeyIsDerived(item || {})
+ ? summarizeOutputKey(item || {})
+ : "";
+}
+
+export default class SummarizeAggregations extends Component {
+ fieldSchema = (name) => {
+ return this.rowFields.find((field) => field.name === name);
+ };
+
+ fieldTitle = (name) => {
+ return propertyLabel(this.args.nodeDefinition, name);
+ };
+
+ get rowFields() {
+ const group = (this.args.schema?.options || [])[0] || { values: {} };
+ return normalizeSchema(group.values || {});
+ }
+
+ get aggregationOptions() {
+ const schema = this.fieldSchema("aggregation");
+
+ return normalizeOptions(schema?.options || []).map((option) => ({
+ ...option,
+ label: propertyOptionLabel(
+ this.args.nodeDefinition,
+ "aggregation",
+ option
+ ),
+ }));
+ }
+
+ get separatorOptions() {
+ const schema = this.fieldSchema("separate_by");
+
+ return normalizeOptions(schema?.options || []).map((option) => ({
+ ...option,
+ label: propertyOptionLabel(
+ this.args.nodeDefinition,
+ "separate_by",
+ option
+ ),
+ }));
+ }
+
+ <template>
+ <FixedCollection
+ @form={{@form}}
+ @formApi={{@formApi}}
+ @configuration={{@configuration}}
+ @connections={{@connections}}
+ @credentials={{@credentials}}
+ @fieldName={{@fieldName}}
+ @showLabel={{false}}
+ @node={{@node}}
+ @nodeDefinition={{@nodeDefinition}}
+ @nodeParameters={{@nodeParameters}}
+ @nodes={{@nodes}}
+ @nodeType={{@nodeType}}
+ @nodeTypes={{@nodeTypes}}
+ @schema={{@schema}}
+ @session={{@session}}
+ as |row|
+ >
+ <div class="workflows-summarize-row">
+ <div class="workflows-summarize-row__line">
+ <row.object.Field
+ @name="aggregation"
+ @title={{this.fieldTitle "aggregation"}}
+ @showTitle={{false}}
+ class="workflows-summarize-row__lead"
+ as |field|
+ >
+ <field.Select @includeNone={{false}} as |select|>
+ {{#each this.aggregationOptions key="value" as |choice|}}
+ <select.Option @value={{choice.value}}>
+ {{choice.label}}
+ </select.Option>
+ {{/each}}
+ </field.Select>
+ </row.object.Field>
+
+ {{#if (isVisible (this.fieldSchema "field") row.item)}}
+ <row.object.Field
+ @name="field"
+ @title={{this.fieldTitle "field"}}
+ @showTitle={{false}}
+ class="workflows-summarize-row__control"
+ as |field|
+ >
+ <FieldPathControl
+ @field={{field}}
+ @schema={{this.fieldSchema "field"}}
+ @node={{@node}}
+ @nodes={{@nodes}}
+ @nodeTypes={{@nodeTypes}}
+ @connections={{@connections}}
+ @session={{@session}}
+ />
+ </row.object.Field>
+ {{/if}}
+ </div>
+
+ {{#if (isVisible (this.fieldSchema "separate_by") row.item)}}
+ <div class="workflows-summarize-row__line">
+ <span
+ class="workflows-summarize-row__lead workflows-summarize-row__lead--label"
+ >
+ {{this.fieldTitle "separate_by"}}
+ </span>
+
+ <row.object.Field
+ @name="separate_by"
+ @title={{this.fieldTitle "separate_by"}}
+ @showTitle={{false}}
+ class="workflows-summarize-row__control"
+ as |field|
+ >
+ <field.Select @includeNone={{false}} as |select|>
+ {{#each this.separatorOptions key="value" as |choice|}}
+ <select.Option @value={{choice.value}}>
+ {{choice.label}}
+ </select.Option>
+ {{/each}}
+ </field.Select>
+ </row.object.Field>
+ </div>
+ {{/if}}
+
+ {{#if (isVisible (this.fieldSchema "custom_separator") row.item)}}
+ <div class="workflows-summarize-row__line">
+ <span
+ class="workflows-summarize-row__lead workflows-summarize-row__lead--label"
+ >
+ {{this.fieldTitle "custom_separator"}}
+ </span>
+
+ <row.object.Field
+ @name="custom_separator"
+ @title={{this.fieldTitle "custom_separator"}}
+ @showTitle={{false}}
+ class="workflows-summarize-row__control"
+ as |field|
+ >
+ <field.Input />
+ </row.object.Field>
+ </div>
+ {{/if}}
+
+ <div class="workflows-summarize-row__line">
+ <span
+ class="workflows-summarize-row__lead workflows-summarize-row__lead--label"
+ >
+ {{i18n "discourse_workflows.summarize.as"}}
+ </span>
+
+ <row.object.Field
+ @name="output_field_name"
+ @title={{this.fieldTitle "output_field_name"}}
+ @showTitle={{false}}
+ @placeholder={{derivedPlaceholder row.item}}
+ class="workflows-summarize-row__control
+ {{if
+ (isDerived row.item)
+ 'workflows-summarize-row__control--derived'
+ }}"
+ as |field|
+ >
+ <field.Input />
+ </row.object.Field>
+ </div>
+ </div>
+ </FixedCollection>
+ </template>
+}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/node/configurator.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/node/configurator.gjs
index 3aa0d26f434..929fdccf640 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/node/configurator.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/node/configurator.gjs
@@ -40,6 +40,7 @@ import CredentialControl from "../configurators/credential";
import PropertyEngineConfigurator from "../configurators/property-engine";
import InputContext from "../context/input";
import OutputContext from "../context/output";
+import LivePreview from "./live-preview";
function credentialSlotLabel(slot) {
return i18n(slot.label_key || "discourse_workflows.credentials.type");
@@ -744,6 +745,12 @@ export default class NodeConfigurator extends Component {
{{#if this.showsOutputContext}}
<div class="workflows-configurator-modal__column --right">
+ <LivePreview
+ @node={{@model.node}}
+ @nodeTypes={{this.nodeTypes}}
+ @session={{@model.session}}
+ @configuration={{this.configuration}}
+ />
<OutputContext
@node={{@model.node}}
@nodes={{@model.nodes}}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/node/live-preview.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/node/live-preview.gjs
new file mode 100644
index 00000000000..1686928dcea
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/node/live-preview.gjs
@@ -0,0 +1,199 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { action } from "@ember/object";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
+import didUpdate from "@ember/render-modifiers/modifiers/did-update";
+import { ajax } from "discourse/lib/ajax";
+import discourseDebounce from "discourse/lib/debounce";
+import { i18n } from "discourse-i18n";
+import { findNodeType } from "../../../lib/workflows/property-engine";
+
+const DEBOUNCE_MS = 400;
+const MAX_CELL_LENGTH = 80;
+
+function cellValue(value) {
+ if (value === null || value === undefined) {
+ return "";
+ }
+
+ if (Array.isArray(value)) {
+ const scalars = value.every((entry) => typeof entry !== "object");
+ if (scalars) {
+ return value.join(", ").slice(0, MAX_CELL_LENGTH);
+ }
+ return i18n("discourse_workflows.node_preview.nested_items", {
+ count: value.length,
+ });
+ }
+
+ if (typeof value === "object") {
+ return JSON.stringify(value).slice(0, MAX_CELL_LENGTH);
+ }
+
+ return String(value).slice(0, MAX_CELL_LENGTH);
+}
+
+export default class LivePreview extends Component {
+ @tracked loading = false;
+ @tracked result = null;
+ @tracked error = null;
+
+ get nodeDefinition() {
+ return findNodeType(this.args.nodeTypes, this.args.node?.type);
+ }
+
+ get previewable() {
+ return this.nodeDefinition?.previewable === true;
+ }
+
+ get items() {
+ return this.result?.outputs?.[0] ?? [];
+ }
+
+ get columns() {
+ const keys = [];
+ for (const item of this.items) {
+ for (const key of Object.keys(item?.json ?? {})) {
+ if (!keys.includes(key)) {
+ keys.push(key);
+ }
+ }
+ }
+ return keys;
+ }
+
+ get rows() {
+ return this.items.map((item) =>
+ this.columns.map((key) => cellValue(item?.json?.[key]))
+ );
+ }
+
+ get countLabel() {
+ return i18n("discourse_workflows.node_preview.counts", {
+ input: i18n("discourse_workflows.node_preview.items", {
+ count: this.result?.input_count ?? 0,
+ }),
+ output: i18n("discourse_workflows.node_preview.items", {
+ count: this.items.length,
+ }),
+ });
+ }
+
+ get emptyReason() {
+ if (this.error) {
+ return this.error;
+ }
+ if (!this.result) {
+ return null;
+ }
+ if (!this.result.input_count) {
+ return i18n("discourse_workflows.node_preview.pending");
+ }
+ if (!this.items.length) {
+ return i18n("discourse_workflows.node_preview.no_items");
+ }
+ return null;
+ }
+
+ @action
+ schedulePreview() {
+ if (!this.previewable) {
+ return;
+ }
+
+ discourseDebounce(this, this.loadPreview, DEBOUNCE_MS);
+ }
+
+ @action
+ async loadPreview() {
+ if (this.isDestroying || this.isDestroyed) {
+ return;
+ }
+
+ const workflowId = this.args.session?.workflowId;
+ const nodeId = this.args.node?.clientId;
+
+ if (!workflowId || !nodeId) {
+ return;
+ }
+
+ this.loading = true;
+
+ try {
+ this.result = await ajax(
+ "/admin/plugins/discourse-workflows/node-previews.json",
+ {
+ type: "POST",
+ contentType: "application/json",
+ data: JSON.stringify({
+ workflow_id: workflowId,
+ node_id: nodeId,
+ parameters: this.args.configuration || {},
+ }),
+ }
+ );
+ this.error = this.result?.error || null;
+ } catch {
+ this.result = null;
+ this.error = i18n("discourse_workflows.node_preview.failed");
+ } finally {
+ this.loading = false;
+ }
+ }
+
+ <template>
+ {{#if this.previewable}}
+ <div
+ class="workflows-node-preview workflows-context-panel__section"
+ {{didInsert this.schedulePreview}}
+ {{didUpdate this.schedulePreview @configuration}}
+ >
+ <div class="workflows-context-panel__header">
+ <h3 class="workflows-context-panel__title">
+ {{i18n "discourse_workflows.node_preview.title"}}
+ {{#if this.result}}
+ <span class="workflows-context-panel__title-meta">
+ {{this.countLabel}}
+ </span>
+ {{/if}}
+ </h3>
+ </div>
+
+ {{#if this.emptyReason}}
+ <p class="workflows-context-panel__empty">{{this.emptyReason}}</p>
+ {{else if this.columns.length}}
+ <div class="workflows-node-preview__table-wrapper">
+ <table class="workflows-node-preview__table">
+ <thead>
+ <tr>
+ {{#each this.columns as |column|}}
+ <th>{{column}}</th>
+ {{/each}}
+ </tr>
+ </thead>
+ <tbody>
+ {{#each this.rows as |row|}}
+ <tr>
+ {{#each row as |cell|}}
+ <td>{{cell}}</td>
+ {{/each}}
+ </tr>
+ {{/each}}
+ </tbody>
+ </table>
+ </div>
+
+ {{#if this.result.truncated}}
+ <p class="workflows-node-preview__truncated">
+ {{i18n "discourse_workflows.node_preview.truncated"}}
+ </p>
+ {{/if}}
+ {{else}}
+ <p class="workflows-context-panel__empty">
+ {{i18n "discourse_workflows.node_preview.pending"}}
+ </p>
+ {{/if}}
+ </div>
+ {{/if}}
+ </template>
+}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js
index 0827fe7069d..783e182e63e 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js
@@ -10,6 +10,7 @@ import DataTableColumns from "../../components/workflows/configurators/data-tabl
import DataTableConditionBuilder from "../../components/workflows/configurators/data-table-condition-builder";
import DataTableSelect from "../../components/workflows/configurators/data-table-select";
import DefaultInputControl from "../../components/workflows/configurators/default-input-control";
+import FieldPathControl from "../../components/workflows/configurators/field-path-control";
import FilterQuery from "../../components/workflows/configurators/filter-query";
import GroupSelect from "../../components/workflows/configurators/group-select";
import IconControl from "../../components/workflows/configurators/icon-control";
@@ -18,6 +19,7 @@ import MultiInput from "../../components/workflows/configurators/multi-input";
import NoticeControl from "../../components/workflows/configurators/notice-control";
import QueryParams from "../../components/workflows/configurators/query-params";
import SelectControl from "../../components/workflows/configurators/select-control";
+import SummarizeAggregations from "../../components/workflows/configurators/summarize-aggregations";
import TagsControl from "../../components/workflows/configurators/tags-control";
import TimezoneControl from "../../components/workflows/configurators/timezone-control";
import UrlPreview from "../../components/workflows/configurators/url-preview";
@@ -36,6 +38,10 @@ const FIELD_CONTROL_REGISTRY = {
},
data_table_columns: { kind: "standalone", renderer: DataTableColumns },
query_params: { kind: "standalone", renderer: QueryParams },
+ summarize_aggregations: {
+ kind: "standalone",
+ renderer: SummarizeAggregations,
+ },
user_seen_trigger_options: {
kind: "standalone",
renderer: UserSeenTriggerOptions,
@@ -58,6 +64,7 @@ const FIELD_CONTROL_REGISTRY = {
multi_combo_box: { kind: "field", type: "custom", renderer: MultiComboBox },
multi_input: { kind: "field", type: "custom", renderer: MultiInput },
filter_query: { kind: "field", type: "custom", renderer: FilterQuery },
+ field_path: { kind: "field", type: "custom", renderer: FieldPathControl },
url_preview: { kind: "field", type: "custom", renderer: UrlPreview },
tags: { kind: "field", type: "custom", renderer: TagsControl },
category: { kind: "field", type: "custom", renderer: CategoryControl },
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/input-fields.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/input-fields.js
new file mode 100644
index 00000000000..7290400a67f
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/input-fields.js
@@ -0,0 +1,89 @@
+import { schemaFieldsForNodeOutput } from "./data-preview";
+import { outputIndexForConnection } from "./expression-paths";
+import { schemaFieldsForItems } from "./schema-fields";
+import {
+ inputConnectionsForNode,
+ previousNodeForConnection,
+ resolveDeclaredOutputSchemas,
+} from "./schema-graph";
+
+function flattenPaths(fields, prefix = "") {
+ const result = [];
+
+ for (const field of fields) {
+ const path = prefix ? `${prefix}.${field.key}` : field.key;
+ if (!field.key) {
+ continue;
+ }
+
+ result.push({ path, type: field.type });
+
+ if (field.children?.length) {
+ result.push(...flattenPaths(field.children, path));
+ }
+ }
+
+ return result;
+}
+
+/**
+ * Dot paths available on a node's input items, relative to the item's json.
+ *
+ * Resolves from the declared output schema of upstream nodes, so paths are known
+ * before the workflow has ever run; falls back to pinned or last-run items when a
+ * node declares no schema.
+ *
+ * @param {object} node - the node being configured
+ * @param {object} context - { nodes, connections, nodeTypes, session }
+ * @returns {Array<{path: string, type: string}>} deduplicated, in declaration order
+ */
+export function inputFieldPathsForNode(node, context = {}) {
+ if (!node) {
+ return [];
+ }
+
+ const graph = {
+ nodes: context.nodes || [],
+ connections: context.connections || [],
+ nodeTypes: context.nodeTypes || [],
+ };
+ const session = context.session;
+ const runData = session?.lastExecutionRunData || {};
+ const declaredOutputSchemas = resolveDeclaredOutputSchemas(graph);
+ const seen = new Set();
+ const paths = [];
+
+ for (const connection of inputConnectionsForNode(node, graph)) {
+ const previousNode = previousNodeForConnection(connection, graph);
+ if (!previousNode) {
+ continue;
+ }
+
+ const outputIndex = outputIndexForConnection(connection);
+ const pinnedItems =
+ outputIndex === 0
+ ? session?.pinnedItemsForNode(previousNode.name)
+ : undefined;
+
+ const fields = pinnedItems
+ ? schemaFieldsForItems(pinnedItems, { prefix: "" })
+ : schemaFieldsForNodeOutput(runData, previousNode.name, {
+ node: previousNode,
+ outputIndex,
+ prefix: "",
+ graph,
+ configuration: previousNode.parameters,
+ declaredOutputSchemas,
+ });
+
+ for (const entry of flattenPaths(fields)) {
+ if (seen.has(entry.path)) {
+ continue;
+ }
+ seen.add(entry.path);
+ paths.push(entry);
+ }
+ }
+
+ return paths;
+}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/summarize-output-key.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/summarize-output-key.js
new file mode 100644
index 00000000000..bc9b3e1a825
--- /dev/null
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/summarize-output-key.js
@@ -0,0 +1,47 @@
+// Mirrors DiscourseWorkflows::Nodes::Summarize::V1.output_key so the editor can show
+// which field an aggregation will write to before the workflow runs.
+const OUTPUT_PREFIXES = {
+ append: "appended_",
+ average: "average_",
+ collect: "collected_",
+ concatenate: "concatenated_",
+ count: "count_",
+ count_unique: "unique_count_",
+ first: "first_",
+ last: "last_",
+ max: "max_",
+ min: "min_",
+ sum: "sum_",
+ unique: "unique_",
+};
+
+function leafName(field) {
+ return field.includes(".") ? field.split(".").pop() : field;
+}
+
+function sanitizeKey(name) {
+ return String(name)
+ .replace(/["[\]]/g, "")
+ .replace(/[ .]/g, "_");
+}
+
+export function summarizeOutputKey(row = {}) {
+ const explicit = String(row.output_field_name ?? "").trim();
+ if (explicit) {
+ return explicit;
+ }
+
+ const aggregation = String(row.aggregation ?? "");
+ const prefix = OUTPUT_PREFIXES[aggregation] ?? "";
+ const field = String(row.field ?? "").trim();
+
+ if (!field) {
+ return prefix.replace(/_$/, "");
+ }
+
+ return `${prefix}${sanitizeKey(leafName(field))}`;
+}
+
+export function summarizeOutputKeyIsDerived(row = {}) {
+ return !String(row.output_field_name ?? "").trim();
+}
diff --git a/plugins/discourse-workflows/app/controllers/discourse_workflows/node_previews_controller.rb b/plugins/discourse-workflows/app/controllers/discourse_workflows/node_previews_controller.rb
new file mode 100644
index 00000000000..3c4049b2584
--- /dev/null
+++ b/plugins/discourse-workflows/app/controllers/discourse_workflows/node_previews_controller.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ class NodePreviewsController < ::Admin::AdminController
+ requires_plugin DiscourseWorkflows::PLUGIN_NAME
+
+ RATE_LIMIT = 60
+ RATE_PERIOD = 60
+
+ before_action :rate_limit_preview, only: :create
+
+ def create
+ Workflow::PreviewNode.call(service_params) do |result|
+ on_success { |output:| render json: output }
+ on_failed_policy(:can_manage_workflows) { raise Discourse::InvalidAccess }
+ on_failed_policy(:node_type_previewable) { head :unprocessable_entity }
+ on_model_not_found(:workflow) { raise Discourse::NotFound }
+ on_model_not_found(:node) { raise Discourse::NotFound }
+ on_model_not_found(:node_type_class) { raise Discourse::NotFound }
+ on_failed_contract do |contract|
+ render(
+ json: failed_json.merge(errors: contract.errors.full_messages),
+ status: :bad_request,
+ )
+ end
+ on_failure { render(json: failed_json, status: :unprocessable_entity) }
+ end
+ end
+
+ private
+
+ def rate_limit_preview
+ RateLimiter.new(
+ current_user,
+ "workflow-node-preview",
+ RATE_LIMIT,
+ RATE_PERIOD,
+ apply_limit_to_staff: true,
+ ).performed!
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/workflow/action/run_node_preview.rb b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/action/run_node_preview.rb
new file mode 100644
index 00000000000..acff925ba24
--- /dev/null
+++ b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/action/run_node_preview.rb
@@ -0,0 +1,82 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ class Workflow::Action::RunNodePreview < Service::ActionBase
+ MAX_ITEMS = 25
+
+ option :workflow_snapshot
+ option :node
+ option :node_type_class
+ option :preview_context
+ option :parameters, optional: true
+ option :user
+
+ def call
+ sandbox = DiscourseWorkflows::JsSandbox.new(preview_context, user: user)
+ resolver =
+ DiscourseWorkflows::ExpressionResolver.new(preview_context, sandbox: sandbox, user: user)
+
+ success_payload(execute_node(resolver))
+ rescue DiscourseWorkflows::NodeError,
+ DiscourseWorkflows::JsSandbox::SandboxError,
+ DiscourseWorkflows::JsSandbox::BudgetExceededError,
+ MiniRacer::Error => e
+ error_payload(e.message)
+ rescue StandardError => e
+ Discourse.warn_exception(e, message: "discourse-workflows: node preview failed")
+ error_payload(I18n.t("discourse_workflows.errors.node_preview_failed"))
+ ensure
+ resolver&.dispose
+ sandbox&.dispose
+ end
+
+ private
+
+ def items
+ @items ||=
+ begin
+ pinned = workflow_snapshot.pinned_items_for(node)
+ pinned.presence || Array(preview_context["__input_items"])
+ end
+ end
+
+ def node_parameters
+ @node_parameters ||= (parameters || node.parameters || {}).deep_stringify_keys
+ end
+
+ def execute_node(resolver)
+ node_type_class.new(parameters: node_parameters, credentials: {}).execute(
+ execution_context(resolver),
+ )
+ end
+
+ def execution_context(resolver)
+ DiscourseWorkflows::Executor::NodeExecutionContext.new(
+ input_items: items,
+ parameters: node_parameters,
+ credentials: {
+ },
+ property_schema: node_type_class.property_schema,
+ credential_schema: node_type_class.credentials,
+ node_identifier: node_type_class.identifier,
+ resolver: resolver,
+ resolver_context: preview_context,
+ user: user,
+ )
+ end
+
+ def success_payload(outputs)
+ ports = Array(outputs).map { |port| Array(port) }
+
+ {
+ input_count: items.length,
+ outputs: ports.map { |port| port.first(MAX_ITEMS) },
+ truncated: ports.any? { |port| port.length > MAX_ITEMS },
+ }
+ end
+
+ def error_payload(message)
+ { input_count: items.length, outputs: [], error: message }
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/workflow/preview_node.rb b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/preview_node.rb
new file mode 100644
index 00000000000..d3fc2ed842e
--- /dev/null
+++ b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/preview_node.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ class Workflow::PreviewNode
+ include Service::Base
+
+ params do
+ attribute :workflow_id, :integer
+ attribute :node_id, :string
+ attribute :parameters
+
+ validates :workflow_id, presence: true
+ validates :node_id, presence: true
+ validate :parameters_must_be_a_hash
+
+ private
+
+ def parameters_must_be_a_hash
+ return if parameters.nil? || parameters.is_a?(Hash)
+ errors.add(:parameters, :invalid)
+ end
+ end
+
+ policy :can_manage_workflows, class_name: Policy::CanManageWorkflows
+ model :workflow
+ model :workflow_snapshot
+ model :node
+ model :node_type_class
+ policy :node_type_previewable
+ model :preview_context, :build_preview_context
+ model :output, :run_node
+
+ private
+
+ def fetch_workflow(params:)
+ DiscourseWorkflows::Workflow.find_by(id: params.workflow_id)
+ end
+
+ def fetch_workflow_snapshot(workflow:)
+ DiscourseWorkflows::WorkflowSnapshot.from_workflow(workflow, published: false)
+ end
+
+ def fetch_node(workflow_snapshot:, params:)
+ workflow_snapshot.find_node(params.node_id)
+ end
+
+ def fetch_node_type_class(node:)
+ DiscourseWorkflows::Registry.find_node_type(node.type, version: node.type_version)
+ end
+
+ def node_type_previewable(node_type_class:)
+ node_type_class.previewable? && node_type_class.available?
+ end
+
+ def build_preview_context(workflow:, params:)
+ Workflow::Action::BuildExpressionPreviewContext.call(workflow:, node_id: params.node_id)
+ end
+
+ def run_node(workflow_snapshot:, node:, node_type_class:, preview_context:, params:, guardian:)
+ Workflow::Action::RunNodePreview.call(
+ workflow_snapshot:,
+ node:,
+ node_type_class:,
+ preview_context:,
+ parameters: params.parameters,
+ user: guardian.user,
+ )
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/assets/stylesheets/common/configurator/configurator-modal.scss b/plugins/discourse-workflows/assets/stylesheets/common/configurator/configurator-modal.scss
index 98600c93134..2b5c6e2556e 100644
--- a/plugins/discourse-workflows/assets/stylesheets/common/configurator/configurator-modal.scss
+++ b/plugins/discourse-workflows/assets/stylesheets/common/configurator/configurator-modal.scss
@@ -3,6 +3,8 @@
}
.workflows-configurator-modal {
+ --input-border-color: var(--primary-low-mid);
+
.d-modal__container {
max-width: 100%;
width: 95vw;
diff --git a/plugins/discourse-workflows/assets/stylesheets/common/configurator/configurator.scss b/plugins/discourse-workflows/assets/stylesheets/common/configurator/configurator.scss
index c17f1d63520..74d38e76555 100644
--- a/plugins/discourse-workflows/assets/stylesheets/common/configurator/configurator.scss
+++ b/plugins/discourse-workflows/assets/stylesheets/common/configurator/configurator.scss
@@ -313,3 +313,137 @@
}
}
}
+
+.workflows-field-path {
+ width: 100%;
+}
+
+// Each aggregation is its own short stack of lines, so the generic repeatable-row
+// card is stripped back and a rule is used to tell one aggregation from the next.
+.workflows-property-engine__collection-row:has(.workflows-summarize-row) {
+ display: flex;
+ align-items: flex-start;
+ gap: var(--space-1);
+ border: 0;
+ background: transparent;
+ padding-block: var(--space-2);
+ padding-inline: 0;
+
+ + .workflows-property-engine__collection-row:has(.workflows-summarize-row) {
+ border-block-start: 1px solid var(--primary-low);
+ }
+
+ .workflows-property-engine__collection-delete {
+ position: static;
+ transform: none;
+ order: 2;
+ border: 0;
+ background: transparent;
+
+ // Match the control height so the delete sits on the first line's centre.
+ height: var(--space-9);
+ display: flex;
+ align-items: center;
+ }
+
+ .workflows-property-engine__collection-fields {
+ order: 1;
+ min-width: 0;
+ }
+}
+
+.workflows-summarize-row {
+ // Shared by every lead, which is what keeps the controls of one row - and of all
+ // the rows below it - on a single vertical rule. Wide enough for the longest
+ // aggregation name, since a truncated one reads as a different calculation.
+ --summarize-lead-width: 11.5rem;
+ display: flex;
+ flex-direction: column;
+ row-gap: var(--space-1);
+ width: 100%;
+
+ &__line {
+ display: flex;
+ align-items: center;
+ gap: var(--space-1);
+ min-width: 0;
+ }
+
+ &__lead {
+ flex: 0 0 var(--summarize-lead-width);
+ min-width: 0;
+
+ &--label {
+ text-align: end;
+ color: var(--primary-medium);
+ }
+ }
+
+ &__control {
+ flex: 1 1 auto;
+ min-width: 0;
+
+ &--derived input::placeholder {
+ font-style: italic;
+ }
+ }
+
+ // A line is a lead and its control, so the form fields inside must not reserve
+ // vertical space for labels or impose a width of their own.
+ .form-kit__field {
+ margin: 0;
+ min-width: 0;
+ }
+
+ .form-kit__container-content {
+ width: 100%;
+ min-width: 0;
+ }
+
+ // Every control shares the height form-kit gives its inputs, otherwise the selects,
+ // the text inputs and the combo boxes each sit on a different baseline.
+ .form-kit__control-select,
+ .form-kit__control-input,
+ .select-kit-header {
+ height: var(--space-9);
+ margin: 0;
+ }
+
+ .form-kit__control-select,
+ .form-kit__control-input {
+ width: 100%;
+ }
+}
+
+.workflows-node-preview {
+ margin-block-end: var(--space-3);
+
+ &__truncated {
+ color: var(--primary-medium);
+ font-size: var(--font-down-1);
+ }
+
+ &__table-wrapper {
+ overflow-x: auto;
+ border: 1px solid var(--primary-low);
+ border-radius: var(--d-border-radius);
+ }
+
+ &__table {
+ width: 100%;
+ margin: 0;
+ font-size: var(--font-down-1);
+
+ th,
+ td {
+ padding: var(--space-1) var(--space-2);
+ text-align: start;
+ white-space: nowrap;
+ }
+
+ th {
+ font-family: var(--d-font-family--monospace);
+ font-weight: 500;
+ }
+ }
+}
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index b0964bcdff2..b5cb4d6733f 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -245,6 +245,7 @@ en:
"flow:loop_over_items": "Loop over items"
"action:split_out": "Split out"
"action:sort": "Sort"
+ "action:summarize": "Summarize"
"action:http_request": "HTTP request"
"action:workflow_call": "Call workflow"
"action:data_table": "Data table"
@@ -297,6 +298,7 @@ en:
"flow:loop_over_items": "Iterate over a list of items and run steps for each"
"action:split_out": "Split an array into individual items for parallel processing"
"action:sort": "Sort items by field values, randomly, or with custom code"
+ "action:summarize": "Group items by a field and compute counts, sums, and other aggregations"
"action:http_request": "Make an HTTP request to an external API"
"action:workflow_call": "Run another workflow and use its result"
"action:data_table": "Read, write, or query a workflow data table"
@@ -640,6 +642,19 @@ en:
all: "Returns an array of the current node's input items."
first: "Returns the current node's first input item."
last: "Returns the current node's last input item."
+ node_preview:
+ title: "Preview"
+ counts: "%{input} → %{output}"
+ items:
+ one: "%{count} item"
+ other: "%{count} items"
+ nested_items:
+ one: "[%{count} item]"
+ other: "[%{count} items]"
+ no_items: "This step produces no items from the current sample data."
+ pending: "Run the workflow once, or pin data on the previous step, to preview the result here."
+ truncated: "Showing the first items only."
+ failed: "Could not preview this step."
property_engine:
add_item: "Add item"
add_option: "Add option"
@@ -935,6 +950,36 @@ en:
descending: "Descending"
code_field: "JavaScript code"
code_description: "Write a comparator function body. Variables a and b are item objects. Return -1, 0, or 1."
+ summarize:
+ fields_to_split_by: "Fields"
+ group_by_none: "Nothing"
+ as: "as"
+ add_fields_to_summarize: "Add"
+ aggregation: "Calculation"
+ aggregation_append: "Every value of"
+ aggregation_average: "Average of"
+ aggregation_collect: "Keep the items"
+ aggregation_concatenate: "Join together"
+ aggregation_count: "How many"
+ aggregation_count_unique: "How many distinct"
+ aggregation_first: "First"
+ aggregation_last: "Last"
+ aggregation_max: "Highest"
+ aggregation_min: "Lowest"
+ aggregation_sum: "Total"
+ aggregation_unique: "Every distinct"
+ field: "Field"
+ field_description: "Field to aggregate. For \"How many\", leave empty to count every item in the group. For \"Keep the items\", leave empty to keep whole items."
+ output_field_name: "Output field name"
+ output_field_name_description: "Name of the field to write the result to. Defaults to a name derived from the calculation and field."
+ separate_by: "Separator"
+ separate_by_comma: "Comma"
+ separate_by_comma_space: "Comma and space"
+ separate_by_space: "Space"
+ separate_by_newline: "New line"
+ separate_by_none: "None"
+ separate_by_custom: "Other"
+ custom_separator: "Custom separator"
merge:
mode: "Mode"
mode_append: "Append"
@@ -968,7 +1013,7 @@ en:
mode_run_once_for_each_item: "Run once for each item"
mode_run_once_for_all_items: "Run once for all items"
template: "Template"
- template_description: "In all-items mode, use loop syntax <code>&#123;% for item in items %&#125;...&#123;% endfor %&#125;</code>. Inside the loop, prefix item fields with <code>item.</code>, for example <code>&#123;&#123; item.topic.title &#125;&#125;</code>. The loop index is available as <code>item.item_index</code>. In each-item mode, use the same <code>item.</code> prefix for the current item. <code>items_count</code>, <code>vars</code>, <code>workflow</code>, <code>execution</code>, and <code>site_settings</code> are also accessible. Workflow expressions are not evaluated inside this field. If you need to modify your items, use the Set fields node before the template node."
+ template_description: "In all-items mode, use loop syntax <code>&#123;% for item in items %&#125;...&#123;% endfor %&#125;</code>. Inside the loop, prefix item fields with <code>item.</code>, for example <code>&#123;&#123; item.topic.title &#125;&#125;</code>. The loop index is available as <code>item.item_index</code>. In each-item mode, use the same <code>item.</code> prefix for the current item. When several branches are connected, each one is available as <code>inputs[0]</code>, <code>inputs[1]</code>, and so on, in the order they are connected; <code>items</code> is always the first input. <code>items_count</code>, <code>vars</code>, <code>workflow</code>, <code>execution</code>, and <code>site_settings</code> are also accessible. Workflow expressions are not evaluated inside this field. If you need to modify your items, use the Set fields node before the template node."
template_placeholder: "Hello item.name"
form:
page_type: "Page type"
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.yml
index c8421f8fe88..c5fa86d6afe 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -54,6 +54,7 @@ en:
invalid_schedule_rule: "One or more schedule rules are invalid."
not_enabled: "The discourse-workflows plugin is not enabled."
no_permission_to_manage: "You don't have permission to manage workflows."
+ node_preview_failed: "Something went wrong while previewing this step."
invalid_form_submission: "The form submission has invalid or missing fields."
invalid_form_token: "The form token is invalid or has expired."
webhook_test_listener_active: "A webhook test listener is already active for this URL."
@@ -126,6 +127,12 @@ en:
uncategorized_not_allowed: "This site does not allow topics without a category."
sort:
code_missing_return: "Code must contain a return statement."
+ summarize:
+ no_aggregations: "No aggregations configured."
+ no_aggregations_help: "Add at least one aggregation, for example a count or a sum."
+ invalid_aggregation: "Unknown aggregation: %{aggregation}."
+ duplicate_output_field: "Two aggregations write to the same output field '%{field}'."
+ duplicate_output_field_help: "Set a different output field name on one of them."
split_out:
destination_count_mismatch: "Destination field name count must match field count."
fields_to_include_required: "No fields specified."
diff --git a/plugins/discourse-workflows/config/routes.rb b/plugins/discourse-workflows/config/routes.rb
index 5b5bbc5de19..1e98c073842 100644
--- a/plugins/discourse-workflows/config/routes.rb
+++ b/plugins/discourse-workflows/config/routes.rb
@@ -59,6 +59,7 @@ DiscourseWorkflows::Engine.routes.draw do
get "/stats" => "stats#index"
get "/stats/:workflow_id" => "stats#index"
post "/expressions/evaluate" => "expressions#evaluate"
+ post "/node-previews" => "node_previews#create"
get "/variables" => "variables#index"
post "/variables" => "variables#create"
put "/variables/:id" => "variables#update"
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
index 053335a8def..b07a5651f37 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
@@ -23,6 +23,7 @@ module DiscourseWorkflows
output_contracts: [],
palette_visible: true,
available: true,
+ previewable: false,
}.freeze
def self.inherited(subclass)
@@ -71,6 +72,10 @@ module DiscourseWorkflows
description_value(:palette_visible)
end
+ def self.previewable?
+ description_value(:previewable)
+ end
+
def self.available?
description_value(:available)
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type_serializer.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type_serializer.rb
index dfdd2e3df67..738ff36c130 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type_serializer.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type_serializer.rb
@@ -66,6 +66,7 @@ module DiscourseWorkflows
operations: klass.operations,
output_contracts: serializable_output_contracts(klass),
palette_visible: klass.palette_visible?,
+ previewable: klass.previewable?,
available: klass.available?,
unavailable_reason_key: (klass.unavailable_reason_key unless klass.available?),
metadata: metadata.presence,
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/summarize/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/summarize/v1.rb
new file mode 100644
index 00000000000..0975e4762e3
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/summarize/v1.rb
@@ -0,0 +1,384 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module Summarize
+ class V1 < NodeType
+ AGGREGATIONS = %w[
+ append
+ average
+ collect
+ concatenate
+ count
+ count_unique
+ first
+ last
+ max
+ min
+ sum
+ unique
+ ].freeze
+
+ OUTPUT_PREFIXES = {
+ "append" => "appended_",
+ "average" => "average_",
+ "collect" => "collected_",
+ "concatenate" => "concatenated_",
+ "count" => "count_",
+ "count_unique" => "unique_count_",
+ "first" => "first_",
+ "last" => "last_",
+ "max" => "max_",
+ "min" => "min_",
+ "sum" => "sum_",
+ "unique" => "unique_",
+ }.freeze
+
+ SEPARATORS = {
+ "comma" => ",",
+ "comma_space" => ", ",
+ "space" => " ",
+ "newline" => "\n",
+ "none" => "",
+ }.freeze
+ SEPARATOR_OPTIONS = (SEPARATORS.keys + ["custom"]).freeze
+
+ NUMBER_TYPE = { "type" => %w[number null] }.freeze
+ INTEGER_TYPE = { "type" => "integer" }.freeze
+ STRING_TYPE = { "type" => "string" }.freeze
+ ARRAY_TYPE = { "type" => "array" }.freeze
+ ANY_TYPE = {}.freeze
+
+ description(
+ name: "action:summarize",
+ version: "1.0",
+ defaults: {
+ icon: "layer-group",
+ color: "yellow",
+ },
+ group: "data",
+ previewable: true,
+ capabilities: {
+ run_scope: "all_items",
+ },
+ properties: {
+ fields_to_split_by: {
+ type: :string,
+ required: false,
+ no_data_expression: true,
+ ui: {
+ control: :field_path,
+ multiple: true,
+ },
+ control_options: {
+ none_label_i18n_key: "discourse_workflows.summarize.group_by_none",
+ },
+ },
+ fields_to_summarize: {
+ type: :fixed_collection,
+ required: true,
+ ui: {
+ control: :summarize_aggregations,
+ },
+ type_options: {
+ multiple_values: true,
+ },
+ options: [
+ {
+ name: "values",
+ values: {
+ aggregation: {
+ type: :options,
+ required: true,
+ options: AGGREGATIONS,
+ default: "count",
+ no_data_expression: true,
+ },
+ field: {
+ type: :string,
+ required: false,
+ no_data_expression: true,
+ ui: {
+ control: :field_path,
+ },
+ },
+ output_field_name: {
+ type: :string,
+ required: false,
+ no_data_expression: true,
+ },
+ separate_by: {
+ type: :options,
+ required: false,
+ options: SEPARATOR_OPTIONS,
+ default: "comma_space",
+ no_data_expression: true,
+ display_options: {
+ show: {
+ aggregation: ["concatenate"],
+ },
+ },
+ },
+ custom_separator: {
+ type: :string,
+ required: false,
+ no_data_expression: true,
+ display_options: {
+ show: {
+ separate_by: ["custom"],
+ },
+ },
+ },
+ },
+ },
+ ],
+ },
+ },
+ )
+
+ # The output keys depend on how the node is configured, so the contract is derived
+ # from the configuration rather than declared as a constant.
+ def self.output_schemas(configuration = {}, input_schemas: [])
+ config = (configuration || {}).deep_stringify_keys
+ rows = Array(config.dig("fields_to_summarize", "values"))
+ split_fields = split_field_list(config["fields_to_split_by"])
+
+ properties = {}
+ split_fields.each { |field| properties[leaf_name(field)] = ANY_TYPE }
+ rows.each { |row| properties[output_key(row)] = schema_type_for(row) }
+
+ [Schema.document(properties)]
+ end
+
+ def self.split_field_list(value)
+ value.to_s.split(",").filter_map { |field| field.strip.presence }
+ end
+
+ def self.leaf_name(field)
+ return field if field.exclude?(".")
+ field.split(".").last
+ end
+
+ def self.output_key(row)
+ row = row.deep_stringify_keys
+ explicit = row["output_field_name"].to_s.strip
+ return explicit if explicit.present?
+
+ prefix = OUTPUT_PREFIXES.fetch(row["aggregation"].to_s, "")
+ field = row["field"].to_s.strip
+ return prefix.chomp("_") if field.empty?
+
+ "#{prefix}#{sanitize_key(leaf_name(field))}"
+ end
+
+ def self.sanitize_key(name)
+ name.to_s.gsub(/["\[\]]/, "").gsub(/[ .]/, "_")
+ end
+
+ def self.schema_type_for(row)
+ case row.deep_stringify_keys["aggregation"].to_s
+ when "count", "count_unique"
+ INTEGER_TYPE
+ when "sum", "average"
+ NUMBER_TYPE
+ when "concatenate"
+ STRING_TYPE
+ when "append", "collect", "unique"
+ ARRAY_TYPE
+ else
+ ANY_TYPE
+ end
+ end
+
+ def execute(exec_ctx)
+ split_fields =
+ self.class.split_field_list(
+ exec_ctx.get_node_parameter("fields_to_split_by", 0, default: ""),
+ )
+ rows = normalized_rows(exec_ctx)
+ groups = group_items(exec_ctx.input_items, split_fields)
+
+ [separate_items(groups, split_fields, rows, exec_ctx)]
+ end
+
+ private
+
+ def normalized_rows(exec_ctx)
+ rows =
+ Array(exec_ctx.get_node_parameter("fields_to_summarize.values", 0, default: [])).map(
+ &:deep_stringify_keys
+ )
+
+ if rows.empty?
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.summarize.no_aggregations"),
+ description: I18n.t("discourse_workflows.errors.summarize.no_aggregations_help"),
+ )
+ end
+
+ rows.each { |row| validate_aggregation!(row) }
+ validate_unique_output_keys!(rows)
+ rows
+ end
+
+ def validate_aggregation!(row)
+ aggregation = row["aggregation"].to_s
+ return if AGGREGATIONS.include?(aggregation)
+
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.summarize.invalid_aggregation",
+ aggregation: aggregation,
+ ),
+ )
+ end
+
+ def validate_unique_output_keys!(rows)
+ keys = rows.map { |row| self.class.output_key(row) }
+ duplicate = keys.tally.find { |_key, count| count > 1 }&.first
+ return if duplicate.nil?
+
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.summarize.duplicate_output_field", field: duplicate),
+ description: I18n.t("discourse_workflows.errors.summarize.duplicate_output_field_help"),
+ )
+ end
+
+ def group_items(input_items, split_fields)
+ return [{ keys: [], items: input_items }] if split_fields.empty?
+
+ input_items
+ .group_by { |item| split_fields.map { |field| dig_field(item_json(item), field) } }
+ .map { |keys, items| { keys: keys, items: items } }
+ end
+
+ def separate_items(groups, split_fields, rows, exec_ctx)
+ groups.map do |group|
+ json = {}
+
+ split_fields.each_with_index do |field, index|
+ json[self.class.leaf_name(field)] = group[:keys][index]
+ end
+
+ json.merge!(aggregate(group[:items], rows))
+
+ wrap(json, paired_item: paired_items_for(group[:items], exec_ctx))
+ end
+ end
+
+ def aggregate(items, rows)
+ rows.each_with_object({}) do |row, result|
+ result[self.class.output_key(row)] = compute(row, items)
+ end
+ end
+
+ def compute(row, items)
+ field = row["field"].to_s.strip
+
+ case row["aggregation"].to_s
+ when "count"
+ return items.length if field.empty?
+ values(items, field).length
+ when "count_unique"
+ unique_values(items, field).length
+ when "unique"
+ unique_values(items, field)
+ when "append"
+ values(items, field)
+ when "collect"
+ return items.map { |item| item_json(item) } if field.empty?
+ values(items, field)
+ when "concatenate"
+ values(items, field).map { |value| cell(value) }.join(separator(row))
+ when "first"
+ values(items, field).first
+ when "last"
+ values(items, field).last
+ when "sum"
+ numeric_values(items, field).sum
+ when "average"
+ numbers = numeric_values(items, field)
+ return nil if numbers.empty?
+ numbers.sum.to_f / numbers.length
+ when "min"
+ comparable_values(items, field).min
+ when "max"
+ comparable_values(items, field).max
+ end
+ end
+
+ def values(items, field)
+ items.filter_map do |item|
+ value = dig_field(item_json(item), field)
+ value unless empty_value?(value)
+ end
+ end
+
+ # Array-valued fields are flattened so that, for example, per-post tag arrays
+ # collapse into the set of tags used across a group.
+ def unique_values(items, field)
+ items
+ .flat_map do |item|
+ value = dig_field(item_json(item), field)
+ value.is_a?(Array) ? value : [value]
+ end
+ .reject { |value| empty_value?(value) }
+ .uniq
+ end
+
+ def numeric_values(items, field)
+ items.filter_map { |item| numeric(dig_field(item_json(item), field)) }
+ end
+
+ def comparable_values(items, field)
+ raw = values(items, field)
+ return [] if raw.empty?
+
+ numbers = raw.map { |value| numeric(value) }
+ return numbers if numbers.none?(&:nil?)
+
+ raw.map(&:to_s)
+ end
+
+ def numeric(value)
+ case value
+ when Numeric
+ value
+ when String
+ Float(value)
+ end
+ rescue ArgumentError, TypeError
+ nil
+ end
+
+ def separator(row)
+ key = row["separate_by"].to_s
+ return row["custom_separator"].to_s if key == "custom"
+ SEPARATORS.fetch(key, ", ")
+ end
+
+ def cell(value)
+ value.is_a?(Hash) || value.is_a?(Array) ? JSON.generate(value) : value.to_s
+ end
+
+ def empty_value?(value)
+ return true if value.nil?
+ return value.empty? if value.respond_to?(:empty?)
+ false
+ end
+
+ def item_json(item)
+ item.fetch("json") { {} }
+ end
+
+ def dig_field(json, field)
+ field.split(".").reduce(json) { |value, key| value.is_a?(Hash) ? value[key] : nil }
+ end
+
+ def paired_items_for(items, exec_ctx)
+ items.map { |item| exec_ctx.paired_item_for(item) }
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb
index 5ef9d876122..2ccefa71c66 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/template/v1.rb
@@ -15,6 +15,11 @@ module DiscourseWorkflows
RUN_ONCE_FOR_ALL_ITEMS = "runOnceForAllItems"
RUN_ONCE_FOR_EACH_ITEM = "runOnceForEachItem"
+
+ RENDER_LENGTH_LIMIT = 1.megabyte
+ RENDER_SCORE_LIMIT = 200_000
+ ASSIGN_SCORE_LIMIT = 1.megabyte
+
OUTPUT_SCHEMA = {
"$schema" => Schema::DRAFT_URI,
"type" => "object",
@@ -43,6 +48,10 @@ module DiscourseWorkflows
},
},
output_contracts: [{ schema: OUTPUT_SCHEMA }],
+ inputs: [
+ { key: "main", type: "main", display_name: "Input", required: false, multiple: true },
+ ],
+ required_inputs: 1,
properties: {
mode: {
type: :options,
@@ -130,7 +139,11 @@ module DiscourseWorkflows
end
def render_template(template, context)
- Liquid::Template.parse(template).render!(context)
+ parsed = Liquid::Template.parse(template)
+ parsed.resource_limits.render_length_limit = RENDER_LENGTH_LIMIT
+ parsed.resource_limits.render_score_limit = RENDER_SCORE_LIMIT
+ parsed.resource_limits.assign_score_limit = ASSIGN_SCORE_LIMIT
+ parsed.render!(context)
rescue Liquid::Error => e
raise_node_error!(
I18n.t("discourse_workflows.errors.template.invalid_template"),
@@ -139,12 +152,15 @@ module DiscourseWorkflows
end
def base_template_context(exec_ctx)
+ workflow = workflow_context(exec_ctx)
+
{
"items" => input_items(exec_ctx),
+ "inputs" => exec_ctx.inputs.map { |group| template_items(group) },
"items_count" => exec_ctx.input_items.length,
"vars" => (exec_ctx.vars || {}).deep_stringify_keys,
- "workflow" => workflow_context(exec_ctx),
- "execution" => execution_context(exec_ctx),
+ "workflow" => workflow,
+ "execution" => execution_context(exec_ctx, workflow),
"site_settings" => site_settings_context,
}
end
@@ -171,9 +187,11 @@ module DiscourseWorkflows
end
def input_items(exec_ctx)
- exec_ctx.input_items.map.with_index do |input_item, item_index|
- template_item(input_item, item_index)
- end
+ template_items(exec_ctx.input_items)
+ end
+
+ def template_items(items)
+ items.map.with_index { |input_item, item_index| template_item(input_item, item_index) }
end
def template_item(input_item, item_index)
@@ -191,8 +209,7 @@ module DiscourseWorkflows
exec_ctx.get_workflow.to_h
end
- def execution_context(exec_ctx)
- workflow = workflow_context(exec_ctx)
+ def execution_context(exec_ctx, workflow)
{
"id" => exec_ctx.execution_id,
"workflow_id" => workflow["id"],
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
index 915bcdfdf62..30899af7742 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
@@ -100,6 +100,7 @@ module DiscourseWorkflows
data_table_columns
data_table_condition_builder
data_table_select
+ field_path
filter_query
group_select
icon
@@ -109,6 +110,7 @@ module DiscourseWorkflows
password
query_params
select
+ summarize_aggregations
tags
textarea
time
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/workflow_snapshot.rb b/plugins/discourse-workflows/lib/discourse_workflows/workflow_snapshot.rb
index 68e087fd9d5..dd8c2afcd08 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/workflow_snapshot.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/workflow_snapshot.rb
@@ -139,6 +139,19 @@ module DiscourseWorkflows
@nodes_by_id[connection.source_node_id]
end
+ def pinned_items_for(node)
+ pinned = pin_data.transform_keys(&:to_s)
+ return [] if pinned.blank?
+
+ items =
+ connections_to(node)
+ .filter_map { |connection| source_node(connection)&.name }
+ .filter_map { |name| pinned[name.to_s] }
+ .first
+
+ Array(items)
+ end
+
def node_has_reachable_downstream_of_type?(node_id, type)
visited = Set.new
queue = [node_id.to_s]
diff --git a/plugins/discourse-workflows/plugin.rb b/plugins/discourse-workflows/plugin.rb
index 69524404f7a..c9adc407f16 100644
--- a/plugins/discourse-workflows/plugin.rb
+++ b/plugins/discourse-workflows/plugin.rb
@@ -51,6 +51,7 @@ register_svg_icon "user-minus"
register_svg_icon "grip-vertical"
register_svg_icon "paragraph"
register_svg_icon "arrow-down-a-z"
+register_svg_icon "layer-group"
register_svg_icon "copy"
register_svg_icon "paste"
register_svg_icon "scissors"
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/summarize_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/summarize_spec.rb
new file mode 100644
index 00000000000..d2364fc497a
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/summarize_spec.rb
@@ -0,0 +1,351 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::Summarize::V1 do
+ def execute(input_items, configuration = {})
+ execute_node_output(configuration: configuration, input_items: input_items).first
+ end
+
+ def aggregations(*rows)
+ { "values" => rows }
+ end
+
+ def json(result)
+ result.map { |item| item["json"] }
+ end
+
+ let(:posts) do
+ [
+ { "json" => { "id" => 1, "topic_id" => 7, "username" => "ann", "likes" => 9 } },
+ { "json" => { "id" => 2, "topic_id" => 7, "username" => "bob", "likes" => 3 } },
+ { "json" => { "id" => 3, "topic_id" => 8, "username" => "ann", "likes" => 4 } },
+ ]
+ end
+
+ describe "without a group key" do
+ it "collapses every item into a single summary item" do
+ result =
+ execute(
+ posts,
+ "fields_to_summarize" =>
+ aggregations(
+ { "aggregation" => "count", "output_field_name" => "returned_posts" },
+ {
+ "aggregation" => "count_unique",
+ "field" => "topic_id",
+ "output_field_name" => "returned_topics",
+ },
+ { "aggregation" => "sum", "field" => "likes", "output_field_name" => "total_likes" },
+ ),
+ )
+
+ expect(result.length).to eq(1)
+ expect(json(result).first).to eq(
+ { "returned_posts" => 3, "returned_topics" => 2, "total_likes" => 16 },
+ )
+ end
+
+ it "derives output field names from the aggregation and field" do
+ result =
+ execute(
+ posts,
+ "fields_to_summarize" =>
+ aggregations(
+ { "aggregation" => "sum", "field" => "likes" },
+ { "aggregation" => "count" },
+ ),
+ )
+
+ expect(json(result).first).to eq({ "sum_likes" => 16, "count" => 3 })
+ end
+ end
+
+ describe "grouping" do
+ it "emits one item per group with the group key alongside the aggregates" do
+ result =
+ execute(
+ posts,
+ "fields_to_split_by" => "username",
+ "fields_to_summarize" =>
+ aggregations(
+ { "aggregation" => "sum", "field" => "likes", "output_field_name" => "likes" },
+ { "aggregation" => "count", "output_field_name" => "posts" },
+ ),
+ )
+
+ expect(json(result)).to eq(
+ [
+ { "username" => "ann", "likes" => 13, "posts" => 2 },
+ { "username" => "bob", "likes" => 3, "posts" => 1 },
+ ],
+ )
+ end
+
+ it "groups by several fields at once" do
+ result =
+ execute(
+ posts,
+ "fields_to_split_by" => "topic_id, username",
+ "fields_to_summarize" => aggregations({ "aggregation" => "count" }),
+ )
+
+ expect(json(result)).to eq(
+ [
+ { "topic_id" => 7, "username" => "ann", "count" => 1 },
+ { "topic_id" => 7, "username" => "bob", "count" => 1 },
+ { "topic_id" => 8, "username" => "ann", "count" => 1 },
+ ],
+ )
+ end
+
+ it "writes dotted group keys under their leaf name" do
+ input = [{ "json" => { "post" => { "topic_id" => 7 } } }]
+ result =
+ execute(
+ input,
+ "fields_to_split_by" => "post.topic_id",
+ "fields_to_summarize" => aggregations({ "aggregation" => "count" }),
+ )
+
+ expect(json(result)).to eq([{ "topic_id" => 7, "count" => 1 }])
+ end
+ end
+
+ describe "collect" do
+ it "nests each group's items under the output field" do
+ result =
+ execute(
+ posts,
+ "fields_to_split_by" => "topic_id",
+ "fields_to_summarize" =>
+ aggregations({ "aggregation" => "collect", "output_field_name" => "posts" }),
+ )
+
+ expect(json(result).first["posts"].map { |post| post["id"] }).to eq([1, 2])
+ expect(json(result).last["posts"].map { |post| post["id"] }).to eq([3])
+ end
+
+ it "collects a single field when one is given" do
+ result =
+ execute(
+ posts,
+ "fields_to_split_by" => "topic_id",
+ "fields_to_summarize" => aggregations({ "aggregation" => "collect", "field" => "id" }),
+ )
+
+ expect(json(result).first["collected_id"]).to eq([1, 2])
+ end
+ end
+
+ describe "unique" do
+ it "flattens array values and removes duplicates" do
+ input = [
+ { "json" => { "topic_id" => 7, "tags" => %w[perf bug] } },
+ { "json" => { "topic_id" => 7, "tags" => %w[bug ux] } },
+ ]
+ result =
+ execute(
+ input,
+ "fields_to_split_by" => "topic_id",
+ "fields_to_summarize" =>
+ aggregations(
+ { "aggregation" => "unique", "field" => "tags", "output_field_name" => "tags" },
+ ),
+ )
+
+ expect(json(result).first["tags"]).to eq(%w[perf bug ux])
+ end
+
+ it "counts distinct values without returning them" do
+ input = [{ "json" => { "tags" => %w[perf bug] } }, { "json" => { "tags" => %w[bug ux] } }]
+ result =
+ execute(
+ input,
+ "fields_to_summarize" =>
+ aggregations({ "aggregation" => "count_unique", "field" => "tags" }),
+ )
+
+ expect(json(result).first["unique_count_tags"]).to eq(3)
+ end
+ end
+
+ describe "first and last" do
+ it "carries a per-group invariant without repeating it" do
+ input = [
+ { "json" => { "topic_id" => 7, "title" => "Ember upgrade" } },
+ { "json" => { "topic_id" => 7, "title" => "Ember upgrade" } },
+ ]
+ result =
+ execute(
+ input,
+ "fields_to_split_by" => "topic_id",
+ "fields_to_summarize" =>
+ aggregations(
+ { "aggregation" => "first", "field" => "title", "output_field_name" => "title" },
+ ),
+ )
+
+ expect(json(result).first["title"]).to eq("Ember upgrade")
+ end
+
+ it "skips empty values so a missing first value does not win" do
+ input = [{ "json" => { "title" => nil } }, { "json" => { "title" => "Real title" } }]
+ result =
+ execute(
+ input,
+ "fields_to_summarize" => aggregations({ "aggregation" => "first", "field" => "title" }),
+ )
+
+ expect(json(result).first["first_title"]).to eq("Real title")
+ end
+ end
+
+ describe "numeric aggregations" do
+ it "computes average, min, and max" do
+ result =
+ execute(
+ posts,
+ "fields_to_summarize" =>
+ aggregations(
+ { "aggregation" => "average", "field" => "likes" },
+ { "aggregation" => "min", "field" => "likes" },
+ { "aggregation" => "max", "field" => "likes" },
+ ),
+ )
+
+ expect(json(result).first).to eq(
+ { "average_likes" => 16 / 3.0, "min_likes" => 3, "max_likes" => 9 },
+ )
+ end
+
+ it "coerces numeric strings" do
+ input = [{ "json" => { "likes" => "4" } }, { "json" => { "likes" => "6" } }]
+ result =
+ execute(
+ input,
+ "fields_to_summarize" => aggregations({ "aggregation" => "sum", "field" => "likes" }),
+ )
+
+ expect(json(result).first["sum_likes"]).to eq(10.0)
+ end
+
+ it "returns nil for an average with no numeric values" do
+ input = [{ "json" => { "likes" => nil } }]
+ result =
+ execute(
+ input,
+ "fields_to_summarize" => aggregations({ "aggregation" => "average", "field" => "likes" }),
+ )
+
+ expect(json(result).first["average_likes"]).to be_nil
+ end
+ end
+
+ describe "concatenate" do
+ it "joins values with the configured separator" do
+ result =
+ execute(
+ posts,
+ "fields_to_summarize" =>
+ aggregations(
+ {
+ "aggregation" => "concatenate",
+ "field" => "username",
+ "separate_by" => "comma_space",
+ },
+ ),
+ )
+
+ expect(json(result).first["concatenated_username"]).to eq("ann, bob, ann")
+ end
+
+ it "supports a custom separator" do
+ result =
+ execute(
+ posts,
+ "fields_to_summarize" =>
+ aggregations(
+ {
+ "aggregation" => "concatenate",
+ "field" => "username",
+ "separate_by" => "custom",
+ "custom_separator" => " | ",
+ },
+ ),
+ )
+
+ expect(json(result).first["concatenated_username"]).to eq("ann | bob | ann")
+ end
+ end
+
+ describe "append" do
+ it "keeps duplicates and does not flatten arrays" do
+ input = [{ "json" => { "tags" => %w[perf bug] } }, { "json" => { "tags" => %w[bug] } }]
+ result =
+ execute(
+ input,
+ "fields_to_summarize" => aggregations({ "aggregation" => "append", "field" => "tags" }),
+ )
+
+ expect(json(result).first["appended_tags"]).to eq([%w[perf bug], %w[bug]])
+ end
+
+ it "skips empty values by default" do
+ input = [{ "json" => { "name" => "ann" } }, { "json" => { "name" => "" } }]
+ result =
+ execute(
+ input,
+ "fields_to_summarize" => aggregations({ "aggregation" => "append", "field" => "name" }),
+ )
+
+ expect(json(result).first["appended_name"]).to eq(["ann"])
+ end
+ end
+
+ describe "validation" do
+ it "fails when no aggregation is configured" do
+ expect { execute(posts, "fields_to_summarize" => aggregations) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /No aggregations configured/,
+ )
+ end
+
+ it "fails on an unknown aggregation" do
+ expect {
+ execute(posts, "fields_to_summarize" => aggregations({ "aggregation" => "median" }))
+ }.to raise_error(DiscourseWorkflows::NodeError, /Unknown aggregation: median/)
+ end
+
+ it "fails when two aggregations write to the same field" do
+ expect {
+ execute(
+ posts,
+ "fields_to_summarize" =>
+ aggregations(
+ { "aggregation" => "sum", "field" => "likes", "output_field_name" => "total" },
+ { "aggregation" => "count", "output_field_name" => "total" },
+ ),
+ )
+ }.to raise_error(DiscourseWorkflows::NodeError, /same output field 'total'/)
+ end
+ end
+
+ describe ".output_schemas" do
+ it "derives properties from the configured aggregations and group keys" do
+ schema =
+ described_class.output_schemas(
+ {
+ "fields_to_split_by" => "topic_id",
+ "fields_to_summarize" => {
+ "values" => [
+ { "aggregation" => "sum", "field" => "likes", "output_field_name" => "post_likes" },
+ { "aggregation" => "count" },
+ ],
+ },
+ },
+ ).first
+
+ expect(schema["properties"].keys).to eq(%w[topic_id post_likes count])
+ expect(schema.dig("properties", "count", "type")).to eq("integer")
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb
index ec6e59bd747..251e44b97f6 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/template_spec.rb
@@ -15,6 +15,7 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 do
let(:mode) { nil }
let(:template_parameters) { { "template" => template, "mode" => mode }.compact }
let(:execution_items) { [{ "json" => {} }] }
+ let(:input_groups) { nil }
let(:template_vars) { {} }
let(:workflow_context) { nil }
let(:execution_id) { nil }
@@ -24,6 +25,7 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 do
let(:execution_context) do
DiscourseWorkflows::Executor::NodeExecutionContext.new(
input_items: execution_items,
+ input_groups: input_groups,
parameters: template_parameters,
property_schema: described_class.property_schema,
resolver: resolver,
@@ -197,4 +199,81 @@ RSpec.describe DiscourseWorkflows::Nodes::Template::V1 do
end
end
end
+
+ describe "multiple inputs" do
+ it "accepts several connections on its main input" do
+ expect(described_class.input_ports).to contain_exactly(
+ include(key: "main", required: false, multiple: true),
+ )
+ end
+
+ context "with several connected branches" do
+ let(:template) do
+ "{% for u in inputs[1] %}@{{ u.username }} {% endfor %}| {{ inputs[0][0].title }}"
+ end
+ let(:execution_items) { [{ "json" => { "title" => "Topics" } }] }
+ let(:input_groups) do
+ {
+ "input_1" => [{ "json" => { "title" => "Topics" } }],
+ "input_2" => [
+ { "json" => { "username" => "ann" } },
+ { "json" => { "username" => "bob" } },
+ ],
+ }
+ end
+
+ it "exposes each connected branch under inputs" do
+ expect(template_result.first["json"]).to eq("template" => "@ann @bob | Topics")
+ end
+ end
+
+ context "with a template reading items" do
+ let(:template) { "{{ items[0].title }}/{{ items_count }}" }
+ let(:execution_items) { [{ "json" => { "title" => "Topics" } }] }
+ let(:input_groups) do
+ {
+ "input_1" => [{ "json" => { "title" => "Topics" } }],
+ "input_2" => [{ "json" => { "username" => "ann" } }],
+ }
+ end
+
+ it "keeps items pointing at the first input" do
+ expect(template_result.first["json"]).to eq("template" => "Topics/1")
+ end
+ end
+
+ context "with a single connected input" do
+ let(:template) { "{{ inputs[0][0].name }}" }
+ let(:execution_items) { [{ "json" => { "name" => "Alice" } }] }
+
+ it "exposes it as inputs[0]" do
+ expect(template_result.first["json"]).to eq("template" => "Alice")
+ end
+ end
+ end
+
+ describe "resource limits" do
+ context "with a runaway loop without a ceiling" do
+ let(:template) { "{% for i in (1..1000000) %}x{% endfor %}" }
+
+ it "raises a node error instead of rendering it" do
+ expect { template_result }.to raise_error(DiscourseWorkflows::NodeError, /Invalid template/)
+ end
+ end
+
+ context "with a report-sized template" do
+ let(:template) do
+ "{% for t in items %}{{ t.title }}{% for p in t.posts %} {{ p.n }}{% endfor %}\n{% endfor %}"
+ end
+ let(:execution_items) do
+ (1..20).map do |index|
+ { "json" => { "title" => "Topic #{index}", "posts" => (1..20).map { |n| { "n" => n } } } }
+ end
+ end
+
+ it "renders well within the limits" do
+ expect(template_result.first["json"]["template"].lines.length).to eq(20)
+ end
+ end
+ end
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/workflow_snapshot_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/workflow_snapshot_spec.rb
new file mode 100644
index 00000000000..8fdad18e03a
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/workflow_snapshot_spec.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::WorkflowSnapshot do
+ describe "#pinned_items_for" do
+ subject(:snapshot) { described_class.new(data) }
+
+ let(:pinned_items) { [{ "json" => { "id" => 1 } }, { "json" => { "id" => 2 } }] }
+ let(:pin_data) { { "Source" => pinned_items } }
+ let(:data) do
+ {
+ "nodes" => [
+ { "id" => "a", "name" => "Source", "type" => "trigger:manual", "typeVersion" => "1.0" },
+ { "id" => "b", "name" => "Target", "type" => "action:sort", "typeVersion" => "1.0" },
+ ],
+ "connections" => {
+ "Source" => {
+ "main" => [[{ "node" => "Target", "type" => "main", "index" => 0 }]],
+ },
+ },
+ "pinData" => pin_data,
+ }
+ end
+
+ it "returns the pinned items of the connected upstream node" do
+ expect(snapshot.pinned_items_for(snapshot.find_node("b"))).to eq(pinned_items)
+ end
+
+ context "when the upstream node has no pinned data" do
+ let(:pin_data) { { "Elsewhere" => pinned_items } }
+
+ it "returns an empty array" do
+ expect(snapshot.pinned_items_for(snapshot.find_node("b"))).to eq([])
+ end
+ end
+
+ context "when the node has no upstream connection" do
+ it "returns an empty array" do
+ expect(snapshot.pinned_items_for(snapshot.find_node("a"))).to eq([])
+ end
+ end
+
+ context "when nothing is pinned at all" do
+ let(:pin_data) { {} }
+
+ it "returns an empty array" do
+ expect(snapshot.pinned_items_for(snapshot.find_node("b"))).to eq([])
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_previews_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_previews_controller_spec.rb
new file mode 100644
index 00000000000..7ccd62ce712
--- /dev/null
+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_previews_controller_spec.rb
@@ -0,0 +1,112 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::NodePreviewsController do
+ fab!(:admin)
+ fab!(:user)
+ fab!(:workflow) { Fabricate(:discourse_workflows_workflow, created_by: admin) }
+
+ before do
+ workflow.update!(
+ nodes: [
+ {
+ "id" => "manual",
+ "type" => "trigger:manual",
+ "typeVersion" => "1.0",
+ "name" => "Manual",
+ "parameters" => {
+ },
+ },
+ {
+ "id" => "summarize",
+ "type" => "action:summarize",
+ "typeVersion" => "1.0",
+ "name" => "Summarize",
+ "parameters" => {
+ "fields_to_summarize" => {
+ "values" => [
+ { "aggregation" => "sum", "field" => "score", "output_field_name" => "total" },
+ ],
+ },
+ },
+ },
+ {
+ "id" => "create-post",
+ "type" => "action:post",
+ "typeVersion" => "1.0",
+ "name" => "Create post",
+ "parameters" => {
+ "operation" => "create",
+ },
+ },
+ ],
+ connections: {
+ "Manual" => {
+ "main" => [[{ "node" => "Summarize", "type" => "main", "index" => 0 }]],
+ },
+ },
+ pin_data: {
+ "Manual" => [
+ { "json" => { "score" => 1 } },
+ { "json" => { "score" => 9 } },
+ { "json" => { "score" => 5 } },
+ ],
+ },
+ )
+ end
+
+ def preview(node_id, parameters = nil)
+ post "/admin/plugins/discourse-workflows/node-previews.json",
+ params: { workflow_id: workflow.id, node_id: node_id, parameters: parameters }.compact
+ end
+
+ context "when signed in as an admin" do
+ before { sign_in(admin) }
+
+ it "returns the node's output items" do
+ preview("summarize")
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["outputs"].first.map { |item| item["json"] }).to eq(
+ [{ "total" => 15 }],
+ )
+ expect(response.parsed_body["input_count"]).to eq(3)
+ end
+
+ it "rejects a node type that is not previewable" do
+ preview("create-post")
+
+ expect(response.status).to eq(422)
+ end
+
+ it "responds with a bad request when parameters is not an object" do
+ preview("summarize", "junk")
+
+ expect(response.status).to eq(400)
+ expect(response.parsed_body["errors"]).to be_present
+ end
+
+ it "404s for an unknown node" do
+ preview("missing")
+
+ expect(response.status).to eq(404)
+ end
+ end
+
+ context "when signed in as a regular user" do
+ before { sign_in(user) }
+
+ it "is not allowed" do
+ preview("summarize")
+
+ expect(response.status).to eq(404)
+ end
+ end
+
+ context "when anonymous" do
+ it "is not allowed" do
+ preview("summarize")
+
+ expect(response.status).to eq(404)
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/preview_node_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/preview_node_spec.rb
new file mode 100644
index 00000000000..b7d168691ea
--- /dev/null
+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/preview_node_spec.rb
@@ -0,0 +1,160 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Workflow::PreviewNode do
+ describe described_class::Contract, type: :model do
+ it { is_expected.to validate_presence_of(:workflow_id) }
+ it { is_expected.to validate_presence_of(:node_id) }
+
+ it "rejects parameters that are not a hash" do
+ contract = described_class.new(parameters: "junk")
+
+ contract.validate
+
+ expect(contract.errors[:parameters]).to be_present
+ end
+
+ it "accepts absent or hash parameters" do
+ expect(described_class.new(workflow_id: 1, node_id: "a").validate).to eq(true)
+ expect(
+ described_class.new(workflow_id: 1, node_id: "a", parameters: { "x" => 1 }).validate,
+ ).to eq(true)
+ end
+ end
+
+ describe ".call" do
+ subject(:result) { described_class.call(params:, guardian:) }
+
+ fab!(:admin)
+ fab!(:workflow) { Fabricate(:discourse_workflows_workflow, created_by: admin) }
+
+ let(:guardian) { admin.guardian }
+ let(:params) { { workflow_id: workflow.id, node_id: "summarize" } }
+
+ let(:nodes) do
+ [
+ {
+ "id" => "manual",
+ "type" => "trigger:manual",
+ "typeVersion" => "1.0",
+ "name" => "Manual",
+ "parameters" => {
+ },
+ },
+ {
+ "id" => "summarize",
+ "type" => "action:summarize",
+ "typeVersion" => "1.0",
+ "name" => "Summarize",
+ "parameters" => {
+ "fields_to_split_by" => "topic_id",
+ "fields_to_summarize" => {
+ "values" => [
+ { "aggregation" => "sum", "field" => "likes", "output_field_name" => "total" },
+ ],
+ },
+ },
+ },
+ {
+ "id" => "create-post",
+ "type" => "action:post",
+ "typeVersion" => "1.0",
+ "name" => "Create post",
+ "parameters" => {
+ "operation" => "create",
+ },
+ },
+ ]
+ end
+
+ let(:connections) do
+ { "Manual" => { "main" => [[{ "node" => "Summarize", "type" => "main", "index" => 0 }]] } }
+ end
+
+ before do
+ workflow.update!(nodes: nodes, connections: connections)
+ workflow.update!(
+ pin_data: {
+ "Manual" => [
+ { "json" => { "topic_id" => 7, "likes" => 4 } },
+ { "json" => { "topic_id" => 7, "likes" => 5 } },
+ { "json" => { "topic_id" => 8, "likes" => 2 } },
+ ],
+ },
+ )
+ end
+
+ it "runs the node against pinned upstream data" do
+ expect(result).to be_a_success
+ expect(result.output[:input_count]).to eq(3)
+ expect(result.output[:outputs].first.map { |item| item["json"] }).to eq(
+ [{ "topic_id" => 7, "total" => 9 }, { "topic_id" => 8, "total" => 2 }],
+ )
+ end
+
+ it "reflects parameters supplied by the editor rather than the saved ones" do
+ params[:parameters] = {
+ "fields_to_split_by" => "",
+ "fields_to_summarize" => {
+ "values" => [{ "aggregation" => "count", "output_field_name" => "rows" }],
+ },
+ }
+
+ expect(result.output[:outputs].first.map { |item| item["json"] }).to eq([{ "rows" => 3 }])
+ end
+
+ it "fails the contract when parameters is not a hash" do
+ params[:parameters] = "junk"
+
+ expect(result).to fail_a_contract
+ end
+
+ it "refuses to preview a node type that is not previewable" do
+ params[:node_id] = "create-post"
+
+ expect(result).to fail_a_policy(:node_type_previewable)
+ end
+
+ it "returns the node's error instead of raising" do
+ params[:parameters] = { "fields_to_summarize" => { "values" => [] } }
+
+ expect(result).to be_a_success
+ expect(result.output[:error]).to match(/No aggregations configured/)
+ expect(result.output[:outputs]).to eq([])
+ end
+
+ context "when the node fails unexpectedly" do
+ before do
+ allow_any_instance_of(DiscourseWorkflows::Nodes::Summarize::V1).to receive(
+ :execute,
+ ).and_raise(StandardError, "secret internals")
+ end
+
+ it "returns a generic message rather than the exception's" do
+ expect(result).to be_a_success
+ expect(result.output[:error]).to eq(
+ I18n.t("discourse_workflows.errors.node_preview_failed"),
+ )
+ expect(result.output[:outputs]).to eq([])
+ end
+ end
+
+ it "fails when the node is not in the workflow" do
+ params[:node_id] = "nope"
+
+ expect(result).to fail_to_find_a_model(:node)
+ end
+
+ it "truncates long output and says so" do
+ workflow.update!(
+ pin_data: {
+ "Manual" => (1..40).map { |n| { "json" => { "topic_id" => n, "likes" => 1 } } },
+ },
+ )
+
+ expect(result.output[:outputs].first.length).to eq(
+ DiscourseWorkflows::Workflow::Action::RunNodePreview::MAX_ITEMS,
+ )
+ expect(result.output[:truncated]).to eq(true)
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/field-path-control-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/field-path-control-test.gjs
new file mode 100644
index 00000000000..e0d99f36510
--- /dev/null
+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/field-path-control-test.gjs
@@ -0,0 +1,159 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import selectKit from "discourse/tests/helpers/select-kit-helper";
+import FieldPathControl from "discourse/plugins/discourse-workflows/admin/components/workflows/configurators/field-path-control";
+
+const NODE = {
+ clientId: "summarize",
+ name: "Summarize",
+ type: "action:summarize",
+ typeVersion: "1.0",
+ parameters: {},
+};
+
+const UPSTREAM = {
+ clientId: "posts",
+ name: "Posts",
+ type: "action:post",
+ typeVersion: "1.0",
+ parameters: {},
+};
+
+const GRAPH = {
+ nodes: [UPSTREAM, NODE],
+ connections: [
+ {
+ sourceClientId: "posts",
+ targetClientId: "summarize",
+ sourceOutputIndex: 0,
+ targetInputIndex: 0,
+ },
+ ],
+ nodeTypes: [],
+};
+
+function buildSession() {
+ return {
+ lastExecutionRunData: {},
+ pinnedItemsForNode(name) {
+ if (name === "Posts") {
+ return [{ json: { topic_id: 7, username: "ann", score: 5 } }];
+ }
+ },
+ };
+}
+
+module(
+ "Integration | Component | workflows field-path-control",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("single mode lists input paths and stores one", async function (assert) {
+ const writes = [];
+ this.setProperties({
+ node: NODE,
+ nodes: GRAPH.nodes,
+ connections: GRAPH.connections,
+ nodeTypes: GRAPH.nodeTypes,
+ session: buildSession(),
+ schema: { ui: {} },
+ field: { value: "", set: (value) => writes.push(value) },
+ });
+
+ await render(
+ <template>
+ <FieldPathControl
+ @field={{this.field}}
+ @schema={{this.schema}}
+ @node={{this.node}}
+ @nodes={{this.nodes}}
+ @nodeTypes={{this.nodeTypes}}
+ @connections={{this.connections}}
+ @session={{this.session}}
+ />
+ </template>
+ );
+
+ const picker = selectKit(".workflows-field-path");
+ await picker.expand();
+
+ assert.deepEqual(
+ picker.displayedContent().map((row) => row.name),
+ ["topic_id", "username", "score"]
+ );
+
+ await picker.selectRowByValue("username");
+
+ assert.deepEqual(writes, ["username"]);
+ });
+
+ test("multiple mode shows the stored list and appends to it", async function (assert) {
+ const writes = [];
+ this.setProperties({
+ node: NODE,
+ nodes: GRAPH.nodes,
+ connections: GRAPH.connections,
+ nodeTypes: GRAPH.nodeTypes,
+ session: buildSession(),
+ schema: { ui: { multiple: true } },
+ field: {
+ value: "topic_id, username",
+ set: (value) => writes.push(value),
+ },
+ });
+
+ await render(
+ <template>
+ <FieldPathControl
+ @field={{this.field}}
+ @schema={{this.schema}}
+ @node={{this.node}}
+ @nodes={{this.nodes}}
+ @nodeTypes={{this.nodeTypes}}
+ @connections={{this.connections}}
+ @session={{this.session}}
+ />
+ </template>
+ );
+
+ const picker = selectKit(".workflows-field-path");
+
+ assert.strictEqual(picker.header().value(), "topic_id,username");
+
+ await picker.expand();
+ await picker.selectRowByValue("score");
+
+ assert.deepEqual(writes, ["topic_id, username, score"]);
+ });
+
+ test("multiple mode keeps a stored path the input no longer offers", async function (assert) {
+ this.setProperties({
+ node: NODE,
+ nodes: GRAPH.nodes,
+ connections: GRAPH.connections,
+ nodeTypes: GRAPH.nodeTypes,
+ session: buildSession(),
+ schema: { ui: { multiple: true } },
+ field: { value: "vanished_field", set: () => {} },
+ });
+
+ await render(
+ <template>
+ <FieldPathControl
+ @field={{this.field}}
+ @schema={{this.schema}}
+ @node={{this.node}}
+ @nodes={{this.nodes}}
+ @nodeTypes={{this.nodeTypes}}
+ @connections={{this.connections}}
+ @session={{this.session}}
+ />
+ </template>
+ );
+
+ const picker = selectKit(".workflows-field-path");
+ assert.strictEqual(picker.header().value(), "vanished_field");
+ });
+ }
+);
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 d5828b57983..f0bea376f35 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
@@ -12,7 +12,7 @@ import Form from "discourse/components/form";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import pretender, { response } from "discourse/tests/helpers/create-pretender";
import selectKit from "discourse/tests/helpers/select-kit-helper";
-import I18n from "discourse-i18n";
+import I18n, { i18n } from "discourse-i18n";
import PropertyEngineConfigurator from "discourse/plugins/discourse-workflows/admin/components/workflows/configurators/property-engine";
import WorkflowEditorSession from "discourse/plugins/discourse-workflows/admin/lib/workflows/editor-session";
@@ -917,6 +917,165 @@ module("Integration | Component | workflows property engine", function (hooks) {
assert.strictEqual(this.formApi.get("entries.values").length, 1);
});
+ test("labels fixed collection remove buttons for items without a name", async function (assert) {
+ this.setProperties({
+ configuration: { entries: { values: [{ aggregation: "count" }] } },
+ nodeType: "action:log",
+ schema: {
+ entries: {
+ type: "fixed_collection",
+ type_options: {
+ multiple_values: true,
+ },
+ options: [
+ {
+ name: "values",
+ values: {
+ aggregation: {
+ type: "string",
+ required: true,
+ no_data_expression: true,
+ },
+ },
+ },
+ ],
+ },
+ },
+ });
+
+ await render(
+ <template>
+ <Form @data={{this.configuration}} as |form transientData|>
+ <PropertyEngineConfigurator
+ @form={{form}}
+ @configuration={{transientData}}
+ @nodeType={{this.nodeType}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ assert
+ .dom(".workflows-property-engine__collection-delete")
+ .hasAria("label", i18n("discourse_workflows.property_engine.remove_item"))
+ .hasAttribute(
+ "title",
+ i18n("discourse_workflows.property_engine.remove_item")
+ );
+ });
+
+ test("labels fixed collection remove buttons with the item name", async function (assert) {
+ this.setProperties({
+ configuration: { entries: { values: [{ name: "Alpha" }] } },
+ nodeType: "action:log",
+ schema: {
+ entries: {
+ type: "fixed_collection",
+ type_options: {
+ multiple_values: true,
+ },
+ options: [
+ {
+ name: "values",
+ values: {
+ name: {
+ type: "string",
+ required: true,
+ no_data_expression: true,
+ },
+ },
+ },
+ ],
+ },
+ },
+ });
+
+ await render(
+ <template>
+ <Form @data={{this.configuration}} as |form transientData|>
+ <PropertyEngineConfigurator
+ @form={{form}}
+ @configuration={{transientData}}
+ @nodeType={{this.nodeType}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ assert.dom(".workflows-property-engine__collection-delete").hasAria(
+ "label",
+ i18n("discourse_workflows.property_engine.remove_assignment", {
+ name: "Alpha",
+ })
+ );
+ });
+
+ test("only titles the fixed collection add button when a maximum exists", async function (assert) {
+ this.setProperties({
+ configuration: { entries: { values: [] }, capped: { values: [] } },
+ nodeType: "action:log",
+ schema: {
+ entries: {
+ type: "fixed_collection",
+ type_options: {
+ multiple_values: true,
+ },
+ options: [
+ {
+ name: "values",
+ values: {
+ key: { type: "string", required: true },
+ },
+ },
+ ],
+ },
+ capped: {
+ type: "fixed_collection",
+ max_items: 1,
+ type_options: {
+ multiple_values: true,
+ },
+ options: [
+ {
+ name: "values",
+ values: {
+ key: { type: "string", required: true },
+ },
+ },
+ ],
+ },
+ },
+ });
+
+ await render(
+ <template>
+ <Form @data={{this.configuration}} as |form transientData|>
+ <PropertyEngineConfigurator
+ @form={{form}}
+ @configuration={{transientData}}
+ @nodeType={{this.nodeType}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ const addButtons = findAll(".form-kit__section .btn-default");
+
+ assert.dom(addButtons[0]).doesNotHaveAttribute("title");
+ assert.dom(addButtons[1]).hasAttribute(
+ "title",
+ i18n("discourse_workflows.property_engine.max_items_reached", {
+ count: 1,
+ })
+ );
+ });
+
test("renders condition builder controls inside the property engine", async function (assert) {
this.setProperties({
configuration: {
diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-input-fields-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-input-fields-test.js
new file mode 100644
index 00000000000..955ce46aadb
--- /dev/null
+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-input-fields-test.js
@@ -0,0 +1,115 @@
+import { module, test } from "qunit";
+import { inputFieldPathsForNode } from "discourse/plugins/discourse-workflows/admin/lib/workflows/input-fields";
+
+const SUMMARIZE = {
+ clientId: "summarize",
+ name: "Topics",
+ type: "action:summarize",
+ typeVersion: "1.0",
+ parameters: {},
+};
+
+const UPSTREAM = {
+ clientId: "flatten",
+ name: "Flatten posts",
+ type: "action:set_fields",
+ typeVersion: "1.0",
+ parameters: {},
+};
+
+const GRAPH = {
+ nodes: [UPSTREAM, SUMMARIZE],
+ connections: [
+ {
+ sourceClientId: "flatten",
+ targetClientId: "summarize",
+ sourceOutputIndex: 0,
+ targetInputIndex: 0,
+ },
+ ],
+ nodeTypes: [],
+};
+
+function sessionWithPinnedItems(items) {
+ return {
+ lastExecutionRunData: {},
+ pinnedItemsForNode(name) {
+ return name === "Flatten posts" ? items : undefined;
+ },
+ };
+}
+
+module(
+ "Unit | lib | discourse-workflows | inputFieldPathsForNode",
+ function () {
+ test("returns nothing without a node", function (assert) {
+ assert.deepEqual(inputFieldPathsForNode(null, {}), []);
+ });
+
+ test("returns nothing when the node has no upstream connection", function (assert) {
+ const paths = inputFieldPathsForNode(SUMMARIZE, {
+ nodes: [SUMMARIZE],
+ connections: [],
+ nodeTypes: [],
+ session: sessionWithPinnedItems([{ json: { id: 1 } }]),
+ });
+
+ assert.deepEqual(paths, []);
+ });
+
+ test("derives paths from the upstream node's pinned items", function (assert) {
+ const paths = inputFieldPathsForNode(SUMMARIZE, {
+ ...GRAPH,
+ session: sessionWithPinnedItems([
+ { json: { id: 1, topic_id: 7, username: "ann" } },
+ ]),
+ });
+
+ assert.deepEqual(
+ paths.map((entry) => entry.path),
+ ["id", "topic_id", "username"]
+ );
+ });
+
+ test("includes nested paths and their parent", function (assert) {
+ const paths = inputFieldPathsForNode(SUMMARIZE, {
+ ...GRAPH,
+ session: sessionWithPinnedItems([
+ { json: { post: { id: 1, raw: "hello" } } },
+ ]),
+ });
+
+ assert.deepEqual(
+ paths.map((entry) => entry.path),
+ ["post", "post.id", "post.raw"]
+ );
+ });
+
+ test("emits paths without an expression prefix", function (assert) {
+ const paths = inputFieldPathsForNode(SUMMARIZE, {
+ ...GRAPH,
+ session: sessionWithPinnedItems([{ json: { topic_id: 7 } }]),
+ });
+
+ assert.false(
+ paths.some((entry) => entry.path.startsWith("$json")),
+ "paths are relative to the item json"
+ );
+ });
+
+ test("deduplicates paths seen on more than one item", function (assert) {
+ const paths = inputFieldPathsForNode(SUMMARIZE, {
+ ...GRAPH,
+ session: sessionWithPinnedItems([
+ { json: { topic_id: 7 } },
+ { json: { topic_id: 8 } },
+ ]),
+ });
+
+ assert.deepEqual(
+ paths.map((entry) => entry.path),
+ ["topic_id"]
+ );
+ });
+ }
+);
diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-summarize-output-key-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-summarize-output-key-test.js
new file mode 100644
index 00000000000..0f105abf689
--- /dev/null
+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-summarize-output-key-test.js
@@ -0,0 +1,75 @@
+import { module, test } from "qunit";
+import {
+ summarizeOutputKey,
+ summarizeOutputKeyIsDerived,
+} from "discourse/plugins/discourse-workflows/admin/lib/workflows/summarize-output-key";
+
+module("Unit | lib | discourse-workflows | summarizeOutputKey", function () {
+ test("prefers an explicit output field name", function (assert) {
+ assert.strictEqual(
+ summarizeOutputKey({
+ aggregation: "sum",
+ field: "like_count",
+ output_field_name: "post_likes",
+ }),
+ "post_likes"
+ );
+ });
+
+ test("derives a name from the aggregation and field", function (assert) {
+ assert.strictEqual(
+ summarizeOutputKey({ aggregation: "sum", field: "like_count" }),
+ "sum_like_count"
+ );
+ assert.strictEqual(
+ summarizeOutputKey({ aggregation: "count_unique", field: "topic_id" }),
+ "unique_count_topic_id"
+ );
+ assert.strictEqual(
+ summarizeOutputKey({ aggregation: "unique", field: "tags" }),
+ "unique_tags"
+ );
+ });
+
+ test("uses the leaf of a dotted field", function (assert) {
+ assert.strictEqual(
+ summarizeOutputKey({ aggregation: "sum", field: "post.like_count" }),
+ "sum_like_count"
+ );
+ });
+
+ test("drops the trailing underscore when there is no field", function (assert) {
+ assert.strictEqual(summarizeOutputKey({ aggregation: "count" }), "count");
+ assert.strictEqual(
+ summarizeOutputKey({ aggregation: "collect" }),
+ "collected"
+ );
+ });
+
+ test("sanitizes characters that cannot appear in a key", function (assert) {
+ assert.strictEqual(
+ summarizeOutputKey({ aggregation: "sum", field: 'a["b c"]' }),
+ "sum_ab_c"
+ );
+ });
+
+ test("ignores whitespace-only explicit names", function (assert) {
+ assert.strictEqual(
+ summarizeOutputKey({
+ aggregation: "count",
+ output_field_name: " ",
+ }),
+ "count"
+ );
+ });
+
+ test("reports whether the key was derived", function (assert) {
+ assert.true(summarizeOutputKeyIsDerived({ aggregation: "count" }));
+ assert.false(
+ summarizeOutputKeyIsDerived({
+ aggregation: "count",
+ output_field_name: "rows",
+ })
+ );
+ });
+});