Skip to content

Commit acfc2c6

Browse files
authored
fix(workflow-code): mark required dropdowns with no default required in sdk types (#10641)
codegen's hasDefaultValue treated every dropdown, switch and slider as holding a value, so the params type and `sim workflows source params` showed a required one with no default optional while `check` refused a call that omitted it. Block creation stores null for such a field (only the open editor panel seeds a dropdown's first option) and the serializer reports it missing, so the compiler was right: drop the always-valued rule, and give a required gate with no default no fallback value.
1 parent 1fe3be9 commit acfc2c6

3 files changed

Lines changed: 111 additions & 12 deletions

File tree

‎packages/workflow-code/src/catalog/surface.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,18 @@ export function typedForBuilder(
101101
)
102102
}
103103

104-
/** Controls that always hold a value (first option, off, minimum), so never required. */
105-
const ALWAYS_VALUED_TYPES: ReadonlySet<string> = new Set(['dropdown', 'switch', 'slider'])
106-
107-
/** Whether a param holds a value without source writing one: a default, or an always-valued control. */
104+
/**
105+
* Whether a param holds a value without source writing one: a declared default. A dropdown,
106+
* switch or slider with none holds nothing either: block creation stores `null` for it (only the
107+
* open editor panel seeds a dropdown's first option), so the serializer and the compiler's
108+
* `W_REQUIRED_MISSING` both treat an omitted required one as missing.
109+
*/
108110
export function hasDefaultValue(declarations: ReadonlyArray<CatalogSubBlock>): boolean {
109111
return declarations.some(
110112
(declaration) =>
111113
declaration.defaultValue !== undefined ||
112114
(declaration.valueDefault !== undefined && 'constant' in declaration.valueDefault) ||
113-
subBlockEditorDefault(declaration) !== undefined ||
114-
ALWAYS_VALUED_TYPES.has(declaration.type)
115+
subBlockEditorDefault(declaration) !== undefined
115116
)
116117
}
117118

‎packages/workflow-sdk/src/codegen/generate.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,19 @@ export function operationLabel(block: CatalogBlock, operation: string): string |
267267
return dropdown.options.find((option) => option.id === operation)?.label
268268
}
269269

