H.Transcript (H v0.3.0)

Copy Markdown View Source

Pure Elixir reducer for conversation transcript SSE/PubSub frames. Applies document deltas with H.Document.apply_ops/2.

Summary

Functions

Applies a commit payload (decoded map or JSON binary).

Applies an ephemeral payload (decoded map or JSON binary) to docs["live"] without advancing seq or mutating committed_docs.

Applies a snapshot payload (decoded map or JSON binary).

Creates a new empty H.Transcript reducer state.

Prepends an older page of entries in ascending order without duplicates and updates before_entry_id and has_more.

Types

t()

@type t() :: %H.Transcript{
  before_entry_id: String.t() | nil,
  committed_docs: map(),
  conversation_id: String.t() | nil,
  docs: map(),
  entries: [map()],
  entry_ids: MapSet.t(String.t()),
  has_more: boolean(),
  seq: non_neg_integer() | nil,
  session_id: String.t() | nil
}

Functions

apply_commit(transcript, payload)

@spec apply_commit(t(), map() | binary()) ::
  {:ok, t(), %{appended_entries: [map()]}}
  | {:gap, t()}
  | {:stale, t()}
  | {:error, term()}

Applies a commit payload (decoded map or JSON binary).

Returns:

  • {:gap, t} when t.seq is nil or frame_seq > t.seq + 1 (caller should resnapshot)
  • {:stale, t} when frame_seq <= t.seq
  • {:ok, updated, %{appended_entries: new_entries}} when frame_seq == t.seq + 1

apply_ephemeral(transcript, payload)

@spec apply_ephemeral(t(), map() | binary()) ::
  {:ok, t()} | :ignored | {:error, term()}

Applies an ephemeral payload (decoded map or JSON binary) to docs["live"] without advancing seq or mutating committed_docs.

Returns :ignored when t.seq == nil or base_seq != t.seq.

apply_snapshot(transcript, payload)

@spec apply_snapshot(t(), map() | binary()) :: {:ok, t()} | {:error, term()}

Applies a snapshot payload (decoded map or JSON binary).

Sets seq, deduplicates and orders entries, updates before_entry_id and has_more, and resets both committed_docs and docs.

new(opts \\ [])

@spec new(keyword() | map()) :: t()

Creates a new empty H.Transcript reducer state.

prepend_older_page(transcript, items, next_cursor)

@spec prepend_older_page(t(), [map()], String.t() | integer() | nil) :: t()

Prepends an older page of entries in ascending order without duplicates and updates before_entry_id and has_more.