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

View Source

Helper for managing Thread in agent state.

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

Example

alias Jido.Thread.Agent, as: ThreadAgent

# Ensure agent has a thread
agent = ThreadAgent.ensure(agent, metadata: %{user_id: "u1"})

# Append an entry
agent = ThreadAgent.append(agent, %{kind: :message, payload: %{text: "hi"}})

# Get the thread
thread = ThreadAgent.get(agent)

Summary

Functions

Append entry to agent's thread (ensures thread exists)

Ensure agent has a thread (initialize if missing)

Get thread from agent state

Check if agent has a thread

Returns the reserved key for thread storage

Put thread into agent state

Update thread using a function

Functions

append(agent, entry_or_entries, opts \\ [])

@spec append(Jido.Agent.t(), term(), keyword()) :: Jido.Agent.t()

Append entry to agent's thread (ensures thread exists)

ensure(agent, opts \\ [])

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

Ensure agent has a thread (initialize if missing)

get(agent, default \\ nil)

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

Get thread from agent state

has_thread?(agent)

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

Check if agent has a thread

key()

@spec key() :: atom()

Returns the reserved key for thread storage

put(agent, thread)

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

Put thread into agent state

update(agent, fun)

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

Update thread using a function