@brizz/sdk 0.1.30 → 0.1.33

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
@@ -18,6 +18,7 @@ libraries including OpenAI, Anthropic, Vercel AI SDK, and more.
18
18
  - [Supported Libraries](#supported-libraries)
19
19
  - [PII Masking](#pii-masking)
20
20
  - [Session Tracking](#session-tracking)
21
+ - [Mute Messages](#mute-messages)
21
22
  - [Custom Events & Logging](#custom-events--logging)
22
23
  - [Environment Variables](#environment-variables)
23
24
  - [Disable Span Export](#disable-span-export)
@@ -199,6 +200,7 @@ pulled in when you need them. Install the matching package(s) to enable them:
199
200
 
200
201
  | To trace | Install |
201
202
  | --- | --- |
203
+ | **Vercel AI SDK v7+** (`ai@7`) — required for span capture | `npm install @ai-sdk/otel` |
202
204
  | **Google Vertex AI** (`@google-cloud/vertexai`) | `npm install @traceloop/instrumentation-vertexai` |
203
205
  | **MCP** (Model Context Protocol) | `npm install @arizeai/openinference-instrumentation-mcp @modelcontextprotocol/sdk` |
204
206
 
@@ -209,6 +211,10 @@ so the misconfiguration is visible.
209
211
 
210
212
  ### Vercel AI SDK Integration
211
213
 
214
+ On `ai@7` and newer, also install `@ai-sdk/otel` — v7 moved AI SDK span collection
215
+ into that package, so without it your `generateText` / `streamText` calls produce no
216
+ spans. It isn't needed on `ai@6`.
217
+
212
218
  For Vercel AI SDK instrumentation, you need to enable telemetry in your function calls:
213
219
 
214
220
  ```typescript
@@ -237,6 +243,47 @@ This enables automatic tracing of:
237
243
  - Tool calls and executions
238
244
  - Streaming data
239
245
 
246
+ ### Vercel eve
247
+
248
+ [Vercel `eve`](https://eve.dev) agents emit standard OpenTelemetry GenAI spans, so Brizz ingests
249
+ their traces, message content, and cost as soon as you point eve's exporter at your Brizz endpoint.
250
+ `@brizz/sdk/eve` gives you a one-line `registerOTel` config that reads `BRIZZ_DSN` — it wires the
251
+ exporter and the session grouping that makes each agent run render as a single conversation:
252
+
253
+ ```typescript
254
+ // agent/instrumentation.ts
255
+ import { defineInstrumentation } from 'eve/instrumentation';
256
+ import { registerOTel } from '@vercel/otel';
257
+ import { createEveOtelConfig } from '@brizz/sdk/eve';
258
+
259
+ export default defineInstrumentation({
260
+ setup: ({ agentName }) => registerOTel(createEveOtelConfig({ serviceName: agentName })),
261
+ });
262
+ ```
263
+
264
+ Then set your DSN in the environment — a single pasted credential:
265
+
266
+ ```bash
267
+ BRIZZ_DSN=https://<bearer>@<gateway-host>/<service-name>
268
+ ```
269
+
270
+ `createEveOtelConfig` derives the bearer and endpoint from the DSN and ignores its service segment —
271
+ eve's `agentName` stays the service name. Prefer to compose it yourself? The pieces are exported too:
272
+
273
+ ```typescript
274
+ import { createEveTraceExporter, createEveSessionProcessor } from '@brizz/sdk/eve';
275
+
276
+ registerOTel({
277
+ serviceName: agentName,
278
+ traceExporter: createEveTraceExporter(),
279
+ // 'auto' keeps the default exporter processor.
280
+ spanProcessors: [createEveSessionProcessor(), 'auto'],
281
+ });
282
+ ```
283
+
284
+ `@brizz/sdk/eve` is a standalone, side-effect-free export: it does not initialize the full SDK, so
285
+ it won't duplicate the spans eve already emits.
286
+
240
287
  ## PII Masking
241
288
 
242
289
  Optional masking for span attributes.
@@ -450,6 +497,51 @@ import { callWithSessionId } from '@brizz/sdk';
450
497
  await callWithSessionId('session-123', processUserWorkflow, null, 'user-456');
451
498
  ```
452
499
 
500
+ ### Identifying Users, Organizations & Messages
501
+
502
+ Attach the end-user, their organization, and a per-message id with typed setters. Call them inside a session — they apply to the turn's spans:
503
+
504
+ ```typescript
505
+ import { startSession, setUser, setOrganization, setMessageId } from '@brizz/sdk';
506
+
507
+ await startSession(sessionId, async () => {
508
+ setUser({ id: user.id, email: user.email, role: user.role, plan: user.plan });
509
+ setOrganization({ id: org.id, name: org.name, plan: org.plan, domain: org.domain });
510
+ setMessageId(message.id); // your own id, to reference this message later
511
+
512
+ return agent.run(prompt);
513
+ });
514
+ ```
515
+
516
+ Only `id` is required. Each field maps to its own attribute (`brizz.user.id`, `brizz.organization.plan`, `brizz.message.id`, …). For anything beyond the named fields, pass a `traits` record — each entry becomes `brizz.user.<key>` / `brizz.organization.<key>`:
517
+
518
+ ```typescript
519
+ setUser({ id: user.id, traits: { department: 'sales', signup_source: 'referral' } });
520
+ setOrganization({ id: org.id, traits: { industry: 'fintech' } });
521
+ ```
522
+
523
+ The same setters are available on the session object (`session.setUser(...)`) and as scoped wrappers (`callWithUser` / `withUser`).
524
+
525
+ ### Recording Feedback
526
+
527
+ Capture an end-user's reaction to a specific reply — a 👍/👎, a rating, a reason. Pair it with the message id you set on the turn:
528
+
529
+ ```typescript
530
+ import { startSession, setMessageId, recordFeedback } from '@brizz/sdk';
531
+
532
+ await startSession(sessionId, async () => {
533
+ setMessageId(message.id); // the id you'll reference this reply by
534
+ const reply = await agent.run(prompt);
535
+ recordFeedback('thumbs_up'); // defaults to the current message
536
+ });
537
+ ```
538
+
539
+ Only `type` is required; `score`, `reason`, `comment`, and `source` are optional, and `traits` adds free-form `brizz.feedback.<key>` entries. Feedback is anchored by `messageId` and/or `sessionId`, so you can send it later — even minutes or days after the reply — by passing the id(s) explicitly:
540
+
541
+ ```typescript
542
+ recordFeedback('thumbs_down', { messageId: message.id, sessionId, reason: 'inaccurate' });
543
+ ```
544
+
453
545
  ### Custom Properties
454
546
 
455
547
  Add custom properties to telemetry context:
@@ -507,6 +599,23 @@ const sessionedProcess = withSessionId(
507
599
  **Note:** The arrow function approach (Option 1) is recommended as it's more explicit, avoids lint
508
600
  warnings, and is less prone to `this` binding issues.
509
601
 
602
+ ## Mute Messages
603
+
604
+ Keep internal or unrelated LLM calls — summarization, title generation, classification, guardrail checks — out of the captured conversation. The call still runs and its telemetry (latency, tokens, cost) is recorded; only the user and/or assistant content is left out.
605
+
606
+ ```typescript
607
+ import { callWithMute, withMute } from '@brizz/sdk';
608
+
609
+ // Hide both sides of an internal call
610
+ await callWithMute({}, () => agent.run('Summarize this conversation for internal logging.'));
611
+
612
+ // Keep the assistant reply, drop the prompt
613
+ await callWithMute({ output: false }, () => agent.run('…a long internal prompt…'));
614
+
615
+ // Pre-wrap a function to reuse the same muting
616
+ const muteTitle = withMute({}, agent.run, agent);
617
+ ```
618
+
510
619
  ## Deployment Environment
511
620
 
512
621
  Optionally specify the deployment environment for better filtering and organization:
@@ -0,0 +1,8 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/internal/version.ts
2
+ function getSDKVersion() {
3
+ return "0.1.33";
4
+ }
5
+
6
+
7
+
8
+ exports.getSDKVersion = getSDKVersion;
@@ -0,0 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
8
+ export {
9
+ __require
10
+ };
@@ -0,0 +1,36 @@
1
+ // src/internal/dsn.ts
2
+ var PLACEHOLDER_SERVICE = "<service-name>";
3
+ var SERVICE_NAME_HEADER = "X-Brizz-Service-Name";
4
+ function parseDSN(dsn, options = {}) {
5
+ let parsed;
6
+ try {
7
+ parsed = new globalThis.URL(dsn);
8
+ } catch {
9
+ return null;
10
+ }
11
+ if (parsed.protocol !== "http:" && parsed.protocol !== "https:" || !parsed.username || !parsed.host) {
12
+ return null;
13
+ }
14
+ const scheme = parsed.protocol === "https:" ? "https" : "http";
15
+ let service;
16
+ try {
17
+ service = decodeURIComponent(parsed.pathname.replace(/^\//, ""));
18
+ } catch {
19
+ return null;
20
+ }
21
+ if (!options.allowPlaceholderService && (service === "" || service === PLACEHOLDER_SERVICE)) {
22
+ return null;
23
+ }
24
+ return {
25
+ scheme,
26
+ host: parsed.host,
27
+ bearer: decodeURIComponent(parsed.username),
28
+ service,
29
+ baseUrl: `${scheme}://${parsed.host}`
30
+ };
31
+ }
32
+
33
+ export {
34
+ SERVICE_NAME_HEADER,
35
+ parseDSN
36
+ };
@@ -1,10 +1,3 @@
1
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
- }) : x)(function(x) {
4
- if (typeof require !== "undefined") return require.apply(this, arguments);
5
- throw Error('Dynamic require of "' + x + '" is not supported');
6
- });
7
-
8
1
  // src/internal/semantic-conventions.ts
9
2
  import { createContextKey } from "@opentelemetry/api";
10
3
  var BRIZZ = "brizz";
@@ -19,6 +12,16 @@ var SESSION_OUTPUT_CONTEXT = "brizz.session.output.context";
19
12
  var SESSION_SPAN_NAME = "brizz.start_session";
20
13
  var MUTE_INPUT = "mute.input";
21
14
  var MUTE_OUTPUT = "mute.output";
15
+ var USER_ID = "user.id";
16
+ var USER_EMAIL = "user.email";
17
+ var USER_NAME = "user.name";
18
+ var USER_ROLE = "user.role";
19
+ var USER_PLAN = "user.plan";
20
+ var ORGANIZATION_ID = "organization.id";
21
+ var ORGANIZATION_NAME = "organization.name";
22
+ var ORGANIZATION_PLAN = "organization.plan";
23
+ var ORGANIZATION_DOMAIN = "organization.domain";
24
+ var MESSAGE_ID = "message.id";
22
25
  var SESSION_TITLE_SPAN_NAME = "brizz.session_title";
23
26
  var SESSION_TITLE_GENERATION = "session.title_generation";
24
27
  var SESSION_TITLE = "brizz.session.title";
@@ -28,9 +31,15 @@ var EXTERNAL_LINK_EVENT_NAME = "brizz.internal.external_link";
28
31
  var EXTERNAL_LINK_URL = "brizz.internal.external_link.url";
29
32
  var EXTERNAL_LINK_TITLE = "brizz.internal.external_link.title";
30
33
  var EXTERNAL_LINK_TYPE = "brizz.internal.external_link.type";
34
+ var FEEDBACK_EVENT_NAME = "brizz.feedback";
35
+ var FEEDBACK_TYPE = "brizz.feedback.type";
36
+ var FEEDBACK_SCORE = "brizz.feedback.score";
37
+ var FEEDBACK_REASON = "brizz.feedback.reason";
38
+ var FEEDBACK_COMMENT = "brizz.feedback.comment";
39
+ var FEEDBACK_SOURCE = "brizz.feedback.source";
40
+ var FEEDBACK_ATTR_PREFIX = "brizz.feedback.";
31
41
 
32
42
  export {
33
- __require,
34
43
  BRIZZ,
35
44
  SESSION_ID,
36
45
  PROPERTIES_CONTEXT_KEY,
@@ -42,6 +51,16 @@ export {
42
51
  SESSION_SPAN_NAME,
43
52
  MUTE_INPUT,
44
53
  MUTE_OUTPUT,
54
+ USER_ID,
55
+ USER_EMAIL,
56
+ USER_NAME,
57
+ USER_ROLE,
58
+ USER_PLAN,
59
+ ORGANIZATION_ID,
60
+ ORGANIZATION_NAME,
61
+ ORGANIZATION_PLAN,
62
+ ORGANIZATION_DOMAIN,
63
+ MESSAGE_ID,
45
64
  SESSION_TITLE_SPAN_NAME,
46
65
  SESSION_TITLE_GENERATION,
47
66
  SESSION_TITLE,
@@ -50,5 +69,12 @@ export {
50
69
  EXTERNAL_LINK_EVENT_NAME,
51
70
  EXTERNAL_LINK_URL,
52
71
  EXTERNAL_LINK_TITLE,
53
- EXTERNAL_LINK_TYPE
72
+ EXTERNAL_LINK_TYPE,
73
+ FEEDBACK_EVENT_NAME,
74
+ FEEDBACK_TYPE,
75
+ FEEDBACK_SCORE,
76
+ FEEDBACK_REASON,
77
+ FEEDBACK_COMMENT,
78
+ FEEDBACK_SOURCE,
79
+ FEEDBACK_ATTR_PREFIX
54
80
  };
@@ -1,22 +1,40 @@
1
+ import {
2
+ SERVICE_NAME_HEADER,
3
+ parseDSN
4
+ } from "./chunk-7SUF4VVI.js";
5
+ import {
6
+ getSDKVersion
7
+ } from "./chunk-PY6FJBVY.js";
1
8
  import {
2
9
  DEFAULT_LOG_LEVEL,
3
- getSDKVersion,
4
10
  logger,
5
11
  parseLogLevel,
6
12
  setLogLevel
7
- } from "./chunk-3OLW4TOG.js";
13
+ } from "./chunk-Y4HNUX3Q.js";
8
14
  import {
9
15
  InterruptPropagator
10
- } from "./chunk-MWX3GIJW.js";
16
+ } from "./chunk-N4X2B6BC.js";
11
17
  import {
12
18
  BRIZZ,
13
19
  EXTERNAL_LINK_EVENT_NAME,
14
20
  EXTERNAL_LINK_TITLE,
15
21
  EXTERNAL_LINK_TYPE,
16
22
  EXTERNAL_LINK_URL,
23
+ FEEDBACK_ATTR_PREFIX,
24
+ FEEDBACK_COMMENT,
25
+ FEEDBACK_EVENT_NAME,
26
+ FEEDBACK_REASON,
27
+ FEEDBACK_SCORE,
28
+ FEEDBACK_SOURCE,
29
+ FEEDBACK_TYPE,
17
30
  INTERNAL_EVENT_PREFIX,
31
+ MESSAGE_ID,
18
32
  MUTE_INPUT,
19
33
  MUTE_OUTPUT,
34
+ ORGANIZATION_DOMAIN,
35
+ ORGANIZATION_ID,
36
+ ORGANIZATION_NAME,
37
+ ORGANIZATION_PLAN,
20
38
  PROPERTIES_CONTEXT_KEY,
21
39
  SESSION_ID,
22
40
  SESSION_INPUT,
@@ -28,8 +46,15 @@ import {
28
46
  SESSION_TITLE,
29
47
  SESSION_TITLE_GENERATION,
30
48
  SESSION_TITLE_SPAN_NAME,
49
+ USER_EMAIL,
50
+ USER_ID,
51
+ USER_NAME,
52
+ USER_PLAN,
53
+ USER_ROLE
54
+ } from "./chunk-GRRKMMG6.js";
55
+ import {
31
56
  __require
32
- } from "./chunk-RORIBZEV.js";
57
+ } from "./chunk-3RG5ZIWI.js";
33
58
 
34
59
  // src/internal/instrumentation/registry.ts
35
60
  import { AnthropicInstrumentation } from "@traceloop/instrumentation-anthropic";
@@ -136,7 +161,7 @@ var InstrumentationRegistry = class _InstrumentationRegistry {
136
161
  const mcpModules = this.manualModules.mcp;
137
162
  void (async () => {
138
163
  try {
139
- const { MCPInstrumentation } = await import("./mcp-56TY4LZ5.js");
164
+ const { MCPInstrumentation } = await import("./mcp-XIHWT2AU.js");
140
165
  const instrumentation = new MCPInstrumentation({ exceptionLogger });
141
166
  instrumentation.manuallyInstrument(mcpModules);
142
167
  logger.debug("Manual instrumentation enabled for MCP");
@@ -619,7 +644,7 @@ var DEFAULT_LOG_MASKING_RULES = [
619
644
  var BrizzSimpleLogRecordProcessor = class extends SimpleLogRecordProcessor {
620
645
  config;
621
646
  constructor(exporter, config) {
622
- super(exporter);
647
+ super({ exporter });
623
648
  this.config = config;
624
649
  }
625
650
  onEmit(logRecord) {
@@ -639,7 +664,7 @@ var BrizzSimpleLogRecordProcessor = class extends SimpleLogRecordProcessor {
639
664
  var BrizzBatchLogRecordProcessor = class extends BatchLogRecordProcessor {
640
665
  config;
641
666
  constructor(exporter, config) {
642
- super(exporter);
667
+ super({ exporter });
643
668
  this.config = config;
644
669
  }
645
670
  onEmit(logRecord) {
@@ -906,6 +931,136 @@ function withProperties(properties, fn, thisArg) {
906
931
  );
907
932
  };
908
933
  }
934
+ function propagatePropertiesImperatively(properties) {
935
+ try {
936
+ const manager = context2;
937
+ const mgr = manager._getContextManager?.();
938
+ const store = mgr?._asyncLocalStorage;
939
+ if (!store || typeof store.enterWith !== "function") {
940
+ return;
941
+ }
942
+ const base = context2.active();
943
+ const prev = base.getValue(PROPERTIES_CONTEXT_KEY);
944
+ const merged = prev ? { ...prev, ...properties } : properties;
945
+ store.enterWith(base.setValue(PROPERTIES_CONTEXT_KEY, merged));
946
+ } catch {
947
+ }
948
+ }
949
+ function setUser(user) {
950
+ const properties = { [USER_ID]: user.id };
951
+ if (user.email !== void 0) {
952
+ properties[USER_EMAIL] = user.email;
953
+ }
954
+ if (user.name !== void 0) {
955
+ properties[USER_NAME] = user.name;
956
+ }
957
+ if (user.role !== void 0) {
958
+ properties[USER_ROLE] = user.role;
959
+ }
960
+ if (user.plan !== void 0) {
961
+ properties[USER_PLAN] = user.plan;
962
+ }
963
+ if (user.traits) {
964
+ for (const [key, value] of Object.entries(user.traits)) {
965
+ const k = `user.${key}`;
966
+ if (properties[k] === void 0) {
967
+ properties[k] = value;
968
+ }
969
+ }
970
+ }
971
+ setCurrentSpanCustomProperties(properties);
972
+ propagatePropertiesImperatively(properties);
973
+ }
974
+ function setOrganization(org) {
975
+ const properties = { [ORGANIZATION_ID]: org.id };
976
+ if (org.name !== void 0) {
977
+ properties[ORGANIZATION_NAME] = org.name;
978
+ }
979
+ if (org.plan !== void 0) {
980
+ properties[ORGANIZATION_PLAN] = org.plan;
981
+ }
982
+ if (org.domain !== void 0) {
983
+ properties[ORGANIZATION_DOMAIN] = org.domain;
984
+ }
985
+ if (org.traits) {
986
+ for (const [key, value] of Object.entries(org.traits)) {
987
+ const k = `organization.${key}`;
988
+ if (properties[k] === void 0) {
989
+ properties[k] = value;
990
+ }
991
+ }
992
+ }
993
+ setCurrentSpanCustomProperties(properties);
994
+ propagatePropertiesImperatively(properties);
995
+ }
996
+ function setMessageId(id) {
997
+ const properties = { [MESSAGE_ID]: id };
998
+ setCurrentSpanCustomProperties(properties);
999
+ propagatePropertiesImperatively(properties);
1000
+ }
1001
+ function userProperties(user) {
1002
+ const properties = { [USER_ID]: user.id };
1003
+ if (user.email !== void 0) {
1004
+ properties[USER_EMAIL] = user.email;
1005
+ }
1006
+ if (user.name !== void 0) {
1007
+ properties[USER_NAME] = user.name;
1008
+ }
1009
+ if (user.role !== void 0) {
1010
+ properties[USER_ROLE] = user.role;
1011
+ }
1012
+ if (user.plan !== void 0) {
1013
+ properties[USER_PLAN] = user.plan;
1014
+ }
1015
+ if (user.traits) {
1016
+ for (const [key, value] of Object.entries(user.traits)) {
1017
+ const k = `user.${key}`;
1018
+ if (properties[k] === void 0) {
1019
+ properties[k] = value;
1020
+ }
1021
+ }
1022
+ }
1023
+ return properties;
1024
+ }
1025
+ function organizationProperties(org) {
1026
+ const properties = { [ORGANIZATION_ID]: org.id };
1027
+ if (org.name !== void 0) {
1028
+ properties[ORGANIZATION_NAME] = org.name;
1029
+ }
1030
+ if (org.plan !== void 0) {
1031
+ properties[ORGANIZATION_PLAN] = org.plan;
1032
+ }
1033
+ if (org.domain !== void 0) {
1034
+ properties[ORGANIZATION_DOMAIN] = org.domain;
1035
+ }
1036
+ if (org.traits) {
1037
+ for (const [key, value] of Object.entries(org.traits)) {
1038
+ const k = `organization.${key}`;
1039
+ if (properties[k] === void 0) {
1040
+ properties[k] = value;
1041
+ }
1042
+ }
1043
+ }
1044
+ return properties;
1045
+ }
1046
+ function callWithUser(user, fn, thisArg, ...args) {
1047
+ return callWithProperties(userProperties(user), fn, thisArg, ...args);
1048
+ }
1049
+ function withUser(user, fn, thisArg) {
1050
+ return withProperties(userProperties(user), fn, thisArg);
1051
+ }
1052
+ function callWithOrganization(org, fn, thisArg, ...args) {
1053
+ return callWithProperties(organizationProperties(org), fn, thisArg, ...args);
1054
+ }
1055
+ function withOrganization(org, fn, thisArg) {
1056
+ return withProperties(organizationProperties(org), fn, thisArg);
1057
+ }
1058
+ function callWithMessageId(id, fn, thisArg, ...args) {
1059
+ return callWithProperties({ [MESSAGE_ID]: id }, fn, thisArg, ...args);
1060
+ }
1061
+ function withMessageId(id, fn, thisArg) {
1062
+ return withProperties({ [MESSAGE_ID]: id }, fn, thisArg);
1063
+ }
909
1064
  function muteProperties(options) {
910
1065
  const { input = true, output = true } = options;
911
1066
  const properties = {};
@@ -994,6 +1149,55 @@ var Session = class {
994
1149
  this.span.setAttribute(`${BRIZZ}.${key}`, value);
995
1150
  }
996
1151
  }
1152
+ /**
1153
+ * Set end-user identity on the session span: `brizz.user.id` and, when
1154
+ * provided, `brizz.user.email` / `brizz.user.name`. Undefined fields are omitted.
1155
+ *
1156
+ * @param user - User identity (`id` required; `email` / `name` optional)
1157
+ */
1158
+ setUser(user) {
1159
+ if (user.traits) {
1160
+ for (const [key, value] of Object.entries(user.traits)) {
1161
+ this.span.setAttribute(`${BRIZZ}.user.${key}`, value);
1162
+ }
1163
+ }
1164
+ this.span.setAttribute(`${BRIZZ}.${USER_ID}`, user.id);
1165
+ if (user.email !== void 0) {
1166
+ this.span.setAttribute(`${BRIZZ}.${USER_EMAIL}`, user.email);
1167
+ }
1168
+ if (user.name !== void 0) {
1169
+ this.span.setAttribute(`${BRIZZ}.${USER_NAME}`, user.name);
1170
+ }
1171
+ if (user.role !== void 0) {
1172
+ this.span.setAttribute(`${BRIZZ}.${USER_ROLE}`, user.role);
1173
+ }
1174
+ if (user.plan !== void 0) {
1175
+ this.span.setAttribute(`${BRIZZ}.${USER_PLAN}`, user.plan);
1176
+ }
1177
+ }
1178
+ /**
1179
+ * Set organization identity on the session span: `brizz.organization.id` and,
1180
+ * when provided, `brizz.organization.name`. Undefined fields are omitted.
1181
+ *
1182
+ * @param org - Organization identity (`id` required; `name` optional)
1183
+ */
1184
+ setOrganization(org) {
1185
+ if (org.traits) {
1186
+ for (const [key, value] of Object.entries(org.traits)) {
1187
+ this.span.setAttribute(`${BRIZZ}.organization.${key}`, value);
1188
+ }
1189
+ }
1190
+ this.span.setAttribute(`${BRIZZ}.${ORGANIZATION_ID}`, org.id);
1191
+ if (org.name !== void 0) {
1192
+ this.span.setAttribute(`${BRIZZ}.${ORGANIZATION_NAME}`, org.name);
1193
+ }
1194
+ if (org.plan !== void 0) {
1195
+ this.span.setAttribute(`${BRIZZ}.${ORGANIZATION_PLAN}`, org.plan);
1196
+ }
1197
+ if (org.domain !== void 0) {
1198
+ this.span.setAttribute(`${BRIZZ}.${ORGANIZATION_DOMAIN}`, org.domain);
1199
+ }
1200
+ }
997
1201
  /**
998
1202
  * Attach an external link (URL) to this session.
999
1203
  *
@@ -1035,6 +1239,10 @@ function resolveSessionIdFromContext() {
1035
1239
  const props = context2.active().getValue(PROPERTIES_CONTEXT_KEY);
1036
1240
  return props?.[SESSION_ID];
1037
1241
  }
1242
+ function resolveMessageIdFromContext() {
1243
+ const props = context2.active().getValue(PROPERTIES_CONTEXT_KEY);
1244
+ return props?.[MESSAGE_ID];
1245
+ }
1038
1246
  function emitExternalLink(sessionId, url, title, linkType) {
1039
1247
  try {
1040
1248
  emitEventWithSessionId(sessionId, EXTERNAL_LINK_EVENT_NAME, {
@@ -1056,6 +1264,52 @@ function addExternalLink(url, options) {
1056
1264
  }
1057
1265
  emitExternalLink(sessionId, url, options?.title, options?.linkType);
1058
1266
  }
1267
+ function recordFeedback(type, options) {
1268
+ const attributes = { [FEEDBACK_TYPE]: type };
1269
+ if (options?.score !== void 0) {
1270
+ attributes[FEEDBACK_SCORE] = options.score;
1271
+ }
1272
+ if (options?.reason !== void 0) {
1273
+ attributes[FEEDBACK_REASON] = options.reason;
1274
+ }
1275
+ if (options?.comment !== void 0) {
1276
+ attributes[FEEDBACK_COMMENT] = options.comment;
1277
+ }
1278
+ if (options?.source !== void 0) {
1279
+ attributes[FEEDBACK_SOURCE] = options.source;
1280
+ }
1281
+ if (options?.traits) {
1282
+ for (const [key, value] of Object.entries(options.traits)) {
1283
+ const k = `${FEEDBACK_ATTR_PREFIX}${key}`;
1284
+ if (attributes[k] === void 0) {
1285
+ attributes[k] = value;
1286
+ }
1287
+ }
1288
+ }
1289
+ const messageId = options?.messageId ?? resolveMessageIdFromContext();
1290
+ const sessionId = options?.sessionId ?? resolveSessionIdFromContext();
1291
+ if (messageId === void 0 && sessionId === void 0) {
1292
+ logger.warn(
1293
+ "recordFeedback called with no resolvable message id or session id; the feedback cannot be attributed to a message or conversation. Pass options.messageId / options.sessionId or call inside a startSession/callWithMessageId scope."
1294
+ );
1295
+ }
1296
+ const anchor = {};
1297
+ if (messageId !== void 0) {
1298
+ anchor[MESSAGE_ID] = messageId;
1299
+ }
1300
+ if (sessionId !== void 0) {
1301
+ anchor[SESSION_ID] = sessionId;
1302
+ }
1303
+ try {
1304
+ if (Object.keys(anchor).length === 0) {
1305
+ emitEvent(FEEDBACK_EVENT_NAME, attributes);
1306
+ } else {
1307
+ callWithProperties(anchor, () => emitEvent(FEEDBACK_EVENT_NAME, attributes));
1308
+ }
1309
+ } catch (error) {
1310
+ logger.warn("recordFeedback: failed to emit feedback event", error);
1311
+ }
1312
+ }
1059
1313
  function startSession(sessionId, callback, extraProperties, options) {
1060
1314
  const isTitle = options?.mode === "title";
1061
1315
  const spanName = isTitle ? SESSION_TITLE_SPAN_NAME : SESSION_SPAN_NAME;
@@ -1523,38 +1777,6 @@ import {
1523
1777
  import { MeterProvider } from "@opentelemetry/sdk-metrics";
1524
1778
  import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node";
1525
1779
 
1526
- // src/internal/dsn.ts
1527
- var PLACEHOLDER_SERVICE = "<service-name>";
1528
- var SERVICE_NAME_HEADER = "X-Brizz-Service-Name";
1529
- function parseDSN(dsn) {
1530
- let parsed;
1531
- try {
1532
- parsed = new globalThis.URL(dsn);
1533
- } catch {
1534
- return null;
1535
- }
1536
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:" || !parsed.username || !parsed.host) {
1537
- return null;
1538
- }
1539
- const scheme = parsed.protocol === "https:" ? "https" : "http";
1540
- let service;
1541
- try {
1542
- service = decodeURIComponent(parsed.pathname.replace(/^\//, ""));
1543
- } catch {
1544
- return null;
1545
- }
1546
- if (service === "" || service === PLACEHOLDER_SERVICE) {
1547
- return null;
1548
- }
1549
- return {
1550
- scheme,
1551
- host: parsed.host,
1552
- bearer: decodeURIComponent(parsed.username),
1553
- service,
1554
- baseUrl: `${scheme}://${parsed.host}`
1555
- };
1556
- }
1557
-
1558
1780
  // src/internal/config.ts
1559
1781
  function resolveConfig(options) {
1560
1782
  const envLogLevel = process.env["BRIZZ_LOG_LEVEL"] || options.logLevel?.toString() || DEFAULT_LOG_LEVEL.toString();
@@ -1946,6 +2168,15 @@ export {
1946
2168
  setCurrentSpanCustomProperties,
1947
2169
  callWithProperties,
1948
2170
  withProperties,
2171
+ setUser,
2172
+ setOrganization,
2173
+ setMessageId,
2174
+ callWithUser,
2175
+ withUser,
2176
+ callWithOrganization,
2177
+ withOrganization,
2178
+ callWithMessageId,
2179
+ withMessageId,
1949
2180
  callWithMute,
1950
2181
  withMute,
1951
2182
  withSessionId,
@@ -1954,6 +2185,7 @@ export {
1954
2185
  SessionTitle,
1955
2186
  getActiveSession,
1956
2187
  addExternalLink,
2188
+ recordFeedback,
1957
2189
  startSession,
1958
2190
  startSessionTitle,
1959
2191
  DEFAULT_PII_PATTERNS,