Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

feat(chats): read the Markdown chat export back into a history (#83)

Durganshuc74c5f8a463b
diff --git a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
index 69992aa..04d3989 100644
--- a/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
+++ b/app/src/main/java/com/gotcha/agent/ChatViewModel.kt
@@ -15,11 +15,13 @@ import com.gotcha.audio.CompletionFeedback
import com.gotcha.audio.SttEngine
import com.gotcha.audio.TtsEngine
import com.gotcha.data.ChatHistoryRepository
+import com.gotcha.data.ChatMarkdown
import com.gotcha.data.ChatSession
import com.gotcha.data.LlmProvider
import com.gotcha.data.RunSummary
import com.gotcha.data.Settings
import com.gotcha.data.SettingsRepository
+import com.gotcha.data.documentPromptText
import com.gotcha.i18n.Language
import com.gotcha.i18n.SpokenPhrases
import com.gotcha.llm.ChatMessage
@@ -692,35 +694,6 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
}
}
- /**
- * The user's prompt portion of a document message's text part, or null when
- * [content] is not a document message. Document messages put
- * `[Attached file: …]` on its own line, so everything before that marker is
- * the user's own words.
- */
- private fun documentPromptText(content: String): String? {
- val index = content.indexOf(ATTACHED_FILE_MARKER)
- return if (index >= 0) content.substring(0, index).trim() else null
- }
-
- /** How many `[Attached file: …]` sections a user message's text part carries. */
- private fun countAttachedFiles(content: String): Int =
- content.split(ATTACHED_FILE_MARKER).size - 1
-
- /**
- * The export's note for a user message's attachments, e.g. "Image attached"
- * or "3 images, 1 document attached"; null when there are none.
- */
- private fun exportAttachmentLabel(documents: Int, images: Int): String? = when {
- documents == 0 && images == 0 -> null
- documents == 1 && images == 0 -> "Document attached"
- documents == 0 && images == 1 -> "Image attached"
- else -> listOfNotNull(
- images.takeIf { it > 0 }?.let { plural(it, "image") },
- documents.takeIf { it > 0 }?.let { plural(it, "document") }
- ).joinToString(", ") + " attached"
- }
-
/** Busy-marking + agent run + NonCancellable cleanup, from the old sendMessage body. */
private suspend fun executeRun(agent: AgentMode, runningId: String) {
val runningTitle = engineTranscript.firstOrNull { it.kind == MessageKind.USER }
@@ -1798,66 +1771,12 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
}
fun exportChat() {
- val sessionId = agentEngine.sessionId ?: "unknown"
- val sb = StringBuilder()
- sb.appendLine("# Gotcha Chat Export")
- sb.appendLine("**Session:** $sessionId")
- sb.appendLine(
- "**Date:** ${java.text.SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss",
- java.util.Locale.US
- ).format(java.util.Date())}"
+ val markdown = ChatMarkdown.export(
+ history = agentEngine.history.toList(),
+ sessionId = agentEngine.sessionId ?: "unknown",
+ title = agentEngine.currentTitle()
)
- sb.appendLine("**Messages:** ${agentEngine.history.size}")
- sb.appendLine()
- sb.appendLine("---")
- sb.appendLine()
-
- for (msg in agentEngine.history) {
- val role = msg.role
- val text = msg.textContent
-
- when (role) {
- "user" -> {
- val docPrompt = documentPromptText(text)
- sb.appendLine("### User")
- val prompt = docPrompt ?: text
- if (prompt.isNotBlank()) sb.appendLine(prompt)
- exportAttachmentLabel(documents = countAttachedFiles(text), images = msg.imageCount)
- ?.let { sb.appendLine("*($it)*") }
- sb.appendLine()
- }
- "assistant" -> {
- sb.appendLine("### Assistant")
- if (text.isNotBlank()) sb.appendLine(text)
- val calls = msg.toolCalls
- if (!calls.isNullOrEmpty()) {
- sb.appendLine()
- sb.appendLine("**Called tools:**")
- for (call in calls) {
- sb.appendLine("- `${call.function.name}(${call.function.arguments.take(200)})`")
- }
- }
- sb.appendLine()
- }
- "tool" -> {
- if (text.startsWith("SUBAGENT_STEPS:")) {
- appendSubAgentExport(sb, text)
- } else {
- sb.appendLine("### Tool Result")
- sb.appendLine(text.ifEmpty { "(no result)" })
- }
- sb.appendLine()
- }
- "system" -> {
- sb.appendLine("### System")
- sb.appendLine(text.ifEmpty { "(system message)" })
- sb.appendLine()
- }
- }
- }
-
- _exportContent.tryEmit(sb.toString())
+ _exportContent.tryEmit(markdown)
}
/**
@@ -1901,46 +1820,12 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), A
return synthesizeRunSummariesFromHistory(snapshot, settings.model, engineAgent.name)
}
- /** Formats a SUBAGENT_STEPS tool message (description, steps, result) for chat export. */
- private fun appendSubAgentExport(sb: StringBuilder, text: String) {
- val descEnd = text.indexOf('\n', "SUBAGENT_STEPS:".length)
- val desc = if (descEnd > 0) {
- text.substring("SUBAGENT_STEPS:".length, descEnd)
- } else {
- text.substring("SUBAGENT_STEPS:".length)
- }
- sb.appendLine("### Sub-Agent: $desc")
- val rest = if (descEnd > 0) text.substring(descEnd + 1) else ""
- val stepsMarker = "── Steps ──\n"
- val resultMarker = "\n── Result ──\n"
- if (!rest.startsWith(stepsMarker)) {
- sb.appendLine(rest)
- return
- }
- val afterSteps = rest.removePrefix(stepsMarker)
- val resIdx = afterSteps.indexOf(resultMarker)
- if (resIdx < 0) {
- sb.appendLine(afterSteps)
- return
- }
- val steps = afterSteps.substring(0, resIdx).split("\n").filter { it.isNotBlank() }
- val answer = afterSteps.substring(resIdx + resultMarker.length)
- sb.appendLine()
- sb.appendLine("**Steps:**")
- for (s in steps) sb.appendLine("- $s")
- sb.appendLine()
- sb.appendLine("**Result:**")
- sb.appendLine(answer)
- }
-
private companion object {
const val GATE_TIMEOUT_MS = 120_000L
/** Set once the one-time notification-permission ask has been shown. */
const val KEY_NOTIFICATION_PERMISSION_ASKED = "chat_notification_permission_asked"
- /** Starts each document section in a user message; see [documentPromptText]. */
- const val ATTACHED_FILE_MARKER = "\n[Attached file:"
const val MIGRATED_CHAT_DIRS_KEY = "migrated_chat_dirs_v1"
}
}
diff --git a/app/src/main/java/com/gotcha/data/ChatMarkdown.kt b/app/src/main/java/com/gotcha/data/ChatMarkdown.kt
new file mode 100644
index 0000000..5bcf31f
--- /dev/null
+++ b/app/src/main/java/com/gotcha/data/ChatMarkdown.kt
@@ -0,0 +1,390 @@
+package com.gotcha.data
+
+import com.gotcha.llm.ChatMessage
+import com.gotcha.llm.FunctionCall
+import com.gotcha.llm.ToolCall
+import kotlinx.serialization.json.Json
+import kotlinx.serialization.json.JsonPrimitive
+import kotlinx.serialization.json.buildJsonObject
+import kotlinx.serialization.json.put
+import java.text.SimpleDateFormat
+import java.util.Locale
+import java.util.TimeZone
+
+/**
+ * A byte-order mark some editors put at the start of a saved file. Built from its
+ * code point: formatters rewrite a "\uFEFF" escape into the invisible character.
+ */
+internal val BYTE_ORDER_MARK = Char(0xFEFF).toString()
+
+/** Starts each document section in a user message; see [documentPromptText]. */
+internal const val ATTACHED_FILE_MARKER = "\n[Attached file:"
+
+/**
+ * The user's prompt portion of a document message's text part, or null when
+ * [content] is not a document message. Document messages put
+ * `[Attached file: …]` on its own line, so everything before that marker is
+ * the user's own words.
+ */
+internal fun documentPromptText(content: String): String? {
+ val index = content.indexOf(ATTACHED_FILE_MARKER)
+ return if (index >= 0) content.substring(0, index).trim() else null
+}
+
+/** How many `[Attached file: …]` sections a user message's text part carries. */
+internal fun countAttachedFiles(content: String): Int =
+ content.split(ATTACHED_FILE_MARKER).size - 1
+
+/**
+ * The Markdown chat export ("Export chat" in the chat menu) and its reader
+ * (issue #83). The export is written for people to read, so it is lossy:
+ * images and documents become a count, tool-call arguments are cut at
+ * [TOOL_ARGS_LIMIT] characters and the on-screen transcript is not included.
+ * [parse] rebuilds as much of the conversation as the export carries, so a
+ * chat shared as Markdown can be imported and continued, and an imported chat
+ * exports back to the same Markdown. For a lossless copy use [ChatArchive].
+ */
+object ChatMarkdown {
+
+ const val HEADER = "# Gotcha Chat Export"
+ const val TOOL_ARGS_LIMIT = 200
+
+ /** Prefix of the synthetic tool-call ids given to calls read back from Markdown. */
+ const val IMPORTED_CALL_PREFIX = "imported_call_"
+
+ /** Name of the call invented for a tool result the export shows no call for. */
+ const val UNKNOWN_TOOL = "imported_result"
+
+ private const val DATE_PATTERN = "yyyy-MM-dd HH:mm:ss"
+ private const val SUBAGENT_PREFIX = "SUBAGENT_STEPS:"
+ private const val STEPS_MARKER = "── Steps ──\n"
+ private const val RESULT_MARKER = "\n── Result ──\n"
+ private const val NO_RESULT = "(no result)"
+ private const val NO_SYSTEM_TEXT = "(system message)"
+ private const val CALLED_TOOLS = "**Called tools:**"
+
+ private val json = Json { ignoreUnknownKeys = true }
+ private val HEADER_FIELD = Regex("""\*\*(\w+):\*\*(.*)""")
+
+ fun export(
+ history: List<ChatMessage>,
+ sessionId: String,
+ title: String?,
+ now: Long = System.currentTimeMillis(),
+ zone: TimeZone = TimeZone.getDefault()
+ ): String {
+ val sb = StringBuilder()
+ sb.appendLine(HEADER)
+ sb.appendLine("**Session:** $sessionId")
+ if (!title.isNullOrBlank()) sb.appendLine("**Title:** ${title.lineSequence().first()}")
+ sb.appendLine("**Date:** ${dateFormat(zone).format(java.util.Date(now))}")
+ sb.appendLine("**Messages:** ${history.size}")
+ sb.appendLine()
+ sb.appendLine("---")
+ sb.appendLine()
+
+ for (msg in history) {
+ val text = msg.textContent
+ when (msg.role) {
+ "user" -> {
+ sb.appendLine("### User")
+ val prompt = documentPromptText(text) ?: text
+ if (prompt.isNotBlank()) sb.appendLine(prompt)
+ attachmentLabel(documents = countAttachedFiles(text), images = msg.imageCount)
+ ?.let { sb.appendLine("*($it)*") }
+ sb.appendLine()
+ }
+ "assistant" -> {
+ sb.appendLine("### Assistant")
+ if (text.isNotBlank()) sb.appendLine(text)
+ val calls = msg.toolCalls
+ if (!calls.isNullOrEmpty()) {
+ sb.appendLine()
+ sb.appendLine(CALLED_TOOLS)
+ for (call in calls) {
+ sb.appendLine("- `${call.function.name}(${call.function.arguments.take(TOOL_ARGS_LIMIT)})`")
+ }
+ }
+ sb.appendLine()
+ }
+ "tool" -> {
+ if (text.startsWith(SUBAGENT_PREFIX)) {
+ appendSubAgent(sb, text)
+ } else {
+ sb.appendLine("### Tool Result")
+ sb.appendLine(text.ifEmpty { NO_RESULT })
+ }
+ sb.appendLine()
+ }
+ "system" -> {
+ sb.appendLine("### System")
+ sb.appendLine(text.ifEmpty { NO_SYSTEM_TEXT })
+ sb.appendLine()
+ }
+ }
+ }
+ return sb.toString()
+ }
+
+ /** True when [text] looks like a Gotcha Markdown export (its first line is [HEADER]). */
+ fun looksLikeExport(text: String): Boolean =
+ text.removePrefix(BYTE_ORDER_MARK).trimStart().lineSequence().firstOrNull()?.trim() == HEADER
+
+ /** A Markdown export read back: the header fields and the rebuilt LLM history. */
+ data class Parsed(
+ val sessionId: String?,
+ val title: String?,
+ val exportedAt: Long?,
+ /** The "**Messages:**" count the export declared, or null when missing. */
+ val declaredCount: Int?,
+ /** How many message sections were found, to compare with [declaredCount]. */
+ val sectionCount: Int,
+ val messages: List<ChatMessage>,
+ /** Anything that was read with a guess, for the import report. */
+ val warnings: List<String>
+ )
+
+ /**
+ * Reads a [export] back into a history. Only the exact section headings the
+ * export writes ("### User", "### Assistant", "### Tool Result",
+ * "### System", "### Sub-Agent: …") start a message, so Markdown inside a
+ * reply (its own "### Steps" heading, say) stays part of that reply.
+ *
+ * Tool calls get synthetic ids and are paired with the results that follow
+ * them in order, so the rebuilt history is one a provider accepts: a call
+ * without a result gets a stub result and a result without a call gets a
+ * stub call, each noted in [Parsed.warnings].
+ *
+ * Throws [IllegalArgumentException] when [text] is not an export at all.
+ */
+ fun parse(text: String, zone: TimeZone = TimeZone.getDefault()): Parsed {
+ require(looksLikeExport(text)) { "Not a Gotcha chat export: the first line should be \"$HEADER\"." }
+ val lines = text.removePrefix(BYTE_ORDER_MARK).replace("\r\n", "\n").trimStart().lines()
+
+ val warnings = mutableListOf<String>()
+ // Header: everything up to the first section heading.
+ val headerEnd = (1 until lines.size).firstOrNull { sectionOf(lines[it]) != null } ?: lines.size
+ val header = headerFields(lines.subList(1, headerEnd))
+ val sections = sections(lines.subList(headerEnd, lines.size))
+
+ val messages = mutableListOf<ChatMessage>()
+ // Calls of the latest assistant message still waiting for their results.
+ val openCalls = ArrayDeque<ToolCall>()
+ var callCounter = 0
+
+ fun closeOpenCalls() {
+ if (openCalls.isEmpty()) return
+ warnings += "${openCalls.size} tool call(s) had no result in the export; marked as missing."
+ while (openCalls.isNotEmpty()) {
+ messages += ChatMessage(
+ role = "tool",
+ content = JsonPrimitive("(result not included in the export)"),
+ toolCallId = openCalls.removeFirst().id
+ )
+ }
+ }
+
+ fun addToolResult(content: String) {
+ val call = openCalls.removeFirstOrNull() ?: run {
+ // A result with no call before it: give it one so the pair is valid.
+ warnings += "A tool result had no matching call in the export; gave it a placeholder call."
+ val stub =
+ ToolCall("$IMPORTED_CALL_PREFIX${callCounter++}", function = FunctionCall(UNKNOWN_TOOL, "{}"))
+ messages += ChatMessage(role = "assistant", content = JsonPrimitive(""), toolCalls = listOf(stub))
+ stub
+ }
+ messages += ChatMessage(role = "tool", content = JsonPrimitive(content), toolCallId = call.id)
+ }
+
+ for ((section, body) in sections) {
+ when (section) {
+ Section.User -> {
+ closeOpenCalls()
+ messages += ChatMessage(role = "user", content = JsonPrimitive(body))
+ }
+ Section.Assistant -> {
+ closeOpenCalls()
+ val (reply, calls) = splitCalledTools(body)
+ val toolCalls = calls.map { (name, args) ->
+ ToolCall(
+ "$IMPORTED_CALL_PREFIX${callCounter++}",
+ function = FunctionCall(name, validArguments(args))
+ )
+ }
+ messages += ChatMessage(
+ role = "assistant",
+ content = JsonPrimitive(reply),
+ toolCalls = toolCalls.ifEmpty { null }
+ )
+ openCalls.addAll(toolCalls)
+ }
+ Section.ToolResult -> addToolResult(if (body == NO_RESULT) "" else body)
+ is Section.SubAgent -> addToolResult(subAgentContent(section.description, body))
+ Section.System -> {
+ closeOpenCalls()
+ messages += ChatMessage(
+ role = "system",
+ content = JsonPrimitive(if (body == NO_SYSTEM_TEXT) "" else body)
+ )
+ }
+ }
+ }
+ closeOpenCalls()
+
+ val declaredCount = header["Messages"]?.toIntOrNull()
+ if (declaredCount != null && declaredCount != sections.size) {
+ warnings += "The export says $declaredCount messages but ${sections.size} were found; " +
+ "the file may have been edited or cut short."
+ }
+ return Parsed(
+ sessionId = header["Session"]?.takeIf { it != "unknown" },
+ title = header["Title"],
+ exportedAt = header["Date"]?.let { runCatching { dateFormat(zone).parse(it)?.time }.getOrNull() },
+ declaredCount = declaredCount,
+ sectionCount = sections.size,
+ messages = messages,
+ warnings = warnings
+ )
+ }
+
+ /** The header's `**Name:** value` lines as name → value, blank values left out. */
+ private fun headerFields(lines: List<String>): Map<String, String> =
+ lines.mapNotNull { line ->
+ HEADER_FIELD.matchEntire(line.trim())?.destructured?.let { (name, value) -> name to value.trim() }
+ }.filter { it.second.isNotEmpty() }.toMap()
+
+ /** Each section heading with the lines up to the next heading, trimmed. [lines] starts at a heading. */
+ private fun sections(lines: List<String>): List<Pair<Section, String>> {
+ val sections = mutableListOf<Pair<Section, String>>()
+ var body = mutableListOf<String>()
+ var heading: Section? = null
+ fun flush() {
+ heading?.let { sections += it to body.joinToString("\n").trim('\n').trimEnd() }
+ }
+ for (line in lines) {
+ val next = sectionOf(line)
+ if (next == null) {
+ body += line
+ } else {
+ flush()
+ heading = next
+ body = mutableListOf()
+ }
+ }
+ flush()
+ return sections
+ }
+
+ private sealed class Section {
+ object User : Section()
+ object Assistant : Section()
+ object ToolResult : Section()
+ object System : Section()
+ data class SubAgent(val description: String) : Section()
+ }
+
+ private fun sectionOf(line: String): Section? = when {
+ line == "### User" -> Section.User
+ line == "### Assistant" -> Section.Assistant
+ line == "### Tool Result" -> Section.ToolResult
+ line == "### System" -> Section.System
+ line.startsWith("### Sub-Agent: ") -> Section.SubAgent(line.removePrefix("### Sub-Agent: "))
+ else -> null
+ }
+
+ /**
+ * Splits an assistant section into its reply and the `name(args)` pairs of
+ * its trailing "**Called tools:**" list. A "**Called tools:**" line that is
+ * not followed only by call entries is ordinary reply text.
+ */
+ private fun splitCalledTools(body: String): Pair<String, List<Pair<String, String>>> {
+ val lines = body.lines()
+ val marker = lines.indexOfLast { it == CALLED_TOOLS }
+ if (marker < 0) return body to emptyList()
+ val entries = lines.drop(marker + 1).filter { it.isNotBlank() }
+ val calls = entries.map { entry ->
+ val inner = entry.takeIf { it.startsWith("- `") && it.endsWith(")`") }
+ ?.removePrefix("- `")?.removeSuffix("`")
+ ?: return body to emptyList()
+ val open = inner.indexOf('(')
+ if (open <= 0) return body to emptyList()
+ inner.substring(0, open) to inner.substring(open + 1, inner.length - 1)
+ }
+ if (calls.isEmpty()) return body to emptyList()
+ return lines.take(marker).joinToString("\n").trimEnd() to calls
+ }
+
+ /**
+ * Arguments as the export shows them, when they are still JSON. Arguments
+ * longer than [TOOL_ARGS_LIMIT] were cut and no longer parse; those are kept
+ * as a string field so no provider rejects the history over them.
+ */
+ private fun validArguments(args: String): String {
+ val parses = runCatching { json.parseToJsonElement(args) }.isSuccess
+ return if (parses) args else buildJsonObject { put("truncated_arguments", args) }.toString()
+ }
+
+ /** Rebuilds a sub-agent tool result from its export section; see [appendSubAgent]. */
+ private fun subAgentContent(description: String, body: String): String {
+ // The section is trimmed, so an empty result leaves "**Result:**" as the last line.
+ val resultAt = "$body\n".indexOf("\n**Result:**\n")
+ if (!body.startsWith("**Steps:**") || resultAt < 0) {
+ return "$SUBAGENT_PREFIX$description\n$body"
+ }
+ val steps = body.substring("**Steps:**".length, resultAt)
+ .lines()
+ .filter { it.isNotBlank() }
+ .map { it.removePrefix("- ") }
+ val answer = "$body\n".substring(resultAt + "\n**Result:**\n".length).removeSuffix("\n")
+ return "$SUBAGENT_PREFIX$description\n$STEPS_MARKER${steps.joinToString("\n")}$RESULT_MARKER$answer"
+ }
+
+ /** Formats a SUBAGENT_STEPS tool message (description, steps, result). */
+ private fun appendSubAgent(sb: StringBuilder, text: String) {
+ val descEnd = text.indexOf('\n', SUBAGENT_PREFIX.length)
+ val desc = if (descEnd > 0) {
+ text.substring(SUBAGENT_PREFIX.length, descEnd)
+ } else {
+ text.substring(SUBAGENT_PREFIX.length)
+ }
+ sb.appendLine("### Sub-Agent: $desc")
+ val rest = if (descEnd > 0) text.substring(descEnd + 1) else ""
+ if (!rest.startsWith(STEPS_MARKER)) {
+ sb.appendLine(rest)
+ return
+ }
+ val afterSteps = rest.removePrefix(STEPS_MARKER)
+ val resIdx = afterSteps.indexOf(RESULT_MARKER)
+ if (resIdx < 0) {
+ sb.appendLine(afterSteps)
+ return
+ }
+ val steps = afterSteps.substring(0, resIdx).split("\n").filter { it.isNotBlank() }
+ val answer = afterSteps.substring(resIdx + RESULT_MARKER.length)
+ sb.appendLine()
+ sb.appendLine("**Steps:**")
+ for (s in steps) sb.appendLine("- $s")
+ sb.appendLine()
+ sb.appendLine("**Result:**")
+ sb.appendLine(answer)
+ }
+
+ /**
+ * The export's note for a user message's attachments, e.g. "Image attached"
+ * or "3 images, 1 document attached"; null when there are none.
+ */
+ private fun attachmentLabel(documents: Int, images: Int): String? = when {
+ documents == 0 && images == 0 -> null
+ documents == 1 && images == 0 -> "Document attached"
+ documents == 0 && images == 1 -> "Image attached"
+ else -> listOfNotNull(
+ images.takeIf { it > 0 }?.let { plural(it, "image") },
+ documents.takeIf { it > 0 }?.let { plural(it, "document") }
+ ).joinToString(", ") + " attached"
+ }
+
+ private fun plural(count: Int, noun: String): String = if (count == 1) "1 $noun" else "$count ${noun}s"
+
+ private fun dateFormat(zone: TimeZone) =
+ SimpleDateFormat(DATE_PATTERN, Locale.US).apply { timeZone = zone }
+}
diff --git a/app/src/test/java/com/gotcha/data/ChatMarkdownTest.kt b/app/src/test/java/com/gotcha/data/ChatMarkdownTest.kt
new file mode 100644
index 0000000..cf3910e
--- /dev/null
+++ b/app/src/test/java/com/gotcha/data/ChatMarkdownTest.kt
@@ -0,0 +1,198 @@
+package com.gotcha.data
+
+import com.gotcha.llm.ChatMessage
+import com.gotcha.llm.FunctionCall
+import com.gotcha.llm.ToolCall
+import com.gotcha.llm.documentUserMessage
+import com.gotcha.llm.visionUserMessage
+import kotlinx.serialization.json.JsonPrimitive
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Test
+import java.util.TimeZone
+
+/**
+ * The Markdown export and its reader (issue #83): an export must read back into
+ * a history that exports to the same Markdown, and a history a provider accepts
+ * (every tool call answered, every tool result called for).
+ */
+class ChatMarkdownTest {
+
+ private val utc = TimeZone.getTimeZone("UTC")
+ private val at = 1_790_000_000_000L
+
+ private fun text(role: String, text: String) = ChatMessage(role = role, content = JsonPrimitive(text))
+
+ private fun export(history: List<ChatMessage>, title: String? = "Weather in Berlin") =
+ ChatMarkdown.export(history, "session-1", title, now = at, zone = utc)
+
+ private val conversation = listOf(
+ text("user", "What's the weather?"),
+ ChatMessage(
+ role = "assistant",
+ content = JsonPrimitive("Let me check."),
+ toolCalls = listOf(
+ ToolCall("call_a", function = FunctionCall("get_weather", """{"city":"Berlin"}""")),
+ ToolCall("call_b", function = FunctionCall("get_time", "{}"))
+ )
+ ),
+ ChatMessage(role = "tool", content = JsonPrimitive("18°C, cloudy"), toolCallId = "call_a"),
+ ChatMessage(role = "tool", content = JsonPrimitive(""), toolCallId = "call_b"),
+ text("assistant", "It's 18°C and cloudy.\n\n### Details\nLight wind."),
+ text("system", "Run stopped by the user."),
+ ChatMessage(
+ role = "assistant",
+ content = JsonPrimitive(""),
+ toolCalls = listOf(ToolCall("call_c", function = FunctionCall("delegate_task", """{"task":"café"}""")))
+ ),
+ ChatMessage(
+ role = "tool",
+ content = JsonPrimitive(
+ "SUBAGENT_STEPS:Find a café\n── Steps ──\nopen maps\nsearch cafés\n── Result ──\nCafé Luna, 200 m"
+ ),
+ toolCallId = "call_c"
+ )
+ )
+
+ @Test
+ fun `an export reads back into the header fields`() {
+ val parsed = ChatMarkdown.parse(export(conversation), utc)
+ assertEquals("session-1", parsed.sessionId)
+ assertEquals("Weather in Berlin", parsed.title)
+ assertEquals(at / 1000 * 1000, parsed.exportedAt)
+ assertEquals(conversation.size, parsed.declaredCount)
+ assertEquals(conversation.size, parsed.sectionCount)
+ }
+
+ @Test
+ fun `an imported export exports back to the same markdown`() {
+ val markdown = export(conversation)
+ val parsed = ChatMarkdown.parse(markdown, utc)
+ assertEquals(markdown, export(parsed.messages))
+ }
+
+ @Test
+ fun `roles, text and tool calls survive the round trip`() {
+ val parsed = ChatMarkdown.parse(export(conversation), utc).messages
+ assertEquals(
+ listOf("user", "assistant", "tool", "tool", "assistant", "system", "assistant", "tool"),
+ parsed.map { it.role }
+ )
+ assertEquals("What's the weather?", parsed[0].textContent)
+ assertEquals("Let me check.", parsed[1].textContent)
+ assertEquals(listOf("get_weather", "get_time"), parsed[1].toolCalls!!.map { it.function.name })
+ assertEquals("""{"city":"Berlin"}""", parsed[1].toolCalls!![0].function.arguments)
+ assertEquals("18°C, cloudy", parsed[2].textContent)
+ assertEquals("", parsed[3].textContent)
+ // A heading inside a reply is part of the reply, not a new message.
+ assertEquals("It's 18°C and cloudy.\n\n### Details\nLight wind.", parsed[4].textContent)
+ assertEquals(conversation[7].textContent, parsed[7].textContent)
+ }
+
+ @Test
+ fun `every tool call is paired with a result by id`() {
+ val parsed = ChatMarkdown.parse(export(conversation), utc).messages
+ val callIds = parsed.flatMap { it.toolCalls.orEmpty() }.map { it.id }
+ val resultIds = parsed.filter { it.role == "tool" }.map { it.toolCallId }
+ assertEquals(callIds, resultIds)
+ assertTrue(callIds.all { it.startsWith(ChatMarkdown.IMPORTED_CALL_PREFIX) })
+ }
+
+ @Test
+ fun `a sub-agent result with no reply stays a sub-agent result`() {
+ val history = listOf(
+ text("user", "go"),
+ ChatMessage(
+ role = "tool",
+ content = JsonPrimitive("SUBAGENT_STEPS:Task\n── Steps ──\nstep\n── Result ──\n")
+ )
+ )
+ val parsed = ChatMarkdown.parse(export(history), utc).messages
+ assertEquals("SUBAGENT_STEPS:Task\n── Steps ──\nstep\n── Result ──\n", parsed.last().textContent)
+ }
+
+ @Test
+ fun `a tool result with no call gets a placeholder call and a warning`() {
+ val history = listOf(text("user", "hi"), ChatMessage(role = "tool", content = JsonPrimitive("orphan")))
+ val parsed = ChatMarkdown.parse(export(history), utc)
+ val stub = parsed.messages[1]
+ assertEquals("assistant", stub.role)
+ assertEquals(ChatMarkdown.UNKNOWN_TOOL, stub.toolCalls!!.single().function.name)
+ assertEquals(stub.toolCalls!!.single().id, parsed.messages[2].toolCallId)
+ assertTrue(parsed.warnings.isNotEmpty())
+ }
+
+ @Test
+ fun `a call whose result is missing from the file gets a stub result`() {
+ val markdown = export(conversation.take(3)) // second call's result cut off
+ val parsed = ChatMarkdown.parse(markdown, utc)
+ val calls = parsed.messages[1].toolCalls!!
+ assertEquals(calls.map { it.id }, parsed.messages.filter { it.role == "tool" }.map { it.toolCallId })
+ assertTrue(parsed.warnings.any { "no result" in it })
+ }
+
+ @Test
+ fun `arguments cut short by the export are kept as a string, not broken JSON`() {
+ val long = """{"text":"${"x".repeat(400)}"}"""
+ val history = listOf(
+ text("user", "send it"),
+ ChatMessage(
+ role = "assistant",
+ content = JsonPrimitive(""),
+ toolCalls = listOf(ToolCall("c", function = FunctionCall("send_sms", long)))
+ ),
+ ChatMessage(role = "tool", content = JsonPrimitive("sent"), toolCallId = "c")
+ )
+ val args = ChatMarkdown.parse(export(history), utc).messages[1].toolCalls!!.single().function.arguments
+ val parsed = kotlinx.serialization.json.Json.parseToJsonElement(args)
+ assertTrue("truncated_arguments" in parsed.toString())
+ }
+
+ @Test
+ fun `attachments are exported as a note that reads back as text`() {
+ val history = listOf(
+ visionUserMessage("What is this?", "AAAA", "jpeg"),
+ documentUserMessage("Summarise", "report.pdf", "application/pdf", "Body text")
+ )
+ val markdown = export(history)
+ assertTrue("*(Image attached)*" in markdown)
+ assertTrue("*(Document attached)*" in markdown)
+ assertFalse("Body text" in markdown)
+ val parsed = ChatMarkdown.parse(markdown, utc)
+ assertEquals("What is this?\n*(Image attached)*", parsed.messages[0].textContent)
+ assertEquals(markdown, export(parsed.messages))
+ }
+
+ @Test
+ fun `an export from before titles were written still reads`() {
+ val markdown = export(conversation, title = null)
+ val parsed = ChatMarkdown.parse(markdown, utc)
+ assertNull(parsed.title)
+ assertEquals(conversation.size, parsed.sectionCount)
+ }
+
+ @Test
+ fun `an unknown session id reads as none`() {
+ val markdown = ChatMarkdown.export(listOf(text("user", "hi")), "unknown", null, now = at, zone = utc)
+ assertNull(ChatMarkdown.parse(markdown, utc).sessionId)
+ }
+
+ @Test
+ fun `a count that disagrees with the sections is reported`() {
+ val markdown = export(conversation).replace("**Messages:** ${conversation.size}", "**Messages:** 99")
+ assertTrue(ChatMarkdown.parse(markdown, utc).warnings.any { "99" in it })
+ }
+
+ @Test
+ fun `windows line endings and a byte-order mark are tolerated`() {
+ val markdown = Char(0xFEFF).toString() + export(conversation).replace("\n", "\r\n")
+ assertEquals(conversation.size, ChatMarkdown.parse(markdown, utc).sectionCount)
+ }
+
+ @Test(expected = IllegalArgumentException::class)
+ fun `markdown that isn't an export is refused`() {
+ ChatMarkdown.parse("# My notes\n\n### User\nhello")
+ }
+}