Class ToolsProvider
- All Implemented Interfaces:
ProcessProvider
The ToolsProvider class extends AbstractAIProvider and
provides infrastructure for registering tool functions, handling prompts, and
executing tool invocations based on structured YAML input. Tools are
registered in a map and can be invoked dynamically.
- Prompts are collected and stored for later processing.
- Tool functions are registered by name and can be invoked with parameters parsed from YAML.
- Supports execution mode where the last prompt is interpreted as a YAML tool call description.
Usage Example
ToolsProvider provider = new ToolsProvider();
provider.addTool("myTool", "Description", myToolFunction, ...);
provider.prompt("tool: myTool\nparams:\n key: value");
String result = provider.perform();
- Since:
- 1.2.0
- Author:
- Viktor Tovstyi
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static org.slf4j.LoggerLogger instance for this provider.List of collected prompt texts.private Map<String, ToolFunction> Map of registered tool functions by tool name.Fields inherited from class org.machanism.machai.process.provider.AbstractAIProvider
DEFAULT_WEBSEARCH_TYPE_NAME, ERROR_TOOL_RESULT_PREFIX, LINE_SEPARATOR, LOG_LINE_LENG, MAX_OUTPUT_TOKENS, MCP_PROP_NAME_PREFIX, PARAGRAPH_SEPARATOR, PASSWORD_PROP_NAME, PROJECT_DIR_PARAM_NAME, USERNAME_PROP_NAME -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newToolsProviderwith a default configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddTool(String name, String description, ToolFunction function, ParamDescriptor... paramsDesc) Registers a tool function with the provider.Returns the names of tools currently registered with this provider.perform()Executes the provider's perform logic.voidAdds a prompt text to the provider's prompt list.Methods inherited from class org.machanism.machai.process.provider.AbstractAIProvider
addMcpServer, addMcpServers, addPrompt, addPrompts, addResource, addResources, addTools, addWebSearch, addWebSearch, clear, getConfigurator, getInstructions, getMaxOutputTokens, getMaxToolCalls, getModel, getParamValue, getProjectDir, getTimeout, init, instructions, isErrorHandling, normalize, safelyInvokeTool, setErrorHandling, setInstructions, setMaxOutputTokens, setMaxToolCalls, setModel, setProjectDir, setTimeout, toSnakeCase
-
Field Details
-
logger
static org.slf4j.Logger loggerLogger instance for this provider. -
prompts
List of collected prompt texts. -
toolMap
Map of registered tool functions by tool name.
-
-
Constructor Details
-
ToolsProvider
public ToolsProvider()Constructs a newToolsProviderwith a default configuration.This constructor initializes the superclass and explicitly disables conversational error handling by calling
setErrorHandling(false).By disabling conversational error interception, this provider operates under a fail-fast model: any exception thrown during a registered tool's execution is not converted into a text message for the model, but is instead immediately propagated up to the calling application as a
SpecialException.
-
-
Method Details
-
prompt
Adds a prompt text to the provider's prompt list.- Specified by:
promptin interfaceProcessProvider- Overrides:
promptin classAbstractAIProvider- Parameters:
text- the prompt text to add
-
perform
Executes the provider's perform logic.If the
chatModelis set to "yaml", the last prompt is parsed as a YAML tool call description. The tool name and parameters are extracted, and the corresponding tool function is invoked.- Returns:
- the result of the tool invocation, or
nullif not applicable
-
addTool
protected void addTool(String name, String description, ToolFunction function, ParamDescriptor... paramsDesc) Registers a tool function with the provider.The tool is added to the internal map by name. Parameter descriptors are accepted but not used in this implementation.
- Specified by:
addToolin classAbstractAIProvider- Parameters:
name- the tool namedescription- the tool descriptionfunction- the tool function implementationparamsDesc- parameter descriptors for the tool (optional)
-
getToolNames
Returns the names of tools currently registered with this provider.- Returns:
- a new list containing registered tool names in registration order
-