Class CommandFunctionTools
- All Implemented Interfaces:
FunctionTools
Genai.
The installed tools provide a controlled wrapper around ProcessBuilder to execute a host command line
within a validated working directory and capture both stdout and stderr into a bounded buffer (see
LimitedStringBuilder).
Security model
Command execution is intended to be restricted by the host application. This implementation supports:
- A deny-list heuristic check via
CommandSecurityChecker - Project-root confinement for the working directory (see
resolveWorkingDir(File, String))
Installed tools
run_command_line_tool– executes a command line and returns outputterminate_process– aborts execution by throwing aCommandFunctionTools.ProcessTerminationException
- Author:
- Viktor Tovstyi
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRuntime exception used byterminate_processto signal early termination to the host. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyTools(Genai provider) Registers system command and process control tools with the providedGenai.executeCommand(Object[] params) Implements therun_command_line_tooltool.Parses theenvparameter string into a map of environment variables.resolveWorkingDir(File projectDir, String dir) Resolves a working directory relative to a canonical project directory.voidsetConfigurator(Configurator configurator) Provides a configurator instance to the tool set.terminateProcess(Object[] params) Implements theterminate_processtool.(package private) StringwaitAndCollect(Process process, Future<?> stdoutFuture, Future<?> stderrFuture, LimitedStringBuilder output, String commandId) Waits for the process to finish (up toprocessTimeoutSeconds) and then returns collected output.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.machanism.machai.ai.tools.FunctionTools
replace
-
Constructor Details
-
CommandFunctionTools
public CommandFunctionTools()
-
-
Method Details
-
applyTools
Registers system command and process control tools with the providedGenai.The following tools are installed:
- run_command_line_tool – Executes a system command using Java's
ProcessBuilder.
Parameters:- command (string, required): The command to execute.
- env (string, optional): Environment variables as
NAME=VALUEpairs separated by newline characters (\n). - dir (string, optional): Working directory relative to the project directory; defaults to
.. - tailResultSize (integer, optional): Maximum number of characters to return from captured output;
defaults to
DEFAULT_RESULT_TAIL_SIZE. - charsetName (string, optional): Character set used to decode process output; defaults to
DEFAULT_CHARSET.
- terminate_process – Throws an exception to immediately terminate execution.
Parameters:- message (string, optional): Exception message; defaults to "Process terminated by function tool."
- cause (string, optional): Optional cause message; wrapped in an
Exception. - exitCode (integer, optional): Exit code; defaults to 1.
- Specified by:
applyToolsin interfaceFunctionTools- Parameters:
provider- the provider instance to which tools will be registered
- run_command_line_tool – Executes a system command using Java's
-
terminateProcess
Implements theterminate_processtool.Reads
message,cause, andexitCodefrom the suppliedJsonNodeand throws aCommandFunctionTools.ProcessTerminationException. This mechanism allows a tool invocation to abort the overall workflow with an explicit exit code.- Parameters:
params- tool invocation parameters (expects a singleJsonNodeargument)- Returns:
- never returns; always throws
- Throws:
CommandFunctionTools.ProcessTerminationException- always thrown to terminate execution
-
executeCommand
Implements therun_command_line_tooltool.Expected parameters:
JsonNodecontainingcommandand optional settingsFileproject working directory supplied by the provider runtime
- Parameters:
params- tool arguments- Returns:
- command output bounded to the configured tail size
- Throws:
IOException- if the process cannot be started or I/O occurs while resolving paths
-
waitAndCollect
String waitAndCollect(Process process, Future<?> stdoutFuture, Future<?> stderrFuture, LimitedStringBuilder output, String commandId) throws InterruptedException, TimeoutException, ExecutionException Waits for the process to finish (up toprocessTimeoutSeconds) and then returns collected output.This method also waits briefly for stdout/stderr reader tasks to complete so that output is not lost.
- Parameters:
process- process being observedstdoutFuture- future representing the stdout reader taskstderrFuture- future representing the stderr reader taskoutput- bounded output buffercommandId- id used for log correlation- Returns:
- collected output, followed by an exit-code line
- Throws:
InterruptedException- if the current thread is interrupted while waitingTimeoutException- if collecting output times outExecutionException- if a reader task fails
-
resolveWorkingDir
Resolves a working directory relative to a canonical project directory.Absolute paths are rejected and attempts to traverse outside the project directory are blocked.
- Parameters:
projectDir- canonical project directorydir- requested relative directory (or.)- Returns:
- resolved directory, or
nullif invalid
-
parseEnv
Parses theenvparameter string into a map of environment variables.Lines are separated by
\n(or\n); empty lines and lines starting with#are ignored.- Parameters:
envString- environment string- Returns:
- parsed environment variables
-
setConfigurator
Description copied from interface:FunctionToolsProvides a configurator instance to the tool set.The default implementation does nothing.
- Specified by:
setConfiguratorin interfaceFunctionTools- Parameters:
configurator- configurator to use for runtime value resolution
-