Files
opencode-manager/frontend/src/api/files.ts
T
Chris Scott 549e0ac7b2 feat: add model state persistence and improve model selection sync (#209)
* 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
2026-04-29 21:44:47 -04:00

80 lines
2.5 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { fetchWrapper, fetchWrapperBlob } from './fetchWrapper'
import { API_BASE_URL } from '@/config'
import type { FileInfo, ChunkedFileInfo, PatchOperation } from '@/types/files'
interface FileApiUrlOptions {
route?: string
params?: Record<string, string | number | boolean | undefined>
}
export function getFileApiUrl(path: string, options: FileApiUrlOptions = {}): string {
const searchParams = new URLSearchParams()
Object.entries(options.params ?? {}).forEach(([key, value]) => {
if (value !== undefined) {
searchParams.append(key, String(value))
}
})
searchParams.set('path', path)
const query = searchParams.toString()
const routePath = options.route ? `/${options.route}` : ''
return `${API_BASE_URL}/api/files${routePath}${query ? `?${query}` : ''}`
}
async function fetchFile(path: string): Promise<FileInfo> {
return fetchWrapper(getFileApiUrl(path))
}
export function useFile(path: string | undefined) {
return useQuery({
queryKey: ['file', path],
queryFn: () => path ? fetchFile(path) : Promise.reject(new Error('No file path provided')),
enabled: !!path,
})
}
export async function fetchFileRange(path: string, startLine: number, endLine: number): Promise<ChunkedFileInfo> {
return fetchWrapper(getFileApiUrl(path), {
params: { startLine, endLine },
})
}
export async function getIgnoredPaths(path: string): Promise<{ ignoredPaths: string[] }> {
return fetchWrapper(getFileApiUrl(path, { route: 'ignored-paths' }))
}
export interface DownloadOptions {
includeGit?: boolean
includePaths?: string[]
}
export async function downloadDirectoryAsZip(path: string, options?: DownloadOptions): Promise<void> {
const url = getFileApiUrl(path, {
route: 'download-zip',
params: {
includeGit: options?.includeGit || undefined,
includePaths: options?.includePaths?.length ? options.includePaths.join(',') : undefined,
},
})
const blob = await fetchWrapperBlob(url)
const urlObj = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = urlObj
const dirName = path.split('/').pop() || 'download'
a.download = `${dirName}.zip`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(urlObj)
}
export async function applyFilePatches(path: string, patches: PatchOperation[]): Promise<{ success: boolean; totalLines: number }> {
const url = getFileApiUrl(path)
return fetchWrapper(url, {
method: 'PATCH',
body: JSON.stringify({ patches }),
})
}