LemonCore.SessionKey (lemon_core v0.1.0)

View Source

Session key generation and parsing.

Session keys provide a stable identifier for routing and state management.

Canonical Formats

  • Main: agent:<agent_id>:main[:sub:<sub_id>]
  • Channel: agent:<agent_id>:<channel_id>:<account_id>:<peer_kind>:<peer_id>[:thread:<thread_id>][:sub:<sub_id>]

Notes

Session keys are strictly validated against the canonical formats above.

Summary

Functions

Extract the agent ID from a session key.

Returns the list of allowed peer_kind strings.

Generate a channel peer session key.

Check if a session key is for a channel peer.

Generate a main session key for an agent.

Check if a session key is for the main session.

Parse a session key into its components.

Check if a session key is valid.

Types

parsed()

@type parsed() :: %{
  agent_id: binary(),
  kind: :main | :channel_peer,
  channel_id: binary() | nil,
  account_id: binary() | nil,
  peer_kind: atom() | nil,
  peer_id: binary() | nil,
  thread_id: binary() | nil,
  sub_id: binary() | nil
}

Functions

agent_id(session_key)

@spec agent_id(binary()) :: binary() | nil

Extract the agent ID from a session key.

Examples

iex> LemonCore.SessionKey.agent_id("agent:my_bot:main")
"my_bot"

iex> LemonCore.SessionKey.agent_id("invalid")
nil

allowed_peer_kinds()

@spec allowed_peer_kinds() :: [String.t()]

Returns the list of allowed peer_kind strings.

channel_peer(opts)

@spec channel_peer(opts :: map()) :: binary()

Generate a channel peer session key.

Required keys in opts:

  • :agent_id
  • :channel_id
  • :account_id
  • :peer_kind (:dm, :group, :channel, :unknown)
  • :peer_id

Optional:

  • :thread_id
  • :sub_id

channel_peer?(session_key)

@spec channel_peer?(binary()) :: boolean()

Check if a session key is for a channel peer.

main(agent_id)

@spec main(agent_id :: binary()) :: binary()

Generate a main session key for an agent.

Examples

iex> LemonCore.SessionKey.main("my_agent")
"agent:my_agent:main"

main?(session_key)

@spec main?(binary()) :: boolean()

Check if a session key is for the main session.

parse(session_key)

@spec parse(binary()) :: parsed() | {:error, :invalid} | {:error, :invalid_peer_kind}

Parse a session key into its components.

Returns the parsed components or {:error, :invalid} for malformed keys.

valid?(session_key)

@spec valid?(binary()) :: boolean()

Check if a session key is valid.

Examples

iex> LemonCore.SessionKey.valid?("agent:test:main")
true

iex> LemonCore.SessionKey.valid?("invalid")
false