@@ -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,11 +470,14 @@ 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 (
480483 `${ REFERENCE . START } ([a-zA-Z_][a-zA-Z0-9_${ REFERENCE . PATH_DELIMITER } ]*[a-zA-Z0-9_])${ REFERENCE . END } ` ,
@@ -486,25 +489,22 @@ 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
@@ -537,18 +537,27 @@ function resolveTagVariables(
537537 */
538538function resolveCodeVariables (
539539 code : string ,
540- params : Record < string , any > ,
540+ params : Record < string , unknown > ,
541541 envVars : Record < string , string > = { } ,
542- blockData : Record < string , any > = { } ,
542+ blockData : Record < string , unknown > = { } ,
543543 blockNameMapping : Record < string , string > = { } ,
544- workflowVariables : Record < string , any > = { }
545- ) : { resolvedCode : string ; contextVariables : Record < string , any > } {
544+ blockOutputSchemas : Record < string , OutputSchema > = { } ,
545+ workflowVariables : Record < string , unknown > = { } ,
546+ language = 'javascript'
547+ ) : { resolvedCode : string ; contextVariables : Record < string , unknown > } {
546548 let resolvedCode = code
547- const contextVariables : Record < string , any > = { }
549+ const contextVariables : Record < string , unknown > = { }
548550
549551 resolvedCode = resolveWorkflowVariables ( resolvedCode , workflowVariables , contextVariables )
550552 resolvedCode = resolveEnvironmentVariables ( resolvedCode , params , envVars , contextVariables )
551- resolvedCode = resolveTagVariables ( resolvedCode , blockData , blockNameMapping , contextVariables )
553+ resolvedCode = resolveTagVariables (
554+ resolvedCode ,
555+ blockData ,
556+ blockNameMapping ,
557+ blockOutputSchemas ,
558+ contextVariables ,
559+ language
560+ )
552561
553562 return { resolvedCode, contextVariables }
554563}
@@ -585,6 +594,7 @@ export async function POST(req: NextRequest) {
585594 envVars = { } ,
586595 blockData = { } ,
587596 blockNameMapping = { } ,
597+ blockOutputSchemas = { } ,
588598 workflowVariables = { } ,
589599 workflowId,
590600 isCustomTool = false ,
@@ -601,20 +611,21 @@ export async function POST(req: NextRequest) {
601611 isCustomTool,
602612 } )
603613
604- // Resolve variables in the code with workflow environment variables
614+ const lang = isValidCodeLanguage ( language ) ? language : DEFAULT_CODE_LANGUAGE
615+
605616 const codeResolution = resolveCodeVariables (
606617 code ,
607618 executionParams ,
608619 envVars ,
609620 blockData ,
610621 blockNameMapping ,
611- workflowVariables
622+ blockOutputSchemas ,
623+ workflowVariables ,
624+ lang
612625 )
613626 resolvedCode = codeResolution . resolvedCode
614627 const contextVariables = codeResolution . contextVariables
615628
616- const lang = isValidCodeLanguage ( language ) ? language : DEFAULT_CODE_LANGUAGE
617-
618629 let jsImports = ''
619630 let jsRemainingCode = resolvedCode
620631 let hasImports = false
@@ -670,7 +681,11 @@ export async function POST(req: NextRequest) {
670681 prologue += `const environmentVariables = JSON.parse(${ JSON . stringify ( JSON . stringify ( envVars ) ) } );\n`
671682 prologueLineCount ++
672683 for ( const [ k , v ] of Object . entries ( contextVariables ) ) {
673- prologue += `const ${ k } = JSON.parse(${ JSON . stringify ( JSON . stringify ( v ) ) } );\n`
684+ if ( v === undefined ) {
685+ prologue += `const ${ k } = undefined;\n`
686+ } else {
687+ prologue += `const ${ k } = JSON.parse(${ JSON . stringify ( JSON . stringify ( v ) ) } );\n`
688+ }
674689 prologueLineCount ++
675690 }
676691
@@ -741,7 +756,11 @@ export async function POST(req: NextRequest) {
741756 prologue += `environmentVariables = json.loads(${ JSON . stringify ( JSON . stringify ( envVars ) ) } )\n`
742757 prologueLineCount ++
743758 for ( const [ k , v ] of Object . entries ( contextVariables ) ) {
744- prologue += `${ k } = json.loads(${ JSON . stringify ( JSON . stringify ( v ) ) } )\n`
759+ if ( v === undefined ) {
760+ prologue += `${ k } = None\n`
761+ } else {
762+ prologue += `${ k } = json.loads(${ JSON . stringify ( JSON . stringify ( v ) ) } )\n`
763+ }
745764 prologueLineCount ++
746765 }
747766 const wrapped = [
0 commit comments