Skip to content

Commit

Permalink
formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-Robusta committed Feb 4, 2025
1 parent 5c7ba44 commit 98694d1
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions holmes/plugins/toolsets/internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
from typing import Any, Optional, Tuple, Dict

from requests import RequestException, Timeout
from holmes.core.tools import Tool, ToolParameter, Toolset, ToolsetTag, CallablePrerequisite
from holmes.core.tools import (
Tool,
ToolParameter,
Toolset,
ToolsetTag,
CallablePrerequisite,
)
from markdownify import markdownify
from bs4 import BeautifulSoup

Expand Down Expand Up @@ -157,7 +163,6 @@ def looks_like_html(content):

class FetchNotion(Tool):
toolset: "InternetToolset"

def __init__(self, toolset: "InternetToolset"):
super().__init__(
name="fetch_notion_webpage",
Expand All @@ -168,15 +173,19 @@ def __init__(self, toolset: "InternetToolset"):
type="string",
required=True,
),
"is_runbook": ToolParameter(description="True if the url is a runbook", type="boolean", required=True)
"is_runbook": ToolParameter(
description="True if the url is a runbook",
type="boolean",
required=True,
),
},
toolset=toolset,
)

def convert_notion_url(self, url):
if "api.notion.com" in url:
return url
match = re.search(r'-(\w{32})$', url)
match = re.search(r"-(\w{32})$", url)
if match:
notion_id = match.group(1)
return f"https://api.notion.com/v1/blocks/{notion_id}/children"
Expand All @@ -189,7 +198,6 @@ def invoke(self, params: Any) -> str:
# Get headers from the toolset configuration
additional_headers = self.toolset.runbook_headers if is_runbook else {}
url = self.convert_notion_url(url)

content, _ = scrape(url, additional_headers)

if not content:
Expand All @@ -201,12 +209,14 @@ def invoke(self, params: Any) -> str:
def parse_notion_content(self, content: Any) -> str:
data = json.loads(content)
texts = []
for result in data['results']:
if 'paragraph' in result and 'rich_text' in result['paragraph']:
texts.extend([text['plain_text'] for text in result['paragraph']['rich_text']])
for result in data["results"]:
if "paragraph" in result and "rich_text" in result["paragraph"]:
texts.extend(
[text["plain_text"] for text in result["paragraph"]["rich_text"]]
)

# Join and print the result
return ''.join(texts)
return "".join(texts)

def get_parameterized_one_liner(self, params) -> str:
url: str = params["url"]
Expand All @@ -216,7 +226,6 @@ def get_parameterized_one_liner(self, params) -> str:

class FetchWebpage(Tool):
toolset: "InternetToolset"

def __init__(self, toolset: "InternetToolset"):
super().__init__(
name="fetch_webpage",
Expand All @@ -227,7 +236,11 @@ def __init__(self, toolset: "InternetToolset"):
type="string",
required=True,
),
"is_runbook": ToolParameter(description="True if the url is a runbook", type="boolean", required=True)
"is_runbook": ToolParameter(
description="True if the url is a runbook",
type="boolean",
required=True,
),
},
toolset=toolset,
)
Expand All @@ -237,7 +250,6 @@ def invoke(self, params: Any) -> str:
is_runbook: bool = params.get("is_runbook", False)

additional_headers = self.toolset.runbook_headers if is_runbook else {}

content, mime_type = scrape(url, additional_headers)

if not content:
Expand All @@ -255,12 +267,14 @@ def invoke(self, params: Any) -> str:
def parse_notion_content(self, content: Any) -> str:
data = json.loads(content)
texts = []
for result in data['results']:
if 'paragraph' in result and 'rich_text' in result['paragraph']:
texts.extend([text['plain_text'] for text in result['paragraph']['rich_text']])
for result in data["results"]:
if "paragraph" in result and "rich_text" in result["paragraph"]:
texts.extend(
[text["plain_text"] for text in result["paragraph"]["rich_text"]]
)

# Join and print the result
return ''.join(texts)
return "".join(texts)

def get_parameterized_one_liner(self, params) -> str:
url: str = params["url"]
Expand All @@ -279,7 +293,10 @@ def __init__(self):
prerequisites=[
CallablePrerequisite(callable=self.prerequisites_callable),
],
tools=[FetchWebpage(self), FetchNotion(self),],
tools=[
FetchWebpage(self),
FetchNotion(self),
],
tags=[
ToolsetTag.CORE,
],
Expand Down

0 comments on commit 98694d1

Please sign in to comment.