H.Document (H v0.3.0)

Copy Markdown View Source

Versioned pure-functional conversation documents (agent, live, inbox, usage), JSON delta operations, and fork policy resolution.

Summary

Functions

Accumulates turn usage metrics into the "usage" document and returns {new_usage, ops}.

Appends text at a path inside a document and returns {new_doc, [op]}.

Applies a single JSON delta operation (replace, r, set, append_text, delete).

Applies a list of JSON delta operations to a document immutably.

Applies validated configuration updates to an "agent" document and emits minimal ["set", [key], val] delta ops.

Returns the definition map for a built-in document kind.

Returns metadata for all built-in document kinds.

Deletes a path inside a document and returns {new_doc, [op]}.

Computes initial documents for a forked conversation at at_entry_seq.

Returns the initial value for a built-in conversation document.

Replaces a document value and emits ["replace", value].

Sets a path inside a document and returns {new_doc, [op]}.

Unwraps a stored document record map, falling back to initial/2 when nil.

Wraps a document value in a storage record map.

Types

op()

@type op() :: [String.t() | term()]

Functions

accumulate_usage(usage_doc, turn_usage)

@spec accumulate_usage(map(), map()) :: {map(), [op()]}

Accumulates turn usage metrics into the "usage" document and returns {new_usage, ops}.

append_text(doc, path, delta)

@spec append_text(term(), [String.t() | non_neg_integer()], String.t()) ::
  {term(), [op()]}

Appends text at a path inside a document and returns {new_doc, [op]}.

apply_op(doc, arg2)

@spec apply_op(term(), op()) :: term()

Applies a single JSON delta operation (replace, r, set, append_text, delete).

apply_ops(doc, ops)

@spec apply_ops(term(), [op()]) :: term()

Applies a list of JSON delta operations to a document immutably.

configure_agent(agent, updates)

@spec configure_agent(map(), map()) :: {map(), [op()]}

Applies validated configuration updates to an "agent" document and emits minimal ["set", [key], val] delta ops.

definition(kind)

@spec definition(String.t()) :: {:ok, map()} | :error

Returns the definition map for a built-in document kind.

definitions()

@spec definitions() :: %{required(String.t()) => map()}

Returns metadata for all built-in document kinds.

delete(doc, path)

@spec delete(term(), [String.t() | non_neg_integer()]) :: {term(), [op()]}

Deletes a path inside a document and returns {new_doc, [op]}.

fork_docs(store, parent_cid, at_entry_seq, opts \\ [])

@spec fork_docs(H.Storage.t(), pos_integer(), non_neg_integer(), keyword()) ::
  {:ok, %{required(String.t()) => term()}, H.Storage.t()} | {:error, term()}

Computes initial documents for a forked conversation at at_entry_seq.

"agent" uses fork: :as_of (read from storage at as_of_seq: at_entry_seq), while "live", "inbox", and "usage" use fork: :initial.

initial(kind, opts \\ [])

@spec initial(String.t(), keyword()) :: term()

Returns the initial value for a built-in conversation document.

replace(doc, new_value)

@spec replace(term(), term()) :: {term(), [op()]}

Replaces a document value and emits ["replace", value].

set(doc, path, value)

@spec set(term(), [String.t() | non_neg_integer()], term()) :: {term(), [op()]}

Sets a path inside a document and returns {new_doc, [op]}.

unwrap(record, kind, opts \\ [])

@spec unwrap(nil | map(), String.t(), keyword()) :: term()

Unwraps a stored document record map, falling back to initial/2 when nil.

wrap(kind, value)

@spec wrap(String.t(), term()) :: map()

Wraps a document value in a storage record map.