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
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package dev.opencode.mobile.data.remote

import dev.opencode.mobile.data.remote.dto.ApiLocatedEnvelope
import dev.opencode.mobile.data.remote.dto.FileNodeDto
import dev.opencode.mobile.data.remote.dto.PathDto
import dev.opencode.mobile.data.remote.dto.V2FsEntryDto
import dev.opencode.mobile.data.remote.dto.V2LocationInfoDto
import dev.opencode.mobile.network.OpencodeHttpClient
import dev.opencode.mobile.network.applyDirectory
import dev.opencode.mobile.network.applyLocationDirectory
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.get
import io.ktor.client.request.parameter

class FilesystemApi(
private val httpClient: OpencodeHttpClient,
class FilesystemApi private constructor(
private val httpClient: ClientProvider,
) {
constructor(httpClient: OpencodeHttpClient) : this(ClientProvider(httpClient::get))

internal constructor(httpClient: HttpClient) : this(ClientProvider { httpClient })

suspend fun getPaths(): PathDto = httpClient.get().get("path").body()

// Lists absoluteDirectory via path=. + directory= so paths outside the boot worktree work.
Expand All @@ -21,4 +30,19 @@ class FilesystemApi(
parameter("path", ".")
applyDirectory(absoluteDirectory)
}.body()

suspend fun getLocationV2(): V2LocationInfoDto = httpClient.get().get("api/location").body()

suspend fun listFilesV2(absoluteDirectory: String): List<V2FsEntryDto> =
httpClient
.get()
.get("api/fs/list") {
parameter("path", ".")
applyLocationDirectory(absoluteDirectory)
}.body<ApiLocatedEnvelope<List<V2FsEntryDto>>>()
.data

private fun interface ClientProvider {
fun get(): HttpClient
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.opencode.mobile.data.remote
import dev.opencode.mobile.data.remote.dto.ApiActiveSessionsEnvelope
import dev.opencode.mobile.data.remote.dto.ApiDataEnvelope
import dev.opencode.mobile.data.remote.dto.ApiListEnvelope
import dev.opencode.mobile.data.remote.dto.ApiLocatedEnvelope
import dev.opencode.mobile.data.remote.dto.ApiSessionActiveDto
import dev.opencode.mobile.data.remote.dto.CreateSessionRequestDto
import dev.opencode.mobile.data.remote.dto.FileDiffDto
Expand All @@ -17,13 +18,16 @@ import dev.opencode.mobile.data.remote.dto.SessionDto
import dev.opencode.mobile.data.remote.dto.SummarizeSessionRequestDto
import dev.opencode.mobile.data.remote.dto.TodoDto
import dev.opencode.mobile.data.remote.dto.UpdateSessionRequestDto
import dev.opencode.mobile.data.remote.dto.V2CompactSessionRequestDto
import dev.opencode.mobile.data.remote.dto.V2CreateSessionRequestDto
import dev.opencode.mobile.data.remote.dto.V2ForkSessionRequestDto
import dev.opencode.mobile.data.remote.dto.V2LocationDto
import dev.opencode.mobile.data.remote.dto.V2PermissionReplyRequestDto
import dev.opencode.mobile.data.remote.dto.V2PermissionRequestDto
import dev.opencode.mobile.data.remote.dto.V2RenameSessionRequestDto
import dev.opencode.mobile.data.remote.dto.V2RevertStageRequestDto
import dev.opencode.mobile.data.remote.dto.V2SessionDto
import dev.opencode.mobile.data.remote.dto.V2VcsInfoDto
import dev.opencode.mobile.data.remote.dto.VcsInfoDto
import dev.opencode.mobile.network.OpencodeHttpClient
import dev.opencode.mobile.network.applyDirectory
Expand Down Expand Up @@ -441,6 +445,56 @@ class SessionsApi private constructor(
}.body<ApiDataEnvelope<V2SessionDto>>()
.data

suspend fun compactSessionV2(id: String): Boolean =
httpClient
.get()
.post("api/session/$id/compact") {
timeout {
requestTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS
socketTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS
}
setJsonBody(json, V2CompactSessionRequestDto(delivery = "steer"))
}.ensureSuccess()

suspend fun stageRevertV2(
id: String,
messageId: String,
partId: String?,
): Boolean =
httpClient
.get()
.post("api/session/$id/revert/stage") {
setJsonBody(json, V2RevertStageRequestDto(messageId = messageId, partId = partId, files = true))
}.ensureSuccess()

suspend fun clearRevertV2(id: String): Boolean =
httpClient
.get()
.post("api/session/$id/revert/clear")
.ensureSuccess()

suspend fun getVcsInfoV2(directory: String? = null): V2VcsInfoDto =
httpClient
.get()
.get("api/vcs") {
applyLocationDirectory(directory)
}.body<ApiLocatedEnvelope<V2VcsInfoDto>>()
.data

suspend fun getVcsDiffV2(
mode: String,
directory: String? = null,
context: Int = 12,
): List<FileDiffDto> =
httpClient
.get()
.get("api/vcs/diff") {
applyLocationDirectory(directory)
parameter("mode", mode)
parameter("context", context)
}.body<ApiLocatedEnvelope<List<FileDiffDto>>>()
.data

private fun HttpResponse.ensureSuccess(): Boolean {
check(status.isSuccess()) { "HTTP ${status.value}: ${status.description}" }
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.opencode.mobile.data.remote.dto

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
Expand Down Expand Up @@ -28,3 +29,23 @@ data class ApiActiveSessionsEnvelope(
data class ApiSessionActiveDto(
val type: String? = null,
)

@Serializable
data class ApiLocatedEnvelope<T>(
val location: V2LocationInfoDto? = null,
val data: T,
)

@Serializable
data class V2LocationInfoDto(
val directory: String,
@SerialName("workspaceID") val workspaceId: String? = null,
val project: V2LocationProjectDto? = null,
)

@Serializable
data class V2LocationProjectDto(
val id: String? = null,
val directory: String? = null,
val canonical: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ data class FileNodeDto(
val type: String,
val ignored: Boolean = false,
)

@Serializable
data class V2FsEntryDto(
val path: String,
val type: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,26 @@ data class V2PermissionSourceDto(
data class V2PermissionReplyRequestDto(
val reply: String,
)

@Serializable
data class V2CompactSessionRequestDto(
val delivery: String,
)

@Serializable
data class V2RevertStageRequestDto(
@SerialName("messageID") val messageId: String,
@SerialName("partID") val partId: String? = null,
val files: Boolean,
)

@Serializable
data class V2VcsInfoDto(
val branch: V2VcsBranchDto? = null,
)

@Serializable
data class V2VcsBranchDto(
val current: String? = null,
val default: String? = null,
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
package dev.opencode.mobile.domain.filesystem

import dev.opencode.mobile.data.remote.FilesystemApi
import dev.opencode.mobile.domain.connection.ServerProtocol
import dev.opencode.mobile.domain.project.ProjectRepository
import dev.opencode.mobile.network.ActiveConnectionManager
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import javax.inject.Inject

class DefaultFilesystemRepository
@Inject
constructor(
private val api: FilesystemApi,
private val adapter: FilesystemDomainAdapter,
private val activeConnectionManager: ActiveConnectionManager? = null,
private val projectRepository: ProjectRepository? = null,
) : FilesystemRepository {
override suspend fun getPaths(): ServerPaths = adapter.toServerPaths(api.getPaths())
private val protocolMutex = Mutex()

override suspend fun listDirectory(path: String): List<DirectoryNode> =
DirectoryListingPolicy.prepare(api.listFiles(absoluteDirectory = path).map(adapter::toDirectoryNode))
private suspend fun isV2(): Boolean {
protocolMutex.withLock {
activeConnectionManager?.currentProtocol()?.let { return it == ServerProtocol.V2 }
val projectRepository = projectRepository ?: return false
runCatching { projectRepository.probeServer() }
return activeConnectionManager?.currentProtocol() == ServerProtocol.V2
}
}

override suspend fun getPaths(): ServerPaths =
if (isV2()) {
adapter.toServerPaths(api.getLocationV2())
} else {
adapter.toServerPaths(api.getPaths())
}

override suspend fun listDirectory(path: String): List<DirectoryNode> {
val nodes =
if (isV2()) {
api.listFilesV2(absoluteDirectory = path).map { adapter.toDirectoryNode(it, path) }
} else {
api.listFiles(absoluteDirectory = path).map(adapter::toDirectoryNode)
}
return DirectoryListingPolicy.prepare(nodes)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package dev.opencode.mobile.domain.filesystem

import dev.opencode.mobile.data.remote.dto.FileNodeDto
import dev.opencode.mobile.data.remote.dto.PathDto
import dev.opencode.mobile.data.remote.dto.V2FsEntryDto
import dev.opencode.mobile.data.remote.dto.V2LocationInfoDto

class FilesystemDomainAdapter {
fun toServerPaths(dto: PathDto): ServerPaths =
Expand All @@ -11,10 +13,48 @@ class FilesystemDomainAdapter {
worktree = dto.worktree,
)

fun toServerPaths(dto: V2LocationInfoDto): ServerPaths {
val directory = dto.directory
val worktree = dto.project?.directory ?: directory
return ServerPaths(
home = directory,
directory = directory,
worktree = worktree,
)
}

fun toDirectoryNode(dto: FileNodeDto): DirectoryNode =
DirectoryNode(
name = dto.name,
absolutePath = dto.absolute,
isDirectory = dto.type == "directory",
)

fun toDirectoryNode(
dto: V2FsEntryDto,
listingDirectory: String,
): DirectoryNode {
val absolutePath = absolutePath(dto.path, listingDirectory)
return DirectoryNode(
name = nameFromPath(absolutePath, dto.path),
absolutePath = absolutePath,
isDirectory = dto.type == "directory",
)
}

private fun absolutePath(
path: String,
listingDirectory: String,
): String {
if (path.startsWith("/")) return path
val base = listingDirectory.trimEnd('/')
val relative = path.trimStart('/')
if (relative.isEmpty() || relative == ".") return listingDirectory
return "$base/$relative"
}

private fun nameFromPath(
absolutePath: String,
fallback: String,
): String = absolutePath.substringAfterLast('/').ifEmpty { fallback.substringAfterLast('/') }
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,15 @@ class DefaultSessionRepository
id: String,
directory: String?,
): List<SessionTodo> =
runCatching { adapter.toTodos(api.listSessionTodos(id, directory)) }
.getOrElse { throw adapter.mapError(it) }
runCatching {
if (isV2()) {
// No REST snapshot on v2. Throw so the live store keeps event-seeded
// todos instead of replacing them with an empty list.
throw SessionError.Http(404, "Todos REST is unavailable on v2")
} else {
adapter.toTodos(api.listSessionTodos(id, directory))
}
}.getOrElse { throw adapter.mapError(it) }

override suspend fun initializeSessionAgents(
id: String,
Expand Down Expand Up @@ -261,16 +268,26 @@ class DefaultSessionRepository
.getOrElse { throw adapter.mapError(it) }

override suspend fun getVcsInfo(directory: String?): VcsInfo =
runCatching { adapter.toVcsInfo(api.getVcsInfo(directory)) }
.getOrElse { throw adapter.mapError(it) }
runCatching {
if (isV2()) {
adapter.toVcsInfo(api.getVcsInfoV2(directory))
} else {
adapter.toVcsInfo(api.getVcsInfo(directory))
}
}.getOrElse { throw adapter.mapError(it) }

override suspend fun getVcsDiff(
mode: VcsDiffMode,
directory: String?,
context: Int,
): List<FileDiff> =
runCatching { api.getVcsDiff(mode.wireValue, directory, context).map(adapter::toFileDiff) }
.getOrElse { throw adapter.mapError(it) }
runCatching {
if (isV2()) {
api.getVcsDiffV2(mode.v2WireValue, directory, context).map(adapter::toFileDiff)
} else {
api.getVcsDiff(mode.wireValue, directory, context).map(adapter::toFileDiff)
}
}.getOrElse { throw adapter.mapError(it) }

override suspend fun summarizeSession(
id: String,
Expand All @@ -279,7 +296,15 @@ class DefaultSessionRepository
directory: String?,
): Boolean =
runCatching {
api.summarizeSession(id, SummarizeSessionRequestDto(providerId = providerId, modelId = modelId), directory)
if (isV2()) {
api.compactSessionV2(id)
} else {
api.summarizeSession(
id,
SummarizeSessionRequestDto(providerId = providerId, modelId = modelId),
directory,
)
}
}.getOrElse { throw adapter.mapError(it) }

override suspend fun revertSession(
Expand All @@ -289,15 +314,24 @@ class DefaultSessionRepository
directory: String?,
): Boolean =
runCatching {
api.revertSession(id, RevertSessionRequestDto(messageId = messageId, partId = partId), directory)
if (isV2()) {
api.stageRevertV2(id, messageId, partId)
} else {
api.revertSession(id, RevertSessionRequestDto(messageId = messageId, partId = partId), directory)
}
}.getOrElse { throw adapter.mapError(it) }

override suspend fun unrevertSession(
id: String,
directory: String?,
): Boolean =
runCatching { api.unrevertSession(id, directory) }
.getOrElse { throw adapter.mapError(it) }
runCatching {
if (isV2()) {
api.clearRevertV2(id)
} else {
api.unrevertSession(id, directory)
}
}.getOrElse { throw adapter.mapError(it) }

override suspend fun respondToPermission(
sessionId: String,
Expand Down
Loading
Loading