Jido.Memory.Agent (Jido v2.0.0-rc.4)

View Source

Helper for managing Memory in agent state.

Memory is stored at the reserved key :__memory__ in agent.state. This follows the same pattern as :__thread__ for thread state and :__strategy__ for strategy state.

Provides generic space operations only. Domain-specific wrappers (world model, task lists, etc.) should be built in your own modules on top of these primitives.

Example

alias Jido.Memory.Agent, as: MemoryAgent

# Ensure agent has memory
agent = MemoryAgent.ensure(agent)

# Work with map spaces
agent = MemoryAgent.put_in_space(agent, :world, :temperature, 22)
temp = MemoryAgent.get_in_space(agent, :world, :temperature)

# Work with list spaces
agent = MemoryAgent.append_to_space(agent, :tasks, %{id: "t1", text: "Check sensor"})

Summary

Functions

Append an item to a list space.

Delete a key from a map space.

Delete a space. Raises on reserved spaces.

Ensure agent has memory (initialize if missing).

Ensure a space exists with default data. Does not overwrite existing.

Get memory from agent state.

Check if agent has memory.

Check if a space exists.

Returns the reserved key for memory storage.

Put memory into agent state.

Put a key/value into a map space.

Put a space by name. Bumps container rev and updated_at.

Get a space by name.

Get the full spaces map.

Update memory using a function.

Update a space using a function. Bumps both space and container revisions.

Functions

append_to_space(agent, space_name, item)

@spec append_to_space(Jido.Agent.t(), atom(), term()) :: Jido.Agent.t()

Append an item to a list space.

delete_from_space(agent, space_name, key)

@spec delete_from_space(Jido.Agent.t(), atom(), term()) :: Jido.Agent.t()

Delete a key from a map space.

delete_space(agent, name, opts \\ [])

@spec delete_space(Jido.Agent.t(), atom(), keyword()) :: Jido.Agent.t()

Delete a space. Raises on reserved spaces.

ensure(agent, opts \\ [])

@spec ensure(
  Jido.Agent.t(),
  keyword()
) :: Jido.Agent.t()

Ensure agent has memory (initialize if missing).

ensure_space(agent, name, default_data)

@spec ensure_space(Jido.Agent.t(), atom(), map() | list()) :: Jido.Agent.t()

Ensure a space exists with default data. Does not overwrite existing.

get(agent, default \\ nil)

@spec get(Jido.Agent.t(), Jido.Memory.t() | nil) :: Jido.Memory.t() | nil

Get memory from agent state.

get_in_space(agent, space_name, key, default \\ nil)

@spec get_in_space(Jido.Agent.t(), atom(), term(), term()) :: term()

Get a key from a map space.

has_memory?(agent)

@spec has_memory?(Jido.Agent.t()) :: boolean()

Check if agent has memory.

has_space?(agent, name)

@spec has_space?(Jido.Agent.t(), atom()) :: boolean()

Check if a space exists.

key()

@spec key() :: atom()

Returns the reserved key for memory storage.

put(agent, memory)

@spec put(Jido.Agent.t(), Jido.Memory.t()) :: Jido.Agent.t()

Put memory into agent state.

put_in_space(agent, space_name, key, value)

@spec put_in_space(Jido.Agent.t(), atom(), term(), term()) :: Jido.Agent.t()

Put a key/value into a map space.

put_space(agent, name, space, opts \\ [])

@spec put_space(Jido.Agent.t(), atom(), Jido.Memory.Space.t(), keyword()) ::
  Jido.Agent.t()

Put a space by name. Bumps container rev and updated_at.

space(agent, name)

@spec space(Jido.Agent.t(), atom()) :: Jido.Memory.Space.t() | nil

Get a space by name.

spaces(agent)

@spec spaces(Jido.Agent.t()) :: map() | nil

Get the full spaces map.

update(agent, fun)

@spec update(Jido.Agent.t(), (Jido.Memory.t() | nil -> Jido.Memory.t())) ::
  Jido.Agent.t()

Update memory using a function.

update_space(agent, name, fun, opts \\ [])

@spec update_space(
  Jido.Agent.t(),
  atom(),
  (Jido.Memory.Space.t() -> Jido.Memory.Space.t()),
  keyword()
) ::
  Jido.Agent.t()

Update a space using a function. Bumps both space and container revisions.