@agentstats/sdk 0.0.0-stage → 0.4.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 +70 -2
- package/dist/browser.iife.js +1 -0
- package/dist/index.cjs +800 -0
- package/dist/index.d.cts +269 -0
- package/dist/index.d.ts +269 -0
- package/dist/index.js +786 -0
- package/dist/lite.iife.js +1 -0
- package/dist/server.cjs +1655 -0
- package/dist/server.d.cts +516 -0
- package/dist/server.d.ts +516 -0
- package/dist/server.js +1638 -0
- package/package.json +53 -4
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
//#region src/web.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Visitors: privacy-friendly web analytics for people. One small request per pageview or custom
|
|
4
|
+
* event, no cookies, nothing stored in the browser. The server counts unique visitors from a
|
|
5
|
+
* daily-salted hash of the IP and user agent, the way Plausible does, and keeps neither.
|
|
6
|
+
*
|
|
7
|
+
* Used by the full SDK (on by default, `trackPageviews: false` turns it off) and on its own by
|
|
8
|
+
* the lite script (`/sdk/lite.js`) for sites without WebMCP tools.
|
|
9
|
+
*/
|
|
10
|
+
type EventProps = Record<string, string | number | boolean>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/index.d.ts
|
|
13
|
+
export declare const SDK_VERSION = "0.4.1";
|
|
14
|
+
export type ToolStatus = 'success' | 'error' | 'cancelled';
|
|
15
|
+
export interface ToolAnnotations {
|
|
16
|
+
readOnlyHint?: boolean;
|
|
17
|
+
untrustedContentHint?: boolean;
|
|
18
|
+
consequentialHint?: boolean;
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
}
|
|
21
|
+
export interface ToolExecuteOptions {
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
[key: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
/** Mirrors the WebMCP `ModelContextTool` dictionary. */
|
|
26
|
+
export interface ModelContextTool {
|
|
27
|
+
name: string;
|
|
28
|
+
title?: string;
|
|
29
|
+
description?: string;
|
|
30
|
+
inputSchema?: unknown;
|
|
31
|
+
annotations?: ToolAnnotations;
|
|
32
|
+
execute: (input: unknown, options?: ToolExecuteOptions) => unknown | Promise<unknown>;
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
}
|
|
35
|
+
export interface ToolDescriptor {
|
|
36
|
+
name: string;
|
|
37
|
+
title?: string;
|
|
38
|
+
description?: string;
|
|
39
|
+
inputSchema?: unknown;
|
|
40
|
+
annotations?: ToolAnnotations;
|
|
41
|
+
origin?: string;
|
|
42
|
+
}
|
|
43
|
+
export type ArgsCapture = boolean | ((tool: string, args: unknown) => unknown);
|
|
44
|
+
/** `true`, `false`, or a function returning what to keep (`undefined` keeps nothing). Stringified and cut at 2,000 chars. */
|
|
45
|
+
export type ResultCapture = boolean | ((tool: string, result: unknown) => unknown);
|
|
46
|
+
/** A failed tool call, as passed to a `captureErrors` function. */
|
|
47
|
+
export interface ErrorInfo {
|
|
48
|
+
tool: string;
|
|
49
|
+
/** `error.message`, or the thrown value as a string. */
|
|
50
|
+
message: string;
|
|
51
|
+
/** `error.name`, e.g. `TypeError`. */
|
|
52
|
+
name: string | null;
|
|
53
|
+
/** `error.code` when the thrown value has one. */
|
|
54
|
+
code: string | number | null;
|
|
55
|
+
/** What was thrown. */
|
|
56
|
+
error: unknown;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* What to keep of an error's message: `true` (default), `false` (the call still counts as
|
|
60
|
+
* failed and keeps the error name), or a function returning the text to keep, or null for none.
|
|
61
|
+
*/
|
|
62
|
+
export type ErrorCapture = boolean | ((error: ErrorInfo) => string | null | undefined);
|
|
63
|
+
export interface AgentStatsOptions {
|
|
64
|
+
/** Public site key from your dashboard, e.g. `al_pub_…`. */
|
|
65
|
+
key: string;
|
|
66
|
+
/** Ingest endpoint. Defaults to the origin the SDK script was loaded from, or the built-in cloud endpoint. */
|
|
67
|
+
endpoint?: string;
|
|
68
|
+
/** Capture tool arguments. `true` (default, sent as-is apart from `redactKeys`), `false`, or a custom sanitizer. */
|
|
69
|
+
captureArgs?: ArgsCapture;
|
|
70
|
+
/** Capture tool results (stringified, cut at 2,000 chars). `false` (default), `true`, or a function. */
|
|
71
|
+
captureResults?: ResultCapture;
|
|
72
|
+
/** Capture error messages of failed calls. `true` (default), `false`, or a function. */
|
|
73
|
+
captureErrors?: ErrorCapture;
|
|
74
|
+
/** Keys whose values are replaced with `[redacted]` in args and `track()` props (case-insensitive substring match). Empty by default. */
|
|
75
|
+
redactKeys?: string[];
|
|
76
|
+
/** Static properties attached to every event. */
|
|
77
|
+
metadata?: Record<string, unknown>;
|
|
78
|
+
/** Idle time before a new agent session is started. Default 30 minutes. */
|
|
79
|
+
sessionTimeoutMs?: number;
|
|
80
|
+
/** How often queued events are sent. Default 2000ms. */
|
|
81
|
+
flushIntervalMs?: number;
|
|
82
|
+
/** Max events per request. Default 25. */
|
|
83
|
+
maxBatchSize?: number;
|
|
84
|
+
/** Automatically patch `document.modelContext`. Default `true`. */
|
|
85
|
+
autoInstrument?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Record AI agents browsing ordinary pages in a real browser (Claude in Chrome taking control,
|
|
88
|
+
* Claude's built-in browser, automation like Playwright, agents the server middleware verified).
|
|
89
|
+
* Only agents are recorded, page by page; nothing about people. Default `true`.
|
|
90
|
+
*/
|
|
91
|
+
trackAgentBrowsers?: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Record pageviews and `track()` events for Visitors, privacy-friendly analytics for people:
|
|
94
|
+
* no cookies, nothing stored in the browser, no IP address kept. Ignored on localhost unless
|
|
95
|
+
* `allowLocalhost` is set. Default `true`.
|
|
96
|
+
*/
|
|
97
|
+
trackPageviews?: boolean;
|
|
98
|
+
/** Count the part after `#/` as the page, for hash-based routers. Default `false`. */
|
|
99
|
+
hashRouting?: boolean;
|
|
100
|
+
/** Record clicks on links to other sites as `Outbound Link: Click`. Default `false`. */
|
|
101
|
+
outboundLinks?: boolean;
|
|
102
|
+
/** Record clicks on file links as `File Download`: `true` for the usual types, or your own extensions. Default `false`. */
|
|
103
|
+
fileDownloads?: boolean | string[];
|
|
104
|
+
/** Record pageviews on localhost too. Default `false`. */
|
|
105
|
+
allowLocalhost?: boolean;
|
|
106
|
+
/** Log to console. */
|
|
107
|
+
debug?: boolean;
|
|
108
|
+
/** Do nothing (useful in dev/test). */
|
|
109
|
+
disabled?: boolean;
|
|
110
|
+
/** Custom fetch implementation (tests / non-browser runtimes). */
|
|
111
|
+
fetch?: typeof fetch;
|
|
112
|
+
}
|
|
113
|
+
export interface ToolCallEvent {
|
|
114
|
+
type: 'tool_call';
|
|
115
|
+
id: string;
|
|
116
|
+
ts: number;
|
|
117
|
+
tool: string;
|
|
118
|
+
status: ToolStatus;
|
|
119
|
+
duration_ms: number;
|
|
120
|
+
args?: unknown;
|
|
121
|
+
result?: string;
|
|
122
|
+
error?: string;
|
|
123
|
+
error_name?: string;
|
|
124
|
+
url?: string;
|
|
125
|
+
}
|
|
126
|
+
export interface ToolRegisteredEvent {
|
|
127
|
+
type: 'tool_registered' | 'tool_unregistered';
|
|
128
|
+
id: string;
|
|
129
|
+
ts: number;
|
|
130
|
+
tool: string;
|
|
131
|
+
descriptor?: ToolDescriptor;
|
|
132
|
+
url?: string;
|
|
133
|
+
}
|
|
134
|
+
export interface ToolsSnapshotEvent {
|
|
135
|
+
type: 'tools_snapshot';
|
|
136
|
+
id: string;
|
|
137
|
+
ts: number;
|
|
138
|
+
tools: ToolDescriptor[];
|
|
139
|
+
url?: string;
|
|
140
|
+
}
|
|
141
|
+
export interface CustomEvent {
|
|
142
|
+
type: 'custom';
|
|
143
|
+
id: string;
|
|
144
|
+
ts: number;
|
|
145
|
+
name: string;
|
|
146
|
+
props?: Record<string, unknown>;
|
|
147
|
+
url?: string;
|
|
148
|
+
}
|
|
149
|
+
/** Landing page reached from an AI assistant (ChatGPT, Perplexity, …). Sent once per session; the server classifies it. */
|
|
150
|
+
export interface ReferralEvent {
|
|
151
|
+
type: 'referral';
|
|
152
|
+
id: string;
|
|
153
|
+
ts: number;
|
|
154
|
+
url: string;
|
|
155
|
+
referrer: string | null;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* An AI agent on an ordinary page, browsing in a real browser: a page it loaded
|
|
159
|
+
* (`agent_pageview`), or when it took and gave back control (`agent_control`, Claude in Chrome).
|
|
160
|
+
*/
|
|
161
|
+
export interface AgentBrowserEvent {
|
|
162
|
+
type: 'agent_pageview' | 'agent_control';
|
|
163
|
+
id: string;
|
|
164
|
+
ts: number;
|
|
165
|
+
/** Which agent: claude-in-chrome, claude-browser, automation, or verified-visit (the server names it from the visit cookie). */
|
|
166
|
+
name: string;
|
|
167
|
+
props: {
|
|
168
|
+
via: 'dom' | 'user-agent' | 'webdriver' | 'visit';
|
|
169
|
+
phase?: 'start' | 'stop';
|
|
170
|
+
duration_ms?: number;
|
|
171
|
+
};
|
|
172
|
+
url?: string;
|
|
173
|
+
}
|
|
174
|
+
export type AgentStatsEvent = ToolCallEvent | ToolRegisteredEvent | ToolsSnapshotEvent | CustomEvent | ReferralEvent | AgentBrowserEvent;
|
|
175
|
+
export interface IngestPayload {
|
|
176
|
+
key: string;
|
|
177
|
+
sdk: string;
|
|
178
|
+
session_id: string;
|
|
179
|
+
sent_at: number;
|
|
180
|
+
metadata?: Record<string, unknown>;
|
|
181
|
+
/** Visit id from the `_al_v` cookie the server integration sets on responses to AI agents. */
|
|
182
|
+
visit?: string;
|
|
183
|
+
/** Client signals used to attribute the session to an agent when there is no visit cookie. */
|
|
184
|
+
client?: {
|
|
185
|
+
ua?: string;
|
|
186
|
+
webdriver?: boolean;
|
|
187
|
+
};
|
|
188
|
+
events: AgentStatsEvent[];
|
|
189
|
+
}
|
|
190
|
+
export declare class AgentStatsClient {
|
|
191
|
+
readonly options: Required<Pick<AgentStatsOptions, 'key' | 'endpoint' | 'captureArgs' | 'captureResults' | 'captureErrors' | 'redactKeys' | 'sessionTimeoutMs' | 'flushIntervalMs' | 'maxBatchSize' | 'autoInstrument' | 'trackAgentBrowsers' | 'debug' | 'disabled'>> & AgentStatsOptions;
|
|
192
|
+
private queue;
|
|
193
|
+
private timer;
|
|
194
|
+
private snapshotTimer;
|
|
195
|
+
private pollTimer;
|
|
196
|
+
private sessionId;
|
|
197
|
+
private lastActivity;
|
|
198
|
+
private lastSnapshotHash;
|
|
199
|
+
private _fetch;
|
|
200
|
+
private inflight;
|
|
201
|
+
private web;
|
|
202
|
+
constructor(opts: AgentStatsOptions);
|
|
203
|
+
/** Wrap a WebMCP tool definition so its `execute` is tracked. Safe to call on already-wrapped tools. */
|
|
204
|
+
wrap<T extends ModelContextTool>(tool: T): T;
|
|
205
|
+
/**
|
|
206
|
+
* Track a custom event (e.g. a conversion). It counts for the agent session it happens in, and
|
|
207
|
+
* as a goal in Visitors when a person does it (string, number and boolean props only there).
|
|
208
|
+
*/
|
|
209
|
+
track(name: string, props?: Record<string, unknown>): void;
|
|
210
|
+
/** Record a pageview now, e.g. after a client-side navigation the SDK can't see. Pageviews are otherwise automatic. */
|
|
211
|
+
pageview(): void;
|
|
212
|
+
/** Take a snapshot of the currently registered tools now (also happens automatically). */
|
|
213
|
+
snapshot(): Promise<ToolDescriptor[]>;
|
|
214
|
+
/** Send everything queued right now. */
|
|
215
|
+
flush(useBeacon?: boolean): Promise<void>;
|
|
216
|
+
/** Remove patches and stop timers. */
|
|
217
|
+
destroy(): void;
|
|
218
|
+
get session(): string;
|
|
219
|
+
private log;
|
|
220
|
+
private url;
|
|
221
|
+
private redactKeys;
|
|
222
|
+
private modelContext;
|
|
223
|
+
private captureArgs;
|
|
224
|
+
private captureResult;
|
|
225
|
+
private captureError;
|
|
226
|
+
/** Runs a user-supplied capture function. One that throws keeps nothing rather than breaking the tool call. */
|
|
227
|
+
private callOption;
|
|
228
|
+
private recordCall;
|
|
229
|
+
private enqueue;
|
|
230
|
+
private scheduleSnapshot;
|
|
231
|
+
private instrument;
|
|
232
|
+
private patch;
|
|
233
|
+
private bindLifecycle;
|
|
234
|
+
/** User agent + webdriver flag, so a session can be attributed to an agent that browses with a real browser. */
|
|
235
|
+
private clientHints;
|
|
236
|
+
/** The agent browsing this page, if one is: in control right now (Claude in Chrome), or the browser itself is one. */
|
|
237
|
+
detectAgentBrowser(): {
|
|
238
|
+
name: string;
|
|
239
|
+
via: AgentBrowserEvent['props']['via'];
|
|
240
|
+
} | null;
|
|
241
|
+
private agentPage;
|
|
242
|
+
private controlStarted;
|
|
243
|
+
/** Record agent page views (on load and on client-side navigation) and Claude in Chrome taking and giving back control. */
|
|
244
|
+
private watchAgentBrowsers;
|
|
245
|
+
/** If this page was reached from an AI assistant, tell the server once per session. */
|
|
246
|
+
private reportReferral;
|
|
247
|
+
private loadSession;
|
|
248
|
+
private touchSession;
|
|
249
|
+
}
|
|
250
|
+
/** Initialise AgentStats. Call once, as early as possible (before registering tools). */
|
|
251
|
+
export declare function init(options: AgentStatsOptions): AgentStatsClient;
|
|
252
|
+
export declare function getClient(): AgentStatsClient | null;
|
|
253
|
+
export declare function track(name: string, props?: Record<string, unknown>): void;
|
|
254
|
+
export declare function wrap<T extends ModelContextTool>(tool: T): T;
|
|
255
|
+
export declare function flush(): Promise<void>;
|
|
256
|
+
export declare function snapshot(): Promise<ToolDescriptor[]>;
|
|
257
|
+
export declare function pageview(): void;
|
|
258
|
+
export declare const AgentStats: {
|
|
259
|
+
init: typeof init;
|
|
260
|
+
track: typeof track;
|
|
261
|
+
pageview: typeof pageview;
|
|
262
|
+
wrap: typeof wrap;
|
|
263
|
+
flush: typeof flush;
|
|
264
|
+
snapshot: typeof snapshot;
|
|
265
|
+
getClient: typeof getClient;
|
|
266
|
+
version: string;
|
|
267
|
+
};
|
|
268
|
+
//#endregion
|
|
269
|
+
export { AgentStats as default, type EventProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
//#region src/web.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Visitors: privacy-friendly web analytics for people. One small request per pageview or custom
|
|
4
|
+
* event, no cookies, nothing stored in the browser. The server counts unique visitors from a
|
|
5
|
+
* daily-salted hash of the IP and user agent, the way Plausible does, and keeps neither.
|
|
6
|
+
*
|
|
7
|
+
* Used by the full SDK (on by default, `trackPageviews: false` turns it off) and on its own by
|
|
8
|
+
* the lite script (`/sdk/lite.js`) for sites without WebMCP tools.
|
|
9
|
+
*/
|
|
10
|
+
type EventProps = Record<string, string | number | boolean>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/index.d.ts
|
|
13
|
+
export declare const SDK_VERSION = "0.4.1";
|
|
14
|
+
export type ToolStatus = 'success' | 'error' | 'cancelled';
|
|
15
|
+
export interface ToolAnnotations {
|
|
16
|
+
readOnlyHint?: boolean;
|
|
17
|
+
untrustedContentHint?: boolean;
|
|
18
|
+
consequentialHint?: boolean;
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
}
|
|
21
|
+
export interface ToolExecuteOptions {
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
[key: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
/** Mirrors the WebMCP `ModelContextTool` dictionary. */
|
|
26
|
+
export interface ModelContextTool {
|
|
27
|
+
name: string;
|
|
28
|
+
title?: string;
|
|
29
|
+
description?: string;
|
|
30
|
+
inputSchema?: unknown;
|
|
31
|
+
annotations?: ToolAnnotations;
|
|
32
|
+
execute: (input: unknown, options?: ToolExecuteOptions) => unknown | Promise<unknown>;
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
}
|
|
35
|
+
export interface ToolDescriptor {
|
|
36
|
+
name: string;
|
|
37
|
+
title?: string;
|
|
38
|
+
description?: string;
|
|
39
|
+
inputSchema?: unknown;
|
|
40
|
+
annotations?: ToolAnnotations;
|
|
41
|
+
origin?: string;
|
|
42
|
+
}
|
|
43
|
+
export type ArgsCapture = boolean | ((tool: string, args: unknown) => unknown);
|
|
44
|
+
/** `true`, `false`, or a function returning what to keep (`undefined` keeps nothing). Stringified and cut at 2,000 chars. */
|
|
45
|
+
export type ResultCapture = boolean | ((tool: string, result: unknown) => unknown);
|
|
46
|
+
/** A failed tool call, as passed to a `captureErrors` function. */
|
|
47
|
+
export interface ErrorInfo {
|
|
48
|
+
tool: string;
|
|
49
|
+
/** `error.message`, or the thrown value as a string. */
|
|
50
|
+
message: string;
|
|
51
|
+
/** `error.name`, e.g. `TypeError`. */
|
|
52
|
+
name: string | null;
|
|
53
|
+
/** `error.code` when the thrown value has one. */
|
|
54
|
+
code: string | number | null;
|
|
55
|
+
/** What was thrown. */
|
|
56
|
+
error: unknown;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* What to keep of an error's message: `true` (default), `false` (the call still counts as
|
|
60
|
+
* failed and keeps the error name), or a function returning the text to keep, or null for none.
|
|
61
|
+
*/
|
|
62
|
+
export type ErrorCapture = boolean | ((error: ErrorInfo) => string | null | undefined);
|
|
63
|
+
export interface AgentStatsOptions {
|
|
64
|
+
/** Public site key from your dashboard, e.g. `al_pub_…`. */
|
|
65
|
+
key: string;
|
|
66
|
+
/** Ingest endpoint. Defaults to the origin the SDK script was loaded from, or the built-in cloud endpoint. */
|
|
67
|
+
endpoint?: string;
|
|
68
|
+
/** Capture tool arguments. `true` (default, sent as-is apart from `redactKeys`), `false`, or a custom sanitizer. */
|
|
69
|
+
captureArgs?: ArgsCapture;
|
|
70
|
+
/** Capture tool results (stringified, cut at 2,000 chars). `false` (default), `true`, or a function. */
|
|
71
|
+
captureResults?: ResultCapture;
|
|
72
|
+
/** Capture error messages of failed calls. `true` (default), `false`, or a function. */
|
|
73
|
+
captureErrors?: ErrorCapture;
|
|
74
|
+
/** Keys whose values are replaced with `[redacted]` in args and `track()` props (case-insensitive substring match). Empty by default. */
|
|
75
|
+
redactKeys?: string[];
|
|
76
|
+
/** Static properties attached to every event. */
|
|
77
|
+
metadata?: Record<string, unknown>;
|
|
78
|
+
/** Idle time before a new agent session is started. Default 30 minutes. */
|
|
79
|
+
sessionTimeoutMs?: number;
|
|
80
|
+
/** How often queued events are sent. Default 2000ms. */
|
|
81
|
+
flushIntervalMs?: number;
|
|
82
|
+
/** Max events per request. Default 25. */
|
|
83
|
+
maxBatchSize?: number;
|
|
84
|
+
/** Automatically patch `document.modelContext`. Default `true`. */
|
|
85
|
+
autoInstrument?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Record AI agents browsing ordinary pages in a real browser (Claude in Chrome taking control,
|
|
88
|
+
* Claude's built-in browser, automation like Playwright, agents the server middleware verified).
|
|
89
|
+
* Only agents are recorded, page by page; nothing about people. Default `true`.
|
|
90
|
+
*/
|
|
91
|
+
trackAgentBrowsers?: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Record pageviews and `track()` events for Visitors, privacy-friendly analytics for people:
|
|
94
|
+
* no cookies, nothing stored in the browser, no IP address kept. Ignored on localhost unless
|
|
95
|
+
* `allowLocalhost` is set. Default `true`.
|
|
96
|
+
*/
|
|
97
|
+
trackPageviews?: boolean;
|
|
98
|
+
/** Count the part after `#/` as the page, for hash-based routers. Default `false`. */
|
|
99
|
+
hashRouting?: boolean;
|
|
100
|
+
/** Record clicks on links to other sites as `Outbound Link: Click`. Default `false`. */
|
|
101
|
+
outboundLinks?: boolean;
|
|
102
|
+
/** Record clicks on file links as `File Download`: `true` for the usual types, or your own extensions. Default `false`. */
|
|
103
|
+
fileDownloads?: boolean | string[];
|
|
104
|
+
/** Record pageviews on localhost too. Default `false`. */
|
|
105
|
+
allowLocalhost?: boolean;
|
|
106
|
+
/** Log to console. */
|
|
107
|
+
debug?: boolean;
|
|
108
|
+
/** Do nothing (useful in dev/test). */
|
|
109
|
+
disabled?: boolean;
|
|
110
|
+
/** Custom fetch implementation (tests / non-browser runtimes). */
|
|
111
|
+
fetch?: typeof fetch;
|
|
112
|
+
}
|
|
113
|
+
export interface ToolCallEvent {
|
|
114
|
+
type: 'tool_call';
|
|
115
|
+
id: string;
|
|
116
|
+
ts: number;
|
|
117
|
+
tool: string;
|
|
118
|
+
status: ToolStatus;
|
|
119
|
+
duration_ms: number;
|
|
120
|
+
args?: unknown;
|
|
121
|
+
result?: string;
|
|
122
|
+
error?: string;
|
|
123
|
+
error_name?: string;
|
|
124
|
+
url?: string;
|
|
125
|
+
}
|
|
126
|
+
export interface ToolRegisteredEvent {
|
|
127
|
+
type: 'tool_registered' | 'tool_unregistered';
|
|
128
|
+
id: string;
|
|
129
|
+
ts: number;
|
|
130
|
+
tool: string;
|
|
131
|
+
descriptor?: ToolDescriptor;
|
|
132
|
+
url?: string;
|
|
133
|
+
}
|
|
134
|
+
export interface ToolsSnapshotEvent {
|
|
135
|
+
type: 'tools_snapshot';
|
|
136
|
+
id: string;
|
|
137
|
+
ts: number;
|
|
138
|
+
tools: ToolDescriptor[];
|
|
139
|
+
url?: string;
|
|
140
|
+
}
|
|
141
|
+
export interface CustomEvent {
|
|
142
|
+
type: 'custom';
|
|
143
|
+
id: string;
|
|
144
|
+
ts: number;
|
|
145
|
+
name: string;
|
|
146
|
+
props?: Record<string, unknown>;
|
|
147
|
+
url?: string;
|
|
148
|
+
}
|
|
149
|
+
/** Landing page reached from an AI assistant (ChatGPT, Perplexity, …). Sent once per session; the server classifies it. */
|
|
150
|
+
export interface ReferralEvent {
|
|
151
|
+
type: 'referral';
|
|
152
|
+
id: string;
|
|
153
|
+
ts: number;
|
|
154
|
+
url: string;
|
|
155
|
+
referrer: string | null;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* An AI agent on an ordinary page, browsing in a real browser: a page it loaded
|
|
159
|
+
* (`agent_pageview`), or when it took and gave back control (`agent_control`, Claude in Chrome).
|
|
160
|
+
*/
|
|
161
|
+
export interface AgentBrowserEvent {
|
|
162
|
+
type: 'agent_pageview' | 'agent_control';
|
|
163
|
+
id: string;
|
|
164
|
+
ts: number;
|
|
165
|
+
/** Which agent: claude-in-chrome, claude-browser, automation, or verified-visit (the server names it from the visit cookie). */
|
|
166
|
+
name: string;
|
|
167
|
+
props: {
|
|
168
|
+
via: 'dom' | 'user-agent' | 'webdriver' | 'visit';
|
|
169
|
+
phase?: 'start' | 'stop';
|
|
170
|
+
duration_ms?: number;
|
|
171
|
+
};
|
|
172
|
+
url?: string;
|
|
173
|
+
}
|
|
174
|
+
export type AgentStatsEvent = ToolCallEvent | ToolRegisteredEvent | ToolsSnapshotEvent | CustomEvent | ReferralEvent | AgentBrowserEvent;
|
|
175
|
+
export interface IngestPayload {
|
|
176
|
+
key: string;
|
|
177
|
+
sdk: string;
|
|
178
|
+
session_id: string;
|
|
179
|
+
sent_at: number;
|
|
180
|
+
metadata?: Record<string, unknown>;
|
|
181
|
+
/** Visit id from the `_al_v` cookie the server integration sets on responses to AI agents. */
|
|
182
|
+
visit?: string;
|
|
183
|
+
/** Client signals used to attribute the session to an agent when there is no visit cookie. */
|
|
184
|
+
client?: {
|
|
185
|
+
ua?: string;
|
|
186
|
+
webdriver?: boolean;
|
|
187
|
+
};
|
|
188
|
+
events: AgentStatsEvent[];
|
|
189
|
+
}
|
|
190
|
+
export declare class AgentStatsClient {
|
|
191
|
+
readonly options: Required<Pick<AgentStatsOptions, 'key' | 'endpoint' | 'captureArgs' | 'captureResults' | 'captureErrors' | 'redactKeys' | 'sessionTimeoutMs' | 'flushIntervalMs' | 'maxBatchSize' | 'autoInstrument' | 'trackAgentBrowsers' | 'debug' | 'disabled'>> & AgentStatsOptions;
|
|
192
|
+
private queue;
|
|
193
|
+
private timer;
|
|
194
|
+
private snapshotTimer;
|
|
195
|
+
private pollTimer;
|
|
196
|
+
private sessionId;
|
|
197
|
+
private lastActivity;
|
|
198
|
+
private lastSnapshotHash;
|
|
199
|
+
private _fetch;
|
|
200
|
+
private inflight;
|
|
201
|
+
private web;
|
|
202
|
+
constructor(opts: AgentStatsOptions);
|
|
203
|
+
/** Wrap a WebMCP tool definition so its `execute` is tracked. Safe to call on already-wrapped tools. */
|
|
204
|
+
wrap<T extends ModelContextTool>(tool: T): T;
|
|
205
|
+
/**
|
|
206
|
+
* Track a custom event (e.g. a conversion). It counts for the agent session it happens in, and
|
|
207
|
+
* as a goal in Visitors when a person does it (string, number and boolean props only there).
|
|
208
|
+
*/
|
|
209
|
+
track(name: string, props?: Record<string, unknown>): void;
|
|
210
|
+
/** Record a pageview now, e.g. after a client-side navigation the SDK can't see. Pageviews are otherwise automatic. */
|
|
211
|
+
pageview(): void;
|
|
212
|
+
/** Take a snapshot of the currently registered tools now (also happens automatically). */
|
|
213
|
+
snapshot(): Promise<ToolDescriptor[]>;
|
|
214
|
+
/** Send everything queued right now. */
|
|
215
|
+
flush(useBeacon?: boolean): Promise<void>;
|
|
216
|
+
/** Remove patches and stop timers. */
|
|
217
|
+
destroy(): void;
|
|
218
|
+
get session(): string;
|
|
219
|
+
private log;
|
|
220
|
+
private url;
|
|
221
|
+
private redactKeys;
|
|
222
|
+
private modelContext;
|
|
223
|
+
private captureArgs;
|
|
224
|
+
private captureResult;
|
|
225
|
+
private captureError;
|
|
226
|
+
/** Runs a user-supplied capture function. One that throws keeps nothing rather than breaking the tool call. */
|
|
227
|
+
private callOption;
|
|
228
|
+
private recordCall;
|
|
229
|
+
private enqueue;
|
|
230
|
+
private scheduleSnapshot;
|
|
231
|
+
private instrument;
|
|
232
|
+
private patch;
|
|
233
|
+
private bindLifecycle;
|
|
234
|
+
/** User agent + webdriver flag, so a session can be attributed to an agent that browses with a real browser. */
|
|
235
|
+
private clientHints;
|
|
236
|
+
/** The agent browsing this page, if one is: in control right now (Claude in Chrome), or the browser itself is one. */
|
|
237
|
+
detectAgentBrowser(): {
|
|
238
|
+
name: string;
|
|
239
|
+
via: AgentBrowserEvent['props']['via'];
|
|
240
|
+
} | null;
|
|
241
|
+
private agentPage;
|
|
242
|
+
private controlStarted;
|
|
243
|
+
/** Record agent page views (on load and on client-side navigation) and Claude in Chrome taking and giving back control. */
|
|
244
|
+
private watchAgentBrowsers;
|
|
245
|
+
/** If this page was reached from an AI assistant, tell the server once per session. */
|
|
246
|
+
private reportReferral;
|
|
247
|
+
private loadSession;
|
|
248
|
+
private touchSession;
|
|
249
|
+
}
|
|
250
|
+
/** Initialise AgentStats. Call once, as early as possible (before registering tools). */
|
|
251
|
+
export declare function init(options: AgentStatsOptions): AgentStatsClient;
|
|
252
|
+
export declare function getClient(): AgentStatsClient | null;
|
|
253
|
+
export declare function track(name: string, props?: Record<string, unknown>): void;
|
|
254
|
+
export declare function wrap<T extends ModelContextTool>(tool: T): T;
|
|
255
|
+
export declare function flush(): Promise<void>;
|
|
256
|
+
export declare function snapshot(): Promise<ToolDescriptor[]>;
|
|
257
|
+
export declare function pageview(): void;
|
|
258
|
+
export declare const AgentStats: {
|
|
259
|
+
init: typeof init;
|
|
260
|
+
track: typeof track;
|
|
261
|
+
pageview: typeof pageview;
|
|
262
|
+
wrap: typeof wrap;
|
|
263
|
+
flush: typeof flush;
|
|
264
|
+
snapshot: typeof snapshot;
|
|
265
|
+
getClient: typeof getClient;
|
|
266
|
+
version: string;
|
|
267
|
+
};
|
|
268
|
+
//#endregion
|
|
269
|
+
export { AgentStats as default, type EventProps };
|