File tree Expand file tree Collapse file tree 7 files changed +1143
-212
lines changed
apps/gateway/src/presenters
services/latitudeTools/runCode Expand file tree Collapse file tree 7 files changed +1143
-212
lines changed Original file line number Diff line number Diff line change @@ -40,14 +40,14 @@ export function documentPresenterWithProviderAndMetadata({
4040 const parameters =
4141 rawParams . length > 0
4242 ? rawParams . reduce (
43- ( acc , rawParam ) => {
44- if ( acc [ rawParam ] ) return acc
45- acc [ rawParam ] = { type : ParameterType . Text }
43+ ( acc , rawParam ) => {
44+ if ( acc [ rawParam ] ) return acc
45+ acc [ rawParam ] = { type : ParameterType . Text }
4646
47- return acc
48- } ,
49- { ...configParams } ,
50- )
47+ return acc
48+ } ,
49+ { ...configParams } ,
50+ )
5151 : configParams
5252
5353 return {
Original file line number Diff line number Diff line change 144144 "@aws-sdk/client-s3" : " 3.850.0" ,
145145 "@aws-sdk/s3-request-presigner" : " 3.850.0" ,
146146 "@google-cloud/storage" : " ^7.7.0" ,
147- "@codesandbox/sdk" : " 0.6.2 " ,
147+ "@codesandbox/sdk" : " 2.2.1 " ,
148148 "@kubernetes/client-node" : " 1.0.0" ,
149149 "@latitude-data/compiler" : " workspace:^" ,
150150 "@latitude-data/constants" : " workspace:*" ,
Original file line number Diff line number Diff line change @@ -18,8 +18,8 @@ async function createSandbox() {
1818 return Result . error ( new BadRequestError ( 'CODESANDBOX_API_KEY is not set' ) )
1919 }
2020 const sdk = new CodeSandbox ( env . CODESANDBOX_API_KEY )
21- const sandbox = await sdk . sandbox . create ( )
22- return Result . ok ( sandbox )
21+ const sandbox = await sdk . sandboxes . create ( )
22+ return Result . ok ( { sandbox, sdk } )
2323 } catch ( error ) {
2424 return Result . error ( error as LatitudeError )
2525 }
@@ -34,7 +34,7 @@ export const withSafeSandbox = async <
3434) : Promise < T > => {
3535 const sandboxResult = await createSandbox ( )
3636 if ( sandboxResult . error ) throw sandboxResult . error
37- const sandbox = sandboxResult . unwrap ( )
37+ const { sandbox, sdk } = sandboxResult . unwrap ( )
3838
3939 const timeoutPromise = new Promise < T > ( ( resolve ) => {
4040 setTimeout ( ( ) => {
@@ -51,6 +51,6 @@ export const withSafeSandbox = async <
5151 } catch ( error ) {
5252 return Result . error ( error as LatitudeError ) as T
5353 } finally {
54- sandbox . shutdown ( )
54+ await sdk . sandboxes . shutdown ( sandbox . id )
5555 }
5656}
Original file line number Diff line number Diff line change @@ -35,25 +35,24 @@ export async function runCodeWithDependencies({
3535 dependencies,
3636} : CodeToolArgs ) : PromisedResult < CodeRunResult , LatitudeError > {
3737 return withSafeSandbox ( async ( sandbox ) => {
38+ const client = await sandbox . connect ( )
3839 const filename = getFilename ( { language } )
3940 const buildDependency = getDependencyBuilder ( { language } )
4041
41- await sandbox . fs . writeFile ( filename , new TextEncoder ( ) . encode ( code ) )
42+ await client . fs . writeFile ( filename , new TextEncoder ( ) . encode ( code ) )
4243
4344 for await ( const dep of dependencies ! ) {
44- const installResult = await sandbox . shells . run ( buildDependency ( dep ) )
45- if ( installResult . exitCode !== 0 ) {
45+ const installResult = await client . commands . run ( buildDependency ( dep ) )
46+ if ( ! installResult ) {
4647 return Result . error (
47- new BadRequestError (
48- `Failed to install dependency: '${ dep } '\n${ installResult . output } ` ,
49- ) ,
48+ new BadRequestError ( `Failed to install dependency: '${ dep } '` ) ,
5049 )
5150 }
5251 }
5352
54- const scriptResult = await sandbox . shells . run (
53+ const scriptResult = await client . commands . run (
5554 getRunCommand ( { language, filename } ) ,
5655 )
57- return Result . ok ( normalizedResult ( scriptResult ) )
56+ return Result . ok ( normalizedResult ( { output : scriptResult , exitCode : 0 } ) )
5857 } )
5958}
Original file line number Diff line number Diff line change @@ -9,14 +9,15 @@ export async function runCodeWithoutDependencies({
99 language,
1010} : CodeToolArgs ) : PromisedResult < CodeRunResult , LatitudeError > {
1111 return withSafeSandbox ( async ( sandbox ) => {
12+ const client = await sandbox . connect ( )
1213 const interpreter = ( ( ) => {
13- if ( language === 'python' ) return sandbox . shells . python
14- if ( language === 'javascript' ) return sandbox . shells . js
14+ if ( language === 'python' ) return client . interpreters . python
15+ if ( language === 'javascript' ) return client . interpreters . javascript
1516
1617 throw new BadRequestError ( `Language ${ language } is not supported` )
1718 } ) ( )
1819
19- const result = await interpreter . run ( code )
20- return Result . ok ( normalizedResult ( result ) )
20+ const result = await interpreter ( code )
21+ return Result . ok ( normalizedResult ( { output : result , exitCode : 0 } ) )
2122 } )
2223}
Original file line number Diff line number Diff line change @@ -66,8 +66,8 @@ async function generateProviderLogs({
6666 typeof message . content === 'string'
6767 ? message . content
6868 : message . content
69- . map ( ( c ) => ( c . type === 'text' ? c . text : '' ) )
70- . join ( '' )
69+ . map ( ( c ) => ( c . type === 'text' ? c . text : '' ) )
70+ . join ( '' )
7171 return acc + content . length
7272 } , 0 )
7373 const completionTokens = mockedResponse . length
@@ -133,7 +133,7 @@ export async function createDocumentLog({
133133 const duration =
134134 totalDuration ??
135135 Math . floor ( Math . random ( ) * 100 ) +
136- providerLogs . reduce ( ( acc , log ) => acc + ( log ?. duration ?? 0 ) , 0 )
136+ providerLogs . reduce ( ( acc , log ) => acc + ( log ?. duration ?? 0 ) , 0 )
137137
138138 let documentLog = await ogCreateDocumentLog ( {
139139 commit,
You can’t perform that action at this time.
0 commit comments