Skip to content

The open alternative to Salesforce, designed for AI.

Compile AI component previews with Workspace app dependencies (#28468)

Weiko0cc15e4489d5
diff --git a/knip.json b/knip.json
index dc494553eaf..6ba79109d86 100644
--- a/knip.json
+++ b/knip.json
@@ -29,6 +29,7 @@
"src/database/clickhouse/seeds/run-seeds.ts",
"src/instrument.ts",
"src/engine/metadata-modules/ai/ai-front-component/utils/execute-front-component-builder-request.util.ts",
+ "src/engine/metadata-modules/ai/ai-front-component/front-component-compiler.service.ts",
"lingui.config.ts",
"scripts/**/*.ts",
"**/*.spec.ts",
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-child-process-runner.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-child-process-runner.service.ts
index f0e77a7fa9d..2b75cd2fcb6 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-child-process-runner.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local/services/local-child-process-runner.service.ts
@@ -1,5 +1,5 @@
import { promises as fs } from 'fs';
-import { spawn } from 'node:child_process';
+import { type ChildProcess, spawn } from 'node:child_process';
import { join } from 'path';
import { isObject } from '@sniptt/guards';
@@ -286,26 +286,35 @@ export class LocalChildProcessRunnerService {
);
} finally {
clearTimeout(timeout);
- if (isDefined(child.pid)) {
- try {
- // Yarn and esbuild must exit before their temporary files are removed.
- process.kill(-child.pid, 'SIGKILL');
- } catch (error) {
- const code =
- isObject(error) && 'code' in error ? error.code : undefined;
-
- if (code === 'EPERM') {
- child.kill('SIGKILL');
- } else if (code !== 'ESRCH') {
- throw error;
- }
- }
- }
+ this.killChildProcess(child);
await closed;
}
return { ...result, stdout, stderr };
}
+ private killChildProcess(child: ChildProcess) {
+ if (!isDefined(child.pid)) {
+ return;
+ }
+
+ try {
+ // Yarn and esbuild must exit before their temporary files are removed.
+ process.kill(-child.pid, 'SIGKILL');
+ } catch (error) {
+ const code = isObject(error) && 'code' in error ? error.code : undefined;
+
+ if (code === 'ESRCH') {
+ return;
+ }
+
+ if (code !== 'EPERM') {
+ throw error;
+ }
+
+ child.kill('SIGKILL');
+ }
+ }
+
private getLayerDirectories(flatApplication: FlatApplication) {
return {
depsLayerDirectory: getLocalDepsLayerPath(flatApplication),
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/front-component-compiler.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/front-component-compiler.service.ts
new file mode 100644
index 00000000000..28dceb23b7a
--- /dev/null
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/front-component-compiler.service.ts
@@ -0,0 +1,124 @@
+import { Injectable } from '@nestjs/common';
+import { createHash } from 'crypto';
+import { CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
+import { InjectCacheStorage } from 'src/engine/core-modules/cache-storage/decorators/cache-storage.decorator';
+import { CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
+import { CacheStorageNamespace } from 'src/engine/core-modules/cache-storage/types/cache-storage-namespace.enum';
+import { LogicFunctionDriverType } from 'src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface';
+import { LogicFunctionDriverFactory } from 'src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory';
+import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
+import { FRONT_COMPONENT_MAX_BUILD_OUTPUT_BYTES } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-max-build-output-bytes.constant';
+import { FRONT_COMPONENT_WRAPPER_NAME } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-wrapper-name.constant';
+import { FrontComponentBuildException } from 'src/engine/metadata-modules/ai/ai-front-component/exceptions/front-component-build.exception';
+import { FrontComponentDependencyManagerService } from 'src/engine/metadata-modules/ai/ai-front-component/front-component-dependency-manager.service';
+import { type FrontComponentBuildInput } from 'src/engine/metadata-modules/ai/ai-front-component/types/front-component-build-input.type';
+import { buildFrontComponentEntrySource } from 'src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-entry-source.util';
+import { convertFrontComponentBuildFailureToException } from 'src/engine/metadata-modules/ai/ai-front-component/utils/convert-front-component-build-failure-to-exception.util';
+import { readFrontComponentSharedDependenciesChecksum } from 'src/engine/metadata-modules/ai/ai-front-component/utils/read-front-component-shared-dependencies-checksum.util';
+import { WorkspaceFrontComponentDependenciesService } from 'src/engine/metadata-modules/ai/ai-front-component/workspace-front-component-dependencies.service';
+import {
+ FrontComponentException,
+ FrontComponentExceptionCode,
+} from 'src/engine/metadata-modules/front-component/front-component.exception';
+
+@Injectable()
+export class FrontComponentCompilerService {
+ private readonly dependencyManager: FrontComponentDependencyManagerService;
+
+ constructor(
+ private readonly twentyConfigService: TwentyConfigService,
+ private readonly driverFactory: LogicFunctionDriverFactory,
+ private readonly workspaceDependencies: WorkspaceFrontComponentDependenciesService,
+ cacheLockService: CacheLockService,
+ @InjectCacheStorage(CacheStorageNamespace.ModuleApplications)
+ cacheStorage: CacheStorageService,
+ ) {
+ this.dependencyManager = new FrontComponentDependencyManagerService(
+ cacheLockService,
+ cacheStorage,
+ );
+ }
+
+ async build({
+ workspaceId,
+ input,
+ }: {
+ workspaceId: string;
+ input: FrontComponentBuildInput;
+ }) {
+ const driverType = this.twentyConfigService.get('LOGIC_FUNCTION_TYPE');
+ if (
+ driverType !== LogicFunctionDriverType.LOCAL &&
+ driverType !== LogicFunctionDriverType.LAMBDA
+ ) {
+ throw new FrontComponentException(
+ 'AI component compilation requires the LOCAL or LAMBDA logic-function driver.',
+ FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED,
+ );
+ }
+ const source = this.buildEntrySource(input);
+ const driver = this.driverFactory.getCurrentDriver();
+ const dependencies =
+ await this.workspaceDependencies.readOrThrow(workspaceId);
+ const builtDependencies =
+ await this.dependencyManager.getSharedDependenciesOrThrow({
+ workspaceId,
+ dependencies,
+ driver,
+ });
+ const built = await driver.transpile({
+ targetRuntime: 'browser',
+ workspaceId,
+ input,
+ manifest: {
+ checksum: builtDependencies.checksum,
+ exports: builtDependencies.exports,
+ },
+ });
+ if (!built.success) {
+ throw convertFrontComponentBuildFailureToException(built);
+ }
+ if (
+ createHash('sha256').update(built.builtCode).digest('hex') !==
+ built.checksum ||
+ built.sharedDependenciesChecksum !== builtDependencies.checksum ||
+ readFrontComponentSharedDependenciesChecksum(built.builtCode) !==
+ builtDependencies.checksum ||
+ Buffer.byteLength(built.builtCode) >
+ FRONT_COMPONENT_MAX_BUILD_OUTPUT_BYTES
+ ) {
+ throw new FrontComponentException(
+ 'Frontend component build checksum mismatch.',
+ FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED,
+ );
+ }
+ return {
+ source,
+ dependencyRevision: dependencies.revision,
+ componentName: FRONT_COMPONENT_WRAPPER_NAME,
+ compiledSource: built.builtCode,
+ checksum: built.checksum,
+ sharedDependencies: {
+ source: builtDependencies.builtCode,
+ checksum: builtDependencies.checksum,
+ exportNamesBySpecifier: new Map(
+ Object.entries(builtDependencies.exports),
+ ),
+ },
+ };
+ }
+
+ private buildEntrySource(input: FrontComponentBuildInput) {
+ try {
+ return buildFrontComponentEntrySource(input);
+ } catch (error) {
+ if (!(error instanceof FrontComponentBuildException)) {
+ throw error;
+ }
+ throw convertFrontComponentBuildFailureToException({
+ error: error.message,
+ code: error.code,
+ });
+ }
+ }
+}
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/front-component-dependency-manager.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/front-component-dependency-manager.service.ts
new file mode 100644
index 00000000000..ab5a0dbf521
--- /dev/null
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/front-component-dependency-manager.service.ts
@@ -0,0 +1,83 @@
+import { createHash } from 'crypto';
+import { isDefined } from 'twenty-shared/utils';
+import { type CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
+import { type CacheStorageService } from 'src/engine/core-modules/cache-storage/services/cache-storage.service';
+import {
+ BUILD_LOCK_RETRY_MS,
+ BUILD_LOCK_MAX_RETRIES,
+ YARN_INSTALL_LAMBDA_TIMEOUT_SECONDS,
+} from 'src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/constants/lambda-driver.constant';
+import { FRONT_COMPONENT_MAX_BUILD_OUTPUT_BYTES } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-max-build-output-bytes.constant';
+import { type LogicFunctionDriver } from 'src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface';
+import { frontComponentDependenciesBuildResultSchema } from 'src/engine/metadata-modules/ai/ai-front-component/schemas/front-component-dependencies-build-result.schema';
+import { convertFrontComponentBuildFailureToException } from 'src/engine/metadata-modules/ai/ai-front-component/utils/convert-front-component-build-failure-to-exception.util';
+import { type WorkspaceFrontComponentDependenciesService } from 'src/engine/metadata-modules/ai/ai-front-component/workspace-front-component-dependencies.service';
+import {
+ FrontComponentException,
+ FrontComponentExceptionCode,
+} from 'src/engine/metadata-modules/front-component/front-component.exception';
+
+export class FrontComponentDependencyManagerService {
+ constructor(
+ private readonly cacheLockService: CacheLockService,
+ private readonly cacheStorage: CacheStorageService,
+ ) {}
+
+ async getSharedDependenciesOrThrow({
+ workspaceId,
+ dependencies,
+ driver,
+ }: {
+ workspaceId: string;
+ dependencies: Pick<
+ Awaited<
+ ReturnType<WorkspaceFrontComponentDependenciesService['readOrThrow']>
+ >,
+ 'revision' | 'packageJson' | 'yarnLock'
+ >;
+ driver: LogicFunctionDriver;
+ }) {
+ const builderKey = await driver.getDependenciesBuildCacheKey({
+ workspaceId,
+ });
+ const key = `ai-front-component-dependencies:${builderKey}:${dependencies.revision}`;
+ return this.cacheLockService.withLock(
+ async () => {
+ const cached = await this.cacheStorage.get<unknown>(key);
+ const result = frontComponentDependenciesBuildResultSchema.parse(
+ cached ??
+ (await driver.buildDependencies({
+ targetRuntime: 'browser',
+ workspaceId,
+ packageJson: dependencies.packageJson,
+ yarnLock: dependencies.yarnLock,
+ })),
+ );
+ if (!result.success) {
+ throw convertFrontComponentBuildFailureToException(result);
+ }
+ if (
+ createHash('sha256').update(result.builtCode).digest('hex') !==
+ result.checksum ||
+ Buffer.byteLength(result.builtCode) >
+ FRONT_COMPONENT_MAX_BUILD_OUTPUT_BYTES
+ ) {
+ throw new FrontComponentException(
+ 'Shared frontend component dependencies checksum mismatch.',
+ FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED,
+ );
+ }
+ if (!isDefined(cached)) {
+ await this.cacheStorage.set(key, result, 30 * 24 * 60 * 60 * 1000);
+ }
+ return result;
+ },
+ key,
+ {
+ ttl: (YARN_INSTALL_LAMBDA_TIMEOUT_SECONDS + 60) * 1000,
+ ms: BUILD_LOCK_RETRY_MS,
+ maxRetries: BUILD_LOCK_MAX_RETRIES,
+ },
+ );
+ }
+}
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/convert-front-component-build-failure-to-exception.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/convert-front-component-build-failure-to-exception.util.spec.ts
new file mode 100644
index 00000000000..c861bd6519e
--- /dev/null
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/convert-front-component-build-failure-to-exception.util.spec.ts
@@ -0,0 +1,115 @@
+import { setupI18n } from '@lingui/core';
+
+import { frontComponentBuildFailureSchema } from 'src/engine/metadata-modules/ai/ai-front-component/schemas/front-component-build-failure.schema';
+import { convertFrontComponentBuildFailureToException } from 'src/engine/metadata-modules/ai/ai-front-component/utils/convert-front-component-build-failure-to-exception.util';
+import { FrontComponentExceptionCode } from 'src/engine/metadata-modules/front-component/front-component.exception';
+
+jest.mock(
+ 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-build-timeout-ms.constant',
+ () => ({ FRONT_COMPONENT_BUILD_TIMEOUT_MS: 17000 }),
+);
+jest.mock(
+ 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-max-build-output-bytes.constant',
+ () => ({ FRONT_COMPONENT_MAX_BUILD_OUTPUT_BYTES: 9 * 1024 * 1024 }),
+);
+
+describe('convertFrontComponentBuildFailureToException', () => {
+ it.each([
+ [
+ 'INVALID_INPUT',
+ 'Invalid front component source.',
+ 'Source invalide.',
+ false,
+ ],
+ [
+ 'INVALID_SETTINGS',
+ 'Invalid front component settings.',
+ 'Paramètres invalides.',
+ false,
+ ],
+ [
+ 'BUILD_FAILED',
+ 'Failed to build front component.',
+ 'Compilation impossible.',
+ true,
+ ],
+ [
+ 'BUILD_TIMEOUT',
+ 'Component build exceeded the {timeoutSeconds} second time limit.',
+ 'Délai de compilation dépassé.',
+ true,
+ ],
+ [
+ 'OUTPUT_TOO_LARGE',
+ 'Component build exceeds the {maximumOutputMegabytes} MB output limit.',
+ 'Composant trop volumineux.',
+ false,
+ ],
+ ] as const)(
+ 'localizes %s on the server while retaining its diagnostic',
+ (code, message, translation, isBuildFailure) => {
+ const failure = frontComponentBuildFailureSchema.parse(
+ JSON.parse(
+ JSON.stringify({
+ success: false,
+ code,
+ error: 'Internal compiler diagnostic',
+ }),
+ ),
+ );
+ const exception = convertFrontComponentBuildFailureToException(failure);
+ const i18n = setupI18n({
+ locale: 'fr',
+ messages: { fr: { [exception.userFriendlyMessage.id]: translation } },
+ });
+
+ expect(exception.message).toBe('Internal compiler diagnostic');
+ expect(exception.code).toBe(
+ isBuildFailure
+ ? FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED
+ : FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT,
+ );
+ expect(exception.userFriendlyMessage.message).toBe(message);
+ expect(i18n._(exception.userFriendlyMessage)).toBe(translation);
+ },
+ );
+
+ it.each([
+ ['BUILD_TIMEOUT', 'timeoutSeconds', 17],
+ ['OUTPUT_TOO_LARGE', 'maximumOutputMegabytes', 9],
+ ] as const)(
+ 'interpolates the configured limit for %s',
+ (code, placeholder, expectedLimit) => {
+ const exception = convertFrontComponentBuildFailureToException({
+ code,
+ error: 'Build limit exceeded',
+ });
+ const i18n = setupI18n({
+ locale: 'fr',
+ messages: {
+ fr: {
+ [exception.userFriendlyMessage.id]: ['Limite : ', [placeholder]],
+ },
+ },
+ });
+
+ expect(i18n._(exception.userFriendlyMessage)).toBe(
+ `Limite : ${expectedLimit}`,
+ );
+ },
+ );
+
+ it('keeps the default user-facing message for unclassified esbuild diagnostics', () => {
+ const exception = convertFrontComponentBuildFailureToException({
+ error: 'Build failed with 1 error: unexpected closing tag',
+ });
+
+ expect(exception.code).toBe(
+ FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT,
+ );
+ expect(exception.userFriendlyMessage.message).toBe(
+ 'Invalid front component input.',
+ );
+ expect(exception.message).toContain('unexpected closing tag');
+ });
+});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/convert-front-component-build-failure-to-exception.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/convert-front-component-build-failure-to-exception.util.ts
new file mode 100644
index 00000000000..5b4d2fe8109
--- /dev/null
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/convert-front-component-build-failure-to-exception.util.ts
@@ -0,0 +1,50 @@
+import { msg } from '@lingui/core/macro';
+
+import { FRONT_COMPONENT_BUILD_TIMEOUT_MS } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-build-timeout-ms.constant';
+import { FRONT_COMPONENT_MAX_BUILD_OUTPUT_BYTES } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-max-build-output-bytes.constant';
+import { type FrontComponentBuildException } from 'src/engine/metadata-modules/ai/ai-front-component/exceptions/front-component-build.exception';
+import {
+ FrontComponentException,
+ FrontComponentExceptionCode,
+} from 'src/engine/metadata-modules/front-component/front-component.exception';
+
+export const convertFrontComponentBuildFailureToException = ({
+ error,
+ code,
+}: {
+ error: string;
+ code?: FrontComponentBuildException['code'];
+}) =>
+ new FrontComponentException(
+ error,
+ code === 'BUILD_FAILED' || code === 'BUILD_TIMEOUT'
+ ? FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED
+ : FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT,
+ { userFriendlyMessage: getUserFriendlyMessage(code) },
+ );
+
+const getUserFriendlyMessage = (
+ code?: FrontComponentBuildException['code'],
+) => {
+ switch (code) {
+ case 'INVALID_INPUT':
+ return msg`Invalid front component source.`;
+ case 'INVALID_SETTINGS':
+ return msg`Invalid front component settings.`;
+ case 'BUILD_FAILED':
+ return msg`Failed to build front component.`;
+ case 'BUILD_TIMEOUT': {
+ const timeoutSeconds = FRONT_COMPONENT_BUILD_TIMEOUT_MS / 1000;
+
+ return msg`Component build exceeded the ${timeoutSeconds} second time limit.`;
+ }
+ case 'OUTPUT_TOO_LARGE': {
+ const maximumOutputMegabytes =
+ FRONT_COMPONENT_MAX_BUILD_OUTPUT_BYTES / (1024 * 1024);
+
+ return msg`Component build exceeds the ${maximumOutputMegabytes} MB output limit.`;
+ }
+ default:
+ return undefined;
+ }
+};
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/workspace-front-component-dependencies.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/workspace-front-component-dependencies.service.ts
new file mode 100644
index 00000000000..6a80bde9563
--- /dev/null
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/workspace-front-component-dependencies.service.ts
@@ -0,0 +1,75 @@
+import { Injectable } from '@nestjs/common';
+import { z } from 'zod';
+import { computeDependencyFileChecksum } from 'twenty-shared/application';
+import { isDefined } from 'twenty-shared/utils';
+import { ApplicationDependencyFilesService } from 'src/engine/core-modules/application/application-package/application-dependency-files.service';
+import { ApplicationService } from 'src/engine/core-modules/application/application.service';
+import {
+ FrontComponentException,
+ FrontComponentExceptionCode,
+} from 'src/engine/metadata-modules/front-component/front-component.exception';
+
+const packageSchema = z.object({
+ dependencies: z.record(z.string(), z.string()).optional(),
+ devDependencies: z.record(z.string(), z.string()).optional(),
+});
+
+@Injectable()
+export class WorkspaceFrontComponentDependenciesService {
+ constructor(
+ private readonly applicationService: ApplicationService,
+ private readonly applicationDependencyFilesService: ApplicationDependencyFilesService,
+ ) {}
+
+ async readOrThrow(workspaceId: string) {
+ const { workspaceCustomFlatApplication: application } =
+ await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
+ { workspaceId },
+ );
+ const {
+ packageJsonContent: packageJson,
+ yarnLockContent: yarnLock,
+ storedFilesChecksums,
+ } = await this.applicationDependencyFilesService.getVerifiedDependencyContentsOrThrow(
+ {
+ workspaceId,
+ applicationUniversalIdentifier: application.universalIdentifier,
+ declaredChecksums: {
+ packageJsonChecksum: application.packageJsonChecksum,
+ yarnLockChecksum: application.yarnLockChecksum,
+ },
+ },
+ );
+
+ if (!isDefined(yarnLock)) {
+ throw new FrontComponentException(
+ 'Add yarn.lock to the Workspace app before creating a component.',
+ FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT,
+ );
+ }
+ const { dependencies, devDependencies } = packageSchema.parse(
+ JSON.parse(packageJson),
+ );
+ const missingPackages = ['react', 'react-dom', 'twenty-ui'].filter(
+ (name) => !isDefined(dependencies?.[name] ?? devDependencies?.[name]),
+ );
+ if (missingPackages.length > 0) {
+ throw new FrontComponentException(
+ `Add ${missingPackages.join(', ')} to the Workspace app package.json and yarn.lock before creating a component.`,
+ FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT,
+ );
+ }
+ return {
+ applicationId: application.id,
+ packageJson,
+ yarnLock,
+ revision: computeDependencyFileChecksum(
+ JSON.stringify([
+ application.id,
+ storedFilesChecksums.packageJsonChecksum,
+ storedFilesChecksums.yarnLockChecksum,
+ ]),
+ ),
+ };
+ }
+}