AI.Agent behaviour (fnord v0.8.82)

View Source

Behavior for AI agents that process instructions and return responses.

This behavior defines the contract between the coordinator and specialized agents, ensuring consistent interfaces and proper error handling across the agent system.

Summary

Functions

Delegate to AI.Completion.get/1 with the agent's name included in the args. Intended to be called by implementors of AI.Agent when they need to generate completions as part of their response processing.

Delegate to the agent implementation's get_response/1 function. Includes the agent in the args map.

Create a new agent instance.

Delegate to AI.Completion.tools_used/1 to extract the tools used from a completion.

Types

t()

@type t() :: %AI.Agent{impl: module(), name: nil | binary(), named?: boolean()}

Callbacks

get_response(map)

@callback get_response(map()) :: {:ok, any()} | {:error, any()}

Functions

get_completion(agent, args)

@spec get_completion(
  t(),
  keyword()
) :: {:ok, AI.Completion.t()} | {:error, any()}

Delegate to AI.Completion.get/1 with the agent's name included in the args. Intended to be called by implementors of AI.Agent when they need to generate completions as part of their response processing.

get_response(agent, args)

@spec get_response(t(), map()) :: {:ok, any()} | {:error, any()}

Delegate to the agent implementation's get_response/1 function. Includes the agent in the args map.

The agent's name is managed here, checking out a name from Services.NamePool if the agent is named but doesn't yet have a name, or associating the agent's existing name if it does. The name is checked back in after the response is generated.

The call is wrapped in a Task to provide a global identifier for logging and tracing purposes, which is associated with the agent's name.

new(impl, opts \\ [])

@spec new(
  module(),
  keyword()
) :: t()

Create a new agent instance.

If :named? is set to false, the agent will not be assigned a name. This is intended specifically for AI.Agent.Nomenclater, which does the naming on behalf of Services.NamePool, which can't be used directly by Nomenclater because that would create a circular dependency.

tools_used(completion)

@spec tools_used(AI.Completion.t()) :: %{required(binary()) => non_neg_integer()}

Delegate to AI.Completion.tools_used/1 to extract the tools used from a completion.