Spaces:
Build error
Build error
File size: 6,263 Bytes
d9494a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import { isNonEmptyArray, isObject } from '@sniptt/guards';
import { isDefined } from '@/utils';
import { TRIGGER_STEP_ID } from '@/workflow/constants/TriggerStepId';
import { parseVariablePath } from '@/workflow/utils/variable-path.util';
import {
type ValidatableWorkflow,
type ValidatableWorkflowStep,
type WorkflowValidationIssue,
} from '@/workflow/validation/types/workflow-validation.type';
import { type WorkflowGraph } from '@/workflow/validation/utils/build-workflow-graph.util';
import { extractVariablesFromInput } from '@/workflow/validation/utils/extract-variables-from-input.util';
import { getVariablePathSuggestions } from '@/workflow/validation/utils/get-variable-path-suggestions.util';
import {
collectOutputSchemaVariablePaths,
resolveVariablePathInOutputSchema,
} from '@/workflow/workflow-schema/utils/resolve-variable-path-in-output-schema';
export const validateWorkflowVariableReferences = ({
workflow,
graph,
stepsById,
}: {
workflow: ValidatableWorkflow;
graph: WorkflowGraph;
stepsById: Map<string, ValidatableWorkflowStep>;
}): WorkflowValidationIssue[] => {
const issues: WorkflowValidationIssue[] = [];
const stepIds = new Set(workflow.steps?.map((step) => step.id) ?? []);
for (const step of workflow.steps ?? []) {
const variables = extractVariablesFromInput(step.settings?.input);
const ancestors = graph.ancestorsByStepId.get(step.id) ?? new Set<string>();
for (const variable of variables) {
const pathSegments = parseVariablePath(variable);
const referencedStepId = pathSegments[0];
if (!isDefined(referencedStepId)) {
issues.push({
severity: 'error',
code: 'VARIABLE_INVALID_PATH',
message: `Step "${step.name ?? step.id}" has a variable "{{${variable}}}" with an invalid path. Variable references must start with a step ID, e.g. "{{stepId.property}}".`,
stepId: step.id,
path: variable,
});
continue;
}
const isTriggerReference = referencedStepId === TRIGGER_STEP_ID;
if (!isTriggerReference && !stepIds.has(referencedStepId)) {
issues.push({
severity: 'error',
code: 'VARIABLE_UNKNOWN_STEP',
message: `Step "${step.name ?? step.id}" references variable "{{${variable}}}" from an unknown step "${referencedStepId}".`,
stepId: step.id,
path: variable,
});
continue;
}
const isSelfReference = referencedStepId === step.id;
// The trigger always runs before every step, so a trigger reference is
// upstream by definition even when it is not present in the ancestor set.
if (
!isTriggerReference &&
!isSelfReference &&
!ancestors.has(referencedStepId)
) {
const referencedStep = stepsById.get(referencedStepId);
const referencedStepLabel = referencedStep?.name ?? referencedStepId;
issues.push({
severity: 'error',
code: 'VARIABLE_NOT_UPSTREAM',
message: `Step "${step.name ?? step.id}" references variable "{{${variable}}}" from step "${referencedStepLabel}", which does not run before it. Ensure step "${referencedStepLabel}" is an ancestor (connected via nextStepIds chain from the trigger, before this step).`,
stepId: step.id,
path: variable,
});
continue;
}
issues.push(
...validateVariablePathAgainstOutputSchema({
step,
variable,
pathSegments,
referencedStepId,
isTriggerReference,
trigger: workflow.trigger,
stepsById,
}),
);
}
}
return issues;
};
const validateVariablePathAgainstOutputSchema = ({
step,
variable,
pathSegments,
referencedStepId,
isTriggerReference,
trigger,
stepsById,
}: {
step: ValidatableWorkflowStep;
variable: string;
pathSegments: string[];
referencedStepId: string;
isTriggerReference: boolean;
trigger: ValidatableWorkflow['trigger'];
stepsById: Map<string, ValidatableWorkflowStep>;
}): WorkflowValidationIssue[] => {
const propertyPath = pathSegments.slice(1);
if (propertyPath.length === 0) {
return [];
}
const outputSchema = isTriggerReference
? trigger?.settings?.outputSchema
: stepsById.get(referencedStepId)?.settings?.outputSchema;
const isEmptyOutputSchema =
isDefined(outputSchema) &&
isObject(outputSchema) &&
!Array.isArray(outputSchema) &&
Object.keys(outputSchema).length === 0;
if (!isDefined(outputSchema) || isEmptyOutputSchema) {
return [];
}
const resolved = resolveVariablePathInOutputSchema({
schema: outputSchema,
propertyPath,
});
if (!resolved.found) {
const suggestions = getVariablePathSuggestions({
schema: outputSchema,
propertyPath,
referencedStepId,
});
const availablePaths = collectAvailablePaths(
outputSchema,
referencedStepId,
);
const hint = isNonEmptyArray(suggestions)
? `Did you mean "{{${suggestions[0]}}}"?${
suggestions.length > 1
? ` Other options: ${suggestions
.slice(1)
.map((suggestion) => `{{${suggestion}}}`)
.join(', ')}.`
: ''
}`
: isNonEmptyArray(availablePaths)
? `Available paths: ${availablePaths.map((path) => `{{${path}}}`).join(', ')}.`
: undefined;
return [
{
severity: 'error',
code: 'VARIABLE_PATH_NOT_FOUND',
message: `Step "${step.name ?? step.id}" references variable "{{${variable}}}" but the path "${propertyPath.join('.')}" was not found in the output of step "${referencedStepId}".`,
stepId: step.id,
path: variable,
...(isDefined(hint) ? { hint } : {}),
...(isNonEmptyArray(suggestions) ? { suggestions } : {}),
...(isNonEmptyArray(availablePaths) ? { availablePaths } : {}),
},
];
}
return [];
};
const MAX_AVAILABLE_PATHS = 20;
const collectAvailablePaths = (
outputSchema: unknown,
referencedStepId: string,
): string[] =>
collectOutputSchemaVariablePaths(outputSchema)
.slice(0, MAX_AVAILABLE_PATHS)
.map((path) => `${referencedStepId}.${path}`);
|