From 30069eefe373052ee7551faa895303df348f5268 Mon Sep 17 00:00:00 2001 From: Dirk Brand <51947788+dirkbrnd@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:30:04 +0200 Subject: [PATCH] Fix style from ClickupTools (#1877) --- cookbook/tools/clickup_tools.py | 41 ++++++++++++++++-------------- phi/tools/clickup_tool.py | 44 ++++++++++++--------------------- phi/tools/slack.py | 2 +- 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/cookbook/tools/clickup_tools.py b/cookbook/tools/clickup_tools.py index 29e137739..7a8b6b109 100644 --- a/cookbook/tools/clickup_tools.py +++ b/cookbook/tools/clickup_tools.py @@ -10,7 +10,7 @@ Steps To find your MASTER_SPACE_ID : clickup space url structure: https://app.clickup.com/{MASTER_SPACE_ID}/v/o/s/{SPACE_ID} -1. copy any space url from your clickup workspace all follow above url structure. +1. copy any space url from your clickup workspace all follow above url structure. 2. To use clickup tool copy the MASTER_SPACE_ID and store it .env file. """ @@ -26,23 +26,26 @@ tools=[ClickUpTools(list_spaces=True, list_lists=True, list_tasks=True)], instructions=[ "You are a ClickUp assistant that helps users manage their tasks and spaces.", - "You can:", - "1. List all available spaces", - "2. List tasks from a specific space", - "3. List all lists in a space", - "4. Create new tasks with title, description, and status", - - "When creating tasks:", - "- Always get space name, task name, and description", - "- Status can be: todo, in progress, or done", - "- If status is not specified, use 'todo' as default", - - "Be helpful and guide users if they need more information.", - ], + "You can:", + "1. List all available spaces", + "2. List tasks from a specific space", + "3. List all lists in a space", + "4. Create new tasks with title, description, and status", + "When creating tasks:", + "- Always get space name, task name, and description", + "- Status can be: todo, in progress, or done", + "- If status is not specified, use 'todo' as default", + "Be helpful and guide users if they need more information.", + ], show_tool_calls=True, - markdown=True - ) - -clickup_agent.print_response("List all spaces i have", markdown=True,) -clickup_agent.print_response("Create a task (status 'To Do') called 'QA task' in Project 1 in the Team Space. The description should be about running basic QA checks on our Python codebase.", markdown=True) + markdown=True, +) +clickup_agent.print_response( + "List all spaces i have", + markdown=True, +) +clickup_agent.print_response( + "Create a task (status 'To Do') called 'QA task' in Project 1 in the Team Space. The description should be about running basic QA checks on our Python codebase.", + markdown=True, +) diff --git a/phi/tools/clickup_tool.py b/phi/tools/clickup_tool.py index 9579b90b1..ef80a89c4 100644 --- a/phi/tools/clickup_tool.py +++ b/phi/tools/clickup_tool.py @@ -1,7 +1,7 @@ import os import json import re -from typing import Optional, List, Dict, Any, Union, Tuple +from typing import Optional, List, Dict, Any from phi.tools import Toolkit from phi.utils.log import logger @@ -23,7 +23,7 @@ def __init__( update_task: bool = True, delete_task: bool = True, list_spaces: bool = True, - list_lists: bool = True + list_lists: bool = True, ): super().__init__(name="clickup") @@ -52,31 +52,26 @@ def __init__( if list_lists: self.register(self.list_lists) - def _make_request(self, method: str, endpoint: str, params: Dict = None, data: Dict = None) -> Dict[str, Any]: + def _make_request( + self, method: str, endpoint: str, params: Optional[Dict] = None, data: Optional[Dict] = None + ) -> Dict[str, Any]: """Make a request to the ClickUp API.""" url = f"{self.base_url}/{endpoint}" try: - response = requests.request( - method=method, - url=url, - headers=self.headers, - params=params, - json=data - ) + response = requests.request(method=method, url=url, headers=self.headers, params=params, json=data) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: logger.error(f"Error making request to {url}: {e}") return {"error": str(e)} - def _find_by_name(self, items: List[Dict[str, Any]], name: str, item_type: str) -> Union[Dict[str, Any], None]: + def _find_by_name(self, items: List[Dict[str, Any]], name: str) -> Optional[Dict[str, Any]]: """Find an item in a list by name using exact match or regex pattern. - + Args: items: List of items to search through name: Name to search for - item_type: Type of item (for error message) - + Returns: Matching item or None if not found """ @@ -93,7 +88,7 @@ def _find_by_name(self, items: List[Dict[str, Any]], name: str, item_type: str) return item return None - def _get_space(self, space_name: str = None) -> Dict[str, Any]: + def _get_space(self, space_name: str) -> Dict[str, Any]: """Get space information by name.""" spaces = self._make_request("GET", f"team/{self.master_space_id}/space") if "error" in spaces: @@ -103,12 +98,12 @@ def _get_space(self, space_name: str = None) -> Dict[str, Any]: if not spaces_list: return {"error": "No spaces found"} - space = self._find_by_name(spaces_list, space_name, "space") + space = self._find_by_name(spaces_list, space_name) if not space: return {"error": f"Space '{space_name}' not found"} return space - def _get_list(self, space_id: str, list_name: str = None) -> Dict[str, Any]: + def _get_list(self, space_id: str, list_name: str) -> Dict[str, Any]: """Get list information by name.""" lists = self._make_request("GET", f"space/{space_id}/list") if "error" in lists: @@ -118,21 +113,18 @@ def _get_list(self, space_id: str, list_name: str = None) -> Dict[str, Any]: if not lists_data: return {"error": "No lists found in space"} - list_item = self._find_by_name(lists_data, list_name, "list") + list_item = self._find_by_name(lists_data, list_name) if not list_item: return {"error": f"List '{list_name}' not found"} return list_item - def _get_tasks(self, list_id: str, task_name: str = None) -> List[Dict[str, Any]]: + def _get_tasks(self, list_id: str) -> List[Dict[str, Any]]: """Get tasks in a list, optionally filtered by name.""" tasks = self._make_request("GET", f"list/{list_id}/task") if "error" in tasks: return [] tasks_data = tasks.get("tasks", []) - if task_name: - task = self._find_by_name(tasks_data, task_name, "task") - return [task] if task else [] return tasks_data def list_tasks(self, space_name: str) -> str: @@ -187,19 +179,15 @@ def create_task(self, space_name: str, task_name: str, task_description: str) -> lists_data = response.get("lists", []) if not lists_data: return json.dumps({"error": f"No lists found in space '{space_name}'"}, indent=2) - + list_info = lists_data[0] # Use first list # Create task - data = { - "name": task_name, - "description": task_description - } + data = {"name": task_name, "description": task_description} task = self._make_request("POST", f"list/{list_info['id']}/task", data=data) return json.dumps(task, indent=2) - def list_spaces(self) -> str: """List all spaces in the workspace. diff --git a/phi/tools/slack.py b/phi/tools/slack.py index bef97a843..4ca473dae 100644 --- a/phi/tools/slack.py +++ b/phi/tools/slack.py @@ -78,7 +78,7 @@ def get_channel_history(self, channel: str, limit: int = 100) -> str: """ try: response = self.client.conversations_history(channel=channel, limit=limit) - messages: List[Dict[str, Any]] = [ + messages: List[Dict[str, Any]] = [ # type: ignore { "text": msg.get("text", ""), "user": "webhook" if msg.get("subtype") == "bot_message" else msg.get("user", "unknown"),