@brizz/sdk 0.1.3 → 0.1.4

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 CHANGED
@@ -50,6 +50,7 @@ pnpm add @brizz/sdk
50
50
  ## Quick Start
51
51
 
52
52
  First, set up your environment variables:
53
+
53
54
  ```bash
54
55
  BRIZZ_API_KEY=your-api-key
55
56
  BRIZZ_BASE_URL=https://telemetry.brizz.dev # Optional
@@ -60,6 +61,7 @@ BRIZZ_LOG_LEVEL=info # Optional: debug, info, warn, error
60
61
  ### CommonJS Projects
61
62
 
62
63
  **Option 1: Preload Only (Zero Config)**
64
+
63
65
  ```bash
64
66
  node --require @brizz/sdk/preload your-app.js
65
67
  ```
@@ -74,10 +76,11 @@ generateText({
74
76
  model: openai('gpt-3.5-turbo'),
75
77
  prompt: 'Hello, world!',
76
78
  experimental_telemetry: { isEnabled: true },
77
- }).then(result => console.log(result.text));
79
+ }).then((result) => console.log(result.text));
78
80
  ```
79
81
 
80
82
  **Option 2: Preload + Initialize (Custom Config)**
83
+
81
84
  ```bash
82
85
  node --require @brizz/sdk/preload your-app.js
83
86
  ```
@@ -97,6 +100,7 @@ const { generateText } = require('ai');
97
100
  ```
98
101
 
99
102
  **Option 3: Manual Import + Initialize**
103
+
100
104
  ```javascript
101
105
  // Must be first import
102
106
  const { Brizz } = require('@brizz/sdk');
@@ -113,9 +117,11 @@ const { openai } = require('@ai-sdk/openai');
113
117
 
114
118
  ### ESM Projects
115
119
 
116
- > ⚠️ **ESM Requirement**: ESM projects **must** use the `--import @brizz/sdk/loader` flag for instrumentation to work. Manual import without the loader will not instrument AI libraries.
120
+ > ⚠️ **ESM Requirement**: ESM projects **must** use the `--import @brizz/sdk/loader` flag for
121
+ > instrumentation to work. Manual import without the loader will not instrument AI libraries.
117
122
 
118
123
  **Loader + Initialize (Required for ESM)**
124
+
119
125
  ```bash
120
126
  node --import @brizz/sdk/loader your-app.mjs
121
127
  ```
