plugin_name = $plugin_name; $this->version = $version; } /** Adds the AI Logs sub-page under Settings. */ public function add_menu(): void { add_submenu_page( 'acrossai-model-manager', __( 'AI Request Logs', 'acrossai-model-manager' ), __( 'AI Logs', 'acrossai-model-manager' ), 'manage_options', self::PAGE_SLUG, array( $this, 'render_page' ) ); } /** Renders the log list page or detail view. */ public function render_page(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'acrossai-model-manager' ) ); } // Handle single-entry detail view. // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['view'] ) && is_numeric( $_GET['view'] ) ) { $this->render_detail_view( (int) $_GET['view'] ); return; } // Handle "Clear All Logs" form submission. if ( isset( $_POST['acai_clear_logs'] ) ) { check_admin_referer( 'acai_clear_logs_nonce' ); global $wpdb; $table = Logger::get_table_name(); $wpdb->query( "TRUNCATE TABLE `{$table}`" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared echo '

' . esc_html__( 'All logs cleared.', 'acrossai-model-manager' ) . '

'; } $list_table = new AI_Log_List_Table(); $list_table->process_bulk_action(); $list_table->prepare_items(); $back_url = admin_url( 'admin.php?page=' . Menu::PAGE_SLUG ); ?>

←  

display(); ?>
get_row( $wpdb->prepare( "SELECT * FROM `{$table}` WHERE id = %d", $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( ! $row ) { echo '

' . esc_html__( 'Log entry not found.', 'acrossai-model-manager' ) . '

'; return; } $back_url = admin_url( 'admin.php?page=' . self::PAGE_SLUG ); ?>

#id ); ?>

←

finish_reason && ! empty( $row->error_message ) ) : ?> thought_tokens ) : ?>
created_at ); ?>
capability ); ?>
provider_name . ' (' . $row->provider_id . ')' ); ?>
model_name . ' (' . $row->model_id . ')' ); ?>
finish_reason ) : ?>   finish_reason ); ?>
error_message ); ?>
duration_ms ); ?> ms
prompt_tokens ); ?>
completion_tokens ); ?>
thought_tokens ); ?>
total_tokens ); ?>
user_id ); ?>
result_id ); ?>
source_type ?: '—' ); ?>
source_name ?: '—' ); ?>
source_file ?: '—' ); ?>
source_line ? absint( $row->source_line ) : '—'; ?>

prompt_text ); ?>

response_text ); ?>
'', 'created_at' => __( 'Date', 'acrossai-model-manager' ), 'capability' => __( 'Capability', 'acrossai-model-manager' ), 'provider_id' => __( 'Provider', 'acrossai-model-manager' ), 'model_id' => __( 'Model', 'acrossai-model-manager' ), 'source_type' => __( 'Source', 'acrossai-model-manager' ), 'total_tokens' => __( 'Tokens', 'acrossai-model-manager' ), 'duration_ms' => __( 'Duration', 'acrossai-model-manager' ), 'finish_reason'=> __( 'Finish', 'acrossai-model-manager' ), ); } /** @inheritDoc */ protected function get_sortable_columns(): array { return array( 'created_at' => array( 'created_at', true ), 'source_type' => array( 'source_type', false ), 'total_tokens' => array( 'total_tokens', false ), 'duration_ms' => array( 'duration_ms', false ), ); } /** @inheritDoc */ protected function get_bulk_actions(): array { return array( 'delete' => __( 'Delete', 'acrossai-model-manager' ), ); } /** @inheritDoc */ public function process_bulk_action(): void { if ( 'delete' !== $this->current_action() ) { return; } if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'bulk-' . $this->_args['plural'] ) ) { wp_die( esc_html__( 'Nonce verification failed.', 'acrossai-model-manager' ) ); } $ids = isset( $_POST['log'] ) ? array_map( 'absint', (array) $_POST['log'] ) : array(); if ( empty( $ids ) ) { return; } global $wpdb; $table = Logger::get_table_name(); $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prepare( "DELETE FROM `{$table}` WHERE id IN ({$placeholders})", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $ids ) ); } /** @inheritDoc */ public function prepare_items(): void { global $wpdb; $table = Logger::get_table_name(); $per_page = 20; $page = $this->get_pagenum(); $offset = ( $page - 1 ) * $per_page; // Sorting. $sortable = $this->get_sortable_columns(); $orderby = isset( $_GET['orderby'] ) && array_key_exists( sanitize_key( $_GET['orderby'] ), $sortable ) // phpcs:ignore WordPress.Security.NonceVerification ? sanitize_key( $_GET['orderby'] ) // phpcs:ignore WordPress.Security.NonceVerification : 'created_at'; $order = isset( $_GET['order'] ) && 'asc' === strtolower( sanitize_key( $_GET['order'] ) ) // phpcs:ignore WordPress.Security.NonceVerification ? 'ASC' : 'DESC'; // Total count. $total = (int) $wpdb->get_var( "SELECT COUNT(*) FROM `{$table}`" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared // Page of results. $this->items = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery $wpdb->prepare( "SELECT * FROM `{$table}` ORDER BY {$orderby} {$order} LIMIT %d OFFSET %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $per_page, $offset ), ARRAY_A ); $this->set_pagination_args( array( 'total_items' => $total, 'per_page' => $per_page, 'total_pages' => ceil( $total / $per_page ), ) ); $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array( $columns, $hidden, $sortable ); } /** @inheritDoc */ protected function column_cb( $item ): string { return sprintf( '', absint( $item['id'] ) ); } /** @inheritDoc */ protected function column_default( $item, $column_name ): string { switch ( $column_name ) { case 'created_at': $view_url = admin_url( 'admin.php?page=' . LogPage::PAGE_SLUG . '&view=' . absint( $item['id'] ) ); return sprintf( '%s', esc_url( $view_url ), esc_html( $item['created_at'] ) ); case 'capability': return esc_html( str_replace( '_', ' ', $item['capability'] ) ); case 'source_type': $type = $item['source_type'] ?? ''; $name = $item['source_name'] ?? ''; if ( '' === $type ) { return '—'; } $label = $name ? esc_html( $type . ': ' . $name ) : esc_html( $type ); $file = $item['source_file'] ?? ''; $line = ! empty( $item['source_line'] ) ? (int) $item['source_line'] : 0; $title = $file ? esc_attr( $file . ( $line ? ':' . $line : '' ) ) : ''; return $title ? '' . $label . '' : $label; case 'provider_id': return esc_html( $item['provider_name'] ?: $item['provider_id'] ); case 'model_id': return esc_html( $item['model_name'] ?: $item['model_id'] ); case 'total_tokens': return sprintf( '%s', esc_attr( sprintf( /* translators: 1: prompt tokens, 2: completion tokens */ __( 'Prompt: %1$d / Completion: %2$d', 'acrossai-model-manager' ), absint( $item['prompt_tokens'] ), absint( $item['completion_tokens'] ) ) ), absint( $item['total_tokens'] ) ); case 'duration_ms': return absint( $item['duration_ms'] ) . ' ms'; case 'finish_reason': $reason = $item['finish_reason'] ?? ''; $err_msg = $item['error_message'] ?? ''; if ( 'error' === $reason ) { $tooltip = $err_msg ? esc_attr( $err_msg ) : esc_attr__( 'Request failed — API error, invalid key, network failure, or timeout.', 'acrossai-model-manager' ); return '' . esc_html__( 'error', 'acrossai-model-manager' ) . ''; } return esc_html( $reason ); default: return esc_html( $item[ $column_name ] ?? '' ); } } }