@@ -12,7 +12,6 @@ import {
1212 resolveRefs ,
1313 groupStepsByLevel ,
1414} from "../types/workflow.ts" ;
15- import { loadWorkflow , listWorkflows } from "./workflow-studio-adapter.ts" ;
1615import { getAllLocalTools } from "../tools/index.ts" ;
1716
1817// ============================================================================
@@ -52,6 +51,8 @@ export interface ExecutorConfig {
5251 fastModel : string ;
5352 smartModel : string ;
5453 onProgress ?: ( stepName : string , message : string ) => void ;
54+ loadWorkflow ?: ( workflowId : string ) => Promise < Workflow | null > ;
55+ listWorkflows ?: ( ) => Promise < Workflow [ ] > ;
5556}
5657
5758export interface ExecutionContext {
@@ -64,6 +65,8 @@ export interface ExecutionContext {
6465 listConnections : ListConnectionsCallback ;
6566 publishEvent ?: ( type : string , data : Record < string , unknown > ) => Promise < void > ;
6667 toolCache : Map < string , ToolDefinition > ;
68+ loadWorkflow ?: ( workflowId : string ) => Promise < Workflow | null > ;
69+ listWorkflows ?: ( ) => Promise < Workflow [ ] > ;
6770}
6871
6972export interface WorkflowResult {
@@ -92,9 +95,17 @@ export async function runWorkflow(
9295 type : string ,
9396 data : Record < string , unknown > ,
9497 ) => Promise < void > ;
98+ loadWorkflow ?: ( workflowId : string ) => Promise < Workflow | null > ;
9599 } ,
96100) : Promise < WorkflowResult > {
97- const workflow = await loadWorkflow ( workflowId ) ;
101+ if ( ! options . loadWorkflow ) {
102+ return {
103+ success : false ,
104+ error : "loadWorkflow callback not provided" ,
105+ } ;
106+ }
107+
108+ const workflow = await options . loadWorkflow ( workflowId ) ;
98109 if ( ! workflow ) {
99110 return { success : false , error : `Workflow not found: ${ workflowId } ` } ;
100111 }
@@ -117,6 +128,8 @@ export async function runWorkflowDirect(
117128 type : string ,
118129 data : Record < string , unknown > ,
119130 ) => Promise < void > ;
131+ loadWorkflow ?: ( workflowId : string ) => Promise < Workflow | null > ;
132+ listWorkflows ?: ( ) => Promise < Workflow [ ] > ;
120133 } ,
121134) : Promise < WorkflowResult > {
122135 const ctx : ExecutionContext = {
@@ -129,6 +142,8 @@ export async function runWorkflowDirect(
129142 listConnections : options . listConnections ,
130143 publishEvent : options . publishEvent ,
131144 toolCache : new Map ( ) ,
145+ loadWorkflow : options . loadWorkflow ,
146+ listWorkflows : options . listWorkflows ,
132147 } ;
133148
134149 try {
@@ -266,6 +281,10 @@ async function executeCodeStep(
266281
267282 const code = step . action . code ;
268283
284+ // SECURITY WARNING: Using new Function() is equivalent to eval() and provides
285+ // full Node.js access without sandboxing. This should only be used with trusted workflows.
286+ // Consider using a sandboxed execution environment (vm2, isolated-vm, or worker threads)
287+ // for untrusted code execution in the future.
269288 try {
270289 const fn = new Function (
271290 "input" ,
@@ -585,7 +604,10 @@ async function executeToolCall(
585604) : Promise < unknown > {
586605 // Meta tools
587606 if ( toolName === "list_workflows" ) {
588- const workflows = await listWorkflows ( ) ;
607+ if ( ! ctx . listWorkflows ) {
608+ throw new Error ( "listWorkflows callback not provided" ) ;
609+ }
610+ const workflows = await ctx . listWorkflows ( ) ;
589611 return {
590612 workflows : workflows . map ( ( w ) => ( {
591613 id : w . id ,
@@ -596,6 +618,9 @@ async function executeToolCall(
596618 }
597619
598620 if ( toolName === "start_workflow" ) {
621+ if ( ! ctx . loadWorkflow ) {
622+ throw new Error ( "loadWorkflow callback not provided" ) ;
623+ }
599624 const workflowId = args . workflowId as string ;
600625 const workflowInput = ( args . input as Record < string , unknown > ) || { } ;
601626 const result = await runWorkflow ( workflowId , workflowInput , {
@@ -604,6 +629,7 @@ async function executeToolCall(
604629 callMeshTool : ctx . callMeshTool ,
605630 listConnections : ctx . listConnections ,
606631 publishEvent : ctx . publishEvent ,
632+ loadWorkflow : ctx . loadWorkflow ,
607633 } ) ;
608634 return result ;
609635 }
0 commit comments