@@ -147,10 +153,12 @@ const result = await generateText({
147
153
  ## Module System Support
148
154
 
149
155
  ### CommonJS
156
+
150
157
  - **Preload**: `node --require @brizz/sdk/preload app.js` ⭐ (with optional `Brizz.initialize()`)
151
158
  - **Manual**: Require `@brizz/sdk` first, then `Brizz.initialize()`, then AI libraries
152
159
 
153
160
  ### ESM (ES Modules)
161
+
154
162
  - **Loader**: `node --import @brizz/sdk/loader app.mjs` + `Brizz.initialize()` ⭐
155
163
  - **Manual**: Import `@brizz/sdk` first, then `Brizz.initialize()`, then AI libraries
156
164
 
@@ -208,6 +216,7 @@ const stream = streamText({
208
216
  ```
209
217
 
210
218
  This enables automatic tracing of:
219
+
211
220
  - Model calls and responses
212
221
  - Token usage and costs
213
222
  - Tool calls and executions
@@ -250,25 +259,81 @@ API keys, crypto addresses, IPs, and more.
250
259
 
251
260
  Group related operations under a session context:
252
261
 
262
+ ### Function Wrapper Pattern
263
+
253
264
  ```typescript
254
- import { WithSessionId, emitEvent } from '@brizz/sdk';
265
+ import { withSessionId, emitEvent } from '@brizz/sdk';
255
266
 
256
267
  async function processUserWorkflow(userId: string) {
257
- // All traces within this function will include session-123
268
+ // All traces within this function will include the session ID
258
269
  const result = await generateText({
259
270
  model: openai('gpt-4'),
260
271
  messages: [{ role: 'user', content: 'Hello' }],
261
272
  experimental_telemetry: { isEnabled: true },
262
273
  });
263
274
 
264
- emitEvent('workflow.completed', { userId, result: result.text });
265
275
  return result;
266
276
  }
267
277
 
268
- // Wrap function with session context
269
- await WithSessionId('session-123', processUserWorkflow, null, 'user-456');
278
+ // Create a wrapped function that always executes with session context
279
+ const sessionedWorkflow = withSessionId('session-123', processUserWorkflow);
280
+
281
+ // Call multiple times, each with the same session context
282
+ await sessionedWorkflow('user-456');
283
+ await sessionedWorkflow('user-789');
284
+ ```
285
+
286
+ ### Immediate Execution Pattern
287
+
288
+ ```typescript
289
+ import { callWithSessionId } from '@brizz/sdk';
290
+
291
+ // Execute function immediately with session context
292
+ await callWithSessionId('session-123', processUserWorkflow, null, 'user-456');
270
293
  ```
271
294
 
295
+ ### Handling Method Context
296
+
297
+ When wrapping methods that use `this`, you have several options:
298
+
299
+ #### Option 1: Arrow Function (Recommended)
300
+
301
+ ```typescript
302
+ class ChatService {
303
+ async processMessage(userId: string, message: string) {
304
+ // This method uses 'this' context
305
+ return `Processed by ${this.serviceName}: ${message}`;
306
+ }
307
+ }
308
+
309
+ const service = new ChatService();
310
+ // Wrap with arrow function to preserve 'this' context
311
+ const sessionedProcess = withSessionId('session-123', (userId: string, message: string) =>
312
+ service.processMessage(userId, message),
313
+ );
314
+ ```
315
+
316
+ #### Option 2: Using bind()
317
+
318
+ ```typescript
319
+ // Pre-bind the method to preserve 'this' context
320
+ const sessionedProcess = withSessionId('session-123', service.processMessage.bind(service));
321
+ ```
322
+
323
+ #### Option 3: Explicit thisArg Parameter
324
+
325
+ ```typescript
326
+ // Pass 'this' context explicitly as third parameter
327
+ const sessionedProcess = withSessionId(
328
+ 'session-123',
329
+ service.processMessage,
330
+ service, // explicit 'this' context
331
+ );
332
+ ```
333
+
334
+ **Note:** The arrow function approach (Option 1) is recommended as it's more explicit, avoids lint
335
+ warnings, and is less prone to `this` binding issues.
336
+
272
337
  ## Custom Events & Logging
273
338
 
274
339
  Emit custom events and structured logs:
package/dist/index.cjs CHANGED
@@ -34,7 +34,7 @@ __export(src_exports, {
34
34
  DEFAULT_PII_PATTERNS: () => DEFAULT_PII_PATTERNS,
35
35
  LogLevel: () => LogLevel,
36
36
  SeverityNumber: () => import_api_logs2.SeverityNumber,
37
- WithSessionId: () => WithSessionId,
37
+ callWithSessionId: () => callWithSessionId,
38
38
  detectRuntime: () => detectRuntime,
39
39
  emitEvent: () => emitEvent,
40
40
  getLogLevel: () => getLogLevel,
@@ -46,7 +46,8 @@ __export(src_exports, {
46
46
  logger: () => logger,
47
47
  maskAttributes: () => maskAttributes,
48
48
  maskValue: () => maskValue,
49
- setLogLevel: () => setLogLevel
49
+ setLogLevel: () => setLogLevel,
50
+ withSessionId: () => withSessionId
50
51
  });
51
52
  module.exports = __toCommonJS(src_exports);
52
53
 
@@ -1791,7 +1792,15 @@ function withProperties(properties, fn, thisArg, ...args) {
1791
1792
  const newContext = import_api5.context.active().setValue(PROPERTIES_CONTEXT_KEY, properties);
1792
1793
  return import_api5.context.with(newContext, fn, thisArg, ...args);
1793
1794
  }
1794
- function WithSessionId(sessionId, fn, thisArg, ...args) {
1795
+ function withSessionId(sessionId, fn, thisArg) {
1796
+ return function wrapped(...args) {
1797
+ const base = import_api5.context.active();
1798
+ const prev = base.getValue(PROPERTIES_CONTEXT_KEY);
1799
+ const next = base.setValue(PROPERTIES_CONTEXT_KEY, prev ? { ...prev, [SESSION_ID]: sessionId } : { [SESSION_ID]: sessionId });
1800
+ return import_api5.context.with(next, fn, thisArg ?? this, ...args);
1801
+ };
1802
+ }
1803
+ function callWithSessionId(sessionId, fn, thisArg, ...args) {
1795
1804
  return withProperties({ [SESSION_ID]: sessionId }, fn, thisArg, ...args);
1796
1805
  }
1797
1806
 
@@ -2035,7 +2044,7 @@ var init_exports = {};
2035
2044
  DEFAULT_PII_PATTERNS,
2036
2045
  LogLevel,
2037
2046
  SeverityNumber,
2038
- WithSessionId,
2047
+ callWithSessionId,
2039
2048
  detectRuntime,
2040
2049
  emitEvent,
2041
2050
  getLogLevel,
@@ -2047,6 +2056,7 @@ var init_exports = {};
2047
2056
  logger,
2048
2057
  maskAttributes,
2049
2058
  maskValue,
2050
- setLogLevel
2059
+ setLogLevel,
2060
+ withSessionId
2051
2061
  });
2052
2062
  //# sourceMappingURL=index.cjs.map