joaomorossini commited on
Commit
49f4f70
·
1 Parent(s): 85378b4

Add CreateTaskTool for creating tasks in Notion database with specified properties

Browse files
agency_ai_demo/agents/NotionProjectAgent/tools/CreateTask.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ notion_database_id = os.getenv("NOTION_DATABASE_ID")
11
+
12
+
13
+ class CreateTaskTool(BaseTool):
14
+ """
15
+ Tool for creating a new task (page) in a Notion database.
16
+ This tool creates a new page with specified properties and optionally
17
+ adds content blocks to the page.
18
+ """
19
+
20
+ # Add example_field with a default value to satisfy BaseTool validation
21
+ example_field: str = Field(
22
+ default="notion_create_task",
23
+ description="Identifier for this tool. Can be left at its default value.",
24
+ )
25
+
26
+ title: str = Field(
27
+ ...,
28
+ description="The title of the task (required).",
29
+ )
30
+
31
+ status: str = Field(
32
+ default=None,
33
+ description="Status of the task. Options: Backlog, In Progress, In Review, Testing, Completed.",
34
+ )
35
+
36
+ priority: str = Field(
37
+ default=None,
38
+ description="Priority of the task. Options: High, Medium, Low.",
39
+ )
40
+
41
+ due_date: str = Field(
42
+ default=None,
43
+ description="Due date of the task in YYYY-MM-DD format.",
44
+ )
45
+
46
+ assigned_to: List[str] = Field(
47
+ default=None,
48
+ description="List of user IDs to assign the task to.",
49
+ )
50
+
51
+ content_blocks: List[Dict[str, Any]] = Field(
52
+ default=None,
53
+ description="List of content blocks to add to the page. See Notion API Block documentation for format.",
54
+ )
55
+
56
+ def run(self):
57
+ """
58
+ Create a new task (page) in a Notion database with the specified properties and content.
59
+
60
+ Returns:
61
+ dict: The JSON response from the Notion API containing the created page.
62
+ """
63
+ import requests
64
+
65
+ # Use the database ID from the parameter or environment variable
66
+ database_id = os.getenv("NOTION_DATABASE_ID")
67
+
68
+ # Set up the API endpoint
69
+ url = "https://api.notion.com/v1/pages"
70
+
71
+ # Set up the headers
72
+ headers = {
73
+ "Authorization": f"Bearer {os.getenv('NOTION_INTEGRATION_SECRET')}",
74
+ "Notion-Version": "2022-06-28",
75
+ "Content-Type": "application/json",
76
+ }
77
+
78
+ # Prepare the request body
79
+ data = {
80
+ "parent": {"type": "database_id", "database_id": database_id},
81
+ "properties": self._build_properties(),
82
+ }
83
+
84
+ # Add children blocks if provided
85
+ if self.content_blocks:
86
+ data["children"] = self.content_blocks
87
+
88
+ # Make the request
89
+ response = requests.post(url, headers=headers, json=data)
90
+
91
+ # Return the JSON response
92
+ return response.json()
93
+
94
+ def _build_properties(self) -> Dict[str, Any]:
95
+ """
96
+ Build the properties object based on provided parameters.
97
+
98
+ Returns:
99
+ dict: A dictionary containing the page properties in Notion API format.
100
+ """
101
+ properties = {}
102
+
103
+ # Title is required
104
+ properties["Task Name"] = {
105
+ "title": [{"type": "text", "text": {"content": self.title}}]
106
+ }
107
+
108
+ # Add status if provided
109
+ if self.status:
110
+ properties["Status"] = {"status": {"name": self.status}}
111
+
112
+ # Add priority if provided
113
+ if self.priority:
114
+ properties["Priority"] = {"select": {"name": self.priority}}
115
+
116
+ # Add due date if provided
117
+ if self.due_date:
118
+ properties["Due Date"] = {"date": {"start": self.due_date}}
119
+
120
+ # Add assigned people if provided
121
+ if self.assigned_to:
122
+ properties["Assigned to"] = {
123
+ "people": [{"id": user_id} for user_id in self.assigned_to]
124
+ }
125
+
126
+ return properties