add export_tools.py
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
"""Выгрузка виртуальных файлов агента в реальную файловую систему."""
|
||||||
|
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}"
|
||||||
Reference in New Issue
Block a user