@goodtiming/baton-sdk 1.2.1 → 1.2.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/dist/index.cjs CHANGED
@@ -53,7 +53,7 @@ __export(index_exports, {
53
53
  module.exports = __toCommonJS(index_exports);
54
54
 
55
55
  // src/version.ts
56
- var SDK_VERSION = "ts-1.2.1";
56
+ var SDK_VERSION = "ts-1.2.2";
57
57
 
58
58
  // src/events.ts
59
59
  var import_zod = require("zod");
@@ -1931,18 +1931,18 @@ Fields:
1931
1931
  - suggested_improvement: one concrete sentence on the product change that would have helped.
1932
1932
  - context: supplementary info not covered above. Common keys: plan, alternatives_considered, likely_cause, user_impact, error_class, downstream_blocked, confidence_in_intent. When no tool covers the request also missing_capability_field and requested_capability.`;
1933
1933
  var CLAUDE_CODE_TRUNCATION_CAP = 2087;
1934
- var INSTRUCTIONS_LENGTH_CAP = 1500;
1934
+ var BATON_SENTENCE_LENGTH_CAP = 1500;
1935
1935
  function buildReportPointer(options) {
1936
1936
  return REPORT_POINTER_TEMPLATE(options.vendorDisplayName, options.annotationToolName);
1937
1937
  }
1938
- function fitsInstructionsCap(options) {
1939
- return buildReportPointer(options).length <= INSTRUCTIONS_LENGTH_CAP;
1938
+ function fitsBatonSentenceCap(options) {
1939
+ return buildReportPointer(options).length <= BATON_SENTENCE_LENGTH_CAP;
1940
1940
  }
1941
1941
  function buildServerInstructions(options) {
1942
1942
  const rendered = buildReportPointer(options);
1943
- if (rendered.length > INSTRUCTIONS_LENGTH_CAP) {
1943
+ if (rendered.length > BATON_SENTENCE_LENGTH_CAP) {
1944
1944
  throw new Error(
1945
- `Rendered server instructions are ${rendered.length} chars, which exceeds the ${INSTRUCTIONS_LENGTH_CAP}-char safety cap (Claude Code truncates at ~${CLAUDE_CODE_TRUNCATION_CAP}). Shorten vendorDisplayName or annotationToolName.`
1945
+ `Baton's report sentence would be ${rendered.length} chars, over its ${BATON_SENTENCE_LENGTH_CAP}-char limit (Claude Code cuts server instructions at ~${CLAUDE_CODE_TRUNCATION_CAP}). Shorten vendorDisplayName or annotationToolName.`
1946
1946
  );
1947
1947
  }
1948
1948
  return rendered + INSTRUCTIONS_SUBAGENT_CLAUSE;
@@ -1992,6 +1992,30 @@ function mayDeclareParam(schema, name) {
1992
1992
  };
1993
1993
  return walk2(schema);
1994
1994
  }
