Skip to content

The open alternative to Salesforce, designed for AI.

Reuse current app dependencies for AI component compilation (#29330)

Weikobd30bb094530
diff --git a/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/__tests__/build-shared-dependencies-bundle.spec.ts b/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/__tests__/build-shared-dependencies-bundle.spec.ts
index 6394d025bae..58e580ab8da 100644
--- a/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/__tests__/build-shared-dependencies-bundle.spec.ts
+++ b/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/__tests__/build-shared-dependencies-bundle.spec.ts
@@ -1,6 +1,6 @@
import { it, expect, vi } from 'vitest';
import { createHash } from 'crypto';
-import { mkdtemp, mkdir, readFile, rm, writeFile } from 'fs/promises';
+import { mkdtemp, mkdir, readFile, rm, symlink, writeFile } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
import { build, transform } from 'esbuild';
@@ -12,10 +12,13 @@ import {
FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER,
} from 'twenty-shared/application';
import {
+ createSharedDependenciesShimPlugin,
getSharedDependenciesShimSource,
type SharedDependenciesBuildManifest,
} from 'twenty-shared/front-component-build';
+import { MINIMAL_APP_PATH } from '@/app/__tests__/utils/fixture-paths';
+
import { buildSharedDependenciesBundle } from '@/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle';
it('ships the export map needed to compile another component against the uploaded app bundle', async () => {
@@ -109,3 +112,87 @@ it('ships the export map needed to compile another component against the uploade
await rm(appPath, { recursive: true, force: true });
}
});
+
+it('keeps React DOM and Twenty UI available when rebuilding a workspace app with a sparse shared dependency declaration', async () => {
+ const appPath = await mkdtemp(
+ join(tmpdir(), 'workspace-shared-dependencies-'),
+ );
+ try {
+ await symlink(
+ join(MINIMAL_APP_PATH, '../../../../node_modules'),
+ join(appPath, 'node_modules'),
+ );
+ await writeFile(
+ join(appPath, 'package.json'),
+ JSON.stringify({
+ dependencies: { react: '19.2.0' },
+ devDependencies: { 'react-dom': '19.2.0', 'twenty-ui': '2.50.0' },
+ frontComponentSharedDependencies: ['react'],
+ }),
+ );
+ const context = await buildSharedDependenciesBundle({
+ appPath,
+ outputDir: 'output',
+ sharedDependencies: {
+ dependencies: ['react'],
+ builtPath: 'shared.mjs',
+ builtChecksum: null,
+ },
+ onFileBuilt: vi.fn(),
+ });
+ const manifest: SharedDependenciesBuildManifest = JSON.parse(
+ await readFile(join(appPath, 'output/shared.mjs.json'), 'utf8'),
+ );
+ expect(manifest.exports['react-dom'].namedExports).toContain(
+ 'createPortal',
+ );
+ expect(manifest.exports['react-dom/client'].namedExports).toContain(
+ 'createRoot',
+ );
+ expect(
+ manifest.exports['twenty-ui/primitives/input'].namedExports,
+ ).toContain('Button');
+ expect(manifest.exports['react/jsx-runtime'].namedExports).toContain('jsx');
+ const component = await build({
+ stdin: {
+ contents: `export { createPortal } from 'react-dom';
+export { createRoot } from 'react-dom/client';
+export { Button } from 'twenty-ui/primitives/input';`,
+ },
+ bundle: true,
+ write: false,
+ format: 'esm',
+ external: [FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER],
+ plugins: [
+ createSharedDependenciesShimPlugin({
+ getSharedDependenciesBuildContext: () => context,
+ }),
+ ],
+ });
+ const linked = await build({
+ stdin: {
+ contents: component.outputFiles[0].text,
+ resolveDir: appPath,
+ },
+ alias: {
+ [FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER]: join(
+ appPath,
+ 'output/shared.mjs',
+ ),
+ },
+ bundle: true,
+ write: false,
+ format: 'cjs',
+ });
+ const module: { exports: Record<string, unknown> } = { exports: {} };
+ runInNewContext(linked.outputFiles[0].text, {
+ module,
+ exports: module.exports,
+ });
+ expect(module.exports.createPortal).toBeTypeOf('function');
+ expect(module.exports.createRoot).toBeTypeOf('function');
+ expect(module.exports.Button).toBeDefined();
+ } finally {
+ await rm(appPath, { recursive: true, force: true });
+ }
+}, 30_000);
diff --git a/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle.ts b/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle.ts
index a39d5e8ffcd..df4347c437a 100644
--- a/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle.ts
+++ b/packages/twenty-cli/src/app/bundles/front-component-build/shared-dependencies-build/build-shared-dependencies-bundle.ts
@@ -1,15 +1,17 @@
+import { type PackageJson } from 'type-fest';
import { getWatchInputPlugins } from '@/app/dev/collect-watch-inputs';
import { getBaseFrontComponentBuildOptions } from '@/app/bundles/front-component-build/utils/get-base-front-component-build-options';
import { type OnFileBuiltCallback } from '@/app/bundles/types/on-file-built-callback.type';
import {
getSharedDependenciesEntrySource,
+ normalizeFrontComponentSharedDependencies,
getSharedDependenciesBuildManifestPath,
type SharedDependenciesBuildContext,
type SharedDependenciesBuildManifest,
} from 'twenty-shared/front-component-build';
import { enumerateSharedDependenciesExportNames } from '@/app/bundles/front-component-build/shared-dependencies-build/utils/enumerate-shared-dependencies-export-names';
import { getSharedDependenciesNamespaceCollisions } from '@/app/bundles/front-component-build/shared-dependencies-build/utils/get-shared-dependencies-namespace-collisions';
-import { ensureDir, pathExists } from '@/app/fs-utils';
+import { ensureDir, pathExists, readJson } from '@/app/fs-utils';
import crypto from 'crypto';
import * as esbuild from 'esbuild';
import { readFile, writeFile } from 'node:fs/promises';
@@ -34,9 +36,18 @@ export const buildSharedDependenciesBundle = async ({
sharedDependencies: FrontComponentSharedDependenciesManifest;
onFileBuilt: OnFileBuiltCallback;
}): Promise<SharedDependenciesBuildContext> => {
- const namespaceCollisions = getSharedDependenciesNamespaceCollisions(
- sharedDependencies.dependencies,
+ const packageJson = await readJson<PackageJson>(
+ join(appPath, 'package.json'),
);
+ const dependencies = normalizeFrontComponentSharedDependencies({
+ dependencies: sharedDependencies.dependencies,
+ packageNames: Object.keys({
+ ...packageJson.dependencies,
+ ...packageJson.devDependencies,
+ }),
+ });
+ const namespaceCollisions =
+ getSharedDependenciesNamespaceCollisions(dependencies);
if (isNonEmptyArray(namespaceCollisions)) {
throw new Error(
@@ -48,7 +59,7 @@ export const buildSharedDependenciesBundle = async ({
const exportNamesBySpecifier = new Map(
await Promise.all(
- sharedDependencies.dependencies.map(
+ dependencies.map(
async (specifier) =>
[
specifier,
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-sync.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-sync.service.ts
index 8fd8f121b14..797f1cfbbe0 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-sync.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/application-sync.service.ts
@@ -35,14 +35,10 @@ import { type LogicFunctionDriverFactory } from 'src/engine/core-modules/logic-f
import { findReservedVariableNamesInApplicationManifest } from 'src/engine/core-modules/application/utils/find-reserved-variable-names-in-application-manifest.util';
import { type AllFlatEntityOperationRecordByMetadataName } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-operation-record-by-metadata-name.type';
import { getMetadataFlatEntityMapsKey } from 'src/engine/metadata-modules/flat-entity/utils/get-metadata-flat-entity-maps-key.util';
-import { FrontComponentEntity } from 'src/engine/metadata-modules/front-component/entities/front-component.entity';
-import { WorkspaceEventBroadcaster } from 'src/engine/subscriptions/workspace-event-broadcaster/workspace-event-broadcaster.service';
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
import { WorkspaceMigrationBuilderException } from 'src/engine/workspace-manager/workspace-migration/exceptions/workspace-migration-builder-exception';
import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service';
import { WorkspaceMigration } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/types/workspace-migration.type';
-import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';
-import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';
@Injectable()
export class ApplicationSyncService {
@@ -59,9 +55,6 @@ export class ApplicationSyncService {
@Inject(LOGIC_FUNCTION_DRIVER_FACTORY_TOKEN)
private readonly logicFunctionDriverFactory: LogicFunctionDriverFactory,
private readonly applicationUninstallService: ApplicationUninstallService,
- @InjectWorkspaceScopedRepository(FrontComponentEntity)
- private readonly frontComponentRepository: WorkspaceScopedRepository<FrontComponentEntity>,
- private readonly workspaceEventBroadcaster: WorkspaceEventBroadcaster,
) {}
public async synchronizeFromManifest({
@@ -317,54 +310,18 @@ export class ApplicationSyncService {
application.frontComponentSharedDependenciesChecksum !==
frontComponentSharedDependenciesChecksum
) {
- await this.broadcastFrontComponentSharedDependenciesChecksumUpdates({
- workspaceId,
- applicationId: application.id,
- frontComponentSharedDependenciesChecksum,
- });
+ await this.applicationService.broadcastFrontComponentSharedDependenciesChecksumUpdates(
+ {
+ workspaceId,
+ applicationId: application.id,
+ frontComponentSharedDependenciesChecksum,
+ },
+ );
}
return updatedApplication;
}
- private async broadcastFrontComponentSharedDependenciesChecksumUpdates({
- workspaceId,
- applicationId,
- frontComponentSharedDependenciesChecksum,
- }: {
- workspaceId: string;
- applicationId: string;
- frontComponentSharedDependenciesChecksum: string | null;
- }): Promise<void> {
- try {
- const frontComponents = await this.frontComponentRepository.find(
- workspaceId,
- { select: ['id'], where: { applicationId } },
- );
-
- await this.workspaceEventBroadcaster.broadcast({
- workspaceId,
- events: frontComponents.map((frontComponent) => ({
- type: 'updated',
- entityName: 'frontComponent',
- recordId: frontComponent.id,
- properties: {
- updatedFields: ['frontComponentSharedDependenciesChecksum'],
- after: {
- id: frontComponent.id,
- frontComponentSharedDependenciesChecksum,
- },
- },
- })),
- });
- } catch (error) {
- this.logger.warn(
- `Failed to broadcast the shared dependencies checksum update for application ${applicationId} in workspace ${workspaceId}`,
- error,
- );
- }
- }
-
public async uninstallApplication({
workspaceId,
applicationUniversalIdentifier,
diff --git a/packages/twenty-server/src/engine/core-modules/application/application.service.ts b/packages/twenty-server/src/engine/core-modules/application/application.service.ts
index 4c351c32480..b24ce348725 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application.service.ts
@@ -805,6 +805,44 @@ export class ApplicationService {
return savedApplication;
}
+ async broadcastFrontComponentSharedDependenciesChecksumUpdates({
+ workspaceId,
+ applicationId,
+ frontComponentSharedDependenciesChecksum,
+ }: {
+ workspaceId: string;
+ applicationId: string;
+ frontComponentSharedDependenciesChecksum: string | null;
+ }): Promise<void> {
+ try {
+ const frontComponents = await this.frontComponentRepository.find(
+ workspaceId,
+ { select: ['id'], where: { applicationId } },
+ );
+
+ await this.workspaceEventBroadcaster.broadcast({
+ workspaceId,
+ events: frontComponents.map((frontComponent) => ({
+ type: 'updated',
+ entityName: 'frontComponent',
+ recordId: frontComponent.id,
+ properties: {
+ updatedFields: ['frontComponentSharedDependenciesChecksum'],
+ after: {
+ id: frontComponent.id,
+ frontComponentSharedDependenciesChecksum,
+ },
+ },
+ })),
+ });
+ } catch (error) {
+ this.logger.warn(
+ `Failed to broadcast the shared dependencies checksum update for application ${applicationId} in workspace ${workspaceId}`,
+ error,
+ );
+ }
+ }
+
async update(
id: string,
data: QueryDeepPartialEntity<ApplicationEntity> & {
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver.spec.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver.spec.ts
index 25266437a89..af31ceec135 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver.spec.ts
@@ -2,13 +2,12 @@ import { DisabledDriver } from 'src/engine/core-modules/logic-function/logic-fun
import { LogicFunctionExceptionCode } from 'src/engine/metadata-modules/logic-function/logic-function.exception';
describe('DisabledDriver', () => {
- it.each([
- 'transpile',
- 'buildDependencies',
- 'getDependenciesBuildCacheKey',
- ] as const)('rejects %s when builds are disabled', async (operation) => {
- await expect(new DisabledDriver()[operation]()).rejects.toMatchObject({
- code: LogicFunctionExceptionCode.LOGIC_FUNCTION_DISABLED,
- });
- });
+ it.each(['transpile', 'buildDependencies'] as const)(
+ 'rejects %s when builds are disabled',
+ async (operation) => {
+ await expect(new DisabledDriver()[operation]()).rejects.toMatchObject({
+ code: LogicFunctionExceptionCode.LOGIC_FUNCTION_DISABLED,
+ });
+ },
+ );
});
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver.ts
index f46827f7e53..1b30b6610e9 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/disabled.driver.ts
@@ -37,10 +37,6 @@ export class DisabledDriver implements LogicFunctionDriver {
);
}
- async getDependenciesBuildCacheKey(): Promise<never> {
- return this.buildDependencies();
- }
-
async installPrebuiltBundle(): Promise<void> {
throw new LogicFunctionException(
'Logic function prebuilt install is disabled. Set LOGIC_FUNCTION_TYPE to LOCAL or LAMBDA to enable.',
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
index fa5fc34a38a..1516cb16900 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda.driver.ts
@@ -103,12 +103,6 @@ export class LambdaDriver implements LogicFunctionDriver {
return this.toolFunctions.buildDependencies(params);
}
- async getDependenciesBuildCacheKey(
- params: Pick<BrowserDependenciesBuildParams, 'workspaceId'>,
- ) {
- return this.toolFunctions.getDependenciesBuildCacheKey(params);
- }
-
async delete(flatLogicFunction: FlatLogicFunction): Promise<void> {
await this.executorManager.delete(flatLogicFunction);
}
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service.ts
index dcbbeb17dd0..b4d6eeba710 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/lambda/services/lambda-tool-functions.service.ts
@@ -162,17 +162,6 @@ export class LambdaToolFunctionsService {
return { builtCode: parsedResult.builtCode };
}
- async getDependenciesBuildCacheKey({
- workspaceId,
- }: Pick<BrowserDependenciesBuildParams, 'workspaceId'>) {
- const name = await getFrontComponentBuilderName({
- workspaceId,
- namespace: this.options.resourceNamespace,
- action: 'buildDependencies',
- });
- return `lambda:${name}`;
- }
-
async buildDependencies({
targetRuntime: _targetRuntime,
...request
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.spec.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.spec.ts
index 09955deaeb4..811eaf4ab27 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.spec.ts
@@ -124,7 +124,11 @@ const escapeUnsafeChars = (value: string): string =>
jest.useRealTimers();
-const PACKAGE_JSON = '{"name":"local-build-fixture","private":true}\n';
+const PACKAGE_JSON = `${JSON.stringify({
+ name: 'local-build-fixture',
+ private: true,
+ dependencies: { react: '*', 'react-dom': '*', 'twenty-ui': '*' },
+})}\n`;
const YARN_LOCK = '# exact app-owned lock bytes\n';
const INPUT = {
componentId: 'local-component',
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.ts
index 4c656d2a6ef..eddeb9a40ba 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/drivers/local.driver.ts
@@ -35,7 +35,6 @@ import { frontComponentBuildResultSchema } from 'src/engine/metadata-modules/ai/
import { frontComponentDependenciesBuildResultSchema } from 'src/engine/metadata-modules/ai/ai-front-component/schemas/front-component-dependencies-build-result.schema';
import { type FrontComponentBuilderRequest } from 'src/engine/metadata-modules/ai/ai-front-component/types/front-component-builder-request.type';
import { copyFrontComponentBuilder } from 'src/engine/metadata-modules/ai/ai-front-component/utils/copy-front-component-builder.util';
-import { getFrontComponentBuilderName } from 'src/engine/metadata-modules/ai/ai-front-component/utils/get-front-component-builder-name.util';
import {
FrontComponentException,
FrontComponentExceptionCode,
@@ -122,17 +121,6 @@ export class LocalDriver implements LogicFunctionDriver {
}
}
- async getDependenciesBuildCacheKey({
- workspaceId,
- }: Pick<BrowserDependenciesBuildParams, 'workspaceId'>) {
- const name = await getFrontComponentBuilderName({
- workspaceId,
- namespace: 'local',
- action: 'buildDependencies',
- });
- return `local:${name}`;
- }
-
async buildDependencies({
targetRuntime: _targetRuntime,
workspaceId: _workspaceId,
diff --git a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface.ts b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface.ts
index 0e624cdd64e..566ccaa8d7c 100644
--- a/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface.ts
+++ b/packages/twenty-server/src/engine/core-modules/logic-function/logic-function-drivers/interfaces/logic-function-driver.interface.ts
@@ -75,9 +75,6 @@ export interface LogicFunctionDriver {
buildDependencies(
params: BrowserDependenciesBuildParams,
): Promise<BrowserDependenciesBuildResult>;
- getDependenciesBuildCacheKey(
- params: Pick<BrowserDependenciesBuildParams, 'workspaceId'>,
- ): Promise<string>;
installPrebuiltBundle(
params: LogicFunctionInstallPrebuiltBundleParams,
): Promise<void>;
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-allowed-imports.constant.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-allowed-imports.constant.ts
index 3d84eccb4af..5b2d93634b0 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-allowed-imports.constant.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-allowed-imports.constant.ts
@@ -1,4 +1,4 @@
-import { FRONT_COMPONENT_UI_IMPORTS } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-ui-imports.constant';
+import { FRONT_COMPONENT_UI_IMPORTS } from 'twenty-shared/front-component-build';
export const FRONT_COMPONENT_ALLOWED_IMPORTS = [
'react',
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-shared-dependencies-banner-prefix.constant.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-shared-dependencies-banner-prefix.constant.ts
deleted file mode 100644
index 1e3cb1b6f9f..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-shared-dependencies-banner-prefix.constant.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export const FRONT_COMPONENT_SHARED_DEPENDENCIES_BANNER_PREFIX =
- '// twenty-ai-shared-dependencies: ';
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
index 28dceb23b7a..7d4a3f46a96 100644
--- 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
@@ -1,20 +1,14 @@
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,
@@ -23,21 +17,11 @@ import {
@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,
@@ -58,14 +42,10 @@ export class FrontComponentCompilerService {
}
const source = this.buildEntrySource(input);
const driver = this.driverFactory.getCurrentDriver();
- const dependencies =
- await this.workspaceDependencies.readOrThrow(workspaceId);
const builtDependencies =
- await this.dependencyManager.getSharedDependenciesOrThrow({
+ await this.workspaceDependencies.getSharedDependenciesOrThrow(
workspaceId,
- dependencies,
- driver,
- });
+ );
const built = await driver.transpile({
targetRuntime: 'browser',
workspaceId,
@@ -82,8 +62,6 @@ export class FrontComponentCompilerService {
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
) {
@@ -94,17 +72,10 @@ export class FrontComponentCompilerService {
}
return {
source,
- dependencyRevision: dependencies.revision,
+ dependencyRevision: builtDependencies.dependencyRevision,
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),
- ),
- },
};
}
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
deleted file mode 100644
index ab5a0dbf521..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/front-component-dependency-manager.service.ts
+++ /dev/null
@@ -1,83 +0,0 @@
-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__/build-front-component-shared-dependencies.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/build-front-component-shared-dependencies.util.spec.ts
index ba9351c905e..f4bf184108a 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/build-front-component-shared-dependencies.util.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/build-front-component-shared-dependencies.util.spec.ts
@@ -45,7 +45,7 @@ const INPUT = {
};
describe('buildFrontComponentSharedDependencies', () => {
- it('loads real twenty-ui controls, icons and theme from one pinned shared bundle', async () => {
+ it('loads real twenty-ui controls, icons and theme from the app shared bundle', async () => {
const { compiledSource, sharedDependencies } = await buildFrontComponent({
...INPUT,
source: `import { Button } from 'twenty-ui/primitives/input';
@@ -61,13 +61,6 @@ export function Component() {
expect(compiledSource).toContain(
FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER,
);
- expect(compiledSource).toMatch(
- new RegExp(
- '^// twenty-ai-shared-dependencies: ' +
- sharedDependencies.checksum +
- '\\n',
- ),
- );
expect(
sharedDependencies.exportNamesBySpecifier.get('react-dom')?.namedExports,
).toContain('createPortal');
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/build-front-component.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/build-front-component.util.spec.ts
index 53d883f0ec6..fd0960d313f 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/build-front-component.util.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/build-front-component.util.spec.ts
@@ -1,7 +1,7 @@
import { build, transform } from 'esbuild';
import { PageLayoutType } from 'twenty-shared/types';
-import { FRONT_COMPONENT_UI_IMPORTS } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-ui-imports.constant';
+import { FRONT_COMPONENT_UI_IMPORTS } from 'twenty-shared/front-component-build';
import { buildFrontComponent as compileFrontComponent } from 'src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component.util';
import { buildFrontComponentEntrySource } from 'src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-entry-source.util';
import { FrontComponentBuildException } from 'src/engine/metadata-modules/ai/ai-front-component/exceptions/front-component-build.exception';
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/install-and-build-front-component-shared-dependencies.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/install-and-build-front-component-shared-dependencies.util.spec.ts
index 0fa4db92dab..ed55d703eb3 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/install-and-build-front-component-shared-dependencies.util.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/install-and-build-front-component-shared-dependencies.util.spec.ts
@@ -4,6 +4,7 @@ import { tmpdir } from 'os';
import { join } from 'path';
import { promisify } from 'util';
+import { FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES } from 'twenty-shared/front-component-build';
import { buildFrontComponentSharedDependencies } from 'src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-shared-dependencies.util';
import { installAndBuildFrontComponentSharedDependencies } from 'src/engine/metadata-modules/ai/ai-front-component/utils/install-and-build-front-component-shared-dependencies.util';
@@ -27,7 +28,14 @@ const install = jest.mocked<
) => Promise<{ stdout: string; stderr: string }>
>(promisify(execFile));
const build = jest.mocked(buildFrontComponentSharedDependencies);
-const packageJson = '{"private":true,"devDependencies":{"twenty-ui":"2.50.0"}}';
+const packageJson = JSON.stringify({
+ private: true,
+ devDependencies: {
+ react: '19.2.0',
+ 'react-dom': '19.2.0',
+ 'twenty-ui': '2.50.0',
+ },
+});
const yarnLock = '# exact stored lock bytes\n';
const vendor: Awaited<
ReturnType<typeof buildFrontComponentSharedDependencies>
@@ -113,6 +121,50 @@ describe('installAndBuildFrontComponentSharedDependencies', () => {
await expect(access(directory)).rejects.toThrow();
});
+ it('includes required React and UI imports alongside the app declaration', async () => {
+ const dependencies = ['react', 'twenty-ui/theme', 'some-other-package'];
+ await installAndBuildFrontComponentSharedDependencies({
+ packageJson: JSON.stringify({
+ ...JSON.parse(packageJson),
+ frontComponentSharedDependencies: dependencies,
+ }),
+ yarnLock,
+ yarnEngineDirectory,
+ });
+ expect(build).toHaveBeenCalledWith(
+ expect.objectContaining({
+ dependencies: expect.arrayContaining([
+ ...FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES,
+ ...dependencies,
+ 'react/jsx-runtime',
+ ]),
+ }),
+ );
+ });
+
+ it.each([
+ ['not an array', 'react'],
+ ['empty specifier', ['']],
+ ['non-string', [42]],
+ ['relative path', ['./local-module']],
+ ['absolute path', ['/tmp/local-module']],
+ ['Windows path', ['C:\\lib']],
+ ['reserved SDK', ['twenty-client-sdk/core']],
+ ['duplicate', ['react', 'react']],
+ ])('rejects %s before installing anything', async (_label, dependencies) => {
+ await expect(
+ installAndBuildFrontComponentSharedDependencies({
+ packageJson: JSON.stringify({
+ frontComponentSharedDependencies: dependencies,
+ }),
+ yarnLock,
+ yarnEngineDirectory,
+ }),
+ ).rejects.toMatchObject({ code: 'INVALID_INPUT' });
+ expect(install).not.toHaveBeenCalled();
+ expect(build).not.toHaveBeenCalled();
+ });
+
it.each(['install', 'build'])(
'removes the temporary install after %s fails',
async (stage) => {
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/read-front-component-shared-dependencies-checksum.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/read-front-component-shared-dependencies-checksum.util.spec.ts
deleted file mode 100644
index 94d2171d720..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/__tests__/read-front-component-shared-dependencies-checksum.util.spec.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import { readFrontComponentSharedDependenciesChecksum } from 'src/engine/metadata-modules/ai/ai-front-component/utils/read-front-component-shared-dependencies-checksum.util';
-
-const CHECKSUM = 'a'.repeat(64);
-const BANNER = '// twenty-ai-shared-dependencies: ' + CHECKSUM;
-
-describe('readFrontComponentSharedDependenciesChecksum', () => {
- it('reads the pinned checksum only from the generated first line', () => {
- expect(
- readFrontComponentSharedDependenciesChecksum(
- BANNER + '\nexport default () => {};',
- ),
- ).toBe(CHECKSUM);
- });
-
- it.each([
- 'export default () => {};',
- '\n' + BANNER,
- BANNER + 'extra\n',
- '// twenty-ai-shared-dependencies: ../../other-file\n',
- ])('ignores missing or malformed metadata: %s', (source) => {
- expect(
- readFrontComponentSharedDependenciesChecksum(source),
- ).toBeUndefined();
- });
-});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-shared-dependencies.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-shared-dependencies.util.ts
index d75b1d5b37f..881ee0a3921 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-shared-dependencies.util.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-shared-dependencies.util.ts
@@ -1,31 +1,27 @@
import { createHash } from 'crypto';
import {
+ FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES,
createJsxRuntimeRemoteWrapperPlugin,
cssInjectionPlugin,
getCommonFrontComponentBuildOptions,
getSharedDependenciesEntrySource,
enumerateSharedDependenciesExportNames,
+ normalizeFrontComponentSharedDependencies,
} from 'twenty-shared/front-component-build';
-import { FRONT_COMPONENT_UI_IMPORTS } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-ui-imports.constant';
import { type FrontComponentSharedDependencies } from 'src/engine/metadata-modules/ai/ai-front-component/types/front-component-shared-dependencies.type';
import { buildFrontComponentWithLimitsOrThrow } from 'src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-with-limits-or-throw.util';
export const buildFrontComponentSharedDependencies = async ({
resolveDirectory,
+ dependencies = FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES,
}: {
resolveDirectory: string;
+ dependencies?: string[];
}): Promise<FrontComponentSharedDependencies> => {
- const dependencies = [
- 'react',
- 'react-dom',
- 'react-dom/client',
- 'react/jsx-runtime',
- ...FRONT_COMPONENT_UI_IMPORTS,
- ];
const exportNamesBySpecifier = new Map(
await Promise.all(
- dependencies.map(
+ normalizeFrontComponentSharedDependencies({ dependencies }).map(
async (specifier) =>
[
specifier,
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component.util.ts
index c65d822a7ee..3116d40eafd 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component.util.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component.util.ts
@@ -10,7 +10,6 @@ import {
import { FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER } from 'twenty-shared/application';
import { type FrontComponentSharedDependencies } from 'src/engine/metadata-modules/ai/ai-front-component/types/front-component-shared-dependencies.type';
-import { FRONT_COMPONENT_SHARED_DEPENDENCIES_BANNER_PREFIX } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-shared-dependencies-banner-prefix.constant';
import { FRONT_COMPONENT_ALLOWED_IMPORTS } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-allowed-imports.constant';
import { FRONT_COMPONENT_WRAPPER_NAME } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-wrapper-name.constant';
import { type FrontComponentBuildInput } from 'src/engine/metadata-modules/ai/ai-front-component/types/front-component-build-input.type';
@@ -48,11 +47,6 @@ export const buildFrontComponent = async ({
write: false,
platform: 'browser',
external: [FRONT_COMPONENT_SHARED_DEPENDENCIES_IMPORT_SPECIFIER],
- banner: {
- js:
- FRONT_COMPONENT_SHARED_DEPENDENCIES_BANNER_PREFIX +
- sharedDependencies.checksum,
- },
plugins: [
{
name: 'front-component-imports',
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/install-and-build-front-component-shared-dependencies.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/install-and-build-front-component-shared-dependencies.util.ts
index 62ed69276bf..8afcfefedab 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/install-and-build-front-component-shared-dependencies.util.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/install-and-build-front-component-shared-dependencies.util.ts
@@ -1,8 +1,15 @@
import { mkdtemp, rm, writeFile } from 'fs/promises';
+import { z } from 'zod';
+import {
+ parseFrontComponentSharedDependencies,
+ normalizeFrontComponentSharedDependencies,
+} from 'twenty-shared/front-component-build';
+import { isNonEmptyArray } from 'twenty-shared/utils';
import { tmpdir } from 'os';
import { join } from 'path';
import { installYarnDependencies } from 'src/engine/core-modules/application/application-package/constants/yarn-engine/install-yarn-dependencies.cjs';
+import { FrontComponentBuildException } from 'src/engine/metadata-modules/ai/ai-front-component/exceptions/front-component-build.exception';
import { buildFrontComponentSharedDependencies } from 'src/engine/metadata-modules/ai/ai-front-component/utils/build-front-component-shared-dependencies.util';
const INSTALL_TIMEOUT_MS = 240_000;
@@ -16,6 +23,24 @@ export const installAndBuildFrontComponentSharedDependencies = async ({
yarnLock: string;
yarnEngineDirectory: string;
}) => {
+ const packageJsonContent = z
+ .object({
+ frontComponentSharedDependencies: z.unknown().optional(),
+ dependencies: z.record(z.string(), z.string()).optional(),
+ devDependencies: z.record(z.string(), z.string()).optional(),
+ })
+ .parse(JSON.parse(packageJson));
+ const { dependencies, errors } = parseFrontComponentSharedDependencies(
+ packageJsonContent.frontComponentSharedDependencies,
+ );
+
+ if (isNonEmptyArray(errors)) {
+ throw new FrontComponentBuildException({
+ message: errors.join('\n'),
+ code: 'INVALID_INPUT',
+ });
+ }
+
const directory = await mkdtemp(join(tmpdir(), 'front-component-vendor-'));
try {
@@ -37,6 +62,13 @@ export const installAndBuildFrontComponentSharedDependencies = async ({
return await buildFrontComponentSharedDependencies({
resolveDirectory: directory,
+ dependencies: normalizeFrontComponentSharedDependencies({
+ dependencies: dependencies ?? [],
+ packageNames: Object.keys({
+ ...packageJsonContent.dependencies,
+ ...packageJsonContent.devDependencies,
+ }),
+ }),
});
} finally {
await rm(directory, { recursive: true, force: true });
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/read-front-component-shared-dependencies-checksum.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/read-front-component-shared-dependencies-checksum.util.ts
deleted file mode 100644
index b9459c7d0d7..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/utils/read-front-component-shared-dependencies-checksum.util.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { FRONT_COMPONENT_SHARED_DEPENDENCIES_BANNER_PREFIX } from 'src/engine/metadata-modules/ai/ai-front-component/constants/front-component-shared-dependencies-banner-prefix.constant';
-
-export const readFrontComponentSharedDependenciesChecksum = (
- source: string,
-): string | undefined => {
- const firstLine = source.split('\n', 1)[0];
-
- if (
- !firstLine.startsWith(FRONT_COMPONENT_SHARED_DEPENDENCIES_BANNER_PREFIX)
- ) {
- return undefined;
- }
- const checksum = firstLine.slice(
- FRONT_COMPONENT_SHARED_DEPENDENCIES_BANNER_PREFIX.length,
- );
-
- return /^[a-f0-9]{64}$/.test(checksum) ? checksum : 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
index 6a80bde9563..74af74b8fc6 100644
--- 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
@@ -1,6 +1,33 @@
+import { msg } from '@lingui/core/macro';
import { Injectable } from '@nestjs/common';
import { z } from 'zod';
-import { computeDependencyFileChecksum } from 'twenty-shared/application';
+import {
+ computeDependencyFileChecksum,
+ FRONT_COMPONENT_SHARED_DEPENDENCIES_BUILT_PATH,
+} from 'twenty-shared/application';
+import {
+ FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES,
+ getSharedDependenciesBuildManifestPath,
+ type SharedDependenciesBuildManifest,
+} from 'twenty-shared/front-component-build';
+import { FileFolder } from 'twenty-shared/types';
+import { createHash } from 'crypto';
+import { CacheLockService } from 'src/engine/core-modules/cache-lock/cache-lock.service';
+import { FileStorageService } from 'src/engine/core-modules/file-storage/services/file-storage.service';
+import {
+ FileStorageException,
+ FileStorageExceptionCode,
+} from 'src/engine/core-modules/file-storage/interfaces/file-storage-exception';
+import { LogicFunctionDriverFactory } from 'src/engine/core-modules/logic-function/logic-function-drivers/logic-function-driver.factory';
+import {
+ BUILD_LOCK_MAX_RETRIES,
+ BUILD_LOCK_RETRY_MS,
+ 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 { frontComponentSharedDependenciesManifestSchema } from 'src/engine/metadata-modules/ai/ai-front-component/schemas/front-component-shared-dependencies-manifest.schema';
+import { convertFrontComponentBuildFailureToException } from 'src/engine/metadata-modules/ai/ai-front-component/utils/convert-front-component-build-failure-to-exception.util';
+import { streamToBuffer } from 'src/utils/stream-to-buffer';
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';
@@ -19,8 +46,169 @@ export class WorkspaceFrontComponentDependenciesService {
constructor(
private readonly applicationService: ApplicationService,
private readonly applicationDependencyFilesService: ApplicationDependencyFilesService,
+ private readonly fileStorageService: FileStorageService,
+ private readonly cacheLockService: CacheLockService,
+ private readonly driverFactory: LogicFunctionDriverFactory,
) {}
+ async getSharedDependenciesOrThrow(workspaceId: string) {
+ return this.cacheLockService.withLock(
+ async () => {
+ const dependencies = await this.readOrThrow(workspaceId);
+ const location = {
+ workspaceId,
+ applicationUniversalIdentifier:
+ dependencies.applicationUniversalIdentifier,
+ fileFolder: FileFolder.BuiltFrontComponent,
+ };
+ const builtPath =
+ dependencies.builtPath ??
+ FRONT_COMPONENT_SHARED_DEPENDENCIES_BUILT_PATH;
+ const existingManifest = await this.readBuildManifest({
+ ...location,
+ fileFolder: FileFolder.Dependencies,
+ resourcePath: getSharedDependenciesBuildManifestPath(builtPath),
+ });
+
+ if (
+ isDefined(existingManifest) &&
+ FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES.every((specifier) =>
+ isDefined(existingManifest.exports[specifier]),
+ ) &&
+ existingManifest.checksum === dependencies.builtChecksum &&
+ existingManifest.packageJsonChecksum ===
+ dependencies.packageJsonChecksum &&
+ existingManifest.yarnLockChecksum === dependencies.yarnLockChecksum &&
+ (await this.fileStorageService.checkFileExists({
+ ...location,
+ resourcePath: builtPath,
+ }))
+ ) {
+ return {
+ ...existingManifest,
+ dependencyRevision: dependencies.revision,
+ };
+ }
+
+ const result = await this.driverFactory
+ .getCurrentDriver()
+ .buildDependencies({
+ workspaceId,
+ targetRuntime: 'browser',
+ packageJson: dependencies.packageJson,
+ yarnLock: dependencies.yarnLock,
+ });
+ if (!result.success) {
+ throw convertFrontComponentBuildFailureToException(result);
+ }
+ if (
+ createHash('sha256').update(result.builtCode).digest('hex') !==
+ result.checksum
+ ) {
+ throw new FrontComponentException(
+ 'Shared frontend component dependencies checksum mismatch.',
+ FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED,
+ {
+ userFriendlyMessage: msg`The component dependencies could not be verified. Try creating the component again.`,
+ },
+ );
+ }
+ if (
+ Buffer.byteLength(result.builtCode) >
+ FRONT_COMPONENT_MAX_BUILD_OUTPUT_BYTES
+ ) {
+ throw new FrontComponentException(
+ 'Shared frontend component dependencies exceed the build output size limit.',
+ FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED,
+ {
+ userFriendlyMessage: msg`The component dependencies are too large. Reduce the Workspace app dependencies and try again.`,
+ },
+ );
+ }
+ const currentDependencies = await this.readOrThrow(workspaceId);
+ if (
+ currentDependencies.revision !== dependencies.revision ||
+ currentDependencies.builtChecksum !== dependencies.builtChecksum
+ ) {
+ throw new FrontComponentException(
+ 'The Workspace app dependencies changed during the build. Retry creating the component.',
+ FrontComponentExceptionCode.FRONT_COMPONENT_CREATE_FAILED,
+ {
+ userFriendlyMessage: msg`The Workspace app dependencies changed during the build. Try creating the component again.`,
+ },
+ );
+ }
+ const manifest: SharedDependenciesBuildManifest = {
+ checksum: result.checksum,
+ exports: result.exports,
+ packageJsonChecksum: dependencies.packageJsonChecksum,
+ yarnLockChecksum: dependencies.yarnLockChecksum,
+ };
+
+ await this.fileStorageService.writeFile({
+ ...location,
+ applicationId: dependencies.applicationId,
+ resourcePath: builtPath,
+ sourceFile: result.builtCode,
+ settings: { isTemporaryFile: false, toDelete: false },
+ });
+ await this.fileStorageService.writeFile({
+ ...location,
+ applicationId: dependencies.applicationId,
+ fileFolder: FileFolder.Dependencies,
+ resourcePath: getSharedDependenciesBuildManifestPath(builtPath),
+ sourceFile: JSON.stringify(manifest),
+ settings: { isTemporaryFile: false, toDelete: false },
+ });
+ await this.applicationService.update(dependencies.applicationId, {
+ workspaceId,
+ frontComponentSharedDependenciesBuiltPath: builtPath,
+ frontComponentSharedDependenciesChecksum: result.checksum,
+ });
+
+ if (dependencies.builtChecksum !== result.checksum) {
+ await this.applicationService.broadcastFrontComponentSharedDependenciesChecksumUpdates(
+ {
+ workspaceId,
+ applicationId: dependencies.applicationId,
+ frontComponentSharedDependenciesChecksum: result.checksum,
+ },
+ );
+ }
+
+ return { ...manifest, dependencyRevision: dependencies.revision };
+ },
+ `front-component-dependencies:${workspaceId}`,
+ {
+ ttl: (YARN_INSTALL_LAMBDA_TIMEOUT_SECONDS + 60) * 1000,
+ ms: BUILD_LOCK_RETRY_MS,
+ maxRetries: BUILD_LOCK_MAX_RETRIES,
+ },
+ );
+ }
+
+ private async readBuildManifest(
+ location: Parameters<FileStorageService['readFile']>[0],
+ ) {
+ try {
+ const stream = await this.fileStorageService.readFile(location);
+ return frontComponentSharedDependenciesManifestSchema
+ .extend({
+ packageJsonChecksum: z.string(),
+ yarnLockChecksum: z.string().nullable(),
+ })
+ .parse(JSON.parse((await streamToBuffer(stream)).toString()));
+ } catch (error) {
+ if (
+ error instanceof FileStorageException &&
+ error.code === FileStorageExceptionCode.FILE_NOT_FOUND
+ ) {
+ return undefined;
+ }
+ throw error;
+ }
+ }
+
async readOrThrow(workspaceId: string) {
const { workspaceCustomFlatApplication: application } =
await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
@@ -45,6 +233,9 @@ export class WorkspaceFrontComponentDependenciesService {
throw new FrontComponentException(
'Add yarn.lock to the Workspace app before creating a component.',
FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT,
+ {
+ userFriendlyMessage: msg`Add yarn.lock to the Workspace app before creating a component.`,
+ },
);
}
const { dependencies, devDependencies } = packageSchema.parse(
@@ -54,15 +245,24 @@ export class WorkspaceFrontComponentDependenciesService {
(name) => !isDefined(dependencies?.[name] ?? devDependencies?.[name]),
);
if (missingPackages.length > 0) {
+ const missingPackageNames = missingPackages.join(', ');
+
throw new FrontComponentException(
- `Add ${missingPackages.join(', ')} to the Workspace app package.json and yarn.lock before creating a component.`,
+ `Add ${missingPackageNames} to the Workspace app package.json and yarn.lock before creating a component.`,
FrontComponentExceptionCode.INVALID_FRONT_COMPONENT_INPUT,
+ {
+ userFriendlyMessage: msg`Add ${missingPackageNames} to the Workspace app package.json and yarn.lock before creating a component.`,
+ },
);
}
return {
applicationId: application.id,
+ applicationUniversalIdentifier: application.universalIdentifier,
packageJson,
yarnLock,
+ ...storedFilesChecksums,
+ builtPath: application.frontComponentSharedDependenciesBuiltPath,
+ builtChecksum: application.frontComponentSharedDependenciesChecksum,
revision: computeDependencyFileChecksum(
JSON.stringify([
application.id,
diff --git a/packages/twenty-shared/src/front-component-build/front-component-required-shared-dependencies.ts b/packages/twenty-shared/src/front-component-build/front-component-required-shared-dependencies.ts
new file mode 100644
index 00000000000..227a7b30f7d
--- /dev/null
+++ b/packages/twenty-shared/src/front-component-build/front-component-required-shared-dependencies.ts
@@ -0,0 +1,9 @@
+import { FRONT_COMPONENT_UI_IMPORTS } from './front-component-ui-imports';
+
+export const FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES = [
+ 'react',
+ 'react-dom',
+ 'react-dom/client',
+ 'react/jsx-runtime',
+ ...FRONT_COMPONENT_UI_IMPORTS,
+];
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-ui-imports.constant.ts b/packages/twenty-shared/src/front-component-build/front-component-ui-imports.ts
similarity index 100%
rename from packages/twenty-server/src/engine/metadata-modules/ai/ai-front-component/constants/front-component-ui-imports.constant.ts
rename to packages/twenty-shared/src/front-component-build/front-component-ui-imports.ts
diff --git a/packages/twenty-shared/src/front-component-build/index.ts b/packages/twenty-shared/src/front-component-build/index.ts
index 1a52280cb56..3bf6199779d 100644
--- a/packages/twenty-shared/src/front-component-build/index.ts
+++ b/packages/twenty-shared/src/front-component-build/index.ts
@@ -13,6 +13,8 @@ export {
cssInjectionPlugin,
} from './css-injection-plugin';
export { enumerateSharedDependenciesExportNames } from './enumerate-shared-dependencies-export-names';
+export { FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES } from './front-component-required-shared-dependencies';
+export { FRONT_COMPONENT_UI_IMPORTS } from './front-component-ui-imports';
export { getCommonFrontComponentBuildOptions } from './get-common-front-component-build-options';
export { getSharedDependenciesBuildManifestPath } from './get-shared-dependencies-build-manifest-path';
export { getSharedDependenciesEntrySource } from './get-shared-dependencies-entry-source';
diff --git a/packages/twenty-shared/src/front-component-build/normalize-front-component-shared-dependencies.ts b/packages/twenty-shared/src/front-component-build/normalize-front-component-shared-dependencies.ts
index c05544766df..4c9d37edede 100644
--- a/packages/twenty-shared/src/front-component-build/normalize-front-component-shared-dependencies.ts
+++ b/packages/twenty-shared/src/front-component-build/normalize-front-component-shared-dependencies.ts
@@ -1,7 +1,21 @@
-export const normalizeFrontComponentSharedDependencies = (
- dependencies: string[],
-): string[] => {
- const normalizedDependencies = new Set(dependencies);
+import { FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES } from './front-component-required-shared-dependencies';
+
+export const normalizeFrontComponentSharedDependencies = ({
+ dependencies,
+ packageNames = [],
+}: {
+ dependencies: string[];
+ packageNames?: string[];
+}): string[] => {
+ const normalizedDependencies = new Set([
+ ...dependencies,
+ ...FRONT_COMPONENT_REQUIRED_SHARED_DEPENDENCIES.filter((specifier) =>
+ packageNames.some(
+ (packageName) =>
+ specifier === packageName || specifier.startsWith(`${packageName}/`),
+ ),
+ ),
+ ]);
if (normalizedDependencies.has('react')) {
normalizedDependencies.add('react/jsx-runtime');
diff --git a/packages/twenty-shared/src/front-component-build/parse-front-component-shared-dependencies.ts b/packages/twenty-shared/src/front-component-build/parse-front-component-shared-dependencies.ts
index 913f0065c8e..a9cc967a51f 100644
--- a/packages/twenty-shared/src/front-component-build/parse-front-component-shared-dependencies.ts
+++ b/packages/twenty-shared/src/front-component-build/parse-front-component-shared-dependencies.ts
@@ -77,9 +77,9 @@ export const parseFrontComponentSharedDependencies = (
return isNonEmptyArray(errors)
? { errors }
: {
- dependencies: normalizeFrontComponentSharedDependencies([
- ...seenDependencies,
- ]),
+ dependencies: normalizeFrontComponentSharedDependencies({
+ dependencies: [...seenDependencies],
+ }),
errors: [],
};
};