add_filter( "mwai_context_search", 'my_page_search', 5, 3 );
function my_page_search( $context, $query, $options = [] ) {
$site_url_regex = "/https?:\/\/[\S]+/i";
if (preg_match($site_url_regex, $query->get_message(), $matches)) {
$site_url = $matches[0]; // The first URL in the message.
$command = "pandoc -s -r html $site_url -t plain --no-highlight";
$output = shell_exec($command);
} else {
return null;
}
// Check if command executed successfully.
if (isset($output) && $output === null) {
return null;
}
// AI Engine expects a type (for logging purposes) and the content (which will be used by AI).
$context["type"] = "sitesearch";
$context["content"] = isset($output) ? $output : '';
// Debug info saved to PHP error.log (comment out if not required)
error_log(print_r("DEBUG: final context", true));
error_log(print_r($context, true));
return $context;
}