Class ToolsProvider
- All Implemented Interfaces:
Genai
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
Fields inherited from class org.machanism.machai.ai.provider.AbstractAIProvider
chatModel, DEFAULT_WEBSEARCH_TYPE_NAME, instructions, LINE_SEPARATOR, LOG_INPUTS_PROP_NAME, LOG_LINE_LENG, LOG_SECTION_SEPARATOR, MAX_OUTPUT_TOKENS, maxOutputTokens, maxToolCalls, MCP_PROP_NAME_PREFIX, PARAGRAPH_SEPARATOR, PASSWORD_PROP_NAME, PROJECT_DIR_PARAM_NAME, projectDir, SERVERID_PROP_NAME, timeoutSec, 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.perform()Executes the provider's perform logic.voidAdds a prompt text to the provider's prompt list.Methods inherited from class org.machanism.machai.ai.provider.AbstractAIProvider
addMcpServer, addMcpServers, addPrompt, addPrompts, addTools, addWebSearch, addWebSearch, clear, getConfigurator, getParamValue, getProjectDir, getTimeout, init, inputsLog, instructions, isErrorHandling, logInputs, logInputsSpec, normalize, safelyInvokeTool, setErrorHandling, setProjectDir, setTimeout
-
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 interfaceGenai- 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)
-