Fix/content attribute (#105)

* [fix]Fix the return of the "content" attribute

* [changes]Improve the code based on AI review

* Apply suggestion from @sourcery-ai[bot]

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

* [fix]Fix the return of the "content" attribute

* [changes]Improve the code based on AI review

* Apply suggestion from @sourcery-ai[bot]

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

* [changes]Improve the code based on AI review

---------

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
This commit is contained in:
乐力齐
2026-01-14 11:39:47 +08:00
committed by GitHub
parent a51eb7c7a0
commit 9eb3e1329f
2 changed files with 83 additions and 28 deletions

View File

@@ -620,34 +620,52 @@ class AccessHistoryManager:
new_version = current_version + 1
# 步骤2使用乐观锁更新节点
# 只有当版本号匹配时才更新
update_query = f"""
MATCH (n:{node_label} {{id: $node_id}})
"""
# 根据节点类型构建完整的查询语句
content_field_map = {
'Statement': 'n.statement as statement',
'MemorySummary': 'n.content as content',
'ExtractedEntity': 'null as content_placeholder' # 占位符,后续会被过滤
}
# 显式检查节点类型,不支持的类型抛出错误
if node_label not in content_field_map:
raise ValueError(
f"Unsupported node_label: {node_label}. "
f"Supported labels are: {list(content_field_map.keys())}"
)
content_field = content_field_map[node_label]
# 构建 WHERE 子句
where_conditions = []
if group_id:
update_query += " WHERE n.group_id = $group_id"
where_conditions.append("n.group_id = $group_id")
# 添加版本检查
if current_version > 0:
update_query += " AND n.version = $current_version"
where_conditions.append("n.version = $current_version")
else:
# 如果节点没有版本号,检查是否为首次更新
update_query += " AND (n.version IS NULL OR n.version = 0)"
where_conditions.append("(n.version IS NULL OR n.version = 0)")
update_query += """
where_clause = " AND ".join(where_conditions) if where_conditions else "true"
# 构建完整的更新查询
update_query = f"""
MATCH (n:{node_label} {{id: $node_id}})
WHERE {where_clause}
SET n.activation_value = $activation_value,
n.access_history = $access_history,
n.last_access_time = $last_access_time,
n.access_count = $access_count,
n.version = $new_version
RETURN n.id as id,
n.statement as statement,
n.activation_value as activation_value,
n.access_history as access_history,
n.last_access_time as last_access_time,
n.access_count as access_count,
n.importance_score as importance_score,
n.version as version
n.version as version,
{content_field}
"""
update_params = {
@@ -671,7 +689,11 @@ class AccessHistoryManager:
f"Expected version {current_version}, but node was modified by another transaction."
)
return dict(updated_node)
# 转换为字典并移除占位符字段
result_dict = dict(updated_node)
result_dict.pop('content_placeholder', None)
return result_dict
# 执行事务
try: