A platform for community discussion. Free, open, simple.
FEATURE: Improve AI workflow authoring (#44508)
Sambafedc841019
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/build-with-ai-modal.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/build-with-ai-modal.gjsindex 30213954d06..863efe71aae 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/build-with-ai-modal.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/build-with-ai-modal.gjs@@ -226,10 +226,20 @@ export default class BuildWithAiModal extends Component {.filter(Boolean);}+ get aiProposalHasFailedScripts() {+ return (this.aiProposal?.script_validations || []).some(+ (validation) => !validation.valid+ );+ }+get aiShowingProgress() {return this.aiGenerating && this.aiProgressEvents.length > 0;}+ get aiLatestProgressEvent() {+ return this.aiProgressEvents.at(-1);+ }+@actionaiQuestionText(question) {if (typeof question === "string") {@@ -389,6 +399,11 @@ export default class BuildWithAiModal extends Component {await this.#submitAiMessage(this.#aiClarificationMessage());}+ @action+ async fixAiProposal() {+ await this.#submitAiMessage(i18n("discourse_workflows.ai.fix_message"));+ }+@actionasync applyAiProposal() {if (!this.aiSessionId || !this.aiProposal) {@@ -638,22 +653,18 @@ export default class BuildWithAiModal extends Component {role="dialog"><div class="workflows-canvas__ai-panel-header">- <div class="workflows-canvas__ai-title-row">- <div class="workflows-canvas__ai-title">- <h3 id="workflow-ai-panel-title">- {{i18n "discourse_workflows.ai.title"}}- </h3>- <span- class={{dConcatClass- "workflows-canvas__ai-status"- this.aiStatusClass- }}- >- {{this.aiStatusLabel}}- </span>-- </div>- <p>{{i18n "discourse_workflows.ai.subtitle"}}</p>+ <div class="workflows-canvas__ai-title">+ <h3 id="workflow-ai-panel-title">+ {{i18n "discourse_workflows.ai.title"}}+ </h3>+ <span+ class={{dConcatClass+ "workflows-canvas__ai-status"+ this.aiStatusClass+ }}+ >+ {{this.aiStatusLabel}}+ </span></div><DButtonclass="btn-transparent workflows-canvas__ai-close"@@ -881,6 +892,13 @@ export default class BuildWithAiModal extends Component {</div><div class="workflows-canvas__ai-review-footer">+ {{#if this.aiProposalHasFailedScripts}}+ <DButton+ class="btn-default"+ @action={{this.fixAiProposal}}+ @translatedLabel={{i18n "discourse_workflows.ai.fix"}}+ />+ {{/if}}<DButtonclass="btn-primary"@action={{this.applyAiProposal}}@@ -923,6 +941,12 @@ export default class BuildWithAiModal extends Component {</div>{{/if}}+ {{#if this.aiShowingProgress}}+ <p aria-live="polite" class="workflows-canvas__ai-progress-current">+ {{this.aiProgressMessage this.aiLatestProgressEvent}}+ </p>+ {{/if}}+{{#if this.aiResponseError}}<divclass="workflows-canvas__ai-response workflows-canvas__ai-response--error"diff --git a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rbindex 6dc3a312db1..9ab30391f38 100644--- a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb+++ b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb@@ -49,15 +49,31 @@ module Jobsparsed = validate_patch_proposal(parsed)end- publish_progress(:summarizing)- persist_response(parsed)- publish_terminal(parsed)+ finish(parsed)rescue => e- publish_error(e.message)+ Discourse.warn_exception(e, message: "discourse-workflows: AI authoring failed")+ begin+ finish({ "status" => "error", "message" => e.message })+ rescue => finish_error+ # Still end the client's wait if recording the question fails too.+ Discourse.warn_exception(+ finish_error,+ message: "discourse-workflows: AI authoring failed",+ )+ publish_error(e.message)+ endendprivate+ def finish(parsed)+ parsed = ask_how_to_proceed(parsed) if parsed["status"] == "error"++ publish_progress(:summarizing)+ persist_response(parsed)+ publish_terminal(parsed)+ end+def find_agent_recordconfigured_agent_id = SiteSetting.discourse_workflows_workflow_authoring_agent.to_idefault_agent_id =@@ -112,17 +128,15 @@ module Jobsdef authoring_context_instructionscontext_tools = {workflow_node_catalog:- "Call this with targeted queries for node parameters, output contracts, capabilities, and examples.",+ "Call this with exact node types from the node menu for their parameters, output contracts, examples, and notes.",workflow_ai_agent_catalog:"Call this before adding action:ai_agent nodes to find existing enabled AI agents to reuse. If none fit, propose create_ai_agent with name, description, and system_prompt.",- workflow_graph_context:- "Call this with workflow_id when you need the current graph nodes and connections.",+ workflow_node_details:+ "Call this with node IDs from the workflow outline for parameters, connections, fields, and recorded output.",workflow_validate_patch:"Call this to dry-run candidate operations and inspect inferred node_fields.",- workflow_script_context:- "Call this before writing Code node JavaScript to inspect the runtime API and mode rules.",workflow_validate_script:- "Call this with exact Code node mode and code; repair any errors before returning a proposal.",+ "Call this to run Code node scripts against recorded workflow data or your own test data; test edge cases and repair errors before returning a proposal.",workflow_authoring_result:"Call this exactly once when authoring is complete; it is the final response.",}@@ -137,17 +151,16 @@ module Jobs] = "Call this before asking the admin to choose an external chat integration channel or before setting action:send_chat_integration_message channel_id. Use returned matches; never invent provider names, channel labels, or IDs."end- payload = {- workflow: workflow_summary,- trigger_author_field_facts: TRIGGER_AUTHOR_FIELD_FACTS,- context_tools: context_tools,- }+ payload = { workflow: workflow_summary, context_tools: context_tools }<<~TEXTImportant workflow field facts:#{TRIGGER_AUTHOR_FIELD_FACTS.map { |fact| "- #{fact}" }.join("\n")}- Compact workflow authoring context. Full graph and node catalog are intentionally not preloaded; use workflow_graph_context and workflow_node_catalog tools for details:+ Workflow outline:+ #{::DiscourseWorkflows::Ai::WorkflowOutline.text(@session.workflow)}++ Workflow authoring context:#{JSON.generate(payload)}TEXTend@@ -156,18 +169,9 @@ module Jobsworkflow = @session.workflowreturn nil if workflow.blank?- nodes = workflow.nodes || []- connection_count =- ::DiscourseWorkflows::WorkflowDocument.connection_records(- nodes,- workflow.connections || {},- ).length-{id: workflow.id,name: workflow.name,- node_count: nodes.length,- connection_count: connection_count,published: workflow.published?,has_unpublished_changes: workflow.has_unpublished_changes?,version_id: workflow.version_id,@@ -387,18 +391,9 @@ module Jobsoperations = normalized_patch_operations(proposal[:operations])proposal["operations"] = operationsvalidations = code_script_validations(operations)+ # Shown in review rather than failing the run; the admin decides whether to ask for a fix.proposal["script_validations"] = validations if validations.present?- if validations.any? { |validation| !validation["valid"] }- return(- parsed.merge(- "status" => "error",- "message" => invalid_script_message(validations),- "proposal" => json_safe(proposal),- )- )- end-parsed.merge("proposal" => json_safe(proposal))end@@ -440,6 +435,27 @@ module Jobsparsed.merge("proposal" => json_safe(proposal))end+ # Hand failures back to the admin as a question rather than ending the conversation.+ def ask_how_to_proceed(parsed)+ problem =+ ::DiscourseWorkflows::Ai::Truncation.text(+ parsed["message"].presence || I18n.t("discourse_workflows.ai.error_invalid_response"),+ 1_000,+ )+ parsed.merge(+ "status" => "needs_clarification",+ "questions" => [+ {+ "id" => "how_to_proceed",+ "question" =>+ I18n.t("discourse_workflows.ai.how_to_proceed.question", problem: problem),+ "options" => [{ "label" => I18n.t("discourse_workflows.ai.how_to_proceed.fix") }],+ "custom_allowed" => true,+ },+ ],+ )+ end+def code_scripts_present?(parsed)operations = normalized_patch_operations(proposal_for(parsed)[:operations])operations.any? do |operation|@@ -447,38 +463,22 @@ module Jobsendend- def invalid_script_message(validations)- details =- validations- .select { |validation| !validation["valid"] }- .flat_map do |validation|- node_name =- validation["node_name"].presence ||- I18n.t("discourse_workflows.ai.default_code_node_name")- Array.wrap(validation["errors"]).map { |error| "#{node_name}: #{error}" }- end- .join("; ")-- if details.present?- I18n.t("discourse_workflows.ai.error_invalid_script_with_errors", errors: details)- else- I18n.t("discourse_workflows.ai.error_invalid_script")- end- end-def code_script_validations(operations)operations.each_with_index.filter_map do |operation, index|script = code_script_for_operation(normalized_hash(operation))next if script.blank?- result = validate_code_script(script)+ errors =+ ::DiscourseWorkflows::Ai::Tools::WorkflowValidateScript.static_errors(+ script[:mode],+ script[:code],+ ){"operation_index" => index,"node_name" => script[:node_name],"mode" => script[:mode],- "valid" => result[:valid],- "errors" => result[:errors] || [],- "warnings" => result[:warnings] || [],+ "valid" => errors.empty?,+ "errors" => errors,}endend@@ -522,16 +522,6 @@ module Jobs}end- def validate_code_script(script)- context = ::DiscourseAi::Agents::BotContext.new(messages: [], user: @user)- ::DiscourseWorkflows::Ai::Tools::WorkflowValidateScript.new(- { mode: script[:mode], code: script[:code] },- bot_user: Discourse.system_user,- llm: nil,- context: context,- ).invoke- end-def code_mode(parameters, existing_parameters = {})(parameters[:mode].presence || existing_parameters["mode"].presence ||@@ -604,6 +594,8 @@ module Jobslatest_response: parsed,proposed_patch: parsed["proposal"] || {},risk_level: proposal_for(parsed)["risk_level"],+ # Lets follow-ups ("fix it", answers) build on what was proposed and why it failed.+ messages: @session.messages + [{ "type" => "model", "content" => parsed.to_json }],)enddiff --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.rbindex acff925ba24..6f03a622b5a 100644--- 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@@ -4,8 +4,8 @@ module DiscourseWorkflowsclass Workflow::Action::RunNodePreview < Service::ActionBaseMAX_ITEMS = 25- option :workflow_snapshot- option :node+ option :workflow_snapshot, optional: true+ option :node, optional: trueoption :node_type_classoption :preview_contextoption :parameters, optional: true@@ -20,6 +20,7 @@ module DiscourseWorkflowsrescue DiscourseWorkflows::NodeError,DiscourseWorkflows::JsSandbox::SandboxError,DiscourseWorkflows::JsSandbox::BudgetExceededError,+ DiscourseWorkflows::JsSandbox::PayloadTooLargeError,MiniRacer::Error => eerror_payload(e.message)rescue StandardError => e@@ -35,19 +36,18 @@ module DiscourseWorkflowsdef items@items ||=begin- pinned = workflow_snapshot.pinned_items_for(node)+ pinned = workflow_snapshot&.pinned_items_for(node) if nodepinned.presence || Array(preview_context["__input_items"])endenddef node_parameters- @node_parameters ||= (parameters || node.parameters || {}).deep_stringify_keys+ @node_parameters ||= (parameters || node&.parameters || {}).deep_stringify_keysenddef execute_node(resolver)- node_type_class.new(parameters: node_parameters, credentials: {}).execute(- execution_context(resolver),- )+ @execution_context = execution_context(resolver)+ node_type_class.new(parameters: node_parameters, credentials: {}).execute(@execution_context)enddef execution_context(resolver)@@ -71,12 +71,18 @@ module DiscourseWorkflows{input_count: items.length,outputs: ports.map { |port| port.first(MAX_ITEMS) },+ output_counts: ports.map(&:length),truncated: ports.any? { |port| port.length > MAX_ITEMS },+ log: log_entries,}enddef error_payload(message)- { input_count: items.length, outputs: [], error: message }+ { input_count: items.length, outputs: [], error: message, log: log_entries }+ end++ def log_entries+ @execution_context&.log&.as_json || []endendenddiff --git a/plugins/discourse-workflows/assets/stylesheets/common/canvas/ai-build-tool.scss b/plugins/discourse-workflows/assets/stylesheets/common/canvas/ai-build-tool.scssindex b22810ffe81..64541985bf4 100644--- a/plugins/discourse-workflows/assets/stylesheets/common/canvas/ai-build-tool.scss+++ b/plugins/discourse-workflows/assets/stylesheets/common/canvas/ai-build-tool.scss@@ -1,3 +1,5 @@+@use "lib/viewport";+.workflows-canvas {--workflows-canvas-ai-panel-top: calc(var(--space-2) + 3rem);--workflows-canvas-ai-panel-viewport-offset: 18rem;@@ -43,6 +45,21 @@border-radius: var(--d-border-radius);background: var(--secondary);box-shadow: 0 10px 32px rgb(0, 0, 0, 0.16);++ // No room beside the assistant on narrow screens; the latest step is shown inline instead.+ @include viewport.until(md) {+ display: none;+ }+ }++ &__ai-progress-current {+ margin: 0;+ color: var(--primary-medium);+ font-size: var(--font-down-1);++ @include viewport.from(md) {+ display: none;+ }}&__ai-progress-title {@@ -76,14 +93,15 @@&__ai-panel-header {display: flex;- align-items: flex-start;+ align-items: center;justify-content: space-between;gap: var(--space-3);}- &__ai-title-row {+ &__ai-title {display: flex;- flex-direction: column;+ gap: var(--space-2);+ align-items: center;min-width: 0;h3 {@@ -92,31 +110,6 @@font-size: var(--font-up-1);line-height: 1.15;}-- p {- margin: var(--space-half) 0 0;- color: var(--primary-medium);- font-size: var(--font-down-2);- line-height: 1.25;- }- }-- &__ai-title {- display: flex;- gap: var(--space-2);- align-items: center;- }-- &__ai-title-icon {- display: flex;- flex: 0 0 auto;- align-items: center;- justify-content: center;- width: 2.25rem;- height: 2.25rem;- border-radius: 50%;- background: var(--tertiary-low);- color: var(--tertiary);}&__ai-close {@@ -213,6 +206,8 @@&__ai-review-footer {display: flex;flex: 0 0 auto;+ flex-wrap: wrap;+ gap: var(--space-2);justify-content: flex-end;padding-top: var(--space-3);border-top: 1px solid var(--primary-low);diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex 15f6678ace8..e320d8ed93e 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -53,14 +53,13 @@ en:ai:button: "Build with AI"title: "Workflow assistant"- subtitle: "Create automatic drafts. Publishing stays manual."close: "Close workflow assistant"- description: "Describe a change, then generate a draft to review. Publishing stays manual."- review_description: "Review this draft, then apply it when it looks right."prompt_label: "What should change?"prompt_placeholder: "Example: When a new support topic is created, tag it needs-review and reply with a checklist."generate: "Generate draft"apply: "Apply draft"+ fix: "Ask assistant to fix"+ fix_message: "The syntax or mode checks failed for a Code node script in your last proposal. Fix the scripts and propose the change again."back_to_prompt: "Edit request"start_over: "Start over"applied: "AI workflow proposal applied to the draft. Publish when you're ready."@@ -84,8 +83,8 @@ en:code_node: "Code node"script_preview: "Code script preview"script_mode: "Mode: %{mode}"- script_validation_passed: "Script validation passed"- script_validation_failed: "Script validation failed"+ script_validation_passed: "Syntax and mode checks passed"+ script_validation_failed: "Syntax or mode checks failed"progress_title: "Progress"progress:queued: "Workflow authoring request queued"diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.ymlindex e01f2f861f4..4b45e8e0da7 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -13,8 +13,6 @@ en:error_invalid_mode: "Invalid AI workflow authoring mode."error_invalid_patch: "The AI proposal did not pass workflow validation: %{errors}"error_invalid_response: "AI returned an invalid response."- error_invalid_script: "The AI generated Code node JavaScript that did not pass validation. Regenerate the proposal or ask the assistant to fix the script."- error_invalid_script_with_errors: "The AI generated Code node JavaScript that did not pass validation: %{errors}. Regenerate the proposal or ask the assistant to fix the script."error_message_required: "Enter a prompt for the workflow assistant."error_message_too_long: "The workflow assistant prompt is too long."error_no_operations: "The AI response did not include any workflow patch operations."@@ -22,6 +20,9 @@ en:error_proposal_not_ready: "This AI proposal is not ready to apply."error_stale_proposal: "The workflow changed after this AI proposal was generated. Regenerate the proposal."clarification_message: "Choose answers so the assistant can continue drafting the workflow."+ how_to_proceed:+ question: "%{problem} How should the assistant proceed?"+ fix: "Fix the problems and try again"tool_call_proposal_message: "The assistant drafted a workflow patch and the server validated it."tool_call_proposal_summary:one: "The assistant drafted one workflow patch operation. Review it before applying."@@ -246,41 +247,38 @@ en:name: "Workflow author"description: "Builds and edits Discourse workflows from natural language"tool_help:- workflow_node_catalog: "Lists available workflow nodes and their configuration schema"+ workflow_node_catalog: "Returns the configuration schema of workflow node types"workflow_ai_agent_catalog: "Lists existing Discourse AI agents that workflow drafts can reuse"- workflow_graph_context: "Describes the current workflow graph"+ workflow_node_details: "Describes nodes in the current workflow, including recorded output"workflow_validate_patch: "Dry-runs and validates workflow patch operations"workflow_ask_questions: "Asks the workflow admin to choose clarification answers"workflow_authoring_result: "Returns the final workflow authoring result"workflow_resolve_entity: "Resolves Discourse names to workflow parameter values"search_chat_channels: "Searches chat channel names and IDs for workflow parameters"search_chat_integration_channels: "Searches external chat integration channel labels and IDs for workflow parameters"- workflow_script_context: "Returns context for writing workflow Code node JavaScript"- workflow_validate_script: "Validates workflow Code node JavaScript"+ workflow_validate_script: "Runs workflow Code node JavaScript with test data"tool_summary:workflow_node_catalog: "Reading workflow node catalog"workflow_ai_agent_catalog: "Reading AI agent catalog"- workflow_graph_context: "Reading workflow graph"+ workflow_node_details: "Reading workflow nodes"workflow_validate_patch: "Validating workflow patch"workflow_ask_questions: "Asking clarification questions"workflow_authoring_result: "Returning workflow authoring result"workflow_resolve_entity: "Resolving Discourse entities"search_chat_channels: "Searching chat channels"search_chat_integration_channels: "Searching external chat integration channels"- workflow_script_context: "Loading script context"- workflow_validate_script: "Validating script"+ workflow_validate_script: "Testing script"tool_description:workflow_node_catalog: "Read workflow node catalog"workflow_ai_agent_catalog: "Read AI agent catalog"- workflow_graph_context: "Read workflow graph"+ workflow_node_details: "Read workflow nodes"workflow_validate_patch: "Validated workflow patch"workflow_ask_questions: "Asked clarification questions"workflow_authoring_result: "Returned workflow authoring result"workflow_resolve_entity: "Resolved Discourse entities"search_chat_channels: "Searched chat channels"search_chat_integration_channels: "Searched external chat integration channels"- workflow_script_context: "Loaded script context"- workflow_validate_script: "Validated script"+ workflow_validate_script: "Tested script"reviewables:reasons:workflow_flagged_user: "A workflow added this user to the review queue."diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/node_menu.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/node_menu.rbnew file mode 100644index 00000000000..88cfdb771d1--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/node_menu.rb@@ -0,0 +1,35 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Ai+ # The node types the authoring agent can choose from, preloaded into its prompt.+ module NodeMenu+ MAX_DESCRIPTION_LENGTH = 160+ # Beyond this the menu costs too much prompt to preload on every request.+ MAX_LENGTH = 16_000++ def self.text(node_classes = available_node_classes)+ node_classes+ .uniq(&:identifier)+ .sort_by(&:identifier)+ .map { |node_class| line(node_class) }+ .join("\n")+ end++ def self.available_node_classes+ DiscourseWorkflows::Registry.nodes.select do |node_class|+ node_class.palette_visible? && node_class.available?+ end+ end++ def self.line(node_class)+ type = node_class.identifier+ description =+ I18n.t("js.discourse_workflows.node_descriptions.#{type}", locale: :en, default: "")+ return "- #{type}" if description.blank?++ "- #{type}: #{Truncation.text(description, MAX_DESCRIPTION_LENGTH)}"+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/sample.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/sample.rbnew file mode 100644index 00000000000..3b10183fb57--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/sample.rb@@ -0,0 +1,62 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Ai+ # Shrinks workflow items shown to the authoring agent and marks every cut, so the agent+ # never mistakes a sample for the complete data.+ class Sample+ NOTE =+ "Sample truncated: compare items with total_items; shortened values are marked [truncated, ...]."+ MAX_ARRAY_LENGTH = 10++ # ports: one array of items per output. totals: real item counts when ports were capped earlier.+ def self.call(ports, totals: nil, max_items: 3, max_chars: 4_000, max_string_length: 500)+ new(max_string_length).call(ports, totals, max_items, max_chars)+ end++ def initialize(max_string_length)+ @max_string_length = max_string_length+ @truncated = false+ end++ def call(ports, totals, max_items, max_chars)+ ports = Array(ports).map { |items| Array(items) }+ totals ||= ports.map(&:length)+ outputs = nil++ [max_items, 1, 0].uniq.each do |limit|+ outputs =+ ports.each_with_index.map do |items, index|+ shown = items.first(limit)+ @truncated ||= totals[index].to_i > shown.length+ { total_items: totals[index].to_i, items: shown.map { |item| trim(item) } }+ end+ break if outputs.to_json.length <= max_chars+ end++ { outputs: outputs, truncated: (NOTE if @truncated) }.compact+ end++ private++ def trim(value)+ case value+ when String+ @truncated ||= value.length > @max_string_length+ Truncation.text(value, @max_string_length)+ when Array+ trimmed = value.first(MAX_ARRAY_LENGTH).map { |element| trim(element) }+ if value.length > MAX_ARRAY_LENGTH+ @truncated = true+ trimmed << "[truncated, #{value.length} items total]"+ end+ trimmed+ when Hash+ value.transform_values { |element| trim(element) }+ else+ value+ end+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_graph_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_graph_context.rbdeleted file mode 100644index d373075f5b6..00000000000--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_graph_context.rb+++ /dev/null@@ -1,94 +0,0 @@-# frozen_string_literal: true--module DiscourseWorkflows- module Ai- module Tools- class WorkflowGraphContext < Base- def self.signature- {- name: name,- description:- "Returns the current draft workflow graph in a compact form for AI workflow authoring.",- parameters: [- {- name: "workflow_id",- description: "The workflow ID. Omit when creating a new workflow.",- type: "integer",- required: false,- },- ],- }- end-- def self.name- "workflow_graph_context"- end-- def invoke- return not_allowed_response if !ensure_can_manage_workflows!-- workflow_id = parameters[:workflow_id].presence- return empty_workflow_context if workflow_id.blank?-- workflow = DiscourseWorkflows::Workflow.find_by(id: workflow_id)- return error_response("Workflow not found") if workflow.blank?-- nodes = workflow.nodes || []- connections =- DiscourseWorkflows::WorkflowDocument.connection_records(- nodes,- workflow.connections || {},- )-- {- status: "success",- workflow: {- id: workflow.id,- name: workflow.name,- published: workflow.published?,- has_unpublished_changes: workflow.has_unpublished_changes?,- version_id: workflow.version_id,- active_version_id: workflow.active_version_id,- graph_digest: DiscourseWorkflows::Ai::GraphDigest.call(workflow),- },- nodes: nodes.map { |node| serialize_node(node) },- connections: connections.map { |connection| serialize_connection(connection, nodes) },- }- end-- private-- def empty_workflow_context- { status: "success", workflow: nil, nodes: [], connections: [] }- end-- def serialize_node(node)- {- id: node["id"],- name: node["name"],- type: node["type"],- type_version: node["typeVersion"],- parameters: DiscourseWorkflows::NodeData.parameters(node),- position: node["position"],- }- end-- def serialize_connection(connection, nodes)- nodes_by_id = nodes.index_by { |node| node["id"].to_s }- source = nodes_by_id[connection["source_node_id"].to_s]- target = nodes_by_id[connection["target_node_id"].to_s]-- {- from_node_id: connection["source_node_id"],- from_node_name: source&.dig("name"),- to_node_id: connection["target_node_id"],- to_node_name: target&.dig("name"),- connection_type: connection["connection_type"],- output_index: connection["source_output_index"],- input_index: connection["target_input_index"],- }- end- end- end- end-enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rbindex 8b234f712d9..4ac8ace55a1 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb@@ -264,75 +264,37 @@ module DiscourseWorkflows],}.freeze- QUERY_STOP_WORDS = %w[- node- nodes- trigger- triggers- action- actions- condition- conditions- flow- ].freeze+ MAX_TYPES = 10- SEARCH_ALIASES = {- "action:send_personal_message" => "dm direct message pm personal private message",- "action:send_chat_message" => "chat channel dm direct message",- "action:send_chat_integration_message" =>- "external chat integration notification slack discord telegram mattermost matrix zulip rocket chat gitter groupme teams power automate webex google chat guilded",- "action:ai_agent" =>- "ai agent bot llm classify summarize generate sentiment triage runner run as permissions uploads attachments",- "action:group" => "group membership member belongs friend friends",- "action:tag_group" => "tag group tags taxonomy add remove organize",- "action:site_setting" =>- "site setting settings configuration config admin toggle enable disable change update value",- "action:flag_user" =>- "flag user report spammer spam suspect review queue moderation approve reject account signup",- "action:post_like" => "like unlike heart favorite upvote",- "action:post_reaction" => "react reaction emoji like heart",- "action:boost" => "boost micro reaction short note emoji",- "trigger:post_like_changed" => "liked unliked like heart favorite upvote",- "trigger:post_reaction_changed" => "reacted reaction emoji",- "trigger:post_boost_changed" => "boosted boost micro reaction",- "trigger:user_added_to_group" => "joined added to group membership member",- "trigger:user_removed_from_group" => "left removed from group membership member",- "trigger:user_created" => "signup sign up register registration new account joined site",- "trigger:user_updated" => "profile edited changed avatar name username email account",- "trigger:reviewable_created" =>- "review queue flag flagged spam moderation pending needs approval queued post akismet",- "trigger:badge_granted" => "badge award achievement medal granted earned",- "trigger:tag_created" => "taxonomy label keyword created new",- "trigger:post_destroyed" => "post deleted removed destroyed trashed",- "trigger:post_recovered" => "post recovered restored undeleted untrashed",- "action:user" =>- "user profile bio title trust level lock groups fields lookup edit update",- "action:flag_post" =>- "flag spam moderation review queue reviewable hide delete silence triage report",- "action:topic_category" => "category recategorize move topic uncategorized",- "action:topic_moderation" =>- "moderate moderation unlist unlisted topic visibility invisible",+ NOTES = {+ "action:code" => [+ "The default value of the code property documents the runtime variables: $input, $json, $(\"Node name\"), $vars, $site_settings, $execution, $current_user, $helpers and console.",+ "runOnceForAllItems must return an array of items such as [{ json: { ... } }]; plain objects are normalized under json.",+ "runOnceForEachItem must return one object per input item, not an array, and cannot use $input.first, $input.last, $input.all or $input.itemMatching.",+ "Output items may use the reserved keys json, pairedItem, error and index; do not mix them with plain fields.",+ ],}.freezedef self.signature{name: name,description:- "Lists available Discourse workflow node types with versions, parameters, capabilities, output contracts, and examples.",- parameters: [- {- name: "query",- description: "Optional search terms for node type, name, group, or parameter names",- type: "string",- required: false,- },- {- name: "include_examples",- description: "Whether to include curated examples for matching nodes",- type: "boolean",- required: false,+ "Returns the full definition of the given node types: version, parameters, credentials, capabilities, output contracts, examples and notes. Pick types from the node menu in your instructions.",+ json_schema: {+ type: "object",+ additionalProperties: false,+ required: %w[types],+ properties: {+ types: {+ type: "array",+ description:+ "Exact node type identifiers, such as action:code or trigger:post_created.",+ items: {+ type: "string",+ },+ },},- ],+ },}end@@ -343,61 +305,44 @@ module DiscourseWorkflowsdef invokereturn not_allowed_response if !ensure_can_manage_workflows!- query_terms = self.class.query_terms(parameters[:query])- include_examples =- parameters[:include_examples].to_s == "true" || parameters[:include_examples] == true-- nodes =- DiscourseWorkflows::Registry.nodes.filter_map do |node_class|- next if !node_class.palette_visible?-- serialize_node(node_class, query_terms, include_examples)- end+ requested = Array.wrap(parameters[:types]).map(&:to_s).uniq+ types = requested.first(MAX_TYPES)+ node_classes =+ types.index_with { |type| DiscourseWorkflows::Registry.latest_node_type(type) }+ node_classes.select! { |_type, node_class| node_class&.palette_visible? }- { status: "success", nodes: nodes }- end-- def self.query_terms(query)- query- .to_s- .downcase- .scan(/[a-z0-9_:-]+/)- .map { |term| term.delete_prefix("type:") }- .reject { |term| QUERY_STOP_WORDS.include?(term) }- .uniq+ result = {+ status: "success",+ nodes: node_classes.values.map { |node_class| serialize_node(node_class) },+ unknown_types: types - node_classes.keys,+ }+ if requested.length > MAX_TYPES+ result[+ :truncated+ ] = "Only the first #{MAX_TYPES} of #{requested.length} requested types are included."+ end+ resultendprivate- def serialize_node(node_class, query_terms, include_examples)- description = node_class.description+ def serialize_node(node_class)identifier = node_class.identifier- properties = json_safe(node_class.properties || {})- group = description[:group].to_s.presence- haystack = [- identifier,- group,- properties.keys.join(" "),- SEARCH_ALIASES[identifier],- ].compact.join(" ").downcase- return if query_terms.present? && query_terms.none? { |term| haystack.include?(term) }-- payload = {+ {type: identifier,version: node_class.version,- group: group,+ group: node_class.description[:group].to_s.presence,available: node_class.available?,- palette_visible: node_class.palette_visible?,inputs: json_safe(node_class.inputs),outputs: json_safe(node_class.outputs),- properties: properties,+ properties: json_safe(node_class.properties || {}),credentials: json_safe(node_class.credentials),- capabilities: json_safe(description[:capabilities] || {}),+ capabilities: json_safe(node_class.description[:capabilities] || {}),output_contracts: serialized_output_contracts(node_class),- }- payload[:examples] = EXAMPLES[identifier] if include_examples && EXAMPLES.key?(identifier)- payload+ examples: EXAMPLES[identifier],+ notes: NOTES[identifier],+ }.compactenddef serialized_output_contracts(node_class)diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_details.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_details.rbnew file mode 100644index 00000000000..d12e4409e35--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_details.rb@@ -0,0 +1,145 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Ai+ module Tools+ class WorkflowNodeDetails < Base+ MAX_NODES = 10++ def self.signature+ {+ name: name,+ description:+ "Returns full details for nodes in the current workflow: parameters (including Code node scripts), connections, input/output fields, and a truncated sample of recorded output from pinned data or the last successful execution.",+ json_schema: {+ type: "object",+ additionalProperties: false,+ required: %w[workflow_id node_ids],+ properties: {+ workflow_id: {+ type: "integer",+ description: "Workflow ID.",+ },+ node_ids: {+ type: "array",+ description: "Node IDs or names from the workflow outline, at most #{MAX_NODES}.",+ items: {+ type: "string",+ },+ },+ },+ },+ }+ end++ def self.name+ "workflow_node_details"+ end++ def invoke+ return not_allowed_response if !ensure_can_manage_workflows!++ workflow = DiscourseWorkflows::Workflow.find_by(id: parameters[:workflow_id])+ return error_response("Workflow not found") if workflow.blank?++ @workflow = workflow+ refs = Array.wrap(parameters[:node_ids]).map(&:to_s).uniq+ nodes_by_ref = refs.first(MAX_NODES).index_with { |ref| find_node(ref) }++ result = {+ status: "success",+ nodes: nodes_by_ref.values.compact.map { |node| node_details(node) },+ not_found: nodes_by_ref.select { |_ref, node| node.nil? }.keys,+ }+ if refs.length > MAX_NODES+ result[+ :truncated+ ] = "Only the first #{MAX_NODES} of #{refs.length} requested nodes are included."+ end+ result+ end++ private++ def find_node(ref)+ nodes = @workflow.nodes || []+ nodes.find { |node| node["id"].to_s == ref } || nodes.find { |node| node["name"] == ref }+ end++ def node_details(node)+ node_id = node["id"].to_s+ {+ id: node_id,+ name: node["name"],+ type: node["type"],+ type_version: node["typeVersion"],+ parameters: DiscourseWorkflows::NodeData.parameters(node),+ incoming: connections_where("target_node_id", node_id, "source_node_id"),+ outgoing: connections_where("source_node_id", node_id, "target_node_id"),+ input_fields: expression_fields(graph[:input_schemas][node_id]),+ output_fields: expression_fields(graph[:output_schemas][node_id]),+ recorded_output: recorded_output(node),+ }+ end++ def connections_where(key, node_id, other_key)+ graph[:connection_records]+ .select { |connection| connection[key].to_s == node_id }+ .map do |connection|+ other_id = connection[other_key].to_s+ {+ node_id: other_id,+ node_name: find_node(other_id)&.dig("name"),+ output_index: connection["source_output_index"].to_i,+ input_index: connection["target_input_index"].to_i,+ connection_type: connection["connection_type"].to_s.presence || "main",+ }+ end+ end++ def graph+ @graph ||=+ DiscourseWorkflows::Schema.resolve_graph(@workflow.nodes, @workflow.connections)+ end++ def expression_fields(schemas)+ Array(schemas).map do |schema|+ DiscourseAi::WorkflowSchemaFields+ .convert(schema)+ .transform_keys { |path| path.start_with?("[") ? "$json#{path}" : "$json.#{path}" }+ end+ end++ def recorded_output(node)+ pinned = @workflow.node_pin_data(node["name"])+ return { source: "pinned", **Sample.call([pinned]) } if pinned.present?++ execution_id = preview_context.dig("__execution", "id").to_i+ if execution_id.zero?+ return { source: "none", note: "The workflow has no successful execution yet." }+ end++ run = Array(preview_context.dig("__node_runs", node["name"])).last+ if run.blank?+ return(+ {+ source: "none",+ note:+ "This node has no successful run in the last successful execution (##{execution_id}): it was skipped or failed, so no sample exists. This is different from running and producing no items.",+ }+ )+ end++ { source: "execution", execution_id: execution_id, **Sample.call(run["outputs"]) }+ end++ def preview_context+ @preview_context ||=+ DiscourseWorkflows::Workflow::Action::BuildExpressionPreviewContext.call(+ workflow: @workflow,+ )+ end+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_script_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_script_context.rbdeleted file mode 100644index 001a3c18a18..00000000000--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_script_context.rb+++ /dev/null@@ -1,220 +0,0 @@-# frozen_string_literal: true--module DiscourseWorkflows- module Ai- module Tools- class WorkflowScriptContext < Base- AVAILABLE_VARIABLES = {- "$input.item.json" => "Current item's JSON data",- "$input.all()" => "Array of all input items; only valid in runOnceForAllItems mode",- "$json" => "Shortcut for $input.item.json",- "$(\"NodeName\").item" => "Paired item from another node",- "$vars.KEY" => "Workflow variables",- "$site_settings.NAME" => "Site settings",- "$execution" => "Execution metadata",- "$current_user" => "User running the workflow",- "console.log" => "Workflow execution log",- }.freeze-- RETURN_CONTRACT = {- "runOnceForAllItems" =>- "Return an array of items. Each item should be { json: { ... } } or a plain object that can be normalized.",- "runOnceForEachItem" =>- "Return one object per input item. Do not return an array in this mode.",- "reserved_top_level_keys" => %w[json pairedItem error index],- }.freeze-- MODE_RESTRICTIONS = {- "runOnceForEachItem" => {- "disallowed_input_methods" => %w[- $input.first- $input.last- $input.all- $input.itemMatching- ],- },- }.freeze-- EXAMPLES = [- {- name: "Add a derived field to each item",- mode: "runOnceForAllItems",- code:- "var items = $input.all();\n" \- "items.forEach(function(item) {\n" \- " item.json.summary = item.json.topic && item.json.topic.title;\n" \- "});\n" \- "return items;",- },- {- name: "Build one Markdown reply from all items",- mode: "runOnceForAllItems",- code:- "var lines = $input.all().map(function(item) {\n" \- " return '- ' + item.json.title;\n" \- "});\n" \- "return [{ json: { raw: lines.join('\\n') } }];",- },- ].freeze-- def self.signature- {- name: name,- description:- "Returns the runtime, input, output, sample data, and return-contract context needed to write a workflow Code node script.",- parameters: [- { name: "workflow_id", description: "Workflow ID", type: "integer", required: false },- {- name: "target_node_id",- description: "Existing Code node ID when editing a script",- type: "string",- required: false,- },- {- name: "upstream_node_id",- description: "Node ID that feeds the Code node",- type: "string",- required: false,- },- {- name: "downstream_node_id",- description: "Node ID that consumes the Code node output",- type: "string",- required: false,- },- {- name: "sample_limit",- description: "Maximum number of pinned sample items to return",- type: "integer",- required: false,- },- ],- }- end-- def self.name- "workflow_script_context"- end-- def invoke- return not_allowed_response if !ensure_can_manage_workflows!-- workflow = find_workflow- if parameters[:workflow_id].present? && workflow.blank?- return error_response("Workflow not found")- end-- @workflow_for_shape = workflow- nodes = workflow&.nodes || []- target_node = find_node(nodes, parameters[:target_node_id])- upstream_node = find_node(nodes, parameters[:upstream_node_id])- downstream_node = find_node(nodes, parameters[:downstream_node_id])-- {- status: "success",- runtime: runtime,- available_variables: AVAILABLE_VARIABLES,- return_contract: RETURN_CONTRACT,- mode_restrictions: MODE_RESTRICTIONS,- upstream_fields: node_fields(upstream_node),- downstream_requirements: downstream_requirements(downstream_node),- sample_input_items: sample_input_items(workflow, upstream_node),- existing_code: existing_code(target_node),- examples: EXAMPLES,- }- end-- private-- def find_workflow- workflow_id = parameters[:workflow_id].presence- return nil if workflow_id.blank?-- DiscourseWorkflows::Workflow.find_by(id: workflow_id)- end-- def find_node(nodes, node_id)- return nil if node_id.blank?-- nodes.find { |node| node["id"].to_s == node_id.to_s }- end-- def runtime- {- language: "javascript",- node_type: "action:code",- version: "1.0",- modes: %w[runOnceForAllItems runOnceForEachItem],- default_mode: "runOnceForAllItems",- }- end-- def node_fields(node)- return nil if node.blank?-- schemas = infer_schema_from_pin_data(node)- schemas =- workflow_schema_resolution[:output_schemas][node["id"].to_s] || [] if schemas.nil?- output_fields = schemas.map { |schema| DiscourseAi::WorkflowSchemaFields.convert(schema) }-- {- node_id: node["id"],- node_name: node["name"],- node_type: node["type"],- output_fields: output_fields,- }- end-- def workflow_schema_resolution- @workflow_schema_resolution ||=- DiscourseWorkflows::Schema.resolve_graph(- @workflow_for_shape&.nodes,- @workflow_for_shape&.connections,- )- end-- def infer_schema_from_pin_data(node)- pin_data = @workflow_for_shape&.node_pin_data(node["name"])- return nil if pin_data.blank?-- sample = pin_data.first- [DiscourseWorkflows::Schema.infer(sample&.dig("json") || {})]- end-- def downstream_requirements(node)- return nil if node.blank?-- node_type =- DiscourseWorkflows::Registry.find_node_type(node["type"], version: node["typeVersion"])- properties = node_type&.properties || {}- required_fields =- properties.each_with_object({}) do |(name, property), result|- next unless property[:required]-- result[name.to_s] = property[:type].to_s- end-- {- node_id: node["id"],- node_name: node["name"],- node_type: node["type"],- required_fields: required_fields,- }- end-- def sample_input_items(workflow, upstream_node)- return [] if workflow.blank? || upstream_node.blank?-- limit = parameters[:sample_limit].presence&.to_i || 5- Array.wrap(workflow.node_pin_data(upstream_node["name"])).first(limit)- end-- def existing_code(node)- return nil if node.blank? || node["type"] != "action:code"-- DiscourseWorkflows::NodeData.parameters(node)["code"]- end- end- end- end-enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_validate_script.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_validate_script.rbindex 803d2c02c7d..68f23c14022 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_validate_script.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_validate_script.rb@@ -8,25 +8,74 @@ module DiscourseWorkflowsRUN_ONCE_FOR_EACH_ITEM = "runOnceForEachItem"MODES = [RUN_ONCE_FOR_ALL_ITEMS, RUN_ONCE_FOR_EACH_ITEM].freezeDISALLOWED_FOR_EACH_METHODS = %w[first last all itemMatching].freeze- RESERVED_ITEM_KEYS = %w[json pairedItem error index].freeze- MAX_SAMPLE_ITEMS = 20+ MAX_INPUT_ITEMS = 20+ MAX_OUTPUT_ITEMS = 5+ MAX_OUTPUT_CHARS = 12_000+ MAX_OUTPUT_STRING_LENGTH = 4_000+ MAX_LOG_ENTRIES = 20+ MAX_LOG_LENGTH = 1_000MAX_CODE_BYTES = 20_000def self.signature{name: name,description:- "Validates JavaScript generated for a Discourse workflow Code node against syntax, mode restrictions, and return shape.",- parameters: [- { name: "mode", description: "Code node mode", type: "string", required: true },- { name: "code", description: "JavaScript source", type: "string", required: true },- {- name: "sample_input_items",- description: "Optional sample input items to execute the script against",- type: "array",- required: false,+ "Runs Code node JavaScript in the production workflow sandbox and returns a sample of output items, console logs, and errors. Use it to test scripts while writing them, including edge cases. With workflow_id, $(\"Node name\") defaults to data recorded by the workflow's last successful execution, and $input to the recorded input of node_id or the recorded output of input_node; input_items and node_outputs replace that with your own test data.",+ json_schema: {+ type: "object",+ additionalProperties: false,+ required: %w[mode code],+ properties: {+ mode: {+ type: "string",+ description: MODES.join(" or "),+ },+ code: {+ type: "string",+ description: "JavaScript source",+ },+ workflow_id: {+ type: "integer",+ description: "Workflow whose last successful execution supplies default data.",+ },+ node_id: {+ type: "string",+ description:+ "Existing Code node ID; its recorded input becomes the default $input.",+ },+ input_node: {+ type: "string",+ description:+ "Name of the node a new Code node will follow; its recorded output becomes the default $input.",+ },+ input_output_index: {+ type: "integer",+ description:+ "Output of input_node the new Code node connects to, such as 1 for a condition's false branch. Defaults to 0.",+ },+ input_items: {+ type: "array",+ description:+ "Test items for $input, such as [{ \"json\": { ... } }]; [] tests empty input. At most #{MAX_INPUT_ITEMS}.",+ items: {+ type: "object",+ additionalProperties: true,+ },+ },+ node_outputs: {+ type: "object",+ description:+ "Test output items keyed by node name, read via $(\"Node name\").first() or .all(). $(\"Node name\").item needs recorded paired items, so it only works with recorded data.",+ additionalProperties: {+ type: "array",+ items: {+ type: "object",+ additionalProperties: true,+ },+ },+ },},- ],+ },}end@@ -34,160 +83,145 @@ module DiscourseWorkflows"workflow_validate_script"end- def invoke- return not_allowed_response if !ensure_can_manage_workflows!-- mode = parameters[:mode].to_s- code = parameters[:code].to_s- sample_input_items = Array.wrap(parameters[:sample_input_items]).first(MAX_SAMPLE_ITEMS)+ # Checks that need no data, so they can run on proposals without knowing what the agent tested with.+ def self.static_errors(mode, code)errors = []- warnings = []-errors << "Mode must be one of #{MODES.join(", ")}" if MODES.exclude?(mode)errors << "Code is required" if code.blank?errors << "Code exceeds #{MAX_CODE_BYTES} bytes" if code.bytesize > MAX_CODE_BYTES- errors.concat(disallowed_method_errors(code)) if mode == RUN_ONCE_FOR_EACH_ITEM-- return invalid_response(errors, warnings) if errors.present?-- sample_output_items = execute_sample(code, mode, sample_input_items)- validate_output_shape(sample_output_items, mode, errors, warnings)+ if mode == RUN_ONCE_FOR_EACH_ITEM+ code+ .scan(/\$input\.(#{DISALLOWED_FOR_EACH_METHODS.join("|")})\b/)+ .flatten+ .uniq+ .each do |method|+ errors << "$input.#{method} is only available in #{RUN_ONCE_FOR_ALL_ITEMS} mode"+ end+ end+ errors << syntax_error(code) if errors.empty?+ errors.compact+ end- {- status: "success",- valid: errors.empty?,- errors: errors,- warnings: warnings,- sample_output_items: errors.empty? ? sample_output_items : [],- }+ def self.syntax_error(code)+ js = MiniRacer::Context.new(timeout: 100)+ js.eval("new Function(#{code.to_json})")+ nilrescue MiniRacer::Error => e- invalid_response(["JavaScript execution error: #{e.message}"], [])+ "JavaScript syntax error: #{e.message}"+ ensure+ js&.disposeend- private+ def invoke+ return not_allowed_response if !ensure_can_manage_workflows!- def invalid_response(errors, warnings)- {- status: "success",- valid: false,- errors: errors,- warnings: warnings,- sample_output_items: [],- }- end+ mode = parameters[:mode].to_s+ code = parameters[:code].to_s+ errors = self.class.static_errors(mode, code)+ return { status: "success", valid: false, errors: errors } if errors.present?- def disallowed_method_errors(code)- code- .scan(/\$input\.(#{DISALLOWED_FOR_EACH_METHODS.join("|")})\b/)- .flatten- .uniq- .map { |method| "$input.#{method} is only available in #{RUN_ONCE_FOR_ALL_ITEMS} mode" }- end+ workflow = DiscourseWorkflows::Workflow.find_by(id: parameters[:workflow_id])+ if parameters[:workflow_id].present? && workflow.blank?+ return error_response("Workflow not found")+ end- def execute_sample(code, mode, sample_input_items)- context = MiniRacer::Context.new(timeout: 100, max_memory: 10.megabytes)- context.eval("var __sampleInputItems = #{sample_items_json(sample_input_items)};")- context.eval(runtime_js(mode))- context.eval("(function() {\n#{code}\n}).call({});")- ensure- context&.dispose- end+ @notes = []+ preview_context = preview_context(workflow)+ return error_response(@input_error) if @input_error++ run =+ DiscourseWorkflows::Workflow::Action::RunNodePreview.call(+ node_type_class: DiscourseWorkflows::Nodes::Code::V1,+ preview_context: preview_context,+ parameters: {+ "mode" => mode,+ "code" => code,+ },+ user: context.user,+ )+ output =+ DiscourseWorkflows::Ai::Sample.call(+ run[:outputs].first(1),+ totals: run[:output_counts]&.first(1),+ max_items: MAX_OUTPUT_ITEMS,+ max_chars: MAX_OUTPUT_CHARS,+ max_string_length: MAX_OUTPUT_STRING_LENGTH,+ )+ @notes << output[:truncated] if output[:truncated]- def sample_items_json(sample_input_items)- items = sample_input_items.presence || [{ "json" => {} }]- JSON.generate(items)+ {+ status: "success",+ valid: run[:error].blank?,+ errors: Array(run[:error]),+ input_count: run[:input_count],+ output: output[:outputs].first,+ logs: logs(Array(run[:log])),+ truncated: @notes.join(" ").presence,+ }.compactend- def runtime_js(mode)- <<~JS- var __mode = #{mode.to_json};- var __currentItem = __sampleInputItems[0] || { json: {} };- var $input = Object.freeze({- item: __currentItem,- all: function() { return __sampleInputItems; },- first: function() { return __sampleInputItems[0] || { json: {} }; },- last: function() { return __sampleInputItems[__sampleInputItems.length - 1] || { json: {} }; },- itemMatching: function(index) { return __sampleInputItems[index] || { json: {} }; }- });- var $json = __currentItem.json || {};- var $vars = Object.freeze({});- var $site_settings = Object.freeze({});- var $execution = Object.freeze({});- var $current_user = Object.freeze({});- var console = Object.freeze({- log: function() {},- info: function() {},- warn: function() {},- error: function() {}- });- function $(name) {- return Object.freeze({- item: { json: {} },- all: function() { return []; },- first: function() { return { json: {} }; },- last: function() { return { json: {} }; }- });- }- JS- end+ private- def validate_output_shape(output, mode, errors, warnings)- if mode == RUN_ONCE_FOR_EACH_ITEM- validate_each_item_output(output, errors, warnings)- else- validate_all_items_output(output, errors, warnings)+ def preview_context(workflow)+ context =+ DiscourseWorkflows::Workflow::Action::BuildExpressionPreviewContext.call(+ workflow: workflow,+ node_id: parameters[:node_id],+ )++ node_outputs = json_safe(parameters[:node_outputs] || {})+ if !node_outputs.is_a?(Hash)+ @input_error = "node_outputs must be an object keyed by node name."+ return context+ end+ node_outputs.each do |name, items|+ next if name.start_with?("_", "$") # internal context keys, not node names+ context[name] = Array.wrap(items)+ context["__node_runs"]&.delete(name)end- end- def validate_each_item_output(output, errors, warnings)- if output.is_a?(Array)- errors << "#{RUN_ONCE_FOR_EACH_ITEM} must return one object, not an array"- return+ if !parameters[:input_items].nil?+ input_items = Array.wrap(json_safe(parameters[:input_items]))+ if input_items.length > MAX_INPUT_ITEMS+ @notes << "Only the first #{MAX_INPUT_ITEMS} of #{input_items.length} input_items were used."+ end+ context["__input_items"] = input_items.first(MAX_INPUT_ITEMS)+ elsif (input_node = parameters[:input_node].presence)+ context["__input_items"] = recorded_output(context, input_node)end- validate_item(output, errors, warnings)+ context["__input_items"] ||= [{ "json" => {} }]+ contextend- def validate_all_items_output(output, errors, warnings)- if output.nil?- warnings << "Script returned null or undefined; this produces no output items"- return- end-- unless output.is_a?(Array) || output.is_a?(Hash)- errors << "#{RUN_ONCE_FOR_ALL_ITEMS} must return an array of objects or one object"- return- end+ def recorded_output(context, node_name)+ output_index = parameters[:input_output_index].to_i+ run = Array(context.dig("__node_runs", node_name)).last+ return Array(run.dig("outputs", output_index)) if run+ return Array.wrap(context[node_name]) if context.key?(node_name) && output_index.zero?- Array.wrap(output).each { |item| validate_item(item, errors, warnings) }+ @input_error =+ "No recorded output #{output_index} for #{node_name.inspect}; pass input_items instead."+ nilend- def validate_item(item, errors, warnings)- unless item.is_a?(Hash)- errors << "Output item must be an object"- return+ def logs(entries)+ if entries.length > MAX_LOG_ENTRIES+ @notes << "Only the first #{MAX_LOG_ENTRIES} of #{entries.length} log entries are shown."end- validate_top_level_keys(item, errors, warnings)- if item.key?("json") && !item["json"].is_a?(Hash)- errors << "Output item json field must be an object"- end+ entries.first(MAX_LOG_ENTRIES).map { |entry| truncated_log_entry(entry) }end- def validate_top_level_keys(item, errors, warnings)- unknown_keys = item.keys.reject { |key| reserved_item_key?(key) }- return if unknown_keys.empty?-- if item.keys.any? { |key| reserved_item_key?(key) }- errors << "Output item mixes reserved item keys with plain fields"- else- warnings << "Output item is a plain object and will be normalized under json"+ def truncated_log_entry(entry)+ entry.transform_values do |value|+ if value.is_a?(String)+ DiscourseWorkflows::Ai::Truncation.text(value, MAX_LOG_LENGTH)+ else+ value+ endendend-- def reserved_item_key?(key)- RESERVED_ITEM_KEYS.include?(key.to_s)- endendendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/truncation.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/truncation.rbnew file mode 100644index 00000000000..a7509819ebf--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/truncation.rb@@ -0,0 +1,16 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Ai+ # Shortens text shown to the authoring agent with a visible marker, so the agent never+ # mistakes a cut value for the complete one.+ module Truncation+ def self.text(value, max_length)+ value = value.to_s+ return value if value.length <= max_length++ "#{value[0, max_length]}… [truncated, #{value.length} chars total]"+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/workflow_outline.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/workflow_outline.rbnew file mode 100644index 00000000000..6360448ac44--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/workflow_outline.rb@@ -0,0 +1,69 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Ai+ # A compact view of the current graph for the authoring prompt; full node data comes from+ # workflow_node_details.+ module WorkflowOutline+ MAX_NODES = 300+ MAX_CONNECTIONS = 600+ MAX_NOTE_LENGTH = 2_000+ STICKY_NOTE_TYPE = "flow:sticky_note"++ def self.text(workflow)+ nodes = workflow&.nodes || []+ return "The workflow has no nodes yet." if nodes.empty?++ connections =+ DiscourseWorkflows::WorkflowDocument.connection_records(nodes, workflow.connections || {})+ names_by_id = nodes.to_h { |node| [node["id"].to_s, node["name"]] }++ [+ "Nodes (id | name | type):",+ *capped(nodes, MAX_NODES, "nodes", omitted_ids: true) { |node| node_line(node) },+ "Connections:",+ *capped(connections, MAX_CONNECTIONS, "connections") do |c|+ connection_line(c, names_by_id)+ end,+ ].join("\n")+ end++ def self.capped(records, max, label, omitted_ids: false, &block)+ lines = records.first(max).map(&block)+ if records.length > max+ omitted = records.length - max+ ids = " (ids: #{records.drop(max).map { |node| node["id"] }.join(", ")})" if omitted_ids+ lines << "- [truncated: #{omitted} more #{label} not shown#{ids}; use workflow_node_details]"+ end+ lines.presence || ["- none"]+ end++ def self.node_line(node)+ line = "- #{node["id"]} | #{node["name"]} | #{node["type"]}"+ return line if node["type"] != STICKY_NOTE_TYPE++ content = DiscourseWorkflows::NodeData.parameters(node)["content"]+ return line if content.blank?++ line = "#{line} | note: #{Truncation.text(content, MAX_NOTE_LENGTH).to_json}"+ line += " (full note: workflow_node_details)" if content.length > MAX_NOTE_LENGTH+ line+ end++ def self.connection_line(connection, names_by_id)+ from = names_by_id[connection["source_node_id"].to_s] || connection["source_node_id"]+ to = names_by_id[connection["target_node_id"].to_s] || connection["target_node_id"]+ output_index = connection["source_output_index"].to_i+ input_index = connection["target_input_index"].to_i+ connection_type = connection["connection_type"].to_s.presence || "main"++ line = +"- #{from}"+ line << " [output #{output_index}]" if output_index > 0+ line << " -> #{to}"+ line << " [input #{input_index}]" if input_index > 0+ line << " (#{connection_type})" if connection_type != "main"+ line+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rbindex 991a88b6b82..cd62a23f40a 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb@@ -20,9 +20,8 @@ module DiscourseWorkflowsDiscourseWorkflows::Ai::Tools::WorkflowResolveEntity,DiscourseWorkflows::Ai::Tools::WorkflowAiAgentCatalog,DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog,- DiscourseWorkflows::Ai::Tools::WorkflowGraphContext,+ DiscourseWorkflows::Ai::Tools::WorkflowNodeDetails,DiscourseWorkflows::Ai::Tools::WorkflowValidatePatch,- DiscourseWorkflows::Ai::Tools::WorkflowScriptContext,DiscourseWorkflows::Ai::Tools::WorkflowValidateScript,*discovery_tools,*chat_tools,@@ -38,9 +37,9 @@ module DiscourseWorkflows- Never say a workflow change is live.- Never publish a workflow.- Produce draft proposals only.- - Use the compact workflow authoring context supplied with the request for workflow id/name and important field facts.- - Use workflow_graph_context when you need the current workflow graph; the full graph is not preloaded.- - Use workflow_node_catalog with targeted queries when you need node parameters, capabilities, schemas, or examples; the full node catalog is not preloaded. Prefer one broad query containing the relevant node names/keywords over many repeated catalog calls.+ - The workflow authoring context supplied with the request has the workflow id/name, an outline of its nodes and connections, and important field facts.+ - Choose node types from the node menu at the end of these instructions. Call workflow_node_catalog with the exact types you plan to add or reconfigure to get their parameters, output contracts, examples and notes; request several types in one call.+ - Call workflow_node_details with node IDs from the outline when you need an existing node's parameters (including Code scripts), connections, input/output fields, or recorded output.- Do not invent node types, node versions, node parameters, expression variables, or existing AI agent IDs.- Ask clarifying questions when required values are missing or ambiguous.- Use tools for all workflow authoring outcomes. Do not write a final prose, markdown, or JSON answer.@@ -49,7 +48,6 @@ module DiscourseWorkflows- When authoring is complete, call workflow_authoring_result exactly once with status, message, questions, and proposal. This final result tool halts the turn and is the only supported final response mechanism.- workflow_authoring_result status must be one of: needs_clarification, proposed_patch, explanation, error.- For proposed_patch results, include a proposal object with title, summary, assumptions, risks, risk_level, and operations.- - Use the compact workflow authoring context supplied with the request before proposing changes, and call workflow_graph_context/workflow_node_catalog for details when needed.- Do not ask admins for exact IDs, slugs, or names when an available tool can look them up; use discovery and resolver tools first.- Use categories, tags, search, read, and time tools for current forum discovery when they help draft the workflow safely; do not use forum search/read to discover workflow node behavior or schemas because workflow_node_catalog and workflow_validate_patch return schema details.- Use workflow_resolve_entity when the user names a category, tag, tag group, group, user, badge, data table, or topic and you need its exact workflow parameter value. Topic ids go in the topic_ids filter, which only the triggers that list it in workflow_node_catalog support.@@ -62,11 +60,11 @@ module DiscourseWorkflows- With action:send_chat_message target "user" or "group", set target_usernames to an array of usernames or an expression such as ={{ $json.user.username }}, and actor_username to the sender (defaults to system). Do not call search_chat_channels or set channel_id in this mode. The recipient must already be able to chat: users from trigger:user_created are usually still inactive and at trust level 0, so welcome new signups with action:send_personal_message instead.- With action:send_chat_message target "channel", use search_chat_channels before asking the admin to choose a chat channel or before setting channel_id. Never invent chat channel names, IDs, or clarification options. If search_chat_channels returns matches, ask using those exact channel names/IDs or use the named match. If it returns no matches, ask for a custom channel name/ID instead of suggesting made-up channels.- Use search_chat_integration_channels before asking the admin to choose an external chat integration channel or before setting action:send_chat_integration_message channel_id. Never invent provider names, channel labels, IDs, or clarification options. If search_chat_integration_channels returns matches, set channel_id and channel_name from the exact match or ask using those exact values. If it returns no matches, explain that a matching channel must be configured instead of suggesting made-up channels.- - Use workflow_validate_patch as a dry-run planning tool when drafting workflow changes. It does not save anything. For non-trivial workflows, query relevant node types with workflow_node_catalog, then call workflow_validate_patch after adding or connecting candidate nodes to inspect node_fields for exact input/output paths, then continue from those fields.+ - Use workflow_validate_patch as a dry-run planning tool when drafting workflow changes. It does not save anything. For non-trivial workflows, look up the relevant node types with workflow_node_catalog, then call workflow_validate_patch after adding or connecting candidate nodes to inspect node_fields for exact input/output paths, then continue from those fields.- If workflow_validate_patch returns expression_errors or schema-path errors, repair the operations and call workflow_validate_patch again before returning a final proposal.- Action nodes may replace the current item JSON. Always use the downstream node's input_fields from workflow_validate_patch rather than assuming trigger fields are still available after an action node.- When a downstream node needs fields from an earlier node after an action replaced the current item JSON, prefer previous-node expressions such as ={{ $("When a post is created").item.json.post.post_url }} or template references such as {{ $("When a post is created").item.json.post.username }}. Do not add a Code node only to copy trigger fields forward.- - Before proposing a Code node, call workflow_script_context for the runtime API and call workflow_validate_script with the exact mode and code. If validation fails, repair the JavaScript and validate it again before returning a final proposal.+ - Before proposing a Code node, read the action:code notes from workflow_node_catalog (and the node's recorded input and upstream outputs with workflow_node_details when editing), then test the script with workflow_validate_script. Run it against the workflow's recorded data (workflow_id, plus node_id when editing an existing node or input_node, plus input_output_index for a non-default branch, for a new node placed after an existing one) and against your own input_items/node_outputs for edge cases. Use console.log to inspect data. A script that deliberately throws on missing data is correct; do not weaken such safeguards to make a test pass. Repair real errors and test again before returning a final proposal.- Do not stop after validating a partial graph; continue drafting until the complete workflow is represented in operations.- Before calling workflow_authoring_result with a proposed_patch status, call workflow_validate_patch with the complete operations unless the change is a trivial edit that does not affect graph structure.- Only call workflow_ask_questions when you cannot safely draft a workflow without more admin input.@@ -104,9 +102,12 @@ module DiscourseWorkflows- Do not assume $json.post.trust_level exists. The post payload never includes trust level; trust level is only available as $json.user.trust_level on triggers that expose a user object (such as trigger:post_created). For topic-only payloads, use the trust_level_N group membership approach above.- Do not ask whether the first post or topic author trust level is available until you have checked the current node schemas with workflow_validate_patch.- In condition nodes, each condition must use leftValue, operator, and rightValue keys. Do not use left/right keys. Use operator.type "number" for integer/number fields, "string" for string fields, and "boolean" for boolean fields.- - Use Code nodes for data transformation when needed; the server validates generated scripts before a proposal can be applied.+ - Use Code nodes for data transformation when needed. The server only checks proposed scripts for syntax and mode rules, so testing them with workflow_validate_script is the only runtime check.- When adding or updating a Code node, set parameters.mode and parameters.code.- The server validates workflow patches before they can be applied.++ Node menu (type: description):+ #{DiscourseWorkflows::Ai::NodeMenu.text}PROMPTenddiff --git a/plugins/discourse-workflows/plugin.rb b/plugins/discourse-workflows/plugin.rbindex e758ac7f2f8..42085e59756 100644--- a/plugins/discourse-workflows/plugin.rb+++ b/plugins/discourse-workflows/plugin.rb@@ -207,15 +207,18 @@ after_initialize dorequire_relative "lib/discourse_workflows/ai/tools/base"require_relative "lib/discourse_workflows/ai/graph_digest"require_relative "lib/discourse_workflows/ai/progress_publisher"+ require_relative "lib/discourse_workflows/ai/truncation"+ require_relative "lib/discourse_workflows/ai/sample"+ require_relative "lib/discourse_workflows/ai/node_menu"+ require_relative "lib/discourse_workflows/ai/workflow_outline"require_relative "lib/discourse_workflows/ai/tools/workflow_node_catalog"require_relative "lib/discourse_workflows/ai/tools/workflow_ai_agent_catalog"- require_relative "lib/discourse_workflows/ai/tools/workflow_graph_context"+ require_relative "lib/discourse_workflows/ai/tools/workflow_node_details"require_relative "lib/discourse_workflows/ai/tools/workflow_validate_patch"require_relative "lib/discourse_workflows/ai/tools/workflow_ask_questions"require_relative "lib/discourse_workflows/ai/tools/workflow_resolve_entity"require_relative "lib/discourse_workflows/ai/tools/search_chat_channels"require_relative "lib/discourse_workflows/ai/tools/search_chat_integration_channels"- require_relative "lib/discourse_workflows/ai/tools/workflow_script_context"require_relative "lib/discourse_workflows/ai/tools/workflow_validate_script"require_relative "lib/discourse_workflows/ai_workflow_author"diff --git a/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/author_with_ai_spec.rb b/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/author_with_ai_spec.rbindex 7afd8ad69e5..07742a91822 100644--- a/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/author_with_ai_spec.rb+++ b/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/author_with_ai_spec.rb@@ -147,24 +147,21 @@ RSpec.describe Jobs::DiscourseWorkflows::AuthorWithAi do"trigger:post_created exposes the created post under post.*","user.trust_level","do not assume post.trust_level",- "Full graph and node catalog are intentionally not preloaded","Do not ask whether trust level is available",+ "Workflow outline:\nThe workflow has no nodes yet.",)- payload_json = captured_custom_instructions.split("tools for details:\n", 2).last+ payload_json = captured_custom_instructions.split("Workflow authoring context:\n", 2).lastpayload = JSON.parse(payload_json)- expect(payload).not_to have_key("node_catalog")- expect(payload).not_to have_key("workflow_graph")- expect(payload.dig("context_tools", "workflow_node_catalog")).to include("node parameters")- expect(payload.dig("context_tools", "workflow_graph_context")).to include("current graph")+ expect(payload.dig("context_tools", "workflow_node_catalog")).to include("exact node types")+ expect(payload.dig("context_tools", "workflow_node_details")).to include("recorded output")expect(payload.dig("context_tools", "search_chat_channels")).to include("never invent")expect(payload.dig("context_tools", "search_chat_integration_channels")).to include("never invent",)- expect(payload.dig("context_tools", "workflow_validate_script")).to include("exact Code node")- expect(payload.dig("context_tools", "workflow_authoring_result")).to include("final response")- expect(payload["trigger_author_field_facts"]).to include(- a_string_including("trigger:post_created"),+ expect(payload.dig("context_tools", "workflow_validate_script")).to include(+ "recorded workflow data",)+ expect(payload.dig("context_tools", "workflow_authoring_result")).to include("final response")endit "uses authoring result tool calls for patch proposals", :aggregate_failures do@@ -409,7 +406,7 @@ RSpec.describe Jobs::DiscourseWorkflows::AuthorWithAi doexpect(session.proposed_patch.dig("patch_validation", "valid")).to eq(true)end- it "stores Code node validation errors in the response", :aggregate_failures do+ it "keeps proposals with failing Code node validation reviewable", :aggregate_failures dosession =Fabricate(:discourse_workflows_ai_authoring_session,@@ -452,10 +449,7 @@ RSpec.describe Jobs::DiscourseWorkflows::AuthorWithAi dogeneration_id: SecureRandom.hex,)- expect(session.reload).to have_attributes(status: "error")- expect(session.latest_response["message"]).to include(- "Merge post data with result: $input.all is only available in runOnceForAllItems mode",- )+ expect(session.reload).to have_attributes(status: "proposal_ready")expect(session.proposed_patch["script_validations"]).to contain_exactly(include("node_name" => "Merge post data with result",@@ -465,7 +459,7 @@ RSpec.describe Jobs::DiscourseWorkflows::AuthorWithAi do)end- it "requires a tool call for the final authoring result" do+ it "asks the admin how to proceed when authoring fails", :aggregate_failures dosession =Fabricate(:discourse_workflows_ai_authoring_session,@@ -491,6 +485,63 @@ RSpec.describe Jobs::DiscourseWorkflows::AuthorWithAi dogeneration_id: SecureRandom.hex,)- expect(session.reload).to have_attributes(status: "error")+ expect(session.reload).to have_attributes(status: "needs_clarification")+ expect(session.latest_response["questions"]).to contain_exactly(+ include(+ "id" => "how_to_proceed",+ "options" => [{ "label" => I18n.t("discourse_workflows.ai.how_to_proceed.fix") }],+ ),+ )+ expect(session.messages.last).to include("type" => "model")+ expect(JSON.parse(session.messages.last["content"])).to include(+ "status" => "needs_clarification",+ )+ end++ it "still reports an error when the question cannot be recorded" do+ session = Fabricate(:discourse_workflows_ai_authoring_session, user: admin)+ fake_bot = double+ job = described_class.new++ allow(fake_bot).to receive(:reply).and_raise(StandardError, "Provider timed out.")+ allow(DiscourseAi::Agents::Bot).to receive(:as).and_return(fake_bot)+ allow(Discourse).to receive(:warn_exception)+ allow(job).to receive(:persist_response).and_raise(ActiveRecord::RecordInvalid)++ job.execute(session_id: session.id, user_id: admin.id, generation_id: SecureRandom.hex)++ expect(session.reload.latest_response).to eq(+ "status" => "error",+ "message" => "Provider timed out.",+ )+ end++ it "asks the admin how to proceed when authoring raises", :aggregate_failures do+ session =+ Fabricate(+ :discourse_workflows_ai_authoring_session,+ user: admin,+ messages: [{ "type" => "user", "content" => "Create a manual log workflow" }],+ )+ fake_bot = double++ allow(fake_bot).to receive(:reply).and_raise(StandardError, "Provider timed out.")+ allow(DiscourseAi::Agents::Bot).to receive(:as).and_return(fake_bot)+ allow(Discourse).to receive(:warn_exception)++ described_class.new.execute(+ session_id: session.id,+ user_id: admin.id,+ generation_id: SecureRandom.hex,+ )++ expect(session.reload).to have_attributes(status: "needs_clarification")+ expect(session.latest_response["questions"]).to contain_exactly(+ include(+ "id" => "how_to_proceed",+ "question" =>+ I18n.t("discourse_workflows.ai.how_to_proceed.question", problem: "Provider timed out."),+ ),+ )endenddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/node_menu_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/node_menu_spec.rbnew file mode 100644index 00000000000..65fde296bd7--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/node_menu_spec.rb@@ -0,0 +1,36 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Ai::NodeMenu do+ let(:all_palette_nodes) do+ DiscourseWorkflows::Registry.nodes(include_disabled_plugins: true).select(&:palette_visible?)+ end++ it "lists each node type once with its description" do+ menu = described_class.text++ expect(menu).to include("- action:code: Run custom JavaScript code")+ expect(menu.lines.grep(/\A- action:code:/).size).to eq(1)+ end++ it "marks descriptions it truncates" do+ node_class = DiscourseWorkflows::Nodes::Code::V1+ allow(I18n).to receive(:t).and_call_original+ allow(I18n).to receive(:t).with(+ "js.discourse_workflows.node_descriptions.action:code",+ locale: :en,+ default: "",+ ).and_return("x" * 200)++ expect(described_class.line(node_class)).to eq(+ "- action:code: #{"x" * described_class::MAX_DESCRIPTION_LENGTH}… [truncated, 200 chars total]",+ )+ end++ it "stays small enough to preload into every authoring prompt" do+ length = described_class.text(all_palette_nodes).length++ expect(length).to be <= described_class::MAX_LENGTH,+ "The node menu is #{length} chars (limit #{described_class::MAX_LENGTH}). It is too large to " \+ "preload; switch node discovery to a search/lookup tool instead of raising the limit."+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rbindex 927cc034a39..34d9be20288 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb@@ -3,22 +3,46 @@RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog dofab!(:admin)- subject(:result) { invoke_tool(query:, include_examples:) }-- def invoke_tool(query: nil, include_examples: false)+ def invoke_tool(types)described_class.new(- { query: query, include_examples: include_examples }.compact,+ { types: types },bot_user: Discourse.system_user,llm: nil,- context: bot_context,+ context: DiscourseAi::Agents::BotContext.new(messages: [], user: admin),).invokeendbefore { SiteSetting.discourse_ai_enabled = true }- let(:query) { nil }- let(:include_examples) { false }- let(:bot_context) { DiscourseAi::Agents::BotContext.new(messages: [], user: admin) }+ let(:result) do+ {+ nodes:+ contract_types+ .each_slice(described_class::MAX_TYPES)+ .flat_map { |types| invoke_tool(types)[:nodes] },+ }+ end+ let(:contract_types) do+ %w[+ trigger:topic_created+ trigger:post_created+ trigger:post_edited+ trigger:topic_closed+ trigger:tag_created+ trigger:user_added_to_group+ trigger:user_removed_from_group+ trigger:chat_message_created+ action:topic+ action:topic_tags+ action:tag_group+ action:post+ action:user+ action:send_personal_message+ action:group+ action:ai_agent+ flow:wait+ ]+ endlet(:nodes_by_type) { result[:nodes].index_by { |node| node[:type] } }let(:output_contracts_by_type) donodes_by_type.transform_values { |node| node.fetch(:output_contracts).first }.compact@@ -27,87 +51,36 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog dooutput_contracts_by_type.transform_values { |contract| contract.fetch(:fields) }end- it "excludes nodes hidden from the palette" do- hidden_node_type = DiscourseWorkflows::Nodes::LoopOverItems::V1.identifier-- result = invoke_tool(query: hidden_node_type)+ it "returns only the requested types and reports unknown ones", :aggregate_failures do+ result = invoke_tool(%w[action:code action:does_not_exist])- expect(result[:nodes]).to be_empty+ expect(result[:nodes].map { |node| node[:type] }).to eq(["action:code"])+ expect(result[:unknown_types]).to eq(["action:does_not_exist"])end- it "finds the external chat integration action by provider keywords" do- SiteSetting.chat_integration_enabled = true+ it "marks when requested types are cut off" do+ types = Array.new(described_class::MAX_TYPES + 2) { |index| "action:missing_#{index}" }- result = invoke_tool(query: "slack", include_examples: true)- node =- result[:nodes].find { |candidate| candidate[:type] == "action:send_chat_integration_message" }-- expect(node).to include(- available: true,- examples: [- a_hash_including(- parameters:- a_hash_including(- channel_id: 123,- channel_name: "Slack: #general",- message: "={{ $json.post.excerpt }}",- ),- ),- ],- )- expect(node.dig(:output_contracts, 0, :fields)).to eq(- "channel_id" => "integer",- "provider" => "string",+ expect(invoke_tool(types)[:truncated]).to eq(+ "Only the first #{described_class::MAX_TYPES} of #{types.length} requested types are included.",)end- it "finds the moderation action by unlist keywords", :aggregate_failures do- result = invoke_tool(query: "unlist topic", include_examples: true)- node = result[:nodes].find { |candidate| candidate[:type] == "action:topic_moderation" }+ it "treats nodes hidden from the palette as unknown" do+ hidden_node_type = DiscourseWorkflows::Nodes::LoopOverItems::V1.identifier- expect(node[:examples]).to contain_exactly(- include(- name: "Unlist the trigger topic",- parameters:- include(- operation: "unlist_topic",- topic_id: "={{ $json.topic.id }}",- actor_username: "system",- ),- ),- )- expect(node.dig(:output_contracts, 0, :fields)).to include(- "topic.id" => "integer",- "topic.visible" => "boolean",- "topic.visibility_reason_id" => "integer",- )+ expect(invoke_tool([hidden_node_type])).to include(nodes: [], unknown_types: [hidden_node_type])end- it "finds the Tag group action with its example and output fields", :aggregate_failures do- result = invoke_tool(query: "organize tag group", include_examples: true)- node = result[:nodes].find { |candidate| candidate[:type] == "action:tag_group" }+ it "includes Code node runtime notes" do+ code_node = invoke_tool(["action:code"])[:nodes].first- expect(node[:examples]).to contain_exactly(- include(- name: "Add tags to a tag group",- parameters:- include(- operation: "add",- tag_group_id: 123,- tag_names: "needs-review, escalated",- actor_username: "system",- ),- ),- )- expect(node.dig(:output_contracts, 0, :fields)).to include(- "tag_group_id" => "integer",- "tag_group_name" => "string",- "tag_names" => "array<string>",+ expect(code_node[:notes]).to include(+ a_string_including("runOnceForEachItem must return one object"),)endit "exposes post author, topic link, and action output fields", :aggregate_failures do- expect(result[:status]).to eq("success")expect(output_fields_by_type.fetch("trigger:topic_created")).to include("post.user_id" => "integer","post.username" => "string",@@ -280,76 +253,19 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog doexpect(output_fields_by_type.fetch("action:ai_agent")).to include("result" => "string")end- it "finds the tag created trigger by taxonomy keywords" do- result = invoke_tool(query: "taxonomy keyword")-- expect(result[:nodes].map { |node| node[:type] }).to include("trigger:tag_created")- end-- it "finds the chat action for DM queries but not for personal message ones",- :aggregate_failures do- skip "Chat plugin is not available" if !defined?(::Chat::Channel)- SiteSetting.chat_enabled = true-- dm_types = invoke_tool(query: "dm")[:nodes].map { |node| node[:type] }- pm_types = invoke_tool(query: "personal pm")[:nodes].map { |node| node[:type] }-- expect(dm_types).to include("action:send_personal_message", "action:send_chat_message")- expect(pm_types).to include("action:send_personal_message")- expect(pm_types).not_to include("action:send_chat_message")- end-- it "matches broad multi-term catalog queries", :aggregate_failures do- result =- described_class.new(- {- query:- "trigger topic_closed action topic get condition filter chat message dm group membership user profile trust title",- include_examples: true,- },- bot_user: Discourse.system_user,- llm: nil,- context: bot_context,- ).invoke- node_types = result[:nodes].map { |node| node[:type] }-- expect(node_types).to include(- "trigger:topic_closed",- "action:topic",- "condition:filter",- "action:group",- "action:user",- "trigger:user_added_to_group",- "trigger:user_removed_from_group",- "action:send_chat_message",- "action:send_personal_message",- )- expect(result[:nodes].find { |node| node[:type] == "action:topic" }[:examples]).to be_present- end-- it "matches AI agent runner and attachment queries" do- result =- described_class.new(- { query: "runner permissions attachments", include_examples: true },- bot_user: Discourse.system_user,- llm: nil,- context: bot_context,- ).invoke- ai_agent_node = result[:nodes].find { |node| node[:type] == "action:ai_agent" }-- expect(ai_agent_node).to be_present- expect(ai_agent_node.dig(:properties, "runner_username", "ui", "control")).to eq("actor")- expect(ai_agent_node.dig(:examples, 0, :parameters)).to include(runner_username: "system")- end-it "includes declarative filter and topic lookup examples", :aggregate_failures doresult =- described_class.new(- { include_examples: true },- bot_user: Discourse.system_user,- llm: nil,- context: bot_context,- ).invoke+ invoke_tool(+ %w[+ condition:filter+ condition:if+ action:group+ action:user+ action:topic+ action:post+ action:send_personal_message+ ],+ )filter_node = result[:nodes].find { |node| node[:type] == "condition:filter" }group_node = result[:nodes].find { |node| node[:type] == "action:group" }user_node = result[:nodes].find { |node| node[:type] == "action:user" }diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_details_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_details_spec.rbnew file mode 100644index 00000000000..609bfc02356--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_details_spec.rb@@ -0,0 +1,117 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeDetails do+ fab!(:admin)++ fab!(:workflow) do+ graph =+ build_workflow_graph do |g|+ g.node "trigger-1", "trigger:manual", name: "Run"+ g.node "config-1",+ "action:code",+ name: "Configuration",+ parameters: {+ mode: "runOnceForAllItems",+ code: "return [{ json: { a: 1 } }];",+ }+ g.node "format-1", "action:code", name: "Format"+ g.chain "trigger-1", "config-1", "format-1"+ end+ Fabricate(:discourse_workflows_workflow, created_by: admin, **graph)+ end++ def invoke_tool(node_ids)+ described_class.new(+ { workflow_id: workflow.id, node_ids: node_ids },+ bot_user: Discourse.system_user,+ llm: nil,+ context: DiscourseAi::Agents::BotContext.new(messages: [], user: admin),+ ).invoke+ end++ def record_execution(runs)+ execution = Fabricate(:discourse_workflows_execution, workflow: workflow, status: :success)+ run_data =+ runs.to_h do |node_id, items|+ node = workflow.find_node(node_id)+ [+ node["name"],+ [+ {+ "node_id" => node_id,+ "node_name" => node["name"],+ "node_type" => node["type"],+ "status" => "success",+ "run_index" => 0,+ "inputs" => [],+ "outputs" => [{ "index" => 0, "items" => items, "item_count" => items.length }],+ },+ ],+ ]+ end+ Fabricate(+ :discourse_workflows_execution_data,+ execution: execution,+ data: {+ "run_data" => run_data,+ },+ )+ execution+ end++ it "returns parameters and connections for nodes found by id or name", :aggregate_failures do+ result = invoke_tool(%w[config-1 Format missing])+ config, format = result[:nodes]++ expect(result[:not_found]).to eq(["missing"])+ expect(config).to include(+ id: "config-1",+ type: "action:code",+ parameters: include("code" => "return [{ json: { a: 1 } }];"),+ incoming: [include(node_id: "trigger-1", node_name: "Run", output_index: 0)],+ outgoing: [include(node_id: "format-1", node_name: "Format")],+ )+ expect(format[:id]).to eq("format-1")+ end++ it "says when there is no recorded output" do+ expect(invoke_tool(["config-1"])[:nodes].first[:recorded_output]).to include(source: "none")+ end++ it "distinguishes a node that did not run from one that produced no items", :aggregate_failures do+ execution = record_execution("config-1" => [])+ nodes = invoke_tool(%w[config-1 format-1])[:nodes]++ expect(nodes.first[:recorded_output]).to include(+ source: "execution",+ execution_id: execution.id,+ outputs: [{ total_items: 0, items: [] }],+ )+ expect(nodes.second[:recorded_output]).to include(+ source: "none",+ note: a_string_including("no successful run"),+ )+ end++ it "samples recorded output and marks what it truncated", :aggregate_failures do+ items = Array.new(5) { |index| { "json" => { "index" => index, "body" => "x" * 600 } } }+ record_execution("config-1" => items)++ output = invoke_tool(["config-1"])[:nodes].first[:recorded_output]+ port = output[:outputs].first++ expect(port[:total_items]).to eq(5)+ expect(port[:items].length).to be < 5+ expect(port[:items].first.dig("json", "body")).to end_with("… [truncated, 600 chars total]")+ expect(output[:truncated]).to include("Sample truncated")+ end++ it "prefers pinned data" do+ workflow.update_node_pin_data!("Configuration", [{ "json" => { "pinned" => true } }])++ expect(invoke_tool(["config-1"])[:nodes].first[:recorded_output]).to eq(+ source: "pinned",+ outputs: [{ total_items: 1, items: [{ "json" => { "pinned" => true } }] }],+ )+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_script_context_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_script_context_spec.rbdeleted file mode 100644index b70f1906c72..00000000000--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_script_context_spec.rb+++ /dev/null@@ -1,94 +0,0 @@-# frozen_string_literal: true--RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowScriptContext do- fab!(:admin)- fab!(:workflow) do- graph =- build_workflow_graph do |builder|- builder.node "post-created", "trigger:post_created"- builder.node "filter", "condition:filter"- builder.node "code", "action:code"- builder.connect "post-created", "filter"- end-- Fabricate(- :discourse_workflows_workflow,- created_by: admin,- **graph,- pin_data: {- "Code" => [{ "json" => { "custom" => { "id" => 1 }, "nothing" => nil } }],- },- )- end-- subject(:result) { invoke_tool(upstream_node_id) }-- def invoke_tool(upstream_node_id)- described_class.new(- { workflow_id: workflow.id, upstream_node_id: upstream_node_id },- bot_user: Discourse.system_user,- llm: nil,- context: bot_context,- ).invoke- end-- let(:upstream_node_id) { "post-created" }- let(:bot_context) { DiscourseAi::Agents::BotContext.new(messages: [], user: admin) }-- it "uses the node's declared fields before it has run" do- expect(result.dig(:upstream_fields, :output_fields, 0)).to include(- "post.id" => "integer",- "user.username" => "string",- )- expect(result.dig(:upstream_fields, :output_fields, 0)).not_to include("pinned_only")- end-- it "uses pinned fields before the node's declaration" do- node_name = workflow.nodes.find { |node| node["id"] == "post-created" }["name"]- workflow.update_node_pin_data!(node_name, [{ "json" => { "pinned_only" => true } }])-- expect(result.dig(:upstream_fields, :output_fields, 0)).to eq("pinned_only" => "boolean")- end-- it "keeps empty pinned JSON authoritative over the declaration" do- node_name = workflow.nodes.find { |node| node["id"] == "post-created" }["name"]- workflow.update_node_pin_data!(node_name, [{ "json" => {} }])-- expect(result.dig(:upstream_fields, :output_fields)).to eq([{}])- end-- it "resolves inherited fields for pass-through nodes" do- result = invoke_tool("filter")-- expect(result.dig(:upstream_fields, :output_fields, 0)).to include(- "post.id" => "integer",- "user.username" => "string",- )- end-- it "infers fields from pin data when the node has no declaration" do- result = invoke_tool("code")-- expect(result.dig(:upstream_fields, :output_fields, 0)).to eq(- "custom" => "object",- "custom.id" => "integer",- "nothing" => "null",- )- end-- it "preserves unsafe property segments inferred from pin data" do- workflow.update_node_pin_data!(- "Code",- [{ "json" => { "full-name" => "Ada", "a.b" => true, "a" => { "b" => 1 } } }],- )-- result = invoke_tool("code")-- expect(result.dig(:upstream_fields, :output_fields, 0)).to eq(- '["full-name"]' => "string",- '["a.b"]' => "boolean",- "a" => "object",- "a.b" => "integer",- )- end-enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_validate_script_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_validate_script_spec.rbindex cd9888281bb..3daa6dd1777 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_validate_script_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_validate_script_spec.rb@@ -3,6 +3,22 @@RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowValidateScript dofab!(:admin)+ fab!(:workflow) do+ graph =+ build_workflow_graph do |g|+ g.node "trigger-1", "trigger:manual", name: "Run sample"+ g.node "config", "action:code", name: "Configuration"+ g.chain "trigger-1", "config"+ end+ Fabricate(:discourse_workflows_workflow, created_by: admin, **graph)+ end++ let(:config_code) { <<~JS }+ var config = $("Configuration").first().json;+ if (!config.heading) { throw new Error("Configuration is required"); }+ return [{ json: { title: config.heading } }];+ JS+def invoke_tool(parameters)context = DiscourseAi::Agents::BotContext.new(messages: [], user: admin)described_class.new(@@ -13,50 +29,214 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowValidateScript do).invokeend- it "validates a run-once script and returns sample output" do+ def record_configuration_run(json)+ execution = Fabricate(:discourse_workflows_execution, workflow: workflow, status: :success)+ Fabricate(+ :discourse_workflows_execution_data,+ execution: execution,+ data: {+ "entries" => {+ },+ "context" => {+ },+ "node_contexts" => {+ },+ "run_data" => {+ "Configuration" => [+ {+ "node_id" => "config",+ "node_name" => "Configuration",+ "node_type" => "action:code",+ "status" => "success",+ "run_index" => 0,+ "inputs" => [],+ "outputs" => [{ "index" => 0, "items" => [{ "json" => json }], "item_count" => 1 }],+ },+ ],+ },+ },+ )+ end++ it "runs a script against supplied input items and returns output and logs",+ :aggregate_failures doresult =invoke_tool(- {- mode: "runOnceForAllItems",- code: "var items = $input.all(); items[0].json.added = true; return items;",- sample_input_items: [{ "json" => { "id" => 1 } }],- },+ mode: "runOnceForAllItems",+ code:+ "console.log('count', $input.all().length); var items = $input.all(); items[0].json.added = true; return items;",+ input_items: [{ "json" => { "id" => 1 } }],)expect(result).to include(- status: "success",valid: true,errors: [],- sample_output_items: [{ "json" => { "id" => 1, "added" => true } }],+ output: {+ total_items: 1,+ items: [{ "json" => { "id" => 1, "added" => true } }],+ },)+ expect(result[:logs]).to contain_exactly(include("message" => "count 1"))end- it "rejects all-items access in per-item mode" do+ it "resolves other nodes from the last successful execution" do+ record_configuration_run({ "heading" => "Recorded" })++ result = invoke_tool(mode: "runOnceForAllItems", code: config_code, workflow_id: workflow.id)++ expect(result).to include(+ valid: true,+ output: {+ total_items: 1,+ items: [{ "json" => { "title" => "Recorded" } }],+ },+ )+ end++ it "uses the recorded output of input_node as $input" do+ record_configuration_run({ "heading" => "Recorded" })+result =invoke_tool(- {- mode: "runOnceForEachItem",- code: "var items = $input.all(); return { json: { count: items.length } };",- },+ mode: "runOnceForAllItems",+ code: "return [{ json: { heading: $input.first().json.heading } }];",+ workflow_id: workflow.id,+ input_node: "Configuration",)- expect(result).to include(- status: "success",- valid: false,- errors: ["$input.all is only available in runOnceForAllItems mode"],+ expect(result.dig(:output, :items)).to eq([{ "json" => { "heading" => "Recorded" } }])+ end++ it "uses the requested output of input_node" do+ record_configuration_run({ "heading" => "First" })+ run_data = workflow.last_successful_execution.execution_data+ run = run_data.data.dig("run_data", "Configuration", 0)+ run["outputs"] << {+ "index" => 1,+ "items" => [{ "json" => { "heading" => "Second" } }],+ "item_count" => 1,+ }+ run_data.save!++ result =+ invoke_tool(+ mode: "runOnceForAllItems",+ code: "return [{ json: { heading: $input.first().json.heading } }];",+ workflow_id: workflow.id,+ input_node: "Configuration",+ input_output_index: 1,+ )++ expect(result.dig(:output, :items)).to eq([{ "json" => { "heading" => "Second" } }])+ end++ it "lets input_items replace an input_node without recorded output" do+ result =+ invoke_tool(+ mode: "runOnceForAllItems",+ code: "return [{ json: { count: $input.all().length } }];",+ workflow_id: workflow.id,+ input_node: "Configuration",+ input_items: [],+ )++ expect(result.dig(:output, :items)).to eq([{ "json" => { "count" => 0 } }])+ end++ it "rejects node_outputs that are not keyed by node name" do+ result = invoke_tool(mode: "runOnceForAllItems", code: "return [];", node_outputs: [])++ expect(result[:error]).to eq("node_outputs must be an object keyed by node name.")+ end++ it "reports an input_node without recorded output" do+ result =+ invoke_tool(+ mode: "runOnceForAllItems",+ code: "return [];",+ workflow_id: workflow.id,+ input_node: "Configuration",+ )++ expect(result[:error]).to include("No recorded output")+ end++ it "runs with empty input when input_items is empty" do+ result =+ invoke_tool(+ mode: "runOnceForAllItems",+ code: "return [{ json: { count: $input.all().length } }];",+ input_items: [],+ )++ expect(result.dig(:output, :items)).to eq([{ "json" => { "count" => 0 } }])+ end++ it "marks truncated logs" do+ result =+ invoke_tool(+ mode: "runOnceForAllItems",+ code: "for (var i = 0; i < 30; i++) { console.log('line ' + i); } return [];",+ )++ expect(result[:logs].length).to eq(described_class::MAX_LOG_ENTRIES)+ expect(result[:truncated]).to include("Only the first 20 of 30 log entries are shown.")+ end++ it "marks truncated input and output" do+ result =+ invoke_tool(+ mode: "runOnceForAllItems",+ code: "return $input.all();",+ input_items: Array.new(30) { |index| { "json" => { "index" => index } } },+ )++ expect(result[:output]).to include(total_items: 20)+ expect(result[:output][:items].length).to eq(described_class::MAX_OUTPUT_ITEMS)+ expect(result[:truncated]).to include(+ "Only the first 20 of 30 input_items were used.",+ "Sample truncated",)end- it "rejects output that mixes reserved item keys with plain fields" do+ it "lets supplied node outputs replace recorded data" do+ record_configuration_run({ "heading" => "Recorded" })++ result =+ invoke_tool(+ mode: "runOnceForAllItems",+ code: config_code,+ workflow_id: workflow.id,+ node_outputs: {+ "Configuration" => [{ "json" => {} }],+ },+ )++ expect(result).to include(valid: false, errors: [include("Configuration is required")])+ end++ it "reports syntax errors without running the script" do+ result = invoke_tool(mode: "runOnceForAllItems", code: "return [{ json: {} ];")++ expect(result).to include(valid: false, errors: [start_with("JavaScript syntax error")])+ end++ it "rejects all-items access in per-item mode" doresult =invoke_tool(- { mode: "runOnceForAllItems", code: "return [{ json: { id: 1 }, extra: true }];" },+ mode: "runOnceForEachItem",+ code: "var items = $input.all(); return { json: { count: items.length } };",)expect(result).to include(- status: "success",valid: false,- errors: ["Output item mixes reserved item keys with plain fields"],+ errors: ["$input.all is only available in runOnceForAllItems mode"],)end++ it "reports output the Code node rejects" do+ result = invoke_tool(mode: "runOnceForAllItems", code: "return 5;")++ expect(result).to include(valid: false, errors: [include("Code doesn't return items properly")])+ endenddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/workflow_outline_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/workflow_outline_spec.rbnew file mode 100644index 00000000000..7257c41942b--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/workflow_outline_spec.rb@@ -0,0 +1,48 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Ai::WorkflowOutline do+ fab!(:admin)++ fab!(:workflow) do+ graph =+ build_workflow_graph do |g|+ g.node "trigger-1", "trigger:manual", name: "Run"+ g.node "if-1", "condition:if", name: "Is staff"+ g.node "note-1",+ "flow:sticky_note",+ name: "Sticky Note",+ parameters: {+ content: "Keep the markers.\n#{"x" * 2_100}",+ }+ g.node "log-1", "action:code", name: "Log"+ g.chain "trigger-1", "if-1"+ g.connect "if-1", "log-1", output: "false"+ end+ Fabricate(:discourse_workflows_workflow, created_by: admin, **graph)+ end++ it "lists nodes and connections, marking truncated sticky notes" do+ outline = described_class.text(workflow)++ expect(outline).to include(+ "- trigger-1 | Run | trigger:manual",+ "- log-1 | Log | action:code",+ "- note-1 | Sticky Note | flow:sticky_note | note: \"Keep the markers.\\n",+ "[truncated, 2118 chars total]\" (full note: workflow_node_details)",+ "- Run -> Is staff",+ "- Is staff [output 1] -> Log",+ )+ end++ it "says when nodes are not shown" do+ stub_const(described_class, "MAX_NODES", 2) do+ expect(described_class.text(workflow)).to include(+ "- [truncated: 2 more nodes not shown (ids: note-1, log-1); use workflow_node_details]",+ )+ end+ end++ it "handles a workflow without nodes" do+ expect(described_class.text(nil)).to eq("The workflow has no nodes yet.")+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai_workflow_author_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai_workflow_author_spec.rbindex f7a5c671715..2fa4a21a2da 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai_workflow_author_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai_workflow_author_spec.rb@@ -11,9 +11,8 @@ RSpec.describe DiscourseWorkflows::AiWorkflowAuthor doDiscourseAi::Agents::Tools::Read,DiscourseAi::Agents::Tools::Time,DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog,- DiscourseWorkflows::Ai::Tools::WorkflowGraphContext,+ DiscourseWorkflows::Ai::Tools::WorkflowNodeDetails,DiscourseWorkflows::Ai::Tools::WorkflowValidatePatch,- DiscourseWorkflows::Ai::Tools::WorkflowScriptContext,DiscourseWorkflows::Ai::Tools::WorkflowValidateScript,DiscourseWorkflows::Ai::Tools::WorkflowAiAgentCatalog,DiscourseWorkflows::Ai::Tools::WorkflowAuthoringResult,@@ -32,9 +31,11 @@ RSpec.describe DiscourseWorkflows::AiWorkflowAuthor do"parameters.upload_ids",'With action:send_chat_message target "user" or "group", set target_usernames to an array of usernames or an expression',"Use search_chat_integration_channels before asking the admin to choose an external chat integration channel",- "call workflow_validate_script with the exact mode and code",+ "then test the script with workflow_validate_script","Do not add a Code node only to copy trigger fields forward","create_ai_agent",+ "Node menu (type: description):\n- ",+ "- action:code: Run custom JavaScript code",)enddiff --git a/plugins/discourse-workflows/test/javascripts/unit/components/workflows/build-with-ai-modal-test.js b/plugins/discourse-workflows/test/javascripts/unit/components/workflows/build-with-ai-modal-test.jsindex d98e5a3df66..7039621496e 100644--- a/plugins/discourse-workflows/test/javascripts/unit/components/workflows/build-with-ai-modal-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/components/workflows/build-with-ai-modal-test.js@@ -35,6 +35,31 @@ module("Unit | Component | Workflows | BuildWithAiModal", function (hooks) {);});+ test("proposal with failing scripts offers a fix", function (assert) {+ const modal = Object.create(BuildWithAiModal.prototype);++ modal.aiResponse = {+ status: "proposed_patch",+ response: {+ proposal: {+ operations: [{ op: "rename_node", node_id: "node-1", name: "Wait" }],+ script_validations: [{ valid: true }],+ },+ },+ };+ assert.false(modal.aiProposalHasFailedScripts, "passing scripts");++ modal.aiResponse.response.proposal.script_validations.push({+ valid: false,+ });+ modal.aiResponse = { ...modal.aiResponse };+ assert.true(modal.aiProposalHasFailedScripts, "failing scripts");+ assert.true(+ modal.aiShowingProposalReview,+ "the proposal stays reviewable so it can still be applied"+ );+ });+test("proposal exposes created agent resources", function (assert) {const modal = Object.create(BuildWithAiModal.prototype);