( code: string, workflowVariables: Record<string, any>, contextVariables: Record<string, any> )
| 460 | } |
| 461 | |
| 462 | function resolveWorkflowVariables( |
| 463 | code: string, |
| 464 | workflowVariables: Record<string, any>, |
| 465 | contextVariables: Record<string, any> |
| 466 | ): string { |
| 467 | let resolvedCode = code |
| 468 | |
| 469 | const regex = createWorkflowVariablePattern() |
| 470 | let match: RegExpExecArray | null |
| 471 | const replacements: Array<{ |
| 472 | match: string |
| 473 | index: number |
| 474 | variableName: string |
| 475 | variableValue: unknown |
| 476 | }> = [] |
| 477 | |
| 478 | while ((match = regex.exec(code)) !== null) { |
| 479 | const variableName = match[1].trim() |
| 480 | |
| 481 | const foundVariable = Object.entries(workflowVariables).find( |
| 482 | ([_, variable]) => normalizeName(variable.name || '') === variableName |
| 483 | ) |
| 484 | |
| 485 | if (!foundVariable) { |
| 486 | const availableVars = Object.values(workflowVariables) |
| 487 | .map((v) => v.name) |
| 488 | .filter(Boolean) |
| 489 | throw new Error( |
| 490 | `Variable "${variableName}" doesn't exist.` + |
| 491 | (availableVars.length > 0 ? ` Available: ${availableVars.join(', ')}` : '') |
| 492 | ) |
| 493 | } |
| 494 | |
| 495 | const variable = foundVariable[1] |
| 496 | let variableValue: unknown = variable.value |
| 497 | |
| 498 | if (variable.value !== undefined && variable.value !== null) { |
| 499 | const type = variable.type === 'string' ? 'plain' : variable.type |
| 500 | |
| 501 | if (type === 'number') { |
| 502 | variableValue = Number(variableValue) |
| 503 | } else if (type === 'boolean') { |
| 504 | if (typeof variableValue === 'boolean') { |
| 505 | // Already a boolean, keep as-is |
| 506 | } else { |
| 507 | const normalized = String(variableValue).toLowerCase().trim() |
| 508 | variableValue = normalized === 'true' |
| 509 | } |
| 510 | } else if (type === 'json' && typeof variableValue === 'string') { |
| 511 | try { |
| 512 | variableValue = JSON.parse(variableValue) |
| 513 | } catch { |
| 514 | // Keep as-is |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | replacements.push({ |
no test coverage detected