Files

45 lines
1.5 KiB
Python

import os
from google.genai import types
def get_files_info(working_directory, directory: str = ".") -> str:
try:
wdp = os.path.abspath(working_directory)
target = os.path.normpath(os.path.join(wdp, directory))
valid_target_dir = os.path.commonpath([wdp, target]) == wdp
if not valid_target_dir:
return f'Error: Cannot list "{directory}" as it is outside the permitted directory'
if not os.path.isdir(target):
return f'Error: "{directory}" is not a directory'
file_descriptors = []
for file_name in os.listdir(target):
file_path = os.path.normpath(os.path.join(target, file_name))
size = os.path.getsize(file_path)
is_dir = os.path.isdir(file_path)
file_descriptors.append(f"- {file_name}: file_size={size}, is_dir={is_dir}")
return "\n".join(file_descriptors)
except Exception as e:
return f"Error: {e}"
schema_get_files_info = types.FunctionDeclaration(
name="get_files_info",
description="Lists files in a specified directory relative to the working directory, providing file size and directory status",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"directory": types.Schema(
type=types.Type.STRING,
description="Directory path to list files from, relative to the working directory (default is the working directory itself)",
),
},
required=["directory"],
),
)