Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
615935d947 | ||
|
|
9ac25c4081 | ||
|
|
2693f88982 | ||
|
|
1cd273494d | ||
|
|
5337a62a13 | ||
|
|
122069e221 | ||
|
|
e5f973c13d | ||
|
|
1a2732fe19 | ||
|
|
633f3ff588 | ||
|
|
2c88ad6fb9 | ||
|
|
a9dd1c14bc | ||
|
|
77e3bd36c5 | ||
|
|
7934d8c947 | ||
|
|
e6839b4aad | ||
|
|
c3e003a572 | ||
|
|
be6a01a06e | ||
|
|
767efdccbb | ||
|
|
bf989ca3f2 |
@@ -6,7 +6,29 @@ on:
|
||||
- published
|
||||
|
||||
jobs:
|
||||
# Pre-cleanup: delete all existing assets from the release to avoid gh release upload failure
|
||||
pre-release-cleanup:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Delete Existing Release Assets
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
TAG_NAME="${{ github.event.release.tag_name }}"
|
||||
# Capture asset list first to avoid pipefail issues
|
||||
ASSETS=$(gh release view "$TAG_NAME" --json assets --jq '.assets[].name' 2>/dev/null || true)
|
||||
if [ -n "$ASSETS" ]; then
|
||||
echo "$ASSETS" | while read -r filename; do
|
||||
echo "Deleting existing asset: $filename"
|
||||
gh release delete-asset "$TAG_NAME" "$filename" --yes 2>/dev/null || true
|
||||
done
|
||||
else
|
||||
echo "No existing assets to delete"
|
||||
fi
|
||||
shell: bash
|
||||
|
||||
cd:
|
||||
needs: pre-release-cleanup
|
||||
uses: halo-sigs/reusable-workflows/.github/workflows/plugin-cd.yaml@v4
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
@@ -63,6 +63,7 @@ lerna-debug.log*
|
||||
*.ctxt
|
||||
|
||||
### Package Files
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
@@ -70,6 +71,12 @@ lerna-debug.log*
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
### UI build output
|
||||
ui/dist/
|
||||
ui/dist-ssr/
|
||||
ui/*.local
|
||||
ui/.eslintcache
|
||||
|
||||
### Local file
|
||||
application-local.yml
|
||||
application-local.yaml
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
# AI回评 / Comment AI Autopilot
|
||||
|
||||
基于 AI 的 Halo 博客评论自动回复插件,支持多 AI 角色、自审核、自动发布和对话式连续回复。
|
||||
基于 AI 的 Halo 博客评论自动回复插件,支持多 AI 角色、合规检测、自审核、自动发布和对话式连续回复。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- **多 AI 角色** — 支持创建多个 AI 角色,每个角色有独立的昵称、人格提示词和 Gravatar 头像,可为不同文章指定不同角色
|
||||
- **多 AI 角色** — 支持创建多个 AI 角色,每个角色有独立的昵称、人格提示词、性别、语气风格和 Gravatar 头像,可为不同文章指定不同角色
|
||||
- **唤醒词** — 评论以唤醒词开头可唤醒指定角色回复,支持自定义唤醒词,可在未启用AI回评的页面使用唤醒词召唤AI
|
||||
- **自动回复** — 监听新评论,自动调用 AI 生成回复,支持多轮对话上下文
|
||||
- **多语言适配** — 根据评论语言自动用对应语言回复
|
||||
- **情感分析** — 分析评论情感倾向(正面/中性/负面),根据情感调整回复语气
|
||||
- **情感分析** — 分析评论情感倾向(非常正面/正面/中性/负面/非常负面),根据情感调整回复语气
|
||||
- **前置过滤(合规检测)** — AI 回复前对评论进行合规性分类,自动拦截广告/辱骂攻击/敏感内容/乱码,违规评论停止生成 AI 回复以节省 Token,可选自动将违规评论设为待审核状态
|
||||
- **误报反馈** — 被误拦截的评论可进行误报反馈,支持"AI回复"和"仅通过"两种处理方式,"仅通过"后可随时补触发 AI 回复
|
||||
- **草稿模式** — AI 回复先存为草稿,管理员审核后再发布,支持批量操作
|
||||
- **失败重试** — AI 生成失败时自动重试,指数退避策略
|
||||
- **对话轮次限制** — 同一评论线程中限制 AI 最多回复轮次,防止无限对话
|
||||
@@ -15,25 +18,30 @@
|
||||
- **文章/页面级开关** — 在文章编辑器中直接控制是否启用 AI 回复,文章默认开启,页面默认关闭
|
||||
- **评论者黑名单** — 支持按名称、邮箱和正则表达式屏蔽指定评论者,可从评论列表选择
|
||||
- **手动触发** — 在评论管理页面对历史评论手动触发 AI 回复
|
||||
- **安全审核** — AI 生成的内容经过安全审核,不合规内容自动拒绝
|
||||
- **Prompt 模板** — 支持自定义 Prompt 模板,提供多种模板变量(文章标题、发布日期、评论数等)
|
||||
- **两阶段安全审核** — 安全检查 + 质量评分(1-5 分映射到 0-100 分),不合规内容自动拒绝
|
||||
- **Prompt 模板** — 支持自定义 Prompt 模板,提供多种模板变量(文章标题、发布日期、评论数、对话历史等)
|
||||
- **Prompt 预设** — 内置友好型、专业型、幽默型、简洁型预设风格,可多选组合
|
||||
- **仪表盘统计** — 显示回复数、情感分布、每日回复趋势等图表,支持时间范围切换
|
||||
- **插件健康检查** — 实时检测 AI Foundation 连接状态和模型可用性
|
||||
- **日志筛选** — 按状态、情感筛选,关键词搜索
|
||||
- **日志筛选** — 按状态、情感筛选,关键词搜索,支持查看拦截原因和分类标签
|
||||
- **数据清理** — 自动清理超过指定天数的旧记录
|
||||
- **AI Foundation 集成** — 必须安装 Halo AI Foundation 插件,使用其提供的 AI 模型能力
|
||||
- **AI Foundation 集成** — 通过 Halo 官方推荐的 `ExtensionGetter` 获取 AI 服务,需安装 AI Foundation 插件
|
||||
|
||||
## 前置要求
|
||||
|
||||
- Halo 2.23+
|
||||
- Halo 2.25+
|
||||
- AI Foundation 插件(必须)
|
||||
|
||||
## 安装
|
||||
|
||||
### 应用商店安装
|
||||
|
||||
进入 **插件** → **安装** → 应用市场搜索 **AI回评** → 安装,或前往 [Halo 应用商店](https://www.halo.run/store/apps/app-mo5tivjt) 一键安装。
|
||||
|
||||
### 手动安装
|
||||
|
||||
1. 前往 [Releases](https://github.com/sunny-335/plugin-comment-ai-autopilot/releases) 下载最新的 `.jar` 文件
|
||||
2. 登录 Halo 管理后台
|
||||
3. 进入 **插件** → **已安装** → 点击右上角 **安装** 按钮
|
||||
3. 进入 **插件** → **安装** → **本地上传**
|
||||
4. 选择下载的 `.jar` 文件上传
|
||||
5. 安装完成后启用插件
|
||||
|
||||
@@ -64,8 +72,8 @@ pnpm dev
|
||||
|
||||
## 文档
|
||||
|
||||
完整文档请访问 [AI回评文档站](https://nxxy335.top/comment-ai-autopilot)
|
||||
完整文档及更新日志请访问 [AI回评文档站](https://nxxy335.top/comment-ai-autopilot)
|
||||
|
||||
## 许可证
|
||||
|
||||
[GPL-3.0](./LICENSE) © 暖心向阳335
|
||||
[GPL-3.0](https://github.com/sunny-335/plugin-comment-ai-autopilot/blob/main/LICENSE) © 暖心向阳335
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ plugins {
|
||||
}
|
||||
|
||||
group 'top.nxxy335.commentaiautopilot'
|
||||
version '1.0.0-beta.1'
|
||||
version project.property('version')
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@@ -41,13 +41,14 @@ export default defineConfig({
|
||||
text: "其他",
|
||||
items: [
|
||||
{ text: "常见问题", link: "/guide/faq" },
|
||||
{ text: "更新日志", link: "/CHANGELOG" },
|
||||
],
|
||||
},
|
||||
],
|
||||
socialLinks: [
|
||||
{
|
||||
icon: "github",
|
||||
link: "https://github.com/nxxy335/plugin-comment-ai-autopilot",
|
||||
link: "https://github.com/sunny-335/plugin-comment-ai-autopilot",
|
||||
},
|
||||
],
|
||||
search: {
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
# 更新日志
|
||||
|
||||
## v1.2.1
|
||||
|
||||
> 2026-07-01
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- **修复 `AiReplyOrchestrator.retryOrFail` 重试计数失效** — `.then()` 丢弃了更新后的记录导致 `retryCount` 始终为 0,AI 生成失败时陷入无限重试。改为 `.flatMap()` 传递更新后的记录
|
||||
- **修复误报反馈"AI 回复"被空字符串覆盖** — `.subscribe()` 在异步流程中过早触发,导致 AI 回复生成完成后被空字符串覆盖。改为在 `.doOnSuccess()` 中触发异步生成
|
||||
- **修复 `PersonaResolver` 在响应式上下文中使用 `.block()`** — 调用阻塞方法会阻塞 Reactor 线程。改为返回 `Mono<String>` 并使用 `Flux.concatMap().next()` 替代 for 循环
|
||||
- **修复 `penalizeComment`/`penalizeReply` 缺少乐观锁重试** — 并发更新 Comment/Reply 时可能静默失败。添加 `Retry.backoff(3, 100ms)` 重试
|
||||
- **修复 `approveOriginalComment` 缺少乐观锁重试** — 同上,添加 `Retry.backoff(3, 100ms)` 重试
|
||||
- **修复误报反馈端点无法重试 `FAIL` 状态记录** — 仅接受 `FILTERED` 和 `FALSE_POSITIVE` 状态,AI 生成失败的记录无法重试。现接受 `FAIL` 状态
|
||||
- **修复 `tag-NEUTRAL` 缺少 CSS 样式** — 中性情感标签无样式显示。补充样式定义
|
||||
- **修复 `handleTriggerAiReply` 缺少加载保护** — 触发 AI 回复按钮可被重复点击导致重复提交。添加 loading 状态
|
||||
- **修复 `filterKeyword` 输入未做防抖** — 每次按键都触发搜索,性能开销大。添加 300ms 防抖
|
||||
- **修复 `performCleanup` 逻辑错误** — 清理逻辑存在判断错误
|
||||
|
||||
### 改进
|
||||
|
||||
- **优化 `extractChoice` 分类匹配优先级** — 优先匹配违规类别(advertising/abuse/sensitive/meaningless),再匹配 `normal`,避免正常评论被误判为违规类别
|
||||
|
||||
---
|
||||
|
||||
## v1.2.0
|
||||
|
||||
> 2026-06-25
|
||||
|
||||
### 新增
|
||||
|
||||
- **误报反馈功能** — 被拦截的评论可进行误报反馈,支持两种处理方式:
|
||||
- **AI 回复**:标记为通过 + 触发 AI 生成回复
|
||||
- **仅通过**:仅标记为通过,不生成回复
|
||||
- **误报通过状态** — 新增 `FALSE_POSITIVE` 状态,"仅通过"的记录显示为"误报通过",不显示"通过/拒绝"按钮
|
||||
- **触发 AI 回复按钮** — "误报通过"状态的记录可随时点击"触发AI回复"按钮补生成 AI 回复
|
||||
- **上下文优先判断原则** — 前置过滤 AI 提示词重写,遵循五条核心原则:上下文优先、口语化宽容、恶意导向判定、宁放勿杀、闲聊不算无意义
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- **修复误报反馈"AI 回复"被前置过滤再次拦截** — `processComment()` 始终调用 `preFilterService.check()`,用户已确认为误报的评论会被再次拦截。新增 `processFalsePositive()` 方法跳过前置过滤和去重检查
|
||||
- **修复误报反馈"AI 回复"被去重检查拦截** — `hasExistingReply()` 找到已有的 FILTERED→PENDING 记录导致 AI 回复无法生成。`processFalsePositive()` 复用已有记录,不经过去重检查
|
||||
- **修复误报反馈"AI 回复"导致全站崩溃** — `processComment()` 同步等待 AI 生成完成,HTTP 请求长时间不返回。改为 `.subscribe()` 异步执行,API 立即返回
|
||||
- **修复误报反馈"仅通过"后显示通过/拒绝按钮** — "仅通过"将记录设为 `status=PASS, published=false, reply=""`,导致显示"通过/拒绝"按钮且内容为空。改为 `status=FALSE_POSITIVE`
|
||||
- **修复 `extractChoice` 无匹配时返回原始文本** — AI 返回非预期文本时被误判为违规类别。改为返回空字符串触发安全拦截
|
||||
- **修复 `approveOriginalComment` 缺少乐观锁重试** — 并发更新 Comment/Reply 时可能静默失败。添加 `Retry.backoff(3, 100ms)` 重试
|
||||
|
||||
### 改进
|
||||
|
||||
- **消除 `checkBlockedCommenters` 重复代码** — `FilterService` 新增 `isCommenterBlocked(commentName)` 公共方法,`AiReplyOrchestrator` 改为调用它
|
||||
- **前端批量操作防重复提交** — 批量通过/拒绝/删除按钮添加 `batchLoading` 状态,操作期间禁用按钮
|
||||
|
||||
---
|
||||
|
||||
## v1.1.2
|
||||
|
||||
> 2026-06-24
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- **修复 AI 分类完全不可用** — `classifyWithChoice` 和 `classifyWithChat` 均使用了 `GenerateTextRequest.Builder.system()` 方法,而该方法在当前 AI Foundation 版本中不被支持或导致运行时错误,导致所有评论均被拦截并显示"AI分类服务不可用,安全拦截"。现改为将 system prompt 合并到 user prompt 中,与可用的 `chat()` 方法保持一致的调用方式
|
||||
- **修复 `classifyWithChoice` NPE** — `.map()` 返回 `null` 时触发 Reactor 内部 NullPointerException,改为 `.flatMap()` + `Mono.empty()` 正确触发 fallback
|
||||
|
||||
### 改进
|
||||
|
||||
- **分类调用诊断日志增强** — 在 `AiFoundationDelegate`、`AiFoundationClient`、`CommentPreFilterService` 中增加关键诊断日志(分类开始、fallback 触发、分类结果、异常详情),便于排查分类链路问题
|
||||
- **AI 分类空结果处理** — 当 AI 返回空字符串时单独拦截,区别于"服务不可用"场景
|
||||
|
||||
---
|
||||
|
||||
## v1.1.1
|
||||
|
||||
> 2026-06-24
|
||||
|
||||
### 改进
|
||||
|
||||
- **"无意义"分类范围收窄** — 与文章主题无关的闲聊、灌水、打招呼不再被判为"无意义",仅纯乱码和无意义字符堆砌(如随机符号、键盘乱敲)才归类为"无意义"
|
||||
- **AI 分类降级方案** — 当 `OutputSpec.choice` 结构化输出不被模型支持时,自动退回到普通 chat 调用并从响应文本中提取分类值(`classifyWithChat` fallback)
|
||||
|
||||
---
|
||||
|
||||
## v1.1.0
|
||||
|
||||
> 2026-06-23
|
||||
|
||||
### 新增
|
||||
|
||||
- **评论前置过滤(合规检测)** — AI 回复前对评论进行合规性分类,识别广告/辱骂攻击/敏感内容/无意义内容,违规评论停止生成 AI 回复,节省 Token
|
||||
- **违规评论自动设为待审核** — 检测到违规评论时自动将原评论 `approved` 置为 `false`,进入待审核队列,前端不再展示该评论
|
||||
- **FILTERED 日志状态** — 被拦截的评论生成"已拦截"状态记录,日志页支持按"已拦截"状态筛选
|
||||
- **拦截原因分类标签** — 日志页显示拦截分类标签(广告/辱骂攻击/敏感内容/无意义)和详细拦截原因(含评论内容摘要)
|
||||
- **安全优先策略** — AI 分类服务不可用或异常时,默认拦截评论而非放行,防止违规内容漏网
|
||||
|
||||
### 改进
|
||||
|
||||
- **AI Foundation 隔离加载** — 将 AI Foundation API 引用隔离到 `AiFoundationDelegate` 类,`AiFoundationClient` 不再直接引用 AI Foundation 类,修复未安装 AI Foundation 时插件无法启动的问题(`NoClassDefFoundError`)
|
||||
- **评论内容 HTML 剥离** — 前置过滤检测前自动剥离评论 HTML 标签,提升 AI 分类准确性
|
||||
- **对话场景精准处罚** — AI 对话场景下违规内容来自 Reply 时,仅取消通过该 Reply 而非父级 Comment,避免误伤
|
||||
- **升级配置自动迁移** — 从 v1.0.x 升级时自动将 `preFilterEnabled` 从 `false` 迁移为 `true`(新默认值)
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- **修复未安装 AI Foundation 时插件无法启动** — `BeanDefinitionStoreException: Failed to parse AiFoundationClient`,将 AI Foundation API 引用隔离到委托类
|
||||
- **修复前置过滤默认关闭** — `preFilterEnabled` 默认值从 `false` 改为 `true`,新安装和升级用户均默认启用
|
||||
- **修复 `penalize()` 遗漏 `approved=null`** — Halo 评论创建时 `approved` 可能为 `null`,原代码仅处理 `approved=true` 的情况
|
||||
- **修复 `classify()` 失败时放行违规评论** — `defaultIfEmpty` 和 `onErrorResume` 改为拦截而非放行
|
||||
- **修复 Windows 构建失败** — Gradle Worker Daemon 执行 pnpm 退出码 268435659,改用系统 pnpm Exec 任务并禁用 Daemon
|
||||
|
||||
---
|
||||
|
||||
## v1.0.4
|
||||
|
||||
> 2026-06-19
|
||||
|
||||
### 改进
|
||||
|
||||
- **对话弹窗头像显示** — 对话弹窗中每条消息显示 Gravatar 头像,基于评论者或 AI 角色的邮箱自动匹配
|
||||
- **对话引用摘要** — 对话弹窗中回复消息显示引用摘要框,标明引用了谁的什么内容,支持截断显示
|
||||
- **UI 全面重构** — LogsView 和 SettingsView 改用纯 Scoped CSS,移除所有 Tailwind 类和自定义 CSS 依赖,避免 Halo 主题冲突
|
||||
- **标签去 Emoji 化** — 状态、情感标签改用纯色背景标签,去除所有 Emoji
|
||||
- **移动端适配优化** — 全面优化移动端响应式布局,解决排版错位问题
|
||||
- **AI角色设置完善** — 支持 CRUD、Gravatar 头像预览、性别/唤醒词/默认角色配置
|
||||
- **配置导入导出** — 支持将插件配置(ConfigMap + AI角色)导出为 JSON 文件,方便备份和迁移
|
||||
- **评论者黑名单弹窗选择** — 设置页面可从已有评论列表中选择评论者添加到黑名单
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- **修复对话弹窗引用溯源** — 后端 `getConversation` 重写,构建 Reply 映射字典正确溯源引用关系
|
||||
- **修复 ConversationMessage 数据结构** — 新增 `quoteOwner`/`quoteContent` 字段支持引用摘要展示
|
||||
- **修复 AI 角色邮箱提取** — 后端新增 `extractOwnerEmail` 方法,正确从 CommentOwner 提取邮箱用于头像生成
|
||||
|
||||
---
|
||||
|
||||
## v1.0.3
|
||||
|
||||
### 改进
|
||||
|
||||
- **SettingsView 完整功能版** — 5个设置面板(基本设置、AI角色、模型设置、Prompt、数据清理)全部实现
|
||||
- **AI角色管理** — 支持 CRUD、Gravatar 头像、性别/唤醒词/默认角色配置
|
||||
- **数据清理** — 自动清理开关、保留天数滑块、手动清理
|
||||
- **导入导出** — JSON 配置导入导出
|
||||
- **评论者黑名单弹窗选择** — 从已有评论列表中选择评论者
|
||||
|
||||
---
|
||||
|
||||
## v1.0.2
|
||||
|
||||
### 改进
|
||||
|
||||
- **LogsView & SettingsView 样式重构** — 移除所有 Tailwind 类,改用 `<style scoped>` 原生 CSS
|
||||
- **标签配色、气泡样式、引用框** — 全部使用纯 CSS 实现,避免 Halo 主题冲突
|
||||
|
||||
---
|
||||
|
||||
## v1.0.1
|
||||
|
||||
### 改进
|
||||
|
||||
- **版本号升级** — 强制刷新 Halo 前端缓存
|
||||
- **历史数据兼容** — LogsView 增加历史 Markdown 引用文本清理正则,防止旧版测试数据套娃显示
|
||||
|
||||
---
|
||||
|
||||
## v1.0.0
|
||||
|
||||
> 2026-06-18
|
||||
|
||||
### 新功能
|
||||
|
||||
- **唤醒词**:评论以唤醒词开头可唤醒指定角色回复,支持自定义唤醒词,可在未启用AI回评的页面使用唤醒词召唤AI,二级评论同样支持
|
||||
- **性别配置**:AI角色支持性别设置(男/女),AI回复时会保持对应性别身份
|
||||
- **语气风格**:支持中性语气复选框,勾选后使用中性语气,取消勾选则跟随性别语气(女性温柔细腻/男性沉稳理性)
|
||||
- **身份提示词强化**:角色身份信息前置到Prompt最开头(【核心身份】),安全规范中增加身份约束,确保AI始终保持角色身份
|
||||
|
||||
### 改进
|
||||
|
||||
- **优化情感分析系统**:从 3 级分类(正面/中性/负面)升级为 5 级分类(非常正面/正面/中性/负面/非常负面),情感判断更精细
|
||||
- **优化日志页面 UI**:批量操作按钮重写样式,确保底色和白色文字清晰可见;搜索框添加搜索图标;重置按钮添加图标和底色
|
||||
- **优化评分显示**:评分数字与等级标签之间添加间距,等级标签增加底色背景(优秀/良好/一般/较差)
|
||||
- **优化状态标签**:通过状态、发布状态、情感标签统一使用带底色的标签样式
|
||||
- **支持页面链接显示**:日志中新增独立页面(SinglePage)链接显示,之前仅支持文章链接
|
||||
- **移动端适配**:仪表盘、配置、日志页面全面适配移动端
|
||||
- **ObjectMapper 统一注入**:FilterService 和 PromptBuilder 中的 `new ObjectMapper()` 改为 Spring 构造函数注入
|
||||
- **服务端过滤优化**:日志列表查询改用 `Queries.equal()` 服务端过滤 status/sentiment,减少内存过滤开销
|
||||
- **新增索引**:为 AiCommentReply 扩展添加 `spec.sentiment`、`spec.published`、`spec.postKind` 索引
|
||||
- **新增 postKind 字段**:区分关联内容类型(Post/SinglePage),支持页面评论的链接生成
|
||||
- **PromptBuilder 情感提示**:适配 5 级情感分类,新增 VERY_POSITIVE 和 VERY_NEGATIVE 的语气提示
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- **修复 ObjectMapper Bean 不存在**:Halo 插件上下文中没有自动注册 ObjectMapper Bean,创建 ObjectMapperConfiguration 手动注册
|
||||
- **修复 AI 回复仍说没有性别**:将身份信息前置到 Prompt 最开头,安全规范中删除"作为AI助手"措辞,新增身份约束
|
||||
- **修复唤醒词无法唤醒**:评论内容提取时对 raw 也做 HTML strip(Jsoup.clean),所有内容做 trim(),wakeWord 也做 trim()
|
||||
- **修复二级评论唤醒词检查位置错误**:唤醒词检查提前到 isReplyToAi 判断之前
|
||||
- **修复 SinglePage 内容获取 404**:PostContentService 不能用于 SinglePage,改用 SinglePage.getStatus().getExcerpt()
|
||||
- **修复 Post/SinglePage 404 容错**:fetch 添加 onErrorResume 降级为空上下文继续处理
|
||||
- **修复 Sort 参数 null 警告**:listAll 调用改为 Sort.unsorted()
|
||||
|
||||
---
|
||||
|
||||
## v1.0.0-beta.2
|
||||
|
||||
> 2026-06-17
|
||||
|
||||
### 改进
|
||||
|
||||
- **改用 ExtensionGetter 集成 AI Foundation**:通过 Halo 官方推荐的 `ExtensionGetter.getEnabledExtension(AiModelService.class)` 获取 AI 服务,替换原先的跨 ClassLoader 反射调用方式([Issue #1](https://github.com/sunny-335/plugin-comment-ai-autopilot/issues/1))
|
||||
- **声明插件依赖**:在 `plugin.yaml` 中声明可选插件依赖 `ai-foundation?: "*"`,建立正确的插件依赖关系,插件在未安装 AI Foundation 时仍可正常加载
|
||||
- **应用市场推荐**:新增 `store.halo.run/recommended-apps` 注解,安装本插件后可在应用市场推荐安装 AI Foundation 插件
|
||||
- **使用结构化输出**:情感分析和内容审核改用 AI Foundation 的 `OutputSpec.choice` 结构化输出,替换原先的字符串匹配解析,分类更可靠
|
||||
- **使用 GenerateTextRequest**:AI 调用改用 `GenerateTextRequest` 并设置 `maxRetries=2`,由 SDK 自动重试瞬时错误
|
||||
- **多轮对话上下文**:AI 对话续接时自动获取之前的回复历史并注入到 Prompt 中,AI 能更好地理解对话上下文
|
||||
- **优化 AI 自审核评分机制**:审核改为两阶段评估(安全检查 + 质量评分 1-5 分),评分映射到 0-100 分(0/30/50/70/85/100),替代原先的二值评分(0/100),评分更有区分度
|
||||
- **精简仪表盘**:移除情感分布、近7日回复趋势、平均审核评分三个卡片,快捷操作精简为回复日志、插件设置、刷新数据
|
||||
- **重做设置页面**:基本设置、AI角色设置、模型设置、Prompt设置、数据清理各为独立页面,通过标签栏切换
|
||||
- **优化设置页面布局**:按钮统一排版并添加图标,侧边栏保存卡片高亮显示,新增"未保存"状态指示器
|
||||
- **优化日志页面**:评分增加等级标签(优秀/良好/一般/较差),筛选下拉框修复文本与箭头重叠
|
||||
- **优化 AI Foundation 状态提示宽度**:状态提示与内容区宽度一致
|
||||
- **优化插件文档**:修复版本要求(2.23→2.25)、变量名(`{{conversation}}`→`{{conversation_history}}`)、GitHub 链接(`nxxy335`→`sunny-335`)、Cravatar 链接(`cravatar.cn`→`cn.cravatar.com`)等错误,补充缺失的配置项文档
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- **修复自动发布后日志显示未发布**:`generateAndPublish` 中 `Mono<Void>` 的 empty 信号触发 `switchIfEmpty` 导致 `publishReply` 被调用两次,第二次覆盖 `published=false`
|
||||
- **修复 RateLimitService 线程泄漏**:清理线程未在插件停止时关闭,实现 `DisposableBean` 正确释放资源
|
||||
- **修复 ReviewService 提示词不匹配**:审核提示词要求"重新生成"但代码未使用重新生成的内容,移除误导性指令
|
||||
- **修复设置页面按钮图标文字对齐**:通过 `:deep(.btn-content)` 设置 inline-flex 布局,图标和文字并排显示
|
||||
- **修复设置页面标签栏无法点击切换**:替换不工作的 VTabbar 组件为自定义按钮实现
|
||||
- **修复设置页面配置区域布局错误**:将标签栏移出 grid 容器,避免挤占配置区域宽度
|
||||
- **修复日志页面筛选下拉框文本与箭头重叠**:将 `px-3` 改为 `pl-3 pr-8` 为下拉箭头预留空间
|
||||
|
||||
---
|
||||
|
||||
## v1.0.0-beta.1
|
||||
|
||||
> 2026-06-05
|
||||
|
||||
### 新功能
|
||||
|
||||
- **自动回复**:监听新评论,自动调用 AI 生成回复
|
||||
- **多语言适配**:根据评论语言自动用对应语言回复
|
||||
- **情感分析**:分析评论情感倾向,根据情感调整回复语气
|
||||
- **草稿模式**:关闭"自动发布"后,AI 回复将保存为草稿,需站长审核后才发布
|
||||
- **多 AI 角色支持**:支持配置多个 AI 虚拟角色,每个角色有独立的提示词、头像和模型
|
||||
- **提示词预设**:内置多种回复风格预设(专业型、幽默型、简洁型等),可自由组合
|
||||
- **文章/页面级开关**:在文章编辑器中直接控制是否启用 AI 回复
|
||||
- **评论者黑名单**:屏蔽指定评论者,不触发 AI 回复
|
||||
- **安全审核**:AI 生成的内容经过安全审核,不合规内容自动拒绝
|
||||
- **Prompt 模板**:支持自定义 Prompt 模板,提供多种模板变量
|
||||
- **仪表盘统计**:显示回复数等统计信息
|
||||
- **日志筛选搜索**:按状态、情感筛选,关键词搜索
|
||||
- **数据清理**:自动清理超过指定天数的旧记录
|
||||
- **AI Foundation 集成**:使用 AI Foundation 插件提供的 AI 模型能力
|
||||
- **对话历史查看**:支持查看 AI 回复的完整对话上下文
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- 修复草稿模式下审批失败("AI回复已存在,无法重复发布")的问题
|
||||
- 修复批量审批时同样的去重检查冲突问题
|
||||
- 修复 AI Foundation 不可用的问题(`PluginManager` 无法通过 Spring 依赖注入获取)
|
||||
- 修复 `DefaultSpringPlugin` 包级私有类反射访问权限问题
|
||||
- 修复 CI 构建失败(`gradlew` 缺少执行权限)
|
||||
|
||||
### 改进
|
||||
|
||||
- 审批逻辑优化:先查找已有 Reply 扩展再决定创建或更新
|
||||
- 移除不必要的 `AiFoundationConfiguration` 配置类
|
||||
- 前端 UI 优化:移除编辑功能、简化角色排序逻辑、清理无用代码
|
||||
@@ -21,6 +21,32 @@
|
||||
|
||||
插件启动时间之前的评论不会触发自动回复,避免安装插件后对大量历史评论批量回复。
|
||||
|
||||
## 唤醒词机制
|
||||
|
||||
唤醒词允许用户在评论中通过特定词语唤醒指定AI角色回复,即使该页面未启用AI回评。
|
||||
|
||||
### 工作方式
|
||||
|
||||
1. 用户发表以唤醒词开头的评论(如"小回小回你好")
|
||||
2. 插件检测到唤醒词匹配,自动唤醒对应角色
|
||||
3. 唤醒词后的内容作为实际评论内容传递给AI
|
||||
4. AI生成回复时自动获取上下文(文章内容、对话历史等)
|
||||
|
||||
### 唤醒词特点
|
||||
|
||||
- **跨页面唤醒**:在未启用AI回评的页面也能使用唤醒词召唤AI
|
||||
- **二级评论支持**:回复中同样可以使用唤醒词
|
||||
- **独立唤醒**:每个角色有独立的唤醒词,可以唤醒不同角色
|
||||
- **绕过限制**:唤醒词触发时绕过页面级启用检查和"必须是回复给AI"的检查,但仍检查黑名单
|
||||
|
||||
### 配置唤醒词
|
||||
|
||||
在 **AI回评** → **插件设置** → **AI角色设置** 中,为每个角色配置唤醒词。唤醒词留空则不启用该角色的唤醒功能。
|
||||
|
||||
::: tip
|
||||
唤醒词建议设置为容易记忆且不易与正常评论混淆的词语。
|
||||
:::
|
||||
|
||||
## 对话式回复
|
||||
|
||||
当评论者回复AI的评论时,插件会自动提取对话上下文(最近5条回复),让AI的回复更连贯自然。
|
||||
@@ -31,6 +57,8 @@
|
||||
|--------|------|--------|
|
||||
| 自动回复 | 是否启用自动回复功能 | 开启 |
|
||||
| 自动发布 | AI回复是否自动发布,关闭则存为草稿 | 开启 |
|
||||
| 最大对话轮次 | 同一评论线程中AI最多自动回复的轮次 | 8 |
|
||||
| 速率限制 | 每分钟最大AI回复数量 | 10 |
|
||||
| 最大重试次数 | AI生成失败时的最大重试次数 | 3 |
|
||||
|
||||
## 重试机制
|
||||
|
||||
@@ -22,6 +22,18 @@
|
||||
- 草稿记录显示 **审核通过** 和 **拒绝** 按钮
|
||||
- 已发布的记录显示正常状态
|
||||
- 被拒绝的记录显示 REJECTED 标签
|
||||
- 失败的记录显示 FAIL 标签,并显示重试次数
|
||||
- 每条记录可点击 **查看对话** 查看完整对话上下文
|
||||
|
||||
## 对话上下文查看
|
||||
|
||||
点击日志记录的 **查看对话** 按钮,弹出对话上下文窗口:
|
||||
|
||||
- 以气泡形式展示完整对话(评论 + 所有回复)
|
||||
- AI 回复和用户回复以不同颜色气泡区分
|
||||
- 每条消息显示发送者头像(通过 Gravatar 服务生成)
|
||||
- 回复消息显示引用摘要框,标明该回复引用了哪条消息
|
||||
- 支持移动端响应式布局
|
||||
|
||||
## 批量操作
|
||||
|
||||
|
||||
+56
-1
@@ -12,7 +12,7 @@
|
||||
|
||||
1. 确认在插件设置中填写了AI角色邮箱
|
||||
2. 邮箱需要在 [Gravatar](https://gravatar.com) 上注册并设置头像
|
||||
3. 插件使用 [Cravatar](https://cravatar.cn) 作为Gravatar镜像服务
|
||||
3. 插件使用 [Cravatar](https://cn.cravatar.com) 作为Gravatar镜像服务
|
||||
|
||||
## 评论没有触发AI回复?
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
3. **已有AI回复记录** — 同一评论不会重复触发
|
||||
4. **历史评论** — 插件启动前的评论不会自动触发,可使用手动触发
|
||||
5. **AI生成失败** — 检查AI模型配置和日志
|
||||
6. **被前置过滤拦截** — 若启用"前置过滤",违规评论会被拦截,可在日志页通过"已拦截"状态筛选查看
|
||||
|
||||
## 如何对历史评论触发AI回复?
|
||||
|
||||
@@ -66,3 +67,57 @@
|
||||
## 黑名单支持邮箱吗?
|
||||
|
||||
支持。黑名单同时匹配评论者的显示名称和邮箱地址,不区分大小写。你也可以在设置页面点击"添加评论者"按钮从评论列表中选择。
|
||||
|
||||
## 对话窗口中的头像是怎么来的?
|
||||
|
||||
对话窗口中每条消息的头像通过 [Gravatar](https://gravatar.com) 服务生成(使用 [Cravatar](https://cn.cravatar.com) 镜像)。头像基于评论者或 AI 角色的邮箱自动匹配。如果未设置邮箱,则显示默认图标。
|
||||
|
||||
## 对话窗口中的引用框是什么?
|
||||
|
||||
当一条回复是针对另一条回复的(即层级回复),对话窗口会在该消息气泡内显示一个引用摘要框,标明该回复引用了谁的什么内容。引用内容会截断显示(最多35个字符),方便快速了解对话脉络。
|
||||
|
||||
## 如何备份和迁移插件配置?
|
||||
|
||||
在插件设置页面顶部点击 **导出** 按钮,将当前配置导出为 JSON 文件。在目标实例中点击 **导入** 按钮选择该文件即可恢复配置。导入会覆盖当前配置,请谨慎操作。
|
||||
|
||||
## AI Foundation 显示"部分功能不可用"怎么办?
|
||||
|
||||
这通常表示 AI Foundation 插件未正确配置模型。请检查:
|
||||
|
||||
1. AI Foundation 插件已安装并启用
|
||||
2. 在 AI Foundation 中配置了至少一个 AI 模型
|
||||
3. 如果回评插件未指定模型名称,将使用 AI Foundation 的默认模型
|
||||
|
||||
## 前置过滤会误伤正常评论吗?
|
||||
|
||||
前置过滤默认启用。AI 会对评论进行分类判断,若 AI 服务不可用或分类失败,为安全起见会拦截评论而非放行。如果你发现正常评论被误拦截,可以在设置中关闭"启用前置过滤"开关。被拦截的评论会在日志页生成一条"已拦截"状态的记录,可查看具体分类标签和拦截原因。
|
||||
|
||||
## 所有评论都显示"AI分类服务不可用,安全拦截"怎么办?
|
||||
|
||||
这表示 AI 分类调用链路存在问题,可能的原因:
|
||||
|
||||
1. **AI Foundation 插件未安装或未启用** — 请确保 AI Foundation 插件已正确安装并启用
|
||||
2. **AI Foundation 中未配置模型** — 请在 AI Foundation 中配置至少一个 AI 模型
|
||||
3. **模型名称配置错误** — 检查插件设置中的模型名称是否与 AI Foundation 中的 AiModel 资源名称一致,留空则使用默认模型
|
||||
4. **AI Foundation 版本过旧** — 请确保使用最新版本的 AI Foundation 插件
|
||||
|
||||
::: tip 排查步骤
|
||||
1. 检查插件设置页面顶部的 AI Foundation 连接状态
|
||||
2. 查看插件日志中 `[Delegate]` 和 `[PreFilter]` 前缀的诊断信息
|
||||
3. 确认 AI 回复功能(非前置过滤)是否正常工作 — 如果 AI 回复也无法生成,说明是 AI Foundation 连接问题
|
||||
:::
|
||||
|
||||
## 被前置过滤拦截的评论会怎样?
|
||||
|
||||
1. **停止生成 AI 回复** — 不会消耗后续 Token
|
||||
2. **创建拦截记录** — 在日志页显示为"已拦截"状态,标注分类标签(如"辱骂攻击")和详细原因(含评论内容摘要)
|
||||
3. **自动设为待审核** — 原评论的 `approved` 会被置为 `false`,前端不再展示该评论,需人工判断后审核通过
|
||||
|
||||
## 被误拦截的评论怎么处理?
|
||||
|
||||
在日志页的"已拦截"记录右侧,点击 **误报反馈** 按钮,可选择:
|
||||
|
||||
- **AI 回复** — 标记为误报 + 自动通过评论 + 触发 AI 生成回复
|
||||
- **仅通过** — 仅标记为误报 + 自动通过评论,不生成 AI 回复
|
||||
|
||||
选择"仅通过"后,记录状态变为"误报通过",可随时点击 **触发AI回复** 按钮补生成 AI 回复。
|
||||
|
||||
+11
-3
@@ -32,9 +32,17 @@
|
||||
|
||||
1. 进入插件设置页面
|
||||
2. 在 **基本设置** 中找到 **评论者黑名单**
|
||||
3. 输入评论者的显示名称或邮箱,多个用逗号分隔
|
||||
3. 输入评论者的显示名称、邮箱或正则表达式,多个用逗号分隔
|
||||
4. 保存设置
|
||||
|
||||
### 支持的格式
|
||||
|
||||
| 格式 | 示例 | 说明 |
|
||||
|------|------|------|
|
||||
| 名称 | `张三` | 匹配评论者的显示名称 |
|
||||
| 邮箱 | `spam@example.com` | 匹配评论者邮箱(不区分大小写) |
|
||||
| 正则表达式 | `regex:^spam.*` | 以 `regex:` 开头,按正则匹配 |
|
||||
|
||||
### 从评论列表选择
|
||||
|
||||
1. 在黑名单输入框旁点击 **添加评论者** 按钮
|
||||
@@ -45,7 +53,7 @@
|
||||
### 示例
|
||||
|
||||
```
|
||||
张三,spam@example.com,李四
|
||||
张三, spam@example.com, 李四, regex:^spam.*
|
||||
```
|
||||
|
||||
黑名单中的评论者发布评论时,插件会同时匹配显示名称和邮箱地址(不区分大小写),匹配成功则跳过AI回复。
|
||||
黑名单中的评论者发布评论时,插件会同时匹配显示名称和邮箱地址(不区分大小写),正则表达式则按模式匹配,匹配成功则跳过AI回复。
|
||||
|
||||
@@ -2,24 +2,28 @@
|
||||
|
||||
## 前置要求
|
||||
|
||||
- Halo 2.23+
|
||||
- AI Foundation 插件(必须) — 本插件依赖 AI Foundation 提供的AI模型能力,请先安装并配置 AI Foundation
|
||||
- Halo 2.25+
|
||||
- AI Foundation 插件(必须) — 本插件通过 `ExtensionGetter` 调用 AI Foundation 提供的 `AiModelService` 扩展点,请先安装并配置 AI Foundation
|
||||
|
||||
## 安装
|
||||
|
||||
### 方式一:从 Release 下载
|
||||
### 方式一:应用商店安装
|
||||
|
||||
1. 前往 [GitHub Releases](https://github.com/nxxy335/plugin-comment-ai-autopilot/releases) 下载最新的 `.jar` 文件
|
||||
进入 **插件** → **安装** → 应用市场搜索 **AI回评** → 安装,或前往 [Halo 应用商店](https://www.halo.run/store/apps/app-mo5tivjt) 一键安装。
|
||||
|
||||
### 方式二:从 Release 下载
|
||||
|
||||
1. 前往 [GitHub Releases](https://github.com/sunny-335/plugin-comment-ai-autopilot/releases) 下载最新的 `.jar` 文件
|
||||
2. 登录 Halo 管理后台
|
||||
3. 进入 **插件** → **已安装** → 点击右上角 **安装** 按钮
|
||||
3. 进入 **插件** → **安装** → **本地上传**
|
||||
4. 选择下载的 `.jar` 文件上传
|
||||
5. 安装完成后启用插件
|
||||
|
||||
### 方式二:从源码构建
|
||||
### 方式三:从源码构建
|
||||
|
||||
```bash
|
||||
# 克隆仓库
|
||||
git clone https://github.com/nxxy335/plugin-comment-ai-autopilot.git
|
||||
git clone https://github.com/sunny-335/plugin-comment-ai-autopilot.git
|
||||
cd plugin-comment-ai-autopilot
|
||||
|
||||
# 构建
|
||||
|
||||
+27
-17
@@ -5,45 +5,55 @@ AI回评(Comment AI Autopilot)是一个 Halo 博客系统的插件,能够
|
||||
## 核心功能
|
||||
|
||||
- **自动回复** — 监听新评论,自动调用AI生成回复,支持多轮对话上下文
|
||||
- **多 AI 角色** — 支持创建多个 AI 角色,每个角色有独立的昵称、人格提示词、性别、语气风格和 Gravatar 头像,可为不同文章指定不同角色
|
||||
- **唤醒词** — 评论以唤醒词开头可唤醒指定角色回复,支持自定义唤醒词,可在未启用AI回评的页面使用唤醒词召唤AI
|
||||
- **性别与语气** — AI角色支持性别配置(男/女)和中性语气风格,AI回复时保持对应性别身份
|
||||
- **多语言适配** — 根据评论语言自动用对应语言回复
|
||||
- **情感分析** — 分析评论情感倾向(正面/中性/负面),根据情感调整回复语气
|
||||
- **情感分析** — 分析评论情感倾向(非常正面/正面/中性/负面/非常负面),根据情感调整回复语气
|
||||
- **草稿模式** — AI回复先存为草稿,管理员审核后再发布
|
||||
- **失败重试** — AI生成失败时自动重试,指数退避策略
|
||||
- **批量操作** — 草稿模式下支持批量通过/拒绝/删除
|
||||
- **文章/页面级开关** — 在文章编辑器中直接控制是否启用AI回复,文章默认开启,页面默认关闭
|
||||
- **评论者黑名单** — 屏蔽指定评论者,不触发AI回复
|
||||
- **评论者黑名单** — 屏蔽指定评论者,不触发AI回复,支持名称、邮箱和正则表达式
|
||||
- **前置过滤(合规检测)** — AI回复前对评论进行合规性分类,自动拦截广告/辱骂/敏感/乱码内容,节省Token;可选将违规评论设为待审核状态
|
||||
- **误报反馈** — 被误拦截的评论可进行误报反馈,支持"AI回复"和"仅通过"两种处理方式,"仅通过"后可随时补触发 AI 回复
|
||||
- **手动触发** — 在评论管理页面对历史评论手动触发AI回复
|
||||
- **AI角色** — 自定义AI回复者的昵称、人格提示词和Gravatar头像
|
||||
- **安全审核** — AI生成的内容经过安全审核,不合规内容自动拒绝
|
||||
- **仪表盘统计** — 显示回复数、情感分布、每日回复趋势等图表
|
||||
- **安全审核** — AI生成的内容经过两阶段安全审核(安全检查 + 质量评分),不合规内容自动拒绝
|
||||
- **Prompt 预设** — 内置友好型、专业型、幽默型、简洁型预设风格,可多选组合
|
||||
- **对话轮次限制** — 同一评论线程中限制 AI 最多回复轮次,防止无限对话
|
||||
- **速率限制** — 每分钟最大 AI 回复数量,防止批量评论消耗过多额度
|
||||
- **日志筛选搜索** — 按状态、情感筛选,关键词搜索
|
||||
- **对话上下文查看** — 在日志页面查看完整对话上下文,支持引用摘要展示和 Gravatar 头像显示
|
||||
- **数据清理** — 自动清理超过指定天数的旧记录
|
||||
- **AI Foundation 集成** — 必须安装 Halo AI Foundation 插件,使用其提供的AI模型能力
|
||||
- **配置导入导出** — 支持将插件配置导出为 JSON 文件,方便备份和迁移
|
||||
- **AI Foundation 集成** — 通过 Halo 官方推荐的 `ExtensionGetter` 获取 AI 服务,需安装 AI Foundation 插件
|
||||
|
||||
## 工作流程
|
||||
|
||||
```
|
||||
新评论 → 过滤检查 → 情感分析 → 构建Prompt → AI生成 → 安全审核 → 发布/草稿
|
||||
新评论 → 唤醒词检查 → 过滤检查 → 前置过滤(合规检测) → 情感分析 → 构建Prompt → AI生成 → 安全审核 → 发布/草稿
|
||||
↓ (失败)
|
||||
重试 → ... → 最终失败
|
||||
```
|
||||
|
||||
1. **新评论到达** — Reconciler 监听到新评论创建事件
|
||||
2. **过滤检查** — 检查文章/页面是否启用AI回复、评论者是否在黑名单中
|
||||
3. **情感分析** — 调用AI分析评论情感倾向
|
||||
4. **构建Prompt** — 结合AI角色人格、情感提示、文章内容、评论上下文构建Prompt
|
||||
5. **AI生成** — 调用AI模型生成回复内容
|
||||
6. **安全审核** — 对生成内容进行安全审核
|
||||
7. **发布/草稿** — 根据设置自动发布或存为草稿等待审核
|
||||
8. **重试** — 如果AI生成失败,系统会自动重试(最多 maxRetryCount 次),每次重试间隔递增
|
||||
2. **唤醒词检查** — 检查评论是否以某个角色的唤醒词开头,匹配则唤醒对应角色
|
||||
3. **过滤检查** — 检查文章/页面是否启用AI回复、评论者是否在黑名单中(唤醒词触发时绕过页面级启用检查)
|
||||
4. **前置过滤(合规检测)** — 若启用,AI 对评论内容进行合规性分类(正常/广告/辱骂攻击/敏感内容/无意义)。违规评论将停止后续流程,可选自动设为待审核状态
|
||||
5. **情感分析** — 调用AI分析评论情感倾向
|
||||
6. **构建Prompt** — 结合AI角色人格、情感提示、文章内容、评论上下文构建Prompt
|
||||
7. **AI生成** — 调用AI模型生成回复内容
|
||||
8. **安全审核** — 对生成内容进行两阶段审核(安全检查 + 质量评分 1-5 分映射到 0-100)
|
||||
9. **发布/草稿** — 根据设置自动发布或存为草稿等待审核
|
||||
10. **重试** — 如果AI生成失败,系统会自动重试(最多 maxRetryCount 次),每次重试间隔递增
|
||||
|
||||
## 前置要求
|
||||
|
||||
- Halo 2.23+
|
||||
- AI Foundation 插件(必须) — 本插件依赖 AI Foundation 提供的AI模型能力
|
||||
- Halo 2.25+
|
||||
- AI Foundation 插件(必须) — 本插件通过 `ExtensionGetter` 调用 AI Foundation 提供的 `AiModelService` 扩展点
|
||||
|
||||
## 技术栈
|
||||
|
||||
- **后端**:Java + Spring WebFlux + Reactive
|
||||
- **前端**:Vue 3 + @halo-dev/components
|
||||
- **AI**:支持 AI Foundation 插件集成
|
||||
- **AI**:通过 AI Foundation 插件集成
|
||||
|
||||
@@ -37,3 +37,15 @@ POST /apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/{replyN
|
||||
```
|
||||
|
||||
对指定回复触发对话式AI回复。
|
||||
|
||||
### 更新草稿回复内容
|
||||
|
||||
```
|
||||
PUT /apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/{name}/content
|
||||
```
|
||||
|
||||
更新草稿状态的AI回复内容。请求体为 JSON 格式:`{"reply": "新的回复内容"}`。仅未发布的草稿回复可编辑。
|
||||
|
||||
::: warning
|
||||
已发布的回复不可编辑。
|
||||
:::
|
||||
|
||||
+24
-2
@@ -1,6 +1,6 @@
|
||||
# AI角色
|
||||
|
||||
AI角色定义了回复评论的虚拟身份,包括昵称、人格和头像。
|
||||
AI角色定义了回复评论的虚拟身份,包括昵称、人格、性别、语气风格和头像。
|
||||
|
||||
## 角色配置
|
||||
|
||||
@@ -8,6 +8,28 @@ AI角色定义了回复评论的虚拟身份,包括昵称、人格和头像。
|
||||
|
||||
AI回复者的显示名称,默认为「小回」。修改后新回复将使用新昵称,已有回复不受影响。
|
||||
|
||||
### 性别与语气
|
||||
|
||||
每个角色可以设置性别(男/女),AI回复时会保持对应性别身份。语气风格通过"中性语气"复选框控制:
|
||||
|
||||
- **勾选中性语气**:AI使用中性语气回复
|
||||
- **取消勾选**:AI根据性别使用对应语气风格(女性→温柔细腻,男性→沉稳理性)
|
||||
|
||||
默认角色「小回」的性别为女,勾选中性语气。
|
||||
|
||||
### 唤醒词
|
||||
|
||||
评论以唤醒词开头可唤醒该角色回复。唤醒词功能的特点:
|
||||
|
||||
- **跨页面唤醒**:在未启用AI回评的页面,使用唤醒词也能召唤AI回复
|
||||
- **二级评论支持**:回复中同样可以使用唤醒词唤醒指定角色
|
||||
- **独立唤醒**:每个角色有独立的唤醒词,可以唤醒不同角色
|
||||
- **留空不启用**:唤醒词留空则不启用该角色的唤醒功能
|
||||
|
||||
::: tip
|
||||
唤醒词匹配时,插件会自动去除评论中的HTML标签并去除首尾空格,确保匹配准确。
|
||||
:::
|
||||
|
||||
### 人格提示词
|
||||
|
||||
人格提示词定义了AI角色的性格和回复风格,是影响回复质量的关键配置。
|
||||
@@ -31,7 +53,7 @@ AI回复者的显示名称,默认为「小回」。修改后新回复将使用
|
||||
填写邮箱后,AI回复者的头像将通过 Gravatar 服务自动生成:
|
||||
|
||||
1. 插件根据邮箱生成 SHA-256 哈希
|
||||
2. 构造 Gravatar URL:`https://cn.cravatar.com/avatar/{hash}`
|
||||
2. 构造 Gravatar URL:`https://cn.cravatar.com/avatar/{hash}`(使用 [Cravatar](https://cn.cravatar.com) 镜像服务)
|
||||
3. 头像URL存储在评论的 `owner.annotations["avatar"]` 中
|
||||
|
||||
::: warning
|
||||
|
||||
+30
-24
@@ -9,6 +9,8 @@ Prompt模板控制AI生成回复时的完整提示词结构。
|
||||
|
||||
{{safety_prompt}}
|
||||
|
||||
【语言要求】请用评论所使用的语言回复。如果评论是英文,请用英文回复;如果是中文,请用中文回复;如果是日文,请用日文回复;以此类推。
|
||||
|
||||
请回复以下评论。注意:
|
||||
- 回复长度应与评论长度匹配,简短问候简短回复
|
||||
- 不要复述或总结文章内容
|
||||
@@ -21,6 +23,7 @@ Prompt模板控制AI生成回复时的完整提示词结构。
|
||||
文章(仅供理解上下文,不要复述):
|
||||
{{article}}
|
||||
|
||||
{{conversation_history}}
|
||||
评论:
|
||||
{{comment}}
|
||||
```
|
||||
@@ -29,23 +32,28 @@ Prompt模板控制AI生成回复时的完整提示词结构。
|
||||
|
||||
| 变量 | 说明 | 注入时机 |
|
||||
|------|------|---------|
|
||||
| `{{persona_prompt}}` | AI角色人格提示词 | 始终注入 |
|
||||
| `{{persona_prompt}}` | AI角色人格提示词(含已启用的预设) | 始终注入 |
|
||||
| `{{safety_prompt}}` | 安全规范提示词 | 始终注入 |
|
||||
| `{{sentiment_prompt}}` | 情感语气提示词 | 情感分析后自动注入,不在模板中显式使用 |
|
||||
| `{{post_title}}` | 文章标题 | 始终注入 |
|
||||
| `{{post_date}}` | 文章发布日期(如 2024-01-15) | 始终注入 |
|
||||
| `{{comment_count}}` | 该文章的评论数 | 始终注入 |
|
||||
| `{{article}}` | 文章/页面内容(含标题) | 始终注入 |
|
||||
| `{{conversation_history}}` | 对话历史上下文 | 多轮对话时注入 |
|
||||
| `{{comment}}` | 评论内容(含评论者名称) | 始终注入 |
|
||||
| `{{conversation}}` | 对话上下文 | 多轮对话时注入 |
|
||||
|
||||
::: warning 变量名注意
|
||||
对话上下文变量是 `{{conversation_history}}`(不是 `{{conversation}}`)。如果模板中使用了错误的变量名,该变量不会被替换。
|
||||
:::
|
||||
|
||||
## 情感提示
|
||||
|
||||
情感提示由插件根据情感分析结果自动注入到Prompt中,不需要在模板中手动添加:
|
||||
情感提示由插件根据情感分析结果自动追加到 Prompt 末尾,不需要在模板中手动添加:
|
||||
|
||||
- **正面** → "评论者情绪积极友好,请用热情友好的语气回复,表达感谢和共鸣。"
|
||||
- **负面** → "评论者情绪偏消极或不满,请用理性温和的语气回复,避免激化矛盾,适当表示理解。"
|
||||
- **中性** → 不注入额外提示
|
||||
- **非常正面** → 追加"评论者情绪非常正面积极,请用热情洋溢的语气回复,表达真诚的感谢和共鸣。"
|
||||
- **正面** → 追加"评论者情绪正面积极,请用热情友好的语气回复,可以表达感谢和共鸣。"
|
||||
- **负面** → 追加"评论者情绪偏负面,请用理性温和的语气回复,避免激化矛盾,展现理解和包容。"
|
||||
- **非常负面** → 追加"评论者情绪非常负面,请用非常温和、理性的语气回复,避免任何可能激化矛盾的表达,展现充分的理解和耐心。"
|
||||
- **中性** → 不追加额外提示
|
||||
|
||||
## 安全提示
|
||||
|
||||
@@ -56,6 +64,17 @@ Prompt模板控制AI生成回复时的完整提示词结构。
|
||||
- 不生成虚假信息
|
||||
- 回复内容与评论相关
|
||||
|
||||
## 预设风格
|
||||
|
||||
在 Prompt 设置页面可以多选启用预设风格,启用后预设提示词会自动合并到 `{{persona_prompt}}` 之后:
|
||||
|
||||
| 预设 | 说明 |
|
||||
|------|------|
|
||||
| 友好型 | 热情友好,多用感叹号和表情符号,口语化表达 |
|
||||
| 专业型 | 专业严谨,正式语言风格,有逻辑性 |
|
||||
| 幽默型 | 适当加入幽默元素,轻松诙谐但不过度 |
|
||||
| 简洁型 | 非常简洁,一两句话即可,不展开讨论 |
|
||||
|
||||
## 自定义建议
|
||||
|
||||
自定义Prompt模板时,建议:
|
||||
@@ -63,20 +82,7 @@ Prompt模板控制AI生成回复时的完整提示词结构。
|
||||
1. 保留 `{{persona_prompt}}` 和 `{{safety_prompt}}` 变量
|
||||
2. 保留 `{{article}}` 和 `{{comment}}` 变量
|
||||
3. 利用 `{{post_title}}`、`{{post_date}}`、`{{comment_count}}` 提供更丰富的上下文
|
||||
4. 在变量之间添加清晰的分隔和指令
|
||||
5. 避免让AI复述文章内容
|
||||
6. 控制回复长度和风格
|
||||
|
||||
## 变量使用示例
|
||||
|
||||
### 根据评论数调整回复风格
|
||||
|
||||
```
|
||||
{{comment_count}}条评论说明这篇文章{{#if comment_count > 10}}很受欢迎{{/if}}。
|
||||
```
|
||||
|
||||
### 利用发布日期
|
||||
|
||||
```
|
||||
这篇文章发布于{{post_date}},回复时请考虑时效性。
|
||||
```
|
||||
4. 保留 `{{conversation_history}}` 以支持多轮对话上下文
|
||||
5. 在变量之间添加清晰的分隔和指令
|
||||
6. 避免让AI复述文章内容
|
||||
7. 控制回复长度和风格
|
||||
|
||||
+13
-9
@@ -6,25 +6,29 @@
|
||||
|
||||
| 分类 | 说明 | AI回复语气 |
|
||||
|------|------|-----------|
|
||||
| 正面 | 评论情绪积极、友好、感谢 | 热情友好,表达感谢和共鸣 |
|
||||
| 中性 | 评论情绪平淡、普通提问 | 正常语气回复,不加额外提示 |
|
||||
| 负面 | 评论情绪偏消极、不满、批评 | 理性温和,避免激化矛盾 |
|
||||
| 非常正面 | 强烈的感谢、赞美、认同(如"太棒了"、"非常感谢") | 热情洋溢,表达真诚的感谢和共鸣 |
|
||||
| 正面 | 友好、肯定、支持(如"不错"、"学习了") | 热情友好,表达感谢和共鸣 |
|
||||
| 中性 | 提问、讨论、陈述事实(如"请问..."、"这个怎么用") | 正常语气回复,不加额外提示 |
|
||||
| 负面 | 不满、质疑、批评(如"不好用"、"有问题") | 理性温和,避免激化矛盾 |
|
||||
| 非常负面 | 攻击、辱骂、极端情绪(如"垃圾"、"骗子") | 非常温和理性,避免激化矛盾,展现理解和耐心 |
|
||||
|
||||
## 工作原理
|
||||
|
||||
1. 评论通过过滤检查后,调用AI对评论内容进行情感分析
|
||||
2. AI返回情感分类结果(POSITIVE / NEUTRAL / NEGATIVE)
|
||||
2. AI 使用结构化输出(`OutputSpec.choice`)返回情感分类结果(VERY_POSITIVE / POSITIVE / NEUTRAL / NEGATIVE / VERY_NEGATIVE)
|
||||
3. 如果情感分析失败(如AI不可用),默认降级为 NEUTRAL
|
||||
4. 情感结果传入 PromptBuilder,在生成Prompt时注入对应的语气提示
|
||||
4. 情感结果传入 PromptBuilder,在生成Prompt时追加对应的语气提示
|
||||
5. 情感结果同时记录在 `AiCommentReply` 的 `sentiment` 字段中
|
||||
|
||||
## 日志展示
|
||||
|
||||
在AI回复日志页面,每条记录会显示情感标签:
|
||||
在AI回复日志页面,每条记录会显示情感标签(纯色背景标签):
|
||||
|
||||
- 🟢 **正面** — 绿色标签
|
||||
- ⚪ **中性** — 灰色标签
|
||||
- 🔴 **负面** — 红色标签
|
||||
- **非常正面** — 深绿色标签
|
||||
- **正面** — 浅绿色标签
|
||||
- **中性** — 灰色标签
|
||||
- **负面** — 浅红色标签
|
||||
- **非常负面** — 深红色标签
|
||||
|
||||
## 性能影响
|
||||
|
||||
|
||||
+108
-15
@@ -1,30 +1,78 @@
|
||||
# 插件设置
|
||||
|
||||
插件设置页面位于 **AI回评** → **插件设置**,包含以下配置组:
|
||||
插件设置页面位于 **AI回评** → **插件设置**,通过标签栏切换以下五个配置页面:
|
||||
|
||||
- 基本设置
|
||||
- AI角色设置
|
||||
- 模型设置
|
||||
- Prompt设置
|
||||
- 数据清理
|
||||
|
||||
页面右侧为操作控制侧边栏,显示保存按钮和未保存状态指示器。在 Prompt 设置页面,侧边栏还会显示可用模板变量列表。
|
||||
|
||||
## 基本设置
|
||||
|
||||
| 配置项 | 说明 | 默认值 |
|
||||
|--------|------|--------|
|
||||
| 自动回复 | 是否启用自动回复功能 | 开启 |
|
||||
| 自动发布 | AI回复是否自动发布 | 开启 |
|
||||
| 自动发布 | AI回复是否自动发布,关闭则存为草稿 | 开启 |
|
||||
| 最大对话轮次 | 同一评论线程中AI最多自动回复的轮次 | 8 |
|
||||
| 速率限制 | 每分钟最大AI回复数量,防止批量评论消耗过多额度 | 10 |
|
||||
| 最大重试次数 | AI生成失败时的最大重试次数 | 3 |
|
||||
| 评论者黑名单 | 不触发AI回复的评论者显示名称或邮箱,逗号分隔 | 空 |
|
||||
| 评论者黑名单 | 不触发AI回复的评论者,支持名称、邮箱和正则表达式(`regex:` 开头),逗号分隔 | 空 |
|
||||
| 启用前置过滤 | AI回复前检测评论合规性,拦截广告/辱骂/敏感内容,节省Token | 开启 |
|
||||
| 违规评论设为待审核 | 检测到违规评论时自动取消通过,需人工审核 | 开启 |
|
||||
|
||||
::: tip 评论者黑名单
|
||||
黑名单支持三种格式:
|
||||
- **名称**:如 `张三`
|
||||
- **邮箱**:如 `spam@example.com`(不区分大小写)
|
||||
- **正则表达式**:以 `regex:` 开头,如 `regex:^spam.*`
|
||||
|
||||
点击"添加评论者"按钮可从已有评论列表中选择评论者自动添加到黑名单。
|
||||
:::
|
||||
|
||||
::: tip 前置过滤(合规检测)
|
||||
启用前置过滤后,AI 在生成回复前会先对评论内容进行合规性分类,识别以下类别:
|
||||
|
||||
- **正常**:放行,继续走 AI 回复流程
|
||||
- **广告**:包含推广链接、产品推销、引流信息等
|
||||
- **辱骂攻击**:包含辱骂、人身攻击、恶意挑衅、歧视性言论等
|
||||
- **敏感内容**:涉及政治敏感、违法违规、色情暴力等
|
||||
- **无意义**:纯乱码、无意义字符堆砌(如随机符号、键盘乱敲)
|
||||
|
||||
对于非"正常"类别的评论,插件会:
|
||||
|
||||
1. **停止生成 AI 回复**,节省 Token 与 API 调用
|
||||
2. 创建一条 `FILTERED` 状态的日志记录(可在日志页通过"已拦截"状态筛选查看)
|
||||
3. 若启用"违规评论设为待审核",会自动将原评论的 `approved` 置为 `false`,使其进入待审核队列,需人工判断后审核通过
|
||||
|
||||
被误拦截的评论可在日志页点击 **误报反馈** 按钮处理,支持"AI 回复"和"仅通过"两种方式。选择"仅通过"后记录变为"误报通过"状态,可随时点击"触发AI回复"按钮补生成回复。
|
||||
|
||||
::: warning
|
||||
前置过滤依赖 AI Foundation 插件进行分类判断,会额外消耗少量 Token。若 AI 服务不可用或分类失败,为安全起见将拦截评论而非放行,防止违规内容漏网。
|
||||
:::
|
||||
:::
|
||||
|
||||
## AI角色设置
|
||||
|
||||
AI角色定义了回复评论的虚拟身份。支持创建多个角色,每个角色有独立的昵称、人格提示词、性别、语气风格和 Gravatar 头像,可指定一个为默认角色。
|
||||
|
||||
| 配置项 | 说明 | 默认值 |
|
||||
|--------|------|--------|
|
||||
| AI角色昵称 | AI回复者的显示名称 | 小回 |
|
||||
| AI角色人格提示词 | 定义AI角色的人格和回复风格 | 见下方 |
|
||||
| AI角色邮箱 | 用于Gravatar头像服务展示头像 | 空 |
|
||||
| 角色昵称 | AI回复者的显示名称 | 小回 |
|
||||
| 性别与语气 | 角色性别(男/女)+ 中性语气复选框(勾选=中性语气,取消勾选=跟随性别语气) | 女 + 中性语气 |
|
||||
| 唤醒词 | 评论以此词开头则唤醒该角色回复,留空不启用 | 空 |
|
||||
| 人格提示词 | 定义AI角色的人格和回复风格 | 见下方 |
|
||||
| 邮箱 | 用于 Gravatar 头像服务展示头像 | 空 |
|
||||
| 设为默认 | 将该角色设为默认角色 | 第一个角色默认 |
|
||||
|
||||
默认人格提示词:
|
||||
|
||||
> 你是「小回」,一个友善的评论者。你的回复简洁自然,像朋友聊天一样。简短的评论就简短回复,有深度的讨论才展开回应。不要长篇大论,不要复述文章内容。
|
||||
|
||||
::: tip Gravatar头像
|
||||
填写邮箱后,AI回复者的头像将通过 [Gravatar](https://gravatar.com) 服务自动生成。如果不填写邮箱,将使用默认头像。
|
||||
填写邮箱后,AI回复者的头像将通过 [Gravatar](https://gravatar.com) 服务自动生成,使用 [Cravatar](https://cn.cravatar.com) 镜像。如果不填写邮箱,将使用默认头像。
|
||||
:::
|
||||
|
||||
## 模型设置
|
||||
@@ -42,17 +90,40 @@
|
||||
| 配置项 | 说明 | 默认值 |
|
||||
|--------|------|--------|
|
||||
| 自定义Prompt模板 | AI生成回复时使用的Prompt模板 | 见下方 |
|
||||
| 启用预设 | 选择要启用的Prompt预设风格(可多选) | 空 |
|
||||
|
||||
### 预设风格
|
||||
|
||||
| 预设 | 说明 |
|
||||
|------|------|
|
||||
| 友好型 | 热情友好,多用感叹号和表情符号,口语化表达 |
|
||||
| 专业型 | 专业严谨,正式语言风格,有逻辑性 |
|
||||
| 幽默型 | 适当加入幽默元素,轻松诙谐但不过度 |
|
||||
| 简洁型 | 非常简洁,一两句话即可,不展开讨论 |
|
||||
|
||||
预设提示词会自动合并到角色人格提示词之后。
|
||||
|
||||
### 模板变量
|
||||
|
||||
| 变量 | 说明 |
|
||||
|------|------|
|
||||
| `{{persona_prompt}}` | AI角色人格提示词 |
|
||||
| `{{safety_prompt}}` | 安全规范提示词 |
|
||||
| `{{sentiment_prompt}}` | 情感语气提示词(自动注入) |
|
||||
| `{{article}}` | 文章内容 |
|
||||
| `{{comment}}` | 评论内容 |
|
||||
| `{{conversation}}` | 对话上下文(多轮对话时) |
|
||||
| 变量 | 说明 | 注入时机 |
|
||||
|------|------|---------|
|
||||
| `{{persona_prompt}}` | AI角色人格提示词(含已启用的预设) | 始终注入 |
|
||||
| `{{safety_prompt}}` | 安全规范提示词 | 始终注入 |
|
||||
| `{{post_title}}` | 文章标题 | 始终注入 |
|
||||
| `{{post_date}}` | 文章发布日期(如 2024-01-15) | 始终注入 |
|
||||
| `{{comment_count}}` | 该文章的评论数 | 始终注入 |
|
||||
| `{{article}}` | 文章/页面内容(含标题) | 始终注入 |
|
||||
| `{{conversation_history}}` | 对话历史上下文 | 多轮对话时注入 |
|
||||
| `{{comment}}` | 评论内容(含评论者名称) | 始终注入 |
|
||||
|
||||
::: tip 情感提示
|
||||
情感提示由插件根据情感分析结果自动追加到 Prompt 末尾,不需要在模板中手动添加:
|
||||
- **非常正面** → 追加热情洋溢的语气提示
|
||||
- **正面** → 追加热情友好的语气提示
|
||||
- **负面** → 追加理性温和的语气提示
|
||||
- **非常负面** → 追加冷静关怀的语气提示
|
||||
- **中性** → 不追加额外提示
|
||||
:::
|
||||
|
||||
## 数据清理
|
||||
|
||||
@@ -64,3 +135,25 @@
|
||||
::: tip
|
||||
你也可以在数据清理页面点击"立即清理"按钮手动触发清理操作。
|
||||
:::
|
||||
|
||||
::: warning
|
||||
清理操作仅删除 `AiCommentReply` 记录(插件内部的日志记录),不会删除已发布的 Halo Reply 评论。
|
||||
:::
|
||||
|
||||
## 配置导入导出
|
||||
|
||||
插件设置页面顶部提供导入导出按钮,方便备份和迁移配置。
|
||||
|
||||
### 导出配置
|
||||
|
||||
点击 **导出** 按钮,将当前配置(包括 ConfigMap 数据和所有 AI 角色)导出为 JSON 文件。
|
||||
|
||||
### 导入配置
|
||||
|
||||
1. 点击 **导入** 按钮,选择 JSON 配置文件
|
||||
2. 确认导入操作(导入会覆盖当前配置,不可撤销)
|
||||
3. 导入完成后自动刷新设置和角色列表
|
||||
|
||||
::: warning
|
||||
导入操作会覆盖当前配置,请谨慎操作。建议在导入前先导出当前配置作为备份。
|
||||
:::
|
||||
|
||||
+7
-5
@@ -16,14 +16,16 @@ hero:
|
||||
features:
|
||||
- title: 自动回复
|
||||
details: 监听新评论,自动调用AI生成回复,支持对话式上下文和失败重试
|
||||
- title: 多语言适配
|
||||
details: 根据评论语言自动用对应语言回复,中文评论中文回复,英文评论英文回复
|
||||
- title: 多 AI 角色
|
||||
details: 创建多个虚拟角色,独立昵称、人格、性别、语气和 Gravatar 头像
|
||||
- title: 情感分析
|
||||
details: 分析评论情感倾向,根据正面/中性/负面调整回复语气
|
||||
- title: 前置过滤
|
||||
details: AI回复前检测评论合规性,拦截广告/辱骂/敏感内容,节省Token
|
||||
- title: 草稿模式
|
||||
details: AI回复先存为草稿,管理员审核后再发布,支持批量操作
|
||||
- title: 灵活过滤
|
||||
details: 文章/页面级开关控制,评论者黑名单支持名称和邮箱匹配
|
||||
- title: 对话上下文
|
||||
details: 查看完整对话上下文,支持引用摘要展示和头像显示
|
||||
- title: 数据管理
|
||||
details: 仪表盘统计、日志筛选搜索、自动清理旧记录
|
||||
details: 仪表盘统计、日志筛选搜索、自动清理旧记录、配置导入导出
|
||||
---
|
||||
|
||||
+4
-1
@@ -1 +1,4 @@
|
||||
version=1.0.0-SNAPSHOT
|
||||
version=1.2.1
|
||||
|
||||
# Fix Windows Gradle Worker Daemon exit code 268435659 when running pnpm via Exec tasks
|
||||
org.gradle.daemon=false
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package top.nxxy335.commentaiautopilot;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.springframework.stereotype.Component;
|
||||
import run.halo.app.extension.ConfigMap;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import run.halo.app.extension.index.IndexSpecs;
|
||||
import run.halo.app.extension.Scheme;
|
||||
@@ -25,13 +29,18 @@ import reactor.core.publisher.Mono;
|
||||
@Component
|
||||
public class CommentAiAutopilotPlugin extends BasePlugin {
|
||||
|
||||
private static final String CONFIG_MAP_NAME = "comment-ai-autopilot-configmap";
|
||||
|
||||
private final SchemeManager schemeManager;
|
||||
private final ReactiveExtensionClient client;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public CommentAiAutopilotPlugin(PluginContext pluginContext, SchemeManager schemeManager, ReactiveExtensionClient client) {
|
||||
public CommentAiAutopilotPlugin(PluginContext pluginContext, SchemeManager schemeManager,
|
||||
ReactiveExtensionClient client, ObjectMapper objectMapper) {
|
||||
super(pluginContext);
|
||||
this.schemeManager = schemeManager;
|
||||
this.client = client;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -43,11 +52,60 @@ public class CommentAiAutopilotPlugin extends BasePlugin {
|
||||
.indexFunc(ext -> ext.getSpec().getPostId()));
|
||||
indexSpecs.add(IndexSpecs.<AiCommentReply, String>single("spec.status", String.class)
|
||||
.indexFunc(ext -> ext.getSpec().getStatus()));
|
||||
indexSpecs.add(IndexSpecs.<AiCommentReply, String>single("spec.sentiment", String.class)
|
||||
.indexFunc(ext -> ext.getSpec().getSentiment()));
|
||||
indexSpecs.add(IndexSpecs.<AiCommentReply, String>single("spec.published", String.class)
|
||||
.indexFunc(ext -> String.valueOf(ext.getSpec().getPublished())));
|
||||
indexSpecs.add(IndexSpecs.<AiCommentReply, String>single("spec.postKind", String.class)
|
||||
.indexFunc(ext -> ext.getSpec().getPostKind()));
|
||||
});
|
||||
schemeManager.register(AiPersona.class);
|
||||
|
||||
// 初始化默认AI角色"小回"
|
||||
initDefaultPersona();
|
||||
|
||||
// 迁移:确保升级用户的前置过滤配置正确
|
||||
migratePreFilterConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* 迁移前置过滤配置:从 v1.0.x 升级到 v1.1.0 时,
|
||||
* ConfigMap 中可能保存了旧默认值 preFilterEnabled=false,
|
||||
* 需要将其更新为 true(新默认值)。
|
||||
*/
|
||||
private void migratePreFilterConfig() {
|
||||
client.fetch(ConfigMap.class, CONFIG_MAP_NAME)
|
||||
.flatMap(cm -> {
|
||||
var data = cm.getData();
|
||||
if (data == null) return Mono.empty();
|
||||
String basicJson = data.get("basic");
|
||||
if (basicJson == null || basicJson.isBlank()) return Mono.empty();
|
||||
try {
|
||||
JsonNode node = objectMapper.readTree(basicJson);
|
||||
if (!node.has("preFilterEnabled")) {
|
||||
// 字段不存在,添加并设为 true
|
||||
((ObjectNode) node).put("preFilterEnabled", true);
|
||||
data.put("basic", objectMapper.writeValueAsString(node));
|
||||
return client.update(cm)
|
||||
.doOnSuccess(c -> log.info("[Migration] Added preFilterEnabled=true to ConfigMap"));
|
||||
}
|
||||
if (node.has("preFilterEnabled") && !node.get("preFilterEnabled").asBoolean(true)) {
|
||||
// 字段存在但为 false(旧默认值),迁移为 true
|
||||
((ObjectNode) node).put("preFilterEnabled", true);
|
||||
data.put("basic", objectMapper.writeValueAsString(node));
|
||||
return client.update(cm)
|
||||
.doOnSuccess(c -> log.info("[Migration] Migrated preFilterEnabled from false to true"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[Migration] Failed to migrate preFilter config: {}", e.getMessage());
|
||||
}
|
||||
return Mono.empty();
|
||||
})
|
||||
.subscribe(
|
||||
null,
|
||||
err -> log.debug("[Migration] PreFilter config migration skipped: {}", err.getMessage()),
|
||||
() -> log.debug("[Migration] PreFilter config migration check completed")
|
||||
);
|
||||
}
|
||||
|
||||
private void initDefaultPersona() {
|
||||
@@ -61,6 +119,9 @@ public class CommentAiAutopilotPlugin extends BasePlugin {
|
||||
spec.setDisplayName("小回");
|
||||
spec.setPrompt("你是一个友善的评论者,回复简洁自然,像朋友聊天一样。");
|
||||
spec.setEmail("");
|
||||
spec.setGender("female");
|
||||
spec.setNeutralVoice(true);
|
||||
spec.setWakeWord("小回小回");
|
||||
spec.setIsDefault(true);
|
||||
persona.setSpec(spec);
|
||||
return client.create(persona);
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package top.nxxy335.commentaiautopilot;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class ObjectMapperConfiguration {
|
||||
|
||||
@Bean
|
||||
ObjectMapper objectMapper() {
|
||||
return new ObjectMapper();
|
||||
}
|
||||
}
|
||||
+207
-172
@@ -8,7 +8,6 @@ import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import run.halo.app.core.extension.content.Comment;
|
||||
import run.halo.app.core.extension.content.Post;
|
||||
import run.halo.app.core.extension.content.Reply;
|
||||
import run.halo.app.core.extension.endpoint.CustomEndpoint;
|
||||
import run.halo.app.extension.ConfigMap;
|
||||
@@ -16,6 +15,7 @@ import run.halo.app.extension.Metadata;
|
||||
import run.halo.app.extension.GroupVersion;
|
||||
import run.halo.app.extension.ListOptions;
|
||||
import run.halo.app.extension.ListResult;
|
||||
import run.halo.app.extension.index.query.Queries;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import run.halo.app.extension.PageRequestImpl;
|
||||
import top.nxxy335.commentaiautopilot.extension.AiCommentReply;
|
||||
@@ -24,6 +24,8 @@ import top.nxxy335.commentaiautopilot.service.AiFoundationClient;
|
||||
import top.nxxy335.commentaiautopilot.service.AiReplyCleanupService;
|
||||
import top.nxxy335.commentaiautopilot.service.AiReplyOrchestrator;
|
||||
import top.nxxy335.commentaiautopilot.service.CommentReplyPublisher;
|
||||
import top.nxxy335.commentaiautopilot.service.PersonaResolver;
|
||||
import top.nxxy335.commentaiautopilot.util.GravatarUtil;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -56,16 +58,18 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
private final AiFoundationClient aiFoundationClient;
|
||||
private final CommentReplyPublisher commentReplyPublisher;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final PersonaResolver personaResolver;
|
||||
|
||||
private static final String CONFIG_MAP_NAME = "comment-ai-autopilot-configmap";
|
||||
|
||||
public CommentAiAutopilotEndpoint(ReactiveExtensionClient client, AiReplyOrchestrator orchestrator, AiReplyCleanupService cleanupService, AiFoundationClient aiFoundationClient, CommentReplyPublisher commentReplyPublisher) {
|
||||
public CommentAiAutopilotEndpoint(ReactiveExtensionClient client, AiReplyOrchestrator orchestrator, AiReplyCleanupService cleanupService, AiFoundationClient aiFoundationClient, CommentReplyPublisher commentReplyPublisher, ObjectMapper objectMapper, PersonaResolver personaResolver) {
|
||||
this.client = client;
|
||||
this.orchestrator = orchestrator;
|
||||
this.cleanupService = cleanupService;
|
||||
this.aiFoundationClient = aiFoundationClient;
|
||||
this.commentReplyPublisher = commentReplyPublisher;
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper = objectMapper;
|
||||
this.personaResolver = personaResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,6 +101,8 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
.POST("/import", this::importConfig)
|
||||
// 更新草稿回复内容(同时更新 AiCommentReply 和 Reply 扩展)
|
||||
.PUT("/replies/{name}/content", this::updateReplyContent)
|
||||
// 误报反馈:将拦截记录标记为误报,可选触发AI回复
|
||||
.POST("/replies/{name}/false-positive", this::falsePositive)
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -133,19 +139,26 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
final Instant finalStartInstant = startInstant;
|
||||
final Instant finalEndInstant = endInstant;
|
||||
|
||||
// Check if we need in-memory filtering (keyword, date range, status, or sentiment)
|
||||
boolean needsMemoryFilter = !keywordFilter.isBlank() || finalStartInstant != null || finalEndInstant != null
|
||||
|| !statusFilter.isBlank() || !sentimentFilter.isBlank();
|
||||
// Check if we need in-memory filtering (keyword or date range)
|
||||
boolean needsMemoryFilter = !keywordFilter.isBlank() || finalStartInstant != null || finalEndInstant != null;
|
||||
|
||||
// Build server-side query for status and sentiment (indexed fields)
|
||||
var listOptionsBuilder = ListOptions.builder();
|
||||
if (!statusFilter.isBlank()) {
|
||||
listOptionsBuilder.andQuery(Queries.equal("spec.status", statusFilter));
|
||||
}
|
||||
if (!sentimentFilter.isBlank()) {
|
||||
listOptionsBuilder.andQuery(Queries.equal("spec.sentiment", sentimentFilter));
|
||||
}
|
||||
var listOptions = listOptionsBuilder.build();
|
||||
|
||||
if (needsMemoryFilter) {
|
||||
// Fall back to listAll + in-memory filter for complex queries
|
||||
return client.listAll(AiCommentReply.class, ListOptions.builder().build(), Sort.unsorted())
|
||||
// Fall back to listAll + in-memory filter for keyword/date queries
|
||||
return client.listAll(AiCommentReply.class, listOptions, Sort.unsorted())
|
||||
.collectList()
|
||||
.map(replies -> {
|
||||
var filtered = replies.stream()
|
||||
.filter(r -> {
|
||||
if (!statusFilter.isBlank() && !statusFilter.equals(r.getSpec().getStatus())) return false;
|
||||
if (!sentimentFilter.isBlank() && !sentimentFilter.equals(r.getSpec().getSentiment())) return false;
|
||||
if (!keywordFilter.isBlank()) {
|
||||
String reply = r.getSpec().getReply();
|
||||
if (reply == null || !reply.contains(keywordFilter)) return false;
|
||||
@@ -184,13 +197,11 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
.flatMap(result -> ServerResponse.ok().bodyValue(result));
|
||||
}
|
||||
|
||||
// No filters - use server-side pagination directly
|
||||
// No memory filters needed - use server-side pagination directly
|
||||
Sort sort = "asc".equalsIgnoreCase(sortOrder)
|
||||
? Sort.by(Sort.Order.asc("metadata.creationTimestamp"))
|
||||
: Sort.by(Sort.Order.desc("metadata.creationTimestamp"));
|
||||
|
||||
var listOptions = ListOptions.builder().build();
|
||||
|
||||
return client.listBy(AiCommentReply.class, listOptions,
|
||||
PageRequestImpl.of(page - 1, size, sort))
|
||||
.map(listResult -> {
|
||||
@@ -216,100 +227,25 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
}
|
||||
|
||||
private Mono<ServerResponse> getStats(ServerRequest request) {
|
||||
String range = request.queryParam("range").orElse("7");
|
||||
|
||||
return client.listAll(AiCommentReply.class, ListOptions.builder().build(), Sort.unsorted())
|
||||
.collectList()
|
||||
.map(allReplies -> {
|
||||
// 根据 range 计算截止时间
|
||||
ZoneId zoneId = ZoneId.systemDefault();
|
||||
LocalDate today = LocalDate.now(zoneId);
|
||||
Instant cutoffInstant;
|
||||
int trendDays;
|
||||
|
||||
if ("all".equals(range)) {
|
||||
cutoffInstant = null; // 不做时间过滤
|
||||
trendDays = 30; // "all" 时趋势也展示最近30天
|
||||
} else {
|
||||
int days = Integer.parseInt(range);
|
||||
cutoffInstant = today.minusDays(days).atStartOfDay(zoneId).toInstant();
|
||||
trendDays = days;
|
||||
}
|
||||
|
||||
// 根据 range 过滤记录
|
||||
List<AiCommentReply> replies;
|
||||
if (cutoffInstant != null) {
|
||||
replies = allReplies.stream()
|
||||
.filter(r -> {
|
||||
Instant ts = r.getMetadata().getCreationTimestamp();
|
||||
return ts != null && !ts.isBefore(cutoffInstant);
|
||||
})
|
||||
.toList();
|
||||
} else {
|
||||
replies = allReplies;
|
||||
}
|
||||
|
||||
.map(replies -> {
|
||||
long total = replies.size();
|
||||
long passCount = replies.stream()
|
||||
.filter(r -> "PASS".equals(r.getSpec().getStatus())).count();
|
||||
long failCount = replies.stream()
|
||||
.filter(r -> "FAIL".equals(r.getSpec().getStatus())).count();
|
||||
double avgScore = replies.stream()
|
||||
.filter(r -> r.getSpec().getScore() != null && r.getSpec().getScore() > 0)
|
||||
.mapToInt(r -> r.getSpec().getScore())
|
||||
.average().orElse(0.0);
|
||||
|
||||
long reviewingCount = replies.stream()
|
||||
.filter(r -> "PASS".equals(r.getSpec().getStatus())
|
||||
&& !Boolean.TRUE.equals(r.getSpec().getPublished()))
|
||||
.count();
|
||||
|
||||
Map<String, Long> sentimentDistribution = new HashMap<>();
|
||||
sentimentDistribution.put("POSITIVE", 0L);
|
||||
sentimentDistribution.put("NEUTRAL", 0L);
|
||||
sentimentDistribution.put("NEGATIVE", 0L);
|
||||
sentimentDistribution.put("UNKNOWN", 0L);
|
||||
for (var r : replies) {
|
||||
String sentiment = r.getSpec().getSentiment();
|
||||
if (sentiment == null || sentiment.isBlank()) {
|
||||
sentimentDistribution.merge("UNKNOWN", 1L, Long::sum);
|
||||
} else {
|
||||
sentimentDistribution.merge(sentiment, 1L, Long::sum);
|
||||
}
|
||||
}
|
||||
|
||||
// 计算 dailyTrend
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
Map<LocalDate, Long> dailyMap = new HashMap<>();
|
||||
for (int i = 0; i < trendDays; i++) {
|
||||
dailyMap.put(today.minusDays(i), 0L);
|
||||
}
|
||||
for (var r : replies) {
|
||||
Instant timestamp = r.getMetadata().getCreationTimestamp();
|
||||
if (timestamp != null) {
|
||||
try {
|
||||
LocalDate date = timestamp.atZone(zoneId).toLocalDate();
|
||||
if (dailyMap.containsKey(date)) {
|
||||
dailyMap.merge(date, 1L, Long::sum);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
List<DailyCount> dailyTrend = new ArrayList<>();
|
||||
for (int i = 0; i < trendDays; i++) {
|
||||
LocalDate date = today.minusDays(i);
|
||||
dailyTrend.add(new DailyCount(date.format(formatter), dailyMap.get(date)));
|
||||
}
|
||||
|
||||
return new StatsResponse(total, passCount, failCount, avgScore,
|
||||
reviewingCount, sentimentDistribution, dailyTrend);
|
||||
return new StatsResponse(total, passCount, failCount, reviewingCount);
|
||||
})
|
||||
.onErrorResume(e -> {
|
||||
log.warn("Failed to fetch stats: {}", e.getMessage());
|
||||
return Mono.just(new StatsResponse(0, 0, 0, 0.0, 0L,
|
||||
Map.of("POSITIVE", 0L, "NEUTRAL", 0L, "NEGATIVE", 0L, "UNKNOWN", 0L),
|
||||
List.of()));
|
||||
return Mono.just(new StatsResponse(0, 0, 0, 0));
|
||||
})
|
||||
.flatMap(stats -> ServerResponse.ok().bodyValue(stats));
|
||||
}
|
||||
@@ -321,18 +257,7 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
.next()
|
||||
.flatMap(persona -> {
|
||||
String email = persona.getSpec().getEmail();
|
||||
String avatarUrl = "";
|
||||
if (email != null && !email.isBlank()) {
|
||||
try {
|
||||
var digest = java.security.MessageDigest.getInstance("SHA-256");
|
||||
var hashBytes = digest.digest(email.trim().toLowerCase().getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
var hexString = new StringBuilder();
|
||||
for (byte b : hashBytes) {
|
||||
hexString.append(String.format("%02x", b));
|
||||
}
|
||||
avatarUrl = "https://cn.cravatar.com/avatar/" + hexString;
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
String avatarUrl = GravatarUtil.generateUrl(email);
|
||||
return ServerResponse.ok().bodyValue(Map.of(
|
||||
"name", persona.getSpec().getDisplayName(),
|
||||
"prompt", persona.getSpec().getPrompt() != null ? persona.getSpec().getPrompt() : "",
|
||||
@@ -346,16 +271,11 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
)));
|
||||
}
|
||||
|
||||
public record DailyCount(String date, long count) {}
|
||||
|
||||
public record StatsResponse(
|
||||
long total,
|
||||
long passCount,
|
||||
long failCount,
|
||||
double avgScore,
|
||||
long reviewingCount,
|
||||
Map<String, Long> sentimentDistribution,
|
||||
List<DailyCount> dailyTrend
|
||||
long reviewingCount
|
||||
) {}
|
||||
|
||||
public record PersonaResponse(
|
||||
@@ -373,25 +293,51 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
var commentTime = String.valueOf(comment.getMetadata().getCreationTimestamp());
|
||||
var isCommentAi = isAiOwner(comment.getSpec().getOwner());
|
||||
|
||||
// 首条评论没有引用对象
|
||||
var commentMsg = new ConversationMessage(
|
||||
"comment", commentOwner, commentContent, commentTime, isCommentAi
|
||||
"comment", commentOwner, commentContent, commentTime, isCommentAi, null, null
|
||||
);
|
||||
|
||||
return client.listAll(Reply.class, ListOptions.builder().build(), Sort.unsorted())
|
||||
.filter(reply -> commentName.equals(reply.getSpec().getCommentName()))
|
||||
return client.list(Reply.class,
|
||||
reply -> commentName.equals(reply.getSpec().getCommentName()),
|
||||
null)
|
||||
.sort(Comparator.comparing(r -> r.getMetadata().getCreationTimestamp()))
|
||||
.map(reply -> {
|
||||
.collectList() // 收集为List以便统一处理引用映射
|
||||
.map(replyList -> {
|
||||
List<ConversationMessage> messages = new ArrayList<>();
|
||||
messages.add(commentMsg);
|
||||
|
||||
// 构建 Reply 的映射字典,方便查找引用关系
|
||||
Map<String, Reply> replyMap = new HashMap<>();
|
||||
for (Reply r : replyList) {
|
||||
replyMap.put(r.getMetadata().getName(), r);
|
||||
}
|
||||
|
||||
for (Reply reply : replyList) {
|
||||
var replyOwner = extractOwnerName(reply.getSpec().getOwner());
|
||||
var replyContent = extractContent(reply.getSpec().getRaw(), reply.getSpec().getContent());
|
||||
var replyTime = String.valueOf(reply.getMetadata().getCreationTimestamp());
|
||||
var isAi = isAiOwner(reply.getSpec().getOwner());
|
||||
return new ConversationMessage("reply", replyOwner, replyContent, replyTime, isAi);
|
||||
})
|
||||
.collectList()
|
||||
.map(replyList -> {
|
||||
List<ConversationMessage> messages = new ArrayList<>();
|
||||
messages.add(commentMsg);
|
||||
messages.addAll(replyList);
|
||||
|
||||
String quoteOwner = null;
|
||||
String quoteContent = null;
|
||||
|
||||
// 获取引用的 Reply 名称 (Halo中如果为空,代表直接回复顶级 Comment)
|
||||
String quoteReplyName = reply.getSpec().getQuoteReply();
|
||||
if (quoteReplyName != null && !quoteReplyName.isBlank()) {
|
||||
Reply quotedReply = replyMap.get(quoteReplyName);
|
||||
if (quotedReply != null) {
|
||||
quoteOwner = extractOwnerName(quotedReply.getSpec().getOwner());
|
||||
quoteContent = extractContent(quotedReply.getSpec().getRaw(), quotedReply.getSpec().getContent());
|
||||
}
|
||||
} else {
|
||||
// 没有 quoteReply 表示直接回复首条评论
|
||||
quoteOwner = commentOwner;
|
||||
quoteContent = commentContent;
|
||||
}
|
||||
|
||||
messages.add(new ConversationMessage("reply", replyOwner, replyContent, replyTime, isAi, quoteOwner, quoteContent));
|
||||
}
|
||||
return messages;
|
||||
});
|
||||
})
|
||||
@@ -722,9 +668,9 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
.bodyValue(Map.of("message", "该评论已有AI回复记录"));
|
||||
}
|
||||
// Read persona name from post annotations
|
||||
return getPersonaNameFromComment(commentName)
|
||||
return personaResolver.getPersonaNameFromComment(commentName)
|
||||
.flatMap(personaName ->
|
||||
orchestrator.processComment(commentName, null, false, personaName)
|
||||
orchestrator.processComment(commentName, null, false, personaName, false)
|
||||
.then(ServerResponse.ok().bodyValue(Map.of("message", "已触发AI回复")))
|
||||
);
|
||||
});
|
||||
@@ -749,9 +695,9 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
return ServerResponse.badRequest()
|
||||
.bodyValue(Map.of("message", "该回复已有AI对话记录"));
|
||||
}
|
||||
return getPersonaNameFromComment(commentName)
|
||||
return personaResolver.getPersonaNameFromComment(commentName)
|
||||
.flatMap(personaName ->
|
||||
orchestrator.processComment(commentName, replyName, true, personaName)
|
||||
orchestrator.processComment(commentName, replyName, true, personaName, false)
|
||||
.then(ServerResponse.ok().bodyValue(Map.of("message", "已触发AI对话回复")))
|
||||
);
|
||||
});
|
||||
@@ -759,31 +705,6 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
.switchIfEmpty(ServerResponse.notFound().build());
|
||||
}
|
||||
|
||||
private static final String AI_PERSONA_ANNOTATION = "comment-ai-autopilot.nxxy335.top/ai-persona";
|
||||
|
||||
private Mono<String> getPersonaNameFromComment(String commentName) {
|
||||
return client.fetch(Comment.class, commentName)
|
||||
.flatMap(comment -> {
|
||||
var subjectRef = comment.getSpec().getSubjectRef();
|
||||
if (subjectRef == null || !"Post".equals(subjectRef.getKind())) {
|
||||
return Mono.justOrEmpty(null);
|
||||
}
|
||||
String postName = subjectRef.getName();
|
||||
return client.fetch(Post.class, postName)
|
||||
.mapNotNull(post -> {
|
||||
var annotations = post.getMetadata().getAnnotations();
|
||||
if (annotations != null) {
|
||||
String persona = annotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (persona != null && !persona.isBlank()) {
|
||||
return persona;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
})
|
||||
.defaultIfEmpty("");
|
||||
}
|
||||
|
||||
private Mono<Reply> findReplyForRecord(AiCommentReply record) {
|
||||
// First try using replyName if available
|
||||
String replyName = record.getSpec().getReplyName();
|
||||
@@ -818,7 +739,9 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
String owner,
|
||||
String content,
|
||||
String time,
|
||||
boolean isAi
|
||||
boolean isAi,
|
||||
String quoteOwner,
|
||||
String quoteContent
|
||||
) {}
|
||||
|
||||
public record CommenterInfo(
|
||||
@@ -828,7 +751,8 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
) {}
|
||||
|
||||
private Mono<ServerResponse> listCommenters(ServerRequest request) {
|
||||
return client.listAll(Comment.class, ListOptions.builder().build(), Sort.unsorted())
|
||||
return client.list(Comment.class, null, null)
|
||||
.take(1000)
|
||||
.collectList()
|
||||
.map(comments -> {
|
||||
Set<String> seen = new HashSet<>();
|
||||
@@ -846,7 +770,7 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
if (owner.getAnnotations() != null && owner.getAnnotations().get(Comment.CommentOwner.AVATAR_ANNO) != null) {
|
||||
avatarUrl = owner.getAnnotations().get(Comment.CommentOwner.AVATAR_ANNO);
|
||||
} else if (!email.isBlank()) {
|
||||
avatarUrl = generateGravatarUrl(email);
|
||||
avatarUrl = GravatarUtil.generateUrl(email);
|
||||
}
|
||||
result.add(new CommenterInfo(displayName, email, avatarUrl));
|
||||
}
|
||||
@@ -856,26 +780,11 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
.flatMap(commenters -> ServerResponse.ok().bodyValue(commenters));
|
||||
}
|
||||
|
||||
private String generateGravatarUrl(String email) {
|
||||
try {
|
||||
var digest = java.security.MessageDigest.getInstance("SHA-256");
|
||||
var hashBytes = digest.digest(email.trim().toLowerCase().getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
var hexString = new StringBuilder();
|
||||
for (byte b : hashBytes) {
|
||||
hexString.append(String.format("%02x", b));
|
||||
}
|
||||
return "https://cn.cravatar.com/avatar/" + hexString;
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private Mono<ServerResponse> triggerCleanup(ServerRequest request) {
|
||||
return Mono.fromCallable(() -> {
|
||||
int retentionDays = cleanupService.getRetentionDays();
|
||||
long deleted = cleanupService.executeCleanup(retentionDays);
|
||||
return Map.of("deletedCount", deleted, "retentionDays", retentionDays);
|
||||
})
|
||||
return cleanupService.getRetentionDays()
|
||||
.flatMap(retentionDays -> cleanupService.executeCleanup(retentionDays)
|
||||
.map(deleted -> Map.of("deletedCount", deleted, "retentionDays", retentionDays))
|
||||
)
|
||||
.flatMap(result -> ServerResponse.ok().bodyValue(result))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("Failed to trigger cleanup: {}", e.getMessage());
|
||||
@@ -1041,7 +950,6 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
for (var personaData : personasList) {
|
||||
importMono = importMono.then(Mono.defer(() -> {
|
||||
try {
|
||||
var objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
|
||||
var personaJson = objectMapper.writeValueAsString(personaData);
|
||||
var persona = objectMapper.readValue(personaJson, AiPersona.class);
|
||||
var personaName = persona.getMetadata().getName();
|
||||
@@ -1129,4 +1037,131 @@ public class CommentAiAutopilotEndpoint implements CustomEndpoint {
|
||||
.switchIfEmpty(ServerResponse.notFound().build());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 误报反馈:将被拦截的评论标记为误报(正常),并可选触发 AI 回复。
|
||||
*
|
||||
* 请求体:{ "action": "aiReply" | "approveOnly" }
|
||||
* - aiReply: 将评论审核状态设为已通过 + 触发 AI 生成回复
|
||||
* - approveOnly: 仅将评论审核状态设为已通过,不触发 AI 回复
|
||||
*/
|
||||
private Mono<ServerResponse> falsePositive(ServerRequest request) {
|
||||
var name = request.pathVariable("name");
|
||||
return request.bodyToMono(String.class)
|
||||
.flatMap(body -> {
|
||||
String actionStr;
|
||||
try {
|
||||
JsonNode node = objectMapper.readTree(body);
|
||||
actionStr = node.has("action") ? node.get("action").asText("approveOnly") : "approveOnly";
|
||||
} catch (Exception e) {
|
||||
actionStr = "approveOnly";
|
||||
}
|
||||
final String action = actionStr;
|
||||
|
||||
return client.fetch(AiCommentReply.class, name)
|
||||
.flatMap(record -> {
|
||||
String currentStatus = record.getSpec().getStatus();
|
||||
// 允许:FILTERED(拦截误报)、FALSE_POSITIVE(已通过但可触发AI)、FAIL(AI生成失败可重试)
|
||||
if (!"FILTERED".equals(currentStatus)
|
||||
&& !"FALSE_POSITIVE".equals(currentStatus)
|
||||
&& !"FAIL".equals(currentStatus)) {
|
||||
return ServerResponse.badRequest()
|
||||
.bodyValue(Map.of("message", "仅已拦截、误报通过或AI生成失败的记录可进行此操作"));
|
||||
}
|
||||
|
||||
String commentName = record.getSpec().getCommentId();
|
||||
String replyName = record.getSpec().getReplyTo();
|
||||
|
||||
// 1. 将原评论/回复的审核状态设为已通过
|
||||
Mono<Void> approveMono = approveOriginalComment(commentName, replyName);
|
||||
|
||||
// 2. 更新 AiCommentReply 记录状态
|
||||
Mono<Void> updateRecordMono = Mono.defer(() -> client.fetch(AiCommentReply.class, name)
|
||||
.flatMap(latest -> {
|
||||
latest.getSpec().setFilterCategory("误报");
|
||||
latest.getSpec().setFilterReason("用户确认为误报,已通过");
|
||||
if ("aiReply".equals(action)) {
|
||||
latest.getSpec().setStatus("PENDING");
|
||||
latest.getSpec().setReply("");
|
||||
} else {
|
||||
// 仅通过:使用 FALSE_POSITIVE 状态,区别于 PASS
|
||||
// 避免前端显示"通过/拒绝"按钮和"未发布"标签
|
||||
latest.getSpec().setStatus("FALSE_POSITIVE");
|
||||
latest.getSpec().setPublished(false);
|
||||
}
|
||||
return client.update(latest);
|
||||
})
|
||||
.retryWhen(Retry.backoff(3, Duration.ofMillis(100))
|
||||
.filter(e -> e instanceof OptimisticLockingFailureException))
|
||||
.then());
|
||||
|
||||
// 3. 异步触发 AI 回复(在记录更新完成后,不阻塞 HTTP 响应)
|
||||
// 使用 processFalsePositive 跳过前置过滤和去重检查
|
||||
final boolean isConversation = Boolean.TRUE.equals(record.getSpec().getIsAiConversation());
|
||||
final String recordName = record.getMetadata().getName();
|
||||
|
||||
return approveMono
|
||||
.then(updateRecordMono)
|
||||
.doOnSuccess(v -> {
|
||||
if ("aiReply".equals(action)) {
|
||||
personaResolver.getPersonaNameFromComment(commentName)
|
||||
.flatMap(personaName ->
|
||||
orchestrator.processFalsePositive(commentName, replyName, isConversation, personaName, recordName)
|
||||
)
|
||||
.subscribe(
|
||||
null,
|
||||
err -> log.warn("[FalsePositive] AI reply trigger failed for {}: {}", commentName, err.getMessage()),
|
||||
() -> log.info("[FalsePositive] AI reply trigger completed for {}", commentName)
|
||||
);
|
||||
}
|
||||
})
|
||||
.then(ServerResponse.ok().bodyValue(Map.of(
|
||||
"message", "aiReply".equals(action) ? "已标记为误报,AI回复正在后台生成" : "已标记为误报并通过"
|
||||
)));
|
||||
})
|
||||
.switchIfEmpty(ServerResponse.notFound().build());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 将被拦截评论的原 Comment 或 Reply 审核状态设为已通过。
|
||||
*/
|
||||
private Mono<Void> approveOriginalComment(String commentName, String replyName) {
|
||||
// 优先处理 Reply(AI 对话场景下违规内容来自 Reply)
|
||||
if (replyName != null && !replyName.isBlank()) {
|
||||
return client.fetch(Reply.class, replyName)
|
||||
.flatMap(reply -> {
|
||||
var spec = reply.getSpec();
|
||||
if (spec != null && !Boolean.TRUE.equals(spec.getApproved())) {
|
||||
spec.setApproved(true);
|
||||
spec.setApprovedTime(Instant.now());
|
||||
return client.update(reply)
|
||||
.retryWhen(Retry.backoff(3, Duration.ofMillis(100))
|
||||
.filter(e -> e instanceof OptimisticLockingFailureException))
|
||||
.doOnSuccess(r -> log.info("[FalsePositive] Reply {} approved", replyName))
|
||||
.then();
|
||||
}
|
||||
return Mono.empty();
|
||||
})
|
||||
.switchIfEmpty(Mono.defer(() -> approveComment(commentName)));
|
||||
}
|
||||
return approveComment(commentName);
|
||||
}
|
||||
|
||||
private Mono<Void> approveComment(String commentName) {
|
||||
return client.fetch(Comment.class, commentName)
|
||||
.flatMap(comment -> {
|
||||
var spec = comment.getSpec();
|
||||
if (spec != null && !Boolean.TRUE.equals(spec.getApproved())) {
|
||||
spec.setApproved(true);
|
||||
spec.setApprovedTime(Instant.now());
|
||||
return client.update(comment)
|
||||
.retryWhen(Retry.backoff(3, Duration.ofMillis(100))
|
||||
.filter(e -> e instanceof OptimisticLockingFailureException))
|
||||
.doOnSuccess(c -> log.info("[FalsePositive] Comment {} approved", commentName))
|
||||
.then();
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,9 @@ public class AiCommentReply extends AbstractExtension {
|
||||
@Schema(description = "关联文章Slug,用于生成文章链接")
|
||||
private String postSlug;
|
||||
|
||||
@Schema(description = "关联内容类型: Post/SinglePage")
|
||||
private String postKind;
|
||||
|
||||
@Schema(description = "AI回复内容")
|
||||
private String reply;
|
||||
|
||||
@@ -62,5 +65,11 @@ public class AiCommentReply extends AbstractExtension {
|
||||
|
||||
@Schema(description = "已发布的回复名称")
|
||||
private String replyName;
|
||||
|
||||
@Schema(description = "前置过滤拦截分类(广告/辱骂攻击/敏感内容/无意义,为空表示未被拦截)")
|
||||
private String filterCategory;
|
||||
|
||||
@Schema(description = "前置过滤拦截原因详情(为空表示未被拦截)")
|
||||
private String filterReason;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,16 @@ public class AiPersona extends AbstractExtension {
|
||||
@Schema(description = "邮箱(用于Gravatar头像)")
|
||||
private String email;
|
||||
|
||||
@Schema(description = "角色性别(male/female)")
|
||||
private String gender;
|
||||
|
||||
@Schema(description = "是否使用中性语气,默认false即跟随性别语气")
|
||||
@JsonProperty("neutralVoice")
|
||||
private Boolean neutralVoice;
|
||||
|
||||
@Schema(description = "唤醒词,评论以此开头则唤醒该角色回复,留空则不启用唤醒")
|
||||
private String wakeWord;
|
||||
|
||||
@Schema(description = "是否为默认角色")
|
||||
@JsonProperty("isDefault")
|
||||
private Boolean isDefault;
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package top.nxxy335.commentaiautopilot.listener;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import run.halo.app.extension.controller.Controller;
|
||||
import run.halo.app.extension.controller.ControllerBuilder;
|
||||
import run.halo.app.extension.controller.Reconciler;
|
||||
import top.nxxy335.commentaiautopilot.extension.AiPersona;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class AiPersonaReconciler implements Reconciler<Reconciler.Request> {
|
||||
|
||||
private final ReactiveExtensionClient client;
|
||||
|
||||
@Override
|
||||
public Result reconcile(Request request) {
|
||||
return new Result(false, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Controller setupWith(ControllerBuilder builder) {
|
||||
return builder
|
||||
.extension(new AiPersona())
|
||||
.syncAllOnStart(false)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -4,22 +4,19 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
import run.halo.app.core.extension.content.Category;
|
||||
import run.halo.app.core.extension.content.Comment;
|
||||
import run.halo.app.core.extension.content.Post;
|
||||
import run.halo.app.core.extension.content.Tag;
|
||||
import run.halo.app.extension.ExtensionClient;
|
||||
import run.halo.app.extension.controller.Controller;
|
||||
import run.halo.app.extension.controller.ControllerBuilder;
|
||||
import run.halo.app.extension.controller.Reconciler;
|
||||
import top.nxxy335.commentaiautopilot.extension.AiCommentReply;
|
||||
import top.nxxy335.commentaiautopilot.service.AiReplyOrchestrator;
|
||||
import top.nxxy335.commentaiautopilot.service.PersonaResolver;
|
||||
import top.nxxy335.commentaiautopilot.service.WakeWordService;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -28,31 +25,20 @@ public class CommentReconciler implements Reconciler<Reconciler.Request> {
|
||||
|
||||
private final ExtensionClient client;
|
||||
private final AiReplyOrchestrator orchestrator;
|
||||
private final PersonaResolver personaResolver;
|
||||
private final WakeWordService wakeWordService;
|
||||
|
||||
private static final String PROCESSED_ANNOTATION = "comment-ai-autopilot.nxxy335.top/processed";
|
||||
private static final String AI_MARKER_ANNOTATION = "comment-ai-autopilot.nxxy335.top/is-ai";
|
||||
private static final String AI_PERSONA_OWNER_PREFIX = "ai-persona-";
|
||||
private static final String AI_PERSONA_ANNOTATION = "comment-ai-autopilot.nxxy335.top/ai-persona";
|
||||
private static final String AI_MARKER_ANNOTATION = "comment-ai-autopilot.nxxy335.top/is-ai";
|
||||
|
||||
// Record the time when this bean was created (plugin startup time)
|
||||
private final Instant pluginStartTime = Instant.now();
|
||||
|
||||
// In-memory dedup lock: prevents the same comment from being processed multiple times
|
||||
// even if reconcile is triggered concurrently
|
||||
private final ConcurrentHashMap<String, Boolean> processingLocks = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public Result reconcile(Request request) {
|
||||
var name = request.name();
|
||||
|
||||
// Acquire lock at the very beginning to prevent any concurrent processing
|
||||
if (processingLocks.putIfAbsent(name, Boolean.TRUE) != null) {
|
||||
log.debug("[CommentReconciler] Already processing comment: {}, skipping", name);
|
||||
return Result.doNotRetry();
|
||||
}
|
||||
|
||||
AtomicBoolean asyncStarted = new AtomicBoolean(false);
|
||||
try {
|
||||
client.fetch(Comment.class, name).ifPresent(comment -> {
|
||||
if (isProcessed(comment.getMetadata().getAnnotations())) {
|
||||
return;
|
||||
@@ -92,33 +78,39 @@ public class CommentReconciler implements Reconciler<Reconciler.Request> {
|
||||
markProcessed(comment);
|
||||
client.update(comment);
|
||||
|
||||
// Read persona name from the post's annotations
|
||||
String personaName = getPersonaNameFromComment(comment);
|
||||
// Check for wake word in comment content
|
||||
String commentContent = getCommentContent(comment);
|
||||
log.info("[CommentReconciler] Wake word check for comment {}: content='{}'",
|
||||
name, commentContent.length() > 80 ? commentContent.substring(0, 80) + "..." : commentContent);
|
||||
var wakeMatch = wakeWordService.checkWakeWordBlocking(client, commentContent);
|
||||
|
||||
if (wakeMatch != null) {
|
||||
// Wake word matched: trigger AI reply with the matched persona,
|
||||
// bypassing normal page-level enable check
|
||||
log.info("[CommentReconciler] Wake word '{}' matched for persona '{}', triggering reply for: {}",
|
||||
wakeMatch.wakeWord(), wakeMatch.personaName(), name);
|
||||
orchestrator.processComment(name, null, false, wakeMatch.personaName(), true)
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.subscribe(
|
||||
null,
|
||||
e -> log.error("[CommentReconciler] Error processing wake word comment {}: {}", name, e.getMessage(), e),
|
||||
() -> log.info("[CommentReconciler] Wake word processing completed for comment: {}", name)
|
||||
);
|
||||
} else {
|
||||
// Normal flow: read persona name from the post's annotations
|
||||
String personaName = personaResolver.getPersonaNameFromCommentBlocking(client, comment);
|
||||
|
||||
// Top-level comment → always trigger AI reply
|
||||
log.info("[CommentReconciler] New top-level comment detected: {}, personaName: {}", name, personaName);
|
||||
asyncStarted.set(true);
|
||||
orchestrator.processComment(name, null, false, personaName)
|
||||
orchestrator.processComment(name, null, false, personaName, false)
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.doFinally(signal -> {
|
||||
processingLocks.remove(name);
|
||||
log.debug("[CommentReconciler] Released processing lock for: {}", name);
|
||||
})
|
||||
.subscribe(
|
||||
null,
|
||||
e -> log.error("[CommentReconciler] Error processing comment {}: {}", name, e.getMessage(), e),
|
||||
() -> log.info("[CommentReconciler] Processing completed for comment: {}", name)
|
||||
);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("[CommentReconciler] Error in reconcile for {}: {}", name, e.getMessage(), e);
|
||||
} finally {
|
||||
// Only release lock here if async processing was NOT started
|
||||
// (async path releases lock in doFinally)
|
||||
if (!asyncStarted.get()) {
|
||||
processingLocks.remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
return Result.doNotRetry();
|
||||
}
|
||||
@@ -139,61 +131,6 @@ public class CommentReconciler implements Reconciler<Reconciler.Request> {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read persona name from the post's annotations associated with this comment.
|
||||
*/
|
||||
private String getPersonaNameFromComment(Comment comment) {
|
||||
var subjectRef = comment.getSpec().getSubjectRef();
|
||||
if (subjectRef == null || !"Post".equals(subjectRef.getKind())) {
|
||||
return null;
|
||||
}
|
||||
String postName = subjectRef.getName();
|
||||
return client.fetch(Post.class, postName)
|
||||
.map(post -> {
|
||||
// 1. 文章注解优先
|
||||
var annotations = post.getMetadata().getAnnotations();
|
||||
if (annotations != null) {
|
||||
String persona = annotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (persona != null && !persona.isBlank()) {
|
||||
return persona;
|
||||
}
|
||||
}
|
||||
// 2. 分类注解
|
||||
var spec = post.getSpec();
|
||||
if (spec != null && spec.getCategories() != null) {
|
||||
for (String categoryName : spec.getCategories()) {
|
||||
var cat = client.fetch(Category.class, categoryName).orElse(null);
|
||||
if (cat != null) {
|
||||
var catAnnotations = cat.getMetadata().getAnnotations();
|
||||
if (catAnnotations != null) {
|
||||
String catPersona = catAnnotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (catPersona != null && !catPersona.isBlank()) {
|
||||
return catPersona;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3. 标签注解
|
||||
if (spec != null && spec.getTags() != null) {
|
||||
for (String tagName : spec.getTags()) {
|
||||
var tag = client.fetch(Tag.class, tagName).orElse(null);
|
||||
if (tag != null) {
|
||||
var tagAnnotations = tag.getMetadata().getAnnotations();
|
||||
if (tagAnnotations != null) {
|
||||
String tagPersona = tagAnnotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (tagPersona != null && !tagPersona.isBlank()) {
|
||||
return tagPersona;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private boolean isProcessed(Map<String, String> annotations) {
|
||||
return annotations != null && "true".equals(annotations.get(PROCESSED_ANNOTATION));
|
||||
}
|
||||
@@ -207,6 +144,22 @@ public class CommentReconciler implements Reconciler<Reconciler.Request> {
|
||||
annotations.put(PROCESSED_ANNOTATION, "true");
|
||||
}
|
||||
|
||||
private String getCommentContent(Comment comment) {
|
||||
if (comment.getSpec() == null) return "";
|
||||
// Always strip HTML to get plain text for wake word matching
|
||||
String raw = comment.getSpec().getRaw();
|
||||
if (raw != null && !raw.isBlank()) {
|
||||
// raw might still contain HTML in some cases, always strip
|
||||
String plain = org.jsoup.Jsoup.clean(raw, org.jsoup.safety.Safelist.none()).trim();
|
||||
if (!plain.isBlank()) return plain;
|
||||
}
|
||||
String content = comment.getSpec().getContent();
|
||||
if (content != null && !content.isBlank()) {
|
||||
return org.jsoup.Jsoup.clean(content, org.jsoup.safety.Safelist.none()).trim();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Controller setupWith(ControllerBuilder builder) {
|
||||
return builder
|
||||
|
||||
@@ -4,17 +4,16 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
import run.halo.app.core.extension.content.Category;
|
||||
import run.halo.app.core.extension.content.Comment;
|
||||
import run.halo.app.core.extension.content.Post;
|
||||
import run.halo.app.core.extension.content.Reply;
|
||||
import run.halo.app.core.extension.content.Tag;
|
||||
import run.halo.app.extension.ExtensionClient;
|
||||
import run.halo.app.extension.controller.Controller;
|
||||
import run.halo.app.extension.controller.ControllerBuilder;
|
||||
import run.halo.app.extension.controller.Reconciler;
|
||||
import top.nxxy335.commentaiautopilot.extension.AiCommentReply;
|
||||
import top.nxxy335.commentaiautopilot.service.AiReplyOrchestrator;
|
||||
import top.nxxy335.commentaiautopilot.service.PersonaResolver;
|
||||
import top.nxxy335.commentaiautopilot.service.WakeWordService;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
@@ -27,11 +26,12 @@ public class ReplyReconciler implements Reconciler<Reconciler.Request> {
|
||||
|
||||
private final ExtensionClient client;
|
||||
private final AiReplyOrchestrator orchestrator;
|
||||
private final PersonaResolver personaResolver;
|
||||
private final WakeWordService wakeWordService;
|
||||
|
||||
private static final String PROCESSED_ANNOTATION = "comment-ai-autopilot.nxxy335.top/processed";
|
||||
private static final String AI_PERSONA_OWNER_PREFIX = "ai-persona-";
|
||||
private static final String AI_MARKER_ANNOTATION = "comment-ai-autopilot.nxxy335.top/is-ai";
|
||||
private static final String AI_PERSONA_ANNOTATION = "comment-ai-autopilot.nxxy335.top/ai-persona";
|
||||
|
||||
// Record the time when this bean was created (plugin startup time)
|
||||
private final Instant pluginStartTime = Instant.now();
|
||||
@@ -77,25 +77,21 @@ public class ReplyReconciler implements Reconciler<Reconciler.Request> {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this reply is specifically replying to an AI reply
|
||||
// by checking the quoteReply field
|
||||
String quoteReply = reply.getSpec().getQuoteReply();
|
||||
|
||||
if (quoteReply == null || quoteReply.isBlank()) {
|
||||
// No quoteReply - this is a direct reply to the top-level comment,
|
||||
// NOT a reply to AI. Skip it (CommentReconciler handles top-level comments).
|
||||
log.debug("[ReplyReconciler] Reply {} has no quoteReply, skipping (not a reply to AI)", name);
|
||||
markProcessed(reply);
|
||||
client.update(reply);
|
||||
return;
|
||||
}
|
||||
// Check for wake word FIRST - wake word can bypass "must be reply to AI" check
|
||||
String replyContent = getReplyContent(reply);
|
||||
log.info("[ReplyReconciler] Wake word check for reply {}: content='{}'",
|
||||
name, replyContent.length() > 80 ? replyContent.substring(0, 80) + "..." : replyContent);
|
||||
var wakeMatch = wakeWordService.checkWakeWordBlocking(client, replyContent);
|
||||
|
||||
// This reply quotes another reply - check if the quoted reply is from AI
|
||||
boolean isReplyToAi = isAiReply(quoteReply);
|
||||
String quoteReply = reply.getSpec().getQuoteReply();
|
||||
boolean isReplyToAi = quoteReply != null && !quoteReply.isBlank() && isAiReply(quoteReply);
|
||||
log.debug("[ReplyReconciler] Reply {} quotes {}, isAiReply={}", name, quoteReply, isReplyToAi);
|
||||
|
||||
if (!isReplyToAi) {
|
||||
log.debug("[ReplyReconciler] Not a reply to AI, skipping: {}", name);
|
||||
// Skip if not a reply to AI AND no wake word matched
|
||||
if (!isReplyToAi && wakeMatch == null) {
|
||||
// No quoteReply or not replying to AI, and no wake word - skip
|
||||
log.debug("[ReplyReconciler] Not a reply to AI and no wake word, skipping: {}", name);
|
||||
markProcessed(reply);
|
||||
client.update(reply);
|
||||
return;
|
||||
@@ -119,16 +115,32 @@ public class ReplyReconciler implements Reconciler<Reconciler.Request> {
|
||||
markProcessed(reply);
|
||||
client.update(reply);
|
||||
|
||||
// Reply to AI → trigger AI reply (conversation continuation)
|
||||
String personaName = getPersonaNameFromComment(parentCommentName);
|
||||
if (wakeMatch != null) {
|
||||
// Wake word matched: trigger AI reply with the matched persona,
|
||||
// bypassing the "must be reply to AI" check and page-level enable check
|
||||
log.info("[ReplyReconciler] Wake word '{}' matched for persona '{}', triggering reply for: {}",
|
||||
wakeMatch.wakeWord(), wakeMatch.personaName(), name);
|
||||
orchestrator.processComment(parentCommentName, name, true, wakeMatch.personaName(), true)
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.subscribe(
|
||||
null,
|
||||
e -> log.error("[ReplyReconciler] Error processing wake word reply {}: {}", name, e.getMessage(), e),
|
||||
() -> log.info("[ReplyReconciler] Wake word processing completed for reply: {}", name)
|
||||
);
|
||||
} else if (isReplyToAi) {
|
||||
// Normal flow: reply to AI → trigger AI reply (conversation continuation)
|
||||
String personaName = client.fetch(Comment.class, parentCommentName)
|
||||
.map(comment -> personaResolver.getPersonaNameFromCommentBlocking(client, comment))
|
||||
.orElse(null);
|
||||
log.info("[ReplyReconciler] Reply to AI detected: {}, triggering conversation, personaName: {}", name, personaName);
|
||||
orchestrator.processComment(parentCommentName, name, true, personaName)
|
||||
orchestrator.processComment(parentCommentName, name, true, personaName, false)
|
||||
.subscribeOn(Schedulers.boundedElastic())
|
||||
.subscribe(
|
||||
null,
|
||||
e -> log.error("[ReplyReconciler] Error processing reply {}: {}", name, e.getMessage(), e),
|
||||
() -> log.info("[ReplyReconciler] Processing completed for reply: {}", name)
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return Result.doNotRetry();
|
||||
@@ -154,65 +166,6 @@ public class ReplyReconciler implements Reconciler<Reconciler.Request> {
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read persona name from the post's annotations associated with the parent comment.
|
||||
*/
|
||||
private String getPersonaNameFromComment(String commentName) {
|
||||
return client.fetch(Comment.class, commentName)
|
||||
.map(comment -> {
|
||||
var subjectRef = comment.getSpec().getSubjectRef();
|
||||
if (subjectRef == null || !"Post".equals(subjectRef.getKind())) {
|
||||
return null;
|
||||
}
|
||||
String postName = subjectRef.getName();
|
||||
return client.fetch(Post.class, postName)
|
||||
.map(post -> {
|
||||
// 1. 文章注解优先
|
||||
var annotations = post.getMetadata().getAnnotations();
|
||||
if (annotations != null) {
|
||||
String persona = annotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (persona != null && !persona.isBlank()) {
|
||||
return persona;
|
||||
}
|
||||
}
|
||||
// 2. 分类注解
|
||||
var spec = post.getSpec();
|
||||
if (spec != null && spec.getCategories() != null) {
|
||||
for (String categoryName : spec.getCategories()) {
|
||||
var cat = client.fetch(Category.class, categoryName).orElse(null);
|
||||
if (cat != null) {
|
||||
var catAnnotations = cat.getMetadata().getAnnotations();
|
||||
if (catAnnotations != null) {
|
||||
String catPersona = catAnnotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (catPersona != null && !catPersona.isBlank()) {
|
||||
return catPersona;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3. 标签注解
|
||||
if (spec != null && spec.getTags() != null) {
|
||||
for (String tagName : spec.getTags()) {
|
||||
var tag = client.fetch(Tag.class, tagName).orElse(null);
|
||||
if (tag != null) {
|
||||
var tagAnnotations = tag.getMetadata().getAnnotations();
|
||||
if (tagAnnotations != null) {
|
||||
String tagPersona = tagAnnotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (tagPersona != null && !tagPersona.isBlank()) {
|
||||
return tagPersona;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.orElse(null);
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
private boolean isProcessed(Map<String, String> annotations) {
|
||||
return annotations != null && "true".equals(annotations.get(PROCESSED_ANNOTATION));
|
||||
}
|
||||
@@ -226,6 +179,22 @@ public class ReplyReconciler implements Reconciler<Reconciler.Request> {
|
||||
annotations.put(PROCESSED_ANNOTATION, "true");
|
||||
}
|
||||
|
||||
private String getReplyContent(Reply reply) {
|
||||
if (reply.getSpec() == null) return "";
|
||||
// Always strip HTML to get plain text for wake word matching
|
||||
String raw = reply.getSpec().getRaw();
|
||||
if (raw != null && !raw.isBlank()) {
|
||||
// raw might still contain HTML in some cases, always strip
|
||||
String plain = org.jsoup.Jsoup.clean(raw, org.jsoup.safety.Safelist.none()).trim();
|
||||
if (!plain.isBlank()) return plain;
|
||||
}
|
||||
String content = reply.getSpec().getContent();
|
||||
if (content != null && !content.isBlank()) {
|
||||
return org.jsoup.Jsoup.clean(content, org.jsoup.safety.Safelist.none()).trim();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Controller setupWith(ControllerBuilder builder) {
|
||||
return builder
|
||||
|
||||
@@ -1,219 +1,101 @@
|
||||
package top.nxxy335.commentaiautopilot.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
import run.halo.app.core.extension.Plugin;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import run.halo.app.plugin.extensionpoint.ExtensionGetter;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI Foundation client that uses runtime class loading and reflection
|
||||
* to call the AI Foundation plugin's AiModelService.
|
||||
* <p>
|
||||
* This approach avoids classloader identity issues by loading AiModelService
|
||||
* from ai-foundation's own classloader, so that Spring's getBeansOfType()
|
||||
* can correctly match the implementation bean.
|
||||
* <p>
|
||||
* No @ConditionalOnClass or pluginDependencies needed.
|
||||
* Always registered as a bean; availability is checked at runtime.
|
||||
* AI Foundation 客户端,通过 Halo 的 {@link ExtensionGetter} 获取 AI 服务。
|
||||
*
|
||||
* <p>此类不直接引用任何 AI Foundation API 类(AiModelService、GenerateTextRequest 等),
|
||||
* 所有 AI Foundation 交互委托给 {@link AiFoundationDelegate}。
|
||||
* 当 AI Foundation 插件未安装时,{@link AiFoundationDelegate} 的类加载会触发
|
||||
* {@link NoClassDefFoundError},在 {@code Mono.defer()} 中被捕获,
|
||||
* 保证插件在无 AI Foundation 环境下仍可正常启动。
|
||||
*
|
||||
* <p>需要在 plugin.yaml 中声明可选依赖:
|
||||
* <pre>
|
||||
* spec:
|
||||
* pluginDependencies:
|
||||
* ai-foundation?: "*"
|
||||
* </pre>
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AiFoundationClient {
|
||||
|
||||
private static final String AI_FOUNDATION_PLUGIN_NAME = "ai-foundation";
|
||||
private static final String AI_MODEL_SERVICE_CLASS = "run.halo.aifoundation.AiModelService";
|
||||
private final ExtensionGetter extensionGetter;
|
||||
|
||||
private final ReactiveExtensionClient client;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public AiFoundationClient(ReactiveExtensionClient client, ApplicationContext applicationContext) {
|
||||
this.client = client;
|
||||
this.applicationContext = applicationContext;
|
||||
public AiFoundationClient(ExtensionGetter extensionGetter) {
|
||||
this.extensionGetter = extensionGetter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call AI Foundation to generate a chat response using the specified model.
|
||||
* 调用 AI Foundation 生成聊天回复。
|
||||
*
|
||||
* @param prompt the prompt text
|
||||
* @param modelName the AiModel metadata.name, null or blank to use default model
|
||||
* @return the generated text, or empty if AI Foundation is unavailable
|
||||
* @param prompt 提示词文本
|
||||
* @param modelName AiModel metadata.name,null 或空则使用默认模型
|
||||
* @return 生成的文本,AI Foundation 不可用时返回 empty
|
||||
*/
|
||||
public Mono<String> chat(String prompt, String modelName) {
|
||||
return isAiFoundationEnabled()
|
||||
.flatMap(enabled -> {
|
||||
if (!enabled) {
|
||||
log.warn("AI Foundation plugin is not installed or not enabled, skipping AI reply");
|
||||
return Mono.defer(() -> {
|
||||
try {
|
||||
return AiFoundationDelegate.chat(extensionGetter, prompt, modelName);
|
||||
} catch (NoClassDefFoundError e) {
|
||||
log.debug("AI Foundation API not on classpath: {}", e.getMessage());
|
||||
return Mono.empty();
|
||||
}
|
||||
return doChat(prompt, modelName);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if AI Foundation is available: plugin installed, enabled, and AiModelService bean found.
|
||||
*/
|
||||
public Mono<Boolean> isAvailable() {
|
||||
return isAiFoundationEnabled()
|
||||
.flatMap(enabled -> {
|
||||
if (!enabled) return Mono.just(false);
|
||||
return findAiModelService().hasElement();
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Boolean> isAiFoundationEnabled() {
|
||||
return client.fetch(Plugin.class, AI_FOUNDATION_PLUGIN_NAME)
|
||||
.map(plugin -> plugin.getSpec().getEnabled())
|
||||
.defaultIfEmpty(false)
|
||||
.onErrorResume(e -> {
|
||||
log.debug("Failed to check AI Foundation plugin status: {}", e.getMessage());
|
||||
return Mono.just(false);
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<String> doChat(String prompt, String modelName) {
|
||||
return findAiModelService()
|
||||
.flatMap(service -> invokeLanguageModel(service, modelName)
|
||||
.flatMap(model -> invokeGenerateText(model, prompt))
|
||||
)
|
||||
.doOnError(e -> log.error("AI Foundation call failed: {}", e.getMessage()))
|
||||
.onErrorResume(e -> {
|
||||
})
|
||||
.onErrorResume(NoClassDefFoundError.class, e -> {
|
||||
log.warn("AI Foundation not available: {}", e.getMessage());
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get PluginManager via the pluginWrapper bean registered in our plugin context.
|
||||
* Halo's DefaultPluginApplicationContextFactory registers pluginWrapper as a singleton:
|
||||
* beanFactory.registerSingleton("pluginWrapper", pluginWrapper);
|
||||
* Then PluginWrapper.getPluginManager() gives us the PluginManager instance.
|
||||
* 调用 AI Foundation 进行文本分类,使用结构化输出(OutputSpec.choice)。
|
||||
*
|
||||
* @param systemPrompt 系统提示词
|
||||
* @param userPrompt 待分类的用户输入
|
||||
* @param choices 允许的分类值列表
|
||||
* @param modelName AiModel metadata.name,null 或空则使用默认模型
|
||||
* @return 选中的分类字符串,AI Foundation 不可用时返回 empty
|
||||
*/
|
||||
private Object findPluginManager() {
|
||||
public Mono<String> classify(String systemPrompt, String userPrompt,
|
||||
List<String> choices, String modelName) {
|
||||
return Mono.defer(() -> {
|
||||
try {
|
||||
Object pluginWrapper = applicationContext.getBean("pluginWrapper");
|
||||
Method getPluginManagerMethod = pluginWrapper.getClass().getMethod("getPluginManager");
|
||||
getPluginManagerMethod.setAccessible(true);
|
||||
Object pm = getPluginManagerMethod.invoke(pluginWrapper);
|
||||
if (pm != null) {
|
||||
log.info("Found PluginManager via pluginWrapper bean: {}", pm.getClass().getName());
|
||||
return AiFoundationDelegate.classify(extensionGetter, systemPrompt, userPrompt, choices, modelName);
|
||||
} catch (NoClassDefFoundError e) {
|
||||
log.warn("[Client] AI Foundation API not on classpath (classify): {}", e.getMessage());
|
||||
return Mono.empty();
|
||||
}
|
||||
return pm;
|
||||
} catch (NoSuchMethodException e) {
|
||||
log.warn("pluginWrapper does not have getPluginManager() method: {}", e.getMessage());
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to get PluginManager via pluginWrapper: {}", e.getMessage());
|
||||
}
|
||||
log.warn("PluginManager not found");
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the AiModelService bean from ai-foundation's PluginApplicationContext.
|
||||
* Uses PluginManager.getPlugin() to get the plugin wrapper, then reflection
|
||||
* to get the plugin's ApplicationContext.
|
||||
*/
|
||||
private Mono<Object> findAiModelService() {
|
||||
return Mono.fromCallable(() -> {
|
||||
Object pm = findPluginManager();
|
||||
if (pm == null) return null;
|
||||
|
||||
// Call pm.getPlugin("ai-foundation") via reflection
|
||||
Method getPluginMethod = pm.getClass().getMethod("getPlugin", String.class);
|
||||
getPluginMethod.setAccessible(true);
|
||||
Object pluginWrapper = getPluginMethod.invoke(pm, AI_FOUNDATION_PLUGIN_NAME);
|
||||
if (pluginWrapper == null) {
|
||||
log.debug("ai-foundation plugin not found in PluginManager");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Call pluginWrapper.getPlugin() to get the plugin instance
|
||||
Method getPluginInstanceMethod = pluginWrapper.getClass().getMethod("getPlugin");
|
||||
getPluginInstanceMethod.setAccessible(true);
|
||||
Object pluginInstance = getPluginInstanceMethod.invoke(pluginWrapper);
|
||||
if (pluginInstance == null) {
|
||||
log.debug("ai-foundation plugin instance is null");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get the plugin's ApplicationContext via reflection on SpringPlugin
|
||||
// DefaultSpringPlugin is package-private, so we need setAccessible
|
||||
Method getCtxMethod = pluginInstance.getClass().getMethod("getApplicationContext");
|
||||
getCtxMethod.setAccessible(true);
|
||||
ApplicationContext pluginAppContext = (ApplicationContext) getCtxMethod.invoke(pluginInstance);
|
||||
|
||||
// Get the plugin classloader
|
||||
Method getClassLoaderMethod = pluginWrapper.getClass().getMethod("getPluginClassLoader");
|
||||
getClassLoaderMethod.setAccessible(true);
|
||||
ClassLoader pluginClassLoader = (ClassLoader) getClassLoaderMethod.invoke(pluginWrapper);
|
||||
|
||||
// Load AiModelService from ai-foundation's classloader
|
||||
Class<?> aiModelServiceClass = pluginClassLoader.loadClass(AI_MODEL_SERVICE_CLASS);
|
||||
|
||||
// Find the AiModelService bean in ai-foundation's ApplicationContext
|
||||
Map<String, ?> beans = pluginAppContext.getBeansOfType(aiModelServiceClass);
|
||||
if (beans.isEmpty()) {
|
||||
log.debug("AiModelService bean not found in ai-foundation's ApplicationContext");
|
||||
return null;
|
||||
}
|
||||
|
||||
log.info("Found AiModelService bean in ai-foundation's ApplicationContext");
|
||||
Object result = beans.values().iterator().next();
|
||||
return (Object) result;
|
||||
}).doOnError(e -> log.error("Failed to find AiModelService: {}", e.getMessage()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Call service.languageModel(modelName) or service.languageModel() via reflection.
|
||||
* Returns Mono<LanguageModel> from ai-foundation's classloader.
|
||||
*/
|
||||
private Mono<Object> invokeLanguageModel(Object service, String modelName) {
|
||||
return Mono.fromCallable(() -> {
|
||||
Method method;
|
||||
if (modelName != null && !modelName.isBlank()) {
|
||||
method = service.getClass().getMethod("languageModel", String.class);
|
||||
method.setAccessible(true);
|
||||
return method.invoke(service, modelName);
|
||||
} else {
|
||||
method = service.getClass().getMethod("languageModel");
|
||||
method.setAccessible(true);
|
||||
return method.invoke(service);
|
||||
}
|
||||
}).flatMap(result -> {
|
||||
if (result instanceof Mono<?> mono) return mono;
|
||||
return Mono.justOrEmpty(result);
|
||||
})
|
||||
.onErrorResume(NoClassDefFoundError.class, e -> {
|
||||
log.warn("[Client] AI Foundation NoClassDefFoundError during classify: {}", e.getMessage());
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Call model.generateText(prompt) via reflection, then extract text from result.
|
||||
* Returns the generated text string.
|
||||
* 检查 AI Foundation 是否可用(插件已安装且 AiModelService 扩展已启用)。
|
||||
*/
|
||||
private Mono<String> invokeGenerateText(Object model, String prompt) {
|
||||
return Mono.fromCallable(() -> {
|
||||
Method method = model.getClass().getMethod("generateText", String.class);
|
||||
method.setAccessible(true);
|
||||
return method.invoke(model, prompt);
|
||||
}).flatMap(result -> {
|
||||
if (result instanceof Mono<?> mono) {
|
||||
return mono.map(this::extractText);
|
||||
}
|
||||
return Mono.justOrEmpty(extractText(result));
|
||||
});
|
||||
}
|
||||
|
||||
private String extractText(Object result) {
|
||||
if (result == null) return null;
|
||||
public Mono<Boolean> isAvailable() {
|
||||
return Mono.defer(() -> {
|
||||
try {
|
||||
Method getText = result.getClass().getMethod("getText");
|
||||
getText.setAccessible(true);
|
||||
return (String) getText.invoke(result);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to call getText() on GenerateTextResult: " + e.getMessage(), e);
|
||||
return AiFoundationDelegate.isAvailable(extensionGetter);
|
||||
} catch (NoClassDefFoundError e) {
|
||||
log.debug("AI Foundation API not on classpath: {}", e.getMessage());
|
||||
return Mono.just(false);
|
||||
}
|
||||
})
|
||||
.onErrorResume(NoClassDefFoundError.class, e -> Mono.just(false))
|
||||
.onErrorResume(e -> {
|
||||
log.debug("AI Foundation not available: {}", e.getMessage());
|
||||
return Mono.just(false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package top.nxxy335.commentaiautopilot.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import reactor.core.publisher.Mono;
|
||||
import run.halo.aifoundation.AiModelService;
|
||||
import run.halo.aifoundation.chat.GenerateTextRequest;
|
||||
import run.halo.aifoundation.chat.GenerateTextResult;
|
||||
import run.halo.aifoundation.schema.OutputSpec;
|
||||
import run.halo.app.plugin.extensionpoint.ExtensionGetter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* AI Foundation API 隔离层。
|
||||
*
|
||||
* <p>此类集中了所有对 AI Foundation 插件 API 的直接引用(AiModelService、
|
||||
* GenerateTextRequest、GenerateTextResult、OutputSpec)。
|
||||
*
|
||||
* <p>关键设计:此类不是 Spring 组件,由 {@link AiFoundationClient} 通过
|
||||
* {@code Mono.defer()} 懒加载调用。当 AI Foundation 插件未安装时,
|
||||
* JVM 加载此类会触发 NoClassDefFoundError,该错误在
|
||||
* {@code AiFoundationClient} 的 defer + try-catch 中被捕获,
|
||||
* 从而保证插件在无 AI Foundation 的环境下仍可正常启动。
|
||||
*/
|
||||
@Slf4j
|
||||
class AiFoundationDelegate {
|
||||
|
||||
private AiFoundationDelegate() {}
|
||||
|
||||
static Mono<String> chat(ExtensionGetter extensionGetter, String prompt, String modelName) {
|
||||
return extensionGetter.getEnabledExtension(AiModelService.class)
|
||||
.flatMap(service -> service.languageModel(modelName != null ? modelName : "")
|
||||
.flatMap(model -> model.generateText(
|
||||
GenerateTextRequest.builder().prompt(prompt).maxRetries(2).build()))
|
||||
.map(GenerateTextResult::getText))
|
||||
.doOnError(e -> log.error("[Delegate] chat call failed: {}", e.getMessage()))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Delegate] chat not available: {}", e.getMessage());
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 AI 进行文本分类。
|
||||
* 优先使用 OutputSpec.choice 结构化输出,失败时退回到普通 chat 并解析响应。
|
||||
*/
|
||||
static Mono<String> classify(ExtensionGetter extensionGetter, String systemPrompt,
|
||||
String userPrompt, List<String> choices, String modelName) {
|
||||
log.info("[Delegate] Starting classification, modelName='{}'", modelName);
|
||||
return classifyWithChoice(extensionGetter, systemPrompt, userPrompt, choices, modelName)
|
||||
.switchIfEmpty(
|
||||
Mono.defer(() -> {
|
||||
log.info("[Delegate] classifyWithChoice returned empty, falling back to classifyWithChat");
|
||||
return classifyWithChat(extensionGetter, systemPrompt, userPrompt, choices, modelName);
|
||||
})
|
||||
)
|
||||
.doOnNext(result -> log.info("[Delegate] Classification succeeded: '{}'", result))
|
||||
.doOnSuccess(result -> {
|
||||
if (result == null) {
|
||||
log.warn("[Delegate] Classification completed with no result (both methods returned empty)");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用 OutputSpec.choice 结构化输出分类(部分模型不支持)。
|
||||
* 注意:不使用 system() 方法,因为部分 AI Foundation 版本可能不支持,
|
||||
* 将 system prompt 合并到 user prompt 中。
|
||||
*/
|
||||
private static Mono<String> classifyWithChoice(ExtensionGetter extensionGetter, String systemPrompt,
|
||||
String userPrompt, List<String> choices, String modelName) {
|
||||
// 合并 system prompt 和 user prompt,避免使用 system() 方法
|
||||
String combinedPrompt = systemPrompt + "\n\n" + userPrompt;
|
||||
return extensionGetter.getEnabledExtension(AiModelService.class)
|
||||
.flatMap(service -> service.languageModel(modelName != null ? modelName : "")
|
||||
.flatMap(model -> model.generateText(
|
||||
GenerateTextRequest.builder()
|
||||
.prompt(combinedPrompt)
|
||||
.output(OutputSpec.choice(choices))
|
||||
.maxRetries(2)
|
||||
.build()))
|
||||
.flatMap(result -> {
|
||||
Object output = result.getOutput();
|
||||
if (output != null) {
|
||||
String value = String.valueOf(output).trim();
|
||||
if (!value.isEmpty()) {
|
||||
log.debug("[Delegate] classifyWithChoice got output: '{}'", value);
|
||||
return Mono.just(value);
|
||||
}
|
||||
}
|
||||
// output 为空可能是模型不支持结构化输出,返回 empty 触发 fallback
|
||||
log.info("[Delegate] classifyWithChoice: output is null/empty, triggering fallback");
|
||||
return Mono.empty();
|
||||
}))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Delegate] classifyWithChoice failed, will fallback to chat: {}", e.getMessage());
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用普通 chat 调用进行分类,从响应文本中提取匹配的分类值。
|
||||
* 作为 OutputSpec.choice 不可用时的降级方案。
|
||||
* 不使用 system() 方法,将 system prompt 合并到 user prompt 中,
|
||||
* 与可用的 chat() 方法保持一致的调用方式。
|
||||
*/
|
||||
private static Mono<String> classifyWithChat(ExtensionGetter extensionGetter, String systemPrompt,
|
||||
String userPrompt, List<String> choices, String modelName) {
|
||||
// 合并 system prompt 和 user prompt,与 chat() 方法保持一致的调用方式
|
||||
String combinedPrompt = systemPrompt + "\n\n" + userPrompt;
|
||||
return extensionGetter.getEnabledExtension(AiModelService.class)
|
||||
.flatMap(service -> service.languageModel(modelName != null ? modelName : "")
|
||||
.flatMap(model -> model.generateText(
|
||||
GenerateTextRequest.builder()
|
||||
.prompt(combinedPrompt)
|
||||
.maxRetries(2)
|
||||
.build()))
|
||||
.map(GenerateTextResult::getText)
|
||||
.map(text -> extractChoice(text, choices)))
|
||||
.doOnError(e -> log.error("[Delegate] classifyWithChat failed: {}", e.getMessage()))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Delegate] classifyWithChat error: {}", e.getMessage());
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 chat 响应文本中提取匹配的分类值。
|
||||
* 优先精确匹配,其次包含匹配。
|
||||
* 包含匹配时优先匹配违规类别(广告/辱骂/敏感/无意义),最后才匹配"正常",
|
||||
* 避免 AI 解释性文本中同时出现"正常"和违规词时误判为"正常"。
|
||||
* 无匹配时返回空字符串(触发 defaultIfEmpty 安全拦截),避免原始文本被误判为违规类别。
|
||||
*/
|
||||
static String extractChoice(String text, List<String> choices) {
|
||||
if (text == null || text.isBlank()) return "";
|
||||
String trimmed = text.trim();
|
||||
// 精确匹配
|
||||
for (String choice : choices) {
|
||||
if (trimmed.equals(choice)) return choice;
|
||||
}
|
||||
// 包含匹配:先匹配违规类别,最后匹配"正常"
|
||||
// 避免"该评论属于广告,不是正常评论"被误匹配为"正常"
|
||||
for (String choice : choices) {
|
||||
if ("正常".equals(choice)) continue;
|
||||
if (trimmed.contains(choice)) return choice;
|
||||
}
|
||||
// 最后检查"正常"
|
||||
for (String choice : choices) {
|
||||
if ("正常".equals(choice) && trimmed.contains(choice)) return choice;
|
||||
}
|
||||
// 无匹配,返回空字符串触发安全拦截
|
||||
log.warn("[Delegate] No matching choice found in response: '{}', returning empty for safety", trimmed);
|
||||
return "";
|
||||
}
|
||||
|
||||
static Mono<Boolean> isAvailable(ExtensionGetter extensionGetter) {
|
||||
return extensionGetter.getEnabledExtension(AiModelService.class)
|
||||
.hasElement()
|
||||
.onErrorResume(e -> {
|
||||
log.debug("[Delegate] AI Foundation not available: {}", e.getMessage());
|
||||
return Mono.just(false);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,9 @@ import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AiReplyCleanupService implements DisposableBean {
|
||||
@@ -27,9 +30,9 @@ public class AiReplyCleanupService implements DisposableBean {
|
||||
|
||||
private static final String CONFIG_MAP_NAME = "comment-ai-autopilot-configmap";
|
||||
|
||||
public AiReplyCleanupService(ReactiveExtensionClient client) {
|
||||
public AiReplyCleanupService(ReactiveExtensionClient client, ObjectMapper objectMapper) {
|
||||
this.client = client;
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper = objectMapper;
|
||||
this.scheduler = Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
Thread t = new Thread(r, "ai-reply-cleanup");
|
||||
t.setDaemon(true);
|
||||
@@ -40,8 +43,25 @@ public class AiReplyCleanupService implements DisposableBean {
|
||||
}
|
||||
|
||||
public void dailyCleanup() {
|
||||
try {
|
||||
Boolean enabled = client.fetch(ConfigMap.class, CONFIG_MAP_NAME)
|
||||
isCleanupEnabled()
|
||||
.flatMap(enabled -> {
|
||||
if (!Boolean.TRUE.equals(enabled)) {
|
||||
log.debug("[Cleanup] Auto cleanup is disabled, skipping");
|
||||
return Mono.empty();
|
||||
}
|
||||
return getRetentionDays()
|
||||
.flatMap(retentionDays -> executeCleanup(retentionDays)
|
||||
.doOnNext(deleted -> log.info("[Cleanup] Auto cleanup completed, deleted {} records older than {} days", deleted, retentionDays))
|
||||
);
|
||||
})
|
||||
.subscribe(
|
||||
null,
|
||||
e -> log.error("[Cleanup] Error during daily cleanup: {}", e.getMessage(), e)
|
||||
);
|
||||
}
|
||||
|
||||
private Mono<Boolean> isCleanupEnabled() {
|
||||
return client.fetch(ConfigMap.class, CONFIG_MAP_NAME)
|
||||
.mapNotNull(cm -> {
|
||||
var data = cm.getData();
|
||||
if (data == null) return false;
|
||||
@@ -55,51 +75,35 @@ public class AiReplyCleanupService implements DisposableBean {
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.defaultIfEmpty(true)
|
||||
.block();
|
||||
|
||||
if (!Boolean.TRUE.equals(enabled)) {
|
||||
log.debug("[Cleanup] Auto cleanup is disabled, skipping");
|
||||
return;
|
||||
.defaultIfEmpty(true);
|
||||
}
|
||||
|
||||
int retentionDays = getRetentionDays();
|
||||
long deleted = executeCleanup(retentionDays);
|
||||
log.info("[Cleanup] Auto cleanup completed, deleted {} records older than {} days", deleted, retentionDays);
|
||||
} catch (Exception e) {
|
||||
log.error("[Cleanup] Error during daily cleanup: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public long executeCleanup(int retentionDays) {
|
||||
public Mono<Long> executeCleanup(int retentionDays) {
|
||||
Instant cutoff = Instant.now().minus(retentionDays, ChronoUnit.DAYS);
|
||||
|
||||
var oldRecords = client.listAll(AiCommentReply.class, ListOptions.builder().build(), Sort.unsorted())
|
||||
return client.listAll(AiCommentReply.class, ListOptions.builder().build(), Sort.unsorted())
|
||||
.filter(r -> {
|
||||
Instant created = r.getMetadata().getCreationTimestamp();
|
||||
return created != null && created.isBefore(cutoff);
|
||||
})
|
||||
.collectList()
|
||||
.block();
|
||||
|
||||
if (oldRecords == null || oldRecords.isEmpty()) {
|
||||
return 0;
|
||||
.flatMap(oldRecords -> {
|
||||
if (oldRecords.isEmpty()) {
|
||||
return Mono.just(0L);
|
||||
}
|
||||
|
||||
long deleted = 0;
|
||||
for (var record : oldRecords) {
|
||||
try {
|
||||
client.delete(record).block();
|
||||
deleted++;
|
||||
} catch (Exception e) {
|
||||
return Flux.fromIterable(oldRecords)
|
||||
.flatMap(record -> client.delete(record)
|
||||
.thenReturn(1L)
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Cleanup] Failed to delete record {}: {}", record.getMetadata().getName(), e.getMessage());
|
||||
}
|
||||
}
|
||||
return deleted;
|
||||
return Mono.just(0L);
|
||||
})
|
||||
)
|
||||
.reduce(0L, Long::sum);
|
||||
});
|
||||
}
|
||||
|
||||
public int getRetentionDays() {
|
||||
try {
|
||||
public Mono<Integer> getRetentionDays() {
|
||||
return client.fetch(ConfigMap.class, CONFIG_MAP_NAME)
|
||||
.mapNotNull(cm -> {
|
||||
var data = cm.getData();
|
||||
@@ -114,11 +118,10 @@ public class AiReplyCleanupService implements DisposableBean {
|
||||
}
|
||||
})
|
||||
.defaultIfEmpty(30)
|
||||
.block();
|
||||
} catch (Exception e) {
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Cleanup] Failed to read retentionDays config: {}", e.getMessage());
|
||||
return 30;
|
||||
}
|
||||
return Mono.just(30);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -31,6 +31,7 @@ public class AiReplyOrchestrator {
|
||||
private final CommentReplyPublisher commentReplyPublisher;
|
||||
private final FilterService filterService;
|
||||
private final RateLimitService rateLimitService;
|
||||
private final CommentPreFilterService preFilterService;
|
||||
private final ReactiveExtensionClient client;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@@ -49,7 +50,9 @@ public class AiReplyOrchestrator {
|
||||
CommentReplyPublisher commentReplyPublisher,
|
||||
FilterService filterService,
|
||||
RateLimitService rateLimitService,
|
||||
ReactiveExtensionClient client) {
|
||||
CommentPreFilterService preFilterService,
|
||||
ReactiveExtensionClient client,
|
||||
ObjectMapper objectMapper) {
|
||||
this.contextExtractor = contextExtractor;
|
||||
this.promptBuilder = promptBuilder;
|
||||
this.aiReplyService = aiReplyService;
|
||||
@@ -58,8 +61,9 @@ public class AiReplyOrchestrator {
|
||||
this.commentReplyPublisher = commentReplyPublisher;
|
||||
this.filterService = filterService;
|
||||
this.rateLimitService = rateLimitService;
|
||||
this.preFilterService = preFilterService;
|
||||
this.client = client;
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,9 +73,10 @@ public class AiReplyOrchestrator {
|
||||
* @param replyName the Reply name that triggered this (null for top-level comments)
|
||||
* @param isAiConversation true when someone replied to AI's reply (conversation continuation)
|
||||
* @param personaName the persona name to use (null for default persona)
|
||||
* @param wakeWordTriggered true when triggered by a wake word (bypasses page-level enable check)
|
||||
*/
|
||||
public Mono<Void> processComment(String commentName, String replyName, boolean isAiConversation,
|
||||
String personaName) {
|
||||
String personaName, boolean wakeWordTriggered) {
|
||||
String lockKey = isAiConversation ? commentName + ":conv:" + replyName : commentName + ":top";
|
||||
|
||||
// Clean up stale locks before acquiring new one
|
||||
@@ -83,12 +88,12 @@ public class AiReplyOrchestrator {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
log.info("[Orchestrator] Start processing: comment={}, replyName={}, isAiConversation={}, personaName={}",
|
||||
commentName, replyName, isAiConversation, personaName);
|
||||
log.info("[Orchestrator] Start processing: comment={}, replyName={}, isAiConversation={}, personaName={}, wakeWordTriggered={}",
|
||||
commentName, replyName, isAiConversation, personaName, wakeWordTriggered);
|
||||
|
||||
return isAutoReplyEnabled()
|
||||
.flatMap(enabled -> {
|
||||
if (!enabled) {
|
||||
if (!enabled && !wakeWordTriggered) {
|
||||
log.info("[Orchestrator] Auto reply disabled, skipping: {}", commentName);
|
||||
return Mono.empty();
|
||||
}
|
||||
@@ -98,12 +103,81 @@ public class AiReplyOrchestrator {
|
||||
log.info("[Orchestrator] 速率限制,跳过: {}", commentName);
|
||||
return Mono.empty();
|
||||
}
|
||||
// Wake word triggered: skip page-level annotation check
|
||||
if (wakeWordTriggered) {
|
||||
return filterService.isCommenterBlocked(commentName)
|
||||
.flatMap(blocked -> {
|
||||
if (blocked) {
|
||||
log.info("[Orchestrator] Commenter blocked, skipping wake word: {}", commentName);
|
||||
return Mono.empty();
|
||||
}
|
||||
return proceedWithProcess(commentName, replyName, isAiConversation, personaName);
|
||||
});
|
||||
}
|
||||
return filterService.shouldProcess(commentName)
|
||||
.flatMap(shouldProcess -> {
|
||||
if (!shouldProcess) {
|
||||
log.info("[Orchestrator] Filtered out by rules: {}", commentName);
|
||||
return Mono.empty();
|
||||
}
|
||||
return proceedWithProcess(commentName, replyName, isAiConversation, personaName);
|
||||
});
|
||||
});
|
||||
})
|
||||
.doOnError(e -> log.error("[Orchestrator] Error processing comment {}: {}", commentName, e.getMessage(), e))
|
||||
.doFinally(signal -> {
|
||||
// Always release the lock when processing completes
|
||||
processingLocks.remove(lockKey);
|
||||
log.debug("[Orchestrator] Released processing lock for: {}", lockKey);
|
||||
})
|
||||
.then();
|
||||
}
|
||||
|
||||
/**
|
||||
* 误报反馈专用:跳过前置过滤和去重检查,直接为已确认误报的评论生成 AI 回复。
|
||||
*
|
||||
* <p>与 {@link #processComment} 不同,此方法:
|
||||
* <ul>
|
||||
* <li>跳过前置过滤(用户已确认评论合规)</li>
|
||||
* <li>跳过去重检查(已有 FILTERED 记录,需复用)</li>
|
||||
* <li>跳过速率限制和黑名单检查(管理员主动操作)</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param commentName the parent Comment name
|
||||
* @param replyName the Reply name (null for top-level comments)
|
||||
* @param isAiConversation true when this is a conversation continuation
|
||||
* @param personaName the persona name to use
|
||||
* @param recordName the existing AiCommentReply record name to update
|
||||
*/
|
||||
public Mono<Void> processFalsePositive(String commentName, String replyName,
|
||||
boolean isAiConversation, String personaName,
|
||||
String recordName) {
|
||||
log.info("[Orchestrator] Processing false-positive: comment={}, record={}", commentName, recordName);
|
||||
|
||||
return getModelName().flatMap(modelName ->
|
||||
contextExtractor.extract(commentName, replyName, isAiConversation)
|
||||
.flatMap(context -> {
|
||||
// Fetch the existing record (was FILTERED, now PENDING)
|
||||
return client.fetch(AiCommentReply.class, recordName)
|
||||
.flatMap(replyRecord ->
|
||||
sentimentService.analyzeSentiment(context.commentContent(), modelName)
|
||||
.flatMap(sentimentResult ->
|
||||
promptBuilder.buildPrompt(context, sentimentResult.sentiment(), personaName)
|
||||
.flatMap(prompt -> generateAndPublish(prompt, context, replyRecord, modelName, personaName))
|
||||
)
|
||||
);
|
||||
})
|
||||
)
|
||||
.doOnError(e -> log.error("[Orchestrator] Error processing false-positive {}: {}", commentName, e.getMessage(), e))
|
||||
.then();
|
||||
}
|
||||
|
||||
/**
|
||||
* Proceed with processing after all checks have passed.
|
||||
* Handles dedup checks and conversation round limits.
|
||||
*/
|
||||
private Mono<Void> proceedWithProcess(String commentName, String replyName,
|
||||
boolean isAiConversation, String personaName) {
|
||||
// For top-level comments: skip if we already have ANY reply record
|
||||
// For AI conversation: skip if we already replied to THIS specific reply
|
||||
if (!isAiConversation) {
|
||||
@@ -134,23 +208,23 @@ public class AiReplyOrchestrator {
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
})
|
||||
.doOnError(e -> log.error("[Orchestrator] Error processing comment {}: {}", commentName, e.getMessage(), e))
|
||||
.doFinally(signal -> {
|
||||
// Always release the lock when processing completes
|
||||
processingLocks.remove(lockKey);
|
||||
log.debug("[Orchestrator] Released processing lock for: {}", lockKey);
|
||||
})
|
||||
.then();
|
||||
}
|
||||
|
||||
private Mono<Void> doProcess(String commentName, String replyName, boolean isAiConversation,
|
||||
String personaName) {
|
||||
return getModelName().flatMap(modelName ->
|
||||
contextExtractor.extract(commentName, replyName, isAiConversation)
|
||||
.flatMap(context -> sentimentService.analyzeSentiment(context.commentContent(), modelName)
|
||||
.flatMap(context -> preFilterService.check(context.commentContent(), modelName)
|
||||
.flatMap(preFilterResult -> {
|
||||
if (!preFilterResult.passed()) {
|
||||
log.warn("[Orchestrator] Comment pre-filtered: {}, reason: {}",
|
||||
commentName, preFilterResult.reason());
|
||||
// 创建拦截记录并执行处罚(针对实际违规的 Comment 或 Reply)
|
||||
return createFilteredRecord(context, preFilterResult)
|
||||
.then(preFilterService.penalize(commentName, replyName))
|
||||
.then();
|
||||
}
|
||||
return sentimentService.analyzeSentiment(context.commentContent(), modelName)
|
||||
.flatMap(sentimentResult -> {
|
||||
log.info("[Orchestrator] Sentiment for {}: {} (confidence: {})",
|
||||
commentName, sentimentResult.sentiment(), sentimentResult.confidence());
|
||||
@@ -158,6 +232,7 @@ public class AiReplyOrchestrator {
|
||||
.flatMap(prompt -> createAiCommentReply(context, sentimentResult.sentiment(), personaName)
|
||||
.flatMap(replyRecord -> generateAndPublish(prompt, context, replyRecord, modelName, personaName))
|
||||
);
|
||||
});
|
||||
})
|
||||
)
|
||||
);
|
||||
@@ -231,12 +306,13 @@ public class AiReplyOrchestrator {
|
||||
}
|
||||
return publishReply(context, aiReply, replyRecord, reviewResult.score(), personaName);
|
||||
})
|
||||
.switchIfEmpty(
|
||||
publishReply(context, aiReply, replyRecord, 100, personaName)
|
||||
)
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Orchestrator] Review error, auto-passing: {}", e.getMessage());
|
||||
return publishReply(context, aiReply, replyRecord, 100, personaName);
|
||||
// review() already handles errors internally (returns PASS),
|
||||
// so this only fires for errors from publishReply/updateRecord.
|
||||
// Do NOT re-call publishReply to avoid double-publish / overwriting published=false.
|
||||
log.error("[Orchestrator] Error during review/publish for {}: {}",
|
||||
context.commentId(), e.getMessage(), e);
|
||||
return Mono.empty();
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -265,7 +341,7 @@ public class AiReplyOrchestrator {
|
||||
// Update retryCount and reset status to PENDING
|
||||
return updateRecordForRetry(replyRecord, newRetryCount)
|
||||
.delayElement(Duration.ofSeconds(delaySeconds))
|
||||
.then(retryGenerate(context, replyRecord, modelName, personaName));
|
||||
.flatMap(updated -> retryGenerate(context, updated, modelName, personaName));
|
||||
} else {
|
||||
log.warn("[Orchestrator] Max retry count ({}) exceeded for: {}, marking as FAIL. Reason: {}",
|
||||
maxRetry, context.commentId(), reason);
|
||||
@@ -514,6 +590,34 @@ public class AiReplyOrchestrator {
|
||||
.defaultIfEmpty(10);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建被前置过滤拦截的记录。
|
||||
*/
|
||||
private Mono<AiCommentReply> createFilteredRecord(ContextExtractor.CommentContext context,
|
||||
CommentPreFilterService.PreFilterResult preFilterResult) {
|
||||
AiCommentReply record = new AiCommentReply();
|
||||
record.setMetadata(new Metadata());
|
||||
record.getMetadata().setName("ai-reply-" + UUID.randomUUID().toString().substring(0, 8));
|
||||
record.setSpec(new AiCommentReply.Spec());
|
||||
record.getSpec().setCommentId(context.commentId());
|
||||
record.getSpec().setPostId(context.postId());
|
||||
record.getSpec().setPostSlug(context.postSlug());
|
||||
record.getSpec().setPostKind(context.postKind());
|
||||
record.getSpec().setReply("");
|
||||
record.getSpec().setScore(0);
|
||||
record.getSpec().setStatus("FILTERED");
|
||||
record.getSpec().setRetryCount(0);
|
||||
record.getSpec().setReplyTo(context.replyTo());
|
||||
record.getSpec().setIsAiConversation(context.isAiConversation());
|
||||
record.getSpec().setPublished(false);
|
||||
record.getSpec().setSentiment("NEUTRAL");
|
||||
record.getSpec().setFilterCategory(preFilterResult.category());
|
||||
record.getSpec().setFilterReason(preFilterResult.reason());
|
||||
return client.create(record)
|
||||
.doOnSuccess(created -> log.info("[Orchestrator] Created filtered record: {} category={} reason={}",
|
||||
created.getMetadata().getName(), preFilterResult.category(), preFilterResult.reason()));
|
||||
}
|
||||
|
||||
private Mono<AiCommentReply> createAiCommentReply(ContextExtractor.CommentContext context, String sentiment,
|
||||
String personaName) {
|
||||
AiCommentReply record = new AiCommentReply();
|
||||
@@ -523,6 +627,7 @@ public class AiReplyOrchestrator {
|
||||
record.getSpec().setCommentId(context.commentId());
|
||||
record.getSpec().setPostId(context.postId());
|
||||
record.getSpec().setPostSlug(context.postSlug());
|
||||
record.getSpec().setPostKind(context.postKind());
|
||||
record.getSpec().setReply("");
|
||||
record.getSpec().setScore(0);
|
||||
record.getSpec().setStatus("PENDING");
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
package top.nxxy335.commentaiautopilot.service;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.safety.Safelist;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.util.retry.Retry;
|
||||
import run.halo.app.core.extension.content.Comment;
|
||||
import run.halo.app.core.extension.content.Reply;
|
||||
import run.halo.app.extension.ConfigMap;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 评论前置过滤服务:在 AI 回复之前检测评论合规性。
|
||||
*
|
||||
* 检测维度:
|
||||
* 1. 敏感词/辱骂/广告/恶意攻击 — 通过 AI 分类判断
|
||||
* 2. 自动处置 — 违规评论跳过 AI 回复,可选将评论设为待审核状态
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CommentPreFilterService {
|
||||
|
||||
private final ReactiveExtensionClient client;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final AiFoundationClient aiFoundationClient;
|
||||
|
||||
private static final String CONFIG_MAP_NAME = "comment-ai-autopilot-configmap";
|
||||
|
||||
private static final String CLEAN = "正常";
|
||||
private static final String SPAM = "广告";
|
||||
private static final String ABUSE = "辱骂攻击";
|
||||
private static final String SENSITIVE = "敏感内容";
|
||||
private static final String MEANINGLESS = "无意义";
|
||||
private static final List<String> CLASSIFY_CHOICES = List.of(CLEAN, SPAM, ABUSE, SENSITIVE, MEANINGLESS);
|
||||
|
||||
private static final Map<String, String> CATEGORY_DESCRIPTIONS = Map.of(
|
||||
SPAM, "检测到推广链接、产品推销或引流信息",
|
||||
ABUSE, "检测到辱骂、人身攻击、恶意挑衅或歧视性言论",
|
||||
SENSITIVE, "检测到政治敏感、违法违规或色情暴力内容",
|
||||
MEANINGLESS, "检测到纯乱码或无意义字符堆砌"
|
||||
);
|
||||
|
||||
private static final String CLASSIFY_SYSTEM_PROMPT = """
|
||||
你是评论内容合规检测员。请判断以下评论属于哪个类别:
|
||||
|
||||
类别定义:
|
||||
- 正常:正常的评论、提问、讨论、赞美、闲聊等,即使与文章主题无关也算正常
|
||||
- 广告:包含推广链接、产品推销、引流信息等
|
||||
- 辱骂攻击:包含辱骂、人身攻击、恶意挑衅、歧视性言论等
|
||||
- 敏感内容:涉及政治敏感、违法违规、色情暴力等
|
||||
- 无意义:纯乱码、无意义字符堆砌(如随机符号、键盘乱敲)
|
||||
|
||||
═══════════════════════════════════════
|
||||
核心判断原则(必须严格遵守):
|
||||
═══════════════════════════════════════
|
||||
|
||||
【原则一:上下文优先】
|
||||
绝对禁止仅凭单个词汇进行机械拦截。必须结合整句话的语境、语气和前后文逻辑进行综合判断。一个词是否违规,取决于它在句子中的功能,而非词汇本身。
|
||||
|
||||
【原则二:口语化宽容】
|
||||
中文互联网存在大量口语化简写、谐音和省略表达。如果某个词在特定语境下明显是中性词或亲属称谓的口语化表达,且整句无攻击性、无恶意,必须判定为"正常"。
|
||||
常见口语化中性用法示例:
|
||||
- "他妈" → 可能是"他妈妈"的简称,如"小轩是他妈的朋友"=小轩是他妈妈的朋友 → 正常
|
||||
- "你妹" → 可能是"你妹妹"的简称,如"你妹在哪上学"=你妹妹在哪上学 → 正常
|
||||
- "卧槽" → 可能是语气词表示惊讶,如"卧槽这也太强了"=哇塞这也太厉害了 → 正常
|
||||
- "牛逼" → 口语化赞美,如"这文章写得牛逼" → 正常
|
||||
- "靠" → 语气词表示无奈或惊讶,如"靠又忘了" → 正常
|
||||
|
||||
【原则三:恶意导向判定】
|
||||
只有当词汇被明确用作辱骂、人身攻击、引战或带有较强负面情绪时,才判定为"辱骂攻击"。
|
||||
恶意用法示例(这些才应判为"辱骂攻击"):
|
||||
- "你他妈的" → 直接对他人进行辱骂 → 辱骂攻击
|
||||
- "你妹的" → 带有攻击性的语气词 → 辱骂攻击
|
||||
- "傻逼" → 直接辱骂他人 → 辱骂攻击
|
||||
|
||||
【原则四:宁放勿杀】
|
||||
当你无法确定评论是否违规时,应判定为"正常"而非"辱骂攻击"。误杀正常评论比漏判违规评论的负面影响更大。
|
||||
|
||||
【原则五:闲聊不算无意义】
|
||||
与文章主题无关的闲聊、灌水、打招呼等属于"正常",不要误判为"无意义"。
|
||||
|
||||
只返回类别名称,不要返回其他内容。""";
|
||||
|
||||
public CommentPreFilterService(ReactiveExtensionClient client,
|
||||
ObjectMapper objectMapper,
|
||||
AiFoundationClient aiFoundationClient) {
|
||||
this.client = client;
|
||||
this.objectMapper = objectMapper;
|
||||
this.aiFoundationClient = aiFoundationClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测评论是否合规。
|
||||
*
|
||||
* @param commentContent 评论内容(纯文本)
|
||||
* @param modelName AI 模型名称
|
||||
* @return 检测结果
|
||||
*/
|
||||
public Mono<PreFilterResult> check(String commentContent, String modelName) {
|
||||
return loadConfig().flatMap(config -> {
|
||||
if (!config.enabled()) {
|
||||
log.info("[PreFilter] Pre-filter is DISABLED, allowing all comments");
|
||||
return Mono.just(new PreFilterResult(true, CLEAN, "前置过滤未启用"));
|
||||
}
|
||||
|
||||
// 剥离 HTML 标签,获取纯文本
|
||||
String plainText = stripHtml(commentContent);
|
||||
String truncated = truncate(plainText, 500);
|
||||
String userPrompt = "评论内容:\n" + truncated;
|
||||
log.info("[PreFilter] Checking comment (enabled=true): {}", truncated.substring(0, Math.min(50, truncated.length())));
|
||||
|
||||
return aiFoundationClient.classify(CLASSIFY_SYSTEM_PROMPT, userPrompt, CLASSIFY_CHOICES, modelName)
|
||||
.doOnNext(result -> log.info("[PreFilter] AI classify returned: '{}'", result))
|
||||
.map(result -> {
|
||||
if (CLEAN.equals(result)) {
|
||||
log.info("[PreFilter] Comment passed: category={}", result);
|
||||
return new PreFilterResult(true, CLEAN, "评论合规");
|
||||
}
|
||||
// 空结果视为分类失败
|
||||
if (result == null || result.isBlank()) {
|
||||
log.warn("[PreFilter] AI classify returned empty/blank result, blocking for safety");
|
||||
return new PreFilterResult(false, MEANINGLESS, "AI分类返回空结果,安全拦截");
|
||||
}
|
||||
String desc = CATEGORY_DESCRIPTIONS.getOrDefault(result, "检测到违规内容");
|
||||
String snippet = truncated.substring(0, Math.min(50, truncated.length()));
|
||||
String reason = desc + " — 「" + snippet + "」";
|
||||
log.warn("[PreFilter] Comment BLOCKED: category={}, content={}", result, snippet);
|
||||
return new PreFilterResult(false, result, reason);
|
||||
})
|
||||
// 分类失败时拦截评论(安全优先),而非放行
|
||||
.defaultIfEmpty(new PreFilterResult(false, MEANINGLESS, "AI分类服务不可用,安全拦截"))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[PreFilter] Detection error, BLOCKING comment for safety: {}", e.getMessage(), e);
|
||||
return Mono.just(new PreFilterResult(false, MEANINGLESS, "AI分类服务异常,安全拦截"));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 对违规评论执行自动处置:将评论或回复设为待审核状态。
|
||||
*
|
||||
* <p>当 replyName 不为空时(AI 对话场景或回复触发),取消通过的是包含违规内容的 Reply;
|
||||
* 否则取消通过的是顶层 Comment。这样可避免误伤父级 Comment 中正常的内容。
|
||||
*
|
||||
* @param commentName 评论的 metadata.name
|
||||
* @param replyName 回复的 metadata.name(可为 null,表示顶层评论)
|
||||
* @return Mono<Void>
|
||||
*/
|
||||
public Mono<Void> penalize(String commentName, String replyName) {
|
||||
return loadConfig().flatMap(config -> {
|
||||
if (!config.pendingOnViolation()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
// 优先处理 Reply:AI 对话场景下违规内容来自 Reply
|
||||
if (replyName != null && !replyName.isBlank()) {
|
||||
return penalizeReply(replyName);
|
||||
}
|
||||
return penalizeComment(commentName);
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Void> penalizeComment(String commentName) {
|
||||
return client.fetch(Comment.class, commentName)
|
||||
.flatMap(comment -> {
|
||||
var spec = comment.getSpec();
|
||||
if (spec == null) return Mono.<Comment>empty();
|
||||
// 只要 approved 不是 false,就强制设为 false
|
||||
// 覆盖 approved=true 和 approved=null 两种情况
|
||||
if (!Boolean.FALSE.equals(spec.getApproved())) {
|
||||
log.info("[PreFilter] Penalizing comment {}: approved={} → false", commentName, spec.getApproved());
|
||||
spec.setApproved(false);
|
||||
spec.setApprovedTime(null);
|
||||
return client.update(comment)
|
||||
.doOnSuccess(c -> log.info("[PreFilter] Comment {} set to pending for violation", commentName));
|
||||
}
|
||||
log.debug("[PreFilter] Comment {} already unapproved, skip penalize", commentName);
|
||||
return Mono.<Comment>empty();
|
||||
})
|
||||
.retryWhen(Retry.backoff(3, Duration.ofMillis(100))
|
||||
.filter(OptimisticLockingFailureException.class::isInstance)
|
||||
.doBeforeRetry(sig -> log.debug("[PreFilter] Retrying penalizeComment {} (attempt {})", commentName, sig.totalRetries() + 1)))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[PreFilter] Failed to penalize comment {} after retries: {}", commentName, e.getMessage());
|
||||
return Mono.empty();
|
||||
})
|
||||
.then();
|
||||
}
|
||||
|
||||
private Mono<Void> penalizeReply(String replyName) {
|
||||
return client.fetch(Reply.class, replyName)
|
||||
.flatMap(reply -> {
|
||||
var spec = reply.getSpec();
|
||||
if (spec == null) return Mono.<Reply>empty();
|
||||
// 只要 approved 不是 false,就强制设为 false
|
||||
if (!Boolean.FALSE.equals(spec.getApproved())) {
|
||||
log.info("[PreFilter] Penalizing reply {}: approved={} → false", replyName, spec.getApproved());
|
||||
spec.setApproved(false);
|
||||
spec.setApprovedTime(null);
|
||||
return client.update(reply)
|
||||
.doOnSuccess(r -> log.info("[PreFilter] Reply {} set to pending for violation", replyName));
|
||||
}
|
||||
log.debug("[PreFilter] Reply {} already unapproved, skip penalize", replyName);
|
||||
return Mono.<Reply>empty();
|
||||
})
|
||||
.retryWhen(Retry.backoff(3, Duration.ofMillis(100))
|
||||
.filter(OptimisticLockingFailureException.class::isInstance)
|
||||
.doBeforeRetry(sig -> log.debug("[PreFilter] Retrying penalizeReply {} (attempt {})", replyName, sig.totalRetries() + 1)))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[PreFilter] Failed to penalize reply {} after retries: {}", replyName, e.getMessage());
|
||||
return Mono.empty();
|
||||
})
|
||||
.then();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载前置过滤配置。
|
||||
*/
|
||||
private Mono<PreFilterConfig> loadConfig() {
|
||||
return client.fetch(ConfigMap.class, CONFIG_MAP_NAME)
|
||||
.mapNotNull(cm -> {
|
||||
var data = cm.getData();
|
||||
if (data == null) return new PreFilterConfig(true, true);
|
||||
String basicJson = data.get("basic");
|
||||
if (basicJson == null || basicJson.isBlank()) return new PreFilterConfig(true, true);
|
||||
try {
|
||||
JsonNode node = objectMapper.readTree(basicJson);
|
||||
boolean enabled = !node.has("preFilterEnabled")
|
||||
|| node.get("preFilterEnabled").asBoolean(true);
|
||||
boolean pendingOnViolation = !node.has("preFilterPendingOnViolation")
|
||||
|| node.get("preFilterPendingOnViolation").asBoolean(true);
|
||||
return new PreFilterConfig(enabled, pendingOnViolation);
|
||||
} catch (Exception e) {
|
||||
log.warn("[PreFilter] Failed to parse config: {}", e.getMessage());
|
||||
return new PreFilterConfig(true, true);
|
||||
}
|
||||
})
|
||||
.defaultIfEmpty(new PreFilterConfig(true, true))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[PreFilter] Failed to load config: {}", e.getMessage());
|
||||
return Mono.just(new PreFilterConfig(true, true));
|
||||
});
|
||||
}
|
||||
|
||||
private String truncate(String text, int maxLength) {
|
||||
if (text == null) return "";
|
||||
return text.length() > maxLength ? text.substring(0, maxLength) : text;
|
||||
}
|
||||
|
||||
private String stripHtml(String html) {
|
||||
if (html == null || html.isBlank()) return "";
|
||||
return Jsoup.clean(html, Safelist.none()).trim();
|
||||
}
|
||||
|
||||
public record PreFilterResult(boolean passed, String category, String reason) {}
|
||||
|
||||
public record PreFilterConfig(boolean enabled, boolean pendingOnViolation) {}
|
||||
}
|
||||
@@ -8,9 +8,8 @@ import run.halo.app.core.extension.content.Reply;
|
||||
import run.halo.app.extension.Metadata;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import top.nxxy335.commentaiautopilot.extension.AiPersona;
|
||||
import top.nxxy335.commentaiautopilot.util.GravatarUtil;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -81,7 +80,10 @@ public class CommentReplyPublisher {
|
||||
private Mono<Reply> doPublish(String parentCommentName, String replyContent,
|
||||
String postName, String quoteReplyName, boolean autoPublish,
|
||||
String personaName) {
|
||||
return resolvePersona(personaName).flatMap(persona -> {
|
||||
|
||||
// 解析 AI 角色并直接发布纯净的回复内容
|
||||
return resolvePersona(personaName)
|
||||
.flatMap(persona -> {
|
||||
String displayName = persona.displayName();
|
||||
String email = persona.email();
|
||||
|
||||
@@ -92,8 +94,11 @@ public class CommentReplyPublisher {
|
||||
|
||||
var spec = reply.getSpec();
|
||||
spec.setCommentName(parentCommentName);
|
||||
|
||||
// 直接存入纯净的 AI 回复内容,不加任何 Markdown 前缀
|
||||
spec.setRaw(replyContent);
|
||||
spec.setContent(replyContent);
|
||||
|
||||
spec.setApproved(autoPublish);
|
||||
if (autoPublish) {
|
||||
spec.setApprovedTime(Instant.now());
|
||||
@@ -103,6 +108,7 @@ public class CommentReplyPublisher {
|
||||
spec.setAllowNotification(false);
|
||||
spec.setHidden(false);
|
||||
|
||||
// Halo 原生评论组件正是靠这个字段来渲染 "回复 @某人" 的
|
||||
if (quoteReplyName != null && !quoteReplyName.isBlank()) {
|
||||
spec.setQuoteReply(quoteReplyName);
|
||||
}
|
||||
@@ -118,24 +124,17 @@ public class CommentReplyPublisher {
|
||||
|
||||
Map<String, String> ownerAnnotations = new HashMap<>();
|
||||
ownerAnnotations.put("comment-ai-autopilot.nxxy335.top/is-ai", "true");
|
||||
// 使用Gravatar邮箱头像
|
||||
if (email != null && !email.isBlank()) {
|
||||
String gravatarUrl = generateGravatarUrl(email);
|
||||
String gravatarUrl = GravatarUtil.generateUrl(email);
|
||||
ownerAnnotations.put(Comment.CommentOwner.AVATAR_ANNO, gravatarUrl);
|
||||
}
|
||||
owner.setAnnotations(ownerAnnotations);
|
||||
spec.setOwner(owner);
|
||||
|
||||
log.info("[Publisher] Creating reply for comment: {}, owner: kind={}, name={}, displayName={}, annotations={}",
|
||||
parentCommentName, owner.getKind(), owner.getName(), owner.getDisplayName(), ownerAnnotations);
|
||||
log.info("[Publisher] Creating reply for comment: {}, content length: {}", parentCommentName, replyContent.length());
|
||||
|
||||
return client.create(reply)
|
||||
.doOnSuccess(created -> {
|
||||
var createdOwner = created.getSpec().getOwner();
|
||||
log.info("[Publisher] AI Persona '{}' reply published for comment: {}, quoteReply: {}, owner annotations after create: {}",
|
||||
displayName, parentCommentName, quoteReplyName,
|
||||
createdOwner != null ? createdOwner.getAnnotations() : "null");
|
||||
})
|
||||
.doOnSuccess(created -> log.info("[Publisher] AI Persona '{}' reply published for comment: {}", displayName, parentCommentName))
|
||||
.doOnError(e -> log.error("[Publisher] Failed to publish AI reply: {}", e.getMessage()));
|
||||
});
|
||||
}
|
||||
@@ -181,22 +180,4 @@ public class CommentReplyPublisher {
|
||||
private String generateReplyName() {
|
||||
return "ai-comment-reply-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Gravatar URL from email address using SHA-256 hash.
|
||||
*/
|
||||
private String generateGravatarUrl(String email) {
|
||||
try {
|
||||
var digest = MessageDigest.getInstance("SHA-256");
|
||||
var hashBytes = digest.digest(email.trim().toLowerCase().getBytes(StandardCharsets.UTF_8));
|
||||
var hexString = new StringBuilder();
|
||||
for (byte b : hashBytes) {
|
||||
hexString.append(String.format("%02x", b));
|
||||
}
|
||||
return "https://cn.cravatar.com/avatar/" + hexString;
|
||||
} catch (Exception e) {
|
||||
log.error("[Publisher] Failed to generate Gravatar URL: {}", e.getMessage());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import run.halo.app.content.ContentWrapper;
|
||||
import run.halo.app.content.PostContentService;
|
||||
import run.halo.app.core.extension.content.Comment;
|
||||
import run.halo.app.core.extension.content.Post;
|
||||
import run.halo.app.core.extension.content.SinglePage;
|
||||
import run.halo.app.core.extension.content.Reply;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
|
||||
@@ -42,6 +43,55 @@ public class ContextExtractor {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch previous replies in the comment thread to provide conversation history.
|
||||
* Only includes replies created before the triggering reply.
|
||||
*/
|
||||
private Mono<String> fetchConversationHistory(String commentName, String triggerReplyName) {
|
||||
if (triggerReplyName == null || triggerReplyName.isBlank()) {
|
||||
return Mono.just("");
|
||||
}
|
||||
return client.fetch(Reply.class, triggerReplyName)
|
||||
.flatMap(triggerReply -> {
|
||||
var triggerTime = triggerReply.getMetadata().getCreationTimestamp();
|
||||
return client.list(Reply.class,
|
||||
reply -> {
|
||||
if (!commentName.equals(reply.getSpec().getCommentName())) {
|
||||
return false;
|
||||
}
|
||||
if (triggerReplyName.equals(reply.getMetadata().getName())) {
|
||||
return false;
|
||||
}
|
||||
// Only include replies created before the trigger reply
|
||||
var replyTime = reply.getMetadata().getCreationTimestamp();
|
||||
return replyTime != null && triggerTime != null
|
||||
&& !replyTime.isAfter(triggerTime);
|
||||
},
|
||||
null)
|
||||
.collectList()
|
||||
.map(replies -> {
|
||||
if (replies.isEmpty()) return "";
|
||||
// Sort by creation time
|
||||
replies.sort(java.util.Comparator.comparing(
|
||||
r -> r.getMetadata().getCreationTimestamp()));
|
||||
var sb = new StringBuilder();
|
||||
for (var r : replies) {
|
||||
var owner = r.getSpec().getOwner();
|
||||
String name = (owner != null && owner.getDisplayName() != null)
|
||||
? owner.getDisplayName() : "匿名用户";
|
||||
boolean isAi = owner != null && owner.getAnnotations() != null
|
||||
&& "true".equals(owner.getAnnotations().get("comment-ai-autopilot.nxxy335.top/is-ai"));
|
||||
String role = isAi ? "AI" : "用户";
|
||||
String content = extractReplyContent(r);
|
||||
sb.append(role).append("(").append(name).append("): ")
|
||||
.append(content).append("\n");
|
||||
}
|
||||
return sb.toString();
|
||||
});
|
||||
})
|
||||
.defaultIfEmpty("");
|
||||
}
|
||||
|
||||
private Mono<CommentContext> buildContext(Comment comment, boolean isAiConversation) {
|
||||
var commentContent = extractCommentContent(comment);
|
||||
var commentOwner = extractCommentOwner(comment);
|
||||
@@ -63,10 +113,16 @@ public class ContextExtractor {
|
||||
null,
|
||||
isAiConversation,
|
||||
formatPostDate(post),
|
||||
commentCount
|
||||
commentCount,
|
||||
"",
|
||||
"Post"
|
||||
))
|
||||
)
|
||||
)
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[ContextExtractor] Failed to fetch Post {}: {}", postName, e.getMessage());
|
||||
return Mono.empty();
|
||||
})
|
||||
.defaultIfEmpty(new CommentContext(
|
||||
comment.getMetadata().getName(),
|
||||
postName,
|
||||
@@ -78,7 +134,52 @@ public class ContextExtractor {
|
||||
null,
|
||||
isAiConversation,
|
||||
"",
|
||||
0
|
||||
0,
|
||||
"",
|
||||
"Post"
|
||||
));
|
||||
}
|
||||
|
||||
if (subjectRef != null && "SinglePage".equals(subjectRef.getKind())) {
|
||||
String postName = subjectRef.getName();
|
||||
return client.fetch(SinglePage.class, postName)
|
||||
.flatMap(singlePage -> getSinglePageContent(postName)
|
||||
.flatMap(content -> getCommentCount(comment.getMetadata().getName())
|
||||
.map(commentCount -> new CommentContext(
|
||||
comment.getMetadata().getName(),
|
||||
postName,
|
||||
singlePage.getSpec().getSlug(),
|
||||
commentContent,
|
||||
commentOwner,
|
||||
singlePage.getSpec().getTitle(),
|
||||
content,
|
||||
null,
|
||||
isAiConversation,
|
||||
formatSinglePageDate(singlePage),
|
||||
commentCount,
|
||||
"",
|
||||
"SinglePage"
|
||||
))
|
||||
)
|
||||
)
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[ContextExtractor] Failed to fetch SinglePage {}: {}", postName, e.getMessage());
|
||||
return Mono.empty();
|
||||
})
|
||||
.defaultIfEmpty(new CommentContext(
|
||||
comment.getMetadata().getName(),
|
||||
postName,
|
||||
"",
|
||||
commentContent,
|
||||
commentOwner,
|
||||
"",
|
||||
"",
|
||||
null,
|
||||
isAiConversation,
|
||||
"",
|
||||
0,
|
||||
"",
|
||||
"SinglePage"
|
||||
));
|
||||
}
|
||||
|
||||
@@ -93,7 +194,9 @@ public class ContextExtractor {
|
||||
null,
|
||||
isAiConversation,
|
||||
"",
|
||||
0
|
||||
0,
|
||||
"",
|
||||
""
|
||||
));
|
||||
}
|
||||
|
||||
@@ -101,54 +204,119 @@ public class ContextExtractor {
|
||||
var replyContent = extractReplyContent(reply);
|
||||
var replyOwner = extractReplyOwner(reply);
|
||||
var subjectRef = comment.getSpec().getSubjectRef();
|
||||
var commentName = comment.getMetadata().getName();
|
||||
var replyName = reply.getMetadata().getName();
|
||||
|
||||
// Fetch conversation history for AI conversations
|
||||
Mono<String> historyMono = isAiConversation
|
||||
? fetchConversationHistory(commentName, replyName)
|
||||
: Mono.just("");
|
||||
|
||||
if (subjectRef != null && "Post".equals(subjectRef.getKind())) {
|
||||
String postName = subjectRef.getName();
|
||||
return client.fetch(Post.class, postName)
|
||||
.flatMap(post -> getPostContent(postName)
|
||||
.flatMap(content -> getCommentCount(comment.getMetadata().getName())
|
||||
.map(commentCount -> new CommentContext(
|
||||
comment.getMetadata().getName(),
|
||||
.flatMap(content -> getCommentCount(commentName)
|
||||
.flatMap(commentCount -> historyMono
|
||||
.map(history -> new CommentContext(
|
||||
commentName,
|
||||
postName,
|
||||
post.getSpec().getSlug(),
|
||||
replyContent,
|
||||
replyOwner,
|
||||
post.getSpec().getTitle(),
|
||||
content,
|
||||
reply.getMetadata().getName(),
|
||||
replyName,
|
||||
isAiConversation,
|
||||
formatPostDate(post),
|
||||
commentCount
|
||||
commentCount,
|
||||
history,
|
||||
"Post"
|
||||
))
|
||||
)
|
||||
)
|
||||
)
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[ContextExtractor] Failed to fetch Post {} for reply: {}", postName, e.getMessage());
|
||||
return Mono.empty();
|
||||
})
|
||||
.defaultIfEmpty(new CommentContext(
|
||||
comment.getMetadata().getName(),
|
||||
commentName,
|
||||
postName,
|
||||
"",
|
||||
replyContent,
|
||||
replyOwner,
|
||||
"",
|
||||
"",
|
||||
reply.getMetadata().getName(),
|
||||
replyName,
|
||||
isAiConversation,
|
||||
"",
|
||||
0
|
||||
0,
|
||||
"",
|
||||
"Post"
|
||||
));
|
||||
}
|
||||
|
||||
return Mono.just(new CommentContext(
|
||||
comment.getMetadata().getName(),
|
||||
if (subjectRef != null && "SinglePage".equals(subjectRef.getKind())) {
|
||||
String postName = subjectRef.getName();
|
||||
return client.fetch(SinglePage.class, postName)
|
||||
.flatMap(singlePage -> getSinglePageContent(postName)
|
||||
.flatMap(content -> getCommentCount(commentName)
|
||||
.flatMap(commentCount -> historyMono
|
||||
.map(history -> new CommentContext(
|
||||
commentName,
|
||||
postName,
|
||||
singlePage.getSpec().getSlug(),
|
||||
replyContent,
|
||||
replyOwner,
|
||||
singlePage.getSpec().getTitle(),
|
||||
content,
|
||||
replyName,
|
||||
isAiConversation,
|
||||
formatSinglePageDate(singlePage),
|
||||
commentCount,
|
||||
history,
|
||||
"SinglePage"
|
||||
))
|
||||
)
|
||||
)
|
||||
)
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[ContextExtractor] Failed to fetch SinglePage {} for reply: {}", postName, e.getMessage());
|
||||
return Mono.empty();
|
||||
})
|
||||
.defaultIfEmpty(new CommentContext(
|
||||
commentName,
|
||||
postName,
|
||||
"",
|
||||
replyContent,
|
||||
replyOwner,
|
||||
"",
|
||||
"",
|
||||
replyName,
|
||||
isAiConversation,
|
||||
"",
|
||||
0,
|
||||
"",
|
||||
"SinglePage"
|
||||
));
|
||||
}
|
||||
|
||||
return historyMono
|
||||
.map(history -> new CommentContext(
|
||||
commentName,
|
||||
"",
|
||||
"",
|
||||
replyContent,
|
||||
replyOwner,
|
||||
"",
|
||||
"",
|
||||
reply.getMetadata().getName(),
|
||||
replyName,
|
||||
isAiConversation,
|
||||
"",
|
||||
0
|
||||
0,
|
||||
history,
|
||||
""
|
||||
));
|
||||
}
|
||||
|
||||
@@ -213,6 +381,23 @@ public class ContextExtractor {
|
||||
.defaultIfEmpty("");
|
||||
}
|
||||
|
||||
private Mono<String> getSinglePageContent(String pageName) {
|
||||
// SinglePage doesn't have a dedicated ContentService in Halo API,
|
||||
// and Snapshot content requires patch merging which is too complex.
|
||||
// Use the excerpt from status as a fallback for context.
|
||||
return client.fetch(SinglePage.class, pageName)
|
||||
.mapNotNull(page -> {
|
||||
if (page.getStatus() != null && page.getStatus().getExcerpt() != null) {
|
||||
String excerpt = page.getStatus().getExcerpt();
|
||||
if (excerpt != null && !excerpt.isBlank()) {
|
||||
return excerpt;
|
||||
}
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.defaultIfEmpty("");
|
||||
}
|
||||
|
||||
private String formatPostDate(Post post) {
|
||||
var publishTime = post.getSpec().getPublishTime();
|
||||
if (publishTime != null) {
|
||||
@@ -225,6 +410,18 @@ public class ContextExtractor {
|
||||
return "";
|
||||
}
|
||||
|
||||
private String formatSinglePageDate(SinglePage singlePage) {
|
||||
var publishTime = singlePage.getSpec().getPublishTime();
|
||||
if (publishTime != null) {
|
||||
return publishTime.toString().substring(0, 10);
|
||||
}
|
||||
var creationTimestamp = singlePage.getMetadata().getCreationTimestamp();
|
||||
if (creationTimestamp != null) {
|
||||
return creationTimestamp.toString().substring(0, 10);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private Mono<Integer> getCommentCount(String commentName) {
|
||||
return client.list(Reply.class,
|
||||
reply -> commentName.equals(reply.getSpec().getCommentName()),
|
||||
@@ -245,6 +442,8 @@ public class ContextExtractor {
|
||||
String replyTo,
|
||||
boolean isAiConversation,
|
||||
String postDate,
|
||||
int commentCount
|
||||
int commentCount,
|
||||
String conversationHistory,
|
||||
String postKind
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -28,9 +28,9 @@ public class FilterService {
|
||||
private static final String ANNOTATION_KEY = "comment-ai-autopilot.nxxy335.top/ai-reply-enabled";
|
||||
private static final String GROUP_CONTENT = "content.halo.run";
|
||||
|
||||
public FilterService(ReactiveExtensionClient client) {
|
||||
public FilterService(ReactiveExtensionClient client, ObjectMapper objectMapper) {
|
||||
this.client = client;
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public Mono<Boolean> shouldProcess(Comment comment) {
|
||||
@@ -58,6 +58,19 @@ public class FilterService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查评论者是否在黑名单中(按 commentName 查询)。
|
||||
*/
|
||||
public Mono<Boolean> isCommenterBlocked(String commentName) {
|
||||
return client.fetch(Comment.class, commentName)
|
||||
.flatMap(this::checkBlockedCommenters)
|
||||
.defaultIfEmpty(false)
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Filter] Error checking blocked commenter: {}", e.getMessage());
|
||||
return Mono.just(false);
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<Boolean> checkBlockedCommenters(Comment comment) {
|
||||
return client.fetch(ConfigMap.class, CONFIG_MAP_NAME)
|
||||
.mapNotNull(cm -> {
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
package top.nxxy335.commentaiautopilot.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import run.halo.app.core.extension.content.Category;
|
||||
import run.halo.app.core.extension.content.Comment;
|
||||
import run.halo.app.core.extension.content.Post;
|
||||
import run.halo.app.core.extension.content.Tag;
|
||||
import run.halo.app.extension.ExtensionClient;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Shared service for resolving AI persona name from a comment's associated
|
||||
* post/category/tag annotations.
|
||||
*
|
||||
* <p>Priority: Post annotation > Category annotation > Tag annotation
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class PersonaResolver {
|
||||
|
||||
private static final String AI_PERSONA_ANNOTATION = "comment-ai-autopilot.nxxy335.top/ai-persona";
|
||||
|
||||
private final ReactiveExtensionClient reactiveClient;
|
||||
|
||||
/**
|
||||
* Resolve persona name from a comment (reactive version).
|
||||
* Reads the post's annotations, then falls back to category and tag annotations.
|
||||
*
|
||||
* @param commentName the Comment metadata.name
|
||||
* @return the persona name, or empty string if none found
|
||||
*/
|
||||
public Mono<String> getPersonaNameFromComment(String commentName) {
|
||||
return reactiveClient.fetch(Comment.class, commentName)
|
||||
.flatMap(comment -> {
|
||||
var subjectRef = comment.getSpec().getSubjectRef();
|
||||
if (subjectRef == null || !"Post".equals(subjectRef.getKind())) {
|
||||
return Mono.just("");
|
||||
}
|
||||
String postName = subjectRef.getName();
|
||||
return resolveFromPost(postName);
|
||||
})
|
||||
.defaultIfEmpty("");
|
||||
}
|
||||
|
||||
private Mono<String> resolveFromPost(String postName) {
|
||||
return reactiveClient.fetch(Post.class, postName)
|
||||
.flatMap(post -> {
|
||||
// 1. Post annotation takes priority
|
||||
var annotations = post.getMetadata().getAnnotations();
|
||||
if (annotations != null) {
|
||||
String persona = annotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (persona != null && !persona.isBlank()) {
|
||||
return Mono.just(persona);
|
||||
}
|
||||
}
|
||||
// 2. Category annotations (check sequentially, return first match)
|
||||
var spec = post.getSpec();
|
||||
List<String> categories = (spec != null && spec.getCategories() != null)
|
||||
? spec.getCategories() : List.of();
|
||||
// 3. Tag annotations (fallback if no category match)
|
||||
List<String> tags = (spec != null && spec.getTags() != null)
|
||||
? spec.getTags() : List.of();
|
||||
|
||||
return resolveFromCategories(categories)
|
||||
.switchIfEmpty(resolveFromTags(tags));
|
||||
})
|
||||
.defaultIfEmpty("");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sequentially check category annotations, returning the first non-empty persona.
|
||||
* Uses concatMap to preserve order and short-circuit on first match.
|
||||
*/
|
||||
private Mono<String> resolveFromCategories(List<String> categoryNames) {
|
||||
if (categoryNames == null || categoryNames.isEmpty()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
return Flux.fromIterable(categoryNames)
|
||||
.concatMap(this::resolveFromCategory)
|
||||
.next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sequentially check tag annotations, returning the first non-empty persona.
|
||||
* Uses concatMap to preserve order and short-circuit on first match.
|
||||
*/
|
||||
private Mono<String> resolveFromTags(List<String> tagNames) {
|
||||
if (tagNames == null || tagNames.isEmpty()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
return Flux.fromIterable(tagNames)
|
||||
.concatMap(this::resolveFromTag)
|
||||
.next();
|
||||
}
|
||||
|
||||
private Mono<String> resolveFromCategory(String categoryName) {
|
||||
return reactiveClient.fetch(Category.class, categoryName)
|
||||
.mapNotNull(cat -> {
|
||||
var catAnnotations = cat.getMetadata().getAnnotations();
|
||||
if (catAnnotations != null) {
|
||||
String catPersona = catAnnotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (catPersona != null && !catPersona.isBlank()) {
|
||||
return catPersona;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.onErrorResume(e -> {
|
||||
log.warn("Failed to resolve persona from category {}: {}", categoryName, e.getMessage());
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
private Mono<String> resolveFromTag(String tagName) {
|
||||
return reactiveClient.fetch(Tag.class, tagName)
|
||||
.mapNotNull(tag -> {
|
||||
var tagAnnotations = tag.getMetadata().getAnnotations();
|
||||
if (tagAnnotations != null) {
|
||||
String tagPersona = tagAnnotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (tagPersona != null && !tagPersona.isBlank()) {
|
||||
return tagPersona;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.onErrorResume(e -> {
|
||||
log.warn("Failed to resolve persona from tag {}: {}", tagName, e.getMessage());
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve persona name from a comment using blocking ExtensionClient
|
||||
* (for use in Reconciler sync context).
|
||||
*/
|
||||
public String getPersonaNameFromCommentBlocking(ExtensionClient client, Comment comment) {
|
||||
var subjectRef = comment.getSpec().getSubjectRef();
|
||||
if (subjectRef == null || !"Post".equals(subjectRef.getKind())) {
|
||||
return null;
|
||||
}
|
||||
String postName = subjectRef.getName();
|
||||
return client.fetch(Post.class, postName)
|
||||
.map(post -> {
|
||||
// 1. Post annotation
|
||||
var annotations = post.getMetadata().getAnnotations();
|
||||
if (annotations != null) {
|
||||
String persona = annotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (persona != null && !persona.isBlank()) {
|
||||
return persona;
|
||||
}
|
||||
}
|
||||
// 2. Category annotations
|
||||
var spec = post.getSpec();
|
||||
if (spec != null && spec.getCategories() != null) {
|
||||
for (String categoryName : spec.getCategories()) {
|
||||
var cat = client.fetch(Category.class, categoryName).orElse(null);
|
||||
if (cat != null) {
|
||||
var catAnnotations = cat.getMetadata().getAnnotations();
|
||||
if (catAnnotations != null) {
|
||||
String catPersona = catAnnotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (catPersona != null && !catPersona.isBlank()) {
|
||||
return catPersona;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3. Tag annotations
|
||||
if (spec != null && spec.getTags() != null) {
|
||||
for (String tagName : spec.getTags()) {
|
||||
var tag = client.fetch(Tag.class, tagName).orElse(null);
|
||||
if (tag != null) {
|
||||
var tagAnnotations = tag.getMetadata().getAnnotations();
|
||||
if (tagAnnotations != null) {
|
||||
String tagPersona = tagAnnotations.get(AI_PERSONA_ANNOTATION);
|
||||
if (tagPersona != null && !tagPersona.isBlank()) {
|
||||
return tagPersona;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -20,9 +20,9 @@ public class PromptBuilder {
|
||||
private final ObjectMapper objectMapper;
|
||||
private static final String CONFIG_MAP_NAME = "comment-ai-autopilot-configmap";
|
||||
|
||||
public PromptBuilder(ReactiveExtensionClient client) {
|
||||
public PromptBuilder(ReactiveExtensionClient client, ObjectMapper objectMapper) {
|
||||
this.client = client;
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
private static final String PRESET_FRIENDLY = """
|
||||
@@ -52,8 +52,9 @@ public class PromptBuilder {
|
||||
private static final String SAFETY_PROMPT = """
|
||||
【安全规范】
|
||||
- 内容红线:坚决不生成任何涉及暴力、歧视、辱骂、人身攻击或违反法律法规的内容。
|
||||
- 恶意诱导处理:当用户要求你骂人、使用侮辱性词汇或进行情绪化对骂时,你必须礼貌地拒绝,例如回复:"抱歉,作为AI助手,我无法提供此类回复。"
|
||||
- 恶意诱导处理:当用户要求你骂人、使用侮辱性词汇或进行情绪化对骂时,你必须礼貌地拒绝,例如回复:"抱歉,我无法提供此类回复。"
|
||||
- 未知与边界:如果不知道答案或遇到敏感话题,请诚实告知并礼貌拒绝,绝不编造或使用极端言辞。
|
||||
- 身份约束:你必须在回复中保持指定的角色身份,绝不能说自己是AI、没有性别或脱离角色设定。
|
||||
""";
|
||||
|
||||
private static final String DEFAULT_PROMPT_TEMPLATE = """
|
||||
@@ -75,6 +76,7 @@ public class PromptBuilder {
|
||||
文章(仅供理解上下文,不要复述):
|
||||
{{article}}
|
||||
|
||||
{{conversation_history}}
|
||||
评论:
|
||||
{{comment}}
|
||||
""";
|
||||
@@ -103,6 +105,7 @@ public class PromptBuilder {
|
||||
.replace("{{post_date}}", context.postDate() != null ? context.postDate() : "")
|
||||
.replace("{{comment_count}}", String.valueOf(context.commentCount()))
|
||||
.replace("{{article}}", context.postTitle() + "\n" + context.postContent())
|
||||
.replace("{{conversation_history}}", formatConversationHistory(context))
|
||||
.replace("{{comment}}", context.commentOwner() + ": " + context.commentContent());
|
||||
|
||||
return prompt;
|
||||
@@ -133,20 +136,35 @@ public class PromptBuilder {
|
||||
.replace("{{post_date}}", context.postDate() != null ? context.postDate() : "")
|
||||
.replace("{{comment_count}}", String.valueOf(context.commentCount()))
|
||||
.replace("{{article}}", context.postTitle() + "\n" + context.postContent())
|
||||
.replace("{{conversation_history}}", formatConversationHistory(context))
|
||||
.replace("{{comment}}", context.commentOwner() + ": " + context.commentContent());
|
||||
|
||||
if (sentiment == null || "NEUTRAL".equals(sentiment)) {
|
||||
return prompt;
|
||||
}
|
||||
String sentimentHint = switch (sentiment) {
|
||||
case "VERY_POSITIVE" -> "\n\n【情感提示】评论者情绪非常正面积极,请用热情洋溢的语气回复,表达真诚的感谢和共鸣。";
|
||||
case "POSITIVE" -> "\n\n【情感提示】评论者情绪正面积极,请用热情友好的语气回复,可以表达感谢和共鸣。";
|
||||
case "NEGATIVE" -> "\n\n【情感提示】评论者情绪偏负面,请用理性温和的语气回复,避免激化矛盾,展现理解和包容。";
|
||||
case "VERY_NEGATIVE" -> "\n\n【情感提示】评论者情绪非常负面,请用非常温和、理性的语气回复,避免任何可能激化矛盾的表达,展现充分的理解和耐心。";
|
||||
default -> "";
|
||||
};
|
||||
return prompt + sentimentHint;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Format conversation history for inclusion in the prompt.
|
||||
* Returns empty string if no history is available.
|
||||
*/
|
||||
private String formatConversationHistory(ContextExtractor.CommentContext context) {
|
||||
String history = context.conversationHistory();
|
||||
if (history == null || history.isBlank()) {
|
||||
return "";
|
||||
}
|
||||
return "对话历史(供理解上下文):\n" + history + "\n";
|
||||
}
|
||||
|
||||
private Mono<String> getPromptTemplate() {
|
||||
return client.fetch(ConfigMap.class, CONFIG_MAP_NAME)
|
||||
.mapNotNull(cm -> {
|
||||
@@ -177,7 +195,10 @@ public class PromptBuilder {
|
||||
return client.fetch(AiPersona.class, personaName)
|
||||
.mapNotNull(persona -> {
|
||||
String prompt = persona.getSpec().getPrompt();
|
||||
return (prompt != null && !prompt.isBlank()) ? prompt : null;
|
||||
if (prompt != null && !prompt.isBlank()) {
|
||||
return appendStyleHint(prompt, persona.getSpec().getDisplayName(), persona.getSpec().getGender(), persona.getSpec().getNeutralVoice());
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.defaultIfEmpty(DEFAULT_PERSONA_PROMPT);
|
||||
}
|
||||
@@ -188,11 +209,40 @@ public class PromptBuilder {
|
||||
.next()
|
||||
.mapNotNull(persona -> {
|
||||
String prompt = persona.getSpec().getPrompt();
|
||||
return (prompt != null && !prompt.isBlank()) ? prompt : null;
|
||||
if (prompt != null && !prompt.isBlank()) {
|
||||
return appendStyleHint(prompt, persona.getSpec().getDisplayName(), persona.getSpec().getGender(), persona.getSpec().getNeutralVoice());
|
||||
}
|
||||
return null;
|
||||
})
|
||||
.defaultIfEmpty(DEFAULT_PERSONA_PROMPT);
|
||||
}
|
||||
|
||||
private String appendStyleHint(String prompt, String displayName, String gender, Boolean neutralVoice) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// 身份信息前置到最开头 - 这是AI最先看到的内容,优先级最高
|
||||
if (gender != null && !gender.isBlank()) {
|
||||
String genderDesc = "female".equals(gender) ? "女生" : "男生";
|
||||
sb.append("【核心身份】你叫「").append(displayName != null ? displayName : "").append("」,你是一个").append(genderDesc).append("。");
|
||||
sb.append("这是你的基本身份,你必须始终以这个身份回复。");
|
||||
sb.append("当有人问你性别时,你必须回答自己是").append(genderDesc).append("。");
|
||||
sb.append("绝对不能说自己是AI、没有性别、或脱离角色。");
|
||||
sb.append("\n\n");
|
||||
}
|
||||
|
||||
sb.append(prompt);
|
||||
|
||||
// 添加语气风格提示
|
||||
if (!Boolean.TRUE.equals(neutralVoice)) {
|
||||
if ("female".equals(gender)) {
|
||||
sb.append("\n请使用温柔、细腻的女性语气风格回复。");
|
||||
} else if ("male".equals(gender)) {
|
||||
sb.append("\n请使用沉稳、理性的男性语气风格回复。");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private Mono<String> getEnabledPresetsPrompt() {
|
||||
return client.fetch(ConfigMap.class, CONFIG_MAP_NAME)
|
||||
.mapNotNull(cm -> {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package top.nxxy335.commentaiautopilot.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -8,15 +9,18 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RateLimitService {
|
||||
public class RateLimitService implements DisposableBean {
|
||||
private final ConcurrentHashMap<Long, AtomicInteger> windowMap = new ConcurrentHashMap<>();
|
||||
private final Thread cleanupThread;
|
||||
private volatile boolean running = true;
|
||||
|
||||
public RateLimitService() {
|
||||
// 每5分钟清理过期窗口,防止内存泄漏
|
||||
Thread cleanupThread = new Thread(() -> {
|
||||
while (!Thread.currentThread().isInterrupted()) {
|
||||
cleanupThread = new Thread(() -> {
|
||||
while (running && !Thread.currentThread().isInterrupted()) {
|
||||
try {
|
||||
Thread.sleep(5 * 60 * 1000);
|
||||
if (!running) break;
|
||||
cleanup();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
@@ -55,4 +59,13 @@ public class RateLimitService {
|
||||
log.debug("[RateLimit] Cleaned up {} expired windows", removed);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
running = false;
|
||||
if (cleanupThread != null) {
|
||||
cleanupThread.interrupt();
|
||||
}
|
||||
log.info("[RateLimit] Cleanup thread stopped");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class ReviewService {
|
||||
@@ -14,58 +16,135 @@ public class ReviewService {
|
||||
this.aiFoundationClient = aiFoundationClient;
|
||||
}
|
||||
|
||||
private static final String REVIEW_PROMPT_TEMPLATE = """
|
||||
请你以内容安全审核员的身份,严格评估你刚刚生成的这段回复:
|
||||
/**
|
||||
* Safety classification choices.
|
||||
* Using OutputSpec.choice for reliable structured output.
|
||||
*/
|
||||
private static final String SAFE = "安全";
|
||||
private static final String UNSAFE = "不安全";
|
||||
private static final List<String> SAFETY_CHOICES = List.of(SAFE, UNSAFE);
|
||||
|
||||
文章:
|
||||
%s
|
||||
/**
|
||||
* Quality rating choices for content quality assessment.
|
||||
* Used to produce a meaningful score (1-5) instead of binary 0/100.
|
||||
*/
|
||||
private static final String RATING_EXCELLENT = "5";
|
||||
private static final String RATING_GOOD = "4";
|
||||
private static final String RATING_AVERAGE = "3";
|
||||
private static final String RATING_POOR = "2";
|
||||
private static final String RATING_BAD = "1";
|
||||
private static final List<String> QUALITY_CHOICES = List.of(
|
||||
RATING_EXCELLENT, RATING_GOOD, RATING_AVERAGE, RATING_POOR, RATING_BAD
|
||||
);
|
||||
|
||||
private static final String SAFETY_SYSTEM_PROMPT = """
|
||||
你是内容安全审核员。请严格评估AI生成的回复内容是否安全合规。
|
||||
检查以下方面:
|
||||
1. 是否包含辱骂、仇恨、偏见或煽动性言论
|
||||
2. 是否包含不适宜公开传播的内容
|
||||
3. 是否涉及暴力、歧视、人身攻击或违反法律法规的内容
|
||||
只返回"安全"或"不安全"。""";
|
||||
|
||||
private static final String QUALITY_SYSTEM_PROMPT = """
|
||||
你是AI回复质量评估员。请对AI生成的回复进行质量评分(1-5分)。
|
||||
评分标准:
|
||||
5分:回复切题、自然流畅、语气恰当、有帮助
|
||||
4分:回复较好,基本切题,语气自然
|
||||
3分:回复一般,基本切题但略显生硬或冗长
|
||||
2分:回复较差,偏离主题或语气不当
|
||||
1分:回复很差,完全不相关或不当
|
||||
只返回数字 1-5。""";
|
||||
|
||||
/**
|
||||
* Review AI reply content using two-stage assessment:
|
||||
* 1. Safety check via OutputSpec.choice (安全/不安全)
|
||||
* 2. Quality rating via OutputSpec.choice (1-5) — only if safe
|
||||
*
|
||||
* <p>Score mapping (0-100):
|
||||
* <ul>
|
||||
* <li>Unsafe → 0 (FAIL)</li>
|
||||
* <li>Rating 5 → 100 (PASS)</li>
|
||||
* <li>Rating 4 → 85 (PASS)</li>
|
||||
* <li>Rating 3 → 70 (PASS)</li>
|
||||
* <li>Rating 2 → 50 (PASS, borderline)</li>
|
||||
* <li>Rating 1 → 30 (PASS, but low quality)</li>
|
||||
* </ul>
|
||||
*/
|
||||
public Mono<ReviewResult> review(String articleContent, String commentContent, String aiReply,
|
||||
String modelName) {
|
||||
String userPrompt = String.format("""
|
||||
原始评论:
|
||||
%s
|
||||
|
||||
生成的回复:
|
||||
%s
|
||||
|
||||
请检查以下内容:
|
||||
1. 是否包含任何辱骂、仇恨、偏见或煽动性言论?
|
||||
2. 是否包含不适宜公开传播的内容?
|
||||
3. 是否涉及暴力、歧视、人身攻击或违反法律法规的内容?
|
||||
|
||||
请直接回答"安全"或"不安全"。如果"不安全",请重新生成一段符合规范的温和回复。
|
||||
""";
|
||||
|
||||
public Mono<ReviewResult> review(String articleContent, String commentContent, String aiReply,
|
||||
String modelName) {
|
||||
String reviewPrompt = String.format(REVIEW_PROMPT_TEMPLATE,
|
||||
truncate(articleContent, 2000),
|
||||
请判断以上回复是否安全合规。""",
|
||||
truncate(commentContent, 500),
|
||||
truncate(aiReply, 500));
|
||||
|
||||
return aiFoundationClient.chat(reviewPrompt, modelName)
|
||||
.map(this::parseSafetyResult)
|
||||
// Stage 1: Safety check
|
||||
return aiFoundationClient.classify(SAFETY_SYSTEM_PROMPT, userPrompt, SAFETY_CHOICES, modelName)
|
||||
.flatMap(safetyResult -> {
|
||||
if (UNSAFE.equals(safetyResult)) {
|
||||
log.warn("[Review] Content is UNSAFE");
|
||||
return Mono.just(new ReviewResult(0, "FAIL", "内容安全审核不通过"));
|
||||
}
|
||||
if (!SAFE.equals(safetyResult)) {
|
||||
log.warn("[Review] Unexpected safety result: {}, treating as unsafe", safetyResult);
|
||||
return Mono.just(new ReviewResult(0, "FAIL", "内容安全审核结果异常"));
|
||||
}
|
||||
// Stage 2: Quality rating (only for safe content)
|
||||
return rateQuality(commentContent, aiReply, modelName);
|
||||
})
|
||||
.defaultIfEmpty(new ReviewResult(100, "PASS", "审核无响应,自动通过"))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("Review failed, auto-passing: {}", e.getMessage());
|
||||
log.warn("[Review] Review failed, auto-passing: {}", e.getMessage());
|
||||
return Mono.just(new ReviewResult(100, "PASS", "审核服务异常,自动通过"));
|
||||
});
|
||||
}
|
||||
|
||||
private ReviewResult parseSafetyResult(String response) {
|
||||
if (response == null || response.isBlank()) {
|
||||
return new ReviewResult(100, "PASS", "审核无响应,自动通过");
|
||||
/**
|
||||
* Rate the quality of a safe AI reply (1-5) and map to a 0-100 score.
|
||||
*/
|
||||
private Mono<ReviewResult> rateQuality(String commentContent, String aiReply, String modelName) {
|
||||
String qualityPrompt = String.format("""
|
||||
评论:
|
||||
%s
|
||||
|
||||
回复:
|
||||
%s
|
||||
|
||||
请对以上回复进行质量评分(1-5分)。""",
|
||||
truncate(commentContent, 500),
|
||||
truncate(aiReply, 500));
|
||||
|
||||
return aiFoundationClient.classify(QUALITY_SYSTEM_PROMPT, qualityPrompt, QUALITY_CHOICES, modelName)
|
||||
.map(rating -> {
|
||||
int score = mapRatingToScore(rating);
|
||||
String reason = "安全通过,质量评分: " + rating + "/5";
|
||||
log.info("[Review] Content is SAFE, quality rating: {}/5, score: {}", rating, score);
|
||||
return new ReviewResult(score, "PASS", reason);
|
||||
})
|
||||
.defaultIfEmpty(new ReviewResult(85, "PASS", "安全通过,质量评分默认 4/5"))
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Review] Quality rating failed, defaulting to 85: {}", e.getMessage());
|
||||
return Mono.just(new ReviewResult(85, "PASS", "安全通过,质量评分异常"));
|
||||
});
|
||||
}
|
||||
String trimmed = response.trim().toLowerCase();
|
||||
if (trimmed.contains("不安全") || trimmed.contains("unsafe")) {
|
||||
log.warn("AI Review: content is UNSAFE, response: {}", response);
|
||||
return new ReviewResult(0, "FAIL", "内容安全审核不通过");
|
||||
}
|
||||
if (trimmed.contains("安全") || trimmed.contains("safe")) {
|
||||
log.info("AI Review: content is SAFE");
|
||||
return new ReviewResult(100, "PASS", "内容安全审核通过");
|
||||
}
|
||||
// If unclear response, default to pass
|
||||
log.warn("AI Review: unclear response, auto-passing: {}", response);
|
||||
return new ReviewResult(100, "PASS", "审核结果不明确,自动通过");
|
||||
|
||||
/**
|
||||
* Map a 1-5 quality rating to a 0-100 score.
|
||||
*/
|
||||
private int mapRatingToScore(String rating) {
|
||||
return switch (rating) {
|
||||
case RATING_EXCELLENT -> 100;
|
||||
case RATING_GOOD -> 85;
|
||||
case RATING_AVERAGE -> 70;
|
||||
case RATING_POOR -> 50;
|
||||
case RATING_BAD -> 30;
|
||||
default -> 70; // default to average
|
||||
};
|
||||
}
|
||||
|
||||
private String truncate(String text, int maxLength) {
|
||||
|
||||
@@ -4,6 +4,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SentimentService {
|
||||
@@ -15,18 +17,44 @@ public class SentimentService {
|
||||
}
|
||||
|
||||
public record SentimentResult(String sentiment, double confidence) {
|
||||
public static final String VERY_POSITIVE = "VERY_POSITIVE";
|
||||
public static final String POSITIVE = "POSITIVE";
|
||||
public static final String NEUTRAL = "NEUTRAL";
|
||||
public static final String NEGATIVE = "NEGATIVE";
|
||||
public static final String VERY_NEGATIVE = "VERY_NEGATIVE";
|
||||
}
|
||||
|
||||
public Mono<SentimentResult> analyzeSentiment(String commentContent, String modelName) {
|
||||
String prompt = buildSentimentPrompt(commentContent);
|
||||
private static final List<String> CHOICES = List.of(
|
||||
SentimentResult.VERY_POSITIVE, SentimentResult.POSITIVE,
|
||||
SentimentResult.NEUTRAL, SentimentResult.NEGATIVE,
|
||||
SentimentResult.VERY_NEGATIVE
|
||||
);
|
||||
|
||||
return aiFoundationClient.chat(prompt, modelName)
|
||||
.map(response -> {
|
||||
String sentiment = parseSentiment(response);
|
||||
return new SentimentResult(sentiment, 1.0);
|
||||
/**
|
||||
* Analyze sentiment using AI Foundation structured output
|
||||
* ({@code OutputSpec.choice}) for reliable classification.
|
||||
*/
|
||||
public Mono<SentimentResult> analyzeSentiment(String commentContent, String modelName) {
|
||||
String systemPrompt = "你是一个专业的情感分析助手。请根据以下标准分析评论的情感倾向:\n"
|
||||
+ "\n"
|
||||
+ "- VERY_POSITIVE:非常正面,包含强烈的感谢、赞美或认同(如\"太棒了\"、\"非常感谢\"、\"写得太好了\")\n"
|
||||
+ "- POSITIVE:正面,友好、肯定或支持的态度(如\"不错\"、\"学习了\"、\"支持\")\n"
|
||||
+ "- NEUTRAL:中性,提问、讨论、陈述事实,无明显情感倾向(如\"请问...\"、\"这个怎么用\"、\"我觉得\")\n"
|
||||
+ "- NEGATIVE:负面,不满、质疑或批评(如\"不好用\"、\"有问题\"、\"不太行\")\n"
|
||||
+ "- VERY_NEGATIVE:非常负面,攻击、辱骂或极端负面情绪(如\"垃圾\"、\"骗子\"、\"太差了\")\n"
|
||||
+ "\n"
|
||||
+ "只返回 VERY_POSITIVE、POSITIVE、NEUTRAL、NEGATIVE 或 VERY_NEGATIVE 之一。";
|
||||
String userPrompt = "分析以下评论的情感倾向:\n\n" + commentContent;
|
||||
|
||||
return aiFoundationClient.classify(systemPrompt, userPrompt, CHOICES, modelName)
|
||||
.map(sentiment -> {
|
||||
String upper = sentiment.toUpperCase();
|
||||
// Validate against known choices; default to NEUTRAL if unexpected
|
||||
if (!CHOICES.contains(upper)) {
|
||||
log.warn("[Sentiment] Unexpected classification result: {}, defaulting to NEUTRAL", sentiment);
|
||||
return new SentimentResult(SentimentResult.NEUTRAL, 0.0);
|
||||
}
|
||||
return new SentimentResult(upper, 1.0);
|
||||
})
|
||||
.onErrorResume(e -> {
|
||||
log.warn("[Sentiment] Failed to analyze sentiment, defaulting to NEUTRAL: {}", e.getMessage());
|
||||
@@ -34,16 +62,4 @@ public class SentimentService {
|
||||
})
|
||||
.defaultIfEmpty(new SentimentResult(SentimentResult.NEUTRAL, 0.0));
|
||||
}
|
||||
|
||||
private String buildSentimentPrompt(String commentContent) {
|
||||
return "请分析以下评论的情感倾向。只回复一个词:POSITIVE(正面)、NEUTRAL(中性)或 NEGATIVE(负面)。\n\n评论内容:\n" + commentContent;
|
||||
}
|
||||
|
||||
private String parseSentiment(String response) {
|
||||
if (response == null || response.isBlank()) return SentimentResult.NEUTRAL;
|
||||
String upper = response.trim().toUpperCase();
|
||||
if (upper.contains("POSITIVE")) return SentimentResult.POSITIVE;
|
||||
if (upper.contains("NEGATIVE")) return SentimentResult.NEGATIVE;
|
||||
return SentimentResult.NEUTRAL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package top.nxxy335.commentaiautopilot.service;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Component;
|
||||
import reactor.core.publisher.Mono;
|
||||
import run.halo.app.extension.ReactiveExtensionClient;
|
||||
import top.nxxy335.commentaiautopilot.extension.AiPersona;
|
||||
|
||||
/**
|
||||
* Service for checking wake words in comment content.
|
||||
* A wake word is a prefix that triggers AI reply from a specific persona,
|
||||
* even if the page hasn't enabled AI auto-reply.
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class WakeWordService {
|
||||
|
||||
private final ReactiveExtensionClient client;
|
||||
|
||||
/**
|
||||
* Result of a wake word match.
|
||||
*
|
||||
* @param personaName the metadata.name of the matched persona
|
||||
* @param wakeWord the wake word that matched
|
||||
* @param contentWithoutWakeWord the comment content with the wake word prefix removed
|
||||
*/
|
||||
public record WakeWordMatch(String personaName, String wakeWord, String contentWithoutWakeWord) {}
|
||||
|
||||
/**
|
||||
* Check if the given content starts with any persona's wake word.
|
||||
* Returns the first matching persona's info, or empty if no match.
|
||||
*
|
||||
* @param content the comment/reply content to check
|
||||
* @return WakeWordMatch if a wake word is found, or empty Mono
|
||||
*/
|
||||
public Mono<WakeWordMatch> checkWakeWord(String content) {
|
||||
if (content == null || content.isBlank()) {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
return client.list(AiPersona.class, null, null)
|
||||
.filter(persona -> {
|
||||
String wakeWord = persona.getSpec().getWakeWord();
|
||||
return wakeWord != null && !wakeWord.isBlank() && content.startsWith(wakeWord);
|
||||
})
|
||||
.next()
|
||||
.map(persona -> {
|
||||
String wakeWord = persona.getSpec().getWakeWord();
|
||||
String remaining = content.substring(wakeWord.length()).trim();
|
||||
log.info("[WakeWord] Matched wake word '{}' for persona '{}'",
|
||||
wakeWord, persona.getSpec().getDisplayName());
|
||||
return new WakeWordMatch(persona.getMetadata().getName(), wakeWord, remaining);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocking version for use in Reconciler (sync context).
|
||||
* Checks if the given content starts with any persona's wake word.
|
||||
*
|
||||
* @param syncClient the blocking ExtensionClient
|
||||
* @param content the comment/reply content to check
|
||||
* @return WakeWordMatch if a wake word is found, or null
|
||||
*/
|
||||
public WakeWordMatch checkWakeWordBlocking(run.halo.app.extension.ExtensionClient syncClient, String content) {
|
||||
if (content == null || content.isBlank()) {
|
||||
log.info("[WakeWord] Content is null or blank, skipping");
|
||||
return null;
|
||||
}
|
||||
|
||||
String trimmedContent = content.trim();
|
||||
var personas = syncClient.listAll(AiPersona.class, null, Sort.unsorted());
|
||||
log.info("[WakeWord] Checking {} personas against content: '{}'", personas.size(),
|
||||
trimmedContent.length() > 50 ? trimmedContent.substring(0, 50) + "..." : trimmedContent);
|
||||
|
||||
for (var persona : personas) {
|
||||
String wakeWord = persona.getSpec().getWakeWord();
|
||||
if (wakeWord == null || wakeWord.isBlank()) {
|
||||
log.info("[WakeWord] Persona '{}' has no wakeWord, skipping", persona.getSpec().getDisplayName());
|
||||
continue;
|
||||
}
|
||||
String trimmedWakeWord = wakeWord.trim();
|
||||
log.info("[WakeWord] Checking persona '{}' with wakeWord '{}' against content starting with '{}'",
|
||||
persona.getSpec().getDisplayName(), trimmedWakeWord,
|
||||
trimmedContent.length() >= trimmedWakeWord.length()
|
||||
? trimmedContent.substring(0, trimmedWakeWord.length()) : trimmedContent);
|
||||
|
||||
if (trimmedContent.startsWith(trimmedWakeWord)) {
|
||||
String remaining = trimmedContent.substring(trimmedWakeWord.length()).trim();
|
||||
log.info("[WakeWord] MATCHED! wakeWord='{}' for persona '{}', remaining content: '{}'",
|
||||
trimmedWakeWord, persona.getSpec().getDisplayName(),
|
||||
remaining.length() > 30 ? remaining.substring(0, 30) + "..." : remaining);
|
||||
return new WakeWordMatch(persona.getMetadata().getName(), trimmedWakeWord, remaining);
|
||||
}
|
||||
}
|
||||
log.info("[WakeWord] No wake word matched");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package top.nxxy335.commentaiautopilot.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* Utility for generating Gravatar/Cravatar avatar URLs from email addresses.
|
||||
*/
|
||||
@Slf4j
|
||||
public class GravatarUtil {
|
||||
|
||||
private static final String CRAVATAR_BASE_URL = "https://cn.cravatar.com/avatar/";
|
||||
|
||||
private GravatarUtil() {}
|
||||
|
||||
/**
|
||||
* Generate Cravatar URL from email address using SHA-256 hash.
|
||||
*
|
||||
* @param email the email address
|
||||
* @return the avatar URL, or empty string if generation fails
|
||||
*/
|
||||
public static String generateUrl(String email) {
|
||||
if (email == null || email.isBlank()) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
var digest = MessageDigest.getInstance("SHA-256");
|
||||
var hashBytes = digest.digest(email.trim().toLowerCase().getBytes(StandardCharsets.UTF_8));
|
||||
var hexString = new StringBuilder();
|
||||
for (byte b : hashBytes) {
|
||||
hexString.append(String.format("%02x", b));
|
||||
}
|
||||
return CRAVATAR_BASE_URL + hexString;
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to generate Gravatar URL: {}", e.getMessage());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,16 @@ spec:
|
||||
label: 评论者黑名单
|
||||
help: "输入评论者显示名称或邮箱,多个用逗号分隔。支持正则表达式,以 regex: 开头,如 regex:^spam.*"
|
||||
value: ""
|
||||
- $formkit: switch
|
||||
name: preFilterEnabled
|
||||
label: 启用前置过滤
|
||||
help: "AI回复前检测评论合规性,拦截广告/辱骂/敏感内容,节省Token"
|
||||
value: true
|
||||
- $formkit: switch
|
||||
name: preFilterPendingOnViolation
|
||||
label: 违规评论设为待审核
|
||||
help: "检测到违规评论时自动取消通过,需人工审核"
|
||||
value: true
|
||||
- group: model
|
||||
label: 模型设置
|
||||
formSchema:
|
||||
@@ -54,7 +64,7 @@ spec:
|
||||
- $formkit: textarea
|
||||
name: customPromptTemplate
|
||||
label: 自定义Prompt模板
|
||||
value: "{{persona_prompt}}\n\n{{safety_prompt}}\n\n【语言要求】请用评论所使用的语言回复。如果评论是英文,请用英文回复;如果是中文,请用中文回复;如果是日文,请用日文回复;以此类推。\n\n请回复以下评论。注意:\n- 回复长度应与评论长度匹配,简短问候简短回复\n- 不要复述或总结文章内容\n- 自然对话,不要写小作文\n- 只有评论涉及具体内容时才针对性回应\n\n文章(仅供理解上下文,不要复述):\n{{article}}\n\n评论:\n{{comment}}"
|
||||
value: "{{persona_prompt}}\n\n{{safety_prompt}}\n\n【语言要求】请用评论所使用的语言回复。如果评论是英文,请用英文回复;如果是中文,请用中文回复;如果是日文,请用日文回复;以此类推。\n\n请回复以下评论。注意:\n- 回复长度应与评论长度匹配,简短问候简短回复\n- 不要复述或总结文章内容\n- 自然对话,不要写小作文\n- 只有评论涉及具体内容时才针对性回应\n\n文章(仅供理解上下文,不要复述):\n{{article}}\n\n{{conversation_history}}\n评论:\n{{comment}}"
|
||||
- $formkit: select
|
||||
name: enabledPresets
|
||||
label: 启用预设
|
||||
|
||||
@@ -5,9 +5,17 @@ kind: Plugin
|
||||
metadata:
|
||||
# The name defines how the plugin is invoked, A unique name
|
||||
name: comment-ai-autopilot
|
||||
annotations:
|
||||
# Recommend installing AI Foundation from the app store after installing this plugin
|
||||
# https://www.halo.run/store/apps/app-acslk9nu
|
||||
"store.halo.run/recommended-apps": '["app-acslk9nu"]'
|
||||
spec:
|
||||
enabled: true
|
||||
requires: ">=2.25.0"
|
||||
pluginDependencies:
|
||||
# Optional dependency: plugin still loads without AI Foundation,
|
||||
# but AI features require it to be installed and enabled.
|
||||
ai-foundation?: "*"
|
||||
author:
|
||||
name: 暖心向阳335
|
||||
website: https://nxxy335.top
|
||||
@@ -22,4 +30,4 @@ spec:
|
||||
url: "https://github.com/sunny-335/plugin-comment-ai-autopilot/blob/main/LICENSE"
|
||||
settingName: "comment-ai-autopilot-settings"
|
||||
configMapName: "comment-ai-autopilot-configmap"
|
||||
version: "1.0.0-beta.1"
|
||||
version: "1.2.1"
|
||||
|
||||
+29
-11
@@ -5,16 +5,33 @@ plugins {
|
||||
|
||||
group 'top.nxxy335.commentaiautopilot.ui'
|
||||
|
||||
// Fix Gradle 9.x compatibility with pnpm symlinks
|
||||
tasks.named('pnpmInstall') {
|
||||
doNotTrackState("pnpm symlinks are not compatible with Gradle state tracking")
|
||||
// Use system pnpm directly — avoids Windows exit code 268435659
|
||||
// caused by Gradle Worker Daemon / node-gradle downloading pnpm on Windows
|
||||
node {
|
||||
download = false
|
||||
}
|
||||
|
||||
tasks.register('pnpmBuild', PnpmTask) {
|
||||
// Skip built-in pnpm tasks (they fail on Windows), replace with Exec-based tasks
|
||||
tasks.named('pnpmSetup').configure { enabled = false }
|
||||
tasks.named('pnpmInstall').configure { enabled = false }
|
||||
|
||||
// Cross-platform: use 'cmd /c' on Windows, direct 'pnpm' on Linux/macOS
|
||||
def isWindows = System.properties['os.name'].toLowerCase().contains('windows')
|
||||
def pnpmCmd = isWindows ? ['cmd', '/c', 'pnpm'] : ['pnpm']
|
||||
|
||||
tasks.register('uiInstall', Exec) {
|
||||
group = 'build'
|
||||
description = 'Install UI dependencies using system pnpm'
|
||||
workingDir layout.projectDirectory
|
||||
commandLine(pnpmCmd + ['install'])
|
||||
}
|
||||
|
||||
tasks.register('uiBuild', Exec) {
|
||||
group = 'build'
|
||||
description = 'Build the UI project using pnpm'
|
||||
args = ['build']
|
||||
dependsOn tasks.named('pnpmInstall')
|
||||
workingDir layout.projectDirectory
|
||||
commandLine(pnpmCmd + ['run', 'build'])
|
||||
dependsOn uiInstall
|
||||
inputs.dir(layout.projectDirectory.dir('src'))
|
||||
inputs.files(fileTree(
|
||||
dir: layout.projectDirectory,
|
||||
@@ -22,17 +39,18 @@ tasks.register('pnpmBuild', PnpmTask) {
|
||||
outputs.dir(layout.buildDirectory.dir('dist'))
|
||||
}
|
||||
|
||||
tasks.register('pnpmCheck', PnpmTask) {
|
||||
tasks.register('uiCheck', Exec) {
|
||||
group = 'verification'
|
||||
description = 'Run unit tests for the UI project using pnpm'
|
||||
args = ['test:unit']
|
||||
dependsOn tasks.named('pnpmInstall')
|
||||
workingDir layout.projectDirectory
|
||||
commandLine(pnpmCmd + ['run', 'test:unit'])
|
||||
dependsOn uiInstall
|
||||
}
|
||||
|
||||
tasks.named('check') {
|
||||
dependsOn tasks.named('pnpmCheck')
|
||||
dependsOn tasks.named('uiCheck')
|
||||
}
|
||||
|
||||
tasks.named('assemble') {
|
||||
dependsOn tasks.named('pnpmBuild')
|
||||
dependsOn tasks.named('uiBuild')
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* 统一 API 路径常量,避免硬编码散布在多个 Vue 文件中。
|
||||
* 修改 API 路径只需在此处更新。
|
||||
*/
|
||||
export const API_BASE = '/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1'
|
||||
|
||||
// ===== 日志相关 =====
|
||||
export const API_REPLIES = `${API_BASE}/replies`
|
||||
export const apiReply = (name: string) => `${API_REPLIES}/${name}`
|
||||
export const apiReplyAction = (name: string, action: string) => `${API_REPLIES}/${name}/${action}`
|
||||
export const API_BATCH_APPROVE = `${API_REPLIES}/batch-approve`
|
||||
export const API_BATCH_REJECT = `${API_REPLIES}/batch-reject`
|
||||
export const API_BATCH_DELETE = `${API_REPLIES}/batch-delete`
|
||||
export const apiConversation = (commentId: string) => `${API_BASE}/conversation/${commentId}`
|
||||
|
||||
// ===== 概览相关 =====
|
||||
export const API_STATS = `${API_BASE}/stats`
|
||||
export const API_PERSONAS = `${API_BASE}/personas`
|
||||
export const apiPersona = (name: string) => `${API_PERSONAS}/${name}`
|
||||
export const API_HEALTH = `${API_BASE}/health`
|
||||
|
||||
// ===== 设置相关 =====
|
||||
export const API_EXPORT = `${API_BASE}/export`
|
||||
export const API_IMPORT = `${API_BASE}/import`
|
||||
export const API_COMMENTERS = `${API_BASE}/commenters`
|
||||
export const API_CLEANUP = `${API_BASE}/cleanup`
|
||||
|
||||
// ===== 评论触发 =====
|
||||
export const apiCommentTrigger = (commentName: string) => `${API_BASE}/comments/${commentName}/trigger`
|
||||
@@ -0,0 +1,14 @@
|
||||
export async function computeGravatarHash(email: string): Promise<string> {
|
||||
if (!email || email.trim() === '') return ''
|
||||
const normalizedEmail = email.trim().toLowerCase()
|
||||
const encoder = new TextEncoder()
|
||||
const data = encoder.encode(normalizedEmail)
|
||||
const hashBuffer = await crypto.subtle.digest('SHA-256', data)
|
||||
const hashArray = Array.from(new Uint8Array(hashBuffer))
|
||||
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('')
|
||||
}
|
||||
|
||||
export function getGravatarUrl(hash: string): string {
|
||||
if (!hash) return ''
|
||||
return `https://cn.cravatar.com/avatar/${hash}`
|
||||
}
|
||||
+28
-202
@@ -108,152 +108,11 @@
|
||||
</VCard>
|
||||
</div>
|
||||
|
||||
<!-- Middle: Sentiment + Trend -->
|
||||
<div class="grid grid-cols-1 gap-4 mt-4 lg:grid-cols-2">
|
||||
<!-- Sentiment Distribution -->
|
||||
<VCard :body-class="['!p-5']">
|
||||
<h3 class="text-sm font-medium text-gray-500 mb-4">情感分布</h3>
|
||||
<div class="space-y-3">
|
||||
<div class="flex items-center gap-3">
|
||||
<div class="w-2 h-2 rounded-full bg-green-500 shrink-0"></div>
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="flex items-center justify-between text-sm">
|
||||
<span class="text-gray-700">正面</span>
|
||||
<span class="font-medium text-green-600">{{ stats?.sentimentDistribution?.POSITIVE || 0 }}</span>
|
||||
</div>
|
||||
<div class="mt-1 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
||||
<div
|
||||
class="h-full bg-green-400 rounded-full transition-all duration-500"
|
||||
:style="{ width: getSentimentPercent('POSITIVE') + '%' }"
|
||||
></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-3">
|
||||
<div class="w-2 h-2 rounded-full bg-gray-400 shrink-0"></div>
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="flex items-center justify-between text-sm">
|
||||
<span class="text-gray-700">中性</span>
|
||||
<span class="font-medium text-gray-600">{{ stats?.sentimentDistribution?.NEUTRAL || 0 }}</span>
|
||||
</div>
|
||||
<div class="mt-1 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
||||
<div
|
||||
class="h-full bg-gray-400 rounded-full transition-all duration-500"
|
||||
:style="{ width: getSentimentPercent('NEUTRAL') + '%' }"
|
||||
></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-3">
|
||||
<div class="w-2 h-2 rounded-full bg-red-500 shrink-0"></div>
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="flex items-center justify-between text-sm">
|
||||
<span class="text-gray-700">负面</span>
|
||||
<span class="font-medium text-red-500">{{ stats?.sentimentDistribution?.NEGATIVE || 0 }}</span>
|
||||
</div>
|
||||
<div class="mt-1 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
||||
<div
|
||||
class="h-full bg-red-400 rounded-full transition-all duration-500"
|
||||
:style="{ width: getSentimentPercent('NEGATIVE') + '%' }"
|
||||
></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-3">
|
||||
<div class="w-2 h-2 rounded-full bg-gray-300 shrink-0"></div>
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="flex items-center justify-between text-sm">
|
||||
<span class="text-gray-700">未知</span>
|
||||
<span class="font-medium text-gray-400">{{ stats?.sentimentDistribution?.UNKNOWN || 0 }}</span>
|
||||
</div>
|
||||
<div class="mt-1 h-1.5 bg-gray-100 rounded-full overflow-hidden">
|
||||
<div
|
||||
class="h-full bg-gray-300 rounded-full transition-all duration-500"
|
||||
:style="{ width: getSentimentPercent('UNKNOWN') + '%' }"
|
||||
></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</VCard>
|
||||
|
||||
<!-- Daily Trend -->
|
||||
<VCard :body-class="['!p-5']">
|
||||
<div class="flex items-center justify-between mb-4">
|
||||
<h3 class="text-sm font-medium text-gray-500">近7日回复趋势</h3>
|
||||
<div class="inline-flex rounded-md border border-gray-200 overflow-hidden">
|
||||
<button
|
||||
class="px-2.5 py-1 text-xs transition-colors"
|
||||
:class="range === '7' ? 'bg-blue-500 text-white' : 'bg-white text-gray-600 hover:bg-gray-50'"
|
||||
@click="range = '7'"
|
||||
>
|
||||
7天
|
||||
</button>
|
||||
<button
|
||||
class="px-2.5 py-1 text-xs border-l border-gray-200 transition-colors"
|
||||
:class="range === '30' ? 'bg-blue-500 text-white' : 'bg-white text-gray-600 hover:bg-gray-50'"
|
||||
@click="range = '30'"
|
||||
>
|
||||
30天
|
||||
</button>
|
||||
<button
|
||||
class="px-2.5 py-1 text-xs border-l border-gray-200 transition-colors"
|
||||
:class="range === 'all' ? 'bg-blue-500 text-white' : 'bg-white text-gray-600 hover:bg-gray-50'"
|
||||
@click="range = 'all'"
|
||||
>
|
||||
全部
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="stats?.dailyTrend?.length" class="flex items-end gap-3" style="height: 160px">
|
||||
<div
|
||||
v-for="day in stats.dailyTrend"
|
||||
:key="day.date"
|
||||
class="flex-1 flex flex-col items-center justify-end h-full"
|
||||
>
|
||||
<div class="text-xs text-gray-500 mb-1 font-medium">{{ day.count }}</div>
|
||||
<div
|
||||
class="w-full rounded-t-md transition-all duration-500"
|
||||
:class="day.count > 0 ? 'bg-gradient-to-t from-blue-500 to-blue-400' : 'bg-gray-100'"
|
||||
:style="{ height: getTrendBarHeight(day.count) + 'px' }"
|
||||
></div>
|
||||
<div class="text-[10px] text-gray-400 mt-2 whitespace-nowrap">{{ formatTrendDate(day.date) }}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div v-else class="flex items-center justify-center text-sm text-gray-400" style="height: 160px">
|
||||
暂无数据
|
||||
</div>
|
||||
</VCard>
|
||||
</div>
|
||||
|
||||
<!-- Bottom: Score + Quick Actions -->
|
||||
<div class="grid grid-cols-1 gap-4 mt-4 sm:grid-cols-2">
|
||||
<!-- Avg Score -->
|
||||
<VCard :body-class="['!p-5']">
|
||||
<h3 class="text-sm font-medium text-gray-500 mb-3">平均审核评分</h3>
|
||||
<div class="flex items-center gap-4">
|
||||
<div class="text-4xl font-bold" :class="scoreColor">{{ stats?.avgScore?.toFixed(1) || '0.0' }}</div>
|
||||
<div class="flex-1">
|
||||
<div class="h-3 bg-gray-100 rounded-full overflow-hidden">
|
||||
<div
|
||||
class="h-full rounded-full transition-all duration-500"
|
||||
:class="scoreBarColor"
|
||||
:style="{ width: (stats?.avgScore || 0) * 10 + '%' }"
|
||||
></div>
|
||||
</div>
|
||||
<div class="flex justify-between text-[10px] text-gray-300 mt-1">
|
||||
<span>0</span>
|
||||
<span>5</span>
|
||||
<span>10</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</VCard>
|
||||
|
||||
<!-- Quick Actions -->
|
||||
<div class="mt-4">
|
||||
<VCard :body-class="['!p-5']">
|
||||
<h3 class="text-sm font-medium text-gray-500 mb-3">快捷操作</h3>
|
||||
<div class="grid grid-cols-2 gap-2">
|
||||
<div class="grid grid-cols-1 gap-2 sm:grid-cols-3">
|
||||
<button
|
||||
class="flex items-center gap-2 px-3 py-2.5 rounded-md bg-gray-50 hover:bg-gray-100 transition-colors text-sm text-gray-700"
|
||||
@click="$router.push({ name: 'CommentAiAutopilotLogs' })"
|
||||
@@ -273,15 +132,6 @@
|
||||
</svg>
|
||||
插件设置
|
||||
</button>
|
||||
<button
|
||||
class="flex items-center gap-2 px-3 py-2.5 rounded-md bg-gray-50 hover:bg-gray-100 transition-colors text-sm text-gray-700"
|
||||
@click="$router.push({ name: 'CommentAiAutopilotSettings' })"
|
||||
>
|
||||
<svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
|
||||
</svg>
|
||||
AI角色
|
||||
</button>
|
||||
<button
|
||||
class="flex items-center gap-2 px-3 py-2.5 rounded-md bg-gray-50 hover:bg-gray-100 transition-colors text-sm text-gray-700"
|
||||
@click="refreshData"
|
||||
@@ -299,24 +149,16 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted, watch } from "vue"
|
||||
import { ref, computed, onMounted } from "vue"
|
||||
import { axiosInstance } from "@halo-dev/api-client"
|
||||
import { VPageHeader, VButton, VCard, Toast } from "@halo-dev/components"
|
||||
import { IconPlug } from "@halo-dev/components"
|
||||
|
||||
interface DailyCount {
|
||||
date: string
|
||||
count: number
|
||||
}
|
||||
|
||||
interface StatsResponse {
|
||||
total: number
|
||||
passCount: number
|
||||
failCount: number
|
||||
reviewingCount: number
|
||||
avgScore: number
|
||||
sentimentDistribution: Record<string, number>
|
||||
dailyTrend: DailyCount[]
|
||||
}
|
||||
|
||||
interface PersonaResponse {
|
||||
@@ -331,7 +173,6 @@ interface HealthResponse {
|
||||
|
||||
const stats = ref<StatsResponse | null>(null)
|
||||
const persona = ref<PersonaResponse | null>(null)
|
||||
const range = ref("7")
|
||||
const health = ref<HealthResponse | null>(null)
|
||||
const healthVisible = ref(true)
|
||||
|
||||
@@ -340,24 +181,10 @@ const passRate = computed(() => {
|
||||
return Math.round((stats.value.passCount / stats.value.total) * 100)
|
||||
})
|
||||
|
||||
const scoreColor = computed(() => {
|
||||
const score = stats.value?.avgScore || 0
|
||||
if (score >= 7) return "text-green-600"
|
||||
if (score >= 4) return "text-amber-500"
|
||||
return "text-red-500"
|
||||
})
|
||||
|
||||
const scoreBarColor = computed(() => {
|
||||
const score = stats.value?.avgScore || 0
|
||||
if (score >= 7) return "bg-gradient-to-r from-green-400 to-green-500"
|
||||
if (score >= 4) return "bg-gradient-to-r from-amber-400 to-amber-500"
|
||||
return "bg-gradient-to-r from-red-400 to-red-500"
|
||||
})
|
||||
|
||||
const fetchStats = async () => {
|
||||
try {
|
||||
const { data } = await axiosInstance.get(
|
||||
`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/stats?range=${range.value}`,
|
||||
`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/stats?range=7`,
|
||||
)
|
||||
stats.value = data
|
||||
} catch (e) {
|
||||
@@ -421,34 +248,33 @@ const openSettings = () => {
|
||||
window.location.href = "/console/comment-ai-autopilot/settings"
|
||||
}
|
||||
|
||||
const getSentimentPercent = (sentiment: string): number => {
|
||||
const dist = stats.value?.sentimentDistribution
|
||||
if (!dist) return 0
|
||||
const total = Object.values(dist).reduce((a, b) => a + b, 0)
|
||||
if (total === 0) return 0
|
||||
return Math.round(((dist[sentiment] || 0) / total) * 100)
|
||||
}
|
||||
|
||||
const getTrendBarHeight = (count: number): number => {
|
||||
const trend = stats.value?.dailyTrend
|
||||
if (!trend || trend.length === 0) return 0
|
||||
const max = Math.max(...trend.map(d => d.count), 1)
|
||||
return Math.max((count / max) * 100, count > 0 ? 8 : 4)
|
||||
}
|
||||
|
||||
const formatTrendDate = (dateStr: string): string => {
|
||||
if (!dateStr) return ''
|
||||
const parts = dateStr.split('-')
|
||||
return parts.length >= 3 ? `${parts[1]}/${parts[2]}` : dateStr
|
||||
}
|
||||
|
||||
watch(range, () => {
|
||||
fetchStats()
|
||||
})
|
||||
|
||||
onMounted(() => {
|
||||
fetchStats()
|
||||
fetchPersona()
|
||||
fetchHealth()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.line-clamp-2 {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
/* Mobile responsive */
|
||||
@media (max-width: 640px) {
|
||||
.comment-ai-autopilot-home :deep(.page-header) {
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
}
|
||||
.comment-ai-autopilot-home :deep(.page-header-actions) {
|
||||
width: 100%;
|
||||
}
|
||||
.comment-ai-autopilot-home :deep(.page-header-actions .space-y-2) {
|
||||
flex-direction: row;
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
+313
-550
@@ -1,300 +1,176 @@
|
||||
<template>
|
||||
<div class="comment-ai-autopilot-logs">
|
||||
<div class="logs-container">
|
||||
<VPageHeader title="AI回复日志">
|
||||
<template #icon>
|
||||
<IconPlug class="mr-2 self-center" />
|
||||
</template>
|
||||
<template #icon><IconPlug class="header-icon" /></template>
|
||||
<template #actions>
|
||||
<VButton @click="$router.push({ name: 'CommentAiAutopilot' })"> 返回概览 </VButton>
|
||||
<VButton @click="$router.push({ name: 'CommentAiAutopilot' })">返回概览</VButton>
|
||||
</template>
|
||||
</VPageHeader>
|
||||
|
||||
<!-- Batch Operation Toolbar -->
|
||||
<div v-if="selectedNames.size > 0" class="mx-4 mt-2 flex items-center gap-3 bg-blue-50 border border-blue-200 rounded-lg px-4 py-2.5">
|
||||
<span class="text-sm text-blue-700">已选择 {{ selectedNames.size }} 项</span>
|
||||
<button
|
||||
class="text-xs px-3 py-1 rounded bg-green-600 text-white hover:bg-green-700 transition-colors"
|
||||
@click="batchApprove"
|
||||
>
|
||||
批量通过
|
||||
</button>
|
||||
<button
|
||||
class="text-xs px-3 py-1 rounded bg-orange-500 text-white hover:bg-orange-600 transition-colors"
|
||||
@click="batchReject"
|
||||
>
|
||||
批量拒绝
|
||||
</button>
|
||||
<button
|
||||
class="text-xs px-3 py-1 rounded bg-red-600 text-white hover:bg-red-700 transition-colors"
|
||||
@click="batchDelete"
|
||||
>
|
||||
批量删除
|
||||
</button>
|
||||
<button
|
||||
class="text-xs text-gray-500 hover:text-gray-700 ml-auto"
|
||||
@click="selectedNames.clear(); selectAll = false"
|
||||
>
|
||||
取消选择
|
||||
</button>
|
||||
<!-- 批量操作工具栏 -->
|
||||
<div v-if="selectedNames.size > 0" class="toolbar batch-toolbar">
|
||||
<span class="batch-text">已选择 {{ selectedNames.size }} 项</span>
|
||||
<div class="batch-actions">
|
||||
<button class="btn-batch btn-pass" @click="batchApprove">批量通过</button>
|
||||
<button class="btn-batch btn-reject" @click="batchReject">批量拒绝</button>
|
||||
<button class="btn-batch btn-delete" @click="batchDelete">批量删除</button>
|
||||
<button class="btn-batch btn-cancel" @click="selectedNames.clear(); selectAll = false">取消选择</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Filter Bar -->
|
||||
<div class="mx-4 mt-2 flex items-center gap-3">
|
||||
<select
|
||||
v-model="filterStatus"
|
||||
class="rounded-md border border-gray-300 px-3 py-1.5 text-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
>
|
||||
<!-- 过滤工具栏 -->
|
||||
<div class="toolbar filter-toolbar">
|
||||
<select v-model="filterStatus" class="filter-select">
|
||||
<option value="">全部状态</option>
|
||||
<option value="PASS">通过</option>
|
||||
<option value="FAIL">失败</option>
|
||||
<option value="PENDING">待审核</option>
|
||||
<option value="REJECTED">已拒绝</option>
|
||||
<option value="FILTERED">已拦截</option>
|
||||
<option value="FALSE_POSITIVE">误报通过</option>
|
||||
</select>
|
||||
<select
|
||||
v-model="filterSentiment"
|
||||
class="rounded-md border border-gray-300 px-3 py-1.5 text-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
>
|
||||
<select v-model="filterSentiment" class="filter-select">
|
||||
<option value="">全部情感</option>
|
||||
<option value="VERY_POSITIVE">非常正面</option>
|
||||
<option value="POSITIVE">正面</option>
|
||||
<option value="NEUTRAL">中性</option>
|
||||
<option value="NEGATIVE">负面</option>
|
||||
<option value="VERY_NEGATIVE">非常负面</option>
|
||||
</select>
|
||||
<input
|
||||
v-model="filterKeyword"
|
||||
type="text"
|
||||
placeholder="搜索回复内容..."
|
||||
class="rounded-md border border-gray-300 px-3 py-1.5 text-sm focus:border-blue-500 focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
<button
|
||||
class="text-xs text-gray-500 hover:text-gray-700"
|
||||
@click="resetFilters"
|
||||
>
|
||||
重置
|
||||
</button>
|
||||
<input v-model="filterKeyword" type="text" placeholder="搜索回复内容..." class="filter-input" />
|
||||
<button class="btn-reset" @click="resetFilters">重置</button>
|
||||
</div>
|
||||
|
||||
<div class="m-4">
|
||||
<!-- 列表区 -->
|
||||
<div class="list-area">
|
||||
<VLoading v-if="loading" />
|
||||
<div v-else-if="replies.length === 0" class="empty-state">暂无记录</div>
|
||||
|
||||
<div v-else-if="replies.length === 0" class="flex flex-col items-center justify-center py-16 text-gray-400">
|
||||
<svg class="w-12 h-12 mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
|
||||
</svg>
|
||||
<span>暂无AI回复记录</span>
|
||||
<div v-else class="reply-list">
|
||||
<div class="select-all-wrap">
|
||||
<input type="checkbox" :checked="selectAll" @change="toggleSelectAll" />
|
||||
<span>全选本页</span>
|
||||
</div>
|
||||
|
||||
<div v-else class="space-y-3">
|
||||
<div v-if="replies.length > 0" class="flex items-center gap-2 mb-2 px-1">
|
||||
<input
|
||||
type="checkbox"
|
||||
:checked="selectAll"
|
||||
@change="toggleSelectAll"
|
||||
class="h-4 w-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500"
|
||||
/>
|
||||
<span class="text-xs text-gray-500">全选</span>
|
||||
<div v-for="reply in replies" :key="reply.metadata.name" class="reply-card">
|
||||
<div class="card-main">
|
||||
<input type="checkbox" :checked="selectedNames.has(reply.metadata.name)" @change="toggleSelect(reply.metadata.name)" />
|
||||
<div class="card-content">
|
||||
<div class="card-header">
|
||||
<div class="tags-wrap">
|
||||
<span class="custom-tag" :class="'tag-' + reply.spec.status">{{ getStatusLabel(reply.spec.status) }}</span>
|
||||
<span class="custom-tag" :class="reply.spec.published ? 'tag-published' : 'tag-draft'">{{ reply.spec.published ? '已发布' : '未发布' }}</span>
|
||||
<span v-if="reply.spec.isAiConversation" class="custom-tag tag-conv">对话</span>
|
||||
<span v-if="reply.spec.sentiment" class="custom-tag" :class="'tag-' + reply.spec.sentiment">{{ getSentimentLabel(reply.spec.sentiment) }}</span>
|
||||
</div>
|
||||
<div
|
||||
v-for="reply in replies"
|
||||
:key="reply.metadata.name"
|
||||
class="bg-white rounded-lg border border-gray-200 overflow-hidden hover:shadow-sm transition-all"
|
||||
>
|
||||
<!-- Card body -->
|
||||
<div class="p-4">
|
||||
<div class="flex items-start gap-3">
|
||||
<input
|
||||
type="checkbox"
|
||||
:checked="selectedNames.has(reply.metadata.name)"
|
||||
@change="toggleSelect(reply.metadata.name)"
|
||||
class="mt-1 h-4 w-4 rounded border-gray-300 text-blue-600 focus:ring-blue-500 shrink-0"
|
||||
/>
|
||||
<div class="flex-1 min-w-0">
|
||||
<!-- Row 1: Status tags + time -->
|
||||
<div class="flex items-center justify-between mb-3">
|
||||
<div class="flex items-center gap-1.5 flex-wrap">
|
||||
<span
|
||||
class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium"
|
||||
:class="getStatusClass(reply.spec.status)"
|
||||
>
|
||||
{{ getStatusLabel(reply.spec.status) }}
|
||||
</span>
|
||||
<span
|
||||
class="inline-flex items-center px-2 py-0.5 rounded text-xs"
|
||||
:class="reply.spec.published ? 'bg-green-50 text-green-600' : 'bg-gray-50 text-gray-400'"
|
||||
>
|
||||
{{ reply.spec.published ? '已发布' : '未发布' }}
|
||||
</span>
|
||||
<span
|
||||
v-if="reply.spec.isAiConversation"
|
||||
class="inline-flex items-center px-2 py-0.5 rounded text-xs bg-blue-50 text-blue-600"
|
||||
>
|
||||
对话
|
||||
</span>
|
||||
<span
|
||||
v-if="reply.spec.sentiment"
|
||||
class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium"
|
||||
:class="getSentimentClass(reply.spec.sentiment)"
|
||||
>
|
||||
{{ getSentimentLabel(reply.spec.sentiment) }}
|
||||
<span class="card-time">{{ formatDate(reply.metadata.creationTimestamp) }}</span>
|
||||
</div>
|
||||
<div class="card-text">{{ stripHtml(reply.spec.reply) || '(空)' }}</div>
|
||||
<div v-if="reply.spec.status === 'FILTERED'" class="card-filter-reason">
|
||||
<svg class="filter-icon" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/></svg>
|
||||
<span class="filter-category" v-if="reply.spec.filterCategory">{{ reply.spec.filterCategory }}</span>
|
||||
<span class="filter-detail">{{ reply.spec.filterReason || '未提供具体原因' }}</span>
|
||||
<button class="btn-false-positive" @click="openFalsePositiveDialog(reply)">误报反馈</button>
|
||||
</div>
|
||||
<div v-if="reply.spec.status === 'FALSE_POSITIVE'" class="card-filter-reason">
|
||||
<svg class="filter-icon fp-icon-ok" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/></svg>
|
||||
<span class="filter-category">误报</span>
|
||||
<span class="filter-detail">{{ reply.spec.filterReason || '用户确认为误报' }}</span>
|
||||
<button class="btn-trigger-ai" :disabled="triggerAiLoadingName === reply.metadata.name" @click="handleTriggerAiReply(reply)">
|
||||
<span v-if="triggerAiLoadingName === reply.metadata.name" class="fp-spinner"></span>
|
||||
触发AI回复
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-footer">
|
||||
<div class="footer-info">
|
||||
<span>评分: <strong>{{ reply.spec.score }}</strong></span>
|
||||
<span v-if="reply.spec.postSlug">
|
||||
关联: <a :href="getPostUrl(reply.spec.postSlug)" target="_blank" class="post-link">{{ reply.spec.postSlug }}</a>
|
||||
</span>
|
||||
<span v-if="reply.spec.retryCount > 0" class="retry-text">重试 {{ reply.spec.retryCount }} 次</span>
|
||||
</div>
|
||||
<span class="text-xs text-gray-400 flex-shrink-0 ml-2">{{ formatDate(reply.metadata.creationTimestamp) }}</span>
|
||||
</div>
|
||||
|
||||
<!-- Row 2: AI reply content (truncated) -->
|
||||
<div class="text-sm text-gray-800 leading-relaxed break-words line-clamp-3 mt-2">
|
||||
{{ stripHtml(reply.spec.reply) || '(空)' }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Card footer: meta info + actions -->
|
||||
<div class="px-4 py-2.5 bg-gray-50 border-t border-gray-100 flex items-center justify-between">
|
||||
<div class="flex items-center gap-4 text-xs text-gray-400">
|
||||
<span>
|
||||
评分 <span :class="getScoreClass(reply.spec.score)" class="font-medium text-gray-600">{{ reply.spec.score }}</span>
|
||||
</span>
|
||||
<span v-if="reply.spec.postSlug" class="flex items-center gap-1">
|
||||
文章
|
||||
<a
|
||||
:href="getPostUrl(reply.spec.postSlug)"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
class="text-blue-500 hover:text-blue-700 hover:underline"
|
||||
>{{ reply.spec.postSlug }}</a>
|
||||
<svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14" />
|
||||
</svg>
|
||||
</span>
|
||||
<span v-if="reply.spec.retryCount > 0">
|
||||
重试 <span class="text-gray-600">{{ reply.spec.retryCount }}</span> 次
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<div class="footer-actions">
|
||||
<template v-if="reply.spec.status === 'PASS' && !reply.spec.published">
|
||||
<button
|
||||
class="inline-flex items-center gap-1 text-xs text-green-600 hover:text-green-800 transition-colors px-2 py-1 rounded hover:bg-green-50"
|
||||
@click="handleApprove(reply.metadata.name)"
|
||||
>
|
||||
审核通过
|
||||
</button>
|
||||
<button
|
||||
class="inline-flex items-center gap-1 text-xs text-red-500 hover:text-red-700 transition-colors px-2 py-1 rounded hover:bg-red-50"
|
||||
@click="handleReject(reply.metadata.name)"
|
||||
>
|
||||
拒绝
|
||||
</button>
|
||||
<button class="action-btn pass" @click="handleApprove(reply.metadata.name)">通过</button>
|
||||
<button class="action-btn reject" @click="handleReject(reply.metadata.name)">拒绝</button>
|
||||
</template>
|
||||
<button
|
||||
class="inline-flex items-center gap-1 text-xs text-blue-500 hover:text-blue-700 transition-colors px-2 py-1 rounded hover:bg-blue-50"
|
||||
@click="openConversation(reply)"
|
||||
>
|
||||
<svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
|
||||
</svg>
|
||||
查看
|
||||
</button>
|
||||
<button
|
||||
class="text-xs text-red-400 hover:text-red-600 transition-colors px-2 py-1 rounded hover:bg-red-50"
|
||||
@click="handleDelete(reply.metadata.name)"
|
||||
>
|
||||
删除
|
||||
</button>
|
||||
<button class="action-btn view" @click="openConversation(reply)">查看对话</button>
|
||||
<button class="action-btn delete" @click="handleDelete(reply.metadata.name)">删除</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Pagination -->
|
||||
<div v-if="totalPages > 1" class="flex items-center justify-between mt-4 px-1">
|
||||
<span class="text-xs text-gray-400">共 {{ total }} 条</span>
|
||||
<div class="flex gap-2">
|
||||
<div v-if="totalPages > 1" class="pagination">
|
||||
<span>共 {{ total }} 条</span>
|
||||
<div class="pagination-btns">
|
||||
<VButton size="sm" :disabled="page <= 1" @click="page--">上一页</VButton>
|
||||
<VButton size="sm" :disabled="page >= totalPages" @click="page++">下一页</VButton>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Conversation Dialog -->
|
||||
<!-- 完整对话弹窗 -->
|
||||
<teleport to="body">
|
||||
<div
|
||||
v-if="showDialog"
|
||||
class="fixed inset-0 z-[9999] flex items-center justify-center"
|
||||
>
|
||||
<!-- Overlay -->
|
||||
<div
|
||||
class="absolute inset-0 bg-black/40"
|
||||
@click="showDialog = false"
|
||||
></div>
|
||||
|
||||
<!-- Dialog -->
|
||||
<div class="relative bg-white rounded-xl shadow-2xl w-full max-w-2xl max-h-[80vh] mx-4 flex flex-col overflow-hidden">
|
||||
<!-- Dialog header -->
|
||||
<div class="flex items-center justify-between px-5 py-4 border-b border-gray-100">
|
||||
<div class="flex items-center gap-2">
|
||||
<svg class="w-5 h-5 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
|
||||
</svg>
|
||||
<h3 class="text-base font-semibold text-gray-800">完整对话</h3>
|
||||
<div v-if="showDialog" class="dialog-overlay" @click.self="showDialog = false">
|
||||
<div class="dialog-box">
|
||||
<div class="dialog-header">
|
||||
<h3>对话上下文</h3>
|
||||
<button class="close-btn" @click="showDialog = false"><svg fill="none" stroke="currentColor" viewBox="0 0 24 24" width="24" height="24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg></button>
|
||||
</div>
|
||||
<button
|
||||
class="text-gray-400 hover:text-gray-600 transition-colors p-1 rounded-lg hover:bg-gray-100"
|
||||
@click="showDialog = false"
|
||||
>
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- Dialog body: conversation -->
|
||||
<div class="flex-1 overflow-y-auto px-5 py-4 space-y-4">
|
||||
<div class="dialog-body">
|
||||
<VLoading v-if="conversationLoading" />
|
||||
<div v-else-if="conversationMessages.length === 0" class="empty-state">暂无内容</div>
|
||||
<div v-else class="chat-container">
|
||||
<div v-for="(msg, idx) in conversationMessages" :key="idx" class="chat-row" :class="msg.isAi ? 'row-ai' : 'row-user'">
|
||||
<div class="chat-message">
|
||||
<div class="chat-owner">{{ msg.owner }}</div>
|
||||
<div class="chat-bubble" :class="msg.isAi ? 'bubble-ai' : 'bubble-user'">
|
||||
|
||||
<div v-else-if="conversationMessages.length === 0" class="text-center text-gray-400 py-8">
|
||||
暂无对话内容
|
||||
<!-- 现代化的精美引用框 (无左边框) -->
|
||||
<div v-if="msg.quoteOwner && msg.quoteContent" class="quote-box">
|
||||
<div class="quote-header">
|
||||
<svg class="quote-icon" fill="currentColor" viewBox="0 0 24 24"><path d="M10 9V5l-7 7 7 7v-4.1c5 0 8.5 1.6 11 5.1-1-5-4-10-11-11z"/></svg>
|
||||
<span class="quote-owner">{{ msg.quoteOwner }}</span>
|
||||
</div>
|
||||
<div class="quote-content">{{ truncateQuote(msg.quoteContent) }}</div>
|
||||
</div>
|
||||
|
||||
<div v-else>
|
||||
<div
|
||||
v-for="(msg, idx) in conversationMessages"
|
||||
:key="idx"
|
||||
class="flex"
|
||||
:class="msg.isAi ? 'justify-start' : 'justify-end'"
|
||||
>
|
||||
<div class="max-w-[80%]">
|
||||
<!-- Owner name -->
|
||||
<div
|
||||
class="text-xs mb-1"
|
||||
:class="msg.isAi ? 'text-blue-500' : 'text-gray-500'"
|
||||
>
|
||||
<span class="font-medium">{{ msg.owner }}</span>
|
||||
<div class="chat-text" v-html="renderContent(msg.content)"></div>
|
||||
</div>
|
||||
<!-- Bubble -->
|
||||
<div
|
||||
class="rounded-2xl px-4 py-2.5 text-sm leading-relaxed break-words"
|
||||
:class="msg.isAi
|
||||
? 'bg-blue-50 text-gray-800 rounded-tl-md'
|
||||
: 'bg-gray-100 text-gray-800 rounded-tr-md'"
|
||||
v-html="renderContent(msg.content)"
|
||||
></div>
|
||||
<!-- Time -->
|
||||
<div class="text-[10px] text-gray-300 mt-1" :class="msg.isAi ? 'text-left' : 'text-right'">
|
||||
{{ formatDate(msg.time) }}
|
||||
<div class="chat-time">{{ formatDate(msg.time) }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</teleport>
|
||||
|
||||
<!-- Dialog footer -->
|
||||
<div class="px-5 py-3 border-t border-gray-100 flex justify-end">
|
||||
<button
|
||||
class="px-4 py-1.5 text-sm text-gray-600 bg-gray-100 hover:bg-gray-200 rounded-lg transition-colors"
|
||||
@click="showDialog = false"
|
||||
>
|
||||
关闭
|
||||
<!-- 误报反馈确认弹窗 -->
|
||||
<teleport to="body">
|
||||
<div v-if="showFalsePositiveDialog" class="dialog-overlay" @click.self="showFalsePositiveDialog = false">
|
||||
<div class="dialog-box fp-dialog">
|
||||
<div class="dialog-header">
|
||||
<h3>确认为误报?</h3>
|
||||
<button class="close-btn" @click="showFalsePositiveDialog = false"><svg fill="none" stroke="currentColor" viewBox="0 0 24 24" width="24" height="24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg></button>
|
||||
</div>
|
||||
<div class="fp-dialog-body">
|
||||
<p class="fp-desc">系统检测到该评论可能包含违规内容,但您认为这是正常表达。请选择处理方式:</p>
|
||||
<div class="fp-actions">
|
||||
<button class="fp-btn fp-btn-primary" :disabled="fpLoading" @click="handleFalsePositive('aiReply')">
|
||||
<span v-if="fpLoading" class="fp-spinner"></span>
|
||||
AI 回复
|
||||
</button>
|
||||
<button class="fp-btn fp-btn-secondary" :disabled="fpLoading" @click="handleFalsePositive('approveOnly')">
|
||||
仅通过
|
||||
</button>
|
||||
<button class="fp-btn fp-btn-ghost" :disabled="fpLoading" @click="showFalsePositiveDialog = false">
|
||||
取消
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -303,341 +179,228 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, watch } from "vue"
|
||||
import { ref, onMounted, onUnmounted, watch } from "vue"
|
||||
import { axiosInstance } from "@halo-dev/api-client"
|
||||
import { VPageHeader, VButton, VLoading, Toast } from "@halo-dev/components"
|
||||
import { IconPlug } from "@halo-dev/components"
|
||||
|
||||
interface AiCommentReplyItem {
|
||||
metadata: {
|
||||
name: string
|
||||
creationTimestamp: string
|
||||
}
|
||||
spec: {
|
||||
commentId: string
|
||||
postId: string
|
||||
postSlug: string
|
||||
reply: string
|
||||
score: number
|
||||
status: string
|
||||
retryCount: number
|
||||
replyTo: string
|
||||
isAiConversation: boolean
|
||||
published: boolean
|
||||
sentiment: string | null
|
||||
}
|
||||
}
|
||||
interface AiCommentReplyItem { metadata: { name: string; creationTimestamp: string }; spec: any }
|
||||
interface ConversationMessage { type: string; owner: string; content: string; time: string; isAi: boolean; quoteOwner?: string; quoteContent?: string }
|
||||
|
||||
interface ConversationMessage {
|
||||
type: string
|
||||
owner: string
|
||||
content: string
|
||||
time: string
|
||||
isAi: boolean
|
||||
}
|
||||
const replies = ref<AiCommentReplyItem[]>([]); const loading = ref(false); const batchLoading = ref(false); const page = ref(1); const size = ref(20); const total = ref(0); const totalPages = ref(0);
|
||||
const selectedNames = ref<Set<string>>(new Set()); const selectAll = ref(false);
|
||||
const filterStatus = ref(""); const filterSentiment = ref(""); const filterKeyword = ref("");
|
||||
const showDialog = ref(false); const conversationLoading = ref(false); const conversationMessages = ref<ConversationMessage[]>([]);
|
||||
const showFalsePositiveDialog = ref(false); const falsePositiveTarget = ref<AiCommentReplyItem | null>(null); const fpLoading = ref(false);
|
||||
const triggerAiLoadingName = ref<string | null>(null);
|
||||
|
||||
const replies = ref<AiCommentReplyItem[]>([])
|
||||
const loading = ref(false)
|
||||
const page = ref(1)
|
||||
const size = ref(20)
|
||||
const total = ref(0)
|
||||
const totalPages = ref(0)
|
||||
|
||||
// Selection state
|
||||
const selectedNames = ref<Set<string>>(new Set())
|
||||
const selectAll = ref(false)
|
||||
|
||||
// Filter state
|
||||
const filterStatus = ref("")
|
||||
const filterSentiment = ref("")
|
||||
const filterKeyword = ref("")
|
||||
|
||||
const toggleSelect = (name: string) => {
|
||||
if (selectedNames.value.has(name)) {
|
||||
selectedNames.value.delete(name)
|
||||
} else {
|
||||
selectedNames.value.add(name)
|
||||
}
|
||||
// Update selectAll state
|
||||
selectAll.value = replies.value.length > 0 && replies.value.every(r => selectedNames.value.has(r.metadata.name))
|
||||
}
|
||||
|
||||
const toggleSelectAll = () => {
|
||||
if (selectAll.value) {
|
||||
selectedNames.value.clear()
|
||||
selectAll.value = false
|
||||
} else {
|
||||
selectedNames.value = new Set(replies.value.map(r => r.metadata.name))
|
||||
selectAll.value = true
|
||||
}
|
||||
}
|
||||
|
||||
// Conversation dialog state
|
||||
const showDialog = ref(false)
|
||||
const conversationLoading = ref(false)
|
||||
const conversationMessages = ref<ConversationMessage[]>([])
|
||||
const toggleSelect = (name: string) => { selectedNames.value.has(name) ? selectedNames.value.delete(name) : selectedNames.value.add(name); selectAll.value = replies.value.length > 0 && replies.value.every(r => selectedNames.value.has(r.metadata.name)) }
|
||||
const toggleSelectAll = () => { if (selectAll.value) { selectedNames.value.clear(); selectAll.value = false } else { selectedNames.value = new Set(replies.value.map(r => r.metadata.name)); selectAll.value = true } }
|
||||
|
||||
const fetchReplies = async () => {
|
||||
loading.value = true
|
||||
loading.value = true;
|
||||
try {
|
||||
const params: Record<string, string | number> = { page: page.value, size: size.value }
|
||||
if (filterStatus.value) params.status = filterStatus.value
|
||||
if (filterSentiment.value) params.sentiment = filterSentiment.value
|
||||
if (filterKeyword.value) params.keyword = filterKeyword.value
|
||||
const { data } = await axiosInstance.get(
|
||||
"/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies",
|
||||
{ params },
|
||||
)
|
||||
replies.value = data.items || []
|
||||
total.value = data.total || 0
|
||||
totalPages.value = Math.ceil(total.value / size.value)
|
||||
} catch (e) {
|
||||
console.error("Failed to fetch replies", e)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
const params: any = { page: page.value, size: size.value }
|
||||
if (filterStatus.value) params.status = filterStatus.value; if (filterSentiment.value) params.sentiment = filterSentiment.value; if (filterKeyword.value) params.keyword = filterKeyword.value;
|
||||
const { data } = await axiosInstance.get("/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies", { params })
|
||||
replies.value = data.items || []; total.value = data.total || 0; totalPages.value = Math.ceil(total.value / size.value)
|
||||
} catch (e) { Toast.error("获取数据失败") } finally { loading.value = false }
|
||||
}
|
||||
|
||||
const openConversation = async (reply: AiCommentReplyItem) => {
|
||||
showDialog.value = true
|
||||
conversationLoading.value = true
|
||||
conversationMessages.value = []
|
||||
showDialog.value = true; conversationLoading.value = true; conversationMessages.value = []
|
||||
try {
|
||||
const { data } = await axiosInstance.get(
|
||||
`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/conversation/${reply.spec.commentId}`,
|
||||
)
|
||||
const { data } = await axiosInstance.get(`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/conversation/${reply.spec.commentId}`)
|
||||
conversationMessages.value = data.messages || []
|
||||
} catch (e) {
|
||||
console.error("Failed to fetch conversation", e)
|
||||
Toast.error("获取对话失败")
|
||||
} finally {
|
||||
conversationLoading.value = false
|
||||
}
|
||||
} catch (e) { Toast.error("获取对话失败") } finally { conversationLoading.value = false }
|
||||
}
|
||||
|
||||
const handleDelete = async (name: string) => {
|
||||
try {
|
||||
await axiosInstance.delete(
|
||||
`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/${name}`,
|
||||
)
|
||||
Toast.success("删除成功")
|
||||
fetchReplies()
|
||||
} catch (e) {
|
||||
console.error("Failed to delete reply", e)
|
||||
Toast.error("删除失败")
|
||||
}
|
||||
const handleDelete = async (name: string) => { try { await axiosInstance.delete(`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/${name}`); Toast.success("删除成功"); fetchReplies() } catch (e) { Toast.error("删除失败") } }
|
||||
const handleApprove = async (name: string) => { try { await axiosInstance.post(`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/${name}/approve`); Toast.success("审核通过"); fetchReplies() } catch (e) { Toast.error("审核失败") } }
|
||||
const handleReject = async (name: string) => { try { await axiosInstance.post(`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/${name}/reject`); Toast.success("已拒绝"); fetchReplies() } catch (e) { Toast.error("拒绝失败") } }
|
||||
const batchApprove = async () => { if(!selectedNames.value.size||batchLoading.value) return; batchLoading.value=true; try { await axiosInstance.post("/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/batch-approve", { names: Array.from(selectedNames.value) }); Toast.success("成功"); selectedNames.value.clear(); selectAll.value=false; fetchReplies() } catch(e) { Toast.error("失败") } finally { batchLoading.value=false } }
|
||||
const batchReject = async () => { if(!selectedNames.value.size||batchLoading.value) return; batchLoading.value=true; try { await axiosInstance.post("/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/batch-reject", { names: Array.from(selectedNames.value) }); Toast.success("成功"); selectedNames.value.clear(); selectAll.value=false; fetchReplies() } catch(e) { Toast.error("失败") } finally { batchLoading.value=false } }
|
||||
const batchDelete = async () => { if(!selectedNames.value.size||batchLoading.value) return; batchLoading.value=true; try { await axiosInstance.post("/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/batch-delete", { names: Array.from(selectedNames.value) }); Toast.success("成功"); selectedNames.value.clear(); selectAll.value=false; fetchReplies() } catch(e) { Toast.error("失败") } finally { batchLoading.value=false } }
|
||||
|
||||
const getStatusLabel = (s: string) => { const m:any = { PASS: '通过', FAIL: '失败', PENDING: '待审', REJECTED: '拒绝', FILTERED: '已拦截', FALSE_POSITIVE: '误报通过' }; return m[s] || s }
|
||||
const getSentimentLabel = (s: string) => { const m:any = { VERY_POSITIVE: '极好', POSITIVE: '正面', NEUTRAL: '中性', NEGATIVE: '负面', VERY_NEGATIVE: '极差' }; return m[s] || s }
|
||||
const formatDate = (ts: string) => ts ? new Date(ts).toLocaleString("zh-CN") : ""
|
||||
const getPostUrl = (slug: string) => `${window.location.origin}/archives/${slug}`
|
||||
const stripHtml = (html: string) => html ? html.replace(/<[^>]+>/g, "").replace(/\n+/g, " ").trim() : ""
|
||||
|
||||
const truncateQuote = (content: string, length = 35) => {
|
||||
if (!content) return ""
|
||||
let plain = stripHtml(content)
|
||||
plain = plain.replace(/^>\s*(?:💬\s*)?\*\*(.*?)\*\*\s*[::]\s*/gm, '').trim()
|
||||
return plain.length > length ? plain.substring(0, length) + "..." : plain
|
||||
}
|
||||
|
||||
const handleApprove = async (name: string) => {
|
||||
try {
|
||||
await axiosInstance.post(
|
||||
`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/${name}/approve`,
|
||||
)
|
||||
Toast.success("审核通过")
|
||||
fetchReplies()
|
||||
} catch (e) {
|
||||
console.error("Failed to approve reply", e)
|
||||
Toast.error("审核操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
const handleReject = async (name: string) => {
|
||||
try {
|
||||
await axiosInstance.post(
|
||||
`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/${name}/reject`,
|
||||
)
|
||||
Toast.success("已拒绝")
|
||||
fetchReplies()
|
||||
} catch (e) {
|
||||
console.error("Failed to reject reply", e)
|
||||
Toast.error("拒绝操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
const batchApprove = async () => {
|
||||
if (selectedNames.value.size === 0) return
|
||||
try {
|
||||
await axiosInstance.post(
|
||||
"/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/batch-approve",
|
||||
{ names: Array.from(selectedNames.value) }
|
||||
)
|
||||
Toast.success("批量审核通过成功")
|
||||
selectedNames.value.clear()
|
||||
selectAll.value = false
|
||||
fetchReplies()
|
||||
} catch (e) {
|
||||
Toast.error("批量审核操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
const batchReject = async () => {
|
||||
if (selectedNames.value.size === 0) return
|
||||
try {
|
||||
await axiosInstance.post(
|
||||
"/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/batch-reject",
|
||||
{ names: Array.from(selectedNames.value) }
|
||||
)
|
||||
Toast.success("批量拒绝成功")
|
||||
selectedNames.value.clear()
|
||||
selectAll.value = false
|
||||
fetchReplies()
|
||||
} catch (e) {
|
||||
Toast.error("批量拒绝操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
const batchDelete = async () => {
|
||||
if (selectedNames.value.size === 0) return
|
||||
try {
|
||||
await axiosInstance.post(
|
||||
"/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/batch-delete",
|
||||
{ names: Array.from(selectedNames.value) }
|
||||
)
|
||||
Toast.success("批量删除成功")
|
||||
selectedNames.value.clear()
|
||||
selectAll.value = false
|
||||
fetchReplies()
|
||||
} catch (e) {
|
||||
Toast.error("批量删除操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
const getScoreClass = (score: number) => {
|
||||
if (score >= 85) return "text-green-600"
|
||||
if (score >= 60) return "text-yellow-600"
|
||||
return "text-red-600"
|
||||
}
|
||||
|
||||
const getStatusClass = (status: string) => {
|
||||
switch (status) {
|
||||
case "PASS":
|
||||
return "bg-green-100 text-green-700"
|
||||
case "FAIL":
|
||||
return "bg-red-100 text-red-700"
|
||||
case "PENDING":
|
||||
return "bg-gray-100 text-gray-700"
|
||||
case "REJECTED":
|
||||
return "bg-red-100 text-red-700"
|
||||
default:
|
||||
return "bg-gray-100 text-gray-700"
|
||||
}
|
||||
}
|
||||
|
||||
const getStatusLabel = (status: string) => {
|
||||
switch (status) {
|
||||
case "PASS":
|
||||
return "通过"
|
||||
case "FAIL":
|
||||
return "失败"
|
||||
case "PENDING":
|
||||
return "待处理"
|
||||
case "REJECTED":
|
||||
return "已拒绝"
|
||||
default:
|
||||
return status
|
||||
}
|
||||
}
|
||||
|
||||
const getSentimentClass = (sentiment: string) => {
|
||||
switch (sentiment) {
|
||||
case "POSITIVE":
|
||||
return "bg-green-50 text-green-600"
|
||||
case "NEGATIVE":
|
||||
return "bg-red-50 text-red-600"
|
||||
case "NEUTRAL":
|
||||
return "bg-gray-100 text-gray-600"
|
||||
default:
|
||||
return "bg-gray-100 text-gray-600"
|
||||
}
|
||||
}
|
||||
|
||||
const getSentimentLabel = (sentiment: string) => {
|
||||
switch (sentiment) {
|
||||
case "POSITIVE":
|
||||
return "正面"
|
||||
case "NEGATIVE":
|
||||
return "负面"
|
||||
case "NEUTRAL":
|
||||
return "中性"
|
||||
default:
|
||||
return sentiment
|
||||
}
|
||||
}
|
||||
|
||||
const formatDate = (timestamp: string) => {
|
||||
if (!timestamp) return ""
|
||||
return new Date(timestamp).toLocaleString("zh-CN")
|
||||
}
|
||||
|
||||
const getPostUrl = (slug: string) => {
|
||||
return `${window.location.origin}/archives/${slug}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip HTML tags for plain text display (card preview)
|
||||
*/
|
||||
const stripHtml = (html: string) => {
|
||||
if (!html) return ""
|
||||
return html
|
||||
.replace(/<p[^>]*>/gi, "")
|
||||
.replace(/<\/p>/gi, "\n")
|
||||
.replace(/<br\s*\/?>/gi, "\n")
|
||||
.replace(/<[^>]+>/g, "")
|
||||
.replace(/\n{3,}/g, "\n\n")
|
||||
.trim()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize and render HTML content for conversation bubbles.
|
||||
* Only allows safe inline tags, strips dangerous elements.
|
||||
*/
|
||||
const renderContent = (content: string) => {
|
||||
if (!content) return "<span class='text-gray-400'>(空)</span>"
|
||||
return content
|
||||
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "")
|
||||
.replace(/<iframe[^>]*>[\s\S]*?<\/iframe>/gi, "")
|
||||
.replace(/<object[^>]*>[\s\S]*?<\/object>/gi, "")
|
||||
.replace(/<embed[^>]*>/gi, "")
|
||||
.replace(/<form[^>]*>[\s\S]*?<\/form>/gi, "")
|
||||
.replace(/on\w+\s*=\s*["'][^"']*["']/gi, "")
|
||||
.replace(/on\w+\s*=\s*[^\s>]*/gi, "")
|
||||
.replace(/<p[^>]*>/gi, "<p style='margin:0 0 0.5em 0'>")
|
||||
.replace(/<a /gi, "<a target='_blank' rel='noopener noreferrer' ")
|
||||
if (!content) return "<span style='opacity:0.5'>(空)</span>"
|
||||
let parsed = content.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, "").replace(/<iframe[^>]*>[\s\S]*?<\/iframe>/gi, "")
|
||||
parsed = parsed.replace(/^>\s*(?:💬\s*)?\*\*(.*?)\*\*\s*[::]\s*/gm, "")
|
||||
return parsed.replace(/\n/g, "<br/>")
|
||||
}
|
||||
|
||||
const resetFilters = () => {
|
||||
filterStatus.value = ""
|
||||
filterSentiment.value = ""
|
||||
filterKeyword.value = ""
|
||||
page.value = 1
|
||||
const resetFilters = () => { filterStatus.value = ""; filterSentiment.value = ""; filterKeyword.value = ""; page.value = 1; fetchReplies() }
|
||||
|
||||
const openFalsePositiveDialog = (reply: AiCommentReplyItem) => { falsePositiveTarget.value = reply; showFalsePositiveDialog.value = true }
|
||||
const handleFalsePositive = async (action: string) => {
|
||||
if (!falsePositiveTarget.value) return
|
||||
fpLoading.value = true
|
||||
try {
|
||||
await axiosInstance.post(`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/${falsePositiveTarget.value.metadata.name}/false-positive`, { action })
|
||||
Toast.success(action === "aiReply" ? "已标记为误报,AI回复正在后台生成" : "已标记为误报并通过")
|
||||
showFalsePositiveDialog.value = false
|
||||
falsePositiveTarget.value = null
|
||||
fetchReplies()
|
||||
} catch (e: any) {
|
||||
Toast.error(e?.response?.data?.message || "操作失败")
|
||||
} finally { fpLoading.value = false }
|
||||
}
|
||||
|
||||
watch([filterStatus, filterSentiment, filterKeyword], () => {
|
||||
page.value = 1
|
||||
const handleTriggerAiReply = async (reply: AiCommentReplyItem) => {
|
||||
if (triggerAiLoadingName.value) return
|
||||
triggerAiLoadingName.value = reply.metadata.name
|
||||
try {
|
||||
await axiosInstance.post(`/apis/console.api.comment-ai-autopilot.nxxy335.top/v1alpha1/replies/${reply.metadata.name}/false-positive`, { action: "aiReply" })
|
||||
Toast.success("AI回复正在后台生成")
|
||||
fetchReplies()
|
||||
} catch (e: any) {
|
||||
Toast.error(e?.response?.data?.message || "触发失败")
|
||||
} finally { triggerAiLoadingName.value = null }
|
||||
}
|
||||
// 状态/情感筛选立即触发;关键词输入防抖 300ms 避免每次按键都请求
|
||||
watch([filterStatus, filterSentiment], () => { page.value = 1; fetchReplies() })
|
||||
let keywordDebounceTimer: ReturnType<typeof setTimeout> | null = null
|
||||
watch(filterKeyword, () => {
|
||||
if (keywordDebounceTimer) clearTimeout(keywordDebounceTimer)
|
||||
keywordDebounceTimer = setTimeout(() => { page.value = 1; fetchReplies() }, 300)
|
||||
})
|
||||
|
||||
watch(page, () => {
|
||||
selectedNames.value.clear()
|
||||
selectAll.value = false
|
||||
fetchReplies()
|
||||
})
|
||||
|
||||
watch(page, () => { selectedNames.value.clear(); selectAll.value = false; fetchReplies() })
|
||||
onMounted(fetchReplies)
|
||||
onUnmounted(() => { if (keywordDebounceTimer) clearTimeout(keywordDebounceTimer) })
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.line-clamp-2 {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
.line-clamp-3 {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 3;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
/* 全局基础布局 */
|
||||
.logs-container { padding-bottom: 20px; }
|
||||
.header-icon { margin-right: 8px; align-self: center; }
|
||||
|
||||
/* 响应式工具栏 */
|
||||
.toolbar { display: flex; flex-direction: column; gap: 12px; margin: 16px; align-items: stretch; }
|
||||
@media (min-width: 768px) { .toolbar { flex-direction: row; align-items: center; } }
|
||||
.batch-toolbar { background: #eff6ff; padding: 12px 16px; border-radius: 8px; border: 1px solid #bfdbfe; }
|
||||
.batch-text { font-size: 14px; color: #1d4ed8; font-weight: bold; }
|
||||
.batch-actions { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
|
||||
@media (min-width: 768px) { .batch-actions { width: auto; margin-left: auto; } }
|
||||
.btn-batch { padding: 6px 12px; border-radius: 6px; border: none; font-size: 12px; cursor: pointer; color: #fff; white-space: nowrap; }
|
||||
.btn-pass { background: #16a34a; } .btn-reject { background: #f97316; } .btn-delete { background: #dc2626; }
|
||||
.btn-cancel { background: transparent; color: #6b7280; border: 1px solid #d1d5db; }
|
||||
.filter-select, .filter-input { width: 100%; padding: 8px 12px; border: 1px solid #e5e7eb; border-radius: 6px; font-size: 14px; outline: none; }
|
||||
@media (min-width: 768px) { .filter-select { width: auto; min-width: 120px; } .filter-input { flex: 1; } }
|
||||
.btn-reset { padding: 8px 16px; border: 1px solid #e5e7eb; border-radius: 6px; background: #f9fafb; cursor: pointer; white-space: nowrap; width: 100%; }
|
||||
@media (min-width: 768px) { .btn-reset { width: auto; } }
|
||||
|
||||
/* 列表区 */
|
||||
.list-area { margin: 16px; }
|
||||
.empty-state { text-align: center; padding: 60px 0; color: #9ca3af; font-size: 14px; }
|
||||
.reply-list { display: flex; flex-direction: column; gap: 16px; }
|
||||
.select-all-wrap { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #6b7280; padding: 0 4px; }
|
||||
.reply-card { background: #fff; border: 1px solid #e5e7eb; border-radius: 12px; overflow: hidden; box-shadow: 0 1px 2px rgba(0,0,0,0.02); }
|
||||
.card-main { display: flex; gap: 12px; padding: 16px; align-items: flex-start; }
|
||||
.card-main input { margin-top: 4px; }
|
||||
.card-content { flex: 1; min-width: 0; }
|
||||
.card-header { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
|
||||
@media (min-width: 640px) { .card-header { flex-direction: row; justify-content: space-between; align-items: center; } }
|
||||
.tags-wrap { display: flex; gap: 6px; flex-wrap: wrap; }
|
||||
.card-time { font-size: 12px; color: #9ca3af; }
|
||||
.card-text { font-size: 14px; color: #374151; line-height: 1.6; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
|
||||
.card-filter-reason { display: flex; align-items: flex-start; gap: 6px; margin-top: 8px; padding: 6px 10px; background: #fef3c7; border: 1px solid #fde68a; border-radius: 6px; font-size: 12px; color: #92400e; }
|
||||
.filter-icon { width: 14px; height: 14px; flex-shrink: 0; margin-top: 1px; }
|
||||
.filter-category { flex-shrink: 0; padding: 1px 6px; background: #b45309; color: #fff; border-radius: 3px; font-weight: 600; font-size: 11px; line-height: 1.5; }
|
||||
.filter-detail { flex: 1; line-height: 1.5; }
|
||||
.btn-false-positive { flex-shrink: 0; margin-left: auto; padding: 2px 8px; border: 1px solid #b45309; border-radius: 4px; background: transparent; color: #b45309; font-size: 11px; cursor: pointer; white-space: nowrap; transition: all 0.15s; }
|
||||
.btn-false-positive:hover { background: #b45309; color: #fff; }
|
||||
.fp-icon-ok { color: #16a34a; }
|
||||
.btn-trigger-ai { flex-shrink: 0; margin-left: auto; padding: 2px 8px; border: 1px solid #2563eb; border-radius: 4px; background: transparent; color: #2563eb; font-size: 11px; cursor: pointer; white-space: nowrap; transition: all 0.15s; display: inline-flex; align-items: center; gap: 4px; }
|
||||
.btn-trigger-ai:hover:not(:disabled) { background: #2563eb; color: #fff; }
|
||||
.btn-trigger-ai:disabled { opacity: 0.6; cursor: not-allowed; }
|
||||
.btn-trigger-ai .fp-spinner { width: 11px; height: 11px; border-color: rgba(37,99,235,0.3); border-top-color: #2563eb; }
|
||||
.card-footer { display: flex; flex-direction: column; gap: 12px; padding: 12px 16px; background: #f9fafb; border-top: 1px solid #f3f4f6; }
|
||||
@media (min-width: 640px) { .card-footer { flex-direction: row; justify-content: space-between; align-items: center; } }
|
||||
.footer-info { font-size: 12px; color: #6b7280; display: flex; flex-wrap: wrap; gap: 12px; }
|
||||
.post-link { color: #3b82f6; text-decoration: none; } .post-link:hover { text-decoration: underline; }
|
||||
.retry-text { color: #f59e0b; }
|
||||
.footer-actions { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; justify-content: flex-end; }
|
||||
@media (min-width: 640px) { .footer-actions { width: auto; } }
|
||||
.action-btn { padding: 4px 10px; border-radius: 4px; font-size: 12px; border: none; cursor: pointer; white-space: nowrap; }
|
||||
.action-btn.pass { background: #dcfce7; color: #16a34a; }
|
||||
.action-btn.reject { background: #fee2e2; color: #dc2626; }
|
||||
.action-btn.view { background: #dbeafe; color: #2563eb; }
|
||||
.action-btn.delete { background: #e5e7eb; color: #4b5563; }
|
||||
|
||||
/* 标签体系 */
|
||||
.custom-tag { padding: 2px 6px; border-radius: 4px; font-size: 11px; font-weight: bold; }
|
||||
.tag-PASS { background: #dcfce7; color: #15803d; } .tag-FAIL { background: #fee2e2; color: #b91c1c; } .tag-PENDING { background: #fef9c3; color: #a16207; } .tag-REJECTED { background: #ffedd5; color: #c2410c; } .tag-FILTERED { background: #f1f5f9; color: #b45309; border: 1px solid #fde68a; } .tag-FALSE_POSITIVE { background: #dbeafe; color: #1d4ed8; border: 1px solid #93c5fd; }
|
||||
.tag-published { background: #dbeafe; color: #1d4ed8; } .tag-draft { background: #f3f4f6; color: #4b5563; }
|
||||
.tag-conv { background: #f3e8ff; color: #7e22ce; }
|
||||
.tag-VERY_POSITIVE { background: #dcfce7; color: #14532d; } .tag-POSITIVE { background: #ecfdf5; color: #15803d; } .tag-NEUTRAL { background: #f3f4f6; color: #4b5563; } .tag-NEGATIVE { background: #ffe4e6; color: #e11d48; } .tag-VERY_NEGATIVE { background: #fee2e2; color: #991b1b; }
|
||||
|
||||
/* 对话弹窗与响应式气泡 */
|
||||
.dialog-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 9999; backdrop-filter: blur(2px); padding: 16px; box-sizing: border-box; }
|
||||
.dialog-box { width: 100%; max-width: 600px; background: #fff; border-radius: 16px; display: flex; flex-direction: column; max-height: 90vh; box-shadow: 0 10px 25px rgba(0,0,0,0.15); }
|
||||
.dialog-header { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid #f3f4f6; }
|
||||
.dialog-header h3 { margin: 0; font-size: 16px; font-weight: bold; }
|
||||
.close-btn { background: none; border: none; cursor: pointer; color: #9ca3af; padding: 0; display: flex; align-items: center; justify-content: center; }
|
||||
.close-btn:hover { color: #4b5563; }
|
||||
.dialog-body { padding: 20px; overflow-y: auto; background: #f8fafc; flex: 1; border-bottom-left-radius: 16px; border-bottom-right-radius: 16px; }
|
||||
|
||||
.chat-container { display: flex; flex-direction: column; gap: 20px; }
|
||||
.chat-row { display: flex; width: 100%; }
|
||||
.row-ai { justify-content: flex-start; }
|
||||
.row-user { justify-content: flex-end; }
|
||||
.chat-message { max-width: 90%; display: flex; flex-direction: column; }
|
||||
@media (min-width: 640px) { .chat-message { max-width: 75%; } }
|
||||
.chat-owner { font-size: 12px; margin-bottom: 6px; font-weight: bold; }
|
||||
.row-ai .chat-owner { color: #2563eb; text-align: left; margin-left: 4px; }
|
||||
.row-user .chat-owner { color: #6b7280; text-align: right; margin-right: 4px; }
|
||||
.chat-time { font-size: 11px; color: #9ca3af; margin-top: 6px; }
|
||||
.row-ai .chat-time { text-align: left; margin-left: 4px; }
|
||||
.row-user .chat-time { text-align: right; margin-right: 4px; }
|
||||
|
||||
/* 气泡样式 */
|
||||
.chat-bubble { padding: 10px 14px; border-radius: 12px; font-size: 14px; line-height: 1.6; word-wrap: break-word; box-shadow: 0 1px 2px rgba(0,0,0,0.05); }
|
||||
.bubble-ai { background: #ffffff; color: #1f2937; border: 1px solid #e2e8f0; border-top-left-radius: 2px; }
|
||||
.bubble-user { background: #2563eb; color: #ffffff; border-top-right-radius: 2px; }
|
||||
|
||||
/* 全新精美引用框 (无左黑条,微信风格) */
|
||||
.quote-box { margin-bottom: 8px; padding: 8px 10px; border-radius: 8px; font-size: 12px; display: block; width: 100%; box-sizing: border-box; }
|
||||
.bubble-ai .quote-box { background: rgba(0,0,0,0.04); color: #6b7280; }
|
||||
.bubble-user .quote-box { background: rgba(255,255,255,0.15); color: #d1d5db; }
|
||||
.quote-header { display: flex; align-items: center; gap: 4px; margin-bottom: 2px; }
|
||||
.quote-icon { width: 12px; height: 12px; opacity: 0.7; }
|
||||
.quote-owner { font-weight: 600; font-size: 11px; }
|
||||
.bubble-ai .quote-owner { color: #374151; }
|
||||
.bubble-user .quote-owner { color: #ffffff; }
|
||||
.quote-content { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.4; }
|
||||
|
||||
.pagination { display: flex; flex-direction: column; gap: 12px; align-items: center; margin-top: 20px; font-size: 14px; color: #6b7280; }
|
||||
@media (min-width: 640px) { .pagination { flex-direction: row; justify-content: space-between; } }
|
||||
.pagination-btns { display: flex; gap: 8px; }
|
||||
|
||||
/* 误报反馈弹窗 */
|
||||
.fp-dialog { max-width: 440px; }
|
||||
.fp-dialog-body { padding: 24px; }
|
||||
.fp-desc { margin: 0 0 20px; font-size: 14px; color: #4b5563; line-height: 1.6; }
|
||||
.fp-actions { display: flex; flex-direction: column; gap: 10px; }
|
||||
.fp-btn { padding: 10px 16px; border-radius: 8px; font-size: 14px; font-weight: 500; cursor: pointer; border: none; transition: all 0.15s; display: flex; align-items: center; justify-content: center; gap: 6px; }
|
||||
.fp-btn:disabled { opacity: 0.6; cursor: not-allowed; }
|
||||
.fp-btn-primary { background: #2563eb; color: #fff; }
|
||||
.fp-btn-primary:hover:not(:disabled) { background: #1d4ed8; }
|
||||
.fp-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }
|
||||
.fp-btn-secondary:hover:not(:disabled) { background: #e5e7eb; }
|
||||
.fp-btn-ghost { background: transparent; color: #9ca3af; }
|
||||
.fp-btn-ghost:hover:not(:disabled) { color: #6b7280; background: #f9fafb; }
|
||||
.fp-spinner { width: 14px; height: 14px; border: 2px solid rgba(255,255,255,0.3); border-top-color: #fff; border-radius: 50%; animation: fp-spin 0.6s linear infinite; }
|
||||
@keyframes fp-spin { to { transform: rotate(360deg); } }
|
||||
</style>
|
||||
|
||||
+316
-1151
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user