StateMachineTool

A tool that manages state transitions, with tools available based on the current state.

Unlike com.embabel.agent.api.tool.playbook.PlaybookTool which uses unlock conditions, StateMachineTool uses explicit states defined by an enum. Tools are registered with specific states where they're available, and can trigger transitions to other states.

Usage

enum class OrderState { DRAFT, CONFIRMED, SHIPPED, DELIVERED }

StateMachineTool("orderProcessor", "Process orders", OrderState::class.java)
.withInitialState(OrderState.DRAFT)
.inState(OrderState.DRAFT)
.withTool(addItemTool)
.withTool(confirmTool).transitionsTo(OrderState.CONFIRMED)
.inState(OrderState.CONFIRMED)
.withTool(shipTool).transitionsTo(OrderState.SHIPPED)
.inState(OrderState.SHIPPED)
.withTool(deliverTool).transitionsTo(OrderState.DELIVERED)
.inState(OrderState.DELIVERED)
.withTool(reviewTool)

Parameters

S

The enum type defining the states

definition

Tool definition (name, description, input schema)

stateType

The enum class for states

initialState

The starting state

stateTools

Map of state to tools available in that state

llm

LLM options for orchestration

systemPromptCreator

Create prompt for the LLM, given context, input, and current state

maxIterations

Maximum iterations before stopping

Constructors

Link copied to clipboard
constructor(name: String, description: String, stateType: Class<S>)

Create a state machine tool with name, description and state enum type.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val definition: Tool.Definition
Link copied to clipboard
Link copied to clipboard
open override val llm: <Error class: unknown class>
Link copied to clipboard
open override val maxIterations: Int
Link copied to clipboard
open override val metadata: Tool.Metadata

Optional metadata

Link copied to clipboard

Count of tools registered for each state.

Link copied to clipboard
Link copied to clipboard

Total count of state-specific tools.

Functions

Link copied to clipboard
open override fun call(input: String): Tool.Result

Execute the tool with JSON input.

Link copied to clipboard
fun inState(state: S): StateBuilder<S>

Begin configuring tools for a specific state.

Link copied to clipboard
inline fun <T : Any> Tool.requireType(message: String? = null): Tool

Wrap this tool to require a value of type T before execution (reified).

fun <T : Any> Tool.requireType(type: Class<T>, messageProvider: (String) -> String? = { null }): Tool

Wrap this tool to require a value of type T before execution.

Link copied to clipboard
open override fun startingIn(state: S): Tool

Create a version of this tool that starts in the specified state. Useful when the starting state depends on runtime context.

Link copied to clipboard
fun Tool.toSpringToolCallback(): <Error class: unknown class>

Extension function to convert an Embabel Tool to a Spring AI ToolCallback.

Link copied to clipboard
inline fun <T : Tool> Tool.unwrapAs(): T?

Unwrap a tool to find a specific type, or return null if not found.

Link copied to clipboard

Wrap this tool to conditionally await before execution.

Link copied to clipboard
fun Tool.withConfirmation(messageProvider: (String) -> String): Tool

Wrap this tool to always require confirmation before execution.

Link copied to clipboard
open fun withDescription(newDescription: String): Tool

Create a new tool with a different description. Useful for providing context-specific descriptions while keeping the same functionality.

Link copied to clipboard
fun Tool.withEventPublication(agentProcess: AgentProcess, action: Action?, llmOptions: <Error class: unknown class>): Tool

Extension function to wrap a Tool with event publication.

Link copied to clipboard

Add a tool available in all states.

Link copied to clipboard
fun withGlobalTools(vararg tools: Tool): StateMachineTool<S>

Add tools available in all states.

Link copied to clipboard

Set the initial state.

Link copied to clipboard
open override fun withLlm(llm: <Error class: unknown class>): StateMachineTool<S>

Create a copy with different LLM options.

Link copied to clipboard
open override fun withMaxIterations(maxIterations: Int): StateMachineTool<S>

Create a copy with a different max iterations limit.

Link copied to clipboard
open fun withName(newName: String): Tool

Create a new tool with a different name. Useful for namespacing tools when combining multiple tool sources.

Link copied to clipboard
open fun withNote(note: String): Tool

Create a new tool with an additional note appended to the description. Useful for adding context-specific hints to an existing tool.

Link copied to clipboard
open override fun withParameter(parameter: Tool.Parameter): StateMachineTool<S>

Create a copy with an additional parameter in the definition.

Link copied to clipboard

Set a custom system prompt creator that also receives the current state. This is useful for state machines where the prompt needs to reflect the current state.

Link copied to clipboard

Create a copy with a fixed system prompt. This is a convenience method that delegates to withSystemPrompt with a creator.

Create a copy with a dynamic system prompt creator. The creator receives the execution context and input string.

Link copied to clipboard

Register a class whose @LlmTool methods become available as tools when a single instance of that type is returned as an artifact.

Register a class that can contribute @LlmTool methods when a single instance is retrieved. Kotlin-friendly version using reified type parameter.

inline fun <T : Any> withToolChainingFrom(noinline predicate: (T, AgentProcess?) -> Boolean): StateMachineTool<S>

Register a class with a predicate. Kotlin-friendly version using reified type parameter.

open override fun <T : Any> withToolChainingFrom(type: Class<T>, predicate: DomainToolPredicate<T>): StateMachineTool<S>

Register a domain class with a predicate to control when its @LlmTool methods are exposed.

Link copied to clipboard

Enable auto-discovery of chained tools from any returned artifact.

Link copied to clipboard
open override fun withToolObject(toolObject: Any): StateMachineTool<S>

Create a copy with tools extracted from an object with @LlmTool methods. If the object has no @LlmTool methods, returns this unchanged.