package com.catshark.lpt.data import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.jsonPrimitive /** lpt-be 的统一响应包装:{ code, message, data } */ @Serializable data class ApiEnvelope( val code: Int = 0, val message: String? = null, val data: T? = null, ) @Serializable data class LoginRequest( val username: String, val password: String, ) @Serializable data class TaskItem( val id: Long = 0, val taskNum: String = "", val taskName: String = "", val taskDescription: String? = null, val materialUrl: String? = null, val taskPriority: Double = 0.0, val lastLearningStatus: String? = null, ) @Serializable data class TaskPage( val records: List = emptyList(), val total: Int = 0, val size: Int = 0, val current: Int = 0, val pages: Int = 0, ) @Serializable data class SessionDetail( val sessionNum: String = "", val taskNum: String? = null, val taskName: String? = null, val taskId: Long? = null, val materialUrl: String? = null, val startTime: String? = null, val lastStartTime: String? = null, val endTime: String? = null, val actualTime: Double = 0.0, val effectiveTime: Double = 0.0, val effectivenessRatio: Double = 0.0, val sessionState: String = "", val pointerPosition: Double = 0.0, val systemMessage: String? = null, val autoConverged: Boolean = false, ) @Serializable data class ActiveSession( val taskNum: String = "", val sessionNum: String = "", val taskName: String? = null, val sessionState: String? = null, val autoConverged: Boolean = false, /** 服务端自动收敛时的用户可见文案 */ val systemMessage: String? = null, ) @Serializable data class Expectation( val description: String = "", ) @Serializable data class ExpectationRequest( val description: String, ) @Serializable data class EndSessionRequest( /** 旧版后端(部署版)要的单段报告正文 */ val content: String? = null, val sessionSummary: String? = null, val expectationVerdict: String? = null, val freeSummary: String? = null, ) @Serializable data class AbortRequest( val confirmation: String, ) @Serializable data class CreateFragmentRequest( /** 新版后端(本地源码)用 contextNum;旧版后端(部署版)用 sessionNum,两个都发 */ val contextNum: String, val sessionNum: String, val content: String, ) @Serializable data class FragmentItem( val id: Int = 0, val contextNum: String = "", val contextType: String = "SESSION", val ownerTaskNum: String? = null, val content: String = "", val createdTime: String? = null, ) /** 结束会话时提交的三段报告文本 */ data class ReportSections( val sessionSummary: String, val expectationVerdict: String, val freeSummary: String, ) { val allBlank: Boolean get() = sessionSummary.isBlank() && expectationVerdict.isBlank() && freeSummary.isBlank() companion object { val EMPTY = ReportSections("", "", "") } /** * 旧版后端(部署版)只要一段 content。把三段拼成一段,并保留小标题, * 这样旧后端的报告页也能看出结构;全空时给一个占位,避免被「请填写学习报告内容」拦下。 */ fun asLegacyContent(): String = buildString { if (sessionSummary.isNotBlank()) appendLine("【会话摘要】$sessionSummary") if (expectationVerdict.isNotBlank()) appendLine("【预期达成判断】$expectationVerdict") if (freeSummary.isNotBlank()) appendLine("【自由总结】$freeSummary") }.trim().ifBlank { "(在手机上结束了本次学习,未填写总结)" } } /** 从登录响应里取 token:线上实际返回纯字符串,源码契约是 {token, aiAdmin},两者都兼容 */ fun extractToken(element: JsonElement?): String? { if (element == null) return null val text = element.toString().trim() if (text.startsWith("\"")) return text.trim('"') val token = (element as? JsonObject)?.get("token") ?: return null val raw = token.toString().trim() return if (raw.startsWith("\"")) raw.trim('"') else raw } /** 某次学习会话产出的学习报告(报告三段 + 该会话的学习预期) */ @Serializable data class StudyReport( val id: Int = 0, val sessionNum: String = "", val sessionSummary: String? = null, val expectationVerdict: String? = null, val freeSummary: String? = null, /** 会话对应的学习预期,随报告返回,不落库 */ val sessionExpectation: String? = null, val createdTime: String? = null, ) @Serializable data class ReportPage( val records: List = emptyList(), val total: Int = 0, val size: Int = 0, val current: Int = 0, val pages: Int = 0, ) /** AI 异步任务(AI-02):提交秒级返回,之后轮询拿状态与结果 */ @Serializable data class AiTask( val taskNum: String = "", val type: String = "", val targetNum: String = "", val status: String = "", val errorMessage: String? = null, val result: JsonObject? = null, ) { val succeeded: Boolean get() = status == "SUCCEEDED" val failed: Boolean get() = status == "FAILED" val inProgress: Boolean get() = status == "PENDING" || status == "RUNNING" } @Serializable data class SubmitAiTaskRequest( val type: String, val targetNum: String, ) /** 取报告草稿三段:REPORT_DRAFT 任务的 result 就是三个同名字段 */ fun AiTask.draftSections(): ReportSections = ReportSections( sessionSummary = result.text("sessionSummary"), expectationVerdict = result.text("expectationVerdict"), freeSummary = result.text("freeSummary"), ) private fun JsonObject?.text(key: String): String { val element = this?.get(key) ?: return "" if (element is JsonNull) return "" return runCatching { element.jsonPrimitive.content }.getOrDefault("") }