1995
+ var VENDOR_SERVED_PIECES = [...INSTRUCTION_PIECES, "report_tool.description", "report_tool.refusal"];
1996
+ function servedAgentText(options) {
1997
+ const served = {};
1998
+ if (options.subagentServed) served["instructions.subagent"] = INTENT_INSTRUCTIONS;
1999
+ if (options.goalParamServed) {
2000
+ served["goal_param.description"] = buildUserGoalParamDescription({ intentParamMode: options.intentParamMode });
2001
+ }
2002
+ if (options.reportNames !== void 0) {
2003
+ served["instructions.report_pointer"] = buildReportPointer(options.reportNames);
2004
+ served["report_tool.description"] = buildAnnotationToolDescription(options.reportNames);
2005
+ served["report_tool.refusal"] = buildRefusalText({ annotationToolName: options.reportNames.annotationToolName });
2006
+ }
2007
+ const pieces = {};
2008
+ for (const [piece, text] of Object.entries(served)) {
2009
+ if (options.serverOverlay !== void 0 && INSTRUCTION_PIECES.includes(piece)) {
2010
+ const changed = changedText(text, options.serverOverlay.pieceActions(piece));
2011
+ pieces[piece] = { text: changed.text ?? text, source: changed.source };
2012
+ } else {
2013
+ pieces[piece] = { text, source: "default" };
2014
+ }
2015
+ }
2016
+ if (options.vendorServesText) for (const piece of VENDOR_SERVED_PIECES) pieces[piece] = null;
2017
+ return pieces;
2018
+ }
1995
2019
  function instructionsSuffixServed(annotationToolName, served) {
1996
2020
  if (annotationToolName) return true;
1997
2021
  return typeof served === "string" && served.includes(INTENT_INSTRUCTIONS);
@@ -2011,8 +2035,19 @@ ${ours}` : vendor;
2011
2035
  function vendorText(holder) {
2012
2036
  const current2 = holder._instructions;
2013
2037
  const written = holder[OURS];
2038
+ if (written === void 0) return current2;
2014
2039
  const vendor = VENDOR_INSTRUCTIONS in holder ? holder[VENDOR_INSTRUCTIONS] : current2;
2015
- return written !== void 0 && current2 === joined(vendor, written) ? vendor : current2;
2040
+ if (current2 === joined(vendor, written)) return vendor;
2041
+ return withoutOurs(current2, written) ?? current2;
2042
+ }
2043
+ function withoutOurs(current2, ours) {
2044
+ if (!ours || typeof current2 !== "string") return void 0;
2045
+ const at = current2.lastIndexOf(ours);
2046
+ if (at < 0) return void 0;
2047
+ const before = current2.slice(0, at);
2048
+ const after = current2.slice(at + ours.length);
2049
+ if (before && !before.endsWith("\n\n") || after && !after.startsWith("\n\n")) return void 0;
2050
+ return joined(before.slice(0, -2), after.slice(2));
2016
2051
  }
2017
2052
  function writeOurs(server, ours, subagent, write, warn3 = stderrLine) {
2018
2053
  const holder = server;
@@ -2026,9 +2061,7 @@ function removeSubagentClause(server, write, warn3 = stderrLine) {
2026
2061
  const holder = server;
2027
2062
  const ours = holder[OURS];
2028
2063
  const subagent = holder[SUBAGENT];
2029
- if (ours === void 0 || !subagent || holder._instructions !== joined(holder[VENDOR_INSTRUCTIONS], ours)) {
2030
- return false;
2031
- }
2064
+ if (ours === void 0 || !subagent || vendorText(holder) === holder._instructions) return false;
2032
2065
  const kept = ours.slice(0, ours.length - subagent.length);
2033
2066
  writeOurs(server, kept.endsWith("\n\n") ? kept.slice(0, -2) : kept, null, write, warn3);
2034
2067
  return true;
@@ -2097,9 +2130,9 @@ function toolNameCandidate(serverName) {
2097
2130
  function displayNameFromServer(serverName, tool) {
2098
2131
  if (serverName === void 0) return void 0;
2099
2132
  const proposed = tool.annotationToolName || toolNameCandidate(serverName) || `${tool.vendorId}${SUFFIX}`;
2100
- if (!fitsInstructionsCap({ vendorDisplayName: serverName, annotationToolName: proposed })) {
2133
+ if (!fitsBatonSentenceCap({ vendorDisplayName: serverName, annotationToolName: proposed })) {
2101
2134
  warn(
2102
- `baton: the server name (${serverName.length} chars) does not fit the server-instructions budget as the display name beside the annotation tool ${JSON.stringify(proposed)}; keeping the DSN's server segment. Pass vendorDisplayName to name the server in fewer characters.`
2135
+ `baton: the server name (${serverName.length} chars) does not fit the budget of Baton's report sentence as the display name beside the annotation tool ${JSON.stringify(proposed)}; keeping the DSN's server segment. Pass vendorDisplayName to name the server in fewer characters.`
2103
2136
  );
2104
2137
  return void 0;
2105
2138
  }
