@@ -124,12 +124,39 @@ private function execute_workflow( array $task ): array|WP_Error {
124124 $ input ['message ' ] = $ this ->string_value ( $ input ['prompt ' ] ?? '' );
125125 }
126126 $ input ['runtime_package_task ' ] = $ task ;
127+ $ input ['run_id ' ] = $ this ->runtime_run_id ( $ input );
128+ $ workspace_context = $ this ->sandbox_workspace_context ( $ input );
127129 $ ability_object = function_exists ( 'wp_get_ability ' ) ? wp_get_ability ( $ ability ) : null ;
128130 if ( ! is_object ( $ ability_object ) || ! method_exists ( $ ability_object , 'execute ' ) ) {
129131 return new WP_Error ( 'wp_codebox_runtime_package_workflow_unavailable ' , 'Runtime package workflow ability is unavailable. ' , array ( 'status ' => 500 , 'ability ' => $ ability ) );
130132 }
131133
132- $ result = $ ability_object ->execute ( $ input );
134+ $ runtime_tool_declarations = static function ( array $ declarations , $ agent , array $ runtime_context ) use ( $ input , $ workspace_context ): array {
135+ if ( ! $ agent instanceof \WP_Agent || ! hash_equals ( (string ) $ input ['agent ' ], (string ) $ runtime_context ['agent_slug ' ] ) || ! hash_equals ( (string ) $ input ['run_id ' ], (string ) $ runtime_context ['run_id ' ] ) ) {
136+ return $ declarations ;
137+ }
138+ return array_merge ( $ declarations , WP_Codebox_Sandbox_Workspace_Executor::tool_declarations_for_enabled_tools ( $ agent ->get_default_config (), $ workspace_context ) );
139+ };
140+ $ tool_executors = static function ( array $ executors , array $ runtime_context ) use ( $ input , $ workspace_context ): array {
141+ if ( ! hash_equals ( (string ) $ input ['agent ' ], (string ) ( $ runtime_context ['agent_slug ' ] ?? '' ) ) || ! hash_equals ( (string ) $ input ['run_id ' ], (string ) ( $ runtime_context ['run_id ' ] ?? '' ) ) ) {
142+ return $ executors ;
143+ }
144+ $ executors [ WP_Codebox_Sandbox_Workspace_Executor::TARGET_ID ] = WP_Codebox_Sandbox_Workspace_Executor::executor_for_context ( $ workspace_context );
145+ return $ executors ;
146+ };
147+ $ filters_available = function_exists ( 'add_filter ' ) && function_exists ( 'remove_filter ' );
148+ if ( $ filters_available ) {
149+ add_filter ( 'agents_api_runtime_tool_declarations ' , $ runtime_tool_declarations , 20 , 3 );
150+ add_filter ( 'agents_api_tool_executors ' , $ tool_executors , 20 , 2 );
151+ }
152+ try {
153+ $ result = $ ability_object ->execute ( $ input );
154+ } finally {
155+ if ( $ filters_available ) {
156+ remove_filter ( 'agents_api_runtime_tool_declarations ' , $ runtime_tool_declarations , 20 );
157+ remove_filter ( 'agents_api_tool_executors ' , $ tool_executors , 20 );
158+ }
159+ }
133160 if ( is_wp_error ( $ result ) ) {
134161 return $ result ;
135162 }
@@ -148,6 +175,19 @@ private function execute_workflow( array $task ): array|WP_Error {
148175 return $ result ;
149176 }
150177
178+ /** @param array<string,mixed> $input @return array<string,mixed> */
179+ private function sandbox_workspace_context ( array $ input ): array {
180+ $ policy = is_array ( $ input ['runner_workspace_policy ' ] ?? null ) ? $ input ['runner_workspace_policy ' ] : array ();
181+ $ paths = is_array ( $ policy ['writable_paths ' ] ?? null ) ? $ policy ['writable_paths ' ] : array ();
182+ return array ( 'workspace_root ' => '/workspace ' , 'writable_paths ' => $ paths );
183+ }
184+
185+ /** @param array<string,mixed> $input */
186+ private function runtime_run_id ( array $ input ): string {
187+ $ run_id = $ this ->string_value ( $ input ['run_id ' ] ?? '' );
188+ return '' !== $ run_id ? $ run_id : 'wp-codebox-runtime- ' . ( function_exists ( 'wp_generate_uuid4 ' ) ? wp_generate_uuid4 () : uniqid ( '' , true ) );
189+ }
190+
151191 /** @param array<string,mixed> $task Runtime package task. @param array<int,array<string,mixed>> $imports Import results. @return string|WP_Error */
152192 private function imported_agent_slug ( array $ task , array $ imports ): string |WP_Error {
153193 $ package = is_array ( $ task ['package ' ] ?? null ) ? $ task ['package ' ] : array ();
0 commit comments