State Machine Tool
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
The enum type defining the states
Tool definition (name, description, input schema)
The enum class for states
The starting state
Map of state to tools available in that state
LLM options for orchestration
Create prompt for the LLM, given context, input, and current state
Maximum iterations before stopping
Properties
Functions
Execute the tool with JSON input.
Begin configuring tools for a specific state.
Create a version of this tool that starts in the specified state. Useful when the starting state depends on runtime context.
Extension function to convert an Embabel Tool to a Spring AI ToolCallback.
Wrap this tool to conditionally await before execution.
Wrap this tool to always require confirmation before execution.
Create a new tool with a different description. Useful for providing context-specific descriptions while keeping the same functionality.
Extension function to wrap a Tool with event publication.
Add a tool available in all states.
Add tools available in all states.
Set the initial state.
Create a copy with different LLM options.
Create a copy with a different max iterations limit.
Create a copy with an additional parameter in the definition.
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.
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.
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.
Register a class with a predicate. Kotlin-friendly version using reified type parameter.
Register a domain class with a predicate to control when its @LlmTool methods are exposed.
Enable auto-discovery of chained tools from any returned artifact.
Create a copy with tools extracted from an object with @LlmTool methods. If the object has no @LlmTool methods, returns this unchanged.