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 f0f0e72..efc18c7 100644 --- a/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt +++ b/app/src/main/java/com/haorui/app/automation/HaoRuiAccessibilityService.kt @@ -430,6 +430,8 @@ class HaoRuiAccessibilityService : AccessibilityService() { addAction("com.haorui.app.ACTION_TEST_WEIBO") // 抖音热榜采集沙盒(debug APK:listOnly=清单;entryIndex=单条详情) addAction("com.haorui.app.ACTION_TEST_DOUYIN") + // 抖音视频链沙盒(debug APK:entryIndex=单条详情→保存本地→导入→ASR→标题→落库) + addAction("com.haorui.app.ACTION_TEST_DOUYIN_VIDEO") // 调试输入法直写(M4 文章编辑器;需先 adb ime set 切到浩瑞调试输入法) addAction("com.haorui.app.ACTION_IME_COMMIT") // 文章发布机链路验证(debug APK:编辑器写入→回读→草稿确认;不碰「下一步」) @@ -680,6 +682,12 @@ class HaoRuiAccessibilityService : AccessibilityService() { val accepted = startDouyinCollectSandbox(entryIndex, listOnly) RunLogger.i(TAG, "抖音采集沙盒受理=$accepted(广播 ACTION_TEST_DOUYIN entry=$entryIndex listOnly=$listOnly)") } + // 抖音视频链沙盒(debug APK;entryIndex=单条详情序号→保存本地→ASR→标题→落库) + "com.haorui.app.ACTION_TEST_DOUYIN_VIDEO" -> { + val entryIndex = intent.getStringExtra("entryIndex")?.toIntOrNull() ?: 0 + val accepted = startDouyinVideoSandbox(entryIndex) + RunLogger.i(TAG, "抖音视频链沙盒受理=$accepted(广播 ACTION_TEST_DOUYIN_VIDEO entry=$entryIndex)") + } // 调试输入法直写(M4 文章编辑器自绘控件写入;commitText 分批,回读走 OCR) "com.haorui.app.ACTION_IME_COMMIT" -> { val text = intent.getStringExtra("text").orEmpty() @@ -1560,6 +1568,158 @@ class HaoRuiAccessibilityService : AccessibilityService() { true } + /** + * 抖音视频链沙盒(debug APK,2026-11-19,用户决策路线:带水印「保存本地」): + * 热榜单条采集进详情 → 分享面板「保存本地」→ 相册定位+导入+校验+音轨+ASR → AI 标题 → + * 落库 VIDEO 任务(DISCOVERED→…→READY,不接分发——视频草稿车道接线待下轮)。 + * 作者禁下载(SAVE_DISABLED)=确定性失败退出;ASR 失败降级 OCR 材料生成标题。 + */ + fun startDouyinVideoSandbox(entryIndex: Int): Boolean = + synchronized(stepLifecycleLock) { + if (applicationInfo.flags and android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE == 0 || + !::scheduler.isInitialized || scheduler.isRunning || publisher.isWorking || interactor.isWorking || + frontGuard.creditNavigating || !foregroundCoordinator.isIdle()) return@synchronized false + launchForegroundTask { + val ticket = foregroundCoordinator.tryEnterLegacy("DOUYIN-VIDEO-SANDBOX") ?: return@launchForegroundTask + try { + val port = com.haorui.app.crossplatform.source.douyin.DouyinDevicePort(this@HaoRuiAccessibilityService) + val machine = com.haorui.app.crossplatform.source.douyin.DouyinHotListCollector(port) { SystemClock.elapsedRealtime() } + machine.runGeneration = startGeneration.toLong() + RunLogger.i(TAG, "抖音视频链沙盒开始:entry=$entryIndex") + + // ── 1) 热榜单条进详情(复用采集机;onDetail 钩子在详情前台时取材料) ── + var detailLines: List = emptyList() + var hotTitle = "" + var savedSinceMs = 0L + var saveOutcome: com.haorui.app.crossplatform.source.douyin.DouyinHotListCollector.SaveLocalOutcome? = null + var copyOk = false + val result = machine.collectOne(entryIndex, onDetail = { + // 详情页前台:先取 OCR 材料(标题链降级兜底),再做分享面板动作 + val full = runCatching { port.readDetailOcrFullText() }.getOrDefault(emptyList()) + detailLines = if (full.isNotEmpty()) full else port.readDetailOcrLines() + savedSinceMs = System.currentTimeMillis() + saveOutcome = port.saveLocalFromSharePanel() + if (saveOutcome == com.haorui.app.crossplatform.source.douyin.DouyinHotListCollector.SaveLocalOutcome.SAVED) { + // 保存成功后稍候再取(落盘+MediaStore 入库有延迟) + Thread.sleep(3_000) + copyOk = port.copyShareLink() + } + // 关闭分享面板恢复详情页(外发面板是弹层,不关会挡后续导航) + port.dismissSharePanel() + }) + RunLogger.i( + TAG, + "抖音视频链采集: outcome=${result.outcome} phase=${result.phase} exited=${result.exitedSafely} reason=${result.reason}", + ) + hotTitle = result.observation?.hotEntry?.title.orEmpty() + + if (saveOutcome != com.haorui.app.crossplatform.source.douyin.DouyinHotListCollector.SaveLocalOutcome.SAVED) { + RunLogger.w(TAG, "抖音视频链:保存本地未成功($saveOutcome),终止(作者禁下载或操作失败)") + return@launchForegroundTask + } + + // ── 2) 视频链编排:定位→导入→校验→音轨→ASR ── + val locator = com.haorui.app.crossplatform.media.DouyinSavedVideoLocator(applicationContext) + val provider = com.haorui.app.crossplatform.media.SourceMediaProvider(applicationContext) { msg -> + RunLogger.i(TAG, "DY-VideoChain: $msg") + } + val chain = com.haorui.app.crossplatform.media.DouyinVideoChain(provider, locator) { msg -> + RunLogger.i(TAG, "DY-VideoChain: $msg") + } + val aiConfig = latestConfig.ai + val chainOutcome = chain.run( + savedSinceMs = savedSinceMs, + hotListTitle = hotTitle, + ocrDetailText = detailLines.joinToString("\n"), + asrApiKey = aiConfig.qwenKey, + ) + if (!chainOutcome.ok) { + RunLogger.e(TAG, "抖音视频链编排失败[${chainOutcome.stage}]: ${chainOutcome.reason}") + return@launchForegroundTask + } + + // ── 3) AI 视频标题(转录优先/OCR 降级) ── + val evidence = com.haorui.app.crossplatform.compose.VideoTitleComposer.buildEvidence( + transcriptText = chainOutcome.transcriptText, + hotListTitle = hotTitle, + ocrDetailText = detailLines.joinToString("\n"), + ) + val titleComposer = com.haorui.app.crossplatform.compose.VideoTitleComposer + val titleResult = titleComposer.composeTitles(evidence, { + Triple(aiConfig.provider, aiConfig.deepseekKey, "https://api.deepseek.com/chat/completions") + }) { msg -> RunLogger.i(TAG, "DY-VideoTitle: $msg") } + val videoTitle = titleResult?.selected ?: hotTitle + RunLogger.i(TAG, "抖音视频标题: AI=${titleResult?.selected ?: "未生成(降级热榜标题)"} 最终=「$videoTitle」") + + // ── 4) 落库 VIDEO 任务(不接分发——视频草稿车道接线待下轮) ── + val store = com.haorui.app.crossplatform.data.CrossPublishTaskStore.get(applicationContext) + val gen: Long = currentCrossRunGeneration() ?: run { + crossRunSession?.configure(true) + crossRunSession?.start() + var opened: Long? = null + for (i in 1..15) { + kotlinx.coroutines.delay(1_000) + opened = currentCrossRunGeneration() + if (opened != null) break + } + if (opened == null) { + RunLogger.w(TAG, "抖音视频落库:会话临时开启后仍无代际,放弃落库") + return@launchForegroundTask + } + opened + } + val identity = "douyin-video-${hotTitle.hashCode()}-${System.currentTimeMillis() % 100_000}" + val task = com.haorui.app.crossplatform.model.CrossPublishTask( + id = identity, + accountKey = "douyin-local", + eventId = "douyin:$identity", + contentKind = com.haorui.app.crossplatform.model.CrossContentKind.VIDEO, + title = videoTitle, + sourcePlatform = "douyin", + sourceUrl = "douyin://video/${chainOutcome.videoSha256.take(16)}", + state = com.haorui.app.crossplatform.model.CrossTaskState.DISCOVERED, + bodyText = "", // 视频无正文;简介由发布桥生成(VideoTaskBridge) + evidenceJson = kotlinx.serialization.json.Json.encodeToString( + kotlinx.serialization.json.buildJsonObject { + put("hotTitle", kotlinx.serialization.json.JsonPrimitive(hotTitle)) + put("videoSha256", kotlinx.serialization.json.JsonPrimitive(chainOutcome.videoSha256)) + put("videoPath", kotlinx.serialization.json.JsonPrimitive(chainOutcome.videoPath)) + put("transcriptLen", kotlinx.serialization.json.JsonPrimitive(chainOutcome.transcriptText.length)) + put("transcriptFailure", kotlinx.serialization.json.JsonPrimitive(chainOutcome.transcriptFailure)) + put("shareLinkCopied", kotlinx.serialization.json.JsonPrimitive(copyOk)) + put("detailLines", kotlinx.serialization.json.JsonPrimitive(detailLines.size)) + }, + ), + publishedAtMs = System.currentTimeMillis(), + expiresAtMs = System.currentTimeMillis() + 7 * 24 * 3600 * 1000L, + payloadHash = java.security.MessageDigest.getInstance("SHA-256") + .digest((videoTitle + chainOutcome.videoSha256).toByteArray()) + .joinToString("") { "%02x".format(it) }, + runGeneration = gen, + ) + val enqueued = kotlinx.coroutines.runBlocking { + runCatching { store.enqueue(task) }.getOrDefault(false) + } + RunLogger.i(TAG, "抖音视频落库: id=$identity 落库=$enqueued gen=$gen") + if (enqueued) { + kotlinx.coroutines.runBlocking { + val t1 = store.transition(identity, com.haorui.app.crossplatform.model.CrossTaskState.DISCOVERED, com.haorui.app.crossplatform.model.CrossTaskState.FILTERED, gen, "视频链采集") + // prepare 素材登记:视频文件路径(MEDIA 资产) + val t2 = store.prepare(identity, gen, "", "douyin-saved-video", task.evidenceJson, "douyin-video-chain", task.payloadHash) + val t3 = store.transition(identity, com.haorui.app.crossplatform.model.CrossTaskState.PREPARED, com.haorui.app.crossplatform.model.CrossTaskState.READY, gen, "视频标题已生成") + RunLogger.i(TAG, "抖音视频任务推进: FILTERED=$t1 PREPARED=$t2 READY=$t3") + } + } + RunLogger.i(TAG, "抖音视频链沙盒完成: id=$identity 标题=「$videoTitle」") + } catch (t: Throwable) { + RunLogger.e(TAG, "抖音视频链沙盒异常: ${t.javaClass.simpleName}:${t.message}", t) + } finally { + foregroundCoordinator.release(ticket) + } + } + true + } + /** * M4 创作链真机验证(debug APK):采集单条 → OCR 全文 → 事实包 → 模型标题候选。 * 只读+模型调用,不落库、不发布;结果全走日志(标题候选逐条输出)。