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
@@ -2,7 +2,6 @@ import { Module } from '@nestjs/common';
import { WorkspaceManyOrAllFlatEntityMapsCacheModule } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.module';
import { LogicFunctionModule } from 'src/engine/metadata-modules/logic-function/logic-function.module';
import { CodeStepBuildModule } from 'src/modules/workflow/workflow-builder/workflow-version-step/code-step/code-step-build.module';
import { WorkflowQueryHookModule } from 'src/modules/workflow/common/query-hooks/workflow-query-hook.module';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
@@ -11,7 +10,6 @@ import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/work
WorkflowQueryHookModule,
LogicFunctionModule,
WorkspaceManyOrAllFlatEntityMapsCacheModule,
CodeStepBuildModule,
],
providers: [WorkflowCommonWorkspaceService],
exports: [WorkflowCommonWorkspaceService],
@@ -6,13 +6,13 @@ import { AiAgentRoleModule } from 'src/engine/metadata-modules/ai/ai-agent-role/
import { AgentEntity } from 'src/engine/metadata-modules/ai/ai-agent/entities/agent.entity';
import { WorkspaceManyOrAllFlatEntityMapsCacheModule } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.module';
import { LogicFunctionModule } from 'src/engine/metadata-modules/logic-function/logic-function.module';
import { CodeStepBuildModule } from 'src/modules/workflow/workflow-builder/workflow-version-step/code-step/code-step-build.module';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { RoleTargetEntity } from 'src/engine/metadata-modules/role-target/role-target.entity';
import { RoleEntity } from 'src/engine/metadata-modules/role/role.entity';
import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache.module';
import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';
import { WorkflowSchemaModule } from 'src/modules/workflow/workflow-builder/workflow-schema/workflow-schema.module';
import { CodeStepBuildModule } from 'src/modules/workflow/workflow-builder/workflow-version-step/code-step/code-step-build.module';
import { WorkflowVersionStepCreationWorkspaceService } from 'src/modules/workflow/workflow-builder/workflow-version-step/workflow-version-step-creation.workspace-service';
import { WorkflowVersionStepDeletionWorkspaceService } from 'src/modules/workflow/workflow-builder/workflow-version-step/workflow-version-step-deletion.workspace-service';
import { WorkflowVersionStepHelpersWorkspaceService } from 'src/modules/workflow/workflow-builder/workflow-version-step/workflow-version-step-helpers.workspace-service';
@@ -48,7 +48,6 @@ import { WorkflowVersionStepWorkspaceService } from 'src/modules/workflow/workfl
WorkflowVersionStepWorkspaceService,
WorkflowVersionStepOperationsWorkspaceService,
WorkflowVersionStepHelpersWorkspaceService,
CodeStepBuildModule,
],
})
export class WorkflowVersionStepModule {}
@@ -3,6 +3,7 @@ import { Module } from '@nestjs/common';
import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
import { MetricsModule } from 'src/engine/core-modules/metrics/metrics.module';
import { WorkflowCommonModule } from 'src/modules/workflow/common/workflow-common.module';
import { CodeStepBuildModule } from 'src/modules/workflow/workflow-builder/workflow-version-step/code-step/code-step-build.module';
import { WorkflowVersionStepModule } from 'src/modules/workflow/workflow-builder/workflow-version-step/workflow-version-step.module';
import { WorkflowExecutorModule } from 'src/modules/workflow/workflow-executor/workflow-executor.module';
import { RunWorkflowJob } from 'src/modules/workflow/workflow-runner/jobs/run-workflow.job';
@@ -19,6 +20,7 @@ import { WorkflowRunnerWorkspaceService } from 'src/modules/workflow/workflow-ru
MetricsModule,
WorkflowRunQueueModule,
WorkflowVersionStepModule,
CodeStepBuildModule,
],
providers: [WorkflowRunnerWorkspaceService, RunWorkflowJob],
exports: [WorkflowRunnerWorkspaceService],
@@ -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,