openai_client = $openai_client ?: new AIGH_OpenAI_Client(); $this->web_research = new AIGH_Web_Research($this->openai_client); } /** * Get default configuration * * @return array */ protected function get_default_config() { return array( 'use_research' => false, // Use research_brief from context if available 'temperature' => 0.7, 'max_tokens' => 4000 ); } /** * Execute the refinement step * * @param AIGH_Pipeline_Context $context * @return AIGH_Pipeline_Context|WP_Error */ public function execute($context) { $this->log_start($context); // Get original content $original_content = $this->get_from_context($context, 'content'); if (empty($original_content)) { $this->log_end($context, false); return $this->create_error('no_content', 'No content available to refine'); } // Get research context if available and enabled $research_context = ''; if ($this->get_config_value('use_research', false)) { $research_context = $this->get_from_context($context, 'research_brief', ''); } // Build the refinement prompt $system_prompt = $this->get_system_prompt(); $user_prompt = $this->build_user_prompt($original_content, $research_context, $context); // Execute refinement $refined_content = $this->openai_client->generate_text( $system_prompt, $user_prompt, $this->get_config_value('temperature', 0.7), $this->get_config_value('max_tokens', 4000) ); if (is_wp_error($refined_content)) { $this->log_end($context, false); return $refined_content; } // Update context with refined content $this->set_in_context($context, 'content', $refined_content); $this->set_in_context($context, 'refinement_applied', $this->step_type); $this->set_in_context($context, 'last_refinement', array( 'type' => $this->step_type, 'name' => $this->step_name, 'timestamp' => current_time('mysql'), 'used_research' => !empty($research_context) )); // Track refinement history $history = $this->get_from_context($context, 'refinement_history', array()); $history[] = array( 'type' => $this->step_type, 'name' => $this->step_name, 'timestamp' => current_time('mysql') ); $this->set_in_context($context, 'refinement_history', $history); $this->log_end($context, true); return $context; } /** * Get the system prompt for this refinement type * * @return string */ protected function get_system_prompt() { return 'You are a professional content editor and writer. Improve and refine existing content based on specific feedback while maintaining the original article structure and key points.'; } /** * Build the user prompt for this refinement * Must be implemented by each refinement step * * @param string $original_content * @param string $research_context * @param AIGH_Pipeline_Context $context * @return string */ abstract protected function build_user_prompt($original_content, $research_context, $context); /** * Get the refinement instruction text * Must be implemented by each refinement step * * @return string */ abstract protected function get_refinement_instruction(); /** * Build standard requirements section for prompt * * @param bool $has_research * @return string */ protected function get_standard_requirements($has_research = false) { $requirements = "Requirements:\n"; $requirements .= "- Maintain proper HTML formatting with

,

,

tags\n"; $requirements .= "- Keep the core message and main points\n"; if ($has_research) { $requirements .= "- Make SUBSTANTIAL changes using the research data\n"; $requirements .= "- Integrate research findings throughout the entire article\n"; $requirements .= "- Use specific numbers, names, and examples\n"; } $requirements .= "- Return ONLY the refined article content\n\n"; $requirements .= "Write the complete refined article:"; return $requirements; } /** * Set the OpenAI client (for dependency injection/testing) * * @param AIGH_OpenAI_Client $client */ public function set_openai_client($client) { $this->openai_client = $client; } /** * Get base configuration schema (refinement steps can extend this) * * @return array */ public function get_config_schema() { return array( 'use_research' => array( 'type' => 'checkbox', 'label' => 'Use Research Context', 'description' => 'Use research brief from pipeline context to enhance refinement', 'default' => false ), 'temperature' => array( 'type' => 'range', 'label' => 'Creativity', 'description' => 'Higher values = more creative changes', 'min' => 0.1, 'max' => 1.0, 'step' => 0.1, 'default' => 0.7 ) ); } }