agentrail-edge 0.2.0 → 0.3.0
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/README.md +38 -128
- package/dist/edge.d.ts +15 -0
- package/dist/edge.js +69 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,48 +1,33 @@
|
|
|
1
1
|
# agentrail-edge
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
Agentrail
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
about one per round trip to the collector. A call that failed is tried
|
|
22
|
-
again after a random wait, so isolates that failed together do not retry
|
|
23
|
-
together, and never sooner than a `Retry-After` the collector sends. Every
|
|
24
|
-
call ends within 25 seconds of its request's answer, inside the 30 seconds
|
|
25
|
-
Cloudflare gives `waitUntil`; one that cannot is given up and told to
|
|
26
|
-
`onError`, never left for the platform to cancel unseen.
|
|
27
|
-
|
|
28
|
-
A visitor's session is a cookie the edge sets and the browser script reads
|
|
29
|
-
(`agentrail_session`). As in web analytics, it ends after 30 minutes
|
|
30
|
-
without a request, and a day after it began: every answer writes it again
|
|
31
|
-
for another 30 minutes.
|
|
3
|
+
[](https://www.npmjs.com/package/agentrail-edge)
|
|
4
|
+
[](https://github.com/agentrail-co/agentrail-js/blob/main/LICENSE)
|
|
5
|
+
|
|
6
|
+
The Agentrail edge runs in front of your site on Cloudflare Workers, Vercel
|
|
7
|
+
Routing Middleware or any Fetch API runtime. It records every request,
|
|
8
|
+
including those from agents that never run JavaScript, and marks the links in
|
|
9
|
+
answers meant for agents (Markdown, `llms.txt`), so the next request an agent
|
|
10
|
+
makes is joined to the page it came from.
|
|
11
|
+
|
|
12
|
+
Recording happens after the answer, so a visitor never waits for it and never
|
|
13
|
+
sees it fail. A visitor's address and any verified identity leave the edge only
|
|
14
|
+
as keyed hashes. With a request's `Accept` and `Accept-Encoding`, the edge
|
|
15
|
+
sends the names of its headers, never their values: the names the caller
|
|
16
|
+
wrote, lower-cased, without those your platform or a proxy added on the way
|
|
17
|
+
(`cf-*`, `x-vercel-*`, `x-forwarded-*` and the like), at most 40 in 512
|
|
18
|
+
characters.
|
|
19
|
+
|
|
20
|
+
## Install
|
|
32
21
|
|
|
33
22
|
```sh
|
|
34
23
|
npm install agentrail-edge
|
|
35
24
|
```
|
|
36
25
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
26
|
+
In Agentrail, open **Journeys → Set up** and choose **Edge**. Agentrail shows
|
|
27
|
+
the **Secret** and the **Hash key** once: keep them in your platform's
|
|
28
|
+
secrets as `AGENTRAIL_SECRET` and `AGENTRAIL_HASH_KEY`.
|
|
40
29
|
|
|
41
|
-
|
|
42
|
-
`AGENTRAIL_SECRET` and `AGENTRAIL_HASH_KEY` environment variables; the
|
|
43
|
-
collector's address is the one the setup page shows. Create the edge once,
|
|
44
|
-
on the first request, so the requests an isolate answers share calls; one
|
|
45
|
-
created per request still works, and sends a call per request.
|
|
30
|
+
## Quick start: Cloudflare Workers
|
|
46
31
|
|
|
47
32
|
```js
|
|
48
33
|
import { cloudflare, createEdge } from 'agentrail-edge';
|
|
@@ -57,104 +42,29 @@ export default {
|
|
|
57
42
|
secret: env.AGENTRAIL_SECRET,
|
|
58
43
|
},
|
|
59
44
|
hashKey: env.AGENTRAIL_HASH_KEY,
|
|
60
|
-
hosts: { 'docs.example.com': 'query' },
|
|
45
|
+
hosts: { 'docs.example.com': 'query' }, // your own hosts
|
|
61
46
|
});
|
|
62
47
|
return edge.handle(request, cloudflare(ctx));
|
|
63
48
|
},
|
|
64
49
|
};
|
|
65
50
|
```
|
|
66
51
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
## Backend trace handoff
|
|
74
|
-
|
|
75
|
-
Each forwarded request carries a new `traceparent` and an
|
|
76
|
-
`x-agentrail-trace-handoff` token authenticated with the workspace hash key.
|
|
77
|
-
The token binds the trace parent, key ID, issue time, HTTP method and final
|
|
78
|
-
origin path/query. The backend accepts it for 60 seconds. The caller's handoff,
|
|
79
|
-
tracestate and baggage are removed; the handoff response header is removed
|
|
80
|
-
before answering the visitor. A signing failure reaches `onError` as
|
|
81
|
-
`TraceHandoffSigningError` and the application request continues unsigned.
|
|
82
|
-
|
|
83
|
-
Apply path/query rewrites in `EdgeAdapter.prepareOriginRequest(request)`, or
|
|
84
|
-
`vercel({ waitUntil, prepareOriginRequest, origin })`. Signing happens after
|
|
85
|
-
that hook. The `origin` callback sends the prepared request; host-only changes
|
|
86
|
-
preserve the signature. A path/query change inside `origin` prevents backend
|
|
87
|
-
verification. `agentrail-node` starts a new trace for unsigned or refused
|
|
88
|
-
handoffs and keeps the caller's context as a directional span link.
|
|
89
|
-
|
|
90
|
-
## Options
|
|
91
|
-
|
|
92
|
-
- `hosts`: the site's own hosts, and how a link to each is marked: `'query'`
|
|
93
|
-
adds the `h` parameter, `'path'` an `/h/<marker>` prefix, for a host whose
|
|
94
|
-
calls an extra parameter would break.
|
|
95
|
-
- `queryAllowlist`: query parameters a recorded path keeps; every other is
|
|
96
|
-
dropped, and named as dropped.
|
|
97
|
-
- `identify(request)`: the verified principal behind a request, by the site's
|
|
98
|
-
own check of its method, URL and headers. It runs after the answer. A
|
|
99
|
-
check that throws is told to `onError` as an `IdentifyError`, and the
|
|
100
|
-
request is recorded without a principal.
|
|
101
|
-
- `ignoreRequest(request)`: requests passed to the origin untouched and
|
|
102
|
-
recorded nowhere. See [Your own servers](#your-own-servers).
|
|
103
|
-
- `sessionIdleTimeout`: seconds without a request after which a session
|
|
104
|
-
ends; 1800 (30 minutes) by default.
|
|
105
|
-
- `sessionCookieDomain`: the domain a session is shared across, as
|
|
106
|
-
`example.com` for `docs.example.com` and `app.example.com`; by default,
|
|
107
|
-
the one host. Give the browser script the same two values.
|
|
108
|
-
- `sessionCookie`, `service`, `fetch`: the session cookie's name, the
|
|
109
|
-
service the events name, and the fetch the collector's calls go through.
|
|
110
|
-
- `signers`: registrable hosts, beside the known signers, whose Web Bot
|
|
111
|
-
Auth key directories the edge may read; a signature by any other host is
|
|
112
|
-
recorded as a claim it did not check, and that host is never contacted.
|
|
113
|
-
- `onError(error)`: told of events that could not be sent, and of an
|
|
114
|
-
`identify` that threw; they are logged otherwise. Neither ever reaches the
|
|
115
|
-
visitor.
|
|
116
|
-
|
|
117
|
-
## Your own servers
|
|
118
|
-
|
|
119
|
-
Calls your own servers make through this edge are not visitors or agents. A
|
|
120
|
-
page rendered on the server that reads your API would otherwise record a step
|
|
121
|
-
for every read, all from a few addresses at a steady rate, and those steps
|
|
122
|
-
crowd out the journeys you want to see.
|
|
123
|
-
|
|
124
|
-
Leave them out with something only your servers can send:
|
|
125
|
-
|
|
126
|
-
```ts
|
|
127
|
-
const edge = createEdge({
|
|
128
|
-
// ...
|
|
129
|
-
ignoreRequest: (request) =>
|
|
130
|
-
request.headers.get('x-my-internal') === env.INTERNAL_SECRET,
|
|
131
|
-
});
|
|
132
|
-
```
|
|
52
|
+
Create the edge once, on the first request, as this does: the requests an
|
|
53
|
+
isolate answers then share calls to the collector. On Vercel, use
|
|
54
|
+
`vercel({ waitUntil, origin })` in Routing Middleware; the guide has the full
|
|
55
|
+
example.
|
|
56
|
+
|
|
57
|
+
## Documentation
|
|
133
58
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
asks who the caller is: the `Accept` sent, and the `Sec-Fetch-Site`, `-Mode`
|
|
143
|
-
and `-Dest` a browser sends and a programmatic fetcher does not; what the
|
|
144
|
-
platform says of the client, where it says anything (`cloudflare(ctx)` reads
|
|
145
|
-
`request.cf`: the autonomous system and country on every plan, the JA4
|
|
146
|
-
fingerprint and the verified-bot category where the zone has Bot Management;
|
|
147
|
-
`vercel()` reads `x-vercel-ip-country` and `x-vercel-ja4-digest`, and BotID's
|
|
148
|
-
verified bots stay in the app, where `checkBotId()` runs; an adapter of your
|
|
149
|
-
own gives `signals(request)` or nothing); and a Web Bot Auth signature,
|
|
150
|
-
checked against the keys the agent publishes. The check reads the agent's
|
|
151
|
-
key directory from the edge, once per its `max-age`, from the known signers'
|
|
152
|
-
hosts and those `signers` names and from no other, so a site's edge contacts
|
|
153
|
-
only hosts it has been told of; a directory that cannot be read leaves the
|
|
154
|
-
signature `directory_unreachable`, never `invalid`.
|
|
155
|
-
Nothing here is hashed, since nothing identifies a person: a fingerprint is
|
|
156
|
-
shared by every copy of one client.
|
|
59
|
+
| Topic | Guide |
|
|
60
|
+
| -------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
|
|
61
|
+
| Cloudflare and Vercel setup, every option, identifying a caller | [Install the edge](https://www.agentrail.co/docs/guides/journeys-edge) |
|
|
62
|
+
| Handing the trace to your servers, and rewriting paths before it | [Install the edge](https://www.agentrail.co/docs/guides/journeys-edge) |
|
|
63
|
+
| Keeping your own servers' calls out (`ignoreRequest`) | [Leave out your own servers](https://www.agentrail.co/docs/guides/journeys-own-servers) |
|
|
64
|
+
| What each request records, including the caller's facts and location | [What Agentrail records](https://www.agentrail.co/docs/guides/journeys-data) |
|
|
65
|
+
|
|
66
|
+
Before 1.0, a minor version (0.1 to 0.2) may change what the package offers.
|
|
157
67
|
|
|
158
68
|
## Licence
|
|
159
69
|
|
|
160
|
-
[Apache-2.0](LICENSE).
|
|
70
|
+
[Apache-2.0](https://github.com/agentrail-co/agentrail-js/blob/main/LICENSE).
|
package/dist/edge.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { type PrincipalKind } from 'agentrail-core';
|
|
2
2
|
import { type MarkedHosts } from './links.js';
|
|
3
|
+
/**
|
|
4
|
+
* The header names no caller writes: what a proxy or the platform added on
|
|
5
|
+
* the way (Cloudflare's `cf-*`, Vercel's `x-vercel-*`, `x-forwarded-*`), and
|
|
6
|
+
* Agentrail's own first-party mark. The contract's
|
|
7
|
+
* (`otlp.headerNames.addedOnTheWay`); the tests hold this copy to it.
|
|
8
|
+
*/
|
|
9
|
+
export declare const ADDED_ON_THE_WAY: RegExp;
|
|
10
|
+
/** The most header names an event keeps, and the most characters they take joined (`otlp.headerNames`). */
|
|
11
|
+
export declare const HEADER_NAMES_KEPT: {
|
|
12
|
+
readonly count: 40;
|
|
13
|
+
readonly characters: 512;
|
|
14
|
+
};
|
|
3
15
|
/** The site's `identify` threw: the request is recorded without a principal. */
|
|
4
16
|
export declare class IdentifyError extends Error {
|
|
5
17
|
readonly name = "IdentifyError";
|
|
@@ -82,6 +94,9 @@ export interface EdgeOptions {
|
|
|
82
94
|
* it tells anything: features for the collector's classifier, never keys.
|
|
83
95
|
*/
|
|
84
96
|
export interface PlatformSignals {
|
|
97
|
+
readonly city?: string | null;
|
|
98
|
+
readonly region?: string | null;
|
|
99
|
+
readonly source?: 'cloudflare' | 'vercel' | 'source';
|
|
85
100
|
/** The client's autonomous system number. */
|
|
86
101
|
readonly asn: number | null;
|
|
87
102
|
/** The client's country, two letters. */
|
package/dist/edge.js
CHANGED
|
@@ -15,7 +15,8 @@ import { MARKER_PARAMETER, markLinks } from './links.js';
|
|
|
15
15
|
* - the client address and any verified principal, hashed with the
|
|
16
16
|
* workspace's key before anything leaves;
|
|
17
17
|
* - what the caller and the network said of it, for the collector's
|
|
18
|
-
* classifier of who the caller is: the `Accept
|
|
18
|
+
* classifier of who the caller is: the `Accept`, the caller's header names
|
|
19
|
+
* and `Accept-Encoding`, and fetch metadata, the
|
|
19
20
|
* platform's word on the network and its verified bots, and a Web Bot
|
|
20
21
|
* Auth signature checked against the keys the agent publishes.
|
|
21
22
|
*
|
|
@@ -38,6 +39,17 @@ const MARKABLE_BYTES = 2 * 1024 * 1024;
|
|
|
38
39
|
const SEND_WITHIN_MS = 25_000;
|
|
39
40
|
/** The longest `Accept` the collector takes; a longer one is cut. */
|
|
40
41
|
const ACCEPT_LENGTH = 256;
|
|
42
|
+
/** The longest `Accept-Encoding` the collector takes; a longer one is cut. */
|
|
43
|
+
const ACCEPT_ENCODING_LENGTH = 128;
|
|
44
|
+
/**
|
|
45
|
+
* The header names no caller writes: what a proxy or the platform added on
|
|
46
|
+
* the way (Cloudflare's `cf-*`, Vercel's `x-vercel-*`, `x-forwarded-*`), and
|
|
47
|
+
* Agentrail's own first-party mark. The contract's
|
|
48
|
+
* (`otlp.headerNames.addedOnTheWay`); the tests hold this copy to it.
|
|
49
|
+
*/
|
|
50
|
+
export const ADDED_ON_THE_WAY = /^(?:x-forwarded-.*|x-amzn-.*|x-vercel-.*|cf-.*|forwarded|via|x-real-ip|cdn-loop|true-client-ip|agentrail-first-party)$/iu;
|
|
51
|
+
/** The most header names an event keeps, and the most characters they take joined (`otlp.headerNames`). */
|
|
52
|
+
export const HEADER_NAMES_KEPT = { count: 40, characters: 512 };
|
|
41
53
|
/** The longest `Sec-Fetch-*` value the collector takes; the browsers' are one word. */
|
|
42
54
|
const FETCH_METADATA_LENGTH = 32;
|
|
43
55
|
/** The longest JA4 the collector takes. */
|
|
@@ -313,6 +325,16 @@ export function createEdge(options) {
|
|
|
313
325
|
linksOut: [...observed.linksOut],
|
|
314
326
|
},
|
|
315
327
|
actor: {
|
|
328
|
+
location: signals === null ||
|
|
329
|
+
(signals.country === null && !signals.city && !signals.region)
|
|
330
|
+
? null
|
|
331
|
+
: {
|
|
332
|
+
country: signals.country,
|
|
333
|
+
city: cut(signals.city, 128),
|
|
334
|
+
region: cut(signals.region, 128),
|
|
335
|
+
source: signals.source ?? 'source',
|
|
336
|
+
observedAt: new Date(input.start).toISOString(),
|
|
337
|
+
},
|
|
316
338
|
egress: input.address === null
|
|
317
339
|
? null
|
|
318
340
|
: await keyedHash(hashKey, input.address),
|
|
@@ -321,6 +343,7 @@ export function createEdge(options) {
|
|
|
321
343
|
clientVersion: null,
|
|
322
344
|
protocol: null,
|
|
323
345
|
accept: cut(request.headers.get('accept'), ACCEPT_LENGTH),
|
|
346
|
+
transport: transportOf(request.headers),
|
|
324
347
|
fetchMetadata: fetchMetadataOf(request.headers),
|
|
325
348
|
network: signals === null
|
|
326
349
|
? null
|
|
@@ -490,6 +513,33 @@ function cut(value, length) {
|
|
|
490
513
|
? value.slice(0, length)
|
|
491
514
|
: null;
|
|
492
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* How the caller's HTTP library wrote the request: its header names, the
|
|
518
|
+
* caller's own only, as a Fetch `Headers` holds them (lower-cased and
|
|
519
|
+
* sorted), within what an event keeps, a name past that left out whole; and
|
|
520
|
+
* its `Accept-Encoding`. Names only, never a value. Null where no name is
|
|
521
|
+
* the caller's.
|
|
522
|
+
*/
|
|
523
|
+
function transportOf(headers) {
|
|
524
|
+
const kept = [];
|
|
525
|
+
let length = 0;
|
|
526
|
+
for (const name of headers.keys()) {
|
|
527
|
+
if (ADDED_ON_THE_WAY.test(name))
|
|
528
|
+
continue;
|
|
529
|
+
const grown = length + name.length + (kept.length === 0 ? 0 : 1);
|
|
530
|
+
if (kept.length === HEADER_NAMES_KEPT.count ||
|
|
531
|
+
grown > HEADER_NAMES_KEPT.characters)
|
|
532
|
+
break;
|
|
533
|
+
kept.push(name);
|
|
534
|
+
length = grown;
|
|
535
|
+
}
|
|
536
|
+
return kept.length === 0
|
|
537
|
+
? null
|
|
538
|
+
: {
|
|
539
|
+
headerNames: kept.join(','),
|
|
540
|
+
acceptEncoding: cut(headers.get('accept-encoding'), ACCEPT_ENCODING_LENGTH),
|
|
541
|
+
};
|
|
542
|
+
}
|
|
493
543
|
/** `Sec-Fetch-Site`, `-Mode` and `-Dest` together, as a browser sends all three. */
|
|
494
544
|
function fetchMetadataOf(headers) {
|
|
495
545
|
const site = cut(headers.get('sec-fetch-site'), FETCH_METADATA_LENGTH);
|
|
@@ -500,6 +550,18 @@ function fetchMetadataOf(headers) {
|
|
|
500
550
|
: { site, mode, dest };
|
|
501
551
|
}
|
|
502
552
|
/** A two-letter country, as the platforms give one; anything else is none. */
|
|
553
|
+
function decodedCity(value) {
|
|
554
|
+
if (value === null)
|
|
555
|
+
return null;
|
|
556
|
+
try {
|
|
557
|
+
return cut(decodeURIComponent(value), 128);
|
|
558
|
+
}
|
|
559
|
+
catch (error) {
|
|
560
|
+
if (error instanceof URIError)
|
|
561
|
+
return null;
|
|
562
|
+
throw error;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
503
565
|
function countryOf(value) {
|
|
504
566
|
return typeof value === 'string' && /^[A-Z]{2}$/u.test(value) ? value : null;
|
|
505
567
|
}
|
|
@@ -521,6 +583,9 @@ function cloudflareSignals(request) {
|
|
|
521
583
|
return {
|
|
522
584
|
asn: typeof cf?.asn === 'number' ? cf.asn : null,
|
|
523
585
|
country: countryOf(cf?.country),
|
|
586
|
+
city: typeof cf?.city === 'string' ? cut(cf.city, 128) : null,
|
|
587
|
+
region: typeof cf?.region === 'string' ? cut(cf.region, 128) : null,
|
|
588
|
+
source: 'cloudflare',
|
|
524
589
|
ja4: typeof bot?.ja4 === 'string' ? bot.ja4 : null,
|
|
525
590
|
verifiedBot: category === null ? null : { category, name: null },
|
|
526
591
|
};
|
|
@@ -553,6 +618,9 @@ export function vercel(options) {
|
|
|
553
618
|
signals: (request) => ({
|
|
554
619
|
asn: null,
|
|
555
620
|
country: countryOf(request.headers.get('x-vercel-ip-country')),
|
|
621
|
+
city: decodedCity(request.headers.get('x-vercel-ip-city')),
|
|
622
|
+
region: cut(request.headers.get('x-vercel-ip-country-region'), 128),
|
|
623
|
+
source: 'vercel',
|
|
556
624
|
ja4: request.headers.get('x-vercel-ja4-digest'),
|
|
557
625
|
verifiedBot: null,
|
|
558
626
|
}),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentrail-edge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "The Agentrail edge: in front of a site on Cloudflare Workers, Vercel or any Fetch API runtime, it records each request and marks the links agents follow.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"node": ">=22"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"agentrail-core": "0.
|
|
28
|
+
"agentrail-core": "0.3.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "24.13.3",
|