48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
import os
|
|||
|
|
|
||
|
|
from google.genai import types
|
||
|
|
|
||
|
|
|
||
|
|
def get_file_content(working_directory: str, file_path: str) -> str:
|
||
|
|
try:
|
||
|
|
wdp = os.path.abspath(working_directory)
|
||
|
|
target = os.path.normpath(os.path.join(wdp, file_path))
|
||
|
|
valid_target_dir = os.path.commonpath([wdp, target]) == wdp
|
||
|
|
|
||
|
|
if not valid_target_dir:
|
||
|
|
return f'Error: Cannot read "{file_path}" as it is outside the permitted working directory'
|
||
|
|
|
||
|
|
if not os.path.isfile(target):
|
||
|
|
return f'Error: File not found or is not a regular file: "{file_path}"'
|
||
|
|
|
||
|
|
MAX_CHARS = 10000
|
||
|
|
content = ""
|
||
|
|
|
||
|
|
with open(target, "r") as f:
|
||
|
|
content += f.read(MAX_CHARS)
|
||
|
|
|
||
|
|
if f.read(1):
|
||
|
|
content += (
|
||
|
|
f'[...File "{file_path}" truncated at {MAX_CHARS} characters]'
|
||
|
|
)
|
||
|
|
|
||
|
|
return content
|
||
|
|
except Exception as e:
|
||
|
|
return f"Error: {e}"
|
||
|
|
|
||
|
|
|
||
|
|
schema_get_file_content = types.FunctionDeclaration(
|
||
|
|
name="get_file_content",
|
||
|
|
description="",
|
||
|
|
parameters=types.Schema(
|
||
|
|
type=types.Type.OBJECT,
|
||
|
|
properties={
|
||
|
|
"file_path": types.Schema(
|
||
|
|
type=types.Type.STRING,
|
||
|
|
description="",
|
||
|
|
),
|
||
|
|
},
|
||
|
|
required=["file_path"],
|
||
|
|
),
|
||
|
|
)
|