package eu.friedcutlet.yokan.ext import kotlinx.coroutines.flow.StateFlow /** Lifecycle shared by the embedded Android bridge and JVM-backed desktop/iOS servers. */ interface ExtensionServer { val status: StateFlow suspend fun start(config: ExtensionServerConfig): ExtensionServerStatus suspend fun stop(): ExtensionServerStatus } data class ExtensionServerConfig( val port: Int, val jvmPath: String? = null, val serverJarPath: String? = null, ) { init { require(port in 1..65535) { "Port must be between 1 and 65535" } } val baseUrl: String get() = "http://127.0.0.1:$port" } enum class ExtensionServerPhase { Stopped, Starting, Running, Stopping, Failed, } data class ExtensionServerStatus( val phase: ExtensionServerPhase, val port: Int? = null, val message: String = "", ) { val isRunning: Boolean get() = phase == ExtensionServerPhase.Running val baseUrl: String? get() = port?.let { "http://127.0.0.1:$it" } companion object { val Stopped = ExtensionServerStatus(ExtensionServerPhase.Stopped) } } class ExtensionServerException( val code: String, message: String, cause: Throwable? = null, ) : IllegalStateException(message, cause)