37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
import os
|
|
from google.genai import types
|
|
|
|
def get_files_info(working_directory, directory="."):
|
|
abs_path = os.path.abspath(working_directory)
|
|
target_dir = os.path.normpath(os.path.join(abs_path, directory))
|
|
# Will be True or False
|
|
valid_target_dir = os.path.commonpath([abs_path, target_dir]) == abs_path
|
|
if not valid_target_dir:
|
|
return f'Error: Cannot list "{directory}" as it is outside the permitted working directory'
|
|
if type(directory) is not str:
|
|
return f'Error: "{directory}" is not a directory'
|
|
|
|
try:
|
|
dir_list = os.listdir(target_dir)
|
|
dir_string = ""
|
|
for item in dir_list:
|
|
dir_string += f"- {item}: file_size={os.path.getsize(f"{target_dir}/{item}")}, is_dir={os.path.isdir(f"{target_dir}/{item}")}\n"
|
|
return dir_string
|
|
except Exception as e:
|
|
return f"Error: encountered during loop over dir_list: {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)",
|
|
),
|
|
},
|
|
),
|
|
)
|