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:
@@ -78,8 +78,7 @@
|
||||
|
||||
function getEditorContent() {
|
||||
var content = '';
|
||||
var ta = document.getElementById('content');
|
||||
if (ta && ta.value) content = ta.value;
|
||||
// Block editor first (post-new.php uses Gutenberg).
|
||||
try {
|
||||
if (window.wp && wp.data && wp.data.select) {
|
||||
var ed = wp.data.select('core/editor');
|
||||
@@ -89,10 +88,27 @@
|
||||
}
|
||||
}
|
||||
} catch (e) { /* ignore */ }
|
||||
// Strip tags for length check / API
|
||||
|
||||
if (!content) {
|
||||
var ta = document.getElementById('content');
|
||||
if (ta && ta.value) content = ta.value;
|
||||
}
|
||||
|
||||
// TinyMCE classic.
|
||||
try {
|
||||
if (!content && window.tinymce && tinymce.get) {
|
||||
var editor = tinymce.get('content');
|
||||
if (editor && editor.getContent) {
|
||||
content = editor.getContent({ format: 'text' }) || editor.getContent() || '';
|
||||
}
|
||||
}
|
||||
} catch (e2) { /* ignore */ }
|
||||
|
||||
// Strip tags for API plain-text summary.
|
||||
var div = document.createElement('div');
|
||||
div.innerHTML = content || '';
|
||||
return (div.textContent || div.innerText || content || '').trim();
|
||||
var plain = (div.textContent || div.innerText || content || '').replace(/\u00a0/g, ' ').trim();
|
||||
return plain;
|
||||
}
|
||||
|
||||
function setExcerptValue(summary) {
|
||||
@@ -238,23 +254,34 @@
|
||||
.then(function (res) {
|
||||
setLoading(sumBtn, false, sumOriginal);
|
||||
if (!res.ok || !res.json || res.json.success === false) {
|
||||
var j = res.json || {};
|
||||
var msg =
|
||||
(res.json && (res.json.message || (res.json.data && res.json.data.message))) ||
|
||||
j.message ||
|
||||
(j.data && (j.data.message || (Array.isArray(j.data) && j.data[0] && j.data[0].message))) ||
|
||||
I18N.failed ||
|
||||
'生成失败';
|
||||
if (res.status === 401 || res.status === 403) {
|
||||
msg = I18N.needLogin || '登录已失效或权限不足,请刷新页面后重试';
|
||||
}
|
||||
notify(sumBtn, msg, 'error');
|
||||
return;
|
||||
}
|
||||
var summary = (res.json.data && res.json.data.summary) || '';
|
||||
var payload = res.json.data || {};
|
||||
var summary = payload.summary || (payload.data && payload.data.summary) || '';
|
||||
if (!summary) {
|
||||
notify(sumBtn, I18N.failed || '生成失败:返回摘要为空', 'error');
|
||||
return;
|
||||
}
|
||||
if (!setExcerptValue(summary)) {
|
||||
notify(sumBtn, I18N.noExcerptField || '未找到摘要字段,请展开「摘要」面板', 'error');
|
||||
return;
|
||||
}
|
||||
notify(sumBtn, I18N.summarySuccess || '已填入摘要字段,请检查后更新保存', 'ok');
|
||||
})
|
||||
.catch(function () {
|
||||
.catch(function (err) {
|
||||
setLoading(sumBtn, false, sumOriginal);
|
||||
notify(sumBtn, I18N.failed || '生成失败', 'error');
|
||||
notify(sumBtn, (err && err.message) || I18N.failed || '生成失败', 'error');
|
||||
});
|
||||
|
||||
});
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user