( code: string, blockData: Record<string, unknown>, blockNameMapping: Record<string, string>, blockOutputSchemas: Record<string, OutputSchema>, contextVariables: Record<string, unknown>, language = 'javascript' )
| 589 | } |
| 590 | |
| 591 | function resolveTagVariables( |
| 592 | code: string, |
| 593 | blockData: Record<string, unknown>, |
| 594 | blockNameMapping: Record<string, string>, |
| 595 | blockOutputSchemas: Record<string, OutputSchema>, |
| 596 | contextVariables: Record<string, unknown>, |
| 597 | language = 'javascript' |
| 598 | ): string { |
| 599 | let resolvedCode = code |
| 600 | const undefinedLiteral = language === 'python' ? 'None' : 'undefined' |
| 601 | |
| 602 | const tagMatches = resolvedCode.match(TAG_PATTERN) || [] |
| 603 | |
| 604 | for (const match of tagMatches) { |
| 605 | const tagName = match.slice(REFERENCE.START.length, -REFERENCE.END.length).trim() |
| 606 | const pathParts = tagName.split(REFERENCE.PATH_DELIMITER) |
| 607 | const blockName = pathParts[0] |
| 608 | const fieldPath = pathParts.slice(1) |
| 609 | |
| 610 | const result = resolveBlockReference(blockName, fieldPath, { |
| 611 | blockNameMapping, |
| 612 | blockData, |
| 613 | blockOutputSchemas, |
| 614 | }) |
| 615 | |
| 616 | if (!result) { |
| 617 | continue |
| 618 | } |
| 619 | |
| 620 | let tagValue = result.value |
| 621 | |
| 622 | if (tagValue === undefined) { |
| 623 | resolvedCode = resolvedCode.replace(new RegExp(escapeRegExp(match), 'g'), undefinedLiteral) |
| 624 | continue |
| 625 | } |
| 626 | |
| 627 | if (typeof tagValue === 'string') { |
| 628 | const trimmed = tagValue.trimStart() |
| 629 | if (trimmed.startsWith('{') || trimmed.startsWith('[')) { |
| 630 | try { |
| 631 | tagValue = JSON.parse(tagValue) |
| 632 | } catch { |
| 633 | // Keep as string if not valid JSON |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | const safeVarName = `__tag_${tagName.replace(/_/g, '_1').replace(/\./g, '_0')}` |
| 639 | contextVariables[safeVarName] = tagValue |
| 640 | resolvedCode = resolvedCode.replace(new RegExp(escapeRegExp(match), 'g'), safeVarName) |
| 641 | } |
| 642 | |
| 643 | return resolvedCode |
| 644 | } |
| 645 | |
| 646 | /** |
| 647 | * Resolves environment variables and tags in code |
no test coverage detected