Skip to content

ChatHistory.kt

src/jvmMain/kotlin/afield/ai/memory/ChatHistory.kt · 106 lines

package afield.ai.memory

import afield.ai.conversation.chatlog.ChatLogViews
import afield.ai.conversation.chatlog.ChatlogStore
import java.io.File
import java.util.concurrent.ConcurrentHashMap

class ChatHistory private constructor(val workspace: File)

The saved AI4 chats of one workspace, searchable from any AI4 turn: the transcript layer under UserMemory.

It reads the same session trees as the chat archive (ChatArchive, registerChatlogTools), straight from the document folders and without opening them, every branch included. A block is cited as <chat>@<address>, for example firstDocument/sketch@t-a3.b2, which is what a memory note's sources hold; read takes that form back.

class ChatHistory private constructor(val workspace: File) {

ChatlogStore caches parsed trees by file stamp and is not thread-safe; one store per workspace, locked.

    private val store = ChatlogStore(listOf(workspace), File(workspace, ".ai4-history-lenses-unused"))

data class Hit(val chat: String, val address: String, val kind: String, val snippet: String)

One matching block: the chat it is in, its address there, its kind, and a snippet around the match.

val citation: String get() = "$chat@$address"

companion object

    companion object {
        private val byRoot = ConcurrentHashMap<String, ChatHistory>()

fun forDocument(documentDir: File): ChatHistory

The history of the workspace holding the document folder documentDir.

        fun forDocument(documentDir: File): ChatHistory = forWorkspace(documentDir.absoluteFile.parentFile ?: documentDir)

fun forWorkspace(workspace: File): ChatHistory

        fun forWorkspace(workspace: File): ChatHistory =
            byRoot.computeIfAbsent(workspace.absoluteFile.normalize().path) { ChatHistory(File(it)) }

fun offeredFor(documentPath: String?): ChatHistory?

The history an interactive AI4 turn on the document at documentPath is offered: null when memory is off (AFIELD_AI4_MEMORY) or there is no document.

        fun offeredFor(documentPath: String?): ChatHistory? =
            if (!UserMemory.enabled() || documentPath.isNullOrBlank()) null else forDocument(File(documentPath))

fun splitCitation(citation: String): Pair<String, String?>

Splits chat@address into its two halves; a bare chat name has a null address.

        fun splitCitation(citation: String): Pair<String, String?> {
            val at = citation.lastIndexOf('@')
            return if (at <= 0) citation.trim() to null else citation.substring(0, at).trim() to citation.substring(at + 1).trim().ifBlank { null }
        }
    }

data class Turn(val chat: String, val node: String, val ts: Long, val first: String, val last: String,

One saved turn: the chat, its session-tree node, when it was saved, the stretch of blocks it added (as addresses), what the person asked, and whether it is on the chat's current branch (false: rewound away).

    data class Turn(val chat: String, val node: String, val ts: Long, val first: String, val last: String,
                    val request: String, val onHead: Boolean) {

val citation: String get() = "$chat@$first"

fun turnsSince(since: Long): List<Turn>

Every turn the person started that was saved after since (epoch ms), oldest first.

    fun turnsSince(since: Long): List<Turn> = synchronized(this) {
        store.all().flatMap { (id, tree) ->
            tree.nodes.mapNotNull { n ->
                val ts = n.ts ?: return@mapNotNull null
                val request = n.user?.takeIf { it.isNotBlank() } ?: return@mapNotNull null
                if (ts <= since || n.blocks.isEmpty()) return@mapNotNull null
                Turn(id, n.id, ts, n.blocks.first().address, n.blocks.last().address, request, n.onHead)
            }
        }.sortedBy { it.ts }
    }

fun chats(): List<String>

The chats, as name · N turns.

    fun chats(): List<String> = synchronized(this) { store.all().map { (id, tree) -> "$id · ${tree.nodes.size} turns" } }

fun search(query: String, chat: String = "", limit: Int = 30): List<Hit>

Blocks containing query (case-insensitive), at most limit, walking the chats in document-name order. chat narrows to one chat, or to chats whose name starts with it.

    fun search(query: String, chat: String = "", limit: Int = 30): List<Hit> = synchronized(this) {
        val out = mutableListOf<Hit>()
        for ((id, tree) in store.all()) {
            if (chat.isNotBlank() && id != chat && !id.startsWith(chat)) continue
            for (h in ChatLogViews.search(tree, query, null, limit - out.size)) {
                out += Hit(id, h.address, h.kind, h.snippet)
            }
            if (out.size >= limit) break
        }
        out
    }

fun read(chat: String, from: String, to: String, maxChars: Int): ChatLogViews.Page

A stretch of chat as text, each block headed by its address. from may be a chat@address citation, in which case chat may be blank. With to blank and from set, reads from from to the end of its branch.

    fun read(chat: String, from: String, to: String, maxChars: Int): ChatLogViews.Page = synchronized(this) {
        val (fromChat, fromAddress) = if ('@' in from) splitCitation(from) else chat to from.ifBlank { null }
        val (_, toAddress) = if ('@' in to) splitCitation(to) else chat to to.ifBlank { null }
        val name = chat.ifBlank { fromChat }
        require(name.isNotBlank()) { "name a chat, or pass from as chat@address" }
        val tree = store.tree(name)
        val end = toAddress ?: if (fromAddress != null) {
            // From a block to the end of the branch that block is on: the tip below it, else the head.
            val node = fromAddress.substringBeforeLast(".b")
            tree.tips.firstOrNull { tip -> tree.pathTo(tip.id).any { it.id == node } }?.blocks?.lastOrNull()?.address
        } else null
        ChatLogViews.read(tree, fromAddress, end, "text", maxChars)
    }
}