@brizz/sdk 0.1.27 → 0.1.29
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 +53 -1
- package/dist/index.cjs +319 -223
- package/dist/index.d.cts +17 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +315 -223
- package/dist/preload.cjs +30 -17
- package/dist/preload.js +30 -17
- package/package.json +42 -42
package/dist/index.js
CHANGED
|
@@ -290,7 +290,7 @@ var init_schemas = __esm({
|
|
|
290
290
|
|
|
291
291
|
// src/internal/semantic-conventions.ts
|
|
292
292
|
import { createContextKey } from "@opentelemetry/api";
|
|
293
|
-
var BRIZZ, PROPERTIES, SESSION_ID, PROPERTIES_CONTEXT_KEY, SESSION_OBJECT_CONTEXT_KEY, SESSION_INPUT, SESSION_OUTPUT, SESSION_INPUT_CONTEXT, SESSION_OUTPUT_CONTEXT, SESSION_SPAN_NAME, SESSION_TITLE_SPAN_NAME, SESSION_TITLE_GENERATION, SESSION_TITLE, INTERRUPT_TOOLS;
|
|
293
|
+
var BRIZZ, PROPERTIES, SESSION_ID, PROPERTIES_CONTEXT_KEY, SESSION_OBJECT_CONTEXT_KEY, SESSION_INPUT, SESSION_OUTPUT, SESSION_INPUT_CONTEXT, SESSION_OUTPUT_CONTEXT, SESSION_SPAN_NAME, MUTE_INPUT, MUTE_OUTPUT, SESSION_TITLE_SPAN_NAME, SESSION_TITLE_GENERATION, SESSION_TITLE, INTERRUPT_TOOLS, INTERNAL_EVENT_PREFIX, EXTERNAL_LINK_EVENT_NAME, EXTERNAL_LINK_URL, EXTERNAL_LINK_TITLE, EXTERNAL_LINK_TYPE;
|
|
294
294
|
var init_semantic_conventions2 = __esm({
|
|
295
295
|
"src/internal/semantic-conventions.ts"() {
|
|
296
296
|
"use strict";
|
|
@@ -304,10 +304,17 @@ var init_semantic_conventions2 = __esm({
|
|
|
304
304
|
SESSION_INPUT_CONTEXT = "brizz.session.input.context";
|
|
305
305
|
SESSION_OUTPUT_CONTEXT = "brizz.session.output.context";
|
|
306
306
|
SESSION_SPAN_NAME = "brizz.start_session";
|
|
307
|
+
MUTE_INPUT = "mute.input";
|
|
308
|
+
MUTE_OUTPUT = "mute.output";
|
|
307
309
|
SESSION_TITLE_SPAN_NAME = "brizz.session_title";
|
|
308
310
|
SESSION_TITLE_GENERATION = "session.title_generation";
|
|
309
311
|
SESSION_TITLE = "brizz.session.title";
|
|
310
312
|
INTERRUPT_TOOLS = "brizz.internal.interrupt";
|
|
313
|
+
INTERNAL_EVENT_PREFIX = "brizz.internal.";
|
|
314
|
+
EXTERNAL_LINK_EVENT_NAME = "brizz.internal.external_link";
|
|
315
|
+
EXTERNAL_LINK_URL = "brizz.internal.external_link.url";
|
|
316
|
+
EXTERNAL_LINK_TITLE = "brizz.internal.external_link.title";
|
|
317
|
+
EXTERNAL_LINK_TYPE = "brizz.internal.external_link.type";
|
|
311
318
|
}
|
|
312
319
|
});
|
|
313
320
|
|
|
@@ -773,7 +780,7 @@ var init_protocol = __esm({
|
|
|
773
780
|
|
|
774
781
|
// src/internal/version.ts
|
|
775
782
|
function getSDKVersion() {
|
|
776
|
-
return "0.1.
|
|
783
|
+
return "0.1.29";
|
|
777
784
|
}
|
|
778
785
|
var init_version = __esm({
|
|
779
786
|
"src/internal/version.ts"() {
|
|
@@ -1483,11 +1490,283 @@ import {
|
|
|
1483
1490
|
LoggerProvider
|
|
1484
1491
|
} from "@opentelemetry/sdk-logs";
|
|
1485
1492
|
init_logger();
|
|
1493
|
+
|
|
1494
|
+
// src/internal/trace/session.ts
|
|
1495
|
+
import { context as context3, trace as trace4, SpanStatusCode as SpanStatusCode3 } from "@opentelemetry/api";
|
|
1496
|
+
init_logger();
|
|
1497
|
+
init_semantic_conventions2();
|
|
1498
|
+
function setCurrentSpanCustomProperties(properties) {
|
|
1499
|
+
const current = trace4.getActiveSpan();
|
|
1500
|
+
if (!current || !current.isRecording()) {
|
|
1501
|
+
return;
|
|
1502
|
+
}
|
|
1503
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
1504
|
+
try {
|
|
1505
|
+
current.setAttribute(`${BRIZZ}.${key}`, value);
|
|
1506
|
+
} catch {
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
function callWithProperties(properties, fn, thisArg, ...args) {
|
|
1511
|
+
const base = context3.active();
|
|
1512
|
+
const prev = base.getValue(PROPERTIES_CONTEXT_KEY);
|
|
1513
|
+
const merged = prev ? { ...prev, ...properties } : properties;
|
|
1514
|
+
const next = base.setValue(PROPERTIES_CONTEXT_KEY, merged);
|
|
1515
|
+
return context3.with(next, fn, thisArg, ...args);
|
|
1516
|
+
}
|
|
1517
|
+
function withProperties(properties, fn, thisArg) {
|
|
1518
|
+
return function wrapped(...args) {
|
|
1519
|
+
return callWithProperties(
|
|
1520
|
+
properties,
|
|
1521
|
+
fn,
|
|
1522
|
+
thisArg !== void 0 ? thisArg : this,
|
|
1523
|
+
...args
|
|
1524
|
+
);
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1527
|
+
function muteProperties(options) {
|
|
1528
|
+
const { input = true, output = true } = options;
|
|
1529
|
+
const properties = {};
|
|
1530
|
+
if (input) {
|
|
1531
|
+
properties[MUTE_INPUT] = "true";
|
|
1532
|
+
}
|
|
1533
|
+
if (output) {
|
|
1534
|
+
properties[MUTE_OUTPUT] = "true";
|
|
1535
|
+
}
|
|
1536
|
+
return properties;
|
|
1537
|
+
}
|
|
1538
|
+
function callWithMute(options, fn, thisArg, ...args) {
|
|
1539
|
+
return callWithProperties(muteProperties(options), fn, thisArg, ...args);
|
|
1540
|
+
}
|
|
1541
|
+
function withMute(options, fn, thisArg) {
|
|
1542
|
+
return withProperties(muteProperties(options), fn, thisArg);
|
|
1543
|
+
}
|
|
1544
|
+
function withSessionId(sessionId, fn, thisArg, extraProperties) {
|
|
1545
|
+
const properties = { [SESSION_ID]: sessionId, ...extraProperties };
|
|
1546
|
+
return withProperties(properties, fn, thisArg);
|
|
1547
|
+
}
|
|
1548
|
+
function callWithSessionId(sessionId, fn, thisArg, ...args) {
|
|
1549
|
+
return callWithProperties({ [SESSION_ID]: sessionId }, fn, thisArg, ...args);
|
|
1550
|
+
}
|
|
1551
|
+
var Session = class {
|
|
1552
|
+
sessionId;
|
|
1553
|
+
span;
|
|
1554
|
+
inputs = [];
|
|
1555
|
+
outputs = [];
|
|
1556
|
+
inputContexts = [];
|
|
1557
|
+
outputContexts = [];
|
|
1558
|
+
constructor(sessionId, span) {
|
|
1559
|
+
this.sessionId = sessionId;
|
|
1560
|
+
this.span = span;
|
|
1561
|
+
}
|
|
1562
|
+
/**
|
|
1563
|
+
* Append a user turn to session input tracking.
|
|
1564
|
+
*
|
|
1565
|
+
* Each call appends one element to `brizz.session.input` (the text) AND one
|
|
1566
|
+
* element to `brizz.session.input.context` (the per-turn context bag). The
|
|
1567
|
+
* two arrays stay index-aligned — the ingestion pipeline zips them together
|
|
1568
|
+
* so the i-th context bag lands on the i-th user_display conversation item's
|
|
1569
|
+
* `span_attributes` map.
|
|
1570
|
+
*
|
|
1571
|
+
* @param text - Text to append (null becomes a null/"hide marker")
|
|
1572
|
+
* @param context - Optional per-turn context bag to attach to this turn
|
|
1573
|
+
*/
|
|
1574
|
+
setInput(text, context6) {
|
|
1575
|
+
this.inputs.push(text);
|
|
1576
|
+
this.inputContexts.push(context6 ?? {});
|
|
1577
|
+
this.span.setAttribute(SESSION_INPUT, JSON.stringify(this.inputs));
|
|
1578
|
+
this.span.setAttribute(SESSION_INPUT_CONTEXT, JSON.stringify(this.inputContexts));
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Append an assistant turn to session output tracking.
|
|
1582
|
+
*
|
|
1583
|
+
* Symmetric to {@link setInput} — appends one text element and one
|
|
1584
|
+
* context-bag element to the index-aligned `brizz.session.output` /
|
|
1585
|
+
* `brizz.session.output.context` arrays.
|
|
1586
|
+
*
|
|
1587
|
+
* @param text - Text to append (null becomes a null/"hide marker")
|
|
1588
|
+
* @param context - Optional per-turn context bag to attach to this turn
|
|
1589
|
+
*/
|
|
1590
|
+
setOutput(text, context6) {
|
|
1591
|
+
this.outputs.push(text);
|
|
1592
|
+
this.outputContexts.push(context6 ?? {});
|
|
1593
|
+
this.span.setAttribute(SESSION_OUTPUT, JSON.stringify(this.outputs));
|
|
1594
|
+
this.span.setAttribute(SESSION_OUTPUT_CONTEXT, JSON.stringify(this.outputContexts));
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Set the session title on the span.
|
|
1598
|
+
*
|
|
1599
|
+
* @param title - The generated title string
|
|
1600
|
+
*/
|
|
1601
|
+
setTitle(title) {
|
|
1602
|
+
this.span.setAttribute(SESSION_TITLE, title);
|
|
1603
|
+
}
|
|
1604
|
+
/**
|
|
1605
|
+
* Update custom properties on the session span.
|
|
1606
|
+
* Properties are prefixed with 'brizz.'.
|
|
1607
|
+
*
|
|
1608
|
+
* @param properties - Key-value properties to set on the session
|
|
1609
|
+
*/
|
|
1610
|
+
updateProperties(properties) {
|
|
1611
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
1612
|
+
this.span.setAttribute(`${BRIZZ}.${key}`, value);
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
/**
|
|
1616
|
+
* Attach an external link (URL) to this session.
|
|
1617
|
+
*
|
|
1618
|
+
* The link surfaces on the session's detail panel in Brizz — use it to
|
|
1619
|
+
* correlate a session with an external record such as a Datadog trace,
|
|
1620
|
+
* Sentry issue, or internal dashboard. Re-sending the same URL is idempotent;
|
|
1621
|
+
* distinct URLs add separate links.
|
|
1622
|
+
*
|
|
1623
|
+
* @param url - The link target
|
|
1624
|
+
* @param options - Optional `title` (defaults to the URL host) and `linkType`
|
|
1625
|
+
* (free-form category, defaults to `'generic'`)
|
|
1626
|
+
*/
|
|
1627
|
+
addExternalLink(url, options) {
|
|
1628
|
+
emitExternalLink(this.sessionId, url, options?.title, options?.linkType);
|
|
1629
|
+
}
|
|
1630
|
+
};
|
|
1631
|
+
var SessionTitle = class {
|
|
1632
|
+
span;
|
|
1633
|
+
constructor(span) {
|
|
1634
|
+
this.span = span;
|
|
1635
|
+
}
|
|
1636
|
+
/**
|
|
1637
|
+
* Set the generated title on the wrapper span.
|
|
1638
|
+
*
|
|
1639
|
+
* @param title - The generated title string
|
|
1640
|
+
*/
|
|
1641
|
+
setTitle(title) {
|
|
1642
|
+
this.span.setAttribute(SESSION_TITLE, title);
|
|
1643
|
+
}
|
|
1644
|
+
};
|
|
1645
|
+
function getActiveSession() {
|
|
1646
|
+
return context3.active().getValue(SESSION_OBJECT_CONTEXT_KEY);
|
|
1647
|
+
}
|
|
1648
|
+
function resolveSessionIdFromContext() {
|
|
1649
|
+
const active = getActiveSession();
|
|
1650
|
+
if (active) {
|
|
1651
|
+
return active.sessionId;
|
|
1652
|
+
}
|
|
1653
|
+
const props = context3.active().getValue(PROPERTIES_CONTEXT_KEY);
|
|
1654
|
+
return props?.[SESSION_ID];
|
|
1655
|
+
}
|
|
1656
|
+
function emitExternalLink(sessionId, url, title, linkType) {
|
|
1657
|
+
try {
|
|
1658
|
+
emitEventWithSessionId(sessionId, EXTERNAL_LINK_EVENT_NAME, {
|
|
1659
|
+
[EXTERNAL_LINK_URL]: url,
|
|
1660
|
+
[EXTERNAL_LINK_TITLE]: title ?? "",
|
|
1661
|
+
[EXTERNAL_LINK_TYPE]: linkType ?? "generic"
|
|
1662
|
+
});
|
|
1663
|
+
} catch (error) {
|
|
1664
|
+
logger.warn("addExternalLink: failed to emit external link", error);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
function addExternalLink(url, options) {
|
|
1668
|
+
const sessionId = options?.sessionId ?? resolveSessionIdFromContext();
|
|
1669
|
+
if (!sessionId) {
|
|
1670
|
+
logger.warn(
|
|
1671
|
+
"addExternalLink called without a resolvable session id; link dropped. Pass options.sessionId or call inside a startSession/callWithSessionId scope."
|
|
1672
|
+
);
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
emitExternalLink(sessionId, url, options?.title, options?.linkType);
|
|
1676
|
+
}
|
|
1677
|
+
function startSession(sessionId, callback, extraProperties, options) {
|
|
1678
|
+
const isTitle = options?.mode === "title";
|
|
1679
|
+
const spanName = isTitle ? SESSION_TITLE_SPAN_NAME : SESSION_SPAN_NAME;
|
|
1680
|
+
const tracer = trace4.getTracer("@brizz/sdk");
|
|
1681
|
+
return tracer.startActiveSpan(spanName, (span) => {
|
|
1682
|
+
span.setAttribute(`${BRIZZ}.${SESSION_ID}`, sessionId);
|
|
1683
|
+
if (extraProperties) {
|
|
1684
|
+
for (const [key, value] of Object.entries(extraProperties)) {
|
|
1685
|
+
span.setAttribute(`${BRIZZ}.${key}`, value);
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
const session = new Session(sessionId, span);
|
|
1689
|
+
const contextProperties = { [SESSION_ID]: sessionId };
|
|
1690
|
+
if (isTitle) {
|
|
1691
|
+
contextProperties[SESSION_TITLE_GENERATION] = "true";
|
|
1692
|
+
}
|
|
1693
|
+
if (extraProperties) {
|
|
1694
|
+
for (const [key, value] of Object.entries(extraProperties)) {
|
|
1695
|
+
contextProperties[key] = String(value);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
return callWithProperties(contextProperties, () => {
|
|
1699
|
+
const sessionCtx = context3.active().setValue(SESSION_OBJECT_CONTEXT_KEY, session);
|
|
1700
|
+
return context3.with(sessionCtx, () => {
|
|
1701
|
+
try {
|
|
1702
|
+
const result = callback(session);
|
|
1703
|
+
if (result && typeof result.then === "function") {
|
|
1704
|
+
return result.then((value) => {
|
|
1705
|
+
span.end();
|
|
1706
|
+
return value;
|
|
1707
|
+
}).catch((error) => {
|
|
1708
|
+
span.recordException(error);
|
|
1709
|
+
span.setStatus({ code: SpanStatusCode3.ERROR });
|
|
1710
|
+
span.end();
|
|
1711
|
+
throw error;
|
|
1712
|
+
});
|
|
1713
|
+
}
|
|
1714
|
+
span.end();
|
|
1715
|
+
return result;
|
|
1716
|
+
} catch (error) {
|
|
1717
|
+
span.recordException(error);
|
|
1718
|
+
span.setStatus({ code: SpanStatusCode3.ERROR });
|
|
1719
|
+
span.end();
|
|
1720
|
+
throw error;
|
|
1721
|
+
}
|
|
1722
|
+
});
|
|
1723
|
+
});
|
|
1724
|
+
});
|
|
1725
|
+
}
|
|
1726
|
+
function startSessionTitle(callback, options) {
|
|
1727
|
+
const resolvedSessionId = options?.sessionId ?? getActiveSession()?.sessionId;
|
|
1728
|
+
const tracer = trace4.getTracer("@brizz/sdk");
|
|
1729
|
+
return tracer.startActiveSpan(SESSION_TITLE_SPAN_NAME, (span) => {
|
|
1730
|
+
if (resolvedSessionId) {
|
|
1731
|
+
span.setAttribute(`${BRIZZ}.${SESSION_ID}`, resolvedSessionId);
|
|
1732
|
+
}
|
|
1733
|
+
const sessionTitle = new SessionTitle(span);
|
|
1734
|
+
const properties = { [SESSION_TITLE_GENERATION]: "true" };
|
|
1735
|
+
if (resolvedSessionId) {
|
|
1736
|
+
properties[SESSION_ID] = resolvedSessionId;
|
|
1737
|
+
}
|
|
1738
|
+
return callWithProperties(properties, () => {
|
|
1739
|
+
try {
|
|
1740
|
+
const result = callback(sessionTitle);
|
|
1741
|
+
if (result && typeof result.then === "function") {
|
|
1742
|
+
return result.then((value) => {
|
|
1743
|
+
span.end();
|
|
1744
|
+
return value;
|
|
1745
|
+
}).catch((error) => {
|
|
1746
|
+
span.recordException(error);
|
|
1747
|
+
span.setStatus({ code: SpanStatusCode3.ERROR });
|
|
1748
|
+
span.end();
|
|
1749
|
+
throw error;
|
|
1750
|
+
});
|
|
1751
|
+
}
|
|
1752
|
+
span.end();
|
|
1753
|
+
return result;
|
|
1754
|
+
} catch (error) {
|
|
1755
|
+
span.recordException(error);
|
|
1756
|
+
span.setStatus({ code: SpanStatusCode3.ERROR });
|
|
1757
|
+
span.end();
|
|
1758
|
+
throw error;
|
|
1759
|
+
}
|
|
1760
|
+
});
|
|
1761
|
+
});
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
// src/internal/log/logging.ts
|
|
1486
1765
|
init_version();
|
|
1487
1766
|
|
|
1488
1767
|
// src/internal/log/processors/log-processor.ts
|
|
1489
1768
|
init_logger();
|
|
1490
|
-
import { context as
|
|
1769
|
+
import { context as context4 } from "@opentelemetry/api";
|
|
1491
1770
|
import { BatchLogRecordProcessor, SimpleLogRecordProcessor } from "@opentelemetry/sdk-logs";
|
|
1492
1771
|
|
|
1493
1772
|
// src/internal/masking/patterns.ts
|
|
@@ -1579,10 +1858,6 @@ var DEFAULT_PII_PATTERNS = [
|
|
|
1579
1858
|
name: "pgp_private_keys",
|
|
1580
1859
|
pattern: "-----BEGIN PGP PRIVATE KEY BLOCK-----"
|
|
1581
1860
|
},
|
|
1582
|
-
{
|
|
1583
|
-
name: "certificates",
|
|
1584
|
-
pattern: "-----BEGIN CERTIFICATE-----"
|
|
1585
|
-
},
|
|
1586
1861
|
// Additional API Keys and Tokens
|
|
1587
1862
|
{
|
|
1588
1863
|
name: "google_oauth",
|
|
@@ -1631,7 +1906,7 @@ var DEFAULT_PII_PATTERNS = [
|
|
|
1631
1906
|
},
|
|
1632
1907
|
{
|
|
1633
1908
|
name: "ssl_certificates",
|
|
1634
|
-
pattern: String.raw`-----BEGIN CERTIFICATE-----(?:.|\n)+?\s
|
|
1909
|
+
pattern: String.raw`-----BEGIN CERTIFICATE-----(?:.|\n)+?\s*-----END CERTIFICATE-----`
|
|
1635
1910
|
},
|
|
1636
1911
|
{
|
|
1637
1912
|
name: "ssh_dss_public",
|
|
@@ -1690,8 +1965,9 @@ function isLikelyReDoSPattern(pattern) {
|
|
|
1690
1965
|
const dangerousPatterns = [
|
|
1691
1966
|
// Nested quantifiers like (a+)+, (a*)+, (a+)*
|
|
1692
1967
|
/\([^)]*[+*]\)[+*]/,
|
|
1693
|
-
//
|
|
1694
|
-
|
|
1968
|
+
// Overlapping alternation under an outer quantifier like (a|a)+, (\w|\d)+. Scoped to
|
|
1969
|
+
// capturing groups so the lazy (?:…)+? built-ins aren't false-flagged.
|
|
1970
|
+
/\((?!\?)[^)]*\|[^)]*\)[+*]/,
|
|
1695
1971
|
// Complex backtracking patterns - but more specific
|
|
1696
1972
|
/\([^)]*[+*][^)]*[+*][^)]*\)[+*]/
|
|
1697
1973
|
];
|
|
@@ -1760,6 +2036,16 @@ function compilePatternEntries(patternEntries) {
|
|
|
1760
2036
|
return null;
|
|
1761
2037
|
}
|
|
1762
2038
|
}
|
|
2039
|
+
var compiledPatternCache = /* @__PURE__ */ new WeakMap();
|
|
2040
|
+
function getCompiledPatternsForRule(rule) {
|
|
2041
|
+
const key = rule.patterns;
|
|
2042
|
+
if (compiledPatternCache.has(key)) {
|
|
2043
|
+
return compiledPatternCache.get(key) ?? null;
|
|
2044
|
+
}
|
|
2045
|
+
const compiled = compilePatternEntries(convertPatternsToPatternEntries(rule.patterns));
|
|
2046
|
+
compiledPatternCache.set(key, compiled);
|
|
2047
|
+
return compiled;
|
|
2048
|
+
}
|
|
1763
2049
|
function getCompiledAttributeNamePattern(rule) {
|
|
1764
2050
|
if (!rule.attributePattern) {
|
|
1765
2051
|
logger.debug("No attribute pattern provided, using default .*");
|
|
@@ -1853,7 +2139,7 @@ function maskStringByPatternBasedRule(value, rule) {
|
|
|
1853
2139
|
if (!patternEntries || patternEntries.length === 0) {
|
|
1854
2140
|
return mode === "partial" && value ? value[0] + "*****" : "*****";
|
|
1855
2141
|
}
|
|
1856
|
-
const compiledPatterns =
|
|
2142
|
+
const compiledPatterns = getCompiledPatternsForRule(rule);
|
|
1857
2143
|
if (!compiledPatterns) {
|
|
1858
2144
|
return value;
|
|
1859
2145
|
}
|
|
@@ -1918,7 +2204,7 @@ var BrizzSimpleLogRecordProcessor = class extends SimpleLogRecordProcessor {
|
|
|
1918
2204
|
if (maskingConfig) {
|
|
1919
2205
|
maskLog(logRecord, maskingConfig);
|
|
1920
2206
|
}
|
|
1921
|
-
const associationProperties =
|
|
2207
|
+
const associationProperties = context4.active().getValue(PROPERTIES_CONTEXT_KEY);
|
|
1922
2208
|
if (associationProperties) {
|
|
1923
2209
|
for (const [key, value] of Object.entries(associationProperties)) {
|
|
1924
2210
|
logRecord.setAttribute(`${BRIZZ}.${key}`, value);
|
|
@@ -1938,7 +2224,7 @@ var BrizzBatchLogRecordProcessor = class extends BatchLogRecordProcessor {
|
|
|
1938
2224
|
if (maskingConfig) {
|
|
1939
2225
|
maskLog(logRecord, maskingConfig);
|
|
1940
2226
|
}
|
|
1941
|
-
const associationProperties =
|
|
2227
|
+
const associationProperties = context4.active().getValue(PROPERTIES_CONTEXT_KEY);
|
|
1942
2228
|
if (associationProperties) {
|
|
1943
2229
|
for (const [key, value] of Object.entries(associationProperties)) {
|
|
1944
2230
|
logRecord.setAttribute(`${BRIZZ}.${key}`, value);
|
|
@@ -1967,6 +2253,11 @@ function maskLog(logRecord, config) {
|
|
|
1967
2253
|
for (const [key, value] of Object.entries(maskedAttributes)) {
|
|
1968
2254
|
newAttributes[key] = value;
|
|
1969
2255
|
}
|
|
2256
|
+
for (const [key, value] of Object.entries(logRecord.attributes)) {
|
|
2257
|
+
if (key.startsWith(INTERNAL_EVENT_PREFIX)) {
|
|
2258
|
+
newAttributes[key] = value;
|
|
2259
|
+
}
|
|
2260
|
+
}
|
|
1970
2261
|
logRecord.setAttributes(newAttributes);
|
|
1971
2262
|
}
|
|
1972
2263
|
}
|
|
@@ -2156,6 +2447,11 @@ var LoggingModule = class _LoggingModule {
|
|
|
2156
2447
|
function emitEvent(name, attributes, body, severityNumber = SeverityNumber.INFO) {
|
|
2157
2448
|
return LoggingModule.getInstance().emitEvent(name, attributes, body, severityNumber);
|
|
2158
2449
|
}
|
|
2450
|
+
function emitEventWithSessionId(sessionId, name, attributes, body, severityNumber = SeverityNumber.INFO) {
|
|
2451
|
+
return callWithSessionId(sessionId, () => {
|
|
2452
|
+
LoggingModule.getInstance().emitEvent(name, attributes, body, severityNumber);
|
|
2453
|
+
});
|
|
2454
|
+
}
|
|
2159
2455
|
|
|
2160
2456
|
// src/internal/sdk.ts
|
|
2161
2457
|
init_logger();
|
|
@@ -2339,14 +2635,14 @@ var BrizzSpanExporter = class {
|
|
|
2339
2635
|
|
|
2340
2636
|
// src/internal/trace/processors/span-processor.ts
|
|
2341
2637
|
init_logger();
|
|
2342
|
-
import { context as
|
|
2638
|
+
import { context as context5 } from "@opentelemetry/api";
|
|
2343
2639
|
import {
|
|
2344
2640
|
BatchSpanProcessor,
|
|
2345
2641
|
SimpleSpanProcessor
|
|
2346
2642
|
} from "@opentelemetry/sdk-trace-base";
|
|
2347
2643
|
init_semantic_conventions2();
|
|
2348
2644
|
function applyContextAttributes(span) {
|
|
2349
|
-
const sessionProperties =
|
|
2645
|
+
const sessionProperties = context5.active().getValue(PROPERTIES_CONTEXT_KEY);
|
|
2350
2646
|
if (sessionProperties) {
|
|
2351
2647
|
for (const [key, value] of Object.entries(sessionProperties)) {
|
|
2352
2648
|
span.setAttribute(`${BRIZZ}.${key}`, value);
|
|
@@ -2531,214 +2827,6 @@ function getSpanProcessor() {
|
|
|
2531
2827
|
return TracingModule.getInstance().getSpanProcessor();
|
|
2532
2828
|
}
|
|
2533
2829
|
|
|
2534
|
-
// src/internal/trace/session.ts
|
|
2535
|
-
init_semantic_conventions2();
|
|
2536
|
-
import { context as context5, trace as trace4, SpanStatusCode as SpanStatusCode3 } from "@opentelemetry/api";
|
|
2537
|
-
function setCurrentSpanCustomProperties(properties) {
|
|
2538
|
-
const current = trace4.getActiveSpan();
|
|
2539
|
-
if (!current || !current.isRecording()) {
|
|
2540
|
-
return;
|
|
2541
|
-
}
|
|
2542
|
-
for (const [key, value] of Object.entries(properties)) {
|
|
2543
|
-
try {
|
|
2544
|
-
current.setAttribute(`${BRIZZ}.${key}`, value);
|
|
2545
|
-
} catch {
|
|
2546
|
-
}
|
|
2547
|
-
}
|
|
2548
|
-
}
|
|
2549
|
-
function callWithProperties(properties, fn, thisArg, ...args) {
|
|
2550
|
-
const base = context5.active();
|
|
2551
|
-
const prev = base.getValue(PROPERTIES_CONTEXT_KEY);
|
|
2552
|
-
const merged = prev ? { ...prev, ...properties } : properties;
|
|
2553
|
-
const next = base.setValue(PROPERTIES_CONTEXT_KEY, merged);
|
|
2554
|
-
return context5.with(next, fn, thisArg, ...args);
|
|
2555
|
-
}
|
|
2556
|
-
function withProperties(properties, fn, thisArg) {
|
|
2557
|
-
return function wrapped(...args) {
|
|
2558
|
-
return callWithProperties(
|
|
2559
|
-
properties,
|
|
2560
|
-
fn,
|
|
2561
|
-
thisArg !== void 0 ? thisArg : this,
|
|
2562
|
-
...args
|
|
2563
|
-
);
|
|
2564
|
-
};
|
|
2565
|
-
}
|
|
2566
|
-
function withSessionId(sessionId, fn, thisArg, extraProperties) {
|
|
2567
|
-
const properties = { [SESSION_ID]: sessionId, ...extraProperties };
|
|
2568
|
-
return withProperties(properties, fn, thisArg);
|
|
2569
|
-
}
|
|
2570
|
-
function callWithSessionId(sessionId, fn, thisArg, ...args) {
|
|
2571
|
-
return callWithProperties({ [SESSION_ID]: sessionId }, fn, thisArg, ...args);
|
|
2572
|
-
}
|
|
2573
|
-
var Session = class {
|
|
2574
|
-
sessionId;
|
|
2575
|
-
span;
|
|
2576
|
-
inputs = [];
|
|
2577
|
-
outputs = [];
|
|
2578
|
-
inputContexts = [];
|
|
2579
|
-
outputContexts = [];
|
|
2580
|
-
constructor(sessionId, span) {
|
|
2581
|
-
this.sessionId = sessionId;
|
|
2582
|
-
this.span = span;
|
|
2583
|
-
}
|
|
2584
|
-
/**
|
|
2585
|
-
* Append a user turn to session input tracking.
|
|
2586
|
-
*
|
|
2587
|
-
* Each call appends one element to `brizz.session.input` (the text) AND one
|
|
2588
|
-
* element to `brizz.session.input.context` (the per-turn context bag). The
|
|
2589
|
-
* two arrays stay index-aligned — the ingestion pipeline zips them together
|
|
2590
|
-
* so the i-th context bag lands on the i-th user_display conversation item's
|
|
2591
|
-
* `span_attributes` map.
|
|
2592
|
-
*
|
|
2593
|
-
* @param text - Text to append (null becomes a null/"hide marker")
|
|
2594
|
-
* @param context - Optional per-turn context bag to attach to this turn
|
|
2595
|
-
*/
|
|
2596
|
-
setInput(text, context6) {
|
|
2597
|
-
this.inputs.push(text);
|
|
2598
|
-
this.inputContexts.push(context6 ?? {});
|
|
2599
|
-
this.span.setAttribute(SESSION_INPUT, JSON.stringify(this.inputs));
|
|
2600
|
-
this.span.setAttribute(SESSION_INPUT_CONTEXT, JSON.stringify(this.inputContexts));
|
|
2601
|
-
}
|
|
2602
|
-
/**
|
|
2603
|
-
* Append an assistant turn to session output tracking.
|
|
2604
|
-
*
|
|
2605
|
-
* Symmetric to {@link setInput} — appends one text element and one
|
|
2606
|
-
* context-bag element to the index-aligned `brizz.session.output` /
|
|
2607
|
-
* `brizz.session.output.context` arrays.
|
|
2608
|
-
*
|
|
2609
|
-
* @param text - Text to append (null becomes a null/"hide marker")
|
|
2610
|
-
* @param context - Optional per-turn context bag to attach to this turn
|
|
2611
|
-
*/
|
|
2612
|
-
setOutput(text, context6) {
|
|
2613
|
-
this.outputs.push(text);
|
|
2614
|
-
this.outputContexts.push(context6 ?? {});
|
|
2615
|
-
this.span.setAttribute(SESSION_OUTPUT, JSON.stringify(this.outputs));
|
|
2616
|
-
this.span.setAttribute(SESSION_OUTPUT_CONTEXT, JSON.stringify(this.outputContexts));
|
|
2617
|
-
}
|
|
2618
|
-
/**
|
|
2619
|
-
* Set the session title on the span.
|
|
2620
|
-
*
|
|
2621
|
-
* @param title - The generated title string
|
|
2622
|
-
*/
|
|
2623
|
-
setTitle(title) {
|
|
2624
|
-
this.span.setAttribute(SESSION_TITLE, title);
|
|
2625
|
-
}
|
|
2626
|
-
/**
|
|
2627
|
-
* Update custom properties on the session span.
|
|
2628
|
-
* Properties are prefixed with 'brizz.'.
|
|
2629
|
-
*
|
|
2630
|
-
* @param properties - Key-value properties to set on the session
|
|
2631
|
-
*/
|
|
2632
|
-
updateProperties(properties) {
|
|
2633
|
-
for (const [key, value] of Object.entries(properties)) {
|
|
2634
|
-
this.span.setAttribute(`${BRIZZ}.${key}`, value);
|
|
2635
|
-
}
|
|
2636
|
-
}
|
|
2637
|
-
};
|
|
2638
|
-
var SessionTitle = class {
|
|
2639
|
-
span;
|
|
2640
|
-
constructor(span) {
|
|
2641
|
-
this.span = span;
|
|
2642
|
-
}
|
|
2643
|
-
/**
|
|
2644
|
-
* Set the generated title on the wrapper span.
|
|
2645
|
-
*
|
|
2646
|
-
* @param title - The generated title string
|
|
2647
|
-
*/
|
|
2648
|
-
setTitle(title) {
|
|
2649
|
-
this.span.setAttribute(SESSION_TITLE, title);
|
|
2650
|
-
}
|
|
2651
|
-
};
|
|
2652
|
-
function getActiveSession() {
|
|
2653
|
-
return context5.active().getValue(SESSION_OBJECT_CONTEXT_KEY);
|
|
2654
|
-
}
|
|
2655
|
-
function startSession(sessionId, callback, extraProperties, options) {
|
|
2656
|
-
const isTitle = options?.mode === "title";
|
|
2657
|
-
const spanName = isTitle ? SESSION_TITLE_SPAN_NAME : SESSION_SPAN_NAME;
|
|
2658
|
-
const tracer = trace4.getTracer("@brizz/sdk");
|
|
2659
|
-
return tracer.startActiveSpan(spanName, (span) => {
|
|
2660
|
-
span.setAttribute(`${BRIZZ}.${SESSION_ID}`, sessionId);
|
|
2661
|
-
if (extraProperties) {
|
|
2662
|
-
for (const [key, value] of Object.entries(extraProperties)) {
|
|
2663
|
-
span.setAttribute(`${BRIZZ}.${key}`, value);
|
|
2664
|
-
}
|
|
2665
|
-
}
|
|
2666
|
-
const session = new Session(sessionId, span);
|
|
2667
|
-
const contextProperties = { [SESSION_ID]: sessionId };
|
|
2668
|
-
if (isTitle) {
|
|
2669
|
-
contextProperties[SESSION_TITLE_GENERATION] = "true";
|
|
2670
|
-
}
|
|
2671
|
-
if (extraProperties) {
|
|
2672
|
-
for (const [key, value] of Object.entries(extraProperties)) {
|
|
2673
|
-
contextProperties[key] = String(value);
|
|
2674
|
-
}
|
|
2675
|
-
}
|
|
2676
|
-
return callWithProperties(contextProperties, () => {
|
|
2677
|
-
const sessionCtx = context5.active().setValue(SESSION_OBJECT_CONTEXT_KEY, session);
|
|
2678
|
-
return context5.with(sessionCtx, () => {
|
|
2679
|
-
try {
|
|
2680
|
-
const result = callback(session);
|
|
2681
|
-
if (result && typeof result.then === "function") {
|
|
2682
|
-
return result.then((value) => {
|
|
2683
|
-
span.end();
|
|
2684
|
-
return value;
|
|
2685
|
-
}).catch((error) => {
|
|
2686
|
-
span.recordException(error);
|
|
2687
|
-
span.setStatus({ code: SpanStatusCode3.ERROR });
|
|
2688
|
-
span.end();
|
|
2689
|
-
throw error;
|
|
2690
|
-
});
|
|
2691
|
-
}
|
|
2692
|
-
span.end();
|
|
2693
|
-
return result;
|
|
2694
|
-
} catch (error) {
|
|
2695
|
-
span.recordException(error);
|
|
2696
|
-
span.setStatus({ code: SpanStatusCode3.ERROR });
|
|
2697
|
-
span.end();
|
|
2698
|
-
throw error;
|
|
2699
|
-
}
|
|
2700
|
-
});
|
|
2701
|
-
});
|
|
2702
|
-
});
|
|
2703
|
-
}
|
|
2704
|
-
function startSessionTitle(callback, options) {
|
|
2705
|
-
const resolvedSessionId = options?.sessionId ?? getActiveSession()?.sessionId;
|
|
2706
|
-
const tracer = trace4.getTracer("@brizz/sdk");
|
|
2707
|
-
return tracer.startActiveSpan(SESSION_TITLE_SPAN_NAME, (span) => {
|
|
2708
|
-
if (resolvedSessionId) {
|
|
2709
|
-
span.setAttribute(`${BRIZZ}.${SESSION_ID}`, resolvedSessionId);
|
|
2710
|
-
}
|
|
2711
|
-
const sessionTitle = new SessionTitle(span);
|
|
2712
|
-
const properties = { [SESSION_TITLE_GENERATION]: "true" };
|
|
2713
|
-
if (resolvedSessionId) {
|
|
2714
|
-
properties[SESSION_ID] = resolvedSessionId;
|
|
2715
|
-
}
|
|
2716
|
-
return callWithProperties(properties, () => {
|
|
2717
|
-
try {
|
|
2718
|
-
const result = callback(sessionTitle);
|
|
2719
|
-
if (result && typeof result.then === "function") {
|
|
2720
|
-
return result.then((value) => {
|
|
2721
|
-
span.end();
|
|
2722
|
-
return value;
|
|
2723
|
-
}).catch((error) => {
|
|
2724
|
-
span.recordException(error);
|
|
2725
|
-
span.setStatus({ code: SpanStatusCode3.ERROR });
|
|
2726
|
-
span.end();
|
|
2727
|
-
throw error;
|
|
2728
|
-
});
|
|
2729
|
-
}
|
|
2730
|
-
span.end();
|
|
2731
|
-
return result;
|
|
2732
|
-
} catch (error) {
|
|
2733
|
-
span.recordException(error);
|
|
2734
|
-
span.setStatus({ code: SpanStatusCode3.ERROR });
|
|
2735
|
-
span.end();
|
|
2736
|
-
throw error;
|
|
2737
|
-
}
|
|
2738
|
-
});
|
|
2739
|
-
});
|
|
2740
|
-
}
|
|
2741
|
-
|
|
2742
2830
|
// src/internal/sdk.ts
|
|
2743
2831
|
init_version();
|
|
2744
2832
|
var _Brizz = class __Brizz {
|
|
@@ -2997,10 +3085,13 @@ export {
|
|
|
2997
3085
|
Session,
|
|
2998
3086
|
SessionTitle,
|
|
2999
3087
|
SeverityNumber2 as SeverityNumber,
|
|
3088
|
+
addExternalLink,
|
|
3089
|
+
callWithMute,
|
|
3000
3090
|
callWithProperties,
|
|
3001
3091
|
callWithSessionId,
|
|
3002
3092
|
detectRuntime,
|
|
3003
3093
|
emitEvent,
|
|
3094
|
+
emitEventWithSessionId,
|
|
3004
3095
|
getActiveSession,
|
|
3005
3096
|
getLogLevel,
|
|
3006
3097
|
getMetricsExporter,
|
|
@@ -3015,6 +3106,7 @@ export {
|
|
|
3015
3106
|
setLogLevel,
|
|
3016
3107
|
startSession,
|
|
3017
3108
|
startSessionTitle,
|
|
3109
|
+
withMute,
|
|
3018
3110
|
withProperties,
|
|
3019
3111
|
withSessionId
|
|
3020
3112
|
};
|