refactor: reuse the results key helper for the error branch predicate

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-11-17 22:01:02 +01:00
co-authored by Claude Opus 5.5
parent a4496be819
commit 1ac0da846c
2 changed files with 5 additions and 8 deletions
@@ -19,7 +19,9 @@ const YES_THRESHOLD = 0.5
const IDENT = /^[A-Za-z_$][\w$]*$/
function key(k: string): string {
/** `.k`, or `["k"]` when `k` is not a JS identifier: a step id pushed as YAML or through the API
* can be `step-a`, which `results.step-a` would read as a subtraction. */
export function key(k: string): string {
return IDENT.test(k) ? `.${k}` : `[${JSON.stringify(k)}]`
}
@@ -1,12 +1,7 @@
import type { FlowEditorContext } from './types'
import { getModuleArrayContainer } from './flowTree'
import { insertBranchOneAfter } from './branchOneInsert'
// The editor only creates identifier ids, but a flow pushed as YAML or through the API can name
// a step `step-a`, which `results.step-a` would read as a subtraction.
function resultKey(stepId: string): string {
return /^[A-Za-z_$][\w$]*$/.test(stepId) ? `.${stepId}` : `[${JSON.stringify(stepId)}]`
}
import { key } from './aiDecisionBranching'
/** 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 {
@@ -20,7 +15,7 @@ export function addErrorBranch(ctx: FlowEditorContext, stepId: string): Promise<
step.continue_on_error = true
if (inserted.value.type === 'branchone') {
inserted.value.branches = [
{ summary: 'On error', expr: `!!results${resultKey(stepId)}?.error`, modules: [] }
{ summary: 'On error', expr: `!!results${key(stepId)}?.error`, modules: [] }
]
}
})