53fdac1417
## Summary Replaces the bolted-on `isTool` + `toolInputSchema` fields on `LogicFunctionManifest` with two distinct, opt-in triggers that align with the existing `cron` / `databaseEvent` / `httpRoute` trigger pattern: - **`toolTriggerSettings`** — exposes the function as an AI tool (chat / MCP / function calling). Uses standard JSON Schema (the format LLMs natively understand). - **`workflowActionTriggerSettings`** — exposes the function as a step in the visual workflow builder. Uses Twenty's rich `InputSchema` so the builder can render proper `FieldMetadataType`-aware editors, variable pickers, labels, and an optional `outputSchema`. A function can opt into none, one, or both. Each surface gets the schema format appropriate for it. ### Why `isTool: true` previously exposed the function as both an AI tool AND a workflow node, with the same JSON Schema feeding both — but the workflow builder really wants Twenty's `InputSchema` (with `CURRENCY`, `RELATION`, `EMAILS`, etc.) and the AI surface really wants standard JSON Schema. Today the workflow builder hacks around this by treating JSON Schema as `InputSchema`, which silently breaks for any non-primitive field type. Splitting the triggers fixes that and lets each surface evolve independently. ### Migration - **Fast** instance command adds the two new nullable columns. - **Slow** instance command backfills `toolTriggerSettings` + `workflowActionTriggerSettings` from `isTool=true` rows (preserving today's both-surfaces behaviour) then drops the legacy columns. ### Stacked Stacked on top of #20181. Merge that first, then this. ## Test plan - [ ] CI green (oxlint, typecheck, jest, vitest) - [ ] Run `--include-slow` upgrade against a workspace with existing `isTool=true` logic functions; verify both new columns populated and old columns dropped - [ ] Verify AI chat sees migrated tool functions (Linear create-issue, Exa search) and can call them with the JSON Schema - [ ] Add an AI-tool function from the Settings UI (toggles `toolTriggerSettings`) and verify it shows up in chat - [ ] Add a workflow-action function from the Settings UI (toggles `workflowActionTriggerSettings`) and verify it appears in the workflow node picker - [ ] In the workflow builder, edit a `LOGIC_FUNCTION` step and verify input fields render (no more JSON-Schema-as-InputSchema hack) - [ ] Try defining a function with no triggers in the SDK and verify `defineLogicFunction` rejects it 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: martmull <martmull@hotmail.fr>
97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
import { useGetOneLogicFunction } from '@/logic-functions/hooks/useGetOneLogicFunction';
|
|
import { type Dispatch, type SetStateAction, useEffect, useState } from 'react';
|
|
import { isDefined } from 'twenty-shared/utils';
|
|
import {
|
|
type CronTriggerSettings,
|
|
type DatabaseEventTriggerSettings,
|
|
type HttpRouteTriggerSettings,
|
|
type ToolTriggerSettings,
|
|
type WorkflowActionTriggerSettings,
|
|
} from 'twenty-shared/application';
|
|
import { type LogicFunction } from '~/generated-metadata/graphql';
|
|
import { useGetLogicFunctionSourceCode } from '@/logic-functions/hooks/useGetLogicFunctionSourceCode';
|
|
|
|
export type LogicFunctionFormValues = {
|
|
name: string;
|
|
description: string;
|
|
timeoutSeconds: number;
|
|
sourceHandlerCode: string;
|
|
cronTriggerSettings: CronTriggerSettings | null;
|
|
databaseEventTriggerSettings: DatabaseEventTriggerSettings | null;
|
|
httpRouteTriggerSettings: HttpRouteTriggerSettings | null;
|
|
toolTriggerSettings: ToolTriggerSettings | null;
|
|
workflowActionTriggerSettings: WorkflowActionTriggerSettings | null;
|
|
};
|
|
|
|
type SetLogicFunctionFormValues = Dispatch<
|
|
SetStateAction<LogicFunctionFormValues>
|
|
>;
|
|
|
|
export const useLogicFunctionUpdateFormState = ({
|
|
logicFunctionId,
|
|
}: {
|
|
logicFunctionId: string;
|
|
}): {
|
|
formValues: LogicFunctionFormValues;
|
|
logicFunction: LogicFunction | null;
|
|
setFormValues: SetLogicFunctionFormValues;
|
|
loading: boolean;
|
|
} => {
|
|
const [formValues, setFormValues] = useState<LogicFunctionFormValues>({
|
|
name: '',
|
|
description: '',
|
|
sourceHandlerCode: '',
|
|
timeoutSeconds: 300,
|
|
cronTriggerSettings: null,
|
|
databaseEventTriggerSettings: null,
|
|
httpRouteTriggerSettings: null,
|
|
toolTriggerSettings: null,
|
|
workflowActionTriggerSettings: null,
|
|
});
|
|
|
|
const { sourceHandlerCode, loading: logicFunctionSourceCodeLoading } =
|
|
useGetLogicFunctionSourceCode({
|
|
logicFunctionId,
|
|
});
|
|
|
|
const { logicFunction, loading: logicFunctionLoading } =
|
|
useGetOneLogicFunction({
|
|
id: logicFunctionId,
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (isDefined(logicFunction)) {
|
|
setFormValues((prevState) => ({
|
|
...prevState,
|
|
name: logicFunction.name || '',
|
|
description: logicFunction.description || '',
|
|
timeoutSeconds: logicFunction.timeoutSeconds ?? 300,
|
|
cronTriggerSettings: logicFunction.cronTriggerSettings ?? null,
|
|
databaseEventTriggerSettings:
|
|
logicFunction.databaseEventTriggerSettings ?? null,
|
|
httpRouteTriggerSettings:
|
|
logicFunction.httpRouteTriggerSettings ?? null,
|
|
toolTriggerSettings: logicFunction.toolTriggerSettings ?? null,
|
|
workflowActionTriggerSettings:
|
|
logicFunction.workflowActionTriggerSettings ?? null,
|
|
}));
|
|
}
|
|
}, [logicFunction]);
|
|
|
|
useEffect(() => {
|
|
if (isDefined(sourceHandlerCode)) {
|
|
setFormValues((prev) => ({
|
|
...prev,
|
|
sourceHandlerCode,
|
|
}));
|
|
}
|
|
}, [sourceHandlerCode]);
|
|
|
|
return {
|
|
formValues,
|
|
setFormValues,
|
|
logicFunction,
|
|
loading: logicFunctionLoading || logicFunctionSourceCodeLoading,
|
|
};
|
|
};
|