Class StateMachineTool
-
- All Implemented Interfaces:
-
com.embabel.agent.api.tool.Tool,com.embabel.agent.api.tool.ToolInfo,com.embabel.agent.api.tool.agentic.AgenticTool,com.embabel.agent.api.tool.agentic.ToolChaining,com.embabel.agent.api.tool.agentic.state.StateMachineToolOperations
public final class StateMachineTool<S extends Enum<S>> implements AgenticTool<StateMachineTool<S>>, StateMachineToolOperations<S>
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.
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)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classStateMachineTool.Companion
-
Field Summary
Fields Modifier and Type Field Description private final Map<S, Integer>stateToolCountsprivate final IntegertotalStateToolsprivate final Tool.Definitiondefinitionprivate final Tool.Metadatametadataprivate final Class<S>stateTypeprivate final SinitialStateprivate final LlmOptionsllmprivate final Function3<ExecutingOperationContext, String, S, String>systemPromptCreatorprivate final IntegermaxIterationspublic final static StateMachineTool.CompanionCompanion
-
Constructor Summary
Constructors Constructor Description StateMachineTool(String name, String description, Class<S> stateType)Create a state machine tool with name, description and state enum type.
-
Method Summary
Modifier and Type Method Description final Map<S, Integer>getStateToolCounts()final IntegergetTotalStateTools()Tool.DefinitiongetDefinition()Tool.MetadatagetMetadata()Optional metadata final Class<S>getStateType()final SgetInitialState()LlmOptionsgetLlm()final Function3<ExecutingOperationContext, String, S, String>getSystemPromptCreator()IntegergetMaxIterations()Tool.Resultcall(String input)Execute the tool with JSON input. final StateMachineTool<S>withInitialState(S state)Set the initial state. ToolstartingIn(S state)Create a version of this tool that starts in the specified state. final StateBuilder<S>inState(S state)Begin configuring tools for a specific state. final StateMachineTool<S>withGlobalTool(Tool tool)Add a tool available in all states. final StateMachineTool<S>withGlobalTools(Tool tools)Add tools available in all states. StateMachineTool<S>withLlm(LlmOptions llm)Create a copy with different LLM options. StateMachineTool<S>withMaxIterations(Integer maxIterations)Create a copy with a different max iterations limit. StateMachineTool<S>withSystemPrompt(AgenticSystemPromptCreator creator)Create a copy with a dynamic system prompt creator. final StateMachineTool<S>withStateAwareSystemPrompt(Function3<ExecutingOperationContext, String, S, String> creator)Set a custom system prompt creator that also receives the current state. StateMachineTool<S>withParameter(Tool.Parameter parameter)Create a copy with an additional parameter in the definition. StateMachineTool<S>withToolObject(Object toolObject)Create a copy with tools extracted from an object with @LlmTool methods. <T extends Any> StateMachineTool<S>withToolChainingFrom(Class<T> type, DomainToolPredicate<T> predicate)Register a domain class with a predicate to control when its @LlmTool methods are exposed. final <T extends Any> StateMachineTool<S>withToolChainingFrom(Function2<T, AgentProcess, Boolean> predicate)Register a class with a predicate. final <T extends Any> StateMachineTool<S>withToolChainingFrom()Register a class that can contribute @LlmTool methods when a single instance is retrieved. StateMachineTool<S>withToolChainingFromAny()Enable auto-discovery of chained tools from any returned artifact. -
Methods inherited from class com.embabel.agent.api.tool.agentic.AgenticTool
withSystemPrompt -
Methods inherited from class com.embabel.agent.api.tool.agentic.ToolChaining
withToolChainingFrom -
Methods inherited from class com.embabel.agent.api.tool.Tool
withDescription, withName, withNote -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
getStateToolCounts
final Map<S, Integer> getStateToolCounts()
-
getTotalStateTools
final Integer getTotalStateTools()
-
getDefinition
Tool.Definition getDefinition()
-
getMetadata
Tool.Metadata getMetadata()
Optional metadata
-
getStateType
final Class<S> getStateType()
-
getInitialState
final S getInitialState()
-
getLlm
LlmOptions getLlm()
-
getSystemPromptCreator
final Function3<ExecutingOperationContext, String, S, String> getSystemPromptCreator()
-
getMaxIterations
Integer getMaxIterations()
-
call
Tool.Result call(String input)
Execute the tool with JSON input.
- Parameters:
input- JSON string matching inputSchema- Returns:
Result to send back to LLM
-
withInitialState
final StateMachineTool<S> withInitialState(S state)
Set the initial state.
-
startingIn
Tool startingIn(S state)
Create a version of this tool that starts in the specified state. Useful when the starting state depends on runtime context.
-
inState
final StateBuilder<S> inState(S state)
Begin configuring tools for a specific state.
-
withGlobalTool
final StateMachineTool<S> withGlobalTool(Tool tool)
Add a tool available in all states.
-
withGlobalTools
final StateMachineTool<S> withGlobalTools(Tool tools)
Add tools available in all states.
-
withLlm
StateMachineTool<S> withLlm(LlmOptions llm)
Create a copy with different LLM options.
-
withMaxIterations
StateMachineTool<S> withMaxIterations(Integer maxIterations)
Create a copy with a different max iterations limit.
-
withSystemPrompt
StateMachineTool<S> withSystemPrompt(AgenticSystemPromptCreator creator)
Create a copy with a dynamic system prompt creator. The creator receives the execution context and input string.
-
withStateAwareSystemPrompt
final StateMachineTool<S> withStateAwareSystemPrompt(Function3<ExecutingOperationContext, String, S, String> creator)
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.
-
withParameter
StateMachineTool<S> withParameter(Tool.Parameter parameter)
Create a copy with an additional parameter in the definition.
-
withToolObject
StateMachineTool<S> withToolObject(Object toolObject)
Create a copy with tools extracted from an object with @LlmTool methods. If the object has no @LlmTool methods, returns this unchanged.
-
withToolChainingFrom
<T extends Any> StateMachineTool<S> withToolChainingFrom(Class<T> type, DomainToolPredicate<T> predicate)
Register a domain class with a predicate to control when its @LlmTool methods are exposed.
When a single artifact of the specified type is returned by any tool and passes the predicate, any @LlmTool annotated methods on that instance become available as tools (globally, not state-bound).
- Parameters:
type- The domain class that may contribute toolspredicate- Predicate to filter which instances contribute tools
-
withToolChainingFrom
final <T extends Any> StateMachineTool<S> withToolChainingFrom(Function2<T, AgentProcess, Boolean> predicate)
Register a class with a predicate. Kotlin-friendly version using reified type parameter.
-
withToolChainingFrom
final <T extends Any> StateMachineTool<S> withToolChainingFrom()
Register a class that can contribute @LlmTool methods when a single instance is retrieved. Kotlin-friendly version using reified type parameter.
Example:
StateMachineTool("orderProcessor", "Process orders", OrderState::class.java) .withToolChainingFrom<Order>() // Order methods become available when a single Order is retrieved .withInitialState(OrderState.DRAFT) ...
-
withToolChainingFromAny
StateMachineTool<S> withToolChainingFromAny()
Enable auto-discovery of chained tools from any returned artifact.
When enabled, any artifact with @LlmTool methods will automatically have its tools exposed, replacing ALL previous bindings.
-
-
-
-