Files

40 lines
1.6 KiB
Python
Raw Permalink Normal View History

2025-08-04 17:35:26 +02:00
import os
from functions.config import FILE_LENGTH_LIMIT
2025-08-15 16:14:02 +02:00
from google.genai import types
2025-08-04 17:35:26 +02:00
def get_file_content(working_directory, file_path):
abs_working_dir = os.path.abspath(working_directory)
abs_file_path = os.path.abspath(os.path.join(working_directory, file_path))
if not abs_file_path.startswith(abs_working_dir):
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}"'
try:
with open(abs_file_path, "r") as f:
content = f.read(FILE_LENGTH_LIMIT)
if os.path.getsize(abs_file_path) > FILE_LENGTH_LIMIT:
content += (
f'[...File "{file_path}" truncated at {FILE_LENGTH_LIMIT} characters]'
)
return content
except Exception as e:
return f'Error reading file "{file_path}": {e}'
2025-08-15 16:14:02 +02:00
schema_get_file_content = types.FunctionDeclaration(
name="get_file_content",
description="Read file contents.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"working_directory": types.Schema(
type=types.Type.STRING,
description="The directory to read the file from. If not provided, lists files in the working directory itself.",
),
"file_path": types.Schema(
type=types.Type.STRING,
description="The file to read the content from. If not provided, the function will not work."
)
},
),
)