feat:抖音 AI 创作链接入——事实包→正文→标题替代模板占位:

M4 组件复用(EvidenceBuilder→ArticleBodyComposer→ArticleComposer):
- DouyinDevicePort.readDetailOcrFullText:详情页逐屏 OCR(半屏上滑+
  行序去重,上限 4 屏,滑动幅度适配搜索结果页卡片密度);
- 材料必须在 collectOne 的 onDetail 钩子内取屏(EXIT 后读屏=榜单,
  微博 M4 教训);材料头部注入热榜元数据(排名/热度/标签)锚定语境;
- 创作链失败(材料过薄/模型失败/核验不过)降级模板正文不阻塞链路;
- 幂等键 identity 基于热榜标题不随 AI 标题漂移;payloadHash 用成品
  (taskTitle+bodyText)。

真机验证:事实包 OCR 885 字→提纲 5 要点→正文 1431 字(第 1 稿过
三关)→标题「贵州辣卤会惩罚每一个嘴硬的人」3 候选 0 淘汰(AI 按
上下文修正了 OCR 形近错字)→落库 gen=617 三跳全 true→图文机器
写入 14 字标题+1396 口径正文→1431 字长文回读「反向滑 10 次未稳定」
锚点未命中→按宁核对不误判暂停→归账 Unknown(设计内保护,草稿
大概率已自动保存待核对)。真库 RESULT_UNKNOWN|attempt UNKNOWN 实锤。

