-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-effective-agent-resolver.php
More file actions
211 lines (181 loc) · 5.63 KB
/
Copy pathclass-wp-agent-effective-agent-resolver.php
File metadata and controls
211 lines (181 loc) · 5.63 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
/**
* Effective agent resolver.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI;
defined( 'ABSPATH' ) || exit;
/**
* Resolves the per-invocation effective agent for scoped operations.
*
* This deliberately models request/job/session identity, not global mutable
* "active agent" state. Consumers should pass explicit operation input when
* available, then principal or persisted context from the current invocation.
*/
final class WP_Agent_Effective_Agent_Resolver {
/**
* Resolve the effective agent slug from the best available context.
*
* Resolution order:
* 1) Explicit operation input: `agent_slug` / `effective_agent_id`.
* 2) Execution principal: `principal->effective_agent_id`.
* 3) Persisted invocation context: `persisted_agent_slug` / `persisted_effective_agent_id`.
* 4) Owner fallback only when exactly one registered/candidate agent matches.
*
* @param array<string,mixed> $context Resolution context.
* @return string Effective agent slug, or an empty string when no candidate exists.
* @throws \InvalidArgumentException When owner fallback is ambiguous.
*/
public static function resolve( array $context = array() ): string {
$explicit = self::first_slug(
array(
$context['agent_slug'] ?? null,
$context['effective_agent_id'] ?? null,
)
);
if ( '' !== $explicit ) {
return $explicit;
}
$principal = $context['principal'] ?? null;
if ( ! $principal instanceof WP_Agent_Execution_Principal && ! empty( $context['resolve_principal'] ) ) {
$principal_context = isset( $context['principal_context'] ) && is_array( $context['principal_context'] ) ? self::string_keyed_array( $context['principal_context'] ) : array();
$principal = WP_Agent_Execution_Principal::resolve( $principal_context );
}
if ( $principal instanceof WP_Agent_Execution_Principal ) {
$principal_slug = self::normalize_slug( $principal->effective_agent_id );
if ( '' !== $principal_slug ) {
return $principal_slug;
}
}
$persisted = self::first_slug(
array(
$context['persisted_agent_slug'] ?? null,
$context['persisted_effective_agent_id'] ?? null,
)
);
if ( '' !== $persisted ) {
return $persisted;
}
$owner_user_id = self::int_value( $context['owner_user_id'] ?? 0 );
if ( $owner_user_id <= 0 ) {
return '';
}
$candidates = isset( $context['owner_agent_slugs'] ) && is_array( $context['owner_agent_slugs'] )
? self::normalize_slug_list( $context['owner_agent_slugs'] )
: self::registered_agent_slugs_for_owner( $owner_user_id );
if ( 0 === count( $candidates ) ) {
return '';
}
if ( 1 === count( $candidates ) ) {
return $candidates[0];
}
throw new \InvalidArgumentException(
'invalid_effective_agent_resolution: owner_user_id is ambiguous; provide an explicit agent_slug or execution principal. Candidates: ' . implode( ', ', array_map( 'esc_html', $candidates ) )
);
}
/**
* Return the first non-empty normalized slug.
*
* @param array<int,mixed> $values Candidate values.
* @return string Normalized slug.
*/
private static function first_slug( array $values ): string {
foreach ( $values as $value ) {
$slug = is_scalar( $value ) ? self::normalize_slug( (string) $value ) : '';
if ( '' !== $slug ) {
return $slug;
}
}
return '';
}
/**
* Normalize an agent slug.
*
* @param string $slug Raw slug.
* @return string Normalized slug.
*/
private static function normalize_slug( string $slug ): string {
if ( function_exists( 'sanitize_title' ) ) {
return sanitize_title( $slug );
}
$slug = strtolower( $slug );
$slug = preg_replace( '/[^a-z0-9]+/', '-', $slug );
return trim( (string) $slug, '-' );
}
/**
* Normalize and dedupe candidate slug list.
*
* @param array<mixed> $slugs Raw slugs.
* @return string[] Normalized slugs.
*/
private static function normalize_slug_list( array $slugs ): array {
$normalized = array();
foreach ( $slugs as $slug ) {
if ( ! is_scalar( $slug ) ) {
continue;
}
$slug = self::normalize_slug( (string) $slug );
if ( '' !== $slug ) {
$normalized[ $slug ] = true;
}
}
return array_keys( $normalized );
}
/**
* Resolve registered agent slugs whose owner resolver matches the user.
*
* @param int $owner_user_id Owner WordPress user ID.
* @return string[] Matching registered agent slugs.
*/
private static function registered_agent_slugs_for_owner( int $owner_user_id ): array {
if ( ! class_exists( '\WP_Agents_Registry' ) ) {
return array();
}
$registry = \WP_Agents_Registry::get_instance();
if ( null === $registry ) {
return array();
}
$matches = array();
foreach ( $registry->get_all_registered() as $agent ) {
$owner_resolver = $agent->get_owner_resolver();
if ( ! is_callable( $owner_resolver ) ) {
continue;
}
if ( self::int_value( call_user_func( $owner_resolver ) ) === $owner_user_id ) {
$matches[] = $agent->get_slug();
}
}
return self::normalize_slug_list( $matches );
}
/**
* 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;
}
/**
* Normalize numeric owner IDs without casting arbitrary values.
*
* @param mixed $value Raw value.
* @return int
*/
private static function int_value( $value ): int {
if ( is_int( $value ) ) {
return $value;
}
if ( is_string( $value ) && is_numeric( $value ) ) {
return (int) $value;
}
return 0;
}
}