mirror of
https://github.com/jie65535/JChatGPT.git
synced 2026-09-15 02:56:10 +08:00
profile: add progressive user profiles
Add cache-expiry profile maintenance and contextual injection, reorganize runtime code by responsibility, remove automatic favorability decay, and prepare version 1.15.0.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package top.jie65535.mirai.profile
|
||||
|
||||
import top.jie65535.mirai.data.FavorabilityInfo
|
||||
|
||||
object UserProfileContextRenderer {
|
||||
fun render(
|
||||
profiles: List<UserProfileSnapshot>,
|
||||
favorabilityByUserId: Map<Long, FavorabilityInfo>,
|
||||
displayNames: Map<Long, String>,
|
||||
activeUserIds: Set<Long>,
|
||||
summaryMaxChars: Int,
|
||||
): String {
|
||||
val profilesByUserId = profiles
|
||||
.filter { it.reliable && it.summary.isNotBlank() }
|
||||
.associateBy { it.userId }
|
||||
val userIds = activeUserIds.filter { userId ->
|
||||
userId in profilesByUserId || favorabilityByUserId[userId]?.hasVisibleContext() == true
|
||||
}
|
||||
if (userIds.isEmpty()) return ""
|
||||
|
||||
val maxChars = summaryMaxChars.coerceAtLeast(50)
|
||||
return buildString {
|
||||
appendLine("## 你对相关群友的认识")
|
||||
appendLine("好感度、代号和主观印象代表你的关系状态;长期认识来自可修正的历史归纳。仅在当前话题相关时自然运用,不要逐条复述或提及信息来源。")
|
||||
userIds.forEach { userId ->
|
||||
val profile = profilesByUserId[userId]
|
||||
val favorability = favorabilityByUserId[userId]
|
||||
val name = favorability?.name.orEmpty().ifBlank {
|
||||
displayNames[userId].orEmpty().ifBlank { userId.toString() }
|
||||
}
|
||||
append("- ").append(name).append('(').append(userId).append(')')
|
||||
favorability?.takeIf { it.hasVisibleContext() }?.let { info ->
|
||||
append(" | 好感度").append(if (info.value >= 0) "+" else "").append(info.value)
|
||||
if (info.tags.isNotEmpty()) append(" | 标签:").append(info.tags.joinToString("、"))
|
||||
if (info.impression.isNotBlank()) append(" | 主观印象:").append(info.impression.normalized())
|
||||
}
|
||||
profile?.let {
|
||||
append(" | 长期认识:").append(it.summary.normalized().take(maxChars))
|
||||
}
|
||||
|
||||
profile?.items?.asSequence()
|
||||
?.filter { item ->
|
||||
item.category == ProfileCategory.RELATIONSHIP_NOTE &&
|
||||
item.relatedUserId != null && item.relatedUserId in activeUserIds
|
||||
}
|
||||
?.take(2)
|
||||
?.forEach { item ->
|
||||
val relatedId = checkNotNull(item.relatedUserId)
|
||||
val relatedName = displayNames[relatedId].orEmpty().ifBlank { relatedId.toString() }
|
||||
append(";与").append(relatedName).append(":").append(item.content.normalized())
|
||||
}
|
||||
appendLine()
|
||||
}
|
||||
appendLine()
|
||||
}
|
||||
}
|
||||
|
||||
private fun FavorabilityInfo.hasVisibleContext(): Boolean =
|
||||
value != 0 || name.isNotBlank() || tags.isNotEmpty() || impression.isNotBlank()
|
||||
|
||||
private fun String.normalized(): String = trim().replace(Regex("\\s+"), " ")
|
||||
}
|
||||
Reference in New Issue
Block a user