superintelligence-android / android/app/src/main/java/net/kentonc/si/agent/Settings.kt

raw file 2571 bytes
package net.kentonc.si.agent

import android.content.Context

/** All settings, the equivalent of src/config.rs in the Rust original (there
 *  they're compile-time constants; here they're user-editable, since a remote
 *  server address changes per device anyway).
 */
data class Config(
    val baseUrl: String = DEFAULT.baseUrl,
    val apiKey: String = DEFAULT.apiKey,
    val model: String = DEFAULT.model,
    val temperature: Float = DEFAULT.temperature,
    val maxTokens: Int = DEFAULT.maxTokens,
    val autoApprove: Boolean = DEFAULT.autoApprove,
) {
    companion object {
        val DEFAULT = Config(
            baseUrl = "http://127.0.0.1:8085",
            apiKey = "none",
            model = "Qwen3.8-27B-GSQ-RCO-IQ3_XXS-mtp.gguf",
            temperature = 0.7f,
            maxTokens = 2048,
            autoApprove = false,
        )
    }
}

/** SharedPreferences-backed persistence for [Config]. */
class SettingsStore(context: Context) {
    private val prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE)

    /** Cached in memory; save() rewrites it and bumps the version counter. */
    var config: Config
        private set

    /** Bumped on every save so UI observing it recomposes with the new values. */
    var version: Int = 0
        private set

    init {
        val loaded = load()
        if (loaded != null) {
            config = loaded
        } else {
            // First launch: persist the defaults so Settings opens populated.
            config = Config.DEFAULT
            save(Config.DEFAULT)
        }
    }

    fun load(): Config? {
        if (!prefs.contains("base_url")) return null
        return Config(
            baseUrl = prefs.getString("base_url", Config.DEFAULT.baseUrl)!!,
            apiKey = prefs.getString("api_key", Config.DEFAULT.apiKey)!!,
            model = prefs.getString("model", Config.DEFAULT.model)!!,
            temperature = prefs.getFloat("temperature", Config.DEFAULT.temperature),
            maxTokens = prefs.getInt("max_tokens", Config.DEFAULT.maxTokens),
            autoApprove = prefs.getBoolean("auto_approve", Config.DEFAULT.autoApprove),
        )
    }

    fun save(c: Config) {
        config = c
        version++
        prefs.edit()
            .putString("base_url", c.baseUrl)
            .putString("api_key", c.apiKey)
            .putString("model", c.model)
            .putFloat("temperature", c.temperature)
            .putInt("max_tokens", c.maxTokens)
            .putBoolean("auto_approve", c.autoApprove)
            .apply()
    }
}