Add pattern for variable tag in tiptap (#16652)

Since we now store rich text value in blocknote rather than markdown,
variables need to be resolved accordingly.

Replacing the variable tag pattern
`{"type":"variableTag","attrs":\{"variable":"(\{\{[^{}]+\}\})"\}\}` by a
blocknote text `{"type":"text","text":"${escapedText}"}`

Fixes https://github.com/twentyhq/twenty/issues/16583

To test :
- build a workflow that creates a note/ sends an email with a variable
in the body
- make sure the result is properly formatted once run

---------

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
Thomas Trompette
2025-12-18 17:24:13 +01:00
committed by GitHub
parent 636cec0f59
commit b2d2babbb9
11 changed files with 429 additions and 55 deletions
@@ -0,0 +1,40 @@
import { isString } from 'class-validator';
import { FieldMetadataType } from 'twenty-shared/types';
import { isDefined, resolveRichTextVariables } from 'twenty-shared/utils';
import { type ObjectMetadataInfo } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
export const resolveRichTextFieldsInRecord = (
objectRecord: Record<string, unknown>,
objectMetadataInfo: ObjectMetadataInfo,
context: Record<string, unknown>,
): Record<string, unknown> => {
const { flatObjectMetadata, flatFieldMetadataMaps } = objectMetadataInfo;
const richTextFieldNames = flatObjectMetadata.fieldMetadataIds
.map((fieldId) => flatFieldMetadataMaps.byId[fieldId])
.filter((field) => field?.type === FieldMetadataType.RICH_TEXT_V2)
.map((field) => field?.name)
.filter(isDefined);
const resolvedRecord = { ...objectRecord };
for (const fieldName of richTextFieldNames) {
const fieldValue = resolvedRecord[fieldName];
if (
isDefined(fieldValue) &&
'blocknote' in fieldValue &&
isString(fieldValue.blocknote)
) {
const richTextValue = fieldValue as { blocknote: string };
resolvedRecord[fieldName] = {
...richTextValue,
blocknote: resolveRichTextVariables(richTextValue.blocknote, context),
};
}
}
return resolvedRecord;
};
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { resolveInput } from 'twenty-shared/utils';
import { type ActorMetadata, FieldActorSource } from 'twenty-shared/types';
import { resolveInput } from 'twenty-shared/utils';
import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/interfaces/workflow-action.interface';
@@ -10,11 +10,13 @@ import {
RecordCrudExceptionCode,
} from 'src/engine/core-modules/record-crud/exceptions/record-crud.exception';
import { CreateRecordService } from 'src/engine/core-modules/record-crud/services/create-record.service';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
import { WorkflowExecutionContextService } from 'src/modules/workflow/workflow-executor/services/workflow-execution-context.service';
import { type WorkflowActionInput } from 'src/modules/workflow/workflow-executor/types/workflow-action-input';
import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';
import { type WorkflowExecutionContext } from 'src/modules/workflow/workflow-executor/types/workflow-execution-context.type';
import { findStepOrThrow } from 'src/modules/workflow/workflow-executor/utils/find-step-or-throw.util';
import { resolveRichTextFieldsInRecord } from 'src/modules/workflow/workflow-executor/utils/resolve-rich-text-fields-in-record.util';
import { type WorkflowCreateRecordActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/record-crud/types/workflow-record-crud-action-input.type';
@Injectable()
@@ -22,6 +24,7 @@ export class CreateRecordWorkflowAction implements WorkflowAction {
constructor(
private readonly createRecordService: CreateRecordService,
private readonly workflowExecutionContextService: WorkflowExecutionContextService,
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,
) {}
async execute({
@@ -37,8 +40,25 @@ export class CreateRecordWorkflowAction implements WorkflowAction {
const { workspaceId } = runInfo;
const rawInput = step.settings.input as WorkflowCreateRecordActionInput;
const objectMetadataInfo =
await this.workflowCommonWorkspaceService.getObjectMetadataInfo(
rawInput.objectName,
workspaceId,
);
const inputWithResolvedRichText = {
...rawInput,
objectRecord: resolveRichTextFieldsInRecord(
rawInput.objectRecord,
objectMetadataInfo,
context,
),
};
const workflowActionInput = resolveInput(
step.settings.input,
inputWithResolvedRichText,
context,
) as WorkflowCreateRecordActionInput;
@@ -9,6 +9,7 @@ import {
RecordCrudExceptionCode,
} from 'src/engine/core-modules/record-crud/exceptions/record-crud.exception';
import { UpdateRecordService } from 'src/engine/core-modules/record-crud/services/update-record.service';
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service';
import {
WorkflowStepExecutorException,
WorkflowStepExecutorExceptionCode,
@@ -17,6 +18,7 @@ import { WorkflowExecutionContextService } from 'src/modules/workflow/workflow-e
import { type WorkflowActionInput } from 'src/modules/workflow/workflow-executor/types/workflow-action-input';
import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';
import { findStepOrThrow } from 'src/modules/workflow/workflow-executor/utils/find-step-or-throw.util';
import { resolveRichTextFieldsInRecord } from 'src/modules/workflow/workflow-executor/utils/resolve-rich-text-fields-in-record.util';
import { isWorkflowUpdateRecordAction } from 'src/modules/workflow/workflow-executor/workflow-actions/record-crud/guards/is-workflow-update-record-action.guard';
import { type WorkflowUpdateRecordActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/record-crud/types/workflow-record-crud-action-input.type';
@@ -25,6 +27,7 @@ export class UpdateRecordWorkflowAction implements WorkflowAction {
constructor(
private readonly updateRecordService: UpdateRecordService,
private readonly workflowExecutionContextService: WorkflowExecutionContextService,
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService,
) {}
async execute({
@@ -45,8 +48,27 @@ export class UpdateRecordWorkflowAction implements WorkflowAction {
);
}
const { workspaceId } = runInfo;
const rawInput = step.settings.input as WorkflowUpdateRecordActionInput;
const objectMetadataInfo =
await this.workflowCommonWorkspaceService.getObjectMetadataInfo(
rawInput.objectName,
workspaceId,
);
const inputWithResolvedRichText = {
...rawInput,
objectRecord: resolveRichTextFieldsInRecord(
rawInput.objectRecord,
objectMetadataInfo,
context,
),
};
const workflowActionInput = resolveInput(
step.settings.input,
inputWithResolvedRichText,
context,
) as WorkflowUpdateRecordActionInput;
@@ -61,8 +83,6 @@ export class UpdateRecordWorkflowAction implements WorkflowAction {
);
}
const { workspaceId } = runInfo;
const executionContext =
await this.workflowExecutionContextService.getExecutionContext(runInfo);
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { resolveInput } from 'twenty-shared/utils';
import { resolveInput, resolveRichTextVariables } from 'twenty-shared/utils';
import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/interfaces/workflow-action.interface';
@@ -10,6 +10,7 @@ import { type ToolInput } from 'src/engine/core-modules/tool/types/tool-input.ty
import { type Tool } from 'src/engine/core-modules/tool/types/tool.type';
import { type WorkflowActionInput } from 'src/modules/workflow/workflow-executor/types/workflow-action-input';
import { type WorkflowActionOutput } from 'src/modules/workflow/workflow-executor/types/workflow-action-output.type';
import { type WorkflowSendEmailActionInput } from 'src/modules/workflow/workflow-executor/workflow-actions/mail-sender/types/workflow-send-email-action-input.type';
import { WorkflowActionType } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
@Injectable()
@@ -44,7 +45,23 @@ export class ToolExecutorWorkflowAction implements WorkflowAction {
throw new Error(`No tool found for workflow action type: ${step.type}`);
}
const toolInput = resolveInput(step.settings.input, context) as ToolInput;
let toolInput = step.settings.input;
if (step.type === WorkflowActionType.SEND_EMAIL) {
const sendEmailInput = toolInput as WorkflowSendEmailActionInput;
if (sendEmailInput.body) {
toolInput = {
...sendEmailInput,
body: resolveRichTextVariables(
sendEmailInput.body,
context,
),
};
}
}
toolInput = resolveInput(toolInput, context) as ToolInput;
const toolOutput = await tool.execute(toolInput, {
workspaceId: runInfo.workspaceId,