cape.agent.tools.toolutils: Common functions for CAPE tool usage

This module provides a function wrap_cli() that places several preprocessing, protection, and postprocessing steps around a tool call eminated by an LLM.

This includes normalizing the arguments, e.g. replacing the string "true" with the Python value True, removing any keyword arguments set to None, capturing STDOUT, and more.

class cape.agent.tools.toolutils.Tee(*streams)

Class to simultaneously capture and display STDOUT

Call:
>>> tee = Tee(*streams)
Inputs:
streams: tuple[io.IOBase]

List of streams to capture/display

cape.agent.tools.toolutils.normalize_kwargs(kw: dict) → dict

Filter keyword args to tool, removing '' and None

Call:
>>> kw_tool = normalize_kwargs(kw)
Inputs:
kw: dict

Keyword args created by LLM tool caller

Outputs:
kw_tool: dict

Modified kw as such:

  • String conversion: "true" -> True

  • Remove keys set to None after normalization

cape.agent.tools.toolutils.normalize_tool_arg(v)

Normalize strings of special values to Pythonic values

E.g. "null" -> None

cape.agent.tools.toolutils.register_module_tools(params: dict)

Register TOOL_SCHEMAS and TOOLS to calling module

Call:
>>> register_module_tools(params)
Inputs:
params: dict

Dictionary of parameter definitiosn for use in this tool set

cape.agent.tools.toolutils.wrap_cli(func: Callable, *a, **kw) → dict

Run a CAPE command-line tool through cape.cfdx.cli

This performs several preprocessing, error handling, and postprocessing steps. The output of this function includes the results of the underlying function and appends the returncode. It also captures STDOUT and appends that to the result

Call:
>>> result = wrap_cli(func, *a, **kw)
Inputs:
func: callable

A function to be wrapped

a: tuple

Args passed into func after preprocessing

kw: dict

Keyword args passed into func after preprocessing

Outputs:
result: dict

Results of code; includes keys result, success, returncode, and stdout