' . esc_html(__('The conversation history entry was deleted.', 'ai-webmaster-studio')) . '

'; } if ($action === 'approve_chat') { $chat_id = AIWEST_Security::post_int('chat_id'); if ($chat_id > 0 && method_exists('AIWEST_History', 'approve_chat')) { $apply_result = class_exists('AIWEST_Chat_Executor') ? AIWEST_Chat_Executor::apply_chat($chat_id) : ['applied' => false, 'note' => __('The technical executor is unavailable. No website changes were made.', 'ai-webmaster-studio')]; if (is_wp_error($apply_result)) { AIWEST_History::record_chat_decision($chat_id, 'failed', __('Could not apply:', 'ai-webmaster-studio') . ' ' . $apply_result->get_error_message()); $notice = '

' . esc_html(__('The command was approved but could not be applied:', 'ai-webmaster-studio')) . ' ' . esc_html($apply_result->get_error_message()) . '

'; } elseif (empty($apply_result['applied'])) { $note = $apply_result['note'] ?? __('The decision was recorded without making website changes.', 'ai-webmaster-studio'); AIWEST_History::record_chat_decision($chat_id, 'recorded', $note); $notice = '

' . esc_html($note) . '

'; } else { $note = $apply_result['note'] ?? __('Command approved and applied.', 'ai-webmaster-studio'); if (!empty($apply_result['menu_link'])) { $note .= ' Menu: ' . esc_url_raw($apply_result['menu_link']); } AIWEST_History::approve_chat($chat_id, $note); $extra = !empty($apply_result['menu_link']) ? ' ' . esc_html(__('Go to menu', 'ai-webmaster-studio')) . '' : ''; if (!empty($apply_result['edit_link'])) { $extra .= ' ' . esc_html(__('Edit content', 'ai-webmaster-studio')) . ''; } $notice = '

' . esc_html(__('The command was approved and applied.', 'ai-webmaster-studio')) . ' ' . esc_html($note) . $extra . '

'; } } } if ($action === 'send') { $message = AIWEST_Security::post_textarea('aiwest_chat_message'); $mode = AIWEST_Security::post_key('aiwest_chat_mode', 'question'); if (!$current_key) { $error = __('No OpenAI API key is saved. Open AI Webmaster Studio → Settings and run diagnostics.', 'ai-webmaster-studio'); } elseif (!$message) { $error = __('Enter a message for AI.', 'ai-webmaster-studio'); } else { $chat_id = AIWEST_History::add_chat($message, $mode); $notice = '

' . esc_html(__('The command was accepted by AI Webmaster Studio. Conversation ID:', 'ai-webmaster-studio')) . ' ' . intval($chat_id) . '

'; $messages = aiwest_chat_build_messages($mode, $message); $result = AIWEST_OpenAI::chat($messages, 0.25); if (is_wp_error($result)) { $error = $result->get_error_message(); AIWEST_History::update_chat($chat_id, 'error', '', $error); if (class_exists('AIWEST_Tasks')) { AIWEST_Tasks::add('chat_error', ['message' => $message, 'error' => $error, 'chat_id' => $chat_id], 'safe'); } } else { $answer = $result; AIWEST_History::update_chat($chat_id, 'completed', $answer, ''); if ($mode === 'command' && method_exists('AIWEST_History', 'mark_chat_needs_approval')) { AIWEST_History::mark_chat_needs_approval($chat_id, __('The Chat command is waiting for an administrator decision.', 'ai-webmaster-studio')); } if (class_exists('AIWEST_Tasks')) { AIWEST_Tasks::add('chat_interaction', ['message' => $message, 'answer_excerpt' => wp_trim_words(wp_strip_all_tags($answer), 60, '...'), 'chat_id' => $chat_id], 'safe'); } $notice = '

' . esc_html(__('AI responded successfully. The conversation was saved in history. Conversation ID:', 'ai-webmaster-studio')) . ' ' . intval($chat_id) . '

'; } } } } $history = AIWEST_History::recent_chat(50); ?>

approval_status) ? $h->approval_status : 'not_required'; $approval_note = isset($h->approval_note) ? $h->approval_note : ''; $applied_at = isset($h->applied_at) ? $h->applied_at : ''; ?>
ID
id); ?> created_at); ?> mode); ?> user_message), 18, '...')); ?> user_message)): ?>
user_message); ?>
status); ?> model)): ?>
model); ?>
status === 'error'): ?> error_message); ?> ai_response), 24, '...')); ?> ai_response)): ?>
ai_response); ?>

mode, ['page_prompt', 'site_builder_prompt'], true)): ?>

$audit['generated_at'] ?? '', 'summary' => $audit['summary'] ?? [], 'seo' => $audit['seo'] ?? [], 'recommendations' => array_slice((array)($audit['recommendations'] ?? []), 0, 8), 'weak_pages' => array_slice((array)($audit['weak_pages'] ?? []), 0, 10), 'implementation_guidance' => $audit['recommendations'] ?? [], ]; } function aiwest_chat_build_messages($mode, $message) { $mode = sanitize_key($mode ?: 'question'); $message = (string)$message; $base = AIWEST_OpenAI::system_prompt(); $site_context = [ 'site_name' => get_bloginfo('name'), 'site_url' => home_url('/'), 'last_site_audit_summary' => aiwest_chat_site_audit_context(), ]; if ($mode === 'page_prompt') { $system = $base . "\n\nCreate an English prompt for editing, generating or improving a WordPress page/post with AI Webmaster Studio. Include audience, page goal, structure, SEO hints, CTA, internal links and quality rules. Return a ready-to-use prompt."; } elseif ($mode === 'site_builder_prompt') { $system = $base . "\n\nCreate an English prompt for AI Webmaster Studio Site Builder work. The user may request homepage, menu, header, footer, subpages, appearance or graphics. Respect AI Policies, avoid unrelated content growth, and specify whether the prompt is menu-only, header-only, footer-only, homepage-only or full-site. Return a ready-to-use prompt."; } elseif ($mode === 'strategy') { $system = $base . "\n\nAnswer as a product and website-growth strategist for AI Webmaster Studio. Give practical recommendations that an administrator can apply with the plugin modules."; } else { $system = $base . "\n\nAnswer inside AI Webmaster Studio admin. Keep the response practical. Explain which module the administrator should use and what must be reviewed before applying changes."; } return [ ['role' => 'system', 'content' => $system], ['role' => 'user', 'content' => "Site context:\n" . wp_json_encode($site_context, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n\nAdministrator message:\n" . $message], ]; }