Skip to content

Tools

Export tool.

ToolsExporter

ToolsExporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
    **kwargs: Any
)

Bases: Exporter[ToolExtras]

Rools exporter with structured extras.

Parameters:

NameTypeDescriptionDefault
flow_namestr

The name of the flow.

required
agentslist[WaldiezAgent]

The agents that use tools.

required
agent_namesdict[str, str]

Mapping of agent IDs to names.

required
toolslist[WaldiezTool]

The tools to export.

required
tool_namesdict[str, str]

Mapping of tool IDs to names.

required
output_dirOptional[Union[str, Path]]

Output directory for generated files, by default None

None
contextOptional[ExporterContext]

Exporter context with dependencies, by default None

None
**kwargsAny

Additional keyword arguments.

{}
Source code in waldiez/exporting/tools/exporter.py
def __init__(
    self,
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
    **kwargs: Any,
):
    """Initialize the tools exporter.

    Parameters
    ----------
    flow_name : str
        The name of the flow.
    agents : list[WaldiezAgent]
        The agents that use tools.
    agent_names : dict[str, str]
        Mapping of agent IDs to names.
    tools : list[WaldiezTool]
        The tools to export.
    tool_names : dict[str, str]
        Mapping of tool IDs to names.
    output_dir : Optional[Union[str, Path]], optional
        Output directory for generated files, by default None
    context : Optional[ExporterContext], optional
        Exporter context with dependencies, by default None
    **kwargs
        Additional keyword arguments.
    """
    super().__init__(context, **kwargs)

    self.flow_name = flow_name
    self.agents = agents
    self.agent_names = agent_names
    self.tools = tools
    self.tool_names = tool_names
    self.output_dir = Path(output_dir) if output_dir else None

    # Initialize extras with processed tool content
    self._extras = self._create_tool_extras()

extras property

extras: ToolExtras

Get the tool extras.

generate_main_content

generate_main_content() -> Optional[str]

Generate the main tools content.

Source code in waldiez/exporting/tools/exporter.py
def generate_main_content(self) -> Optional[str]:
    """Generate the main tools content."""
    # handled as positioned content
    return None

get_tool_secrets_loader_script

get_tool_secrets_loader_script(tool_name: str) -> str

Get the tool secrets loader script.

Parameters:

NameTypeDescriptionDefault
tool_namestr

The name of the tool for which to generate the loader script.

required

Returns:

TypeDescription
str

The tool secrets loader script.

Source code in waldiez/exporting/tools/exporter.py
    def get_tool_secrets_loader_script(self, tool_name: str) -> str:
        """Get the tool secrets loader script.

        Parameters
        ----------
        tool_name : str
            The name of the tool for which to generate the loader script.

        Returns
        -------
        str
            The tool secrets loader script.
        """
        comment = get_comment(
            "Load tool secrets module if needed",
            for_notebook=self.config.for_notebook,
        )
        loader_script = f'''{comment}# NOTE:
# This section assumes that a file named "{self.flow_name}_{tool_name}_secrets"
# exists in the same directory as this file.
# This file contains the secrets for the tool used in this flow.
# It should be .gitignored and not shared publicly.
# If this file is not present, you can either create it manually
# or change the way secrets are loaded in the flow.


def load_tool_secrets_module(flow_name: str, tool_name: str) -> ModuleType:
    """Load the tool secrets module for the given flow name and tool name.

    Parameters
    ----------
    flow_name : str
        The flow name.

    Returns
    -------
    ModuleType
        The loaded module.
    """
    module_name = f"{{flow_name}}_{{tool_name}}_secrets"
    if module_name in sys.modules:
        return importlib.reload(sys.modules[module_name])
    return importlib.import_module(module_name)

load_tool_secrets_module("{self.flow_name}", "{tool_name}")
'''
        return loader_script

create_tools_exporter

create_tools_exporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
) -> ToolsExporter

Create a tools exporter.

Parameters:

NameTypeDescriptionDefault
flow_namestr

The name of the flow.

required
agentslist[WaldiezAgent]

The agents that use tools.

required
agent_namesdict[str, str]

Mapping of agent IDs to names.

required
toolslist[WaldiezTool]

The tools to export.

required
tool_namesdict[str, str]

Mapping of tool IDs to names.

required
output_dirOptional[Union[str, Path]]

Output directory for generated files, by default None

None
contextOptional[ExporterContext]

Exporter context with dependencies, by default None

None

Returns:

TypeDescription
ToolsExporter

The created tools exporter.

Source code in waldiez/exporting/tools/factory.py
def create_tools_exporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
) -> ToolsExporter:
    """Create a tools exporter.

    Parameters
    ----------
    flow_name : str
        The name of the flow.
    agents : list[WaldiezAgent]
        The agents that use tools.
    agent_names : dict[str, str]
        Mapping of agent IDs to names.
    tools : list[WaldiezTool]
        The tools to export.
    tool_names : dict[str, str]
        Mapping of tool IDs to names.
    output_dir : Optional[Union[str, Path]], optional
        Output directory for generated files, by default None
    context : Optional[ExporterContext], optional
        Exporter context with dependencies, by default None

    Returns
    -------
    ToolsExporter
        The created tools exporter.
    """
    if context is None:
        context = get_default_exporter_context()
    return ToolsExporter(
        flow_name=flow_name,
        agents=agents,
        agent_names=agent_names,
        tools=tools,
        tool_names=tool_names,
        output_dir=output_dir,
        context=context,
    )

