Lightning.AiAssistant.ChatMessage (Lightning v2.14.5-pre1)

View Source

Represents a message within an AI chat session.

Messages can be from users (role: :user) or from the AI assistant (role: :assistant). User messages start with :pending status and are updated based on processing results. Assistant messages typically have :success status when created.

Schema Fields

  • content - The text content of the message (required, 1-10,000 characters)
  • code - Optional code associated with the message (e.g., generated workflows)
  • role - Who sent the message: :user or :assistant
  • status - Processing status: :pending, :success, :error, or :cancelled
  • is_deleted - Soft deletion flag (defaults to false)
  • is_public - Whether the message is publicly visible (defaults to true)
  • chat_session_id - Reference to the parent chat session
  • user_id - Reference to the user who sent the message (required for user messages)

Summary

Functions

Creates a changeset for a chat message.

Creates a changeset for updating message status.

Types

role()

@type role() :: :user | :assistant

status()

@type status() :: :pending | :processing | :success | :error | :cancelled

t()

@type t() :: %Lightning.AiAssistant.ChatMessage{
  __meta__: term(),
  chat_session: term(),
  chat_session_id: Ecto.UUID.t(),
  code: String.t() | nil,
  content: String.t() | nil,
  id: Ecto.UUID.t(),
  inserted_at: DateTime.t(),
  is_deleted: boolean(),
  is_public: boolean(),
  processing_completed_at: DateTime.t() | nil,
  processing_started_at: DateTime.t() | nil,
  role: role(),
  status: status(),
  updated_at: DateTime.t(),
  user: term(),
  user_id: Ecto.UUID.t() | nil
}

Functions

changeset(chat_message, attrs)

Creates a changeset for a chat message.

Parameters

  • chat_message - The ChatMessage struct to update (typically %ChatMessage{})
  • attrs - Map of attributes to set/update

Validation Rules

  • content and role are required
  • content must be between 1 and 10,000 characters
  • User messages (role: :user) require an associated user
  • Status defaults based on role: :pending for users, :success for assistant
  • If status is explicitly provided, it takes precedence over role-based defaults

status_changeset(chat_message, status)

Creates a changeset for updating message status.

This is a focused changeset that only updates the status field, useful for updating message state during processing.

Parameters

  • chat_message - The existing ChatMessage struct
  • status - New status (:pending, :success, :error, or :cancelled)

Examples

# Mark message as successful
ChatMessage.status_changeset(message, :success)

# Mark message as failed
ChatMessage.status_changeset(message, :error)