Skip to content

Commit

Permalink
move code to python file
Browse files Browse the repository at this point in the history
  • Loading branch information
EricGustin committed Feb 20, 2025
1 parent 799afc3 commit 8e82125
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
46 changes: 46 additions & 0 deletions examples/code/home/crewai/custom_auth_flow_callback_section.py
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)
2 changes: 1 addition & 1 deletion pages/home/crewai/_meta.ts
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",
};
48 changes: 1 addition & 47 deletions pages/home/crewai/custom-auth-flow.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,53 +38,7 @@ export OPENAI_API_KEY="your_openai_api_key"

The custom auth flow defined in the following code snippet is a function that will be called whenever CrewAI needs to call a tool.

```python
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)
```python file=<rootDir>/examples/code/home/crewai/custom_auth_flow_callback_section.py
```

### Get Arcade tools
Expand Down

0 comments on commit 8e82125

Please sign in to comment.