270+
/**
271+
* The value an omitted gate runs as: its declared default, else its first option. A gate the
272+
* builder requires has none (`requiredForBuilder`), since creation stores `null` for it and the
273+
* compiler reports it missing.
274+
*/
270275
function defaultGateValue(
271276
candidates: readonly CatalogSubBlock[],
272-
field: string
277+
field: string,
278+
pinned: VisibilityContext['pinned']
273279
): Scalar | undefined {
274-
const subBlock = candidates.find((candidate) => candidate.id === field)
275-
if (!subBlock) return undefined
280+
const declarations = candidates.filter((candidate) => candidate.id === field)
281+
const [subBlock] = declarations
282+
if (!subBlock || requiredForBuilder(declarations, pinned)) return undefined
276283
const { defaultValue } = subBlock
277284
if (
278285
typeof defaultValue === 'string' ||
@@ -330,7 +337,7 @@ function paramFacts(
330337
.filter((param) => !gated.has(param.name))
331338
.map((param) => fact(param, requiredForBuilder(param.members, pinned)))
332339
if (!gate) return { common, gate: null }
333-
const fallback = defaultGateValue(candidates, gate.field)
340+
const fallback = defaultGateValue(candidates, gate.field, pinned)
334341
return {
335342
common,
336343
gate: {
@@ -365,7 +372,7 @@ function renderParamsType(
365372
const commonType = commonLines.length > 0 ? `{\n${commonLines.join('\n')}\n}` : '{}'
366373
if (!gate) return commonType
367374
const gateParam = params.find((param) => param.name === gate.field)
368-
const fallback = defaultGateValue(candidates, gate.field)
375+
const fallback = defaultGateValue(candidates, gate.field, pinned)
369376
const nullable =
370377
gateParam?.members.some((member) => subBlockEditorDefault(member) !== undefined) === true
371378
const variants = gate.variants.map((variant) => {

‎packages/workflow-sdk/src/codegen/reference.test.ts‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { readFileSync } from 'node:fs'
22
import { join } from 'node:path'
3-
import type { BlockCatalog } from '@sim/workflow-code'
3+
import {
4+
type BlockCatalog,
5+
type CatalogSubBlock,
6+
catalogBlock,
7+
catalogBuilder,
8+
compileWorkflow,
9+
} from '@sim/workflow-code'
410
import { catalogBuilderPaths } from '@sim/workflow-code/catalog/view'
511
import { describe, expect, it } from 'vitest'
612
import { CORE_DTS } from './core-text.generated'
@@ -88,3 +94,88 @@ describe('lookupBuilder for a name the SDK spells differently', () => {
8894
expect(hint('humanOutOfTheLoop')).not.toBe('Did you mean human?')
8995
})
9096
})
97+
98+
/** The sibling fields a declaration's visibility and requirement read. */
99+
function fieldsRead(sub: CatalogSubBlock): string[] {
100+
return [sub.condition, sub.required].flatMap((clause) =>
101+
clause && typeof clause === 'object' && 'field' in clause
102+
? [clause.field, ...(clause.and ? [clause.and.field] : [])]
103+
: []
104+
)
105+
}
106+
107+
/**
108+
* Compiles `<builder>('X', {})` for every builder and returns each `W_REQUIRED_MISSING` param the
109+
* builder's params facts show optional. A field whose requirement reads another field the call may
110+
* set (`provider`, `authType`) is not counted: the type cannot know that value.
111+
*/
112+
function optionalButRefused(catalog: BlockCatalog): { refused: number; optional: string[] } {
113+
const optional: string[] = []
114+
let refused = 0
115+
for (const key of catalogBuilderPaths(catalog)) {
116+
const reference = describeBuilder(catalog, key)
117+
const builder = catalogBuilder(catalog, key)
118+
const block = reference && catalogBlock(catalog, reference.blockType)
119+
if (!reference || !builder || !block || reference.coreParams.length > 0) continue
120+
const pinned = new Set(['operation', ...Object.keys(builder.fixed ?? {})])
121+
const root = key.split('.')[0]
122+
const result = compileWorkflow({
123+
entry: {
124+
path: 'workflows/w.ts',
125+
text: `// @sim-sdk 1\nimport { ${root}, workflow } from '@sim/workflow-sdk'\n\nexport default workflow('W', {}, () => {\n const x = ${key}('X', {})\n})\n`,
126+
},
127+
files: new Map(),
128+
catalog,
129+
sdkMajor: catalog.sdkMajor,
130+
})
131+
for (const { code, param } of result.diagnostics) {
132+
if (code !== 'W_REQUIRED_MISSING' || !param) continue
133+
const declarations = block.subBlocks.filter(
134+
(sub) => sub.id === param || sub.canonicalParamId === param
135+
)
136+
if (declarations.some((sub) => fieldsRead(sub).some((field) => !pinned.has(field)))) {
137+
continue
138+
}
139+
refused += 1
140+
const { common, gate } = reference.params
141+
const omittedVariant = gate?.variants.find(
142+
(variant) => gate.default !== undefined && variant.values.includes(gate.default)
143+
)
144+
const fact = [...common, ...(omittedVariant?.params ?? [])].find((f) => f.key === param)
145+
if (gate?.key === param ? gate.default !== undefined : fact?.required !== true) {
146+
optional.push(`${key}.${param}`)
147+
}
148+
}
149+
}
150+
return { refused, optional }
151+
}
152+
153+
/** The fixture catalog with Slack's required `listShareTarget` gate stripped of its default. */
154+
function withBareRequiredGate(): BlockCatalog {
155+
const catalog = structuredClone(CATALOG)
156+
const subBlocks = catalog.blocks.slack_v2?.subBlocks ?? []
157+
const index = subBlocks.findIndex((sub) => 'id' in sub && sub.id === 'listShareTarget')
158+
const gate = subBlocks[index]
159+
if (!gate || !('id' in gate) || gate.required !== true || !gate.valueDefault) {
160+
throw new Error('fixture slack_v2 has no required, defaulted listShareTarget')
161+
}
162+
const { valueDefault: _dropped, ...bare } = gate
163+
subBlocks[index] = bare
164+
return catalog
165+
}
166+
167+
describe('required params', () => {
168+
/**
169+
* `check` refuses a call that omits a field the builder alone decides is required, so its params
170+
* type and `sim workflows source params` must not show it optional: a dropdown or gate with no
171+
* default holds nothing until source writes it.
172+
*/
173+
it.each([
174+
['the fixture catalog', () => CATALOG],
175+
['a required gate with no default', withBareRequiredGate],
176+
])('marks required every param the compiler refuses to omit, over %s', (_label, catalog) => {
177+
const { refused, optional } = optionalButRefused(catalog())
178+
expect(refused).toBeGreaterThan(0)
179+
expect(optional).toEqual([])
180+
})
181+
})

0 commit comments

Comments
 (0)