Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions src/uipath_langchain/agent/tools/tool_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,13 @@
logger = getLogger(__name__)


def _is_user_token() -> bool:
"""Check if the current token is a user token (sub_type == 'user')."""
try:
from uipath._cli._utils._common import get_claim_from_token

sub_type = get_claim_from_token("sub_type")
logger.info("Token sub_type=%r", sub_type)
return sub_type == "user"
except Exception as e:
logger.info("Token sub_type check failed: %s", e)
return False


async def create_tools_from_resources(
agent: LowCodeAgentDefinition, llm: BaseChatModel
agent: LowCodeAgentDefinition,
llm: BaseChatModel,
run_as_me: bool = False,
) -> list[BaseTool]:

tools: list[BaseTool] = []
is_user = _is_user_token()
run_as_me = agent.is_conversational and is_user
logger.info(
"RunAsMe decision: is_conversational=%s, is_user_token=%s, run_as_me=%s",
agent.is_conversational,
is_user,
run_as_me,
)

logger.info("Creating tools for agent '%s' from resources", agent.name)
logger.info("Creating tools for agent '%s' (run_as_me=%s)", agent.name, run_as_me)

for resource in agent.resources:
if not resource.is_enabled:
Expand Down
50 changes: 50 additions & 0 deletions tests/agent/tools/test_tool_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,53 @@ async def test_function_resource_routes_through_process_tool_path(
function_resource, run_as_me=False
)
assert tool is not None

async def test_run_as_me_propagates_to_process_tool(
self, process_resource, mock_uipath_sdk
):
"""run_as_me passed into create_tools_from_resources is forwarded to create_process_tool."""
process_resource.is_enabled = True
agent = LowCodeAgentDefinition(
input_schema={"type": "object", "properties": {}},
output_schema={"type": "object", "properties": {}},
messages=[],
settings=Mock(spec=AgentSettings),
resources=[process_resource],
)
mock_llm = AsyncMock(spec=BaseChatModel)
with patch(
"uipath_langchain.agent.tools.tool_factory.create_process_tool"
) as mock_create_process_tool:
mock_create_process_tool.return_value = MagicMock(
spec=BaseUiPathStructuredTool
)
await create_tools_from_resources(agent, mock_llm, run_as_me=True)

mock_create_process_tool.assert_called_once_with(
process_resource, run_as_me=True
)

async def test_run_as_me_defaults_false_when_not_provided(
self, process_resource, mock_uipath_sdk
):
"""run_as_me defaults to False when omitted (non-conversational agents)."""
process_resource.is_enabled = True
agent = LowCodeAgentDefinition(
input_schema={"type": "object", "properties": {}},
output_schema={"type": "object", "properties": {}},
messages=[],
settings=Mock(spec=AgentSettings),
resources=[process_resource],
)
mock_llm = AsyncMock(spec=BaseChatModel)
with patch(
"uipath_langchain.agent.tools.tool_factory.create_process_tool"
) as mock_create_process_tool:
mock_create_process_tool.return_value = MagicMock(
spec=BaseUiPathStructuredTool
)
await create_tools_from_resources(agent, mock_llm)

mock_create_process_tool.assert_called_once_with(
process_resource, run_as_me=False
)
Loading