| @@ -1,21 +1,25 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — AI Copilot WP-Cron jobs. | |
| 3 | + * OpenStation — AI Copilot WP-Cron jobs. | |
| 4 | 4 | * |
| 5 | - * Registers the async comment-analysis hook that calls OpenAI and stores | |
| 6 | - * the spam/harmful verdict. The job is scheduled by the comment hook in | |
| 7 | - * `hooks.php` with a short delay so it runs outside the HTTP request that | |
| 8 | - * triggered the comment — keeping moderation snappy even when the OpenAI | |
| 9 | - * API is slow. | |
| 5 | + * Registers the async comment-analysis hook that scores a comment for | |
| 6 | + * spam/harmful content and stores the verdict in comment meta. | |
| 10 | 7 | * |
| 11 | - * Comment analysis is the only auto-analysis the copilot performs; posts, | |
| 12 | - * pages, and terms are no longer analyzed. | |
| 8 | + * Nothing in the plugin schedules this hook: automatic comment scoring was | |
| 9 | + * removed (`docs/migration-comments-ai-scoring.md`). The callback is kept | |
| 10 | + * because the hook name is a frozen identifier that a site may still have | |
| 11 | + * queued events for, and because an external plugin re-implementing the | |
| 12 | + * feature can schedule it rather than re-deriving the prompt and schema. | |
| 13 | 13 | * |
| 14 | + * Generation routes through the WordPress AI Client (`wp_ai_client_prompt()`), | |
| 15 | + * which sources credentials from Settings → Connectors — the copilot never | |
| 16 | + * handles an API key. | |
| 17 | + * | |
| 14 | 18 | * Hook name: |
| 15 | 19 | * desktop_mode_ai_analyze_comment ($comment_id, $user_id) |
| 16 | 20 | * |
| 17 | - * @package WPDesktopMode | |
| 21 | + * @package OpenStation | |
| 18 | 22 | */ |
| 19 | 23 | |
| 20 | 24 | defined( 'ABSPATH' ) || exit; |
| 21 | 25 | |
| @@ -25,18 +29,19 @@ | ||
| 25 | 29 | |
| 26 | 30 | /** |
| 27 | 31 | * Analyzes a comment and stores the result in comment meta. |
| 28 | 32 | * |
| 29 | - * @since 0.5.0 | |
| 30 | - * | |
| 31 | 33 | * @param int $comment_id The comment ID. |
| 32 | - * @param int $user_id The ID of the user whose API key to use. | |
| 34 | + * @param int $user_id The user to attribute the request to. | |
| 33 | 35 | */ |
| 34 | -function desktop_mode_ai_job_analyze_comment( $comment_id, $user_id ) { | |
| 36 | +function openstation_ai_job_analyze_comment( $comment_id, $user_id ) { | |
| 35 | 37 | $comment_id = (int) $comment_id; |
| 36 | 38 | $user_id = (int) $user_id; |
| 37 | 39 | |
| 38 | - if ( ! desktop_mode_ai_is_enabled( $user_id ) ) { | |
| 40 | + // No usable text-generation provider is configured in Connectors — nothing | |
| 41 | + // to do. Whoever queued the event may have checked already, but the job runs | |
| 42 | + // async, so re-check to avoid failed requests if the provider was removed. | |
| 43 | + if ( ! openstation_ai_provider_configured() ) { | |
| 39 | 44 | return; |
| 40 | 45 | } |
| 41 | 46 | |
| 42 | 47 | $comment = get_comment( $comment_id ); |
| @@ -43,21 +48,17 @@ | ||
| 43 | 48 | if ( ! $comment instanceof WP_Comment ) { |
| 44 | 49 | return; |
| 45 | 50 | } |
| 46 | 51 | |
| 47 | - $api_key = desktop_mode_ai_get_api_key( $user_id ); | |
| 48 | - $messages = desktop_mode_ai_messages_for_comment( $comment ); | |
| 49 | - $schema = desktop_mode_ai_schema_comment(); | |
| 52 | + $result = openstation_ai_analyze_comment_now( $comment, $user_id ); | |
| 50 | 53 | |
| 51 | - $result = desktop_mode_ai_provider_structured_request( $user_id, $api_key, $messages, $schema, 'comment_analysis' ); | |
| 52 | - | |
| 53 | 54 | if ( is_wp_error( $result ) ) { |
| 54 | 55 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 55 | - error_log( '[WP Desktop Mode AI] Comment ' . $comment_id . ' analysis failed: ' . $result->get_error_message() ); | |
| 56 | + error_log( '[WP OpenStation AI] Comment ' . $comment_id . ' analysis failed: ' . $result->get_error_message() ); | |
| 56 | 57 | return; |
| 57 | 58 | } |
| 58 | 59 | |
| 59 | - desktop_mode_ai_save_meta( 'comment', $comment_id, $result ); | |
| 60 | + openstation_ai_save_meta( 'comment', $comment_id, $result ); | |
| 60 | 61 | |
| 61 | 62 | /** |
| 62 | 63 | * Fires after a comment has been successfully analyzed by the AI copilot. |
| 63 | 64 | * |
| @@ -64,13 +65,67 @@ | ||
| 64 | 65 | * The `$result` array always contains `topic`, `ai_summary`, `harmful`, |
| 65 | 66 | * and `spam`. Downstream plugins (e.g. a moderation helper) can act on |
| 66 | 67 | * `harmful` or `spam` here rather than polling meta. |
| 67 | 68 | * |
| 68 | - * @since 0.5.0 | |
| 69 | - * | |
| 70 | 69 | * @param int $comment_id The comment ID. |
| 71 | 70 | * @param array $result The structured analysis result. |
| 72 | 71 | * @param WP_Comment $comment The comment object. |
| 73 | 72 | */ |
| 74 | - do_action( 'desktop_mode_ai_comment_analyzed', $comment_id, $result, $comment ); | |
| 73 | + do_action( 'openstation_ai_comment_analyzed', $comment_id, $result, $comment ); | |
| 75 | 74 | } |
| 76 | -add_action( 'desktop_mode_ai_analyze_comment', 'desktop_mode_ai_job_analyze_comment', 10, 2 ); | |
| 75 | +add_action( 'desktop_mode_ai_analyze_comment', 'openstation_ai_job_analyze_comment', 10, 2 ); | |
| 76 | + | |
| 77 | +/** | |
| 78 | + * Runs the structured comment analysis through the AI Client. | |
| 79 | + * | |
| 80 | + * Converts the chat-style prompt from {@see openstation_ai_messages_for_comment()} | |
| 81 | + * into a system instruction + user prompt, requests JSON constrained to the | |
| 82 | + * comment schema, and decodes the result. | |
| 83 | + * | |
| 84 | + * @param WP_Comment $comment The comment to analyze. | |
| 85 | + * @param int $user_id Requesting user id. | |
| 86 | + * @return array|WP_Error Structured `{ topic, ai_summary, harmful, spam }` or an error. | |
| 87 | + */ | |
| 88 | +function openstation_ai_analyze_comment_now( WP_Comment $comment, $user_id ) { | |
| 89 | + $messages = openstation_ai_messages_for_comment( $comment ); | |
| 90 | + $schema = openstation_ai_schema_comment(); | |
| 91 | + | |
| 92 | + $system = ''; | |
| 93 | + $prompt = ''; | |
| 94 | + foreach ( $messages as $message ) { | |
| 95 | + $role = isset( $message['role'] ) ? $message['role'] : ''; | |
| 96 | + if ( 'system' === $role ) { | |
| 97 | + $system = (string) $message['content']; | |
| 98 | + } elseif ( 'user' === $role ) { | |
| 99 | + $prompt = (string) $message['content']; | |
| 100 | + } | |
| 101 | + } | |
| 102 | + | |
| 103 | + $builder = wp_ai_client_prompt( $prompt ); | |
| 104 | + if ( '' !== $system ) { | |
| 105 | + $builder = $builder->using_system_instruction( $system ); | |
| 106 | + } | |
| 107 | + // Provider + model are chosen by the Core AI Client unless the | |
| 108 | + // model-config filter says otherwise. | |
| 109 | + | |
| 110 | + $builder = $builder->as_json_response( openstation_ai_normalize_response_schema( $schema ) ); | |
| 111 | + $builder = openstation_ai_apply_model_config( | |
| 112 | + $builder, | |
| 113 | + array( | |
| 114 | + 'user_id' => (int) $user_id, | |
| 115 | + 'source' => 'ai-copilot/comment-analysis', | |
| 116 | + 'has_schema' => true, | |
| 117 | + ) | |
| 118 | + ); | |
| 119 | + | |
| 120 | + $json = $builder->generate_text(); | |
| 121 | + if ( is_wp_error( $json ) ) { | |
| 122 | + return $json; | |
| 123 | + } | |
| 124 | + | |
| 125 | + $result = json_decode( (string) $json, true ); | |
| 126 | + if ( ! is_array( $result ) ) { | |
| 127 | + return new WP_Error( 'openstation_ai_bad_json', 'The AI response was not valid JSON.' ); | |
| 128 | + } | |
| 129 | + | |
| 130 | + return $result; | |
| 131 | +} | |