@@ -4,6 +4,8 @@ import { ConnectionConfig } from '../connectionConfig'
44import { BuildError } from '../errors'
55import { runtime } from '../utils'
66import {
7+ getBuildStatus ,
8+ GetBuildStatusResponse ,
79 getFileUploadLink ,
810 requestBuild ,
911 triggerBuild ,
@@ -16,8 +18,10 @@ import { parseDockerfile } from './dockerfileParser'
1618import { LogEntry , LogEntryEnd , LogEntryStart } from './logger'
1719import { ReadyCmd , waitForFile } from './readycmd'
1820import {
21+ BuildInfo ,
1922 BuildOptions ,
2023 CopyItem ,
24+ GetBuildStatusOptions ,
2125 Instruction ,
2226 InstructionType ,
2327 McpServerName ,
@@ -113,15 +117,94 @@ export class TemplateBase
113117 static async build (
114118 template : TemplateClass ,
115119 options : BuildOptions
116- ) : Promise < void > {
120+ ) : Promise < BuildInfo > {
117121 try {
118122 options . onBuildLogs ?.( new LogEntryStart ( new Date ( ) , 'Build started' ) )
119- return await ( template as TemplateBase ) . build ( options )
123+ const baseTemplate = template as TemplateBase
124+
125+ const config = new ConnectionConfig ( {
126+ domain : options . domain ,
127+ apiKey : options . apiKey ,
128+ } )
129+ const client = new ApiClient ( config )
130+
131+ const data = await baseTemplate . build ( client , options )
132+
133+ options . onBuildLogs ?.(
134+ new LogEntry ( new Date ( ) , 'info' , 'Waiting for logs...' )
135+ )
136+
137+ await waitForBuildFinish ( client , {
138+ templateID : data . templateId ,
139+ buildID : data . buildId ,
140+ onBuildLogs : options . onBuildLogs ,
141+ logsRefreshFrequency : baseTemplate . logsRefreshFrequency ,
142+ stackTraces : baseTemplate . stackTraces ,
143+ } )
144+
145+ return data
120146 } finally {
121147 options . onBuildLogs ?.( new LogEntryEnd ( new Date ( ) , 'Build finished' ) )
122148 }
123149 }
124150
151+ /**
152+ * Build and deploy a template to E2B infrastructure.
153+ *
154+ * @param template The template to build
155+ * @param options Build configuration options
156+ *
157+ * @example
158+ * ```ts
159+ * const template = Template().fromPythonImage('3')
160+ * const data = await Template.buildInBackground(template, {
161+ * alias: 'my-python-env',
162+ * cpuCount: 2,
163+ * memoryMB: 1024
164+ * })
165+ * ```
166+ */
167+ static async buildInBackground (
168+ template : TemplateClass ,
169+ options : BuildOptions
170+ ) : Promise < BuildInfo > {
171+ const config = new ConnectionConfig ( {
172+ domain : options . domain ,
173+ apiKey : options . apiKey ,
174+ } )
175+ const client = new ApiClient ( config )
176+
177+ return await ( template as TemplateBase ) . build ( client , options )
178+ }
179+
180+ /**
181+ * Get the status of a build.
182+ *
183+ * @param data Build identifiers
184+ * @param options Authentication options
185+ *
186+ * @example
187+ * ```ts
188+ * const status = await Template.getBuildStatus(data, { logsOffset: 0 })
189+ * ```
190+ */
191+ static async getBuildStatus (
192+ data : Pick < BuildInfo , 'templateId' | 'buildId' > ,
193+ options ?: GetBuildStatusOptions
194+ ) : Promise < GetBuildStatusResponse > {
195+ const config = new ConnectionConfig ( {
196+ domain : options ?. domain ,
197+ apiKey : options ?. apiKey ,
198+ } )
199+ const client = new ApiClient ( config )
200+
201+ return await getBuildStatus ( client , {
202+ templateID : data . templateId ,
203+ buildID : data . buildId ,
204+ logsOffset : options ?. logsOffset ,
205+ } )
206+ }
207+
125208 fromDebianImage ( variant : string = 'stable' ) : TemplateBuilder {
126209 return this . fromImage ( `debian:${ variant } ` )
127210 }
@@ -768,16 +851,14 @@ export class TemplateBase
768851 /**
769852 * Internal implementation of the template build process.
770853 *
854+ * @param client API client for communicating with E2B backend
771855 * @param options Build configuration options
772856 * @throws BuildError if the build fails
773857 */
774- private async build ( options : BuildOptions ) : Promise < void > {
775- const config = new ConnectionConfig ( {
776- domain : options . domain ,
777- apiKey : options . apiKey ,
778- } )
779- const client = new ApiClient ( config )
780-
858+ private async build (
859+ client : ApiClient ,
860+ options : BuildOptions
861+ ) : Promise < BuildInfo > {
781862 if ( options . skipCache ) {
782863 this . force = true
783864 }
@@ -880,17 +961,11 @@ export class TemplateBase
880961 template : this . serialize ( instructionsWithHashes ) ,
881962 } )
882963
883- options . onBuildLogs ?.(
884- new LogEntry ( new Date ( ) , 'info' , 'Waiting for logs...' )
885- )
886-
887- await waitForBuildFinish ( client , {
888- templateID,
889- buildID,
890- onBuildLogs : options . onBuildLogs ,
891- logsRefreshFrequency : this . logsRefreshFrequency ,
892- stackTraces : this . stackTraces ,
893- } )
964+ return {
965+ alias : options . alias ,
966+ templateId : templateID ,
967+ buildId : buildID ,
968+ }
894969 }
895970
896971 /**
@@ -989,12 +1064,16 @@ export function Template(options?: TemplateOptions): TemplateFromImage {
9891064}
9901065
9911066Template . build = TemplateBase . build
1067+ Template . buildInBackground = TemplateBase . buildInBackground
1068+ Template . getBuildStatus = TemplateBase . getBuildStatus
9921069Template . toJSON = TemplateBase . toJSON
9931070Template . toDockerfile = TemplateBase . toDockerfile
9941071
9951072export type {
1073+ BuildInfo ,
9961074 BuildOptions ,
9971075 CopyItem ,
1076+ GetBuildStatusOptions ,
9981077 McpServerName ,
9991078 TemplateBuilder ,
10001079 TemplateClass ,
0 commit comments