@@ -47,7 +47,6 @@ import type {
4747import { isTool } from '../tools/defineTool.js' ;
4848import type { ToolDefinition } from '../tools/defineTool.js' ;
4949import { TOOL_AWAIT_RESULT , toolResultHelper } from '../tools/toolResult.js' ;
50- import { resolveExpression , resolveLiteral } from './utils/resolveExpression.js' ;
5150
5251type ExecuteAgentInputOptions < TInput extends AgentInputBase > = TInput extends EmptyObject
5352 ? {
@@ -759,6 +758,38 @@ async function runFunctionCall(
759758 throw new RuntimeError ( `Expression is not a function` ) ;
760759}
761760
761+ async function runNewExpression (
762+ ctx : ExecuteAgentContext ,
763+ closure : StackFrame ,
764+ parent : StackFrame ,
765+ index : number ,
766+ expr : NewExpression ,
767+ ) {
768+ const frame = getFrame ( parent , index , expr ) ;
769+
770+ if ( expr . func . type !== 'ident' ) {
771+ throw new RuntimeError ( 'Dynamic constructor calls are not supported' ) ;
772+ }
773+
774+ const constructor = ( globalThis as Record < string , unknown > ) [ expr . func . name ] as Constructor ;
775+ if ( typeof constructor !== 'function' ) {
776+ throw new RuntimeError ( `Expression is not a function` ) ;
777+ }
778+
779+ if ( ! ctx . allowedNatives . has ( constructor ) ) {
780+ throw new RuntimeError ( `Constructor ${ constructor . name } is not allowed` ) ;
781+ }
782+
783+ const args = await runExpressionArray ( ctx , closure , frame , 0 , expr . args ?? [ ] ) ;
784+
785+ if ( Array . isArray ( args ) ) {
786+ frame . value = new constructor ( ...args ) ;
787+ return updateFrame ( frame , 'done' ) ;
788+ }
789+
790+ return updateFrame ( frame , args ) ;
791+ }
792+
762793async function runArrayMap (
763794 ctx : ExecuteAgentContext ,
764795 frame : StackFrame ,
@@ -1242,34 +1273,6 @@ async function runTernaryExpression(
12421273 return updateFrame ( frame , 'done' ) ;
12431274}
12441275
1245- async function runNewExpression (
1246- ctx : ExecuteAgentContext ,
1247- closure : StackFrame ,
1248- parent : StackFrame ,
1249- index : number ,
1250- expr : NewExpression ,
1251- ) {
1252- const frame = getFrame ( parent , index , expr ) ;
1253-
1254- const constructor = resolveExpression ( ctx . agent , frame , expr . func ) as Constructor ;
1255- if ( typeof constructor !== 'function' ) {
1256- throw new RuntimeError ( `Expression is not a function` ) ;
1257- }
1258-
1259- if ( ! ctx . allowedNatives . has ( constructor ) ) {
1260- throw new RuntimeError ( `Constructor ${ constructor . name } is not allowed` ) ;
1261- }
1262-
1263- const args = await runExpressionArray ( ctx , closure , frame , 0 , expr . args ?? [ ] ) ;
1264-
1265- if ( Array . isArray ( args ) ) {
1266- frame . value = new constructor ( ...args ) ;
1267- return updateFrame ( frame , 'done' ) ;
1268- }
1269-
1270- return updateFrame ( frame , args ) ;
1271- }
1272-
12731276async function runTemplateLiteral (
12741277 ctx : ExecuteAgentContext ,
12751278 closure : StackFrame ,
@@ -1442,3 +1445,16 @@ function isSafeValue(value: unknown) {
14421445function isDone ( frame : StackFrame | StackFrameResult ) {
14431446 return frame . status === 'done' ;
14441447}
1448+
1449+ function resolveLiteral ( expression : LiteralExpression ) {
1450+ const value = expression . value ;
1451+ if ( ! value ) {
1452+ return value ;
1453+ }
1454+
1455+ if ( typeof value === 'object' ) {
1456+ return JSON . parse ( JSON . stringify ( value ) ) as unknown ;
1457+ }
1458+
1459+ return value ;
1460+ }
0 commit comments