@brizz/sdk 0.1.8 → 0.1.10

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,64 @@ 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(
271
+ 'session-123',
272
+ async (session) => {
273
+ // Add custom properties (optional)
274
+ session.updateProperties({ userId: 'user-456', model: 'gpt-4' });
275
+
276
+ const response = await openai.chat.completions.create({
277
+ model: 'gpt-4',
278
+ messages: [{ role: 'user', content: userQuery }],
279
+ });
280
+
281
+ return response;
282
+ },
283
+ { feature: 'chat' }, // optional: extraProperties propagated to all spans
284
+ );
285
+ ```
286
+
287
+ **Session Methods:**
288
+ - `session.updateProperties({ key: value })` - Add custom properties to the session span
289
+ - `session.setInput(text)` - (Optional) Manually track input text
290
+ - `session.setOutput(text)` - (Optional) Manually track output text
291
+
292
+ **When to use manual input/output tracking:**
293
+
294
+ In most cases, Brizz automatically captures inputs and outputs from your LLM calls. Use `setInput`/`setOutput` for special scenarios:
295
+
296
+ - **Multi-agent flows**: Track only user-facing input/output, not intermediate agent communications
297
+ - **Structured data extraction**: Track a specific field from complex JSON requests
298
+ - **Post-processing**: Track transformed responses before returning to the user
299
+
300
+ ```typescript
301
+ await startSession('session-456', async (session) => {
302
+ // Extract just the query from a structured request
303
+ const requestData = { query: "What's the weather?", context: {...} };
304
+ session.setInput(requestData.query);
305
+
306
+ const response = await openai.chat.completions.create({...});
307
+
308
+ // Extract answer from structured response
309
+ const responseJson = JSON.parse(response.choices[0].message.content);
310
+ session.setOutput(responseJson.answer);
311
+
312
+ return responseJson;
313
+ });
314
+ ```
315
+
262
316
  ### Function Wrapper Pattern
263
317
 
318
+ For simpler cases where you just need to tag traces with a session ID:
319
+
264
320
  ```typescript
265
321
  import { withSessionId, emitEvent } from '@brizz/sdk';
266
322
 
@@ -276,7 +332,8 @@ async function processUserWorkflow(userId: string) {
276
332
  }
277
333
 
278
334
  // Create a wrapped function that always executes with session context
279
- const sessionedWorkflow = withSessionId('session-123', processUserWorkflow);
335
+ // withSessionId(sessionId, fn, thisArg?, extraProperties?)
336
+ const sessionedWorkflow = withSessionId('session-123', processUserWorkflow, undefined, { feature: 'workflow' });
280
337
 
281
338
  // Call multiple times, each with the same session context
