LightningWeb.Live.AiAssistant.Modes.WorkflowTemplate (Lightning v2.13.5)

View Source

AI Assistant mode for intelligent workflow template generation and management.

This mode leverages advanced AI capabilities to transform natural language descriptions into complete, production-ready Lightning workflow templates. It provides an intuitive interface for creating complex data integration workflows without requiring deep technical knowledge of Lightning's YAML structure.

Summary

Functions

Determines if workflow template input should be disabled.

Generates contextual titles for workflow template sessions.

Creates a new workflow template generation session.

Generates appropriate tooltip messages when template input is disabled.

Formats errors consistently for workflow template mode.

Retrieves a workflow template session with full context.

Handles post-processing after workflow template generation.

Provides workflow-specific placeholder text for input guidance.

Lists workflow template sessions for the current project.

Provides metadata for the workflow template generation mode.

Checks if more workflow template sessions exist for the project.

Initializes the UI state when starting a new template session.

Processes workflow template requests through the AI service.

Saves a user message to the workflow template session.

Indicates that workflow mode supports template generation.

Functions

chat_input_disabled?(map)

@spec chat_input_disabled?(map()) :: boolean()

Determines if workflow template input should be disabled.

Evaluates conditions specific to template generation to ensure the feature is only available when appropriate permissions and service availability allow it.

Examples

# Input enabled for template generation
chat_input_disabled?(%{
  can_edit_workflow: true,
  ai_limit_result: :ok,
  endpoint_available?: true,
  pending_message: %{loading: nil}
})
# => false

# Input disabled due to usage limits
chat_input_disabled?(%{
  can_edit_workflow: true,
  ai_limit_result: {:error, :limit_exceeded},
  endpoint_available?: true,
  pending_message: %{loading: nil}
})
# => true

chat_title(session)

@spec chat_title(map()) :: String.t()

Generates contextual titles for workflow template sessions.

Creates descriptive titles that include project context when available, making it easier to identify and organize template generation sessions.

Examples

# With custom title
chat_title(%{title: "Salesforce to PostgreSQL Sync"})
# => "Salesforce to PostgreSQL Sync"

# With project context
chat_title(%{project: %{name: "Customer Data Platform"}})
# => "Customer Data Platform Workflow"

# Fallback
chat_title(%{})
# => "New Workflow"

create_session(map, content)

@spec create_session(map(), String.t()) :: {:ok, map()} | {:error, any()}

Creates a new workflow template generation session.

Initializes a project-scoped session for AI-powered workflow creation. The session is configured for template generation with appropriate metadata and context for the AI service.

Examples

# Create session for new workflow
{:ok, session} = WorkflowTemplate.create_session(
  %{project: project, current_user: user},
  "Create a daily Salesforce to PostgreSQL sync workflow"
)

# Create session for workflow enhancement
{:ok, session} = WorkflowTemplate.create_session(
  %{project: project, current_user: user},
  "Add error handling and retry logic to existing webhook workflow"
)

disabled_tooltip_message(assigns)

@spec disabled_tooltip_message(map()) :: String.t() | nil

Generates appropriate tooltip messages when template input is disabled.

Provides specific explanations for why workflow template generation is unavailable, helping users understand required actions.

Parameters

  • assigns - Map containing permission and limit information

Returns

String explanation or nil if input should be enabled.

Examples

# Permission denied
disabled_tooltip_message(%{can_edit_workflow: false})
# => "You are not authorized to use the AI Assistant"

# Usage limit reached
disabled_tooltip_message(%{ai_limit_result: {:error, :monthly_limit}})
# => "Monthly AI usage limit exceeded"

# Service available
disabled_tooltip_message(%{can_edit_workflow: true, ai_limit_result: :ok})
# => nil

enable_attachment_options_component?()

Callback implementation for LightningWeb.Live.AiAssistant.ModeBehavior.enable_attachment_options_component?/0.

error_message(error)

@spec error_message(any()) :: String.t()
@spec error_message(any()) :: String.t()

Formats errors consistently for workflow template mode.

Leverages shared error handling to provide user-friendly error messages for template generation failures and validation issues.

Parameters

  • error - Error to format (changeset, atom, string, etc.)

Returns

Human-readable error message string.

Examples

error_message({:error, :service_unavailable})
# => "AI service is temporarily unavailable. Please try again."

