Files
aiagent/backend/app/models/knowledge_entry.py
renjianbo ab1589921a fix: 修复35个安全与功能缺陷,补全知识进化/数字孪生/行为采集模块
## 安全修复 (12项)
- Webhook接口添加全局Token认证,过滤敏感请求头
- 修复JWT Base64 padding公式,防止签名验证绕过
- 数据库密码/飞书Token从源码移除,改为环境变量
- 工作流引擎添加路径遍历防护 (_resolve_safe_path)
- eval()添加模板长度上限检查
- 审批API添加认证依赖
- 前端v-html增强XSS转义,console.log仅开发模式输出
- 500错误不再暴露内部异常详情

## Agent运行时修复 (7项)
- 删除_inject_knowledge_context中未定义db变量的finally块
- 工具执行添加try/except保护,异常不崩溃Agent
- LLM重试计入budget计数器
- self_review异常时passed=False
- max_iterations截断标记success=False
- 工具参数JSON解析失败时记录警告日志
- run()开始时重置_llm_invocations计数器

## 配置与基础设施
- DEBUG默认False,SQL_ECHO独立配置项
- init_db()补全13个缺失模型导入
- 新增WEBHOOK_AUTH_TOKEN/SQL_ECHO配置项
- 新增.env.example模板文件

## 前端修复 (12项)
- 登录改用URLSearchParams替代FormData
- 401拦截器通过Pinia store统一清理状态
- SSE流超时从60s延长至300s
- final/error事件时清除streamTimeout
- localStorage聊天记录添加24h TTL
- safeParseArgCount替代模板中裸JSON.parse
- fetchUser 401时同时清除user对象

## 新增模块
- 知识进化: knowledge_extractor/retriever/tasks
- 数字孪生: shadow_executor/comparison模型
- 行为采集: behavior_middleware/collector/fingerprint_engine
- 代码审查: code_review_agent/document_review_agent
- 反馈学习: feedback_learner
- 瓶颈检测/优化引擎/成本估算/需求估算
- 速率限制器 (rate_limiter)
- Alembic迁移 015-020

## 文档
- 商业化落地计划
- 8篇docs文档 (架构/API/部署/开发/贡献等)
- Docker Compose生产配置

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-10 19:50:20 +08:00

68 lines
2.9 KiB
Python

"""
知识条目模型 — Agent 执行经验的结构化沉淀
"""
import uuid
from datetime import datetime
from sqlalchemy import Column, String, Text, Integer, DateTime, Boolean, JSON, Float, Index
from app.core.database import Base
class KnowledgeEntry(Base):
"""从 Agent 执行日志中提取的可复用知识条目"""
__tablename__ = "knowledge_entries"
id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
title = Column(String(500), nullable=False, comment="知识标题(一句话概括)")
category = Column(String(30), nullable=False, index=True,
comment="类别: bug_fix/best_practice/workaround/optimization/insight")
tags = Column(JSON, nullable=True, comment="标签列表: ['mysql','deadlock','retry']")
# 知识内容
situation = Column(Text, nullable=True, comment="适用场景")
solution = Column(Text, nullable=True, comment="解决方案")
caveats = Column(Text, nullable=True, comment="注意事项/踩坑记录")
# 来源追溯
source_execution_ids = Column(JSON, nullable=True, comment="原始执行日志ID列表")
source_agent_name = Column(String(200), nullable=True, comment="来源 Agent 名称")
source_model = Column(String(100), nullable=True, comment="来源模型")
# RAG 检索
embedding_text = Column(Text, nullable=True, comment="用于生成 embedding 的合并文本")
embedding = Column(Text, nullable=True, comment="JSON 序列化的 embedding 向量")
# 效果度量
retrieval_count = Column(Integer, default=0, comment="被检索次数")
success_rate = Column(Float, nullable=True, comment="应用成功率")
# 提取信息
extracted_by = Column(String(100), nullable=True, comment="提取方式: llm_auto/manual/reviewed")
confidence = Column(Float, default=0.5, comment="提取置信度(0-1)")
is_active = Column(Boolean, default=True, comment="是否启用")
created_at = Column(DateTime, default=datetime.now, comment="创建时间")
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment="更新时间")
__table_args__ = (
Index("ix_knowledge_entries_category", "category"),
Index("ix_knowledge_entries_active", "is_active"),
)
def __repr__(self):
return f"<KnowledgeEntry(id={self.id}, title={self.title}, category={self.category})>"
def to_dict(self) -> dict:
return {
"id": self.id,
"title": self.title,
"category": self.category,
"tags": self.tags or [],
"situation": self.situation,
"solution": self.solution,
"caveats": self.caveats,
"source_agent_name": self.source_agent_name,
"retrieval_count": self.retrieval_count,
"confidence": self.confidence,
"created_at": self.created_at.isoformat() if self.created_at else None,
}