diff --git a/frontend/src/lib/components/flows/content/FlowRunSettings.svelte b/frontend/src/lib/components/flows/content/FlowRunSettings.svelte index c200ab24b4..d6221d8e0b 100644 --- a/frontend/src/lib/components/flows/content/FlowRunSettings.svelte +++ b/frontend/src/lib/components/flows/content/FlowRunSettings.svelte @@ -33,8 +33,11 @@ import SettingGroup from '$lib/components/itemSettings/SettingGroup.svelte' import WorkspaceScriptSettingInfo from './WorkspaceScriptSettingInfo.svelte' import { slideDynamic } from '$lib/transitions' + import { GitBranch } from 'lucide-svelte' + import { addErrorBranch, canAddErrorBranch } from '../stepErrorBranch' - const { pathStore } = getContext('FlowEditorContext') + const flowEditorContext = getContext('FlowEditorContext') + const { pathStore } = flowEditorContext interface Props { flowModule: FlowModule @@ -110,6 +113,10 @@ const s3Snippet = $derived(s3Language ? s3Scripts[s3Language][s3Kind] : undefined) const concurrencyOn = $derived(hasInlineConcurrency(flowModule)) const concurrencyOff = $derived(!$enterpriseLicense || !concurrencyOn) + // Agent tools are not flow steps, and the preprocessor and error handler have no step after them. + const showAddErrorBranch = $derived( + !isAgentTool && !isFailure && canAddErrorBranch(flowEditorContext, flowModule.id) + ) // A resolved approval is recorded against the step the gate holds back, not this one, so // `continue_on_error` never sees it — the suspend option is the only way to continue past it. const suspendNeedsItsOwnContinueToggle = $derived( @@ -164,6 +171,18 @@ "The flow continues to the next step even if this step fails (after exhausting retries, if any). The step's error becomes its return, so a following branch can handle it." }} /> + {#if showAddErrorBranch} + + {/if} {#if suspendNeedsItsOwnContinueToggle} This only applies when the step's own code fails. A disapproval or an approval timeout diff --git a/frontend/src/lib/components/flows/stepErrorBranch.ts b/frontend/src/lib/components/flows/stepErrorBranch.ts new file mode 100644 index 0000000000..d11b97df80 --- /dev/null +++ b/frontend/src/lib/components/flows/stepErrorBranch.ts @@ -0,0 +1,38 @@ +import type { FlowModule } from '$lib/gen' +import { push } from '$lib/history.svelte' +import { refreshStateStore } from '$lib/svelte5Utils.svelte' +import type { FlowEditorContext } from './types' +import { getModuleArrayContainer } from './flowTree' +import { insertNewModuleAtIndex } from './flowStateUtils.svelte' + +/** Whether `stepId` sits in a step list a Branch to one can be inserted into after it. */ +export function canAddErrorBranch(ctx: FlowEditorContext, stepId: string): boolean { + return getModuleArrayContainer(ctx.flowStore.val.value, stepId) != null +} + +/** Turn on continue on error for `stepId` and insert a Branch to one right after it, whose one + * branch runs when the step failed and whose default runs otherwise. One undo entry. */ +export async function addErrorBranch( + { flowStore, flowStateStore, history, selectionManager }: FlowEditorContext, + stepId: string +): Promise { + const container = getModuleArrayContainer(flowStore.val.value, stepId) + if (!container) return + push(history, flowStore.val) + container.modules[container.index].continue_on_error = true + const modules = (await insertNewModuleAtIndex( + flowStore, + flowStateStore, + container.modules, + container.index + 1, + 'branchone' + )) as FlowModule[] + const inserted = modules[container.index + 1] + if (inserted.value.type === 'branchone') { + inserted.value.branches = [ + { summary: 'On error', expr: `!!results.${stepId}?.error`, modules: [] } + ] + } + refreshStateStore(flowStore) + selectionManager.selectId(inserted.id) +}