Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(nodespec): read Optional fields' choices and limits from their anyOf branch
trohitg326c17e8aba8
diff --git a/client/src/adapters/__tests__/nodeSpecToDescription.test.ts b/client/src/adapters/__tests__/nodeSpecToDescription.test.tsindex 51307f62..4c2a90ab 100644--- a/client/src/adapters/__tests__/nodeSpecToDescription.test.ts+++ b/client/src/adapters/__tests__/nodeSpecToDescription.test.ts@@ -110,6 +110,41 @@ describe('nodeSpecToDescription contract', () => {expect(def.properties![0].type).toBe('string');});+ it('reads options and bounds from the non-null branch of Optional[T]', () => {+ // Pydantic writes Optional[Literal[...]] and Optional[float] with+ // ge/le exactly like this: enum and bounds live on the anyOf branch.+ // Reading them only from the top level gave every chat model's+ // reasoning_effort an empty dropdown.+ const spec: NodeSpec = {+ type: 'optEnumNode',+ displayName: 'Optional Enum Node',+ icon: 'π΅',+ group: ['utility'],+ version: 1,+ inputs: {+ properties: {+ reasoning_effort: {+ anyOf: [{ enum: ['low', 'medium', 'high'], type: 'string' }, { type: 'null' }],+ default: null,+ },+ temperature: {+ anyOf: [{ type: 'number', minimum: 0, maximum: 2 }, { type: 'null' }],+ },+ },+ },+ };+ expectValid(spec);+ const [effort, temperature] = nodeSpecToDescription(spec).properties!;+ expect(effort.type).toBe('options');+ expect(effort.options).toEqual([+ { name: 'low', value: 'low' },+ { name: 'medium', value: 'medium' },+ { name: 'high', value: 'high' },+ ]);+ expect(temperature.type).toBe('number');+ expect(temperature.typeOptions).toMatchObject({ minValue: 0, maxValue: 2 });+ });+it.each([['code editor hint', { editor: 'code' }, 'code'],['json editor hint', { editor: 'json' }, 'json'],diff --git a/client/src/adapters/nodeSpecToDescription.ts b/client/src/adapters/nodeSpecToDescription.tsindex 920cda84..7a86b1d7 100644--- a/client/src/adapters/nodeSpecToDescription.ts+++ b/client/src/adapters/nodeSpecToDescription.ts@@ -104,6 +104,15 @@ interface JsonSchemaProperty {type InodeType = INodeProperties['type'];+/**+ * The non-null branch of a Pydantic `Optional[T]`, which JSON Schema writes+ * as `anyOf: [T, {type: 'null'}]`. `T`'s own keywords (`enum`, `minimum`,+ * `maximum`) live on that branch, not on the property itself.+ */+function nonNullBranch(prop: JsonSchemaProperty): JsonSchemaProperty | undefined {+ return prop.anyOf?.find(b => (b.type && b.type !== 'null') || (b.enum && b.enum.length > 0));+}+/*** Convert a JSON Schema property β INodeProperties.type value expected* by the parameter renderer. Prefers `enum` β `options`, then falls back@@ -129,10 +138,8 @@ function mapPropertyType(prop: JsonSchemaProperty): InodeType {if (widget === 'file' || prop.format === 'binary') return 'file';if (prop.format === 'date-time' || prop.format === 'date') return 'dateTime';// anyOf([T, null]) is the Pydantic Optional[T] pattern β take the non-null branch.- if (prop.anyOf) {- const nonNull = prop.anyOf.find(b => b.type && b.type !== 'null');- if (nonNull) return mapPropertyType(nonNull);- }+ const nonNull = nonNullBranch(prop);+ if (nonNull) return mapPropertyType(nonNull);const t = Array.isArray(prop.type) ? prop.type[0] : prop.type;switch (t) {case 'boolean':@@ -156,12 +163,17 @@ function toInodeProperty(required: boolean,): INodeProperties {const type = mapPropertyType(prop);+ // An Optional[T] field keeps T's keywords on its non-null anyOf branch.+ const branch = nonNullBranch(prop);+ const enumValues = prop.enum ?? branch?.enum;+ const minimum = prop.minimum ?? branch?.minimum;+ const maximum = prop.maximum ?? branch?.maximum;// For `options`: prefer uiHints.options (richer labels) when supplied,// otherwise derive bare {name, value} pairs from JSON Schema enum.const richOptions = prop.uiHints?.options as INodeProperties['options'] | undefined;const options =richOptions ??- prop.enum?.map(v => ({ name: String(v), value: v as string | number | boolean }));+ enumValues?.map(v => ({ name: String(v), value: v as string | number | boolean }));const out: INodeProperties = {displayName: (prop.uiHints?.displayName as string | undefined) || prop.title || name,name,@@ -171,10 +183,10 @@ function toInodeProperty(options,};if (required) out.required = true;- if (prop.minimum !== undefined || prop.maximum !== undefined) {+ if (minimum !== undefined || maximum !== undefined) {out.typeOptions = {- ...(prop.minimum !== undefined ? { minValue: prop.minimum } : {}),- ...(prop.maximum !== undefined ? { maxValue: prop.maximum } : {}),+ ...(minimum !== undefined ? { minValue: minimum } : {}),+ ...(maximum !== undefined ? { maxValue: maximum } : {}),};}// Lift Pydantic Field(json_schema_extra=...) hints. Pydantic merges