Infer array current item schema (#15115)
This PR allows to infer the schema of the current item of an iterator step: - iterator step receive a variable - added an util that navigate to the array in schema - navigateOutputSchemaProperty - use the array value in schema to generate a new schema - used the existing getFunctionOutputSchema that I renamed and moved to twenty-shared Also cleaned a bit the existing schema for AI. Before https://github.com/user-attachments/assets/9767fc89-3524-4bfb-b1ab-8abe92084767 After https://github.com/user-attachments/assets/3650c1d2-14f2-44f9-b10c-e649fe04128d
This commit is contained in:
-53
@@ -1,53 +0,0 @@
|
||||
import { getFunctionOutputSchema } from '@/serverless-functions/utils/getFunctionOutputSchema';
|
||||
|
||||
describe('getFunctionOutputSchema', () => {
|
||||
it('should compute outputSchema properly', () => {
|
||||
const testResult = {
|
||||
a: null,
|
||||
b: 'b',
|
||||
c: { cc: 1 },
|
||||
d: true,
|
||||
e: [1, 2, 3],
|
||||
};
|
||||
const expectedOutputSchema = {
|
||||
a: {
|
||||
isLeaf: true,
|
||||
type: 'unknown',
|
||||
value: null,
|
||||
label: 'a',
|
||||
},
|
||||
b: {
|
||||
isLeaf: true,
|
||||
type: 'string',
|
||||
value: 'b',
|
||||
label: 'b',
|
||||
},
|
||||
c: {
|
||||
isLeaf: false,
|
||||
type: 'object',
|
||||
label: 'c',
|
||||
value: {
|
||||
cc: {
|
||||
isLeaf: true,
|
||||
type: 'number',
|
||||
value: 1,
|
||||
label: 'cc',
|
||||
},
|
||||
},
|
||||
},
|
||||
d: {
|
||||
isLeaf: true,
|
||||
type: 'boolean',
|
||||
value: true,
|
||||
label: 'd',
|
||||
},
|
||||
e: {
|
||||
isLeaf: true,
|
||||
type: 'array',
|
||||
value: [1, 2, 3],
|
||||
label: 'e',
|
||||
},
|
||||
};
|
||||
expect(getFunctionOutputSchema(testResult)).toEqual(expectedOutputSchema);
|
||||
});
|
||||
});
|
||||
-51
@@ -1,51 +0,0 @@
|
||||
import {
|
||||
type BaseOutputSchemaV2,
|
||||
type LeafType,
|
||||
} from '@/workflow/workflow-variables/types/BaseOutputSchemaV2';
|
||||
import { isObject } from '@sniptt/guards';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
const getValueType = (value: any): LeafType => {
|
||||
if (!isDefined(value) || value === null) {
|
||||
return 'unknown';
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return 'string';
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
return 'number';
|
||||
}
|
||||
if (typeof value === 'boolean') {
|
||||
return 'boolean';
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return 'array';
|
||||
}
|
||||
return 'unknown';
|
||||
};
|
||||
|
||||
export const getFunctionOutputSchema = (testResult: object) => {
|
||||
return testResult
|
||||
? Object.entries(testResult).reduce(
|
||||
(acc: BaseOutputSchemaV2, [key, value]) => {
|
||||
if (isObject(value) && !Array.isArray(value)) {
|
||||
acc[key] = {
|
||||
isLeaf: false,
|
||||
type: 'object',
|
||||
label: key,
|
||||
value: getFunctionOutputSchema(value),
|
||||
};
|
||||
} else {
|
||||
acc[key] = {
|
||||
isLeaf: true,
|
||||
value,
|
||||
type: getValueType(value),
|
||||
label: key,
|
||||
};
|
||||
}
|
||||
return acc;
|
||||
},
|
||||
{},
|
||||
)
|
||||
: {};
|
||||
};
|
||||
Reference in New Issue
Block a user