PtcRunner.Context (PtcRunner v0.9.0)

Copy Markdown View Source

Manages context, memory, and tools for program execution.

  • ctx: External input data (read-only)
  • memory: Mutable state passed through evaluation
  • tools: Tool registry

See PtcRunner.SubAgent for usage in agentic loops.

Summary

Types

t()

Context structure containing external data, memory, and tool registry.

Functions

Retrieves a value from context (external data).

Retrieves a value from memory (mutable state).

Creates a new context with external data, memory, tools, and optional turn history.

Sets a value in memory.

Types

t()

@type t() :: %PtcRunner.Context{
  ctx: map(),
  journal: map() | nil,
  memory: map(),
  tools: map(),
  turn_history: list()
}

Context structure containing external data, memory, and tool registry.

  • journal: Optional journal map for (task) idempotent execution. When non-nil, task results are cached by ID. When nil, tasks execute without caching (with a trace warning).

Functions

get_ctx(context, name)

@spec get_ctx(t(), String.t()) :: {:ok, any()} | {:error, {atom(), String.t()}}

Retrieves a value from context (external data).

Returns {:ok, nil} if key doesn't exist.

Examples

iex> ctx = PtcRunner.Context.new(%{"users" => [1, 2, 3]})
iex> PtcRunner.Context.get_ctx(ctx, "users")
{:ok, [1, 2, 3]}

iex> ctx = PtcRunner.Context.new()
iex> PtcRunner.Context.get_ctx(ctx, "missing")
{:ok, nil}

get_memory(context, name)

@spec get_memory(t(), String.t()) :: {:ok, any()} | {:error, {atom(), String.t()}}

Retrieves a value from memory (mutable state).

Returns {:ok, nil} if key doesn't exist.

Examples

iex> ctx = PtcRunner.Context.new(%{}, %{"counter" => 42})
iex> PtcRunner.Context.get_memory(ctx, "counter")
{:ok, 42}

iex> ctx = PtcRunner.Context.new()
iex> PtcRunner.Context.get_memory(ctx, "missing")
{:ok, nil}

new(ctx \\ %{}, memory \\ %{}, tools \\ %{}, turn_history \\ [], journal \\ nil)

@spec new(map(), map(), map(), list(), map() | nil) :: t()

Creates a new context with external data, memory, tools, and optional turn history.

Examples

iex> ctx = PtcRunner.Context.new(%{"users" => [1, 2, 3]})
iex> ctx.ctx
%{"users" => [1, 2, 3]}

iex> ctx = PtcRunner.Context.new(%{}, %{"counter" => 0})
iex> ctx.memory
%{"counter" => 0}

put_memory(context, name, value)

@spec put_memory(t(), String.t(), any()) :: t()

Sets a value in memory.

Examples

iex> ctx = PtcRunner.Context.new()
iex> ctx = PtcRunner.Context.put_memory(ctx, "result", 100)
iex> ctx.memory
%{"result" => 100}