Jido.AI.Reasoning.RequestLifecycle (Jido AI v2.2.0)

Copy Markdown View Source

Shared request lifecycle helpers for non-delegated reasoning strategies.

Summary

Functions

Emits normalized request lifecycle telemetry for a reasoning strategy.

Emits the public request-started signal and matching request telemetry.

Emits the terminal request signal and telemetry when a strategy completes or fails.

Extracts usage accounting from strategy state or its terminal result payload.

Infers a normalized error type atom from common runtime error shapes.

Tracks the active request identifier across non-delegated strategy transitions.

Types

lifecycle_event()

@type lifecycle_event() :: :start | :complete | :failed

Functions

emit_request_telemetry(strategy, state, event, request_id, opts \\ [])

@spec emit_request_telemetry(atom(), map(), lifecycle_event(), String.t(), keyword()) ::
  :ok

Emits normalized request lifecycle telemetry for a reasoning strategy.

Callers can override derived metadata such as usage, model, iteration, or llm call id through opts when the state does not yet contain that data.

emit_started(strategy, state, request_id, query, opts \\ [])

@spec emit_started(atom(), map(), String.t(), String.t(), keyword()) :: :ok

Emits the public request-started signal and matching request telemetry.

This is used by non-delegated strategies that own their request lifecycle directly rather than projecting it from worker runtime events.

emit_terminal(strategy, previous_state, new_state, opts \\ [])

@spec emit_terminal(atom(), map(), map(), keyword()) :: :ok

Emits the terminal request signal and telemetry when a strategy completes or fails.

A terminal event is emitted only on the transition into a completed or error status, and only when a request id can be resolved from state or options.

extract_usage(arg1)

@spec extract_usage(map()) :: map()

Extracts usage accounting from strategy state or its terminal result payload.

infer_error_type(reason)

@spec infer_error_type(term()) :: atom() | nil

Infers a normalized error type atom from common runtime error shapes.

This accepts both the new canonical envelope and legacy tuple or map forms that still appear at runtime boundaries during the 2.0 transition.

put_active_request_id(previous_state, new_state, request_id, active_statuses)

@spec put_active_request_id(map(), map(), String.t() | nil, [term()]) :: map()

Tracks the active request identifier across non-delegated strategy transitions.

The request id is retained while the state remains active and is cleared once the strategy reaches a terminal status.