briyah 2.0.17 → 2.0.18

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.
@@ -36,6 +36,8 @@ export interface AgentConfig {
36
36
  usageTags?: {
37
37
  user?: string;
38
38
  feature?: string;
39
+ activity?: string;
40
+ detail?: string;
39
41
  };
40
42
  toolNames?: string[];
41
43
  useModerator?: boolean;
@@ -134,7 +134,7 @@ class BalanceService {
134
134
  ...(source?.agentName ? { agentName: source.agentName } : {}),
135
135
  ...(source?.serviceName ? { serviceName: source.serviceName } : {}),
136
136
  ...(source?.modelName ? { modelName: source.modelName } : {}),
137
- ...(tags.user || tags.feature ? { tags } : {}),
137
+ ...(Object.keys(tags).length ? { tags } : {}),
138
138
  });
139
139
  }
140
140
  /**
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Who and what an AI charge is for, recorded with it in the user's usage ledger
3
- * (see BalanceService). Both are the host's own labels: Briyah stores and
3
+ * (see BalanceService). All are the host's own labels: Briyah stores and
4
4
  * groups by them but never interprets them.
5
5
  */
6
6
  export interface UsageTags {
@@ -8,6 +8,15 @@ export interface UsageTags {
8
8
  user?: string;
9
9
  /** What the work was, e.g. "Loss run import" or "AI Assistant". */
10
10
  feature?: string;
11
+ /**
12
+ * One piece of work the charges belong to, e.g. a request id. A single user
13
+ * action — an import, a question — makes many AI calls, each its own charge;
14
+ * charges sharing an activity are that one action, so a host can show "what was
15
+ * done" as one line with its total rather than as every call behind it.
16
+ */
17
+ activity?: string;
18
+ /** What that piece of work was, in the host's words, e.g. "Imported mvrs.pdf". */
19
+ detail?: string;
11
20
  }
12
21
  /**
13
22
  * Runs `fn` with these usage tags in force: every AI charge made while it runs,
@@ -18,6 +27,15 @@ export interface UsageTags {
18
27
  * Tags set on an agent (`agent.usageTags`) win over these.
19
28
  */
20
29
  export declare function runWithUsageTags<T>(tags: UsageTags, fn: () => T): T;
30
+ /**
31
+ * Adds or replaces tags on the context already in force, for every charge made in
32
+ * it from now on — including by work it has already started, which shares that
33
+ * context. For a tag only known partway through: a host that tags each request as it
34
+ * arrives learns what the request was about (the file it uploaded, say) only once
35
+ * the request has been read. Outside any `runWithUsageTags` it does nothing, and a
36
+ * context nested inside this one keeps the tags it copied when it began.
37
+ */
38
+ export declare function updateUsageTags(tags: UsageTags): void;
21
39
  /** The usage tags in force here, or an empty object. */
22
40
  export declare function currentUsageTags(): UsageTags;
23
41
  /** The tags a charge is recorded with: the context's, then the agent's own on top. */
@@ -1,9 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.runWithUsageTags = runWithUsageTags;
4
+ exports.updateUsageTags = updateUsageTags;
4
5
  exports.currentUsageTags = currentUsageTags;
5
6
  exports.resolveUsageTags = resolveUsageTags;
6
7
  const async_hooks_1 = require("async_hooks");
8
+ /** The tag keys a charge records. */
9
+ const TAG_KEYS = ['user', 'feature', 'activity', 'detail'];
7
10
  const storage = new async_hooks_1.AsyncLocalStorage();
8
11
  /**
9
12
  * Runs `fn` with these usage tags in force: every AI charge made while it runs,
@@ -17,6 +20,19 @@ function runWithUsageTags(tags, fn) {
17
20
  const outer = storage.getStore();
18
21
  return storage.run({ ...outer, ...clean(tags) }, fn);
19
22
  }
23
+ /**
24
+ * Adds or replaces tags on the context already in force, for every charge made in
25
+ * it from now on — including by work it has already started, which shares that
26
+ * context. For a tag only known partway through: a host that tags each request as it
27
+ * arrives learns what the request was about (the file it uploaded, say) only once
28
+ * the request has been read. Outside any `runWithUsageTags` it does nothing, and a
29
+ * context nested inside this one keeps the tags it copied when it began.
30
+ */
31
+ function updateUsageTags(tags) {
32
+ const store = storage.getStore();
33
+ if (store)
34
+ Object.assign(store, clean(tags));
35
+ }
20
36
  /** The usage tags in force here, or an empty object. */
21
37
  function currentUsageTags() {
22
38
  return { ...storage.getStore() };
@@ -27,9 +43,10 @@ function resolveUsageTags(agentTags) {
27
43
  }
28
44
  function clean(tags) {
29
45
  const out = {};
30
- if (typeof tags.user === 'string' && tags.user.trim())
31
- out.user = tags.user.trim();
32
- if (typeof tags.feature === 'string' && tags.feature.trim())
33
- out.feature = tags.feature.trim();
46
+ for (const key of TAG_KEYS) {
47
+ const value = tags[key];
48
+ if (typeof value === 'string' && value.trim())
49
+ out[key] = value.trim();
50
+ }
34
51
  return out;
35
52
  }
@@ -5,7 +5,7 @@ export { detectDocumentType, convertLocalDocument, UnsupportedDocumentError, } f
5
5
  export type { DocumentFileType, DocumentTypeDetection, DocumentPage as ConvertedDocumentPage, LocalDocumentConversion, LocalConversionOptions, } from '../documents/document-conversion';
6
6
  export { detectAttachmentMimeType, isAttachmentImage, ATTACHMENT_IMAGE_TYPES, MAX_ATTACHED_IMAGE_BYTES, } from '../ai/LLM/attachment-type';
7
7
  export type { AttachmentImageType } from '../ai/LLM/attachment-type';
8
- export { runWithUsageTags, currentUsageTags } from '../app/usage-context';
8
+ export { runWithUsageTags, currentUsageTags, updateUsageTags } from '../app/usage-context';
9
9
  export type { UsageTags } from '../app/usage-context';
10
10
  export { InsufficientBalanceError, setInsufficientBalanceMessage, insufficientBalanceMessage, } from '../common/errors';
11
11
  export type { AllowanceState, CreditSummary, UsageEntry, UsageReport, UsageTotal, UsageSource, } from '../app/balance.service';
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatChartValue = exports.renderChartSvg = exports.validateChart = exports.CHART_TYPES = exports.CHART_BLOCK_LANGUAGE = exports.queryRows = exports.buildDataGuideMarkdown = exports.listDatasets = exports.listDataSources = exports.unregisterDataSource = exports.registerDataSource = exports.MAX_ROW_LIMIT = exports.DEFAULT_ROW_LIMIT = exports.NativeToolRegistry = exports.availableToolPacks = exports.registerToolPacks = exports.installToolPacks = exports.dnd5eTools = exports.buildMarkdown = exports.pageMarker = exports.stripWrappingFence = exports.splitMarkdownIntoPages = exports.libraryDocumentIdOf = exports.isLibraryAttachment = exports.libraryAttachmentName = exports.DEFAULT_EMBEDDING_MODEL = exports.EMBEDDING_DIMENSIONS = exports.embedTexts = exports.DEFAULT_CHUNK_OVERLAP_CHARS = exports.DEFAULT_CHUNK_MAX_CHARS = exports.embeddingInput = exports.chunkPages = exports.buildDocumentGuideMarkdown = exports.listDocumentSources = exports.unregisterDocumentSource = exports.registerDocumentSource = exports.insufficientBalanceMessage = exports.setInsufficientBalanceMessage = exports.InsufficientBalanceError = exports.currentUsageTags = exports.runWithUsageTags = exports.MAX_ATTACHED_IMAGE_BYTES = exports.ATTACHMENT_IMAGE_TYPES = exports.isAttachmentImage = exports.detectAttachmentMimeType = exports.UnsupportedDocumentError = exports.convertLocalDocument = exports.detectDocumentType = exports.BriyahConfigService = exports.Briyah = void 0;
4
- exports.RoomMessage = exports.Room = exports.Agent = exports.PackInstantiationService = exports.PackService = void 0;
3
+ exports.renderChartSvg = exports.validateChart = exports.CHART_TYPES = exports.CHART_BLOCK_LANGUAGE = exports.queryRows = exports.buildDataGuideMarkdown = exports.listDatasets = exports.listDataSources = exports.unregisterDataSource = exports.registerDataSource = exports.MAX_ROW_LIMIT = exports.DEFAULT_ROW_LIMIT = exports.NativeToolRegistry = exports.availableToolPacks = exports.registerToolPacks = exports.installToolPacks = exports.dnd5eTools = exports.buildMarkdown = exports.pageMarker = exports.stripWrappingFence = exports.splitMarkdownIntoPages = exports.libraryDocumentIdOf = exports.isLibraryAttachment = exports.libraryAttachmentName = exports.DEFAULT_EMBEDDING_MODEL = exports.EMBEDDING_DIMENSIONS = exports.embedTexts = exports.DEFAULT_CHUNK_OVERLAP_CHARS = exports.DEFAULT_CHUNK_MAX_CHARS = exports.embeddingInput = exports.chunkPages = exports.buildDocumentGuideMarkdown = exports.listDocumentSources = exports.unregisterDocumentSource = exports.registerDocumentSource = exports.insufficientBalanceMessage = exports.setInsufficientBalanceMessage = exports.InsufficientBalanceError = exports.updateUsageTags = exports.currentUsageTags = exports.runWithUsageTags = exports.MAX_ATTACHED_IMAGE_BYTES = exports.ATTACHMENT_IMAGE_TYPES = exports.isAttachmentImage = exports.detectAttachmentMimeType = exports.UnsupportedDocumentError = exports.convertLocalDocument = exports.detectDocumentType = exports.BriyahConfigService = exports.Briyah = void 0;
4
+ exports.RoomMessage = exports.Room = exports.Agent = exports.PackInstantiationService = exports.PackService = exports.formatChartValue = void 0;
5
5
  // Main SDK exports
6
6
  var briyah_1 = require("./briyah");
7
7
  Object.defineProperty(exports, "Briyah", { enumerable: true, get: function () { return briyah_1.Briyah; } });
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "MAX_ATTACHED_IMAGE_BYTES", { enumerable: true, g
26
26
  var usage_context_1 = require("../app/usage-context");
27
27
  Object.defineProperty(exports, "runWithUsageTags", { enumerable: true, get: function () { return usage_context_1.runWithUsageTags; } });
28
28
  Object.defineProperty(exports, "currentUsageTags", { enumerable: true, get: function () { return usage_context_1.currentUsageTags; } });
29
+ Object.defineProperty(exports, "updateUsageTags", { enumerable: true, get: function () { return usage_context_1.updateUsageTags; } });
29
30
  // What an AI call throws once the credit is gone. Its message is the host's when
30
31
  // one is set, so the host's wording (and where to buy more) is what users read.
31
32
  var errors_1 = require("../common/errors");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "briyah",
3
- "version": "2.0.17",
3
+ "version": "2.0.18",
4
4
  "description": "SDK for AI agents, multi-agent rooms, interactive stories, and role-playing games with per-user data isolation and cost tracking",
5
5
  "private": false,
6
6
  "license": "Apache-2.0",