The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Ship code interpreter sandbox scripts and let E2B sandboxes use a custom template (#29466)
Etienne7157d1547abe
diff --git a/packages/twenty-server/nest-cli.json b/packages/twenty-server/nest-cli.jsonindex aed19c6a32c..febde5e51b0 100644--- a/packages/twenty-server/nest-cli.json+++ b/packages/twenty-server/nest-cli.json@@ -65,6 +65,10 @@"include": "engine/metadata-modules/ai/ai-models/ai-providers.json","outDir": "dist"},+ {+ "include": "engine/core-modules/code-interpreter/sandbox-scripts/**",+ "outDir": "dist"+ },{"include": "engine/core-modules/dpa/constants/subprocessors.json","outDir": "dist"diff --git a/packages/twenty-server/scripts/build-code-interpreter-e2b-template.ts b/packages/twenty-server/scripts/build-code-interpreter-e2b-template.tsnew file mode 100644index 00000000000..45151668f98--- /dev/null+++ b/packages/twenty-server/scripts/build-code-interpreter-e2b-template.ts@@ -0,0 +1,38 @@+import { defaultBuildLogger, Template } from '@e2b/code-interpreter';++const templateName = process.argv[2] ?? 'twenty-code-interpreter';++const template = Template()+ .fromTemplate('code-interpreter-v1')+ .aptInstall([+ 'poppler-utils',+ 'libreoffice-calc',+ 'libreoffice-impress',+ 'libreoffice-writer',+ ])+ .pipInstall([+ 'pdf2image==1.17.0',+ 'pdfplumber==0.11.10',+ 'pymupdf==2.0',+ 'pypdf==6.21.4',+ 'PyPDF2==3.0.1',+ 'python-pptx==1.0.2',+ 'reportlab==5.1.0',+ ]);++const main = async (): Promise<void> => {+ const { name } = await Template.build(template, templateName, {+ cpuCount: 2,+ memoryMB: 2048,+ onBuildLogs: defaultBuildLogger(),+ });++ // oxlint-disable-next-line no-console+ console.log(`Built E2B template ${name}, set E2B_TEMPLATE=${name}`);+};++main().catch((error) => {+ // oxlint-disable-next-line no-console+ console.error('E2B template build failed:', error);+ process.exit(1);+});diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/code-interpreter-driver.factory.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/code-interpreter-driver.factory.tsindex b4ccb4ffae8..aefd45a44e0 100644--- a/packages/twenty-server/src/engine/core-modules/code-interpreter/code-interpreter-driver.factory.ts+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/code-interpreter-driver.factory.ts@@ -71,6 +71,7 @@ export class CodeInterpreterDriverFactory extends DriverFactoryBase<CodeInterprereturn new E2BDriver({apiKey,+ template: this.twentyConfigService.get('E2B_TEMPLATE'),timeoutMs,idleTimeoutMs: this.twentyConfigService.get('CODE_INTERPRETER_IDLE_TIMEOUT_MS',diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/e2b.driver.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/e2b.driver.tsindex 0efa91db979..2b8964e0d75 100644--- a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/e2b.driver.ts+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/e2b.driver.ts@@ -29,6 +29,7 @@ import {export type E2BDriverOptions = {apiKey: string;+ template: string;timeoutMs?: number;idleTimeoutMs?: number;};@@ -68,7 +69,7 @@ export class E2BDriver implements CodeInterpreterDriver {context?: ExecutionContext,callbacks?: StreamCallbacks,): Promise<CodeExecutionResult> {- const { apiKey } = this.options;+ const { apiKey, template } = this.options;const sessionId = context?.sessionId;const idleTimeoutMs =this.options.idleTimeoutMs ?? DEFAULT_CODE_INTERPRETER_TIMEOUT_MS;@@ -84,13 +85,14 @@ export class E2BDriver implements CodeInterpreterDriver {({ sandbox, isReused } = await getOrCreateSessionSandbox({sandboxApi: Sandbox,apiKey,+ template,sessionId,actorKey: context?.actorKey,timeoutMs,idleTimeoutMs,}));} else {- sandbox = await Sandbox.create({ apiKey, timeoutMs });+ sandbox = await Sandbox.create(template, { apiKey, timeoutMs });}try {@@ -101,8 +103,10 @@ export class E2BDriver implements CodeInterpreterDriver {SANDBOX_SCRIPTS_PATH,'/home/user/scripts',);- } catch {- // Scripts directory might not exist+ } catch (error) {+ const reason = error instanceof Error ? error.message : String(error);++ this.logger.warn(`Failed to upload sandbox scripts: ${reason}`);}}diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/utils/__tests__/get-or-create-session-sandbox.util.spec.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/utils/__tests__/get-or-create-session-sandbox.util.spec.tsindex 178faac650e..eeed63bc7fc 100644--- a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/utils/__tests__/get-or-create-session-sandbox.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/utils/__tests__/get-or-create-session-sandbox.util.spec.ts@@ -13,6 +13,7 @@ type SandboxApiMock = {type ListedSandbox = { sandboxId: string; metadata: Record<string, string> };const apiKey = 'test-api-key';+const template = 'test-template';const sessionId = 'workspace-1:thread-1';const timeoutMs = 600_000;const idleTimeoutMs = 300_000;@@ -73,6 +74,7 @@ describe('getOrCreateSessionSandbox', () => {const result = await getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,@@ -102,13 +104,14 @@ describe('getOrCreateSessionSandbox', () => {const result = await getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,});expect(result).toEqual({ sandbox, isReused: false });- expect(mock.create).toHaveBeenCalledWith({+ expect(mock.create).toHaveBeenCalledWith(template, {apiKey,timeoutMs: aliveTimeoutMs,lifecycle: { onTimeout: 'pause', autoResume: true },@@ -133,6 +136,7 @@ describe('getOrCreateSessionSandbox', () => {const result = await getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,@@ -158,6 +162,7 @@ describe('getOrCreateSessionSandbox', () => {const result = await getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,@@ -183,6 +188,7 @@ describe('getOrCreateSessionSandbox', () => {const result = await getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,@@ -207,6 +213,7 @@ describe('getOrCreateSessionSandbox', () => {const result = await getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,@@ -235,6 +242,7 @@ describe('getOrCreateSessionSandbox', () => {const result = await getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,@@ -263,6 +271,7 @@ describe('Participant isolation', () => {const result = await getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,@@ -272,6 +281,7 @@ describe('Participant isolation', () => {expect(mock.connect).not.toHaveBeenCalled();expect(mock.kill).toHaveBeenCalledWith('old-sandbox', { apiKey });expect(mock.create).toHaveBeenCalledWith(+ template,expect.objectContaining({metadata: {[SESSION_SANDBOX_METADATA_KEY]: sessionId,@@ -293,6 +303,7 @@ describe('Participant isolation', () => {getOrCreateSessionSandbox({sandboxApi: api,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,diff --git a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/utils/get-or-create-session-sandbox.util.ts b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/utils/get-or-create-session-sandbox.util.tsindex a429dfa01a4..e67398939e7 100644--- a/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/utils/get-or-create-session-sandbox.util.ts+++ b/packages/twenty-server/src/engine/core-modules/code-interpreter/drivers/utils/get-or-create-session-sandbox.util.ts@@ -11,6 +11,7 @@ const SESSION_ACTOR_METADATA_KEY = 'twentySessionActor';type GetOrCreateSessionSandboxArgs = {sandboxApi: SandboxApi;apiKey: string;+ template: string;sessionId: string;actorKey?: string;timeoutMs: number;@@ -76,11 +77,12 @@ const killSandboxById = (const createSessionSandbox = (sandboxApi: SandboxApi,apiKey: string,+ template: string,sessionId: string,timeoutMs: number,actorKey?: string,) =>- sandboxApi.create({+ sandboxApi.create(template, {apiKey,timeoutMs,lifecycle: { onTimeout: 'pause', autoResume: true },@@ -95,6 +97,7 @@ const createSessionSandbox = (export const getOrCreateSessionSandbox = async ({sandboxApi,apiKey,+ template,sessionId,timeoutMs,idleTimeoutMs,@@ -146,6 +149,7 @@ export const getOrCreateSessionSandbox = async ({const sandbox = await createSessionSandbox(sandboxApi,apiKey,+ template,sessionId,aliveTimeoutMs,actorKey,diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.tsindex 3a122dd92f1..26aa97486f6 100644--- a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts@@ -900,6 +900,15 @@ export class ConfigVariables {)E2B_API_KEY?: string;+ @ConfigVariablesMetadata({+ group: ConfigVariablesGroup.CODE_INTERPRETER_CONFIG,+ description:+ 'E2B template for code interpreter sandboxes; build one with the libraries the PDF and Office skills need using scripts/build-code-interpreter-e2b-template.ts (default: code-interpreter-v1)',+ type: ConfigVariableType.STRING,+ })+ @IsOptional()+ E2B_TEMPLATE: string = 'code-interpreter-v1';+@ConfigVariablesMetadata({group: ConfigVariablesGroup.CODE_INTERPRETER_CONFIG,description: 'Timeout in milliseconds for code execution (default: 300000)',