@goodtiming/baton-sdk 0.5.1 → 0.5.2

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/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var SDK_VERSION = "ts-0.5.1";
2
+ var SDK_VERSION = "ts-0.5.2";
3
3
 
4
4
  // src/events.ts
5
5
  import { z } from "zod";
@@ -639,9 +639,6 @@ function roundMetaCoordinates(meta) {
639
639
  return walk(meta, 0);
640
640
  }
641
641
 
642
- // src/identity.ts
643
- import { createHmac } from "crypto";
644
-
645
642
  // src/_text.ts
646
643
  function exceedsCodePoints(value, max) {
647
644
  if (value.length <= max) return false;
@@ -657,7 +654,7 @@ function capCodePoints(value, max) {
657
654
  var PRINCIPAL_SOURCE_ASSERTED = "asserted";
658
655
  var PRINCIPAL_FORM_HASHED = "hashed";
659
656
  var PRINCIPAL_FORM_RAW = "raw";
660
- var RAW_PRINCIPAL_ID_MAX_LEN = 128;
657
+ var PRINCIPAL_ID_MAX_LEN = 128;
661
658
  var DISPLAY_NAME_MAX_LEN = 128;
662
659
  var ALL_WHITE_SPACE = /^\p{White_Space}*$/u;
663
660
  function wireDisplayName(value) {
@@ -665,59 +662,40 @@ function wireDisplayName(value) {
665
662
  if (LONE_SURROGATE.test(value) || value.includes("\0") || ALL_WHITE_SPACE.test(value)) return null;
666
663
  return value;
667
664
  }
668
- var PRINCIPAL_ID_MODE_HASHED = "hashed";
669
- var PRINCIPAL_ID_MODE_RAW = "raw";
670
- var FORM_BY_MODE = {
671
- [PRINCIPAL_ID_MODE_HASHED]: PRINCIPAL_FORM_HASHED,
672
- [PRINCIPAL_ID_MODE_RAW]: PRINCIPAL_FORM_RAW
673
- };
674
- var PRINCIPAL_ID_MODES = Object.keys(
675
- FORM_BY_MODE
676
- );
665
+ var PRINCIPAL_FORMS = /* @__PURE__ */ new Set([PRINCIPAL_FORM_RAW, PRINCIPAL_FORM_HASHED]);
677
666
  var PYTHON_WHITESPACE = "\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
678
667
  var PYTHON_STRIP = new RegExp(`^[${PYTHON_WHITESPACE}]+|[${PYTHON_WHITESPACE}]+$`, "gu");
679
668
  var LONE_SURROGATE = /\p{Surrogate}/u;
680
- function canonicalize(rawPrincipal) {
681
- return pythonStrip(rawPrincipal.normalize("NFC")).toLowerCase();
682
- }
683
669
  function pythonStrip(value) {
684
670
  return value.replace(PYTHON_STRIP, "");
685
671
  }
686
- function hashPrincipalId(rawPrincipal, options) {
687
- const { tenantId, key, issuer = null } = options;
688
- let message = `${tenantId}\0${canonicalize(rawPrincipal)}`;
689
- if (issuer !== null) {
690
- message += `\0${canonicalize(issuer)}`;
691
- }
692
- const keyBytes = typeof key === "string" ? Buffer.from(key, "utf8") : key;
693
- return createHmac("sha256", keyBytes).update(message, "utf8").digest("hex");
694
- }
695
672
  function normalizePrincipal(result) {
696
673
  if (result === null || typeof result !== "object") return null;
697
674
  const candidate = result;
698
675
  if (typeof candidate.principalId !== "string") return null;
699
676
  if (LONE_SURROGATE.test(candidate.principalId)) return null;
700
- if (canonicalize(candidate.principalId) === "") return null;
701
- const issuerRaw = typeof candidate.issuer === "string" && candidate.issuer ? candidate.issuer : null;
702
- const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
703
- return typeof candidate.displayName === "string" ? { principalId: candidate.principalId, issuer, displayName: candidate.displayName } : { principalId: candidate.principalId, issuer };
704
- }
705
- function principalFor(principal, options) {
706
- let id;
707
- if (options.mode === PRINCIPAL_ID_MODE_RAW) {
708
- id = capCodePoints(principal.principalId, RAW_PRINCIPAL_ID_MAX_LEN);
709
- } else {
710
- if (options.key === null || options.key === void 0) return null;
711
- id = hashPrincipalId(principal.principalId, {
712
- tenantId: options.tenantId,
713
- key: options.key,
714
- issuer: principal.issuer ?? null
715
- });
677
+ if (candidate.principalId.includes("\0")) return null;
678
+ if (pythonStrip(candidate.principalId) === "") return null;
679
+ const principal = { principalId: candidate.principalId };
680
+ if (candidate.form !== void 0) principal.form = candidate.form;
681
+ if (typeof candidate.displayName === "string") principal.displayName = candidate.displayName;
682
+ return principal;
683
+ }
684
+ function principalFor(principal, onUnregisteredForm) {
685
+ let form = PRINCIPAL_FORM_RAW;
686
+ if (principal.form !== void 0) {
687
+ if (PRINCIPAL_FORMS.has(principal.form)) {
688
+ form = principal.form;
689
+ } else {
690
+ onUnregisteredForm?.(principal.form);
691
+ }
716
692
  }
717
693
  const wire = {
718
- id,
694
+ // `capCodePoints`, not `.slice()`: a plain slice counts UTF-16 units, so
695
+ // an astral id cut at the boundary would ship a lone surrogate.
696
+ id: capCodePoints(principal.principalId, PRINCIPAL_ID_MAX_LEN),
719
697
  source: PRINCIPAL_SOURCE_ASSERTED,
720
- form: FORM_BY_MODE[options.mode]
698
+ form
721
699
  };
722
700
  const displayName = wireDisplayName(principal.displayName);
723
701
  if (displayName !== null) wire.display_name = displayName;
@@ -827,12 +805,18 @@ function slugServerName(serverName) {
827
805
  const collapsed = mapped.replace(/-+/g, "-").replace(/^-+|-+$/g, "");
828
806
  return collapsed.slice(0, SLUG_CAP).replace(/^-+|-+$/g, "") || void 0;
829
807
  }
808
+ function toolNameCandidate(serverName) {
809
+ const slug = slugServerName(serverName);
810
+ if (slug === void 0) return void 0;
811
+ const candidate = `${slug}${SUFFIX}`;
812
+ return TOOL_NAME_PATTERN.test(candidate) ? candidate : void 0;
813
+ }
830
814
  function displayNameFromServer(serverName, tool) {
831
815
  if (serverName === void 0) return void 0;
832
- const fallbackToolName = tool.annotationToolName || `${tool.vendorId}${SUFFIX}`;
833
- if (!fitsInstructionsCap({ vendorDisplayName: serverName, annotationToolName: fallbackToolName })) {
816
+ const proposed = tool.annotationToolName || toolNameCandidate(serverName) || `${tool.vendorId}${SUFFIX}`;
817
+ if (!fitsInstructionsCap({ vendorDisplayName: serverName, annotationToolName: proposed })) {
834
818
  warn(
835
- `baton: the server name (${serverName.length} chars) does not fit the server-instructions budget as the display name; keeping the DSN's server segment. Pass vendorDisplayName to choose a shorter one.`
819
+ `baton: the server name (${serverName.length} chars) does not fit the server-instructions budget as the display name beside the annotation tool ${JSON.stringify(proposed)}; keeping the DSN's server segment. Pass vendorDisplayName to name the server in fewer characters.`
836
820
  );
837
821
  return void 0;
838
822
  }
@@ -840,10 +824,8 @@ function displayNameFromServer(serverName, tool) {
840
824
  }
841
825
  function annotationToolNameFromServer(serverName, vendorDisplayName) {
842
826
  if (serverName === void 0) return void 0;
843
- const slug = slugServerName(serverName);
844
- if (slug === void 0) return void 0;
845
- const candidate = `${slug}${SUFFIX}`;
846
- if (!TOOL_NAME_PATTERN.test(candidate)) return void 0;
827
+ const candidate = toolNameCandidate(serverName);
828
+ if (candidate === void 0) return void 0;
847
829
  if (!fitsInstructionsCap({ vendorDisplayName, annotationToolName: candidate })) {
848
830
  warn(
849
831
  `baton: the server-derived annotation tool name ${JSON.stringify(candidate)} does not fit the server-instructions budget beside the display name (${vendorDisplayName.length} chars); keeping {vendorId}_annotate. A shorter display name gets the readable one.`
@@ -934,16 +916,6 @@ function buildPrincipalResolutionContext(extra, toolName, args) {
934
916
  authInfo: extraAuthInfo(extra)
935
917
  };
936
918
  }
937
- function warnIfIdentityCannotResolve(config) {
938
- if (config.resolvePrincipal === void 0) return;
939
- if (config.principalIdMode !== PRINCIPAL_ID_MODE_HASHED) return;
940
- if (config.principalIdHmacKey !== void 0) return;
941
- if (typeof process === "undefined" || typeof process.emitWarning !== "function") return;
942
- const renamed = process.env?.BATON_USER_ID_HMAC_KEY ? "BATON_USER_ID_HMAC_KEY is set, but it was renamed to BATON_PRINCIPAL_ID_HMAC_KEY in 0.3.5 and is no longer read. " : "";
943
- process.emitWarning(
944
- `baton: resolvePrincipal is configured but no principal HMAC key is set, so the principal is dropped from every event (events still emit). ${renamed}Set BATON_PRINCIPAL_ID_HMAC_KEY or BatonConfig.principalIdHmacKey, or pass principalIdMode: "raw" if you intend to emit the subject verbatim.`
945
- );
946
- }
947
919
  var warnedPreRenameShape = false;
948
920
  function warnIfPreRenameShape(result) {
949
921
  const userId = result?.userId;
@@ -953,30 +925,28 @@ function warnIfPreRenameShape(result) {
953
925
  "baton: resolvePrincipal returned { userId }, which was renamed to { principalId } in 0.3.5, so the principal is dropped from every event until the hook returns the new key."
954
926
  );
955
927
  }
956
- async function resolveCallPrincipal(hook, call, options) {
928
+ async function resolveCallPrincipal(hook, call) {
957
929
  if (hook === void 0) return null;
958
- let result;
959
930
  try {
960
931
  const context = buildPrincipalResolutionContext(call.extra, call.toolName, call.arguments);
961
- result = await hook(context);
962
- } catch {
963
- return null;
964
- }
965
- const principal = normalizePrincipal(result);
966
- if (principal === null) {
967
- warnIfPreRenameShape(result);
968
- return null;
969
- }
970
- try {
971
- return principalFor(principal, {
972
- mode: options.mode,
973
- tenantId: options.tenantId,
974
- key: options.key
932
+ const result = await hook(context);
933
+ const principal = normalizePrincipal(result);
934
+ if (principal === null) {
935
+ warnIfPreRenameShape(result);
936
+ return null;
937
+ }
938
+ return principalFor(principal, (form) => {
939
+ warn(
940
+ `baton: resolvePrincipal returned form ${describeForm(form)}, expected "raw" or "hashed" \u2014 sending it as "${PRINCIPAL_FORM_RAW}".`
941
+ );
975
942
  });
976
943
  } catch {
977
944
  return null;
978
945
  }
979
946
  }
947
+ function describeForm(form) {
948
+ return typeof form === "string" ? JSON.stringify(form.slice(0, 32)) : `of type ${typeof form}`;
949
+ }
980
950
 
981
951
  // src/integrations/mcp/safeScrub.ts
982
952
  var warned = /* @__PURE__ */ new Set();
@@ -1091,12 +1061,7 @@ function registerAnnotationTool(server, options) {
1091
1061
  }
1092
1062
  const principal = await resolveCallPrincipal(
1093
1063
  options.resolvePrincipal,
1094
- { extra, toolName: name, arguments: args },
1095
- {
1096
- mode: options.principalIdMode,
1097
- tenantId: options.tenantId,
1098
- key: options.principalIdHmacKey
1099
- }
1064
+ { extra, toolName: name, arguments: args }
1100
1065
  );
1101
1066
  await emit(
1102
1067
  options.sink,
@@ -1410,18 +1375,12 @@ function requestSideErrorFields(text, errorBody2) {
1410
1375
  // src/integrations/mcp/config.ts
1411
1376
  var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
1412
1377
  var RESULT_CAPTURE_MODE_SET = new Set(RESULT_CAPTURE_MODES);
1413
- var PRINCIPAL_ID_MODE_SET = new Set(PRINCIPAL_ID_MODES);
1414
1378
  function resolveTenantId(explicit, vendorId) {
1415
1379
  if (explicit) return explicit;
1416
1380
  const fromEnv = typeof process !== "undefined" ? process.env?.BATON_TENANT_ID : void 0;
1417
1381
  if (fromEnv) return fromEnv;
1418
1382
  return vendorId;
1419
1383
  }
1420
- function resolvePrincipalIdHmacKey(explicit) {
1421
- if (explicit !== void 0) return explicit;
1422
- const fromEnv = typeof process !== "undefined" ? process.env?.BATON_PRINCIPAL_ID_HMAC_KEY : void 0;
1423
- return fromEnv ? fromEnv : void 0;
1424
- }
1425
1384
  function resolveBatonConfig(config, serverName) {
1426
1385
  const dsnString = selectDsn(
1427
1386
  config.dsn,
@@ -1480,9 +1439,7 @@ function resolveBatonConfig(config, serverName) {
1480
1439
  };
1481
1440
  }
1482
1441
  var RENAMED_KEYS = {
1483
- resolveUser: "resolvePrincipal",
1484
- userIdMode: "principalIdMode",
1485
- userIdHmacKey: "principalIdHmacKey"
1442
+ resolveUser: "resolvePrincipal"
1486
1443
  };
1487
1444
  function validateBatonConfig(config) {
1488
1445
  for (const [was, now] of Object.entries(RENAMED_KEYS)) {
@@ -1531,14 +1488,6 @@ function validateBatonConfig(config) {
1531
1488
  if (config.resolvePrincipal !== void 0 && typeof config.resolvePrincipal !== "function") {
1532
1489
  throw new Error("BatonConfig.resolvePrincipal must be a function.");
1533
1490
  }
1534
- if (config.principalIdHmacKey !== void 0 && typeof config.principalIdHmacKey !== "string" && !(config.principalIdHmacKey instanceof Uint8Array)) {
1535
- throw new Error("BatonConfig.principalIdHmacKey must be a string or a Uint8Array.");
1536
- }
1537
- if (config.principalIdMode !== void 0 && !PRINCIPAL_ID_MODE_SET.has(config.principalIdMode)) {
1538
- throw new Error(
1539
- `BatonConfig.principalIdMode ${JSON.stringify(config.principalIdMode)} must be one of ${JSON.stringify([...PRINCIPAL_ID_MODE_SET].sort())}.`
1540
- );
1541
- }
1542
1491
  }
1543
1492
 
1544
1493
  // src/optout.ts
@@ -1923,8 +1872,7 @@ async function openCall(ctx, toolName, extra, params, registered = true) {
1923
1872
  const callId = uuidv72();
1924
1873
  const principal = await resolveCallPrincipal(
1925
1874
  ctx.resolvePrincipal,
1926
- { extra, toolName, arguments: params },
1927
- { mode: ctx.principalIdMode, tenantId: ctx.tenantId, key: ctx.principalIdHmacKey }
1875
+ { extra, toolName, arguments: params }
1928
1876
  );
1929
1877
  const common = {
1930
1878
  tenant_id: ctx.tenantId,
@@ -2687,15 +2635,12 @@ function withBaton(server, supplied = {}) {
2687
2635
  resultCaptureMode: config.resultCaptureMode ?? "full",
2688
2636
  paramRegistry: /* @__PURE__ */ new Map(),
2689
2637
  resolvePrincipal: config.resolvePrincipal,
2690
- principalIdMode: config.principalIdMode ?? "hashed",
2691
- principalIdHmacKey: resolvePrincipalIdHmacKey(config.principalIdHmacKey),
2692
2638
  vendorToolJsonSchema,
2693
2639
  bustSchemaMemo,
2694
2640
  tracker,
2695
2641
  surfaceState,
2696
2642
  emitSurface
2697
2643
  };
2698
- warnIfIdentityCannotResolve(ctx);
2699
2644
  const instructions = buildServerInstructions({
2700
2645
  vendorDisplayName: config.vendorDisplayName,
2701
2646
  annotationToolName
@@ -2725,15 +2670,7 @@ function withBaton(server, supplied = {}) {
2725
2670
  vendorId: ctx.vendorId,
2726
2671
  vendorDisplayName: config.vendorDisplayName,
2727
2672
  consentToken: ctx.consentToken,
2728
- // Read off `ctx`, never re-resolved from `config`: the two paths must
2729
- // agree on the actor, and `principalIdMode`/`principalIdHmacKey` each have a
2730
- // default and an env fallback that a second resolution could take
2731
- // differently. Required (not optional) on the options type ON PURPOSE —
2732
- // the compiler then refuses a registration that forgets them, which is
2733
- // the mechanical version of "wiring two of four call sites".
2734
2673
  resolvePrincipal: ctx.resolvePrincipal,
2735
- principalIdMode: ctx.principalIdMode,
2736
- principalIdHmacKey: ctx.principalIdHmacKey,
2737
2674
  fallbackSessionId: ctx.fallbackSessionId,
2738
2675
  scrubber: ctx.scrubber,
2739
2676
  // The RESOLVED name, never `config.annotationToolName`: handing over the
@@ -2779,8 +2716,7 @@ function principalFromOAuthSub(context) {
2779
2716
  const claims = claimsOf(context);
2780
2717
  if (claims === null) return null;
2781
2718
  const sub = stringClaim(claims, "sub");
2782
- const iss = claims.iss;
2783
- return sub === null ? null : { principalId: sub, issuer: typeof iss === "string" && iss !== "" ? iss : null };
2719
+ return sub === null ? null : { principalId: sub };
2784
2720
  }
2785
2721
  function principalFromOAuthEmail(context) {
2786
2722
  const claims = claimsOf(context);
@@ -2841,7 +2777,6 @@ export {
2841
2777
  ToolCallErrorPayloadSchema,
2842
2778
  ToolCallStartEventSchema,
2843
2779
  ToolCallStartPayloadSchema,
2844
- hashPrincipalId,
2845
2780
  identityScrub,
2846
2781
  principalFromOAuthEmail,
2847
2782
  principalFromOAuthSub,