@relvehq/agent-ready 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,226 +1,226 @@
1
- <?php
2
- /**
3
- * Response: headers, ETag / 304, Cache-Control, token estimate.
4
- *
5
- * PHP port of @relvehq/agent-ready core/respond.ts and the adapters' header
6
- * choices. build() is pure (an array in, an array out) so it is unit-tested;
7
- * send() writes it with header() and echo.
8
- *
9
- * @package Relve_Agent_Ready
10
- */
11
-
12
- if ( ! defined( 'ABSPATH' ) ) {
13
- exit;
14
- }
15
-
16
- /**
17
- * Builds and sends Markdown / llms.txt / 404 responses.
18
- */
19
- class Relve_AR_Response {
20
-
21
- const MARKDOWN_CONTENT_TYPE = 'text/markdown; charset=utf-8';
22
- const TEXT_CONTENT_TYPE = 'text/plain; charset=utf-8';
23
-
24
- /** `.md`, `?format=md` and llms.txt: their own URLs, safe for shared caches. */
25
- const CACHE_CONTROL = 'public, s-maxage=300, stale-while-revalidate=86400';
26
-
27
- /** Negotiated by Accept on the page's own URL: never in a shared cache. */
28
- const NEGOTIATED_CACHE_CONTROL = 'private, max-age=300';
29
-
30
- /**
31
- * Cache-Control for a request that asked via $via.
32
- *
33
- * @param string $via suffix|query|accept|llms.
34
- * @return string
35
- */
36
- public static function cache_control_for( $via ) {
37
- return 'accept' === $via ? self::NEGOTIATED_CACHE_CONTROL : self::CACHE_CONTROL;
38
- }
39
-
40
- /**
41
- * Rough token count, as the package: ceil(length / 4), where length is
42
- * the JavaScript string length (UTF-16 code units).
43
- *
44
- * @param string $text Text.
45
- * @return int
46
- */
47
- public static function estimate_tokens( $text ) {
48
- return (int) ceil( self::utf16_length( $text ) / 4 );
49
- }
50
-
51
- /**
52
- * Length in UTF-16 code units (what JavaScript's .length counts).
53
- *
54
- * @param string $text UTF-8 text.
55
- * @return int
56
- */
57
- public static function utf16_length( $text ) {
58
- $text = (string) $text;
59
- $chars = preg_match_all( '/./su', $text );
60
- if ( false === $chars ) {
61
- return strlen( $text ); // Not valid UTF-8: count bytes.
62
- }
63
- $astral = preg_match_all( '/[\x{10000}-\x{10FFFF}]/u', $text );
64
- return $chars + (int) $astral;
65
- }
66
-
67
- /**
68
- * Strong ETag: first 32 hex chars of SHA-256(body).
69
- *
70
- * @param string $body Body.
71
- * @return string
72
- */
73
- public static function etag_for( $body ) {
74
- return '"' . substr( hash( 'sha256', (string) $body ), 0, 32 ) . '"';
75
- }
76
-
77
- /**
78
- * ETag for a body. A `generated_at:` front-matter line is left out of the
79
- * hash (it changes every time) and the ETag is then weak, as the package.
80
- *
81
- * @param string $body Body.
82
- * @return string
83
- */
84
- public static function etag_for_body( $body ) {
85
- $stable = preg_replace( '/^generated_at:.*\n?/m', '', (string) $body, 1 );
86
- return $stable === $body ? self::etag_for( $body ) : 'W/' . self::etag_for( $stable );
87
- }
88
-
89
- /**
90
- * RFC 9110 weak comparison; "*" matches any current representation.
91
- *
92
- * @param string|null $header If-None-Match value.
93
- * @param string $etag Our ETag.
94
- * @return bool
95
- */
96
- public static function if_none_match_hits( $header, $etag ) {
97
- if ( ! is_string( $header ) || '' === $header ) {
98
- return false;
99
- }
100
- $bare = preg_replace( '#^W/#', '', $etag );
101
- foreach ( explode( ',', $header ) as $raw ) {
102
- $t = trim( $raw );
103
- if ( '*' === $t || preg_replace( '#^W/#', '', $t ) === $bare ) {
104
- return true;
105
- }
106
- }
107
- return false;
108
- }
109
-
110
- /**
111
- * Build a Markdown (or llms.txt) response.
112
- *
113
- * @param string $body Body.
114
- * @param array $args {
115
- * @type string $method GET or HEAD.
116
- * @type string|null $if_none_match If-None-Match header.
117
- * @type string|null $canonical Absolute canonical URL; null leaves Link out.
118
- * @type string $cache_control Cache-Control.
119
- * @type string $content_type Content-Type (default text/markdown).
120
- * @type string $source x-agent-ready-source value.
121
- * @type bool $noindex Send X-Robots-Tag: noindex (default true).
122
- * @type int|null $tokens Token count (default: estimated from body).
123
- * }
124
- * @return array { status: int, headers: array<string,string>, body: string }
125
- */
126
- public static function build( $body, $args ) {
127
- $args = array_merge(
128
- array(
129
- 'method' => 'GET',
130
- 'if_none_match' => null,
131
- 'canonical' => null,
132
- 'cache_control' => self::CACHE_CONTROL,
133
- 'content_type' => self::MARKDOWN_CONTENT_TYPE,
134
- 'source' => 'native',
135
- 'noindex' => true,
136
- 'tokens' => null,
137
- ),
138
- (array) $args
139
- );
140
- $body = (string) $body;
141
- $etag = self::etag_for_body( $body );
142
- $tokens = null === $args['tokens'] ? self::estimate_tokens( $body ) : (int) $args['tokens'];
143
-
144
- $headers = array( 'Content-Type' => $args['content_type'] );
145
- $headers['Vary'] = 'Accept';
146
- if ( $args['canonical'] ) {
147
- $headers['Link'] = '<' . $args['canonical'] . '>; rel="canonical"';
148
- }
149
- if ( $args['noindex'] ) {
150
- $headers['X-Robots-Tag'] = 'noindex';
151
- }
152
- $headers['x-markdown-tokens'] = (string) $tokens;
153
- $headers['Cache-Control'] = $args['cache_control'];
154
- $headers['ETag'] = $etag;
155
- $headers['x-agent-ready-source'] = $args['source'];
156
-
157
- if ( self::if_none_match_hits( $args['if_none_match'], $etag ) ) {
158
- unset( $headers['Content-Type'] );
159
- return array(
160
- 'status' => 304,
161
- 'headers' => $headers,
162
- 'body' => '',
163
- );
164
- }
165
- $headers['Content-Length'] = (string) strlen( $body );
166
- return array(
167
- 'status' => 200,
168
- 'headers' => $headers,
169
- 'body' => 'HEAD' === strtoupper( (string) $args['method'] ) ? '' : $body,
170
- );
171
- }
172
-
173
- /**
174
- * A plain-text error (the 404 for `.md` / `?format=md` with nothing to serve).
175
- *
176
- * @param int $status Status code.
177
- * @param string $method GET or HEAD.
178
- * @param string|null $canonical Canonical URL of the page, if known.
179
- * @return array
180
- */
181
- public static function plain( $status, $method = 'GET', $canonical = null ) {
182
- $body = "No Markdown version of this page is available.\n";
183
- $headers = array(
184
- 'Content-Type' => self::TEXT_CONTENT_TYPE,
185
- 'Cache-Control' => 'no-store',
186
- 'Vary' => 'Accept',
187
- 'X-Robots-Tag' => 'noindex',
188
- );
189
- if ( $canonical ) {
190
- $headers['Link'] = '<' . $canonical . '>; rel="canonical"';
191
- }
192
- $headers['Content-Length'] = (string) strlen( $body );
193
- return array(
194
- 'status' => (int) $status,
195
- 'headers' => $headers,
196
- 'body' => 'HEAD' === strtoupper( (string) $method ) ? '' : $body,
197
- );
198
- }
199
-
200
- /**
201
- * Write a built response. Removes headers WordPress already queued that
202
- * do not belong on Markdown (HTML Link headers, no-cache from nocache_headers()).
203
- * The caller exits afterwards.
204
- *
205
- * @param array $response From build() or plain().
206
- * @return void
207
- */
208
- public static function send( $response ) {
209
- if ( ! headers_sent() ) {
210
- foreach ( array( 'Link', 'X-Pingback', 'Expires', 'Pragma', 'Last-Modified', 'Cache-Control', 'Content-Type', 'X-Robots-Tag' ) as $name ) {
211
- header_remove( $name );
212
- }
213
- if ( function_exists( 'status_header' ) ) {
214
- status_header( $response['status'] );
215
- } else {
216
- http_response_code( $response['status'] );
217
- }
218
- foreach ( $response['headers'] as $name => $value ) {
219
- header( $name . ': ' . $value, true );
220
- }
221
- }
222
- if ( '' !== $response['body'] ) {
223
- echo $response['body']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- text/markdown or text/plain body, never HTML.
224
- }
225
- }
226
- }
1
+ <?php
2
+ /**
3
+ * Response: headers, ETag / 304, Cache-Control, token estimate.
4
+ *
5
+ * PHP port of @relvehq/agent-ready core/respond.ts and the adapters' header
6
+ * choices. build() is pure (an array in, an array out) so it is unit-tested;
7
+ * send() writes it with header() and echo.
8
+ *
9
+ * @package Relve_Agent_Ready
10
+ */
11
+
12
+ if ( ! defined( 'ABSPATH' ) ) {
13
+ exit;
14
+ }
15
+
16
+ /**
17
+ * Builds and sends Markdown / llms.txt / 404 responses.
18
+ */
19
+ class Relve_AR_Response {
20
+
21
+ const MARKDOWN_CONTENT_TYPE = 'text/markdown; charset=utf-8';
22
+ const TEXT_CONTENT_TYPE = 'text/plain; charset=utf-8';
23
+
24
+ /** `.md`, `?format=md` and llms.txt: their own URLs, safe for shared caches. */
25
+ const CACHE_CONTROL = 'public, s-maxage=300, stale-while-revalidate=86400';
26
+
27
+ /** Negotiated by Accept on the page's own URL: never in a shared cache. */
28
+ const NEGOTIATED_CACHE_CONTROL = 'private, max-age=300';
29
+
30
+ /**
31
+ * Cache-Control for a request that asked via $via.
32
+ *
33
+ * @param string $via suffix|query|accept|llms.
34
+ * @return string
35
+ */
36
+ public static function cache_control_for( $via ) {
37
+ return 'accept' === $via ? self::NEGOTIATED_CACHE_CONTROL : self::CACHE_CONTROL;
38
+ }
39
+
40
+ /**
41
+ * Rough token count, as the package: ceil(length / 4), where length is
42
+ * the JavaScript string length (UTF-16 code units).
43
+ *
44
+ * @param string $text Text.
45
+ * @return int
46
+ */
47
+ public static function estimate_tokens( $text ) {
48
+ return (int) ceil( self::utf16_length( $text ) / 4 );
49
+ }
50
+
51
+ /**
52
+ * Length in UTF-16 code units (what JavaScript's .length counts).
53
+ *
54
+ * @param string $text UTF-8 text.
55
+ * @return int
56
+ */
57
+ public static function utf16_length( $text ) {
58
+ $text = (string) $text;
59
+ $chars = preg_match_all( '/./su', $text );
60
+ if ( false === $chars ) {
61
+ return strlen( $text ); // Not valid UTF-8: count bytes.
62
+ }
63
+ $astral = preg_match_all( '/[\x{10000}-\x{10FFFF}]/u', $text );
64
+ return $chars + (int) $astral;
65
+ }
66
+
67
+ /**
68
+ * Strong ETag: first 32 hex chars of SHA-256(body).
69
+ *
70
+ * @param string $body Body.
71
+ * @return string
72
+ */
73
+ public static function etag_for( $body ) {
74
+ return '"' . substr( hash( 'sha256', (string) $body ), 0, 32 ) . '"';
75
+ }
76
+
77
+ /**
78
+ * ETag for a body. A `generated_at:` front-matter line is left out of the
79
+ * hash (it changes every time) and the ETag is then weak, as the package.
80
+ *
81
+ * @param string $body Body.
82
+ * @return string
83
+ */
84
+ public static function etag_for_body( $body ) {
85
+ $stable = preg_replace( '/^generated_at:.*\n?/m', '', (string) $body, 1 );
86
+ return $stable === $body ? self::etag_for( $body ) : 'W/' . self::etag_for( $stable );
87
+ }
88
+
89
+ /**
90
+ * RFC 9110 weak comparison; "*" matches any current representation.
91
+ *
92
+ * @param string|null $header If-None-Match value.
93
+ * @param string $etag Our ETag.
94
+ * @return bool
95
+ */
96
+ public static function if_none_match_hits( $header, $etag ) {
97
+ if ( ! is_string( $header ) || '' === $header ) {
98
+ return false;
99
+ }
100
+ $bare = preg_replace( '#^W/#', '', $etag );
101
+ foreach ( explode( ',', $header ) as $raw ) {
102
+ $t = trim( $raw );
103
+ if ( '*' === $t || preg_replace( '#^W/#', '', $t ) === $bare ) {
104
+ return true;
105
+ }
106
+ }
107
+ return false;
108
+ }
109
+
110
+ /**
111
+ * Build a Markdown (or llms.txt) response.
112
+ *
113
+ * @param string $body Body.
114
+ * @param array $args {
115
+ * @type string $method GET or HEAD.
116
+ * @type string|null $if_none_match If-None-Match header.
117
+ * @type string|null $canonical Absolute canonical URL; null leaves Link out.
118
+ * @type string $cache_control Cache-Control.
119
+ * @type string $content_type Content-Type (default text/markdown).
120
+ * @type string $source x-agent-ready-source value.
121
+ * @type bool $noindex Send X-Robots-Tag: noindex (default true).
122
+ * @type int|null $tokens Token count (default: estimated from body).
123
+ * }
124
+ * @return array { status: int, headers: array<string,string>, body: string }
125
+ */
126
+ public static function build( $body, $args ) {
127
+ $args = array_merge(
128
+ array(
129
+ 'method' => 'GET',
130
+ 'if_none_match' => null,
131
+ 'canonical' => null,
132
+ 'cache_control' => self::CACHE_CONTROL,
133
+ 'content_type' => self::MARKDOWN_CONTENT_TYPE,
134
+ 'source' => 'native',
135
+ 'noindex' => true,
136
+ 'tokens' => null,
137
+ ),
138
+ (array) $args
139
+ );
140
+ $body = (string) $body;
141
+ $etag = self::etag_for_body( $body );
142
+ $tokens = null === $args['tokens'] ? self::estimate_tokens( $body ) : (int) $args['tokens'];
143
+
144
+ $headers = array( 'Content-Type' => $args['content_type'] );
145
+ $headers['Vary'] = 'Accept';
146
+ if ( $args['canonical'] ) {
147
+ $headers['Link'] = '<' . $args['canonical'] . '>; rel="canonical"';
148
+ }
149
+ if ( $args['noindex'] ) {
150
+ $headers['X-Robots-Tag'] = 'noindex';
151
+ }
152
+ $headers['x-markdown-tokens'] = (string) $tokens;
153
+ $headers['Cache-Control'] = $args['cache_control'];
154
+ $headers['ETag'] = $etag;
155
+ $headers['x-agent-ready-source'] = $args['source'];
156
+
157
+ if ( self::if_none_match_hits( $args['if_none_match'], $etag ) ) {
158
+ unset( $headers['Content-Type'] );
159
+ return array(
160
+ 'status' => 304,
161
+ 'headers' => $headers,
162
+ 'body' => '',
163
+ );
164
+ }
165
+ $headers['Content-Length'] = (string) strlen( $body );
166
+ return array(
167
+ 'status' => 200,
168
+ 'headers' => $headers,
169
+ 'body' => 'HEAD' === strtoupper( (string) $args['method'] ) ? '' : $body,
170
+ );
171
+ }
172
+
173
+ /**
174
+ * A plain-text error (the 404 for `.md` / `?format=md` with nothing to serve).
175
+ *
176
+ * @param int $status Status code.
177
+ * @param string $method GET or HEAD.
178
+ * @param string|null $canonical Canonical URL of the page, if known.
179
+ * @return array
180
+ */
181
+ public static function plain( $status, $method = 'GET', $canonical = null ) {
182
+ $body = "No Markdown version of this page is available.\n";
183
+ $headers = array(
184
+ 'Content-Type' => self::TEXT_CONTENT_TYPE,
185
+ 'Cache-Control' => 'no-store',
186
+ 'Vary' => 'Accept',
187
+ 'X-Robots-Tag' => 'noindex',
188
+ );
189
+ if ( $canonical ) {
190
+ $headers['Link'] = '<' . $canonical . '>; rel="canonical"';
191
+ }
192
+ $headers['Content-Length'] = (string) strlen( $body );
193
+ return array(
194
+ 'status' => (int) $status,
195
+ 'headers' => $headers,
196
+ 'body' => 'HEAD' === strtoupper( (string) $method ) ? '' : $body,
197
+ );
198
+ }
199
+
200
+ /**
201
+ * Write a built response. Removes headers WordPress already queued that
202
+ * do not belong on Markdown (HTML Link headers, no-cache from nocache_headers()).
203
+ * The caller exits afterwards.
204
+ *
205
+ * @param array $response From build() or plain().
206
+ * @return void
207
+ */
208
+ public static function send( $response ) {
209
+ if ( ! headers_sent() ) {
210
+ foreach ( array( 'Link', 'X-Pingback', 'Expires', 'Pragma', 'Last-Modified', 'Cache-Control', 'Content-Type', 'X-Robots-Tag' ) as $name ) {
211
+ header_remove( $name );
212
+ }
213
+ if ( function_exists( 'status_header' ) ) {
214
+ status_header( $response['status'] );
215
+ } else {
216
+ http_response_code( $response['status'] );
217
+ }
218
+ foreach ( $response['headers'] as $name => $value ) {
219
+ header( $name . ': ' . $value, true );
220
+ }
221
+ }
222
+ if ( '' !== $response['body'] ) {
223
+ echo $response['body']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- text/markdown or text/plain body, never HTML.
224
+ }
225
+ }
226
+ }