feat(config): add config center and runtime path resolution

This commit is contained in:
2026-04-01 20:42:45 +08:00
parent 4f200cadfc
commit 632e47ec13
2 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
package work.slhaf.partner.api.agent.runtime.config
import java.nio.file.Path
object ConfigCenter {
val paths = resolvePaths()
private val registrations = mutableMapOf<Path, ConfigRegistration<out Config>>()
fun register(configurable: Configurable) {
fun normalizeRelativePath(path: Path): Path {
require(!path.isAbsolute) {
"Config path must be relative: $path"
}
return path.normalize()
}
val declared = configurable.declare()
val normalized = mutableMapOf<Path, ConfigRegistration<out Config>>()
declared.forEach { (path, registration) ->
val normalizedPath = normalizeRelativePath(path)
check(!normalized.containsKey(normalizedPath)) {
"Duplicated config path declared in the same configurable: $normalizedPath"
}
check(!registrations.containsKey(normalizedPath)) {
"Config path already registered: $normalizedPath"
}
normalized[normalizedPath] = registration
}
registrations.putAll(normalized)
}
}
abstract class Config
interface Configurable {
fun declare(): Map<Path, ConfigRegistration<out Config>>
fun register() {
ConfigCenter.register(this)
}
}
interface ConfigRegistration<T : Config> {
fun type(): Class<T>
fun init(config: T)
fun onReload(config: T) {}
}

View File

@@ -0,0 +1,44 @@
package work.slhaf.partner.api.agent.runtime.config
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
data class ConfigPaths(
val workDir: Path,
val configDir: Path,
val workspaceDir: Path,
val stateDir: Path,
val resourcesDir: Path
) {
fun ensureDirectories() {
Files.createDirectories(workDir)
Files.createDirectories(configDir)
Files.createDirectories(workspaceDir)
Files.createDirectories(stateDir)
Files.createDirectories(resourcesDir)
}
}
internal fun resolvePaths(): ConfigPaths {
fun resolveWorkdir(): Path {
val envHome = System.getenv("PARTNER_HOME")?.trim()
if (!envHome.isNullOrEmpty()) {
return Paths.get(envHome).toAbsolutePath().normalize()
}
val userHome = System.getProperty("user.home")
?: throw IllegalStateException(" System property 'user.home' is missing.")
return Paths.get(userHome, ".partner").toAbsolutePath().normalize()
}
val workDir = resolveWorkdir()
return ConfigPaths(
workDir,
workDir.resolve("config"),
workDir.resolve("workspace"),
workDir.resolve("state"),
workDir.resolve("resources")
).apply { ensureDirectories() }
}