Allow AI to update code steps (#17761)

https://github.com/user-attachments/assets/35ac5c23-4d5c-4c86-8233-7b58fbeb5a27

- add tool
- move resolver code into a service

---------

Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
This commit is contained in:
Thomas Trompette
2026-02-09 16:40:44 +01:00
committed by GitHub
parent 4ae375308f
commit 2a76e1791e
11 changed files with 398 additions and 187 deletions
@@ -3,6 +3,7 @@ import { Injectable } from '@nestjs/common';
import { type ToolSet } from 'ai';
import { RecordPositionService } from 'src/engine/core-modules/record-position/services/record-position.service';
import { LogicFunctionService } from 'src/engine/metadata-modules/logic-function/services/logic-function.service';
import { GlobalWorkspaceOrmManager } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-orm.manager';
import { type RolePermissionConfig } from 'src/engine/twenty-orm/types/role-permission-config';
import { WorkflowSchemaWorkspaceService } from 'src/modules/workflow/workflow-builder/workflow-schema/workflow-schema.workspace-service';
@@ -20,6 +21,7 @@ import { createDeactivateWorkflowVersionTool } from 'src/modules/workflow/workfl
import { createDeleteWorkflowVersionEdgeTool } from 'src/modules/workflow/workflow-tools/tools/delete-workflow-version-edge.tool';
import { createDeleteWorkflowVersionStepTool } from 'src/modules/workflow/workflow-tools/tools/delete-workflow-version-step.tool';
import { createGetWorkflowCurrentVersionTool } from 'src/modules/workflow/workflow-tools/tools/get-workflow-current-version.tool';
import { createUpdateLogicFunctionSourceTool } from 'src/modules/workflow/workflow-tools/tools/update-logic-function-source.tool';
import { createUpdateWorkflowVersionPositionsTool } from 'src/modules/workflow/workflow-tools/tools/update-workflow-version-positions.tool';
import { createUpdateWorkflowVersionStepTool } from 'src/modules/workflow/workflow-tools/tools/update-workflow-version-step.tool';
import { createUpdateWorkflowVersionTriggerTool } from 'src/modules/workflow/workflow-tools/tools/update-workflow-version-trigger.tool';
@@ -39,6 +41,7 @@ export class WorkflowToolWorkspaceService {
workflowSchemaService: WorkflowSchemaWorkspaceService,
globalWorkspaceOrmManager: GlobalWorkspaceOrmManager,
recordPositionService: RecordPositionService,
logicFunctionService: LogicFunctionService,
) {
this.deps = {
workflowVersionStepService,
@@ -49,6 +52,7 @@ export class WorkflowToolWorkspaceService {
workflowSchemaService,
globalWorkspaceOrmManager,
recordPositionService,
logicFunctionService,
};
}
@@ -108,6 +112,10 @@ export class WorkflowToolWorkspaceService {
this.deps,
context,
);
const updateLogicFunctionSource = createUpdateLogicFunctionSourceTool(
this.deps,
context,
);
return {
[createCompleteWorkflow.name]: createCompleteWorkflow,
@@ -123,6 +131,7 @@ export class WorkflowToolWorkspaceService {
[deactivateWorkflowVersion.name]: deactivateWorkflowVersion,
[computeStepOutputSchema.name]: computeStepOutputSchema,
[getWorkflowCurrentVersion.name]: getWorkflowCurrentVersion,
[updateLogicFunctionSource.name]: updateLogicFunctionSource,
};
}
}
@@ -0,0 +1,85 @@
import { z } from 'zod';
import {
type WorkflowToolContext,
type WorkflowToolDependencies,
} from 'src/modules/workflow/workflow-tools/types/workflow-tool-dependencies.type';
const updateLogicFunctionSourceSchema = z.object({
logicFunctionId: z
.string()
.uuid()
.describe(
'The ID of the logic function to update (from the code step settings.input.logicFunctionId)',
),
code: z
.object({
src: z
.object({
'index.ts': z
.string()
.describe(
'The TypeScript source code for the logic function. Must export a main function.',
),
})
.describe('Source folder containing the index.ts file'),
})
.describe(
'The source code structure. Use { src: { "index.ts": "your code here" } }',
),
});
export const createUpdateLogicFunctionSourceTool = (
deps: Pick<WorkflowToolDependencies, 'logicFunctionService'>,
context: WorkflowToolContext,
) => ({
name: 'update_logic_function_source' as const,
description: `Update the TypeScript source code of a logic function used in a workflow code step.
Use this tool to modify the actual code that runs when a CODE step executes.
The code must:
- Export a 'main' function as the entry point
- Use TypeScript syntax
- Return an object with the result
- Use native APIs only (fetch, etc.) - external packages cannot be imported
Example code using fetch for HTTP requests:
\`\`\`typescript
export const main = async (params: { url: string }) => {
const response = await fetch(params.url);
const data = await response.json();
return { data };
};
\`\`\`
To find the logicFunctionId, look at the code step's settings.input.logicFunctionId field.`,
inputSchema: updateLogicFunctionSourceSchema,
execute: async (parameters: {
logicFunctionId: string;
code: { src: { 'index.ts': string } };
}) => {
try {
const { logicFunctionId, code } = parameters;
const { workspaceId } = context;
await deps.logicFunctionService.updateLogicFunctionSource({
id: logicFunctionId,
code,
workspaceId,
});
return {
success: true,
message: `Successfully updated source code for logic function ${logicFunctionId}`,
logicFunctionId,
};
} catch (error) {
return {
success: false,
error: error.message,
message: `Failed to update logic function source: ${error.message}`,
};
}
},
});
@@ -1,4 +1,5 @@
import type { RecordPositionService } from 'src/engine/core-modules/record-position/services/record-position.service';
import type { LogicFunctionService } from 'src/engine/metadata-modules/logic-function/services/logic-function.service';
import type { GlobalWorkspaceOrmManager } from 'src/engine/twenty-orm/global-workspace-datasource/global-workspace-orm.manager';
import type { WorkflowSchemaWorkspaceService } from 'src/modules/workflow/workflow-builder/workflow-schema/workflow-schema.workspace-service';
import type { WorkflowVersionEdgeWorkspaceService } from 'src/modules/workflow/workflow-builder/workflow-version-edge/workflow-version-edge.workspace-service';
@@ -16,6 +17,7 @@ export type WorkflowToolDependencies = {
workflowSchemaService: WorkflowSchemaWorkspaceService;
globalWorkspaceOrmManager: GlobalWorkspaceOrmManager;
recordPositionService: RecordPositionService;
logicFunctionService: LogicFunctionService;
};
export type WorkflowToolContext = {
@@ -2,6 +2,7 @@ import { Global, Module } from '@nestjs/common';
import { RecordPositionModule } from 'src/engine/core-modules/record-position/record-position.module';
import { WORKFLOW_TOOL_SERVICE_TOKEN } from 'src/engine/core-modules/tool-provider/constants/workflow-tool-service.token';
import { LogicFunctionModule } from 'src/engine/metadata-modules/logic-function/logic-function.module';
import { WorkflowSchemaModule } from 'src/modules/workflow/workflow-builder/workflow-schema/workflow-schema.module';
import { WorkflowVersionEdgeModule } from 'src/modules/workflow/workflow-builder/workflow-version-edge/workflow-version-edge.module';
import { WorkflowVersionStepModule } from 'src/modules/workflow/workflow-builder/workflow-version-step/workflow-version-step.module';
@@ -21,6 +22,7 @@ import { WorkflowToolWorkspaceService } from './services/workflow-tool.workspace
WorkflowTriggerModule,
WorkflowSchemaModule,
RecordPositionModule,
LogicFunctionModule,
],
providers: [
WorkflowToolWorkspaceService,