Files
opencode-manager/frontend/src/hooks/useChangeWalkthrough.ts
T
Chris Scott 0752ebf360 feat(walkthrough): stop an in-flight change walkthrough generation
Add DELETE /api/change-walkthroughs/:sessionId to cancel the generation for a session and source. The service removes the in-flight entry, persists the partial walkthrough, and aborts a per-generation signal threaded through model resolution and every model call. Cancellation is not recorded as a failure and leaves the remaining stops pending. A Stop button in the walkthrough chrome calls the new endpoint.
2026-10-10 17:48:14 +00:00

104 lines
3.9 KiB
TypeScript

import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { cancelChangeWalkthrough, generateChangeWalkthrough, getChangeWalkthrough } from '@/api/changeWalkthroughs'
import { walkthroughHunksIdentity, walkthroughSourceKey } from '@opencode-manager/shared/schemas'
import type {
ChangeWalkthrough,
ChangeWalkthroughState,
ChangeWalkthroughStateWire,
ChangeWalkthroughWire,
GenerateChangeWalkthroughRequest,
WalkthroughSource,
} from '@opencode-manager/shared/schemas'
const GENERATING_POLL_INTERVAL_MS = 2_000
export function changeWalkthroughQueryKey(sessionId: string, sourceKey?: string) {
return sourceKey === undefined
? (['change-walkthrough', sessionId] as const)
: (['change-walkthrough', sessionId, sourceKey] as const)
}
function isCompleteWalkthrough(walkthrough: ChangeWalkthroughWire): walkthrough is ChangeWalkthrough {
return walkthrough.hunks !== undefined
}
function mergeChangeWalkthroughState(
state: ChangeWalkthroughStateWire,
cached: ChangeWalkthrough | null,
): ChangeWalkthroughState {
const incoming = state.walkthrough
if (!incoming) {
return { ...state, walkthrough: null }
}
if (isCompleteWalkthrough(incoming)) {
return { ...state, walkthrough: incoming }
}
const hunks =
cached && walkthroughHunksIdentity(cached) === walkthroughHunksIdentity(incoming)
? cached.hunks
: []
return { ...state, walkthrough: { ...incoming, hunks } }
}
export function useChangeWalkthrough(
sessionId: string | undefined,
enabled: boolean,
source: WalkthroughSource,
) {
const sourceKey = walkthroughSourceKey(source)
const queryClient = useQueryClient()
const queryKey = changeWalkthroughQueryKey(sessionId ?? '', sourceKey)
return useQuery({
queryKey,
queryFn: async () => {
const cached = queryClient.getQueryData<ChangeWalkthroughState>(queryKey)
const cachedWalkthrough = cached?.walkthrough ?? null
const hunksFor = cachedWalkthrough ? walkthroughHunksIdentity(cachedWalkthrough) : undefined
const state =
hunksFor === undefined
? await getChangeWalkthrough(sessionId!, source)
: await getChangeWalkthrough(sessionId!, source, hunksFor)
return mergeChangeWalkthroughState(state, cachedWalkthrough)
},
enabled: enabled && !!sessionId,
staleTime: 30_000,
refetchInterval: (query) => (query.state.data?.generating ? GENERATING_POLL_INTERVAL_MS : false),
})
}
export function useGenerateChangeWalkthrough(sessionId: string | undefined, source: WalkthroughSource) {
const queryClient = useQueryClient()
const sourceKey = walkthroughSourceKey(source)
return useMutation({
mutationFn: async (request: GenerateChangeWalkthroughRequest) => {
const targetSessionId = sessionId!
const state = await generateChangeWalkthrough(targetSessionId, { ...request, source })
return { sessionId: targetSessionId, sourceKey, state }
},
onSuccess: ({ sessionId: targetSessionId, sourceKey: targetSourceKey, state }) => {
queryClient.setQueryData(changeWalkthroughQueryKey(targetSessionId, targetSourceKey), state)
},
})
}
export function useCancelChangeWalkthrough(sessionId: string | undefined, source: WalkthroughSource) {
const queryClient = useQueryClient()
const sourceKey = walkthroughSourceKey(source)
return useMutation({
mutationFn: async () => {
const targetSessionId = sessionId!
await cancelChangeWalkthrough(targetSessionId, source)
return { sessionId: targetSessionId, sourceKey }
},
onSuccess: ({ sessionId: targetSessionId, sourceKey: targetSourceKey }) => {
const queryKey = changeWalkthroughQueryKey(targetSessionId, targetSourceKey)
queryClient.setQueryData<ChangeWalkthroughState>(queryKey, (cached) =>
cached ? { ...cached, generating: false, error: null } : cached,
)
queryClient.invalidateQueries({ queryKey })
},
})
}