2025-08-05 14:48:45 +02:00
|
|
|
import os
|
2025-08-15 16:14:02 +02:00
|
|
|
from google.genai import types
|
2025-08-05 14:48:45 +02:00
|
|
|
|
|
|
|
|
def write_file(working_directory, file_path, content):
|
|
|
|
|
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 write to "{file_path}" as it is outside the permitted working directory'
|
|
|
|
|
if not os.path.exists(abs_file_path):
|
|
|
|
|
os.makedirs(abs_file_path)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if os.path.exists(abs_file_path):
|
|
|
|
|
with open(abs_file_path, 'w') as f:
|
|
|
|
|
f.write(content)
|
|
|
|
|
return f'Successfully wrote to "{file_path}" ({len(content)} characters written)'
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error: {e}")
|
2025-08-15 16:14:02 +02:00
|
|
|
|
|
|
|
|
schema_write_file = types.FunctionDeclaration(
|
|
|
|
|
name="write_file",
|
|
|
|
|
description="Write file.",
|
|
|
|
|
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 write the content to. If not provided, the function will not work."
|
|
|
|
|
),
|
|
|
|
|
"content": types.Schema(
|
|
|
|
|
type=types.Type.STRING,
|
|
|
|
|
description="Content to write to the file."
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)
|