( userProvidedParams: Record<string, unknown>, llmGeneratedParams: Record<string, unknown> )
| 776 | * fields that user left empty in the UI. |
| 777 | */ |
| 778 | export function mergeToolParameters( |
| 779 | userProvidedParams: Record<string, unknown>, |
| 780 | llmGeneratedParams: Record<string, unknown> |
| 781 | ): Record<string, unknown> { |
| 782 | // Filter out empty and effectively-empty values from user-provided params |
| 783 | // so that cleared fields don't override LLM values |
| 784 | const filteredUserParams: Record<string, unknown> = {} |
| 785 | for (const [key, value] of Object.entries(userProvidedParams)) { |
| 786 | if (isNonEmpty(value)) { |
| 787 | // Skip tag-based params if they're effectively empty (only default/unfilled entries) |
| 788 | if ((key === 'documentTags' || key === 'tagFilters') && isEmptyTagValue(value)) { |
| 789 | continue |
| 790 | } |
| 791 | filteredUserParams[key] = value |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | // Start with LLM params as base |
| 796 | const result: Record<string, unknown> = { ...llmGeneratedParams } |
| 797 | |
| 798 | // Apply user params, with special handling for inputMapping |
| 799 | for (const [key, userValue] of Object.entries(filteredUserParams)) { |
| 800 | if (key === 'inputMapping') { |
| 801 | // Deep merge inputMapping so LLM values fill in empty user fields |
| 802 | const llmInputMapping = llmGeneratedParams.inputMapping as Record<string, unknown> | undefined |
| 803 | const mergedInputMapping = deepMergeInputMapping( |
| 804 | llmInputMapping, |
| 805 | userValue as Record<string, unknown> | string | undefined |
| 806 | ) |
| 807 | result.inputMapping = mergedInputMapping |
| 808 | } else { |
| 809 | // Normal override for other params |
| 810 | result[key] = userValue |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | // If LLM provided inputMapping but user didn't, ensure it's included |
| 815 | if (llmGeneratedParams.inputMapping && !filteredUserParams.inputMapping) { |
| 816 | result.inputMapping = llmGeneratedParams.inputMapping |
| 817 | } |
| 818 | |
| 819 | return result |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * Filters out user-provided parameters from tool schema for LLM |
no test coverage detected