@goodtiming/baton-sdk 0.3.2 → 0.3.3
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.cjs +187 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +137 -2
- package/dist/index.d.ts +137 -2
- package/dist/index.js +183 -14
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -49,6 +49,8 @@ __export(index_exports, {
|
|
|
49
49
|
ToolCallErrorPayloadSchema: () => ToolCallErrorPayloadSchema,
|
|
50
50
|
ToolCallStartEventSchema: () => ToolCallStartEventSchema,
|
|
51
51
|
ToolCallStartPayloadSchema: () => ToolCallStartPayloadSchema,
|
|
52
|
+
VENDOR_HASH_SCHEME: () => VENDOR_HASH_SCHEME,
|
|
53
|
+
hashUserId: () => hashUserId,
|
|
52
54
|
identityScrub: () => identityScrub,
|
|
53
55
|
safeWrite: () => safeWrite,
|
|
54
56
|
withBaton: () => withBaton
|
|
@@ -56,7 +58,7 @@ __export(index_exports, {
|
|
|
56
58
|
module.exports = __toCommonJS(index_exports);
|
|
57
59
|
|
|
58
60
|
// src/version.ts
|
|
59
|
-
var SDK_VERSION = "ts-0.3.
|
|
61
|
+
var SDK_VERSION = "ts-0.3.3";
|
|
60
62
|
|
|
61
63
|
// src/events.ts
|
|
62
64
|
var import_zod = require("zod");
|
|
@@ -463,6 +465,134 @@ async function emit(sink, build) {
|
|
|
463
465
|
await safeWrite(sink, event);
|
|
464
466
|
}
|
|
465
467
|
|
|
468
|
+
// src/identity.ts
|
|
469
|
+
var import_node_crypto = require("crypto");
|
|
470
|
+
|
|
471
|
+
// src/_text.ts
|
|
472
|
+
function capCodePoints(value, max) {
|
|
473
|
+
if (value.length <= max) return value;
|
|
474
|
+
return [...value].slice(0, max).join("");
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// src/identity.ts
|
|
478
|
+
var VENDOR_HASH_SCHEME = "v1";
|
|
479
|
+
var RAW_USER_ID_MAX_LEN = 128;
|
|
480
|
+
var USER_ID_MODE_HASHED = "hashed";
|
|
481
|
+
var USER_ID_MODE_RAW = "raw";
|
|
482
|
+
var PYTHON_WHITESPACE = "\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
483
|
+
var PYTHON_STRIP = new RegExp(`^[${PYTHON_WHITESPACE}]+|[${PYTHON_WHITESPACE}]+$`, "gu");
|
|
484
|
+
var LONE_SURROGATE = /\p{Surrogate}/u;
|
|
485
|
+
function canonicalize(rawPrincipal) {
|
|
486
|
+
return rawPrincipal.normalize("NFC").replace(PYTHON_STRIP, "").toLowerCase();
|
|
487
|
+
}
|
|
488
|
+
function hashUserId(rawPrincipal, options) {
|
|
489
|
+
const { tenantId, key, issuer = null, scheme } = options;
|
|
490
|
+
let message = `${tenantId}\0${canonicalize(rawPrincipal)}`;
|
|
491
|
+
if (issuer !== null) {
|
|
492
|
+
message += `\0${canonicalize(issuer)}`;
|
|
493
|
+
}
|
|
494
|
+
const keyBytes = typeof key === "string" ? Buffer.from(key, "utf8") : key;
|
|
495
|
+
const digest = (0, import_node_crypto.createHmac)("sha256", keyBytes).update(message, "utf8").digest("hex");
|
|
496
|
+
return `${scheme}:${digest}`;
|
|
497
|
+
}
|
|
498
|
+
function normalizePrincipal(result) {
|
|
499
|
+
if (result === null || typeof result !== "object") return null;
|
|
500
|
+
const candidate = result;
|
|
501
|
+
if (typeof candidate.userId !== "string") return null;
|
|
502
|
+
if (LONE_SURROGATE.test(candidate.userId)) return null;
|
|
503
|
+
if (canonicalize(candidate.userId) === "") return null;
|
|
504
|
+
const issuerRaw = typeof candidate.issuer === "string" && candidate.issuer ? candidate.issuer : null;
|
|
505
|
+
const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
|
|
506
|
+
return { userId: candidate.userId, issuer };
|
|
507
|
+
}
|
|
508
|
+
function userIdForPrincipal(principal, options) {
|
|
509
|
+
if (options.mode === USER_ID_MODE_RAW) {
|
|
510
|
+
return capCodePoints(principal.userId, RAW_USER_ID_MAX_LEN);
|
|
511
|
+
}
|
|
512
|
+
if (options.key === null || options.key === void 0) return null;
|
|
513
|
+
return hashUserId(principal.userId, {
|
|
514
|
+
tenantId: options.tenantId,
|
|
515
|
+
key: options.key,
|
|
516
|
+
issuer: principal.issuer ?? null,
|
|
517
|
+
// Unconditional: this arm has no attested rung, so every principal it
|
|
518
|
+
// hashes is vendor-ASSERTED. A `scheme` option here would be an argument
|
|
519
|
+
// no caller can vary — configurability for a rung the design says is not
|
|
520
|
+
// coming to this SDK.
|
|
521
|
+
scheme: VENDOR_HASH_SCHEME
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// src/integrations/mcp/mcpTypes.ts
|
|
526
|
+
function extraMeta(extra) {
|
|
527
|
+
const direct = extra._meta;
|
|
528
|
+
if (direct) return direct;
|
|
529
|
+
const nested = extra.mcpReq?._meta;
|
|
530
|
+
return nested ?? null;
|
|
531
|
+
}
|
|
532
|
+
function extraEnvelope(extra) {
|
|
533
|
+
const envelope = extra.mcpReq?.envelope;
|
|
534
|
+
return envelope ?? null;
|
|
535
|
+
}
|
|
536
|
+
function extraHeaders(extra) {
|
|
537
|
+
const fromV2 = extra.http?.req?.headers;
|
|
538
|
+
if (fromV2 && typeof fromV2.get === "function") return fromV2;
|
|
539
|
+
const fromV1 = extra.requestInfo?.headers;
|
|
540
|
+
if (fromV1 && typeof fromV1 === "object") {
|
|
541
|
+
const headers = new Headers();
|
|
542
|
+
for (const [name, value] of Object.entries(fromV1)) {
|
|
543
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
544
|
+
if (typeof item !== "string") continue;
|
|
545
|
+
try {
|
|
546
|
+
headers.append(name, item);
|
|
547
|
+
} catch {
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return headers;
|
|
552
|
+
}
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// src/integrations/mcp/userResolution.ts
|
|
557
|
+
function buildUserResolutionContext(extra, toolName, args) {
|
|
558
|
+
return {
|
|
559
|
+
headers: extraHeaders(extra),
|
|
560
|
+
meta: extraMeta(extra),
|
|
561
|
+
toolName,
|
|
562
|
+
arguments: args
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
function warnIfIdentityCannotResolve(config) {
|
|
566
|
+
if (config.resolveUser === void 0) return;
|
|
567
|
+
if (config.userIdMode !== USER_ID_MODE_HASHED) return;
|
|
568
|
+
if (config.userIdHmacKey !== void 0) return;
|
|
569
|
+
if (typeof process === "undefined" || typeof process.emitWarning !== "function") return;
|
|
570
|
+
process.emitWarning(
|
|
571
|
+
'baton: resolveUser is configured but no user_id HMAC key is set, so user_id is dropped from every event (events still emit). Set BATON_USER_ID_HMAC_KEY or BatonConfig.userIdHmacKey, or pass userIdMode: "raw" if you intend to emit the subject verbatim.'
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
async function resolveCallUserId(hook, call, options) {
|
|
575
|
+
if (hook === void 0) return null;
|
|
576
|
+
let result;
|
|
577
|
+
try {
|
|
578
|
+
const context = buildUserResolutionContext(call.extra, call.toolName, call.arguments);
|
|
579
|
+
result = await hook(context);
|
|
580
|
+
} catch {
|
|
581
|
+
return null;
|
|
582
|
+
}
|
|
583
|
+
const principal = normalizePrincipal(result);
|
|
584
|
+
if (principal === null) return null;
|
|
585
|
+
try {
|
|
586
|
+
return userIdForPrincipal(principal, {
|
|
587
|
+
mode: options.mode,
|
|
588
|
+
tenantId: options.tenantId,
|
|
589
|
+
key: options.key
|
|
590
|
+
});
|
|
591
|
+
} catch {
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
466
596
|
// src/integrations/mcp/llmText.ts
|
|
467
597
|
var SERVER_INSTRUCTIONS_TEMPLATE = (vendorDisplayName, annotationToolName) => `This server is wrapped in the ${vendorDisplayName} support-signal SDK. Use \`${annotationToolName}\` to record what the user is trying to do and how each ${vendorDisplayName} tool call went. See that tool's description for field-level detail.
|
|
468
598
|
|
|
@@ -524,18 +654,6 @@ function buildExpectedResultParamDescription() {
|
|
|
524
654
|
return EXPECTED_RESULT_PARAM_DESCRIPTION;
|
|
525
655
|
}
|
|
526
656
|
|
|
527
|
-
// src/integrations/mcp/mcpTypes.ts
|
|
528
|
-
function extraMeta(extra) {
|
|
529
|
-
const direct = extra._meta;
|
|
530
|
-
if (direct) return direct;
|
|
531
|
-
const nested = extra.mcpReq?._meta;
|
|
532
|
-
return nested ?? null;
|
|
533
|
-
}
|
|
534
|
-
function extraEnvelope(extra) {
|
|
535
|
-
const envelope = extra.mcpReq?.envelope;
|
|
536
|
-
return envelope ?? null;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
657
|
// src/integrations/mcp/runtimeAdapter.ts
|
|
540
658
|
var CLIENT_INFO_META_KEY = "io.modelcontextprotocol/clientInfo";
|
|
541
659
|
var CLIENT_NAME_MAX_LEN = 128;
|
|
@@ -549,7 +667,7 @@ function clean(name, scrubber) {
|
|
|
549
667
|
cleaned = scrubbed;
|
|
550
668
|
}
|
|
551
669
|
if (!cleaned) return null;
|
|
552
|
-
return
|
|
670
|
+
return capCodePoints(cleaned, CLIENT_NAME_MAX_LEN);
|
|
553
671
|
}
|
|
554
672
|
function declaredOnRequest(meta, envelope) {
|
|
555
673
|
for (const source of [envelope, meta]) {
|
|
@@ -635,6 +753,15 @@ function registerAnnotationTool(server, options) {
|
|
|
635
753
|
if (args.signal_type === void 0) {
|
|
636
754
|
options.tracker?.mark(sessionId);
|
|
637
755
|
}
|
|
756
|
+
const userId = await resolveCallUserId(
|
|
757
|
+
options.resolveUser,
|
|
758
|
+
{ extra, toolName: name, arguments: args },
|
|
759
|
+
{
|
|
760
|
+
mode: options.userIdMode,
|
|
761
|
+
tenantId: options.tenantId,
|
|
762
|
+
key: options.userIdHmacKey
|
|
763
|
+
}
|
|
764
|
+
);
|
|
638
765
|
await emit(
|
|
639
766
|
options.sink,
|
|
640
767
|
() => AnnotationEventSchema.parse({
|
|
@@ -645,6 +772,7 @@ function registerAnnotationTool(server, options) {
|
|
|
645
772
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
646
773
|
consent_token: options.consentToken,
|
|
647
774
|
agent_runtime: runtime,
|
|
775
|
+
user_id: userId,
|
|
648
776
|
runtime_meta: scrubbedMeta,
|
|
649
777
|
payload: {
|
|
650
778
|
// Agent-facing names -> wire keys, as with `overall_task`
|
|
@@ -882,12 +1010,18 @@ function sealKey(dsn) {
|
|
|
882
1010
|
|
|
883
1011
|
// src/integrations/mcp/config.ts
|
|
884
1012
|
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
|
|
1013
|
+
var USER_ID_MODES = /* @__PURE__ */ new Set(["hashed", "raw"]);
|
|
885
1014
|
function resolveTenantId(explicit, vendorId) {
|
|
886
1015
|
if (explicit) return explicit;
|
|
887
1016
|
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_TENANT_ID : void 0;
|
|
888
1017
|
if (fromEnv) return fromEnv;
|
|
889
1018
|
return vendorId;
|
|
890
1019
|
}
|
|
1020
|
+
function resolveUserIdHmacKey(explicit) {
|
|
1021
|
+
if (explicit !== void 0) return explicit;
|
|
1022
|
+
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_USER_ID_HMAC_KEY : void 0;
|
|
1023
|
+
return fromEnv ? fromEnv : void 0;
|
|
1024
|
+
}
|
|
891
1025
|
function resolveBatonConfig(config) {
|
|
892
1026
|
const dsnString = selectDsn(
|
|
893
1027
|
config.dsn,
|
|
@@ -969,6 +1103,17 @@ function validateBatonConfig(config) {
|
|
|
969
1103
|
`BatonConfig.intentParamMode ${JSON.stringify(config.intentParamMode)} must be one of ${JSON.stringify([...INTENT_PARAM_MODES].sort())}.`
|
|
970
1104
|
);
|
|
971
1105
|
}
|
|
1106
|
+
if (config.resolveUser !== void 0 && typeof config.resolveUser !== "function") {
|
|
1107
|
+
throw new Error("BatonConfig.resolveUser must be a function.");
|
|
1108
|
+
}
|
|
1109
|
+
if (config.userIdHmacKey !== void 0 && typeof config.userIdHmacKey !== "string" && !(config.userIdHmacKey instanceof Uint8Array)) {
|
|
1110
|
+
throw new Error("BatonConfig.userIdHmacKey must be a string or a Uint8Array.");
|
|
1111
|
+
}
|
|
1112
|
+
if (config.userIdMode !== void 0 && !USER_ID_MODES.has(config.userIdMode)) {
|
|
1113
|
+
throw new Error(
|
|
1114
|
+
`BatonConfig.userIdMode ${JSON.stringify(config.userIdMode)} must be one of ${JSON.stringify([...USER_ID_MODES].sort())}.`
|
|
1115
|
+
);
|
|
1116
|
+
}
|
|
972
1117
|
}
|
|
973
1118
|
|
|
974
1119
|
// src/optout.ts
|
|
@@ -1194,10 +1339,10 @@ function toolInputJsonSchema(inputSchema) {
|
|
|
1194
1339
|
}
|
|
1195
1340
|
|
|
1196
1341
|
// src/integrations/mcp/surface.ts
|
|
1197
|
-
var
|
|
1342
|
+
var import_node_crypto2 = require("crypto");
|
|
1198
1343
|
function surfaceHash(surface) {
|
|
1199
1344
|
const canonical = canonicalJson(surface);
|
|
1200
|
-
const digest = (0,
|
|
1345
|
+
const digest = (0, import_node_crypto2.createHash)("sha256").update(canonical, "utf-8").digest("hex");
|
|
1201
1346
|
return `sha256:${digest}`;
|
|
1202
1347
|
}
|
|
1203
1348
|
function canonicalJson(value) {
|
|
@@ -1320,12 +1465,23 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1320
1465
|
const scrubbedExpected = rawExpected !== null ? ctx.scrubber(rawExpected) : null;
|
|
1321
1466
|
const scrubbedWorkflow = rawWorkflow !== null ? ctx.scrubber(rawWorkflow) : null;
|
|
1322
1467
|
const callId = (0, import_uuid2.v7)();
|
|
1468
|
+
const userId = await resolveCallUserId(
|
|
1469
|
+
ctx.resolveUser,
|
|
1470
|
+
{ extra, toolName, arguments: params },
|
|
1471
|
+
{ mode: ctx.userIdMode, tenantId: ctx.tenantId, key: ctx.userIdHmacKey }
|
|
1472
|
+
);
|
|
1323
1473
|
const common = {
|
|
1324
1474
|
tenant_id: ctx.tenantId,
|
|
1325
1475
|
vendor_id: ctx.vendorId,
|
|
1326
1476
|
session_id: sessionId,
|
|
1327
1477
|
consent_token: ctx.consentToken,
|
|
1328
1478
|
agent_runtime: runtime,
|
|
1479
|
+
// The four `...common` sites below are the tool-call legs and the
|
|
1480
|
+
// proactive annotation — the same four Python stamps
|
|
1481
|
+
// (`middleware.py` 481/524/562/606). `surface_snapshot` is deliberately
|
|
1482
|
+
// NOT among them: it describes the SERVER and is captured outside any
|
|
1483
|
+
// call, so there is no caller to name (register D5).
|
|
1484
|
+
user_id: userId,
|
|
1329
1485
|
runtime_meta: scrubbedMeta
|
|
1330
1486
|
};
|
|
1331
1487
|
if (scrubbedIntent !== null && ctx.tracker.claim(sessionId)) {
|
|
@@ -1628,12 +1784,16 @@ function withBaton(server, supplied = {}) {
|
|
|
1628
1784
|
annotationToolName,
|
|
1629
1785
|
intentParamMode,
|
|
1630
1786
|
paramRegistry: /* @__PURE__ */ new Map(),
|
|
1787
|
+
resolveUser: config.resolveUser,
|
|
1788
|
+
userIdMode: config.userIdMode ?? "hashed",
|
|
1789
|
+
userIdHmacKey: resolveUserIdHmacKey(config.userIdHmacKey),
|
|
1631
1790
|
vendorToolJsonSchema,
|
|
1632
1791
|
bustSchemaMemo,
|
|
1633
1792
|
tracker,
|
|
1634
1793
|
surfaceState,
|
|
1635
1794
|
emitSurface
|
|
1636
1795
|
};
|
|
1796
|
+
warnIfIdentityCannotResolve(ctx);
|
|
1637
1797
|
const instructions = buildServerInstructions({
|
|
1638
1798
|
vendorDisplayName: config.vendorDisplayName,
|
|
1639
1799
|
annotationToolName
|
|
@@ -1646,6 +1806,15 @@ function withBaton(server, supplied = {}) {
|
|
|
1646
1806
|
vendorId: ctx.vendorId,
|
|
1647
1807
|
vendorDisplayName: config.vendorDisplayName,
|
|
1648
1808
|
consentToken: ctx.consentToken,
|
|
1809
|
+
// Read off `ctx`, never re-resolved from `config`: the two paths must
|
|
1810
|
+
// agree on the actor, and `userIdMode`/`userIdHmacKey` each have a
|
|
1811
|
+
// default and an env fallback that a second resolution could take
|
|
1812
|
+
// differently. Required (not optional) on the options type ON PURPOSE —
|
|
1813
|
+
// the compiler then refuses a registration that forgets them, which is
|
|
1814
|
+
// the mechanical version of "wiring two of four call sites".
|
|
1815
|
+
resolveUser: ctx.resolveUser,
|
|
1816
|
+
userIdMode: ctx.userIdMode,
|
|
1817
|
+
userIdHmacKey: ctx.userIdHmacKey,
|
|
1649
1818
|
fallbackSessionId: ctx.fallbackSessionId,
|
|
1650
1819
|
scrubber: ctx.scrubber,
|
|
1651
1820
|
annotationToolName: config.annotationToolName,
|
|
@@ -1691,6 +1860,8 @@ function withBaton(server, supplied = {}) {
|
|
|
1691
1860
|
ToolCallErrorPayloadSchema,
|
|
1692
1861
|
ToolCallStartEventSchema,
|
|
1693
1862
|
ToolCallStartPayloadSchema,
|
|
1863
|
+
VENDOR_HASH_SCHEME,
|
|
1864
|
+
hashUserId,
|
|
1694
1865
|
identityScrub,
|
|
1695
1866
|
safeWrite,
|
|
1696
1867
|
withBaton
|