|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Run-scoped runtime tool source. |
| 4 | + * |
| 5 | + * Adapts client/transport-declared runtime tool definitions into the normal |
| 6 | + * tool source pipeline. These tools are visible to model requests only after |
| 7 | + * the existing ToolPolicyResolver/Agents API policy pass allows them; execution |
| 8 | + * remains outside PHP because declarations are marked with a client executor. |
| 9 | + * |
| 10 | + * @package DataMachine\Engine\AI\Tools\Sources |
| 11 | + */ |
| 12 | + |
| 13 | +namespace DataMachine\Engine\AI\Tools\Sources; |
| 14 | + |
| 15 | +use AgentsAPI\AI\Tools\WP_Agent_Tool_Declaration; |
| 16 | + |
| 17 | +defined( 'ABSPATH' ) || exit; |
| 18 | + |
| 19 | +final class RuntimeToolSource { |
| 20 | + |
| 21 | + /** |
| 22 | + * Gather normalized runtime tool declarations from resolver context. |
| 23 | + * |
| 24 | + * @param array $modes Agent mode slugs. |
| 25 | + * @param array $args Full resolution arguments. |
| 26 | + * @return array Tools keyed by tool name. |
| 27 | + */ |
| 28 | + public function __invoke( array $modes, array $args = array() ): array { |
| 29 | + $tools = array(); |
| 30 | + |
| 31 | + foreach ( $this->declarationsFromContext( $args ) as $declaration ) { |
| 32 | + if ( ! is_array( $declaration ) ) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + $normalized = WP_Agent_Tool_Declaration::normalize( $declaration ); |
| 38 | + } catch ( \InvalidArgumentException ) { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + $name = (string) $normalized['name']; |
| 43 | + $tools[ $name ] = array_merge( |
| 44 | + $normalized, |
| 45 | + array( |
| 46 | + 'modes' => $modes, |
| 47 | + 'access_level' => 'public', |
| 48 | + 'runtime_tool' => true, |
| 49 | + 'external_executor' => true, |
| 50 | + 'requires_opt_in' => true, |
| 51 | + ) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + return $tools; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Extract declarations from explicit resolver args and nested client context. |
| 60 | + * |
| 61 | + * @param array $args Full resolution arguments. |
| 62 | + * @return array<int,array|string|mixed> Runtime declarations. |
| 63 | + */ |
| 64 | + private function declarationsFromContext( array $args ): array { |
| 65 | + $sets = array( |
| 66 | + $args['runtime_tool_declarations'] ?? null, |
| 67 | + $args['runtime_tools'] ?? null, |
| 68 | + ); |
| 69 | + |
| 70 | + $client_context = is_array( $args['client_context'] ?? null ) ? $args['client_context'] : array(); |
| 71 | + $sets[] = $client_context['runtime_tool_declarations'] ?? null; |
| 72 | + $sets[] = $client_context['runtime_tools'] ?? null; |
| 73 | + $sets[] = $client_context['tool_declarations'] ?? null; |
| 74 | + |
| 75 | + $declarations = array(); |
| 76 | + foreach ( $sets as $set ) { |
| 77 | + if ( ! is_array( $set ) ) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + foreach ( $set as $name => $declaration ) { |
| 82 | + if ( ! is_array( $declaration ) ) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + if ( is_string( $name ) && '' !== $name && empty( $declaration['name'] ) ) { |
| 87 | + $declaration['name'] = $name; |
| 88 | + } |
| 89 | + |
| 90 | + $declarations[] = $declaration; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return $declarations; |
| 95 | + } |
| 96 | +} |
0 commit comments