@@ -14,8 +14,7 @@ import {
1414import { generateRequestId } from '@/lib/core/utils/request'
1515import { getEffectiveDecryptedEnv } from '@/lib/environment/utils'
1616import { refreshTokenIfNeeded } from '@/app/api/auth/oauth/utils'
17- import { REFERENCE } from '@/executor/constants'
18- import { createEnvVarPattern } from '@/executor/utils/reference-validation'
17+ import { resolveEnvVarReferences } from '@/executor/utils/reference-validation'
1918import { executeTool } from '@/tools'
2019import { getTool , resolveToolId } from '@/tools/utils'
2120
@@ -28,45 +27,6 @@ const ExecuteToolSchema = z.object({
2827 workflowId : z . string ( ) . optional ( ) ,
2928} )
3029
31- /**
32- * Resolves all {{ENV_VAR}} references in a value recursively
33- * Works with strings, arrays, and objects
34- */
35- function resolveEnvVarReferences ( value : any , envVars : Record < string , string > ) : any {
36- if ( typeof value === 'string' ) {
37- // Check for exact match: entire string is "{{VAR_NAME}}"
38- const exactMatchPattern = new RegExp (
39- `^\\${ REFERENCE . ENV_VAR_START } ([^}]+)\\${ REFERENCE . ENV_VAR_END } $`
40- )
41- const exactMatch = exactMatchPattern . exec ( value )
42- if ( exactMatch ) {
43- const envVarName = exactMatch [ 1 ] . trim ( )
44- return envVars [ envVarName ] ?? value
45- }
46-
47- // Check for embedded references: "prefix {{VAR}} suffix"
48- const envVarPattern = createEnvVarPattern ( )
49- return value . replace ( envVarPattern , ( match , varName ) => {
50- const trimmedName = varName . trim ( )
51- return envVars [ trimmedName ] ?? match
52- } )
53- }
54-
55- if ( Array . isArray ( value ) ) {
56- return value . map ( ( item ) => resolveEnvVarReferences ( item , envVars ) )
57- }
58-
59- if ( value !== null && typeof value === 'object' ) {
60- const resolved : Record < string , any > = { }
61- for ( const [ key , val ] of Object . entries ( value ) ) {
62- resolved [ key ] = resolveEnvVarReferences ( val , envVars )
63- }
64- return resolved
65- }
66-
67- return value
68- }
69-
7030export async function POST ( req : NextRequest ) {
7131 const tracker = createRequestTracker ( )
7232
@@ -145,7 +105,17 @@ export async function POST(req: NextRequest) {
145105
146106 // Build execution params starting with LLM-provided arguments
147107 // Resolve all {{ENV_VAR}} references in the arguments
148- const executionParams : Record < string , any > = resolveEnvVarReferences ( toolArgs , decryptedEnvVars )
108+ const executionParams : Record < string , any > = resolveEnvVarReferences (
109+ toolArgs ,
110+ decryptedEnvVars ,
111+ {
112+ resolveExactMatch : true ,
113+ allowEmbedded : true ,
114+ trimKeys : true ,
115+ onMissing : 'keep' ,
116+ deep : true ,
117+ }
118+ ) as Record < string , any >
149119
150120 logger . info ( `[${ tracker . requestId } ] Resolved env var references in arguments` , {
151121 toolName,
0 commit comments