-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrait-wp-codebox-abilities-agents-api-executors.php
More file actions
66 lines (55 loc) · 2.5 KB
/
Copy pathtrait-wp-codebox-abilities-agents-api-executors.php
File metadata and controls
66 lines (55 loc) · 2.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
<?php
/**
* Internal task executor adapters for WP Codebox's Agents API facade.
*
* These adapters let the upstream task runtime call Codebox-owned execution
* targets. Consumer-facing integrations should use the wp-codebox/* abilities
* and schemas registered by WP_Codebox_Abilities instead of upstream names.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
trait WP_Codebox_Abilities_Agents_API_Executors {
private function register_agents_api_executor_adapters(): void {
WP_Codebox_Agents_API_Adapter::register_executor_adapters(
array( self::class, 'register_agents_api_executor_targets' ),
array( self::class, 'execute_agents_api_task' )
);
}
/** @param mixed $targets Existing executor targets. @return array<int|string,mixed> */
public static function register_agents_api_executor_targets( mixed $targets ): array {
$targets = is_array( $targets ) ? $targets : array();
foreach ( self::agents_api_executor_target_declarations() as $target ) {
$targets[ (string) $target['id'] ] = $target;
}
return $targets;
}
/** @param mixed $pre Existing dispatch result. @param mixed $request Generic task request. @param mixed $target Target id or declaration. @return mixed */
public static function execute_agents_api_task( mixed $pre, mixed $request, mixed $target = null ): mixed {
if ( null !== $pre ) {
return $pre;
}
$target_id = WP_Codebox_Agents_API_Adapter::executor_target_id( $target, $request );
if ( ! in_array( $target_id, array( WP_Codebox_Agents_API_Adapter::browser_executor_target_id(), WP_Codebox_Agents_API_Adapter::host_executor_target_id() ), true ) ) {
return $pre;
}
if ( ! is_array( $request ) ) {
return new WP_Error( 'wp_codebox_agents_api_task_request_invalid', 'Agents API task requests must be objects.', array( 'status' => 400 ) );
}
$input = WP_Codebox_Agents_API_Adapter::task_request_input( $request );
return WP_Codebox_Agents_API_Adapter::browser_executor_target_id() === $target_id
? self::create_browser_task_contract( $input )
: self::run_agent_task( $input );
}
/** @return array<int,array<string,mixed>> */
private static function agents_api_executor_target_declarations(): array {
return WP_Codebox_Agents_API_Adapter::executor_target_declarations(
self::agents_api_task_input_schema(),
self::browser_task_contract_schema()
);
}
/** @return array<string,mixed> */
private static function agents_api_task_input_schema(): array {
return WP_Codebox_Agents_API_Adapter::task_input_schema( self::task_input_schema() );
}
}