|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""A function a group makes available as a tool can be the entry point of a workflow (as MCP servers do).""" |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from nat.builder.builder import Builder |
| 20 | +from nat.builder.function import FunctionGroup |
| 21 | +from nat.builder.function_info import FunctionInfo |
| 22 | +from nat.builder.workflow_builder import WorkflowBuilder |
| 23 | +from nat.cli.register_workflow import register_function |
| 24 | +from nat.cli.register_workflow import register_function_group |
| 25 | +from nat.cli.register_workflow import register_per_user_function |
| 26 | +from nat.cli.register_workflow import register_per_user_function_group |
| 27 | +from nat.data_models.config import Config |
| 28 | +from nat.data_models.function import FunctionBaseConfig |
| 29 | +from nat.data_models.function import FunctionGroupBaseConfig |
| 30 | +from nat.runtime.session import SessionManager |
| 31 | + |
| 32 | + |
| 33 | +class TextToolsConfig(FunctionGroupBaseConfig, name="entry_test_text_tools"): |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +class EchoWorkflowConfig(FunctionBaseConfig, name="entry_test_echo_workflow"): |
| 38 | + pass |
| 39 | + |
| 40 | + |
| 41 | +class PerUserTextToolsConfig(FunctionGroupBaseConfig, name="entry_test_per_user_text_tools"): |
| 42 | + pass |
| 43 | + |
| 44 | + |
| 45 | +class PerUserEchoWorkflowConfig(FunctionBaseConfig, name="entry_test_per_user_echo_workflow"): |
| 46 | + pass |
| 47 | + |
| 48 | + |
| 49 | +def _text_tools_group(config) -> FunctionGroup: |
| 50 | + group = FunctionGroup(config=config) |
| 51 | + |
| 52 | + async def upper(text: str) -> str: |
| 53 | + return text.upper() |
| 54 | + |
| 55 | + async def reverse(text: str) -> str: |
| 56 | + return text[::-1] |
| 57 | + |
| 58 | + group.add_function("upper", upper, description="upper-case the text") |
| 59 | + group.add_function("reverse", reverse, description="reverse the text") |
| 60 | + return group |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture(name="register_components", scope="module", autouse=True) |
| 64 | +def register_components_fixture(): |
| 65 | + |
| 66 | + @register_function_group(config_type=TextToolsConfig) |
| 67 | + async def _text_tools(config: TextToolsConfig, builder: Builder): |
| 68 | + yield _text_tools_group(config) |
| 69 | + |
| 70 | + @register_per_user_function_group(config_type=PerUserTextToolsConfig) |
| 71 | + async def _per_user_text_tools(config: PerUserTextToolsConfig, builder: Builder): |
| 72 | + yield _text_tools_group(config) |
| 73 | + |
| 74 | + @register_per_user_function(config_type=PerUserEchoWorkflowConfig, input_type=str, single_output_type=str) |
| 75 | + async def _per_user_workflow(config, builder: Builder): |
| 76 | + |
| 77 | + async def _inner(text: str) -> str: |
| 78 | + return text |
| 79 | + |
| 80 | + yield FunctionInfo.from_fn(_inner, description="echo") |
| 81 | + |
| 82 | + @register_function(config_type=EchoWorkflowConfig) |
| 83 | + async def _workflow(config, builder: Builder): |
| 84 | + |
| 85 | + async def _inner(text: str) -> str: |
| 86 | + return text |
| 87 | + |
| 88 | + yield FunctionInfo.from_fn(_inner, description="echo") |
| 89 | + |
| 90 | + |
| 91 | +def _config(group_type: str = "entry_test_text_tools", |
| 92 | + workflow_type: str = "entry_test_echo_workflow", |
| 93 | + **group_options) -> Config: |
| 94 | + return Config.model_validate({ |
| 95 | + "function_groups": { |
| 96 | + "text": { |
| 97 | + "_type": group_type, **group_options |
| 98 | + } |
| 99 | + }, |
| 100 | + "workflow": { |
| 101 | + "_type": workflow_type |
| 102 | + }, |
| 103 | + }) |
| 104 | + |
| 105 | + |
| 106 | +async def _run(config: Config, entry_function: str, user_id: str | None = None) -> str: |
| 107 | + async with WorkflowBuilder.from_config(config=config) as builder: |
| 108 | + session_manager = await SessionManager.create(config=config, |
| 109 | + shared_builder=builder, |
| 110 | + entry_function=entry_function) |
| 111 | + try: |
| 112 | + async with session_manager.session(user_id=user_id) as session: |
| 113 | + async with session.run("abc") as runner: |
| 114 | + return await runner.result(to_type=str) |
| 115 | + finally: |
| 116 | + await session_manager.shutdown() |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.parametrize("group_options", [{}, { |
| 120 | + "exclude": ["reverse"] |
| 121 | +}, { |
| 122 | + "include": ["upper"] |
| 123 | +}], |
| 124 | + ids=["no_filter", "exclude", "include"]) |
| 125 | +async def test_an_available_group_function_can_be_the_entry_function(group_options): |
| 126 | + assert await _run(_config(**group_options), "text__upper") == "ABC" |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.parametrize("entry_function", ["text__reverse", "text__missing", "missing"]) |
| 130 | +async def test_a_function_that_is_not_available_is_still_not_found(entry_function): |
| 131 | + with pytest.raises(ValueError, match="not found"): |
| 132 | + await _run(_config(exclude=["reverse"]), entry_function) |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize("group_type", ["entry_test_text_tools", "entry_test_per_user_text_tools"], |
| 136 | + ids=["shared_group", "per_user_group"]) |
| 137 | +async def test_a_per_user_workflow_can_use_a_group_function_as_the_entry_function(group_type): |
| 138 | + config = _config(group_type=group_type, workflow_type="entry_test_per_user_echo_workflow") |
| 139 | + assert await _run(config, "text__upper", user_id="user-1") == "ABC" |
| 140 | + |
| 141 | + |
| 142 | +class NestedToolsConfig(FunctionGroupBaseConfig, name="entry_test_nested_tools"): |
| 143 | + function_name: str |
| 144 | + |
| 145 | + |
| 146 | +@pytest.fixture(name="register_nested_group", scope="module") |
| 147 | +def register_nested_group_fixture(): |
| 148 | + |
| 149 | + @register_function_group(config_type=NestedToolsConfig) |
| 150 | + async def _nested_tools(config: NestedToolsConfig, builder: Builder): |
| 151 | + group = FunctionGroup(config=config) |
| 152 | + |
| 153 | + async def nested(text: str) -> str: |
| 154 | + return text |
| 155 | + |
| 156 | + group.add_function(config.function_name, nested, description="a function whose name may hold the separator") |
| 157 | + yield group |
| 158 | + |
| 159 | + |
| 160 | +@pytest.mark.usefixtures("register_nested_group") |
| 161 | +async def test_two_groups_making_the_same_name_available_is_an_error(): |
| 162 | + """Group `a` with `b__c` and group `a__b` with `c` both offer `a__b__c`.""" |
| 163 | + config = Config.model_validate({ |
| 164 | + "function_groups": { |
| 165 | + "a": { |
| 166 | + "_type": "entry_test_nested_tools", "function_name": "b__c" |
| 167 | + }, |
| 168 | + "a__b": { |
| 169 | + "_type": "entry_test_nested_tools", "function_name": "c" |
| 170 | + } |
| 171 | + }, |
| 172 | + "workflow": { |
| 173 | + "_type": "entry_test_echo_workflow" |
| 174 | + }, |
| 175 | + }) |
| 176 | + with pytest.raises(ValueError, match="ambiguous"): |
| 177 | + await _run(config, "a__b__c") |
0 commit comments