@goodtiming/baton-sdk 1.1.0 → 1.1.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/README.md +1 -0
- package/dist/index.cjs +142 -110
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -7
- package/dist/index.d.ts +9 -7
- package/dist/index.js +142 -110
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -129,6 +129,7 @@ Baton runs on Workers with the `nodejs_compat` flag, in a plain `fetch` handler
|
|
|
129
129
|
|
|
130
130
|
- **`settings: "console"` is not supported on Workers yet.** Baton warns once and the capture values in your config apply. The report tool it turns on stays on.
|
|
131
131
|
- **A tool that only computes can show a duration of 0 ms.** A Worker's clock moves only across I/O.
|
|
132
|
+
- **In tests, Baton may still send events after a test ends.** If a test puts back the real `fetch` afterwards, those sends go to your DSN's host. Leave `fetch` stubbed, or give Baton no `dsn` in tests.
|
|
132
133
|
|
|
133
134
|
## Without MCP: the library API
|
|
134
135
|
|
package/dist/index.cjs
CHANGED
|
@@ -53,18 +53,18 @@ __export(index_exports, {
|
|
|
53
53
|
module.exports = __toCommonJS(index_exports);
|
|
54
54
|
|
|
55
55
|
// src/version.ts
|
|
56
|
-
var SDK_VERSION = "ts-1.1.
|
|
56
|
+
var SDK_VERSION = "ts-1.1.1";
|
|
57
57
|
|
|
58
58
|
// src/events.ts
|
|
59
59
|
var import_zod = require("zod");
|
|
60
60
|
|
|
61
61
|
// src/uuid7.ts
|
|
62
|
-
var
|
|
62
|
+
var crypto = __toESM(require("crypto"), 1);
|
|
63
63
|
var COUNTER_MAX = 4095;
|
|
64
64
|
var lastMs = -1;
|
|
65
65
|
var counter = 0;
|
|
66
66
|
function uuid7() {
|
|
67
|
-
const bytes =
|
|
67
|
+
const bytes = crypto.randomBytes(18);
|
|
68
68
|
const seed = (bytes[16] << 8 | bytes[17]) & 2047;
|
|
69
69
|
const now = Date.now();
|
|
70
70
|
if (now > lastMs) {
|
|
@@ -443,13 +443,49 @@ function onWorkers() {
|
|
|
443
443
|
return typeof globalThis.WebSocketPair === "function" || typeof navigator !== "undefined" && navigator.userAgent === "Cloudflare-Workers";
|
|
444
444
|
}
|
|
445
445
|
var globalFetch = (input, init) => fetch(input, init);
|
|
446
|
+
function onProcess(event, listener) {
|
|
447
|
+
const target = typeof process !== "undefined" ? process : void 0;
|
|
448
|
+
if (typeof target?.on !== "function") return void 0;
|
|
449
|
+
try {
|
|
450
|
+
target.on(event, listener);
|
|
451
|
+
} catch {
|
|
452
|
+
return void 0;
|
|
453
|
+
}
|
|
454
|
+
return () => {
|
|
455
|
+
try {
|
|
456
|
+
target.off(event, listener);
|
|
457
|
+
} catch {
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
}
|
|
446
461
|
|
|
447
462
|
// src/warn.ts
|
|
448
463
|
function warn(message) {
|
|
449
464
|
if (onWorkers()) {
|
|
450
465
|
console.warn(message);
|
|
451
|
-
|
|
452
|
-
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
try {
|
|
469
|
+
if (typeof process !== "undefined" && typeof process.emitWarning === "function") return process.emitWarning(message);
|
|
470
|
+
} catch {
|
|
471
|
+
}
|
|
472
|
+
stderrLine(message);
|
|
473
|
+
}
|
|
474
|
+
function stderrLine(message) {
|
|
475
|
+
stderrLines([message]);
|
|
476
|
+
}
|
|
477
|
+
function stderrLines(lines) {
|
|
478
|
+
try {
|
|
479
|
+
const stderr = typeof process !== "undefined" ? process.stderr : void 0;
|
|
480
|
+
if (typeof stderr?.write === "function") return void stderr.write(lines.map((line) => `${line}
|
|
481
|
+
`).join(""));
|
|
482
|
+
} catch {
|
|
483
|
+
}
|
|
484
|
+
for (const line of lines) {
|
|
485
|
+
try {
|
|
486
|
+
console.warn(line);
|
|
487
|
+
} catch {
|
|
488
|
+
}
|
|
453
489
|
}
|
|
454
490
|
}
|
|
455
491
|
function printable(err) {
|
|
@@ -579,8 +615,8 @@ function identityScrub(value) {
|
|
|
579
615
|
}
|
|
580
616
|
|
|
581
617
|
// src/sinks.ts
|
|
582
|
-
var
|
|
583
|
-
var
|
|
618
|
+
var fs = __toESM(require("fs"), 1);
|
|
619
|
+
var fsp = __toESM(require("fs/promises"), 1);
|
|
584
620
|
|
|
585
621
|
// src/_text.ts
|
|
586
622
|
var WHITE_SPACE = /^\p{White_Space}$/u;
|
|
@@ -702,14 +738,9 @@ async function safeWrite(sink, event) {
|
|
|
702
738
|
} catch (err) {
|
|
703
739
|
if (isSinkClosed(err)) return droppedAfterClose(sink);
|
|
704
740
|
PROCESS_DROPS.add("write_failed");
|
|
705
|
-
|
|
706
|
-
`);
|
|
741
|
+
stderrLine(`baton: sink.write failed; event dropped, tool call continues: ${printable(err)}`);
|
|
707
742
|
}
|
|
708
743
|
}
|
|
709
|
-
function stderrLine(message) {
|
|
710
|
-
process.stderr.write(`${message}
|
|
711
|
-
`);
|
|
712
|
-
}
|
|
713
744
|
function isTimerMs(value) {
|
|
714
745
|
return typeof value === "number" && value >= 0 && value <= MAX_TIMER_MS;
|
|
715
746
|
}
|
|
@@ -749,11 +780,13 @@ var StdoutSink = class {
|
|
|
749
780
|
stream;
|
|
750
781
|
closed = false;
|
|
751
782
|
constructor(options = {}) {
|
|
752
|
-
this.stream = options.stream
|
|
783
|
+
this.stream = options.stream;
|
|
753
784
|
}
|
|
754
785
|
async write(event) {
|
|
755
786
|
if (this.closed) throw new SinkClosedError("StdoutSink is closed");
|
|
756
|
-
|
|
787
|
+
const line = JSON.stringify(event);
|
|
788
|
+
if (this.stream) this.stream.write(line + "\n");
|
|
789
|
+
else stderrLine(line);
|
|
757
790
|
}
|
|
758
791
|
async flush() {
|
|
759
792
|
}
|
|
@@ -775,7 +808,7 @@ var FileSink = class {
|
|
|
775
808
|
if (this.closed) throw new SinkClosedError("FileSink is closed");
|
|
776
809
|
const line = JSON.stringify(event) + "\n";
|
|
777
810
|
const written = this.last.then(async () => {
|
|
778
|
-
this.file ??= await
|
|
811
|
+
this.file ??= await fsp.open(this.path, "a");
|
|
779
812
|
await this.file.appendFile(line);
|
|
780
813
|
});
|
|
781
814
|
this.last = written.catch(() => void 0);
|
|
@@ -832,30 +865,31 @@ var later = (run) => {
|
|
|
832
865
|
else setTimeout(run, 0);
|
|
833
866
|
};
|
|
834
867
|
var withUnwrittenEvents = /* @__PURE__ */ new Set();
|
|
835
|
-
var
|
|
868
|
+
var exitTriedOn = /* @__PURE__ */ new WeakSet();
|
|
836
869
|
function hookExit() {
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
} catch {
|
|
849
|
-
}
|
|
870
|
+
const target = typeof process !== "undefined" ? process : void 0;
|
|
871
|
+
if (target === void 0 || exitTriedOn.has(target)) return;
|
|
872
|
+
exitTriedOn.add(target);
|
|
873
|
+
onProcess("exit", () => {
|
|
874
|
+
try {
|
|
875
|
+
let unsure = 0;
|
|
876
|
+
for (const sink of withUnwrittenEvents) unsure += sink.handOffAtExit();
|
|
877
|
+
if (unsure > 0) stderrLine(`baton: process exiting; ${unsure} event(s) may not have been written.`);
|
|
878
|
+
} catch {
|
|
879
|
+
}
|
|
880
|
+
});
|
|
850
881
|
}
|
|
851
882
|
function writeNow(sink, events) {
|
|
852
|
-
const
|
|
883
|
+
const json = () => events.map((event) => JSON.stringify(event));
|
|
884
|
+
const lines = () => json().map((line) => line + "\n").join("");
|
|
853
885
|
if (sink instanceof StdoutSink) {
|
|
854
886
|
const stream = sink["stream"];
|
|
855
|
-
if (stream !== process.stdout && stream !== process.stderr) return false;
|
|
856
|
-
if (
|
|
887
|
+
if (stream !== void 0 && stream !== process.stdout && stream !== process.stderr) return false;
|
|
888
|
+
if (sink["closed"]) return true;
|
|
889
|
+
if (stream === void 0) stderrLines(json());
|
|
890
|
+
else stream.write(lines());
|
|
857
891
|
} else if (sink instanceof FileSink) {
|
|
858
|
-
if (!sink["closed"])
|
|
892
|
+
if (!sink["closed"]) fs.appendFileSync(sink["path"], lines());
|
|
859
893
|
} else return false;
|
|
860
894
|
return true;
|
|
861
895
|
}
|
|
@@ -1113,6 +1147,8 @@ var HttpSink = class _HttpSink {
|
|
|
1113
1147
|
/** Set for the life of the sink by a 404 or 405 on the batch path. */
|
|
1114
1148
|
batchless = false;
|
|
1115
1149
|
exitHooks = null;
|
|
1150
|
+
/** The `process` the exit hooks were last tried on, so a refusal is not retried per write. */
|
|
1151
|
+
exitHooksTriedOn;
|
|
1116
1152
|
/** The least time between two of this sink's loss reports. */
|
|
1117
1153
|
static reportIntervalMs = 6e4;
|
|
1118
1154
|
/** Totals only grow, so their sum tells whether they changed. */
|
|
@@ -1210,11 +1246,8 @@ var HttpSink = class _HttpSink {
|
|
|
1210
1246
|
warn(`baton: the sink closed with ${unsent} event(s) unsent.`);
|
|
1211
1247
|
}
|
|
1212
1248
|
this.warnUnloggedRefusals("the sink closed");
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
process.off("exit", this.exitHooks.exit);
|
|
1216
|
-
this.exitHooks = null;
|
|
1217
|
-
}
|
|
1249
|
+
this.exitHooks?.off();
|
|
1250
|
+
this.exitHooks = null;
|
|
1218
1251
|
}
|
|
1219
1252
|
/** SPEC §11.2 rule 7: an explicit flush or close returns within `ms` and
|
|
1220
1253
|
* never rejects. While an earlier wait's drain is still stuck, a flush
|
|
@@ -1308,7 +1341,9 @@ var HttpSink = class _HttpSink {
|
|
|
1308
1341
|
* both hooks: a vendor needing that guarantee must call `aclose()` from
|
|
1309
1342
|
* their own handler. */
|
|
1310
1343
|
ensureExitHooksRegistered() {
|
|
1311
|
-
|
|
1344
|
+
const target = typeof process !== "undefined" ? process : void 0;
|
|
1345
|
+
if (this.exitHooks || target === void 0 || target === this.exitHooksTriedOn) return;
|
|
1346
|
+
this.exitHooksTriedOn = target;
|
|
1312
1347
|
const beforeExit = () => {
|
|
1313
1348
|
if (this.unsent === 0 || this.exitPassRan) {
|
|
1314
1349
|
this.warnUnloggedRefusals("process exiting");
|
|
@@ -1326,9 +1361,14 @@ var HttpSink = class _HttpSink {
|
|
|
1326
1361
|
} catch {
|
|
1327
1362
|
}
|
|
1328
1363
|
};
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1364
|
+
const offBeforeExit = onProcess("beforeExit", beforeExit);
|
|
1365
|
+
const offExit = onProcess("exit", exit);
|
|
1366
|
+
if (offBeforeExit === void 0 || offExit === void 0) {
|
|
1367
|
+
offBeforeExit?.();
|
|
1368
|
+
offExit?.();
|
|
1369
|
+
return;
|
|
1370
|
+
}
|
|
1371
|
+
this.exitHooks = { beforeExit, exit, off: () => (offBeforeExit(), offExit()) };
|
|
1332
1372
|
}
|
|
1333
1373
|
/** One attempt per batch, no retry, within `shutdownFlushTimeoutMs` in
|
|
1334
1374
|
* total (SPEC §11.2); what is left is reported, not sent. */
|
|
@@ -1709,7 +1749,7 @@ var HttpSink = class _HttpSink {
|
|
|
1709
1749
|
};
|
|
1710
1750
|
|
|
1711
1751
|
// src/integrations/mcp/withBaton.ts
|
|
1712
|
-
var
|
|
1752
|
+
var asyncHooks = __toESM(require("async_hooks"), 1);
|
|
1713
1753
|
|
|
1714
1754
|
// src/integrations/mcp/annotation.ts
|
|
1715
1755
|
var import_zod3 = require("zod");
|
|
@@ -1852,7 +1892,7 @@ function endsInOurs(current2, vendor) {
|
|
|
1852
1892
|
const rest = current2.slice(prefix.length);
|
|
1853
1893
|
return OURS_STARTS.some((start) => start.test(rest));
|
|
1854
1894
|
}
|
|
1855
|
-
function withVendorInstructions(server, ours, warn3 =
|
|
1895
|
+
function withVendorInstructions(server, ours, warn3 = stderrLine) {
|
|
1856
1896
|
const holder = server;
|
|
1857
1897
|
const current2 = holder._instructions;
|
|
1858
1898
|
let vendor = VENDOR_INSTRUCTIONS in holder ? holder[VENDOR_INSTRUCTIONS] : current2;
|
|
@@ -1866,8 +1906,7 @@ function withVendorInstructions(server, ours, warn3 = (message) => process.stder
|
|
|
1866
1906
|
${ours}`;
|
|
1867
1907
|
if (combined.length > CLAUDE_CODE_TRUNCATION_CAP) {
|
|
1868
1908
|
warn3(
|
|
1869
|
-
`baton: the server instructions are ${combined.length} chars (yours ${vendor.length}, Baton's ${ours.length}); Claude Code cuts them at ~${CLAUDE_CODE_TRUNCATION_CAP}, so the end of Baton's text is lost there
|
|
1870
|
-
`
|
|
1909
|
+
`baton: the server instructions are ${combined.length} chars (yours ${vendor.length}, Baton's ${ours.length}); Claude Code cuts them at ~${CLAUDE_CODE_TRUNCATION_CAP}, so the end of Baton's text is lost there`
|
|
1871
1910
|
);
|
|
1872
1911
|
}
|
|
1873
1912
|
return combined;
|
|
@@ -1974,8 +2013,7 @@ async function emit(sink, build) {
|
|
|
1974
2013
|
event = build();
|
|
1975
2014
|
} catch (err) {
|
|
1976
2015
|
PROCESS_DROPS.add("encode_failed");
|
|
1977
|
-
|
|
1978
|
-
`);
|
|
2016
|
+
stderrLine(`baton: event construction failed; event dropped: ${printable(err)}`);
|
|
1979
2017
|
return;
|
|
1980
2018
|
}
|
|
1981
2019
|
await safeWrite(sink, event);
|
|
@@ -2389,6 +2427,15 @@ function advertiseIntentParamsRequired(result, names, isInjected) {
|
|
|
2389
2427
|
});
|
|
2390
2428
|
return changed ? { ...result, tools: advertised } : result;
|
|
2391
2429
|
}
|
|
2430
|
+
function withToolInputSchema(result, name, inputSchema2) {
|
|
2431
|
+
const tools = result?.tools;
|
|
2432
|
+
if (!Array.isArray(tools)) return result;
|
|
2433
|
+
const at = tools.findIndex((tool) => isRecord(tool) && tool.name === name);
|
|
2434
|
+
if (at === -1) return result;
|
|
2435
|
+
const served = [...tools];
|
|
2436
|
+
served[at] = { ...tools[at], inputSchema: inputSchema2 };
|
|
2437
|
+
return { ...result, tools: served };
|
|
2438
|
+
}
|
|
2392
2439
|
function standardInputJsonSchema(schema) {
|
|
2393
2440
|
try {
|
|
2394
2441
|
const standard = schema?.["~standard"];
|
|
@@ -2415,9 +2462,8 @@ function scrubOrNull(scrubber, value, field) {
|
|
|
2415
2462
|
} catch (err) {
|
|
2416
2463
|
if (!warned2.has(field)) {
|
|
2417
2464
|
warned2.add(field);
|
|
2418
|
-
|
|
2419
|
-
`baton: scrubber threw on ${field}; that field is dropped, tool call continues. Further failures on this field will be silent: ${printable(err)}
|
|
2420
|
-
`
|
|
2465
|
+
stderrLine(
|
|
2466
|
+
`baton: scrubber threw on ${field}; that field is dropped, tool call continues. Further failures on this field will be silent: ${printable(err)}`
|
|
2421
2467
|
);
|
|
2422
2468
|
}
|
|
2423
2469
|
return null;
|
|
@@ -2552,7 +2598,8 @@ var ANNOTATION_INPUT_SHAPE = {
|
|
|
2552
2598
|
// not rejected by validation.
|
|
2553
2599
|
tool_name: import_zod3.z.string().nullable().optional(),
|
|
2554
2600
|
suggested_improvement: import_zod3.z.string().optional(),
|
|
2555
|
-
|
|
2601
|
+
// zod renders "any value" as `{}`, which some MCP clients refuse (reportToolSchema.test.ts).
|
|
2602
|
+
context: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()).meta({ additionalProperties: true }).optional()
|
|
2556
2603
|
};
|
|
2557
2604
|
function isReport(whatHappened) {
|
|
2558
2605
|
return typeof whatHappened === "string" && !isBlank(whatHappened);
|
|
@@ -2923,7 +2970,7 @@ function scrubbedErrorBody(scrubber, text) {
|
|
|
2923
2970
|
}
|
|
2924
2971
|
|
|
2925
2972
|
// src/integrations/mcp/captureSettings.ts
|
|
2926
|
-
var
|
|
2973
|
+
var crypto2 = __toESM(require("crypto"), 1);
|
|
2927
2974
|
var SETTINGS_PATH = "/v0/settings";
|
|
2928
2975
|
var TENANT_HEADER = "X-Baton-Tenant-Id";
|
|
2929
2976
|
var SETTINGS_MODES = ["local", "console"];
|
|
@@ -2935,7 +2982,7 @@ var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required"]);
|
|
|
2935
2982
|
var REPORT_MODES = /* @__PURE__ */ new Set(["on", "off"]);
|
|
2936
2983
|
var RESULT_CAPTURE_MODE_SET = new Set(RESULT_CAPTURE_MODES);
|
|
2937
2984
|
function shareBucket(vendorId2, key) {
|
|
2938
|
-
const digest =
|
|
2985
|
+
const digest = crypto2.createHash("sha256").update(`${vendorId2}
|
|
2939
2986
|
${key}`, "utf-8").digest();
|
|
2940
2987
|
return Number(digest.readBigUInt64BE(0) % 10000n);
|
|
2941
2988
|
}
|
|
@@ -3394,8 +3441,7 @@ var PerCallerSurface = class {
|
|
|
3394
3441
|
this.options.writeInstructions(pointerOn ? this.options.instructions : INTENT_INSTRUCTIONS);
|
|
3395
3442
|
this.pointerOn = pointerOn;
|
|
3396
3443
|
} catch (err) {
|
|
3397
|
-
|
|
3398
|
-
`);
|
|
3444
|
+
stderrLine(`baton: could not update the server instructions for new settings: ${printable(err)}`);
|
|
3399
3445
|
}
|
|
3400
3446
|
}
|
|
3401
3447
|
/** `result` with the caller's goal and reports settings; as served on any failure. */
|
|
@@ -3418,8 +3464,7 @@ var PerCallerSurface = class {
|
|
|
3418
3464
|
}
|
|
3419
3465
|
return { ...result, tools: served };
|
|
3420
3466
|
} catch (err) {
|
|
3421
|
-
|
|
3422
|
-
`);
|
|
3467
|
+
stderrLine(`baton: could not apply capture settings to the tool list: ${printable(err)}`);
|
|
3423
3468
|
return result;
|
|
3424
3469
|
}
|
|
3425
3470
|
}
|
|
@@ -3478,10 +3523,10 @@ var ServerCounter = class extends SessionCounter {
|
|
|
3478
3523
|
};
|
|
3479
3524
|
|
|
3480
3525
|
// src/integrations/mcp/surface.ts
|
|
3481
|
-
var
|
|
3526
|
+
var crypto3 = __toESM(require("crypto"), 1);
|
|
3482
3527
|
function surfaceHash(surface) {
|
|
3483
3528
|
const canonical = canonicalJson(surface);
|
|
3484
|
-
const digest =
|
|
3529
|
+
const digest = crypto3.createHash("sha256").update(canonical, "utf-8").digest("hex");
|
|
3485
3530
|
return `sha256:${digest}`;
|
|
3486
3531
|
}
|
|
3487
3532
|
function canonicalJson(value) {
|
|
@@ -3543,7 +3588,7 @@ var BATON_WRAPPED = /* @__PURE__ */ Symbol("batonWrapped");
|
|
|
3543
3588
|
var wired = /* @__PURE__ */ new WeakSet();
|
|
3544
3589
|
var CLEAN_OUTCOME = { failed: false, raised: false, result: void 0 };
|
|
3545
3590
|
var INTENT_PARAM_NAMES = [USER_GOAL_PARAM_NAME, ...RETIRED_PARAM_NAMES];
|
|
3546
|
-
var callSlots = new
|
|
3591
|
+
var callSlots = new asyncHooks.AsyncLocalStorage();
|
|
3547
3592
|
var SurfaceState = class {
|
|
3548
3593
|
constructor(serverMeta, sentSurfaces) {
|
|
3549
3594
|
this.serverMeta = serverMeta;
|
|
@@ -3834,9 +3879,8 @@ function extractGoalParam(args, paramName, toolName, dispositions, unlisted) {
|
|
|
3834
3879
|
if (disposition === "native") return null;
|
|
3835
3880
|
if (disposition === void 0 && unlisted === "leave") return null;
|
|
3836
3881
|
if (disposition === void 0 && unlisted === "warn") {
|
|
3837
|
-
|
|
3838
|
-
`baton: stripping ${paramName} from unlisted tool ${JSON.stringify(toolName)} (cold registry)
|
|
3839
|
-
`
|
|
3882
|
+
stderrLine(
|
|
3883
|
+
`baton: stripping ${paramName} from unlisted tool ${JSON.stringify(toolName)} (cold registry)`
|
|
3840
3884
|
);
|
|
3841
3885
|
}
|
|
3842
3886
|
return popText(args, paramName);
|
|
@@ -3870,8 +3914,7 @@ async function maybeEmitSurfaceSnapshot(ctx) {
|
|
|
3870
3914
|
snapshot = ctx.surfaceState.buildSnapshot();
|
|
3871
3915
|
digest = surfaceHash(snapshot);
|
|
3872
3916
|
} catch (err) {
|
|
3873
|
-
|
|
3874
|
-
`);
|
|
3917
|
+
stderrLine(`baton: surface snapshot capture failed: ${String(err)}`);
|
|
3875
3918
|
return;
|
|
3876
3919
|
}
|
|
3877
3920
|
const sent = ctx.surfaceState.sentSurfaces;
|
|
@@ -3939,16 +3982,16 @@ function installRequestSeam(lowLevel, method, seamTag, wrap) {
|
|
|
3939
3982
|
return registered;
|
|
3940
3983
|
};
|
|
3941
3984
|
}
|
|
3942
|
-
function installToolsListSeam(lowLevel, ctx) {
|
|
3985
|
+
function installToolsListSeam(lowLevel, ctx, listsOwnReportTool) {
|
|
3943
3986
|
const names = [...requiredParamNames(ctx.intentParamMode), TOOL_NAME_PARAM_NAME];
|
|
3944
3987
|
const isInjected = (toolName, paramName) => toolName === ctx.annotationToolName ? paramName === TOOL_NAME_PARAM_NAME : ctx.paramRegistry.get(toolName)?.[paramName] === "injected";
|
|
3945
3988
|
const advertise = (result) => {
|
|
3946
3989
|
try {
|
|
3947
|
-
|
|
3990
|
+
const served = listsOwnReportTool || ctx.annotationToolName === "" ? result : withToolInputSchema(result, ctx.annotationToolName, annotationInputJsonSchema(lowLevelMajor(lowLevel)));
|
|
3991
|
+
return advertiseIntentParamsRequired(served, names, isInjected);
|
|
3948
3992
|
} catch (err) {
|
|
3949
|
-
|
|
3950
|
-
`baton: tools/list advertisement failed; serving the SDK's own result: ${String(err)}
|
|
3951
|
-
`
|
|
3993
|
+
stderrLine(
|
|
3994
|
+
`baton: tools/list advertisement failed; serving the SDK's own result: ${String(err)}`
|
|
3952
3995
|
);
|
|
3953
3996
|
return result;
|
|
3954
3997
|
}
|
|
@@ -3972,14 +4015,12 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
|
|
|
3972
4015
|
known.unread.add(name);
|
|
3973
4016
|
warnOnce2(
|
|
3974
4017
|
`tool-unread:${name}`,
|
|
3975
|
-
`baton: could not read tool ${JSON.stringify(name)}; its calls keep user_goal until Baton can read it: ${printable(err)}
|
|
3976
|
-
`
|
|
4018
|
+
`baton: could not read tool ${JSON.stringify(name)}; its calls keep user_goal until Baton can read it: ${printable(err)}`
|
|
3977
4019
|
);
|
|
3978
4020
|
};
|
|
3979
4021
|
const skipNote = (name, err) => {
|
|
3980
4022
|
if (known.unread.has(name)) return;
|
|
3981
|
-
warnOnce2(`tool-unnoted:${name}`, `baton: could not note tool ${JSON.stringify(name)}; Baton adds no user_goal to it: ${printable(err)}
|
|
3982
|
-
`);
|
|
4023
|
+
warnOnce2(`tool-unnoted:${name}`, `baton: could not note tool ${JSON.stringify(name)}; Baton adds no user_goal to it: ${printable(err)}`);
|
|
3983
4024
|
};
|
|
3984
4025
|
const keepGoalIfDeclared = (tools) => {
|
|
3985
4026
|
for (const { name, inputSchema: inputSchema2 } of tools) {
|
|
@@ -3999,8 +4040,7 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
|
|
|
3999
4040
|
if (surface && reportTool2 !== void 0 && tools.some((tool) => tool.name === reportTool2?.name)) {
|
|
4000
4041
|
warnOnce2(
|
|
4001
4042
|
"report-tool-name-taken",
|
|
4002
|
-
`baton: the server lists its own tool ${JSON.stringify(reportTool2.name)}, so Baton's report tool is not added; set annotationToolName to another name
|
|
4003
|
-
`
|
|
4043
|
+
`baton: the server lists its own tool ${JSON.stringify(reportTool2.name)}, so Baton's report tool is not added; set annotationToolName to another name`
|
|
4004
4044
|
);
|
|
4005
4045
|
reportTool2 = void 0;
|
|
4006
4046
|
ctx.annotationToolName = "";
|
|
@@ -4031,8 +4071,7 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
|
|
|
4031
4071
|
} catch (err) {
|
|
4032
4072
|
warnOnce2(
|
|
4033
4073
|
"tools-unread",
|
|
4034
|
-
`baton: could not read the tools option; Baton learns the tools from tools/list: ${printable(err)}
|
|
4035
|
-
`
|
|
4074
|
+
`baton: could not read the tools option; Baton learns the tools from tools/list: ${printable(err)}`
|
|
4036
4075
|
);
|
|
4037
4076
|
}
|
|
4038
4077
|
}
|
|
@@ -4051,8 +4090,7 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
|
|
|
4051
4090
|
const firstPage = args[0]?.params?.cursor === void 0;
|
|
4052
4091
|
return servedToolList(result, listed, ctx, firstPage && reportTool2 ? listedReportTool : void 0);
|
|
4053
4092
|
} catch (err) {
|
|
4054
|
-
|
|
4055
|
-
`);
|
|
4093
|
+
stderrLine(`baton: tools/list capture failed; serving the vendor's own result: ${printable(err)}`);
|
|
4056
4094
|
return result;
|
|
4057
4095
|
}
|
|
4058
4096
|
});
|
|
@@ -4076,8 +4114,7 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
|
|
|
4076
4114
|
if (unlisted === "goalOnly" && USER_GOAL_PARAM_NAME in toolArgs && !known.served.has(name) && !known.givenTools.some((tool) => tool.name === name)) {
|
|
4077
4115
|
warnOnce2(
|
|
4078
4116
|
"user-goal-unlisted",
|
|
4079
|
-
`baton: ${JSON.stringify(name)} is not in any tool list this process has read, so Baton took user_goal off it without knowing whether the tool declares its own; pass your tools as the tools option
|
|
4080
|
-
`
|
|
4117
|
+
`baton: ${JSON.stringify(name)} is not in any tool list this process has read, so Baton took user_goal off it without knowing whether the tool declares its own; pass your tools as the tools option`
|
|
4081
4118
|
);
|
|
4082
4119
|
}
|
|
4083
4120
|
if (request?.params?.task !== void 0) {
|
|
@@ -4213,8 +4250,7 @@ async function openLifecycleCall(ctx, spec, requestParams, extra) {
|
|
|
4213
4250
|
}
|
|
4214
4251
|
};
|
|
4215
4252
|
} catch (err) {
|
|
4216
|
-
|
|
4217
|
-
`);
|
|
4253
|
+
stderrLine(`baton: ${spec.method} capture failed: ${String(err)}`);
|
|
4218
4254
|
return null;
|
|
4219
4255
|
}
|
|
4220
4256
|
}
|
|
@@ -4390,8 +4426,7 @@ async function settleCall(slot, outcome, args, toolEntry, ctx) {
|
|
|
4390
4426
|
await emitRequestSideFailure(outcome, args, slot, toolEntry, ctx);
|
|
4391
4427
|
} catch (err) {
|
|
4392
4428
|
PROCESS_DROPS.add("encode_failed");
|
|
4393
|
-
|
|
4394
|
-
`);
|
|
4429
|
+
stderrLine(`baton: settling a tool call failed; events dropped: ${String(err)}`);
|
|
4395
4430
|
}
|
|
4396
4431
|
}
|
|
4397
4432
|
async function emitRequestSideFailure(outcome, args, slot, toolEntry, ctx) {
|
|
@@ -4617,6 +4652,11 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned3, co
|
|
|
4617
4652
|
const annotationToolName = reports ? resolveAnnotationToolName(serverName, { ...config, vendorDisplayName: reports.vendorDisplayName }) : "";
|
|
4618
4653
|
const ours = reports ? buildServerInstructions({ vendorDisplayName: reports.vendorDisplayName, annotationToolName }) : INTENT_INSTRUCTIONS;
|
|
4619
4654
|
if (unconfigured) return inertHandle(new DisabledSink(), "baton-unconfigured");
|
|
4655
|
+
if (shape === "mcpServer" && reports && internals._registeredTools?.[annotationToolName]) {
|
|
4656
|
+
throw new Error(
|
|
4657
|
+
`Tool ${annotationToolName} is already registered, so Baton cannot add its report tool. If it is your tool, set annotationToolName to another name; if Baton is already on this server, call withBaton once.`
|
|
4658
|
+
);
|
|
4659
|
+
}
|
|
4620
4660
|
const given = config.sink ?? new StdoutSink();
|
|
4621
4661
|
const sink = offTheCall(given);
|
|
4622
4662
|
const intentParamMode = config.intentParamMode ?? DEFAULT_INTENT_PARAM_MODE;
|
|
@@ -4654,7 +4694,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned3, co
|
|
|
4654
4694
|
const warnOnce2 = (key, message) => {
|
|
4655
4695
|
if (warned3.has(key)) return;
|
|
4656
4696
|
warned3.add(key);
|
|
4657
|
-
|
|
4697
|
+
stderrLine(message);
|
|
4658
4698
|
};
|
|
4659
4699
|
const dropSubagentClause = (name) => {
|
|
4660
4700
|
const current2 = lowLevel._instructions;
|
|
@@ -4667,8 +4707,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned3, co
|
|
|
4667
4707
|
}
|
|
4668
4708
|
warnOnce2(
|
|
4669
4709
|
"native-user-goal",
|
|
4670
|
-
`baton: tool ${JSON.stringify(name)} declares its own user_goal, so the subagent sentence was removed from the server instructions; a session that already started keeps it
|
|
4671
|
-
`
|
|
4710
|
+
`baton: tool ${JSON.stringify(name)} declares its own user_goal, so the subagent sentence was removed from the server instructions; a session that already started keeps it`
|
|
4672
4711
|
);
|
|
4673
4712
|
};
|
|
4674
4713
|
const isV2Server = typeof internals.toolInputSchemaJson === "function";
|
|
@@ -4765,7 +4804,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned3, co
|
|
|
4765
4804
|
() => rewriteInstructions(INTENT_INSTRUCTIONS)
|
|
4766
4805
|
);
|
|
4767
4806
|
}
|
|
4768
|
-
installToolsListSeam(lowLevel, ctx);
|
|
4807
|
+
installToolsListSeam(lowLevel, ctx, shape === "lowLevel");
|
|
4769
4808
|
if (shape === "mcpServer") installToolsCallSeam(
|
|
4770
4809
|
lowLevel,
|
|
4771
4810
|
// ⚠ `Object.hasOwn`, never a bare index. `_registeredTools` is a plain
|
|
@@ -4811,9 +4850,8 @@ function reportsFor(config) {
|
|
|
4811
4850
|
}
|
|
4812
4851
|
function warnAboutSettings(config) {
|
|
4813
4852
|
if (config.reportMode !== "on" && config.annotationToolName !== void 0) {
|
|
4814
|
-
|
|
4815
|
-
`baton: annotationToolName=${JSON.stringify(config.annotationToolName)} has no effect with reportMode: "off", because no annotation tool is registered
|
|
4816
|
-
`
|
|
4853
|
+
stderrLine(
|
|
4854
|
+
`baton: annotationToolName=${JSON.stringify(config.annotationToolName)} has no effect with reportMode: "off", because no annotation tool is registered`
|
|
4817
4855
|
);
|
|
4818
4856
|
}
|
|
4819
4857
|
if (config.resultCaptureMode === WITHHELD) {
|
|
@@ -4855,7 +4893,7 @@ function principalFromOAuthEmail(context) {
|
|
|
4855
4893
|
}
|
|
4856
4894
|
|
|
4857
4895
|
// src/library/session.ts
|
|
4858
|
-
var
|
|
4896
|
+
var asyncHooks2 = __toESM(require("async_hooks"), 1);
|
|
4859
4897
|
var TRANSPORT_LIBRARY = "library";
|
|
4860
4898
|
var TURN_ID_META_KEY = "baton/turn_id";
|
|
4861
4899
|
function sessionEnvelope(session) {
|
|
@@ -4871,8 +4909,7 @@ function sessionEnvelope(session) {
|
|
|
4871
4909
|
};
|
|
4872
4910
|
}
|
|
4873
4911
|
function warn2(message) {
|
|
4874
|
-
|
|
4875
|
-
`);
|
|
4912
|
+
stderrLine(`baton: ${message}`);
|
|
4876
4913
|
}
|
|
4877
4914
|
function vendorId(name, value) {
|
|
4878
4915
|
const cleaned = typeof value === "string" ? stripWhiteSpace(value) : "";
|
|
@@ -4909,7 +4946,7 @@ function newSession(values, scrubber, disabled) {
|
|
|
4909
4946
|
runtimeMeta: runtimeMeta(meta, vendorId("turnId", values.turnId), scrubber)
|
|
4910
4947
|
};
|
|
4911
4948
|
}
|
|
4912
|
-
var current = new
|
|
4949
|
+
var current = new asyncHooks2.AsyncLocalStorage();
|
|
4913
4950
|
function runInSession(owner, session, fn) {
|
|
4914
4951
|
return current.run({ owner, session }, fn);
|
|
4915
4952
|
}
|
|
@@ -4924,8 +4961,7 @@ function serveTools(tools, options) {
|
|
|
4924
4961
|
try {
|
|
4925
4962
|
served = tools.map(servedEntry);
|
|
4926
4963
|
} catch (err) {
|
|
4927
|
-
|
|
4928
|
-
`);
|
|
4964
|
+
stderrLine(`baton: could not add user_goal to the tool list; serving it unchanged: ${printable(err)}`);
|
|
4929
4965
|
return { served: tools, surface: null };
|
|
4930
4966
|
}
|
|
4931
4967
|
try {
|
|
@@ -4944,8 +4980,7 @@ function serveTools(tools, options) {
|
|
|
4944
4980
|
);
|
|
4945
4981
|
return { served, surface: { surface, digest: surfaceHash(surface) } };
|
|
4946
4982
|
} catch (err) {
|
|
4947
|
-
|
|
4948
|
-
`);
|
|
4983
|
+
stderrLine(`baton: surface snapshot capture failed: ${printable(err)}`);
|
|
4949
4984
|
return { served, surface: null };
|
|
4950
4985
|
}
|
|
4951
4986
|
}
|
|
@@ -5109,8 +5144,7 @@ var Core = class {
|
|
|
5109
5144
|
});
|
|
5110
5145
|
} catch (err) {
|
|
5111
5146
|
PROCESS_DROPS.add("encode_failed");
|
|
5112
|
-
|
|
5113
|
-
`);
|
|
5147
|
+
stderrLine(`baton: surface snapshot dropped; it does not fit the schema: ${printable(err)}`);
|
|
5114
5148
|
return Promise.resolve();
|
|
5115
5149
|
}
|
|
5116
5150
|
return safeWrite(this.sink, event);
|
|
@@ -5168,7 +5202,7 @@ var Trace = class _Trace {
|
|
|
5168
5202
|
observed(result, failure) {
|
|
5169
5203
|
if ((this.#result !== UNSET || this.#error !== null) && !this.#observedWarned) {
|
|
5170
5204
|
this.#observedWarned = true;
|
|
5171
|
-
|
|
5205
|
+
stderrLine("baton: Trace.observed() called more than once; the last call wins.");
|
|
5172
5206
|
}
|
|
5173
5207
|
this.#result = UNSET;
|
|
5174
5208
|
this.#error = null;
|
|
@@ -5188,8 +5222,7 @@ var Trace = class _Trace {
|
|
|
5188
5222
|
try {
|
|
5189
5223
|
this.#result = this.#core.scrubber(result);
|
|
5190
5224
|
} catch (err) {
|
|
5191
|
-
|
|
5192
|
-
`);
|
|
5225
|
+
stderrLine(`baton: scrubber threw on a result; that call's end event is dropped: ${printable(err)}`);
|
|
5193
5226
|
this.#result = SCRUB_FAILED;
|
|
5194
5227
|
}
|
|
5195
5228
|
}
|
|
@@ -5335,8 +5368,7 @@ var Client = class {
|
|
|
5335
5368
|
try {
|
|
5336
5369
|
block = newSession(options, this.#core.scrubber, this.#core.disabled);
|
|
5337
5370
|
} catch (err) {
|
|
5338
|
-
|
|
5339
|
-
`);
|
|
5371
|
+
stderrLine(`baton: session values could not be read; using a minted session: ${printable(err)}`);
|
|
5340
5372
|
block = newSession({}, this.#core.scrubber, this.#core.disabled);
|
|
5341
5373
|
}
|
|
5342
5374
|
return runInSession(this, block, fn);
|