@@ -2109,9 +2142,9 @@ function annotationToolNameFromServer(serverName, vendorDisplayName) {
2109
2142
  if (serverName === void 0) return void 0;
2110
2143
  const candidate = toolNameCandidate(serverName);
2111
2144
  if (candidate === void 0) return void 0;
2112
- if (!fitsInstructionsCap({ vendorDisplayName, annotationToolName: candidate })) {
2145
+ if (!fitsBatonSentenceCap({ vendorDisplayName, annotationToolName: candidate })) {
2113
2146
  warn(
2114
- `baton: the server-derived annotation tool name ${JSON.stringify(candidate)} does not fit the server-instructions budget beside the display name (${vendorDisplayName.length} chars); keeping {vendorId}_annotate. A shorter display name gets the readable one.`
2147
+ `baton: the server-derived annotation tool name ${JSON.stringify(candidate)} does not fit the budget of Baton's report sentence beside the display name (${vendorDisplayName.length} chars); keeping {vendorId}_annotate. A shorter display name gets the readable one.`
2115
2148
  );
2116
2149
  return void 0;
2117
2150
  }
@@ -2120,7 +2153,7 @@ function annotationToolNameFromServer(serverName, vendorDisplayName) {
2120
2153
  function toolNameForDisplayName(vendorDisplayName) {
2121
2154
  if (isPlaceholderLiteral(vendorDisplayName)) return void 0;
2122
2155
  const candidate = toolNameCandidate(vendorDisplayName);
2123
- return candidate !== void 0 && fitsInstructionsCap({ vendorDisplayName, annotationToolName: candidate }) ? candidate : void 0;
2156
+ return candidate !== void 0 && fitsBatonSentenceCap({ vendorDisplayName, annotationToolName: candidate }) ? candidate : void 0;
2124
2157
  }
2125
2158
  function deriveAnnotationToolName(vendorId2, override) {
2126
2159
  return validateAnnotationToolName(override || `${vendorId2}${SUFFIX}`);
@@ -2954,6 +2987,14 @@ function takesUserGoalJson(schema) {
2954
2987
  const properties = schema.properties ?? {};
2955
2988
  return isRecord(properties) && !ROOT_COMBINERS.some((key) => key in schema);
2956
2989
  }
2990
+ function goalTextsServed(tools) {
2991
+ const schemas = Array.isArray(tools) ? tools.map((tool) => isRecord(tool) ? tool.inputSchema : void 0) : [];
2992
+ const native = schemas.map((schema) => declaresParam(schema, USER_GOAL_PARAM_NAME));
2993
+ return {
2994
+ goalParamServed: schemas.some((schema, i) => isRecord(schema) && !native[i] && takesUserGoalJson(schema)),
2995
+ subagentServed: !native.some(Boolean)
2996
+ };
2997
+ }
2957
2998
  function withUserGoalJson(schema, intentParamMode, listRequired) {
2958
2999
  if (!takesUserGoalJson(schema) || declaresParam(schema, USER_GOAL_PARAM_NAME)) return schema;
2959
3000
  const properties = schema.properties ?? {};
@@ -3189,13 +3230,13 @@ function sessionIdFromHeader(extra) {
3189
3230
  var TOOL_NAME_PARAM_NAME = "tool_name";
3190
3231
  var ANNOTATION_INPUT_SHAPE = {
3191
3232
  user_goal: import_zod3.z.string(),
3192
- what_happened: import_zod3.z.string().optional(),
3193
3233
  // Nullable so a report whose agent sends an explicit null is taken,
3194
3234
  // not rejected by validation.
3235
+ what_happened: import_zod3.z.string().nullable().optional(),
3195
3236
  tool_name: import_zod3.z.string().nullable().optional(),
3196
- suggested_improvement: import_zod3.z.string().optional(),
3237
+ suggested_improvement: import_zod3.z.string().nullable().optional(),
3197
3238
  // zod renders "any value" as `{}`, which some MCP clients refuse (reportToolSchema.test.ts).
3198
- context: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()).meta({ additionalProperties: true }).optional()
3239
+ context: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.unknown()).meta({ additionalProperties: true }).nullable().optional()
3199
3240
  };
3200
3241
  function isReport(whatHappened) {
3201
3242
  return typeof whatHappened === "string" && !isBlank(whatHappened);
@@ -3274,7 +3315,7 @@ function buildAnnotationTool(server, options) {
3274
3315
  payload: {
3275
3316
  // The agent-facing `user_goal` is stored as the wire key `intent`.
3276
3317
  // eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
3277
- intent: options.scrubber(args.user_goal),
3318
+ intent: args.user_goal ? options.scrubber(args.user_goal) : null,
3278
3319
  // eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
3279
3320
  what_happened: options.scrubber(whatHappened),
3280
3321
  // eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
@@ -3299,7 +3340,7 @@ var PUBLISHABLE_PREFIX = "baton_pk_";
3299
3340
  var SECRET_PREFIX = "baton_sk_";
3300
3341
  var TEST_PREFIX = "baton_tk_";
3301
3342
  var KEY_PREFIXES = [PUBLISHABLE_PREFIX, SECRET_PREFIX, TEST_PREFIX];
3302
- var KEY_RUN = /baton_(?:(?:pk|sk|tk)_[A-Za-z0-9_%-]{8,}|[A-Za-z0-9_%-]{43,})/g;
3343
+ var KEY_RUN = /baton_(?:(?:pk|sk|tk)_[A-Za-z0-9_%-]{8,}|[A-Za-z0-9_%-]{43,})/gi;
3303
3344
  function looksLikeAKey(value) {
3304
3345
  return KEY_PREFIXES.some((prefix) => value.startsWith(prefix));
3305
3346
  }
@@ -4017,7 +4058,8 @@ function buildSeamAugmentations(options) {
4017
4058
  required_names: [...options.requiredParamNames].sort(),
4018
4059
  mode: options.intentParamMode
4019
4060
  },
4020
- instructions_suffix: options.instructionsSuffix
4061
+ instructions_suffix: options.instructionsSuffix,
4062
+ agent_text: options.agentText
4021
4063
  };
4022
4064
  }
4023
4065
 
@@ -4372,7 +4414,13 @@ async function maybeEmitSurfaceSnapshot(ctx) {
4372
4414
  intentParamNames: [USER_GOAL_PARAM_NAME],
4373
4415
  intentParamMode: ctx.intentParamMode,
4374
4416
  requiredParamNames: requiredParamNames(ctx.intentParamMode),
4375
- instructionsSuffix: instructionsSuffixServed(ctx.annotationToolName, ctx.servedInstructions())
4417
+ instructionsSuffix: instructionsSuffixServed(ctx.annotationToolName, ctx.servedInstructions()),
4418
+ agentText: servedAgentText({
4419
+ reportNames: ctx.reportNames(),
4420
+ intentParamMode: ctx.intentParamMode,
4421
+ ...goalTextsServed(snapshot.tools),
4422
+ serverOverlay: ctx.perCaller?.serverOverlay
4423
+ })
4376
4424
  });
4377
4425
  if (ctx.captureSettings !== void 0) seam.server_overlay = overlayId;
4378
4426
  await ctx.emitSurface(ctx.fallbackSessionId, digest, { ...snapshot, seam_augmentations: seam });
@@ -4485,16 +4533,21 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4485
4533
  }
4486
4534
  }
4487
4535
  };
4536
+ const leaveReportNameToVendor = (name) => {
4537
+ warnOnce2(
4538
+ "report-tool-name-taken",
4539
+ `baton: the server lists its own tool ${JSON.stringify(name)}, so Baton's report tool is not added; set annotationToolName to another name`
4540
+ );
4541
+ reportTool2 = void 0;
4542
+ ctx.annotationToolName = "";
4543
+ reportToolTaken();
4544
+ };
4545
+ const takeReportNameIfListed = (tools) => {
4546
+ const name = reportTool2?.name;
4547
+ if (name !== void 0 && tools.some((tool) => tool.name === name)) leaveReportNameToVendor(name);
4548
+ };
4488
4549
  const read = (tools, { surface }) => {
4489
- if (surface && reportTool2 !== void 0 && tools.some((tool) => tool.name === reportTool2?.name)) {
4490
- warnOnce2(
4491
- "report-tool-name-taken",
4492
- `baton: the server lists its own tool ${JSON.stringify(reportTool2.name)}, so Baton's report tool is not added; set annotationToolName to another name`
4493
- );
4494
- reportTool2 = void 0;
4495
- ctx.annotationToolName = "";
4496
- reportToolTaken();
4497
- }
4550
+ if (surface) takeReportNameIfListed(tools);
4498
4551
  readToolList(tools, ctx, surface, skipNote);
4499
4552
  };
4500
4553
  const learn = (tools) => {
@@ -4582,6 +4635,10 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4582
4635
  }
4583
4636
  return batonWrap({ current: name }, () => handler(...args), ctx, unlisted, slot?.caller)(toolArgs, args[1]);
4584
4637
  });
