@introspection-sdk/introspection-browser 0.4.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.
@@ -0,0 +1,4 @@
1
+
2
+ > @introspection-sdk/introspection-browser@0.1.0 build /Users/julian/projects/introspection-org/introspection-js-sdk/packages/introspection-browser
3
+ > tsc
4
+
@@ -0,0 +1,5 @@
1
+
2
+ 
3
+ > @introspection-sdk/introspection-browser@0.1.0 check-types /Users/julian/projects/introspection-org/introspection-js-sdk/packages/introspection-browser
4
+ > tsc --noEmit
5
+
@@ -0,0 +1,4 @@
1
+
2
+ > @introspection-sdk/introspection-browser@0.1.0 lint /Users/julian/projects/introspection-org/introspection-js-sdk/packages/introspection-browser
3
+ > eslint . --max-warnings 0
4
+
@@ -0,0 +1,326 @@
1
+ /**
2
+ * Introspection Client for Browser
3
+ * Provides an API for tracking events and feedback using OTLP Logs.
4
+ *
5
+ * Identity and gen_ai context can be managed via baggage (requires OpenTelemetry
6
+ * browser SDK with Zone.js for automatic propagation) or via client instance.
7
+ */
8
+ import { type IntrospectionClientOptions, type FeedbackOptions, type UserTraits } from "./types.js";
9
+ /**
10
+ * Introspection client for tracking events and feedback in the browser using OTLP Logs.
11
+ *
12
+ * Identity (user_id, anonymous_id) and gen_ai context (conversation_id, previous_response_id, agent)
13
+ * can be managed via client instance (persisted to localStorage) or via OpenTelemetry baggage
14
+ * for scoped context.
15
+ *
16
+ * Note: Automatic baggage propagation in browsers requires Zone.js from @opentelemetry/context-zone.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * const client = new IntrospectionClient({
21
+ * token: "intro_xxx",
22
+ * debug: true,
23
+ * });
24
+ *
25
+ * // Set identity once (persisted to localStorage)
26
+ * client.identify("user_123", { email: "user@example.com" });
27
+ *
28
+ * // Simple feedback
29
+ * client.feedback("thumbs_up", { comments: "Great response!" });
30
+ *
31
+ * // With explicit gen_ai context
32
+ * client.feedback("thumbs_down", {
33
+ * conversationId: "conv_456",
34
+ * previousResponseId: "msg_123",
35
+ * comments: "Off topic",
36
+ * });
37
+ *
38
+ * // With scoped baggage context
39
+ * await client.withConversation("conv_456", undefined, async () => {
40
+ * client.feedback("thumbs_up"); // automatically includes conversation_id
41
+ * });
42
+ *
43
+ * // Track a custom event
44
+ * client.track("Button Clicked", { buttonId: "submit" });
45
+ * ```
46
+ */
47
+ export declare class IntrospectionClient {
48
+ private loggerProvider;
49
+ private otelLogger;
50
+ private userId;
51
+ private anonymousId;
52
+ private traits;
53
+ private logger;
54
+ /**
55
+ * @param options - Client configuration (token, service name, advanced settings).
56
+ */
57
+ constructor(options?: IntrospectionClientOptions);
58
+ /**
59
+ * Load anonymous ID from localStorage, or generate a new one
60
+ */
61
+ private loadAnonymousId;
62
+ /**
63
+ * Persist anonymous ID to localStorage
64
+ */
65
+ private persistAnonymousId;
66
+ /**
67
+ * Load user ID from localStorage
68
+ */
69
+ private loadUserId;
70
+ /**
71
+ * Persist user ID to localStorage
72
+ */
73
+ private persistUserId;
74
+ /**
75
+ * Load traits from localStorage
76
+ */
77
+ private loadTraits;
78
+ /**
79
+ * Persist traits to localStorage
80
+ */
81
+ private persistTraits;
82
+ /** Get gen_ai context from baggage */
83
+ private getGenAiFromContext;
84
+ /** Get identity from baggage or client instance */
85
+ private getIdentityFromContext;
86
+ /**
87
+ * Get current timestamp as OpenTelemetry HrTime.
88
+ * Uses performance.now() for high-resolution timing in browsers.
89
+ */
90
+ private getTimestamp;
91
+ /**
92
+ * Build log record attributes.
93
+ */
94
+ private buildAttributes;
95
+ /**
96
+ * Track a custom event.
97
+ *
98
+ * @param eventName - The event name (e.g., `"Button Clicked"`).
99
+ * @param properties - Optional properties to attach to the event.
100
+ * @param options - Optional overrides (e.g., custom `eventId`).
101
+ *
102
+ * @example
103
+ * ```ts
104
+ * client.track("Button Clicked", { buttonId: "submit" });
105
+ * ```
106
+ */
107
+ track(eventName: string, properties?: Record<string, unknown>, options?: {
108
+ eventId?: string;
109
+ }): void;
110
+ /**
111
+ * Track feedback on a message or response.
112
+ *
113
+ * gen_ai.conversation.id, gen_ai.request.previous_response_id, and agent context
114
+ * are automatically extracted from baggage if not provided explicitly.
115
+ *
116
+ * @param name - Feedback name/action (e.g., "thumbs_up", "thumbs_down")
117
+ * @param options - Optional feedback options
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * // Simple feedback
122
+ * client.feedback("thumbs_up");
123
+ *
124
+ * // With comments
125
+ * client.feedback("thumbs_down", { comments: "Off topic" });
126
+ *
127
+ * // With explicit gen_ai context
128
+ * client.feedback("thumbs_up", {
129
+ * conversationId: "conv_456",
130
+ * previousResponseId: "msg_123",
131
+ * });
132
+ *
133
+ * // With baggage context
134
+ * await client.withConversation("conv_456", undefined, async () => {
135
+ * client.feedback("thumbs_up"); // auto-includes conversation_id
136
+ * });
137
+ * ```
138
+ */
139
+ feedback(name: string, options?: FeedbackOptions): void;
140
+ /**
141
+ * Identify a user and persist their traits to `localStorage`.
142
+ *
143
+ * @param userId - The user's unique identifier.
144
+ * @param traits - Optional user traits (email, name, plan, etc.).
145
+ * @param anonymousId - Optional anonymous ID to associate.
146
+ * @param eventId - Optional event ID (auto-generated if omitted).
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * client.identify("user-123", { email: "user@example.com", plan: "pro" });
151
+ * ```
152
+ */
153
+ identify(userId: string, traits?: UserTraits, anonymousId?: string, eventId?: string): void;
154
+ /**
155
+ * Set the authenticated user ID and persist it to `localStorage`.
156
+ *
157
+ * @param userId - The user's unique identifier.
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * client.setUserId("user-123");
162
+ * ```
163
+ */
164
+ setUserId(userId: string): void;
165
+ /**
166
+ * Override the anonymous ID and persist it to `localStorage`.
167
+ *
168
+ * @param anonymousId - The anonymous visitor identifier.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * client.setAnonymousId("anon-abc");
173
+ * ```
174
+ */
175
+ setAnonymousId(anonymousId: string): void;
176
+ /**
177
+ * Get the current anonymous visitor identifier.
178
+ *
179
+ * @returns The anonymous ID (auto-generated on first access if not set).
180
+ *
181
+ * @example
182
+ * ```ts
183
+ * const anonId = client.getAnonymousId();
184
+ * ```
185
+ */
186
+ getAnonymousId(): string;
187
+ /**
188
+ * Create a context with baggage values set.
189
+ * Use with context.with() to run code in that context.
190
+ *
191
+ * Note: In browsers, automatic context propagation requires Zone.js.
192
+ * Without it, you must manually pass context through async operations.
193
+ *
194
+ * @param values - Key/value pairs to set as baggage
195
+ * @returns Context with baggage set
196
+ *
197
+ * @example
198
+ * ```typescript
199
+ * const ctx = client.createBaggageContext({
200
+ * "gen_ai.agent.name": "support-bot",
201
+ * "gen_ai.conversation.id": "conv_456"
202
+ * });
203
+ *
204
+ * await context.with(ctx, async () => {
205
+ * client.feedback("thumbs_up"); // picks up baggage
206
+ * });
207
+ * ```
208
+ */
209
+ createBaggageContext(values: Record<string, string>): import("@opentelemetry/api").Context;
210
+ /**
211
+ * Helper to set multiple baggage values and run a callback.
212
+ *
213
+ * @param values - Key/value pairs to set as baggage.
214
+ * @param callback - Function to run with baggage context.
215
+ * @returns The return value of the callback.
216
+ *
217
+ * @example
218
+ * ```typescript
219
+ * await client.withBaggage({
220
+ * "gen_ai.conversation.id": "conv_456",
221
+ * "gen_ai.request.previous_response_id": "resp_123"
222
+ * }, async () => {
223
+ * client.feedback("thumbs_up");
224
+ * });
225
+ * ```
226
+ */
227
+ withBaggage<T>(values: Record<string, string>, callback: () => T | Promise<T>): Promise<T>;
228
+ /**
229
+ * Helper to set agent baggage and run a callback.
230
+ *
231
+ * @param agentName - Name of the agent.
232
+ * @param agentId - Optional unique identifier for the agent.
233
+ * @param callback - Function to run with agent context.
234
+ * @returns The return value of the callback.
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * await client.withAgent("support-bot", "agent_123", async () => {
239
+ * client.feedback("thumbs_up");
240
+ * });
241
+ * ```
242
+ */
243
+ withAgent<T>(agentName: string, agentId: string | undefined, callback: () => T | Promise<T>): Promise<T>;
244
+ /**
245
+ * Helper to set conversation baggage and run a callback.
246
+ *
247
+ * @param conversationId - Unique identifier for the conversation (optional).
248
+ * @param previousResponseId - Previous response ID for conversation continuity (optional).
249
+ * @param callback - Function to run with conversation context.
250
+ * @returns The return value of the callback.
251
+ *
252
+ * @example
253
+ * ```typescript
254
+ * // Simple conversation context
255
+ * await client.withConversation("conv_456", undefined, async () => {
256
+ * client.feedback("thumbs_up");
257
+ * });
258
+ *
259
+ * // With previous response for conversation continuity
260
+ * await client.withConversation("conv_456", "resp_123", async () => {
261
+ * client.feedback("thumbs_up");
262
+ * });
263
+ *
264
+ * // Just previousResponseId when already in a conversation
265
+ * await client.withConversation("conv_456", undefined, async () => {
266
+ * await client.withConversation(undefined, "resp_1", async () => {
267
+ * client.feedback("thumbs_up");
268
+ * });
269
+ * });
270
+ * ```
271
+ */
272
+ withConversation<T>(conversationId: string | undefined, previousResponseId: string | undefined, callback: () => T | Promise<T>): Promise<T>;
273
+ /**
274
+ * Helper to set user ID baggage and run a callback.
275
+ *
276
+ * @param userId - User identifier.
277
+ * @param callback - Function to run with user context.
278
+ * @returns The return value of the callback.
279
+ *
280
+ * @example
281
+ * ```typescript
282
+ * await client.withUserId("user_123", async () => {
283
+ * client.feedback("thumbs_up");
284
+ * });
285
+ * ```
286
+ */
287
+ withUserId<T>(userId: string, callback: () => T | Promise<T>): Promise<T>;
288
+ /**
289
+ * Helper to set anonymous ID baggage and run a callback.
290
+ *
291
+ * @param anonymousId - Anonymous visitor identifier.
292
+ * @param callback - Function to run with anonymous context.
293
+ * @returns The return value of the callback.
294
+ *
295
+ * @example
296
+ * ```ts
297
+ * await client.withAnonymousId("anon-abc", async () => {
298
+ * client.feedback("thumbs_up");
299
+ * });
300
+ * ```
301
+ */
302
+ withAnonymousId<T>(anonymousId: string, callback: () => T | Promise<T>): Promise<T>;
303
+ /**
304
+ * Reset the client state, clearing user identity and generating a new
305
+ * anonymous ID. Equivalent to a logout operation.
306
+ *
307
+ * @example
308
+ * ```ts
309
+ * client.reset();
310
+ * ```
311
+ */
312
+ reset(): void;
313
+ /**
314
+ * Flush all queued log records to the Introspection backend.
315
+ *
316
+ * @returns A promise that resolves once the flush completes.
317
+ */
318
+ flush(): Promise<void>;
319
+ /**
320
+ * Shut down the client, flushing all pending log records.
321
+ *
322
+ * @returns A promise that resolves once shutdown is complete.
323
+ */
324
+ shutdown(): Promise<void>;
325
+ }
326
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAUH,OAAO,EAEL,KAAK,0BAA0B,EAC/B,KAAK,eAAe,EACpB,KAAK,UAAU,EAGhB,MAAM,YAAY,CAAC;AAsCpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,qBAAa,mBAAmB;IAC9B,OAAO,CAAC,cAAc,CAAiB;IACvC,OAAO,CAAC,UAAU,CAA0C;IAC5D,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,MAAM,CAA+B;IAC7C,OAAO,CAAC,MAAM,CAAS;IAEvB;;OAEG;gBACS,OAAO,GAAE,0BAA+B;IAyEpD;;OAEG;IACH,OAAO,CAAC,eAAe;IAYvB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAM1B;;OAEG;IACH,OAAO,CAAC,UAAU;IAOlB;;OAEG;IACH,OAAO,CAAC,aAAa;IAMrB;;OAEG;IACH,OAAO,CAAC,UAAU;IAclB;;OAEG;IACH,OAAO,CAAC,aAAa;IAMrB,sCAAsC;IACtC,OAAO,CAAC,mBAAmB;IAY3B,mDAAmD;IACnD,OAAO,CAAC,sBAAsB;IAS9B;;;OAGG;IACH,OAAO,CAAC,YAAY;IASpB;;OAEG;IACH,OAAO,CAAC,eAAe;IAuGvB;;;;;;;;;;;OAWG;IACH,KAAK,CACH,SAAS,EAAE,MAAM,E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