Mimir.TurnEvents (Mimir v0.1.0)

Copy Markdown View Source

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

request_id()

@type request_id() :: String.t() | integer()

Functions

append(rid, type, gen_ai)

@spec append(request_id() | nil, String.t() | atom(), map()) :: :ok

Append one event under rid. No-op for a nil id. Never raises.

append_current(type, gen_ai)

@spec append_current(String.t() | atom(), map()) :: :ok

Append under the process-current id.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

current()

@spec current() :: request_id() | nil

The process-local current request id, or nil.

envelope(seq, ts, type, gen_ai)

@spec envelope(non_neg_integer(), integer(), String.t() | atom(), map()) :: map()

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.

put_current(rid)

@spec put_current(request_id() | any()) :: :ok

Set the process-local current request id (the buffer key).

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Start the buffer's table owner. opts are unused; accepted for supervision-tree conformance.

take(rid)

@spec take(request_id() | nil) :: [map()]

Take (and clear) the seq-ordered event list for rid.

take_current()

@spec take_current() :: [map()]

Take the current id's list and clear the current id.