ADK.Session (ADK v0.0.1-alpha.1)

Copy Markdown View Source

Session GenServer — one process per active session.

Holds session state (key-value map) and event history.

Persistence

Sessions can optionally persist to a store. Pass the :store option to start_link/1:

ADK.Session.start_link(
  app_name: "my_app",
  user_id: "user1",
  session_id: "sess1",
  store: {ADK.Session.Store.InMemory, []}
)

On init, the session will attempt to load existing data from the store. Call save/1 to persist the current state, or configure auto_save: true to auto-save on process termination.

Summary

Functions

Append an event to the session.

Returns a specification to start this module under a supervisor.

Get the full session struct.

Get all session state as a map.

Get all events from the session.

Get a value from session state.

Look up an existing session by app_name, user_id, and session_id.

Put a value into session state.

Persist the current session state to the configured store.

Start a session process.

Start a session under ADK.SessionSupervisor.

Subscribe to session events. The subscriber will receive {:adk_session_event, event} messages.

Unsubscribe from session events.

Types

t()

@type t() :: %ADK.Session{
  app_name: String.t(),
  events: [ADK.Event.t()],
  id: String.t(),
  state: map(),
  user_id: String.t()
}

Functions

append_event(pid, event)

@spec append_event(pid() | atom(), ADK.Event.t()) :: :ok

Append an event to the session.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get(pid)

@spec get(pid() | atom()) :: {:ok, t()}

Get the full session struct.

get_all_state(pid)

@spec get_all_state(pid() | atom()) :: map()

Get all session state as a map.

get_events(pid)

@spec get_events(pid() | atom()) :: [ADK.Event.t()]

Get all events from the session.

get_state(pid, key)

@spec get_state(pid() | atom(), term()) :: term() | nil

Get a value from session state.

lookup(app_name, user_id, session_id)

@spec lookup(String.t(), String.t(), String.t()) :: {:ok, pid()} | :error

Look up an existing session by app_name, user_id, and session_id.

Returns {:ok, pid} or :error.

put_state(pid, key, value)

@spec put_state(pid() | atom(), term(), term()) :: :ok

Put a value into session state.

save(pid)

@spec save(pid() | atom()) :: :ok | {:error, term()}

Persist the current session state to the configured store.

start_link(opts)

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

Start a session process.

start_supervised(opts)

@spec start_supervised(keyword()) :: {:ok, pid()} | {:error, term()}

Start a session under ADK.SessionSupervisor.

Returns {:ok, pid} or {:error, reason}.

subscribe(pid)

@spec subscribe(pid() | atom()) :: :ok

Subscribe to session events. The subscriber will receive {:adk_session_event, event} messages.

unsubscribe(pid)

@spec unsubscribe(pid() | atom()) :: :ok

Unsubscribe from session events.