Lightning.AiAssistant.MessageProcessor (Lightning v2.19.0-pre)

View Source

Asynchronous message processor for AI Assistant using Oban.

This module handles the background processing of AI chat messages, ensuring reliable and scalable AI interactions. It processes messages outside of the web request lifecycle, providing better user experience and system resilience.

Summary

Functions

Tells a session one of its messages has failed.

Handles exceptions for ai_assistant Oban jobs.

Handles :stop events for ai_assistant Oban jobs.

The same ceiling as timeout/1, for callers that have no job in hand.

Processes an AI assistant message asynchronously.

How long one AI job may run, in milliseconds.

Updates a message's status and broadcasts the change.

Functions

broadcast_message_error(session_id, message_id)

@spec broadcast_message_error(Ecto.UUID.t(), Ecto.UUID.t()) :: :ok

Tells a session one of its messages has failed.

For callers that write the status themselves - the reaper uses a guarded update rather than a read-modify-write - and so cannot go through update_message_status/3.

handle_ai_assistant_exception(measure, meta)

Handles exceptions for ai_assistant Oban jobs.

Parameters

  • measure — A map containing job execution metrics (duration, memory, reductions).
  • meta — A map containing job metadata (job, error, stacktrace, etc.).

handle_ai_assistant_stop(measure, meta)

Handles :stop events for ai_assistant Oban jobs.

This function is invoked when a job in the ai_assistant queue stops with a non-success state (e.g., :discard, :cancelled, or other custom stop reasons).

Jobs with the :success state are ignored.

Parameters

  • measure — A map containing job execution metrics (duration, memory, reductions).
  • meta — A map containing job metadata (job, state, etc.).

job_timeout()

@spec job_timeout() :: pos_integer()

The same ceiling as timeout/1, for callers that have no job in hand.

perform(job)

@spec perform(Oban.Job.t()) :: :ok

Processes an AI assistant message asynchronously.

This is the main entry point called by Oban. It handles the complete lifecycle of message processing including status updates and broadcasting.

Arguments

  • job - Oban job containing message_id and session_id in args

Returns

Always returns :ok to prevent Oban retries, even on errors. Errors are handled by updating message status and logging.

timeout(job)

@spec timeout(Oban.Job.t()) :: pos_integer()

How long one AI job may run, in milliseconds.

The three Apollo timeouts added together, plus a 10-second buffer, so the transport gets to fail first and say why.

update_message_status(message, status, failure \\ nil)

Updates a message's status and broadcasts the change.

This function updates the message status in the database, fetches the updated session with all associations, and broadcasts the status change to connected clients via Phoenix PubSub.

Parameters

  • message - The ChatMessage struct to update
  • status - The new status atom (:processing, :success, or :error)
  • failure - {category, sentence} to record why it failed, or nil to leave the failure columns as they are

Returns

{:ok, updated_session, updated_message} - Tuple with the updated session and message structs