4638
+ return () => {
4639
+ if (Array.isArray(known.given)) takeReportNameIfListed(toolInfos(known.given, () => {
4640
+ }));
4641
+ };
4585
4642
  }
4586
4643
  function lowLevelMajor(lowLevel) {
4587
4644
  return typeof lowLevel._wrapHandler === "function" ? 2 : 1;
@@ -5225,6 +5282,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5225
5282
  server,
5226
5283
  scrubber,
5227
5284
  annotationToolName,
5285
+ reportNames: () => reports && ctx.annotationToolName ? { vendorDisplayName: reports.vendorDisplayName, annotationToolName: ctx.annotationToolName } : void 0,
5228
5286
  intentParamMode,
5229
5287
  configResultCaptureMode: config.resultCaptureMode ?? "full",
5230
5288
  paramRegistry: /* @__PURE__ */ new Map(),
@@ -5255,7 +5313,10 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5255
5313
  get annotationToolName() {
5256
5314
  return ctx.annotationToolName;
5257
5315
  },
5258
- pointer: () => reports && ctx.annotationToolName ? buildReportPointer({ vendorDisplayName: reports.vendorDisplayName, annotationToolName: ctx.annotationToolName }) : void 0,
5316
+ pointer: () => {
5317
+ const names = ctx.reportNames();
5318
+ return names && buildReportPointer(names);
5319
+ },
5259
5320
  writeInstructions,
5260
5321
  subagentLine: () => ![...ctx.paramRegistry.values()].some((d) => d[USER_GOAL_PARAM_NAME] === "native"),
5261
5322
  injectsGoal: (name) => ctx.paramRegistry.get(name)?.[USER_GOAL_PARAM_NAME] === "injected"
@@ -5278,8 +5339,9 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5278
5339
  // `srv-..._annotate` while the instructions name the server-derived one.
5279
5340
  annotationToolName
5280
5341
  };
