-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-compaction-item.php
More file actions
294 lines (251 loc) · 8.37 KB
/
Copy pathclass-wp-agent-compaction-item.php
File metadata and controls
294 lines (251 loc) · 8.37 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
<?php
/**
* Generic compaction item normalization contract.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Normalizes ordered compaction inputs into a generic item shape.
*/
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Validation exceptions are not rendered output.
class WP_Agent_Compaction_Item {
public const SCHEMA = 'agents-api.compaction-item';
public const VERSION = 1;
/**
* Normalize a raw item to the canonical compaction item shape.
*
* @param array<mixed> $item Raw compaction item.
* @param int|null $index Optional item position used for generated IDs.
* @return array<string, mixed> Normalized compaction item.
* @throws \InvalidArgumentException When the item is invalid.
*/
public static function normalize( array $item, ?int $index = null ): array {
$type = self::normalize_string( $item['type'] ?? null, 'type' );
$content = self::normalize_content( $item );
$metadata = self::normalize_metadata( $item['metadata'] ?? array() );
$group = self::normalize_optional_string( $item['group'] ?? null, 'group' );
$boundary = self::normalize_boundary( $item['boundary'] ?? null );
$normalized = array(
'schema' => self::SCHEMA,
'version' => self::VERSION,
'id' => self::normalize_id( $item['id'] ?? null, $type, $content, $metadata, $group, $boundary, $index ),
'type' => $type,
'content' => $content,
'metadata' => $metadata,
);
if ( null !== $group ) {
$normalized['group'] = $group;
}
if ( null !== $boundary ) {
$normalized['boundary'] = $boundary;
}
if ( false === self::json_encode( $normalized ) ) {
throw new \InvalidArgumentException( 'invalid_ai_compaction_item: item must be JSON serializable' );
}
return $normalized;
}
/**
* Normalize an ordered list of compaction items.
*
* @param array<mixed> $items Raw compaction items.
* @return array<int, array<string, mixed>>
* @throws \InvalidArgumentException When an item is invalid.
*/
public static function normalize_many( array $items ): array {
$normalized = array();
foreach ( $items as $index => $item ) {
if ( ! is_array( $item ) ) {
throw new \InvalidArgumentException( 'invalid_ai_compaction_item: item must be an array' );
}
$normalized[] = self::normalize( $item, is_int( $index ) ? $index : count( $normalized ) );
}
return $normalized;
}
/**
* Project a message envelope into the generic compaction item contract.
*
* @param array<mixed> $message Message envelope or plain role/content message.
* @param int|null $index Optional item position used for generated IDs.
* @return array<string, mixed> Normalized compaction item.
*/
public static function from_message( array $message, ?int $index = null ): array {
$envelope = WP_Agent_Message::normalize( $message );
$metadata = self::normalize_metadata( $envelope['metadata'] ?? array() );
$role = self::normalize_string( $envelope['role'] ?? null, 'role' );
$type = self::normalize_string( $envelope['type'] ?? null, 'type' );
$metadata['message'] = array(
'role' => $role,
'payload' => $envelope['payload'],
);
$item = array(
'type' => 'message:' . $type,
'content' => $envelope['content'],
'metadata' => $metadata,
);
if ( isset( $envelope['id'] ) ) {
$item['id'] = $envelope['id'];
}
return self::normalize( $item, $index );
}
/**
* Project message envelopes into ordered compaction items.
*
* @param array<mixed> $messages Message envelopes or plain role/content messages.
* @return array<int, array<string, mixed>>
*/
public static function from_messages( array $messages ): array {
$items = array();
foreach ( $messages as $index => $message ) {
if ( ! is_array( $message ) ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: message must be an array' );
}
$items[] = self::from_message( $message, is_int( $index ) ? $index : count( $items ) );
}
return $items;
}
/**
* Normalize a required string field.
*
* @param mixed $value Raw value.
* @param string $field Field name.
* @return string
*/
private static function normalize_string( $value, string $field ): string {
if ( ! is_string( $value ) || '' === trim( $value ) ) {
throw new \InvalidArgumentException( 'invalid_ai_compaction_item: ' . $field . ' must be a non-empty string' );
}
return trim( $value );
}
/**
* Normalize an optional string field.
*
* @param mixed $value Raw value.
* @param string $field Field name.
* @return string|null
*/
private static function normalize_optional_string( $value, string $field ): ?string {
if ( null === $value ) {
return null;
}
return self::normalize_string( $value, $field );
}
/**
* Normalize item content.
*
* @param array<mixed> $item Raw item.
* @return string|array<mixed>
*/
private static function normalize_content( array $item ) {
if ( ! array_key_exists( 'content', $item ) ) {
throw new \InvalidArgumentException( 'invalid_ai_compaction_item: content is required' );
}
$content = $item['content'];
if ( ! is_string( $content ) && ! is_array( $content ) ) {
throw new \InvalidArgumentException( 'invalid_ai_compaction_item: content must be a string or array' );
}
return $content;
}
/**
* Normalize metadata.
*
* @param mixed $metadata Raw metadata.
* @return array<string, mixed>
*/
private static function normalize_metadata( $metadata ): array {
if ( ! is_array( $metadata ) ) {
throw new \InvalidArgumentException( 'invalid_ai_compaction_item: metadata must be an array' );
}
return self::string_keyed_array( $metadata );
}
/**
* Normalize optional grouping or boundary hints.
*
* @param mixed $boundary Raw boundary hints.
* @return array<string, mixed>|null
*/
private static function normalize_boundary( $boundary ): ?array {
if ( null === $boundary ) {
return null;
}
if ( ! is_array( $boundary ) ) {
throw new \InvalidArgumentException( 'invalid_ai_compaction_item: boundary must be an array' );
}
return self::string_keyed_array( $boundary );
}
/**
* Normalize or generate a stable item ID.
*
* @param mixed $id Raw ID.
* @param string $type Item type.
* @param string|array<mixed> $content Item content.
* @param array<string, mixed> $metadata Item metadata.
* @param string|null $group Item group.
* @param array<string, mixed>|null $boundary Boundary hints.
* @param int|null $index Item position.
* @return string
*/
private static function normalize_id( $id, string $type, $content, array $metadata, ?string $group, ?array $boundary, ?int $index ): string {
if ( null !== $id ) {
return self::normalize_string( $id, 'id' );
}
$source = array(
'index' => $index,
'type' => $type,
'content' => $content,
'metadata' => $metadata,
'group' => $group,
'boundary' => $boundary,
);
return 'item-' . substr( hash( 'sha256', (string) self::json_encode( self::sort_recursive( $source ) ) ), 0, 16 );
}
/**
* Sort associative array keys recursively for deterministic ID hashes.
*
* @param mixed $value Raw value.
* @return mixed
*/
private static function sort_recursive( $value ) {
if ( ! is_array( $value ) ) {
return $value;
}
if ( array_keys( $value ) !== range( 0, count( $value ) - 1 ) ) {
ksort( $value );
}
foreach ( $value as $key => $nested_value ) {
$value[ $key ] = self::sort_recursive( $nested_value );
}
return $value;
}
/**
* Keep only associative string keys while preserving values.
*
* @param array<mixed> $value Raw array.
* @return array<string, mixed>
*/
private static function string_keyed_array( array $value ): array {
$normalized = array();
foreach ( $value as $key => $item ) {
if ( is_string( $key ) ) {
$normalized[ $key ] = $item;
}
}
return $normalized;
}
/**
* Encode data for serializability checks with a pure-PHP fallback for smokes.
*
* @param mixed $data Data to encode.
* @return string|false Encoded JSON or false on failure.
*/
private static function json_encode( $data ) {
if ( function_exists( 'wp_json_encode' ) ) {
return wp_json_encode( $data );
}
// phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode -- Pure-PHP smoke tests run without WordPress loaded.
return json_encode( $data );
}
}