@particle-academy/agent-integrations 0.17.0 → 0.19.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/dist/chunk-7R6RMROL.js +110 -0
- package/dist/chunk-7R6RMROL.js.map +1 -0
- package/dist/components-shared-whiteboard.cjs +7 -2
- package/dist/components-shared-whiteboard.cjs.map +1 -1
- package/dist/heuristics/sink.d.cts +78 -0
- package/dist/heuristics/sink.d.ts +78 -0
- package/dist/heuristics.cjs +115 -0
- package/dist/heuristics.cjs.map +1 -0
- package/dist/heuristics.js +6 -0
- package/dist/heuristics.js.map +1 -0
- package/dist/index.cjs +185 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -1
- package/dist/index.d.ts +32 -1
- package/dist/index.js +72 -1
- package/dist/index.js.map +1 -1
- package/dist/sharing.cjs +7 -2
- package/dist/sharing.cjs.map +1 -1
- package/package.json +14 -1
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { AutoActivityEvent } from '@particle-academy/fancy-auto-common';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* In-browser heuristics analytics sink for agent traffic.
|
|
5
|
+
*
|
|
6
|
+
* Agent bridge mutations emit `AutoActivityEvent`s into the in-page
|
|
7
|
+
* `@particle-academy/fancy-auto-common` activity bus (presence / cursors).
|
|
8
|
+
* This sink — living in THIS package so it shares the SAME bundled
|
|
9
|
+
* `fancy-auto-common` module instance the bridges emit into — subscribes to
|
|
10
|
+
* that bus and POSTs each event to the `fancy-heuristics` `/collect` endpoint
|
|
11
|
+
* as `actor:"agent"`, so agent traffic shows up in the heuristics dashboard.
|
|
12
|
+
*
|
|
13
|
+
* It does NOT depend on `fancy-heuristics` / `fancy-heuristics-js`: it only
|
|
14
|
+
* emits the frozen wire shape over HTTP. The mapping mirrors
|
|
15
|
+
* `fancy-heuristics-js/src/agent.ts` `mapActivityToEvent` — keep parity.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** What kind of interaction an event captures (mirrors fancy-heuristics-js). */
|
|
19
|
+
type HeuristicsEventKind = "pageview" | "click" | "scroll" | "pointer" | "dwell";
|
|
20
|
+
/** Who produced the interaction. */
|
|
21
|
+
type HeuristicsActor = "human" | "agent";
|
|
22
|
+
/**
|
|
23
|
+
* A single interaction event — mirrors `fancy-heuristics-js` `HeuristicsEvent`.
|
|
24
|
+
* Optional fields are omitted (never `null`) when not relevant to the `kind`.
|
|
25
|
+
*/
|
|
26
|
+
interface HeuristicsEvent {
|
|
27
|
+
kind: HeuristicsEventKind;
|
|
28
|
+
actor: HeuristicsActor;
|
|
29
|
+
/** location.pathname at capture time. */
|
|
30
|
+
path: string;
|
|
31
|
+
/** ms epoch. */
|
|
32
|
+
ts: number;
|
|
33
|
+
x?: number;
|
|
34
|
+
y?: number;
|
|
35
|
+
vw?: number;
|
|
36
|
+
vh?: number;
|
|
37
|
+
scrollPct?: number;
|
|
38
|
+
dwellMs?: number;
|
|
39
|
+
targetId?: string;
|
|
40
|
+
label?: string;
|
|
41
|
+
meta?: Record<string, unknown>;
|
|
42
|
+
}
|
|
43
|
+
/** The batched POST body sent to `${endpoint}/collect`. */
|
|
44
|
+
interface CollectBatch {
|
|
45
|
+
siteKey: string;
|
|
46
|
+
sessionId: string;
|
|
47
|
+
events: HeuristicsEvent[];
|
|
48
|
+
}
|
|
49
|
+
interface AttachHeuristicsSinkOptions {
|
|
50
|
+
/** Base URL, e.g. "/heuristics". POSTs to `${endpoint}/collect`. */
|
|
51
|
+
endpoint: string;
|
|
52
|
+
/** Identifies the site to the ingestion endpoint. */
|
|
53
|
+
siteKey: string;
|
|
54
|
+
/** Stable session id. Default: a generated "agent-<rand>" per attach. */
|
|
55
|
+
sessionId?: string;
|
|
56
|
+
/** Resolves the current path. Default: `() => location.pathname`. */
|
|
57
|
+
path?: () => string;
|
|
58
|
+
/** Which activity sources to record. Default "all". */
|
|
59
|
+
source?: "agent" | "flow" | "all";
|
|
60
|
+
/** Flush interval in ms. Default 2000. */
|
|
61
|
+
batchMs?: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Map one activity event to an `actor:"agent"` HeuristicsEvent. Activities
|
|
65
|
+
* carrying a finite `meta.dwellMs` become `kind:"dwell"`; everything else is a
|
|
66
|
+
* discrete `kind:"click"` with x/y/vw/vh pulled from numeric meta (default 0).
|
|
67
|
+
* Mirrors `fancy-heuristics-js/src/agent.ts` `mapActivityToEvent`.
|
|
68
|
+
*/
|
|
69
|
+
declare function mapActivityToEvent(e: AutoActivityEvent, path: string): HeuristicsEvent;
|
|
70
|
+
/**
|
|
71
|
+
* Subscribe to the in-page agent activity bus and forward each matching event
|
|
72
|
+
* to the heuristics `/collect` endpoint as `actor:"agent"`. SSR-safe (returns a
|
|
73
|
+
* no-op when there is no `window`). Returns an unsubscribe that flushes the
|
|
74
|
+
* buffer and detaches all listeners/timers.
|
|
75
|
+
*/
|
|
76
|
+
declare function attachHeuristicsSink(opts: AttachHeuristicsSinkOptions): () => void;
|
|
77
|
+
|
|
78
|
+
export { type AttachHeuristicsSinkOptions, type CollectBatch, type HeuristicsEvent, attachHeuristicsSink, mapActivityToEvent };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { AutoActivityEvent } from '@particle-academy/fancy-auto-common';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* In-browser heuristics analytics sink for agent traffic.
|
|
5
|
+
*
|
|
6
|
+
* Agent bridge mutations emit `AutoActivityEvent`s into the in-page
|
|
7
|
+
* `@particle-academy/fancy-auto-common` activity bus (presence / cursors).
|
|
8
|
+
* This sink — living in THIS package so it shares the SAME bundled
|
|
9
|
+
* `fancy-auto-common` module instance the bridges emit into — subscribes to
|
|
10
|
+
* that bus and POSTs each event to the `fancy-heuristics` `/collect` endpoint
|
|
11
|
+
* as `actor:"agent"`, so agent traffic shows up in the heuristics dashboard.
|
|
12
|
+
*
|
|
13
|
+
* It does NOT depend on `fancy-heuristics` / `fancy-heuristics-js`: it only
|
|
14
|
+
* emits the frozen wire shape over HTTP. The mapping mirrors
|
|
15
|
+
* `fancy-heuristics-js/src/agent.ts` `mapActivityToEvent` — keep parity.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** What kind of interaction an event captures (mirrors fancy-heuristics-js). */
|
|
19
|
+
type HeuristicsEventKind = "pageview" | "click" | "scroll" | "pointer" | "dwell";
|
|
20
|
+
/** Who produced the interaction. */
|
|
21
|
+
type HeuristicsActor = "human" | "agent";
|
|
22
|
+
/**
|
|
23
|
+
* A single interaction event — mirrors `fancy-heuristics-js` `HeuristicsEvent`.
|
|
24
|
+
* Optional fields are omitted (never `null`) when not relevant to the `kind`.
|
|
25
|
+
*/
|
|
26
|
+
interface HeuristicsEvent {
|
|
27
|
+
kind: HeuristicsEventKind;
|
|
28
|
+
actor: HeuristicsActor;
|
|
29
|
+
/** location.pathname at capture time. */
|
|
30
|
+
path: string;
|
|
31
|
+
/** ms epoch. */
|
|
32
|
+
ts: number;
|
|
33
|
+
x?: number;
|
|
34
|
+
y?: number;
|
|
35
|
+
vw?: number;
|
|
36
|
+
vh?: number;
|
|
37
|
+
scrollPct?: number;
|
|
38
|
+
dwellMs?: number;
|
|
39
|
+
targetId?: string;
|
|
40
|
+
label?: string;
|
|
41
|
+
meta?: Record<string, unknown>;
|
|
42
|
+
}
|
|
43
|
+
/** The batched POST body sent to `${endpoint}/collect`. */
|
|
44
|
+
interface CollectBatch {
|
|
45
|
+
siteKey: string;
|
|
46
|
+
sessionId: string;
|
|
47
|
+
events: HeuristicsEvent[];
|
|
48
|
+
}
|
|
49
|
+
interface AttachHeuristicsSinkOptions {
|
|
50
|
+
/** Base URL, e.g. "/heuristics". POSTs to `${endpoint}/collect`. */
|
|
51
|
+
endpoint: string;
|
|
52
|
+
/** Identifies the site to the ingestion endpoint. */
|
|
53
|
+
siteKey: string;
|
|
54
|
+
/** Stable session id. Default: a generated "agent-<rand>" per attach. */
|
|
55
|
+
sessionId?: string;
|
|
56
|
+
/** Resolves the current path. Default: `() => location.pathname`. */
|
|
57
|
+
path?: () => string;
|
|
58
|
+
/** Which activity sources to record. Default "all". */
|
|
59
|
+
source?: "agent" | "flow" | "all";
|
|
60
|
+
/** Flush interval in ms. Default 2000. */
|
|
61
|
+
batchMs?: number;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Map one activity event to an `actor:"agent"` HeuristicsEvent. Activities
|
|
65
|
+
* carrying a finite `meta.dwellMs` become `kind:"dwell"`; everything else is a
|
|
66
|
+
* discrete `kind:"click"` with x/y/vw/vh pulled from numeric meta (default 0).
|
|
67
|
+
* Mirrors `fancy-heuristics-js/src/agent.ts` `mapActivityToEvent`.
|
|
68
|
+
*/
|
|
69
|
+
declare function mapActivityToEvent(e: AutoActivityEvent, path: string): HeuristicsEvent;
|
|
70
|
+
/**
|
|
71
|
+
* Subscribe to the in-page agent activity bus and forward each matching event
|
|
72
|
+
* to the heuristics `/collect` endpoint as `actor:"agent"`. SSR-safe (returns a
|
|
73
|
+
* no-op when there is no `window`). Returns an unsubscribe that flushes the
|
|
74
|
+
* buffer and detaches all listeners/timers.
|
|
75
|
+
*/
|
|
76
|
+
declare function attachHeuristicsSink(opts: AttachHeuristicsSinkOptions): () => void;
|
|
77
|
+
|
|
78
|
+
export { type AttachHeuristicsSinkOptions, type CollectBatch, type HeuristicsEvent, attachHeuristicsSink, mapActivityToEvent };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var fancyAutoCommon = require('@particle-academy/fancy-auto-common');
|
|
4
|
+
|
|
5
|
+
// src/presence/registry.ts
|
|
6
|
+
|
|
7
|
+
// src/heuristics/sink.ts
|
|
8
|
+
function numericMeta(meta, key) {
|
|
9
|
+
const v = meta?.[key];
|
|
10
|
+
return typeof v === "number" && Number.isFinite(v) ? v : void 0;
|
|
11
|
+
}
|
|
12
|
+
function mapActivityToEvent(e, path) {
|
|
13
|
+
const source = e.source ?? "agent";
|
|
14
|
+
const targetId = e.target?.elementId;
|
|
15
|
+
const label = e.target?.label;
|
|
16
|
+
const meta = {
|
|
17
|
+
action: e.action,
|
|
18
|
+
agentId: e.agentId,
|
|
19
|
+
source,
|
|
20
|
+
kind: e.target?.kind
|
|
21
|
+
};
|
|
22
|
+
const dwellMs = numericMeta(e.meta, "dwellMs");
|
|
23
|
+
if (dwellMs !== void 0) {
|
|
24
|
+
const ev2 = {
|
|
25
|
+
kind: "dwell",
|
|
26
|
+
actor: "agent",
|
|
27
|
+
path,
|
|
28
|
+
ts: e.timestamp,
|
|
29
|
+
dwellMs,
|
|
30
|
+
meta
|
|
31
|
+
};
|
|
32
|
+
if (targetId !== void 0) ev2.targetId = targetId;
|
|
33
|
+
if (label !== void 0) ev2.label = label;
|
|
34
|
+
return ev2;
|
|
35
|
+
}
|
|
36
|
+
const ev = {
|
|
37
|
+
kind: "click",
|
|
38
|
+
actor: "agent",
|
|
39
|
+
path,
|
|
40
|
+
ts: e.timestamp,
|
|
41
|
+
x: numericMeta(e.meta, "x") ?? 0,
|
|
42
|
+
y: numericMeta(e.meta, "y") ?? 0,
|
|
43
|
+
vw: numericMeta(e.meta, "vw") ?? 0,
|
|
44
|
+
vh: numericMeta(e.meta, "vh") ?? 0,
|
|
45
|
+
meta
|
|
46
|
+
};
|
|
47
|
+
if (targetId !== void 0) ev.targetId = targetId;
|
|
48
|
+
if (label !== void 0) ev.label = label;
|
|
49
|
+
return ev;
|
|
50
|
+
}
|
|
51
|
+
function randomId() {
|
|
52
|
+
return Math.random().toString(36).slice(2, 10);
|
|
53
|
+
}
|
|
54
|
+
function attachHeuristicsSink(opts) {
|
|
55
|
+
if (typeof window === "undefined") return () => {
|
|
56
|
+
};
|
|
57
|
+
const endpoint = opts.endpoint.replace(/\/$/, "");
|
|
58
|
+
const url = `${endpoint}/collect`;
|
|
59
|
+
const siteKey = opts.siteKey;
|
|
60
|
+
const sessionId = opts.sessionId ?? `agent-${randomId()}`;
|
|
61
|
+
const getPath = opts.path ?? (() => location.pathname);
|
|
62
|
+
const source = opts.source ?? "all";
|
|
63
|
+
const batchMs = opts.batchMs ?? 2e3;
|
|
64
|
+
const filter = source === "all" ? void 0 : { source };
|
|
65
|
+
let buffer = [];
|
|
66
|
+
function flush() {
|
|
67
|
+
if (buffer.length === 0) return;
|
|
68
|
+
const events = buffer;
|
|
69
|
+
buffer = [];
|
|
70
|
+
const batch = { siteKey, sessionId, events };
|
|
71
|
+
const body = JSON.stringify(batch);
|
|
72
|
+
try {
|
|
73
|
+
if (typeof navigator !== "undefined" && typeof navigator.sendBeacon === "function") {
|
|
74
|
+
const blob = new Blob([body], { type: "application/json" });
|
|
75
|
+
navigator.sendBeacon(url, blob);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
} catch {
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
void fetch(url, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: { "content-type": "application/json" },
|
|
84
|
+
body,
|
|
85
|
+
keepalive: true
|
|
86
|
+
});
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const unsubscribeActivity = fancyAutoCommon.onActivity((e) => {
|
|
91
|
+
try {
|
|
92
|
+
buffer.push(mapActivityToEvent(e, getPath()));
|
|
93
|
+
} catch {
|
|
94
|
+
}
|
|
95
|
+
}, filter);
|
|
96
|
+
const timer = setInterval(flush, batchMs);
|
|
97
|
+
const onPageHide = () => flush();
|
|
98
|
+
const onVisibility = () => {
|
|
99
|
+
if (document.visibilityState === "hidden") flush();
|
|
100
|
+
};
|
|
101
|
+
window.addEventListener("pagehide", onPageHide);
|
|
102
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
103
|
+
return () => {
|
|
104
|
+
unsubscribeActivity();
|
|
105
|
+
window.removeEventListener("pagehide", onPageHide);
|
|
106
|
+
document.removeEventListener("visibilitychange", onVisibility);
|
|
107
|
+
clearInterval(timer);
|
|
108
|
+
flush();
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
exports.attachHeuristicsSink = attachHeuristicsSink;
|
|
113
|
+
exports.mapActivityToEvent = mapActivityToEvent;
|
|
114
|
+
//# sourceMappingURL=heuristics.cjs.map
|
|
115
|
+
//# sourceMappingURL=heuristics.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/heuristics/sink.ts"],"names":["ev","onActivity"],"mappings":";;;;;;;AAkEA,SAAS,WAAA,CACP,MACA,GAAA,EACoB;AACpB,EAAA,MAAM,CAAA,GAAI,OAAO,GAAG,CAAA;AACpB,EAAA,OAAO,OAAO,CAAA,KAAM,QAAA,IAAY,OAAO,QAAA,CAAS,CAAC,IAAI,CAAA,GAAI,MAAA;AAC3D;AAQO,SAAS,kBAAA,CACd,GACA,IAAA,EACiB;AACjB,EAAA,MAAM,MAAA,GAAS,EAAE,MAAA,IAAU,OAAA;AAC3B,EAAA,MAAM,QAAA,GAAW,EAAE,MAAA,EAAQ,SAAA;AAC3B,EAAA,MAAM,KAAA,GAAQ,EAAE,MAAA,EAAQ,KAAA;AACxB,EAAA,MAAM,IAAA,GAAgC;AAAA,IACpC,QAAQ,CAAA,CAAE,MAAA;AAAA,IACV,SAAS,CAAA,CAAE,OAAA;AAAA,IACX,MAAA;AAAA,IACA,IAAA,EAAM,EAAE,MAAA,EAAQ;AAAA,GAClB;AACA,EAAA,MAAM,OAAA,GAAU,WAAA,CAAY,CAAA,CAAE,IAAA,EAAM,SAAS,CAAA;AAE7C,EAAA,IAAI,YAAY,MAAA,EAAW;AACzB,IAAA,MAAMA,GAAAA,GAAsB;AAAA,MAC1B,IAAA,EAAM,OAAA;AAAA,MACN,KAAA,EAAO,OAAA;AAAA,MACP,IAAA;AAAA,MACA,IAAI,CAAA,CAAE,SAAA;AAAA,MACN,OAAA;AAAA,MACA;AAAA,KACF;AACA,IAAA,IAAI,QAAA,KAAa,MAAA,EAAWA,GAAAA,CAAG,QAAA,G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CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF;AAEA,EAAA,MAAM,mBAAA,GAAsBC,0BAAA,CAAW,CAAC,CAAA,KAAM;AAC5C,IAAA,IAAI;AACF,MAAA,MAAA,CAAO,IAAA,CAAK,kBAAA,CAAmB,CAAA,EAAG,OAAA,EAAS,CAAC,CAAA;AAAA,IAC9C,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF,GAAG,MAAM,CAAA;AAET,EAAA,MAAM,KAAA,GAAwC,WAAA,CAAY,KAAA,EAAO,OAAO,CAAA;AAExE,EAAA,MAAM,UAAA,GAAa,MAAM,KAAA,EAAM;AAC/B,EAAA,MAAM,eAAe,MAAM;AACzB,IAAA,IAAI,QAAA,CAAS,eAAA,KAAoB,QAAA,EAAU,KAAA,EAAM;AAAA,EACnD,CAAA;AACA,EAAA,MAAA,CAAO,gBAAA,CAAiB,YAAY,UAAU,CAAA;AAC9C,EAAA,QAAA,CAAS,gBAAA,CAAiB,oBAAoB,YAAY,CAAA;AAE1D,EAAA,OAAO,MAAM;AACX,IAAA,mBAAA,EAAoB;AACpB,IAAA,MAAA,CAAO,mBAAA,CAAoB,YAAY,UAAU,CAAA;AACjD,IAAA,QAAA,CAAS,mBAAA,CAAoB,oBAAoB,YAAY,CAAA;AAC7D,IAAA,aAAA,CAAc,KAAK,CAAA;AACnB,IAAA,KAAA,EAAM;AAAA,EACR,CAAA;AACF","file":"heuristics.cjs","sourcesContent":["/**\n * In-browser heuristics analytics sink for agent traffic.\n *\n * Agent bridge mutations emit `AutoActivityEvent`s into the in-page\n * `@particle-academy/fancy-auto-common` activity bus (presence / cursors).\n * This sink — living in THIS package so it shares the SAME bundled\n * `fancy-auto-common` module instance the bridges emit into — subscribes to\n * that bus and POSTs each event to the `fancy-heuristics` `/collect` endpoint\n * as `actor:\"agent\"`, so agent traffic shows up in the heuristics dashboard.\n *\n * It does NOT depend on `fancy-heuristics` / `fancy-heuristics-js`: it only\n * emits the frozen wire shape over HTTP. The mapping mirrors\n * `fancy-heuristics-js/src/agent.ts` `mapActivityToEvent` — keep parity.\n */\nimport { onActivity, type AgentActivityEvent } from \"../presence\";\n\n/** What kind of interaction an event captures (mirrors fancy-heuristics-js). */\ntype HeuristicsEventKind = \"pageview\" | \"click\" | \"scroll\" | \"pointer\" | \"dwell\";\n\n/** Who produced the interaction. */\ntype HeuristicsActor = \"human\" | \"agent\";\n\n/**\n * A single interaction event — mirrors `fancy-heuristics-js` `HeuristicsEvent`.\n * Optional fields are omitted (never `null`) when not relevant to the `kind`.\n */\nexport interface HeuristicsEvent {\n kind: HeuristicsEventKind;\n actor: HeuristicsActor;\n /** location.pathname at capture time. */\n path: string;\n /** ms epoch. */\n ts: number;\n x?: number;\n y?: number;\n vw?: number;\n vh?: number;\n scrollPct?: number;\n dwellMs?: number;\n targetId?: string;\n label?: string;\n meta?: Record<string, unknown>;\n}\n\n/** The batched POST body sent to `${endpoint}/collect`. */\nexport interface CollectBatch {\n siteKey: string;\n sessionId: string;\n events: HeuristicsEvent[];\n}\n\nexport interface AttachHeuristicsSinkOptions {\n /** Base URL, e.g. \"/heuristics\". POSTs to `${endpoint}/collect`. */\n endpoint: string;\n /** Identifies the site to the ingestion endpoint. */\n siteKey: string;\n /** Stable session id. Default: a generated \"agent-<rand>\" per attach. */\n sessionId?: string;\n /** Resolves the current path. Default: `() => location.pathname`. */\n path?: () => string;\n /** Which activity sources to record. Default \"all\". */\n source?: \"agent\" | \"flow\" | \"all\";\n /** Flush interval in ms. Default 2000. */\n batchMs?: number;\n}\n\nfunction numericMeta(\n meta: Record<string, unknown> | undefined,\n key: string,\n): number | undefined {\n const v = meta?.[key];\n return typeof v === \"number\" && Number.isFinite(v) ? v : undefined;\n}\n\n/**\n * Map one activity event to an `actor:\"agent\"` HeuristicsEvent. Activities\n * carrying a finite `meta.dwellMs` become `kind:\"dwell\"`; everything else is a\n * discrete `kind:\"click\"` with x/y/vw/vh pulled from numeric meta (default 0).\n * Mirrors `fancy-heuristics-js/src/agent.ts` `mapActivityToEvent`.\n */\nexport function mapActivityToEvent(\n e: AgentActivityEvent,\n path: string,\n): HeuristicsEvent {\n const source = e.source ?? \"agent\";\n const targetId = e.target?.elementId;\n const label = e.target?.label;\n const meta: Record<string, unknown> = {\n action: e.action,\n agentId: e.agentId,\n source,\n kind: e.target?.kind,\n };\n const dwellMs = numericMeta(e.meta, \"dwellMs\");\n\n if (dwellMs !== undefined) {\n const ev: HeuristicsEvent = {\n kind: \"dwell\",\n actor: \"agent\",\n path,\n ts: e.timestamp,\n dwellMs,\n meta,\n };\n if (targetId !== undefined) ev.targetId = targetId;\n if (label !== undefined) ev.label = label;\n return ev;\n }\n\n const ev: HeuristicsEvent = {\n kind: \"click\",\n actor: \"agent\",\n path,\n ts: e.timestamp,\n x: numericMeta(e.meta, \"x\") ?? 0,\n y: numericMeta(e.meta, \"y\") ?? 0,\n vw: numericMeta(e.meta, \"vw\") ?? 0,\n vh: numericMeta(e.meta, \"vh\") ?? 0,\n meta,\n };\n if (targetId !== undefined) ev.targetId = targetId;\n if (label !== undefined) ev.label = label;\n return ev;\n}\n\nfunction randomId(): string {\n return Math.random().toString(36).slice(2, 10);\n}\n\n/**\n * Subscribe to the in-page agent activity bus and forward each matching event\n * to the heuristics `/collect` endpoint as `actor:\"agent\"`. SSR-safe (returns a\n * no-op when there is no `window`). Returns an unsubscribe that flushes the\n * buffer and detaches all listeners/timers.\n */\nexport function attachHeuristicsSink(\n opts: AttachHeuristicsSinkOptions,\n): () => void {\n // Browser guard — SSR-safe no-op.\n if (typeof window === \"undefined\") return () => {};\n\n const endpoint = opts.endpoint.replace(/\\/$/, \"\");\n const url = `${endpoint}/collect`;\n const siteKey = opts.siteKey;\n const sessionId = opts.sessionId ?? `agent-${randomId()}`;\n const getPath = opts.path ?? (() => location.pathname);\n const source = opts.source ?? \"all\";\n const batchMs = opts.batchMs ?? 2000;\n\n // onActivity's `source` filter is strict-equality; only set it when we want a\n // single source. \"all\" subscribes unfiltered.\n const filter = source === \"all\" ? undefined : { source };\n\n let buffer: HeuristicsEvent[] = [];\n\n function flush(): void {\n if (buffer.length === 0) return;\n const events = buffer;\n buffer = [];\n const batch: CollectBatch = { siteKey, sessionId, events };\n const body = JSON.stringify(batch);\n try {\n if (\n typeof navigator !== \"undefined\" &&\n typeof navigator.sendBeacon === \"function\"\n ) {\n const blob = new Blob([body], { type: \"application/json\" });\n navigator.sendBeacon(url, blob);\n return;\n }\n } catch {\n // fall through to fetch\n }\n try {\n void fetch(url, {\n method: \"POST\",\n headers: { \"content-type\": \"application/json\" },\n body,\n keepalive: true,\n });\n } catch {\n // never throw from a flush\n }\n }\n\n const unsubscribeActivity = onActivity((e) => {\n try {\n buffer.push(mapActivityToEvent(e, getPath()));\n } catch {\n // never let a malformed event break the bus\n }\n }, filter);\n\n const timer: ReturnType<typeof setInterval> = setInterval(flush, batchMs);\n\n const onPageHide = () => flush();\n const onVisibility = () => {\n if (document.visibilityState === \"hidden\") flush();\n };\n window.addEventListener(\"pagehide\", onPageHide);\n document.addEventListener(\"visibilitychange\", onVisibility);\n\n return () => {\n unsubscribeActivity();\n window.removeEventListener(\"pagehide\", onPageHide);\n document.removeEventListener(\"visibilitychange\", onVisibility);\n clearInterval(timer);\n flush();\n };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"heuristics.js"}
|
package/dist/index.cjs
CHANGED
|
@@ -8,8 +8,13 @@ var reactDom = require('react-dom');
|
|
|
8
8
|
|
|
9
9
|
var __defProp = Object.defineProperty;
|
|
10
10
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
11
|
-
var __esm = (fn, res) => function __init() {
|
|
12
|
-
|
|
11
|
+
var __esm = (fn, res, err) => function __init() {
|
|
12
|
+
if (err) throw err[0];
|
|
13
|
+
try {
|
|
14
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
15
|
+
} catch (e) {
|
|
16
|
+
throw err = [e], e;
|
|
17
|
+
}
|
|
13
18
|
};
|
|
14
19
|
var __export = (target, all) => {
|
|
15
20
|
for (var name in all)
|
|
@@ -3479,9 +3484,184 @@ function CoBrowseCursorLayer({ active = true, zIndex = 2147483e3 }) {
|
|
|
3479
3484
|
);
|
|
3480
3485
|
}
|
|
3481
3486
|
CoBrowseCursorLayer.displayName = "CoBrowseCursorLayer";
|
|
3487
|
+
function resolveCsrf(explicit) {
|
|
3488
|
+
if (explicit) return explicit;
|
|
3489
|
+
if (typeof document === "undefined") return void 0;
|
|
3490
|
+
return document.querySelector('meta[name="csrf-token"]')?.content;
|
|
3491
|
+
}
|
|
3492
|
+
function SimulateUsersButton({
|
|
3493
|
+
endpoint = "/active-users/simulate",
|
|
3494
|
+
count = 10,
|
|
3495
|
+
label,
|
|
3496
|
+
csrfToken,
|
|
3497
|
+
onTriggered,
|
|
3498
|
+
onError,
|
|
3499
|
+
className,
|
|
3500
|
+
style
|
|
3501
|
+
}) {
|
|
3502
|
+
const [busy, setBusy] = react.useState(false);
|
|
3503
|
+
async function trigger() {
|
|
3504
|
+
if (busy) return;
|
|
3505
|
+
setBusy(true);
|
|
3506
|
+
try {
|
|
3507
|
+
const token = resolveCsrf(csrfToken);
|
|
3508
|
+
const res = await fetch(endpoint, {
|
|
3509
|
+
method: "POST",
|
|
3510
|
+
headers: {
|
|
3511
|
+
"Content-Type": "application/json",
|
|
3512
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
3513
|
+
...token ? { "X-CSRF-TOKEN": token } : {}
|
|
3514
|
+
},
|
|
3515
|
+
credentials: "same-origin",
|
|
3516
|
+
body: JSON.stringify({ count })
|
|
3517
|
+
});
|
|
3518
|
+
if (!res.ok) throw new Error(`Simulate request failed: ${res.status}`);
|
|
3519
|
+
onTriggered?.();
|
|
3520
|
+
} catch (err) {
|
|
3521
|
+
onError?.(err);
|
|
3522
|
+
} finally {
|
|
3523
|
+
setBusy(false);
|
|
3524
|
+
}
|
|
3525
|
+
}
|
|
3526
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
3527
|
+
"button",
|
|
3528
|
+
{
|
|
3529
|
+
type: "button",
|
|
3530
|
+
"data-fai-simulate-users": "",
|
|
3531
|
+
className: ["fai-simulate-users", className ?? ""].filter(Boolean).join(" "),
|
|
3532
|
+
onClick: trigger,
|
|
3533
|
+
disabled: busy,
|
|
3534
|
+
style: {
|
|
3535
|
+
display: "inline-flex",
|
|
3536
|
+
alignItems: "center",
|
|
3537
|
+
gap: 8,
|
|
3538
|
+
padding: "8px 14px",
|
|
3539
|
+
borderRadius: 8,
|
|
3540
|
+
border: "1px solid rgba(139,92,246,0.4)",
|
|
3541
|
+
background: busy ? "rgba(139,92,246,0.15)" : "rgba(139,92,246,0.1)",
|
|
3542
|
+
color: "#7c3aed",
|
|
3543
|
+
font: "inherit",
|
|
3544
|
+
fontSize: 13,
|
|
3545
|
+
fontWeight: 500,
|
|
3546
|
+
cursor: busy ? "progress" : "pointer",
|
|
3547
|
+
opacity: busy ? 0.7 : 1,
|
|
3548
|
+
...style
|
|
3549
|
+
},
|
|
3550
|
+
children: [
|
|
3551
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { "aria-hidden": true, children: busy ? "\u23F3" : "\u2728" }),
|
|
3552
|
+
label ?? `Simulate ${count} active users`
|
|
3553
|
+
]
|
|
3554
|
+
}
|
|
3555
|
+
);
|
|
3556
|
+
}
|
|
3482
3557
|
|
|
3483
3558
|
// src/presence/index.ts
|
|
3484
3559
|
init_registry();
|
|
3560
|
+
|
|
3561
|
+
// src/heuristics/sink.ts
|
|
3562
|
+
function numericMeta(meta, key) {
|
|
3563
|
+
const v = meta?.[key];
|
|
3564
|
+
return typeof v === "number" && Number.isFinite(v) ? v : void 0;
|
|
3565
|
+
}
|
|
3566
|
+
function mapActivityToEvent(e, path) {
|
|
3567
|
+
const source = e.source ?? "agent";
|
|
3568
|
+
const targetId = e.target?.elementId;
|
|
3569
|
+
const label = e.target?.label;
|
|
3570
|
+
const meta = {
|
|
3571
|
+
action: e.action,
|
|
3572
|
+
agentId: e.agentId,
|
|
3573
|
+
source,
|
|
3574
|
+
kind: e.target?.kind
|
|
3575
|
+
};
|
|
3576
|
+
const dwellMs = numericMeta(e.meta, "dwellMs");
|
|
3577
|
+
if (dwellMs !== void 0) {
|
|
3578
|
+
const ev2 = {
|
|
3579
|
+
kind: "dwell",
|
|
3580
|
+
actor: "agent",
|
|
3581
|
+
path,
|
|
3582
|
+
ts: e.timestamp,
|
|
3583
|
+
dwellMs,
|
|
3584
|
+
meta
|
|
3585
|
+
};
|
|
3586
|
+
if (targetId !== void 0) ev2.targetId = targetId;
|
|
3587
|
+
if (label !== void 0) ev2.label = label;
|
|
3588
|
+
return ev2;
|
|
3589
|
+
}
|
|
3590
|
+
const ev = {
|
|
3591
|
+
kind: "click",
|
|
3592
|
+
actor: "agent",
|
|
3593
|
+
path,
|
|
3594
|
+
ts: e.timestamp,
|
|
3595
|
+
x: numericMeta(e.meta, "x") ?? 0,
|
|
3596
|
+
y: numericMeta(e.meta, "y") ?? 0,
|
|
3597
|
+
vw: numericMeta(e.meta, "vw") ?? 0,
|
|
3598
|
+
vh: numericMeta(e.meta, "vh") ?? 0,
|
|
3599
|
+
meta
|
|
3600
|
+
};
|
|
3601
|
+
if (targetId !== void 0) ev.targetId = targetId;
|
|
3602
|
+
if (label !== void 0) ev.label = label;
|
|
3603
|
+
return ev;
|
|
3604
|
+
}
|
|
3605
|
+
function randomId2() {
|
|
3606
|
+
return Math.random().toString(36).slice(2, 10);
|
|
3607
|
+
}
|
|
3608
|
+
function attachHeuristicsSink(opts) {
|
|
3609
|
+
if (typeof window === "undefined") return () => {
|
|
3610
|
+
};
|
|
3611
|
+
const endpoint = opts.endpoint.replace(/\/$/, "");
|
|
3612
|
+
const url = `${endpoint}/collect`;
|
|
3613
|
+
const siteKey = opts.siteKey;
|
|
3614
|
+
const sessionId = opts.sessionId ?? `agent-${randomId2()}`;
|
|
3615
|
+
const getPath = opts.path ?? (() => location.pathname);
|
|
3616
|
+
const source = opts.source ?? "all";
|
|
3617
|
+
const batchMs = opts.batchMs ?? 2e3;
|
|
3618
|
+
const filter = source === "all" ? void 0 : { source };
|
|
3619
|
+
let buffer = [];
|
|
3620
|
+
function flush() {
|
|
3621
|
+
if (buffer.length === 0) return;
|
|
3622
|
+
const events = buffer;
|
|
3623
|
+
buffer = [];
|
|
3624
|
+
const batch = { siteKey, sessionId, events };
|
|
3625
|
+
const body = JSON.stringify(batch);
|
|
3626
|
+
try {
|
|
3627
|
+
if (typeof navigator !== "undefined" && typeof navigator.sendBeacon === "function") {
|
|
3628
|
+
const blob = new Blob([body], { type: "application/json" });
|
|
3629
|
+
navigator.sendBeacon(url, blob);
|
|
3630
|
+
return;
|
|
3631
|
+
}
|
|
3632
|
+
} catch {
|
|
3633
|
+
}
|
|
3634
|
+
try {
|
|
3635
|
+
void fetch(url, {
|
|
3636
|
+
method: "POST",
|
|
3637
|
+
headers: { "content-type": "application/json" },
|
|
3638
|
+
body,
|
|
3639
|
+
keepalive: true
|
|
3640
|
+
});
|
|
3641
|
+
} catch {
|
|
3642
|
+
}
|
|
3643
|
+
}
|
|
3644
|
+
const unsubscribeActivity = fancyAutoCommon.onActivity((e) => {
|
|
3645
|
+
try {
|
|
3646
|
+
buffer.push(mapActivityToEvent(e, getPath()));
|
|
3647
|
+
} catch {
|
|
3648
|
+
}
|
|
3649
|
+
}, filter);
|
|
3650
|
+
const timer = setInterval(flush, batchMs);
|
|
3651
|
+
const onPageHide = () => flush();
|
|
3652
|
+
const onVisibility = () => {
|
|
3653
|
+
if (document.visibilityState === "hidden") flush();
|
|
3654
|
+
};
|
|
3655
|
+
window.addEventListener("pagehide", onPageHide);
|
|
3656
|
+
document.addEventListener("visibilitychange", onVisibility);
|
|
3657
|
+
return () => {
|
|
3658
|
+
unsubscribeActivity();
|
|
3659
|
+
window.removeEventListener("pagehide", onPageHide);
|
|
3660
|
+
document.removeEventListener("visibilitychange", onVisibility);
|
|
3661
|
+
clearInterval(timer);
|
|
3662
|
+
flush();
|
|
3663
|
+
};
|
|
3664
|
+
}
|
|
3485
3665
|
function useUndoStack(agentId, intervalMs = 500) {
|
|
3486
3666
|
const [history, setHistory] = react.useState(() => fancyAutoCommon.readHistory(agentId));
|
|
3487
3667
|
react.useEffect(() => {
|
|
@@ -3622,10 +3802,12 @@ exports.MicroMcpServer = MicroMcpServer;
|
|
|
3622
3802
|
exports.RelayTransport = RelayTransport;
|
|
3623
3803
|
exports.ScreensActivityBridge = ScreensActivityBridge;
|
|
3624
3804
|
exports.ShareControls = ShareControls;
|
|
3805
|
+
exports.SimulateUsersButton = SimulateUsersButton;
|
|
3625
3806
|
exports.SseRelayTransport = SseRelayTransport;
|
|
3626
3807
|
exports.ToolRegistry = ToolRegistry;
|
|
3627
3808
|
exports.VscodeMark = VscodeMark;
|
|
3628
3809
|
exports.WrenchMark = WrenchMark;
|
|
3810
|
+
exports.attachHeuristicsSink = attachHeuristicsSink;
|
|
3629
3811
|
exports.attachInProcess = attachInProcess;
|
|
3630
3812
|
exports.attachRelay = attachRelay;
|
|
3631
3813
|
exports.attachSseRelay = attachSseRelay;
|
|
@@ -3644,6 +3826,7 @@ exports.describeSession = describeSession;
|
|
|
3644
3826
|
exports.encodeBase64Json = encodeBase64Json;
|
|
3645
3827
|
exports.ensureUndoToolsRegistered = ensureUndoToolsRegistered;
|
|
3646
3828
|
exports.errorResult = errorResult;
|
|
3829
|
+
exports.mapActivityToHeuristicsEvent = mapActivityToEvent;
|
|
3647
3830
|
exports.readSessionFromUrl = readSessionFromUrl;
|
|
3648
3831
|
exports.registerChartsBridge = registerChartsBridge;
|
|
3649
3832
|
exports.registerCodeBridge = registerCodeBridge;
|