@goodtiming/baton-sdk 0.3.7 → 0.4.1
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 +140 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +169 -48
- package/dist/index.d.ts +169 -48
- package/dist/index.js +136 -41
- 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.
|
|
2
|
+
var SDK_VERSION = "ts-0.4.1";
|
|
3
3
|
|
|
4
4
|
// src/events.ts
|
|
5
5
|
import { z } from "zod";
|
|
@@ -21,7 +21,23 @@ var ToolCallErrorPayloadSchema = z.object({
|
|
|
21
21
|
tool_name: z.string(),
|
|
22
22
|
error_type: z.string(),
|
|
23
23
|
error_body: z.string(),
|
|
24
|
-
duration_ms: z.number().int().nullable().default(null)
|
|
24
|
+
duration_ms: z.number().int().nullable().default(null),
|
|
25
|
+
// ⚠ `.optional()`, NOT `.default(null)` like the sibling fields — and it
|
|
26
|
+
// stays that way now that the producer emits the field.
|
|
27
|
+
//
|
|
28
|
+
// A default makes the schema an EMITTER: parsing a payload without the key
|
|
29
|
+
// ADDS it. That is wrong in a schema that also READS — a stored event from
|
|
30
|
+
// before the field existed would come back carrying an invented key, and
|
|
31
|
+
// against the pre-`f1e0280` spec pin, whose `events.schema.json` is
|
|
32
|
+
// `additionalProperties: false`, it reddened three tests saying so: a
|
|
33
|
+
// byte-identical vector round-trip, an ajv validation of TS-built events,
|
|
34
|
+
// and a field-for-field payload comparison.
|
|
35
|
+
//
|
|
36
|
+
// `.optional()` accepts the field when a vector carries it and stays
|
|
37
|
+
// silent when it does not. What makes the EMITTED shape complete is the
|
|
38
|
+
// emitter declaring `result` on both failure legs (`withBaton.ts`),
|
|
39
|
+
// explicitly `null` on a throw — not the schema filling it in.
|
|
40
|
+
result: z.unknown().nullable().optional()
|
|
25
41
|
}).strict();
|
|
26
42
|
var AnnotationPayloadSchema = z.object({
|
|
27
43
|
intent: z.string().nullable().default(null),
|
|
@@ -42,6 +58,11 @@ var SurfaceSnapshotPayloadSchema = z.object({
|
|
|
42
58
|
seam_augmentations: z.record(z.string(), z.unknown()).default({})
|
|
43
59
|
}).strict();
|
|
44
60
|
var DEFAULT_CONSENT_TOKEN = "customer-consented";
|
|
61
|
+
var PrincipalWireSchema = z.object({
|
|
62
|
+
id: z.string(),
|
|
63
|
+
source: z.string(),
|
|
64
|
+
form: z.string()
|
|
65
|
+
}).strict();
|
|
45
66
|
var envelopeShape = {
|
|
46
67
|
event_id: z.uuid().default(() => uuidv7()),
|
|
47
68
|
tenant_id: z.string(),
|
|
@@ -52,7 +73,7 @@ var envelopeShape = {
|
|
|
52
73
|
consent_token: z.string(),
|
|
53
74
|
sdk_version: z.string().default(SDK_VERSION),
|
|
54
75
|
agent_runtime: z.string().default("unknown"),
|
|
55
|
-
|
|
76
|
+
principal: PrincipalWireSchema.nullable().default(null),
|
|
56
77
|
transport_observed: z.string().nullable().default(null),
|
|
57
78
|
call_id: z.string().nullable().default(null),
|
|
58
79
|
runtime_meta: z.record(z.string(), z.unknown()).nullable().default(null)
|
|
@@ -449,10 +470,19 @@ function capCodePoints(value, max) {
|
|
|
449
470
|
}
|
|
450
471
|
|
|
451
472
|
// src/identity.ts
|
|
452
|
-
var
|
|
473
|
+
var PRINCIPAL_SOURCE_ASSERTED = "asserted";
|
|
474
|
+
var PRINCIPAL_FORM_HASHED = "hashed";
|
|
475
|
+
var PRINCIPAL_FORM_RAW = "raw";
|
|
453
476
|
var RAW_PRINCIPAL_ID_MAX_LEN = 128;
|
|
454
477
|
var PRINCIPAL_ID_MODE_HASHED = "hashed";
|
|
455
478
|
var PRINCIPAL_ID_MODE_RAW = "raw";
|
|
479
|
+
var FORM_BY_MODE = {
|
|
480
|
+
[PRINCIPAL_ID_MODE_HASHED]: PRINCIPAL_FORM_HASHED,
|
|
481
|
+
[PRINCIPAL_ID_MODE_RAW]: PRINCIPAL_FORM_RAW
|
|
482
|
+
};
|
|
483
|
+
var PRINCIPAL_ID_MODES = Object.keys(
|
|
484
|
+
FORM_BY_MODE
|
|
485
|
+
);
|
|
456
486
|
var PYTHON_WHITESPACE = "\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
457
487
|
var PYTHON_STRIP = new RegExp(`^[${PYTHON_WHITESPACE}]+|[${PYTHON_WHITESPACE}]+$`, "gu");
|
|
458
488
|
var LONE_SURROGATE = /\p{Surrogate}/u;
|
|
@@ -463,14 +493,13 @@ function pythonStrip(value) {
|
|
|
463
493
|
return value.replace(PYTHON_STRIP, "");
|
|
464
494
|
}
|
|
465
495
|
function hashPrincipalId(rawPrincipal, options) {
|
|
466
|
-
const { tenantId, key, issuer = null
|
|
496
|
+
const { tenantId, key, issuer = null } = options;
|
|
467
497
|
let message = `${tenantId}\0${canonicalize(rawPrincipal)}`;
|
|
468
498
|
if (issuer !== null) {
|
|
469
499
|
message += `\0${canonicalize(issuer)}`;
|
|
470
500
|
}
|
|
471
501
|
const keyBytes = typeof key === "string" ? Buffer.from(key, "utf8") : key;
|
|
472
|
-
|
|
473
|
-
return `${scheme}:${digest}`;
|
|
502
|
+
return createHmac("sha256", keyBytes).update(message, "utf8").digest("hex");
|
|
474
503
|
}
|
|
475
504
|
function normalizePrincipal(result) {
|
|
476
505
|
if (result === null || typeof result !== "object") return null;
|
|
@@ -482,21 +511,19 @@ function normalizePrincipal(result) {
|
|
|
482
511
|
const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
|
|
483
512
|
return { principalId: candidate.principalId, issuer };
|
|
484
513
|
}
|
|
485
|
-
function
|
|
514
|
+
function principalFor(principal, options) {
|
|
515
|
+
let id;
|
|
486
516
|
if (options.mode === PRINCIPAL_ID_MODE_RAW) {
|
|
487
|
-
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
// coming to this SDK.
|
|
498
|
-
scheme: VENDOR_HASH_SCHEME
|
|
499
|
-
});
|
|
517
|
+
id = capCodePoints(principal.principalId, RAW_PRINCIPAL_ID_MAX_LEN);
|
|
518
|
+
} else {
|
|
519
|
+
if (options.key === null || options.key === void 0) return null;
|
|
520
|
+
id = hashPrincipalId(principal.principalId, {
|
|
521
|
+
tenantId: options.tenantId,
|
|
522
|
+
key: options.key,
|
|
523
|
+
issuer: principal.issuer ?? null
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
return { id, source: PRINCIPAL_SOURCE_ASSERTED, form: FORM_BY_MODE[options.mode] };
|
|
500
527
|
}
|
|
501
528
|
|
|
502
529
|
// src/integrations/mcp/llmText.ts
|
|
@@ -711,7 +738,7 @@ function warnIfIdentityCannotResolve(config) {
|
|
|
711
738
|
if (typeof process === "undefined" || typeof process.emitWarning !== "function") return;
|
|
712
739
|
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. " : "";
|
|
713
740
|
process.emitWarning(
|
|
714
|
-
`baton: resolvePrincipal is configured but no
|
|
741
|
+
`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.`
|
|
715
742
|
);
|
|
716
743
|
}
|
|
717
744
|
var warnedPreRenameShape = false;
|
|
@@ -720,10 +747,10 @@ function warnIfPreRenameShape(result) {
|
|
|
720
747
|
if (warnedPreRenameShape || typeof userId !== "string") return;
|
|
721
748
|
warnedPreRenameShape = true;
|
|
722
749
|
warn(
|
|
723
|
-
"baton: resolvePrincipal returned { userId }, which was renamed to { principalId } in 0.3.5, so
|
|
750
|
+
"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."
|
|
724
751
|
);
|
|
725
752
|
}
|
|
726
|
-
async function
|
|
753
|
+
async function resolveCallPrincipal(hook, call, options) {
|
|
727
754
|
if (hook === void 0) return null;
|
|
728
755
|
let result;
|
|
729
756
|
try {
|
|
@@ -738,7 +765,7 @@ async function resolveCallPrincipalId(hook, call, options) {
|
|
|
738
765
|
return null;
|
|
739
766
|
}
|
|
740
767
|
try {
|
|
741
|
-
return
|
|
768
|
+
return principalFor(principal, {
|
|
742
769
|
mode: options.mode,
|
|
743
770
|
tenantId: options.tenantId,
|
|
744
771
|
key: options.key
|
|
@@ -837,7 +864,7 @@ function registerAnnotationTool(server, options) {
|
|
|
837
864
|
if (args.signal_type === void 0) {
|
|
838
865
|
options.tracker?.mark(sessionId);
|
|
839
866
|
}
|
|
840
|
-
const
|
|
867
|
+
const principal = await resolveCallPrincipal(
|
|
841
868
|
options.resolvePrincipal,
|
|
842
869
|
{ extra, toolName: name, arguments: args },
|
|
843
870
|
{
|
|
@@ -856,7 +883,7 @@ function registerAnnotationTool(server, options) {
|
|
|
856
883
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
857
884
|
consent_token: options.consentToken,
|
|
858
885
|
agent_runtime: runtime,
|
|
859
|
-
|
|
886
|
+
principal,
|
|
860
887
|
transport_observed: observeTransport(extra),
|
|
861
888
|
runtime_meta: scrubbedMeta,
|
|
862
889
|
payload: {
|
|
@@ -1095,7 +1122,7 @@ function sealKey(dsn) {
|
|
|
1095
1122
|
|
|
1096
1123
|
// src/integrations/mcp/config.ts
|
|
1097
1124
|
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
|
|
1098
|
-
var
|
|
1125
|
+
var PRINCIPAL_ID_MODE_SET = new Set(PRINCIPAL_ID_MODES);
|
|
1099
1126
|
function resolveTenantId(explicit, vendorId) {
|
|
1100
1127
|
if (explicit) return explicit;
|
|
1101
1128
|
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_TENANT_ID : void 0;
|
|
@@ -1211,9 +1238,9 @@ function validateBatonConfig(config) {
|
|
|
1211
1238
|
if (config.principalIdHmacKey !== void 0 && typeof config.principalIdHmacKey !== "string" && !(config.principalIdHmacKey instanceof Uint8Array)) {
|
|
1212
1239
|
throw new Error("BatonConfig.principalIdHmacKey must be a string or a Uint8Array.");
|
|
1213
1240
|
}
|
|
1214
|
-
if (config.principalIdMode !== void 0 && !
|
|
1241
|
+
if (config.principalIdMode !== void 0 && !PRINCIPAL_ID_MODE_SET.has(config.principalIdMode)) {
|
|
1215
1242
|
throw new Error(
|
|
1216
|
-
`BatonConfig.principalIdMode ${JSON.stringify(config.principalIdMode)} must be one of ${JSON.stringify([...
|
|
1243
|
+
`BatonConfig.principalIdMode ${JSON.stringify(config.principalIdMode)} must be one of ${JSON.stringify([...PRINCIPAL_ID_MODE_SET].sort())}.`
|
|
1217
1244
|
);
|
|
1218
1245
|
}
|
|
1219
1246
|
}
|
|
@@ -1250,6 +1277,34 @@ var DisabledSink = class {
|
|
|
1250
1277
|
}
|
|
1251
1278
|
};
|
|
1252
1279
|
|
|
1280
|
+
// src/integrations/mcp/errorResult.ts
|
|
1281
|
+
var TOOL_ERROR_TYPE = "tool_error";
|
|
1282
|
+
var ERROR_BODY_MAX_CODE_POINTS = 2e3;
|
|
1283
|
+
function isErrorResult(value) {
|
|
1284
|
+
try {
|
|
1285
|
+
if (typeof value !== "object" || value === null) return false;
|
|
1286
|
+
return Boolean(value.isError);
|
|
1287
|
+
} catch {
|
|
1288
|
+
return false;
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
function errorText(value) {
|
|
1292
|
+
const parts = [];
|
|
1293
|
+
try {
|
|
1294
|
+
const content = value?.content;
|
|
1295
|
+
if (!Array.isArray(content)) return "";
|
|
1296
|
+
for (const part of content) {
|
|
1297
|
+
const text = part?.text;
|
|
1298
|
+
if (typeof text !== "string") continue;
|
|
1299
|
+
const trimmed = text.trim();
|
|
1300
|
+
if (trimmed) parts.push(trimmed);
|
|
1301
|
+
}
|
|
1302
|
+
} catch {
|
|
1303
|
+
return "";
|
|
1304
|
+
}
|
|
1305
|
+
return parts.join("\n");
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1253
1308
|
// src/integrations/mcp/handle.ts
|
|
1254
1309
|
var BatonHandle = class {
|
|
1255
1310
|
sink;
|
|
@@ -1554,6 +1609,9 @@ var SurfaceState = class {
|
|
|
1554
1609
|
);
|
|
1555
1610
|
}
|
|
1556
1611
|
};
|
|
1612
|
+
function errorBody(ctx, text) {
|
|
1613
|
+
return capCodePoints(String(ctx.scrubber(text)), ERROR_BODY_MAX_CODE_POINTS);
|
|
1614
|
+
}
|
|
1557
1615
|
function batonWrap(nameRef, original, ctx) {
|
|
1558
1616
|
return async (...callArgs) => {
|
|
1559
1617
|
const toolName = nameRef.current;
|
|
@@ -1586,7 +1644,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1586
1644
|
const scrubbedExpected = rawExpected !== null ? ctx.scrubber(rawExpected) : null;
|
|
1587
1645
|
const scrubbedWorkflow = rawWorkflow !== null ? ctx.scrubber(rawWorkflow) : null;
|
|
1588
1646
|
const callId = uuidv72();
|
|
1589
|
-
const
|
|
1647
|
+
const principal = await resolveCallPrincipal(
|
|
1590
1648
|
ctx.resolvePrincipal,
|
|
1591
1649
|
{ extra, toolName, arguments: params },
|
|
1592
1650
|
{ mode: ctx.principalIdMode, tenantId: ctx.tenantId, key: ctx.principalIdHmacKey }
|
|
@@ -1597,13 +1655,15 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1597
1655
|
session_id: sessionId,
|
|
1598
1656
|
consent_token: ctx.consentToken,
|
|
1599
1657
|
agent_runtime: runtime,
|
|
1600
|
-
// The
|
|
1601
|
-
//
|
|
1602
|
-
// (`middleware.py`
|
|
1603
|
-
//
|
|
1604
|
-
//
|
|
1605
|
-
|
|
1606
|
-
//
|
|
1658
|
+
// The five `...common` sites below are the tool-call legs — start, end,
|
|
1659
|
+
// and BOTH failure shapes (SPEC §11.4.3) — plus the proactive
|
|
1660
|
+
// annotation, the same five Python stamps (`middleware.py`
|
|
1661
|
+
// 503/547/586/644/674; it was four until the returned shape landed
|
|
1662
|
+
// there too). `surface_snapshot` is deliberately NOT among them: it
|
|
1663
|
+
// describes the SERVER and is captured outside any call, so there is no
|
|
1664
|
+
// caller to name (register D5).
|
|
1665
|
+
principal,
|
|
1666
|
+
// Same five stamps, same exclusion: a surface_snapshot describes the
|
|
1607
1667
|
// SERVER and is captured outside any call, so it has no caller's
|
|
1608
1668
|
// transport to name any more than it has a caller to name.
|
|
1609
1669
|
transport_observed: observeTransport(extra),
|
|
@@ -1659,14 +1719,46 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1659
1719
|
payload: {
|
|
1660
1720
|
tool_name: toolName,
|
|
1661
1721
|
error_type: err instanceof Error ? err.constructor.name : "Error",
|
|
1662
|
-
error_body:
|
|
1663
|
-
duration_ms: durationMs2
|
|
1722
|
+
error_body: errorBody(ctx, message),
|
|
1723
|
+
duration_ms: durationMs2,
|
|
1724
|
+
// Explicit, though the field is `.optional()` and this is its
|
|
1725
|
+
// absent value. The schema is deliberately not an emitter (see
|
|
1726
|
+
// `ToolCallErrorPayloadSchema`), so if this leg stayed silent the
|
|
1727
|
+
// key would be missing — and Python's `tool_call_error` vector
|
|
1728
|
+
// carries `result: null`, so the cross-SDK key-set check in
|
|
1729
|
+
// `emitterConformance.test.ts` is what would say so. Each of the
|
|
1730
|
+
// two failure shapes declares which it is, the same reason
|
|
1731
|
+
// Python's emitter made the parameter positional-without-default.
|
|
1732
|
+
result: null
|
|
1664
1733
|
}
|
|
1665
1734
|
})
|
|
1666
1735
|
);
|
|
1667
1736
|
throw err;
|
|
1668
1737
|
}
|
|
1669
1738
|
const durationMs = Math.round(performance.now() - startedAt);
|
|
1739
|
+
if (isErrorResult(result)) {
|
|
1740
|
+
await emit(
|
|
1741
|
+
ctx.sink,
|
|
1742
|
+
() => ToolCallErrorEventSchema.parse({
|
|
1743
|
+
...common,
|
|
1744
|
+
call_id: callId,
|
|
1745
|
+
sequence_number: ctx.counter.next(sessionId),
|
|
1746
|
+
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1747
|
+
payload: {
|
|
1748
|
+
tool_name: toolName,
|
|
1749
|
+
error_type: TOOL_ERROR_TYPE,
|
|
1750
|
+
error_body: errorBody(ctx, errorText(result)),
|
|
1751
|
+
duration_ms: durationMs,
|
|
1752
|
+
// The whole envelope. ⚠ On this producer that is the same shape
|
|
1753
|
+
// `tool_call_end.result` records, which is NOT what Python's
|
|
1754
|
+
// contrast says — `ToolCallErrorPayloadSchema` in `events.ts`
|
|
1755
|
+
// holds that claim and names the test that enforces it.
|
|
1756
|
+
result: ctx.scrubber(result)
|
|
1757
|
+
}
|
|
1758
|
+
})
|
|
1759
|
+
);
|
|
1760
|
+
return result;
|
|
1761
|
+
}
|
|
1670
1762
|
await emit(
|
|
1671
1763
|
ctx.sink,
|
|
1672
1764
|
() => ToolCallEndEventSchema.parse({
|
|
@@ -2021,6 +2113,10 @@ export {
|
|
|
2021
2113
|
EventSchema,
|
|
2022
2114
|
EventTypeSchema,
|
|
2023
2115
|
HttpSink,
|
|
2116
|
+
PRINCIPAL_FORM_HASHED,
|
|
2117
|
+
PRINCIPAL_FORM_RAW,
|
|
2118
|
+
PRINCIPAL_SOURCE_ASSERTED,
|
|
2119
|
+
PrincipalWireSchema,
|
|
2024
2120
|
SDK_VERSION,
|
|
2025
2121
|
Scrubber,
|
|
2026
2122
|
StdoutSink,
|
|
@@ -2032,7 +2128,6 @@ export {
|
|
|
2032
2128
|
ToolCallErrorPayloadSchema,
|
|
2033
2129
|
ToolCallStartEventSchema,
|
|
2034
2130
|
ToolCallStartPayloadSchema,
|
|
2035
|
-
VENDOR_HASH_SCHEME,
|
|
2036
2131
|
hashPrincipalId,
|
|
2037
2132
|
identityScrub,
|
|
2038
2133
|
safeWrite,
|