本次变更是对版本控制功能的大范围重构,核心目标:
- 默认使用浅备份(只追踪 write_file/edit_file 实际修改的文件),避免大工作区首次初始化耗时十几秒;
- 保留可选的完全备份(git write-tree),不污染 git log;
- 回溯时统一选择范围(仅对话 / 对话+工作区)和模式(覆盖 / 复制);
- 修复复制对话、侧边栏同步、新建对话延迟、diff 统计与渲染等连锁问题。
后端改动:
- modules/shallow_versioning.py(新增)
- 实现 ShallowVersioningManager:track_edit、make_snapshot、rewind、list_snapshots。
- 基于 difflib.SequenceMatcher 计算真实 insertions/deletions,replace 场景正确显示 +N/-M。
- diff 详情使用结构化 add/remove/context 行,不再解析 unified_diff 文本。
- modules/versioning_manager.py
- 完全备份从 git commit 改为 git write-tree,字段 commit/parent_commit 改为 tree_hash/parent_tree_hash。
- 恢复改用 read-tree + checkout-index;create_checkpoint 支持 diff_summary 参数。
- get_checkpoint_detail 保留 API 字段名 last_commit/restored_commit 以兼容前端。
- core/main_terminal_parts/tools_execution.py
- write_file/edit_file 在执行前调用浅备份 track_edit,保留编辑前状态。
- server/chat_flow_task_main.py
- 消息轮次结束时根据 backup_mode 选择完全备份 create_checkpoint 或浅备份 make_snapshot。
- 浅备份模式下同时写入 ConversationVersioningManager 检查点行,让前端能统一展示。
- server/conversation.py
- restore/detail/list API 支持 shallow 模式;list/detail 对浅备份实时重新计算统计。
- 创建对话时读取 personalization 的 versioning_backup_mode,shallow 模式固定 tracking_mode 为 conversation_only。
- core/web_terminal.py
- _ensure_conversation_versioning_enabled 读取默认备份模式并写入 versioning meta。
- modules/file_manager/crud_mixin.py / replace_mixin.py
- write_file / edit_file 返回 original_file / new_file / replacement_details(真实旧行/新行)。
- modules/personalization_manager.py
- 新增 versioning_enabled_by_default、versioning_backup_mode(shallow/full)。
- utils/perf_log.py(新增)
- 创建新对话全链路性能日志,用于定位延迟瓶颈。
- utils/conversation_manager/{index,list_search,crud}_mixin.py
- 加性能日志;index_mixin 主动检查新增对话文件,修复侧边栏不同步。
前端改动:
- static/src/components/overlay/VersioningDialog.vue
- 去掉顶部管理范围下拉;底部新增回溯范围与回溯模式选择。
- diff 详情改为 div.diff-line 网格布局(marker + content),支持横向滚动,状态显示缩写 A/M/D。
- 修复 pre 标签空白黑行、容器被挤窄导致折行等问题。
- static/src/app/methods/versioning.ts
- 移除 mismatch 弹窗逻辑;支持 copy 模式;selectVersioningCheckpoint 增强错误捕获。
- static/src/components/personalization/PersonalizationDrawer.vue
- 「工作区与权限」新增版本控制默认开关与备份方式选择。
- static/src/stores/personalization.ts
- 新增 versioning_enabled_by_default、versioning_backup_mode 字段。
- static/src/components/chat/actions/ToolAction.vue / toolRenderers.ts
- edit_file / write_file 工具展示基于 result.details / result.new_file 的真实替换结果。
- static/src/app/state.ts / watchers.ts / App.vue
- 新增 versioningInitializingBackupToastId,完全备份初始化时显示 toast。
- static/src/app/methods/conversation/action.ts / message/send.ts
- host + 完全备份 + 默认开启时,创建新对话/从 /new 发送首条消息显示「正在初始化备份」toast。
验证:
- /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/bin/python3.9 -m unittest test.test_server_refactor_smoke 通过。
- npm run build 通过。
- 宿主机与 docker 模式新建对话、编辑文件、回溯、复制对话均验证通过。
349 lines
13 KiB
Python
349 lines
13 KiB
Python
# modules/file_manager.py - 文件管理模块(添加行编辑功能)
|
||
|
||
import os
|
||
import shutil
|
||
from pathlib import Path
|
||
import re
|
||
from bisect import bisect_right
|
||
from typing import Any, Optional, Dict, List, Set, Tuple, TYPE_CHECKING
|
||
from datetime import datetime
|
||
try:
|
||
from config import (
|
||
MAX_FILE_SIZE,
|
||
FORBIDDEN_PATHS,
|
||
FORBIDDEN_ROOT_PATHS,
|
||
OUTPUT_FORMATS,
|
||
READ_TOOL_MAX_FILE_SIZE,
|
||
PROJECT_MAX_STORAGE_BYTES,
|
||
TERMINAL_SANDBOX_MODE,
|
||
LINUX_SAFETY,
|
||
)
|
||
except ImportError: # 兼容全局环境中存在同名包的情况
|
||
import sys
|
||
from pathlib import Path
|
||
project_root = Path(__file__).resolve().parents[1]
|
||
if str(project_root) not in sys.path:
|
||
sys.path.insert(0, str(project_root))
|
||
from config import (
|
||
MAX_FILE_SIZE,
|
||
FORBIDDEN_PATHS,
|
||
FORBIDDEN_ROOT_PATHS,
|
||
OUTPUT_FORMATS,
|
||
READ_TOOL_MAX_FILE_SIZE,
|
||
PROJECT_MAX_STORAGE_BYTES,
|
||
TERMINAL_SANDBOX_MODE,
|
||
LINUX_SAFETY,
|
||
)
|
||
from modules.container_file_proxy import ContainerFileProxy
|
||
from modules.host_sandbox_policy import get_macos_writable_paths, get_macos_readable_paths
|
||
from utils.logger import setup_logger
|
||
|
||
if TYPE_CHECKING:
|
||
from modules.user_container_manager import ContainerHandle
|
||
|
||
# 临时禁用长度检查
|
||
DISABLE_LENGTH_CHECK = True
|
||
|
||
logger = setup_logger(__name__)
|
||
|
||
class CrudMixin:
|
||
"""FileManager crud mixin 能力 mixin。"""
|
||
|
||
def create_file(self, path: str, content: str = "", file_type: str = "txt") -> Dict:
|
||
"""创建文件"""
|
||
valid, error, full_path = self._validate_path(path)
|
||
if not valid:
|
||
return {"success": False, "error": error}
|
||
|
||
# 添加文件扩展名
|
||
if not full_path.suffix:
|
||
full_path = full_path.with_suffix(f".{file_type}")
|
||
ok, msg = self._ensure_host_access(full_path, "write")
|
||
if not ok:
|
||
return {"success": False, "error": msg}
|
||
relative_path = self._relative_path(full_path)
|
||
|
||
try:
|
||
# 检查是否允许在根目录创建文件
|
||
personalization_config = self._load_personalization_config()
|
||
allow_root_creation = bool(
|
||
personalization_config.get("allow_root_file_creation", False)
|
||
) if isinstance(personalization_config, dict) else False
|
||
|
||
if full_path.parent == self.project_path and not allow_root_creation:
|
||
return {
|
||
"success": False,
|
||
"error": "禁止在项目根目录直接创建文件,请先创建或选择合适的子目录。",
|
||
"suggestion": "然后必须**重新**再次创建文件。"
|
||
}
|
||
if self._use_container():
|
||
result = self._container_call("create_file", {
|
||
"path": relative_path,
|
||
"content": ""
|
||
})
|
||
if result.get("success"):
|
||
print(f"{OUTPUT_FORMATS['file']} 创建文件: {relative_path}")
|
||
return result
|
||
|
||
# 创建父目录
|
||
full_path.parent.mkdir(parents=True, exist_ok=True)
|
||
|
||
with open(full_path, 'w', encoding='utf-8') as f:
|
||
f.write("")
|
||
|
||
print(f"{OUTPUT_FORMATS['file']} 创建文件: {relative_path}")
|
||
|
||
return {
|
||
"success": True,
|
||
"path": relative_path,
|
||
"size": 0
|
||
}
|
||
except Exception as e:
|
||
return {"success": False, "error": str(e)}
|
||
|
||
def delete_file(self, path: str) -> Dict:
|
||
"""删除文件"""
|
||
valid, error, full_path = self._validate_path(path)
|
||
if not valid:
|
||
return {"success": False, "error": error}
|
||
|
||
if not full_path.exists():
|
||
return {"success": False, "error": "文件不存在"}
|
||
|
||
if not full_path.is_file():
|
||
return {"success": False, "error": "不是文件"}
|
||
ok, msg = self._ensure_host_access(full_path, "write")
|
||
if not ok:
|
||
return {"success": False, "error": msg}
|
||
|
||
try:
|
||
relative_path = self._relative_path(full_path)
|
||
if self._use_container():
|
||
result = self._container_call("delete_file", {
|
||
"path": relative_path
|
||
})
|
||
if result.get("success"):
|
||
print(f"{OUTPUT_FORMATS['file']} 删除文件: {relative_path}")
|
||
return result
|
||
|
||
full_path.unlink()
|
||
print(f"{OUTPUT_FORMATS['file']} 删除文件: {relative_path}")
|
||
|
||
# 删除文件备注(如果存在)
|
||
# 这需要通过context_manager处理,但file_manager没有直接访问权限
|
||
# 所以返回相对路径,让调用者处理备注删除
|
||
|
||
return {
|
||
"success": True,
|
||
"path": relative_path,
|
||
"action": "deleted"
|
||
}
|
||
except Exception as e:
|
||
return {"success": False, "error": str(e)}
|
||
|
||
def rename_file(self, old_path: str, new_path: str) -> Dict:
|
||
"""重命名文件"""
|
||
valid_old, error_old, full_old_path = self._validate_path(old_path)
|
||
if not valid_old:
|
||
return {"success": False, "error": error_old}
|
||
|
||
valid_new, error_new, full_new_path = self._validate_path(new_path)
|
||
if not valid_new:
|
||
return {"success": False, "error": error_new}
|
||
|
||
if not full_old_path.exists():
|
||
return {"success": False, "error": "原文件不存在"}
|
||
|
||
if full_new_path.exists():
|
||
return {"success": False, "error": "目标文件已存在"}
|
||
ok_old, msg_old = self._ensure_host_access(full_old_path, "write")
|
||
if not ok_old:
|
||
return {"success": False, "error": msg_old}
|
||
ok_new, msg_new = self._ensure_host_access(full_new_path, "write")
|
||
if not ok_new:
|
||
return {"success": False, "error": msg_new}
|
||
|
||
try:
|
||
old_relative = self._relative_path(full_old_path)
|
||
new_relative = self._relative_path(full_new_path)
|
||
if self._use_container():
|
||
result = self._container_call("rename_file", {
|
||
"old_path": old_relative,
|
||
"new_path": new_relative
|
||
})
|
||
if result.get("success"):
|
||
print(f"{OUTPUT_FORMATS['file']} 重命名: {old_relative} -> {new_relative}")
|
||
return result
|
||
|
||
full_old_path.rename(full_new_path)
|
||
|
||
print(f"{OUTPUT_FORMATS['file']} 重命名: {old_relative} -> {new_relative}")
|
||
|
||
return {
|
||
"success": True,
|
||
"old_path": old_relative,
|
||
"new_path": new_relative,
|
||
"action": "renamed"
|
||
}
|
||
except Exception as e:
|
||
return {"success": False, "error": str(e)}
|
||
|
||
def create_folder(self, path: str) -> Dict:
|
||
"""创建文件夹"""
|
||
valid, error, full_path = self._validate_path(path)
|
||
if not valid:
|
||
return {"success": False, "error": error}
|
||
|
||
if full_path.exists():
|
||
return {"success": False, "error": "文件夹已存在"}
|
||
ok, msg = self._ensure_host_access(full_path, "write")
|
||
if not ok:
|
||
return {"success": False, "error": msg}
|
||
|
||
try:
|
||
relative_path = self._relative_path(full_path)
|
||
if self._use_container():
|
||
result = self._container_call("create_folder", {"path": relative_path})
|
||
if result.get("success"):
|
||
print(f"{OUTPUT_FORMATS['file']} 创建文件夹: {relative_path}")
|
||
return result
|
||
|
||
full_path.mkdir(parents=True, exist_ok=True)
|
||
print(f"{OUTPUT_FORMATS['file']} 创建文件夹: {relative_path}")
|
||
|
||
return {"success": True, "path": relative_path}
|
||
except Exception as e:
|
||
return {"success": False, "error": str(e)}
|
||
|
||
def delete_folder(self, path: str) -> Dict:
|
||
"""删除文件夹"""
|
||
valid, error, full_path = self._validate_path(path)
|
||
if not valid:
|
||
return {"success": False, "error": error}
|
||
|
||
if not full_path.exists():
|
||
return {"success": False, "error": "文件夹不存在"}
|
||
|
||
if not full_path.is_dir():
|
||
return {"success": False, "error": "不是文件夹"}
|
||
|
||
try:
|
||
relative_path = self._relative_path(full_path)
|
||
if self._use_container():
|
||
result = self._container_call("delete_folder", {"path": relative_path})
|
||
if result.get("success"):
|
||
print(f"{OUTPUT_FORMATS['file']} 删除文件夹: {relative_path}")
|
||
return result
|
||
|
||
shutil.rmtree(full_path)
|
||
print(f"{OUTPUT_FORMATS['file']} 删除文件夹: {relative_path}")
|
||
|
||
return {"success": True, "path": relative_path}
|
||
except Exception as e:
|
||
return {"success": False, "error": str(e)}
|
||
|
||
def write_file(self, path: str, content: str, mode: str = "w") -> Dict:
|
||
"""
|
||
写入文件
|
||
|
||
Args:
|
||
path: 文件路径
|
||
content: 内容
|
||
mode: 写入模式 - "w"(覆盖), "a"(追加)
|
||
"""
|
||
valid, error, full_path = self._validate_path(path)
|
||
if not valid:
|
||
return {"success": False, "error": error}
|
||
ok, msg = self._ensure_host_access(full_path, "write")
|
||
if not ok:
|
||
return {"success": False, "error": msg}
|
||
|
||
# === 新增:内容预处理和验证 ===
|
||
if content:
|
||
# 长度检查
|
||
if not DISABLE_LENGTH_CHECK and len(content) > 9999999999: # 100KB限制
|
||
return {
|
||
"success": False,
|
||
"error": f"内容过长({len(content)}字符),超过100KB限制",
|
||
"suggestion": "请分块处理或使用部分修改方式"
|
||
}
|
||
|
||
# 检查潜在的JSON格式问题
|
||
if content.count('"') % 2 != 0:
|
||
print(f"{OUTPUT_FORMATS['warning']} 检测到奇数个引号,可能存在格式问题")
|
||
|
||
# 检查大量转义字符
|
||
if content.count('\\') > len(content) / 20:
|
||
print(f"{OUTPUT_FORMATS['warning']} 检测到大量转义字符,建议检查内容格式")
|
||
|
||
try:
|
||
relative_path = self._relative_path(full_path)
|
||
if PROJECT_MAX_STORAGE_BYTES and self._is_docker_mode():
|
||
current_size = self._get_project_size()
|
||
existing_size = full_path.stat().st_size if full_path.exists() else 0
|
||
if mode == "a":
|
||
projected_total = current_size + len(content)
|
||
else:
|
||
projected_total = current_size - existing_size + len(content)
|
||
if projected_total > PROJECT_MAX_STORAGE_BYTES:
|
||
return {
|
||
"success": False,
|
||
"error": "写入失败:超出项目磁盘配额",
|
||
"limit_bytes": PROJECT_MAX_STORAGE_BYTES,
|
||
"project_size_bytes": current_size,
|
||
"attempt_size_bytes": len(content)
|
||
}
|
||
# 记录写入前的原始内容(用于版本控制和前端 diff 显示)
|
||
original_file: Optional[str] = None
|
||
try:
|
||
if full_path.exists():
|
||
original_file = full_path.read_text(encoding='utf-8')
|
||
except Exception:
|
||
original_file = None
|
||
|
||
if self._use_container():
|
||
result = self._container_call("write_file", {
|
||
"path": relative_path,
|
||
"content": content,
|
||
"mode": mode
|
||
})
|
||
if result.get("success"):
|
||
action = "覆盖" if mode == "w" else "追加"
|
||
print(f"{OUTPUT_FORMATS['file']} {action}文件: {relative_path}")
|
||
result["original_file"] = original_file
|
||
return result
|
||
|
||
# 创建父目录
|
||
full_path.parent.mkdir(parents=True, exist_ok=True)
|
||
|
||
with open(full_path, mode, encoding='utf-8') as f:
|
||
f.write(content)
|
||
|
||
action = "覆盖" if mode == "w" else "追加"
|
||
print(f"{OUTPUT_FORMATS['file']} {action}文件: {relative_path}")
|
||
|
||
# 读取写入后的实际内容(追加模式需要从文件读取)
|
||
try:
|
||
new_file = full_path.read_text(encoding='utf-8')
|
||
except Exception:
|
||
new_file = content
|
||
|
||
return {
|
||
"success": True,
|
||
"path": relative_path,
|
||
"size": len(content),
|
||
"mode": mode,
|
||
"original_file": original_file,
|
||
"new_file": new_file,
|
||
}
|
||
except Exception as e:
|
||
return {"success": False, "error": str(e)}
|
||
|
||
def append_file(self, path: str, content: str) -> Dict:
|
||
"""追加内容到文件"""
|
||
return self.write_file(path, content, mode="a")
|
||
|
||
def clear_file(self, path: str) -> Dict:
|
||
"""清空文件内容"""
|
||
return self.write_file(path, "", mode="w")
|