history: add indexed search and context retrieval

This commit is contained in:
2026-08-06 21:26:37 +08:00
parent 0303bf0ac8
commit c8cdea6fab
12 changed files with 1514 additions and 222 deletions
+29
View File
@@ -1,5 +1,10 @@
package top.jie65535.mirai
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandSender.Companion.toCommandSender
import net.mamoe.mirai.console.permission.PermissionId
@@ -48,6 +53,7 @@ object JChatGPT : KotlinPlugin(
}
) {
internal var includeHistory: Boolean = false
private var historyIndexJob: Job? = null
val chatPermission = PermissionId("JChatGPT", "Chat")
@@ -69,6 +75,27 @@ object JChatGPT : KotlinPlugin(
logger.error("初始化 SQLite 聊天记录失败,历史上下文与搜索将暂时禁用", cause)
false
}
if (includeHistory && ChatHistoryStore.isSearchIndexAvailable && !ChatHistoryStore.isSearchIndexReady) {
historyIndexJob = launch(Dispatchers.IO) {
try {
val processed = ChatHistoryStore.backfillSearchIndex(
shouldContinue = { isActive },
onProgress = { count ->
if (count % 10_000 == 0) {
logger.info("聊天记录全文索引回填进度: 已处理 $count")
}
},
)
if (isActive && processed > 0) {
logger.info("聊天记录全文索引回填完成: 共处理 $processed")
}
} catch (cause: CancellationException) {
throw cause
} catch (cause: Throwable) {
logger.warning("聊天记录全文索引回填失败,后续启动将继续尝试", cause)
}
}
}
if (includeHistory) {
runCatching { ContactSnapshotStore.init(dataFolder) }
.onFailure { logger.error("初始化联系人快照数据库失败,联系人画像辅助将暂时禁用", it) }
@@ -103,6 +130,8 @@ object JChatGPT : KotlinPlugin(
}
override fun onDisable() {
historyIndexJob?.cancel()
historyIndexJob = null
ConversationEngine.clear()
ConversationContext.clearAll()
ProfileAutoMaintenance.clear()