@goodtiming/baton-sdk 0.3.1 → 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 +299 -53
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +146 -15
- package/dist/index.d.ts +146 -15
- package/dist/index.js +295 -51
- 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.3";
|
|
3
3
|
|
|
4
4
|
// src/events.ts
|
|
5
5
|
import { z } from "zod";
|
|
@@ -53,6 +53,7 @@ var envelopeShape = {
|
|
|
53
53
|
sdk_version: z.string().default(SDK_VERSION),
|
|
54
54
|
agent_runtime: z.string().default("unknown"),
|
|
55
55
|
user_id: z.string().nullable().default(null),
|
|
56
|
+
call_id: z.string().nullable().default(null),
|
|
56
57
|
runtime_meta: z.record(z.string(), z.unknown()).nullable().default(null)
|
|
57
58
|
};
|
|
58
59
|
var EventTypeSchema = z.enum([
|
|
@@ -405,6 +406,134 @@ async function emit(sink, build) {
|
|
|
405
406
|
await safeWrite(sink, event);
|
|
406
407
|
}
|
|
407
408
|
|
|
409
|
+
// src/identity.ts
|
|
410
|
+
import { createHmac } from "crypto";
|
|
411
|
+
|
|
412
|
+
// src/_text.ts
|
|
413
|
+
function capCodePoints(value, max) {
|
|
414
|
+
if (value.length <= max) return value;
|
|
415
|
+
return [...value].slice(0, max).join("");
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// src/identity.ts
|
|
419
|
+
var VENDOR_HASH_SCHEME = "v1";
|
|
420
|
+
var RAW_USER_ID_MAX_LEN = 128;
|
|
421
|
+
var USER_ID_MODE_HASHED = "hashed";
|
|
422
|
+
var USER_ID_MODE_RAW = "raw";
|
|
423
|
+
var PYTHON_WHITESPACE = "\\t\\n\\v\\f\\r\\x1c-\\x1f \\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
424
|
+
var PYTHON_STRIP = new RegExp(`^[${PYTHON_WHITESPACE}]+|[${PYTHON_WHITESPACE}]+$`, "gu");
|
|
425
|
+
var LONE_SURROGATE = /\p{Surrogate}/u;
|
|
426
|
+
function canonicalize(rawPrincipal) {
|
|
427
|
+
return rawPrincipal.normalize("NFC").replace(PYTHON_STRIP, "").toLowerCase();
|
|
428
|
+
}
|
|
429
|
+
function hashUserId(rawPrincipal, options) {
|
|
430
|
+
const { tenantId, key, issuer = null, scheme } = options;
|
|
431
|
+
let message = `${tenantId}\0${canonicalize(rawPrincipal)}`;
|
|
432
|
+
if (issuer !== null) {
|
|
433
|
+
message += `\0${canonicalize(issuer)}`;
|
|
434
|
+
}
|
|
435
|
+
const keyBytes = typeof key === "string" ? Buffer.from(key, "utf8") : key;
|
|
436
|
+
const digest = createHmac("sha256", keyBytes).update(message, "utf8").digest("hex");
|
|
437
|
+
return `${scheme}:${digest}`;
|
|
438
|
+
}
|
|
439
|
+
function normalizePrincipal(result) {
|
|
440
|
+
if (result === null || typeof result !== "object") return null;
|
|
441
|
+
const candidate = result;
|
|
442
|
+
if (typeof candidate.userId !== "string") return null;
|
|
443
|
+
if (LONE_SURROGATE.test(candidate.userId)) return null;
|
|
444
|
+
if (canonicalize(candidate.userId) === "") return null;
|
|
445
|
+
const issuerRaw = typeof candidate.issuer === "string" && candidate.issuer ? candidate.issuer : null;
|
|
446
|
+
const issuer = issuerRaw !== null && !LONE_SURROGATE.test(issuerRaw) ? issuerRaw : null;
|
|
447
|
+
return { userId: candidate.userId, issuer };
|
|
448
|
+
}
|
|
449
|
+
function userIdForPrincipal(principal, options) {
|
|
450
|
+
if (options.mode === USER_ID_MODE_RAW) {
|
|
451
|
+
return capCodePoints(principal.userId, RAW_USER_ID_MAX_LEN);
|
|
452
|
+
}
|
|
453
|
+
if (options.key === null || options.key === void 0) return null;
|
|
454
|
+
return hashUserId(principal.userId, {
|
|
455
|
+
tenantId: options.tenantId,
|
|
456
|
+
key: options.key,
|
|
457
|
+
issuer: principal.issuer ?? null,
|
|
458
|
+
// Unconditional: this arm has no attested rung, so every principal it
|
|
459
|
+
// hashes is vendor-ASSERTED. A `scheme` option here would be an argument
|
|
460
|
+
// no caller can vary — configurability for a rung the design says is not
|
|
461
|
+
// coming to this SDK.
|
|
462
|
+
scheme: VENDOR_HASH_SCHEME
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// src/integrations/mcp/mcpTypes.ts
|
|
467
|
+
function extraMeta(extra) {
|
|
468
|
+
const direct = extra._meta;
|
|
469
|
+
if (direct) return direct;
|
|
470
|
+
const nested = extra.mcpReq?._meta;
|
|
471
|
+
return nested ?? null;
|
|
472
|
+
}
|
|
473
|
+
function extraEnvelope(extra) {
|
|
474
|
+
const envelope = extra.mcpReq?.envelope;
|
|
475
|
+
return envelope ?? null;
|
|
476
|
+
}
|
|
477
|
+
function extraHeaders(extra) {
|
|
478
|
+
const fromV2 = extra.http?.req?.headers;
|
|
479
|
+
if (fromV2 && typeof fromV2.get === "function") return fromV2;
|
|
480
|
+
const fromV1 = extra.requestInfo?.headers;
|
|
481
|
+
if (fromV1 && typeof fromV1 === "object") {
|
|
482
|
+
const headers = new Headers();
|
|
483
|
+
for (const [name, value] of Object.entries(fromV1)) {
|
|
484
|
+
for (const item of Array.isArray(value) ? value : [value]) {
|
|
485
|
+
if (typeof item !== "string") continue;
|
|
486
|
+
try {
|
|
487
|
+
headers.append(name, item);
|
|
488
|
+
} catch {
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
return headers;
|
|
493
|
+
}
|
|
494
|
+
return null;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// src/integrations/mcp/userResolution.ts
|
|
498
|
+
function buildUserResolutionContext(extra, toolName, args) {
|
|
499
|
+
return {
|
|
500
|
+
headers: extraHeaders(extra),
|
|
501
|
+
meta: extraMeta(extra),
|
|
502
|
+
toolName,
|
|
503
|
+
arguments: args
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
function warnIfIdentityCannotResolve(config) {
|
|
507
|
+
if (config.resolveUser === void 0) return;
|
|
508
|
+
if (config.userIdMode !== USER_ID_MODE_HASHED) return;
|
|
509
|
+
if (config.userIdHmacKey !== void 0) return;
|
|
510
|
+
if (typeof process === "undefined" || typeof process.emitWarning !== "function") return;
|
|
511
|
+
process.emitWarning(
|
|
512
|
+
'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.'
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
async function resolveCallUserId(hook, call, options) {
|
|
516
|
+
if (hook === void 0) return null;
|
|
517
|
+
let result;
|
|
518
|
+
try {
|
|
519
|
+
const context = buildUserResolutionContext(call.extra, call.toolName, call.arguments);
|
|
520
|
+
result = await hook(context);
|
|
521
|
+
} catch {
|
|
522
|
+
return null;
|
|
523
|
+
}
|
|
524
|
+
const principal = normalizePrincipal(result);
|
|
525
|
+
if (principal === null) return null;
|
|
526
|
+
try {
|
|
527
|
+
return userIdForPrincipal(principal, {
|
|
528
|
+
mode: options.mode,
|
|
529
|
+
tenantId: options.tenantId,
|
|
530
|
+
key: options.key
|
|
531
|
+
});
|
|
532
|
+
} catch {
|
|
533
|
+
return null;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
408
537
|
// src/integrations/mcp/llmText.ts
|
|
409
538
|
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.
|
|
410
539
|
|
|
@@ -466,32 +595,60 @@ function buildExpectedResultParamDescription() {
|
|
|
466
595
|
return EXPECTED_RESULT_PARAM_DESCRIPTION;
|
|
467
596
|
}
|
|
468
597
|
|
|
469
|
-
// src/integrations/mcp/mcpTypes.ts
|
|
470
|
-
function extraMeta(extra) {
|
|
471
|
-
const direct = extra._meta;
|
|
472
|
-
if (direct) return direct;
|
|
473
|
-
const nested = extra.mcpReq?._meta;
|
|
474
|
-
return nested ?? null;
|
|
475
|
-
}
|
|
476
|
-
|
|
477
598
|
// src/integrations/mcp/runtimeAdapter.ts
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
599
|
+
var CLIENT_INFO_META_KEY = "io.modelcontextprotocol/clientInfo";
|
|
600
|
+
var CLIENT_NAME_MAX_LEN = 128;
|
|
601
|
+
var UNKNOWN_AGENT_RUNTIME = "unknown";
|
|
602
|
+
function clean(name, scrubber) {
|
|
603
|
+
if (typeof name !== "string" || !name) return null;
|
|
604
|
+
let cleaned = name;
|
|
605
|
+
if (scrubber) {
|
|
606
|
+
const scrubbed = scrubber(cleaned);
|
|
607
|
+
if (typeof scrubbed !== "string") return null;
|
|
608
|
+
cleaned = scrubbed;
|
|
609
|
+
}
|
|
610
|
+
if (!cleaned) return null;
|
|
611
|
+
return capCodePoints(cleaned, CLIENT_NAME_MAX_LEN);
|
|
612
|
+
}
|
|
613
|
+
function declaredOnRequest(meta, envelope) {
|
|
614
|
+
for (const source of [envelope, meta]) {
|
|
615
|
+
if (!source) continue;
|
|
616
|
+
const info = source[CLIENT_INFO_META_KEY];
|
|
617
|
+
if (info && typeof info === "object") {
|
|
618
|
+
const name = info.name;
|
|
619
|
+
if (name !== void 0) return name;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return void 0;
|
|
623
|
+
}
|
|
624
|
+
function declaredOnConnection(options) {
|
|
625
|
+
try {
|
|
626
|
+
const server = options.server;
|
|
627
|
+
if (!server) return void 0;
|
|
628
|
+
const inner = server.server;
|
|
629
|
+
const holder = inner ?? server;
|
|
630
|
+
if (typeof holder.getClientVersion !== "function") return void 0;
|
|
631
|
+
return holder.getClientVersion()?.name;
|
|
632
|
+
} catch {
|
|
633
|
+
return void 0;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
function detectAgentRuntime(meta, options = {}) {
|
|
637
|
+
const { envelope, scrubber } = options;
|
|
638
|
+
const onRequest = clean(declaredOnRequest(meta, envelope), scrubber);
|
|
639
|
+
if (onRequest !== null) return onRequest;
|
|
640
|
+
const onConnection = clean(declaredOnConnection(options), scrubber);
|
|
641
|
+
if (onConnection !== null) return onConnection;
|
|
642
|
+
if (meta) {
|
|
643
|
+
for (const key of Object.keys(meta)) {
|
|
644
|
+
if (key.startsWith("claudecode/")) return "claude-code";
|
|
645
|
+
}
|
|
482
646
|
}
|
|
483
647
|
return null;
|
|
484
648
|
}
|
|
485
649
|
|
|
486
650
|
// src/integrations/mcp/sessionResolution.ts
|
|
487
|
-
async function resolveSessionId(
|
|
488
|
-
if (hook) {
|
|
489
|
-
try {
|
|
490
|
-
const hookResult = await hook({ meta, toolName, arguments: args });
|
|
491
|
-
if (typeof hookResult === "string" && hookResult) return hookResult;
|
|
492
|
-
} catch {
|
|
493
|
-
}
|
|
494
|
-
}
|
|
651
|
+
async function resolveSessionId(fallbackSessionId, extra) {
|
|
495
652
|
if (typeof extra.sessionId === "string" && extra.sessionId) return extra.sessionId;
|
|
496
653
|
return fallbackSessionId;
|
|
497
654
|
}
|
|
@@ -527,19 +684,25 @@ function registerAnnotationTool(server, options) {
|
|
|
527
684
|
},
|
|
528
685
|
async (args, extra) => {
|
|
529
686
|
const meta = extraMeta(extra);
|
|
530
|
-
const runtime = detectAgentRuntime(meta
|
|
687
|
+
const runtime = detectAgentRuntime(meta, {
|
|
688
|
+
envelope: extraEnvelope(extra),
|
|
689
|
+
server,
|
|
690
|
+
scrubber: options.scrubber
|
|
691
|
+
}) ?? UNKNOWN_AGENT_RUNTIME;
|
|
531
692
|
const scrubbedMeta = meta ? options.scrubber(meta) : null;
|
|
532
|
-
const sessionId = await resolveSessionId(
|
|
533
|
-
options.resolveSessionId,
|
|
534
|
-
options.fallbackSessionId,
|
|
535
|
-
extra,
|
|
536
|
-
meta,
|
|
537
|
-
name,
|
|
538
|
-
args
|
|
539
|
-
);
|
|
693
|
+
const sessionId = await resolveSessionId(options.fallbackSessionId, extra);
|
|
540
694
|
if (args.signal_type === void 0) {
|
|
541
695
|
options.tracker?.mark(sessionId);
|
|
542
696
|
}
|
|
697
|
+
const userId = await resolveCallUserId(
|
|
698
|
+
options.resolveUser,
|
|
699
|
+
{ extra, toolName: name, arguments: args },
|
|
700
|
+
{
|
|
701
|
+
mode: options.userIdMode,
|
|
702
|
+
tenantId: options.tenantId,
|
|
703
|
+
key: options.userIdHmacKey
|
|
704
|
+
}
|
|
705
|
+
);
|
|
543
706
|
await emit(
|
|
544
707
|
options.sink,
|
|
545
708
|
() => AnnotationEventSchema.parse({
|
|
@@ -550,6 +713,7 @@ function registerAnnotationTool(server, options) {
|
|
|
550
713
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
551
714
|
consent_token: options.consentToken,
|
|
552
715
|
agent_runtime: runtime,
|
|
716
|
+
user_id: userId,
|
|
553
717
|
runtime_meta: scrubbedMeta,
|
|
554
718
|
payload: {
|
|
555
719
|
// Agent-facing names -> wire keys, as with `overall_task`
|
|
@@ -583,7 +747,7 @@ function registerAnnotationTool(server, options) {
|
|
|
583
747
|
|
|
584
748
|
// src/dsn.ts
|
|
585
749
|
var VENDOR_ID_PATTERN = /^[a-zA-Z0-9_-]{1,48}$/;
|
|
586
|
-
var WORKSPACE_PATTERN = /^ten_[0-9a-fA-F]{32}$/;
|
|
750
|
+
var WORKSPACE_PATTERN = /^ten_(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{32})$/;
|
|
587
751
|
var NOT_IN_A_HOST = /[\\\s\x00-\x1f\x7f]/;
|
|
588
752
|
var NOT_IN_A_KEY = /[\x00\n\r]/;
|
|
589
753
|
var PUBLISHABLE_PREFIX = "baton_pk_";
|
|
@@ -624,7 +788,11 @@ function splitDsn(raw) {
|
|
|
624
788
|
function splitKeyFromAuthority(netloc) {
|
|
625
789
|
const index = netloc.lastIndexOf("@");
|
|
626
790
|
if (index === -1) return { key: "", at: false, authority: netloc };
|
|
627
|
-
return {
|
|
791
|
+
return {
|
|
792
|
+
key: netloc.slice(0, index),
|
|
793
|
+
at: true,
|
|
794
|
+
authority: netloc.slice(index + 1)
|
|
795
|
+
};
|
|
628
796
|
}
|
|
629
797
|
function redact(raw) {
|
|
630
798
|
const parts = splitDsn(raw);
|
|
@@ -704,6 +872,11 @@ function parseDsn(raw) {
|
|
|
704
872
|
if (!authority) {
|
|
705
873
|
fail(`dsn ${safe} has no host`);
|
|
706
874
|
}
|
|
875
|
+
if (containsKey(authority)) {
|
|
876
|
+
fail(
|
|
877
|
+
`dsn ${safe} has the KEY where the host belongs. The order is key, @, host \u2014 check whether the two are the wrong way round: https://baton_pk_...@host/ten_.../<server>`
|
|
878
|
+
);
|
|
879
|
+
}
|
|
707
880
|
if (NOT_IN_A_HOST.test(authority)) {
|
|
708
881
|
fail(
|
|
709
882
|
`dsn ${safe} has whitespace, a control character or a backslash inside its host. Those are silently REMOVED or reinterpreted when a URL is parsed, so the address dialled would not be the one you wrote \u2014 check for a line wrap or a stray space where the value was copied.`
|
|
@@ -723,7 +896,7 @@ function parseDsn(raw) {
|
|
|
723
896
|
const [workspace, server, ...extra] = segments;
|
|
724
897
|
if (workspace === void 0 || server === void 0 || extra.length > 0) {
|
|
725
898
|
fail(
|
|
726
|
-
`dsn ${safe} must carry exactly two path segments \u2014 the workspace and the server, as in /ten_
|
|
899
|
+
`dsn ${safe} must carry exactly two path segments \u2014 the workspace and the server, as in /ten_.../<server>. A missing server is never defaulted: it is what the key is bound to.`
|
|
727
900
|
);
|
|
728
901
|
}
|
|
729
902
|
for (const [slot, segment] of [
|
|
@@ -732,13 +905,13 @@ function parseDsn(raw) {
|
|
|
732
905
|
]) {
|
|
733
906
|
if (containsKey(segment)) {
|
|
734
907
|
fail(
|
|
735
|
-
`dsn ${safe} has a KEY in the ${slot} slot. The key goes before the @, and the path carries the workspace and the server: https://baton_pk_...@host/ten_
|
|
908
|
+
`dsn ${safe} has a KEY in the ${slot} slot. The key goes before the @, and the path carries the workspace and the server: https://baton_pk_...@host/ten_.../<server>`
|
|
736
909
|
);
|
|
737
910
|
}
|
|
738
911
|
}
|
|
739
912
|
if (!WORKSPACE_PATTERN.test(workspace)) {
|
|
740
913
|
fail(
|
|
741
|
-
`dsn ${safe} has ${JSON.stringify(workspace)} where the workspace belongs \u2014 expected ten_ followed by 32 hex characters. If the two path segments are the right way round, this is not a Baton DSN.`
|
|
914
|
+
`dsn ${safe} has ${JSON.stringify(workspace)} where the workspace belongs \u2014 expected ten_ followed by 8 or 32 hex characters. If the two path segments are the right way round, this is not a Baton DSN.`
|
|
742
915
|
);
|
|
743
916
|
}
|
|
744
917
|
if (!VENDOR_ID_PATTERN.test(server)) {
|
|
@@ -759,8 +932,16 @@ function parseDsn(raw) {
|
|
|
759
932
|
});
|
|
760
933
|
}
|
|
761
934
|
function sealKey(dsn) {
|
|
762
|
-
const shown = {
|
|
763
|
-
|
|
935
|
+
const shown = {
|
|
936
|
+
origin: dsn.origin,
|
|
937
|
+
tenantId: dsn.tenantId,
|
|
938
|
+
vendorId: dsn.vendorId,
|
|
939
|
+
key: "<key>"
|
|
940
|
+
};
|
|
941
|
+
Object.defineProperty(dsn, "toJSON", {
|
|
942
|
+
value: () => shown,
|
|
943
|
+
enumerable: false
|
|
944
|
+
});
|
|
764
945
|
Object.defineProperty(dsn, /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom"), {
|
|
765
946
|
value: () => shown,
|
|
766
947
|
enumerable: false
|
|
@@ -770,12 +951,18 @@ function sealKey(dsn) {
|
|
|
770
951
|
|
|
771
952
|
// src/integrations/mcp/config.ts
|
|
772
953
|
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
|
|
954
|
+
var USER_ID_MODES = /* @__PURE__ */ new Set(["hashed", "raw"]);
|
|
773
955
|
function resolveTenantId(explicit, vendorId) {
|
|
774
956
|
if (explicit) return explicit;
|
|
775
957
|
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_TENANT_ID : void 0;
|
|
776
958
|
if (fromEnv) return fromEnv;
|
|
777
959
|
return vendorId;
|
|
778
960
|
}
|
|
961
|
+
function resolveUserIdHmacKey(explicit) {
|
|
962
|
+
if (explicit !== void 0) return explicit;
|
|
963
|
+
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_USER_ID_HMAC_KEY : void 0;
|
|
964
|
+
return fromEnv ? fromEnv : void 0;
|
|
965
|
+
}
|
|
779
966
|
function resolveBatonConfig(config) {
|
|
780
967
|
const dsnString = selectDsn(
|
|
781
968
|
config.dsn,
|
|
@@ -857,6 +1044,17 @@ function validateBatonConfig(config) {
|
|
|
857
1044
|
`BatonConfig.intentParamMode ${JSON.stringify(config.intentParamMode)} must be one of ${JSON.stringify([...INTENT_PARAM_MODES].sort())}.`
|
|
858
1045
|
);
|
|
859
1046
|
}
|
|
1047
|
+
if (config.resolveUser !== void 0 && typeof config.resolveUser !== "function") {
|
|
1048
|
+
throw new Error("BatonConfig.resolveUser must be a function.");
|
|
1049
|
+
}
|
|
1050
|
+
if (config.userIdHmacKey !== void 0 && typeof config.userIdHmacKey !== "string" && !(config.userIdHmacKey instanceof Uint8Array)) {
|
|
1051
|
+
throw new Error("BatonConfig.userIdHmacKey must be a string or a Uint8Array.");
|
|
1052
|
+
}
|
|
1053
|
+
if (config.userIdMode !== void 0 && !USER_ID_MODES.has(config.userIdMode)) {
|
|
1054
|
+
throw new Error(
|
|
1055
|
+
`BatonConfig.userIdMode ${JSON.stringify(config.userIdMode)} must be one of ${JSON.stringify([...USER_ID_MODES].sort())}.`
|
|
1056
|
+
);
|
|
1057
|
+
}
|
|
860
1058
|
}
|
|
861
1059
|
|
|
862
1060
|
// src/optout.ts
|
|
@@ -1183,16 +1381,18 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1183
1381
|
const extra = callArgs[callArgs.length - 1];
|
|
1184
1382
|
const params = (callArgs.length > 1 ? callArgs[0] : void 0) ?? {};
|
|
1185
1383
|
const meta = extraMeta(extra);
|
|
1186
|
-
const runtime = detectAgentRuntime(meta
|
|
1384
|
+
const runtime = detectAgentRuntime(meta, {
|
|
1385
|
+
// v2 lifts the reserved `io.modelcontextprotocol/*` keys out of
|
|
1386
|
+
// `_meta`; 1.x leaves them in. Both are handed over — see
|
|
1387
|
+
// `mcpTypes.extraEnvelope`.
|
|
1388
|
+
envelope: extraEnvelope(extra),
|
|
1389
|
+
// Tier 2's carrier: neither peer puts client identity on the
|
|
1390
|
+
// handler context, both expose the cached handshake on the server.
|
|
1391
|
+
server: ctx.server,
|
|
1392
|
+
scrubber: ctx.scrubber
|
|
1393
|
+
}) ?? UNKNOWN_AGENT_RUNTIME;
|
|
1187
1394
|
const scrubbedMeta = meta ? ctx.scrubber(meta) : null;
|
|
1188
|
-
const sessionId = await resolveSessionId(
|
|
1189
|
-
ctx.resolveSessionId,
|
|
1190
|
-
ctx.fallbackSessionId,
|
|
1191
|
-
extra,
|
|
1192
|
-
meta,
|
|
1193
|
-
toolName,
|
|
1194
|
-
params
|
|
1195
|
-
);
|
|
1395
|
+
const sessionId = await resolveSessionId(ctx.fallbackSessionId, extra);
|
|
1196
1396
|
const dispositions = ctx.paramRegistry.get(toolName);
|
|
1197
1397
|
const rawIntent = extractGoalParam(params, USER_GOAL_PARAM_NAME, toolName, dispositions);
|
|
1198
1398
|
const rawExpected = extractGoalParam(
|
|
@@ -1205,12 +1405,24 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1205
1405
|
const scrubbedIntent = rawIntent !== null ? ctx.scrubber(rawIntent) : null;
|
|
1206
1406
|
const scrubbedExpected = rawExpected !== null ? ctx.scrubber(rawExpected) : null;
|
|
1207
1407
|
const scrubbedWorkflow = rawWorkflow !== null ? ctx.scrubber(rawWorkflow) : null;
|
|
1408
|
+
const callId = uuidv72();
|
|
1409
|
+
const userId = await resolveCallUserId(
|
|
1410
|
+
ctx.resolveUser,
|
|
1411
|
+
{ extra, toolName, arguments: params },
|
|
1412
|
+
{ mode: ctx.userIdMode, tenantId: ctx.tenantId, key: ctx.userIdHmacKey }
|
|
1413
|
+
);
|
|
1208
1414
|
const common = {
|
|
1209
1415
|
tenant_id: ctx.tenantId,
|
|
1210
1416
|
vendor_id: ctx.vendorId,
|
|
1211
1417
|
session_id: sessionId,
|
|
1212
1418
|
consent_token: ctx.consentToken,
|
|
1213
1419
|
agent_runtime: runtime,
|
|
1420
|
+
// The four `...common` sites below are the tool-call legs and the
|
|
1421
|
+
// proactive annotation — the same four Python stamps
|
|
1422
|
+
// (`middleware.py` 481/524/562/606). `surface_snapshot` is deliberately
|
|
1423
|
+
// NOT among them: it describes the SERVER and is captured outside any
|
|
1424
|
+
// call, so there is no caller to name (register D5).
|
|
1425
|
+
user_id: userId,
|
|
1214
1426
|
runtime_meta: scrubbedMeta
|
|
1215
1427
|
};
|
|
1216
1428
|
if (scrubbedIntent !== null && ctx.tracker.claim(sessionId)) {
|
|
@@ -1233,6 +1445,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1233
1445
|
ctx.sink,
|
|
1234
1446
|
() => ToolCallStartEventSchema.parse({
|
|
1235
1447
|
...common,
|
|
1448
|
+
call_id: callId,
|
|
1236
1449
|
sequence_number: ctx.counter.next(sessionId),
|
|
1237
1450
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1238
1451
|
payload: {
|
|
@@ -1256,6 +1469,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1256
1469
|
ctx.sink,
|
|
1257
1470
|
() => ToolCallErrorEventSchema.parse({
|
|
1258
1471
|
...common,
|
|
1472
|
+
call_id: callId,
|
|
1259
1473
|
sequence_number: ctx.counter.next(sessionId),
|
|
1260
1474
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1261
1475
|
payload: {
|
|
@@ -1273,6 +1487,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
1273
1487
|
ctx.sink,
|
|
1274
1488
|
() => ToolCallEndEventSchema.parse({
|
|
1275
1489
|
...common,
|
|
1490
|
+
call_id: callId,
|
|
1276
1491
|
sequence_number: ctx.counter.next(sessionId),
|
|
1277
1492
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1278
1493
|
payload: { tool_name: toolName, result: ctx.scrubber(result), duration_ms: durationMs }
|
|
@@ -1442,7 +1657,24 @@ function withBaton(server, supplied = {}) {
|
|
|
1442
1657
|
consent_token: config.consentToken,
|
|
1443
1658
|
sequence_number: counter.next(sessionId),
|
|
1444
1659
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1445
|
-
|
|
1660
|
+
// Deliberately the literal, NOT the ladder — and an earlier comment
|
|
1661
|
+
// here justified that by saying no call is in scope, which is false:
|
|
1662
|
+
// `maybeEmitSurfaceSnapshot` is the first line of `batonWrap`, so the
|
|
1663
|
+
// handshake has happened and tier 2's carrier is live in this closure.
|
|
1664
|
+
// The real reason is that the snapshot describes the VENDOR'S SURFACE,
|
|
1665
|
+
// which is the same whoever is calling. It is hashed and emitted at
|
|
1666
|
+
// most once per process per surface, so the client that happens to
|
|
1667
|
+
// trigger it is whichever one called first — attributing the surface to
|
|
1668
|
+
// that client would read as a fact about the surface and be an accident
|
|
1669
|
+
// of timing. Python hardcodes it from the same in-call position
|
|
1670
|
+
// (`_tool_wrap.py`), so this is parity, not a gap.
|
|
1671
|
+
//
|
|
1672
|
+
// ⚠ Consequence worth knowing: one session emits `surface_snapshot`
|
|
1673
|
+
// with `unknown` and everything else with the client's name, so a
|
|
1674
|
+
// consumer grouping on `agent_runtime` alone sees two runtimes for one
|
|
1675
|
+
// client. Intended; group surfaces on `(tenant_id, vendor_id,
|
|
1676
|
+
// surface_hash)`, which is what the Console's table is keyed on.
|
|
1677
|
+
agent_runtime: UNKNOWN_AGENT_RUNTIME,
|
|
1446
1678
|
payload: {
|
|
1447
1679
|
surface_hash: digest,
|
|
1448
1680
|
server_info: snapshot.server_info,
|
|
@@ -1488,18 +1720,21 @@ function withBaton(server, supplied = {}) {
|
|
|
1488
1720
|
vendorId: config.vendorId,
|
|
1489
1721
|
consentToken: config.consentToken,
|
|
1490
1722
|
fallbackSessionId,
|
|
1491
|
-
|
|
1723
|
+
server,
|
|
1492
1724
|
scrubber,
|
|
1493
|
-
resolveSessionId: config.resolveSessionId,
|
|
1494
1725
|
annotationToolName,
|
|
1495
1726
|
intentParamMode,
|
|
1496
1727
|
paramRegistry: /* @__PURE__ */ new Map(),
|
|
1728
|
+
resolveUser: config.resolveUser,
|
|
1729
|
+
userIdMode: config.userIdMode ?? "hashed",
|
|
1730
|
+
userIdHmacKey: resolveUserIdHmacKey(config.userIdHmacKey),
|
|
1497
1731
|
vendorToolJsonSchema,
|
|
1498
1732
|
bustSchemaMemo,
|
|
1499
1733
|
tracker,
|
|
1500
1734
|
surfaceState,
|
|
1501
1735
|
emitSurface
|
|
1502
1736
|
};
|
|
1737
|
+
warnIfIdentityCannotResolve(ctx);
|
|
1503
1738
|
const instructions = buildServerInstructions({
|
|
1504
1739
|
vendorDisplayName: config.vendorDisplayName,
|
|
1505
1740
|
annotationToolName
|
|
@@ -1512,10 +1747,17 @@ function withBaton(server, supplied = {}) {
|
|
|
1512
1747
|
vendorId: ctx.vendorId,
|
|
1513
1748
|
vendorDisplayName: config.vendorDisplayName,
|
|
1514
1749
|
consentToken: ctx.consentToken,
|
|
1750
|
+
// Read off `ctx`, never re-resolved from `config`: the two paths must
|
|
1751
|
+
// agree on the actor, and `userIdMode`/`userIdHmacKey` each have a
|
|
1752
|
+
// default and an env fallback that a second resolution could take
|
|
1753
|
+
// differently. Required (not optional) on the options type ON PURPOSE —
|
|
1754
|
+
// the compiler then refuses a registration that forgets them, which is
|
|
1755
|
+
// the mechanical version of "wiring two of four call sites".
|
|
1756
|
+
resolveUser: ctx.resolveUser,
|
|
1757
|
+
userIdMode: ctx.userIdMode,
|
|
1758
|
+
userIdHmacKey: ctx.userIdHmacKey,
|
|
1515
1759
|
fallbackSessionId: ctx.fallbackSessionId,
|
|
1516
|
-
defaultAgentRuntime: ctx.defaultAgentRuntime,
|
|
1517
1760
|
scrubber: ctx.scrubber,
|
|
1518
|
-
resolveSessionId: ctx.resolveSessionId,
|
|
1519
1761
|
annotationToolName: config.annotationToolName,
|
|
1520
1762
|
tracker
|
|
1521
1763
|
});
|
|
@@ -1558,6 +1800,8 @@ export {
|
|
|
1558
1800
|
ToolCallErrorPayloadSchema,
|
|
1559
1801
|
ToolCallStartEventSchema,
|
|
1560
1802
|
ToolCallStartPayloadSchema,
|
|
1803
|
+
VENDOR_HASH_SCHEME,
|
|
1804
|
+
hashUserId,
|
|
1561
1805
|
identityScrub,
|
|
1562
1806
|
safeWrite,
|
|
1563
1807
|
withBaton
|