feat(schedules): add workspace modes and worktree management

This commit is contained in:
Chris Scott committed 2026-10-06 10:16:34 -04:00
1 parent c06075651d
commit 6ec3f3bd91
90 files changed
+2084 -801

No files matched your search

+4 -2
View File
@@ -180,11 +180,13 @@ export const useCreateSession = (
title?: string;
agent?: string;
model?: string;
directory?: string;
}) => {
if (!directory) {
const targetDirectory = data.directory ?? directory;
if (!targetDirectory) {
throw new Error('A directory is required to create a session');
}
return createSession({ directory, ...data });
return createSession({ ...data, directory: targetDirectory });
},
onSuccess: (session) => {
invalidateSessionListCaches(queryClient);
+12 -4
View File
@@ -3,21 +3,29 @@ import { openDialogParam, useDialogParam } from './useDialogParam'
import { useUrlParams } from './useUrlParams'
const TERMINAL_DIALOG = 'terminal'
const TERMINAL_DIALOG_PARAMS = ['terminal'] as const
const TERMINAL_DIALOG_PARAMS = ['terminal', 'terminalDirectory'] as const
export function useTerminalDialogParam(): [boolean, (open: boolean) => void] {
return useDialogParam(TERMINAL_DIALOG, TERMINAL_DIALOG_PARAMS)
}
/**
* The directory the terminal dialog was opened for, such as a repo worktree; null means the repo itself.
*/
export function useTerminalDirectoryParam(): string | null {
const { searchParams } = useUrlParams()
return searchParams.get('terminalDirectory')
}
export function useOpenTerminal(): (
terminalId: string,
terminalId: string | null,
extraParams?: Record<string, string>,
) => void {
const { updateParams } = useUrlParams()
return useCallback(
(terminalId: string, extraParams?: Record<string, string>) => {
openDialogParam(updateParams, TERMINAL_DIALOG, { terminal: terminalId, ...extraParams })
(terminalId: string | null, extraParams?: Record<string, string>) => {
openDialogParam(updateParams, TERMINAL_DIALOG, { ...(terminalId ? { terminal: terminalId } : {}), ...extraParams })
},
[updateParams],
)
+16 -12
View File
@@ -1,6 +1,7 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { createRepoWorkspace, deleteRepoWorkspace, getRepoSiblings, type RepoSibling } from '@/api/repos'
import { showToast } from '@/lib/toast'
import type { CreateRepoWorkspaceRequest } from '@opencode-manager/shared/types'
export function repoSiblingsQueryKey(repoId: number | undefined) {
return ['repo', 'siblings', repoId] as const
@@ -24,22 +25,25 @@ export function useDeleteRepoWorkspaces(repoId: number | undefined) {
const results = await Promise.allSettled(
directories.map((directory) => deleteRepoWorkspace(repoId, directory)),
)
const failed = results.filter((result) => result.status === 'rejected').length
return { total: directories.length, failed }
const failures = results.flatMap((result) => (result.status === 'rejected' ? [result.reason] : []))
return { total: directories.length, failed: failures.length, firstError: failures[0] instanceof Error ? failures[0].message : null }
},
onSuccess: ({ total, failed }) => {
onSuccess: ({ total, failed, firstError }) => {
queryClient.invalidateQueries({ queryKey: repoSiblingsQueryKey(repoId) })
queryClient.invalidateQueries({ queryKey: ['schedule-worktrees'] })
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
const deleted = total - failed
const reason = firstError ? `: ${firstError}` : ''
if (failed === 0) {
showToast.success(deleted === 1 ? 'Workspace deleted' : `${deleted} workspaces deleted`)
showToast.success(deleted === 1 ? 'Worktree deleted' : `${deleted} worktrees deleted`)
} else if (deleted === 0) {
showToast.error('Failed to delete workspaces')
showToast.error(`Failed to delete worktrees${reason}`)
} else {
showToast.error(`Deleted ${deleted}, failed ${failed}`)
showToast.error(`Deleted ${deleted}, failed ${failed}${reason}`)
}
},
onError: () => {
showToast.error('Failed to delete workspaces')
showToast.error('Failed to delete worktrees')
},
})
}
@@ -48,16 +52,16 @@ export function useCreateRepoWorkspace(repoId: number | undefined) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async () => {
mutationFn: async (request: CreateRepoWorkspaceRequest = {}) => {
if (!repoId) throw new Error('Repo id is required')
return createRepoWorkspace(repoId)
return createRepoWorkspace(repoId, request)
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: repoSiblingsQueryKey(repoId) })
showToast.success('Workspace created')
showToast.success('Worktree created')
},
onError: () => {
showToast.error('Failed to create workspace')
onError: (error) => {
showToast.error(`Failed to create worktree: ${error.message}`)
},
})
}
+36
View File
@@ -11,9 +11,11 @@ import {
listAllScheduleRuns,
listAllSchedules,
listRepoScheduleRuns,
listScheduleWorktrees,
listUnreadScheduleRuns,
markAllScheduleRunsViewed,
markScheduleRunViewed,
removeScheduleWorktrees,
runRepoSchedule,
updateRepoSchedule,
} from '@/api/schedules'
@@ -228,6 +230,7 @@ export function useClearRepoScheduleRuns() {
onSuccess: (result, variables) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, variables.jobId] })
queryClient.invalidateQueries({ queryKey: ['all-schedule-runs'] })
queryClient.invalidateQueries({ queryKey: ['schedule-worktrees', variables.repoId, variables.jobId] })
showToast.success(result.cleared > 0 ? `Cleared ${result.cleared} run${result.cleared === 1 ? '' : 's'}` : 'No runs to clear')
},
onError: (error: unknown) => {
@@ -246,6 +249,7 @@ export function useDeleteRepoScheduleRun() {
onSuccess: (_data, variables) => {
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, variables.jobId] })
queryClient.invalidateQueries({ queryKey: ['all-schedule-runs'] })
queryClient.invalidateQueries({ queryKey: ['schedule-worktrees', variables.repoId, variables.jobId] })
showToast.success('Run deleted')
},
onError: (error: unknown) => {
@@ -253,3 +257,35 @@ export function useDeleteRepoScheduleRun() {
},
})
}
export function useScheduleWorktrees(repoId: number | undefined, jobId: number | null) {
return useQuery({
queryKey: ['schedule-worktrees', repoId, jobId],
queryFn: async () => {
const response = await listScheduleWorktrees(repoId!, jobId!)
return response.worktrees
},
enabled: repoId !== undefined && jobId !== null,
refetchInterval: jobId !== null ? 10000 : false,
})
}
export function useRemoveScheduleWorktrees() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ repoId, jobId, worktreePath }: { repoId: number; jobId: number; worktreePath?: string }) => {
return removeScheduleWorktrees(repoId, jobId, worktreePath)
},
onSuccess: (result, variables) => {
queryClient.invalidateQueries({ queryKey: ['schedule-worktrees', variables.repoId, variables.jobId] })
queryClient.invalidateQueries({ queryKey: ['repo-schedule-runs', variables.repoId, variables.jobId] })
queryClient.invalidateQueries({ queryKey: ['all-schedules'] })
queryClient.invalidateQueries({ queryKey: ['repo', 'siblings'] })
showToast.success(`Removed ${result.removed} worktree${result.removed === 1 ? '' : 's'}`)
},
onError: (error: unknown) => {
showToast.error(`Failed to remove worktree: ${error instanceof Error ? error.message : String(error)}`)
},
})
}