Skip to content

The open alternative to Salesforce, designed for AI.

Infer the code step input schema in the debounced save so keystrokes can't be saved out of order (#28766)

Charles Bochet617518d010f8
diff --git a/packages/twenty-front/src/modules/logic-functions/hooks/useLogicFunctionForm.ts b/packages/twenty-front/src/modules/logic-functions/hooks/useLogicFunctionForm.ts
index ac5cfa246a3..1b8b6efb8fe 100644
--- a/packages/twenty-front/src/modules/logic-functions/hooks/useLogicFunctionForm.ts
+++ b/packages/twenty-front/src/modules/logic-functions/hooks/useLogicFunctionForm.ts
@@ -3,9 +3,9 @@ import {
useLogicFunctionUpdateFormState,
} from '@/logic-functions/hooks/useLogicFunctionUpdateFormState';
import { usePersistLogicFunction } from '@/logic-functions/hooks/usePersistLogicFunction';
+import { buildTriggerSettingsWithInputSchema } from '@/logic-functions/utils/buildTriggerSettingsWithInputSchema';
import {
getInputSchemaFromSourceCode,
- jsonSchemaToInputSchema,
type InputJsonSchema,
} from 'twenty-shared/logic-function';
import { isString } from '@sniptt/guards';
@@ -21,9 +21,14 @@ type UnsavedSourceHandlerCode = {
export const useLogicFunctionForm = ({
logicFunctionId,
getTargetLogicFunctionId,
+ onSourceHandlerCodeSaved,
}: {
logicFunctionId: string;
getTargetLogicFunctionId?: () => Promise<string>;
+ onSourceHandlerCodeSaved?: (savedSourceHandlerCode: {
+ targetLogicFunctionId: string;
+ inferredJsonSchema: InputJsonSchema;
+ }) => Promise<void> | void;
}) => {
const { updateLogicFunction } = usePersistLogicFunction();
@@ -43,12 +48,40 @@ export const useLogicFunctionForm = ({
async ({
targetLogicFunctionId,
update,
+ isSourceHandlerCodeChange = false,
}: {
targetLogicFunctionId: string;
update: LogicFunctionFormValues;
+ isSourceHandlerCodeChange?: boolean;
}) => {
+ if (!isSourceHandlerCodeChange) {
+ await updateLogicFunction({
+ input: { id: targetLogicFunctionId, update },
+ });
+
+ return;
+ }
+
+ // Not per keystroke: concurrent dynamic imports resolve out of order
+ const inferredJsonSchema = await getInputSchemaFromSourceCode(
+ update.sourceHandlerCode,
+ );
+
+ const triggerSettings = buildTriggerSettingsWithInputSchema({
+ formValues: update,
+ inferredJsonSchema,
+ });
+
+ setFormValues((prevState: LogicFunctionFormValues) => ({
+ ...prevState,
+ ...triggerSettings,
+ }));
+
const result = await updateLogicFunction({
- input: { id: targetLogicFunctionId, update },
+ input: {
+ id: targetLogicFunctionId,
+ update: { ...update, ...triggerSettings },
+ },
});
if (result.status !== 'successful') {
@@ -61,14 +94,17 @@ export const useLogicFunctionForm = ({
? undefined
: currentUnsavedSourceHandlerCode,
);
+
+ await onSourceHandlerCodeSaved?.({
+ targetLogicFunctionId,
+ inferredJsonSchema,
+ });
},
500,
);
const onChange = <TKey extends keyof LogicFunctionFormValues>(key: TKey) => {
- return async (
- value: LogicFunctionFormValues[TKey],
- ): Promise<InputJsonSchema | undefined> => {
+ return async (value: LogicFunctionFormValues[TKey]) => {
setFormValues((prevState: LogicFunctionFormValues) => ({
...prevState,
[key]: value,
@@ -91,7 +127,7 @@ export const useLogicFunctionForm = ({
update: { ...formValues, [key]: value },
});
- return undefined;
+ return;
}
setUnsavedSourceHandlerCode((currentUnsavedSourceHandlerCode) =>
@@ -100,36 +136,11 @@ export const useLogicFunctionForm = ({
: currentUnsavedSourceHandlerCode,
);
- const inferredJsonSchema = await getInputSchemaFromSourceCode(value);
-
- const triggerSettings = {
- toolTriggerSettings: isDefined(formValues.toolTriggerSettings)
- ? {
- ...formValues.toolTriggerSettings,
- inputSchema: inferredJsonSchema,
- }
- : null,
- workflowActionTriggerSettings: isDefined(
- formValues.workflowActionTriggerSettings,
- )
- ? {
- ...formValues.workflowActionTriggerSettings,
- inputSchema: jsonSchemaToInputSchema(inferredJsonSchema),
- }
- : null,
- };
-
- setFormValues((prevState: LogicFunctionFormValues) => ({
- ...prevState,
- ...triggerSettings,
- }));
-
await handleSave({
targetLogicFunctionId,
- update: { ...formValues, sourceHandlerCode: value, ...triggerSettings },
+ update: { ...formValues, sourceHandlerCode: value },
+ isSourceHandlerCodeChange,
});
-
- return inferredJsonSchema;
};
};
diff --git a/packages/twenty-front/src/modules/logic-functions/utils/__tests__/buildTriggerSettingsWithInputSchema.test.ts b/packages/twenty-front/src/modules/logic-functions/utils/__tests__/buildTriggerSettingsWithInputSchema.test.ts
new file mode 100644
index 00000000000..be73e4c8163
--- /dev/null
+++ b/packages/twenty-front/src/modules/logic-functions/utils/__tests__/buildTriggerSettingsWithInputSchema.test.ts
@@ -0,0 +1,44 @@
+import { buildTriggerSettingsWithInputSchema } from '@/logic-functions/utils/buildTriggerSettingsWithInputSchema';
+import {
+ jsonSchemaToInputSchema,
+ type InputJsonSchema,
+} from 'twenty-shared/logic-function';
+
+const INFERRED_JSON_SCHEMA: InputJsonSchema = {
+ type: 'object',
+ properties: { a: { type: 'string' } },
+};
+
+describe('buildTriggerSettingsWithInputSchema', () => {
+ it('sets the inferred schema on the defined trigger settings', () => {
+ expect(
+ buildTriggerSettingsWithInputSchema({
+ formValues: {
+ toolTriggerSettings: { inputSchema: { type: 'object' } },
+ workflowActionTriggerSettings: { inputSchema: [] },
+ },
+ inferredJsonSchema: INFERRED_JSON_SCHEMA,
+ }),
+ ).toEqual({
+ toolTriggerSettings: { inputSchema: INFERRED_JSON_SCHEMA },
+ workflowActionTriggerSettings: {
+ inputSchema: jsonSchemaToInputSchema(INFERRED_JSON_SCHEMA),
+ },
+ });
+ });
+
+ it('keeps missing trigger settings null', () => {
+ expect(
+ buildTriggerSettingsWithInputSchema({
+ formValues: {
+ toolTriggerSettings: null,
+ workflowActionTriggerSettings: null,
+ },
+ inferredJsonSchema: INFERRED_JSON_SCHEMA,
+ }),
+ ).toEqual({
+ toolTriggerSettings: null,
+ workflowActionTriggerSettings: null,
+ });
+ });
+});
diff --git a/packages/twenty-front/src/modules/logic-functions/utils/buildTriggerSettingsWithInputSchema.ts b/packages/twenty-front/src/modules/logic-functions/utils/buildTriggerSettingsWithInputSchema.ts
new file mode 100644
index 00000000000..36f6aa6b044
--- /dev/null
+++ b/packages/twenty-front/src/modules/logic-functions/utils/buildTriggerSettingsWithInputSchema.ts
@@ -0,0 +1,35 @@
+import { type LogicFunctionFormValues } from '@/logic-functions/hooks/useLogicFunctionUpdateFormState';
+import {
+ jsonSchemaToInputSchema,
+ type InputJsonSchema,
+} from 'twenty-shared/logic-function';
+import { isDefined } from 'twenty-shared/utils';
+
+export const buildTriggerSettingsWithInputSchema = ({
+ formValues,
+ inferredJsonSchema,
+}: {
+ formValues: Pick<
+ LogicFunctionFormValues,
+ 'toolTriggerSettings' | 'workflowActionTriggerSettings'
+ >;
+ inferredJsonSchema: InputJsonSchema;
+}): Pick<
+ LogicFunctionFormValues,
+ 'toolTriggerSettings' | 'workflowActionTriggerSettings'
+> => ({
+ toolTriggerSettings: isDefined(formValues.toolTriggerSettings)
+ ? {
+ ...formValues.toolTriggerSettings,
+ inputSchema: inferredJsonSchema,
+ }
+ : null,
+ workflowActionTriggerSettings: isDefined(
+ formValues.workflowActionTriggerSettings,
+ )
+ ? {
+ ...formValues.workflowActionTriggerSettings,
+ inputSchema: jsonSchemaToInputSchema(inferredJsonSchema),
+ }
+ : null,
+});
diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/code-action/components/WorkflowEditActionCode.tsx b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/code-action/components/WorkflowEditActionCode.tsx
index 99f0b7ecf0c..3ef9e022212 100644
--- a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/code-action/components/WorkflowEditActionCode.tsx
+++ b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/code-action/components/WorkflowEditActionCode.tsx
@@ -128,11 +128,6 @@ export const WorkflowEditActionCode = ({
return updatableStep.settings.input.logicFunctionId;
};
- const { formValues, loading, onChange } = useLogicFunctionForm({
- logicFunctionId,
- getTargetLogicFunctionId: getUpdatableLogicFunctionIdOrThrow,
- });
-
const {
executeLogicFunction,
isExecuting,
@@ -152,59 +147,62 @@ export const WorkflowEditActionCode = ({
action.settings.input.logicFunctionInput ?? {},
);
- const handleUpdateFunctionInputSchema = useDebouncedCallback(
- async ({
- inferredJsonSchema,
- logicFunctionId: updatableLogicFunctionId,
- }: {
- inferredJsonSchema: InputJsonSchema;
- logicFunctionId: string;
- }) => {
- if (actionOptions.readonly === true) {
- return;
- }
+ const handleSourceHandlerCodeSaved = ({
+ targetLogicFunctionId,
+ inferredJsonSchema,
+ }: {
+ targetLogicFunctionId: string;
+ inferredJsonSchema: InputJsonSchema;
+ }) => {
+ if (actionOptions.readonly === true) {
+ return;
+ }
- const inputSchema = jsonSchemaToInputSchema(inferredJsonSchema);
+ const inputSchema = jsonSchemaToInputSchema(inferredJsonSchema);
- const newFunctionInput = getFunctionInputFromInputSchema(inputSchema)[0];
+ const newFunctionInput = getFunctionInputFromInputSchema(inputSchema)[0];
- const newMergedInput = mergeDefaultFunctionInputAndFunctionInput({
- newInput: newFunctionInput,
- oldInput: action.settings.input.logicFunctionInput,
- });
+ const newMergedInput = mergeDefaultFunctionInputAndFunctionInput({
+ newInput: newFunctionInput,
+ oldInput: action.settings.input.logicFunctionInput,
+ });
- const newMergedTestInput = mergeDefaultFunctionInputAndFunctionInput({
- newInput: newFunctionInput,
- oldInput: logicFunctionTestData.input,
- });
+ const newMergedTestInput = mergeDefaultFunctionInputAndFunctionInput({
+ newInput: newFunctionInput,
+ oldInput: logicFunctionTestData.input,
+ });
- setFunctionInput(newMergedInput);
+ setFunctionInput(newMergedInput);
- updateLogicFunctionInput(newMergedTestInput);
+ updateLogicFunctionInput(newMergedTestInput);
- updateAction({
- ...action,
- settings: {
- ...action.settings,
- outputSchema: {
- link: {
- isLeaf: true,
- icon: 'IconVariable',
- tab: 'test',
- label: t`Generate Function Output`,
- },
- _outputSchemaType: 'LINK',
- },
- input: {
- ...action.settings.input,
- logicFunctionId: updatableLogicFunctionId,
- logicFunctionInput: newMergedInput,
+ updateAction({
+ ...action,
+ settings: {
+ ...action.settings,
+ outputSchema: {
+ link: {
+ isLeaf: true,
+ icon: 'IconVariable',
+ tab: 'test',
+ label: t`Generate Function Output`,
},
+ _outputSchemaType: 'LINK',
},
- });
- },
- 500,
- );
+ input: {
+ ...action.settings.input,
+ logicFunctionId: targetLogicFunctionId,
+ logicFunctionInput: newMergedInput,
+ },
+ },
+ });
+ };
+
+ const { formValues, loading, onChange } = useLogicFunctionForm({
+ logicFunctionId,
+ getTargetLogicFunctionId: getUpdatableLogicFunctionIdOrThrow,
+ onSourceHandlerCodeSaved: handleSourceHandlerCodeSaved,
+ });
const handleInputChange = async (value: any, path: string[]) => {
const updatedFunctionInput = setNestedValue(functionInput, path, value);
@@ -297,14 +295,7 @@ export const WorkflowEditActionCode = ({
return;
}
- const inferredJsonSchema = await onChange('sourceHandlerCode')(newCode);
-
- if (isDefined(inferredJsonSchema)) {
- await handleUpdateFunctionInputSchema({
- inferredJsonSchema,
- logicFunctionId: await getUpdatableLogicFunctionIdOrThrow(),
- });
- }
+ await onChange('sourceHandlerCode')(newCode);
};
const tabs = [