Files
ai-agent-gemini/functions/get_file_content.py
T

21 lines
848 B
Python

import os
def get_file_content(working_directory, file_path):
abs_dir_path = os.path.abspath(working_directory)
abs_file_path = os.path.abspath(os.path.join(abs_dir_path, file_path))
if os.path.commonpath([abs_dir_path, abs_file_path]) != abs_dir_path:
return f'Error: Cannot read "{file_path}" as it is outside the permitted working directory'
if not os.path.isfile(abs_file_path):
return f'Error: File not found or is not a regular file: "{file_path}"'
MAX_CHARS = 10000
try:
with open(abs_file_path, "r") as f:
file_content_string = f.read(MAX_CHARS)
if f.read(1):
file_content_string += f'[...File "{file_path}" truncated at {MAX_CHARS} characters]'
except Exception as e:
return f"Error: {e}"
return file_content_string