Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions admin/views/embeddings.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,35 +101,35 @@ class="regular-text">
$db_type = $database->get_db_type();
$has_vector_support = $database->has_native_vector_support() ? 'Yes' : 'No';

error_log( '[WPVDB DEBUG] Performing semantic search for query: ' . $search_query );
error_log( '[WPVDB DEBUG] API Key exists: ' . ( ! empty( $api_key ) ? 'Yes' : 'No' ) );
error_log( '[WPVDB DEBUG] Model: ' . $model );
error_log( '[WPVDB DEBUG] API Base: ' . $api_base );
\WPVDB\Logger::debug( 'Performing semantic search for query: ' . $search_query );
\WPVDB\Logger::debug( 'API key exists: ' . ( ! empty( $api_key ) ? 'Yes' : 'No' ) );
\WPVDB\Logger::debug( 'Model: ' . $model );
\WPVDB\Logger::debug( 'API base: ' . $api_base );

if ( $api_key && $model ) {
try {
$embedding_result = \WPVDB\Core::get_embedding( $search_query, $model, $api_base, $api_key );

if ( is_wp_error( $embedding_result ) ) {
error_log( '[WPVDB ERROR] Error getting embedding: ' . $embedding_result->get_error_message() );
\WPVDB\Logger::error( 'Error getting embedding: ' . $embedding_result->get_error_message() );
} else {
error_log( '[WPVDB DEBUG] Successfully generated embedding with dimensions: ' . count( $embedding_result ) );
\WPVDB\Logger::debug( 'Successfully generated embedding with dimensions: ' . count( $embedding_result ) );

$embedding = $embedding_result;
$has_vector = $database->has_native_vector_support();
error_log( '[WPVDB DEBUG] Database has native vector support: ' . ( $has_vector ? 'Yes' : 'No' ) );
\WPVDB\Logger::debug( 'Database has native vector support: ' . ( $has_vector ? 'Yes' : 'No' ) );

if ( $has_vector ) {
// Convert the embedding array to JSON.
$embedding_json = json_encode( $embedding );
$embedding_json = wp_json_encode( $embedding );

// Use Database class to get the appropriate vector function.
$vector_function = $database->get_vector_from_string_function( $embedding_json );
error_log( '[WPVDB DEBUG] Using vector function: ' . $vector_function );
\WPVDB\Logger::debug( 'Using vector function: ' . $vector_function );

// Get total count of vectors.
$total_vectors_searched = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
error_log( '[WPVDB DEBUG] Total vectors searched: ' . $total_vectors_searched );
\WPVDB\Logger::debug( 'Total vectors searched: ' . $total_vectors_searched );

// Use Database class to get the appropriate distance function with both vectors.
$db_type = $database->get_db_type();
Expand All @@ -138,7 +138,7 @@ class="regular-text">
} else {
$distance_function = "DISTANCE(e.embedding, $vector_function, 'COSINE')";
}
error_log( '[WPVDB DEBUG] Using distance function: ' . $distance_function );
\WPVDB\Logger::debug( 'Using distance function: ' . $distance_function );

// Optimized query that will use the vector index.
// The ORDER BY + LIMIT pattern is what triggers the vector index usage.
Expand All @@ -153,34 +153,34 @@ class="regular-text">
20 // Show top 20 matches.
);

error_log( '[WPVDB DEBUG] Executing SQL query: ' . $sql );
\WPVDB\Logger::debug( 'Executing SQL query: ' . $sql );

$search_results = $wpdb->get_results( $sql );

if ( $wpdb->last_error ) {
error_log( '[WPVDB ERROR] SQL error: ' . $wpdb->last_error );
\WPVDB\Logger::error( 'SQL error: ' . $wpdb->last_error );

// Try executing a simpler query to test database connection.
$test_query = "SELECT COUNT(*) FROM $table_name";
$test_result = $wpdb->get_var( $test_query );

if ( $wpdb->last_error ) {
error_log( '[WPVDB ERROR] Even simple query failed: ' . $wpdb->last_error );
\WPVDB\Logger::error( 'Even simple query failed: ' . $wpdb->last_error );
} else {
error_log( '[WPVDB DEBUG] Simple query succeeded, embedding count: ' . $test_result );
\WPVDB\Logger::debug( 'Simple query succeeded, embedding count: ' . $test_result );

// Try a direct query without the vector function to see if that's the issue.
$basic_query = "SELECT e.* FROM $table_name e LIMIT 20";
$basic_results = $wpdb->get_results( $basic_query );

if ( $wpdb->last_error ) {
error_log( '[WPVDB ERROR] Basic query failed: ' . $wpdb->last_error );
\WPVDB\Logger::error( 'Basic query failed: ' . $wpdb->last_error );
} else {
error_log( '[WPVDB DEBUG] Basic query succeeded, returned ' . count( $basic_results ) . ' results' );
error_log( '[WPVDB DEBUG] Issue is likely with the vector function: ' . $distance_function );
\WPVDB\Logger::debug( 'Basic query succeeded, returned ' . count( $basic_results ) . ' results' );
\WPVDB\Logger::debug( 'Issue is likely with the vector function: ' . $distance_function );

// Fall back to PHP-based distance calculation.
error_log( '[WPVDB DEBUG] Falling back to PHP-based distance calculation' );
\WPVDB\Logger::debug( 'Falling back to PHP-based distance calculation' );
$all_rows = $wpdb->get_results(
$wpdb->prepare( "SELECT * FROM $table_name WHERE model = %s", $model ),
ARRAY_A
Expand All @@ -205,37 +205,37 @@ function ( $a, $b ) {
);

$search_results = array_slice( $distances, 0, 20 );
$search_results = json_decode( json_encode( $search_results ) ); // Convert to objects.
$search_results = json_decode( wp_json_encode( $search_results ) ); // Convert to objects.

error_log( '[WPVDB DEBUG] PHP fallback found ' . count( $search_results ) . ' results' );
\WPVDB\Logger::debug( 'PHP fallback found ' . count( $search_results ) . ' results' );
}
}
} else {
error_log( '[WPVDB DEBUG] Found ' . count( $search_results ) . ' results' );
\WPVDB\Logger::debug( 'Found ' . count( $search_results ) . ' results' );
if ( count( $search_results ) > 0 ) {
error_log(
'[WPVDB DEBUG] First result distance: ' .
\WPVDB\Logger::debug(
'First result distance: ' .
( isset( $search_results[0]->distance ) ?
$search_results[0]->distance : 'Not set' )
);
}
}
} else {
// Fallback: do in PHP.
error_log( '[WPVDB DEBUG] Using PHP fallback search' );
\WPVDB\Logger::debug( 'Using PHP fallback search' );
$all_rows = $wpdb->get_results(
$wpdb->prepare( "SELECT * FROM $table_name WHERE model = %s", $model ),
ARRAY_A
);
$total_vectors_searched = count( $all_rows );
error_log( '[WPVDB DEBUG] Total vectors searched: ' . $total_vectors_searched );
\WPVDB\Logger::debug( 'Total vectors searched: ' . $total_vectors_searched );

$distances = array();

foreach ( $all_rows as $r ) {
$stored_emb = json_decode( $r['embedding'], true );
if ( ! is_array( $stored_emb ) ) {
error_log( '[WPVDB DEBUG] Invalid embedding in row: ' . $r['id'] );
\WPVDB\Logger::debug( 'Invalid embedding in row: ' . $r['id'] );
continue;
}
$similarity_score = \WPVDB\REST::cosine_distance( $embedding, $stored_emb );
Expand All @@ -251,12 +251,12 @@ function ( $a, $b ) {
);

$search_results = array_slice( $distances, 0, 20 );
$search_results = json_decode( json_encode( $search_results ) ); // Convert to objects.
$search_results = json_decode( wp_json_encode( $search_results ) ); // Convert to objects.

error_log( '[WPVDB DEBUG] PHP fallback found ' . count( $search_results ) . ' results' );
\WPVDB\Logger::debug( 'PHP fallback found ' . count( $search_results ) . ' results' );
if ( count( $search_results ) > 0 ) {
error_log(
'[WPVDB DEBUG] First result similarity score: ' .
\WPVDB\Logger::debug(
'First result similarity score: ' .
( isset( $search_results[0]->distance ) ?
$search_results[0]->distance : 'Not set' )
);
Expand All @@ -271,11 +271,11 @@ function ( $a, $b ) {
}
} catch ( \Exception $e ) {
// Handle errors.
error_log( '[WPVDB ERROR] Exception: ' . $e->getMessage() );
\WPVDB\Logger::error( 'Exception: ' . $e->getMessage() );
echo '<div class="notice notice-error"><p>' . esc_html__( 'Error performing semantic search: ', 'wpvdb' ) . esc_html( $e->getMessage() ) . '</p></div>';
}
} else {
error_log( '[WPVDB ERROR] API key or model not configured' );
\WPVDB\Logger::error( 'API key or model not configured' );
echo '<div class="notice notice-warning"><p>' . esc_html__( 'API key or model not configured. Please check your settings.', 'wpvdb' ) . '</p></div>';
}
}
Expand Down Expand Up @@ -323,7 +323,7 @@ function ( $a, $b ) {
if ( $total_embeddings > 0 ) {
$embeddings_query = "SELECT * FROM $table_name ORDER BY id DESC LIMIT 20";
$embeddings = $wpdb->get_results( $embeddings_query );
error_log( '[WPVDB DEBUG] Loaded ' . count( $embeddings ) . ' embeddings for display' );
\WPVDB\Logger::debug( 'Loaded ' . count( $embeddings ) . ' embeddings for display' );
}
}
?>
Expand Down Expand Up @@ -410,7 +410,7 @@ function ( $a, $b ) {
} else {
// If no distance property, check what properties are available.
$props = array_keys( get_object_vars( $embedding ) );
error_log( '[WPVDB DEBUG] Properties available: ' . print_r( $props, true ) );
\WPVDB\Logger::debug( 'Properties available: ' . wp_json_encode( $props ) );

echo esc_html__( 'No similarity data available', 'wpvdb' );
}
Expand Down
4 changes: 2 additions & 2 deletions admin/views/status.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@
$debug_settings['automattic']['api_key'] = '********' . substr( $debug_settings['automattic']['api_key'], -4 );
}

echo esc_html( print_r( $debug_settings, true ) );
echo esc_html( wp_json_encode( $debug_settings, JSON_PRETTY_PRINT ) );
?>
</pre>

Expand All @@ -480,7 +480,7 @@
'embedding_table_exists' => $embedding_table_exists,
'embedding_count' => $system_info['embedding_count'],
);
echo esc_html( print_r( $db_info, true ) );
echo esc_html( wp_json_encode( $db_info, JSON_PRETTY_PRINT ) );
?>
</pre>

Expand Down
51 changes: 19 additions & 32 deletions includes/class-wpvdb-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public static function activate() {
update_option( 'wpvdb_incompatible_db', true );

// Log the incompatible activation.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Activated on incompatible database. Vector features require MySQL 8.0.32+ or MariaDB 11.7+.' ); }
Logger::warning( 'Activated on incompatible database. Vector features require MySQL 8.0.32+ or MariaDB 11.7+.' );

// Restore error reporting and return early (we'll show the warning later).
error_reporting( $old_error_reporting );
Expand Down Expand Up @@ -222,8 +221,7 @@ public static function add_vector_index_to_existing_table() {

// Only proceed if database is ready and we've initialized properly.
if ( ! self::$database ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Database not initialized, skipping vector index creation' ); }
Logger::warning( 'Database not initialized, skipping vector index creation' );
return false;
}

Expand All @@ -233,8 +231,7 @@ public static function add_vector_index_to_existing_table() {
try {
$table_exists = $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name;
} catch ( \Exception $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Error checking if table exists: ' . $e->getMessage() ); }
Logger::error( 'Error checking if table exists: ' . $e->getMessage() );
return false;
}

Expand All @@ -246,8 +243,7 @@ public static function add_vector_index_to_existing_table() {
$is_mariadb = self::$database->get_db_type() === 'mariadb';
$has_vector_support = $is_mariadb && self::$database->has_native_vector_support();
} catch ( \Exception $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Error checking database type or vector support: ' . $e->getMessage() ); }
Logger::error( 'Error checking database type or vector support: ' . $e->getMessage() );
return false;
}

Expand All @@ -259,8 +255,7 @@ public static function add_vector_index_to_existing_table() {
$index_check = $wpdb->get_results( "SHOW INDEX FROM $table_name WHERE Key_name = 'embedding_idx'" );
$index_exists = ! empty( $index_check );
} catch ( \Exception $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Error checking for existing index: ' . $e->getMessage() ); }
Logger::warning( 'Error checking for existing index: ' . $e->getMessage() );
}

if ( ! $index_exists ) {
Expand All @@ -273,8 +268,7 @@ public static function add_vector_index_to_existing_table() {
);

if ( false === $result ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Failed to add vector index using new syntax: ' . $wpdb->last_error ); }
Logger::warning( 'Failed to add vector index using new syntax: ' . $wpdb->last_error );

Comment thread
rbcorrales marked this conversation as resolved.
// Try with simpler syntax as fallback.
$result = $wpdb->query(
Expand All @@ -285,16 +279,15 @@ public static function add_vector_index_to_existing_table() {
);

if ( false !== $result ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Added vector index with simplified syntax' ); }
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Failed to add vector index with simplified syntax: ' . $wpdb->last_error );
Logger::debug( 'Added vector index with simplified syntax' );
} else {
Logger::error( 'Failed to add vector index with simplified syntax: ' . $wpdb->last_error );
}
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Added optimized vector index to embeddings table' );
} else {
Logger::debug( 'Added optimized vector index to embeddings table' );
}
} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Vector index already exists, skipping creation' );
} else {
Logger::debug( 'Vector index already exists, skipping creation' );
}

// After creating the main vector index, add supporting indexes if needed.
Expand All @@ -313,27 +306,23 @@ public static function add_vector_index_to_existing_table() {
}
} catch ( \Exception $e ) {
// Ignore errors for supporting indexes, they're not critical.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "[WPVDB] Error creating supporting index $index_name: " . $e->getMessage() ); }
Logger::warning( "Error creating supporting index $index_name: " . $e->getMessage() );
}
}
} catch ( \Exception $e ) {
// Ignore errors for supporting indexes, they're not critical.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Error creating supporting indexes: ' . $e->getMessage() ); }
Logger::warning( 'Error creating supporting indexes: ' . $e->getMessage() );
}
} catch ( \Exception $e ) {
// Log error but don't let it crash the activation.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Error adding vector index: ' . $e->getMessage() ); }
Logger::error( 'Error adding vector index: ' . $e->getMessage() );
}
}

