-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
799afc3
commit 8e82125
Showing
3 changed files
with
48 additions
and
48 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
examples/code/home/crewai/custom_auth_flow_callback_section.py
This file contains 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,46 @@ | ||
from typing import Any | ||
|
||
from crewai_arcade import ArcadeToolManager | ||
|
||
USER_ID = "user@example.com" | ||
|
||
def custom_auth_flow( | ||
manager: ArcadeToolManager, tool_name: str, **tool_input: dict[str, Any] | ||
) -> Any: | ||
"""Custom auth flow for the ArcadeToolManager | ||
This function is called when CrewAI needs to call a tool that requires authorization. | ||
Authorization is handled before executing the tool. | ||
This function overrides the ArcadeToolManager's default auth flow performed by ArcadeToolManager.authorize_tool | ||
""" | ||
# Get authorization status | ||
auth_response = manager.authorize(tool_name, USER_ID) | ||
|
||
# If the user is not authorized for the tool, | ||
# then we need to handle the authorization before executing the tool | ||
if not manager.is_authorized(auth_response.id): | ||
print(f"Authorization required for tool: '{tool_name}' with inputs:") | ||
for input_name, input_value in tool_input.items(): | ||
print(f" {input_name}: {input_value}") | ||
# Handle authorization | ||
print(f"\nTo authorize, visit: {auth_response.url}") | ||
# Block until the user has completed the authorization | ||
auth_response = manager.wait_for_auth(auth_response) | ||
|
||
# Ensure authorization completed successfully | ||
if not manager.is_authorized(auth_response.id): | ||
raise ValueError(f"Authorization failed for {tool_name}. URL: {auth_response.url}") | ||
else: | ||
print(f"Authorization already granted for tool: '{tool_name}' with inputs:") | ||
for input_name, input_value in tool_input.items(): | ||
print(f" {input_name}: {input_value}") | ||
|
||
|
||
def tool_manager_callback(tool_manager: ArcadeToolManager, tool_name: str, **tool_input: dict[str, Any]) -> Any: | ||
"""Tool executor callback with custom auth flow for the ArcadeToolManager | ||
ArcadeToolManager's default executor handles authorization and tool execution. | ||
This function overrides the default executor to handle authorization in a custom way and then executes the tool. | ||
""" | ||
custom_auth_flow(tool_manager, tool_name, **tool_input) | ||
return tool_manager.execute_tool(USER_ID, tool_name, **tool_input) |
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export default { | ||
"use-arcade-tools": "Using Arcade tools", | ||
"custom-auth-flow": "Custom auth flow", | ||
"custom-auth-flow": "Custom Auth Flow", | ||
}; |
This file contains 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