-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-tool-result.php
More file actions
118 lines (101 loc) · 3.49 KB
/
Copy pathclass-wp-agent-tool-result.php
File metadata and controls
118 lines (101 loc) · 3.49 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
<?php
/**
* Tool execution result normalizer.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI\Tools;
use AgentsAPI\AI\WP_Agent_Citation_Metadata;
defined( 'ABSPATH' ) || exit;
/**
* Normalizes tool execution results to a stable JSON-friendly shape.
*/
class WP_Agent_Tool_Result {
/**
* Build a successful result.
*
* @param string $tool_name Tool identifier.
* @param mixed $result Executor result payload.
* @param array<mixed> $metadata Optional result metadata.
* @param array<string,mixed> $runtime Optional runtime metadata.
* @return array<string, mixed>
*/
public static function success( string $tool_name, $result, array $metadata = array(), array $runtime = array() ): array {
return self::normalize(
array(
'success' => true,
'tool_name' => $tool_name,
'result' => $result,
'metadata' => $metadata,
'runtime' => $runtime,
)
);
}
/**
* Build an error result.
*
* @param string $tool_name Tool identifier.
* @param string $error Human-readable error.
* @param array<mixed> $metadata Optional result metadata.
* @param array<string,mixed> $runtime Optional runtime metadata.
* @return array<string, mixed>
*/
public static function error( string $tool_name, string $error, array $metadata = array(), array $runtime = array() ): array {
return self::normalize(
array(
'success' => false,
'tool_name' => $tool_name,
'error' => $error,
'metadata' => $metadata,
'runtime' => $runtime,
)
);
}
/**
* Normalize arbitrary executor output.
*
* @param array<mixed> $result Raw result.
* @return array<string, mixed>
*/
public static function normalize( array $result ): array {
$tool_name = $result['tool_name'] ?? '';
if ( ! is_string( $tool_name ) || '' === $tool_name ) {
throw new \InvalidArgumentException( 'invalid_tool_execution_result: tool_name must be a non-empty string' );
}
$success = (bool) ( $result['success'] ?? false );
$metadata = WP_Agent_Citation_Metadata::normalize_metadata( $result['metadata'] ?? array() );
// Preserve a machine-readable error code. Executors may either place
// error_type under metadata (the canonical home consumers read from) or
// return it as a top-level field; promote a top-level code into metadata
// so it survives normalization without clobbering an explicit metadata code.
if ( ! isset( $metadata['error_type'] )
&& isset( $result['error_type'] )
&& is_string( $result['error_type'] )
&& '' !== trim( $result['error_type'] )
) {
$metadata['error_type'] = trim( $result['error_type'] );
}
$runtime = WP_Agent_Tool_Declaration::normalizeRuntimeMetadata( $result['runtime'] ?? array() );
$normalized = array(
'success' => $success,
'tool_name' => $tool_name,
'metadata' => $metadata,
);
if ( ! empty( $runtime ) ) {
$normalized['runtime'] = $runtime;
}
if ( isset( $result['status'] ) && is_string( $result['status'] ) && '' !== trim( $result['status'] ) ) {
$normalized['status'] = trim( $result['status'] );
}
if ( isset( $result['runtime_tool_request'] ) && is_array( $result['runtime_tool_request'] ) ) {
$normalized['runtime_tool_request'] = $result['runtime_tool_request'];
}
if ( $success ) {
$normalized['result'] = $result['result'] ?? array();
return $normalized;
}
$error = $result['error'] ?? 'Tool execution failed.';
$normalized['error'] = is_string( $error ) && '' !== $error ? $error : 'Tool execution failed.';
return $normalized;
}
}