diff --git a/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/domain/SenderPosition.kt b/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/domain/SenderPosition.kt index 0e16df91..2a2cf2f6 100644 --- a/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/domain/SenderPosition.kt +++ b/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/domain/SenderPosition.kt @@ -24,8 +24,8 @@ internal fun isPresent(peers: List, ghosts: List, insta peers.any { it.instanceId == instanceId } || ghosts.any { it.instanceId == instanceId } /** - * Who a join link follows: the instance it names, else the instance of the sender that was active last. A ghost with more - * time left has left later. + * Who a join link follows: the instance it names, else the instance of the sender that was active last, never one of + * their AI agents. A ghost with more time left has left later. * * @return null if the sender is fully away */ @@ -36,8 +36,9 @@ internal fun joinFollowInstance( instance: String?, ): String? { if (instance != null && isPresent(peers, ghosts, instance)) return instance - return peers.filter { it.accountId == from }.maxByOrNull { it.lastActiveAt.orEmpty() }?.instanceId - ?: ghosts.filter { it.accountId == from }.maxByOrNull { it.remainingMs }?.instanceId + // an agent of the sender has no cursor or viewport, and while it edits it is the one that was active last + return peers.filter { it.accountId == from && it.agent == null }.maxByOrNull { it.lastActiveAt.orEmpty() }?.instanceId + ?: ghosts.filter { it.accountId == from && it.agent == null }.maxByOrNull { it.remainingMs }?.instanceId } /** @return null while there is nothing to look at: no connection of the instance has a viewport or a cursor */ diff --git a/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/ui/compose/OnlineUsersAction.kt b/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/ui/compose/OnlineUsersAction.kt index 2554587c..f11eccbe 100644 --- a/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/ui/compose/OnlineUsersAction.kt +++ b/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/ui/compose/OnlineUsersAction.kt @@ -14,9 +14,13 @@ import androidx.compose.foundation.border import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.SmartToy +import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable @@ -27,12 +31,14 @@ import androidx.compose.ui.unit.dp import de.markusressel.mkdocseditor.feature.avatar.ui.compose.UserAvatar import de.markusressel.mkdocseditor.feature.theme.MkDocsEditorTheme import de.markusressel.mkdocseditor.util.compose.CombinedPreview +import de.markusressel.mkdocsrestclient.sync.automerge.RemoteAgent import de.markusressel.mkdocsrestclient.sync.automerge.RemotePeer private const val MAX_VISIBLE_USERS = 3 /** - * Shows the other client instances in this document as colored circles with the initial of their user + * Shows the other client instances in this document as colored circles with the initial of their user, AI agents as a + * robot * * @param remotePeers the other connections in this document */ @@ -50,13 +56,24 @@ internal fun OnlineUsersAction( ) DropdownMenu(expanded = menuOpen, onDismissRequest = { menuOpen = false }) { remotePeers.instances().forEach { user -> - DropdownMenuItem( - text = { Text(stringResource(R.string.follow_user, user.label)) }, - onClick = { - menuOpen = false - onFollowClicked(user.instanceId) - }, - ) + val agent = user.agent + if (agent != null) { + // an agent has no cursor or viewport to follow + DropdownMenuItem( + text = { Text(stringResource(R.string.online_agent, agent.name, user.name)) }, + leadingIcon = { AgentAvatar(modifier = Modifier.size(24.dp), color = user.composeColor) }, + enabled = false, + onClick = {}, + ) + } else { + DropdownMenuItem( + text = { Text(stringResource(R.string.follow_user, user.label)) }, + onClick = { + menuOpen = false + onFollowClicked(user.instanceId) + }, + ) + } } } } @@ -75,12 +92,16 @@ private fun OnlineUsersBadges( verticalAlignment = Alignment.CenterVertically, ) { users.take(MAX_VISIBLE_USERS).forEach { user -> - UserAvatar( - modifier = Modifier.badge(), - name = user.name, - color = user.composeColor, - avatar = user.avatarModel, - ) + if (user.agent != null) { + AgentAvatar(modifier = Modifier.badge(), color = user.composeColor) + } else { + UserAvatar( + modifier = Modifier.badge(), + name = user.name, + color = user.composeColor, + avatar = user.avatarModel, + ) + } } if (users.size > MAX_VISIBLE_USERS) { @@ -113,6 +134,22 @@ private fun OnlineUserBadge( } } +/** An AI agent: a robot in the color of its person */ +@Composable +private fun AgentAvatar(modifier: Modifier = Modifier, color: Color) { + Box( + modifier = modifier.background(color, CircleShape), + contentAlignment = Alignment.Center, + ) { + Icon( + imageVector = Icons.Outlined.SmartToy, + contentDescription = null, + tint = labelTextColorOn(color), + modifier = Modifier.fillMaxSize(0.7f), + ) + } +} + /** the ring keeps overlapping badges apart */ @Composable private fun Modifier.badge() = this @@ -132,6 +169,7 @@ private fun OnlineUsersActionPreview() { displayName = null, color = null, selections = emptyList(), + agent = if (index == 1) RemoteAgent(tokenId = "t1", name = "Claude Code") else null, ) } ) diff --git a/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/ui/compose/RemotePeerColor.kt b/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/ui/compose/RemotePeerColor.kt index 55ec2c53..e3c537f9 100644 --- a/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/ui/compose/RemotePeerColor.kt +++ b/app/src/main/java/de/markusressel/mkdocseditor/feature/editor/ui/compose/RemotePeerColor.kt @@ -146,4 +146,5 @@ internal fun RemoteGhost.asPeer() = RemotePeer( deviceName = deviceName, instanceColor = instanceColor, avatar = avatar, + agent = agent, ) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9ef122b0..c83f6b7e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -141,6 +141,7 @@ Open in browser More Follow %1$s + %1$s (agent of %2$s) Following %1$s Following %1$s (paused) Stop following diff --git a/app/src/test/java/de/markusressel/mkdocseditor/feature/editor/domain/SenderPositionTest.kt b/app/src/test/java/de/markusressel/mkdocseditor/feature/editor/domain/SenderPositionTest.kt index 8414b576..d6b94e07 100644 --- a/app/src/test/java/de/markusressel/mkdocseditor/feature/editor/domain/SenderPositionTest.kt +++ b/app/src/test/java/de/markusressel/mkdocseditor/feature/editor/domain/SenderPositionTest.kt @@ -1,5 +1,6 @@ package de.markusressel.mkdocseditor.feature.editor.domain +import de.markusressel.mkdocsrestclient.sync.automerge.RemoteAgent import de.markusressel.mkdocsrestclient.sync.automerge.RemoteGhost import de.markusressel.mkdocsrestclient.sync.automerge.RemotePeer import de.markusressel.mkdocsrestclient.sync.automerge.RemotePosition @@ -20,6 +21,7 @@ class SenderPositionTest { lastActiveAt: String? = null, viewport: RemoteViewport? = null, instance: String = account, + agent: RemoteAgent? = null, ) = RemotePeer( clientId = "c", username = "u", @@ -30,6 +32,7 @@ class SenderPositionTest { lastActiveAt = lastActiveAt, viewport = viewport, instanceId = instance, + agent = agent, ) private fun ghost( @@ -49,6 +52,15 @@ class SenderPositionTest { remainingMs = remainingMs, ) + @Test + fun `a join link follows the sender and not their agent that edits right now`() { + val peers = listOf( + peer("sender", 1, "2026-11-05T10:00:00Z", instance = "phone"), + peer("sender", null, "2026-11-05T10:05:00Z", instance = "agent", agent = RemoteAgent("t1", "Claude Code")), + ) + assertEquals("phone", joinFollowInstance(peers, emptyList(), "sender", instance = null)) + } + @Test fun `follows the connection of the instance that moved last`() { val peers = listOf( diff --git a/rest/src/main/java/de/markusressel/mkdocsrestclient/sync/automerge/DocumentSyncSession.kt b/rest/src/main/java/de/markusressel/mkdocsrestclient/sync/automerge/DocumentSyncSession.kt index b45e0d53..882689d4 100644 --- a/rest/src/main/java/de/markusressel/mkdocsrestclient/sync/automerge/DocumentSyncSession.kt +++ b/rest/src/main/java/de/markusressel/mkdocsrestclient/sync/automerge/DocumentSyncSession.kt @@ -2,6 +2,7 @@ package de.markusressel.mkdocsrestclient.sync.automerge import de.markusressel.mkdocsrestclient.moshi import de.markusressel.mkdocsrestclient.sync.automerge.protocol.ActorInfo +import de.markusressel.mkdocsrestclient.sync.automerge.protocol.AgentMessage import de.markusressel.mkdocsrestclient.sync.automerge.protocol.ActorMessage import de.markusressel.mkdocsrestclient.sync.automerge.protocol.DocumentRenamedMessage import de.markusressel.mkdocsrestclient.sync.automerge.protocol.ErrorMessage @@ -59,6 +60,15 @@ data class RemotePeer( val instanceColor: String? = null, /** version of the profile picture of [accountId], null without one */ val avatar: String? = null, + /** set when this is an AI agent of [accountId], which has no cursor and no viewport */ + val agent: RemoteAgent? = null, +) + +/** An AI agent that works with an app token of an account */ +data class RemoteAgent( + val tokenId: String, + /** the name of the app token */ + val name: String, ) /** The text another user is looking at, as UTF-16 indices of the first and the last visible character */ @@ -90,8 +100,11 @@ data class RemoteGhost( val deviceName: String? = null, val instanceColor: String? = null, val avatar: String? = null, + val agent: RemoteAgent? = null, ) +private fun AgentMessage.toRemote() = RemoteAgent(tokenId, name) + /** A new id of a client instance, which the server expects as 32 lowercase hex characters */ fun newInstanceId(random: SecureRandom = SecureRandom()): String = ByteArray(16).also { random.nextBytes(it) }.toHex() @@ -198,6 +211,7 @@ class DocumentSyncSession( val deviceName: String?, val instanceColor: String?, val avatar: String?, + val agent: RemoteAgent?, ) private class Peer( @@ -356,7 +370,7 @@ class DocumentSyncSession( MessageType.PRESENCE_SNAPSHOT -> onPresenceSnapshot(parse(text)) MessageType.PRESENCE_LEAVE -> onPresenceLeave(parse(text)) MessageType.ACTOR -> parse(text).let { - actors[it.actorId] = ActorInfo(it.actorId, it.username, it.displayName, it.color) + actors[it.actorId] = ActorInfo(it.actorId, it.username, it.displayName, it.color, it.agent) reportAuthorColors() } @@ -637,6 +651,7 @@ class DocumentSyncSession( deviceName = ghost.deviceName, instanceColor = ghost.instanceColor, avatar = ghost.avatar, + agent = ghost.agent?.toRemote(), ) } notifyGhosts() @@ -664,6 +679,7 @@ class DocumentSyncSession( deviceName = message.deviceName, instanceColor = message.instanceColor, avatar = message.avatar ?: left?.presence?.avatar, + agent = left?.presence?.agent?.toRemote(), ) notifyGhosts() } @@ -686,6 +702,7 @@ class DocumentSyncSession( deviceName = ghost.deviceName, instanceColor = ghost.instanceColor, avatar = ghost.avatar, + agent = ghost.agent, ) } ) @@ -785,6 +802,7 @@ class DocumentSyncSession( deviceName = peer.presence.deviceName, instanceColor = peer.presence.instanceColor, avatar = peer.presence.avatar, + agent = peer.presence.agent?.toRemote(), ) } ) diff --git a/rest/src/main/java/de/markusressel/mkdocsrestclient/sync/automerge/protocol/SyncProtocolMessages.kt b/rest/src/main/java/de/markusressel/mkdocsrestclient/sync/automerge/protocol/SyncProtocolMessages.kt index 74d1f301..5990f321 100644 --- a/rest/src/main/java/de/markusressel/mkdocsrestclient/sync/automerge/protocol/SyncProtocolMessages.kt +++ b/rest/src/main/java/de/markusressel/mkdocsrestclient/sync/automerge/protocol/SyncProtocolMessages.kt @@ -83,6 +83,8 @@ data class PresenceMessage( val instanceColor: String? = null, /** version of the profile picture of the account, null without one */ val avatar: String? = null, + /** set when an AI agent with an app token of the account is this connection */ + val agent: AgentMessage? = null, val type: String = MessageType.PRESENCE, ) @@ -96,6 +98,14 @@ data class LocalPresenceMessage( val type: String = MessageType.PRESENCE, ) +/** An AI agent that works with an app token of an account. It has no cursor and no viewport. */ +@JsonClass(generateAdapter = true) +data class AgentMessage( + val tokenId: String, + /** the name of the app token */ + val name: String, +) + /** Cursors of the first and the last visible character */ @JsonClass(generateAdapter = true) data class ViewportMessage( @@ -138,6 +148,7 @@ data class GhostMessage( val selections: List = emptyList(), val viewport: ViewportMessage? = null, val avatar: String? = null, + val agent: AgentMessage? = null, ) @JsonClass(generateAdapter = true) @@ -173,6 +184,8 @@ data class ActorInfo( val username: String, val displayName: String? = null, val color: String? = null, + /** set when the changes of this actor come from an AI agent of the account */ + val agent: AgentMessage? = null, ) @JsonClass(generateAdapter = true) @@ -181,6 +194,7 @@ data class ActorMessage( val username: String, val displayName: String? = null, val color: String? = null, + val agent: AgentMessage? = null, val type: String = MessageType.ACTOR, ) diff --git a/rest/src/test/java/de/markusressel/mkdocsrestclient/sync/automerge/DocumentSyncSessionTest.kt b/rest/src/test/java/de/markusressel/mkdocsrestclient/sync/automerge/DocumentSyncSessionTest.kt index a2115b6c..fb5bf328 100644 --- a/rest/src/test/java/de/markusressel/mkdocsrestclient/sync/automerge/DocumentSyncSessionTest.kt +++ b/rest/src/test/java/de/markusressel/mkdocsrestclient/sync/automerge/DocumentSyncSessionTest.kt @@ -550,6 +550,26 @@ class DocumentSyncSessionTest { assertEquals("fedcba9876543210", listener.ghosts.single { it.instanceId == "i2" }.avatar) } + @Test + fun `peers of a snapshot and of a presence name their agent`() { + val listener = EditorListener() + val (server, session) = connectedSession(listener) + + session.onMessage( + """{"type":"presence-snapshot","documentId":"${server.documentId}","peers":[ + {"documentId":"${server.documentId}","clientId":"agent-0123456789ab","instanceId":"i1","accountId":"a1","username":"bob","deviceName":"Claude Code", + "agent":{"tokenId":"t1","name":"Claude Code"},"selections":[]} + ]}""" + ) + assertEquals(RemoteAgent("t1", "Claude Code"), listener.peers.single().agent) + + session.onMessage( + """{"type":"presence","documentId":"${server.documentId}","clientId":"c2","instanceId":"i2","accountId":"a1","username":"bob", + "selections":[]}""" + ) + assertEquals(null, listener.peers.single { it.instanceId == "i2" }.agent) + } + @Test fun `hello names the instance and its color, the same after a reconnect`() { val session = DocumentSyncSession("doc", InMemoryDocumentStore(), EditorListener(), instanceId = "0f".repeat(16))