-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-message.php
More file actions
556 lines (491 loc) · 17.9 KB
/
Copy pathclass-wp-agent-message.php
File metadata and controls
556 lines (491 loc) · 17.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php
/**
* JSON-friendly agent message envelope contract.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Normalizes agent messages into the canonical typed envelope.
*/
class WP_Agent_Message {
public const SCHEMA = 'agents-api.message';
public const VERSION = 1;
public const TYPE_TEXT = 'text';
public const TYPE_TOOL_CALL = 'tool_call';
public const TYPE_TOOL_RESULT = 'tool_result';
public const TYPE_INPUT_REQUIRED = 'input_required';
public const TYPE_APPROVAL_REQUIRED = 'approval_required';
public const TYPE_FINAL_RESULT = 'final_result';
public const TYPE_ERROR = 'error';
public const TYPE_DELTA = 'delta';
public const TYPE_MULTIMODAL_PART = 'multimodal_part';
private const SUPPORTED_TYPES = array(
self::TYPE_TEXT,
self::TYPE_TOOL_CALL,
self::TYPE_TOOL_RESULT,
self::TYPE_INPUT_REQUIRED,
self::TYPE_APPROVAL_REQUIRED,
self::TYPE_FINAL_RESULT,
self::TYPE_ERROR,
self::TYPE_DELTA,
self::TYPE_MULTIMODAL_PART,
);
/**
* Return the supported envelope type names.
*
* @return array<int, string>
*/
public static function supported_types(): array {
return self::SUPPORTED_TYPES;
}
/**
* Build a canonical text envelope.
*
* @param string $role Message role.
* @param string|array<mixed> $content Message content.
* @param array<mixed> $metadata Extension metadata.
* @return array<string, mixed>
*/
public static function text( string $role, $content, array $metadata = array() ): array {
return self::buildEnvelope( $role, $content, self::inferContentType( $content, $metadata ), array(), $metadata, array() );
}
/**
* Build a canonical tool-call envelope.
*
* @param string $content Human-readable tool-call content.
* @param string $tool_name Tool identifier.
* @param array<mixed> $parameters Tool parameters.
* @param int $turn Conversation turn.
* @param array<mixed> $metadata Extension metadata.
* @return array<string, mixed>
*/
public static function toolCall( string $content, string $tool_name, array $parameters, int $turn, array $metadata = array() ): array {
return self::buildEnvelope(
'assistant',
$content,
self::TYPE_TOOL_CALL,
array(
'tool_name' => $tool_name,
'parameters' => $parameters,
'turn' => $turn,
),
$metadata,
array()
);
}
/**
* Build a canonical tool-result envelope.
*
* @param string $content Human-readable tool-result content.
* @param string $tool_name Tool identifier.
* @param array<mixed> $payload Type-specific result payload.
* @param array<mixed> $metadata Extension metadata.
* @return array<string, mixed>
*/
public static function toolResult( string $content, string $tool_name, array $payload, array $metadata = array() ): array {
$payload['tool_name'] = $tool_name;
return self::buildEnvelope( 'user', $content, self::TYPE_TOOL_RESULT, $payload, $metadata, array() );
}
/**
* Build a canonical approval-required envelope.
*
* The payload is intentionally generic so consumers can describe any pending
* action without coupling the envelope contract to a specific runtime.
*
* @param string $content Human-readable approval request content.
* @param array<mixed> $payload Approval payload, for example action_id, kind, summary, preview, resolve, expires_at.
* @param array<mixed> $metadata Extension metadata.
* @return array<string, mixed>
*/
public static function approvalRequired( string $content, array $payload, array $metadata = array() ): array {
return self::buildEnvelope( self::roleForType( self::TYPE_APPROVAL_REQUIRED ), $content, self::TYPE_APPROVAL_REQUIRED, $payload, $metadata, array() );
}
/**
* Normalize a plain role/content message or typed envelope to the canonical envelope.
*
* @param array<mixed> $message Message array.
* @return array<string, mixed> Normalized envelope.
* @throws \InvalidArgumentException When the message is invalid.
*/
public static function normalize( array $message ): array {
$envelope = self::isEnvelope( $message )
? self::normalizeEnvelope( $message )
: self::fromPlainMessage( $message );
if ( false === self::jsonEncode( $envelope ) ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: envelope must be JSON serializable' );
}
return $envelope;
}
/**
* Normalize a list of messages to envelopes.
*
* @param array<mixed> $messages Message arrays.
* @return array<int, array<string, mixed>>
*/
public static function normalize_many( array $messages ): array {
$normalized = array();
foreach ( $messages as $message ) {
if ( ! is_array( $message ) ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: message must be an array' );
}
$normalized[] = self::normalize( $message );
}
return $normalized;
}
/**
* Project an envelope to a provider request message shape.
*
* @param array<mixed> $message Typed envelope or plain role/content message.
* @return array<string, mixed> Provider-facing message.
*/
public static function to_provider_message( array $message ): array {
$envelope = self::normalize( $message );
$metadata = is_array( $envelope['metadata'] ?? null ) ? $envelope['metadata'] : array();
$type = self::string_value( $envelope['type'] ?? self::TYPE_TEXT );
$payload = is_array( $envelope['payload'] ?? null ) ? $envelope['payload'] : array();
if ( self::TYPE_TEXT !== $type || ! empty( $payload ) || ! empty( $metadata ) ) {
$metadata['type'] = $type;
}
foreach ( $payload as $key => $value ) {
if ( ! array_key_exists( $key, $metadata ) ) {
$metadata[ $key ] = $value;
}
}
$provider_message = array(
'role' => $envelope['role'],
'content' => $envelope['content'],
);
if ( ! empty( $metadata ) ) {
$provider_message['metadata'] = $metadata;
}
return $provider_message;
}
/**
* Project envelopes to a provider request message shape.
*
* @param array<mixed> $messages Typed envelopes or plain role/content messages.
* @return array<int, array<string, mixed>> Provider-facing messages.
*/
public static function to_provider_messages( array $messages ): array {
$provider_messages = array();
foreach ( $messages as $message ) {
if ( ! is_array( $message ) ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: message must be an array' );
}
$provider_messages[] = self::to_provider_message( $message );
}
return $provider_messages;
}
/**
* Extract the canonical type from a message.
*
* @param array<mixed> $message Typed envelope or plain role/content message.
* @return string Message type.
*/
public static function type( array $message ): string {
$envelope = self::normalize( $message );
return self::string_value( $envelope['type'] ?? self::TYPE_TEXT );
}
/**
* Coalesce consecutive same-role envelopes into multi-part shapes.
*
* When an assistant turn emits both narrative text and one or more tool
* calls, the substrate persists them as separate envelopes (one TYPE_TEXT
* followed by one or more TYPE_TOOL_CALL, all role=assistant). Replay
* against providers that reject consecutive same-role messages
* (Anthropic notably) then fails until the consumer merges them back into
* a single multi-part assistant message before dispatch.
*
* This helper does that merge: consecutive envelopes of the same role are
* combined into one TYPE_MULTIMODAL_PART envelope whose `payload.parts`
* carries each original envelope verbatim. Provider adapters can then map
* the multi-part envelope to whatever shape the target provider expects
* (Anthropic's content-block array, OpenAI's tool_calls + content split,
* etc.) without losing per-part metadata or having to re-derive grouping
* from message order.
*
* The helper is opt-in: it does not change the substrate's persisted
* transcript format. Consumers call it on the message list immediately
* before constructing the provider request. Calling it twice is a no-op
* because already-multipart envelopes are preserved as-is.
*
* @param array<int, array<string, mixed>> $messages Messages to coalesce.
* @return array<int, array<string, mixed>> Messages with consecutive same-role envelopes merged.
*/
public static function coalesce_consecutive_same_role( array $messages ): array {
$normalized = self::normalize_many( $messages );
$coalesced = array();
foreach ( $normalized as $envelope ) {
$role = $envelope['role'] ?? '';
$tail = end( $coalesced );
if ( false === $tail || ( $tail['role'] ?? '' ) !== $role ) {
$coalesced[] = $envelope;
continue;
}
$tail_key = array_key_last( $coalesced );
if ( null === $tail_key ) {
$coalesced[] = $envelope;
continue;
}
$tail_parts = self::extract_parts( $tail );
$new_parts = self::extract_parts( $envelope );
$coalesced[ $tail_key ] = self::buildEnvelope(
$role,
self::join_text_content( $tail_parts, $new_parts ),
self::TYPE_MULTIMODAL_PART,
array(
'parts' => array_merge( $tail_parts, $new_parts ),
),
is_array( $tail['metadata'] ?? null ) ? $tail['metadata'] : array(),
array()
);
}
return $coalesced;
}
/**
* Return the parts that should be carried inside a coalesced envelope.
*
* Already-multipart envelopes contribute their existing parts; all other
* envelopes contribute themselves as a single part so no payload data is
* lost in the merge.
*
* @param array<string, mixed> $envelope Source envelope.
* @return array<int, array<string, mixed>>
*/
private static function extract_parts( array $envelope ): array {
$payload = is_array( $envelope['payload'] ?? null ) ? $envelope['payload'] : array();
if ( self::TYPE_MULTIMODAL_PART === ( $envelope['type'] ?? '' ) && isset( $payload['parts'] ) && is_array( $payload['parts'] ) ) {
$parts = array();
foreach ( $payload['parts'] as $part ) {
if ( is_array( $part ) ) {
$parts[] = self::assoc_array( $part );
}
}
return $parts;
}
return array( $envelope );
}
/**
* Produce a human-readable content string for a coalesced envelope.
*
* The substrate keeps the original per-part envelopes inside the payload;
* the top-level `content` is reduced to the concatenated text of any
* TYPE_TEXT parts so logs and transcripts stay readable without provider
* adapter introspection.
*
* @param array<int, array<string, mixed>> $left_parts Parts already inside the coalesced envelope.
* @param array<int, array<string, mixed>> $right_parts Parts being merged in.
* @return string Joined text content.
*/
private static function join_text_content( array $left_parts, array $right_parts ): string {
$texts = array();
foreach ( array_merge( $left_parts, $right_parts ) as $part ) {
if ( self::TYPE_TEXT === ( $part['type'] ?? '' ) && is_string( $part['content'] ?? null ) && '' !== $part['content'] ) {
$texts[] = $part['content'];
}
}
return implode( "\n\n", $texts );
}
/**
* Detect whether an array already uses the envelope shape.
*
* @param array<mixed> $message Message array.
* @return bool
*/
private static function isEnvelope( array $message ): bool {
return isset( $message['version'], $message['type'] )
&& ( isset( $message['schema'] ) || array_key_exists( 'payload', $message ) || array_key_exists( 'data', $message ) );
}
/**
* Normalize an already-typed envelope.
*
* @param array<mixed> $message Raw envelope.
* @return array<string, mixed> Canonical envelope.
*/
private static function normalizeEnvelope( array $message ): array {
$version = $message['version'] ?? null;
if ( ! is_int( $version ) && ! is_string( $version ) ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: unsupported version' );
}
if ( self::VERSION !== (int) $version ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: unsupported version' );
}
$type = self::normalizeType( $message['type'] ?? self::TYPE_TEXT );
$payload = is_array( $message['payload'] ?? null ) ? $message['payload'] : array();
if ( empty( $payload ) && is_array( $message['data'] ?? null ) ) {
$payload = $message['data'];
}
return self::buildEnvelope(
$message['role'] ?? self::roleForType( $type ),
$message['content'] ?? '',
$type,
$payload,
is_array( $message['metadata'] ?? null ) ? $message['metadata'] : array(),
$message
);
}
/**
* Normalize the plain role/content/metadata message shape.
*
* @param array<mixed> $message Plain role/content message.
* @return array<string, mixed> Canonical envelope.
*/
private static function fromPlainMessage( array $message ): array {
$metadata = is_array( $message['metadata'] ?? null ) ? $message['metadata'] : array();
$type = self::inferType( $message, $metadata );
return self::buildEnvelope(
$message['role'] ?? self::roleForType( $type ),
$message['content'] ?? '',
$type,
self::payloadFromPlainMetadata( $type, $metadata ),
$metadata,
$message
);
}
/**
* Build a canonical envelope with common optional fields preserved.
*
* @param mixed $role Raw role.
* @param mixed $content Raw content.
* @param string $type Envelope type.
* @param array<mixed> $payload Type-specific payload.
* @param array<mixed> $metadata Extension metadata.
* @param array<mixed> $source Source message.
* @return array<string, mixed> Canonical envelope.
*/
private static function buildEnvelope( $role, $content, string $type, array $payload, array $metadata, array $source ): array {
if ( ! is_string( $role ) || '' === $role ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: role must be a non-empty string' );
}
if ( ! is_string( $content ) && ! is_array( $content ) ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: content must be a string or array' );
}
$envelope = array(
'schema' => self::SCHEMA,
'version' => self::VERSION,
'type' => self::normalizeType( $type ),
'role' => $role,
'content' => $content,
'payload' => $payload,
'metadata' => $metadata,
);
foreach ( array( 'id', 'created_at', 'updated_at' ) as $field ) {
if ( isset( $source[ $field ] ) && is_string( $source[ $field ] ) && '' !== $source[ $field ] ) {
$envelope[ $field ] = $source[ $field ];
}
}
return $envelope;
}
/**
* Infer the typed envelope event from plain message fields.
*
* @param array<mixed> $message Plain role/content message.
* @param array<mixed> $metadata Plain message metadata.
* @return string Envelope type.
*/
private static function inferType( array $message, array $metadata ): string {
$metadata_type = is_string( $metadata['type'] ?? null ) ? $metadata['type'] : '';
if ( in_array( $metadata_type, self::SUPPORTED_TYPES, true ) ) {
return $metadata_type;
}
if ( 'multimodal' === $metadata_type || is_array( $message['content'] ?? null ) ) {
return self::TYPE_MULTIMODAL_PART;
}
if ( isset( $metadata['error'] ) ) {
return self::TYPE_ERROR;
}
return self::TYPE_TEXT;
}
/**
* Infer the type for newly built messages.
*
* @param mixed $content Message content.
* @param array<mixed> $metadata Message metadata.
* @return string Envelope type.
*/
private static function inferContentType( $content, array $metadata ): string {
return self::inferType( array( 'content' => $content ), $metadata );
}
/**
* Pull common plain-message metadata fields into type-specific envelope payload.
*
* @param string $type Envelope type.
* @param array<mixed> $metadata Plain message metadata.
* @return array<string, mixed> Type-specific payload.
*/
private static function payloadFromPlainMetadata( string $type, array $metadata ): array {
$payload = array();
if ( self::TYPE_TOOL_CALL === $type ) {
foreach ( array( 'tool_name', 'parameters', 'turn' ) as $key ) {
if ( array_key_exists( $key, $metadata ) ) {
$payload[ $key ] = $metadata[ $key ];
}
}
}
if ( self::TYPE_TOOL_RESULT === $type ) {
foreach ( array( 'tool_name', 'success', 'turn', 'tool_data', 'media', 'error' ) as $key ) {
if ( array_key_exists( $key, $metadata ) ) {
$payload[ $key ] = $metadata[ $key ];
}
}
}
return $payload;
}
/**
* Normalize and validate a message type.
*
* @param mixed $type Raw type.
* @return string Supported type.
*/
private static function normalizeType( $type ): string {
if ( ! is_string( $type ) || ! in_array( $type, self::SUPPORTED_TYPES, true ) ) {
throw new \InvalidArgumentException( 'invalid_ai_message_envelope: unsupported type' );
}
return $type;
}
private static function string_value( mixed $value ): string {
return is_int( $value ) || is_float( $value ) || is_string( $value ) || is_bool( $value ) ? (string) $value : '';
}
/**
* @param array<mixed> $value Raw array.
* @return array<string,mixed>
*/
private static function assoc_array( array $value ): array {
$assoc = array();
foreach ( $value as $field => $field_value ) {
if ( is_string( $field ) ) {
$assoc[ $field ] = $field_value;
}
}
return $assoc;
}
/**
* 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 jsonEncode( $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 );
}
/**
* Pick a default role for future typed envelopes that omit one.
*
* @param string $type Envelope type.
* @return string Role.
*/
private static function roleForType( string $type ): string {
if ( in_array( $type, array( self::TYPE_TOOL_RESULT, self::TYPE_INPUT_REQUIRED, self::TYPE_APPROVAL_REQUIRED ), true ) ) {
return 'tool';
}
return 'assistant';
}
}