mirror of
https://github.com/leonvanzyl/autocoder.git
synced 2026-01-30 06:12:06 +00:00
Add the ability to reset a project to its initial state with two options:
- Quick Reset: Clears features.db, assistant.db, and settings files while
preserving app spec and prompts
- Full Reset: Deletes everything including prompts directory, triggering
the setup wizard for project reconfiguration
Backend changes:
- Add POST /{name}/reset endpoint to projects router with full_reset query param
- Validate agent lock file to prevent reset while agent is running (409 Conflict)
- Dispose database engines before deleting files to release Windows file locks
- Add engine caching to api/database.py for better connection management
- Add dispose_engine() functions to both database modules
- Delete WAL mode journal files (*.db-wal, *.db-shm) during reset
Frontend changes:
- Add ResetProjectModal component with toggle between Quick/Full reset modes
- Add ProjectSetupRequired component shown when has_spec is false
- Add resetProject API function and useResetProject React Query hook
- Integrate reset button in header (disabled when agent running)
- Add 'R' keyboard shortcut to open reset modal
- Show ProjectSetupRequired when project needs setup after full reset
This implements the feature from PR #4 directly on master to avoid merge
conflicts.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
323 lines
9.2 KiB
TypeScript
323 lines
9.2 KiB
TypeScript
/**
|
|
* React Query hooks for project data
|
|
*/
|
|
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import * as api from '../lib/api'
|
|
import type { FeatureCreate, FeatureUpdate, ModelsResponse, ProjectSettingsUpdate, Settings, SettingsUpdate } from '../lib/types'
|
|
|
|
// ============================================================================
|
|
// Projects
|
|
// ============================================================================
|
|
|
|
export function useProjects() {
|
|
return useQuery({
|
|
queryKey: ['projects'],
|
|
queryFn: api.listProjects,
|
|
})
|
|
}
|
|
|
|
export function useProject(name: string | null) {
|
|
return useQuery({
|
|
queryKey: ['project', name],
|
|
queryFn: () => api.getProject(name!),
|
|
enabled: !!name,
|
|
})
|
|
}
|
|
|
|
export function useCreateProject() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: ({ name, path, specMethod }: { name: string; path: string; specMethod?: 'claude' | 'manual' }) =>
|
|
api.createProject(name, path, specMethod),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteProject() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (name: string) => api.deleteProject(name),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useResetProject(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (fullReset: boolean) => api.resetProject(projectName, fullReset),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
|
queryClient.invalidateQueries({ queryKey: ['project', projectName] })
|
|
queryClient.invalidateQueries({ queryKey: ['features', projectName] })
|
|
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useUpdateProjectSettings(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (settings: ProjectSettingsUpdate) =>
|
|
api.updateProjectSettings(projectName, settings),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['projects'] })
|
|
queryClient.invalidateQueries({ queryKey: ['project', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// ============================================================================
|
|
// Features
|
|
// ============================================================================
|
|
|
|
export function useFeatures(projectName: string | null) {
|
|
return useQuery({
|
|
queryKey: ['features', projectName],
|
|
queryFn: () => api.listFeatures(projectName!),
|
|
enabled: !!projectName,
|
|
refetchInterval: 5000, // Refetch every 5 seconds for real-time updates
|
|
})
|
|
}
|
|
|
|
export function useCreateFeature(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (feature: FeatureCreate) => api.createFeature(projectName, feature),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['features', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useDeleteFeature(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (featureId: number) => api.deleteFeature(projectName, featureId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['features', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useSkipFeature(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (featureId: number) => api.skipFeature(projectName, featureId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['features', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useUpdateFeature(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: ({ featureId, update }: { featureId: number; update: FeatureUpdate }) =>
|
|
api.updateFeature(projectName, featureId, update),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['features', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// ============================================================================
|
|
// Agent
|
|
// ============================================================================
|
|
|
|
export function useAgentStatus(projectName: string | null) {
|
|
return useQuery({
|
|
queryKey: ['agent-status', projectName],
|
|
queryFn: () => api.getAgentStatus(projectName!),
|
|
enabled: !!projectName,
|
|
refetchInterval: 3000, // Poll every 3 seconds
|
|
})
|
|
}
|
|
|
|
export function useStartAgent(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (options: {
|
|
yoloMode?: boolean
|
|
parallelMode?: boolean
|
|
maxConcurrency?: number
|
|
testingAgentRatio?: number
|
|
} = {}) => api.startAgent(projectName, options),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useStopAgent(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: () => api.stopAgent(projectName),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
|
|
// Invalidate schedule status to reflect manual stop override
|
|
queryClient.invalidateQueries({ queryKey: ['nextRun', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function usePauseAgent(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: () => api.pauseAgent(projectName),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useResumeAgent(projectName: string) {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: () => api.resumeAgent(projectName),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
|
|
},
|
|
})
|
|
}
|
|
|
|
// ============================================================================
|
|
// Setup
|
|
// ============================================================================
|
|
|
|
export function useSetupStatus() {
|
|
return useQuery({
|
|
queryKey: ['setup-status'],
|
|
queryFn: api.getSetupStatus,
|
|
staleTime: 60000, // Cache for 1 minute
|
|
})
|
|
}
|
|
|
|
export function useHealthCheck() {
|
|
return useQuery({
|
|
queryKey: ['health'],
|
|
queryFn: api.healthCheck,
|
|
retry: false,
|
|
})
|
|
}
|
|
|
|
// ============================================================================
|
|
// Filesystem
|
|
// ============================================================================
|
|
|
|
export function useListDirectory(path?: string) {
|
|
return useQuery({
|
|
queryKey: ['filesystem', 'list', path],
|
|
queryFn: () => api.listDirectory(path),
|
|
})
|
|
}
|
|
|
|
export function useCreateDirectory() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (path: string) => api.createDirectory(path),
|
|
onSuccess: (_, path) => {
|
|
// Invalidate parent directory listing
|
|
const parentPath = path.split('/').slice(0, -1).join('/') || undefined
|
|
queryClient.invalidateQueries({ queryKey: ['filesystem', 'list', parentPath] })
|
|
},
|
|
})
|
|
}
|
|
|
|
export function useValidatePath() {
|
|
return useMutation({
|
|
mutationFn: (path: string) => api.validatePath(path),
|
|
})
|
|
}
|
|
|
|
// ============================================================================
|
|
// Settings
|
|
// ============================================================================
|
|
|
|
// Default models response for placeholder (until API responds)
|
|
const DEFAULT_MODELS: ModelsResponse = {
|
|
models: [
|
|
{ id: 'claude-opus-4-5-20251101', name: 'Claude Opus 4.5' },
|
|
{ id: 'claude-sonnet-4-5-20250929', name: 'Claude Sonnet 4.5' },
|
|
],
|
|
default: 'claude-opus-4-5-20251101',
|
|
}
|
|
|
|
const DEFAULT_SETTINGS: Settings = {
|
|
yolo_mode: false,
|
|
model: 'claude-opus-4-5-20251101',
|
|
glm_mode: false,
|
|
ollama_mode: false,
|
|
testing_agent_ratio: 1,
|
|
}
|
|
|
|
export function useAvailableModels() {
|
|
return useQuery({
|
|
queryKey: ['available-models'],
|
|
queryFn: api.getAvailableModels,
|
|
staleTime: 300000, // Cache for 5 minutes - models don't change often
|
|
retry: 1,
|
|
placeholderData: DEFAULT_MODELS,
|
|
})
|
|
}
|
|
|
|
export function useSettings() {
|
|
return useQuery({
|
|
queryKey: ['settings'],
|
|
queryFn: api.getSettings,
|
|
staleTime: 60000, // Cache for 1 minute
|
|
retry: 1,
|
|
placeholderData: DEFAULT_SETTINGS,
|
|
})
|
|
}
|
|
|
|
export function useUpdateSettings() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (settings: SettingsUpdate) => api.updateSettings(settings),
|
|
onMutate: async (newSettings) => {
|
|
// Cancel outgoing refetches
|
|
await queryClient.cancelQueries({ queryKey: ['settings'] })
|
|
|
|
// Snapshot previous value
|
|
const previous = queryClient.getQueryData<Settings>(['settings'])
|
|
|
|
// Optimistically update
|
|
queryClient.setQueryData<Settings>(['settings'], (old) => ({
|
|
...DEFAULT_SETTINGS,
|
|
...old,
|
|
...newSettings,
|
|
}))
|
|
|
|
return { previous }
|
|
},
|
|
onError: (_err, _newSettings, context) => {
|
|
// Rollback on error
|
|
if (context?.previous) {
|
|
queryClient.setQueryData(['settings'], context.previous)
|
|
}
|
|
},
|
|
onSettled: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['settings'] })
|
|
},
|
|
})
|
|
}
|