| @@ -1,14 +1,17 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Desktop Mode — AI Copilot WP-Cron jobs. | |
| 3 | + * OpenStation — AI Copilot WP-Cron jobs. | |
| 4 | 4 | * |
| 5 | 5 | * Registers the async comment-analysis hook that scores a comment for |
| 6 | - * spam/harmful content and stores the verdict in comment meta. The job is | |
| 7 | - * scheduled by the Comments-window moderation feature (which is opt-in and | |
| 8 | - * off by default) with a short delay so it runs outside the HTTP request that | |
| 9 | - * triggered the comment. | |
| 6 | + * spam/harmful content and stores the verdict in comment meta. | |
| 10 | 7 | * |
| 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 | + * | |
| 11 | 14 | * Generation routes through the WordPress AI Client (`wp_ai_client_prompt()`), |
| 12 | 15 | * which sources credentials from Settings → Connectors — the copilot never |
| 13 | 16 | * handles an API key. |
| 14 | 17 | * |
| @@ -14,9 +17,9 @@ | ||
| 14 | 17 | * |
| 15 | 18 | * Hook name: |
| 16 | 19 | * desktop_mode_ai_analyze_comment ($comment_id, $user_id) |
| 17 | 20 | * |
| 18 | - * @package WPDesktopMode | |
| 21 | + * @package OpenStation | |
| 19 | 22 | */ |
| 20 | 23 | |
| 21 | 24 | defined( 'ABSPATH' ) || exit; |
| 22 | 25 | |
| @@ -29,16 +32,16 @@ | ||
| 29 | 32 | * |
| 30 | 33 | * @param int $comment_id The comment ID. |
| 31 | 34 | * @param int $user_id The user to attribute the request to. |
| 32 | 35 | */ |
| 33 | -function desktop_mode_ai_job_analyze_comment( $comment_id, $user_id ) { | |
| 36 | +function openstation_ai_job_analyze_comment( $comment_id, $user_id ) { | |
| 34 | 37 | $comment_id = (int) $comment_id; |
| 35 | 38 | $user_id = (int) $user_id; |
| 36 | 39 | |
| 37 | 40 | // No usable text-generation provider is configured in Connectors — nothing |
| 38 | - // to do. The scheduler already checks this, but the job runs async so we | |
| 39 | - // re-check to avoid emitting failed requests if the provider was removed. | |
| 40 | - if ( ! desktop_mode_ai_provider_configured() ) { | |
| 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() ) { | |
| 41 | 44 | return; |
| 42 | 45 | } |
| 43 | 46 | |
| 44 | 47 | $comment = get_comment( $comment_id ); |
| @@ -45,17 +48,17 @@ | ||
| 45 | 48 | if ( ! $comment instanceof WP_Comment ) { |
| 46 | 49 | return; |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | - $result = desktop_mode_ai_analyze_comment_now( $comment, $user_id ); | |
| 52 | + $result = openstation_ai_analyze_comment_now( $comment, $user_id ); | |
| 50 | 53 | |
| 51 | 54 | if ( is_wp_error( $result ) ) { |
| 52 | 55 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 53 | - 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() ); | |
| 54 | 57 | return; |
| 55 | 58 | } |
| 56 | 59 | |
| 57 | - desktop_mode_ai_save_meta( 'comment', $comment_id, $result ); | |
| 60 | + openstation_ai_save_meta( 'comment', $comment_id, $result ); | |
| 58 | 61 | |
| 59 | 62 | /** |
| 60 | 63 | * Fires after a comment has been successfully analyzed by the AI copilot. |
| 61 | 64 | * |
| @@ -66,29 +69,26 @@ | ||
| 66 | 69 | * @param int $comment_id The comment ID. |
| 67 | 70 | * @param array $result The structured analysis result. |
| 68 | 71 | * @param WP_Comment $comment The comment object. |
| 69 | 72 | */ |
| 70 | - do_action( 'desktop_mode_ai_comment_analyzed', $comment_id, $result, $comment ); | |
| 73 | + do_action( 'openstation_ai_comment_analyzed', $comment_id, $result, $comment ); | |
| 71 | 74 | } |
| 72 | -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 ); | |
| 73 | 76 | |
| 74 | 77 | /** |
| 75 | 78 | * Runs the structured comment analysis through the AI Client. |
| 76 | 79 | * |
| 77 | - * Converts the chat-style prompt from {@see desktop_mode_ai_messages_for_comment()} | |
| 80 | + * Converts the chat-style prompt from {@see openstation_ai_messages_for_comment()} | |
| 78 | 81 | * into a system instruction + user prompt, requests JSON constrained to the |
| 79 | 82 | * comment schema, and decodes the result. |
| 80 | 83 | * |
| 81 | 84 | * @param WP_Comment $comment The comment to analyze. |
| 82 | - * @param int $user_id Requesting user id. Currently unused — the builder | |
| 83 | - * is built from the prompt alone and the provider | |
| 84 | - * comes from Connectors; retained for signature | |
| 85 | - * stability and future attribution. | |
| 85 | + * @param int $user_id Requesting user id. | |
| 86 | 86 | * @return array|WP_Error Structured `{ topic, ai_summary, harmful, spam }` or an error. |
| 87 | 87 | */ |
| 88 | -function desktop_mode_ai_analyze_comment_now( WP_Comment $comment, $user_id ) { | |
| 89 | - $messages = desktop_mode_ai_messages_for_comment( $comment ); | |
| 90 | - $schema = desktop_mode_ai_schema_comment(); | |
| 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 | 91 | |
| 92 | 92 | $system = ''; |
| 93 | 93 | $prompt = ''; |
| 94 | 94 | foreach ( $messages as $message ) { |
| @@ -103,11 +103,22 @@ | ||
| 103 | 103 | $builder = wp_ai_client_prompt( $prompt ); |
| 104 | 104 | if ( '' !== $system ) { |
| 105 | 105 | $builder = $builder->using_system_instruction( $system ); |
| 106 | 106 | } |
| 107 | - // Provider + model are chosen by the Core AI Client; Desktop Mode pins neither. | |
| 107 | + // Provider + model are chosen by the Core AI Client unless the | |
| 108 | + // model-config filter says otherwise. | |
| 108 | 109 | |
| 109 | - $json = $builder->as_json_response( desktop_mode_ai_normalize_response_schema( $schema ) )->generate_text(); | |
| 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(); | |
| 110 | 121 | if ( is_wp_error( $json ) ) { |
| 111 | 122 | return $json; |
| 112 | 123 | } |
| 113 | 124 | |
| @@ -112,9 +123,9 @@ | ||
| 112 | 123 | } |
| 113 | 124 | |
| 114 | 125 | $result = json_decode( (string) $json, true ); |
| 115 | 126 | if ( ! is_array( $result ) ) { |
| 116 | - return new WP_Error( 'desktop_mode_ai_bad_json', 'The AI response was not valid JSON.' ); | |
| 127 | + return new WP_Error( 'openstation_ai_bad_json', 'The AI response was not valid JSON.' ); | |
| 117 | 128 | } |
| 118 | 129 | |
| 119 | 130 | return $result; |
| 120 | 131 | } |