@@ -7,6 +7,10 @@ import ts from 'typescript';
77import { WorkerOutput } from './code-tool-types' ;
88import { Finch , ClientOptions } from '@tryfinch/finch-api' ;
99
10+ async function tseval ( code : string ) {
11+ return import ( 'data:application/typescript;charset=utf-8;base64,' + Buffer . from ( code ) . toString ( 'base64' ) ) ;
12+ }
13+
1014function getRunFunctionSource ( code : string ) : {
1115 type : 'declaration' | 'expression' ;
1216 client : string | undefined ;
@@ -283,7 +287,9 @@ const fetch = async (req: Request): Promise<Response> => {
283287
284288 const log_lines : string [ ] = [ ] ;
285289 const err_lines : string [ ] = [ ] ;
286- const console = {
290+ const originalConsole = globalThis . console ;
291+ globalThis . console = {
292+ ...originalConsole ,
287293 log : ( ...args : unknown [ ] ) => {
288294 log_lines . push ( util . format ( ...args ) ) ;
289295 } ,
@@ -293,7 +299,7 @@ const fetch = async (req: Request): Promise<Response> => {
293299 } ;
294300 try {
295301 let run_ = async ( client : any ) => { } ;
296- eval ( `${ code } \nrun_ = run;` ) ;
302+ run_ = ( await tseval ( `${ code } \nexport default run;` ) ) . default ;
297303 const result = await run_ ( makeSdkProxy ( client , { path : [ 'client' ] } ) ) ;
298304 return Response . json ( {
299305 is_error : false ,
@@ -311,6 +317,8 @@ const fetch = async (req: Request): Promise<Response> => {
311317 } satisfies WorkerOutput ,
312318 { status : 400 , statusText : 'Code execution error' } ,
313319 ) ;
320+ } finally {
321+ globalThis . console = originalConsole ;
314322 }
315323} ;
316324
0 commit comments