@goodtiming/baton-sdk 0.3.4 → 0.3.5
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 +1 -1
- package/dist/index.cjs +74 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +37 -37
- package/dist/index.d.ts +37 -37
- package/dist/index.js +73 -50
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/version.ts
|
|
2
|
-
var SDK_VERSION = "ts-0.3.
|
|
2
|
+
var SDK_VERSION = "ts-0.3.5";
|
|
3
3
|
|
|
4
4
|
// src/events.ts
|
|
5
5
|
import { z } from "zod";
|
|
@@ -52,7 +52,7 @@ var envelopeShape = {
|
|
|
52
52
|
consent_token: z.string(),
|
|
53
53
|
sdk_version: z.string().default(SDK_VERSION),
|
|
54
54
|
agent_runtime: z.string().default("unknown"),
|
|
55
|
-
|
|
55
|
+
principal_id: z.string().nullable().default(null),
|
|
56
56
|
call_id: z.string().nullable().default(null),
|
|
57
57
|
runtime_meta: z.record(z.string(), z.unknown()).nullable().default(null)
|
|
58
58
|
};
|
|
@@ -404,9 +404,9 @@ function capCodePoints(value, max) {
|
|
|
404
404
|
|
|
405
405
|
// src/identity.ts
|
|
406
406
|
var VENDOR_HASH_SCHEME = "v1";
|
|
407
|
-
var
|
|
408
|
-
var
|
|
409
|
-
var
|
|
407
|
+
var RAW_PRINCIPAL_ID_MAX_LEN = 128;
|
|
408
|
+
var PRINCIPAL_ID_MODE_HASHED = "hashed";
|
|
409
|
+
var PRINCIPAL_ID_MODE_RAW = "raw";
|
|
410
410
|
var PYTHON_WHITESPACE = "\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
411
411
|
var PYTHON_STRIP = new RegExp(`^[${PYTHON_WHITESPACE}]+|[${PYTHON_WHITESPACE}]+$`, "gu");
|
|
412
412
|
var LONE_SURROGATE = /\p{Surrogate}/u;
|
|
@@ -416,7 +416,7 @@ function canonicalize(rawPrincipal) {
|
|
|
416
416
|
function pythonStrip(value) {
|
|
417
417
|
return value.replace(PYTHON_STRIP, "");
|
|
418
418
|
}
|
|
419
|
-
function
|
|
419
|
+
function hashPrincipalId(rawPrincipal, options) {
|
|
420
420
|
const { tenantId, key, issuer = null, scheme } = options;
|
|
421
421
|
let message = `${tenantId}\0${canonicalize(rawPrincipal)}`;
|
|
422
422
|
if (issuer !== null) {
|
|
@@ -429,19 +429,19 @@ function hashUserId(rawPrincipal, options) {
|
|
|
429
429
|
function normalizePrincipal(result) {
|
|
430
430
|
if (result === null || typeof result !== "object") return null;
|
|
431
431
|
const candidate = result;
|
|
432
|
-
if (typeof candidate.
|
|
433
|
-
if (LONE_SURROGATE.test(candidate.
|
|
434
|
-
if (canonicalize(candidate.
|
|
432
|
+
if (typeof candidate.principalId !== "string") return null;
|
|
433
|
+
if (LONE_SURROGATE.test(candidate.principalId)) return null;
|
|
434
|
+
if (canonicalize(candidate.principalId) === "") return null;
|
|
435
435
|
const issuerRaw = typeof candidate.issuer === "string" && candidate.issuer ? candidate.issuer : null;
|
|
436
436
|
const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
|
|
437
|
-
return {
|
|
437
|
+
return { principalId: candidate.principalId, issuer };
|
|
438
438
|
}
|
|
439
|
-
function
|
|
440
|
-
if (options.mode ===
|
|
441
|
-
return capCodePoints(principal.
|
|
439
|
+
function principalIdFor(principal, options) {
|
|
440
|
+
if (options.mode === PRINCIPAL_ID_MODE_RAW) {
|
|
441
|
+
return capCodePoints(principal.principalId, RAW_PRINCIPAL_ID_MAX_LEN);
|
|
442
442
|
}
|
|
443
443
|
if (options.key === null || options.key === void 0) return null;
|
|
444
|
-
return
|
|
444
|
+
return hashPrincipalId(principal.principalId, {
|
|
445
445
|
tenantId: options.tenantId,
|
|
446
446
|
key: options.key,
|
|
447
447
|
issuer: principal.issuer ?? null,
|
|
@@ -639,8 +639,8 @@ function extraHeaders(extra) {
|
|
|
639
639
|
return null;
|
|
640
640
|
}
|
|
641
641
|
|
|
642
|
-
// src/integrations/mcp/
|
|
643
|
-
function
|
|
642
|
+
// src/integrations/mcp/principalResolution.ts
|
|
643
|
+
function buildPrincipalResolutionContext(extra, toolName, args) {
|
|
644
644
|
return {
|
|
645
645
|
headers: extraHeaders(extra),
|
|
646
646
|
meta: extraMeta(extra),
|
|
@@ -649,27 +649,40 @@ function buildUserResolutionContext(extra, toolName, args) {
|
|
|
649
649
|
};
|
|
650
650
|
}
|
|
651
651
|
function warnIfIdentityCannotResolve(config) {
|
|
652
|
-
if (config.
|
|
653
|
-
if (config.
|
|
654
|
-
if (config.
|
|
652
|
+
if (config.resolvePrincipal === void 0) return;
|
|
653
|
+
if (config.principalIdMode !== PRINCIPAL_ID_MODE_HASHED) return;
|
|
654
|
+
if (config.principalIdHmacKey !== void 0) return;
|
|
655
655
|
if (typeof process === "undefined" || typeof process.emitWarning !== "function") return;
|
|
656
|
+
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. " : "";
|
|
656
657
|
process.emitWarning(
|
|
657
|
-
|
|
658
|
+
`baton: resolvePrincipal is configured but no principal_id HMAC key is set, so principal_id 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.`
|
|
658
659
|
);
|
|
659
660
|
}
|
|
660
|
-
|
|
661
|
+
var warnedPreRenameShape = false;
|
|
662
|
+
function warnIfPreRenameShape(result) {
|
|
663
|
+
const userId = result?.userId;
|
|
664
|
+
if (warnedPreRenameShape || typeof userId !== "string") return;
|
|
665
|
+
warnedPreRenameShape = true;
|
|
666
|
+
warn(
|
|
667
|
+
"baton: resolvePrincipal returned { userId }, which was renamed to { principalId } in 0.3.5, so principal_id is dropped from every event until the hook returns the new key."
|
|
668
|
+
);
|
|
669
|
+
}
|
|
670
|
+
async function resolveCallPrincipalId(hook, call, options) {
|
|
661
671
|
if (hook === void 0) return null;
|
|
662
672
|
let result;
|
|
663
673
|
try {
|
|
664
|
-
const context =
|
|
674
|
+
const context = buildPrincipalResolutionContext(call.extra, call.toolName, call.arguments);
|
|
665
675
|
result = await hook(context);
|
|
666
676
|
} catch {
|
|
667
677
|
return null;
|
|
668
678
|
}
|
|
669
679
|
const principal = normalizePrincipal(result);
|
|
670
|
-
if (principal === null)
|
|
680
|
+
if (principal === null) {
|
|
681
|
+
warnIfPreRenameShape(result);
|
|
682
|
+
return null;
|
|
683
|
+
}
|
|
671
684
|
try {
|
|
672
|
-
return
|
|
685
|
+
return principalIdFor(principal, {
|
|
673
686
|
mode: options.mode,
|
|
674
687
|
tenantId: options.tenantId,
|
|
675
688
|
key: options.key
|
|
@@ -768,13 +781,13 @@ function registerAnnotationTool(server, options) {
|
|
|
768
781
|
if (args.signal_type === void 0) {
|
|
769
782
|
options.tracker?.mark(sessionId);
|
|
770
783
|
}
|
|
771
|
-
const
|
|
772
|
-
options.
|
|
784
|
+
const principalId = await resolveCallPrincipalId(
|
|
785
|
+
options.resolvePrincipal,
|
|
773
786
|
{ extra, toolName: name, arguments: args },
|
|
774
787
|
{
|
|
775
|
-
mode: options.
|
|
788
|
+
mode: options.principalIdMode,
|
|
776
789
|
tenantId: options.tenantId,
|
|
777
|
-
key: options.
|
|
790
|
+
key: options.principalIdHmacKey
|
|
778
791
|
}
|
|
779
792
|
);
|
|
780
793
|
await emit(
|
|
@@ -787,7 +800,7 @@ function registerAnnotationTool(server, options) {
|
|
|
787
800
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
788
801
|
consent_token: options.consentToken,
|
|
789
802
|
agent_runtime: runtime,
|
|
790
|
-
|
|
803
|
+
principal_id: principalId,
|
|
791
804
|
runtime_meta: scrubbedMeta,
|
|
792
805
|
payload: {
|
|
793
806
|
// Agent-facing names -> wire keys, as with `overall_task`
|
|
@@ -1025,16 +1038,16 @@ function sealKey(dsn) {
|
|
|
1025
1038
|
|
|
1026
1039
|
// src/integrations/mcp/config.ts
|
|
1027
1040
|
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
|
|
1028
|
-
var
|
|
1041
|
+
var PRINCIPAL_ID_MODES = /* @__PURE__ */ new Set(["hashed", "raw"]);
|
|
1029
1042
|
function resolveTenantId(explicit, vendorId) {
|
|
1030
1043
|
if (explicit) return explicit;
|
|
1031
1044
|
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_TENANT_ID : void 0;
|
|
1032
1045
|
if (fromEnv) return fromEnv;
|
|
1033
1046
|
return vendorId;
|
|
1034
1047
|
}
|
|
1035
|
-
function
|
|
1048
|
+
function resolvePrincipalIdHmacKey(explicit) {
|
|
1036
1049
|
if (explicit !== void 0) return explicit;
|
|
1037
|
-
const fromEnv = typeof process !== "undefined" ? process.env?.
|
|
1050
|
+
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_PRINCIPAL_ID_HMAC_KEY : void 0;
|
|
1038
1051
|
return fromEnv ? fromEnv : void 0;
|
|
1039
1052
|
}
|
|
1040
1053
|
function resolveBatonConfig(config, serverName) {
|
|
@@ -1094,7 +1107,17 @@ function resolveBatonConfig(config, serverName) {
|
|
|
1094
1107
|
sink: new HttpSink(dsn.origin, { apiKey: dsn.key })
|
|
1095
1108
|
};
|
|
1096
1109
|
}
|
|
1110
|
+
var RENAMED_KEYS = {
|
|
1111
|
+
resolveUser: "resolvePrincipal",
|
|
1112
|
+
userIdMode: "principalIdMode",
|
|
1113
|
+
userIdHmacKey: "principalIdHmacKey"
|
|
1114
|
+
};
|
|
1097
1115
|
function validateBatonConfig(config) {
|
|
1116
|
+
for (const [was, now] of Object.entries(RENAMED_KEYS)) {
|
|
1117
|
+
if (was in config) {
|
|
1118
|
+
throw new Error(`BatonConfig.${was} was renamed to ${now} in 0.3.5.`);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1098
1121
|
if (!config.vendorId) {
|
|
1099
1122
|
throw new Error(
|
|
1100
1123
|
"BatonConfig needs a vendorId \u2014 either directly, or via a dsn whose last path segment names the server (see /account)."
|
|
@@ -1125,15 +1148,15 @@ function validateBatonConfig(config) {
|
|
|
1125
1148
|
`BatonConfig.intentParamMode ${JSON.stringify(config.intentParamMode)} must be one of ${JSON.stringify([...INTENT_PARAM_MODES].sort())}.`
|
|
1126
1149
|
);
|
|
1127
1150
|
}
|
|
1128
|
-
if (config.
|
|
1129
|
-
throw new Error("BatonConfig.
|
|
1151
|
+
if (config.resolvePrincipal !== void 0 && typeof config.resolvePrincipal !== "function") {
|
|
1152
|
+
throw new Error("BatonConfig.resolvePrincipal must be a function.");
|
|
1130
1153
|
}
|
|
1131
|
-
if (config.
|
|
1132
|
-
throw new Error("BatonConfig.
|
|
1154
|
+
if (config.principalIdHmacKey !== void 0 && typeof config.principalIdHmacKey !== "string" && !(config.principalIdHmacKey instanceof Uint8Array)) {
|
|
1155
|
+
throw new Error("BatonConfig.principalIdHmacKey must be a string or a Uint8Array.");
|
|
1133
1156
|
}
|
|
1134
|
-
if (config.
|
|
1157
|
+
if (config.principalIdMode !== void 0 && !PRINCIPAL_ID_MODES.has(config.principalIdMode)) {
|
|
1135
1158
|
throw new Error(
|
|
1136
|
-
`BatonConfig.
|
|
1159
|
+
`BatonConfig.principalIdMode ${JSON.stringify(config.principalIdMode)} must be one of ${JSON.stringify([...PRINCIPAL_ID_MODES].sort())}.`
|
|
1137
1160
|
);
|
|
1138
1161
|
}
|
|
1139
1162
|
}
|
|
@@ -1488,10 +1511,10 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1488
1511
|
const scrubbedExpected = rawExpected !== null ? ctx.scrubber(rawExpected) : null;
|
|
1489
1512
|
const scrubbedWorkflow = rawWorkflow !== null ? ctx.scrubber(rawWorkflow) : null;
|
|
1490
1513
|
const callId = uuidv72();
|
|
1491
|
-
const
|
|
1492
|
-
ctx.
|
|
1514
|
+
const principalId = await resolveCallPrincipalId(
|
|
1515
|
+
ctx.resolvePrincipal,
|
|
1493
1516
|
{ extra, toolName, arguments: params },
|
|
1494
|
-
{ mode: ctx.
|
|
1517
|
+
{ mode: ctx.principalIdMode, tenantId: ctx.tenantId, key: ctx.principalIdHmacKey }
|
|
1495
1518
|
);
|
|
1496
1519
|
const common = {
|
|
1497
1520
|
tenant_id: ctx.tenantId,
|
|
@@ -1504,7 +1527,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1504
1527
|
// (`middleware.py` 481/524/562/606). `surface_snapshot` is deliberately
|
|
1505
1528
|
// NOT among them: it describes the SERVER and is captured outside any
|
|
1506
1529
|
// call, so there is no caller to name (register D5).
|
|
1507
|
-
|
|
1530
|
+
principal_id: principalId,
|
|
1508
1531
|
runtime_meta: scrubbedMeta
|
|
1509
1532
|
};
|
|
1510
1533
|
if (scrubbedIntent !== null && ctx.tracker.claim(sessionId)) {
|
|
@@ -1811,9 +1834,9 @@ function withBaton(server, supplied = {}) {
|
|
|
1811
1834
|
annotationToolName,
|
|
1812
1835
|
intentParamMode,
|
|
1813
1836
|
paramRegistry: /* @__PURE__ */ new Map(),
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1837
|
+
resolvePrincipal: config.resolvePrincipal,
|
|
1838
|
+
principalIdMode: config.principalIdMode ?? "hashed",
|
|
1839
|
+
principalIdHmacKey: resolvePrincipalIdHmacKey(config.principalIdHmacKey),
|
|
1817
1840
|
vendorToolJsonSchema,
|
|
1818
1841
|
bustSchemaMemo,
|
|
1819
1842
|
tracker,
|
|
@@ -1834,14 +1857,14 @@ function withBaton(server, supplied = {}) {
|
|
|
1834
1857
|
vendorDisplayName: config.vendorDisplayName,
|
|
1835
1858
|
consentToken: ctx.consentToken,
|
|
1836
1859
|
// Read off `ctx`, never re-resolved from `config`: the two paths must
|
|
1837
|
-
// agree on the actor, and `
|
|
1860
|
+
// agree on the actor, and `principalIdMode`/`principalIdHmacKey` each have a
|
|
1838
1861
|
// default and an env fallback that a second resolution could take
|
|
1839
1862
|
// differently. Required (not optional) on the options type ON PURPOSE —
|
|
1840
1863
|
// the compiler then refuses a registration that forgets them, which is
|
|
1841
1864
|
// the mechanical version of "wiring two of four call sites".
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1865
|
+
resolvePrincipal: ctx.resolvePrincipal,
|
|
1866
|
+
principalIdMode: ctx.principalIdMode,
|
|
1867
|
+
principalIdHmacKey: ctx.principalIdHmacKey,
|
|
1845
1868
|
fallbackSessionId: ctx.fallbackSessionId,
|
|
1846
1869
|
scrubber: ctx.scrubber,
|
|
1847
1870
|
// The RESOLVED name, never `config.annotationToolName`: handing over the
|
|
@@ -1890,7 +1913,7 @@ export {
|
|
|
1890
1913
|
ToolCallStartEventSchema,
|
|
1891
1914
|
ToolCallStartPayloadSchema,
|
|
1892
1915
|
VENDOR_HASH_SCHEME,
|
|
1893
|
-
|
|
1916
|
+
hashPrincipalId,
|
|
1894
1917
|
identityScrub,
|
|
1895
1918
|
safeWrite,
|
|
1896
1919
|
withBaton
|