import os from google.genai import types schema_write_file = types.FunctionDeclaration( name="write_file", description="Writes or overwrites file contents within the working directory.", parameters=types.Schema( type=types.Type.OBJECT, properties={ "file_path": types.Schema( type=types.Type.STRING, description="Path to the file relative to the working directory.", ), "content": types.Schema( type=types.Type.STRING, description="The text content to write into the file.", ), }, required=["file_path", "content"], ), ) def write_file(working_directory, file_path, content): try: full_path = os.path.join(working_directory, file_path) abs_working_directory = os.path.abspath(working_directory) abs_target_path = os.path.abspath(full_path) if not abs_target_path.startswith(abs_working_directory): return ( f'Error: Cannot write to "{file_path}" as it is outside the permitted working directory' ) parent_dir = os.path.dirname(abs_target_path) if parent_dir and not os.path.exists(parent_dir): os.makedirs(parent_dir, exist_ok=True) with open(abs_target_path, "w", encoding="utf-8") as f: f.write(content) return ( f'Successfully wrote to "{file_path}" ({len(content)} characters written)' ) except Exception as e: return f"Error: {e}"