-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-provider-turn-request.php
More file actions
211 lines (176 loc) · 6.73 KB
/
Copy pathclass-wp-agent-provider-turn-request.php
File metadata and controls
211 lines (176 loc) · 6.73 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
<?php
/**
* Provider-turn request contract.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI;
use AgentsAPI\AI\Tools\WP_Agent_Tool_Declaration;
defined( 'ABSPATH' ) || exit;
/**
* Storage-neutral request object passed to provider-turn adapters.
*/
// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Validation exceptions are not rendered output.
class WP_Agent_Provider_Turn_Request {
/** @var array<int, array<string, mixed>> Canonical transcript messages. */
private array $messages;
/** @var array<string, array<string, mixed>> Tool declarations keyed by name. */
private array $tool_declarations;
/** @var array<string, mixed> Provider/model metadata. */
private array $model;
/** @var array<string, mixed> Runtime-owned metadata. */
private array $runtime;
/** @var array<string, mixed> Turn context. */
private array $context;
/** @var array<string, mixed> Budget state. */
private array $budgets;
/** @var string Run identifier. */
private string $run_id;
/** @var string Session identifier. */
private string $session_id;
/** @var array<string, mixed> Caller-owned metadata. */
private array $metadata;
/**
* @param array<mixed> $messages Canonical transcript messages.
* @param array<mixed> $tool_declarations Tool declarations keyed by name.
* @param array<string, mixed> $model Provider/model metadata.
* @param array<string, mixed> $runtime Runtime-owned metadata.
* @param array<string, mixed> $context Turn context.
* @param array<string, mixed> $budgets Budget state.
* @param string $run_id Run identifier.
* @param string $session_id Session identifier.
* @param array<string, mixed> $metadata Caller-owned metadata.
*/
public function __construct( array $messages, array $tool_declarations = array(), array $model = array(), array $runtime = array(), array $context = array(), array $budgets = array(), string $run_id = '', string $session_id = '', array $metadata = array() ) {
$this->messages = WP_Agent_Message::normalize_many( $messages );
$this->tool_declarations = self::normalize_tool_declarations( $tool_declarations );
$this->model = self::normalize_json_array( $model, 'model' );
$this->runtime = self::normalize_json_array( $runtime, 'runtime' );
$this->context = self::normalize_json_array( $context, 'context' );
$this->budgets = self::normalize_json_array( $budgets, 'budgets' );
$this->run_id = $run_id;
$this->session_id = $session_id;
$this->metadata = self::normalize_json_array( $metadata, 'metadata' );
}
/** @return array<int, array<string, mixed>> Canonical transcript messages. */
public function messages(): array {
return $this->messages;
}
/** @return array<string, array<string, mixed>> Tool declarations keyed by name. */
public function toolDeclarations(): array {
return $this->tool_declarations;
}
/** @return array<string, mixed> Provider/model metadata. */
public function model(): array {
return $this->model;
}
/** @return array<string, mixed> Runtime-owned metadata. */
public function runtime(): array {
return $this->runtime;
}
/** @return array<string, mixed> Turn context. */
public function context(): array {
return $this->context;
}
/** @return array<string, mixed> Budget state. */
public function budgets(): array {
return $this->budgets;
}
/** @return string Run identifier. */
public function runId(): string {
return $this->run_id;
}
/** @return string Session identifier. */
public function sessionId(): string {
return $this->session_id;
}
/** @return array<string, mixed> Caller-owned metadata. */
public function metadata(): array {
return $this->metadata;
}
/**
* Return a normalized array representation.
*
* @return array<string, mixed>
*/
public function to_array(): array {
return array(
'messages' => $this->messages,
'tool_declarations' => $this->tool_declarations,
'model' => $this->model,
'runtime' => $this->runtime,
'context' => $this->context,
'budgets' => $this->budgets,
'run_id' => $this->run_id,
'session_id' => $this->session_id,
'metadata' => $this->metadata,
);
}
/**
* Normalize tool declarations keyed by tool name.
*
* @param array<mixed> $tool_declarations Tool declarations.
* @return array<string, array<string, mixed>>
*/
private static function normalize_tool_declarations( array $tool_declarations ): array {
$normalized = array();
foreach ( $tool_declarations as $key => $declaration ) {
if ( ! is_array( $declaration ) ) {
throw self::invalid( 'tool_declarations.' . (string) $key, 'must be an array' );
}
$declaration = self::string_keyed_array( $declaration );
if ( is_string( $key ) && '' !== $key && ! isset( $declaration['name'] ) ) {
$declaration['name'] = $key;
}
try {
$tool = self::string_keyed_array( WP_Agent_Tool_Declaration::normalizeForConversationRequest( $declaration ) );
} catch ( \InvalidArgumentException $error ) {
throw self::invalid( 'tool_declarations.' . (string) $key, $error->getMessage() );
}
$name = is_string( $tool['name'] ?? null ) ? $tool['name'] : '';
if ( '' === $name ) {
throw self::invalid( 'tool_declarations.' . (string) $key . '.name', 'must be a non-empty string' );
}
$normalized[ $name ] = $tool;
}
return $normalized;
}
/**
* Validate that an associative array is JSON serializable.
*
* @param array<mixed> $value Raw value.
* @param string $path Field path.
* @return array<string, mixed>
*/
private static function normalize_json_array( array $value, string $path ): array {
if ( false === wp_json_encode( $value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ) {
throw self::invalid( $path, 'must be JSON serializable' );
}
return self::string_keyed_array( $value );
}
/**
* Normalize an associative array to string keys.
*
* @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;
}
/**
* Build a machine-readable validation exception.
*
* @param string $path Field path.
* @param string $reason Failure reason.
* @return \InvalidArgumentException Validation exception.
*/
private static function invalid( string $path, string $reason ): \InvalidArgumentException {
return new \InvalidArgumentException( 'invalid_agent_provider_turn_request: ' . $path . ' ' . $reason );
}
}