60,
'display' => __('Every Minute (Aiutoma)', 'aiutoma')
];
}
return $schedules;
});
// Cron hook
add_action('aiutoma_automated_tasks_cron', [$this, 'run_automated_tasks']);
// Register cron if not registered
if (!wp_next_scheduled('aiutoma_automated_tasks_cron')) {
wp_schedule_event(time(), 'aiutoma_every_minute', 'aiutoma_automated_tasks_cron');
}
add_action('admin_enqueue_scripts', [$this, 'enqueue_automation_scripts']);
}
public function register_automated_task_cpt()
{
$labels = [
'name' => _x('Automated Tasks', 'Post type general name', 'aiutoma'),
'singular_name' => _x('Automated Task', 'Post type singular name', 'aiutoma'),
'menu_name' => _x('Automated Tasks', 'Admin Menu text', 'aiutoma'),
'name_admin_bar' => _x('Automated Task', 'Add New on Toolbar', 'aiutoma'),
'add_new' => __('Add New', 'aiutoma'),
'add_new_item' => __('Add New Task', 'aiutoma'),
'new_item' => __('New Task', 'aiutoma'),
'edit_item' => __('Edit Task', 'aiutoma'),
'view_item' => __('View Task', 'aiutoma'),
'all_items' => __('All Tasks', 'aiutoma'),
'search_items' => __('Search Tasks', 'aiutoma'),
'parent_item_colon' => __('Parent Tasks:', 'aiutoma'),
'not_found' => __('No tasks found.', 'aiutoma'),
'not_found_in_trash' => __('No tasks found in Trash.', 'aiutoma'),
'item_published' => __('Task published.', 'aiutoma'),
'item_published_privately' => __('Task published privately.', 'aiutoma'),
'item_reverted_to_draft' => __('Task reverted to draft.', 'aiutoma'),
'item_scheduled' => __('Task scheduled.', 'aiutoma'),
'item_updated' => __('Task updated.', 'aiutoma'),
];
$args = [
'label' => __('Automated Tasks', 'aiutoma'),
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => false,
'capability_type' => 'post',
'supports' => ['title', 'editor', 'author'],
];
register_post_type('aiutoma_task', $args);
}
public function add_aiutoma_task_meta_boxes()
{
add_meta_box('aiutoma_task_settings', __('Task Settings', 'aiutoma'), [$this, 'aiutoma_task_settings_html'], 'aiutoma_task', 'side', 'default');
}
public function aiutoma_task_settings_html($post)
{
wp_nonce_field('aiutoma_task_save_meta', 'aiutoma_task_meta_nonce');
$meta = get_post_meta($post->ID, '_aiutoma_task_data', true);
if (!is_array($meta)) $meta = [];
$schedule = $meta['schedule'] ?? 'hourly';
$pause_time = $meta['pause_time'] ?? '0';
$model = $meta['model'] ?? '';
$execute_time = $meta['execute_time'] ?? '';
$sched = $schedule;
$custom_cron = '';
if (!in_array($sched, ['hourly', 'twicedaily', 'daily', 'once', 'minute'])) {
$custom_cron = $sched;
$sched = 'custom';
}
?>
_get_all_automated_tasks();
if (!isset($tasks[$post_id])) return;
$task = $tasks[$post_id];
switch ($column) {
case 'schedule':
if (($task['schedule'] ?? '') === 'once' && !empty($task['execute_time'])) {
echo esc_html__('Once at ', 'aiutoma') . esc_html(wp_date('Y-m-d H:i', $task['execute_time']));
} else {
echo esc_html(ucfirst($task['schedule'] ?? 'Hourly'));
}
break;
case 'pause':
echo esc_html(($task['pause_time'] ?? '2') . 's');
break;
case 'last_run':
echo esc_html(!empty($task['last_run']) ? wp_date('Y-m-d H:i:s', $task['last_run']) : __('Never', 'aiutoma'));
break;
case 'next_run':
$next = $this->_get_next_run_time($task);
echo esc_html($next ? wp_date('Y-m-d H:i:s', $next) : '-');
break;
case 'count':
echo esc_html($task['run_count'] ?? '0');
break;
case 'status':
$post = get_post($post_id);
if ($post->post_status !== 'publish') {
echo '' . esc_html(ucfirst($post->post_status)) . '';
} else {
if (empty($task['active'])) {
echo '' . esc_html__('Paused', 'aiutoma') . '';
} else {
echo '' . esc_html__('Active', 'aiutoma') . '';
}
}
break;
case 'actions':
echo ' ';
if (!empty($task['last_log'])) {
echo '' . esc_html__('View Log', 'aiutoma') . ' ';
}
$upload_dir = wp_upload_dir();
$session_id_prefix = 'cron_' . $post_id;
$log_dir = \Aiutoma\Modules\Ai\Ai::get_storage_dir() . '/logs/sessions';
if (is_dir($log_dir)) {
$files = glob($log_dir . '/' . $session_id_prefix . '*.json');
if (!empty($files)) {
rsort($files);
$latest_file = basename($files[0], '.json');
$playground_url = admin_url('admin.php?page=aiutoma&session_id=' . urlencode($latest_file));
echo '
' . esc_html__('Session', 'aiutoma') . '';
}
}
break;
}
}
private function _get_all_automated_tasks()
{
$tasks = [];
$query = new \WP_Query([
'post_type' => 'aiutoma_task',
'post_status' => 'any',
'posts_per_page' => -1
]);
foreach ($query->posts as $post) {
$meta = get_post_meta($post->ID, '_aiutoma_task_data', true);
if (is_array($meta)) {
$tasks[$post->ID] = $meta;
$tasks[$post->ID]['name'] = $post->post_title;
$tasks[$post->ID]['prompt'] = $post->post_content;
$tasks[$post->ID]['post_status'] = $post->post_status;
$tasks[$post->ID]['post_author'] = $post->post_author;
}
}
return $tasks;
}
private function _update_task_meta($post_id, $task_data)
{
// Don't duplicate title/content/status/author in meta
$meta_data = $task_data;
unset($meta_data['name']);
unset($meta_data['prompt']);
unset($meta_data['post_status']);
unset($meta_data['post_author']);
update_post_meta($post_id, '_aiutoma_task_data', $meta_data);
}
public function enqueue_automation_scripts($hook)
{
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading screen context to conditionally enqueue admin scripts.
$screen_post_type = isset($_GET['post_type']) ? sanitize_key(wp_unslash($_GET['post_type'])) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading screen context to conditionally enqueue admin scripts.
$screen_post_id = isset($_GET['post']) ? absint(wp_unslash($_GET['post'])) : 0;
$is_cpt_page = ($screen_post_type === 'aiutoma_task') ||
($screen_post_id > 0 && get_post_type($screen_post_id) === 'aiutoma_task');
$is_valid_hook = in_array($hook, ['edit.php', 'post.php', 'post-new.php']);
if (strpos($hook, 'aiutoma-automation') !== false || ($is_valid_hook && $is_cpt_page)) {
wp_enqueue_style('aiutoma-select2');
wp_enqueue_script('aiutoma-select2');
wp_enqueue_style('aiutoma-automation-style', AIUTOMA_URL . 'modules/playground/assets/css/automation.css', [], filemtime(AIUTOMA_PATH . 'modules/playground/assets/css/automation.css'));
wp_enqueue_script('aiutoma-automation-script', AIUTOMA_URL . 'modules/playground/assets/js/automation.js', ['jquery', 'aiutoma-select2'], filemtime(AIUTOMA_PATH . 'modules/playground/assets/js/automation.js'), true);
wp_localize_script('aiutoma-automation-script', 'aiutomaAutomationData', [
'restModelsUrl' => esc_url_raw(rest_url('aiutoma/v1/ai-models')),
'restSaveUrl' => esc_url_raw(rest_url('aiutoma/v1/automation/save')),
'restDeleteUrl' => esc_url_raw(rest_url('aiutoma/v1/automation/delete')),
'restRunUrl' => esc_url_raw(rest_url('aiutoma/v1/automation/run-now')),
'nonce' => wp_create_nonce('wp_rest')
]);
}
}
public function add_automation_menu()
{
add_submenu_page(
null,
__('Task Log', 'aiutoma'),
__('Task Log', 'aiutoma'),
'manage_options',
'aiutoma-automation-log',
[$this, 'aiutoma_automation_log_page_html']
);
}
public function register_automation_routes()
{
register_rest_route('aiutoma/v1', '/automation/run-now', [
'methods' => 'POST',
'callback' => [$this, 'run_task_now'],
'permission_callback' => function () {
return current_user_can('manage_options');
}
]);
}
public function aiutoma_automation_log_page_html()
{
if (!current_user_can('manage_options')) return;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading task ID parameter for view-only admin log page.
$id = isset($_GET['id']) ? sanitize_text_field(wp_unslash($_GET['id'])) : '';
$tasks = $this->_get_all_automated_tasks();
if (empty($id) || !isset($tasks[$id])) {
echo '' . esc_html__('Task Not Found', 'aiutoma') . '
';
return;
}
$task = $tasks[$id];
?>
←
[$1]", $raw_log);
// Style the timestamps with emphasis
$raw_log = preg_replace('/\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]/', '[$1]', $raw_log);
// Use a custom KSES array to strictly allow our classes
$allowed_html = array_merge(wp_kses_allowed_html('post'), [
'hr' => ['class' => true],
'span' => ['class' => true]
]);
echo wp_kses(trim($raw_log), $allowed_html);
?>
get_param('id');
$this->_execute_task($id);
return new \WP_REST_Response(['success' => true], 200);
}
private function _is_cron_due($expression, $last_run)
{
$parts = explode(' ', preg_replace('/\s+/', ' ', trim($expression)));
if (count($parts) !== 5) return false;
$now = time();
if ($now - $last_run < 50) return false;
if (empty($last_run)) return true; // First time execution
$match = function ($val, $current) {
if ($val === '*') return true;
if (strpos($val, '*/') === 0) {
$div = intval(substr($val, 2));
return $div > 0 && $current % $div === 0;
}
if (strpos($val, ',') !== false) {
$arr = explode(',', $val);
foreach ($arr as $v) {
if (is_numeric($v) && intval($v) === $current) return true;
}
return false;
}
if (is_numeric($val)) return intval($val) === $current;
return false;
};
// Align last run to the start of the next minute
$check_time = $last_run - ($last_run % 60) + 60;
// Cap the check at max 30 days to prevent infinite loops if last_run is very old
$max_time = min($now, $check_time + (30 * 86400));
while ($check_time <= $max_time) {
$c_min = intval(gmdate('i', $check_time));
$c_hour = intval(gmdate('H', $check_time));
$c_dom = intval(gmdate('d', $check_time));
$c_month = intval(gmdate('m', $check_time));
$c_dow = intval(gmdate('w', $check_time));
if (
$match($parts[0], $c_min) &&
$match($parts[1], $c_hour) &&
$match($parts[2], $c_dom) &&
$match($parts[3], $c_month) &&
$match($parts[4], $c_dow)
) {
return true;
}
$check_time += 60;
}
return false;
}
private function _get_next_run_time($task)
{
if (empty($task['active'])) return false;
$sched = $task['schedule'] ?? '';
$last_run = $task['last_run'] ?? 0;
$now = time();
if ($sched === 'once') {
$exec_time = $task['execute_time'] ?? 0;
if ($exec_time > $now) return $exec_time;
return false;
}
if (empty($last_run)) return $now;
if ($sched === 'hourly') {
return $last_run + 3600;
}
if ($sched === 'twicedaily') {
return $last_run + 43200;
}
if ($sched === 'daily') {
return $last_run + 86400;
}
if (!in_array($sched, ['hourly', 'twicedaily', 'daily', 'once'])) {
$parts = explode(' ', preg_replace('/\s+/', ' ', trim($sched)));
if (count($parts) !== 5) return false;
$match = function ($val, $current) {
if ($val === '*') return true;
if (strpos($val, '*/') === 0) {
$div = intval(substr($val, 2));
return $div > 0 && $current % $div === 0;
}
if (strpos($val, ',') !== false) {
$arr = explode(',', $val);
foreach ($arr as $v) {
if (is_numeric($v) && intval($v) === $current) return true;
}
return false;
}
if (is_numeric($val)) return intval($val) === $current;
return false;
};
$start = max($last_run + 60, $now);
$check_time = $start - ($start % 60);
$max_time = $check_time + (365 * 86400);
while ($check_time <= $max_time) {
$c_min = intval(gmdate('i', $check_time));
$c_hour = intval(gmdate('H', $check_time));
$c_dom = intval(gmdate('d', $check_time));
$c_month = intval(gmdate('m', $check_time));
$c_dow = intval(gmdate('w', $check_time));
if (
$match($parts[0], $c_min) &&
$match($parts[1], $c_hour) &&
$match($parts[2], $c_dom) &&
$match($parts[3], $c_month) &&
$match($parts[4], $c_dow)
) {
return $check_time;
}
$check_time += 60;
}
}
return false;
}
public function run_automated_tasks()
{
$tasks = $this->_get_all_automated_tasks();
if (empty($tasks)) return;
$now = time();
$intervals = [
'hourly' => 3600,
'twicedaily' => 43200,
'daily' => 86400,
'weekly' => 604800
];
foreach ($tasks as $id => $task) {
if (empty($task['active'])) continue;
if (isset($task['post_status']) && $task['post_status'] !== 'publish') continue;
$should_run = false;
// If it's currently running a long task, continue immediately
if (!empty($task['running_conversation_id'])) {
$should_run = true;
} else {
$sched = $task['schedule'];
if ($sched === 'once') {
if (!empty($task['execute_time']) && $now >= $task['execute_time'] && empty($task['last_run'])) {
$should_run = true;
}
} elseif (isset($intervals[$sched])) {
if ($now - $task['last_run'] >= $intervals[$sched]) {
$should_run = true;
}
} else {
$should_run = $this->_is_cron_due($sched, $task['last_run']);
}
}
if ($should_run) {
$this->_execute_task($id, true);
}
}
}
public function append_to_task_log($task_id, $message)
{
$tasks = $this->_get_all_automated_tasks();
if (isset($tasks[$task_id])) {
$task = $tasks[$task_id];
// Limit individual message length to prevent massive tool payloads from flooding the log
if (strlen($message) > 1500) {
$message = substr($message, 0, 1500) . "\n... [Message truncated due to length]";
}
$time = current_time('mysql');
$log_entry = "[$time] $message\n";
$current_log = $task['last_log'] ?? '';
// Prevent overall log from growing infinitely (keep last 50000 chars)
if (strlen($current_log) > 50000) {
$current_log = substr($current_log, 0, 50000) . "... [Truncated]";
}
$task['last_log'] = $log_entry . $current_log;
$this->_update_task_meta($task_id, $task);
}
}
public function aiutoma_test_recovery($task_id, $log_entry)
{
$tasks = $this->_get_all_automated_tasks();
if (isset($tasks[$task_id])) {
$task = $tasks[$task_id];
$task['is_running'] = false;
$task['running_conversation_id'] = '';
$task['running_action'] = '';
$current_log = $task['last_log'] ?? '';
if (strlen($current_log) > 50000) {
$current_log = substr($current_log, 0, 50000) . "... [Truncated]";
}
$task['last_log'] = $log_entry . $current_log;
$this->_update_task_meta($task_id, $task);
}
}
private function _execute_task($task_id, $is_cron = false)
{
$tasks = $this->_get_all_automated_tasks();
if (!isset($tasks[$task_id])) return false;
$task = $tasks[$task_id];
if (!class_exists('\WordPress\AiClient\AiClient')) return false;
$author_id = !empty($task['post_author']) ? (int) $task['post_author'] : (int) get_post_field('post_author', $task_id);
if (!$author_id) {
$author_id = 1;
}
$author = get_userdata($author_id);
if (!$author || !$author->has_cap('manage_options')) {
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Error: Task execution aborted. The task author is invalid or lacks administrative privileges (manage_options).\n" . ($tasks[$task_id]['last_log'] ?? '');
$this->_update_task_meta($task_id, $tasks[$task_id]);
return false;
}
// Establish the task author user context via WordPress core's determine_current_user hook
$determine_user_callback = function ($user_id) use ($author_id) {
return $author_id;
};
add_filter('determine_current_user', $determine_user_callback, 20);
global $current_user;
$prev_user = $current_user;
$current_user = null;
wp_get_current_user();
try {
// Crash detection
$crashed_last_time = !empty($task['is_running']);
// Lock the task
$tasks[$task_id]['is_running'] = true;
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
$is_resuming = !empty($task['running_conversation_id']);
$max_iterations = apply_filters('aiutoma_automation_max_iterations_per_tick', 3);
$conversation_id = $is_resuming ? $task['running_conversation_id'] : 'cron_' . $task_id;
$request = new \WP_REST_Request('POST', '/aiutoma/v1/ai-chat');
$request->set_param('conversation_id', $conversation_id);
if (!empty($task['model'])) {
$request->set_param('model', $task['model']);
}
if (!$is_resuming) {
$prompt = "CRON AUTOMATED TASK: " . $task['name'] . "\n\n" . $task['prompt'];
if ($crashed_last_time) {
$prompt .= "\n\n[SYSTEM NOTE: The previous execution encountered an error. Please use tools carefully to debug and resolve the issue.]";
}
$request->set_param('prompt', $prompt);
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Task Started.\n" . ($tasks[$task_id]['last_log'] ?? '');
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
} elseif (!empty($task['running_action']) && $task['running_action'] === 'tool_calls') {
$request->set_param('execute_tools', true);
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Resuming task execution (tool calls)...\n" . ($tasks[$task_id]['last_log'] ?? '');
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
} elseif (!empty($task['running_action']) && $task['running_action'] === 'broken_site') {
$request->set_param('prompt', "[CRITICAL ALERT]: Your last actions completed, but caused the entire website to return a 500 Fatal Error! Please review the changes you just made and fix the site immediately.");
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Resuming task execution (Recovery)...\n" . ($tasks[$task_id]['last_log'] ?? '');
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
}
$request->set_param('is_cron', true);
$response = $this->handle_chat_request($request);
$iterations = 0;
$is_done = false;
$last_action = '';
while ($iterations < $max_iterations && !is_wp_error($response) && $response instanceof \WP_REST_Response) {
$data = $response->get_data();
$last_action = $data['action'] ?? '';
if ($last_action === 'tool_calls') {
$iterations++;
if (!empty($data['tools'])) {
foreach ($data['tools'] as $tool) {
$tool_args = !empty($tool['args']) ? json_encode($tool['args']) : '';
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Executing tool: " . $tool['name'] . (!empty($tool_args) ? " (Args: " . $tool_args . ")" : "") . "\n" . ($tasks[$task_id]['last_log'] ?? '');
}
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
}
if ($iterations >= $max_iterations) {
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Yielding execution to next cron cycle to prevent timeouts...\n" . ($tasks[$task_id]['last_log'] ?? '');
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
break;
}
$exec_request = new \WP_REST_Request('POST', '/aiutoma/v1/ai-chat');
$exec_request->set_param('conversation_id', $conversation_id);
$exec_request->set_param('execute_tools', true);
$exec_request->set_param('is_cron', true);
$pause_setting = $task['pause_time'] ?? '2';
if ($pause_setting === 'random') {
sleep(wp_rand(1, 5));
} elseif (is_numeric($pause_setting) && intval($pause_setting) > 0) {
sleep(intval($pause_setting));
}
$response = $this->handle_chat_request($exec_request);
} else {
$is_done = true;
break;
}
}
$tasks = $this->_get_all_automated_tasks();
if (is_wp_error($response)) {
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Error: " . $response->get_error_message() . "\n" . ($tasks[$task_id]['last_log'] ?? '');
} else if ($response instanceof \WP_REST_Response) {
$data = $response->get_data();
if (isset($data['success']) && $data['success'] === false) {
$tasks[$task_id]['is_running'] = false;
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Error: " . ($data['message'] ?? 'Unknown API Error') . "\n" . ($tasks[$task_id]['last_log'] ?? '');
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
}
}
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
$transient_data = get_transient($conversation_id);
$has_error = false;
$error_msg = '';
if (is_wp_error($response)) {
$has_error = true;
$error_msg = $response->get_error_message();
} else if ($response instanceof \WP_REST_Response) {
$data = $response->get_data();
if (isset($data['success']) && $data['success'] === false) {
$has_error = true;
$error_msg = $data['message'] ?? 'Unknown API Error';
}
}
if ($transient_data || $has_error) {
$messages = [];
if ($transient_data) {
$json_data = json_decode($transient_data, true);
if (is_array($json_data)) {
foreach ($json_data as $msg_array) {
if (is_array($msg_array) && isset($msg_array['role'])) {
$messages[] = \WordPress\AiClient\Messages\DTO\Message::fromArray($msg_array);
}
}
}
} else {
if (class_exists('\WordPress\AiClient\Messages\DTO\UserMessage') && class_exists('\WordPress\AiClient\Messages\DTO\MessagePart')) {
$messages = [
new \WordPress\AiClient\Messages\DTO\UserMessage([
new \WordPress\AiClient\Messages\DTO\MessagePart($task['prompt'])
])
];
}
}
if (is_array($messages)) {
$html = '';
$first_prompt = $task['prompt'];
foreach ($messages as $msg) {
$role_obj = method_exists($msg, 'getRole') ? $msg->getRole() : '';
$role = (is_object($role_obj) && property_exists($role_obj, 'value')) ? $role_obj->value : (string) $role_obj;
$content = '';
if (method_exists($msg, 'getParts')) {
$parts = $msg->getParts();
foreach ($parts as $part) {
if (method_exists($part, 'getText') && (string)$part->getText() !== '') {
$content .= (string) $part->getText() . "\n";
} elseif (method_exists($part, 'getFunctionCall') && $part->getFunctionCall()) {
$func = $part->getFunctionCall();
$name = method_exists($func, 'getName') ? $func->getName() : '';
$args = method_exists($func, 'getArgs') ? json_encode($func->getArgs(), JSON_PRETTY_PRINT) : '';
$content .= "🛠️ **[Tool Execution]**: `$name`\n```json\n$args\n```\n";
} elseif (method_exists($part, 'getFunctionResponse') && $part->getFunctionResponse()) {
$resp = $part->getFunctionResponse();
$name = method_exists($resp, 'getName') ? $resp->getName() : '';
$res = method_exists($resp, 'getResponse') ? json_encode($resp->getResponse(), JSON_PRETTY_PRINT) : '';
$content .= "✅ **[Tool Response]**: `$name`\n```json\n$res\n```\n";
}
}
}
if ($role === 'user') {
$html .= 'You (Cron):
' . nl2br(esc_html($content)) . '
';
} elseif ($role === 'model') {
$display_text = $content;
if (class_exists('\League\CommonMark\CommonMarkConverter')) {
if (class_exists('\League\CommonMark\GithubFlavoredMarkdownConverter')) {
$converter = new \League\CommonMark\GithubFlavoredMarkdownConverter(['html_input' => 'escape', 'allow_unsafe_links' => false]);
} else {
$converter = new \League\CommonMark\CommonMarkConverter(['html_input' => 'escape', 'allow_unsafe_links' => false]);
}
$display_text = $converter->convert($content)->getContent();
} else {
$display_text = nl2br(esc_html($content));
}
$html .= 'AI:
' . $display_text . '
';
} elseif ($role === 'tool' || $role === 'function') {
$html .= '';
}
}
if ($has_error) {
$html .= 'Error:
' . esc_html($error_msg) . '
';
}
$upload_dir = wp_upload_dir();
$log_dir = \Aiutoma\Modules\Ai\Ai::get_storage_dir() . '/logs/sessions';
if (!is_dir($log_dir)) wp_mkdir_p($log_dir);
$file_path = $log_dir . '/' . sanitize_file_name($conversation_id) . '.json';
$sess_data = [
'is_full_state' => true,
'id' => sanitize_file_name($conversation_id),
'date' => current_time('mysql'),
'user' => 'Automated Task: ' . $task['name'],
'first_prompt' => $first_prompt,
'html' => $html,
'conversation_id' => $conversation_id,
'session_prompts' => [$first_prompt],
'raw_messages' => base64_encode(wp_json_encode($messages))
];
file_put_contents($file_path, json_encode($sess_data));
}
}
if (!$is_done && !is_wp_error($response) && $last_action === 'tool_calls') {
$tasks[$task_id]['running_conversation_id'] = $conversation_id;
$tasks[$task_id]['running_action'] = 'tool_calls';
$tasks[$task_id]['is_running'] = false;
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
} else {
$test_url = add_query_arg('aiutoma_test', time(), home_url());
$test_response = wp_remote_get($test_url, ['timeout' => 5]);
$is_error = is_wp_error($test_response);
$code = wp_remote_retrieve_response_code($test_response);
if ($is_error || $code >= 500) {
do_action('aiutoma_site_error_detected');
$tasks[$task_id]['running_conversation_id'] = $conversation_id;
$tasks[$task_id]['running_action'] = 'broken_site';
$tasks[$task_id]['is_running'] = false;
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] CRITICAL ERROR: The background task finished, but the website returned a 500 Error! Forcing an autonomous recovery iteration." . "\n" . ($tasks[$task_id]['last_log'] ?? '');
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
} else {
do_action('aiutoma_site_healthy');
unset($tasks[$task_id]['running_conversation_id']);
unset($tasks[$task_id]['running_action']);
$tasks[$task_id]['is_running'] = false; // Safely finished
// Only update last_run if it fully completed cleanly
$tasks[$task_id]['last_run'] = time();
$tasks[$task_id]['run_count'] = ($tasks[$task_id]['run_count'] ?? 0) + 1;
if ($tasks[$task_id]['schedule'] === 'once') {
$tasks[$task_id]['active'] = false;
}
if (!is_wp_error($response) && $response instanceof \WP_REST_Response) {
$data = $response->get_data();
if (!empty($data['response'])) {
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Task Completed Successfully.\n" . wp_kses_post(is_string($data['response']) ? $data['response'] : (isset($data['response']['text']) ? $data['response']['text'] : json_encode($data['response']))) . "\n" . ($tasks[$task_id]['last_log'] ?? '');
} elseif (!empty($data['message'])) {
$tasks[$task_id]['last_log'] = "[" . current_time('mysql') . "] Task Completed.\n" . wp_kses_post($data['message']) . "\n" . ($tasks[$task_id]['last_log'] ?? '');
}
}
if (isset($tasks[$task_id]['last_log']) && strlen($tasks[$task_id]['last_log']) > 50000) {
$tasks[$task_id]['last_log'] = substr($tasks[$task_id]['last_log'], 0, 50000) . "\n[Truncated...]";
}
$this->_update_task_meta($task_id, $tasks[$task_id]);
}
}
} finally {
remove_filter('determine_current_user', $determine_user_callback, 20);
global $current_user;
$current_user = $prev_user;
}
}
}