| 1 |
<?php |
| 2 |
/** |
| 3 |
* SingleShotAIService — Handles single-turn AI tasks without agent loop history. |
| 4 |
* |
| 5 |
* @package WPFunnels\AI |
| 6 |
* @since 3.13.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPFunnels\AI; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
use Exception; |
| 14 |
use WP_Error; |
| 15 |
use WPFunnels\AI\Settings\AISettings; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class SingleShotAIService |
| 19 |
*/ |
| 20 |
class SingleShotAIService { |
| 21 |
|
| 22 |
/** |
| 23 |
* Registry of single-shot tasks. |
| 24 |
* |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
public static function taskRegistry() { |
| 28 |
return [ |
| 29 |
'headline' => [ |
| 30 |
'description' => 'Generate high-converting headline', |
| 31 |
'prompt' => 'Generate 3 high-converting headline variations for this product/offer.', |
| 32 |
], |
| 33 |
'subheadline' => [ |
| 34 |
'description' => 'Generate supporting subheadline', |
| 35 |
'prompt' => 'Generate 3 supporting subheadline options explaining the value proposition.', |
| 36 |
], |
| 37 |
'cta_text' => [ |
| 38 |
'description' => 'Generate action-oriented CTA button text (<=5 words)', |
| 39 |
'prompt' => 'Generate 5 short action-oriented CTA button text options (under 5 words).', |
| 40 |
], |
| 41 |
'rewrite_text' => [ |
| 42 |
'description' => 'Rewrite text for tone, brevity, or clarity', |
| 43 |
'prompt' => 'Rewrite the provided copy for enhanced clarity, urgency, and conversion power.', |
| 44 |
], |
| 45 |
'step_copy' => [ |
| 46 |
'description' => 'Generate full copy set for a step type', |
| 47 |
'prompt' => 'Generate headline, subheadline, benefits list, and CTA text tailored for this step type.', |
| 48 |
], |
| 49 |
'funnel_name' => [ |
| 50 |
'description' => 'Generate marketing funnel name and goal description', |
| 51 |
'prompt' => 'Suggest 3 catchy sales funnel titles and concise goal statements.', |
| 52 |
], |
| 53 |
'order_bump_copy' => [ |
| 54 |
'description' => 'Generate order bump headline, checkbox label, and offer summary', |
| 55 |
'prompt' => 'Generate order bump title, checkbox offer label, and brief 2-sentence description.', |
| 56 |
], |
| 57 |
'offer_copy' => [ |
| 58 |
'description' => 'Generate upsell or downsell offer page copy', |
| 59 |
'prompt' => 'Generate headline, value stack bullet points, accept button text, and decline button text.', |
| 60 |
], |
| 61 |
'summarize_funnel_analytics' => [ |
| 62 |
'description' => 'Narrative summary of funnel analytics performance', |
| 63 |
'prompt' => 'Summarize funnel analytics performance data into a concise marketing insight report.', |
| 64 |
], |
| 65 |
'summarize_store' => [ |
| 66 |
'description' => 'Narrative summary of store catalog and funnel recommendations', |
| 67 |
'prompt' => 'Summarize store product catalog and outline recommended funnel strategy.', |
| 68 |
], |
| 69 |
'ab_test_hypothesis' => [ |
| 70 |
'description' => 'Formulate A/B test hypotheses for page elements', |
| 71 |
'prompt' => 'Suggest 3 high-impact A/B test variant hypotheses based on performance metrics.', |
| 72 |
], |
| 73 |
'extract_business_info' => [ |
| 74 |
'description' => 'Extract business profile fields from text/URL', |
| 75 |
'prompt' => 'Extract business name, industry niche, target audience, and primary offer in structured format.', |
| 76 |
], |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Run a single-shot AI task. |
| 82 |
* |
| 83 |
* @param string $task Task identifier. |
| 84 |
* @param string $prompt Input prompt text. |
| 85 |
* @param array $params Additional task parameters. |
| 86 |
* @return array|\WP_Error |
| 87 |
*/ |
| 88 |
public static function executeTask( $task, $prompt, $params = [] ) { |
| 89 |
$registry = self::taskRegistry(); |
| 90 |
|
| 91 |
$task_info = isset( $registry[ $task ] ) ? $registry[ $task ] : null; |
| 92 |
$instruction = $task_info ? $task_info['prompt'] : 'Process the task as requested.'; |
| 93 |
$provider = AIInit::activeProvider(); |
| 94 |
|
| 95 |
if ( ! $provider ) { |
| 96 |
return new WP_Error( 'ai_disabled', __( 'No active AI provider configured.', 'wpfnl' ), [ 'status' => 400 ] ); |
| 97 |
} |
| 98 |
|
| 99 |
$system_prompt = "You are WPFunnels AI Copilot. Execute the requested single-shot task efficiently.\n" . |
| 100 |
"Task Instruction: {$instruction}\n" . |
| 101 |
AISettings::getCustomInstructions(); |
| 102 |
|
| 103 |
$messages = [ |
| 104 |
[ |
| 105 |
'role' => 'user', |
| 106 |
'content' => json_encode( |
| 107 |
[ |
| 108 |
'task' => $task, |
| 109 |
'input' => $prompt, |
| 110 |
'params' => $params, |
| 111 |
] |
| 112 |
), |
| 113 |
], |
| 114 |
]; |
| 115 |
|
| 116 |
try { |
| 117 |
$res = $provider->chat( $system_prompt, $messages, [] ); |
| 118 |
return [ |
| 119 |
'success' => true, |
| 120 |
'task' => $task, |
| 121 |
'result' => $res['text'], |
| 122 |
]; |
| 123 |
} catch ( Exception $e ) { |
| 124 |
return new WP_Error( 'task_execution_failed', $e->getMessage(), [ 'status' => 500 ] ); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|