-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-wp-codebox-cli-command.php
More file actions
351 lines (303 loc) · 13.5 KB
/
Copy pathclass-wp-codebox-cli-command.php
File metadata and controls
351 lines (303 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* WP-CLI adapter for WP Codebox public API operations.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
final class WP_Codebox_CLI_Command {
/** Register focused `wp codebox ...` commands. */
public static function register(): void {
$command = new self();
\WP_CLI::add_command( 'codebox artifacts list', array( $command, 'artifacts_list' ) );
\WP_CLI::add_command( 'codebox artifacts get', array( $command, 'artifacts_get' ) );
\WP_CLI::add_command( 'codebox artifacts inspect', array( $command, 'artifacts_inspect' ) );
\WP_CLI::add_command( 'codebox artifacts preflight-apply', array( $command, 'artifacts_preflight_apply' ) );
\WP_CLI::add_command( 'codebox artifacts stage-apply', array( $command, 'artifacts_stage_apply' ) );
\WP_CLI::add_command( 'codebox artifacts apply', array( $command, 'artifacts_apply' ) );
\WP_CLI::add_command( 'codebox runtime descriptor', array( $command, 'runtime_descriptor' ) );
\WP_CLI::add_command( 'codebox browser-session create', array( $command, 'browser_session_create' ) );
\WP_CLI::add_command( 'codebox run-agent-task', array( $command, 'run_agent_task' ) );
\WP_CLI::add_command( 'codebox run-agent-task-batch', array( $command, 'run_agent_task_batch' ) );
\WP_CLI::add_command( 'codebox run-agent-task-fanout', array( $command, 'run_agent_task_fanout' ) );
\WP_CLI::add_command( 'codebox run-runtime-task', array( $command, 'run_runtime_task' ) );
\WP_CLI::add_command( 'codebox run-wordpress-workload', array( $command, 'run_wordpress_workload' ) );
\WP_CLI::add_command( 'codebox run-runtime-package', array( $command, 'run_runtime_package' ) );
\WP_CLI::add_command( 'codebox resolve-runtime-requirements', array( $command, 'resolve_runtime_requirements' ) );
\WP_CLI::add_command( 'codebox wordpress-fuzz-runtime-contract', array( $command, 'wordpress_fuzz_runtime_contract' ) );
\WP_CLI::add_command( 'codebox run-fuzz-suite', array( $command, 'run_fuzz_suite' ) );
}
/**
* List artifact bundles.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function artifacts_list( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::list_artifacts( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Read one artifact bundle.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function artifacts_get( array $args, array $assoc_args ): void {
$this->emit( WP_Codebox_API::get_artifact( $this->artifact_input( $args, $assoc_args ) ), $assoc_args );
}
/**
* Inspect one artifact bundle and its verification payload.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function artifacts_inspect( array $args, array $assoc_args ): void {
$this->emit( WP_Codebox_API::inspect_artifact( $this->artifact_input( $args, $assoc_args ) ), $assoc_args );
}
/**
* Preflight a reviewed artifact apply without mutating the parent control plane.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function artifacts_preflight_apply( array $args, array $assoc_args ): void {
$this->emit( WP_Codebox_API::preflight_artifact_apply( $this->apply_input( $args, $assoc_args ) ), $assoc_args );
}
/**
* Stage a reviewed artifact apply through the host approval adapter.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function artifacts_stage_apply( array $args, array $assoc_args ): void {
$this->emit( WP_Codebox_API::stage_artifact_apply( $this->apply_input( $args, $assoc_args ) ), $assoc_args );
}
/**
* Apply a reviewed artifact through the configured apply-back adapter.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function artifacts_apply( array $args, array $assoc_args ): void {
$this->emit( WP_Codebox_API::apply_approved_artifact( $this->apply_input( $args, $assoc_args ) ), $assoc_args );
}
/**
* Print public runtime readiness, capabilities, ability names, and contract manifest.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function runtime_descriptor( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::runtime_descriptor(), $assoc_args );
}
/**
* Create a browser-executed Playground session payload.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function browser_session_create( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::create_browser_session( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Run a bounded task inside an isolated WP Codebox agent sandbox.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function run_agent_task( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::run_agent_task( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Run multiple bounded tasks sequentially through the public API facade.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function run_agent_task_batch( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::run_agent_task_batch( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Run multiple workers with bounded WP Codebox host fanout.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function run_agent_task_fanout( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::run_agent_task_fanout( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Run a public runtime task through the public API facade.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function run_runtime_task( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::run_runtime_task( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Run a safe WordPress workload through the public API facade.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function run_wordpress_workload( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::run_wordpress_workload( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Run a runtime package through the public API facade.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function run_runtime_package( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::run_runtime_package( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Resolve runtime/provider readiness through the public API facade.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function resolve_runtime_requirements( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::resolve_runtime_requirements( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* Print the public WordPress fuzz runtime descriptor contract.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function wordpress_fuzz_runtime_contract( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::wordpress_fuzz_runtime_contract(), $assoc_args );
}
/**
* Run a public fuzz suite through the public API facade.
*
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
*/
public function run_fuzz_suite( array $args, array $assoc_args ): void {
unset( $args );
$this->emit( WP_Codebox_API::run_fuzz_suite( $this->input_from_args( $assoc_args ) ), $assoc_args );
}
/**
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
* @return array<string,mixed>
*/
private function artifact_input( array $args, array $assoc_args ): array {
$input = $this->input_from_args( $assoc_args );
$input['artifact_id'] = (string) ( $args[0] ?? $input['artifact_id'] ?? '' );
return $input;
}
/**
* @param array<int,string> $args Positional arguments.
* @param array<string,mixed> $assoc_args Associated arguments.
* @return array<string,mixed>
*/
private function apply_input( array $args, array $assoc_args ): array {
$input = $this->artifact_input( $args, $assoc_args );
if ( isset( $assoc_args['approved-files'] ) ) {
$input['approved_files'] = $this->string_list( $assoc_args['approved-files'] );
}
return $input;
}
/**
* @param array<string,mixed> $assoc_args Associated arguments.
* @return array<string,mixed>
*/
private function input_from_args( array $assoc_args ): array {
$input = array();
if ( isset( $assoc_args['input-file'] ) ) {
$input = array_merge( $input, $this->json_file( (string) $assoc_args['input-file'] ) );
}
if ( isset( $assoc_args['input-json'] ) ) {
$input = array_merge( $input, $this->json_object( (string) $assoc_args['input-json'], 'input-json' ) );
}
foreach ( $assoc_args as $key => $value ) {
if ( in_array( $key, array( 'format', 'input-file', 'input-json' ), true ) ) {
continue;
}
$field = str_replace( '-', '_', (string) $key );
$input[ $field ] = $this->normalize_value( $field, $value );
}
return $input;
}
private function normalize_value( string $field, mixed $value ): mixed {
if ( in_array( $field, array( 'target', 'tool_bridge', 'parent_tool_bridge', 'sandbox_tool_policy', 'policy', 'context', 'inherit', 'orchestrator', 'parent_request', 'runtime_task', 'runtime_env', 'playground', 'browser_runner', 'runtime', 'blueprint', 'apply_target', 'aggregator', 'suite', 'package', 'requirements', 'runner_capabilities', 'metadata' ), true ) ) {
return $this->json_object( (string) $value, $field );
}
if ( in_array( $field, array( 'allowed_tools', 'expected_artifacts', 'agent_bundles', 'provider_plugin_paths', 'secret_env', 'mounts', 'workspaces', 'runtime_stack_mounts', 'runtime_state_mounts', 'runtime_config_mounts', 'runtime_overlays', 'browser_plugins', 'artifact_files', 'approved_files', 'workers', 'dependencies', 'tasks', 'cases', 'coverage', 'capabilities' ), true ) ) {
return $this->json_or_list( (string) $value, $field );
}
if ( in_array( $field, array( 'max_turns', 'task_timeout_seconds', 'preview_hold_seconds', 'preview_port', 'agent_id', 'user_id', 'concurrency' ), true ) ) {
return (int) $value;
}
return $value;
}
/** @return array<string,mixed> */
private function json_file( string $path ): array {
$contents = is_readable( $path ) ? file_get_contents( $path ) : false;
if ( false === $contents ) {
\WP_CLI::error( 'Input file is not readable: ' . $path );
}
return $this->json_object( (string) $contents, 'input-file' );
}
/** @return array<string,mixed> */
private function json_object( string $json, string $field ): array {
$decoded = WP_Codebox_Json::decode_object( $json );
if ( null === $decoded ) {
\WP_CLI::error( $field . ' must be a JSON object.' );
}
return $decoded;
}
/** @return array<int,mixed> */
private function json_or_list( string $value, string $field ): array {
$trimmed = trim( $value );
if ( str_starts_with( $trimmed, '[' ) ) {
$decoded = WP_Codebox_Json::decode_list( $trimmed );
if ( null === $decoded ) {
\WP_CLI::error( $field . ' must be a JSON array.' );
}
return $decoded;
}
return $this->string_list( $value );
}
/** @return string[] */
private function string_list( mixed $value ): array {
$items = is_array( $value ) ? $value : explode( ',', (string) $value );
return array_values(
array_filter(
array_map( static fn( mixed $item ): string => trim( (string) $item ), $items ),
static fn( string $item ): bool => '' !== $item
)
);
}
/** @param array<string,mixed>|WP_Error $result Service result. */
private function emit( array|WP_Error $result, array $assoc_args ): void {
if ( is_wp_error( $result ) ) {
\WP_CLI::error( $result->get_error_message() );
}
$format = (string) ( $assoc_args['format'] ?? 'json' );
if ( 'json' !== $format ) {
\WP_CLI::warning( 'WP Codebox WP-CLI wrappers currently emit JSON; ignoring --format=' . $format . '.' );
}
\WP_CLI::line( $this->json_encode( $result ) );
}
/** @param array<string,mixed> $data Data to encode. */
private function json_encode( array $data ): string {
return WP_Codebox_Json::encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES, '{}' );
}
}