diff --git a/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt b/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt index 2185a33..d63b0c7 100644 --- a/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt +++ b/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt @@ -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 = 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, ) diff --git a/app/src/main/java/com/haorui/app/crossplatform/source/douyin/DouyinDevicePort.kt b/app/src/main/java/com/haorui/app/crossplatform/source/douyin/DouyinDevicePort.kt index 335cbd4..6371851 100644 --- a/app/src/main/java/com/haorui/app/crossplatform/source/douyin/DouyinDevicePort.kt +++ b/app/src/main/java/com/haorui/app/crossplatform/source/douyin/DouyinDevicePort.kt @@ -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 { + val allLines = mutableListOf() + val seen = HashSet() + 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 {