|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +// Copyright (C) 2026 CrewForm |
| 3 | + |
| 4 | +/** |
| 5 | + * Hook for re-running a single pipeline step. |
| 6 | + * |
| 7 | + * Calls the task-runner endpoint to re-execute a specific step |
| 8 | + * using the output of the previous step as input. |
| 9 | + * Only applicable to pipeline mode. |
| 10 | + */ |
| 11 | + |
| 12 | +import { useMutation, useQueryClient } from '@tanstack/react-query' |
| 13 | + |
| 14 | +interface RerunStepInput { |
| 15 | + runId: string |
| 16 | + teamId: string |
| 17 | + stepIdx: number |
| 18 | +} |
| 19 | + |
| 20 | +interface RerunStepResult { |
| 21 | + success: boolean |
| 22 | + message?: string |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Re-run a single pipeline step. |
| 27 | + * |
| 28 | + * The task-runner will: |
| 29 | + * 1. Fetch the previous step's output from team_messages |
| 30 | + * 2. Re-execute the agent at stepIdx with that input |
| 31 | + * 3. Write new messages for the re-run |
| 32 | + * 4. Reset downstream steps to pending |
| 33 | + */ |
| 34 | +export function useRerunStep() { |
| 35 | + const queryClient = useQueryClient() |
| 36 | + |
| 37 | + return useMutation<RerunStepResult, Error, RerunStepInput>({ |
| 38 | + mutationFn: async ({ runId, stepIdx }) => { |
| 39 | + const response = await fetch(`/api/team-runs/${runId}/steps/${stepIdx}/rerun`, { |
| 40 | + method: 'POST', |
| 41 | + headers: { 'Content-Type': 'application/json' }, |
| 42 | + }) |
| 43 | + |
| 44 | + if (!response.ok) { |
| 45 | + const body = await response.json().catch(() => ({})) as { error?: string } |
| 46 | + throw new Error(body.error ?? `Failed to re-run step ${stepIdx}`) |
| 47 | + } |
| 48 | + |
| 49 | + return response.json() as Promise<RerunStepResult> |
| 50 | + }, |
| 51 | + onSuccess: (_data, variables) => { |
| 52 | + // Invalidate the team run and messages queries |
| 53 | + void queryClient.invalidateQueries({ queryKey: ['team-run', variables.runId] }) |
| 54 | + void queryClient.invalidateQueries({ queryKey: ['team-messages', variables.runId] }) |
| 55 | + void queryClient.invalidateQueries({ queryKey: ['team', variables.teamId] }) |
| 56 | + }, |
| 57 | + }) |
| 58 | +} |
0 commit comments