fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解
This commit is contained in:
@@ -4,20 +4,21 @@ InsightFlow Plugin Manager - Phase 7 Task 7
|
||||
插件与集成系统:Chrome插件、飞书/钉钉机器人、Zapier/Make集成、WebDAV同步
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import base64
|
||||
import hashlib
|
||||
import hmac
|
||||
import base64
|
||||
import time
|
||||
import uuid
|
||||
import httpx
|
||||
import urllib.parse
|
||||
from datetime import datetime
|
||||
from typing import Dict, List, Optional, Any
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
import time
|
||||
import urllib.parse
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
# WebDAV 支持
|
||||
try:
|
||||
@@ -58,10 +59,10 @@ class Plugin:
|
||||
plugin_type: str
|
||||
project_id: str
|
||||
status: str = "active"
|
||||
config: Dict = field(default_factory=dict)
|
||||
config: dict = field(default_factory=dict)
|
||||
created_at: str = ""
|
||||
updated_at: str = ""
|
||||
last_used_at: Optional[str] = None
|
||||
last_used_at: str | None = None
|
||||
use_count: int = 0
|
||||
|
||||
|
||||
@@ -86,13 +87,13 @@ class BotSession:
|
||||
bot_type: str # feishu, dingtalk
|
||||
session_id: str # 群ID或会话ID
|
||||
session_name: str
|
||||
project_id: Optional[str] = None
|
||||
project_id: str | None = None
|
||||
webhook_url: str = ""
|
||||
secret: str = ""
|
||||
is_active: bool = True
|
||||
created_at: str = ""
|
||||
updated_at: str = ""
|
||||
last_message_at: Optional[str] = None
|
||||
last_message_at: str | None = None
|
||||
message_count: int = 0
|
||||
|
||||
|
||||
@@ -104,14 +105,14 @@ class WebhookEndpoint:
|
||||
name: str
|
||||
endpoint_type: str # zapier, make, custom
|
||||
endpoint_url: str
|
||||
project_id: Optional[str] = None
|
||||
project_id: str | None = None
|
||||
auth_type: str = "none" # none, api_key, oauth, custom
|
||||
auth_config: Dict = field(default_factory=dict)
|
||||
trigger_events: List[str] = field(default_factory=list)
|
||||
auth_config: dict = field(default_factory=dict)
|
||||
trigger_events: list[str] = field(default_factory=list)
|
||||
is_active: bool = True
|
||||
created_at: str = ""
|
||||
updated_at: str = ""
|
||||
last_triggered_at: Optional[str] = None
|
||||
last_triggered_at: str | None = None
|
||||
trigger_count: int = 0
|
||||
|
||||
|
||||
@@ -128,7 +129,7 @@ class WebDAVSync:
|
||||
remote_path: str = "/insightflow"
|
||||
sync_mode: str = "bidirectional" # bidirectional, upload_only, download_only
|
||||
sync_interval: int = 3600 # 秒
|
||||
last_sync_at: Optional[str] = None
|
||||
last_sync_at: str | None = None
|
||||
last_sync_status: str = "pending" # pending, success, failed
|
||||
last_sync_error: str = ""
|
||||
is_active: bool = True
|
||||
@@ -143,13 +144,13 @@ class ChromeExtensionToken:
|
||||
|
||||
id: str
|
||||
token: str
|
||||
user_id: Optional[str] = None
|
||||
project_id: Optional[str] = None
|
||||
user_id: str | None = None
|
||||
project_id: str | None = None
|
||||
name: str = ""
|
||||
permissions: List[str] = field(default_factory=lambda: ["read", "write"])
|
||||
expires_at: Optional[str] = None
|
||||
permissions: list[str] = field(default_factory=lambda: ["read", "write"])
|
||||
expires_at: str | None = None
|
||||
created_at: str = ""
|
||||
last_used_at: Optional[str] = None
|
||||
last_used_at: str | None = None
|
||||
use_count: int = 0
|
||||
is_revoked: bool = False
|
||||
|
||||
@@ -171,7 +172,7 @@ class PluginManager:
|
||||
self._handlers[PluginType.MAKE] = WebhookIntegration(self, "make")
|
||||
self._handlers[PluginType.WEBDAV] = WebDAVSyncManager(self)
|
||||
|
||||
def get_handler(self, plugin_type: PluginType) -> Optional[Any]:
|
||||
def get_handler(self, plugin_type: PluginType) -> Any | None:
|
||||
"""获取插件处理器"""
|
||||
return self._handlers.get(plugin_type)
|
||||
|
||||
@@ -205,7 +206,7 @@ class PluginManager:
|
||||
plugin.updated_at = now
|
||||
return plugin
|
||||
|
||||
def get_plugin(self, plugin_id: str) -> Optional[Plugin]:
|
||||
def get_plugin(self, plugin_id: str) -> Plugin | None:
|
||||
"""获取插件"""
|
||||
conn = self.db.get_conn()
|
||||
row = conn.execute("SELECT * FROM plugins WHERE id = ?", (plugin_id,)).fetchone()
|
||||
@@ -215,7 +216,7 @@ class PluginManager:
|
||||
return self._row_to_plugin(row)
|
||||
return None
|
||||
|
||||
def list_plugins(self, project_id: str = None, plugin_type: str = None, status: str = None) -> List[Plugin]:
|
||||
def list_plugins(self, project_id: str = None, plugin_type: str = None, status: str = None) -> list[Plugin]:
|
||||
"""列出插件"""
|
||||
conn = self.db.get_conn()
|
||||
|
||||
@@ -239,7 +240,7 @@ class PluginManager:
|
||||
|
||||
return [self._row_to_plugin(row) for row in rows]
|
||||
|
||||
def update_plugin(self, plugin_id: str, **kwargs) -> Optional[Plugin]:
|
||||
def update_plugin(self, plugin_id: str, **kwargs) -> Plugin | None:
|
||||
"""更新插件"""
|
||||
conn = self.db.get_conn()
|
||||
|
||||
@@ -341,7 +342,7 @@ class PluginManager:
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
def get_plugin_config(self, plugin_id: str, key: str) -> Optional[str]:
|
||||
def get_plugin_config(self, plugin_id: str, key: str) -> str | None:
|
||||
"""获取插件配置"""
|
||||
conn = self.db.get_conn()
|
||||
row = conn.execute(
|
||||
@@ -351,7 +352,7 @@ class PluginManager:
|
||||
|
||||
return row["config_value"] if row else None
|
||||
|
||||
def get_all_plugin_configs(self, plugin_id: str) -> Dict[str, str]:
|
||||
def get_all_plugin_configs(self, plugin_id: str) -> dict[str, str]:
|
||||
"""获取插件所有配置"""
|
||||
conn = self.db.get_conn()
|
||||
rows = conn.execute(
|
||||
@@ -396,7 +397,7 @@ class ChromeExtensionHandler:
|
||||
name: str,
|
||||
user_id: str = None,
|
||||
project_id: str = None,
|
||||
permissions: List[str] = None,
|
||||
permissions: list[str] = None,
|
||||
expires_days: int = None,
|
||||
) -> ChromeExtensionToken:
|
||||
"""创建 Chrome 扩展令牌"""
|
||||
@@ -448,7 +449,7 @@ class ChromeExtensionHandler:
|
||||
created_at=now,
|
||||
)
|
||||
|
||||
def validate_token(self, token: str) -> Optional[ChromeExtensionToken]:
|
||||
def validate_token(self, token: str) -> ChromeExtensionToken | None:
|
||||
"""验证 Chrome 扩展令牌"""
|
||||
token_hash = hashlib.sha256(token.encode()).hexdigest()
|
||||
|
||||
@@ -501,7 +502,7 @@ class ChromeExtensionHandler:
|
||||
|
||||
return cursor.rowcount > 0
|
||||
|
||||
def list_tokens(self, user_id: str = None, project_id: str = None) -> List[ChromeExtensionToken]:
|
||||
def list_tokens(self, user_id: str = None, project_id: str = None) -> list[ChromeExtensionToken]:
|
||||
"""列出令牌"""
|
||||
conn = self.pm.db.get_conn()
|
||||
|
||||
@@ -544,7 +545,7 @@ class ChromeExtensionHandler:
|
||||
|
||||
async def import_webpage(
|
||||
self, token: ChromeExtensionToken, url: str, title: str, content: str, html_content: str = None
|
||||
) -> Dict:
|
||||
) -> dict:
|
||||
"""导入网页内容"""
|
||||
if not token.project_id:
|
||||
return {"success": False, "error": "Token not associated with any project"}
|
||||
@@ -617,7 +618,7 @@ class BotHandler:
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
def get_session(self, session_id: str) -> Optional[BotSession]:
|
||||
def get_session(self, session_id: str) -> BotSession | None:
|
||||
"""获取会话"""
|
||||
conn = self.pm.db.get_conn()
|
||||
row = conn.execute(
|
||||
@@ -631,7 +632,7 @@ class BotHandler:
|
||||
return self._row_to_session(row)
|
||||
return None
|
||||
|
||||
def list_sessions(self, project_id: str = None) -> List[BotSession]:
|
||||
def list_sessions(self, project_id: str = None) -> list[BotSession]:
|
||||
"""列出会话"""
|
||||
conn = self.pm.db.get_conn()
|
||||
|
||||
@@ -652,7 +653,7 @@ class BotHandler:
|
||||
|
||||
return [self._row_to_session(row) for row in rows]
|
||||
|
||||
def update_session(self, session_id: str, **kwargs) -> Optional[BotSession]:
|
||||
def update_session(self, session_id: str, **kwargs) -> BotSession | None:
|
||||
"""更新会话"""
|
||||
conn = self.pm.db.get_conn()
|
||||
|
||||
@@ -709,7 +710,7 @@ class BotHandler:
|
||||
message_count=row["message_count"],
|
||||
)
|
||||
|
||||
async def handle_message(self, session: BotSession, message: Dict) -> Dict:
|
||||
async def handle_message(self, session: BotSession, message: dict) -> dict:
|
||||
"""处理收到的消息"""
|
||||
now = datetime.now().isoformat()
|
||||
|
||||
@@ -740,7 +741,7 @@ class BotHandler:
|
||||
|
||||
return {"success": False, "error": "Unsupported message type"}
|
||||
|
||||
async def _handle_text_message(self, session: BotSession, text: str, raw_message: Dict) -> Dict:
|
||||
async def _handle_text_message(self, session: BotSession, text: str, raw_message: dict) -> dict:
|
||||
"""处理文本消息"""
|
||||
# 简单命令处理
|
||||
if text.startswith("/help"):
|
||||
@@ -772,7 +773,7 @@ class BotHandler:
|
||||
# 默认回复
|
||||
return {"success": True, "response": f"收到消息:{text[:100]}...\n\n使用 /help 查看可用命令"}
|
||||
|
||||
async def _handle_audio_message(self, session: BotSession, message: Dict) -> Dict:
|
||||
async def _handle_audio_message(self, session: BotSession, message: dict) -> dict:
|
||||
"""处理音频消息"""
|
||||
if not session.project_id:
|
||||
return {"success": False, "error": "Session not bound to any project"}
|
||||
@@ -802,7 +803,7 @@ class BotHandler:
|
||||
except Exception as e:
|
||||
return {"success": False, "error": f"Failed to process audio: {str(e)}"}
|
||||
|
||||
async def _handle_file_message(self, session: BotSession, message: Dict) -> Dict:
|
||||
async def _handle_file_message(self, session: BotSession, message: dict) -> dict:
|
||||
"""处理文件消息"""
|
||||
return {"success": True, "response": "📎 收到文件,正在处理中..."}
|
||||
|
||||
@@ -825,8 +826,8 @@ class BotHandler:
|
||||
|
||||
async def _send_feishu_message(self, session: BotSession, message: str, msg_type: str) -> bool:
|
||||
"""发送飞书消息"""
|
||||
import hashlib
|
||||
import base64
|
||||
import hashlib
|
||||
|
||||
timestamp = str(int(time.time()))
|
||||
|
||||
@@ -850,8 +851,8 @@ class BotHandler:
|
||||
|
||||
async def _send_dingtalk_message(self, session: BotSession, message: str, msg_type: str) -> bool:
|
||||
"""发送钉钉消息"""
|
||||
import hashlib
|
||||
import base64
|
||||
import hashlib
|
||||
|
||||
timestamp = str(round(time.time() * 1000))
|
||||
|
||||
@@ -890,8 +891,8 @@ class WebhookIntegration:
|
||||
endpoint_url: str,
|
||||
project_id: str = None,
|
||||
auth_type: str = "none",
|
||||
auth_config: Dict = None,
|
||||
trigger_events: List[str] = None,
|
||||
auth_config: dict = None,
|
||||
trigger_events: list[str] = None,
|
||||
) -> WebhookEndpoint:
|
||||
"""创建 Webhook 端点"""
|
||||
endpoint_id = str(uuid.uuid4())[:8]
|
||||
@@ -935,7 +936,7 @@ class WebhookIntegration:
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
def get_endpoint(self, endpoint_id: str) -> Optional[WebhookEndpoint]:
|
||||
def get_endpoint(self, endpoint_id: str) -> WebhookEndpoint | None:
|
||||
"""获取端点"""
|
||||
conn = self.pm.db.get_conn()
|
||||
row = conn.execute(
|
||||
@@ -947,7 +948,7 @@ class WebhookIntegration:
|
||||
return self._row_to_endpoint(row)
|
||||
return None
|
||||
|
||||
def list_endpoints(self, project_id: str = None) -> List[WebhookEndpoint]:
|
||||
def list_endpoints(self, project_id: str = None) -> list[WebhookEndpoint]:
|
||||
"""列出端点"""
|
||||
conn = self.pm.db.get_conn()
|
||||
|
||||
@@ -968,7 +969,7 @@ class WebhookIntegration:
|
||||
|
||||
return [self._row_to_endpoint(row) for row in rows]
|
||||
|
||||
def update_endpoint(self, endpoint_id: str, **kwargs) -> Optional[WebhookEndpoint]:
|
||||
def update_endpoint(self, endpoint_id: str, **kwargs) -> WebhookEndpoint | None:
|
||||
"""更新端点"""
|
||||
conn = self.pm.db.get_conn()
|
||||
|
||||
@@ -1034,7 +1035,7 @@ class WebhookIntegration:
|
||||
trigger_count=row["trigger_count"],
|
||||
)
|
||||
|
||||
async def trigger(self, endpoint: WebhookEndpoint, event_type: str, data: Dict) -> bool:
|
||||
async def trigger(self, endpoint: WebhookEndpoint, event_type: str, data: dict) -> bool:
|
||||
"""触发 Webhook"""
|
||||
if not endpoint.is_active:
|
||||
return False
|
||||
@@ -1079,7 +1080,7 @@ class WebhookIntegration:
|
||||
print(f"Failed to trigger webhook: {e}")
|
||||
return False
|
||||
|
||||
async def test_endpoint(self, endpoint: WebhookEndpoint) -> Dict:
|
||||
async def test_endpoint(self, endpoint: WebhookEndpoint) -> dict:
|
||||
"""测试端点"""
|
||||
test_data = {
|
||||
"message": "This is a test event from InsightFlow",
|
||||
@@ -1160,7 +1161,7 @@ class WebDAVSyncManager:
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
def get_sync(self, sync_id: str) -> Optional[WebDAVSync]:
|
||||
def get_sync(self, sync_id: str) -> WebDAVSync | None:
|
||||
"""获取同步配置"""
|
||||
conn = self.pm.db.get_conn()
|
||||
row = conn.execute("SELECT * FROM webdav_syncs WHERE id = ?", (sync_id,)).fetchone()
|
||||
@@ -1170,7 +1171,7 @@ class WebDAVSyncManager:
|
||||
return self._row_to_sync(row)
|
||||
return None
|
||||
|
||||
def list_syncs(self, project_id: str = None) -> List[WebDAVSync]:
|
||||
def list_syncs(self, project_id: str = None) -> list[WebDAVSync]:
|
||||
"""列出同步配置"""
|
||||
conn = self.pm.db.get_conn()
|
||||
|
||||
@@ -1185,7 +1186,7 @@ class WebDAVSyncManager:
|
||||
|
||||
return [self._row_to_sync(row) for row in rows]
|
||||
|
||||
def update_sync(self, sync_id: str, **kwargs) -> Optional[WebDAVSync]:
|
||||
def update_sync(self, sync_id: str, **kwargs) -> WebDAVSync | None:
|
||||
"""更新同步配置"""
|
||||
conn = self.pm.db.get_conn()
|
||||
|
||||
@@ -1252,7 +1253,7 @@ class WebDAVSyncManager:
|
||||
sync_count=row["sync_count"],
|
||||
)
|
||||
|
||||
async def test_connection(self, sync: WebDAVSync) -> Dict:
|
||||
async def test_connection(self, sync: WebDAVSync) -> dict:
|
||||
"""测试 WebDAV 连接"""
|
||||
if not WEBDAV_AVAILABLE:
|
||||
return {"success": False, "error": "WebDAV library not available"}
|
||||
@@ -1268,7 +1269,7 @@ class WebDAVSyncManager:
|
||||
except Exception as e:
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
async def sync_project(self, sync: WebDAVSync) -> Dict:
|
||||
async def sync_project(self, sync: WebDAVSync) -> dict:
|
||||
"""同步项目到 WebDAV"""
|
||||
if not WEBDAV_AVAILABLE:
|
||||
return {"success": False, "error": "WebDAV library not available"}
|
||||
|
||||
Reference in New Issue
Block a user