return true;
} catch ( \Exception $e ) {
// Catch all exceptions to prevent activation failure.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Fatal error in add_vector_index_to_existing_table: ' . $e->getMessage() ); }
Logger::critical( 'Fatal error in add_vector_index_to_existing_table: ' . $e->getMessage() );
return false;
}
}
Expand Down Expand Up @@ -377,13 +366,11 @@ public static function recreate_tables() {
// Update table statistics for optimal query planning.
$wpdb->query( "ANALYZE TABLE $table_name" );

if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Added optimized vector index and supporting indexes to embeddings table' ); }
Logger::debug( 'Added optimized vector index and supporting indexes to embeddings table' );
}
} catch ( \Exception $e ) {
// Ignore errors.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB] Error adding vector index during recreation: ' . $e->getMessage() ); }
Logger::error( 'Error adding vector index during recreation: ' . $e->getMessage() );
}
}

Expand Down
14 changes: 5 additions & 9 deletions includes/class-wpvdb-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,7 @@ public function ajax_confirm_provider_change() {
$cancel = true;
}
}
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'WPVDB: Cancel flag: ' . ( $cancel ? 'true' : 'false' ) ); }
Logger::debug( 'Cancel flag: ' . ( $cancel ? 'true' : 'false' ) );

$settings = get_option( 'wpvdb_settings', array() );

