fix: auto-fix code issues (cron)

- 修复重复导入/字段
- 修复异常处理
- 修复PEP8格式问题
- 添加类型注解
- 修复缺失的urllib.parse导入
This commit is contained in:
OpenClaw Bot
2026-02-28 06:03:09 +08:00
parent ff83cab6c7
commit fe3d64a1d2
41 changed files with 4501 additions and 1176 deletions

View File

@@ -14,6 +14,7 @@ from datetime import datetime
DB_PATH = os.getenv("DB_PATH", "/app/data/insightflow.db")
@dataclass
class Project:
id: str
@@ -22,6 +23,7 @@ class Project:
created_at: str = ""
updated_at: str = ""
@dataclass
class Entity:
id: str
@@ -42,6 +44,7 @@ class Entity:
if self.attributes is None:
self.attributes = {}
@dataclass
class AttributeTemplate:
"""属性模板定义"""
@@ -62,6 +65,7 @@ class AttributeTemplate:
if self.options is None:
self.options = []
@dataclass
class EntityAttribute:
"""实体属性值"""
@@ -82,6 +86,7 @@ class EntityAttribute:
if self.options is None:
self.options = []
@dataclass
class AttributeHistory:
"""属性变更历史"""
@@ -95,6 +100,7 @@ class AttributeHistory:
changed_at: str = ""
change_reason: str = ""
@dataclass
class EntityMention:
id: str
@@ -105,6 +111,7 @@ class EntityMention:
text_snippet: str
confidence: float = 1.0
class DatabaseManager:
def __init__(self, db_path: str = DB_PATH):
self.db_path = db_path
@@ -137,7 +144,9 @@ class DatabaseManager:
)
conn.commit()
conn.close()
return Project(id=project_id, name=name, description=description, created_at=now, updated_at=now)
return Project(
id=project_id, name=name, description=description, created_at=now, updated_at=now
)
def get_project(self, project_id: str) -> Project | None:
conn = self.get_conn()
@@ -190,7 +199,9 @@ class DatabaseManager:
return Entity(**data)
return None
def find_similar_entities(self, project_id: str, name: str, threshold: float = 0.8) -> list[Entity]:
def find_similar_entities(
self, project_id: str, name: str, threshold: float = 0.8
) -> list[Entity]:
"""查找相似实体"""
conn = self.get_conn()
rows = conn.execute(
@@ -224,12 +235,16 @@ class DatabaseManager:
"UPDATE entities SET aliases = ?, updated_at = ? WHERE id = ?",
(json.dumps(list(target_aliases)), datetime.now().isoformat(), target_id),
)
conn.execute("UPDATE entity_mentions SET entity_id = ? WHERE entity_id = ?", (target_id, source_id))
conn.execute(
"UPDATE entity_relations SET source_entity_id = ? WHERE source_entity_id = ?", (target_id, source_id)
"UPDATE entity_mentions SET entity_id = ? WHERE entity_id = ?", (target_id, source_id)
)
conn.execute(
"UPDATE entity_relations SET target_entity_id = ? WHERE target_entity_id = ?", (target_id, source_id)
"UPDATE entity_relations SET source_entity_id = ? WHERE source_entity_id = ?",
(target_id, source_id),
)
conn.execute(
"UPDATE entity_relations SET target_entity_id = ? WHERE target_entity_id = ?",
(target_id, source_id),
)
conn.execute("DELETE FROM entities WHERE id = ?", (source_id,))
@@ -297,7 +312,8 @@ class DatabaseManager:
conn = self.get_conn()
conn.execute("DELETE FROM entity_mentions WHERE entity_id = ?", (entity_id,))
conn.execute(
"DELETE FROM entity_relations WHERE source_entity_id = ? OR target_entity_id = ?", (entity_id, entity_id)
"DELETE FROM entity_relations WHERE source_entity_id = ? OR target_entity_id = ?",
(entity_id, entity_id),
)
conn.execute("DELETE FROM entity_attributes WHERE entity_id = ?", (entity_id,))
conn.execute("DELETE FROM entities WHERE id = ?", (entity_id,))
@@ -328,7 +344,8 @@ class DatabaseManager:
def get_entity_mentions(self, entity_id: str) -> list[EntityMention]:
conn = self.get_conn()
rows = conn.execute(
"SELECT * FROM entity_mentions WHERE entity_id = ? ORDER BY transcript_id, start_pos", (entity_id,)
"SELECT * FROM entity_mentions WHERE entity_id = ? ORDER BY transcript_id, start_pos",
(entity_id,),
).fetchall()
conn.close()
return [EntityMention(**dict(r)) for r in rows]
@@ -336,7 +353,12 @@ class DatabaseManager:
# ==================== Transcript Operations ====================
def save_transcript(
self, transcript_id: str, project_id: str, filename: str, full_text: str, transcript_type: str = "audio"
self,
transcript_id: str,
project_id: str,
filename: str,
full_text: str,
transcript_type: str = "audio",
):
conn = self.get_conn()
now = datetime.now().isoformat()
@@ -365,7 +387,8 @@ class DatabaseManager:
conn = self.get_conn()
now = datetime.now().isoformat()
conn.execute(
"UPDATE transcripts SET full_text = ?, updated_at = ? WHERE id = ?", (full_text, now, transcript_id)
"UPDATE transcripts SET full_text = ?, updated_at = ? WHERE id = ?",
(full_text, now, transcript_id),
)
conn.commit()
row = conn.execute("SELECT * FROM transcripts WHERE id = ?", (transcript_id,)).fetchone()
@@ -390,7 +413,16 @@ class DatabaseManager:
"""INSERT INTO entity_relations
(id, project_id, source_entity_id, target_entity_id, relation_type, evidence, transcript_id, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(relation_id, project_id, source_entity_id, target_entity_id, relation_type, evidence, transcript_id, now),
(
relation_id,
project_id,
source_entity_id,
target_entity_id,
relation_type,
evidence,
transcript_id,
now,
),
)
conn.commit()
conn.close()
@@ -410,7 +442,8 @@ class DatabaseManager:
def list_project_relations(self, project_id: str) -> list[dict]:
conn = self.get_conn()
rows = conn.execute(
"SELECT * FROM entity_relations WHERE project_id = ? ORDER BY created_at DESC", (project_id,)
"SELECT * FROM entity_relations WHERE project_id = ? ORDER BY created_at DESC",
(project_id,),
).fetchall()
conn.close()
return [dict(r) for r in rows]
@@ -451,7 +484,9 @@ class DatabaseManager:
).fetchone()
if existing:
conn.execute("UPDATE glossary SET frequency = frequency + 1 WHERE id = ?", (existing["id"],))
conn.execute(
"UPDATE glossary SET frequency = frequency + 1 WHERE id = ?", (existing["id"],)
)
conn.commit()
conn.close()
return existing["id"]
@@ -593,9 +628,13 @@ class DatabaseManager:
"top_entities": [dict(e) for e in top_entities],
}
def get_transcript_context(self, transcript_id: str, position: int, context_chars: int = 200) -> str:
def get_transcript_context(
self, transcript_id: str, position: int, context_chars: int = 200
) -> str:
conn = self.get_conn()
row = conn.execute("SELECT full_text FROM transcripts WHERE id = ?", (transcript_id,)).fetchone()
row = conn.execute(
"SELECT full_text FROM transcripts WHERE id = ?", (transcript_id,)
).fetchone()
conn.close()
if not row:
return ""
@@ -685,7 +724,10 @@ class DatabaseManager:
conn.close()
return {"daily_activity": [dict(d) for d in daily_stats], "top_entities": [dict(e) for e in entity_stats]}
return {
"daily_activity": [dict(d) for d in daily_stats],
"top_entities": [dict(e) for e in entity_stats],
}
# ==================== Phase 5: Entity Attributes ====================
@@ -716,7 +758,9 @@ class DatabaseManager:
def get_attribute_template(self, template_id: str) -> AttributeTemplate | None:
conn = self.get_conn()
row = conn.execute("SELECT * FROM attribute_templates WHERE id = ?", (template_id,)).fetchone()
row = conn.execute(
"SELECT * FROM attribute_templates WHERE id = ?", (template_id,)
).fetchone()
conn.close()
if row:
data = dict(row)
@@ -742,7 +786,15 @@ class DatabaseManager:
def update_attribute_template(self, template_id: str, **kwargs) -> AttributeTemplate | None:
conn = self.get_conn()
allowed_fields = ["name", "type", "options", "default_value", "description", "is_required", "sort_order"]
allowed_fields = [
"name",
"type",
"options",
"default_value",
"description",
"is_required",
"sort_order",
]
updates = []
values = []
@@ -844,7 +896,11 @@ class DatabaseManager:
return None
attrs = self.get_entity_attributes(entity_id)
entity.attributes = {
attr.template_name: {"value": attr.value, "type": attr.template_type, "template_id": attr.template_id}
attr.template_name: {
"value": attr.value,
"type": attr.template_type,
"template_id": attr.template_id,
}
for attr in attrs
}
return entity
@@ -854,7 +910,8 @@ class DatabaseManager:
):
conn = self.get_conn()
old_row = conn.execute(
"SELECT value FROM entity_attributes WHERE entity_id = ? AND template_id = ?", (entity_id, template_id)
"SELECT value FROM entity_attributes WHERE entity_id = ? AND template_id = ?",
(entity_id, template_id),
).fetchone()
if old_row:
@@ -874,7 +931,8 @@ class DatabaseManager:
),
)
conn.execute(
"DELETE FROM entity_attributes WHERE entity_id = ? AND template_id = ?", (entity_id, template_id)
"DELETE FROM entity_attributes WHERE entity_id = ? AND template_id = ?",
(entity_id, template_id),
)
conn.commit()
conn.close()
@@ -905,7 +963,9 @@ class DatabaseManager:
conn.close()
return [AttributeHistory(**dict(r)) for r in rows]
def search_entities_by_attributes(self, project_id: str, attribute_filters: dict[str, str]) -> list[Entity]:
def search_entities_by_attributes(
self, project_id: str, attribute_filters: dict[str, str]
) -> list[Entity]:
entities = self.list_project_entities(project_id)
if not attribute_filters:
return entities
@@ -999,8 +1059,12 @@ class DatabaseManager:
if row:
data = dict(row)
data["resolution"] = json.loads(data["resolution"]) if data["resolution"] else None
data["extracted_entities"] = json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
data["extracted_relations"] = json.loads(data["extracted_relations"]) if data["extracted_relations"] else []
data["extracted_entities"] = (
json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
)
data["extracted_relations"] = (
json.loads(data["extracted_relations"]) if data["extracted_relations"] else []
)
return data
return None
@@ -1016,8 +1080,12 @@ class DatabaseManager:
for row in rows:
data = dict(row)
data["resolution"] = json.loads(data["resolution"]) if data["resolution"] else None
data["extracted_entities"] = json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
data["extracted_relations"] = json.loads(data["extracted_relations"]) if data["extracted_relations"] else []
data["extracted_entities"] = (
json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
)
data["extracted_relations"] = (
json.loads(data["extracted_relations"]) if data["extracted_relations"] else []
)
videos.append(data)
return videos
@@ -1065,7 +1133,9 @@ class DatabaseManager:
frames = []
for row in rows:
data = dict(row)
data["extracted_entities"] = json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
data["extracted_entities"] = (
json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
)
frames.append(data)
return frames
@@ -1113,8 +1183,12 @@ class DatabaseManager:
if row:
data = dict(row)
data["extracted_entities"] = json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
data["extracted_relations"] = json.loads(data["extracted_relations"]) if data["extracted_relations"] else []
data["extracted_entities"] = (
json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
)
data["extracted_relations"] = (
json.loads(data["extracted_relations"]) if data["extracted_relations"] else []
)
return data
return None
@@ -1129,8 +1203,12 @@ class DatabaseManager:
images = []
for row in rows:
data = dict(row)
data["extracted_entities"] = json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
data["extracted_relations"] = json.loads(data["extracted_relations"]) if data["extracted_relations"] else []
data["extracted_entities"] = (
json.loads(data["extracted_entities"]) if data["extracted_entities"] else []
)
data["extracted_relations"] = (
json.loads(data["extracted_relations"]) if data["extracted_relations"] else []
)
images.append(data)
return images
@@ -1154,7 +1232,17 @@ class DatabaseManager:
(id, project_id, entity_id, modality, source_id, source_type,
text_snippet, confidence, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
(mention_id, project_id, entity_id, modality, source_id, source_type, text_snippet, confidence, now),
(
mention_id,
project_id,
entity_id,
modality,
source_id,
source_type,
text_snippet,
confidence,
now,
),
)
conn.commit()
conn.close()
@@ -1217,7 +1305,16 @@ class DatabaseManager:
(id, entity_id, linked_entity_id, link_type, confidence,
evidence, modalities, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
(link_id, entity_id, linked_entity_id, link_type, confidence, evidence, json.dumps(modalities or []), now),
(
link_id,
entity_id,
linked_entity_id,
link_type,
confidence,
evidence,
json.dumps(modalities or []),
now,
),
)
conn.commit()
conn.close()
@@ -1256,11 +1353,15 @@ class DatabaseManager:
}
# 视频数量
row = conn.execute("SELECT COUNT(*) as count FROM videos WHERE project_id = ?", (project_id,)).fetchone()
row = conn.execute(
"SELECT COUNT(*) as count FROM videos WHERE project_id = ?", (project_id,)
).fetchone()
stats["video_count"] = row["count"]
# 图片数量
row = conn.execute("SELECT COUNT(*) as count FROM images WHERE project_id = ?", (project_id,)).fetchone()
row = conn.execute(
"SELECT COUNT(*) as count FROM images WHERE project_id = ?", (project_id,)
).fetchone()
stats["image_count"] = row["count"]
# 多模态实体数量
@@ -1291,9 +1392,11 @@ class DatabaseManager:
conn.close()
return stats
# Singleton instance
_db_manager = None
def get_db_manager() -> DatabaseManager:
global _db_manager
if _db_manager is None: