@alpic-ai/insights 0.0.0-dev.gb1f574c → 0.0.0-dev.gb23efa8
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/react/index.d.mts +24 -0
- package/dist/react/index.mjs +183 -0
- package/package.json +15 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
//#region src/react/analytics-client.d.ts
|
|
3
|
+
interface CaptureOptions {
|
|
4
|
+
message?: string;
|
|
5
|
+
properties?: Record<string, unknown>;
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/react/alpic-analytics.d.ts
|
|
9
|
+
interface Analytics {
|
|
10
|
+
/** Queue a custom widget event for delivery to Alpic Analytics. */
|
|
11
|
+
capture: (name: string, options?: CaptureOptions) => void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Provides Alpic Analytics to descendant components and configures itself from the widget host.
|
|
15
|
+
* Wrap the widget once, then call `useAnalytics()` from components that capture events.
|
|
16
|
+
* Events captured before configuration is available are buffered automatically.
|
|
17
|
+
*/
|
|
18
|
+
declare function AlpicAnalytics({ children }: {
|
|
19
|
+
children?: ReactNode;
|
|
20
|
+
}): import("react").JSX.Element;
|
|
21
|
+
/** Returns the analytics client from the nearest `AlpicAnalytics` provider. */
|
|
22
|
+
declare function useAnalytics(): Analytics;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { AlpicAnalytics, type Analytics, type CaptureOptions, useAnalytics };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect, useState } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
//#region src/react/analytics-transport.ts
|
|
4
|
+
function postAnalyticsBatch(ingestUrl, events) {
|
|
5
|
+
return fetch(ingestUrl, {
|
|
6
|
+
method: "POST",
|
|
7
|
+
keepalive: true,
|
|
8
|
+
headers: { "Content-Type": "application/json" },
|
|
9
|
+
body: JSON.stringify({ events })
|
|
10
|
+
}).then(() => void 0, () => void 0);
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/react/event-queue.ts
|
|
14
|
+
var EventQueue = class {
|
|
15
|
+
maxSize;
|
|
16
|
+
items = [];
|
|
17
|
+
constructor(maxSize) {
|
|
18
|
+
this.maxSize = maxSize;
|
|
19
|
+
}
|
|
20
|
+
get length() {
|
|
21
|
+
return this.items.length;
|
|
22
|
+
}
|
|
23
|
+
get isFull() {
|
|
24
|
+
return this.items.length >= this.maxSize;
|
|
25
|
+
}
|
|
26
|
+
push(item) {
|
|
27
|
+
if (this.items.length >= this.maxSize) return false;
|
|
28
|
+
this.items.push(item);
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
drain(maxItems) {
|
|
32
|
+
return this.items.splice(0, maxItems);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
const FLUSH_DELAY_MS = 2e3;
|
|
36
|
+
var AnalyticsClient = class {
|
|
37
|
+
queue = new EventQueue(100);
|
|
38
|
+
ingestUrl = null;
|
|
39
|
+
flushTimer = null;
|
|
40
|
+
warnedBufferFull = false;
|
|
41
|
+
configure(ingestUrl) {
|
|
42
|
+
this.ingestUrl = ingestUrl;
|
|
43
|
+
if (this.queue.length > 0) this.scheduleFlush();
|
|
44
|
+
}
|
|
45
|
+
capture = (name, options) => {
|
|
46
|
+
if (this.queue.isFull) {
|
|
47
|
+
if (!this.warnedBufferFull) {
|
|
48
|
+
console.warn("[@alpic-ai/insights] analytics buffer is full, dropping events");
|
|
49
|
+
this.warnedBufferFull = true;
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const event = {
|
|
54
|
+
name,
|
|
55
|
+
timestamp: Date.now()
|
|
56
|
+
};
|
|
57
|
+
if (options?.message !== void 0) event.message = options.message;
|
|
58
|
+
if (options?.properties !== void 0) event.properties = options.properties;
|
|
59
|
+
this.queue.push(event);
|
|
60
|
+
if (this.queue.length >= 50) {
|
|
61
|
+
this.flush();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.scheduleFlush();
|
|
65
|
+
};
|
|
66
|
+
flush = () => {
|
|
67
|
+
if (this.flushTimer !== null) {
|
|
68
|
+
clearTimeout(this.flushTimer);
|
|
69
|
+
this.flushTimer = null;
|
|
70
|
+
}
|
|
71
|
+
if (this.ingestUrl === null) return;
|
|
72
|
+
while (this.queue.length > 0) {
|
|
73
|
+
const events = this.queue.drain(50);
|
|
74
|
+
postAnalyticsBatch(this.ingestUrl, events);
|
|
75
|
+
}
|
|
76
|
+
this.warnedBufferFull = false;
|
|
77
|
+
};
|
|
78
|
+
scheduleFlush() {
|
|
79
|
+
if (this.ingestUrl === null || this.flushTimer !== null) return;
|
|
80
|
+
this.flushTimer = setTimeout(this.flush, FLUSH_DELAY_MS);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
//#endregion
|
|
84
|
+
//#region src/react/stamp.ts
|
|
85
|
+
const ANALYTICS_META_KEY = "alpic/analytics";
|
|
86
|
+
function parseAnalyticsStamp(metadata) {
|
|
87
|
+
if (metadata === null || typeof metadata !== "object") return null;
|
|
88
|
+
const candidate = metadata[ANALYTICS_META_KEY];
|
|
89
|
+
if (candidate === null || candidate === void 0 || typeof candidate !== "object") return null;
|
|
90
|
+
const { distinctId, sessionId, ingestUrl } = candidate;
|
|
91
|
+
if (typeof distinctId !== "string" || typeof sessionId !== "string" || typeof ingestUrl !== "string") return null;
|
|
92
|
+
return {
|
|
93
|
+
distinctId,
|
|
94
|
+
sessionId,
|
|
95
|
+
ingestUrl
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/react/stamp-source.ts
|
|
100
|
+
const TOOL_RESULT_METHOD = "ui/notifications/tool-result";
|
|
101
|
+
const SET_GLOBALS_EVENT_TYPE = "openai:set_globals";
|
|
102
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
103
|
+
let latestProtocolStamp = null;
|
|
104
|
+
let isObservingRuntime = false;
|
|
105
|
+
function subscribeToAnalyticsStamp(onStamp) {
|
|
106
|
+
observeRuntime();
|
|
107
|
+
subscribers.add(onStamp);
|
|
108
|
+
if (latestProtocolStamp !== null) onStamp(latestProtocolStamp);
|
|
109
|
+
else emitOpenAiStamp(onStamp);
|
|
110
|
+
return () => {
|
|
111
|
+
subscribers.delete(onStamp);
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function observeRuntime() {
|
|
115
|
+
if (isObservingRuntime || typeof window === "undefined") return;
|
|
116
|
+
window.addEventListener("message", handleToolResultMessage);
|
|
117
|
+
window.addEventListener(SET_GLOBALS_EVENT_TYPE, handleOpenAiGlobals, { passive: true });
|
|
118
|
+
isObservingRuntime = true;
|
|
119
|
+
}
|
|
120
|
+
function handleToolResultMessage(event) {
|
|
121
|
+
if (event.source !== window.parent || event.data === null || typeof event.data !== "object") return;
|
|
122
|
+
const message = event.data;
|
|
123
|
+
if (message.jsonrpc !== "2.0" || message.method !== TOOL_RESULT_METHOD) return;
|
|
124
|
+
const params = message.params;
|
|
125
|
+
if (params === null || typeof params !== "object") return;
|
|
126
|
+
const stamp = parseAnalyticsStamp(params._meta);
|
|
127
|
+
if (stamp === null) return;
|
|
128
|
+
latestProtocolStamp = stamp;
|
|
129
|
+
for (const subscriber of subscribers) subscriber(stamp);
|
|
130
|
+
}
|
|
131
|
+
function handleOpenAiGlobals() {
|
|
132
|
+
if (latestProtocolStamp !== null) return;
|
|
133
|
+
const globals = window;
|
|
134
|
+
if (globals.openai === void 0) return;
|
|
135
|
+
const stamp = parseAnalyticsStamp(globals.openai.toolResponseMetadata);
|
|
136
|
+
if (stamp === null) return;
|
|
137
|
+
for (const subscriber of subscribers) subscriber(stamp);
|
|
138
|
+
}
|
|
139
|
+
function emitOpenAiStamp(onStamp) {
|
|
140
|
+
if (typeof window === "undefined") return;
|
|
141
|
+
const stamp = parseAnalyticsStamp(window.openai?.toolResponseMetadata);
|
|
142
|
+
if (stamp !== null) onStamp(stamp);
|
|
143
|
+
}
|
|
144
|
+
observeRuntime();
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/react/alpic-analytics.tsx
|
|
147
|
+
const AnalyticsContext = createContext(null);
|
|
148
|
+
/**
|
|
149
|
+
* Provides Alpic Analytics to descendant components and configures itself from the widget host.
|
|
150
|
+
* Wrap the widget once, then call `useAnalytics()` from components that capture events.
|
|
151
|
+
* Events captured before configuration is available are buffered automatically.
|
|
152
|
+
*/
|
|
153
|
+
function AlpicAnalytics({ children }) {
|
|
154
|
+
const [client] = useState(() => new AnalyticsClient());
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
const unsubscribe = subscribeToAnalyticsStamp((stamp) => {
|
|
157
|
+
client.configure(stamp.ingestUrl);
|
|
158
|
+
});
|
|
159
|
+
const flushWhenHidden = () => {
|
|
160
|
+
if (document.visibilityState === "hidden") client.flush();
|
|
161
|
+
};
|
|
162
|
+
document.addEventListener("visibilitychange", flushWhenHidden);
|
|
163
|
+
window.addEventListener("pagehide", client.flush);
|
|
164
|
+
return () => {
|
|
165
|
+
unsubscribe();
|
|
166
|
+
document.removeEventListener("visibilitychange", flushWhenHidden);
|
|
167
|
+
window.removeEventListener("pagehide", client.flush);
|
|
168
|
+
client.flush();
|
|
169
|
+
};
|
|
170
|
+
}, [client]);
|
|
171
|
+
return /* @__PURE__ */ jsx(AnalyticsContext.Provider, {
|
|
172
|
+
value: client,
|
|
173
|
+
children
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
/** Returns the analytics client from the nearest `AlpicAnalytics` provider. */
|
|
177
|
+
function useAnalytics() {
|
|
178
|
+
const analytics = useContext(AnalyticsContext);
|
|
179
|
+
if (analytics === null) throw new Error("useAnalytics must be used within an <AlpicAnalytics> provider");
|
|
180
|
+
return analytics;
|
|
181
|
+
}
|
|
182
|
+
//#endregion
|
|
183
|
+
export { AlpicAnalytics, useAnalytics };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alpic-ai/insights",
|
|
3
|
-
"version": "0.0.0-dev.
|
|
3
|
+
"version": "0.0.0-dev.gb23efa8",
|
|
4
4
|
"description": "User insights middlewares for Alpic-hosted MCP servers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.mjs",
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.mts",
|
|
11
11
|
"default": "./dist/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"./react": {
|
|
14
|
+
"types": "./dist/react/index.d.mts",
|
|
15
|
+
"default": "./dist/react/index.mjs"
|
|
12
16
|
}
|
|
13
17
|
},
|
|
14
18
|
"files": [
|
|
@@ -18,17 +22,27 @@
|
|
|
18
22
|
"license": "ISC",
|
|
19
23
|
"peerDependencies": {
|
|
20
24
|
"@modelcontextprotocol/sdk": ">=1.29.0 <2",
|
|
25
|
+
"react": ">=18.0.0",
|
|
21
26
|
"skybridge": "^0.36.3 || ^1.0.0"
|
|
22
27
|
},
|
|
23
28
|
"peerDependenciesMeta": {
|
|
29
|
+
"react": {
|
|
30
|
+
"optional": true
|
|
31
|
+
},
|
|
24
32
|
"skybridge": {
|
|
25
33
|
"optional": true
|
|
26
34
|
}
|
|
27
35
|
},
|
|
28
36
|
"devDependencies": {
|
|
29
37
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
38
|
+
"@testing-library/dom": "^10.4.1",
|
|
39
|
+
"@testing-library/react": "^16.3.2",
|
|
30
40
|
"@total-typescript/tsconfig": "^1.0.4",
|
|
31
41
|
"@types/node": "^25.9.5",
|
|
42
|
+
"@types/react": "19.2.17",
|
|
43
|
+
"jsdom": "^29.1.1",
|
|
44
|
+
"react": "^19.2.7",
|
|
45
|
+
"react-dom": "^19.2.7",
|
|
32
46
|
"shx": "^0.4.0",
|
|
33
47
|
"skybridge": "^1.2.7",
|
|
34
48
|
"tsdown": "^0.22.5",
|