40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
import os
|
|
from google.genai import types
|
|
|
|
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
|
|
|
|
schema_get_file_content = types.FunctionDeclaration(
|
|
name="get_file_content",
|
|
description="Open and read file content",
|
|
parameters=types.Schema(
|
|
type=types.Type.OBJECT,
|
|
properties={
|
|
"working_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)",
|
|
),
|
|
"file_path": types.Schema(
|
|
type=types.Type.STRING,
|
|
description="File path",
|
|
),
|
|
},
|
|
required=["file_path"]
|
|
),
|
|
) |