( variables: Record<string, VarValue>, skipResolveVars?: string[], varsResolvedFromSkipped?: Set<string>, )
| 52 | } |
| 53 | |
| 54 | export function resolveVariables( |
| 55 | variables: Record<string, VarValue>, |
| 56 | skipResolveVars?: string[], |
| 57 | varsResolvedFromSkipped?: Set<string>, |
| 58 | ): Record<string, VarValue> { |
| 59 | let resolved: boolean; |
| 60 | const regex = /\{\{\s*(\w+)\s*\}\}/; // Matches {{variableName}}, {{ variableName }}, etc. |
| 61 | |
| 62 | let iterations = 0; |
| 63 | do { |
| 64 | resolved = true; |
| 65 | for (const key of Object.keys(variables)) { |
| 66 | if ( |
| 67 | skipResolveVars?.includes(key) || |
| 68 | varsResolvedFromSkipped?.has(key) || |
| 69 | typeof variables[key] !== 'string' |
| 70 | ) { |
| 71 | continue; |
| 72 | } |
| 73 | const value = variables[key] as string; |
| 74 | const match = regex.exec(value); |
| 75 | if (match) { |
| 76 | const [placeholder, varName] = match; |
| 77 | if (variables[varName] === undefined) { |
| 78 | // Do nothing - final nunjucks render will fail if necessary. |
| 79 | // logger.warn(`Variable "${varName}" not found for substitution.`); |
| 80 | } else { |
| 81 | variables[key] = value.replace(placeholder, variables[varName] as string); |
| 82 | if (skipResolveVars?.includes(varName) || varsResolvedFromSkipped?.has(varName)) { |
| 83 | varsResolvedFromSkipped?.add(key); |
| 84 | } |
| 85 | resolved = false; // Indicate that we've made a replacement and should check again |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | iterations++; |
| 90 | } while (!resolved && iterations < 5); |
| 91 | |
| 92 | return variables; |
| 93 | } |
| 94 | |
| 95 | // Utility: Detect partial/unclosed Nunjucks tags and wrap in {% raw %} if needed |
| 96 | function autoWrapRawIfPartialNunjucks(prompt: string): string { |
no test coverage detected
searching dependent graphs…