|
| 1 | +import { isRecordLike } from '@sim/utils/object' |
| 2 | +import { |
| 3 | + evaluateSubBlockCondition, |
| 4 | + isNonEmptyValue, |
| 5 | + type SubBlockCondition, |
| 6 | +} from '@/lib/workflows/subblocks/visibility' |
| 7 | +import type { SubBlockConfig } from '@/blocks/types' |
| 8 | + |
| 9 | +type ConditionSubBlock = Pick< |
| 10 | + SubBlockConfig, |
| 11 | + | 'id' |
| 12 | + | 'type' |
| 13 | + | 'mode' |
| 14 | + | 'condition' |
| 15 | + | 'reactiveCondition' |
| 16 | + | 'canonicalParamId' |
| 17 | + | 'password' |
| 18 | + | 'options' |
| 19 | +> |
| 20 | + |
| 21 | +/** |
| 22 | + * Whether `operation` is one of the block's own operation choices. Only then can a condition on |
| 23 | + * `operation` be decided: a stored row with no operation, or one holding a tool id the dropdown |
| 24 | + * does not list, leaves every operation-gated field undecided. |
| 25 | + */ |
| 26 | +function isBlockOperation( |
| 27 | + subBlocks: ConditionSubBlock[], |
| 28 | + operation: string, |
| 29 | + values: Record<string, unknown> |
| 30 | +): boolean { |
| 31 | + return subBlocks.some((subBlock) => { |
| 32 | + if (subBlock.id !== 'operation' || subBlock.mode === 'trigger') return false |
| 33 | + if (subBlock.mode === 'trigger-advanced' || !subBlock.options) return false |
| 34 | + let options: unknown |
| 35 | + try { |
| 36 | + options = |
| 37 | + typeof subBlock.options === 'function' ? subBlock.options({ values }) : subBlock.options |
| 38 | + } catch { |
| 39 | + return false |
| 40 | + } |
| 41 | + return ( |
| 42 | + Array.isArray(options) && |
| 43 | + options.some((option) => isRecordLike(option) && option.id === operation) |
| 44 | + ) |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +/** A stored scalar a condition can be decided against; JSON-shaped and empty values cannot. */ |
| 49 | +function isDecidableValue(value: unknown): boolean { |
| 50 | + if (typeof value === 'boolean' || typeof value === 'number') return true |
| 51 | + if (typeof value !== 'string') return false |
| 52 | + const trimmed = value.trim() |
| 53 | + return trimmed.length > 0 && !trimmed.startsWith('[') && !trimmed.startsWith('{') |
| 54 | +} |
| 55 | + |
| 56 | +/** The typed reading of a stored string, since a tool row stores `true` and `3` as text. */ |
| 57 | +function decodeScalar(value: unknown): unknown { |
| 58 | + if (value === 'true') return true |
| 59 | + if (value === 'false') return false |
| 60 | + if (typeof value === 'string' && value.trim() !== '' && Number.isFinite(Number(value))) { |
| 61 | + return Number(value) |
| 62 | + } |
| 63 | + return value |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * True only when the clause is decidably false. A clause on a field with no decidable value is |
| 68 | + * treated as satisfiable, and a stored string is also tried in its typed reading, so every doubt |
| 69 | + * resolves toward keeping the value. |
| 70 | + */ |
| 71 | +function isClauseFalse( |
| 72 | + clause: SubBlockCondition, |
| 73 | + values: Record<string, unknown>, |
| 74 | + isDecidableField: (field: string) => boolean |
| 75 | +): boolean { |
| 76 | + const { field } = clause |
| 77 | + if (!isDecidableField(field)) return false |
| 78 | + const single: SubBlockCondition = { field, value: clause.value, not: clause.not } |
| 79 | + if (evaluateSubBlockCondition(single, values)) return false |
| 80 | + const decoded = decodeScalar(values[field]) |
| 81 | + return decoded === values[field] || !evaluateSubBlockCondition(single, { [field]: decoded }) |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Resolves a function condition, or `null` when it read a field the row cannot decide. A function |
| 86 | + * condition names one field in what it returns but may branch on others (Video Generator gates a |
| 87 | + * setting on the model, which the model itself supplies at call time), so every read is watched. |
| 88 | + */ |
| 89 | +function resolveFunctionCondition( |
| 90 | + condition: (values?: Record<string, unknown>) => SubBlockCondition, |
| 91 | + values: Record<string, unknown>, |
| 92 | + isDecidableField: (field: string) => boolean |
| 93 | +): SubBlockCondition | null { |
| 94 | + let readUndecidedField = false |
| 95 | + const watched = new Proxy(values, { |
| 96 | + get(target, key, receiver) { |
| 97 | + if (typeof key === 'string' && !isDecidableField(key)) readUndecidedField = true |
| 98 | + return Reflect.get(target, key, receiver) |
| 99 | + }, |
| 100 | + has(target, key) { |
| 101 | + if (typeof key === 'string' && !isDecidableField(key)) readUndecidedField = true |
| 102 | + return Reflect.has(target, key) |
| 103 | + }, |
| 104 | + ownKeys(target) { |
| 105 | + readUndecidedField = true |
| 106 | + return Reflect.ownKeys(target) |
| 107 | + }, |
| 108 | + }) |
| 109 | + const resolved = condition(watched) |
| 110 | + return readUndecidedField ? null : resolved |
| 111 | +} |
| 112 | + |
| 113 | +function isCredentialSubBlock(subBlock: ConditionSubBlock): boolean { |
| 114 | + return ( |
| 115 | + subBlock.type === 'oauth-input' || |
| 116 | + subBlock.canonicalParamId === 'oauthCredential' || |
| 117 | + subBlock.password === true |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | +function isConditionHidden( |
| 122 | + subBlock: ConditionSubBlock, |
| 123 | + values: Record<string, unknown>, |
| 124 | + isDecidableField: (field: string) => boolean |
| 125 | +): boolean { |
| 126 | + if (isCredentialSubBlock(subBlock) || !subBlock.condition || subBlock.reactiveCondition) { |
| 127 | + return false |
| 128 | + } |
| 129 | + let condition: SubBlockCondition | null |
| 130 | + try { |
| 131 | + condition = |
| 132 | + typeof subBlock.condition === 'function' |
| 133 | + ? resolveFunctionCondition(subBlock.condition, values, isDecidableField) |
| 134 | + : subBlock.condition |
| 135 | + } catch { |
| 136 | + return false |
| 137 | + } |
| 138 | + if (!condition) return false |
| 139 | + const clauses = condition.and ? [condition, condition.and] : [condition] |
| 140 | + return clauses.some((clause) => isClauseFalse(clause, values, isDecidableField)) |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * Every sub-block that stores a value under `paramId`: those whose id or canonical id it is, |
| 145 | + * widened to the whole basic/advanced group, since either member may hold the group's value. |
| 146 | + */ |
| 147 | +function getWritingSubBlocks(subBlocks: ConditionSubBlock[], paramId: string): ConditionSubBlock[] { |
| 148 | + const groups = new Set<string>() |
| 149 | + for (const subBlock of subBlocks) { |
| 150 | + if ( |
| 151 | + (subBlock.id === paramId || subBlock.canonicalParamId === paramId) && |
| 152 | + subBlock.canonicalParamId |
| 153 | + ) { |
| 154 | + groups.add(subBlock.canonicalParamId) |
| 155 | + } |
| 156 | + } |
| 157 | + return subBlocks.filter( |
| 158 | + (subBlock) => |
| 159 | + subBlock.id === paramId || |
| 160 | + (subBlock.canonicalParamId !== undefined && groups.has(subBlock.canonicalParamId)) |
| 161 | + ) |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * Drops stored tool-row values the selected operation hides. |
| 166 | + * |
| 167 | + * A tool row keeps values across operation switches, but renders only the sub-blocks whose |
| 168 | + * `condition` holds for the selected operation. A value left by a previous operation is then |
| 169 | + * invisible to the user, and because it counts as user-provided it is also withheld from the |
| 170 | + * model's schema, yet it still reaches the block mapper and the tool. A value is dropped only |
| 171 | + * when every sub-block that writes it is decidably hidden under `{ ...params, operation }`. |
| 172 | + * |
| 173 | + * Kept: values no sub-block writes (pure tool params), values any writing sub-block shows (a |
| 174 | + * canonical pair counts as shown when any member is), credential and password fields, and any |
| 175 | + * value whose condition depends on a field the row has no decidable value for. Returns `params` |
| 176 | + * itself when nothing is dropped. |
| 177 | + */ |
| 178 | +export function omitConditionHiddenParams<T>( |
| 179 | + subBlocks: ConditionSubBlock[] | undefined, |
| 180 | + params: Record<string, T>, |
| 181 | + operation: string | undefined |
| 182 | +): Record<string, T> { |
| 183 | + if (!subBlocks?.length) return params |
| 184 | + const values: Record<string, unknown> = { ...params, operation } |
| 185 | + const operationKnown = Boolean(operation && isBlockOperation(subBlocks, operation, values)) |
| 186 | + const isDecidableField = (field: string) => |
| 187 | + field === 'operation' ? operationKnown : isDecidableValue(values[field]) |
| 188 | + const isHidden = (paramId: string, value: T): boolean => { |
| 189 | + if (paramId === 'operation' || !isNonEmptyValue(value)) return false |
| 190 | + const writers = getWritingSubBlocks(subBlocks, paramId) |
| 191 | + return ( |
| 192 | + writers.length > 0 && |
| 193 | + writers.every((subBlock) => isConditionHidden(subBlock, values, isDecidableField)) |
| 194 | + ) |
| 195 | + } |
| 196 | + const entries = Object.entries(params) |
| 197 | + const kept = entries.filter(([paramId, value]) => !isHidden(paramId, value)) |
| 198 | + return kept.length === entries.length ? params : Object.fromEntries(kept) |
| 199 | +} |
0 commit comments