Agent.Builder

class Agent.Builder


Builder for constructing Agent instances.

Summary

Public constructors

Public functions

Agent.Builder

Registers a specific AppFunction identifier (either a full ID such as "com.example.MyAppFunctionService#createNote" or a short method name such as "createNote") to be made available to the agent.

Agent.Builder

Registers a list of AppFunction identifiers (either full IDs such as "com.example.MyAppFunctionService#createNote" or short method names such as "createNote") to be made available to the agent.

Agent.Builder

Registers AppFunction identifiers (either full IDs such as "com.example.MyAppFunctionService#createNote" or short method names such as "createNote") to be made available to the agent.

Agent.Builder

Adds a single AppFunction tool (or a decorator wrapping one) to the agent.

Agent.Builder

Adds a single AppFunction tool (or a decorator wrapping one) to the agent.

Agent.Builder

Adds multiple AppFunction tools (or decorators wrapping them) to the agent.

Agent.Builder

Adds a collection of AppFunction tools (or decorators wrapping them) to the agent.

Agent

Builds the Agent instance.

Agent.Builder

Enables automatic discovery and execution of functions annotated with @androidx.appfunctions.AppFunction across the calling application.

Agent.Builder

Sets the system instructions for the agent.

Agent.Builder

Sets the maximum execution steps allowed per turn before forced termination.

Agent.Builder

Sets the name of the agent.

Public constructors

Builder

Builder()

Public functions

addAppFunctionId

@CanIgnoreReturnValue
fun addAppFunctionId(functionId: String): Agent.Builder

Registers a specific AppFunction identifier (either a full ID such as "com.example.MyAppFunctionService#createNote" or a short method name such as "createNote") to be made available to the agent.

Scoping to specific function IDs disables package-level metadata instructions injection.

Parameters
functionId: String

The AppFunction identifier or method name to enable.

Returns
Agent.Builder

This builder

addAppFunctionIds

@CanIgnoreReturnValue
fun addAppFunctionIds(functionIds: List<String>): Agent.Builder

Registers a list of AppFunction identifiers (either full IDs such as "com.example.MyAppFunctionService#createNote" or short method names such as "createNote") to be made available to the agent.

Parameters
functionIds: List<String>

The AppFunction identifiers or method names to enable.

Returns
Agent.Builder

This builder

addAppFunctionIds

@CanIgnoreReturnValue
fun addAppFunctionIds(vararg functionIds: String): Agent.Builder

Registers AppFunction identifiers (either full IDs such as "com.example.MyAppFunctionService#createNote" or short method names such as "createNote") to be made available to the agent.

Parameters
vararg functionIds: String

The AppFunction identifiers or method names to enable.

Returns
Agent.Builder

This builder

addTool

@CanIgnoreReturnValue
fun addTool(tool: AgentTool): Agent.Builder

Adds a single AppFunction tool (or a decorator wrapping one) to the agent.

Parameters
tool: AgentTool

Tool to add

Returns
Agent.Builder

This builder

Throws
IllegalArgumentException

if tool is not an AppFunction tool discovered via AppFunctionAgentTools.discover (or a decorator wrapping one).

addTools

@CanIgnoreReturnValue
fun addTools(tool: AgentTool): Agent.Builder

Adds a single AppFunction tool (or a decorator wrapping one) to the agent.

Parameters
tool: AgentTool

Tool to add

Returns
Agent.Builder

This builder

Throws
IllegalArgumentException

if tool is not an AppFunction tool discovered via AppFunctionAgentTools.discover (or a decorator wrapping one).

addTools

@CanIgnoreReturnValue
fun addTools(vararg tools: AgentTool): Agent.Builder

Adds multiple AppFunction tools (or decorators wrapping them) to the agent.

Parameters
vararg tools: AgentTool

Vararg tools to be made available to the agent

Returns
Agent.Builder

This builder

Throws
IllegalArgumentException

if any tool in tools is not an AppFunction tool discovered via AppFunctionAgentTools.discover (or a decorator wrapping one).

addTools

@CanIgnoreReturnValue
fun addTools(tools: List<AgentTool>): Agent.Builder

Adds a collection of AppFunction tools (or decorators wrapping them) to the agent.

Parameters
tools: List<AgentTool>

Tools to be made available to the agent

Returns
Agent.Builder

This builder

Throws
IllegalArgumentException

if any tool in tools is not an AppFunction tool discovered via AppFunctionAgentTools.discover (or a decorator wrapping one).

build

fun build(): Agent

Builds the Agent instance.

enableAppFunctions

@CanIgnoreReturnValue
fun enableAppFunctions(enabled: Boolean = true): Agent.Builder

Enables automatic discovery and execution of functions annotated with @androidx.appfunctions.AppFunction across the calling application.

When enabled, discovered functions are invoked automatically by AgentRunner during a turn whenever the model emits a matching tool call. If a function requires user confirmation (Human-in-the-Loop), runtime permissions, or custom validation before execution, use AppFunctionAgentTools.discover instead, wrap the sensitive AgentTool's AgentTool.execute method to prompt the user before delegating, and register the wrapped tools via addTools.

Parameters
enabled: Boolean = true

Whether to enable AppFunctions discovery and execution. Defaults to true.

Returns
Agent.Builder

This builder

setInstructions

@CanIgnoreReturnValue
fun setInstructions(instructions: String): Agent.Builder

Sets the system instructions for the agent. Defaults to an empty string.

Parameters
instructions: String

System prompt guiding agent behavior and personality

Returns
Agent.Builder

This builder

setMaxTurns

@CanIgnoreReturnValue
fun setMaxTurns(maxTurns: Int): Agent.Builder

Sets the maximum execution steps allowed per turn before forced termination. Defaults to 10.

Parameters
maxTurns: Int

Maximum turns allowed. Must be greater than 0

Returns
Agent.Builder

This builder

setName

@CanIgnoreReturnValue
fun setName(name: String): Agent.Builder

Sets the name of the agent. Defaults to "Assistant".

Parameters
name: String

Identifier name for the agent

Returns
Agent.Builder

This builder