(opts: {
defaults: Record<string, unknown>;
explicitArgs: Record<string, unknown>;
exclusivePairs?: readonly ExclusiveParameterGroup[];
})
| 31 | } |
| 32 | |
| 33 | export function mergeSessionDefaultArgs(opts: { |
| 34 | defaults: Record<string, unknown>; |
| 35 | explicitArgs: Record<string, unknown>; |
| 36 | exclusivePairs?: readonly ExclusiveParameterGroup[]; |
| 37 | }): Record<string, unknown> { |
| 38 | const sanitizedArgs: Record<string, unknown> = {}; |
| 39 | for (const [key, value] of Object.entries(opts.explicitArgs)) { |
| 40 | if (!hasConcreteSessionDefaultValue(value)) { |
| 41 | continue; |
| 42 | } |
| 43 | sanitizedArgs[key] = value; |
| 44 | } |
| 45 | |
| 46 | const merged: Record<string, unknown> = { ...opts.defaults, ...sanitizedArgs }; |
| 47 | |
| 48 | if ( |
| 49 | opts.defaults.env && |
| 50 | typeof opts.defaults.env === 'object' && |
| 51 | !Array.isArray(opts.defaults.env) && |
| 52 | sanitizedArgs.env && |
| 53 | typeof sanitizedArgs.env === 'object' && |
| 54 | !Array.isArray(sanitizedArgs.env) |
| 55 | ) { |
| 56 | merged.env = { |
| 57 | ...(opts.defaults.env as Record<string, string>), |
| 58 | ...(sanitizedArgs.env as Record<string, string>), |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | for (const pair of opts.exclusivePairs ?? []) { |
| 63 | const userProvidedConcrete = pair.some((key) => |
| 64 | Object.prototype.hasOwnProperty.call(sanitizedArgs, key), |
| 65 | ); |
| 66 | if (!userProvidedConcrete) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | for (const key of pair) { |
| 71 | if (!Object.prototype.hasOwnProperty.call(sanitizedArgs, key) && key in merged) { |
| 72 | delete merged[key]; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | for (const pair of opts.exclusivePairs ?? []) { |
| 78 | const allFromDefaults = pair.every( |
| 79 | (key) => !Object.prototype.hasOwnProperty.call(sanitizedArgs, key), |
| 80 | ); |
| 81 | if (!allFromDefaults) { |
| 82 | continue; |
| 83 | } |
| 84 | |
| 85 | const presentKeys = pair.filter((key) => hasConcreteSessionDefaultValue(merged[key])); |
| 86 | if (presentKeys.length <= 1) { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | for (let index = 1; index < presentKeys.length; index += 1) { |
no test coverage detected