mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
refactor(config): make on-disk opencode.json the single source of truth (#354)
* refactor(config): make on-disk opencode.json the single source of truth Make <workspace>/.config/opencode/opencode.json the only store for the active OpenCode configuration. One owner (opencode-config-file.ts) reads, validates, writes, archives, and seeds the file; one owner (opencode-config-apply.ts) decides how an edit is applied: restart pending, or a live patch with recovery. Settings routes, the internal assistant API, the health-watch supervisor, boot, and host import all go through them, so an edit that lands on the file without going through the database is visible immediately instead of at the next Manager boot. Remove the named-config profiles. Migration 019 archives every opencode_configs row to .config/opencode-configs-archive/<name>.json, restores the default row to opencode.json when no file exists, then drops the table, its indexes, and repos.opencode_config_name. The /opencode-configs* and config/switch routes and the CreateConfigDialog/SwitchConfigDialog dialogs are gone, and the ocm tool gains GET/PUT /opencode-config backed by the same apply owner. Harden the write path: serialize and atomically write config updates while preserving the file mode, keep withFileLock usable after a rejection, route reloadConfig's cleaned-config write through the file owner, stop boot from rewriting an existing valid config when importing state, prune the health-watch directory to its newest 20 entries, resolve archive paths with realpath, and share one frontend config query across dialogs. * fix(mirror): canonicalize worktree paths before branch ownership check * refactor(config): unify opencode config apply and rollback paths - share one config route factory between public and internal mounts - capture last-known-good before every config write and restore via one helper - stop auto-restarting on save; flag restart-required fields and live-patch the rest - add CONFIG_PATCH_TIMEOUT_MS for config patch requests - extract canonicalPath helpers, health-watch artifact writer, and useProvidersWithModels hook * refactor(model-state): use OpenCode state file as model state source of truth
This commit is contained in:
1 parent
7a3c5229df
commit
295d24ced7
116 files changed
+10291
-4208
No files matched your search
@@ -0,0 +1,101 @@
|
||||
import { z } from 'zod'
|
||||
import { getOpenCodeModelStatePath } from '@opencode-manager/shared/config/env'
|
||||
import { readJsonSafe, withFileLock, writeJsonAtomic } from '../utils/atomic-json'
|
||||
import { logger } from '../utils/logger'
|
||||
|
||||
export const ModelSelectionSchema = z.object({
|
||||
providerID: z.string().min(1),
|
||||
modelID: z.string().min(1),
|
||||
})
|
||||
|
||||
export const ModelStateSchema = z.object({
|
||||
recent: z.array(ModelSelectionSchema).default([]),
|
||||
favorite: z.array(ModelSelectionSchema).default([]),
|
||||
variant: z.record(z.string(), z.string().optional()).default({}),
|
||||
}).passthrough()
|
||||
|
||||
export interface ModelSelectionRecord {
|
||||
providerID: string
|
||||
modelID: string
|
||||
}
|
||||
|
||||
export interface OpenCodeModelStateRecord {
|
||||
recent: ModelSelectionRecord[]
|
||||
favorite: ModelSelectionRecord[]
|
||||
variant: Record<string, string | undefined>
|
||||
}
|
||||
|
||||
export const MAX_RECENT_MODELS = 10
|
||||
|
||||
function createEmptyState(): OpenCodeModelStateRecord {
|
||||
return { recent: [], favorite: [], variant: {} }
|
||||
}
|
||||
|
||||
function toRecord(data: z.infer<typeof ModelStateSchema>): OpenCodeModelStateRecord {
|
||||
return { recent: data.recent, favorite: data.favorite, variant: data.variant }
|
||||
}
|
||||
|
||||
export async function readOpenCodeModelState(): Promise<OpenCodeModelStateRecord> {
|
||||
const raw = await readJsonSafe<unknown>(getOpenCodeModelStatePath(), null)
|
||||
const parsed = ModelStateSchema.safeParse(raw)
|
||||
|
||||
if (!parsed.success) {
|
||||
if (raw !== null) {
|
||||
logger.warn('OpenCode model state file has invalid structure', parsed.error)
|
||||
}
|
||||
return createEmptyState()
|
||||
}
|
||||
|
||||
return toRecord(parsed.data)
|
||||
}
|
||||
|
||||
export function addRecentModel(state: OpenCodeModelStateRecord, model: ModelSelectionRecord): OpenCodeModelStateRecord {
|
||||
const deduped = [
|
||||
model,
|
||||
...state.recent.filter(m => m.providerID !== model.providerID || m.modelID !== model.modelID),
|
||||
]
|
||||
return { ...state, recent: deduped.slice(0, MAX_RECENT_MODELS) }
|
||||
}
|
||||
|
||||
export function removeRecentModel(state: OpenCodeModelStateRecord, model: ModelSelectionRecord): OpenCodeModelStateRecord {
|
||||
return {
|
||||
...state,
|
||||
recent: state.recent.filter(m => m.providerID !== model.providerID || m.modelID !== model.modelID),
|
||||
}
|
||||
}
|
||||
|
||||
export function toggleFavoriteModel(state: OpenCodeModelStateRecord, model: ModelSelectionRecord): OpenCodeModelStateRecord {
|
||||
const exists = state.favorite.some(
|
||||
m => m.providerID === model.providerID && m.modelID === model.modelID,
|
||||
)
|
||||
const favorite = exists
|
||||
? state.favorite.filter(m => m.providerID !== model.providerID || m.modelID !== model.modelID)
|
||||
: [...state.favorite, model]
|
||||
|
||||
return { ...state, favorite }
|
||||
}
|
||||
|
||||
export async function updateOpenCodeModelState(
|
||||
mutate: (state: OpenCodeModelStateRecord) => OpenCodeModelStateRecord,
|
||||
): Promise<OpenCodeModelStateRecord> {
|
||||
const modelStatePath = getOpenCodeModelStatePath()
|
||||
|
||||
return withFileLock(modelStatePath, async () => {
|
||||
const raw = await readJsonSafe<unknown>(modelStatePath, {})
|
||||
const parsed = ModelStateSchema.safeParse(raw)
|
||||
const current = parsed.success ? toRecord(parsed.data) : createEmptyState()
|
||||
const next = mutate(current)
|
||||
const rawObject = raw && typeof raw === 'object' && !Array.isArray(raw)
|
||||
? raw as Record<string, unknown>
|
||||
: {}
|
||||
|
||||
await writeJsonAtomic(modelStatePath, {
|
||||
...rawObject,
|
||||
recent: next.recent,
|
||||
favorite: next.favorite,
|
||||
variant: next.variant,
|
||||
})
|
||||
|
||||
return next
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user