-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathAI_Request_Log_Controller.php
More file actions
337 lines (310 loc) · 10 KB
/
Copy pathAI_Request_Log_Controller.php
File metadata and controls
337 lines (310 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
* REST API controller for AI request logs.
*
* @package WordPress\AI\Logging
*/
declare( strict_types=1 );
namespace WordPress\AI\Logging\REST;
use WP_Error;
use WP_REST_Controller;
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
use WordPress\AI\Logging\AI_Request_Log_Manager;
defined( 'ABSPATH' ) || exit;
/**
* Provides `/ai/v1/logs` routes for the AI Request Logs admin UI.
*
* @since 1.0.0
*/
class AI_Request_Log_Controller extends WP_REST_Controller {
/**
* Log manager instance.
*/
private AI_Request_Log_Manager $manager;
/**
* Constructor.
*
* @param \WordPress\AI\Logging\AI_Request_Log_Manager $manager Log manager.
*/
public function __construct( AI_Request_Log_Manager $manager ) {
$this->namespace = 'ai/v1';
$this->rest_base = 'logs';
$this->manager = $manager;
}
/**
* Registers REST routes.
*/
public function register_routes(): void {
// GET /ai/v1/logs - List logs with filtering.
// DELETE /ai/v1/logs - Purge all logs.
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_logs' ),
'permission_callback' => array( $this, 'permissions_check' ),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'purge_logs' ),
'permission_callback' => array( $this, 'permissions_check' ),
'args' => array(
'before_days' => array(
'description' => __( 'Delete logs older than this many days. 0 deletes all logs.', 'ai' ),
'type' => 'integer',
'minimum' => 0,
'default' => 0,
),
),
),
)
);
// GET /ai/v1/logs/summary - Get aggregate statistics.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/summary',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_summary' ),
'permission_callback' => array( $this, 'permissions_check' ),
'args' => array(
'period' => array(
'type' => 'string',
'enum' => array( 'minute', 'hour', 'day', 'week', 'month', 'all' ),
'default' => 'day',
),
),
),
)
);
// GET /ai/v1/logs/filters - Get filter options.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/filters',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_filters' ),
'permission_callback' => array( $this, 'permissions_check' ),
),
)
);
// GET /ai/v1/logs/{id} - Get single log entry.
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[a-f0-9\-]+)',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_log' ),
'permission_callback' => array( $this, 'permissions_check' ),
'args' => array(
'id' => array(
'type' => 'string',
'required' => true,
'validate_callback' => static function ( $value ): bool {
return wp_is_uuid( $value );
},
),
),
),
)
);
}
/**
* Permission check - restricted to administrators.
*/
public function permissions_check(): bool {
return current_user_can( 'manage_options' );
}
/**
* Retrieves logs with filtering and pagination.
*
* @param \WP_REST_Request $request Request.
* @return \WP_REST_Response
*/
public function get_logs( WP_REST_Request $request ): WP_REST_Response {
$args = array(
'type' => $request->get_param( 'type' ) ?? '',
'status' => $request->get_param( 'status' ) ?? '',
'provider' => $request->get_param( 'provider' ) ?? '',
'operation' => $request->get_param( 'operation' ) ?? '',
'tokens_filter' => $request->get_param( 'tokens_filter' ) ?? '',
'user_id' => $request->get_param( 'user_id' ) ?? 0,
'date_from' => $request->get_param( 'date_from' ) ?? '',
'date_to' => $request->get_param( 'date_to' ) ?? '',
'search' => $request->get_param( 'search' ) ?? '',
'page' => $request->get_param( 'page' ) ?? 1,
'per_page' => $request->get_param( 'per_page' ) ?? 25,
'orderby' => $request->get_param( 'orderby' ) ?? 'timestamp',
'order' => $request->get_param( 'order' ) ?? 'DESC',
'cursor_id' => $request->get_param( 'cursor_id' ),
'cursor_timestamp' => $request->get_param( 'cursor_timestamp' ),
);
$result = $this->manager->get_logs( $args );
$response = rest_ensure_response( $result['items'] );
$response->header( 'X-WP-Total', (string) $result['total'] );
$response->header( 'X-WP-TotalPages', (string) $result['pages'] );
// Include cursor info for cursor-based pagination.
if ( isset( $result['next_cursor'] ) ) {
$response->header( 'X-WP-NextCursorId', (string) $result['next_cursor']['id'] );
$response->header( 'X-WP-NextCursorTimestamp', $result['next_cursor']['timestamp'] );
}
return $response;
}
/**
* Retrieves a single log entry.
*
* @param \WP_REST_Request $request Request.
* @return \WP_REST_Response|\WP_Error
*/
public function get_log( WP_REST_Request $request ) {
$log_id = $request->get_param( 'id' );
$log = $this->manager->get_log( $log_id );
if ( ! $log ) {
return new WP_Error(
'wpai_log_not_found',
__( 'Log entry not found.', 'ai' ),
array( 'status' => 404 )
);
}
return rest_ensure_response( $log );
}
/**
* Retrieves aggregate statistics.
*
* @param \WP_REST_Request $request Request.
* @return \WP_REST_Response
*/
public function get_summary( WP_REST_Request $request ): WP_REST_Response {
$period = $request->get_param( 'period' ) ?? 'day';
$summary = $this->manager->get_summary( $period );
return rest_ensure_response( $summary );
}
/**
* Retrieves filter options.
*
* @param \WP_REST_Request $request Request.
* @return \WP_REST_Response
*/
public function get_filters( WP_REST_Request $request ): WP_REST_Response {
$filters = $this->manager->get_filter_options();
return rest_ensure_response( $filters );
}
/**
* Deletes logs, optionally limiting to entries older than a given number of days.
*
* When before_days is 0 (the default), all logs are purged. When before_days
* is a positive integer, only logs older than that many days are removed.
*
* @param \WP_REST_Request $request Request.
* @return \WP_REST_Response
*/
public function purge_logs( WP_REST_Request $request ): WP_REST_Response {
$before_days = (int) $request->get_param( 'before_days' );
$deleted = $this->manager->delete_logs_older_than( $before_days );
return rest_ensure_response(
array(
'success' => true,
'deleted' => $deleted,
'message' => sprintf(
/* translators: %d: Number of deleted logs. */
__( 'Successfully deleted %d log entries.', 'ai' ),
$deleted
),
)
);
}
/**
* Gets collection parameters for logs list endpoint.
*
* @return array<string, array<string, mixed>> Parameter definitions.
*/
public function get_collection_params(): array {
return array(
'type' => array(
'description' => __( 'Filter by log type.', 'ai' ),
'type' => 'string',
'enum' => array( '', 'ai_client', 'mcp_tool', 'ability' ),
'default' => '',
),
'status' => array(
'description' => __( 'Filter by status.', 'ai' ),
'type' => 'string',
'enum' => array( '', 'success', 'error', 'timeout' ),
'default' => '',
),
'provider' => array(
'description' => __( 'Filter by AI provider.', 'ai' ),
'type' => 'string',
'default' => '',
),
'user_id' => array(
'description' => __( 'Filter by user ID.', 'ai' ),
'type' => 'integer',
'default' => 0,
),
'date_from' => array(
'description' => __( 'Filter logs from this date (YYYY-MM-DD HH:MM:SS).', 'ai' ),
'type' => 'string',
'format' => 'date-time',
),
'date_to' => array(
'description' => __( 'Filter logs until this date (YYYY-MM-DD HH:MM:SS).', 'ai' ),
'type' => 'string',
'format' => 'date-time',
),
'search' => array(
'description' => __( 'Search in operations, request previews, response previews, and error messages.', 'ai' ),
'type' => 'string',
'default' => '',
),
'tokens_filter' => array(
'description' => __( 'Filter by tokens: "gt:N", "lt:N", or "none".', 'ai' ),
'type' => 'string',
'default' => '',
),
'page' => array(
'description' => __( 'Current page of the collection.', 'ai' ),
'type' => 'integer',
'default' => 1,
'minimum' => 1,
),
'per_page' => array(
'description' => __( 'Maximum number of items per page.', 'ai' ),
'type' => 'integer',
'default' => 25,
'minimum' => 1,
'maximum' => 100,
),
'orderby' => array(
'description' => __( 'Sort collection by attribute.', 'ai' ),
'type' => 'string',
'enum' => array( 'timestamp', 'type', 'operation', 'duration_ms', 'tokens_total', 'status' ),
'default' => 'timestamp',
),
'order' => array(
'description' => __( 'Order sort attribute ascending or descending.', 'ai' ),
'type' => 'string',
'enum' => array( 'ASC', 'DESC' ),
'default' => 'DESC',
),
'cursor_id' => array(
'description' => __( 'Cursor ID for cursor-based pagination (use with cursor_timestamp).', 'ai' ),
'type' => 'integer',
'minimum' => 1,
),
'cursor_timestamp' => array(
'description' => __( 'Cursor timestamp for cursor-based pagination (use with cursor_id).', 'ai' ),
'type' => 'string',
'format' => 'date-time',
),
);
}
}