@brizz/sdk 0.1.14 → 0.1.16

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
@@ -288,6 +288,7 @@ const result = await startSession(
288
288
  - `session.updateProperties({ key: value })` - Add custom properties to the session span
289
289
  - `session.setInput(text)` - (Optional) Manually track input text
290
290
  - `session.setOutput(text)` - (Optional) Manually track output text
291
+ - `session.setTitle(text)` - Set a session title (typically used with `mode: 'title'`)
291
292
 
292
293
  **When to use manual input/output tracking:**
293
294
 
@@ -313,6 +314,58 @@ await startSession('session-456', async (session) => {
313
314
  });
314
315
  ```
315
316
 
317
+ ### Session Title Generation
318
+
319
+ If you use an LLM call to generate session titles, wrap it so those spans don't appear as part of the conversation:
320
+
321
+ ```typescript
322
+ import { startSession, startSessionTitle } from '@brizz/sdk';
323
+
324
+ await startSession('session-123', async (session) => {
325
+ const response = await openai.chat.completions.create({...});
326
+
327
+ // Title generation — excluded from conversation view
328
+ await startSessionTitle(async (title) => {
329
+ const t = await openai.chat.completions.create({
330
+ model: 'gpt-4',
331
+ messages: [{ role: 'user', content: 'Summarize this chat in 3 words' }],
332
+ });
333
+ title.setTitle(t.choices[0].message.content);
334
+ });
335
+ });
336
+
337
+ // Or use mode: 'title' on startSession directly
338
+ await startSession('session-123', async (session) => {
339
+ const t = await openai.chat.completions.create({...});
340
+ session.setTitle(t.choices[0].message.content);
341
+ }, undefined, { mode: 'title' });
342
+
343
+ // Or use startSessionTitle outside a session (pass sessionId explicitly)
344
+ await startSessionTitle(async (title) => {
345
+ title.setTitle('My Title');
346
+ }, { sessionId: 'session-123' });
347
+ ```
348
+
349
+ ### Accessing the Active Session
350
+
351
+ Use `getActiveSession()` to retrieve the current session from anywhere within a `startSession` scope — no need to pass the session object through your call stack:
352
+
353
+ ```typescript
354
+ import { startSession, getActiveSession } from '@brizz/sdk';
355
+
356
+ function deepHelper() {
357
+ const session = getActiveSession();
358
+ session?.updateProperties({ step: 'helper' });
359
+ }
360
+
361
+ await startSession('session-123', async () => {
362
+ deepHelper(); // accesses session without it being passed as a parameter
363
+ });
364
+
365
+ // Outside a session, returns undefined
366
+ getActiveSession(); // undefined
367
+ ```
368
+
316
369
  ### Function Wrapper Pattern
317
370
 
318
371
  For simpler cases where you just need to tag traces with a session ID:
package/dist/index.cjs CHANGED
@@ -34,11 +34,13 @@ __export(src_exports, {
34
34
  DEFAULT_PII_PATTERNS: () => DEFAULT_PII_PATTERNS,
35
35
  LogLevel: () => LogLevel,
36
36
  Session: () => Session,
37
+ SessionTitle: () => SessionTitle,
37
38
  SeverityNumber: () => import_api_logs2.SeverityNumber,
38
39
  callWithProperties: () => callWithProperties,
39
40
  callWithSessionId: () => callWithSessionId,
40
41
  detectRuntime: () => detectRuntime,
41
42
  emitEvent: () => emitEvent,
43
+ getActiveSession: () => getActiveSession,
42
44
  getLogLevel: () => getLogLevel,
43
45
  getMetricsExporter: () => getMetricsExporter,
44
46
  getMetricsReader: () => getMetricsReader,
@@ -50,6 +52,7 @@ __export(src_exports, {
50
52
  maskValue: () => maskValue,
51
53
  setLogLevel: () => setLogLevel,
52
54
  startSession: () => startSession,
55
+ startSessionTitle: () => startSessionTitle,
53
56
  withProperties: () => withProperties,
54
57
  withSessionId: () => withSessionId
55
58
  });
@@ -493,7 +496,7 @@ var import_sdk_logs2 = require("@opentelemetry/sdk-logs");
493
496
 
494
497
  // src/internal/version.ts
495
498
  function getSDKVersion() {
496
- return "0.1.14";
499
+ return "0.1.16";
497
500
  }
498
501
 
499
502
  // src/internal/log/processors/log-processor.ts
@@ -1130,9 +1133,13 @@ var BRIZZ = "brizz";
1130
1133
  var PROPERTIES = "properties";
1131
1134
  var SESSION_ID = "session.id";
1132
1135
  var PROPERTIES_CONTEXT_KEY = (0, import_api2.createContextKey)(PROPERTIES);
1136
+ var SESSION_OBJECT_CONTEXT_KEY = (0, import_api2.createContextKey)("brizz.session.object");
1133
1137
  var SESSION_INPUT = "brizz.session.input";
1134
1138
  var SESSION_OUTPUT = "brizz.session.output";
1135
1139
  var SESSION_SPAN_NAME = "brizz.start_session";
1140
+ var SESSION_TITLE_SPAN_NAME = "brizz.session_title";
1141
+ var SESSION_TITLE_GENERATION = "session.title_generation";
1142
+ var SESSION_TITLE = "brizz.session.title";
1136
1143
 
1137
1144
  // src/internal/log/processors/log-processor.ts
1138
1145
  var DEFAULT_LOG_MASKING_RULES = [
@@ -1762,7 +1769,7 @@ var Session = class {
1762
1769
  * Use when you need to track specific input data that differs from what's sent to the LLM.
1763
1770
  * Multiple calls accumulate in an array.
1764
1771
  *
1765
- * @param text - Text to append to session input
1772
+ * @param text - Text to append to session input, or null to append null
1766
1773
  */
1767
1774
  setInput(text) {
1768
1775
  this.inputs.push(text);
@@ -1773,12 +1780,20 @@ var Session = class {
1773
1780
  * Use when you need to track specific output data that differs from what's received from the LLM.
1774
1781
  * Multiple calls accumulate in an array.
1775
1782
  *
1776
- * @param text - Text to append to session output
1783
+ * @param text - Text to append to session output, or null to append null
1777
1784
  */
1778
1785
  setOutput(text) {
1779
1786
  this.outputs.push(text);
1780
1787
  this.span.setAttribute(SESSION_OUTPUT, JSON.stringify(this.outputs));
1781
1788
  }
1789
+ /**
1790
+ * Set the session title on the span.
1791
+ *
1792
+ * @param title - The generated title string
1793
+ */
1794
+ setTitle(title) {
1795
+ this.span.setAttribute(SESSION_TITLE, title);
1796
+ }
1782
1797
  /**
1783
1798
  * Update custom properties on the session span.
1784
1799
  * Properties are prefixed with 'brizz.'.
@@ -1791,9 +1806,28 @@ var Session = class {
1791
1806
  }
1792
1807
  }
1793
1808
  };
1794
- function startSession(sessionId, callback, extraProperties) {
1809
+ var SessionTitle = class {
1810
+ span;
1811
+ constructor(span) {
1812
+ this.span = span;
1813
+ }
1814
+ /**
1815
+ * Set the generated title on the wrapper span.
1816
+ *
1817
+ * @param title - The generated title string
1818
+ */
1819
+ setTitle(title) {
1820
+ this.span.setAttribute(SESSION_TITLE, title);
1821
+ }
1822
+ };
1823
+ function getActiveSession() {
1824
+ return import_api5.context.active().getValue(SESSION_OBJECT_CONTEXT_KEY);
1825
+ }
1826
+ function startSession(sessionId, callback, extraProperties, options) {
1827
+ const isTitle = options?.mode === "title";
1828
+ const spanName = isTitle ? SESSION_TITLE_SPAN_NAME : SESSION_SPAN_NAME;
1795
1829
  const tracer = import_api5.trace.getTracer("@brizz/sdk");
1796
- return tracer.startActiveSpan(SESSION_SPAN_NAME, (span) => {
1830
+ return tracer.startActiveSpan(spanName, (span) => {
1797
1831
  span.setAttribute(`${BRIZZ}.${SESSION_ID}`, sessionId);
1798
1832
  if (extraProperties) {
1799
1833
  for (const [key, value] of Object.entries(extraProperties)) {
@@ -1802,14 +1836,57 @@ function startSession(sessionId, callback, extraProperties) {
1802
1836
  }
1803
1837
  const session = new Session(sessionId, span);
1804
1838
  const contextProperties = { [SESSION_ID]: sessionId };
1839
+ if (isTitle) {
1840
+ contextProperties[SESSION_TITLE_GENERATION] = "true";
1841
+ }
1805
1842
  if (extraProperties) {
1806
1843
  for (const [key, value] of Object.entries(extraProperties)) {
1807
1844
  contextProperties[key] = String(value);
1808
1845
  }
1809
1846
  }
1810
1847
  return callWithProperties(contextProperties, () => {
1848
+ const sessionCtx = import_api5.context.active().setValue(SESSION_OBJECT_CONTEXT_KEY, session);
1849
+ return import_api5.context.with(sessionCtx, () => {
1850
+ try {
1851
+ const result = callback(session);
1852
+ if (result && typeof result.then === "function") {
1853
+ return result.then((value) => {
1854
+ span.end();
1855
+ return value;
1856
+ }).catch((error) => {
1857
+ span.recordException(error);
1858
+ span.setStatus({ code: import_api5.SpanStatusCode.ERROR });
1859
+ span.end();
1860
+ throw error;
1861
+ });
1862
+ }
1863
+ span.end();
1864
+ return result;
1865
+ } catch (error) {
1866
+ span.recordException(error);
1867
+ span.setStatus({ code: import_api5.SpanStatusCode.ERROR });
1868
+ span.end();
1869
+ throw error;
1870
+ }
1871
+ });
1872
+ });
1873
+ });
1874
+ }
1875
+ function startSessionTitle(callback, options) {
1876
+ const resolvedSessionId = options?.sessionId ?? getActiveSession()?.sessionId;
1877
+ const tracer = import_api5.trace.getTracer("@brizz/sdk");
1878
+ return tracer.startActiveSpan(SESSION_TITLE_SPAN_NAME, (span) => {
1879
+ if (resolvedSessionId) {
1880
+ span.setAttribute(`${BRIZZ}.${SESSION_ID}`, resolvedSessionId);
1881
+ }
1882
+ const sessionTitle = new SessionTitle(span);
1883
+ const properties = { [SESSION_TITLE_GENERATION]: "true" };
1884
+ if (resolvedSessionId) {
1885
+ properties[SESSION_ID] = resolvedSessionId;
1886
+ }
1887
+ return callWithProperties(properties, () => {
1811
1888
  try {
1812
- const result = callback(session);
1889
+ const result = callback(sessionTitle);
1813
1890
  if (result && typeof result.then === "function") {
1814
1891
  return result.then((value) => {
1815
1892
  span.end();
@@ -2083,11 +2160,13 @@ var init_exports = {};
2083
2160
  DEFAULT_PII_PATTERNS,
2084
2161
  LogLevel,
2085
2162
  Session,
2163
+ SessionTitle,
2086
2164
  SeverityNumber,
2087
2165
  callWithProperties,
2088
2166
  callWithSessionId,
2089
2167
  detectRuntime,
2090
2168
  emitEvent,
2169
+ getActiveSession,
2091
2170
  getLogLevel,
2092
2171
  getMetricsExporter,
2093
2172
  getMetricsReader,
@@ -2099,6 +2178,7 @@ var init_exports = {};
2099
2178
  maskValue,
2100
2179
  setLogLevel,
2101
2180
  startSession,
2181
+ startSessionTitle,
2102
2182
  withProperties,
2103
2183
  withSessionId
2104
2184
  });