hexo-ai-summary-pro
Version:
Professional AI summary generator for Hexo - supports multiple AI providers with automatic generation
179 lines (152 loc) • 6.16 kB
JavaScript
/**
* hexo-ai-summary-pro
* Professional AI summary generator for Hexo
* Supports multiple AI providers with automatic generation
*
* @author y0un92
* @version 1.0.2
*/
/* global hexo */
;
const AISummaryGenerator = require('./lib/generator');
// 初始化插件
hexo.log.info('AI Summary: 插件正在初始化...');
const aiSummary = new AISummaryGenerator(hexo);
hexo.log.info('AI Summary: 配置加载完成,启用状态:', aiSummary.config.enable);
// 注册文章处理过滤器 - 自动生成AI摘要
hexo.extend.filter.register('before_post_render', async function(data) {
// 只处理文章类型的内容
if (data.layout !== 'post') {
return data;
}
// 检查是否启用AI摘要功能
if (!aiSummary.config.enable) {
return data;
}
// 检查是否启用自动生成
if (!aiSummary.config.auto_generate) {
return data;
}
// 检查是否应该生成摘要(排除规则等)
if (!aiSummary.shouldGenerateSummary(data)) {
return data;
}
try {
aiSummary.log.info(`AI Summary: 自动生成摘要 - ${data.title}`);
// 添加小延迟避免API限制
if (aiSummary.config.batch_delay > 0) {
await new Promise(resolve => setTimeout(resolve, aiSummary.config.batch_delay));
}
const summary = await aiSummary.generateSummary(data.content, data.title);
data.ai_text = summary;
data.ai_summary = summary;
aiSummary.log.info(`AI Summary: 摘要生成完成 - ${data.title}`);
} catch (error) {
aiSummary.log.warn(`AI Summary: 生成摘要失败,使用默认摘要 - ${data.title}: ${error.message}`);
data.ai_text = aiSummary.config.fallback_summary;
data.ai_summary = aiSummary.config.fallback_summary;
}
return data;
});
// 注册helper函数
hexo.extend.helper.register('get_ai_summary', async function(post) {
if (post.ai_text) return post.ai_text;
if (post.ai_summary) return post.ai_summary;
try {
return await aiSummary.generateSummary(post.content || '', post.title || '');
} catch (error) {
aiSummary.log.warn(`AI Summary: 生成摘要失败: ${error.message}`);
return aiSummary.config.fallback_summary;
}
});
// 注册控制台命令
hexo.extend.console.register('ai-summary', 'Generate AI summaries using API', {
desc: 'Generate AI summaries for posts using configured AI API',
usage: '[options]',
options: [
{ name: '--force', desc: 'Force regenerate all summaries' },
{ name: '--test', desc: 'Test API connection' },
{ name: '--provider', desc: 'Specify AI provider: openai, claude, gemini, qianwen, baidu, custom' }
]
}, async function(args) {
// 测试API连接
if (args.test) {
try {
const testSummary = await aiSummary.generateSummary(
'这是一个测试文章内容,用于验证AI API是否正常工作。文章包含了技术介绍和实现方法。',
'测试文章'
);
this.log.info('✅ API连接测试成功!');
this.log.info(`📝 测试摘要: ${testSummary}`);
return;
} catch (error) {
this.log.error(`❌ API连接测试失败: ${error.message}`);
return;
}
}
// 临时更改提供商
const originalProvider = aiSummary.config.provider;
if (args.provider && ['openai', 'claude', 'gemini', 'qianwen', 'baidu', 'custom'].includes(args.provider)) {
aiSummary.config.provider = args.provider;
this.log.info(`使用AI提供商: ${args.provider}`);
}
const posts = this.locals.get('posts');
let count = 0;
let successCount = 0;
this.log.info('🚀 开始生成AI摘要...');
for (const post of posts.data) {
if (post.layout === 'post') {
count++;
if (args.force || !post.ai_text) {
try {
this.log.info(`正在处理: ${post.title}`);
const summary = await aiSummary.generateSummary(post.content, post.title);
post.ai_text = summary;
post.ai_summary = summary;
successCount++;
this.log.info(`✅ ${post.title}`);
// 添加延迟避免API限制
await new Promise(resolve => setTimeout(resolve, aiSummary.config.batch_delay || 1000));
} catch (error) {
this.log.error(`❌ ${post.title}: ${error.message}`);
}
}
}
}
// 恢复原提供商
aiSummary.config.provider = originalProvider;
this.log.info('\n📊 AI摘要生成完成!');
this.log.info(`成功生成: ${successCount}/${count} 篇文章`);
this.log.info('💡 运行 "hexo generate" 应用更改');
});
// 注册配置检查命令
hexo.extend.console.register('ai-config', 'Check AI summary configuration', {
desc: 'Validate AI summary API configuration'
}, function() {
const config = aiSummary.config;
this.log.info('🔧 AI摘要配置检查:');
this.log.info(`启用状态: ${config.enable ? '✅' : '❌'}`);
this.log.info(`自动生成: ${config.auto_generate ? '✅' : '❌'}`);
this.log.info(`AI提供商: ${config.provider}`);
this.log.info(`API密钥: ${config.api_key ? '✅ 已配置' : '❌ 未配置'}`);
this.log.info(`API地址: ${config.api_url || '未配置'}`);
this.log.info(`模型: ${config.model || '未配置'}`);
this.log.info(`最大令牌: ${config.max_tokens}`);
this.log.info(`生成温度: ${config.temperature}`);
this.log.info(`批量延迟: ${config.batch_delay}ms`);
this.log.info(`最小内容长度: ${config.min_content_length}字符`);
this.log.info(`重试次数: ${config.retry_count}`);
if (!config.enable) {
this.log.warn('⚠️ AI摘要功能未启用');
}
if (!config.auto_generate) {
this.log.warn('⚠️ 自动生成已禁用,需要手动运行 "hexo ai-summary" 生成摘要');
}
if (!config.api_key) {
this.log.warn('⚠️ 未配置API密钥,请在_config.yml中配置ai_summary_api.api_key');
}
this.log.info('\n💡 使用说明:');
this.log.info('- 设置 auto_generate: true 可在 hexo generate 时自动生成摘要');
this.log.info('- 设置 auto_generate: false 需要手动运行 hexo ai-summary 命令');
this.log.info('- 运行 hexo ai-summary --test 测试API连接');
});