Recent

Ask_HandlerclassPro only

Ask_Handler

Runs a visitor question through the guards, cache and prompt service.

Shared by the REST endpoint and the better-search/ask ability, so both apply the same checks. A provider request is only made when the module is enabled, a provider is configured, the question is valid, the answer is not cached, and neither the visitor limit nor the daily cap has been reached.

Namespace: WebberZone\Better_Search\Pro\AI
Since: 4.5.0
Source: includes/pro/ai/class-ask-handler.php line 24

Methods

__construct()

Constructor.

Line: 60

public function __construct( AI $ai, ?Prompt_Service $service = ... );
Type Name Description
AI $ai AI module.
Prompt_Service|null $service Prompt service. Created on first use if null.

handle()

Handle a question.

Since: 4.5.0
Line: 74

public function handle( $question, array $context = ... ): array|\WP_Error;
Type Name Description
mixed $question Raw question.
array $context Public search context.

Returns: array|\WP_Error — Response data (see format_response()), or WP_Error with an HTTP status.

validate_question() static

Sanitise and validate a question.

Since: 4.5.0
Line: 237

static public function validate_question( $question ): string|\WP_Error;
Type Name Description
mixed $question Raw question.

Returns: string|\WP_Error — Clean question, or WP_Error with status 400.

normalise_question() static

Normalise a question for caching and grouping.

Since: 4.5.0
Line: 266

static public function normalise_question( string $question ): string;
Type Name Description
string $question Question.

Returns: string — Lower-case question with collapsed whitespace and no trailing punctuation.

canonical_question() static

Reduce a question to the words that decide its answer, for the answer cache.

Rephrasings such as “how does X work” and “how do Xs work please” share a key. Question words and negations are kept, so “why does X not work” stays apart from “why does X work”.

Since: 4.5.1
Line: 284

static public function canonical_question( string $question ): string;
Type Name Description
string $question Question.

Returns: string — Canonical question.

format_response() static

Build the response returned to visitors.

Since: 4.5.0
Line: 365

static public function format_response( string $question, array $result, bool $cached, ?string $reason = ... ): array;
Type Name Description
string $question Question.
array $result Result with answered, answer, sources and related.
bool $cached Whether the result came from the cache.
string|null $reason Why there is no answer: daily_cap, provider_error, provider_paused, provider_unavailable, provider_busy or invalid_response.

Returns: array — {

get_search_url() static

Knowledge base search results URL for a question.

Since: 4.5.0
Line: 402

static public function get_search_url( string $question ): string;
Type Name Description
string $question Question.

Returns: string — URL.