fix(ai): stop false WP AI priority and harden summary-suggest

Prefer configured theme OpenAI provider over incomplete WP AI Client stubs,
return clearer status codes/messages for summary generation, and improve
block-editor content collection for AI 摘要 on post-new.
This commit is contained in:
2026-07-13 08:47:15 +08:00
parent 344f28f89b
commit 62310891e5
3 changed files with 143 additions and 42 deletions
+44 -24
View File
@@ -111,16 +111,20 @@ class AKSRT_AI {
}
/**
* WordPress 核心 AI Client 是否可用。
* WordPress 核心 AI Client 是否真正可用。
*
* 仅有函数存根不够:没有 Connectors / wp_supports_ai=false 时不应抢占主题 provider。
*/
public function is_wp_ai_available(): bool {
if (!function_exists('wp_ai_client_prompt')) {
return false;
}
if (function_exists('wp_supports_ai') && !wp_supports_ai()) {
return false;
// Prefer the official capability gate when present.
if (function_exists('wp_supports_ai')) {
return (bool) wp_supports_ai();
}
return true;
// Without wp_supports_ai, do not claim availability (avoids false-positive 400s).
return false;
}
/**
@@ -128,28 +132,37 @@ class AKSRT_AI {
*/
public function is_theme_provider_available(): bool {
$config = $this->get_config();
$provider = sanitize_key((string) ($config['provider'] ?? self::PROVIDER_NONE));
$api_key = trim((string) ($config['api_key'] ?? ''));
$api_base = trim((string) ($config['api_base'] ?? ''));
return !empty($config['enabled'])
&& ($config['provider'] ?? self::PROVIDER_NONE) !== self::PROVIDER_NONE
&& !empty($config['api_key']);
&& $provider !== self::PROVIDER_NONE
&& $provider !== ''
&& $api_key !== ''
&& $api_base !== '';
}
/**
* 任一后端可用即可生成。
*/
public function is_available(): bool {
return $this->is_wp_ai_available() || $this->is_theme_provider_available();
return $this->is_theme_provider_available() || $this->is_wp_ai_available();
}
/**
* 当前优先后端标识。
*
* Theme provider wins when explicitly configured — WP AI stubs often exist
* without a working connector and previously produced opaque 400 failures.
*/
public function resolve_backend(): string {
if ($this->is_wp_ai_available()) {
return 'wp_ai';
}
if ($this->is_theme_provider_available()) {
return 'theme';
}
if ($this->is_wp_ai_available()) {
return 'wp_ai';
}
return 'none';
}
@@ -205,28 +218,35 @@ class AKSRT_AI {
? (string) $options['system_instruction']
: __('你是一个内容助手,提供建议性内容。输出仅作为参考建议,不会自动应用到线上内容。', 'aksrt');
// 1) Prefer WP AI Client
$last_error = [
'success' => false,
'message' => __('AI 服务未启用或未配置。请配置 WordPress AI Connectors,或在「扩展 → AI 辅助」填写 API Key。', 'aksrt'),
];
// 1) Prefer explicitly configured theme OpenAI-compatible provider.
if ($this->is_theme_provider_available()) {
$theme_result = $this->generate_via_theme($prompt, $system, $options);
$this->record_call();
if (!empty($theme_result['success'])) {
$theme_result['backend'] = 'theme';
return $theme_result;
}
$last_error = $theme_result;
// Fall through to WP AI if available.
}
// 2) WordPress AI Client (Connectors).
if ($this->is_wp_ai_available()) {
$wp_result = $this->generate_via_wp_ai($prompt, $system, $options);
$this->record_call();
if (!empty($wp_result['success'])) {
$this->record_call();
$wp_result['backend'] = 'wp_ai';
return $wp_result;
}
// Fall through to theme if configured
if (!$this->is_theme_provider_available()) {
$this->record_call();
return $wp_result;
}
$last_error = $wp_result;
}
// 2) Theme OpenAI-compatible
$theme_result = $this->generate_via_theme($prompt, $system, $options);
$this->record_call();
if (!empty($theme_result['success'])) {
$theme_result['backend'] = 'theme';
}
return $theme_result;
return $last_error;
}
/**