joaomorossini commited on
Commit
df8c722
·
1 Parent(s): 43dd578

Add UpdateTaskTool for modifying existing tasks in Notion

Browse files
agency_ai_demo/agents/NotionProjectAgent/NotionProjectAgent.py CHANGED
@@ -4,6 +4,7 @@ from agency_swarm.agents import Agent
4
  from .tools.GetTasks import GetTasksTool
5
  from .tools.GetTask import GetTaskTool
6
  from .tools.CreateTask import CreateTaskTool
 
7
 
8
 
9
  class NotionProjectAgent(Agent):
@@ -23,7 +24,7 @@ class NotionProjectAgent(Agent):
23
  instructions="./instructions.md",
24
  files_folder="./files",
25
  schemas_folder="./schemas",
26
- tools=[GetTasksTool, GetTaskTool, CreateTaskTool],
27
  tools_folder="./tools",
28
  model="gpt-4o",
29
  temperature=0.3,
 
4
  from .tools.GetTasks import GetTasksTool
5
  from .tools.GetTask import GetTaskTool
6
  from .tools.CreateTask import CreateTaskTool
7
+ from .tools.UpdateTask import UpdateTaskTool
8
 
9
 
10
  class NotionProjectAgent(Agent):
 
24
  instructions="./instructions.md",
25
  files_folder="./files",
26
  schemas_folder="./schemas",
27
+ tools=[GetTasksTool, GetTaskTool, CreateTaskTool, UpdateTaskTool],
28
  tools_folder="./tools",
29
  model="gpt-4o",
30
  temperature=0.3,
agency_ai_demo/agents/NotionProjectAgent/tools/UpdateTask.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from dotenv import load_dotenv
3
+ from agency_swarm.tools import BaseTool
4
+ from pydantic import Field
5
+ from typing import Dict, List, Optional, Any, Union
6
+
7
+ load_dotenv()
8
+
9
+ notion_integration_secret = os.getenv("NOTION_INTEGRATION_SECRET")
10
+
11
+
12
+ class UpdateTaskTool(BaseTool):
13
+ """
14
+ Tool for updating an existing task (page) in Notion.
15
+ This tool allows modifying task properties such as title, status, priority, due date, and assignments.
16
+ Note that this tool only updates properties, not the content blocks of the page.
17
+ """
18
+
19
+ # Add example_field with a default value to satisfy BaseTool validation
20
+ example_field: str = Field(
21
+ default="notion_update_task",
22
+ description="Identifier for this tool. Can be left at its default value.",
23
+ )
24
+
25
+ page_id: str = Field(
26
+ ...,
27
+ description="The ID of the Notion page (task) to update. This is a required field.",
28
+ )
29
+
30
+ title: str = Field(
31
+ default=None,
32
+ description="The new title of the task.",
33
+ )
34
+
35
+ status: str = Field(
36
+ default=None,
37
+ description="New status of the task. Options: Backlog, In Progress, In Review, Testing, Completed.",
38
+ )
39
+
40
+ priority: str = Field(
41
+ default=None,
42
+ description="New priority of the task. Options: High, Medium, Low.",
43
+ )
44
+
45
+ due_date: str = Field(
46
+ default=None,
47
+ description="New due date of the task in YYYY-MM-DD format. Use 'null' to remove the date.",
48
+ )
49
+
50
+ def run(self):
51
+ """
52
+ Update an existing Notion page (task) with the specified properties.
53
+
54
+ Returns:
55
+ dict: The JSON response from the Notion API containing the updated page.
56
+ """
57
+ import requests
58
+
59
+ # Set up the API endpoint
60
+ url = f"https://api.notion.com/v1/pages/{self.page_id}"
61
+
62
+ # Set up the headers
63
+ headers = {
64
+ "Authorization": f"Bearer {os.getenv('NOTION_INTEGRATION_SECRET')}",
65
+ "Notion-Version": "2022-06-28",
66
+ "Content-Type": "application/json",
67
+ }
68
+
69
+ # Prepare the request body
70
+ data = {"properties": self._build_properties()}
71
+
72
+ # Make the request (PATCH method for updates)
73
+ response = requests.patch(url, headers=headers, json=data)
74
+
75
+ # Return the JSON response
76
+ return response.json()
77
+
78
+ def _build_properties(self) -> Dict[str, Any]:
79
+ """
80
+ Build the properties object based on provided parameters.
81
+ Only includes properties that were specified for update.
82
+
83
+ Returns:
84
+ dict: A dictionary containing the page properties to update in Notion API format.
85
+ """
86
+ properties = {}
87
+
88
+ # Add title if provided
89
+ if self.title is not None:
90
+ properties["Task Name"] = {
91
+ "title": [{"type": "text", "text": {"content": self.title}}]
92
+ }
93
+
94
+ # Add status if provided
95
+ if self.status is not None:
96
+ properties["Status"] = {"status": {"name": self.status}}
97
+
98
+ # Add priority if provided
99
+ if self.priority is not None:
100
+ properties["Priority"] = {"select": {"name": self.priority}}
101
+
102
+ # Add due date if provided
103
+ if self.due_date is not None:
104
+ # Check if we're clearing the date
105
+ if self.due_date.lower() == "null":
106
+ properties["Due Date"] = {"date": None}
107
+ else:
108
+ properties["Due Date"] = {"date": {"start": self.due_date}}
109
+
110
+ # Add assigned people if provided
111
+ # if self.assigned_to is not None:
112
+ # properties["Assigned to"] = {
113
+ # "people": [{"id": user_id} for user_id in self.assigned_to]
114
+ # }
115
+
116
+ return properties