-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclass-wp-agent-memory-registry.php
More file actions
298 lines (262 loc) · 9.44 KB
/
Copy pathclass-wp-agent-memory-registry.php
File metadata and controls
298 lines (262 loc) · 9.44 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
<?php
/**
* Canonical generic agent memory/context source registry.
*
* @package AgentsAPI
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WP_Agent_Memory_Registry' ) ) {
/**
* Registers memory and context sources without prescribing storage shape.
*
* Hosts and adapters should register their available sources here, then map the
* normalized metadata to their own storage, filesystem, retrieval, or projection
* layers. The registry is the portable source contract; persistence remains the
* responsibility of memory stores or host-owned adapters.
*/
final class WP_Agent_Memory_Registry {
public const MODE_ALL = 'all';
/**
* Registered sources keyed by source ID.
*
* @var array<string, array<string, mixed>>
*/
private static array $sources = array();
/**
* Whether extension hooks have been fired.
*
* @var bool
*/
private static bool $hooks_fired = false;
/**
* Register a memory or context source.
*
* @param string $source_id Source identifier, e.g. `workspace/instructions`.
* @param array<mixed> $args Registration metadata. Adapter hints such as
* `convention_path` and
* `external_projection_target` describe how a host
* may project the source; they are not source
* identity and do not imply file-backed storage.
* @return array<string, mixed>|null Normalized source metadata, or null on invalid ID.
*/
public static function register( string $source_id, array $args = array() ): ?array {
$source_id = self::sanitize_source_id( $source_id );
if ( '' === $source_id ) {
return null;
}
$composable = (bool) ( $args['composable'] ?? false );
$editable = $composable ? false : ( $args['editable'] ?? true );
if ( ! is_bool( $editable ) && ! is_string( $editable ) ) {
$editable = true;
}
$context_slug = $args['context_slug'] ?? $source_id;
$metadata = array(
'id' => $source_id,
'layer' => WP_Agent_Memory_Layer::normalize( $args['layer'] ?? null ),
'priority' => self::optional_int( $args['priority'] ?? null, 50 ),
'protected' => (bool) ( $args['protected'] ?? false ),
'editable' => $editable,
'capability' => isset( $args['capability'] ) && is_string( $args['capability'] ) ? $args['capability'] : '',
'modes' => self::normalize_modes( $args['modes'] ?? array( self::MODE_ALL ) ),
'retrieval_policy' => WP_Agent_Context_Injection_Policy::normalize( $args['retrieval_policy'] ?? null ),
'composable' => $composable,
'context_slug' => self::sanitize_source_id( is_string( $context_slug ) ? $context_slug : $source_id ),
'convention_path' => self::normalize_relative_path( $args['convention_path'] ?? '' ),
'external_projection_target' => is_string( $args['external_projection_target'] ?? null ) ? $args['external_projection_target'] : '',
'label' => is_string( $args['label'] ?? null ) ? $args['label'] : self::id_to_label( $source_id ),
'description' => is_string( $args['description'] ?? null ) ? $args['description'] : '',
'meta' => is_array( $args['meta'] ?? null ) ? $args['meta'] : array(),
);
self::$sources[ $source_id ] = $metadata;
return $metadata;
}
/**
* Remove a registered source.
*
* @param string $source_id Source identifier.
* @return void
*/
public static function unregister( string $source_id ): void {
unset( self::$sources[ self::sanitize_source_id( $source_id ) ] );
}
/**
* Return a single registered source.
*
* @param string $source_id Source identifier.
* @return array<string, mixed>|null
*/
public static function get( string $source_id ): ?array {
$sources = self::get_all();
return $sources[ self::sanitize_source_id( $source_id ) ] ?? null;
}
/**
* Return all registered sources sorted by priority.
*
* @return array<string, array<string, mixed>>
*/
public static function get_all(): array {
return self::get_resolved();
}
/**
* Return sources for a layer.
*
* @param string $layer Layer value.
* @return array<string, array<string, mixed>>
*/
public static function get_by_layer( string $layer ): array {
$layer = WP_Agent_Memory_Layer::normalize( $layer );
return array_filter(
self::get_resolved(),
static function ( array $source ) use ( $layer ): bool {
return $layer === $source['layer'];
}
);
}
/**
* Return sources applicable to a runtime mode and retrieval policy.
*
* @param string $mode Runtime mode slug.
* @param string|null $retrieval_policy Optional policy filter.
* @return array<string, array<string, mixed>>
*/
public static function get_for_mode( string $mode, ?string $retrieval_policy = null ): array {
$mode = self::sanitize_slug( $mode );
$retrieval_policy = null === $retrieval_policy ? null : WP_Agent_Context_Injection_Policy::normalize( $retrieval_policy );
return array_filter(
self::get_resolved(),
static function ( array $source ) use ( $mode, $retrieval_policy ): bool {
$modes = self::normalize_modes( $source['modes'] ?? array( self::MODE_ALL ) );
if ( '' !== $mode && ! in_array( self::MODE_ALL, $modes, true ) && ! in_array( $mode, $modes, true ) ) {
return false;
}
return null === $retrieval_policy || $retrieval_policy === $source['retrieval_policy'];
}
);
}
/**
* Return sources that are injected without dynamic retrieval.
*
* @param string $mode Runtime mode slug.
* @return array<string, array<string, mixed>>
*/
public static function get_always_injected( string $mode = '' ): array {
return self::get_for_mode( $mode, WP_Agent_Context_Injection_Policy::ALWAYS );
}
/**
* Return composable sources.
*
* @return array<string, array<string, mixed>>
*/
public static function get_composable(): array {
return array_filter(
self::get_resolved(),
static function ( array $source ): bool {
return ! empty( $source['composable'] );
}
);
}
/**
* Reset registry state. Intended for tests.
*
* @return void
*/
public static function reset(): void {
self::$sources = array();
self::$hooks_fired = false;
}
/**
* Resolve hooks and return sorted sources.
*
* @return array<string, array<string, mixed>>
*/
private static function get_resolved(): array {
if ( ! self::$hooks_fired ) {
if ( function_exists( 'do_action' ) ) {
/**
* Fires before memory/context sources are resolved.
*
* Extensions should call {@see WP_Agent_Memory_Registry::register()} from
* this hook to add sources lazily. The hook argument is a snapshot for
* inspection, not a mutable source of truth.
*
* @param array<string, array<string, mixed>> $sources Registered source snapshot.
*/
do_action( 'agents_api_memory_sources', self::$sources );
}
self::$hooks_fired = true;
}
$sources = self::$sources;
uasort(
$sources,
static function ( array $a, array $b ): int {
$priority_order = $a['priority'] <=> $b['priority'];
$a_id = is_string( $a['id'] ?? null ) ? $a['id'] : '';
$b_id = is_string( $b['id'] ?? null ) ? $b['id'] : '';
return 0 !== $priority_order ? $priority_order : strcmp( $a_id, $b_id );
}
);
return $sources;
}
/**
* @param mixed $modes Raw modes.
* @return string[]
*/
private static function normalize_modes( $modes ): array {
if ( ! is_array( $modes ) || empty( $modes ) ) {
return array( self::MODE_ALL );
}
$normalized = array();
foreach ( $modes as $mode ) {
if ( ! is_string( $mode ) ) {
continue;
}
$mode = self::sanitize_slug( $mode );
if ( '' !== $mode ) {
$normalized[] = $mode;
}
}
$normalized = array_values( array_unique( $normalized ) );
return empty( $normalized ) ? array( self::MODE_ALL ) : $normalized;
}
/**
* @param mixed $value Raw value.
* @param int $fallback Default value.
* @return int
*/
private static function optional_int( $value, int $fallback ): int {
return is_scalar( $value ) ? (int) $value : $fallback;
}
/**
* @param mixed $path Raw relative path.
* @return string
*/
private static function normalize_relative_path( $path ): string {
return is_string( $path ) ? ltrim( $path, '/' ) : '';
}
/**
* @param string $source_id Source identifier.
* @return string
*/
private static function sanitize_source_id( string $source_id ): string {
$source_id = strtolower( $source_id );
$source_id = preg_replace( '/[^a-z0-9_\.\-\/]+/', '-', $source_id );
return trim( is_string( $source_id ) ? $source_id : '', '-/' );
}
/**
* @param string $slug Raw slug.
* @return string
*/
private static function sanitize_slug( string $slug ): string {
$slug = strtolower( $slug );
$slug = preg_replace( '/[^a-z0-9_\-]+/', '-', $slug );
return trim( is_string( $slug ) ? $slug : '', '-' );
}
/**
* @param string $source_id Source identifier.
* @return string
*/
private static function id_to_label( string $source_id ): string {
return ucwords( str_replace( array( '-', '_', '/', '.' ), ' ', $source_id ) );
}
}
}