|
13 | 13 |
|
14 | 14 | defined('ABSPATH') || exit; |
15 | 15 |
|
16 | | -class CodeTaskAbilities |
17 | | -{ |
| 16 | +class CodeTaskAbilities { |
18 | 17 |
|
19 | | - private static bool $registered = false; |
20 | 18 |
|
21 | | - public function __construct() |
22 | | - { |
23 | | - if (self::$registered ) { |
24 | | - return; |
25 | | - } |
26 | 19 |
|
27 | | - if (! function_exists('wp_register_ability') ) { |
28 | | - add_action( |
29 | | - 'wp_abilities_api_init', function (): void { |
30 | | - if (self::$registered || ! function_exists('wp_register_ability') ) { |
31 | | - return; |
32 | | - } |
| 20 | + private static bool $registered = false; |
33 | 21 |
|
34 | | - $this->register(); |
35 | | - self::$registered = true; |
36 | | - } |
37 | | - ); |
38 | | - return; |
39 | | - } |
| 22 | + public function __construct() { |
| 23 | + if ( self::$registered ) { |
| 24 | + return; |
| 25 | + } |
40 | 26 |
|
41 | | - if (function_exists('doing_action') && doing_action('wp_abilities_api_init') ) { |
42 | | - $this->register(); |
43 | | - } else { |
44 | | - add_action('wp_abilities_api_init', array( $this, 'register' )); |
45 | | - } |
46 | | - self::$registered = true; |
47 | | - } |
| 27 | + if ( ! function_exists('wp_register_ability') ) { |
| 28 | + add_action( |
| 29 | + 'wp_abilities_api_init', function (): void { |
| 30 | + if ( self::$registered || ! function_exists('wp_register_ability') ) { |
| 31 | + return; |
| 32 | + } |
48 | 33 |
|
49 | | - public function register(): void |
50 | | - { |
51 | | - wp_register_ability( |
52 | | - 'datamachine/create-code-task', |
53 | | - array( |
54 | | - 'label' => 'Create Code Task', |
55 | | - 'description' => 'Create an isolated workspace worktree from a structured evidence packet.', |
56 | | - 'category' => 'datamachine-code-code-task', |
57 | | - 'input_schema' => self::input_schema(), |
58 | | - 'output_schema' => array( |
59 | | - 'type' => 'object', |
60 | | - 'properties' => array( |
61 | | - 'success' => array( 'type' => 'boolean' ), |
62 | | - 'repo' => array( 'type' => 'string' ), |
63 | | - 'branch' => array( 'type' => 'string' ), |
64 | | - 'handle' => array( 'type' => 'string' ), |
65 | | - 'path' => array( 'type' => 'string' ), |
66 | | - 'prompt_path' => array( 'type' => 'string' ), |
67 | | - 'packet_path' => array( 'type' => 'string' ), |
68 | | - 'source_url' => array( 'type' => 'string' ), |
69 | | - ), |
70 | | - ), |
71 | | - 'execute_callback' => array( self::class, 'create' ), |
72 | | - 'permission_callback' => fn() => PermissionHelper::can_manage(), |
73 | | - 'meta' => array( 'show_in_rest' => false ), |
74 | | - ) |
75 | | - ); |
76 | | - } |
| 34 | + $this->register(); |
| 35 | + self::$registered = true; |
| 36 | + } |
| 37 | + ); |
| 38 | + return; |
| 39 | + } |
77 | 40 |
|
78 | | - /** |
79 | | - * @return array<string,mixed> |
80 | | - */ |
81 | | - public static function input_schema(): array |
82 | | - { |
83 | | - return array( |
84 | | - 'type' => 'object', |
85 | | - 'required' => array( 'packet' ), |
86 | | - 'properties' => array( |
87 | | - 'packet' => array( |
88 | | - 'type' => 'object', |
89 | | - 'description' => 'Structured evidence packet describing the source evidence and target repository.', |
90 | | - ), |
91 | | - 'branch' => array( |
92 | | - 'type' => 'string', |
93 | | - 'description' => 'Optional explicit branch name. Defaults to deterministic code-task/<source>-<title>-<hash>.', |
94 | | - ), |
95 | | - 'base_ref' => array( |
96 | | - 'type' => 'string', |
97 | | - 'description' => 'Optional base ref for the worktree. Defaults to origin/main.', |
98 | | - ), |
99 | | - 'allow_stale' => array( 'type' => 'boolean' ), |
100 | | - 'force' => array( 'type' => 'boolean' ), |
101 | | - ), |
102 | | - ); |
103 | | - } |
| 41 | + if ( function_exists('doing_action') && doing_action('wp_abilities_api_init') ) { |
| 42 | + $this->register(); |
| 43 | + } else { |
| 44 | + add_action('wp_abilities_api_init', array( $this, 'register' )); |
| 45 | + } |
| 46 | + self::$registered = true; |
| 47 | + } |
104 | 48 |
|
105 | | - /** |
106 | | - * @param array<string,mixed> $input Ability input. |
107 | | - * @return array<string,mixed>|\WP_Error |
108 | | - */ |
109 | | - public static function create( array $input ): array|\WP_Error |
110 | | - { |
111 | | - $packet = EvidencePacket::from_array($input['packet'] ?? null); |
112 | | - if ($packet instanceof \WP_Error ) { |
113 | | - return $packet; |
114 | | - } |
| 49 | + public function register(): void { |
| 50 | + wp_register_ability( |
| 51 | + 'datamachine/create-code-task', |
| 52 | + array( |
| 53 | + 'label' => 'Create Code Task', |
| 54 | + 'description' => 'Create an isolated workspace worktree from a structured evidence packet.', |
| 55 | + 'category' => 'datamachine-code-code-task', |
| 56 | + 'input_schema' => self::input_schema(), |
| 57 | + 'output_schema' => array( |
| 58 | + 'type' => 'object', |
| 59 | + 'properties' => array( |
| 60 | + 'success' => array( 'type' => 'boolean' ), |
| 61 | + 'repo' => array( 'type' => 'string' ), |
| 62 | + 'branch' => array( 'type' => 'string' ), |
| 63 | + 'handle' => array( 'type' => 'string' ), |
| 64 | + 'path' => array( 'type' => 'string' ), |
| 65 | + 'prompt_path' => array( 'type' => 'string' ), |
| 66 | + 'packet_path' => array( 'type' => 'string' ), |
| 67 | + 'source_url' => array( 'type' => 'string' ), |
| 68 | + ), |
| 69 | + ), |
| 70 | + 'execute_callback' => array( self::class, 'create' ), |
| 71 | + 'permission_callback' => fn() => PermissionHelper::can_manage(), |
| 72 | + 'meta' => array( 'show_in_rest' => false ), |
| 73 | + ) |
| 74 | + ); |
| 75 | + } |
115 | 76 |
|
116 | | - $creator = new CodeTaskCreator(); |
117 | | - return $creator->create($packet, $input); |
118 | | - } |
| 77 | + /** |
| 78 | + * @return array<string,mixed> |
| 79 | + */ |
| 80 | + public static function input_schema(): array { |
| 81 | + return array( |
| 82 | + 'type' => 'object', |
| 83 | + 'required' => array( 'packet' ), |
| 84 | + 'properties' => array( |
| 85 | + 'packet' => array( |
| 86 | + 'type' => 'object', |
| 87 | + 'description' => 'Structured evidence packet describing the source evidence and target repository.', |
| 88 | + ), |
| 89 | + 'branch' => array( |
| 90 | + 'type' => 'string', |
| 91 | + 'description' => 'Optional explicit branch name. Defaults to deterministic code-task/<source>-<title>-<hash>.', |
| 92 | + ), |
| 93 | + 'base_ref' => array( |
| 94 | + 'type' => 'string', |
| 95 | + 'description' => 'Optional base ref for the worktree. Defaults to origin/main.', |
| 96 | + ), |
| 97 | + 'allow_stale' => array( 'type' => 'boolean' ), |
| 98 | + 'force' => array( 'type' => 'boolean' ), |
| 99 | + ), |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param array<string,mixed> $input Ability input. |
| 105 | + * @return array<string,mixed>|\WP_Error |
| 106 | + */ |
| 107 | + public static function create( array $input ): array|\WP_Error { |
| 108 | + $packet = EvidencePacket::from_array($input['packet'] ?? null); |
| 109 | + if ( $packet instanceof \WP_Error ) { |
| 110 | + return $packet; |
| 111 | + } |
| 112 | + |
| 113 | + $creator = new CodeTaskCreator(); |
| 114 | + return $creator->create($packet, $input); |
| 115 | + } |
119 | 116 | } |
0 commit comments