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
Expand Up @@ -3,12 +3,18 @@ package dev.opencode.mobile.data.remote
import dev.opencode.mobile.data.remote.dto.ProjectDto
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

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

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

suspend fun listProjects(): List<ProjectDto> = httpClient.get().get("project").body()

suspend fun getCurrentProject(directory: String? = null): ProjectDto =
Expand All @@ -17,4 +23,15 @@ class ProjectsApi(
.get("project/current") {
applyDirectory(directory)
}.body()

suspend fun getCurrentProjectV2(directory: String? = null): ProjectDto =
httpClient
.get()
.get("api/project/current") {
applyLocationDirectory(directory)
}.body()

private fun interface ClientProvider {
fun get(): HttpClient
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
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.ApiSessionActiveDto
import dev.opencode.mobile.data.remote.dto.CreateSessionRequestDto
import dev.opencode.mobile.data.remote.dto.FileDiffDto
import dev.opencode.mobile.data.remote.dto.ForkSessionRequestDto
Expand All @@ -13,6 +17,11 @@ 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.V2CreateSessionRequestDto
import dev.opencode.mobile.data.remote.dto.V2ForkSessionRequestDto
import dev.opencode.mobile.data.remote.dto.V2LocationDto
import dev.opencode.mobile.data.remote.dto.V2RenameSessionRequestDto
import dev.opencode.mobile.data.remote.dto.V2SessionDto
import dev.opencode.mobile.data.remote.dto.VcsInfoDto
import dev.opencode.mobile.network.OpencodeHttpClient
import dev.opencode.mobile.network.applyDirectory
Expand All @@ -26,6 +35,8 @@ import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.request.patch
import io.ktor.client.request.post
import io.ktor.client.statement.HttpResponse
import io.ktor.http.isSuccess
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import javax.inject.Inject
Expand Down Expand Up @@ -291,7 +302,99 @@ class SessionsApi private constructor(
applyDirectory(directory)
}.body()

// --- V2 home / session CRUD ---

suspend fun listSessionsV2(directory: String? = null): List<V2SessionDto> {
val sessions = mutableListOf<V2SessionDto>()
var cursor: String? = null
var pages = 0
do {
val envelope =
httpClient
.get()
.get("api/session") {
applyDirectory(directory)
cursor?.let { parameter("cursor", it) }
}.body<ApiListEnvelope<V2SessionDto>>()
sessions += envelope.data
cursor = envelope.cursor?.next?.takeIf { it.isNotBlank() }
pages++
} while (cursor != null && pages < MAX_V2_SESSION_LIST_PAGES)
return sessions
}

suspend fun createSessionV2(directory: String? = null): V2SessionDto =
httpClient
.get()
.post("api/session") {
setJsonBody(
json,
V2CreateSessionRequestDto(
location = directory?.let { V2LocationDto(directory = it) },
),
)
}.body<ApiDataEnvelope<V2SessionDto>>()
.data

suspend fun getActiveSessionsV2(): Map<String, ApiSessionActiveDto> =
httpClient
.get()
.get("api/session/active")
.body<ApiActiveSessionsEnvelope>()
.data

suspend fun getSessionV2(id: String): V2SessionDto =
httpClient
.get()
.get("api/session/$id")
.body<ApiDataEnvelope<V2SessionDto>>()
.data

suspend fun renameSessionV2(
id: String,
title: String,
) {
httpClient
.get()
.post("api/session/$id/rename") {
setJsonBody(json, V2RenameSessionRequestDto(title = title))
}.ensureSuccess()
}

suspend fun archiveSessionV2(id: String) {
httpClient
.get()
.post("api/session/$id/archive")
.ensureSuccess()
}

suspend fun deleteSessionV2(id: String): Boolean =
httpClient
.get()
.delete("api/session/$id")
.ensureSuccess()

suspend fun forkSessionV2(
id: String,
messageId: String?,
): V2SessionDto =
httpClient
.get()
.post("api/session/$id/fork") {
setJsonBody(json, V2ForkSessionRequestDto(messageId = messageId))
}.body<ApiDataEnvelope<V2SessionDto>>()
.data

private fun HttpResponse.ensureSuccess(): Boolean {
check(status.isSuccess()) { "HTTP ${status.value}: ${status.description}" }
return true
}

private fun interface ClientProvider {
fun get(): HttpClient
}

private companion object {
const val MAX_V2_SESSION_LIST_PAGES = 20
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.opencode.mobile.data.remote.dto

import kotlinx.serialization.Serializable

@Serializable
data class ApiDataEnvelope<T>(
val data: T,
)

@Serializable
data class ApiListEnvelope<T>(
val data: List<T> = emptyList(),
val cursor: ApiCursorDto? = null,
)

@Serializable
data class ApiCursorDto(
val previous: String? = null,
val next: String? = null,
)

@Serializable
data class ApiActiveSessionsEnvelope(
val data: Map<String, ApiSessionActiveDto> = emptyMap(),
)

@Serializable
data class ApiSessionActiveDto(
val type: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ data class HealthDto(
@Serializable
data class ProjectDto(
val id: String,
val worktree: String,
val worktree: String? = null,
/** V2 `/api/project/current` uses `directory` instead of `worktree`. */
val directory: String? = null,
val vcs: String? = null,
)
) {
val resolvedWorktree: String
get() =
worktree?.takeIf { it.isNotBlank() }
?: directory?.takeIf { it.isNotBlank() }
?: error("Project requires worktree or directory")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.opencode.mobile.data.remote.dto

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

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

@Serializable
data class V2SessionDto(
val id: String,
val title: String,
@SerialName("projectID") val projectId: String? = null,
@SerialName("parentID") val parentId: String? = null,
val cost: Double? = null,
val tokens: MessageTokensDto? = null,
val time: SessionTimeDto? = null,
val location: V2LocationDto? = null,
val revert: SessionRevertDto? = null,
)

@Serializable
data class V2CreateSessionRequestDto(
val location: V2LocationDto? = null,
)

@Serializable
data class V2RenameSessionRequestDto(
val title: String,
)

@Serializable
data class V2ForkSessionRequestDto(
@SerialName("messageID") val messageId: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package dev.opencode.mobile.domain.project

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

class DefaultProjectRepository
Expand All @@ -13,7 +16,21 @@ class DefaultProjectRepository
private val adapter: ProjectDomainAdapter,
private val activeConnectionManager: ActiveConnectionManager,
) : ProjectRepository {
private val protocolMutex = Mutex()

private suspend fun isV2(): Boolean {
protocolMutex.withLock {
activeConnectionManager.currentProtocol()?.let { return it == ServerProtocol.V2 }
runCatching { probeServerLocked() }
return activeConnectionManager.currentProtocol() == ServerProtocol.V2
}
}

override suspend fun probeServer() {
protocolMutex.withLock { probeServerLocked() }
}

private suspend fun probeServerLocked() {
runCatching {
val protocol = healthApi.detectProtocol()
activeConnectionManager.updateProtocol(protocol)
Expand All @@ -25,6 +42,13 @@ class DefaultProjectRepository
.getOrElse { throw adapter.mapProbeError(it) }

override suspend fun getCurrentProject(directory: String?): Project =
runCatching { adapter.toProject(projectsApi.getCurrentProject(directory)) }
.getOrElse { throw adapter.mapProbeError(it) }
runCatching {
val dto =
if (isV2()) {
projectsApi.getCurrentProjectV2(directory)
} else {
projectsApi.getCurrentProject(directory)
}
adapter.toProject(dto)
}.getOrElse { throw adapter.mapProbeError(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import io.ktor.client.plugins.ResponseException
import java.io.File

class ProjectDomainAdapter {
fun toProject(dto: ProjectDto): Project =
Project(
fun toProject(dto: ProjectDto): Project {
val worktree = dto.resolvedWorktree
return Project(
id = dto.id,
worktree = dto.worktree,
displayName = displayNameFromWorktree(dto.worktree),
worktree = worktree,
displayName = displayNameFromWorktree(worktree),
)
}

fun mapProbeError(throwable: Throwable): ServerProbeError =
when (throwable) {
Expand Down
Loading
Loading