Recent

Ask_HandlerclassPro only

Ask_Handler

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

Shared by the REST endpoint and the knowledgebase/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\Knowledge_Base\Pro\AI
Since: 3.2.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: 3.2.0
Line: 73

public function handle( $question ): array|\WP_Error;
Type Name Description
mixed $question Raw question.

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: 3.2.0
Line: 202

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: 3.2.0
Line: 231

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: 3.2.0
Line: 249

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: 3.2.0
Line: 330

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_busy or invalid_response.

Returns: array — {

get_search_url() static

Knowledge base search results URL for a question.

Since: 3.2.0
Line: 367

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

Returns: string — URL.