feat(原生支持AI摘要功能): 使用一层API作为缓存而非直接请求AI,可以实现缓存/后端保密和预渲染

(cherry picked from commit 611a7d1d5dc7bc200d4390e29217ab8c8f76b0f0)
This commit is contained in:
anime
2024-12-23 03:01:35 +08:00
parent b70c0f4ee7
commit 5eb390bc38
6 changed files with 212 additions and 0 deletions

31
lib/plugins/aiSummary.js Normal file
View File

@@ -0,0 +1,31 @@
/**
* get Ai summary
* @returns {Promise<string>}
* @param aiSummaryAPI
* @param aiSummaryKey
* @param truncatedText
*/
export async function getAiSummary(aiSummaryAPI, aiSummaryKey, truncatedText) {
try {
const response = await fetch(aiSummaryAPI, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
token: aiSummaryKey,
content: truncatedText
})
})
if (response.ok) {
const data = await response.json()
return data.summary
} else {
throw new Error('Response not ok')
}
} catch (error) {
console.error('ChucklePostAI请求失败', error)
return '获取文章摘要失败,请稍后再试。'
}
}