type ) { return AgentResult::refused( 'unsupported task type' ); } $query = trim( (string) ( $task->payload['query'] ?? '' ) ); if ( '' === $query ) { return AgentResult::failed( 'empty query' ); } $lang = (string) ( $task->payload['lang'] ?? '' ); $limit = max( 1, min( 20, (int) ( $task->payload['limit'] ?? 8 ) ) ); try { $results = $this->retriever->search( $query, array( 'lang' => $lang, 'limit' => $limit, ) ); } catch ( Throwable $e ) { $context->journal()->error( self::ID, $e, $task ); return AgentResult::failed( 'search error: ' . $e->getMessage() ); } $top_score = $results ? (float) $results[0]['score'] : 0.0; $confidence = min( 1.0, $top_score / 8 ); $needs = empty( $results ) || $top_score < 1; return new AgentResult( AgentResult::STATUS_OK, array( 'count' => count( $results ), 'top_score' => $top_score, 'results' => $results, ), $confidence, array(), $needs ); } /** * {@inheritDoc} */ public function self_check( AgentContext $context ): HealthReport { global $wpdb; $report = new HealthReport(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $terms_table = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->prefix . 'agyl_kb_terms' ) ); $report->add( 'terms_table_exists', ! empty( $terms_table ), '', true ); return $report; } }