Expand Down Expand Up @@ -1141,8 +1140,7 @@ public function ajax_confirm_provider_change() {
} else {
// Mirror of handle_apply_provider_change for the AJAX path.
if ( empty( $settings['pending_provider'] ) || empty( $settings['pending_model'] ) ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'WPVDB: No pending provider change found' ); }
Logger::debug( 'No pending provider change found' );
wp_send_json_error(
array(
'message' => __( 'No pending provider change found.', 'wpvdb' ),
Expand Down Expand Up @@ -1843,7 +1841,7 @@ public function ajax_test_embedding() {

// Get sample of embedding values (first 5).
$sample = array_slice( $embedding, 0, 5 );
$sample_json = json_encode( $sample, JSON_PRETTY_PRINT );
$sample_json = wp_json_encode( $sample, JSON_PRETTY_PRINT );

wp_send_json_success(
array(
Expand Down Expand Up @@ -1925,8 +1923,7 @@ public function handle_admin_actions() {
}

// Log that diagnostics were run.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( '[WPVDB ADMIN] Running database diagnostics from admin UI' ); }
Logger::debug( 'Running database diagnostics from admin UI' );

// Redirect back to the page with a parameter to show diagnostics.
wp_redirect( add_query_arg( 'diagnostics', 'run', admin_url( 'admin.php?page=wpvdb-status' ) ) );
Expand Down Expand Up @@ -2747,8 +2744,7 @@ public function handle_cancel_provider_change() {
admin_url( 'admin.php' )
);

if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'WPVDB CRITICAL: Redirecting to: ' . $redirect_url ); }
Logger::debug( 'Redirecting to: ' . $redirect_url );
wp_redirect( $redirect_url );
exit;
}
Expand Down
Loading