( context: ToolPermissionContext, update: PermissionUpdate, )
| 53 | * @returns The updated permission context |
| 54 | */ |
| 55 | export function applyPermissionUpdate( |
| 56 | context: ToolPermissionContext, |
| 57 | update: PermissionUpdate, |
| 58 | ): ToolPermissionContext { |
| 59 | switch (update.type) { |
| 60 | case 'setMode': |
| 61 | logForDebugging( |
| 62 | `Applying permission update: Setting mode to '${update.mode}'`, |
| 63 | ) |
| 64 | return { |
| 65 | ...context, |
| 66 | mode: update.mode, |
| 67 | } |
| 68 | |
| 69 | case 'addRules': { |
| 70 | const ruleStrings = update.rules.map(rule => |
| 71 | permissionRuleValueToString(rule), |
| 72 | ) |
| 73 | logForDebugging( |
| 74 | `Applying permission update: Adding ${update.rules.length} ${update.behavior} rule(s) to destination '${update.destination}': ${jsonStringify(ruleStrings)}`, |
| 75 | ) |
| 76 | |
| 77 | // Determine which collection to update based on behavior |
| 78 | const ruleKind = |
| 79 | update.behavior === 'allow' |
| 80 | ? 'alwaysAllowRules' |
| 81 | : update.behavior === 'deny' |
| 82 | ? 'alwaysDenyRules' |
| 83 | : 'alwaysAskRules' |
| 84 | |
| 85 | return { |
| 86 | ...context, |
| 87 | [ruleKind]: { |
| 88 | ...context[ruleKind], |
| 89 | [update.destination]: [ |
| 90 | ...(context[ruleKind][update.destination] || []), |
| 91 | ...ruleStrings, |
| 92 | ], |
| 93 | }, |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | case 'replaceRules': { |
| 98 | const ruleStrings = update.rules.map(rule => |
| 99 | permissionRuleValueToString(rule), |
| 100 | ) |
| 101 | logForDebugging( |
| 102 | `Replacing all ${update.behavior} rules for destination '${update.destination}' with ${update.rules.length} rule(s): ${jsonStringify(ruleStrings)}`, |
| 103 | ) |
| 104 | |
| 105 | // Determine which collection to update based on behavior |
| 106 | const ruleKind = |
| 107 | update.behavior === 'allow' |
| 108 | ? 'alwaysAllowRules' |
| 109 | : update.behavior === 'deny' |
| 110 | ? 'alwaysDenyRules' |
| 111 | : 'alwaysAskRules' |
| 112 |
no test coverage detected