(task: SummaryTask)
| 725 | async function executeSummary(task: SummaryTask): Promise<{ success: boolean; message: string }> { |
| 726 | try { |
| 727 | const client = await getGlobalClient(); |
| 728 | if (!client) throw new Error("Telegram 客户端未初始化"); |
| 729 | |
| 730 | const db = await getDB(); |
| 731 | |
| 732 | // 获取消息 |
| 733 | let messageData: MessageData[]; |
| 734 | |
| 735 | if (task.timeRange) { |
| 736 | messageData = await getGroupMessagesByTime(task.chatId, task.timeRange); |
| 737 | } else { |
| 738 | messageData = await getGroupMessages(task.chatId, task.messageCount); |
| 739 | } |
| 740 | |
| 741 | if (!messageData || messageData.length === 0) { |
| 742 | return { success: false, message: "未找到可总结的消息" }; |
| 743 | } |
| 744 | |
| 745 | // AI 总结 |
| 746 | const summaryResult = await summarizeMessages(task, messageData); |
| 747 | if (!summaryResult.success) { |
| 748 | return { success: false, message: summaryResult.error! }; |
| 749 | } |
| 750 | |
| 751 | // 发送总结 |
| 752 | const pushTarget = task.pushTarget || db.data.defaultPushTarget || "me"; |
| 753 | let summaryContent = summaryResult.result!; |
| 754 | |
| 755 | // 过滤掉思考标签内容(如 <thinking>...</thinking>、<think>...</think>) |
| 756 | summaryContent = summaryContent.replace(/<thinking>[\s\S]*?<\/thinking>/gi, ""); |
| 757 | summaryContent = summaryContent.replace(/<think>[\s\S]*?<\/think>/gi, ""); |
| 758 | summaryContent = summaryContent.trim(); |
| 759 | |
| 760 | // 应用最大输出长度限制(过滤思考内容后再计算) |
| 761 | // 应用最大输出长度限制(0表示不限制) |
| 762 | const maxOutputLength = db.data.aiConfig.max_output_length ?? 0; |
| 763 | if (maxOutputLength > 0 && summaryContent.length > maxOutputLength) { |
| 764 | summaryContent = summaryContent.substring(0, maxOutputLength) + "\n\n⚠️ 内容已截断(超过最大长度限制)"; |
| 765 | } |
| 766 | |
| 767 | const header = `📊 群组总结\n来源: ${htmlEscape(task.chatDisplay || task.chatId)}\n时间: ${formatDate(new Date())}\n\n`; |
| 768 | |
| 769 | // 应用折叠标签(如果启用) |
| 770 | const wrappedContent = wrapWithSpoiler(summaryContent, task.useSpoiler || false); |
| 771 | const summaryText = `${header}${wrappedContent}`; |
| 772 | |
| 773 | // 如果启用折叠或内容包含 HTML 标签,使用 HTML 解析模式 |
| 774 | const needHtmlParse = task.useSpoiler || summaryContent.includes('<'); |
| 775 | |
| 776 | await client.sendMessage(pushTarget, { |
| 777 | message: summaryText, |
| 778 | parseMode: needHtmlParse ? "html" : undefined |
| 779 | }); |
| 780 | |
| 781 | return { success: true, message: `总结完成,已推送到 ${pushTarget}` }; |
| 782 | } catch (e: any) { |
| 783 | return { success: false, message: `总结失败: ${e?.message || e}` }; |
| 784 | } |
no test coverage detected