File size: 971 Bytes
a31f556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# backend/tasks/tools/filesystem_tools.py
import os
import shutil
import glob

async def read_file(path: str) -> str:
    with open(path, 'r', encoding='utf-8') as f:
        return f.read()

async def write_file(path: str, content: str) -> bool:
    os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
    with open(path, 'w', encoding='utf-8') as f:
        f.write(content)
    return True

async def list_dir(path: str) -> list[str]:
    if not os.path.isdir(path):
        return []
    return os.listdir(path)

async def delete_file(path: str) -> bool:
    if os.path.isfile(path):
        os.remove(path)
        return True
    elif os.path.isdir(path):
        shutil.rmtree(path)
        return True
    return False

async def search_files(query: str, root: str) -> list[str]:
    # Using glob for simple search
    pattern = os.path.join(root, '**', f"*{query}*")
    return [p for p in glob.glob(pattern, recursive=True) if os.path.isfile(p)]