fix(knowledge): list_collections 增加异常保护,防止 ChromaDB 数据丢失导致 /collections 500
遍历 kb_metadata.json 时对每个集合的 get_collection/count 操作包裹 try/except,捕获 NotFoundError/InternalError 等异常后跳过并记录警告, 循环结束后自动清理失效的元数据条目。
This commit is contained in:
@@ -382,16 +382,31 @@ class CollectionMixin:
|
|||||||
|
|
||||||
self._save_metadata()
|
self._save_metadata()
|
||||||
|
|
||||||
|
stale_collections = []
|
||||||
|
|
||||||
for name, info in self._metadata.get("collections", {}).items():
|
for name, info in self._metadata.get("collections", {}).items():
|
||||||
collection = self.get_collection(name)
|
try:
|
||||||
result.append(CollectionInfo(
|
collection = self.get_collection(name)
|
||||||
name=name,
|
result.append(CollectionInfo(
|
||||||
display_name=info.get("display_name", name),
|
name=name,
|
||||||
document_count=collection.count() if collection else 0,
|
display_name=info.get("display_name", name),
|
||||||
created_at=info.get("created_at", ""),
|
document_count=collection.count() if collection else 0,
|
||||||
department=info.get("department", ""),
|
created_at=info.get("created_at", ""),
|
||||||
description=info.get("description", "")
|
department=info.get("department", ""),
|
||||||
))
|
description=info.get("description", "")
|
||||||
|
))
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(
|
||||||
|
f"跳过异常向量库 '{name}': {e},可能是 ChromaDB 数据目录已丢失"
|
||||||
|
)
|
||||||
|
stale_collections.append(name)
|
||||||
|
|
||||||
|
# 清理元数据中指向已失效集合的条目
|
||||||
|
if stale_collections:
|
||||||
|
for name in stale_collections:
|
||||||
|
self._metadata.get("collections", {}).pop(name, None)
|
||||||
|
logger.info(f"清理失效向量库元数据: {name}")
|
||||||
|
self._save_metadata()
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user