Co-Authored-By: Claude Code <[email protected]>
This commit is contained in:
2026-11-17 22:03:43 +08:00
co-authored by Claude Code
parent a2de5d2275
commit d55d41f610
2 changed files with 115 additions and 6 deletions
@@ -1326,7 +1326,14 @@ class HaoRuiAccessibilityService : AccessibilityService() {
}
} else {
RunLogger.i(TAG, "抖音采集沙盒开始:单条模式 entry=$entryIndex")
val result = machine.collectOne(entryIndex)
// ⚠️ AI 创作链材料必须在详情页仍在前台时取(collectOne 的 finally 会 EXIT
// 返回榜单,退出后读屏=榜单不是词条——微博 M4 真机取证教训)。
// 全文失败退单屏兜底;作者行/时间行等噪声由 OcrArticleExtractor 剔除。
var detailLines: List<String> = emptyList()
val result = machine.collectOne(entryIndex, onDetail = {
val full = runCatching { port.readDetailOcrFullText() }.getOrDefault(emptyList())
detailLines = if (full.isNotEmpty()) full else port.readDetailOcrLines()
})
RunLogger.i(
TAG,
"抖音采集沙盒结果: outcome=${result.outcome} phase=${result.phase} " +
@@ -1337,7 +1344,7 @@ class HaoRuiAccessibilityService : AccessibilityService() {
TAG,
"抖音采集沙盒观察: 标题=${obs.hotEntry?.title?.take(24)} 排名=${obs.hotEntry?.rank} " +
"热度=${obs.hotEntry?.heatRaw} 发布时间=${obs.publishedAtText}(${obs.publishedAtMs}) " +
"未知=${obs.unknowns}",
"材料=${detailLines.size}行 未知=${obs.unknowns}",
)
// 落库:采集成功后 enqueue 到任务库(仿微博模式)
if (result.outcome == "已观察") {
@@ -1363,8 +1370,8 @@ class HaoRuiAccessibilityService : AccessibilityService() {
}
val title = obs.hotEntry?.title ?: "抖音热榜第${obs.hotEntry?.rank ?: entryIndex}条"
val identity = "douyin-rank${obs.hotEntry?.rank ?: entryIndex}-${title.hashCode()}"
// 图文发布:用热榜标题生成模板正文(AI 创作链后续接入)
val bodyText = buildString {
// 模板正文(降级保底):AI 创作链失败时任务仍可推进,不阻塞链路
val fallbackBody = buildString {
appendLine("抖音热榜第 ${obs.hotEntry?.rank ?: entryIndex} 名:$title")
obs.hotEntry?.heatRaw?.let { appendLine("热度:$it") }
obs.hotEntry?.label?.let { appendLine("标签:$it") }
@@ -1372,12 +1379,67 @@ class HaoRuiAccessibilityService : AccessibilityService() {
appendLine()
appendLine("以上内容来自抖音热榜实时采集。")
}
// ── AI 创作链(M4 组件复用):事实包→正文→标题,失败降级模板 ──
// 材料头部注入热榜元数据(排名/热度/标签)给模型锚定「抖音热榜第N名」语境。
var aiTitle: String? = null
var aiBody: String? = null
if (detailLines.isNotEmpty()) {
try {
val meta = buildString {
appendLine("(抖音热榜第 ${obs.hotEntry?.rank ?: entryIndex} 名" +
(obs.hotEntry?.heatRaw?.let { ",热度 $it" } ?: "") +
(obs.hotEntry?.label?.let { ",标签「$it」" } ?: "") + ")")
}
val material = listOf(meta, obs.publishedAtText?.let { "(发布时间:$it)" } ?: "")
.filter { it.isNotBlank() } + detailLines
val builder = com.haorui.app.crossplatform.compose.EvidenceBuilder(
logger = { msg -> RunLogger.i(TAG, "DY-Evidence: $msg") },
)
val evidence = builder.build(
title, "douyin://hot/${obs.hotEntry?.rank ?: entryIndex}",
material, System.currentTimeMillis(),
)
if (evidence != null) {
RunLogger.i(TAG, "DY 创作链:事实包 ${evidence.sourceRoute} ${evidence.sourceText.length} 字(首行=${evidence.sourceText.lineSequence().firstOrNull().orEmpty().take(40)})")
val aiConfig = latestConfig.ai
val bodyComposer = com.haorui.app.crossplatform.compose.ArticleBodyComposer(aiConfig) { msg ->
RunLogger.i(TAG, "DY-Body: $msg")
}
val bodyResult = bodyComposer.composeBody(evidence)
if (bodyResult != null) {
val composer = com.haorui.app.crossplatform.compose.ArticleComposer(aiConfig) { msg ->
RunLogger.i(TAG, "DY-Composer: $msg")
}
val titles = composer.composeTitles(evidence)
if (titles != null) {
aiTitle = titles.selected
aiBody = bodyResult.body
RunLogger.i(TAG, "DY 创作链成功:标题「${titles.selected}」正文 ${bodyResult.body.codePointCount(0, bodyResult.body.length)} 字")
titles.candidates.forEachIndexed { i, c -> RunLogger.i(TAG, "DY 标题候选[$i]: $c") }
} else {
RunLogger.w(TAG, "DY 创作链:标题失败 ${composer.failureReason},降级模板")
}
} else {
RunLogger.w(TAG, "DY 创作链:正文失败 ${bodyComposer.failureReason},降级模板")
}
} else {
RunLogger.w(TAG, "DY 创作链:事实包失败 ${builder.failureReason},降级模板")
}
} catch (t: Throwable) {
RunLogger.e(TAG, "DY 创作链异常(降级模板): ${t.javaClass.simpleName}:${t.message}", t)
}
} else {
RunLogger.w(TAG, "DY 创作链:详情材料 0 行(OCR 未取得),降级模板")
}
// 生成内容优先,模板兜底;幂等键 identity 基于热榜标题不随 AI 标题漂移
val bodyText = aiBody ?: fallbackBody
val taskTitle = aiTitle ?: title
val task = com.haorui.app.crossplatform.model.CrossPublishTask(
id = identity,
accountKey = "douyin-local",
eventId = "douyin:$identity",
contentKind = com.haorui.app.crossplatform.model.CrossContentKind.ARTICLE,
title = title,
title = taskTitle,
sourcePlatform = "douyin",
sourceUrl = "douyin://hot/${obs.hotEntry?.rank ?: entryIndex}",
state = com.haorui.app.crossplatform.model.CrossTaskState.DISCOVERED,
@@ -1398,7 +1460,7 @@ class HaoRuiAccessibilityService : AccessibilityService() {
publishedAtMs = obs.publishedAtMs ?: System.currentTimeMillis(),
expiresAtMs = System.currentTimeMillis() + 7 * 24 * 3600 * 1000L,
payloadHash = java.security.MessageDigest.getInstance("SHA-256")
.digest((title + bodyText).toByteArray())
.digest((taskTitle + bodyText).toByteArray())
.joinToString("") { "%02x".format(it) },
runGeneration = gen,
)
@@ -430,6 +430,53 @@ internal class DouyinDevicePort(
return ocrCurrentScreen()
}
/**
* 详情页逐屏全文 OCR(AI 创作链材料,2026-11-17):半屏上滑+行序去重,最多 [maxScreens] 屏。
* 仿微博 readPostOcrFullText;滑动幅度 0.72→0.34(抖音详情=搜索结果页,卡片密集防跨卡)。
* 调用时机:必须在详情页仍在前台时(collectOne 的 onDetail 钩子)——EXIT 后读到的是榜单。
*/
fun readDetailOcrFullText(maxScreens: Int = 4): List<String> {
val allLines = mutableListOf<String>()
val seen = HashSet<String>()
var noNewCount = 0
for (screen in 0 until maxScreens) {
val lines = ocrCurrentScreen()
if (lines.isEmpty()) {
log("详情全文 OCR 第 $screen 屏识别失败,继续尝试")
Thread.sleep(1_000)
continue
}
var appended = 0
lines.forEach { line ->
val text = line.trim()
if (text.isNotEmpty() && seen.add(text)) {
allLines.add(text)
appended++
}
}
log("详情全文 OCR 第 $screen 屏:识别 ${lines.size} 行,新增 $appended 行")
// 页底判定:连续两屏无新行(微博同款口径)
if (appended == 0) {
noNewCount++
if (noNewCount >= 2) {
log("详情全文 OCR 连续两屏无新行,判定到页底")
break
}
} else {
noNewCount = 0
}
val scrolled = kotlinx.coroutines.runBlocking {
service.gestures.swipe(0.5f, 0.72f, 0.5f, 0.34f)
}
if (!scrolled) {
log("详情全文 OCR 第 $screen 屏滑动失败,终止")
break
}
Thread.sleep(1_200)
}
return allLines
}
override fun readDetailTree(): SourceNode = tree() ?: SourceNode()
override fun backToHotList(): Boolean {