@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.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
- declare const SDK_VERSION = "ts-1.2.1";
3
+ declare const SDK_VERSION = "ts-1.2.2";
4
4
 
5
5
  /**
6
6
  * Zod schemas + TS types for the Baton event stream per SPEC §11.4.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
 
3
- declare const SDK_VERSION = "ts-1.2.1";
3
+ declare const SDK_VERSION = "ts-1.2.2";
4
4
 
5
5
  /**
6
6
  * Zod schemas + TS types for the Baton event stream per SPEC §11.4.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var SDK_VERSION = "ts-1.2.1";
2
+ var SDK_VERSION = "ts-1.2.2";
3
3
 
4
4
  // src/events.ts
5
5
  import { z } from "zod";
@@ -1877,18 +1877,18 @@ Fields:
1877
1877
  - suggested_improvement: one concrete sentence on the product change that would have helped.
1878
1878
  - 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.`;
1879
1879
  var CLAUDE_CODE_TRUNCATION_CAP = 2087;
1880
- var INSTRUCTIONS_LENGTH_CAP = 1500;
1880
+ var BATON_SENTENCE_LENGTH_CAP = 1500;
1881
1881
  function buildReportPointer(options) {
1882
1882
  return REPORT_POINTER_TEMPLATE(options.vendorDisplayName, options.annotationToolName);
1883
1883
  }
1884
- function fitsInstructionsCap(options) {
1885
- return buildReportPointer(options).length <= INSTRUCTIONS_LENGTH_CAP;
1884
+ function fitsBatonSentenceCap(options) {
1885
+ return buildReportPointer(options).length <= BATON_SENTENCE_LENGTH_CAP;
1886
1886
  }
1887
1887
  function buildServerInstructions(options) {
1888
1888
  const rendered = buildReportPointer(options);
1889
- if (rendered.length > INSTRUCTIONS_LENGTH_CAP) {
1889
+ if (rendered.length > BATON_SENTENCE_LENGTH_CAP) {
1890
1890
  throw new Error(
1891
- `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.`
1891
+ `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.`
1892
1892
  );
1893
1893
  }
1894
1894
  return rendered + INSTRUCTIONS_SUBAGENT_CLAUSE;
@@ -1938,6 +1938,30 @@ function mayDeclareParam(schema, name) {
1938
1938
  };
1939
1939
  return walk2(schema);
1940
1940
  }
1941
+ var VENDOR_SERVED_PIECES = [...INSTRUCTION_PIECES, "report_tool.description", "report_tool.refusal"];
1942
+ function servedAgentText(options) {
1943
+ const served = {};
1944
+ if (options.subagentServed) served["instructions.subagent"] = INTENT_INSTRUCTIONS;
1945
+ if (options.goalParamServed) {
1946
+ served["goal_param.description"] = buildUserGoalParamDescription({ intentParamMode: options.intentParamMode });
1947
+ }
1948
+ if (options.reportNames !== void 0) {
1949
+ served["instructions.report_pointer"] = buildReportPointer(options.reportNames);
1950
+ served["report_tool.description"] = buildAnnotationToolDescription(options.reportNames);
1951
+ served["report_tool.refusal"] = buildRefusalText({ annotationToolName: options.reportNames.annotationToolName });
1952
+ }
1953
+ const pieces = {};
1954
+ for (const [piece, text] of Object.entries(served)) {
1955
+ if (options.serverOverlay !== void 0 && INSTRUCTION_PIECES.includes(piece)) {
1956
+ const changed = changedText(text, options.serverOverlay.pieceActions(piece));
1957
+ pieces[piece] = { text: changed.text ?? text, source: changed.source };
1958
+ } else {
1959
+ pieces[piece] = { text, source: "default" };
1960
+ }
1961
+ }
1962
+ if (options.vendorServesText) for (const piece of VENDOR_SERVED_PIECES) pieces[piece] = null;
1963
+ return pieces;
1964
+ }
1941
1965
  function instructionsSuffixServed(annotationToolName, served) {
1942
1966
  if (annotationToolName) return true;
1943
1967
  return typeof served === "string" && served.includes(INTENT_INSTRUCTIONS);
@@ -1957,8 +1981,19 @@ ${ours}` : vendor;
1957
1981
  function vendorText(holder) {
1958
1982
  const current2 = holder._instructions;
1959
1983
  const written = holder[OURS];
1984
+ if (written === void 0) return current2;
1960
1985
  const vendor = VENDOR_INSTRUCTIONS in holder ? holder[VENDOR_INSTRUCTIONS] : current2;
1961
- return written !== void 0 && current2 === joined(vendor, written) ? vendor : current2;
1986
+ if (current2 === joined(vendor, written)) return vendor;
1987
+ return withoutOurs(current2, written) ?? current2;
1988
+ }
1989
+ function withoutOurs(current2, ours) {
1990
+ if (!ours || typeof current2 !== "string") return void 0;
1991
+ const at = current2.lastIndexOf(ours);
1992
+ if (at < 0) return void 0;
1993
+ const before = current2.slice(0, at);
1994
+ const after = current2.slice(at + ours.length);
1995
+ if (before && !before.endsWith("\n\n") || after && !after.startsWith("\n\n")) return void 0;
1996
+ return joined(before.slice(0, -2), after.slice(2));
1962
1997
  }
1963
1998
  function writeOurs(server, ours, subagent, write, warn3 = stderrLine) {
1964
1999
  const holder = server;
@@ -1972,9 +2007,7 @@ function removeSubagentClause(server, write, warn3 = stderrLine) {
1972
2007
  const holder = server;
1973
2008
  const ours = holder[OURS];
1974
2009
  const subagent = holder[SUBAGENT];
1975
- if (ours === void 0 || !subagent || holder._instructions !== joined(holder[VENDOR_INSTRUCTIONS], ours)) {
1976
- return false;
1977
- }
2010
+ if (ours === void 0 || !subagent || vendorText(holder) === holder._instructions) return false;
1978
2011
  const kept = ours.slice(0, ours.length - subagent.length);
1979
2012
  writeOurs(server, kept.endsWith("\n\n") ? kept.slice(0, -2) : kept, null, write, warn3);
1980
2013
  return true;
@@ -2043,9 +2076,9 @@ function toolNameCandidate(serverName) {
2043
2076
  function displayNameFromServer(serverName, tool) {
2044
2077
  if (serverName === void 0) return void 0;
2045
2078
  const proposed = tool.annotationToolName || toolNameCandidate(serverName) || `${tool.vendorId}${SUFFIX}`;
2046
- if (!fitsInstructionsCap({ vendorDisplayName: serverName, annotationToolName: proposed })) {
2079
+ if (!fitsBatonSentenceCap({ vendorDisplayName: serverName, annotationToolName: proposed })) {
2047
2080
  warn(
2048
- `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.`
2081
+ `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.`
2049
2082
  );
2050
2083
  return void 0;
2051
2084
  }
@@ -2055,9 +2088,9 @@ function annotationToolNameFromServer(serverName, vendorDisplayName) {
2055
2088
  if (serverName === void 0) return void 0;
2056
2089
  const candidate = toolNameCandidate(serverName);
2057
2090
  if (candidate === void 0) return void 0;
2058
- if (!fitsInstructionsCap({ vendorDisplayName, annotationToolName: candidate })) {
2091
+ if (!fitsBatonSentenceCap({ vendorDisplayName, annotationToolName: candidate })) {
2059
2092
  warn(
2060
- `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.`
2093
+ `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.`
2061
2094
  );
2062
2095
  return void 0;
2063
2096
  }
@@ -2066,7 +2099,7 @@ function annotationToolNameFromServer(serverName, vendorDisplayName) {
2066
2099
  function toolNameForDisplayName(vendorDisplayName) {
2067
2100
  if (isPlaceholderLiteral(vendorDisplayName)) return void 0;
2068
2101
  const candidate = toolNameCandidate(vendorDisplayName);
2069
- return candidate !== void 0 && fitsInstructionsCap({ vendorDisplayName, annotationToolName: candidate }) ? candidate : void 0;
2102
+ return candidate !== void 0 && fitsBatonSentenceCap({ vendorDisplayName, annotationToolName: candidate }) ? candidate : void 0;
2070
2103
  }
2071
2104
  function deriveAnnotationToolName(vendorId2, override) {
2072
2105
  return validateAnnotationToolName(override || `${vendorId2}${SUFFIX}`);
@@ -2900,6 +2933,14 @@ function takesUserGoalJson(schema) {
2900
2933
  const properties = schema.properties ?? {};
2901
2934
  return isRecord(properties) && !ROOT_COMBINERS.some((key) => key in schema);
2902
2935
  }
2936
+ function goalTextsServed(tools) {
2937
+ const schemas = Array.isArray(tools) ? tools.map((tool) => isRecord(tool) ? tool.inputSchema : void 0) : [];
2938
+ const native = schemas.map((schema) => declaresParam(schema, USER_GOAL_PARAM_NAME));
2939
+ return {
2940
+ goalParamServed: schemas.some((schema, i) => isRecord(schema) && !native[i] && takesUserGoalJson(schema)),
2941
+ subagentServed: !native.some(Boolean)
2942
+ };
2943
+ }
2903
2944
  function withUserGoalJson(schema, intentParamMode, listRequired) {
2904
2945
  if (!takesUserGoalJson(schema) || declaresParam(schema, USER_GOAL_PARAM_NAME)) return schema;
2905
2946
  const properties = schema.properties ?? {};
@@ -3135,13 +3176,13 @@ function sessionIdFromHeader(extra) {
3135
3176
  var TOOL_NAME_PARAM_NAME = "tool_name";
3136
3177
  var ANNOTATION_INPUT_SHAPE = {
3137
3178
  user_goal: z2.string(),
3138
- what_happened: z2.string().optional(),
3139
3179
  // Nullable so a report whose agent sends an explicit null is taken,
3140
3180
  // not rejected by validation.
3181
+ what_happened: z2.string().nullable().optional(),
3141
3182
  tool_name: z2.string().nullable().optional(),
3142
- suggested_improvement: z2.string().optional(),
3183
+ suggested_improvement: z2.string().nullable().optional(),
3143
3184
  // zod renders "any value" as `{}`, which some MCP clients refuse (reportToolSchema.test.ts).
3144
- context: z2.record(z2.string(), z2.unknown()).meta({ additionalProperties: true }).optional()
3185
+ context: z2.record(z2.string(), z2.unknown()).meta({ additionalProperties: true }).nullable().optional()
3145
3186
  };
3146
3187
  function isReport(whatHappened) {
3147
3188
  return typeof whatHappened === "string" && !isBlank(whatHappened);
@@ -3220,7 +3261,7 @@ function buildAnnotationTool(server, options) {
3220
3261
  payload: {
3221
3262
  // The agent-facing `user_goal` is stored as the wire key `intent`.
3222
3263
  // eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
3223
- intent: options.scrubber(args.user_goal),
3264
+ intent: args.user_goal ? options.scrubber(args.user_goal) : null,
3224
3265
  // eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
3225
3266
  what_happened: options.scrubber(whatHappened),
3226
3267
  // eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
@@ -3245,7 +3286,7 @@ var PUBLISHABLE_PREFIX = "baton_pk_";
3245
3286
  var SECRET_PREFIX = "baton_sk_";
3246
3287
  var TEST_PREFIX = "baton_tk_";
3247
3288
  var KEY_PREFIXES = [PUBLISHABLE_PREFIX, SECRET_PREFIX, TEST_PREFIX];
3248
- var KEY_RUN = /baton_(?:(?:pk|sk|tk)_[A-Za-z0-9_%-]{8,}|[A-Za-z0-9_%-]{43,})/g;
3289
+ var KEY_RUN = /baton_(?:(?:pk|sk|tk)_[A-Za-z0-9_%-]{8,}|[A-Za-z0-9_%-]{43,})/gi;
3249
3290
  function looksLikeAKey(value) {
3250
3291
  return KEY_PREFIXES.some((prefix) => value.startsWith(prefix));
3251
3292
  }
@@ -3963,7 +4004,8 @@ function buildSeamAugmentations(options) {
3963
4004
  required_names: [...options.requiredParamNames].sort(),
3964
4005
  mode: options.intentParamMode
3965
4006
  },
3966
- instructions_suffix: options.instructionsSuffix
4007
+ instructions_suffix: options.instructionsSuffix,
4008
+ agent_text: options.agentText
3967
4009
  };
3968
4010
  }
3969
4011
 
@@ -4318,7 +4360,13 @@ async function maybeEmitSurfaceSnapshot(ctx) {
4318
4360
  intentParamNames: [USER_GOAL_PARAM_NAME],
4319
4361
  intentParamMode: ctx.intentParamMode,
4320
4362
  requiredParamNames: requiredParamNames(ctx.intentParamMode),
4321
- instructionsSuffix: instructionsSuffixServed(ctx.annotationToolName, ctx.servedInstructions())
4363
+ instructionsSuffix: instructionsSuffixServed(ctx.annotationToolName, ctx.servedInstructions()),
4364
+ agentText: servedAgentText({
4365
+ reportNames: ctx.reportNames(),
4366
+ intentParamMode: ctx.intentParamMode,
4367
+ ...goalTextsServed(snapshot.tools),
4368
+ serverOverlay: ctx.perCaller?.serverOverlay
4369
+ })
4322
4370
  });
4323
4371
  if (ctx.captureSettings !== void 0) seam.server_overlay = overlayId;
4324
4372
  await ctx.emitSurface(ctx.fallbackSessionId, digest, { ...snapshot, seam_augmentations: seam });
@@ -4431,16 +4479,21 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4431
4479
  }
4432
4480
  }
4433
4481
  };
4482
+ const leaveReportNameToVendor = (name) => {
4483
+ warnOnce2(
4484
+ "report-tool-name-taken",
4485
+ `baton: the server lists its own tool ${JSON.stringify(name)}, so Baton's report tool is not added; set annotationToolName to another name`
4486
+ );
4487
+ reportTool2 = void 0;
4488
+ ctx.annotationToolName = "";
4489
+ reportToolTaken();
4490
+ };
4491
+ const takeReportNameIfListed = (tools) => {
4492
+ const name = reportTool2?.name;
4493
+ if (name !== void 0 && tools.some((tool) => tool.name === name)) leaveReportNameToVendor(name);
4494
+ };
4434
4495
  const read = (tools, { surface }) => {
4435
- if (surface && reportTool2 !== void 0 && tools.some((tool) => tool.name === reportTool2?.name)) {
4436
- warnOnce2(
4437
- "report-tool-name-taken",
4438
- `baton: the server lists its own tool ${JSON.stringify(reportTool2.name)}, so Baton's report tool is not added; set annotationToolName to another name`
4439
- );
4440
- reportTool2 = void 0;
4441
- ctx.annotationToolName = "";
4442
- reportToolTaken();
4443
- }
4496
+ if (surface) takeReportNameIfListed(tools);
4444
4497
  readToolList(tools, ctx, surface, skipNote);
4445
4498
  };
4446
4499
  const learn = (tools) => {
@@ -4528,6 +4581,10 @@ function installLowLevelToolSeams(lowLevel, ctx, annotationTool, known, warnOnce
4528
4581
  }
4529
4582
  return batonWrap({ current: name }, () => handler(...args), ctx, unlisted, slot?.caller)(toolArgs, args[1]);
4530
4583
  });
4584
+ return () => {
4585
+ if (Array.isArray(known.given)) takeReportNameIfListed(toolInfos(known.given, () => {
4586
+ }));
4587
+ };
4531
4588
  }
4532
4589
  function lowLevelMajor(lowLevel) {
4533
4590
  return typeof lowLevel._wrapHandler === "function" ? 2 : 1;
@@ -5171,6 +5228,7 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5171
5228
  server,
5172
5229
  scrubber,
5173
5230
  annotationToolName,
5231
+ reportNames: () => reports && ctx.annotationToolName ? { vendorDisplayName: reports.vendorDisplayName, annotationToolName: ctx.annotationToolName } : void 0,
5174
5232
  intentParamMode,
5175
5233
  configResultCaptureMode: config.resultCaptureMode ?? "full",
5176
5234
  paramRegistry: /* @__PURE__ */ new Map(),
