WardrikCore.Context (wardrik_core v0.1.0-rc.3)

Copy Markdown

The state passed through a turn's middleware.

step is :turn, :generate (a model call), {:tool_call, call}, or {:step, name} for your own. Middleware reads and changes the rest, such as messages, response and attributes.

Summary

Functions

Returns who the turn acts for: the sender of the latest message, or else the agent's own principal, or nil.

Sets an attribute on the current step, like an OpenTelemetry span attribute.

Returns a capability or middleware entry as {module, opts}. A bare module gets [].

Returns the private value under key, or default.

Returns the labels in the turn's messages, or in messages, sorted and unique.

Adds each of measurements, such as those WardrikCore.evaluate/2 returns.

Adds amount to the measurement key, such as tokens or dollars spent.

Puts value in the private data under key. Use your own module as the key.

Runs fun through the context's middleware. The first in the list runs outermost.

Runs fun through the middleware as step, in a span of its own.

Returns the tool call names, or nil.

Updates the private value under key with fun. Puts default if there's none yet.

Types

entry()

@type entry() :: module() | {module(), term()}

step()

@type step() ::
  :turn
  | :generate
  | {:tool_call, WardrikCore.ToolCall.t()}
  | {:step, String.t()}

t()

@type t() :: %WardrikCore.Context{
  agent_id: String.t(),
  attributes: map(),
  call_started: (String.t() -> :ok) | nil,
  capabilities: [entry()],
  checkpoint: (map() -> :ok) | nil,
  conversation: String.t() | nil,
  credentials: entry() | nil,
  events: entry() | nil,
  instructions: String.t() | nil,
  keys: entry() | nil,
  max_rounds: pos_integer() | :infinity,
  measurements: %{optional(term()) => number()},
  messages: [ReqLLM.Message.t()],
  metadata: map(),
  middleware: [entry()],
  model: LLMDB.Model.t() | String.t(),
  model_timeouts: keyword(),
  on_chunk: [(ReqLLM.StreamChunk.t() -> any())],
  prices: %{optional(String.t()) => WardrikCore.price()},
  principal: WardrikCore.Principal.t() | nil,
  private: map(),
  response: ReqLLM.Response.t() | {:ok, term()} | {:error, term()} | nil,
  resume: WardrikCore.Resume.t() | nil,
  retries: WardrikCore.Retry.t(),
  span: WardrikCore.Span.t() | nil,
  step: step() | nil,
  subagent: String.t() | nil,
  timeout: timeout(),
  tools: [WardrikCore.Tool.t()]
}

Functions

acting_for(context)

@spec acting_for(t()) :: WardrikCore.Principal.t() | nil

Returns who the turn acts for: the sender of the latest message, or else the agent's own principal, or nil.

attribute(context, key, value)

@spec attribute(t(), String.t() | atom(), term()) :: t()

Sets an attribute on the current step, like an OpenTelemetry span attribute.

Context.attribute(context, "gen_ai.tool.name", call.name)

Use OpenTelemetry's names, or your own prefix.

entry(module)

@spec entry(entry()) :: {module(), term()}

Returns a capability or middleware entry as {module, opts}. A bare module gets [].

get_private(struct, key, default \\ nil)

@spec get_private(t(), term(), term()) :: term()

Returns the private value under key, or default.

labels(messages)

@spec labels(t() | [ReqLLM.Message.t()]) :: [String.t()]

Returns the labels in the turn's messages, or in messages, sorted and unique.

"outside" marks content nobody on the agent's side wrote, such as a web page. A label stays until the history is cleared.

measure(context, measurements)

@spec measure(t(), map()) :: t()

Adds each of measurements, such as those WardrikCore.evaluate/2 returns.

measure(context, key, amount)

@spec measure(t(), term(), number()) :: t()

Adds amount to the measurement key, such as tokens or dollars spent.

Context.measure(context, :usd, 0.01)

Measurements add up over the turn. Every model call measures input_tokens, output_tokens, and usd if the model has a price, or else counts unpriced_calls.

new(opts)

put_private(struct, key, value)

@spec put_private(t(), term(), term()) :: t()

Puts value in the private data under key. Use your own module as the key.

run(context, fun)

Runs fun through the context's middleware. The first in the list runs outermost.

run(context, step, fun)

Runs fun through the middleware as step, in a span of its own.

Context.run(context, {:step, "summarize"}, &summarize/1)

tool(context, tool_call)

@spec tool(t(), WardrikCore.ToolCall.t()) :: WardrikCore.Tool.t() | nil

Returns the tool call names, or nil.

update_private(struct, key, default, fun)

@spec update_private(t(), term(), term(), (term() -> term())) :: t()

Updates the private value under key with fun. Puts default if there's none yet.