fix(knowledge): list_collections 增加异常保护,防止 ChromaDB 数据丢失导致 /collections 500

遍历 kb_metadata.json 时对每个集合的 get_collection/count 操作包裹
try/except,捕获 NotFoundError/InternalError 等异常后跳过并记录警告,
循环结束后自动清理失效的元数据条目。
This commit is contained in:
lacerate551
2026-06-09 11:43:34 +08:00
parent 6deba8fae2
commit 84a8be0ce8

View File

@@ -382,7 +382,10 @@ 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():
try:
collection = self.get_collection(name) collection = self.get_collection(name)
result.append(CollectionInfo( result.append(CollectionInfo(
name=name, name=name,
@@ -392,6 +395,18 @@ class CollectionMixin:
department=info.get("department", ""), department=info.get("department", ""),
description=info.get("description", "") 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