fabricatio_tool.models.executor

Module containing the ToolExecutor class for managing and executing a sequence of tools.

Classes

ToolExecutor

A class representing a tool executor with a sequence of tools to execute.

Module Contents

class fabricatio_tool.models.executor.ToolExecutor

A class representing a tool executor with a sequence of tools to execute.

This class manages a sequence of tools and provides methods to inject tools and data into a module, execute the tools, and retrieve specific outputs.

collector: fabricatio_tool.models.collector.ResultCollector
collector_varname: ClassVar[str] = 'collector'
fn_name: ClassVar[str] = 'execute'

The name of the function to execute.

candidates: List[fabricatio_tool.models.tool.Tool] = []

The sequence of tools to execute.

data: Dict[str, Any]

The data that could be used when invoking the tools.

inject_tools[C: Dict[str, Any]](cxt: C | None = None) C

Inject the tools into the provided module or default.

This method injects the tools into the provided module or creates a new module if none is provided. It checks for potential collisions before injecting to avoid overwriting existing keys and raises KeyError.

Parameters:

cxt (Optional[M]) – The module to inject tools into. If None, a new module is created.

Returns:

The module with injected tools.

Return type:

M

Raises:

KeyError – If a tool name already exists in the context.

inject_data[C: Dict[str, Any]](cxt: C | None = None) C

Inject the data into the provided module or default.

This method injects the data into the provided module or creates a new module if none is provided. It checks for potential collisions before injecting to avoid overwriting existing keys and raises KeyError.

Parameters:

cxt (Optional[M]) – The module to inject data into. If None, a new module is created.

Returns:

The module with injected data.

Return type:

M

Raises:

KeyError – If a data key already exists in the context.

inject_collector[C: Dict[str, Any]](cxt: C | None = None) C

Inject the collector into the provided module or default.

This method injects the collector into the provided module or creates a new module if none is provided. It checks for potential collisions before injecting to avoid overwriting existing keys and raises KeyError.

Parameters:

cxt (Optional[M]) – The module to inject the collector into. If None, a new module is created.

Returns:

The module with injected collector.

Return type:

M

Raises:

KeyError – If the collector name already exists in the context.

async execute[C: Dict[str, Any]](body: str, cxt: C | None = None, check_modules: fabricatio_tool.config.CheckConfigModel | None = None, check_imports: fabricatio_tool.config.CheckConfigModel | None = None, check_calls: fabricatio_tool.config.CheckConfigModel | None = None, err_key: str = tool_config.error_key) fabricatio_tool.models.collector.ResultCollector

Execute the sequence of tools with the provided context.

This method orchestrates the execution process by injecting the collector, tools, and data into the context, assembling the source code, checking for violations, and finally executing the compiled function.

Parameters:
  • body (str) – The source code to execute.

  • cxt (Optional[C]) – The context to execute the tools with. If None, an empty dictionary is used.

  • check_modules (Optional[CheckConfigModel]) – Configuration for module-related checks.

  • check_imports (Optional[CheckConfigModel]) – Configuration for import-related checks.

  • check_calls (Optional[CheckConfigModel]) – Configuration for call-related checks.

  • err_key (str) – The key to use for error handling.

Returns:

The collector containing results from the executed tools.

Return type:

ResultCollector

validate_callcheck_config(check_calls: fabricatio_tool.config.CheckConfigModel) fabricatio_tool.config.CheckConfigModel

Validate the call check configuration.

This method ensures that the tools defined in the executor are properly accounted for in the call check configuration. If the configuration is in blacklist mode and any tool names appear in the targets, a ValueError is raised. Otherwise, all tool names are added to the targets in whitelist mode.

Parameters:

check_calls (CheckConfigModel) – The call check configuration to validate and update.

Returns:

The validated and potentially updated call check configuration.

Return type:

CheckConfigModel

Raises:

ValueError – If blacklist mode is used and any tool names are found in the targets.

signature() str

Generate the header for the source code.

assemble(body: str) str

Assemble the source code with the provided context.

This method assembles the source code by injecting the tools and data into the context.

Parameters:

body (str) – The source code to assemble.

Returns:

The assembled source code.

Return type:

str

classmethod from_recipe(recipe: List[str], toolboxes: List[fabricatio_tool.models.tool.ToolBox]) Self

Create a tool executor from a recipe and a list of toolboxes.

This method creates a tool executor by retrieving tools from the provided toolboxes based on the recipe.

Parameters:
  • recipe (List[str]) – The recipe specifying the names of the tools to be added.

  • toolboxes (List[ToolBox]) – The list of toolboxes to retrieve tools from.

Returns:

A new instance of the tool executor with the specified tools.

Return type:

Self