From d349b09afd1fc59d4c35dd7d0b9a060d00b43648 Mon Sep 17 00:00:00 2001 From: ogt Date: Mon, 20 Apr 2026 20:23:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A3=9C=E5=BB=BA=20AIInsight=20ORM=20?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=EF=BC=88ai=5Finsights=20=E8=A1=A8=E7=BC=BA?= =?UTF-8?q?=E5=B0=91=20class=20=E5=AE=9A=E7=BE=A9=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ai_insights 表在 DB 存在且有 39 筆資料,但 database/ai_models.py 從未定義 AIInsight class,導致 quality_rescore_task、openclaw_learning_service 以及所有 AI KM 讀寫全部 ImportError 崩潰。 同步補入 __all__ 匯出,修復 embedding_retry_queue 2 筆卡住。 Co-Authored-By: Claude Sonnet 4.6 --- database/ai_models.py | 45 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/database/ai_models.py b/database/ai_models.py index 68e5a98..36364b1 100644 --- a/database/ai_models.py +++ b/database/ai_models.py @@ -119,7 +119,50 @@ class AIUsageTracking(Base): 'created_at': self.created_at.isoformat() if self.created_at else None } +class AIInsight(Base): + """ + AI 洞察知識庫(ai_insights 表)— KM 沉澱核心,支援 RAG 向量化。 + 對應 openclaw_learning_service / scheduler 各任務寫入。 + """ + __tablename__ = 'ai_insights' + + id = Column(Integer, primary_key=True, autoincrement=True) + insight_type = Column(String(50), nullable=False) # backup_status / human_review / auto_heal_playbook 等 + period = Column(String(50)) # 分析週期,e.g. "2026-04-20" + product_sku = Column(String(50)) + content = Column(Text, nullable=False) + metadata_json = Column(Text) # JSON extra payload + avg_quality = Column(Float, default=0.0) + status = Column(String(20), default='active') # active / archived + decay_exempt = Column(Boolean, default=False) + ai_model = Column(String(50)) + feedback_up = Column(Integer, default=0) + feedback_down = Column(Integer, default=0) + confidence = Column(Float, default=1.0) + created_by = Column(String(50)) + created_at = Column(DateTime, default=datetime.now) + updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now) + # embedding 欄位為 pgvector 型別,透過 raw SQL 寫入,此處不聲明以避免型別衝突 + + def to_dict(self): + return { + 'id': self.id, + 'insight_type': self.insight_type, + 'period': self.period, + 'product_sku': self.product_sku, + 'content': self.content, + 'avg_quality': self.avg_quality, + 'status': self.status, + 'ai_model': self.ai_model, + 'feedback_up': self.feedback_up, + 'feedback_down': self.feedback_down, + 'confidence': self.confidence, + 'created_by': self.created_by, + 'created_at': self.created_at.isoformat() if self.created_at else None, + } + + __all__ = [ "AgentContext", "ActionPlan", "ActionOutcome", "AgentStrategyWeights", - "AIGenerationHistory", "AIPromptTemplate", "AIUsageTracking" + "AIGenerationHistory", "AIPromptTemplate", "AIUsageTracking", "AIInsight", ]