ADK.Session.State (adk_ex v1.1.0)

Copy Markdown View Source

Utilities for working with prefixed session state keys.

State keys use prefixes to control their scope:

  • app: — shared across all users and sessions for an app
  • user: — shared across all sessions for a specific user
  • temp: — temporary, discarded after each invocation
  • (no prefix) — session-local state

Summary

Functions

Splits a state delta map into {app_delta, user_delta, session_delta}.

Gets a value from state by key.

Merges app, user, and session state maps back into a single map with prefixes.

Sets a value in state by key.

Returns the scope of a state key.

Removes all keys with the temp prefix from a map.

Removes all temp-prefixed keys from a delta map.

Functions

app_prefix()

extract_deltas(delta)

@spec extract_deltas(map()) :: {map(), map(), map()}

Splits a state delta map into {app_delta, user_delta, session_delta}.

Strips prefixes from app/user keys. Discards temp keys entirely. Session-local keys (no prefix) are kept as-is.

get(state, key)

@spec get(map(), String.t()) :: any()

Gets a value from state by key.

merge_states(app_state, user_state, session_state)

@spec merge_states(map(), map(), map()) :: map()

Merges app, user, and session state maps back into a single map with prefixes.

put(state, key, value)

@spec put(map(), String.t(), any()) :: map()

Sets a value in state by key.

scope(arg1)

@spec scope(String.t()) :: :app | :user | :temp | :session

Returns the scope of a state key.

Examples

iex> ADK.Session.State.scope("app:model")
:app
iex> ADK.Session.State.scope("user:pref")
:user
iex> ADK.Session.State.scope("temp:scratch")
:temp
iex> ADK.Session.State.scope("counter")
:session

strip_temp(state)

@spec strip_temp(map()) :: map()

Removes all keys with the temp prefix from a map.

temp_prefix()

trim_temp_delta(delta)

@spec trim_temp_delta(map()) :: map()

Removes all temp-prefixed keys from a delta map.

user_prefix()