-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-runtime-package-run-result.php
More file actions
182 lines (158 loc) · 4.78 KB
/
Copy pathclass-wp-agent-runtime-package-run-result.php
File metadata and controls
182 lines (158 loc) · 4.78 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
<?php
/**
* Runtime package run result value object.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI;
defined( 'ABSPATH' ) || exit;
/**
* Normalized status/result/evidence envelope for portable package runs.
*
* @since 0.3.0
*/
final class WP_Agent_Runtime_Package_Run_Result {
public const STATUS_PENDING = 'pending';
public const STATUS_RUNNING = 'running';
public const STATUS_SUCCEEDED = 'succeeded';
public const STATUS_FAILED = 'failed';
public const STATUS_CANCELLED = 'cancelled';
public const STATUS_SKIPPED = 'skipped';
/**
* @param array<string,mixed> $result Consumer-defined output.
* @param array<string,mixed> $error Stable error envelope.
* @param array<int,array<string,mixed>> $evidence_refs Neutral artifact/log refs.
* @param array<string,mixed> $metadata Host/runtime metadata.
* @param array<string,mixed> $replay Replay/materialization metadata.
* @param array<int,array<string,mixed>> $artifact_refs Canonical artifact refs.
*/
public function __construct(
private string $status,
private string $run_id = '',
private array $result = array(),
private array $error = array(),
private array $evidence_refs = array(),
private array $metadata = array(),
private array $replay = array(),
private array $artifact_refs = array()
) {}
/**
* @param array<mixed> $value Raw handler result.
*/
public static function from_array( array $value ): self {
$status = self::string_value( $value['status'] ?? '' );
if ( ! in_array( $status, self::statuses(), true ) ) {
$status = self::STATUS_SUCCEEDED;
}
return new self(
$status,
self::string_value( $value['run_id'] ?? '' ),
self::array_value( $value['result'] ?? array() ),
self::array_value( $value['error'] ?? array() ),
self::list_of_arrays( $value['evidence_refs'] ?? array() ),
self::array_value( $value['metadata'] ?? array() ),
self::array_value( $value['replay'] ?? array() ),
self::list_of_arrays( $value['artifact_refs'] ?? array() )
);
}
/** @return array<int,string> */
public static function statuses(): array {
return array(
self::STATUS_PENDING,
self::STATUS_RUNNING,
self::STATUS_SUCCEEDED,
self::STATUS_FAILED,
self::STATUS_CANCELLED,
self::STATUS_SKIPPED,
);
}
public function get_status(): string {
return $this->status;
}
public function get_run_id(): string {
return $this->run_id;
}
/** @return array<string,mixed> */
public function get_result(): array {
return $this->result;
}
/** @return array<string,mixed> */
public function get_error(): array {
return $this->error;
}
/** @return array<int,array<string,mixed>> */
public function get_evidence_refs(): array {
return $this->evidence_refs;
}
/** @return array<int,array<string,mixed>> */
public function get_artifact_refs(): array {
return self::list_of_arrays( $this->artifact_refs );
}
/** @return array<string,mixed> */
public function get_metadata(): array {
return $this->metadata;
}
/** @return array<string,mixed> */
public function get_replay(): array {
return $this->replay;
}
/** @return array<string,mixed> */
public function to_array(): array {
return array(
'status' => $this->status,
'run_id' => $this->run_id,
'result' => $this->result,
'error' => $this->error,
'artifact_refs' => $this->artifact_refs,
'evidence_refs' => $this->evidence_refs,
'metadata' => $this->metadata,
'replay' => $this->replay,
);
}
public function to_run_result_envelope(): WP_Agent_Run_Result_Envelope {
return WP_Agent_Run_Result_Envelope::from_array(
array(
'run_id' => $this->run_id,
'status' => $this->status,
'outputs' => $this->result,
'artifact_refs' => $this->artifact_refs,
'evidence_refs' => $this->evidence_refs,
'replay' => $this->replay,
'error' => $this->error,
'metadata' => $this->metadata,
)
);
}
public function to_canonical_envelope(): WP_Agent_Run_Result_Envelope {
return $this->to_run_result_envelope();
}
private static function string_value( mixed $value ): string {
return is_scalar( $value ) ? trim( (string) $value ) : '';
}
/** @return array<string,mixed> */
private static function array_value( mixed $value ): array {
if ( ! is_array( $value ) ) {
return array();
}
$normalized = array();
foreach ( $value as $key => $item ) {
if ( is_string( $key ) ) {
$normalized[ $key ] = $item;
}
}
return $normalized;
}
/** @return array<int,array<string,mixed>> */
private static function list_of_arrays( mixed $value ): array {
if ( ! is_array( $value ) ) {
return array();
}
$items = array();
foreach ( $value as $item ) {
if ( is_array( $item ) ) {
$items[] = self::array_value( $item );
}
}
return $items;
}
}