@devtune/ai-traffic 0.1.2 → 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/README.md +156 -62
- package/dist/cjs/client.js +505 -0
- package/dist/cjs/client.js.map +1 -0
- package/dist/cjs/detection.js +47 -0
- package/dist/cjs/detection.js.map +1 -0
- package/dist/cjs/express.js +61 -0
- package/dist/cjs/express.js.map +1 -0
- package/dist/cjs/index.js +25 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/node.js +60 -0
- package/dist/cjs/node.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/registry.js +264 -0
- package/dist/cjs/registry.js.map +1 -0
- package/dist/client.d.ts +40 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +150 -19
- package/dist/client.js.map +1 -1
- package/dist/detection.d.ts +51 -0
- package/dist/detection.d.ts.map +1 -0
- package/dist/detection.js +42 -0
- package/dist/detection.js.map +1 -0
- package/dist/express.d.ts +24 -0
- package/dist/express.d.ts.map +1 -0
- package/dist/express.js +58 -0
- package/dist/express.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +23 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +57 -0
- package/dist/node.js.map +1 -0
- package/package.json +15 -2
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
# @devtune/ai-traffic
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Cloudflare-proxied sites should prefer the Cloudflare pull integration when available. For Vercel and self-hosted sites, this package keeps volume low by filtering at your edge instead of shipping full request logs.
|
|
3
|
+
Measure which AI crawlers visit your site with a dependency-free sensor that filters at the edge and forwards only matched machine traffic to DevTune.
|
|
6
4
|
|
|
7
5
|
## Install
|
|
8
6
|
|
|
@@ -10,118 +8,214 @@ Cloudflare-proxied sites should prefer the Cloudflare pull integration when avai
|
|
|
10
8
|
pnpm add @devtune/ai-traffic
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
Create a project-scoped server-side ingest key and expose it only to your server runtime:
|
|
14
12
|
|
|
15
13
|
```bash
|
|
16
14
|
DEVTUNE_AI_TRAFFIC_INGEST_KEY=dt_ingest_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
|
17
15
|
```
|
|
18
16
|
|
|
19
|
-
##
|
|
17
|
+
## Quickstarts
|
|
18
|
+
|
|
19
|
+
### Next.js
|
|
20
20
|
|
|
21
|
-
For Next.js 16, add `proxy.ts
|
|
21
|
+
For Next.js 16, add `proxy.ts`. For older Next.js projects, use the same body in `middleware.ts` and export `middleware()` instead.
|
|
22
22
|
|
|
23
23
|
```ts
|
|
24
24
|
// proxy.ts
|
|
25
|
-
import {
|
|
25
|
+
import { createDevTuneAiTrafficMiddleware } from "@devtune/ai-traffic";
|
|
26
26
|
import { NextResponse, type NextFetchEvent, type NextRequest } from "next/server";
|
|
27
27
|
|
|
28
|
-
const
|
|
28
|
+
const trackAiTraffic = createDevTuneAiTrafficMiddleware({
|
|
29
29
|
ingestKey: process.env.DEVTUNE_AI_TRAFFIC_INGEST_KEY!,
|
|
30
|
-
batchSize: 1,
|
|
31
|
-
defaultStatus: null,
|
|
32
|
-
flushIntervalMs: 0,
|
|
33
|
-
waitUntilRegistryRefresh: false,
|
|
34
30
|
});
|
|
35
31
|
|
|
36
32
|
export function proxy(request: NextRequest, event: NextFetchEvent) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
aiTraffic.trackRequest(request, event);
|
|
33
|
+
trackAiTraffic(request, event);
|
|
40
34
|
|
|
41
|
-
return
|
|
35
|
+
return NextResponse.next();
|
|
42
36
|
}
|
|
43
37
|
```
|
|
44
38
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
When your proxy or middleware returns a response directly, pass that exact status:
|
|
39
|
+
### Express
|
|
48
40
|
|
|
49
41
|
```ts
|
|
50
|
-
|
|
51
|
-
|
|
42
|
+
import express from "express";
|
|
43
|
+
import { createDevTuneAiTrafficExpressMiddleware } from "@devtune/ai-traffic/express";
|
|
52
44
|
|
|
53
|
-
|
|
45
|
+
const app = express();
|
|
54
46
|
|
|
55
|
-
|
|
56
|
-
|
|
47
|
+
app.use(
|
|
48
|
+
createDevTuneAiTrafficExpressMiddleware({
|
|
49
|
+
ingestKey: process.env.DEVTUNE_AI_TRAFFIC_INGEST_KEY!,
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
57
52
|
```
|
|
58
53
|
|
|
59
|
-
|
|
54
|
+
The middleware calls `next()` immediately and records the actual response status after Express emits `finish`.
|
|
55
|
+
|
|
56
|
+
### Node HTTP
|
|
60
57
|
|
|
61
58
|
```ts
|
|
62
|
-
import {
|
|
63
|
-
import { NextResponse, type NextFetchEvent, type NextRequest } from "next/server";
|
|
59
|
+
import { createServer } from "node:http";
|
|
64
60
|
|
|
65
|
-
|
|
61
|
+
import { createDevTuneAiTrafficNodeHandler } from "@devtune/ai-traffic/node";
|
|
62
|
+
|
|
63
|
+
const trackAiTraffic = createDevTuneAiTrafficNodeHandler({
|
|
66
64
|
ingestKey: process.env.DEVTUNE_AI_TRAFFIC_INGEST_KEY!,
|
|
67
65
|
});
|
|
68
66
|
|
|
69
|
-
|
|
70
|
-
trackAiTraffic(request,
|
|
67
|
+
createServer((request, response) => {
|
|
68
|
+
trackAiTraffic(request, response);
|
|
71
69
|
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
response.statusCode = 200;
|
|
71
|
+
response.end("ok");
|
|
72
|
+
}).listen(3000);
|
|
74
73
|
```
|
|
75
74
|
|
|
76
|
-
The
|
|
75
|
+
The hook records the final `response.statusCode` without blocking or changing the response.
|
|
77
76
|
|
|
78
|
-
##
|
|
77
|
+
## Privacy by Design
|
|
79
78
|
|
|
80
|
-
|
|
79
|
+
The sensor filters requests where your application runs. It uses a cheap user-agent hint before consulting the AI bot registry, and only matched machine hits are eligible to be forwarded. It uses no cookies, no fingerprinting, and no full request logs.
|
|
81
80
|
|
|
82
|
-
|
|
81
|
+
For a matched crawler, DevTune receives only:
|
|
82
|
+
|
|
83
|
+
- `path`
|
|
84
|
+
- origin plus path, with query parameters removed
|
|
85
|
+
- user agent
|
|
86
|
+
- response status when the adapter can know it
|
|
87
|
+
- timestamp
|
|
88
|
+
|
|
89
|
+
Request bodies, cookies, IP addresses, unrelated headers, and query-derived identifiers are not sent.
|
|
90
|
+
|
|
91
|
+
## Detect Without Sending
|
|
92
|
+
|
|
93
|
+
The standalone classifiers need no ingest key and never make a network request or push data to DevTune:
|
|
83
94
|
|
|
84
95
|
```ts
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
+
import { detectAiCrawler, detectAiReferrer } from "@devtune/ai-traffic";
|
|
97
|
+
|
|
98
|
+
const crawler = detectAiCrawler(request);
|
|
99
|
+
const referrer = detectAiReferrer(request);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Each function also accepts the relevant string directly: a user-agent for `detectAiCrawler()` or a referrer URL for `detectAiReferrer()`.
|
|
103
|
+
|
|
104
|
+
`detectAiCrawler()` returns the matched bot registry entry or `null`:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
type AiCrawlerDetection = {
|
|
108
|
+
uaPattern: string;
|
|
109
|
+
llmPlatform: string;
|
|
110
|
+
botClass: "training_crawler" | "index_bot" | "answer_fetcher" | "acting_agent";
|
|
111
|
+
label: string;
|
|
112
|
+
status?: "active" | "retired";
|
|
113
|
+
asnHints?: number[] | null;
|
|
114
|
+
notes?: string | null;
|
|
115
|
+
};
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
`detectAiReferrer()` returns the matched referrer registry entry or `null`:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
type AiReferrerDetection = {
|
|
122
|
+
hostname: string;
|
|
123
|
+
llmPlatform: string;
|
|
124
|
+
label: string;
|
|
125
|
+
};
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Both use bundled registry snapshots by default, so they work offline and in CI. Pass `registryEntries` as the second argument when you need to classify against a pinned or private registry:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
const match = detectAiCrawler(userAgent, {
|
|
132
|
+
registryEntries: myRegistryEntries,
|
|
133
|
+
});
|
|
96
134
|
```
|
|
97
135
|
|
|
98
|
-
|
|
136
|
+
## Machine Traffic and Human Referrals
|
|
137
|
+
|
|
138
|
+
The sensor deliberately measures machine traffic only. DevTune gets human visits from AI products through its GA4 integration, where sessions, engagement, and conversions provide a richer picture than middleware referrer matching. The referrer detector is available for local classification, but the sensor adapters never forward human referral visits.
|
|
139
|
+
|
|
140
|
+
## Production Notes
|
|
141
|
+
|
|
142
|
+
### Batching and Runtime Lifetime
|
|
143
|
+
|
|
144
|
+
The default client configuration sends up to 10 matched events per unchanged ingest payload, with a 250 ms flush window for low-volume traffic. Set `batchSize: 1` and `flushIntervalMs: 0` to opt out and send each matched event immediately.
|
|
145
|
+
|
|
146
|
+
Express and Node servers are long-lived enough to benefit directly from the default. Next.js proxy and middleware pass the delayed send to `event.waitUntil()` when available, which prevents the response from waiting but may keep the middleware invocation alive for the short flush window. Use the explicit opt-out in short-lived runtimes that cannot reliably preserve delayed work, or when minimizing middleware duration matters more than request coalescing:
|
|
99
147
|
|
|
100
148
|
```ts
|
|
101
149
|
const trackAiTraffic = createDevTuneAiTrafficMiddleware({
|
|
102
150
|
ingestKey: process.env.DEVTUNE_AI_TRAFFIC_INGEST_KEY!,
|
|
103
|
-
|
|
151
|
+
batchSize: 1,
|
|
152
|
+
flushIntervalMs: 0,
|
|
104
153
|
});
|
|
105
154
|
```
|
|
106
155
|
|
|
107
|
-
|
|
156
|
+
The middleware helper leaves registry refresh work outside `waitUntil` by default. Ordinary browser user agents are rejected before refresh or ingest work is scheduled.
|
|
108
157
|
|
|
109
|
-
|
|
158
|
+
### Rate-Limit Retries
|
|
110
159
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
- `
|
|
114
|
-
|
|
115
|
-
|
|
160
|
+
A `429` from the ingest endpoint is the one failure the server tells us is temporary, so the batch is redelivered rather than dropped. The client waits for the response's `Retry-After` — delay-seconds or an HTTP date — and falls back to exponential backoff from 250 ms when the header is absent or unusable. The batch being retried is held intact, so events that arrive during a backoff are sent separately rather than folded into it.
|
|
161
|
+
|
|
162
|
+
Retries are bounded so a short-lived runtime cannot be held open by a throttled endpoint. The budget belongs to one flush, not to each batch: `maxRetryAttempts` defaults to 3 and `maxRetryDelayMs` caps each wait at 5000 ms, including a `Retry-After` longer than the cap, so a flush adds at most 15 s no matter how deep the queue is. When the budget is spent the current batch is dropped with the usual sampled warning and the drain stops; whatever is behind it stays queued for the next flush, which gets a fresh budget. `maxRetryAttempts: 0` disables waiting entirely: a 429 drops the batch on its first rejection, as it did before this behaviour existed. The drain still stops there rather than dropping every batch behind it, so the rest stays queued. Tune both:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
const trackAiTraffic = createDevTuneAiTrafficMiddleware({
|
|
166
|
+
ingestKey: process.env.DEVTUNE_AI_TRAFFIC_INGEST_KEY!,
|
|
167
|
+
maxRetryAttempts: 2,
|
|
168
|
+
maxRetryDelayMs: 2_000,
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Because backing off lets the queue drain slower than traffic arrives, the queue itself is bounded. `maxQueuedEvents` defaults to 1000; past it the oldest events are shed with a sampled warning, so a sustained throttle cannot grow memory without limit.
|
|
173
|
+
|
|
174
|
+
Non-429 responses and network errors are unchanged: the batch is dropped after a single attempt, so a hard outage never keeps the queue alive.
|
|
175
|
+
|
|
176
|
+
### Status Semantics
|
|
177
|
+
|
|
178
|
+
Express and Node adapters observe the completed response and report its actual status. Next.js proxy and middleware cannot observe the final route status after pass-through, so the convenience middleware omits status instead of guessing a 200.
|
|
179
|
+
|
|
180
|
+
When a Next.js proxy returns a response directly, use the client and pass the known status:
|
|
116
181
|
|
|
117
|
-
|
|
182
|
+
```ts
|
|
183
|
+
import { createDevTuneAiTraffic } from "@devtune/ai-traffic";
|
|
184
|
+
import type { NextFetchEvent, NextRequest } from "next/server";
|
|
185
|
+
|
|
186
|
+
const aiTraffic = createDevTuneAiTraffic({
|
|
187
|
+
ingestKey: process.env.DEVTUNE_AI_TRAFFIC_INGEST_KEY!,
|
|
188
|
+
defaultStatus: null,
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
export function proxy(request: NextRequest, event: NextFetchEvent) {
|
|
192
|
+
const response = new Response("Forbidden", { status: 403 });
|
|
193
|
+
|
|
194
|
+
aiTraffic.trackRequest(request, event, response.status);
|
|
118
195
|
|
|
119
|
-
|
|
196
|
+
return response;
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
Use `withDevTuneAiTrafficRoute()` where you own a Fetch-compatible route handler and want its exact response status captured automatically. `notFoundPathPatterns` and `statusResolver` remain available for applications that can provide additional status knowledge.
|
|
201
|
+
|
|
202
|
+
### Registry Refresh and Fallback
|
|
203
|
+
|
|
204
|
+
Clients start with the bundled AI bot snapshot, refresh from `https://devtune.ai/api/v1/llm-traffic/registry`, cache active entries for about an hour, and use `ETag` revalidation. Failed refreshes are guarded and sampled; they never fail the application response, and the bundled snapshot remains usable.
|
|
120
205
|
|
|
121
|
-
|
|
206
|
+
User-agent matching is a conservative signal. Some agents spoof ordinary browsers or require network-level signals, so reported counts are a floor rather than exact bot truth. Cloudflare-proxied sites should prefer DevTune's Cloudflare pull integration when available because it can combine request, bot-score, and network signals without running middleware.
|
|
122
207
|
|
|
123
|
-
|
|
208
|
+
### Forwarded Origins
|
|
209
|
+
|
|
210
|
+
Express and Node adapters build event URLs from the request protocol and host, preferring the first value in standard `X-Forwarded-Proto` and `X-Forwarded-Host` chains. Only accept those headers from a trusted proxy. If they are unavailable or not trustworthy in your deployment, pass a fixed `origin`:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
const trackAiTraffic = createDevTuneAiTrafficNodeHandler({
|
|
214
|
+
ingestKey: process.env.DEVTUNE_AI_TRAFFIC_INGEST_KEY!,
|
|
215
|
+
origin: "https://www.example.com",
|
|
216
|
+
});
|
|
217
|
+
```
|
|
124
218
|
|
|
125
|
-
|
|
219
|
+
### Failure Behavior
|
|
126
220
|
|
|
127
|
-
|
|
221
|
+
Registry refresh and ingest sends are fire-and-forget and guarded. Network failures may drop telemetry, but they do not block, throw into, or alter the customer response. The ingest endpoint and batched wire format are unchanged.
|