@goodtiming/baton-sdk 0.3.0 → 0.3.2
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 +121 -21
- package/dist/index.cjs +431 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +87 -23
- package/dist/index.d.ts +87 -23
- package/dist/index.js +430 -39
- 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.2";
|
|
3
3
|
|
|
4
4
|
// src/events.ts
|
|
5
5
|
import { z } from "zod";
|
|
@@ -41,6 +41,7 @@ var SurfaceSnapshotPayloadSchema = z.object({
|
|
|
41
41
|
tools: z.array(z.record(z.string(), z.unknown())).default([]),
|
|
42
42
|
seam_augmentations: z.record(z.string(), z.unknown()).default({})
|
|
43
43
|
}).strict();
|
|
44
|
+
var DEFAULT_CONSENT_TOKEN = "customer-consented";
|
|
44
45
|
var envelopeShape = {
|
|
45
46
|
event_id: z.uuid().default(() => uuidv7()),
|
|
46
47
|
tenant_id: z.string(),
|
|
@@ -52,6 +53,7 @@ var envelopeShape = {
|
|
|
52
53
|
sdk_version: z.string().default(SDK_VERSION),
|
|
53
54
|
agent_runtime: z.string().default("unknown"),
|
|
54
55
|
user_id: z.string().nullable().default(null),
|
|
56
|
+
call_id: z.string().nullable().default(null),
|
|
55
57
|
runtime_meta: z.record(z.string(), z.unknown()).nullable().default(null)
|
|
56
58
|
};
|
|
57
59
|
var EventTypeSchema = z.enum([
|
|
@@ -264,6 +266,25 @@ var HttpSink = class _HttpSink {
|
|
|
264
266
|
);
|
|
265
267
|
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
266
268
|
}
|
|
269
|
+
/**
|
|
270
|
+
* What this sink shows when something prints it — never the bearer.
|
|
271
|
+
*
|
|
272
|
+
* ⚠ **A sink built from a DSN holds a key the vendor never typed**, and it
|
|
273
|
+
* hangs off `BatonHandle.sink` and off the resolved config, so
|
|
274
|
+
* `console.log(handle.sink)` or a structured logger serialising either one
|
|
275
|
+
* wrote a publishable key into the vendor's logs. Found by the S2 test that
|
|
276
|
+
* asserts the DSN's own string does not survive resolution: it does not, and
|
|
277
|
+
* the key was one field further down. Same treatment as the parsed DSN —
|
|
278
|
+
* `toJSON` for serialisers, the inspect symbol for `console.log`, and
|
|
279
|
+
* `Symbol.for` rather than importing `node:util`, since this package runs on
|
|
280
|
+
* runtimes that have no such module.
|
|
281
|
+
*/
|
|
282
|
+
toJSON() {
|
|
283
|
+
return { url: this.url, apiKey: "<key>" };
|
|
284
|
+
}
|
|
285
|
+
[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
|
|
286
|
+
return this.toJSON();
|
|
287
|
+
}
|
|
267
288
|
async write(event) {
|
|
268
289
|
if (this.closed) throw new Error("HttpSink is closed");
|
|
269
290
|
this.enqueue(event);
|
|
@@ -453,25 +474,65 @@ function extraMeta(extra) {
|
|
|
453
474
|
const nested = extra.mcpReq?._meta;
|
|
454
475
|
return nested ?? null;
|
|
455
476
|
}
|
|
477
|
+
function extraEnvelope(extra) {
|
|
478
|
+
const envelope = extra.mcpReq?.envelope;
|
|
479
|
+
return envelope ?? null;
|
|
480
|
+
}
|
|
456
481
|
|
|
457
482
|
// src/integrations/mcp/runtimeAdapter.ts
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
483
|
+
var CLIENT_INFO_META_KEY = "io.modelcontextprotocol/clientInfo";
|
|
484
|
+
var CLIENT_NAME_MAX_LEN = 128;
|
|
485
|
+
var UNKNOWN_AGENT_RUNTIME = "unknown";
|
|
486
|
+
function clean(name, scrubber) {
|
|
487
|
+
if (typeof name !== "string" || !name) return null;
|
|
488
|
+
let cleaned = name;
|
|
489
|
+
if (scrubber) {
|
|
490
|
+
const scrubbed = scrubber(cleaned);
|
|
491
|
+
if (typeof scrubbed !== "string") return null;
|
|
492
|
+
cleaned = scrubbed;
|
|
493
|
+
}
|
|
494
|
+
if (!cleaned) return null;
|
|
495
|
+
return [...cleaned].slice(0, CLIENT_NAME_MAX_LEN).join("");
|
|
496
|
+
}
|
|
497
|
+
function declaredOnRequest(meta, envelope) {
|
|
498
|
+
for (const source of [envelope, meta]) {
|
|
499
|
+
if (!source) continue;
|
|
500
|
+
const info = source[CLIENT_INFO_META_KEY];
|
|
501
|
+
if (info && typeof info === "object") {
|
|
502
|
+
const name = info.name;
|
|
503
|
+
if (name !== void 0) return name;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
return void 0;
|
|
507
|
+
}
|
|
508
|
+
function declaredOnConnection(options) {
|
|
509
|
+
try {
|
|
510
|
+
const server = options.server;
|
|
511
|
+
if (!server) return void 0;
|
|
512
|
+
const inner = server.server;
|
|
513
|
+
const holder = inner ?? server;
|
|
514
|
+
if (typeof holder.getClientVersion !== "function") return void 0;
|
|
515
|
+
return holder.getClientVersion()?.name;
|
|
516
|
+
} catch {
|
|
517
|
+
return void 0;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
function detectAgentRuntime(meta, options = {}) {
|
|
521
|
+
const { envelope, scrubber } = options;
|
|
522
|
+
const onRequest = clean(declaredOnRequest(meta, envelope), scrubber);
|
|
523
|
+
if (onRequest !== null) return onRequest;
|
|
524
|
+
const onConnection = clean(declaredOnConnection(options), scrubber);
|
|
525
|
+
if (onConnection !== null) return onConnection;
|
|
526
|
+
if (meta) {
|
|
527
|
+
for (const key of Object.keys(meta)) {
|
|
528
|
+
if (key.startsWith("claudecode/")) return "claude-code";
|
|
529
|
+
}
|
|
462
530
|
}
|
|
463
531
|
return null;
|
|
464
532
|
}
|
|
465
533
|
|
|
466
534
|
// src/integrations/mcp/sessionResolution.ts
|
|
467
|
-
async function resolveSessionId(
|
|
468
|
-
if (hook) {
|
|
469
|
-
try {
|
|
470
|
-
const hookResult = await hook({ meta, toolName, arguments: args });
|
|
471
|
-
if (typeof hookResult === "string" && hookResult) return hookResult;
|
|
472
|
-
} catch {
|
|
473
|
-
}
|
|
474
|
-
}
|
|
535
|
+
async function resolveSessionId(fallbackSessionId, extra) {
|
|
475
536
|
if (typeof extra.sessionId === "string" && extra.sessionId) return extra.sessionId;
|
|
476
537
|
return fallbackSessionId;
|
|
477
538
|
}
|
|
@@ -507,16 +568,13 @@ function registerAnnotationTool(server, options) {
|
|
|
507
568
|
},
|
|
508
569
|
async (args, extra) => {
|
|
509
570
|
const meta = extraMeta(extra);
|
|
510
|
-
const runtime = detectAgentRuntime(meta
|
|
571
|
+
const runtime = detectAgentRuntime(meta, {
|
|
572
|
+
envelope: extraEnvelope(extra),
|
|
573
|
+
server,
|
|
574
|
+
scrubber: options.scrubber
|
|
575
|
+
}) ?? UNKNOWN_AGENT_RUNTIME;
|
|
511
576
|
const scrubbedMeta = meta ? options.scrubber(meta) : null;
|
|
512
|
-
const sessionId = await resolveSessionId(
|
|
513
|
-
options.resolveSessionId,
|
|
514
|
-
options.fallbackSessionId,
|
|
515
|
-
extra,
|
|
516
|
-
meta,
|
|
517
|
-
name,
|
|
518
|
-
args
|
|
519
|
-
);
|
|
577
|
+
const sessionId = await resolveSessionId(options.fallbackSessionId, extra);
|
|
520
578
|
if (args.signal_type === void 0) {
|
|
521
579
|
options.tracker?.mark(sessionId);
|
|
522
580
|
}
|
|
@@ -561,8 +619,211 @@ function registerAnnotationTool(server, options) {
|
|
|
561
619
|
return name;
|
|
562
620
|
}
|
|
563
621
|
|
|
564
|
-
// src/
|
|
622
|
+
// src/dsn.ts
|
|
565
623
|
var VENDOR_ID_PATTERN = /^[a-zA-Z0-9_-]{1,48}$/;
|
|
624
|
+
var WORKSPACE_PATTERN = /^ten_(?:[0-9a-fA-F]{8}|[0-9a-fA-F]{32})$/;
|
|
625
|
+
var NOT_IN_A_HOST = /[\\\s\x00-\x1f\x7f]/;
|
|
626
|
+
var NOT_IN_A_KEY = /[\x00\n\r]/;
|
|
627
|
+
var PUBLISHABLE_PREFIX = "baton_pk_";
|
|
628
|
+
var SECRET_PREFIX = "baton_sk_";
|
|
629
|
+
var KEY_RUN = /baton_(?:pk|sk)_[A-Za-z0-9_%-]{8,}/g;
|
|
630
|
+
function looksLikeAKey(value) {
|
|
631
|
+
return value.startsWith(PUBLISHABLE_PREFIX) || value.startsWith(SECRET_PREFIX);
|
|
632
|
+
}
|
|
633
|
+
function containsKey(value) {
|
|
634
|
+
return sweep(value) !== value;
|
|
635
|
+
}
|
|
636
|
+
var BARE_KEY_HINT = "this looks like a bare key, not a DSN \u2014 copy the full value from /account, which starts with https:// and ends with your server's name";
|
|
637
|
+
function warn(message) {
|
|
638
|
+
if (typeof process !== "undefined" && typeof process.emitWarning === "function") {
|
|
639
|
+
process.emitWarning(message);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
function splitDsn(raw) {
|
|
643
|
+
const schemeEnd = raw.indexOf("://");
|
|
644
|
+
if (schemeEnd === -1) return null;
|
|
645
|
+
const scheme = raw.slice(0, schemeEnd).toLowerCase();
|
|
646
|
+
const rest = raw.slice(schemeEnd + 3);
|
|
647
|
+
const end = rest.search(/[/?#]/);
|
|
648
|
+
if (end === -1) return { scheme, netloc: rest, slash: "", path: "" };
|
|
649
|
+
const netloc = rest.slice(0, end);
|
|
650
|
+
if (rest[end] !== "/") {
|
|
651
|
+
return { scheme, netloc, slash: "", path: "" };
|
|
652
|
+
}
|
|
653
|
+
const afterSlash = rest.slice(end + 1);
|
|
654
|
+
const pathEnd = afterSlash.search(/[?#]/);
|
|
655
|
+
return {
|
|
656
|
+
scheme,
|
|
657
|
+
netloc,
|
|
658
|
+
slash: "/",
|
|
659
|
+
path: pathEnd === -1 ? afterSlash : afterSlash.slice(0, pathEnd)
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
function splitKeyFromAuthority(netloc) {
|
|
663
|
+
const index = netloc.lastIndexOf("@");
|
|
664
|
+
if (index === -1) return { key: "", at: false, authority: netloc };
|
|
665
|
+
return {
|
|
666
|
+
key: netloc.slice(0, index),
|
|
667
|
+
at: true,
|
|
668
|
+
authority: netloc.slice(index + 1)
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
function redact(raw) {
|
|
672
|
+
const parts = splitDsn(raw);
|
|
673
|
+
if (parts === null) return "<dsn>";
|
|
674
|
+
const { at, authority } = splitKeyFromAuthority(parts.netloc);
|
|
675
|
+
const assembled = at ? `${parts.scheme}://***@${authority}${parts.slash}${parts.path}` : `${parts.scheme}://<no key>@${parts.netloc}${parts.slash}${parts.path}`;
|
|
676
|
+
return sweep(assembled);
|
|
677
|
+
}
|
|
678
|
+
function sweep(message) {
|
|
679
|
+
return message.replace(KEY_RUN, "<key>");
|
|
680
|
+
}
|
|
681
|
+
function fail(message) {
|
|
682
|
+
throw new Error(sweep(message));
|
|
683
|
+
}
|
|
684
|
+
function dsnFromEnvironment() {
|
|
685
|
+
const fromEnv = typeof process !== "undefined" ? process.env?.BATON_DSN : void 0;
|
|
686
|
+
return fromEnv ? fromEnv : void 0;
|
|
687
|
+
}
|
|
688
|
+
function selectDsn(explicit, supplied, door) {
|
|
689
|
+
const conflicts = Object.keys(supplied).filter((name) => supplied[name]).sort();
|
|
690
|
+
if (explicit) {
|
|
691
|
+
if (conflicts.length > 0) {
|
|
692
|
+
fail(
|
|
693
|
+
`${door} got both a dsn and an explicit ${conflicts[0]} \u2014 the dsn already supplies it. Drop one: the dsn is the single value from /account, and ${conflicts[0]} is what it unpacks to.`
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
return explicit;
|
|
697
|
+
}
|
|
698
|
+
const ambient = dsnFromEnvironment();
|
|
699
|
+
if (ambient === void 0) return void 0;
|
|
700
|
+
if (conflicts.length > 0) {
|
|
701
|
+
warn(
|
|
702
|
+
`baton: BATON_DSN is set, but this ${door} supplies ${conflicts.join(", ")} directly, so BATON_DSN is being IGNORED and these events are NOT going to the collector it names. Remove the explicit value to use it, or unset BATON_DSN if it was meant for a different server.`
|
|
703
|
+
);
|
|
704
|
+
return void 0;
|
|
705
|
+
}
|
|
706
|
+
return ambient;
|
|
707
|
+
}
|
|
708
|
+
function parseDsn(raw) {
|
|
709
|
+
if (typeof raw !== "string" || raw.trim() === "") {
|
|
710
|
+
fail("dsn must be a non-empty string");
|
|
711
|
+
}
|
|
712
|
+
const dsn = raw.trim();
|
|
713
|
+
if (looksLikeAKey(dsn)) {
|
|
714
|
+
fail(`dsn is not a URL: ${BARE_KEY_HINT}`);
|
|
715
|
+
}
|
|
716
|
+
const safe = redact(dsn);
|
|
717
|
+
const parts = splitDsn(dsn);
|
|
718
|
+
if (parts === null || parts.scheme !== "https" && parts.scheme !== "http") {
|
|
719
|
+
fail(
|
|
720
|
+
`dsn ${safe} must start with https:// (or http:// for local development) \u2014 ${BARE_KEY_HINT}`
|
|
721
|
+
);
|
|
722
|
+
}
|
|
723
|
+
const { key, at, authority } = splitKeyFromAuthority(parts.netloc);
|
|
724
|
+
if (!at || !key) {
|
|
725
|
+
if (looksLikeAKey(parts.netloc)) {
|
|
726
|
+
fail(
|
|
727
|
+
`dsn ${safe} is a bare key with a scheme in front of it: ${BARE_KEY_HINT}. The full value also carries a host, your workspace and your server \u2014 https://baton_pk_...@host/ten_.../server`
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
const misplaced = parts.path.split("/").some((segment) => containsKey(segment));
|
|
731
|
+
const detail = misplaced ? "the key is in the PATH \u2014 it goes before an @" : "the value from /account has the key before an @";
|
|
732
|
+
fail(
|
|
733
|
+
`dsn ${safe} carries no key: ${detail}, as in https://baton_pk_...@host/ten_.../server`
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
if (NOT_IN_A_KEY.test(key)) {
|
|
737
|
+
fail(
|
|
738
|
+
`dsn ${safe} has a line break or a null inside its key \u2014 those can never be sent in an Authorization header, so this DSN could not have worked. Check for a line wrap where the value was copied.`
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
if (key.includes(":")) {
|
|
742
|
+
fail(
|
|
743
|
+
`dsn ${safe} has a ':' in its key \u2014 a DSN carries one credential and no password field`
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
if (!authority) {
|
|
747
|
+
fail(`dsn ${safe} has no host`);
|
|
748
|
+
}
|
|
749
|
+
if (containsKey(authority)) {
|
|
750
|
+
fail(
|
|
751
|
+
`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>`
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
if (NOT_IN_A_HOST.test(authority)) {
|
|
755
|
+
fail(
|
|
756
|
+
`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.`
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
let hostReason = null;
|
|
760
|
+
let parsedAuthority = null;
|
|
761
|
+
try {
|
|
762
|
+
parsedAuthority = new URL(`${parts.scheme}://${authority}`);
|
|
763
|
+
} catch (error) {
|
|
764
|
+
hostReason = error instanceof Error ? error.constructor.name : "Error";
|
|
765
|
+
}
|
|
766
|
+
if (parsedAuthority === null) {
|
|
767
|
+
fail(`dsn ${safe} is not a parseable URL (${hostReason ?? "Error"})`);
|
|
768
|
+
}
|
|
769
|
+
const segments = parts.path.split("/").filter((segment) => segment !== "");
|
|
770
|
+
const [workspace, server, ...extra] = segments;
|
|
771
|
+
if (workspace === void 0 || server === void 0 || extra.length > 0) {
|
|
772
|
+
fail(
|
|
773
|
+
`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.`
|
|
774
|
+
);
|
|
775
|
+
}
|
|
776
|
+
for (const [slot, segment] of [
|
|
777
|
+
["workspace", workspace],
|
|
778
|
+
["server", server]
|
|
779
|
+
]) {
|
|
780
|
+
if (containsKey(segment)) {
|
|
781
|
+
fail(
|
|
782
|
+
`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>`
|
|
783
|
+
);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
if (!WORKSPACE_PATTERN.test(workspace)) {
|
|
787
|
+
fail(
|
|
788
|
+
`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.`
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
if (!VENDOR_ID_PATTERN.test(server)) {
|
|
792
|
+
fail(
|
|
793
|
+
`dsn ${safe} has ${JSON.stringify(server)} where the server belongs \u2014 it must match ${VENDOR_ID_PATTERN.source}, because this value becomes the annotation tool name prefix as well as the envelope's vendor_id.`
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
if (key.startsWith(SECRET_PREFIX)) {
|
|
797
|
+
warn(
|
|
798
|
+
`baton: this DSN carries a ${SECRET_PREFIX} key (a workspace SECRET) where a ${PUBLISHABLE_PREFIX} key belongs. It will work. But if this server ships to anyone else, that key reads and writes everything the workspace holds \u2014 mint a publishable key at /account instead.`
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
return sealKey({
|
|
802
|
+
origin: `${parts.scheme}://${authority}`,
|
|
803
|
+
tenantId: workspace,
|
|
804
|
+
vendorId: server,
|
|
805
|
+
key
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
function sealKey(dsn) {
|
|
809
|
+
const shown = {
|
|
810
|
+
origin: dsn.origin,
|
|
811
|
+
tenantId: dsn.tenantId,
|
|
812
|
+
vendorId: dsn.vendorId,
|
|
813
|
+
key: "<key>"
|
|
814
|
+
};
|
|
815
|
+
Object.defineProperty(dsn, "toJSON", {
|
|
816
|
+
value: () => shown,
|
|
817
|
+
enumerable: false
|
|
818
|
+
});
|
|
819
|
+
Object.defineProperty(dsn, /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom"), {
|
|
820
|
+
value: () => shown,
|
|
821
|
+
enumerable: false
|
|
822
|
+
});
|
|
823
|
+
return dsn;
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
// src/integrations/mcp/config.ts
|
|
566
827
|
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required", "off"]);
|
|
567
828
|
function resolveTenantId(explicit, vendorId) {
|
|
568
829
|
if (explicit) return explicit;
|
|
@@ -570,7 +831,62 @@ function resolveTenantId(explicit, vendorId) {
|
|
|
570
831
|
if (fromEnv) return fromEnv;
|
|
571
832
|
return vendorId;
|
|
572
833
|
}
|
|
834
|
+
function resolveBatonConfig(config) {
|
|
835
|
+
const dsnString = selectDsn(
|
|
836
|
+
config.dsn,
|
|
837
|
+
{
|
|
838
|
+
// Truthiness per field, mirroring Python's `bool()` / `is not None`
|
|
839
|
+
// split: an empty `vendorId` is an unset one, while an explicitly
|
|
840
|
+
// supplied `tenantId` or `sink` is a conflict whatever it holds.
|
|
841
|
+
vendorId: Boolean(config.vendorId),
|
|
842
|
+
tenantId: config.tenantId !== void 0,
|
|
843
|
+
sink: config.sink !== void 0
|
|
844
|
+
},
|
|
845
|
+
"BatonConfig"
|
|
846
|
+
);
|
|
847
|
+
if (dsnString === void 0) {
|
|
848
|
+
const defaulted = {
|
|
849
|
+
...config,
|
|
850
|
+
consentToken: config.consentToken ?? DEFAULT_CONSENT_TOKEN
|
|
851
|
+
};
|
|
852
|
+
validateBatonConfig(defaulted);
|
|
853
|
+
return defaulted;
|
|
854
|
+
}
|
|
855
|
+
const dsn = parseDsn(dsnString);
|
|
856
|
+
const rest = { ...config };
|
|
857
|
+
delete rest.dsn;
|
|
858
|
+
const identity = {
|
|
859
|
+
...rest,
|
|
860
|
+
vendorId: dsn.vendorId,
|
|
861
|
+
tenantId: dsn.tenantId,
|
|
862
|
+
// `??`, for the same reason as `consentToken` directly below, and the
|
|
863
|
+
// inconsistency was worth fixing rather than mirroring: with no dsn,
|
|
864
|
+
// `vendorDisplayName: ""` is REFUSED by validation; with one, `||` quietly
|
|
865
|
+
// substituted the server slug. One input, two answers, decided by whether
|
|
866
|
+
// a dsn happens to be present — and this string reaches the calling agent
|
|
867
|
+
// in the server instructions and the annotation tool description, which is
|
|
868
|
+
// the whitelabel obligation the validator cites when it refuses the empty
|
|
869
|
+
// one. ⚠ Python uses `or` here and still has the inconsistency.
|
|
870
|
+
vendorDisplayName: config.vendorDisplayName ?? dsn.vendorId,
|
|
871
|
+
// `??`, not `||`: an explicit empty string must survive to the validation
|
|
872
|
+
// below and be REFUSED there, rather than be quietly replaced by the
|
|
873
|
+
// default it was deliberately not left as.
|
|
874
|
+
consentToken: config.consentToken ?? DEFAULT_CONSENT_TOKEN
|
|
875
|
+
};
|
|
876
|
+
validateBatonConfig(identity);
|
|
877
|
+
return {
|
|
878
|
+
...identity,
|
|
879
|
+
// The origin is scheme + authority; `HttpSink` appends `/v0/events`
|
|
880
|
+
// itself, exactly as it does for an explicitly-constructed sink.
|
|
881
|
+
sink: new HttpSink(dsn.origin, { apiKey: dsn.key })
|
|
882
|
+
};
|
|
883
|
+
}
|
|
573
884
|
function validateBatonConfig(config) {
|
|
885
|
+
if (!config.vendorId) {
|
|
886
|
+
throw new Error(
|
|
887
|
+
"BatonConfig needs a vendorId \u2014 either directly, or via a dsn whose last path segment names the server (see /account)."
|
|
888
|
+
);
|
|
889
|
+
}
|
|
574
890
|
if (!VENDOR_ID_PATTERN.test(config.vendorId)) {
|
|
575
891
|
throw new Error(
|
|
576
892
|
`vendorId ${JSON.stringify(config.vendorId)} must match ${VENDOR_ID_PATTERN.source} \u2014 used as the default annotation tool name prefix.`
|
|
@@ -581,6 +897,11 @@ function validateBatonConfig(config) {
|
|
|
581
897
|
"BatonConfig.vendorDisplayName is required \u2014 used in server instructions and the annotation tool description (whitelabel obligation, SPEC \xA75.4)."
|
|
582
898
|
);
|
|
583
899
|
}
|
|
900
|
+
if (config.consentToken !== void 0 && !config.consentToken) {
|
|
901
|
+
throw new Error(
|
|
902
|
+
"BatonConfig.consentToken was set to an empty string, and events without a valid consent_token MUST be rejected by the consumer per SPEC \xA72.3. Omit it to take the SDK's default."
|
|
903
|
+
);
|
|
904
|
+
}
|
|
584
905
|
if (!config.consentToken) {
|
|
585
906
|
throw new Error(
|
|
586
907
|
"BatonConfig.consentToken is required per SPEC \xA72.3 \u2014 events without a valid consent_token MUST be rejected by the consumer."
|
|
@@ -593,6 +914,38 @@ function validateBatonConfig(config) {
|
|
|
593
914
|
}
|
|
594
915
|
}
|
|
595
916
|
|
|
917
|
+
// src/optout.ts
|
|
918
|
+
var SWITCH = "BATON_DISABLED";
|
|
919
|
+
var OFF_VALUES = /* @__PURE__ */ new Set(["", "0", "false", "no", "off"]);
|
|
920
|
+
function captureDisabled() {
|
|
921
|
+
const value = typeof process !== "undefined" ? process.env?.[SWITCH] : void 0;
|
|
922
|
+
if (value !== void 0 && !OFF_VALUES.has(value.trim().toLowerCase())) {
|
|
923
|
+
return SWITCH;
|
|
924
|
+
}
|
|
925
|
+
return null;
|
|
926
|
+
}
|
|
927
|
+
function logDisabled(switchName, surface) {
|
|
928
|
+
if (typeof process !== "undefined" && typeof process.emitWarning === "function") {
|
|
929
|
+
process.emitWarning(
|
|
930
|
+
`baton: ${switchName} is set, so capture is OFF \u2014 ${surface} installed nothing and will emit no events. Unset it to re-enable.`
|
|
931
|
+
);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
var DisabledSink = class {
|
|
935
|
+
// The parameter is omitted rather than named-and-ignored: a narrower
|
|
936
|
+
// signature satisfies `Sink` in TypeScript, and there is nothing to do with
|
|
937
|
+
// an event that cannot arrive.
|
|
938
|
+
async write() {
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
async flush() {
|
|
942
|
+
return;
|
|
943
|
+
}
|
|
944
|
+
async aclose() {
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
|
|
596
949
|
// src/integrations/mcp/handle.ts
|
|
597
950
|
var BatonHandle = class {
|
|
598
951
|
sink;
|
|
@@ -885,16 +1238,18 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
885
1238
|
const extra = callArgs[callArgs.length - 1];
|
|
886
1239
|
const params = (callArgs.length > 1 ? callArgs[0] : void 0) ?? {};
|
|
887
1240
|
const meta = extraMeta(extra);
|
|
888
|
-
const runtime = detectAgentRuntime(meta
|
|
1241
|
+
const runtime = detectAgentRuntime(meta, {
|
|
1242
|
+
// v2 lifts the reserved `io.modelcontextprotocol/*` keys out of
|
|
1243
|
+
// `_meta`; 1.x leaves them in. Both are handed over — see
|
|
1244
|
+
// `mcpTypes.extraEnvelope`.
|
|
1245
|
+
envelope: extraEnvelope(extra),
|
|
1246
|
+
// Tier 2's carrier: neither peer puts client identity on the
|
|
1247
|
+
// handler context, both expose the cached handshake on the server.
|
|
1248
|
+
server: ctx.server,
|
|
1249
|
+
scrubber: ctx.scrubber
|
|
1250
|
+
}) ?? UNKNOWN_AGENT_RUNTIME;
|
|
889
1251
|
const scrubbedMeta = meta ? ctx.scrubber(meta) : null;
|
|
890
|
-
const sessionId = await resolveSessionId(
|
|
891
|
-
ctx.resolveSessionId,
|
|
892
|
-
ctx.fallbackSessionId,
|
|
893
|
-
extra,
|
|
894
|
-
meta,
|
|
895
|
-
toolName,
|
|
896
|
-
params
|
|
897
|
-
);
|
|
1252
|
+
const sessionId = await resolveSessionId(ctx.fallbackSessionId, extra);
|
|
898
1253
|
const dispositions = ctx.paramRegistry.get(toolName);
|
|
899
1254
|
const rawIntent = extractGoalParam(params, USER_GOAL_PARAM_NAME, toolName, dispositions);
|
|
900
1255
|
const rawExpected = extractGoalParam(
|
|
@@ -907,6 +1262,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
907
1262
|
const scrubbedIntent = rawIntent !== null ? ctx.scrubber(rawIntent) : null;
|
|
908
1263
|
const scrubbedExpected = rawExpected !== null ? ctx.scrubber(rawExpected) : null;
|
|
909
1264
|
const scrubbedWorkflow = rawWorkflow !== null ? ctx.scrubber(rawWorkflow) : null;
|
|
1265
|
+
const callId = uuidv72();
|
|
910
1266
|
const common = {
|
|
911
1267
|
tenant_id: ctx.tenantId,
|
|
912
1268
|
vendor_id: ctx.vendorId,
|
|
@@ -935,6 +1291,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
935
1291
|
ctx.sink,
|
|
936
1292
|
() => ToolCallStartEventSchema.parse({
|
|
937
1293
|
...common,
|
|
1294
|
+
call_id: callId,
|
|
938
1295
|
sequence_number: ctx.counter.next(sessionId),
|
|
939
1296
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
940
1297
|
payload: {
|
|
@@ -958,6 +1315,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
958
1315
|
ctx.sink,
|
|
959
1316
|
() => ToolCallErrorEventSchema.parse({
|
|
960
1317
|
...common,
|
|
1318
|
+
call_id: callId,
|
|
961
1319
|
sequence_number: ctx.counter.next(sessionId),
|
|
962
1320
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
963
1321
|
payload: {
|
|
@@ -975,6 +1333,7 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
975
1333
|
ctx.sink,
|
|
976
1334
|
() => ToolCallEndEventSchema.parse({
|
|
977
1335
|
...common,
|
|
1336
|
+
call_id: callId,
|
|
978
1337
|
sequence_number: ctx.counter.next(sessionId),
|
|
979
1338
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
980
1339
|
payload: { tool_name: toolName, result: ctx.scrubber(result), duration_ms: durationMs }
|
|
@@ -1108,8 +1467,25 @@ function wireEntry(name, entry, ctx) {
|
|
|
1108
1467
|
};
|
|
1109
1468
|
}
|
|
1110
1469
|
}
|
|
1111
|
-
function withBaton(server,
|
|
1112
|
-
|
|
1470
|
+
function withBaton(server, supplied = {}) {
|
|
1471
|
+
const disabledBy = captureDisabled();
|
|
1472
|
+
if (disabledBy !== null) {
|
|
1473
|
+
logDisabled(disabledBy, "withBaton");
|
|
1474
|
+
return new BatonHandle({
|
|
1475
|
+
// A sink the VENDOR constructed is held rather than dropped, so their
|
|
1476
|
+
// `handle.aclose()` still releases it — we took ownership of that object
|
|
1477
|
+
// the moment they passed it, and the switch does not undo that. Nothing
|
|
1478
|
+
// writes to it: nothing is wrapped.
|
|
1479
|
+
sink: supplied.sink ?? new DisabledSink(),
|
|
1480
|
+
// Empty, and a constant, because when the switch is on nothing was
|
|
1481
|
+
// resolved: inventing a session id would put a real identifier on a
|
|
1482
|
+
// handle whose whole meaning is that no events exist under it.
|
|
1483
|
+
vendorId: "",
|
|
1484
|
+
sessionId: "baton-disabled",
|
|
1485
|
+
annotationToolName: ""
|
|
1486
|
+
});
|
|
1487
|
+
}
|
|
1488
|
+
const config = resolveBatonConfig(supplied);
|
|
1113
1489
|
const sink = config.sink ?? new StdoutSink();
|
|
1114
1490
|
const annotationToolName = config.annotationToolName || `${config.vendorId}_annotate`;
|
|
1115
1491
|
const intentParamMode = config.intentParamMode ?? "optional";
|
|
@@ -1127,7 +1503,24 @@ function withBaton(server, config) {
|
|
|
1127
1503
|
consent_token: config.consentToken,
|
|
1128
1504
|
sequence_number: counter.next(sessionId),
|
|
1129
1505
|
captured_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1130
|
-
|
|
1506
|
+
// Deliberately the literal, NOT the ladder — and an earlier comment
|
|
1507
|
+
// here justified that by saying no call is in scope, which is false:
|
|
1508
|
+
// `maybeEmitSurfaceSnapshot` is the first line of `batonWrap`, so the
|
|
1509
|
+
// handshake has happened and tier 2's carrier is live in this closure.
|
|
1510
|
+
// The real reason is that the snapshot describes the VENDOR'S SURFACE,
|
|
1511
|
+
// which is the same whoever is calling. It is hashed and emitted at
|
|
1512
|
+
// most once per process per surface, so the client that happens to
|
|
1513
|
+
// trigger it is whichever one called first — attributing the surface to
|
|
1514
|
+
// that client would read as a fact about the surface and be an accident
|
|
1515
|
+
// of timing. Python hardcodes it from the same in-call position
|
|
1516
|
+
// (`_tool_wrap.py`), so this is parity, not a gap.
|
|
1517
|
+
//
|
|
1518
|
+
// ⚠ Consequence worth knowing: one session emits `surface_snapshot`
|
|
1519
|
+
// with `unknown` and everything else with the client's name, so a
|
|
1520
|
+
// consumer grouping on `agent_runtime` alone sees two runtimes for one
|
|
1521
|
+
// client. Intended; group surfaces on `(tenant_id, vendor_id,
|
|
1522
|
+
// surface_hash)`, which is what the Console's table is keyed on.
|
|
1523
|
+
agent_runtime: UNKNOWN_AGENT_RUNTIME,
|
|
1131
1524
|
payload: {
|
|
1132
1525
|
surface_hash: digest,
|
|
1133
1526
|
server_info: snapshot.server_info,
|
|
@@ -1173,9 +1566,8 @@ function withBaton(server, config) {
|
|
|
1173
1566
|
vendorId: config.vendorId,
|
|
1174
1567
|
consentToken: config.consentToken,
|
|
1175
1568
|
fallbackSessionId,
|
|
1176
|
-
|
|
1569
|
+
server,
|
|
1177
1570
|
scrubber,
|
|
1178
|
-
resolveSessionId: config.resolveSessionId,
|
|
1179
1571
|
annotationToolName,
|
|
1180
1572
|
intentParamMode,
|
|
1181
1573
|
paramRegistry: /* @__PURE__ */ new Map(),
|
|
@@ -1198,9 +1590,7 @@ function withBaton(server, config) {
|
|
|
1198
1590
|
vendorDisplayName: config.vendorDisplayName,
|
|
1199
1591
|
consentToken: ctx.consentToken,
|
|
1200
1592
|
fallbackSessionId: ctx.fallbackSessionId,
|
|
1201
|
-
defaultAgentRuntime: ctx.defaultAgentRuntime,
|
|
1202
1593
|
scrubber: ctx.scrubber,
|
|
1203
|
-
resolveSessionId: ctx.resolveSessionId,
|
|
1204
1594
|
annotationToolName: config.annotationToolName,
|
|
1205
1595
|
tracker
|
|
1206
1596
|
});
|
|
@@ -1227,6 +1617,7 @@ export {
|
|
|
1227
1617
|
AnnotationEventSchema,
|
|
1228
1618
|
AnnotationPayloadSchema,
|
|
1229
1619
|
BatonHandle,
|
|
1620
|
+
DEFAULT_CONSENT_TOKEN,
|
|
1230
1621
|
DEPTH_LIMIT,
|
|
1231
1622
|
EventSchema,
|
|
1232
1623
|
EventTypeSchema,
|