exporter

Tools exporter.

ToolsExporter

ToolsExporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
    **kwargs: Any
)

Bases: Exporter[ToolExtras]

Rools exporter with structured extras.

Parameters:

NameTypeDescriptionDefault
flow_namestr

The name of the flow.

required
agentslist[WaldiezAgent]

The agents that use tools.

required
agent_namesdict[str, str]

Mapping of agent IDs to names.

required
toolslist[WaldiezTool]

The tools to export.

required
tool_namesdict[str, str]

Mapping of tool IDs to names.

required
output_dirOptional[Union[str, Path]]

Output directory for generated files, by default None

None
contextOptional[ExporterContext]

Exporter context with dependencies, by default None

None
**kwargsAny

Additional keyword arguments.

{}
Source code in waldiez/exporting/tools/exporter.py
def __init__(
    self,
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
    **kwargs: Any,
):
    """Initialize the tools exporter.

    Parameters
    ----------
    flow_name : str
        The name of the flow.
    agents : list[WaldiezAgent]
        The agents that use tools.
    agent_names : dict[str, str]
        Mapping of agent IDs to names.
    tools : list[WaldiezTool]
        The tools to export.
    tool_names : dict[str, str]
        Mapping of tool IDs to names.
    output_dir : Optional[Union[str, Path]], optional
        Output directory for generated files, by default None
    context : Optional[ExporterContext], optional
        Exporter context with dependencies, by default None
    **kwargs
        Additional keyword arguments.
    """
    super().__init__(context, **kwargs)

    self.flow_name = flow_name
    self.agents = agents
    self.agent_names = agent_names
    self.tools = tools
    self.tool_names = tool_names
    self.output_dir = Path(output_dir) if output_dir else None

    # Initialize extras with processed tool content
    self._extras = self._create_tool_extras()

extras property

extras: ToolExtras

Get the tool extras.

generate_main_content

generate_main_content() -> Optional[str]

Generate the main tools content.

Source code in waldiez/exporting/tools/exporter.py
def generate_main_content(self) -> Optional[str]:
    """Generate the main tools content."""
    # handled as positioned content
    return None

get_tool_secrets_loader_script

get_tool_secrets_loader_script(tool_name: str) -> str

Get the tool secrets loader script.

Parameters:

NameTypeDescriptionDefault
tool_namestr

The name of the tool for which to generate the loader script.

required

Returns:

TypeDescription
str

The tool secrets loader script.

Source code in waldiez/exporting/tools/exporter.py
    def get_tool_secrets_loader_script(self, tool_name: str) -> str:
        """Get the tool secrets loader script.

        Parameters
        ----------
        tool_name : str
            The name of the tool for which to generate the loader script.

        Returns
        -------
        str
            The tool secrets loader script.
        """
        comment = get_comment(
            "Load tool secrets module if needed",
            for_notebook=self.config.for_notebook,
        )
        loader_script = f'''{comment}# NOTE:
# This section assumes that a file named "{self.flow_name}_{tool_name}_secrets"
# exists in the same directory as this file.
# This file contains the secrets for the tool used in this flow.
# It should be .gitignored and not shared publicly.
# If this file is not present, you can either create it manually
# or change the way secrets are loaded in the flow.


def load_tool_secrets_module(flow_name: str, tool_name: str) -> ModuleType:
    """Load the tool secrets module for the given flow name and tool name.

    Parameters
    ----------
    flow_name : str
        The flow name.

    Returns
    -------
    ModuleType
        The loaded module.
    """
    module_name = f"{{flow_name}}_{{tool_name}}_secrets"
    if module_name in sys.modules:
        return importlib.reload(sys.modules[module_name])
    return importlib.import_module(module_name)

load_tool_secrets_module("{self.flow_name}", "{tool_name}")
'''
        return loader_script

factory

Factory function for creating a ToolsExporter instance.

create_tools_exporter

create_tools_exporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
) -> ToolsExporter

Create a tools exporter.

Parameters:

NameTypeDescriptionDefault
flow_namestr

The name of the flow.

required
agentslist[WaldiezAgent]

The agents that use tools.

required
agent_namesdict[str, str]

Mapping of agent IDs to names.

required
toolslist[WaldiezTool]

The tools to export.

required
tool_namesdict[str, str]

Mapping of tool IDs to names.

required
output_dirOptional[Union[str, Path]]

Output directory for generated files, by default None

None
contextOptional[ExporterContext]

Exporter context with dependencies, by default None

None

Returns:

TypeDescription
ToolsExporter

The created tools exporter.

