1
1
import { FragmentSchema } from '@/lib/schema'
2
2
import { ExecutionResultInterpreter , ExecutionResultWeb } from '@/lib/types'
3
- import { Sandbox , CodeInterpreter } from '@e2b/code-interpreter'
3
+ import { Sandbox } from '@e2b/code-interpreter'
4
4
5
5
const sandboxTimeout = 10 * 60 * 1000 // 10 minute in ms
6
6
@@ -15,76 +15,55 @@ export async function POST(req: Request) {
15
15
await req . json ( )
16
16
console . log ( 'fragment' , fragment )
17
17
console . log ( 'userID' , userID )
18
- console . log ( 'apiKey' , apiKey )
19
-
20
- let sbx : Sandbox | CodeInterpreter | undefined = undefined
18
+ // console.log('apiKey', apiKey)
21
19
22
20
// Create a interpreter or a sandbox
23
- if ( fragment . template === 'code-interpreter-multilang' ) {
24
- sbx = await CodeInterpreter . create ( {
25
- metadata : { template : fragment . template , userID : userID } ,
26
- timeoutMs : sandboxTimeout ,
27
- apiKey,
28
- } )
29
- console . log ( 'Created code interpreter' , sbx . sandboxID )
30
- } else {
31
- sbx = await Sandbox . create ( fragment . template , {
32
- metadata : { template : fragment . template , userID : userID } ,
33
- timeoutMs : sandboxTimeout ,
34
- apiKey,
35
- } )
36
- console . log ( 'Created sandbox' , sbx . sandboxID )
37
- }
21
+ const sbx = await Sandbox . create ( fragment . template , {
22
+ metadata : { template : fragment . template , userID : userID } ,
23
+ timeoutMs : sandboxTimeout ,
24
+ apiKey,
25
+ } )
38
26
39
27
// Install packages
40
28
if ( fragment . has_additional_dependencies ) {
41
- if ( sbx instanceof CodeInterpreter ) {
42
- await sbx . notebook . execCell ( fragment . install_dependencies_command )
43
- console . log (
44
- `Installed dependencies: ${ fragment . additional_dependencies . join ( ', ' ) } in code interpreter ${ sbx . sandboxID } ` ,
45
- )
46
- } else if ( sbx instanceof Sandbox ) {
47
- await sbx . commands . run ( fragment . install_dependencies_command )
48
- console . log (
49
- `Installed dependencies: ${ fragment . additional_dependencies . join ( ', ' ) } in sandbox ${ sbx . sandboxID } ` ,
50
- )
51
- }
29
+ await sbx . commands . run ( fragment . install_dependencies_command )
30
+ console . log (
31
+ `Installed dependencies: ${ fragment . additional_dependencies . join ( ', ' ) } in sandbox ${ sbx . sandboxId } ` ,
32
+ )
52
33
}
53
34
54
35
// Copy code to fs
55
36
if ( fragment . code && Array . isArray ( fragment . code ) ) {
56
37
fragment . code . forEach ( async ( file ) => {
57
38
await sbx . files . write ( file . file_path , file . file_content )
58
- console . log ( `Copied file to ${ file . file_path } in ${ sbx . sandboxID } ` )
39
+ console . log ( `Copied file to ${ file . file_path } in ${ sbx . sandboxId } ` )
59
40
} )
60
41
} else {
61
42
await sbx . files . write ( fragment . file_path , fragment . code )
62
- console . log ( `Copied file to ${ fragment . file_path } in ${ sbx . sandboxID } ` )
43
+ console . log ( `Copied file to ${ fragment . file_path } in ${ sbx . sandboxId } ` )
63
44
}
64
45
65
46
// Execute code or return a URL to the running sandbox
66
- if ( fragment . template === 'code-interpreter-multilang' ) {
67
- const result = await ( sbx as CodeInterpreter ) . notebook . execCell (
68
- fragment . code || '' ,
69
- )
70
- await ( sbx as CodeInterpreter ) . close ( )
47
+ if ( fragment . template === 'code-interpreter-v1' ) {
48
+ const { logs, error, results } = await sbx . runCode ( fragment . code || '' )
49
+
71
50
return new Response (
72
51
JSON . stringify ( {
73
- sbxId : sbx ?. sandboxID ,
52
+ sbxId : sbx ?. sandboxId ,
74
53
template : fragment . template ,
75
- stdout : result . logs . stdout ,
76
- stderr : result . logs . stderr ,
77
- runtimeError : result . error ,
78
- cellResults : result . results ,
54
+ stdout : logs . stdout ,
55
+ stderr : logs . stderr ,
56
+ runtimeError : error ,
57
+ cellResults : results ,
79
58
} as ExecutionResultInterpreter ) ,
80
59
)
81
- } else {
82
- return new Response (
83
- JSON . stringify ( {
84
- sbxId : sbx ?. sandboxID ,
85
- template : fragment . template ,
86
- url : `https://${ sbx ?. getHost ( fragment . port || 80 ) } ` ,
87
- } as ExecutionResultWeb ) ,
88
- )
89
60
}
61
+
62
+ return new Response (
63
+ JSON . stringify ( {
64
+ sbxId : sbx ?. sandboxId ,
65
+ template : fragment . template ,
66
+ url : `https://${ sbx ?. getHost ( fragment . port || 80 ) } ` ,
67
+ } as ExecutionResultWeb ) ,
68
+ )
90
69
}
0 commit comments