@@ -6,11 +6,11 @@ import { executeInE2B } from '@/lib/execution/e2b'
66import { executeInIsolatedVM } from '@/lib/execution/isolated-vm'
77import { CodeLanguage , DEFAULT_CODE_LANGUAGE , isValidCodeLanguage } from '@/lib/execution/languages'
88import { escapeRegExp , normalizeName , REFERENCE } from '@/executor/constants'
9+ import { type OutputSchema , resolveBlockReference } from '@/executor/utils/block-reference'
910import {
1011 createEnvVarPattern ,
1112 createWorkflowVariablePattern ,
1213} from '@/executor/utils/reference-validation'
13- import { navigatePath } from '@/executor/variables/resolvers/reference'
1414export const dynamic = 'force-dynamic'
1515export const runtime = 'nodejs'
1616
@@ -470,14 +470,17 @@ function resolveEnvironmentVariables(
470470
471471function resolveTagVariables (
472472 code : string ,
473- blockData : Record < string , any > ,
473+ blockData : Record < string , unknown > ,
474474 blockNameMapping : Record < string , string > ,
475- contextVariables : Record < string , any >
475+ blockOutputSchemas : Record < string , OutputSchema > ,
476+ contextVariables : Record < string , unknown > ,
477+ language = 'javascript'
476478) : string {
477479 let resolvedCode = code
480+ const undefinedLiteral = language === 'python' ? 'None' : 'undefined'
478481
479482 const tagPattern = new RegExp (
480- `${ REFERENCE . START } ([a-zA-Z_][a-zA-Z0-9_${ REFERENCE . PATH_DELIMITER } ]*[a-zA-Z0-9_])${ REFERENCE . END } ` ,
483+ `${ REFERENCE . START } ([a-zA-Z_](?: [a-zA-Z0-9_${ REFERENCE . PATH_DELIMITER } ]*[a-zA-Z0-9_])? )${ REFERENCE . END } ` ,
481484 'g'
482485 )
483486 const tagMatches = resolvedCode . match ( tagPattern ) || [ ]
@@ -486,41 +489,37 @@ function resolveTagVariables(
486489 const tagName = match . slice ( REFERENCE . START . length , - REFERENCE . END . length ) . trim ( )
487490 const pathParts = tagName . split ( REFERENCE . PATH_DELIMITER )
488491 const blockName = pathParts [ 0 ]
492+ const fieldPath = pathParts . slice ( 1 )
489493
490- const blockId = blockNameMapping [ blockName ]
491- if ( ! blockId ) {
492- continue
493- }
494+ const result = resolveBlockReference ( blockName , fieldPath , {
495+ blockNameMapping,
496+ blockData,
497+ blockOutputSchemas,
498+ } )
494499
495- const blockOutput = blockData [ blockId ]
496- if ( blockOutput === undefined ) {
500+ if ( ! result ) {
497501 continue
498502 }
499503
500- let tagValue : any
501- if ( pathParts . length === 1 ) {
502- tagValue = blockOutput
503- } else {
504- tagValue = navigatePath ( blockOutput , pathParts . slice ( 1 ) )
505- }
504+ let tagValue = result . value
506505
507506 if ( tagValue === undefined ) {
507+ resolvedCode = resolvedCode . replace ( new RegExp ( escapeRegExp ( match ) , 'g' ) , undefinedLiteral )
508508 continue
509509 }
510510
511- if (
512- typeof tagValue === 'string' &&
513- tagValue . length > 100 &&
514- ( tagValue . startsWith ( '{' ) || tagValue . startsWith ( '[' ) )
515- ) {
516- try {
517- tagValue = JSON . parse ( tagValue )
518- } catch {
519- // Keep as-is
511+ if ( typeof tagValue === 'string' ) {
512+ const trimmed = tagValue . trimStart ( )
513+ if ( trimmed . startsWith ( '{' ) || trimmed . startsWith ( '[' ) ) {
514+ try {
515+ tagValue = JSON . parse ( tagValue )
516+ } catch {
517+ // Keep as string if not valid JSON
518+ }
520519 }
521520 }
522521
523- const safeVarName = `__tag_${ tagName . replace ( / [ ^ a - z A - Z 0 - 9 _ ] / g, '_ ' ) } `
522+ const safeVarName = `__tag_${ tagName . replace ( / _ / g, '_1' ) . replace ( / \. / g , '_0 ') } `
524523 contextVariables [ safeVarName ] = tagValue
525524 resolvedCode = resolvedCode . replace ( new RegExp ( escapeRegExp ( match ) , 'g' ) , safeVarName )
526525 }
@@ -537,18 +536,27 @@ function resolveTagVariables(
537536 */
538537function resolveCodeVariables (
539538 code : string ,
540- params : Record < string , any > ,
539+ params : Record < string , unknown > ,
541540 envVars : Record < string , string > = { } ,
542- blockData : Record < string , any > = { } ,
541+ blockData : Record < string , unknown > = { } ,
543542 blockNameMapping : Record < string , string > = { } ,
544- workflowVariables : Record < string , any > = { }
545- ) : { resolvedCode : string ; contextVariables : Record < string , any > } {
543+ blockOutputSchemas : Record < string , OutputSchema > = { } ,
544+ workflowVariables : Record < string , unknown > = { } ,
545+ language = 'javascript'
546+ ) : { resolvedCode : string ; contextVariables : Record < string , unknown > } {
546547 let resolvedCode = code
547- const contextVariables : Record < string , any > = { }
548+ const contextVariables : Record < string , unknown > = { }
548549
549550 resolvedCode = resolveWorkflowVariables ( resolvedCode , workflowVariables , contextVariables )
550551 resolvedCode = resolveEnvironmentVariables ( resolvedCode , params , envVars , contextVariables )
551- resolvedCode = resolveTagVariables ( resolvedCode , blockData , blockNameMapping , contextVariables )
552+ resolvedCode = resolveTagVariables (
553+ resolvedCode ,
554+ blockData ,
555+ blockNameMapping ,
556+ blockOutputSchemas ,
557+ contextVariables ,
558+ language
559+ )
552560
553561 return { resolvedCode, contextVariables }
554562}
@@ -585,6 +593,7 @@ export async function POST(req: NextRequest) {
585593 envVars = { } ,
586594 blockData = { } ,
587595 blockNameMapping = { } ,
596+ blockOutputSchemas = { } ,
588597 workflowVariables = { } ,
589598 workflowId,
590599 isCustomTool = false ,
@@ -601,20 +610,21 @@ export async function POST(req: NextRequest) {
601610 isCustomTool,
602611 } )
603612
604- // Resolve variables in the code with workflow environment variables
613+ const lang = isValidCodeLanguage ( language ) ? language : DEFAULT_CODE_LANGUAGE
614+
605615 const codeResolution = resolveCodeVariables (
606616 code ,
607617 executionParams ,
608618 envVars ,
609619 blockData ,
610620 blockNameMapping ,
611- workflowVariables
621+ blockOutputSchemas ,
622+ workflowVariables ,
623+ lang
612624 )
613625 resolvedCode = codeResolution . resolvedCode
614626 const contextVariables = codeResolution . contextVariables
615627
616- const lang = isValidCodeLanguage ( language ) ? language : DEFAULT_CODE_LANGUAGE
617-
618628 let jsImports = ''
619629 let jsRemainingCode = resolvedCode
620630 let hasImports = false
@@ -670,7 +680,11 @@ export async function POST(req: NextRequest) {
670680 prologue += `const environmentVariables = JSON.parse(${ JSON . stringify ( JSON . stringify ( envVars ) ) } );\n`
671681 prologueLineCount ++
672682 for ( const [ k , v ] of Object . entries ( contextVariables ) ) {
673- prologue += `const ${ k } = JSON.parse(${ JSON . stringify ( JSON . stringify ( v ) ) } );\n`
683+ if ( v === undefined ) {
684+ prologue += `const ${ k } = undefined;\n`
685+ } else {
686+ prologue += `const ${ k } = JSON.parse(${ JSON . stringify ( JSON . stringify ( v ) ) } );\n`
687+ }
674688 prologueLineCount ++
675689 }
676690
@@ -741,7 +755,11 @@ export async function POST(req: NextRequest) {
741755 prologue += `environmentVariables = json.loads(${ JSON . stringify ( JSON . stringify ( envVars ) ) } )\n`
742756 prologueLineCount ++
743757 for ( const [ k , v ] of Object . entries ( contextVariables ) ) {
744- prologue += `${ k } = json.loads(${ JSON . stringify ( JSON . stringify ( v ) ) } )\n`
758+ if ( v === undefined ) {
759+ prologue += `${ k } = None\n`
760+ } else {
761+ prologue += `${ k } = json.loads(${ JSON . stringify ( JSON . stringify ( v ) ) } )\n`
762+ }
745763 prologueLineCount ++
746764 }
747765 const wrapped = [
0 commit comments