5342
+ let checkGivenTools;
5281
5343
  if (shape === "lowLevel") {
5282
- installLowLevelToolSeams(
5344
+ checkGivenTools = installLowLevelToolSeams(
5283
5345
  lowLevel,
5284
5346
  ctx,
5285
5347
  annotationOptions && buildAnnotationTool(server, annotationOptions),
@@ -5323,7 +5385,10 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5323
5385
  sessionId: fallbackSessionId,
5324
5386
  annotationToolName: name
5325
5387
  });
5326
- if (shape === "lowLevel") return handle(annotationToolName);
5388
+ if (shape === "lowLevel") {
5389
+ checkGivenTools?.();
5390
+ return handle(annotationToolName);
5391
+ }
5327
5392
  const resolvedAnnotationToolName = annotationOptions ? registerAnnotationTool(server, annotationOptions) : "";
5328
5393
  const existing = internals._registeredTools;
5329
5394
  if (existing) {
@@ -5489,7 +5554,13 @@ function snapshotPayload({ surface, digest }) {
5489
5554
  intentParamNames: [USER_GOAL_PARAM_NAME],
5490
5555
  intentParamMode: DEFAULT_INTENT_PARAM_MODE,
5491
5556
  requiredParamNames: requiredParamNames(DEFAULT_INTENT_PARAM_MODE),
5492
- instructionsSuffix: null
5557
+ instructionsSuffix: null,
5558
+ agentText: servedAgentText({
5559
+ reportNames: void 0,
5560
+ intentParamMode: DEFAULT_INTENT_PARAM_MODE,
5561
+ ...goalTextsServed(surface.tools),
5562
+ vendorServesText: true
5563
+ })
5493
5564
  })
5494
5565
  };
5495
5566
  }
@@ -5947,16 +6018,19 @@ function annotateArgs(args) {
5947
6018
  }
5948
6019
  function reportTool(options) {
5949
6020
  const { vendorDisplayName } = options;
6021
+ if (isBlank(vendorDisplayName)) {
6022
+ throw new Error("vendorDisplayName is blank. Baton's report sentence names the server by it.");
6023
+ }
5950
6024
  const derived = options.name ?? toolNameForDisplayName(vendorDisplayName);
5951
6025
  if (derived === void 0) {
5952
6026
  throw new Error(
5953
- `vendorDisplayName ${JSON.stringify(vendorDisplayName)} gives no usable tool name, or one too long for the instructions. Pass name.`
6027
+ `vendorDisplayName ${JSON.stringify(vendorDisplayName)} gives no usable tool name, or one too long for Baton's report sentence. Pass name.`
5954
6028
  );
5955
6029
  }
5956
6030
  const name = validateAnnotationToolName(derived);
5957
- if (!fitsInstructionsCap({ vendorDisplayName, annotationToolName: name })) {
6031
+ if (!fitsBatonSentenceCap({ vendorDisplayName, annotationToolName: name })) {
5958
6032
  throw new Error(
5959
- `vendorDisplayName and name ${JSON.stringify(name)} are too long together for the instructions. Shorten one of them.`
6033
+ `vendorDisplayName and name ${JSON.stringify(name)} are too long together for Baton's report sentence. Shorten one of them.`
5960
6034
  );
5961
6035
  }
5962
6036
  return {