Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CONTEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,19 @@ allowlist).
### Agent Repository

The mobile integration layer that lists agents for the current project so
Session Chat can offer **mentionable agents** under `@`.
Session Chat can offer **selectable agents** in the composer mode picker and
**mentionable agents** under `@`.

Distinct from the composer's selected agent/mode picker and from **Command
Repository**. Mentions do not change which agent owns the turn.
Distinct from **Command Repository**. Mentions do not change which agent owns
the turn.

### mentionable agent

An agent from the server catalog that the user may `@` mention in the Session
Chat composer: not a primary turn-owning agent, and not hidden from `@`
autocomplete.

Distinct from the composer's selected Build/Plan (or other) mode.
Distinct from the composer's selected turn-owning agent.
_Avoid_: subagent (as the product name for this concept).

### agent mention
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ import io.ktor.client.plugins.ResponseException
import io.ktor.serialization.JsonConvertException

class AgentDomainAdapter {
fun toSelectableAgents(dtos: List<AgentDto>): List<SelectableAgent> =
dtos.mapNotNull { dto ->
selectableAgent(
id = dto.name,
mode = dto.mode,
hidden = dto.hidden,
description = dto.description,
)
}

fun toSelectableAgentsFromV2(dtos: List<V2AgentDto>): List<SelectableAgent> =
dtos.mapNotNull { dto ->
selectableAgent(
id = dto.id,
mode = dto.mode,
hidden = dto.hidden,
description = dto.description,
)
}

fun toMentionableAgents(dtos: List<AgentDto>): List<MentionableAgent> =
dtos.mapNotNull { dto ->
if (dto.mode.equals("primary", ignoreCase = true)) return@mapNotNull null
Expand All @@ -26,6 +46,22 @@ class AgentDomainAdapter {
)
}

private fun selectableAgent(
id: String,
mode: String,
hidden: Boolean?,
description: String?,
): SelectableAgent? {
val normalized = mode.lowercase()
if (normalized != "primary" && normalized != "all") return null
if (hidden == true) return null
return SelectableAgent(
id = id,
displayName = SelectableAgent.displayNameFor(id),
description = description?.takeIf { it.isNotBlank() },
)
}

fun mapError(throwable: Throwable): AgentError =
when (throwable) {
is AgentError -> throwable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package dev.opencode.mobile.domain.agent

import java.util.Locale

data class SelectableAgent(
val id: String,
val displayName: String,
val description: String? = null,
) {
companion object {
const val DefaultId = "build"

val Fallback =
listOf(
SelectableAgent(id = "build", displayName = "Build"),
SelectableAgent(id = "plan", displayName = "Plan"),
)

fun displayNameFor(id: String): String =
id
.split('-', '_')
.filter { it.isNotEmpty() }
.joinToString(" ") { word ->
word.replaceFirstChar { ch ->
if (ch.isLowerCase()) ch.titlecase(Locale.US) else ch.toString()
}
}.ifEmpty { id }
}
}

data class MentionableAgent(
val name: String,
val description: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.opencode.mobile.domain.agent

interface AgentRepository {
suspend fun listSelectableAgents(directory: String? = null): List<SelectableAgent>

suspend fun listMentionableAgents(directory: String? = null): List<MentionableAgent>
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ class DefaultAgentRepository
private val adapter: AgentDomainAdapter,
private val activeConnectionManager: ActiveConnectionManager? = null,
) : AgentRepository {
override suspend fun listSelectableAgents(directory: String?): List<SelectableAgent> =
try {
if (activeConnectionManager?.currentProtocolOrProbe() == ServerProtocol.V2) {
adapter.toSelectableAgentsFromV2(api.listAgentsV2(directory))
} else {
adapter.toSelectableAgents(api.listAgents(directory))
}
} catch (throwable: Throwable) {
throw adapter.mapError(throwable)
}

override suspend fun listMentionableAgents(directory: String?): List<MentionableAgent> =
try {
if (activeConnectionManager?.currentProtocolOrProbe() == ServerProtocol.V2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ fun SessionChatScreen(
attachments =
uiState.composer.attachments.map(AttachmentPresentationMapper::from),
selectedModelLabel = uiState.composer.selectedModelLabel,
selectedModeLabel = uiState.composer.selectedMode,
selectedModeLabel = uiState.composer.selectedModeLabel,
selectedVariantLabel = uiState.composer.selectedVariantLabel,
showVariant = uiState.composer.canSelectVariant,
sendEnabled =
Expand Down Expand Up @@ -780,6 +780,7 @@ fun SessionChatScreen(
modelOptions = uiState.composer.modelOptions,
selectedModel = uiState.composer.selectedModel?.option,
selectedMode = uiState.composer.selectedMode,
selectableAgents = uiState.composer.selectableAgents,
selectedVariantLabel = uiState.composer.selectedVariantLabel,
variantOptions = uiState.composer.variantOptions,
onDismiss = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.opencode.mobile.ui.sessionchat

import dev.opencode.mobile.domain.agent.AgentMention
import dev.opencode.mobile.domain.agent.MentionableAgent
import dev.opencode.mobile.domain.agent.SelectableAgent
import dev.opencode.mobile.domain.command.ClientSlashAction
import dev.opencode.mobile.domain.command.ServerCommand
import dev.opencode.mobile.domain.command.ServerCommandSource
Expand All @@ -25,7 +26,8 @@ data class SessionChatComposerState(
val attachments: List<Attachment> = emptyList(),
val selectedModel: ModelSelection? = null,
val modelOptions: List<ModelSelectionOption> = emptyList(),
val selectedMode: String = SessionChatComposer.AvailableModes.first(),
val selectedMode: String = SelectableAgent.DefaultId,
val selectableAgents: List<SelectableAgent> = SelectableAgent.Fallback,
val selectedVariant: String? = null,
val modelNotice: String? = null,
val isModelSelectionReady: Boolean = false,
Expand All @@ -34,6 +36,11 @@ data class SessionChatComposerState(
val cursor: Int = 0,
val selectionRevision: Int = 0,
) {
val selectedModeLabel: String
get() =
selectableAgents.firstOrNull { it.id.equals(selectedMode, ignoreCase = true) }?.displayName
?: SelectableAgent.displayNameFor(selectedMode)

val selectedModelLabel: String
get() = if (isModelSelectionReady) selectedModel?.modelLabel ?: "Model" else "Loading model…"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1362,7 +1362,6 @@ class SessionChatViewModel

companion object {
private const val TAG = "SessionChat"
val AvailableModes: List<String> = SessionChatComposer.AvailableModes
const val ModelFilterThreshold: Int = SessionChatComposer.ModelFilterThreshold
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.opencode.mobile.ui.sessionchat.region

import android.util.Log
import dev.opencode.mobile.domain.agent.AgentRepository
import dev.opencode.mobile.domain.agent.SelectableAgent
import dev.opencode.mobile.domain.agent.ComposerMentionRouting
import dev.opencode.mobile.domain.agent.ComposerSuggestionMode
import dev.opencode.mobile.domain.command.CommandInvocationCollapser
Expand Down Expand Up @@ -76,6 +77,7 @@ class SessionChatComposer(
private var boundSessionId: String? = null
private var boundWorktree: String? = null
private var loadedCatalog: ModelSelectionCatalog? = null
private var loadedSelectableAgents = false
private var cachedSelection: SessionModelSelection? = null
private var newSessionPreference: SessionModelSelection? = null
private var initialSelectionState = InitialSelectionState.LoadingCache
Expand All @@ -100,6 +102,7 @@ class SessionChatComposer(
loadCachedSelection()
loadServerCommands()
loadMentionableAgents()
loadSelectableAgents()
}
return
}
Expand All @@ -122,6 +125,7 @@ class SessionChatComposer(
boundSessionId = sessionId
boundWorktree = worktree
loadedCatalog = null
loadedSelectableAgents = false
cachedSelection = null
newSessionPreference = null
initialSelectionState = InitialSelectionState.LoadingCache
Expand All @@ -135,6 +139,7 @@ class SessionChatComposer(
loadCachedSelection()
loadServerCommands()
loadMentionableAgents()
loadSelectableAgents()
applyOrLoadDraft(preserve)
}

Expand Down Expand Up @@ -170,6 +175,25 @@ class SessionChatComposer(
}
}

fun loadSelectableAgents(force: Boolean = false) {
val worktree = boundWorktree ?: return
if (!force && loadedSelectableAgents) return
scope.launch {
runCatching {
withContext(ioDispatcher) {
agentRepository.listSelectableAgents(directory = worktree)
}
}.onSuccess { agents ->
if (boundWorktree != worktree) return@onSuccess
loadedSelectableAgents = true
applySelectableAgents(agents)
}.onFailure { throwable ->
if (boundWorktree != worktree) return@onFailure
logW("selectable agent catalog failed: ${throwable.message}")
}
}
}

fun loadMentionableAgents(force: Boolean = false) {
val worktree = boundWorktree ?: return
if (!force && _state.value.mentionableAgents.isNotEmpty()) return
Expand Down Expand Up @@ -537,9 +561,10 @@ class SessionChatComposer(
}

fun selectMode(mode: String) {
if (mode in AvailableModes) {
updateComposer { it.copy(selectedMode = mode) }
}
val match =
_state.value.composer.selectableAgents.firstOrNull { it.id.equals(mode, ignoreCase = true) }
?: return
updateComposer { it.copy(selectedMode = match.id) }
}

fun selectVariant(variant: String) {
Expand Down Expand Up @@ -673,7 +698,7 @@ class SessionChatComposer(
selectedModel.option.modelId,
),
variant = composer.selectedVariant,
mode = agentMode(composer.selectedMode),
mode = composer.selectedMode,
),
directory = worktree,
)
Expand All @@ -690,7 +715,7 @@ class SessionChatComposer(
selectedModel.option.modelId,
),
variant = composer.selectedVariant,
mode = agentMode(composer.selectedMode),
mode = composer.selectedMode,
agentMentions = mentionsForSend,
),
directory = worktree,
Expand Down Expand Up @@ -1032,17 +1057,30 @@ class SessionChatComposer(

private fun composerMetadataLabel(composer: SessionChatComposerState): String =
listOfNotNull(
composer.selectedMode,
composer.selectedModeLabel,
composer.selectedModel?.modelLabel,
composer.selectedVariant,
formatSentTime(System.currentTimeMillis()),
).joinToString(" · ")

private fun agentMode(mode: String): String =
when (mode) {
"Plan" -> "plan"
else -> "build"
private fun applySelectableAgents(agents: List<SelectableAgent>) {
val catalog = agents.ifEmpty { SelectableAgent.Fallback }
updateComposer { composer ->
composer.copy(
selectableAgents = catalog,
selectedMode = resolveSelectedAgentId(composer.selectedMode, catalog),
)
}
}

private fun resolveSelectedAgentId(
current: String,
catalog: List<SelectableAgent>,
): String {
catalog.firstOrNull { it.id.equals(current, ignoreCase = true) }?.let { return it.id }
catalog.firstOrNull { it.id.equals(SelectableAgent.DefaultId, ignoreCase = true) }?.let { return it.id }
return catalog.first().id
}

private fun updateComposer(transform: (SessionChatComposerState) -> SessionChatComposerState) {
_state.update { state ->
Expand Down Expand Up @@ -1129,7 +1167,6 @@ class SessionChatComposer(
companion object {
private const val TAG = "SessionChatComposer"
private const val DRAFT_PERSIST_DEBOUNCE_MS = 300L
val AvailableModes = listOf("Build", "Plan")
const val ModelFilterThreshold = 10
private val SentTimeFormatter = DateTimeFormatter.ofPattern("h:mm a", Locale.US)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.unit.dp
import dev.opencode.mobile.ProductAnalytics
import dev.opencode.mobile.ProductPresentedScreenViewEffect
import dev.opencode.mobile.domain.agent.SelectableAgent
import dev.opencode.mobile.domain.model.ModelSelectionCatalog
import dev.opencode.mobile.domain.model.ModelSelectionOption
import dev.opencode.mobile.domain.model.ModelSelectionProvider
Expand Down Expand Up @@ -62,6 +63,7 @@ internal fun SessionChatPickerSheet(
modelOptions: List<ModelSelectionOption>,
selectedModel: ModelSelectionOption?,
selectedMode: String,
selectableAgents: List<SelectableAgent>,
selectedVariantLabel: String,
variantOptions: List<String>,
onDismiss: () -> Unit,
Expand Down Expand Up @@ -173,16 +175,16 @@ internal fun SessionChatPickerSheet(
onSelectModel = onSelectModel,
)
ComposerPickerKind.Mode ->
SessionChatViewModel.AvailableModes.forEach { mode ->
selectableAgents.forEach { agent ->
OpenCodeBottomSheetEntry(
title = mode,
title = agent.displayName,
endDecorator =
if (mode == selectedMode) {
if (agent.id.equals(selectedMode, ignoreCase = true)) {
BottomSheetEntryDecorator.Icon(OpenCodeIcons.Check, "Selected")
} else {
BottomSheetEntryDecorator.None
},
onClick = { onSelectMode(mode) },
onClick = { onSelectMode(agent.id) },
)
}
ComposerPickerKind.Variant ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,23 @@ class AgentDomainAdapterTest {
assertEquals("Explorer", agents[0].description)
assertEquals(null, agents[1].description)
}

@Test
fun toSelectableAgents_keepsPrimaryAndAllNotHidden() {
val agents =
adapter.toSelectableAgents(
listOf(
AgentDto(name = "build", description = "Primary", mode = "primary", hidden = false),
AgentDto(name = "plan", description = "Plan", mode = "PRIMARY", hidden = false),
AgentDto(name = "title", description = null, mode = "primary", hidden = true),
AgentDto(name = "explore", description = "Explorer", mode = "subagent", hidden = false),
AgentDto(name = "review", description = "Reviews work", mode = "all", hidden = null),
AgentDto(name = "code-reviewer", description = " ", mode = "primary", hidden = false),
),
)
assertEquals(listOf("build", "plan", "review", "code-reviewer"), agents.map { it.id })
assertEquals(listOf("Build", "Plan", "Review", "Code Reviewer"), agents.map { it.displayName })
assertEquals("Reviews work", agents[2].description)
assertEquals(null, agents[3].description)
}
}
Loading
Loading