@@ -5201,7 +5259,10 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5201
5259
  get annotationToolName() {
5202
5260
  return ctx.annotationToolName;
5203
5261
  },
5204
- pointer: () => reports && ctx.annotationToolName ? buildReportPointer({ vendorDisplayName: reports.vendorDisplayName, annotationToolName: ctx.annotationToolName }) : void 0,
5262
+ pointer: () => {
5263
+ const names = ctx.reportNames();
5264
+ return names && buildReportPointer(names);
5265
+ },
5205
5266
  writeInstructions,
5206
5267
  subagentLine: () => ![...ctx.paramRegistry.values()].some((d) => d[USER_GOAL_PARAM_NAME] === "native"),
5207
5268
  injectsGoal: (name) => ctx.paramRegistry.get(name)?.[USER_GOAL_PARAM_NAME] === "injected"
@@ -5224,8 +5285,9 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5224
5285
  // `srv-..._annotate` while the instructions name the server-derived one.
5225
5286
  annotationToolName
5226
5287
  };
5288
+ let checkGivenTools;
5227
5289
  if (shape === "lowLevel") {
5228
- installLowLevelToolSeams(
5290
+ checkGivenTools = installLowLevelToolSeams(
5229
5291
  lowLevel,
5230
5292
  ctx,
5231
5293
  annotationOptions && buildAnnotationTool(server, annotationOptions),
@@ -5269,7 +5331,10 @@ function install(server, supplied, resolved, { sentSurfaces, warned: warned4, co
5269
5331
  sessionId: fallbackSessionId,
5270
5332
  annotationToolName: name
5271
5333
  });
5272
- if (shape === "lowLevel") return handle(annotationToolName);
5334
+ if (shape === "lowLevel") {
5335
+ checkGivenTools?.();
5336
+ return handle(annotationToolName);
5337
+ }
5273
5338
  const resolvedAnnotationToolName = annotationOptions ? registerAnnotationTool(server, annotationOptions) : "";
5274
5339
  const existing = internals._registeredTools;
5275
5340
  if (existing) {
@@ -5435,7 +5500,13 @@ function snapshotPayload({ surface, digest }) {
5435
5500
  intentParamNames: [USER_GOAL_PARAM_NAME],
5436
5501
  intentParamMode: DEFAULT_INTENT_PARAM_MODE,
5437
5502
  requiredParamNames: requiredParamNames(DEFAULT_INTENT_PARAM_MODE),
5438
- instructionsSuffix: null
5503
+ instructionsSuffix: null,
5504
+ agentText: servedAgentText({
5505
+ reportNames: void 0,
5506
+ intentParamMode: DEFAULT_INTENT_PARAM_MODE,
5507
+ ...goalTextsServed(surface.tools),
5508
+ vendorServesText: true
5509
+ })
5439
5510
  })
5440
5511
  };
5441
5512
  }
@@ -5893,16 +5964,19 @@ function annotateArgs(args) {
5893
5964
  }
5894
5965
  function reportTool(options) {
5895
5966
  const { vendorDisplayName } = options;
5967
+ if (isBlank(vendorDisplayName)) {
5968
+ throw new Error("vendorDisplayName is blank. Baton's report sentence names the server by it.");
5969
+ }
5896
5970
  const derived = options.name ?? toolNameForDisplayName(vendorDisplayName);
5897
5971
  if (derived === void 0) {
5898
5972
  throw new Error(
5899
- `vendorDisplayName ${JSON.stringify(vendorDisplayName)} gives no usable tool name, or one too long for the instructions. Pass name.`
5973
+ `vendorDisplayName ${JSON.stringify(vendorDisplayName)} gives no usable tool name, or one too long for Baton's report sentence. Pass name.`
5900
5974
  );
5901
5975
  }
5902
5976
  const name = validateAnnotationToolName(derived);
5903
- if (!fitsInstructionsCap({ vendorDisplayName, annotationToolName: name })) {
5977
+ if (!fitsBatonSentenceCap({ vendorDisplayName, annotationToolName: name })) {
5904
5978
  throw new Error(
5905
- `vendorDisplayName and name ${JSON.stringify(name)} are too long together for the instructions. Shorten one of them.`
5979
+ `vendorDisplayName and name ${JSON.stringify(name)} are too long together for Baton's report sentence. Shorten one of them.`
5906
5980
  );
5907
5981
  }
5908
5982
  return {