From b79f5a49a34be140ee0ce423ad022be053e059fc Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Thu, 13 Feb 2025 16:33:28 -0800 Subject: [PATCH] Resolved failed to evaluate condition error by adding util to wrap strings before evaluating them --- executor/utils.ts | 48 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/executor/utils.ts b/executor/utils.ts index 6dc4272d35..c799c5885b 100644 --- a/executor/utils.ts +++ b/executor/utils.ts @@ -1,3 +1,16 @@ +function stringifyValue(value: any): string { + if (typeof value === 'string') { + return `"${value.replace(/"/g, '\\"').replace(/\n/g, '\\n')}"` + } else if (value === null) { + return 'null' + } else if (typeof value === 'undefined') { + return 'undefined' + } else if (typeof value === 'object') { + return JSON.stringify(value) + } + return String(value) +} + export function resolveEnvVariables(value: any, environmentVariables: Record): any { if (typeof value === 'string') { const envMatches = value.match(/\{\{([^}]+)\}\}/g) @@ -33,30 +46,36 @@ export function resolveBlockReferences( ): string { const blockMatches = value.match(/<([^>]+)>/g) let resolvedValue = value + if (blockMatches) { for (const match of blockMatches) { // e.g. "" const path = match.slice(1, -1) const [blockRef, ...pathParts] = path.split('.') let sourceBlock = blockById.get(blockRef) + if (!sourceBlock) { const normalized = blockRef.toLowerCase().replace(/\s+/g, '') sourceBlock = blockByName.get(normalized) } + if (!sourceBlock) { throw new Error(`Block reference "${blockRef}" was not found.`) } + if (sourceBlock.enabled === false) { throw new Error( `Block "${sourceBlock.metadata?.title}" is disabled, and block "${currentBlockTitle}" depends on it.` ) } + const sourceState = contextBlockStates.get(sourceBlock.id) if (!sourceState) { throw new Error( `No state found for block "${sourceBlock.metadata?.title}" (ID: ${sourceBlock.id}).` ) } + // Drill into the property path. let replacementValue: any = sourceState for (const part of pathParts) { @@ -65,13 +84,29 @@ export function resolveBlockReferences( } replacementValue = replacementValue[part] } + if (replacementValue !== undefined) { - resolvedValue = resolvedValue.replace( - match, - typeof replacementValue === 'object' - ? JSON.stringify(replacementValue) - : String(replacementValue) - ) + // For condition blocks, we need to properly stringify the value + const isConditionBlock = + value.includes('!==') || + value.includes('===') || + value.includes('>=') || + value.includes('<=') || + value.includes('>') || + value.includes('<') || + value.includes('&&') || + value.includes('||') + + if (isConditionBlock) { + resolvedValue = resolvedValue.replace(match, stringifyValue(replacementValue)) + } else { + resolvedValue = resolvedValue.replace( + match, + typeof replacementValue === 'object' + ? JSON.stringify(replacementValue) + : String(replacementValue) + ) + } } else { throw new Error( `No value found at path "${path}" in block "${sourceBlock.metadata?.title}".` @@ -79,5 +114,6 @@ export function resolveBlockReferences( } } } + return resolvedValue }