mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
* feat: add persistent model state storage with database backend - Create opencode_model_state table for storing user model preferences - Implement database-backed model state management (recent, favorite, variant) - Add atomic JSON file utilities with file locking for concurrent safety - Migrate providers routes from file-based to database storage - Add backfill migration to preserve existing model.json data - Include comprehensive tests for model-state and atomic-json modules - Refactor useSessionAgent hook to simplify agent/model resolution logic - Update getFileApiUrl to use consistent query parameter approach * fix: include workspace dependencies in Docker image * fix: overlay workspace node_modules after source to prevent layer overwrite * fix: ensure model state table exists for existing databases * feat: add model state persistence and improve model selection sync - Add setActiveModel to modelStore and useModelSelection for direct model updates - Sync session agent model/variant to store when messages load - Include model, agent, and variant in optimistic user messages - Fix useSessionAgent to check isFetching during message loading - Update env.ts to use getter for BASE_PATH for dynamic resolution - Fix test environment variable from OPENCODE_WORKSPACE_PATH to WORKSPACE_PATH
51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import { promises as fs } from 'node:fs'
|
|
import { randomBytes } from 'node:crypto'
|
|
import { logger } from './logger'
|
|
|
|
const fileLockPromises = new Map<string, Promise<unknown>>()
|
|
|
|
export async function readJsonSafe<T>(filePath: string, fallback: T): Promise<T> {
|
|
try {
|
|
const content = await fs.readFile(filePath, 'utf8')
|
|
return JSON.parse(content) as T
|
|
} catch (error: unknown) {
|
|
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
|
return fallback
|
|
}
|
|
logger.warn(`Failed to read or parse JSON from ${filePath}: ${error instanceof Error ? error.message : String(error)}`)
|
|
return fallback
|
|
}
|
|
}
|
|
|
|
export async function writeJsonAtomic(filePath: string, data: unknown): Promise<void> {
|
|
const tmpPath = `${filePath}.tmp.${process.pid}.${Date.now()}.${randomBytes(4).toString('hex')}`
|
|
try {
|
|
await fs.mkdir(filePath.substring(0, filePath.lastIndexOf('/')), { recursive: true })
|
|
await fs.writeFile(tmpPath, JSON.stringify(data, null, 2), 'utf8')
|
|
await fs.rename(tmpPath, filePath)
|
|
} catch (error) {
|
|
await fs.unlink(tmpPath).catch(() => undefined)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export async function withFileLock<T>(filePath: string, fn: () => Promise<T>): Promise<T> {
|
|
const absolutePath = filePath
|
|
const previousLock = fileLockPromises.get(absolutePath)
|
|
|
|
const executeWithLock = async (): Promise<T> => {
|
|
try {
|
|
return await fn()
|
|
} finally {
|
|
if (fileLockPromises.get(absolutePath) === newLock) {
|
|
fileLockPromises.delete(absolutePath)
|
|
}
|
|
}
|
|
}
|
|
|
|
const newLock = previousLock ? previousLock.then(executeWithLock) : executeWithLock()
|
|
fileLockPromises.set(absolutePath, newLock)
|
|
|
|
return newLock as Promise<T>
|
|
}
|