refactor: update type hints to use union syntax and improve import order

This commit is contained in:
Harry
2025-07-02 16:15:28 +08:00
parent 1d92e2dbc0
commit e8cc4760cb
66 changed files with 500 additions and 591 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
from dify_plugin import Plugin, DifyPluginEnv
from dify_plugin import DifyPluginEnv, Plugin
plugin = Plugin(DifyPluginEnv(MAX_REQUEST_TIMEOUT=120))
if __name__ == '__main__':
if __name__ == "__main__":
plugin.run()
@@ -1,10 +1,13 @@
import json
from collections.abc import Generator
from datetime import datetime
from typing import Any, Generator
from typing import Any
from urllib.parse import quote
import requests
from dify_plugin.entities.tool import ToolInvokeMessage
from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage
class GithubRepositoriesTool(Tool):
@@ -69,4 +72,4 @@ class GithubRepositoriesTool(Tool):
else:
yield self.create_text_message(response.json().get("message"))
except Exception as e:
yield self.create_text_message("GitHub API Key and Api Version is invalid. {}".format(e))
yield self.create_text_message(f"GitHub API Key and Api Version is invalid. {e}")
@@ -1,16 +1,16 @@
import base64
from typing import Any, Generator
from collections.abc import Generator
from typing import Any
import requests
from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage
from dify_plugin.errors.model import InvokeError
class GithubRepositoryReadmeTool(Tool):
def _invoke(
self, tool_parameters: dict[str, Any]
) -> Generator[ToolInvokeMessage, None, None]:
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]:
"""
invoke tools
"""
@@ -48,14 +48,15 @@ class GithubRepositoryReadmeTool(Tool):
)
response_data = response.json()
if response.status_code == 200:
if "base64" != response_data.get("encoding"):
if response_data.get("encoding") != "base64":
raise InvokeError(
f"Can not get base64 encoded readme, response encoding is {response_data.get('encoding')}")
f"Can not get base64 encoded readme, response encoding is {response_data.get('encoding')}"
)
content = response_data.get("content")
if not content:
raise InvokeError("README content is empty")
decoded_bytes = base64.b64decode(content)
decoded_str = decoded_bytes.decode('utf-8')
decoded_str = decoded_bytes.decode("utf-8")
yield self.create_text_message(decoded_str)
else:
raise InvokeError(f"Request failed: {response.status_code} {response_data.get('message')}")