@goodtiming/baton-sdk 0.3.6 → 0.4.0
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 +41 -160
- package/dist/index.cjs +202 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +203 -47
- package/dist/index.d.ts +203 -47
- package/dist/index.js +197 -40
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -37,7 +37,12 @@ __export(index_exports, {
|
|
|
37
37
|
DEPTH_LIMIT: () => DEPTH_LIMIT,
|
|
38
38
|
EventSchema: () => EventSchema,
|
|
39
39
|
EventTypeSchema: () => EventTypeSchema,
|
|
40
|
+
HASH_SCHEME: () => HASH_SCHEME,
|
|
40
41
|
HttpSink: () => HttpSink,
|
|
42
|
+
PRINCIPAL_FORM_HASHED: () => PRINCIPAL_FORM_HASHED,
|
|
43
|
+
PRINCIPAL_FORM_RAW: () => PRINCIPAL_FORM_RAW,
|
|
44
|
+
PRINCIPAL_SOURCE_ASSERTED: () => PRINCIPAL_SOURCE_ASSERTED,
|
|
45
|
+
PrincipalWireSchema: () => PrincipalWireSchema,
|
|
41
46
|
SDK_VERSION: () => SDK_VERSION,
|
|
42
47
|
Scrubber: () => Scrubber,
|
|
43
48
|
StdoutSink: () => StdoutSink,
|
|
@@ -49,7 +54,6 @@ __export(index_exports, {
|
|
|
49
54
|
ToolCallErrorPayloadSchema: () => ToolCallErrorPayloadSchema,
|
|
50
55
|
ToolCallStartEventSchema: () => ToolCallStartEventSchema,
|
|
51
56
|
ToolCallStartPayloadSchema: () => ToolCallStartPayloadSchema,
|
|
52
|
-
VENDOR_HASH_SCHEME: () => VENDOR_HASH_SCHEME,
|
|
53
57
|
hashPrincipalId: () => hashPrincipalId,
|
|
54
58
|
identityScrub: () => identityScrub,
|
|
55
59
|
safeWrite: () => safeWrite,
|
|
@@ -58,7 +62,7 @@ __export(index_exports, {
|
|
|
58
62
|
module.exports = __toCommonJS(index_exports);
|
|
59
63
|
|
|
60
64
|
// src/version.ts
|
|
61
|
-
var SDK_VERSION = "ts-0.
|
|
65
|
+
var SDK_VERSION = "ts-0.4.0";
|
|
62
66
|
|
|
63
67
|
// src/events.ts
|
|
64
68
|
var import_zod = require("zod");
|
|
@@ -80,7 +84,23 @@ var ToolCallErrorPayloadSchema = import_zod.z.object({
|
|
|
80
84
|
tool_name: import_zod.z.string(),
|
|
81
85
|
error_type: import_zod.z.string(),
|
|
82
86
|
error_body: import_zod.z.string(),
|
|
83
|
-
duration_ms: import_zod.z.number().int().nullable().default(null)
|
|
87
|
+
duration_ms: import_zod.z.number().int().nullable().default(null),
|
|
88
|
+
// ⚠ `.optional()`, NOT `.default(null)` like the sibling fields — and it
|
|
89
|
+
// stays that way now that the producer emits the field.
|
|
90
|
+
//
|
|
91
|
+
// A default makes the schema an EMITTER: parsing a payload without the key
|
|
92
|
+
// ADDS it. That is wrong in a schema that also READS — a stored event from
|
|
93
|
+
// before the field existed would come back carrying an invented key, and
|
|
94
|
+
// against the pre-`f1e0280` spec pin, whose `events.schema.json` is
|
|
95
|
+
// `additionalProperties: false`, it reddened three tests saying so: a
|
|
96
|
+
// byte-identical vector round-trip, an ajv validation of TS-built events,
|
|
97
|
+
// and a field-for-field payload comparison.
|
|
98
|
+
//
|
|
99
|
+
// `.optional()` accepts the field when a vector carries it and stays
|
|
100
|
+
// silent when it does not. What makes the EMITTED shape complete is the
|
|
101
|
+
// emitter declaring `result` on both failure legs (`withBaton.ts`),
|
|
102
|
+
// explicitly `null` on a throw — not the schema filling it in.
|
|
103
|
+
result: import_zod.z.unknown().nullable().optional()
|
|
84
104
|
}).strict();
|
|
85
105
|
var AnnotationPayloadSchema = import_zod.z.object({
|
|
86
106
|
intent: import_zod.z.string().nullable().default(null),
|
|
@@ -101,6 +121,11 @@ var SurfaceSnapshotPayloadSchema = import_zod.z.object({
|
|
|
101
121
|
seam_augmentations: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).default({})
|
|
102
122
|
}).strict();
|
|
103
123
|
var DEFAULT_CONSENT_TOKEN = "customer-consented";
|
|
124
|
+
var PrincipalWireSchema = import_zod.z.object({
|
|
125
|
+
id: import_zod.z.string(),
|
|
126
|
+
source: import_zod.z.string(),
|
|
127
|
+
form: import_zod.z.string()
|
|
128
|
+
}).strict();
|
|
104
129
|
var envelopeShape = {
|
|
105
130
|
event_id: import_zod.z.uuid().default(() => (0, import_uuid.v7)()),
|
|
106
131
|
tenant_id: import_zod.z.string(),
|
|
@@ -111,7 +136,8 @@ var envelopeShape = {
|
|
|
111
136
|
consent_token: import_zod.z.string(),
|
|
112
137
|
sdk_version: import_zod.z.string().default(SDK_VERSION),
|
|
113
138
|
agent_runtime: import_zod.z.string().default("unknown"),
|
|
114
|
-
|
|
139
|
+
principal: PrincipalWireSchema.nullable().default(null),
|
|
140
|
+
transport_observed: import_zod.z.string().nullable().default(null),
|
|
115
141
|
call_id: import_zod.z.string().nullable().default(null),
|
|
116
142
|
runtime_meta: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).nullable().default(null)
|
|
117
143
|
};
|
|
@@ -452,6 +478,51 @@ var import_uuid2 = require("uuid");
|
|
|
452
478
|
// src/integrations/mcp/annotation.ts
|
|
453
479
|
var import_zod2 = require("zod");
|
|
454
480
|
|
|
481
|
+
// src/metaCoordinates.ts
|
|
482
|
+
var COORD_FIELD_NAMES = /* @__PURE__ */ new Set(["latitude", "longitude"]);
|
|
483
|
+
var DECIMAL_STRING = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/;
|
|
484
|
+
function isPlainObject2(value) {
|
|
485
|
+
if (typeof value !== "object" || value === null) return false;
|
|
486
|
+
const proto = Object.getPrototypeOf(value);
|
|
487
|
+
return proto === Object.prototype || proto === null;
|
|
488
|
+
}
|
|
489
|
+
function toOneDecimal(value) {
|
|
490
|
+
const magnitude = Math.abs(value);
|
|
491
|
+
if (Number.isInteger(magnitude * 4) && !Number.isInteger(magnitude * 2)) {
|
|
492
|
+
const tenths = Math.floor(magnitude * 10);
|
|
493
|
+
const even = tenths % 2 === 0 ? tenths : tenths + 1;
|
|
494
|
+
return (Math.sign(value) * even / 10).toFixed(1);
|
|
495
|
+
}
|
|
496
|
+
return value.toFixed(1);
|
|
497
|
+
}
|
|
498
|
+
function roundCoordinate(value) {
|
|
499
|
+
if (typeof value === "number") {
|
|
500
|
+
return Number.isFinite(value) && !Number.isInteger(value) ? Number(toOneDecimal(value)) : void 0;
|
|
501
|
+
}
|
|
502
|
+
if (typeof value === "string" && DECIMAL_STRING.test(value)) {
|
|
503
|
+
return toOneDecimal(Number(value));
|
|
504
|
+
}
|
|
505
|
+
return void 0;
|
|
506
|
+
}
|
|
507
|
+
function walk(value, depth) {
|
|
508
|
+
if (depth >= DEPTH_LIMIT) return value;
|
|
509
|
+
if (isPlainObject2(value)) {
|
|
510
|
+
const out = {};
|
|
511
|
+
for (const [k, v] of Object.entries(value)) {
|
|
512
|
+
const rounded = COORD_FIELD_NAMES.has(k.toLowerCase()) && depth + 1 < DEPTH_LIMIT ? roundCoordinate(v) : void 0;
|
|
513
|
+
out[k] = rounded ?? walk(v, depth + 1);
|
|
514
|
+
}
|
|
515
|
+
return out;
|
|
516
|
+
}
|
|
517
|
+
if (Array.isArray(value)) {
|
|
518
|
+
return value.map((item) => walk(item, depth + 1));
|
|
519
|
+
}
|
|
520
|
+
return value;
|
|
521
|
+
}
|
|
522
|
+
function roundMetaCoordinates(meta) {
|
|
523
|
+
return walk(meta, 0);
|
|
524
|
+
}
|
|
525
|
+
|
|
455
526
|
// src/identity.ts
|
|
456
527
|
var import_node_crypto = require("crypto");
|
|
457
528
|
|
|
@@ -462,10 +533,20 @@ function capCodePoints(value, max) {
|
|
|
462
533
|
}
|
|
463
534
|
|
|
464
535
|
// src/identity.ts
|
|
465
|
-
var
|
|
536
|
+
var HASH_SCHEME = "h1";
|
|
537
|
+
var PRINCIPAL_SOURCE_ASSERTED = "asserted";
|
|
538
|
+
var PRINCIPAL_FORM_HASHED = "hashed";
|
|
539
|
+
var PRINCIPAL_FORM_RAW = "raw";
|
|
466
540
|
var RAW_PRINCIPAL_ID_MAX_LEN = 128;
|
|
467
541
|
var PRINCIPAL_ID_MODE_HASHED = "hashed";
|
|
468
542
|
var PRINCIPAL_ID_MODE_RAW = "raw";
|
|
543
|
+
var FORM_BY_MODE = {
|
|
544
|
+
[PRINCIPAL_ID_MODE_HASHED]: PRINCIPAL_FORM_HASHED,
|
|
545
|
+
[PRINCIPAL_ID_MODE_RAW]: PRINCIPAL_FORM_RAW
|
|
546
|
+
};
|
|
547
|
+
var PRINCIPAL_ID_MODES = Object.keys(
|
|
548
|
+
FORM_BY_MODE
|
|
549
|
+
);
|
|
469
550
|
var PYTHON_WHITESPACE = "\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
470
551
|
var PYTHON_STRIP = new RegExp(`^[${PYTHON_WHITESPACE}]+|[${PYTHON_WHITESPACE}]+$`, "gu");
|
|
471
552
|
var LONE_SURROGATE = /\p{Surrogate}/u;
|
|
@@ -476,7 +557,7 @@ function pythonStrip(value) {
|
|
|
476
557
|
return value.replace(PYTHON_STRIP, "");
|
|
477
558
|
}
|
|
478
559
|
function hashPrincipalId(rawPrincipal, options) {
|
|
479
|
-
const { tenantId, key, issuer = null, scheme } = options;
|
|
560
|
+
const { tenantId, key, issuer = null, scheme = HASH_SCHEME } = options;
|
|
480
561
|
let message = `${tenantId}\0${canonicalize(rawPrincipal)}`;
|
|
481
562
|
if (issuer !== null) {
|
|
482
563
|
message += `\0${canonicalize(issuer)}`;
|
|
@@ -495,21 +576,19 @@ function normalizePrincipal(result) {
|
|
|
495
576
|
const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
|
|
496
577
|
return { principalId: candidate.principalId, issuer };
|
|
497
578
|
}
|
|
498
|
-
function
|
|
579
|
+
function principalFor(principal, options) {
|
|
580
|
+
let id;
|
|
499
581
|
if (options.mode === PRINCIPAL_ID_MODE_RAW) {
|
|
500
|
-
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
// coming to this SDK.
|
|
511
|
-
scheme: VENDOR_HASH_SCHEME
|
|
512
|
-
});
|
|
582
|
+
id = capCodePoints(principal.principalId, RAW_PRINCIPAL_ID_MAX_LEN);
|
|
583
|
+
} else {
|
|
584
|
+
if (options.key === null || options.key === void 0) return null;
|
|
585
|
+
id = hashPrincipalId(principal.principalId, {
|
|
586
|
+
tenantId: options.tenantId,
|
|
587
|
+
key: options.key,
|
|
588
|
+
issuer: principal.issuer ?? null
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
return { id, source: PRINCIPAL_SOURCE_ASSERTED, form: FORM_BY_MODE[options.mode] };
|
|
513
592
|
}
|
|
514
593
|
|
|
515
594
|
// src/integrations/mcp/llmText.ts
|
|
@@ -680,6 +759,14 @@ function extraEnvelope(extra) {
|
|
|
680
759
|
const envelope = extra.mcpReq?.envelope;
|
|
681
760
|
return envelope ?? null;
|
|
682
761
|
}
|
|
762
|
+
function observeTransport(extra) {
|
|
763
|
+
try {
|
|
764
|
+
const carrier = extra.http?.req ?? extra.requestInfo;
|
|
765
|
+
return carrier ? "http" : "no-http-request";
|
|
766
|
+
} catch {
|
|
767
|
+
return "read-failed";
|
|
768
|
+
}
|
|
769
|
+
}
|
|
683
770
|
function extraHeaders(extra) {
|
|
684
771
|
const fromV2 = extra.http?.req?.headers;
|
|
685
772
|
if (fromV2 && typeof fromV2.get === "function") return fromV2;
|
|
@@ -716,7 +803,7 @@ function warnIfIdentityCannotResolve(config) {
|
|
|
716
803
|
if (typeof process === "undefined" || typeof process.emitWarning !== "function") return;
|
|
717
804
|
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. " : "";
|
|
718
805
|
process.emitWarning(
|
|
719
|
-
`baton: resolvePrincipal is configured but no
|
|
806
|
+
`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.`
|
|
720
807
|
);
|
|
721
808
|
}
|
|
722
809
|
var warnedPreRenameShape = false;
|
|
@@ -725,10 +812,10 @@ function warnIfPreRenameShape(result) {
|
|
|
725
812
|
if (warnedPreRenameShape || typeof userId !== "string") return;
|
|
726
813
|
warnedPreRenameShape = true;
|
|
727
814
|
warn(
|
|
728
|
-
"baton: resolvePrincipal returned { userId }, which was renamed to { principalId } in 0.3.5, so
|
|
815
|
+
"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."
|
|
729
816
|
);
|
|
730
817
|
}
|
|
731
|
-
async function
|
|
818
|
+
async function resolveCallPrincipal(hook, call, options) {
|
|
732
819
|
if (hook === void 0) return null;
|
|
733
820
|
let result;
|
|
734
821
|
try {
|
|
@@ -743,7 +830,7 @@ async function resolveCallPrincipalId(hook, call, options) {
|
|
|
743
830
|
return null;
|
|
744
831
|
}
|
|
745
832
|
try {
|
|
746
|
-
return
|
|
833
|
+
return principalFor(principal, {
|
|
747
834
|
mode: options.mode,
|
|
748
835
|
tenantId: options.tenantId,
|
|
749
836
|
key: options.key
|
|
@@ -837,12 +924,12 @@ function registerAnnotationTool(server, options) {
|
|
|
837
924
|
server,
|
|
838
925
|
scrubber: options.scrubber
|
|
839
926
|
}) ?? UNKNOWN_AGENT_RUNTIME;
|
|
840
|
-
const scrubbedMeta = meta ? options.scrubber(meta) : null;
|
|
927
|
+
const scrubbedMeta = meta ? options.scrubber(roundMetaCoordinates(meta)) : null;
|
|
841
928
|
const sessionId = await resolveSessionId(options.fallbackSessionId, extra);
|
|
842
929
|
if (args.signal_type === void 0) {
|
|
843
930
|
options.tracker?.mark(sessionId);
|
|
844
931
|
}
|
|
845
|
-
const
|
|
932
|
+
const principal = await resolveCallPrincipal(
|
|
846
933
|
options.resolvePrincipal,
|
|
847
934
|
{ extra, toolName: name, arguments: args },
|
|
848
935
|
{
|
|
@@ -861,7 +948,8 @@ function registerAnnotationTool(server, options) {
|
|
|
861
948
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
862
949
|
consent_token: options.consentToken,
|
|
863
950
|
agent_runtime: runtime,
|
|
864
|
-
|
|
951
|
+
principal,
|
|
952
|
+
transport_observed: observeTransport(extra),
|
|
865
953
|
runtime_meta: scrubbedMeta,
|
|
866
954
|
payload: {
|
|
867
955
|
// Agent-facing names -> wire keys, as with `overall_task`
|
|
@@ -1099,7 +1187,7 @@ function sealKey(dsn) {
|
|
|
1099
1187
|
|
|
1100
1188
|
// src/integrations/mcp/config.ts
|
|
1101
1189
|
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
|
|
1102
|
-
var
|
|
1190
|
+
var PRINCIPAL_ID_MODE_SET = new Set(PRINCIPAL_ID_MODES);
|
|
1103
1191
|
function resolveTenantId(explicit, vendorId) {
|
|
1104
1192
|
if (explicit) return explicit;
|
|
1105
1193
|
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_TENANT_ID : void 0;
|
|
@@ -1215,9 +1303,9 @@ function validateBatonConfig(config) {
|
|
|
1215
1303
|
if (config.principalIdHmacKey !== void 0 && typeof config.principalIdHmacKey !== "string" && !(config.principalIdHmacKey instanceof Uint8Array)) {
|
|
1216
1304
|
throw new Error("BatonConfig.principalIdHmacKey must be a string or a Uint8Array.");
|
|
1217
1305
|
}
|
|
1218
|
-
if (config.principalIdMode !== void 0 && !
|
|
1306
|
+
if (config.principalIdMode !== void 0 && !PRINCIPAL_ID_MODE_SET.has(config.principalIdMode)) {
|
|
1219
1307
|
throw new Error(
|
|
1220
|
-
`BatonConfig.principalIdMode ${JSON.stringify(config.principalIdMode)} must be one of ${JSON.stringify([...
|
|
1308
|
+
`BatonConfig.principalIdMode ${JSON.stringify(config.principalIdMode)} must be one of ${JSON.stringify([...PRINCIPAL_ID_MODE_SET].sort())}.`
|
|
1221
1309
|
);
|
|
1222
1310
|
}
|
|
1223
1311
|
}
|
|
@@ -1254,6 +1342,34 @@ var DisabledSink = class {
|
|
|
1254
1342
|
}
|
|
1255
1343
|
};
|
|
1256
1344
|
|
|
1345
|
+
// src/integrations/mcp/errorResult.ts
|
|
1346
|
+
var TOOL_ERROR_TYPE = "tool_error";
|
|
1347
|
+
var ERROR_BODY_MAX_CODE_POINTS = 2e3;
|
|
1348
|
+
function isErrorResult(value) {
|
|
1349
|
+
try {
|
|
1350
|
+
if (typeof value !== "object" || value === null) return false;
|
|
1351
|
+
return Boolean(value.isError);
|
|
1352
|
+
} catch {
|
|
1353
|
+
return false;
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
function errorText(value) {
|
|
1357
|
+
const parts = [];
|
|
1358
|
+
try {
|
|
1359
|
+
const content = value?.content;
|
|
1360
|
+
if (!Array.isArray(content)) return "";
|
|
1361
|
+
for (const part of content) {
|
|
1362
|
+
const text = part?.text;
|
|
1363
|
+
if (typeof text !== "string") continue;
|
|
1364
|
+
const trimmed = text.trim();
|
|
1365
|
+
if (trimmed) parts.push(trimmed);
|
|
1366
|
+
}
|
|
1367
|
+
} catch {
|
|
1368
|
+
return "";
|
|
1369
|
+
}
|
|
1370
|
+
return parts.join("\n");
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1257
1373
|
// src/integrations/mcp/handle.ts
|
|
1258
1374
|
var BatonHandle = class {
|
|
1259
1375
|
sink;
|
|
@@ -1558,6 +1674,9 @@ var SurfaceState = class {
|
|
|
1558
1674
|
);
|
|
1559
1675
|
}
|
|
1560
1676
|
};
|
|
1677
|
+
function errorBody(ctx, text) {
|
|
1678
|
+
return capCodePoints(String(ctx.scrubber(text)), ERROR_BODY_MAX_CODE_POINTS);
|
|
1679
|
+
}
|
|
1561
1680
|
function batonWrap(nameRef, original, ctx) {
|
|
1562
1681
|
return async (...callArgs) => {
|
|
1563
1682
|
const toolName = nameRef.current;
|
|
@@ -1575,7 +1694,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1575
1694
|
server: ctx.server,
|
|
1576
1695
|
scrubber: ctx.scrubber
|
|
1577
1696
|
}) ?? UNKNOWN_AGENT_RUNTIME;
|
|
1578
|
-
const scrubbedMeta = meta ? ctx.scrubber(meta) : null;
|
|
1697
|
+
const scrubbedMeta = meta ? ctx.scrubber(roundMetaCoordinates(meta)) : null;
|
|
1579
1698
|
const sessionId = await resolveSessionId(ctx.fallbackSessionId, extra);
|
|
1580
1699
|
const dispositions = ctx.paramRegistry.get(toolName);
|
|
1581
1700
|
const rawIntent = extractGoalParam(params, USER_GOAL_PARAM_NAME, toolName, dispositions);
|
|
@@ -1590,7 +1709,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1590
1709
|
const scrubbedExpected = rawExpected !== null ? ctx.scrubber(rawExpected) : null;
|
|
1591
1710
|
const scrubbedWorkflow = rawWorkflow !== null ? ctx.scrubber(rawWorkflow) : null;
|
|
1592
1711
|
const callId = (0, import_uuid2.v7)();
|
|
1593
|
-
const
|
|
1712
|
+
const principal = await resolveCallPrincipal(
|
|
1594
1713
|
ctx.resolvePrincipal,
|
|
1595
1714
|
{ extra, toolName, arguments: params },
|
|
1596
1715
|
{ mode: ctx.principalIdMode, tenantId: ctx.tenantId, key: ctx.principalIdHmacKey }
|
|
@@ -1601,12 +1720,18 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1601
1720
|
session_id: sessionId,
|
|
1602
1721
|
consent_token: ctx.consentToken,
|
|
1603
1722
|
agent_runtime: runtime,
|
|
1604
|
-
// The
|
|
1605
|
-
//
|
|
1606
|
-
// (`middleware.py`
|
|
1607
|
-
//
|
|
1608
|
-
//
|
|
1609
|
-
|
|
1723
|
+
// The five `...common` sites below are the tool-call legs — start, end,
|
|
1724
|
+
// and BOTH failure shapes (SPEC §11.4.3) — plus the proactive
|
|
1725
|
+
// annotation, the same five Python stamps (`middleware.py`
|
|
1726
|
+
// 503/547/586/644/674; it was four until the returned shape landed
|
|
1727
|
+
// there too). `surface_snapshot` is deliberately NOT among them: it
|
|
1728
|
+
// describes the SERVER and is captured outside any call, so there is no
|
|
1729
|
+
// caller to name (register D5).
|
|
1730
|
+
principal,
|
|
1731
|
+
// Same five stamps, same exclusion: a surface_snapshot describes the
|
|
1732
|
+
// SERVER and is captured outside any call, so it has no caller's
|
|
1733
|
+
// transport to name any more than it has a caller to name.
|
|
1734
|
+
transport_observed: observeTransport(extra),
|
|
1610
1735
|
runtime_meta: scrubbedMeta
|
|
1611
1736
|
};
|
|
1612
1737
|
if (scrubbedIntent !== null && ctx.tracker.claim(sessionId)) {
|
|
@@ -1659,14 +1784,46 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1659
1784
|
payload: {
|
|
1660
1785
|
tool_name: toolName,
|
|
1661
1786
|
error_type: err instanceof Error ? err.constructor.name : "Error",
|
|
1662
|
-
error_body:
|
|
1663
|
-
duration_ms: durationMs2
|
|
1787
|
+
error_body: errorBody(ctx, message),
|
|
1788
|
+
duration_ms: durationMs2,
|
|
1789
|
+
// Explicit, though the field is `.optional()` and this is its
|
|
1790
|
+
// absent value. The schema is deliberately not an emitter (see
|
|
1791
|
+
// `ToolCallErrorPayloadSchema`), so if this leg stayed silent the
|
|
1792
|
+
// key would be missing — and Python's `tool_call_error` vector
|
|
1793
|
+
// carries `result: null`, so the cross-SDK key-set check in
|
|
1794
|
+
// `emitterConformance.test.ts` is what would say so. Each of the
|
|
1795
|
+
// two failure shapes declares which it is, the same reason
|
|
1796
|
+
// Python's emitter made the parameter positional-without-default.
|
|
1797
|
+
result: null
|
|
1664
1798
|
}
|
|
1665
1799
|
})
|
|
1666
1800
|
);
|
|
1667
1801
|
throw err;
|
|
1668
1802
|
}
|
|
1669
1803
|
const durationMs = Math.round(performance.now() - startedAt);
|
|
1804
|
+
if (isErrorResult(result)) {
|
|
1805
|
+
await emit(
|
|
1806
|
+
ctx.sink,
|
|
1807
|
+
() => ToolCallErrorEventSchema.parse({
|
|
1808
|
+
...common,
|
|
1809
|
+
call_id: callId,
|
|
1810
|
+
sequence_number: ctx.counter.next(sessionId),
|
|
1811
|
+
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1812
|
+
payload: {
|
|
1813
|
+
tool_name: toolName,
|
|
1814
|
+
error_type: TOOL_ERROR_TYPE,
|
|
1815
|
+
error_body: errorBody(ctx, errorText(result)),
|
|
1816
|
+
duration_ms: durationMs,
|
|
1817
|
+
// The whole envelope. ⚠ On this producer that is the same shape
|
|
1818
|
+
// `tool_call_end.result` records, which is NOT what Python's
|
|
1819
|
+
// contrast says — `ToolCallErrorPayloadSchema` in `events.ts`
|
|
1820
|
+
// holds that claim and names the test that enforces it.
|
|
1821
|
+
result: ctx.scrubber(result)
|
|
1822
|
+
}
|
|
1823
|
+
})
|
|
1824
|
+
);
|
|
1825
|
+
return result;
|
|
1826
|
+
}
|
|
1670
1827
|
await emit(
|
|
1671
1828
|
ctx.sink,
|
|
1672
1829
|
() => ToolCallEndEventSchema.parse({
|
|
@@ -2021,7 +2178,12 @@ function withBaton(server, supplied = {}) {
|
|
|
2021
2178
|
DEPTH_LIMIT,
|
|
2022
2179
|
EventSchema,
|
|
2023
2180
|
EventTypeSchema,
|
|
2181
|
+
HASH_SCHEME,
|
|
2024
2182
|
HttpSink,
|
|
2183
|
+
PRINCIPAL_FORM_HASHED,
|
|
2184
|
+
PRINCIPAL_FORM_RAW,
|
|
2185
|
+
PRINCIPAL_SOURCE_ASSERTED,
|
|
2186
|
+
PrincipalWireSchema,
|
|
2025
2187
|
SDK_VERSION,
|
|
2026
2188
|
Scrubber,
|
|
2027
2189
|
StdoutSink,
|
|
@@ -2033,7 +2195,6 @@ function withBaton(server, supplied = {}) {
|
|
|
2033
2195
|
ToolCallErrorPayloadSchema,
|
|
2034
2196
|
ToolCallStartEventSchema,
|
|
2035
2197
|
ToolCallStartPayloadSchema,
|
|
2036
|
-
VENDOR_HASH_SCHEME,
|
|
2037
2198
|
hashPrincipalId,
|
|
2038
2199
|
identityScrub,
|
|
2039
2200
|
safeWrite,
|