202 lines
6.7 KiB
PHP
202 lines
6.7 KiB
PHP
<?php
|
|
/**
|
|
* AKSRT Admin — AI Controller
|
|
*
|
|
* Owns REST routes for the "AI 子系统" domain.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class AKSRT_Admin_AI_Controller extends AKSRT_Admin_Controller_Base {
|
|
|
|
private static $instance = null;
|
|
|
|
public static function get_instance(): self {
|
|
if (null === self::$instance) self::$instance = new self();
|
|
return self::$instance;
|
|
}
|
|
|
|
public function register_routes(): void {
|
|
// AI status
|
|
register_rest_route(self::API_NAMESPACE, '/ai/status', [
|
|
'methods' => 'GET',
|
|
'callback' => [$this, 'get_status'],
|
|
'permission_callback' => [$this, 'check_permission'],
|
|
]);
|
|
|
|
// AI settings update
|
|
register_rest_route(self::API_NAMESPACE, '/ai/settings', [
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'update_settings'],
|
|
'permission_callback' => [$this, 'check_permission'],
|
|
]);
|
|
|
|
// AI SEO suggest
|
|
register_rest_route(self::API_NAMESPACE, '/ai/seo-suggest', [
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'seo_suggest'],
|
|
'permission_callback' => [$this, 'check_permission'],
|
|
]);
|
|
|
|
// AI summary suggest
|
|
register_rest_route(self::API_NAMESPACE, '/ai/summary-suggest', [
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'summary_suggest'],
|
|
'permission_callback' => [$this, 'check_permission'],
|
|
]);
|
|
|
|
// AI tags suggest
|
|
register_rest_route(self::API_NAMESPACE, '/ai/tags-suggest', [
|
|
'methods' => 'POST',
|
|
'callback' => [$this, 'tags_suggest'],
|
|
'permission_callback' => [$this, 'check_permission'],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* GET /ai/status — AI 状态
|
|
*/
|
|
public function get_status(): WP_REST_Response {
|
|
if (!class_exists('AKSRT_AI')) {
|
|
return $this->success(['data' => [
|
|
'enabled' => false,
|
|
'provider' => 'none',
|
|
'configured' => false,
|
|
'available' => false,
|
|
]]);
|
|
}
|
|
|
|
return $this->success(['data' => AKSRT_AI::get_instance()->get_status()]);
|
|
}
|
|
|
|
/**
|
|
* POST /ai/settings — 更新 AI 设置
|
|
*/
|
|
public function update_settings(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_AI')) {
|
|
return $this->error('AI 模块未加载', 500);
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
|
|
$allowed = [
|
|
'enabled' => 'bool',
|
|
'provider' => 'string',
|
|
'api_key' => 'string',
|
|
'api_base' => 'url',
|
|
'model' => 'string',
|
|
'max_tokens' => 'int',
|
|
'rate_limit' => 'int',
|
|
];
|
|
|
|
$config = [];
|
|
foreach ($allowed as $key => $type) {
|
|
if (!isset($params[$key])) continue;
|
|
switch ($type) {
|
|
case 'bool':
|
|
$config[$key] = !empty($params[$key]);
|
|
break;
|
|
case 'int':
|
|
$config[$key] = (int) $params[$key];
|
|
break;
|
|
case 'url':
|
|
$config[$key] = esc_url_raw((string) $params[$key]);
|
|
break;
|
|
default:
|
|
$config[$key] = sanitize_text_field((string) $params[$key]);
|
|
}
|
|
}
|
|
|
|
$success = AKSRT_AI::get_instance()->update_config($config);
|
|
|
|
if ($success) {
|
|
return $this->success(['message' => __('AI 设置已保存', 'aksrt')]);
|
|
}
|
|
return $this->error(__('保存失败', 'aksrt'));
|
|
}
|
|
|
|
/**
|
|
* POST /ai/seo-suggest — SEO 建议
|
|
*/
|
|
public function seo_suggest(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_AI_SEO')) {
|
|
return $this->error('AI SEO 模块未加载', 500);
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$title = sanitize_text_field((string) ($params['title'] ?? ''));
|
|
$content = wp_kses_post((string) ($params['content'] ?? ''));
|
|
|
|
if (empty($title)) {
|
|
return $this->error('标题不能为空');
|
|
}
|
|
|
|
$result = AKSRT_AI_SEO::get_instance()->suggest($title, $content);
|
|
|
|
if ($result['success']) {
|
|
return $this->success(['data' => $result['data']]);
|
|
}
|
|
|
|
return $this->error($result['message'] ?? '生成失败');
|
|
}
|
|
|
|
/**
|
|
* POST /ai/summary-suggest — 摘要建议
|
|
*/
|
|
public function summary_suggest(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_AI') || !class_exists('AKSRT_AI_Prompt')) {
|
|
return $this->error('AI 模块未加载', 500);
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$title = sanitize_text_field((string) ($params['title'] ?? ''));
|
|
$content = wp_kses_post((string) ($params['content'] ?? ''));
|
|
$max_len = max(50, min(500, (int) ($params['max_length'] ?? 200)));
|
|
|
|
if (empty($content)) {
|
|
return $this->error('内容不能为空');
|
|
}
|
|
|
|
$prompt = AKSRT_AI_Prompt::get_instance()->build_summary_prompt($content, $max_len);
|
|
$result = AKSRT_AI::get_instance()->generate($prompt);
|
|
|
|
if ($result['success']) {
|
|
return $this->success(['data' => ['summary' => $result['content']]]);
|
|
}
|
|
|
|
return $this->error($result['message'] ?? '生成失败');
|
|
}
|
|
|
|
/**
|
|
* POST /ai/tags-suggest — 标签建议
|
|
*/
|
|
public function tags_suggest(WP_REST_Request $request): WP_REST_Response {
|
|
if (!class_exists('AKSRT_AI') || !class_exists('AKSRT_AI_Prompt')) {
|
|
return $this->error('AI 模块未加载', 500);
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$title = sanitize_text_field((string) ($params['title'] ?? ''));
|
|
$content = wp_kses_post((string) ($params['content'] ?? ''));
|
|
|
|
if (empty($title) && empty($content)) {
|
|
return $this->error('标题或内容不能为空');
|
|
}
|
|
|
|
$prompt = AKSRT_AI_Prompt::get_instance()->build_tags_prompt($title, $content);
|
|
$result = AKSRT_AI::get_instance()->generate($prompt);
|
|
|
|
if ($result['success']) {
|
|
$tags = json_decode($result['content'], true);
|
|
if (!is_array($tags)) {
|
|
$tags = array_filter(array_map('trim', explode(',', $result['content'])));
|
|
}
|
|
return $this->success(['data' => ['tags' => $tags]]);
|
|
}
|
|
|
|
return $this->error($result['message'] ?? '生成失败');
|
|
}
|
|
}
|