// retoor #include "tool.h" #include "r_config.h" #include "bash_executor.h" #include #include #include #include static struct json_object *python_execute_get_description(void); static char *python_execute_execute(tool_t *self, struct json_object *args); static void python_execute_print_action(const char *name, struct json_object *args); static const tool_vtable_t python_execute_vtable = { .get_description = python_execute_get_description, .execute = python_execute_execute, .print_action = python_execute_print_action }; static tool_t python_execute_tool = { .vtable = &python_execute_vtable, .name = "python_execute" }; tool_t *tool_python_execute_create(void) { return &python_execute_tool; } static void python_execute_print_action(const char *name, struct json_object *args) { (void)name; (void)args; fprintf(stderr, " \033[1m-> Executing Python code\033[0m\n"); } static char *python_execute_execute(tool_t *self, struct json_object *args) { (void)self; struct json_object *source_obj; if (!json_object_object_get_ex(args, "source", &source_obj)) { return strdup("Error: missing 'source' argument"); } const char *source_code = json_object_get_string(source_obj); char *output = NULL; int fd = -1; char tmp_file[] = "/tmp/r_python_tool_XXXXXX.py"; fd = mkstemps(tmp_file, 3); if (fd == -1) { return strdup("Failed to create temporary file for Python code."); } FILE *fp = fdopen(fd, "w"); if (!fp) { close(fd); unlink(tmp_file); return strdup("Failed to open temporary file for writing."); } fwrite(source_code, 1, strlen(source_code), fp); fclose(fp); char command[4096]; snprintf(command, sizeof(command), "python3 '%s'", tmp_file); output = r_bash_execute(command, false, 300); unlink(tmp_file); return output; } static struct json_object *python_execute_get_description(void) { struct json_object *root = json_object_new_object(); json_object_object_add(root, "type", json_object_new_string("function")); struct json_object *function = json_object_new_object(); json_object_object_add(function, "name", json_object_new_string("python_execute")); json_object_object_add(function, "description", json_object_new_string("Executes Python source code using the python3 interpreter and returns stdout/stderr.")); struct json_object *parameters = json_object_new_object(); json_object_object_add(parameters, "type", json_object_new_string("object")); struct json_object *properties = json_object_new_object(); struct json_object *source = json_object_new_object(); json_object_object_add(source, "type", json_object_new_string("string")); json_object_object_add(source, "description", json_object_new_string("Python source code to execute.")); json_object_object_add(properties, "source", source); json_object_object_add(parameters, "properties", properties); struct json_object *required = json_object_new_array(); json_object_array_add(required, json_object_new_string("source")); json_object_object_add(parameters, "required", required); json_object_object_add(parameters, "additionalProperties", json_object_new_boolean(0)); json_object_object_add(function, "parameters", parameters); r_config_handle cfg = r_config_get_instance(); if (r_config_use_strict(cfg)) { json_object_object_add(function, "strict", json_object_new_boolean(1)); } json_object_object_add(root, "function", function); return root; }