Spaces:
Runtime error
Runtime error
Commit
·
85378b4
1
Parent(s):
30c27ae
Add GetTaskTool to retrieve specific tasks from Notion, including content and pagination support
Browse files
agency_ai_demo/agents/NotionProjectAgent/NotionProjectAgent.py
CHANGED
@@ -2,6 +2,8 @@ import os
|
|
2 |
|
3 |
from agency_swarm.agents import Agent
|
4 |
from .tools.GetTasks import GetTasksTool
|
|
|
|
|
5 |
|
6 |
class NotionProjectAgent(Agent):
|
7 |
def __init__(self):
|
@@ -20,7 +22,7 @@ class NotionProjectAgent(Agent):
|
|
20 |
instructions="./instructions.md",
|
21 |
files_folder="./files",
|
22 |
schemas_folder="./schemas",
|
23 |
-
tools=[GetTasksTool],
|
24 |
tools_folder="./tools",
|
25 |
model="gpt-4o",
|
26 |
temperature=0.3,
|
|
|
2 |
|
3 |
from agency_swarm.agents import Agent
|
4 |
from .tools.GetTasks import GetTasksTool
|
5 |
+
from .tools.GetTask import GetTaskTool
|
6 |
+
|
7 |
|
8 |
class NotionProjectAgent(Agent):
|
9 |
def __init__(self):
|
|
|
22 |
instructions="./instructions.md",
|
23 |
files_folder="./files",
|
24 |
schemas_folder="./schemas",
|
25 |
+
tools=[GetTasksTool, GetTaskTool],
|
26 |
tools_folder="./tools",
|
27 |
model="gpt-4o",
|
28 |
temperature=0.3,
|
agency_ai_demo/agents/NotionProjectAgent/tools/GetTask.py
CHANGED
@@ -2,6 +2,7 @@ import os
|
|
2 |
from dotenv import load_dotenv
|
3 |
from agency_swarm.tools import BaseTool
|
4 |
from pydantic import Field
|
|
|
5 |
|
6 |
load_dotenv()
|
7 |
|
@@ -11,8 +12,8 @@ notion_integration_secret = os.getenv("NOTION_INTEGRATION_SECRET")
|
|
11 |
class GetTaskTool(BaseTool):
|
12 |
"""
|
13 |
Tool for retrieving a specific task (page) from Notion.
|
14 |
-
This tool fetches
|
15 |
-
|
16 |
"""
|
17 |
|
18 |
# Add example_field with a default value to satisfy BaseTool validation
|
@@ -26,9 +27,41 @@ class GetTaskTool(BaseTool):
|
|
26 |
description="The ID of the Notion page (task) to retrieve. This is a required field.",
|
27 |
)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
def run(self):
|
30 |
"""
|
31 |
-
Retrieve a Notion page (task) by its ID.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
Returns:
|
34 |
dict: The JSON response from the Notion API containing the page properties.
|
@@ -48,5 +81,92 @@ class GetTaskTool(BaseTool):
|
|
48 |
# Make the request
|
49 |
response = requests.get(url, headers=headers)
|
50 |
|
51 |
-
# Return the JSON response
|
52 |
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
6 |
|
7 |
load_dotenv()
|
8 |
|
|
|
12 |
class GetTaskTool(BaseTool):
|
13 |
"""
|
14 |
Tool for retrieving a specific task (page) from Notion.
|
15 |
+
This tool fetches the properties of a specific task using its page ID and
|
16 |
+
by default, also fetches the page content (blocks) with all nested blocks.
|
17 |
"""
|
18 |
|
19 |
# Add example_field with a default value to satisfy BaseTool validation
|
|
|
27 |
description="The ID of the Notion page (task) to retrieve. This is a required field.",
|
28 |
)
|
29 |
|
30 |
+
include_content: bool = Field(
|
31 |
+
default=True,
|
32 |
+
description="Whether to include the page content (blocks) in the response. When True, all nested blocks will be included.",
|
33 |
+
)
|
34 |
+
|
35 |
+
page_size: int = Field(
|
36 |
+
default=100,
|
37 |
+
description="Number of blocks to retrieve per request. Maximum is 100.",
|
38 |
+
)
|
39 |
+
|
40 |
def run(self):
|
41 |
"""
|
42 |
+
Retrieve a Notion page (task) by its ID, optionally including its content.
|
43 |
+
|
44 |
+
Returns:
|
45 |
+
dict: A dictionary containing the page properties and optionally its content.
|
46 |
+
"""
|
47 |
+
import requests
|
48 |
+
|
49 |
+
result = {}
|
50 |
+
|
51 |
+
# Fetch page properties
|
52 |
+
properties = self._get_page_properties()
|
53 |
+
result["properties"] = properties
|
54 |
+
|
55 |
+
# Fetch page content if requested
|
56 |
+
if self.include_content:
|
57 |
+
content = self._get_page_content()
|
58 |
+
result["content"] = content
|
59 |
+
|
60 |
+
return result
|
61 |
+
|
62 |
+
def _get_page_properties(self) -> Dict[str, Any]:
|
63 |
+
"""
|
64 |
+
Retrieve the properties of a Notion page.
|
65 |
|
66 |
Returns:
|
67 |
dict: The JSON response from the Notion API containing the page properties.
|
|
|
81 |
# Make the request
|
82 |
response = requests.get(url, headers=headers)
|
83 |
|
|
|
84 |
return response.json()
|
85 |
+
|
86 |
+
def _get_page_content(self) -> List[Dict[str, Any]]:
|
87 |
+
"""
|
88 |
+
Retrieve the content (blocks) of a Notion page including all nested blocks.
|
89 |
+
|
90 |
+
Returns:
|
91 |
+
list: A list of block objects representing the page content.
|
92 |
+
"""
|
93 |
+
import requests
|
94 |
+
|
95 |
+
# Since pages are also blocks, we can use the page ID as a block ID
|
96 |
+
blocks = []
|
97 |
+
has_more = True
|
98 |
+
start_cursor = None
|
99 |
+
|
100 |
+
# Set up the headers
|
101 |
+
headers = {
|
102 |
+
"Authorization": f"Bearer {os.getenv('NOTION_INTEGRATION_SECRET')}",
|
103 |
+
"Notion-Version": "2022-06-28",
|
104 |
+
"Content-Type": "application/json",
|
105 |
+
}
|
106 |
+
|
107 |
+
# Paginate through all blocks
|
108 |
+
while has_more:
|
109 |
+
# Build URL with pagination parameters
|
110 |
+
url = f"https://api.notion.com/v1/blocks/{self.page_id}/children?page_size={self.page_size}"
|
111 |
+
if start_cursor:
|
112 |
+
url += f"&start_cursor={start_cursor}"
|
113 |
+
|
114 |
+
# Make the request
|
115 |
+
response = requests.get(url, headers=headers)
|
116 |
+
data = response.json()
|
117 |
+
|
118 |
+
# Add blocks to our result
|
119 |
+
if "results" in data:
|
120 |
+
current_blocks = data["results"]
|
121 |
+
|
122 |
+
# Always fetch children of blocks with children
|
123 |
+
for i, block in enumerate(current_blocks):
|
124 |
+
if block.get("has_children", False):
|
125 |
+
current_blocks[i]["children"] = self._get_block_children(
|
126 |
+
block["id"]
|
127 |
+
)
|
128 |
+
|
129 |
+
blocks.extend(current_blocks)
|
130 |
+
|
131 |
+
# Update pagination info
|
132 |
+
has_more = data.get("has_more", False)
|
133 |
+
start_cursor = data.get("next_cursor")
|
134 |
+
|
135 |
+
return blocks
|
136 |
+
|
137 |
+
def _get_block_children(self, block_id: str) -> List[Dict[str, Any]]:
|
138 |
+
"""
|
139 |
+
Recursively retrieve children of a block.
|
140 |
+
|
141 |
+
Args:
|
142 |
+
block_id: The ID of the block to retrieve children for.
|
143 |
+
|
144 |
+
Returns:
|
145 |
+
list: A list of block objects representing the block's children.
|
146 |
+
"""
|
147 |
+
import requests
|
148 |
+
|
149 |
+
# Set up the headers
|
150 |
+
headers = {
|
151 |
+
"Authorization": f"Bearer {os.getenv('NOTION_INTEGRATION_SECRET')}",
|
152 |
+
"Notion-Version": "2022-06-28",
|
153 |
+
"Content-Type": "application/json",
|
154 |
+
}
|
155 |
+
|
156 |
+
# Build URL
|
157 |
+
url = f"https://api.notion.com/v1/blocks/{block_id}/children?page_size={self.page_size}"
|
158 |
+
|
159 |
+
# Make the request
|
160 |
+
response = requests.get(url, headers=headers)
|
161 |
+
data = response.json()
|
162 |
+
|
163 |
+
blocks = []
|
164 |
+
if "results" in data:
|
165 |
+
blocks = data["results"]
|
166 |
+
|
167 |
+
# Recursively fetch children
|
168 |
+
for i, block in enumerate(blocks):
|
169 |
+
if block.get("has_children", False):
|
170 |
+
blocks[i]["children"] = self._get_block_children(block["id"])
|
171 |
+
|
172 |
+
return blocks
|