* Processes variables from various input formats into a standardized key-value object
(variables: any)
| 42 | * Processes variables from various input formats into a standardized key-value object |
| 43 | */ |
| 44 | function processVariables(variables: any): Record<string, string> | undefined { |
| 45 | if (!variables) return undefined |
| 46 | |
| 47 | let variablesObject: Record<string, string> = {} |
| 48 | |
| 49 | if (Array.isArray(variables)) { |
| 50 | variables.forEach((item: any) => { |
| 51 | if (item?.cells?.Key && typeof item.cells.Key === 'string') { |
| 52 | variablesObject[item.cells.Key] = item.cells.Value || '' |
| 53 | } |
| 54 | }) |
| 55 | } else if (typeof variables === 'object' && variables !== null) { |
| 56 | variablesObject = { ...variables } |
| 57 | } else if (typeof variables === 'string') { |
| 58 | try { |
| 59 | variablesObject = JSON.parse(variables) |
| 60 | } catch (_e) { |
| 61 | logger.warn('Failed to parse variables string as JSON', { variables }) |
| 62 | return undefined |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (Object.keys(variablesObject).length === 0) { |
| 67 | return undefined |
| 68 | } |
| 69 | |
| 70 | return variablesObject |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Substitutes variable placeholders in text with their actual values |