-
Notifications
You must be signed in to change notification settings - Fork 34
feat: add client side tools to mapper and runtime [JAR-9629] #856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4ceca3b
feat: add client side tools to mapper and runtime
norman-le 748b832
feat: refactor functions and include support for simulated tools and …
norman-le 3e7f29c
chore: add client side tool validation and tool passing here
norman-le 1d108d2
chore: dont emit execute tool call for confirmation
norman-le 0f6e711
chore: extra validation
norman-le 050977d
test: validation and tests
norman-le f1ebd89
feat: update validation and remove tool name input from event
norman-le 451cd1f
refactor: send executing tool call event for unconfirmed tools
norman-le d5b66d1
refactor: rename and add is error check
norman-le 4a664cf
chore: update uv lock
norman-le 865a9ab
test: formatting and tests
norman-le 9a2f2f6
test: update tests
norman-le File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| """Factory for creating client-side tools that execute on the client SDK.""" | ||
|
|
||
| import json | ||
| from contextvars import ContextVar | ||
| from typing import Annotated, Any, TypedDict | ||
|
|
||
| from langchain_core.messages import ToolMessage | ||
| from langchain_core.tools import InjectedToolCallId, StructuredTool | ||
| from uipath.agent.models.agent import AgentClientSideToolResourceConfig | ||
| from uipath.eval.mocks import mockable | ||
|
|
||
| from uipath_langchain._utils.durable_interrupt import durable_interrupt | ||
| from uipath_langchain.agent.react.jsonschema_pydantic_converter import ( | ||
| create_model as create_model_from_schema, | ||
| ) | ||
| from uipath_langchain.chat.hitl import IS_CONVERSATIONAL_CLIENT_SIDE_TOOL | ||
|
|
||
| from .utils import sanitize_tool_name | ||
|
|
||
| # When set, only tools in this set are available for the current exchange. | ||
| # None means all client-side tools are available (default for CAS/web UI). | ||
| available_client_side_tools: ContextVar[set[str] | None] = ContextVar( | ||
| "available_client_side_tools", default=None | ||
| ) | ||
|
|
||
| UIPATH_CLIENT_SIDE_TOOLS_INPUT_KEY = "uipath__client_side_tools" | ||
|
|
||
|
|
||
| class ClientSideToolInfo(TypedDict): | ||
| input_schema: dict[str, Any] | None | ||
| output_schema: dict[str, Any] | None | ||
|
|
||
|
|
||
| def apply_tool_filter( | ||
| declared_tools: list[str | dict[str, Any]], | ||
| agent_tools: dict[str, ClientSideToolInfo], | ||
| ) -> None: | ||
| """Filter available client-side tools to the intersection of declared and agent tools. | ||
|
|
||
| Extracts tool names from the client's declarations, intersects with the agent's | ||
| defined client-side tools, and sets the availability filter. Unknown names are | ||
| silently ignored. | ||
|
|
||
| Args: | ||
| declared_tools: List of tool names (strings) or dicts with a 'name' field | ||
| from uipath__client_side_tools input. | ||
| agent_tools: The agent's client-side tools keyed by name. | ||
| """ | ||
| declared_names: set[str] = set() | ||
| for t in declared_tools: | ||
| if isinstance(t, str): | ||
| declared_names.add(t) | ||
| elif isinstance(t, dict) and "name" in t: | ||
| declared_names.add(t["name"]) | ||
|
|
||
| available_client_side_tools.set(declared_names & set(agent_tools.keys())) | ||
|
|
||
|
|
||
| def create_client_side_tool( | ||
|
Check failure on line 59 in src/uipath_langchain/agent/tools/client_side_tool.py
|
||
| resource: AgentClientSideToolResourceConfig, | ||
| ) -> StructuredTool: | ||
| """Create a client-side tool that pauses the graph and waits for the client to execute it. | ||
|
|
||
| The tool uses @durable_interrupt to suspend the graph. The client receives | ||
| an executingToolCall event, executes its registered handler, and sends | ||
| endToolCall back through CAS. | ||
| """ | ||
| tool_name = sanitize_tool_name(resource.name) | ||
| input_model = create_model_from_schema(resource.input_schema) | ||
|
|
||
| async def client_side_tool_fn( | ||
| *, tool_call_id: Annotated[str, InjectedToolCallId], **kwargs: Any | ||
| ) -> Any: | ||
| allowed = available_client_side_tools.get() | ||
| if allowed is not None and tool_name not in allowed: | ||
| return ToolMessage( | ||
| content=f"Tool '{tool_name}' is not available — the client has not registered a handler for it.", | ||
| tool_call_id=tool_call_id, | ||
| status="error", | ||
| ) | ||
|
|
||
| @mockable( | ||
| name=resource.name, | ||
| description=resource.description, | ||
| input_schema=input_model.model_json_schema(), | ||
| output_schema=(resource.output_schema or {}), | ||
| example_calls=getattr(resource.properties, "example_calls", None), | ||
| ) | ||
| async def execute_tool() -> dict[str, Any]: | ||
| """Execute client-side tool, pausing for client response.""" | ||
|
|
||
| @durable_interrupt | ||
| async def wait_for_client_execution() -> dict[str, Any]: | ||
| return { | ||
| "tool_call_id": tool_call_id, | ||
| "tool_name": tool_name, | ||
| "input": kwargs, | ||
| } | ||
|
|
||
| result = await wait_for_client_execution() | ||
| return result if isinstance(result, dict) else {"output": result} | ||
|
|
||
| result = await execute_tool() | ||
|
norman-le marked this conversation as resolved.
|
||
|
|
||
| is_error = result.get("isError", False) | ||
| output = result.get("output", result) | ||
|
|
||
| if isinstance(output, dict): | ||
| try: | ||
| content = json.dumps(output) | ||
| except TypeError: | ||
| content = str(output) | ||
| else: | ||
| content = str(output) if output is not None else "" | ||
|
|
||
| return ToolMessage( | ||
| content=content, | ||
| tool_call_id=tool_call_id, | ||
| status="error" if is_error else "success", | ||
| response_metadata={IS_CONVERSATIONAL_CLIENT_SIDE_TOOL: True}, | ||
| ) | ||
|
|
||
| tool = StructuredTool( | ||
| name=tool_name, | ||
| description=resource.description or f"Client-side tool: {tool_name}", | ||
| args_schema=input_model, | ||
| coroutine=client_side_tool_fn, | ||
| metadata={ | ||
| IS_CONVERSATIONAL_CLIENT_SIDE_TOOL: True, | ||
| "output_schema": resource.output_schema, | ||
| }, | ||
| ) | ||
|
|
||
| return tool | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question - how come
input_schemais fromt.args_schemaandoutput_schemais frommeta?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
StructuredToolclass in LangChain has a built-inargs_schemafield for defining what inputs a tool accepts. There's no equivalent built-in field for output schema. So when creating client-side tools, the input schema goes intoargs_schema(LangChain's standard field) and the output schema gets stashed in themetadatadict as a custom key. That's why inagent.py, input_schema is read fromt.args_schemaand output_schema is read frommeta.get("output_schema")— they live in different places because LangChain only has first-class support for the input side.