end of project

This commit is contained in:
2025-08-15 16:14:02 +02:00
parent 495935dc74
commit d6ab65d7cf
10 changed files with 278 additions and 41 deletions

View File

@@ -1,5 +1,6 @@
import os
from functions.config import FILE_LENGTH_LIMIT
from google.genai import types
def get_file_content(working_directory, file_path):
abs_working_dir = os.path.abspath(working_directory)
@@ -18,3 +19,21 @@ def get_file_content(working_directory, file_path):
return content
except Exception as e:
return f'Error reading file "{file_path}": {e}'
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."
)
},
),
)

View File

@@ -1,4 +1,6 @@
import os
from google.genai import types
def get_files_info(working_directory, directory="."):
abs_working_dir = os.path.abspath(working_directory)
@@ -20,3 +22,17 @@ def get_files_info(working_directory, directory="."):
return "\n".join(files_info)
except Exception as e:
return f"Error listing files: {e}"
schema_get_files_info = types.FunctionDeclaration(
name="get_files_info",
description="Lists files in the specified directory along with their sizes, constrained to the working directory.",
parameters=types.Schema(
type=types.Type.OBJECT,
properties={
"directory": types.Schema(
type=types.Type.STRING,
description="The directory to list files from, relative to the working directory. If not provided, lists files in the working directory itself.",
),
},
),
)

57
functions/run_python.py Normal file
View File

@@ -0,0 +1,57 @@
import os
import subprocess
from google.genai import types
def run_python_file(working_directory, file_path, args=[]):
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 execute "{file_path}" as it is outside the permitted working directory'
if not os.path.isfile(abs_file_path):
return f'Error: File "{file_path}" not found'
if not abs_file_path.endswith(".py"):
return f'Error: "{file_path}" is not a Python file.'
try:
completed = subprocess.run(args=["python3", abs_file_path]+args, capture_output=True, cwd=abs_working_dir, timeout=30)
if completed == None:
raise Exception("Subprocess holder is empty")
if completed.stdout == None or completed.stdout == "":
return f"No output produced."
message = f"STDOUT: {completed.stdout.decode()}. STDERR: {completed.stderr.decode()}."
if completed.returncode != 0:
message += f" Error: Process exited with code {completed.returncode}"
except Exception as e:
return f"Error: executing Python file: {e}"
return message
schema_run_python_file = types.FunctionDeclaration(
name="run_python_file",
description="Run the python 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 execute. If not provided, the function will not work."
),
"args": types.Schema(
type=types.Type.ARRAY,
items=types.Schema(
type=types.Type.STRING,
description="Optional arguments to pass to the Python file.",
),
description="Arguments to run the python file with."
)
},
),
)

View File

@@ -1,4 +1,5 @@
import os
from google.genai import types
def write_file(working_directory, file_path, content):
abs_working_dir = os.path.abspath(working_directory)
@@ -15,3 +16,25 @@ def write_file(working_directory, file_path, content):
return f'Successfully wrote to "{file_path}" ({len(content)} characters written)'
except Exception as e:
print(f"Error: {e}")
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."
)
},
),
)