@relvehq/agent-ready 0.0.0-stage → 0.2.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.
- package/LICENSE +21 -0
- package/README.md +338 -3
- package/SETUP.md +337 -0
- package/dist/bin/agent-ready.d.ts +23 -0
- package/dist/bin/agent-ready.js +350 -0
- package/dist/cli/init.d.ts +70 -0
- package/dist/cli/init.js +366 -0
- package/dist/core/accept.d.ts +8 -0
- package/dist/core/accept.js +76 -0
- package/dist/core/config.d.ts +145 -0
- package/dist/core/config.js +103 -0
- package/dist/core/convert.d.ts +22 -0
- package/dist/core/convert.js +334 -0
- package/dist/core/detect.d.ts +30 -0
- package/dist/core/detect.js +82 -0
- package/dist/core/easy.d.ts +19 -0
- package/dist/core/easy.js +117 -0
- package/dist/core/enrich.d.ts +30 -0
- package/dist/core/enrich.js +155 -0
- package/dist/core/llms.d.ts +11 -0
- package/dist/core/llms.js +71 -0
- package/dist/core/report.d.ts +15 -0
- package/dist/core/report.js +46 -0
- package/dist/core/resolve.d.ts +43 -0
- package/dist/core/resolve.js +204 -0
- package/dist/core/respond.d.ts +18 -0
- package/dist/core/respond.js +59 -0
- package/dist/core/sitemap.d.ts +31 -0
- package/dist/core/sitemap.js +140 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +76 -0
- package/dist/next/dashboard.d.ts +14 -0
- package/dist/next/dashboard.js +56 -0
- package/dist/next/index.d.ts +42 -0
- package/dist/next/index.js +100 -0
- package/dist/next/route.d.ts +26 -0
- package/dist/next/route.js +167 -0
- package/dist/node/index.d.ts +8 -0
- package/dist/node/index.js +191 -0
- package/dist/proof/audit.d.ts +47 -0
- package/dist/proof/audit.js +253 -0
- package/dist/proof/bots.d.ts +24 -0
- package/dist/proof/bots.js +60 -0
- package/dist/proof/dashboard.d.ts +8 -0
- package/dist/proof/dashboard.js +77 -0
- package/dist/proof/hits.d.ts +18 -0
- package/dist/proof/hits.js +51 -0
- package/dist/proof/jsonl.d.ts +5 -0
- package/dist/proof/jsonl.js +80 -0
- package/dist/proof/stats.d.ts +39 -0
- package/dist/proof/stats.js +127 -0
- package/dist/refine/check.d.ts +17 -0
- package/dist/refine/check.js +134 -0
- package/dist/refine/extract.d.ts +7 -0
- package/dist/refine/extract.js +120 -0
- package/dist/refine/file-store.d.ts +6 -0
- package/dist/refine/file-store.js +87 -0
- package/dist/refine/model.d.ts +30 -0
- package/dist/refine/model.js +105 -0
- package/dist/refine/prompts.d.ts +49 -0
- package/dist/refine/prompts.js +178 -0
- package/dist/refine/refine.d.ts +38 -0
- package/dist/refine/refine.js +75 -0
- package/dist/refine/run.d.ts +52 -0
- package/dist/refine/run.js +66 -0
- package/dist/refine/types.d.ts +17 -0
- package/dist/refine/types.js +8 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-admin.php +363 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-converter.php +762 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-llms.php +192 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-markdown-builder.php +447 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-negotiation.php +334 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-response.php +226 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-router.php +421 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-settings.php +115 -0
- package/dist/wp/relve-agent-ready/includes/class-relve-ar-store.php +543 -0
- package/dist/wp/relve-agent-ready/includes/index.php +2 -0
- package/dist/wp/relve-agent-ready/index.php +2 -0
- package/dist/wp/relve-agent-ready/readme.txt +75 -0
- package/dist/wp/relve-agent-ready/relve-agent-ready.php +70 -0
- package/dist/wp/relve-agent-ready/uninstall.php +24 -0
- package/package.json +64 -6
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Negotiation: does this request want Markdown, and for which path?
|
|
4
|
+
*
|
|
5
|
+
* PHP port of @relvehq/agent-ready core/accept.ts and core/detect.ts. Keep the
|
|
6
|
+
* two in step: the cases in tests/run.php mirror test/detect.test.ts.
|
|
7
|
+
*
|
|
8
|
+
* Accept rule: serve Markdown only when `text/markdown` is listed explicitly
|
|
9
|
+
* and its q-value is greater than or equal to the q-value that applies to
|
|
10
|
+
* `text/html`. Ties go to Markdown. Wildcards never select Markdown on their
|
|
11
|
+
* own, but they count towards HTML's q-value. The user agent is never an
|
|
12
|
+
* input (choosing by user agent is cloaking).
|
|
13
|
+
*
|
|
14
|
+
* Three ways to ask, in order of precedence: a `.md` suffix, `?format=md`
|
|
15
|
+
* (also `format=markdown`), the Accept header. Plus the llms.txt path(s).
|
|
16
|
+
* Only GET and HEAD are ever considered.
|
|
17
|
+
*
|
|
18
|
+
* Pure PHP: no WordPress calls, so it is unit-testable without WordPress.
|
|
19
|
+
*
|
|
20
|
+
* @package Relve_Agent_Ready
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
if ( ! defined( 'ABSPATH' ) ) {
|
|
24
|
+
exit;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Accept negotiation and request detection.
|
|
29
|
+
*/
|
|
30
|
+
class Relve_AR_Negotiation {
|
|
31
|
+
|
|
32
|
+
/** Longer Accept headers are treated as absent (HTML). */
|
|
33
|
+
const MAX_ACCEPT_LENGTH = 4096;
|
|
34
|
+
|
|
35
|
+
/** Longer paths are never Markdown requests. */
|
|
36
|
+
const MAX_PATH_LENGTH = 1024;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Which format does this Accept header prefer?
|
|
40
|
+
*
|
|
41
|
+
* @param string|null $accept The Accept header, or null when absent.
|
|
42
|
+
* @return string "markdown" or "html".
|
|
43
|
+
*/
|
|
44
|
+
public static function choose_format( $accept ) {
|
|
45
|
+
if ( ! is_string( $accept ) || '' === $accept || strlen( $accept ) > self::MAX_ACCEPT_LENGTH ) {
|
|
46
|
+
return 'html';
|
|
47
|
+
}
|
|
48
|
+
$ranges = self::parse_accept( $accept );
|
|
49
|
+
$markdown_q = self::explicit_q( $ranges, 'text', 'markdown' );
|
|
50
|
+
if ( null === $markdown_q || $markdown_q <= 0 ) {
|
|
51
|
+
return 'html';
|
|
52
|
+
}
|
|
53
|
+
return $markdown_q >= self::html_q( $ranges ) ? 'markdown' : 'html';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Parse an Accept header into media ranges.
|
|
58
|
+
*
|
|
59
|
+
* @param string $accept Header value.
|
|
60
|
+
* @return array[] List of array( 'type' => , 'subtype' => , 'q' => float ).
|
|
61
|
+
*/
|
|
62
|
+
public static function parse_accept( $accept ) {
|
|
63
|
+
$ranges = array();
|
|
64
|
+
foreach ( explode( ',', $accept ) as $part ) {
|
|
65
|
+
$params = explode( ';', $part );
|
|
66
|
+
$media_type = strtolower( trim( (string) array_shift( $params ) ) );
|
|
67
|
+
$bits = explode( '/', $media_type );
|
|
68
|
+
if ( count( $bits ) !== 2 || '' === $bits[0] || '' === $bits[1] ) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
$q = 1.0;
|
|
72
|
+
foreach ( $params as $param ) {
|
|
73
|
+
$kv = explode( '=', $param );
|
|
74
|
+
$name = strtolower( trim( $kv[0] ) );
|
|
75
|
+
if ( 'q' !== $name ) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
$value = isset( $kv[1] ) ? trim( $kv[1] ) : '';
|
|
79
|
+
$q = self::parse_q( $value );
|
|
80
|
+
}
|
|
81
|
+
$ranges[] = array(
|
|
82
|
+
'type' => $bits[0],
|
|
83
|
+
'subtype' => $bits[1],
|
|
84
|
+
'q' => $q,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return $ranges;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* RFC 9110 qvalue. Malformed means unacceptable (0), never preferred.
|
|
92
|
+
*
|
|
93
|
+
* @param string $value Raw q value.
|
|
94
|
+
* @return float
|
|
95
|
+
*/
|
|
96
|
+
public static function parse_q( $value ) {
|
|
97
|
+
if ( ! preg_match( '/^(0(\.\d{0,3})?|1(\.0{0,3})?)$/', $value ) ) {
|
|
98
|
+
return 0.0;
|
|
99
|
+
}
|
|
100
|
+
return (float) $value;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Highest q given to exactly type/subtype, or null if not listed.
|
|
105
|
+
*
|
|
106
|
+
* @param array[] $ranges Parsed ranges.
|
|
107
|
+
* @param string $type Type.
|
|
108
|
+
* @param string $subtype Subtype.
|
|
109
|
+
* @return float|null
|
|
110
|
+
*/
|
|
111
|
+
private static function explicit_q( $ranges, $type, $subtype ) {
|
|
112
|
+
$best = null;
|
|
113
|
+
foreach ( $ranges as $r ) {
|
|
114
|
+
if ( $r['type'] === $type && $r['subtype'] === $subtype ) {
|
|
115
|
+
$best = max( null === $best ? 0.0 : $best, $r['q'] );
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return $best;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* The q that applies to text/html: text/html, then text/*, then *\/*.
|
|
123
|
+
*
|
|
124
|
+
* @param array[] $ranges Parsed ranges.
|
|
125
|
+
* @return float
|
|
126
|
+
*/
|
|
127
|
+
private static function html_q( $ranges ) {
|
|
128
|
+
foreach ( array( array( 'text', 'html' ), array( 'text', '*' ), array( '*', '*' ) ) as $pair ) {
|
|
129
|
+
$q = self::explicit_q( $ranges, $pair[0], $pair[1] );
|
|
130
|
+
if ( null !== $q ) {
|
|
131
|
+
return $q;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return 0.0;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Leading slash, no trailing slash, no query or hash. Null for anything
|
|
139
|
+
* that is not a plain path (traversal, backslashes, "//host", too long).
|
|
140
|
+
*
|
|
141
|
+
* @param mixed $raw Path.
|
|
142
|
+
* @return string|null
|
|
143
|
+
*/
|
|
144
|
+
public static function normalize_path( $raw ) {
|
|
145
|
+
if ( ! is_string( $raw ) ) {
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
$path = explode( '#', explode( '?', $raw, 2 )[0], 2 )[0];
|
|
149
|
+
if ( '' === $path || '/' !== $path[0] || strlen( $path ) > self::MAX_PATH_LENGTH ) {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
if ( false !== strpos( $path, '..' ) || false !== strpos( $path, '\\' ) || preg_match( '#//.#', $path ) ) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
$trimmed = rtrim( $path, '/' );
|
|
156
|
+
return '' === $trimmed ? '/' : $trimmed;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* "/x.md" -> "/x"; "<home>/index.md" -> "<home>/" ("/" when home is the
|
|
161
|
+
* root); null when not a .md URL or a dotfile ("/.md", "/docs/.md").
|
|
162
|
+
*
|
|
163
|
+
* @param string $pathname Request path.
|
|
164
|
+
* @param string $home_path Home path without trailing slash ("" or "/blog").
|
|
165
|
+
* @return string|null
|
|
166
|
+
*/
|
|
167
|
+
public static function strip_md_suffix( $pathname, $home_path = '' ) {
|
|
168
|
+
$lower = strtolower( $pathname );
|
|
169
|
+
if ( substr( $lower, -3 ) !== '.md' ) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
if ( strtolower( $home_path . '/index.md' ) === $lower ) {
|
|
173
|
+
return $home_path . '/';
|
|
174
|
+
}
|
|
175
|
+
$base = substr( $pathname, 0, -3 );
|
|
176
|
+
if ( '' === $base || '/' === substr( $base, -1 ) ) {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
return $base;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* "/x" -> "/x.md"; the home path -> "<home>/index.md".
|
|
184
|
+
*
|
|
185
|
+
* @param string $path Normalised path.
|
|
186
|
+
* @param string $home_path Home path without trailing slash.
|
|
187
|
+
* @return string
|
|
188
|
+
*/
|
|
189
|
+
public static function md_url_path( $path, $home_path = '' ) {
|
|
190
|
+
$path = rtrim( $path, '/' );
|
|
191
|
+
if ( '' === $path || $path === $home_path ) {
|
|
192
|
+
return $home_path . '/index.md';
|
|
193
|
+
}
|
|
194
|
+
return $path . '.md';
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Detect a Markdown request.
|
|
199
|
+
*
|
|
200
|
+
* @param string $method HTTP method.
|
|
201
|
+
* @param string $uri Raw REQUEST_URI (path plus query).
|
|
202
|
+
* @param string $accept Accept header ('' when absent).
|
|
203
|
+
* @param string $home_path Home path without trailing slash.
|
|
204
|
+
* @param string[] $llms_paths Paths llms.txt is answered at (empty = none).
|
|
205
|
+
* @return array {
|
|
206
|
+
* @type bool $markdown Is this a Markdown request?
|
|
207
|
+
* @type string|null $via suffix|query|accept|llms, null when not.
|
|
208
|
+
* @type string $path Normalised page path without ".md".
|
|
209
|
+
* @type bool $llms Is this the llms.txt request?
|
|
210
|
+
* @type string|null $uri REQUEST_URI for WordPress to parse (".md" and format removed), null to keep.
|
|
211
|
+
* }
|
|
212
|
+
*/
|
|
213
|
+
public static function detect( $method, $uri, $accept, $home_path = '', $llms_paths = array() ) {
|
|
214
|
+
$no = array(
|
|
215
|
+
'markdown' => false,
|
|
216
|
+
'via' => null,
|
|
217
|
+
'path' => '/',
|
|
218
|
+
'llms' => false,
|
|
219
|
+
'uri' => null,
|
|
220
|
+
);
|
|
221
|
+
$method = strtoupper( (string) $method );
|
|
222
|
+
if ( 'GET' !== $method && 'HEAD' !== $method ) {
|
|
223
|
+
return $no;
|
|
224
|
+
}
|
|
225
|
+
$uri = (string) $uri;
|
|
226
|
+
$parts = explode( '?', $uri, 2 );
|
|
227
|
+
$pathname = explode( '#', $parts[0], 2 )[0];
|
|
228
|
+
$query = isset( $parts[1] ) ? explode( '#', $parts[1], 2 )[0] : '';
|
|
229
|
+
|
|
230
|
+
$format = '';
|
|
231
|
+
$kept = array();
|
|
232
|
+
if ( '' !== $query ) {
|
|
233
|
+
foreach ( explode( '&', $query ) as $pair ) {
|
|
234
|
+
if ( '' === $pair ) {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
$kv = explode( '=', $pair, 2 );
|
|
238
|
+
$name = urldecode( $kv[0] );
|
|
239
|
+
if ( 'format' === $name ) {
|
|
240
|
+
$format = strtolower( urldecode( isset( $kv[1] ) ? $kv[1] : '' ) );
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
$kept[] = $pair;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
$search = $kept ? '?' . implode( '&', $kept ) : '';
|
|
247
|
+
|
|
248
|
+
if ( in_array( $pathname, (array) $llms_paths, true ) ) {
|
|
249
|
+
return array(
|
|
250
|
+
'markdown' => true,
|
|
251
|
+
'via' => 'llms',
|
|
252
|
+
'path' => $pathname,
|
|
253
|
+
'llms' => true,
|
|
254
|
+
'uri' => null,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
$stripped = self::strip_md_suffix( $pathname, $home_path );
|
|
259
|
+
if ( null !== $stripped ) {
|
|
260
|
+
$path = self::normalize_path( $stripped );
|
|
261
|
+
if ( null === $path ) {
|
|
262
|
+
return $no;
|
|
263
|
+
}
|
|
264
|
+
// WordPress parses the path without ".md". Keep the slash a
|
|
265
|
+
// "<home>/index.md" mapped to, so the home path itself parses.
|
|
266
|
+
$wp_path = ( '/' === substr( $stripped, -1 ) ) ? ( '/' === $path ? '/' : $path . '/' ) : $path;
|
|
267
|
+
return array(
|
|
268
|
+
'markdown' => true,
|
|
269
|
+
'via' => 'suffix',
|
|
270
|
+
'path' => $path,
|
|
271
|
+
'llms' => false,
|
|
272
|
+
'uri' => $wp_path . $search,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
$path = self::normalize_path( $pathname );
|
|
277
|
+
if ( null === $path ) {
|
|
278
|
+
return $no;
|
|
279
|
+
}
|
|
280
|
+
if ( 'md' === $format || 'markdown' === $format ) {
|
|
281
|
+
return array(
|
|
282
|
+
'markdown' => true,
|
|
283
|
+
'via' => 'query',
|
|
284
|
+
'path' => $path,
|
|
285
|
+
'llms' => false,
|
|
286
|
+
'uri' => $pathname . $search,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
if ( 'markdown' === self::choose_format( $accept ) ) {
|
|
290
|
+
return array(
|
|
291
|
+
'markdown' => true,
|
|
292
|
+
'via' => 'accept',
|
|
293
|
+
'path' => $path,
|
|
294
|
+
'llms' => false,
|
|
295
|
+
'uri' => null,
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
$no['path'] = $path;
|
|
299
|
+
return $no;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Segment-aware prefix match, like the package's isExcluded: "/cart"
|
|
304
|
+
* excludes "/cart" and "/cart/x", not "/cartoon". Rules are matched
|
|
305
|
+
* against the full path and the path relative to the home path.
|
|
306
|
+
*
|
|
307
|
+
* @param string $path Normalised path.
|
|
308
|
+
* @param string[] $rules Path prefixes.
|
|
309
|
+
* @param string $home_path Home path without trailing slash.
|
|
310
|
+
* @return bool
|
|
311
|
+
*/
|
|
312
|
+
public static function is_excluded( $path, $rules, $home_path = '' ) {
|
|
313
|
+
$candidates = array( $path );
|
|
314
|
+
if ( '' !== $home_path && ( $path === $home_path || 0 === strpos( $path, $home_path . '/' ) ) ) {
|
|
315
|
+
$rel = substr( $path, strlen( $home_path ) );
|
|
316
|
+
$candidates[] = '' === $rel ? '/' : $rel;
|
|
317
|
+
}
|
|
318
|
+
foreach ( (array) $rules as $rule ) {
|
|
319
|
+
$p = rtrim( trim( (string) $rule ), '/' );
|
|
320
|
+
if ( '' === $p ) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
if ( '/' !== $p[0] ) {
|
|
324
|
+
$p = '/' . $p;
|
|
325
|
+
}
|
|
326
|
+
foreach ( $candidates as $c ) {
|
|
327
|
+
if ( $c === $p || 0 === strpos( $c, $p . '/' ) ) {
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
@@ -0,0 +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
|
+
}
|