feat:客户agent以及记忆模块优化
This commit is contained in:
@@ -28,15 +28,14 @@ class MemoryUnitRepo(BaseRepository):
|
||||
return obj
|
||||
|
||||
async def find_exact(
|
||||
self, customer_id: int, memory_type: str, tag: str, content: str
|
||||
self, customer_id: int, memory_type: str, tag: str
|
||||
) -> MemoryUnit | None:
|
||||
"""按客户、类型、标签和内容精确查找去重对象。"""
|
||||
"""按客户、类型和标签查找当前有效记忆。"""
|
||||
return await self.db.scalar(
|
||||
select(MemoryUnit).where(
|
||||
MemoryUnit.customer_id == customer_id,
|
||||
MemoryUnit.memory_type == memory_type,
|
||||
MemoryUnit.tag == tag,
|
||||
MemoryUnit.content == content,
|
||||
MemoryUnit.status.not_in(INACTIVE_STATUSES),
|
||||
)
|
||||
)
|
||||
@@ -70,16 +69,52 @@ class MemoryUnitRepo(BaseRepository):
|
||||
return list((await self.db.scalars(statement)).all())
|
||||
|
||||
async def merge_evidence(
|
||||
self, memory: MemoryUnit, *, evidence_count: int = 1, conflict_count: int = 0
|
||||
self,
|
||||
memory: MemoryUnit,
|
||||
*,
|
||||
content: str | None = None,
|
||||
source: str | None = None,
|
||||
evidence_count: int = 1,
|
||||
evidence_ref: list[dict] | None = None,
|
||||
) -> MemoryUnit:
|
||||
"""合并证据和冲突计数,不改变客户隔离范围。"""
|
||||
"""更新最新内容并合并证据,不改变客户隔离范围。"""
|
||||
if content:
|
||||
memory.content = content
|
||||
if source:
|
||||
memory.source = source
|
||||
memory.evidence_count = (memory.evidence_count or 0) + evidence_count
|
||||
memory.conflict_count = (memory.conflict_count or 0) + conflict_count
|
||||
if evidence_ref:
|
||||
memory.evidence_ref = [*(memory.evidence_ref or []), *evidence_ref]
|
||||
memory.last_verified_at = datetime.now()
|
||||
memory.update_time = datetime.now()
|
||||
await self.db.commit()
|
||||
await self.db.refresh(memory)
|
||||
return memory
|
||||
|
||||
async def list_for_confidence_refresh(
|
||||
self, customer_id: int, limit: int = 500
|
||||
) -> list[MemoryUnit]:
|
||||
"""返回需要重新计算时间衰减置信度的有效记忆。"""
|
||||
statement = (
|
||||
select(MemoryUnit)
|
||||
.where(
|
||||
MemoryUnit.customer_id == customer_id,
|
||||
MemoryUnit.status.in_(ACTIVE_STATUSES),
|
||||
)
|
||||
.order_by(MemoryUnit.id)
|
||||
.limit(limit)
|
||||
)
|
||||
return list((await self.db.scalars(statement)).all())
|
||||
|
||||
async def update_confidence(self, memory_id: int, **values) -> None:
|
||||
"""只更新置信度字段,不改变记忆内容和证据。"""
|
||||
memory = await self.db.get(MemoryUnit, memory_id)
|
||||
if memory is None:
|
||||
return
|
||||
for key, value in values.items():
|
||||
setattr(memory, key, value)
|
||||
await self.db.commit()
|
||||
|
||||
async def update_sync_status(self, memory_id: int, **values) -> None:
|
||||
"""更新向量/图谱索引 ID 和同步状态。"""
|
||||
memory = await self.db.get(MemoryUnit, memory_id)
|
||||
|
||||
Reference in New Issue
Block a user