@brizz/sdk 0.1.7 → 0.1.9

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
@@ -259,8 +259,60 @@ API keys, crypto addresses, IPs, and more.
259
259
 
260
260
  Group related operations under a session context:
261
261
 
262
+ ### Session Context Manager (Recommended)
263
+
264
+ The `startSession` function creates a session span and provides a `Session` object:
265
+
266
+ ```typescript
267
+ import { startSession } from '@brizz/sdk';
268
+
269
+ // Basic usage - all LLM calls within the callback are automatically linked
270
+ const result = await startSession('session-123', async (session) => {
271
+ // Add custom properties (optional)
272
+ session.updateProperties({ userId: 'user-456', model: 'gpt-4' });
273
+
274
+ const response = await openai.chat.completions.create({
275
+ model: 'gpt-4',
276
+ messages: [{ role: 'user', content: userQuery }],
277
+ });
278
+
279
+ return response;
280
+ });
281
+ ```
282
+
283
+ **Session Methods:**
284
+ - `session.updateProperties({ key: value })` - Add custom properties to the session span
285
+ - `session.setInput(text)` - (Optional) Manually track input text
286
+ - `session.setOutput(text)` - (Optional) Manually track output text
287
+
288
+ **When to use manual input/output tracking:**
289
+
290
+ In most cases, Brizz automatically captures inputs and outputs from your LLM calls. Use `setInput`/`setOutput` for special scenarios:
291
+
292
+ - **Multi-agent flows**: Track only user-facing input/output, not intermediate agent communications
293
+ - **Structured data extraction**: Track a specific field from complex JSON requests
294
+ - **Post-processing**: Track transformed responses before returning to the user
295
+
296
+ ```typescript
297
+ await startSession('session-456', async (session) => {
298
+ // Extract just the query from a structured request
299
+ const requestData = { query: "What's the weather?", context: {...} };
300
+ session.setInput(requestData.query);
301
+
302
+ const response = await openai.chat.completions.create({...});
303
+
304
+ // Extract answer from structured response
305
+ const responseJson = JSON.parse(response.choices[0].message.content);
306
+ session.setOutput(responseJson.answer);
307
+
308
+ return responseJson;
309
+ });
310
+ ```
311
+
262
312
  ### Function Wrapper Pattern
263
313
 
