Per-request ordered gen_ai.* event buffer.
Two ETS tables: a seq counter and an ordered-set event store, both keyed
by a request id. Table names default to :mimir_turn_seq /
:mimir_turn_events and are configurable via :mimir, :turn_events_tables
(a {seq_table, buf_table} tuple) — set this if an embedder needs to run
more than one buffer instance side by side.
The "current" request id is held in the process dictionary; the embedder
sets it (put_current/1) at the start of a request, telemetry handlers and
the embedder append under it (append_current/2), and the embedder drains
the buffer with take/1 (or take_current/0) when it meters the request.
A TTL sweep reclaims buffers orphaned by a crashed request.
Summary
Functions
Append one event under rid. No-op for a nil id. Never raises.
Append under the process-current id.
Returns a specification to start this module under a supervisor.
The process-local current request id, or nil.
The persisted event envelope — the single source of the shape stored by
callers that persist gen_ai events (e.g. a route log) and returned by
take/1. Anything writing an event row outside the buffer builds it here
so the envelope vocabulary cannot fork.
Set the process-local current request id (the buffer key).
Start the buffer's table owner. opts are unused; accepted for supervision-tree conformance.
Take (and clear) the seq-ordered event list for rid.
Take the current id's list and clear the current id.
Types
Functions
@spec append(request_id() | nil, String.t() | atom(), map()) :: :ok
Append one event under rid. No-op for a nil id. Never raises.
Append under the process-current id.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec current() :: request_id() | nil
The process-local current request id, or nil.
The persisted event envelope — the single source of the shape stored by
callers that persist gen_ai events (e.g. a route log) and returned by
take/1. Anything writing an event row outside the buffer builds it here
so the envelope vocabulary cannot fork.
@spec put_current(request_id() | any()) :: :ok
Set the process-local current request id (the buffer key).
@spec start_link(keyword()) :: GenServer.on_start()
Start the buffer's table owner. opts are unused; accepted for supervision-tree conformance.
@spec take(request_id() | nil) :: [map()]
Take (and clear) the seq-ordered event list for rid.
@spec take_current() :: [map()]
Take the current id's list and clear the current id.