' . 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)) { if (!in_array($apply_result->get_error_code(), ['aiwest_chat_decision','aiwest_chat_busy'], true)) 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 { $target=AIWEST_Security::post_int('aiwest_chat_target_id'); $operation=AIWEST_Security::post_key('aiwest_chat_operation','auto'); if (!in_array($operation,['auto','improve','rewrite','seo','faq','cta','visual'],true)) $operation='auto'; if ($mode!=='command') {$target=0;$operation='auto';} if ($target && (!in_array(get_post_type($target),['page','post'],true) || !current_user_can('edit_post',$target))) { $error=__('Choose an editable page or post.','ai-webmaster-studio'); $target=0; } if ($error) { $result=new WP_Error('aiwest_bad_target',$error); } $chat_id = $error?0:AIWEST_History::add_chat($message, $mode, $target, $operation); $notice = '

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

'; $context_message=$message; if ($target) { $target_post=get_post($target); $context_message.="\n\nSelected WordPress target: ".get_permalink($target)."\nRequested operation: ".$operation."\nTitle: ".$target_post->post_title."\nPage text excerpt (planning context only):\n".(function_exists('mb_substr')?mb_substr(wp_strip_all_tags($target_post->post_content),0,12000):substr(wp_strip_all_tags($target_post->post_content),0,12000)); } $messages = aiwest_chat_build_messages($mode, $context_message); if (!$error) $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)); ?>target_post_id)): ?>
target_post_id)); ?> ยท command_action)); ?>
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 a prompt in the administrator interface language 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 a prompt in the administrator interface language 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 === 'command') { $system = $base . "\n\nPrepare a concise proposed scope for an administrator-approved WordPress command. No changes have been applied yet. Respect the selected target and operation. Supported page actions are content improvement, explicit rewrite, SEO, FAQ, CTA and applying the current Design System while preserving text. Menu requests may add items and create missing pages as drafts. Never claim arbitrary theme-file edits, unsupported actions or successful execution. Ask for a target when it cannot be determined."; } 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], ]; }