Files
2026-05-26 07:31:05 +00:00

46 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Выгрузка виртуальных файлов агента в реальную файловую систему."""
from __future__ import annotations
import shutil
from pathlib import Path
from langchain.tools import tool
WORKSPACE_DIR = Path("./agent_workspace")
DEFAULT_OUTPUT_DIR = Path("./output")
def export_workspace_to_disk(
workspace: Path = WORKSPACE_DIR,
output_dir: Path = DEFAULT_OUTPUT_DIR,
) -> list[str]:
"""Копирует все файлы из workspace в output_dir. Возвращает список путей."""
output_dir.mkdir(parents=True, exist_ok=True)
if not workspace.exists():
return []
exported: list[str] = []
for src in workspace.rglob("*"):
if not src.is_file():
continue
rel = src.relative_to(workspace)
dst = output_dir / rel
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
exported.append(str(dst))
return exported
@tool
def export_virtual_files(target_dir: str = "./output") -> str:
"""Выгрузить все виртуальные файлы агента в реальную папку на диске.
Args:
target_dir: каталог назначения (по умолчанию ./output)
"""
paths = export_workspace_to_disk(output_dir=Path(target_dir))
if not paths:
return f"В {WORKSPACE_DIR} нет файлов для экспорта."
lines = "\n".join(f" - {p}" for p in paths)
return f"Экспортировано {len(paths)} файл(ов) в {target_dir}:\n{lines}"