282
339
  await sessionedWorkflow('user-456');
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.8";
485
+ return "0.1.10";
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 = [
@@ -1473,7 +1478,7 @@ function getMetricsReader() {
1473
1478
  }
1474
1479
 
1475
1480
  // src/internal/trace/tracing.ts
1476
- var import_exporter_trace_otlp_http = require("@opentelemetry/exporter-trace-otlp-http");
1481
+ var import_exporter_trace_otlp_proto = require("@opentelemetry/exporter-trace-otlp-proto");
1477
1482
 
1478
1483
  // src/internal/trace/processors/span-processor.ts
1479
1484
  var import_api4 = require("@opentelemetry/api");
@@ -1606,7 +1611,7 @@ var TracingModule = class _TracingModule {
1606
1611
  }
1607
1612
  const tracesUrl = config.baseUrl.replace(/\/$/, "") + "/v1/traces";
1608
1613
  logger.debug("Initializing default OTLP span exporter", { url: tracesUrl });
1609
- this.spanExporter = new import_exporter_trace_otlp_http.OTLPTraceExporter({
1614
+ this.spanExporter = new import_exporter_trace_otlp_proto.OTLPTraceExporter({
1610
1615
  url: tracesUrl,
1611
1616
  headers: config.headers
1612
1617
  });
@@ -1689,12 +1694,97 @@ function withProperties(properties, fn, thisArg) {
1689
1694
  );
1690
1695
  };
1691
1696
  }
1692
- function withSessionId(sessionId, fn, thisArg) {
1693
- return withProperties({ [SESSION_ID]: sessionId }, fn, thisArg);
1697
+ function withSessionId(sessionId, fn, thisArg, extraProperties) {
1698
+ const properties = { [SESSION_ID]: sessionId, ...extraProperties };
1699
+ return withProperties(properties, fn, thisArg);
1694
1700
  }
1695
1701
  function callWithSessionId(sessionId, fn, thisArg, ...args) {
1696
1702
  return callWithProperties({ [SESSION_ID]: sessionId }, fn, thisArg, ...args);
1697
1703
  }
1704
+ var Session = class {
1705
+ sessionId;
1706
+ span;
1707
+ inputs = [];
1708
+ outputs = [];
1709
+ constructor(sessionId, span) {
1710
+ this.sessionId = sessionId;
1711
+ this.span = span;
1712
+ }
1713
+ /**
1714
+ * (Optional) Append text to session input tracking.
1715
+ * Use when you need to track specific input data that differs from what's sent to the LLM.
1716
+ * Multiple calls accumulate in an array.
1717
+ *
1718
+ * @param text - Text to append to session input
1719
+ */
1720
+ setInput(text) {
1721
+ this.inputs.push(text);
1722
+ this.span.setAttribute(SESSION_INPUT, JSON.stringify(this.inputs));
1723
+ }
1724
+ /**
1725
+ * (Optional) Append text to session output tracking.
1726
+ * Use when you need to track specific output data that differs from what's received from the LLM.
1727
+ * Multiple calls accumulate in an array.
1728
+ *
1729
+ * @param text - Text to append to session output
1730
+ */
1731
+ setOutput(text) {
1732
+ this.outputs.push(text);
1733
+ this.span.setAttribute(SESSION_OUTPUT, JSON.stringify(this.outputs));
1734
+ }
1735
+ /**
1736
+ * Update custom properties on the session span.
1737
+ * Properties are prefixed with 'brizz.'.
1738
+ *
1739
+ * @param properties - Key-value properties to set on the session
1740
+ */
1741
+ updateProperties(properties) {
1742
+ for (const [key, value] of Object.entries(properties)) {
1743
+ this.span.setAttribute(`${BRIZZ}.${key}`, value);
1744
+ }
1745
+ }
1746
+ };
1747
+ function startSession(sessionId, callback, extraProperties) {
1748
+ const tracer = import_api5.trace.getTracer("@brizz/sdk");
1749
+ return tracer.startActiveSpan(SESSION_SPAN_NAME, (span) => {
1750
+ span.setAttribute(`${BRIZZ}.${SESSION_ID}`, sessionId);
1751
+ if (extraProperties) {
1752
+ for (const [key, value] of Object.entries(extraProperties)) {
1753
+ span.setAttribute(`${BRIZZ}.${key}`, value);
1754
+ }
1755
+ }
1756
+ const session = new Session(sessionId, span);
1757
+ const contextProperties = { [SESSION_ID]: sessionId };
1758
+ if (extraProperties) {
1759
+ for (const [key, value] of Object.entries(extraProperties)) {
1760
+ contextProperties[key] = String(value);
1761
+ }
1762
+ }
1763
+ return callWithProperties(contextProperties, () => {
1764
+ try {
1765
+ const result = callback(session);
1766
+ if (result && typeof result.then === "function") {
1767
+ return result.then((value) => {
1768
+ span.end();
1769
+ return value;
1770
+ }).catch((error) => {
1771
+ span.recordException(error);
1772
+ span.setStatus({ code: import_api5.SpanStatusCode.ERROR });
1773
+ span.end();
1774
+ throw error;
1775
+ });
1776
+ }
1777
+ span.end();
1778
+ return result;
1779
+ } catch (error) {
1780
+ span.recordException(error);
1781
+ span.setStatus({ code: import_api5.SpanStatusCode.ERROR });
1782
+ span.end();
1783
+ throw error;
1784
+ }
1785
+ });
1786
+ });
1787
+ }
1698
1788
 
1699
1789
  // src/internal/sdk.ts
1700
1790
  var _Brizz = class __Brizz {
@@ -1945,6 +2035,7 @@ var init_exports = {};
1945
2035
  Brizz,
1946
2036
  DEFAULT_PII_PATTERNS,
1947
2037
  LogLevel,
2038
+ Session,
1948
2039
  SeverityNumber,
1949
2040
  callWithProperties,
1950
2041
  callWithSessionId,
@@ -1960,6 +2051,7 @@ var init_exports = {};
1960
2051
  maskAttributes,
1961
2052
  maskValue,
1962
2053
  setLogLevel,
2054
+ startSession,
1963
2055
  withProperties,
1964
2056
  withSessionId
1965
2057
  });