-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-runtime-tool-request.php
More file actions
108 lines (95 loc) · 4.02 KB
/
Copy pathclass-wp-agent-runtime-tool-request.php
File metadata and controls
108 lines (95 loc) · 4.02 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
<?php
/**
* External runtime tool pending request contract.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI;
defined( 'ABSPATH' ) || exit;
/**
* Normalizes pending external/client tool requests.
*/
class WP_Agent_Runtime_Tool_Request {
public const STATUS_PENDING = 'runtime_tool_pending';
public const STATUS_COMPLETED = 'runtime_tool_completed';
public const STATUS_TIMEOUT = 'runtime_tool_timeout';
/**
* Normalize a pending runtime tool request.
*
* @param array<string, mixed> $request Raw request payload.
* @return array<string, mixed> Normalized request payload.
*/
public static function normalize( array $request ): array {
$tool_name = $request['tool_name'] ?? '';
if ( ! is_string( $tool_name ) || '' === trim( $tool_name ) ) {
throw new \InvalidArgumentException( 'invalid_runtime_tool_request: tool_name must be a non-empty string' );
}
$tool_call_id = $request['tool_call_id'] ?? '';
if ( ! is_string( $tool_call_id ) || '' === trim( $tool_call_id ) ) {
throw new \InvalidArgumentException( 'invalid_runtime_tool_request: tool_call_id must be a non-empty string' );
}
$request_id = $request['request_id'] ?? '';
if ( ! is_string( $request_id ) || '' === trim( $request_id ) ) {
$request_id = self::request_id( $tool_name, $tool_call_id, $request['run_id'] ?? '' );
}
$parameters = $request['parameters'] ?? array();
$runtime = $request['runtime'] ?? array();
$metadata = $request['metadata'] ?? array();
return array(
'status' => self::STATUS_PENDING,
'request_id' => $request_id,
'tool_name' => trim( $tool_name ),
'tool_call_id' => trim( $tool_call_id ),
'parameters' => is_array( $parameters ) ? $parameters : array(),
'run_id' => is_string( $request['run_id'] ?? null ) ? $request['run_id'] : '',
'timeout_at' => is_string( $request['timeout_at'] ?? null ) ? $request['timeout_at'] : '',
'runtime' => is_array( $runtime ) ? $runtime : array(),
'metadata' => is_array( $metadata ) ? $metadata : array(),
);
}
/**
* Build a pending request from a loop-mediated tool call.
*
* @param string $tool_name Tool identifier.
* @param string $tool_call_id Tool call identifier.
* @param array<string, mixed> $parameters Tool parameters.
* @param array<string, mixed> $context Turn context.
* @param array<string, mixed> $runtime Runtime metadata.
* @param array<string, mixed> $metadata Host metadata.
* @return array<string, mixed> Normalized request payload.
*/
public static function from_tool_call( string $tool_name, string $tool_call_id, array $parameters, array $context = array(), array $runtime = array(), array $metadata = array() ): array {
return self::normalize( array(
'tool_name' => $tool_name,
'tool_call_id' => $tool_call_id,
'parameters' => $parameters,
'run_id' => is_string( $context['run_id'] ?? null ) ? $context['run_id'] : '',
'timeout_at' => is_string( $context['runtime_tool_timeout_at'] ?? null ) ? $context['runtime_tool_timeout_at'] : '',
'runtime' => $runtime,
'metadata' => $metadata,
) );
}
/**
* Mark a normalized request as timed out without changing its identity.
*
* @param array<string, mixed> $request Raw or normalized runtime tool request.
* @return array<string, mixed> Timeout request payload.
*/
public static function timeout( array $request ): array {
$normalized = self::normalize( $request );
$normalized['status'] = self::STATUS_TIMEOUT;
return $normalized;
}
/**
* Build a stable request id when the host did not supply one.
*
* @param string $tool_name Tool name.
* @param string $tool_call_id Tool call id.
* @param mixed $run_id Optional run id.
* @return string Request id.
*/
private static function request_id( string $tool_name, string $tool_call_id, $run_id ): string {
$parts = array_filter( array( is_string( $run_id ) ? $run_id : '', $tool_name, $tool_call_id ) );
return 'runtime-tool-' . hash( 'sha256', implode( '|', $parts ) );
}
}