ocr improved

This commit is contained in:
2026-01-13 18:25:49 +08:00
parent 9745ca2476
commit a5eb381384
104 changed files with 818 additions and 229 deletions

View File

@@ -63,6 +63,58 @@ class WorkspaceManager:
logger.error(f"Failed to create workspace '{name}': {e}")
raise
async def rename_workspace(self, old_name: str, new_name: str) -> bool:
"""Rename a workspace directory and update cached instances."""
if not old_name or not old_name.strip():
raise ValueError("Old workspace name cannot be empty")
if not new_name or not new_name.strip():
raise ValueError("New workspace name cannot be empty")
old_name = old_name.strip()
new_name = new_name.strip()
# Validate new name (alphanumeric, underscore, hyphen)
if not all(c.isalnum() or c in ('_', '-') for c in new_name):
raise ValueError("New workspace name can only contain alphanumeric characters, underscores, and hyphens")
# Check if old workspace exists
old_workspace_dir = self.base_working_dir / old_name
old_input_subdir = self.base_input_dir / old_name
if not old_workspace_dir.exists() and not old_input_subdir.exists():
raise ValueError(f"Workspace '{old_name}' does not exist")
# Check if new workspace already exists
new_workspace_dir = self.base_working_dir / new_name
new_input_subdir = self.base_input_dir / new_name
if new_workspace_dir.exists() or new_input_subdir.exists():
raise ValueError(f"Workspace '{new_name}' already exists")
# Move directories
import shutil
try:
if old_workspace_dir.exists():
shutil.move(str(old_workspace_dir), str(new_workspace_dir))
logger.info(f"Moved workspace directory from {old_workspace_dir} to {new_workspace_dir}")
if old_input_subdir.exists():
shutil.move(str(old_input_subdir), str(new_input_subdir))
logger.info(f"Moved input directory from {old_input_subdir} to {new_input_subdir}")
except Exception as e:
logger.error(f"Failed to rename workspace '{old_name}' to '{new_name}': {e}")
raise
# Update cached instances
if old_name in self._rag_instances:
self._rag_instances[new_name] = self._rag_instances.pop(old_name)
# Optionally update the workspace name inside the LightRAG instance? Not needed as path changed.
if old_name in self._doc_managers:
self._doc_managers[new_name] = self._doc_managers.pop(old_name)
# Update the workspace name in DocumentManager? It uses workspace parameter.
# For simplicity, we'll just keep the cached instance; but the workspace attribute may be wrong.
# Better to discard and let it be recreated when needed.
del self._doc_managers[new_name] # discard, will be recreated with new workspace name
logger.info(f"Renamed workspace '{old_name}' to '{new_name}' successfully")
return True
async def delete_workspace(self, name: str) -> bool:
"""Delete a workspace directory and all its data."""
import shutil