error_message(%Ecto.Changeset{})
# => "Template validation failed: [specific field errors]"

get_session!(map)

@spec get_session!(map()) :: map()

Retrieves a workflow template session with full context.

Loads the complete session including all messages, generated templates, and conversation history for template generation continuation.

Examples

session = WorkflowTemplate.get_session!(%{chat_session_id: session_id})
# Session includes all messages and any generated workflow YAML

handle_response_generated(assigns, session_or_message, ui_callback)

@spec handle_response_generated(map(), map(), function()) :: map()

Handles post-processing after workflow template generation.

Detects when the AI has generated workflow YAML code and triggers appropriate UI updates to enable template application and preview.

Parameters

  • assigns - Current LiveView assigns
  • session_or_message - Updated session or new message with potential YAML
  • ui_callback - Function for triggering UI updates

Examples

# When YAML is generated
handle_response_generated(assigns, session_with_yaml, ui_callback)
# Triggers :workflow_code_generated event with the YAML content

# When no YAML is present
handle_response_generated(assigns, session_without_yaml, ui_callback)
# No UI update triggered, returns assigns unchanged

input_placeholder()

@spec input_placeholder() :: String.t()

Provides workflow-specific placeholder text for input guidance.

Encourages users to describe their desired workflow functionality in natural language for AI template generation.

list_sessions(map, sort_direction, opts \\ [])

@spec list_sessions(map(), atom(), keyword()) :: %{
  sessions: [map()],
  pagination: map()
}

Lists workflow template sessions for the current project.

Retrieves paginated sessions associated with the project, filtered to show only workflow template generation conversations.

Examples

# Load recent template sessions
%{sessions: sessions, pagination: meta} = WorkflowTemplate.list_sessions(
  %{project: project},
  :desc,
  limit: 15
)

metadata()

@spec metadata() :: map()

Provides metadata for the workflow template generation mode.

Returns information used by the UI to display mode selection options and identify the mode's template generation capabilities.

more_sessions?(map, current_count)

@spec more_sessions?(map(), integer()) :: boolean()

Checks if more workflow template sessions exist for the project.

Determines if additional sessions are available beyond the current count for implementing pagination controls.

Examples

if WorkflowTemplate.more_sessions?(%{project: project}, 15) do
  # Show "Load More" button
end

on_session_start(socket, ui_callback)

@spec on_session_start(map(), function()) :: map()

Initializes the UI state when starting a new template session.

Clears any existing template data and prepares the interface for new workflow generation.

Parameters

  • socket - LiveView socket with current state
  • ui_callback - Function for triggering UI updates

Examples

# Starting new template session
on_session_start(socket, ui_callback)
# Triggers :clear_template to reset UI state

query(session, content, opts)

@spec query(map(), String.t(), map()) :: {:ok, map()} | {:error, any()}

Processes workflow template requests through the AI service.

Sends user descriptions and requirements to the specialized workflow generation AI service, which returns complete YAML templates and explanatory content.

Parameters

  • session - Session with conversation history and context
  • content - User's workflow description or modification request

Examples

# Generate new workflow template
{:ok, updated_session} = WorkflowTemplate.query(
  session,
  "Create a workflow that processes CSV files uploaded to Google Drive"
)

# Modify existing template
{:ok, updated_session} = WorkflowTemplate.query(
  session,
  "Add error notifications to Slack when the sync fails"
)

query_options(changeset)

Callback implementation for LightningWeb.Live.AiAssistant.ModeBehavior.query_options/1.

save_message(map, content)

@spec save_message(map(), String.t()) :: {:ok, map()} | {:error, any()}

Saves a user message to the workflow template session.

Adds user requests, modifications, or questions to the conversation history for AI processing and template generation.

Examples

# Save template modification request
{:ok, updated_session} = WorkflowTemplate.save_message(
  %{session: session, current_user: user},
  "Add data validation before database insertion"
)

# Save error correction request
{:ok, updated_session} = WorkflowTemplate.save_message(
  %{session: session, current_user: user},
  "Fix the cron expression to run every 2 hours"
)

supports_template_generation?()

@spec supports_template_generation?() :: boolean()

Indicates that workflow mode supports template generation.

This mode's primary purpose is generating workflow templates, enabling UI features like template application and export.

validate_form_changeset(params)

Callback implementation for LightningWeb.Live.AiAssistant.ModeBehavior.validate_form_changeset/1.