Skip to content

Commit bff8a9e

Browse files
authored
Merge branch 'staging' into sweep2/v1
2 parents 1b22de1 + e018bde commit bff8a9e

9 files changed

Lines changed: 436 additions & 23 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/param-dependents.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getTransitiveSubBlockDependents } from '@/lib/workflows/subblocks/dependencies'
2+
import { omitConditionHiddenParams } from '@/lib/workflows/tool-input/hidden-params'
23
import type { StoredTool } from '@/lib/workflows/tool-input/types'
34
import { getBlock } from '@/blocks/registry'
45
import type { ToolMetadata } from '@/tools/metadata'
@@ -27,10 +28,14 @@ export function clearDependentToolParams(
2728
return next ?? params
2829
}
2930

30-
/** Keeps user-configured inputs that the newly selected operation accepts. */
31+
/**
32+
* Keeps user-configured inputs that the newly selected operation accepts and still shows: a
33+
* value whose fields the operation hides would otherwise linger out of sight and still run.
34+
*/
3135
export function getParamsForToolOperation(
3236
tool: Pick<StoredTool, 'type' | 'params'>,
33-
nextTool: ToolMetadata
37+
nextTool: ToolMetadata,
38+
operation: string
3439
): Record<string, string> {
3540
const newParamIds = new Set(
3641
Object.entries(nextTool.params ?? {})
@@ -64,5 +69,6 @@ export function getParamsForToolOperation(
6469
Object.entries(tool.params || {}).forEach(([paramId, value]) => {
6570
if (newParamIds.has(paramId) && value) preservedParams[paramId] = value
6671
})
67-
return clearDependentToolParams(tool.type, preservedParams, 'operation')
72+
const visibleParams = omitConditionHiddenParams(subBlocks, preservedParams, operation)
73+
return clearDependentToolParams(tool.type, visibleParams, 'operation')
6874
}

‎apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ export const ToolInput = memo(function ToolInput({
981981
return
982982
}
983983

984-
const preservedParams = getParamsForToolOperation(tool, newToolConfig)
984+
const preservedParams = getParamsForToolOperation(tool, newToolConfig, operation)
985985

986986
if (tool.type === 'jira') {
987987
const subBlockStore = useSubBlockStore.getState()

‎apps/sim/executor/handlers/human-in-the-loop/handler.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isCanonicalPair,
88
scopeCanonicalModesForTool,
99
} from '@/lib/workflows/subblocks/visibility'
10+
import { omitConditionHiddenParams } from '@/lib/workflows/tool-input/hidden-params'
1011
import { getBlock } from '@/blocks/registry'
1112
import type { BlockOutput } from '@/blocks/types'
1213
import {
@@ -422,10 +423,13 @@ export class HumanInTheLoopBlockHandler implements BlockHandler {
422423
Object.values(buildCanonicalIndex(subBlocks).groupsById) as CanonicalGroup[]
423424
).filter(isCanonicalPair)
424425

426+
const operation =
427+
toolConfig.operation ??
428+
(typeof storedParams.operation === 'string' ? storedParams.operation : undefined)
429+
const visibleParams = omitConditionHiddenParams(subBlocks, storedParams, operation)
430+
425431
const { paramsTransform } = buildBlockToolParamsTransform({
426-
operation:
427-
toolConfig.operation ??
428-
(typeof storedParams.operation === 'string' ? storedParams.operation : undefined),
432+
operation,
429433
blockSubBlocks: subBlocks,
430434
blockParamsFn: notificationBlock?.tools?.config?.params,
431435
blockInputDefs: notificationBlock?.inputs,
@@ -438,18 +442,18 @@ export class HumanInTheLoopBlockHandler implements BlockHandler {
438442
),
439443
})
440444

441-
if (!paramsTransform) return storedParams
445+
if (!paramsTransform) return visibleParams
442446

443447
try {
444-
return paramsTransform(storedParams)
448+
return paramsTransform(visibleParams)
445449
} catch (error) {
446450
// Mirrors `prepareToolExecution`: a transform that throws must not take the
447451
// notification down with it, so the raw params still go out.
448452
logger.warn('Notification tool params transform failed, using raw params', {
449453
toolId,
450454
error,
451455
})
452-
return storedParams
456+
return visibleParams
453457
}
454458
}
455459

‎apps/sim/executor/handlers/pi/local/sim-tools.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,15 @@ function buildSimToolSpec(
167167
if (!inputProjection.complete || !projectedTool) {
168168
return unavailableToolResult()
169169
}
170+
// Only the stored keys the transform kept: a value the selected operation hides was
171+
// dropped from `preseededParams`, so its projected copy must be dropped too.
172+
const projectedPreseededParams = Object.fromEntries(
173+
Object.entries(projectedTool.params || {}).filter(([key]) =>
174+
Object.hasOwn(preseededParams, key)
175+
)
176+
)
170177
const mergedProjectedParams = mergeToolParameters(
171-
projectedTool.params || {},
178+
projectedPreseededParams,
172179
args as Record<string, unknown>
173180
)
174181

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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

Comments
 (0)