LightningWeb.Live.AiAssistant.ModeBehavior behaviour (Lightning v2.14.5-pre1)

View Source

Defines the contract for AI Assistant interaction modes.

Each mode implements different AI assistance functionality (e.g., job code help, workflow generation). The component delegates all mode-specific decisions to the implementing module.

Summary

Callbacks

Determines if the chat input should be disabled.

Returns a display title for the chat session.

Creates a new chat session with initial content.

Returns tooltip text when chat is disabled.

Formats an error for display.

Extracts options from a validated changeset.

Returns the form module for this mode.

Retrieves an existing session by ID from assigns.

Returns placeholder text for the chat input.

Lists chat sessions with pagination support.

Returns mode metadata for UI display.

Checks if more sessions are available beyond the current count.

Called when an AI response is received.

Called when a message is selected in the UI.

Called when a message is sent.

Called when a chat session is closed.

Called when a chat session is opened.

Sends a query to the AI service.

Renders mode-specific configuration form elements.

Saves a user message to the current session.

Validates form parameters.

Types

assigns()

@type assigns() :: %{required(atom()) => any()}

error_reason()

@type error_reason() :: :validation_failed | :unauthorized | :not_found | term()

job()

@type job() :: Lightning.Workflows.Job.t()

list_opts()

@type list_opts() :: [
  offset: non_neg_integer(),
  limit: pos_integer(),
  search: String.t() | nil
]

mode_metadata()

@type mode_metadata() :: %{
  optional(:category) => String.t(),
  optional(:features) => [String.t()],
  name: String.t(),
  description: String.t(),
  icon: String.t(),
  chat_param: String.t()
}

project()

@type project() :: Lightning.Projects.Project.t()

session()

session_id()

@type session_id() :: Ecto.UUID.t()

session_list()

@type session_list() :: %{
  sessions: [session()],
  pagination: LightningWeb.Live.AiAssistant.PaginationMeta.t()
}

session_result()

@type session_result() :: {:ok, session()} | {:error, error_reason()}

sort_direction()

@type sort_direction() :: :asc | :desc

update_result()

@type update_result() ::
  {:ok, Phoenix.LiveView.Socket.t()} | {:unhandled, Phoenix.LiveView.Socket.t()}

user()

@type user() :: Lightning.Accounts.User.t()

Callbacks

chat_input_disabled?(assigns)

@callback chat_input_disabled?(assigns()) :: boolean()

Determines if the chat input should be disabled.

chat_title(session)

(optional)
@callback chat_title(session()) :: String.t()

Returns a display title for the chat session.

create_session(assigns, t, opts)

@callback create_session(assigns(), String.t(), opts :: Keyword.t()) :: session_result()

Creates a new chat session with initial content.

disabled_tooltip_message(assigns)

(optional)
@callback disabled_tooltip_message(assigns()) :: String.t() | nil

Returns tooltip text when chat is disabled.

error_message(any)

(optional)
@callback error_message(any()) :: String.t()

Formats an error for display.

extract_form_options(t)

@callback extract_form_options(Ecto.Changeset.t()) :: Keyword.t()

Extracts options from a validated changeset.

form_module()

@callback form_module() :: module()

Returns the form module for this mode.

get_session!(assigns)

@callback get_session!(assigns()) :: session() | no_return()

Retrieves an existing session by ID from assigns.

input_placeholder()

(optional)
@callback input_placeholder() :: String.t()

Returns placeholder text for the chat input.

list_sessions(assigns, sort_direction, list_opts)

@callback list_sessions(assigns(), sort_direction(), list_opts()) :: session_list()

Lists chat sessions with pagination support.

metadata()

@callback metadata() :: mode_metadata()

Returns mode metadata for UI display.

more_sessions?(assigns, current_count)

@callback more_sessions?(assigns(), current_count :: integer()) :: boolean()

Checks if more sessions are available beyond the current count.

on_message_received(t, session)

(optional)
@callback on_message_received(Phoenix.LiveView.Socket.t(), session()) ::
  Phoenix.LiveView.Socket.t()

Called when an AI response is received.

on_message_selected(t, t)

Called when a message is selected in the UI.

on_message_send(t)

@callback on_message_send(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()

Called when a message is sent.

on_session_close(t)

(optional)
@callback on_session_close(Phoenix.LiveView.Socket.t()) :: Phoenix.LiveView.Socket.t()

Called when a chat session is closed.

on_session_open(t, session)

(optional)
@callback on_session_open(Phoenix.LiveView.Socket.t(), session()) ::
  Phoenix.LiveView.Socket.t()

Called when a chat session is opened.

query(session, t, t)

@callback query(session(), String.t(), Keyword.t()) :: session_result()

Sends a query to the AI service.

render_config_form(assigns)

(optional)
@callback render_config_form(assigns()) :: Phoenix.LiveView.Rendered.t() | nil

Renders mode-specific configuration form elements.

save_message(assigns, t)

@callback save_message(assigns(), String.t()) :: session_result()

Saves a user message to the current session.

validate_form(params)

@callback validate_form(params :: map()) :: Ecto.Changeset.t()

Validates form parameters.