314
+ For simpler cases where you just need to tag traces with a session ID:
315
+
264
316
  ```typescript
265
317
  import { withSessionId, emitEvent } from '@brizz/sdk';
266
318
 
package/dist/index.cjs CHANGED
@@ -33,6 +33,7 @@ __export(src_exports, {
33
33
  Brizz: () => Brizz,
34
34
  DEFAULT_PII_PATTERNS: () => DEFAULT_PII_PATTERNS,
35
35
  LogLevel: () => LogLevel,
36
+ Session: () => Session,
36
37
  SeverityNumber: () => import_api_logs2.SeverityNumber,
37
38
  callWithProperties: () => callWithProperties,
38
39
  callWithSessionId: () => callWithSessionId,
@@ -48,6 +49,7 @@ __export(src_exports, {
48
49
  maskAttributes: () => maskAttributes,
49
50
  maskValue: () => maskValue,
50
51
  setLogLevel: () => setLogLevel,
52
+ startSession: () => startSession,
51
53
  withProperties: () => withProperties,
52
54
  withSessionId: () => withSessionId
53
55
  });
@@ -480,7 +482,7 @@ var import_sdk_logs2 = require("@opentelemetry/sdk-logs");
480
482
 
481
483
  // src/internal/version.ts
482
484
  function getSDKVersion() {
483
- return "0.1.7";
485
+ return "0.1.9";
484
486
  }
485
487
 
486
488
  // src/internal/log/processors/log-processor.ts
@@ -1115,6 +1117,9 @@ var BRIZZ = "brizz";
1115
1117
  var PROPERTIES = "properties";
1116
1118
  var SESSION_ID = "session.id";
1117
1119
  var PROPERTIES_CONTEXT_KEY = (0, import_api2.createContextKey)(PROPERTIES);
1120
+ var SESSION_INPUT = "brizz.session.input";
1121
+ var SESSION_OUTPUT = "brizz.session.output";
1122
+ var SESSION_SPAN_NAME = "brizz.start_session";
1118
1123
 
1119
1124
  // src/internal/log/processors/log-processor.ts
1120
1125
  var DEFAULT_LOG_MASKING_RULES = [
@@ -1695,6 +1700,79 @@ function withSessionId(sessionId, fn, thisArg) {
1695
1700
  function callWithSessionId(sessionId, fn, thisArg, ...args) {
1696
1701
  return callWithProperties({ [SESSION_ID]: sessionId }, fn, thisArg, ...args);
1697
1702
  }
1703
+ var Session = class {
1704
+ sessionId;
1705
+ span;
1706
+ inputs = [];
1707
+ outputs = [];
1708
+ constructor(sessionId, span) {
1709
+ this.sessionId = sessionId;
1710
+ this.span = span;
1711
+ }
1712
+ /**
1713
+ * (Optional) Append text to session input tracking.
1714
+ * Use when you need to track specific input data that differs from what's sent to the LLM.
1715
+ * Multiple calls accumulate in an array.
1716
+ *
1717
+ * @param text - Text to append to session input
1718
+ */
1719
+ setInput(text) {
1720
+ this.inputs.push(text);
1721
+ this.span.setAttribute(SESSION_INPUT, JSON.stringify(this.inputs));
1722
+ }
1723
+ /**
1724
+ * (Optional) Append text to session output tracking.
1725
+ * Use when you need to track specific output data that differs from what's received from the LLM.
1726
+ * Multiple calls accumulate in an array.
1727
+ *
1728
+ * @param text - Text to append to session output
1729
+ */
1730
+ setOutput(text) {
1731
+ this.outputs.push(text);
1732
+ this.span.setAttribute(SESSION_OUTPUT, JSON.stringify(this.outputs));
1733
+ }
1734
+ /**
1735
+ * Update custom properties on the session span.
1736
+ * Properties are prefixed with 'brizz.'.
1737
+ *
1738
+ * @param properties - Key-value properties to set on the session
1739
+ */
1740
+ updateProperties(properties) {
1741
+ for (const [key, value] of Object.entries(properties)) {
1742
+ this.span.setAttribute(`${BRIZZ}.${key}`, value);
1743
+ }
1744
+ }
1745
+ };
1746
+ function startSession(sessionId, callback) {
1747
+ const tracer = import_api5.trace.getTracer("@brizz/sdk");
1748
+ return tracer.startActiveSpan(SESSION_SPAN_NAME, (span) => {
1749
+ span.setAttribute(`${BRIZZ}.${SESSION_ID}`, sessionId);
1750
+ const session = new Session(sessionId, span);
1751
+ return callWithProperties({ [SESSION_ID]: sessionId }, () => {
1752
+ try {
1753
+ const result = callback(session);
1754
+ if (result && typeof result.then === "function") {
1755
+ return result.then((value) => {
1756
+ span.end();
1757
+ return value;
1758
+ }).catch((error) => {
1759
+ span.recordException(error);
1760
+ span.setStatus({ code: import_api5.SpanStatusCode.ERROR });
1761
+ span.end();
1762
+ throw error;
1763
+ });
1764
+ }
1765
+ span.end();
1766
+ return result;
1767
+ } catch (error) {
1768
+ span.recordException(error);
1769
+ span.setStatus({ code: import_api5.SpanStatusCode.ERROR });
1770
+ span.end();
1771
+ throw error;
1772
+ }
1773
+ });
1774
+ });
1775
+ }
1698
1776
 
1699
1777
  // src/internal/sdk.ts
1700
1778
  var _Brizz = class __Brizz {
@@ -1945,6 +2023,7 @@ var init_exports = {};
1945
2023
  Brizz,
1946
2024
  DEFAULT_PII_PATTERNS,
1947
2025
  LogLevel,
2026
+ Session,
1948
2027
  SeverityNumber,
1949
2028
  callWithProperties,
1950
2029
  callWithSessionId,
@@ -1960,6 +2039,7 @@ var init_exports = {};
1960
2039
  maskAttributes,
1961
2040
  maskValue,
1962
2041
  setLogLevel,
2042
+ startSession,
1963
2043
  withProperties,
1964
2044
  withSessionId
1965
2045
  });