Source code in waldiez/exporting/tools/factory.py
def create_tools_exporter(
    flow_name: str,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Union[str, Path]] = None,
    context: Optional[ExporterContext] = None,
) -> ToolsExporter:
    """Create a tools exporter.

    Parameters
    ----------
    flow_name : str
        The name of the flow.
    agents : list[WaldiezAgent]
        The agents that use tools.
    agent_names : dict[str, str]
        Mapping of agent IDs to names.
    tools : list[WaldiezTool]
        The tools to export.
    tool_names : dict[str, str]
        Mapping of tool IDs to names.
    output_dir : Optional[Union[str, Path]], optional
        Output directory for generated files, by default None
    context : Optional[ExporterContext], optional
        Exporter context with dependencies, by default None

    Returns
    -------
    ToolsExporter
        The created tools exporter.
    """
    if context is None:
        context = get_default_exporter_context()
    return ToolsExporter(
        flow_name=flow_name,
        agents=agents,
        agent_names=agent_names,
        tools=tools,
        tool_names=tool_names,
        output_dir=output_dir,
        context=context,
    )

processor

Tool related utilities and processors.

ToolProcessingResult dataclass

ToolProcessingResult(
    content: str = "",
    builtin_imports: list[str] = list[str](),
    third_party_imports: list[str] = list[str](),
    environment_variables: list[EnvironmentVariable] = list[
        EnvironmentVariable
    ](),
)

Result from processing tools.

ToolProcessor

ToolProcessor(
    flow_name: str,
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Path] = None,
)

Processor for tool content generation.

Parameters:

NameTypeDescriptionDefault
flow_namestr

The name of the flow.

required
toolslist[WaldiezTool]

The tools to process.

required
tool_namesdict[str, str]

Mapping of tool IDs to names.

required
output_dirOptional[Path]

Output directory for generated files, by default None

None
Source code in waldiez/exporting/tools/processor.py
def __init__(
    self,
    flow_name: str,
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
    output_dir: Optional[Path] = None,
):
    """Initialize the tool processor.

    Parameters
    ----------
    flow_name : str
        The name of the flow.
    tools : list[WaldiezTool]
        The tools to process.
    tool_names : dict[str, str]
        Mapping of tool IDs to names.
    output_dir : Optional[Path], optional
        Output directory for generated files, by default None
    """
    self.flow_name = flow_name
    self.tools = tools
    self.tool_names = tool_names
    self.output_dir = output_dir

process

process() -> ToolProcessingResult

Process all tools and return consolidated result.

Returns:

TypeDescription
ToolProcessingResult

The processed result containing tool content, imports, and environment variables.

Source code in waldiez/exporting/tools/processor.py
def process(self) -> ToolProcessingResult:
    """Process all tools and return consolidated result.

    Returns
    -------
    ToolProcessingResult
        The processed result containing tool content,
        imports, and environment variables.
    """
    result = ToolProcessingResult()

    # Separate shared and regular tools
    shared_tools = [tool for tool in self.tools if tool.is_shared]
    regular_tools = [tool for tool in self.tools if not tool.is_shared]

    # Process shared tools first (they need to be available to other tools)
    for tool in shared_tools:
        self._process_single_tool(tool, result)

    # Then regular tools
    for tool in regular_tools:
        self._process_single_tool(tool, result)

    # Clean up and finalize result
    self._finalize_result(result)

    return result

registration

Tool registration processor.

ToolRegistrationProcessor

ToolRegistrationProcessor(
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
)

Processor for tool registration code.

Parameters:

NameTypeDescriptionDefault
agentslist[WaldiezAgent]

The agents that use tools.

required
agent_namesdict[str, str]

Mapping of agent IDs to names.

required
toolslist[WaldiezTool]

The available tools.

required
tool_namesdict[str, str]

Mapping of tool IDs to names.

required
Source code in waldiez/exporting/tools/registration.py
def __init__(
    self,
    agents: list[WaldiezAgent],
    agent_names: dict[str, str],
    tools: list[WaldiezTool],
    tool_names: dict[str, str],
):
    """Initialize the tool registration processor.

    Parameters
    ----------
    agents : list[WaldiezAgent]
        The agents that use tools.
    agent_names : dict[str, str]
        Mapping of agent IDs to names.
    tools : list[WaldiezTool]
        The available tools.
    tool_names : dict[str, str]
        Mapping of tool IDs to names.
    """
    self.agents = agents
    self.agent_names = agent_names
    self.tools = tools
    self.tool_names = tool_names

process

process() -> str

Process tool registrations for all agents.

Returns:

TypeDescription
str

The generated tool registration code for all agents.

Source code in waldiez/exporting/tools/registration.py
def process(self) -> str:
    """Process tool registrations for all agents.

    Returns
    -------
    str
        The generated tool registration code for all agents.
    """
    registrations: list[str] = []

    for agent in self.agents:
        agent_registration = self._process_agent_registrations(agent)
        if agent_registration:
            registrations.append(agent_registration)

    return "\n\n".join(registrations) if registrations else ""