Files

39 lines
1.5 KiB
Python
Raw Permalink Normal View History

2025-08-04 17:35:26 +02:00
import os
2025-08-15 16:14:02 +02:00
from google.genai import types
2025-08-04 17:35:26 +02:00
def get_files_info(working_directory, directory="."):
abs_working_dir = os.path.abspath(working_directory)
target_dir = os.path.abspath(os.path.join(working_directory, directory))
if not target_dir.startswith(abs_working_dir):
return f'Error: Cannot list "{directory}" as it is outside the permitted working directory'
if not os.path.isdir(target_dir):
return f'Error: "{directory}" is not a directory'
try:
files_info = []
for filename in os.listdir(target_dir):
filepath = os.path.join(target_dir, filename)
file_size = 0
is_dir = os.path.isdir(filepath)
file_size = os.path.getsize(filepath)
files_info.append(
f"- {filename}: file_size={file_size} bytes, is_dir={is_dir}"
)
return "\n".join(files_info)
except Exception as e:
return f"Error listing files: {e}"
2025-08-15 16:14:02 +02:00
schema_get_files_info = types.FunctionDeclaration(
name="get_files_info",
description="Lists files in the specified directory along with their sizes, constrained to the working directory.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"directory": types.Schema(
type=types.Type.STRING,
description="The directory to list files from, relative to the working directory. If not provided, lists files in the working directory itself.",
),
},
),
)