@goodtiming/baton-sdk 0.5.7 → 0.5.8
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 +4 -3
- package/dist/index.cjs +187 -110
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +33 -20
- package/dist/index.d.ts +33 -20
- package/dist/index.js +186 -110
- 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.5.
|
|
2
|
+
var SDK_VERSION = "ts-0.5.8";
|
|
3
3
|
|
|
4
4
|
// src/events.ts
|
|
5
5
|
import { z } from "zod";
|
|
@@ -769,25 +769,22 @@ function principalFor(principal, onUnregisteredForm) {
|
|
|
769
769
|
}
|
|
770
770
|
|
|
771
771
|
// src/integrations/mcp/llmText.ts
|
|
772
|
-
var
|
|
772
|
+
var REPORT_POINTER_TEMPLATE = (vendorDisplayName, annotationToolName) => `Use \`${annotationToolName}\` to report a ${vendorDisplayName} tool call that went wrong, or a request no tool covers; its description says when and how.`;
|
|
773
|
+
var USER_GOAL_FORMAT = "'<n>: <what this call is for>; success is <what its result should show>'";
|
|
774
|
+
var ANNOTATION_TOOL_DESCRIPTION_TEMPLATE = (vendorDisplayName) => `Report a ${vendorDisplayName} tool call that went wrong, so ${vendorDisplayName} can fix it. Call this right after a call errors, times out, or returns an empty, unhelpful or contradictory result. Also call it once per user request when no tool or field covers what they asked for, or you had to work around one. Do not call it before a tool call or for normal successful work.
|
|
773
775
|
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
IF a ${vendorDisplayName} tool response lacks a structured field for what the user asked about, OR you satisfied the user's intent via a workaround because no tool matched what they asked for, OR the user asked for something this server can't do \u2014 you MUST call \`${annotationToolName}\` once per user request, not per call, with what_happened and tool_name (none if no tool) AND still answer the user with your best inference. Filing the annotation does NOT replace answering.`;
|
|
777
|
-
var ANNOTATION_TOOL_DESCRIPTION_TEMPLATE = (vendorDisplayName) => `Report a ${vendorDisplayName} tool call that went wrong \u2014 call this AFTER a call returns an unhelpful, empty, failed or contradictory result, or when no tool covers what the user asked for. Do NOT call it before a tool call or to narrate normal successful work.
|
|
776
|
+
A report does not replace the work: after it, work out why the call failed, fix the input and try again (do not repeat the same failing call), and still answer the user.
|
|
778
777
|
|
|
779
778
|
Fields:
|
|
780
|
-
- user_goal:
|
|
781
|
-
- what_happened: REQUIRED
|
|
782
|
-
- tool_name: REQUIRED
|
|
783
|
-
-
|
|
784
|
-
- overall_task: short stable label for the broader task this call serves, e.g., 'morning meeting prep', 'pre-outreach research'. REPEAT the exact same string on every call serving the same task; change it only when the user starts a different task.
|
|
785
|
-
- suggested_improvement: A concrete sentence about what product change would have helped.
|
|
779
|
+
- user_goal: the same ${USER_GOAL_FORMAT} as on this server's other tools.
|
|
780
|
+
- what_happened: REQUIRED. 1-2 plain sentences in your own words: what you asked for, what came back, and why it was unusable. Not a category.
|
|
781
|
+
- tool_name: REQUIRED. The ${vendorDisplayName} tool that went wrong, or none if no tool exists for the request.
|
|
782
|
+
- suggested_improvement: one concrete sentence on the product change that would have helped.
|
|
786
783
|
- 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.`;
|
|
787
784
|
var CLAUDE_CODE_TRUNCATION_CAP = 2087;
|
|
788
785
|
var INSTRUCTIONS_LENGTH_CAP = 1500;
|
|
789
786
|
function renderServerInstructions(options) {
|
|
790
|
-
return
|
|
787
|
+
return REPORT_POINTER_TEMPLATE(options.vendorDisplayName, options.annotationToolName);
|
|
791
788
|
}
|
|
792
789
|
function fitsInstructionsCap(options) {
|
|
793
790
|
return renderServerInstructions(options).length <= INSTRUCTIONS_LENGTH_CAP;
|
|
@@ -799,7 +796,7 @@ function buildServerInstructions(options) {
|
|
|
799
796
|
`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.`
|
|
800
797
|
);
|
|
801
798
|
}
|
|
802
|
-
return
|
|
799
|
+
return rendered + INSTRUCTIONS_SUBAGENT_CLAUSE;
|
|
803
800
|
}
|
|
804
801
|
function buildAnnotationToolDescription(options) {
|
|
805
802
|
return ANNOTATION_TOOL_DESCRIPTION_TEMPLATE(options.vendorDisplayName);
|
|
@@ -807,39 +804,68 @@ function buildAnnotationToolDescription(options) {
|
|
|
807
804
|
var USER_GOAL_PARAM_NAME = "user_goal";
|
|
808
805
|
var EXPECTED_RESULT_PARAM_NAME = "expected_result";
|
|
809
806
|
var OVERALL_TASK_PARAM_NAME = "overall_task";
|
|
807
|
+
var RETIRED_PARAM_NAMES = [EXPECTED_RESULT_PARAM_NAME, OVERALL_TASK_PARAM_NAME];
|
|
810
808
|
var INTENT_SOURCE_PARAM = "injected_param";
|
|
811
809
|
var INTENT_SOURCE_LIBRARY = "library_arg";
|
|
812
|
-
var USER_GOAL_PARAM_BODY =
|
|
810
|
+
var USER_GOAL_PARAM_BODY = `Write it as ${USER_GOAL_FORMAT}. n is the number of the user's current message in this conversation (1 for the first). Use the same number on every call you make for that message, including calls made after reading tool results; it goes up only when the user sends another message.`;
|
|
813
811
|
var USER_GOAL_PARAM_DESCRIPTION = "OPTIONAL. " + USER_GOAL_PARAM_BODY;
|
|
814
812
|
var USER_GOAL_PARAM_DESCRIPTION_REQUIRED = "REQUIRED. " + USER_GOAL_PARAM_BODY;
|
|
815
|
-
var
|
|
816
|
-
var
|
|
817
|
-
var EXPECTED_RESULT_PARAM_DESCRIPTION_REQUIRED = "REQUIRED. " + EXPECTED_RESULT_PARAM_BODY;
|
|
818
|
-
var OVERALL_TASK_PARAM_BODY = "The number of the user's current message in this conversation (1 for the first), a colon, then a short stable label for the broader task this call serves (e.g. '3: prepare campaign approval'). Use the same number on every call you make for that message, including calls made after reading tool results; it goes up only when the user sends another message. REPEAT the exact same label text after the colon on every call serving the same task, across messages; change the label only when the user starts a different task.";
|
|
819
|
-
var OVERALL_TASK_PARAM_DESCRIPTION = "OPTIONAL. " + OVERALL_TASK_PARAM_BODY;
|
|
820
|
-
var OVERALL_TASK_PARAM_DESCRIPTION_REQUIRED = "REQUIRED. " + OVERALL_TASK_PARAM_BODY;
|
|
821
|
-
var INSTRUCTIONS_SUBAGENT_CLAUSE = "\n\nWhen you hand work to a subagent that may call this server's tools, tell it the number of the user's current message in this conversation, and that it must start overall_task with that number on every call to this server's tools. This applies only to this server's tools.";
|
|
813
|
+
var INSTRUCTIONS_SUBAGENT_CLAUSE = "\n\nWhen you hand work to a subagent that may call this server's tools, tell it the number of the user's current message in this conversation, and that it must start user_goal with that number on every call to this server's tools. This applies only to this server's tools.";
|
|
814
|
+
var INTENT_INSTRUCTIONS = INSTRUCTIONS_SUBAGENT_CLAUSE.trim();
|
|
822
815
|
function buildUserGoalParamDescription(options = {}) {
|
|
823
816
|
return options.intentParamMode === "required" ? USER_GOAL_PARAM_DESCRIPTION_REQUIRED : USER_GOAL_PARAM_DESCRIPTION;
|
|
824
817
|
}
|
|
825
|
-
function
|
|
826
|
-
return
|
|
818
|
+
function requiredParamNames(intentParamMode) {
|
|
819
|
+
return intentParamMode === "required" ? [USER_GOAL_PARAM_NAME] : [];
|
|
827
820
|
}
|
|
828
|
-
function
|
|
829
|
-
|
|
821
|
+
function declaresParam(schema, name) {
|
|
822
|
+
const props = schema?.properties;
|
|
823
|
+
return typeof props === "object" && props !== null && name in props;
|
|
830
824
|
}
|
|
831
|
-
function
|
|
832
|
-
|
|
825
|
+
function instructionsSuffixServed(annotationToolName, served) {
|
|
826
|
+
if (annotationToolName) return true;
|
|
827
|
+
return typeof served === "string" && served.includes(INTENT_INSTRUCTIONS);
|
|
833
828
|
}
|
|
834
829
|
var INTENT_PARAMS = Object.freeze([
|
|
835
|
-
Object.freeze({ name: USER_GOAL_PARAM_NAME, description: USER_GOAL_PARAM_BODY, traceOption: "intent" })
|
|
836
|
-
Object.freeze({
|
|
837
|
-
name: EXPECTED_RESULT_PARAM_NAME,
|
|
838
|
-
description: EXPECTED_RESULT_PARAM_BODY,
|
|
839
|
-
traceOption: "expectedOutcome"
|
|
840
|
-
}),
|
|
841
|
-
Object.freeze({ name: OVERALL_TASK_PARAM_NAME, description: OVERALL_TASK_PARAM_BODY, traceOption: "workflow" })
|
|
830
|
+
Object.freeze({ name: USER_GOAL_PARAM_NAME, description: USER_GOAL_PARAM_BODY, traceOption: "intent" })
|
|
842
831
|
]);
|
|
832
|
+
var VENDOR_INSTRUCTIONS = /* @__PURE__ */ Symbol("batonVendorInstructions");
|
|
833
|
+
var escapeRegExp = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
834
|
+
var OURS_STARTS = [
|
|
835
|
+
new RegExp(
|
|
836
|
+
"^" + REPORT_POINTER_TEMPLATE("\0", "\0").split("\0").map(escapeRegExp).join(".+?")
|
|
837
|
+
),
|
|
838
|
+
new RegExp("^" + escapeRegExp(INTENT_INSTRUCTIONS))
|
|
839
|
+
];
|
|
840
|
+
function endsInOurs(current2, vendor) {
|
|
841
|
+
if (typeof current2 !== "string") return false;
|
|
842
|
+
const prefix = typeof vendor === "string" && vendor.trim() ? `${vendor}
|
|
843
|
+
|
|
844
|
+
` : "";
|
|
845
|
+
if (!current2.startsWith(prefix)) return false;
|
|
846
|
+
const rest = current2.slice(prefix.length);
|
|
847
|
+
return OURS_STARTS.some((start) => start.test(rest));
|
|
848
|
+
}
|
|
849
|
+
function withVendorInstructions(server, ours, warn4 = (message) => process.stderr.write(message)) {
|
|
850
|
+
const holder = server;
|
|
851
|
+
const current2 = holder._instructions;
|
|
852
|
+
let vendor = VENDOR_INSTRUCTIONS in holder ? holder[VENDOR_INSTRUCTIONS] : current2;
|
|
853
|
+
if (!endsInOurs(current2, vendor)) {
|
|
854
|
+
vendor = current2;
|
|
855
|
+
holder[VENDOR_INSTRUCTIONS] = vendor;
|
|
856
|
+
}
|
|
857
|
+
if (typeof vendor !== "string" || !vendor.trim()) return ours;
|
|
858
|
+
const combined = `${vendor}
|
|
859
|
+
|
|
860
|
+
${ours}`;
|
|
861
|
+
if (combined.length > CLAUDE_CODE_TRUNCATION_CAP) {
|
|
862
|
+
warn4(
|
|
863
|
+
`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
|
|
864
|
+
`
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
return combined;
|
|
868
|
+
}
|
|
843
869
|
|
|
844
870
|
// src/integrations/mcp/annotationName.ts
|
|
845
871
|
var TOOL_NAME_PATTERN = /^[a-zA-Z0-9_-]{1,64}$/;
|
|
@@ -1196,12 +1222,10 @@ function registerAnnotationTool(server, options) {
|
|
|
1196
1222
|
description,
|
|
1197
1223
|
inputSchema: {
|
|
1198
1224
|
user_goal: z2.string(),
|
|
1199
|
-
expected_result: z2.string().optional(),
|
|
1200
1225
|
what_happened: z2.string().optional(),
|
|
1201
1226
|
// Nullable so a report whose agent sends an explicit null is taken,
|
|
1202
1227
|
// not rejected by validation.
|
|
1203
1228
|
tool_name: z2.string().nullable().optional(),
|
|
1204
|
-
overall_task: z2.string().optional(),
|
|
1205
1229
|
suggested_improvement: z2.string().optional(),
|
|
1206
1230
|
context: z2.record(z2.string(), z2.unknown()).optional()
|
|
1207
1231
|
}
|
|
@@ -1248,21 +1272,13 @@ function registerAnnotationTool(server, options) {
|
|
|
1248
1272
|
client_observed: observeClient(extra, { server, scrubber: options.scrubber }),
|
|
1249
1273
|
runtime_meta: scrubbedMeta,
|
|
1250
1274
|
payload: {
|
|
1251
|
-
//
|
|
1252
|
-
// below: `user_goal` is stored as `intent`, `expected_result` as
|
|
1253
|
-
// `expected_outcome`.
|
|
1275
|
+
// The agent-facing `user_goal` is stored as the wire key `intent`.
|
|
1254
1276
|
// eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
|
|
1255
1277
|
intent: options.scrubber(args.user_goal),
|
|
1256
|
-
expected_outcome: args.expected_result ? options.scrubber(args.expected_result) : null,
|
|
1257
1278
|
// eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
|
|
1258
1279
|
what_happened: options.scrubber(whatHappened),
|
|
1259
1280
|
// eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
|
|
1260
1281
|
tool_name: reportedTool ? options.scrubber(reportedTool) : reportedTool,
|
|
1261
|
-
// Agent-facing param `overall_task` -> wire key `workflow`, the same
|
|
1262
|
-
// split the injected params use (`overall_task` -> `call_workflow`):
|
|
1263
|
-
// renaming the param must not move the key the console groups on.
|
|
1264
|
-
// eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
|
|
1265
|
-
workflow: args.overall_task ? options.scrubber(args.overall_task) : null,
|
|
1266
1282
|
suggested_improvement: args.suggested_improvement ? options.scrubber(args.suggested_improvement) : null,
|
|
1267
1283
|
// eslint-disable-next-line no-restricted-syntax -- inside the emit() build thunk: a throw is already contained, and null here would fabricate a shape
|
|
1268
1284
|
context: args.context ? options.scrubber(args.context) : null
|
|
@@ -1548,7 +1564,8 @@ function scrubbedErrorBody(scrubber, text) {
|
|
|
1548
1564
|
}
|
|
1549
1565
|
|
|
1550
1566
|
// src/integrations/mcp/config.ts
|
|
1551
|
-
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required"
|
|
1567
|
+
var INTENT_PARAM_MODES = /* @__PURE__ */ new Set(["optional", "required"]);
|
|
1568
|
+
var REPORT_MODES = /* @__PURE__ */ new Set(["on", "off"]);
|
|
1552
1569
|
var RESULT_CAPTURE_MODE_SET = new Set(RESULT_CAPTURE_MODES);
|
|
1553
1570
|
function resolveTenantId(explicit, vendorId2) {
|
|
1554
1571
|
if (explicit) return explicit;
|
|
@@ -1633,9 +1650,14 @@ function validateBatonConfig(config) {
|
|
|
1633
1650
|
`vendorId ${JSON.stringify(config.vendorId)} must match ${VENDOR_ID_PATTERN.source} \u2014 used as the default annotation tool name prefix.`
|
|
1634
1651
|
);
|
|
1635
1652
|
}
|
|
1636
|
-
if (!config.
|
|
1653
|
+
if (config.reportMode !== void 0 && !REPORT_MODES.has(config.reportMode)) {
|
|
1654
|
+
throw new Error(
|
|
1655
|
+
`BatonConfig.reportMode ${JSON.stringify(config.reportMode)} must be one of ${JSON.stringify([...REPORT_MODES].sort())}.`
|
|
1656
|
+
);
|
|
1657
|
+
}
|
|
1658
|
+
if (!config.vendorDisplayName && config.reportMode === "on") {
|
|
1637
1659
|
throw new Error(
|
|
1638
|
-
|
|
1660
|
+
'BatonConfig needs a vendorDisplayName with reportMode: "on" \u2014 used in server instructions and the annotation tool description (whitelabel obligation, SPEC \xA75.4).'
|
|
1639
1661
|
);
|
|
1640
1662
|
}
|
|
1641
1663
|
if (config.consentToken !== void 0 && !config.consentToken) {
|
|
@@ -1648,6 +1670,11 @@ function validateBatonConfig(config) {
|
|
|
1648
1670
|
"BatonConfig.consentToken is required per SPEC \xA72.3 \u2014 events without a valid consent_token MUST be rejected by the consumer."
|
|
1649
1671
|
);
|
|
1650
1672
|
}
|
|
1673
|
+
if (config.intentParamMode === "off") {
|
|
1674
|
+
throw new Error(
|
|
1675
|
+
'BatonConfig.intentParamMode: "off" would leave nothing capturing what the user is trying to do. Use "required" (the default) or "optional".'
|
|
1676
|
+
);
|
|
1677
|
+
}
|
|
1651
1678
|
if (config.intentParamMode !== void 0 && !INTENT_PARAM_MODES.has(config.intentParamMode)) {
|
|
1652
1679
|
throw new Error(
|
|
1653
1680
|
`BatonConfig.intentParamMode ${JSON.stringify(config.intentParamMode)} must be one of ${JSON.stringify([...INTENT_PARAM_MODES].sort())}.`
|
|
@@ -1833,17 +1860,14 @@ function injectGoalParams(inputSchema, intentParamMode) {
|
|
|
1833
1860
|
function buildIntentFields(existingShape, zodImpl, intentParamMode) {
|
|
1834
1861
|
const dispositions = {};
|
|
1835
1862
|
const toInject = {};
|
|
1836
|
-
for (const
|
|
1837
|
-
[
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
dispositions[name] = "injected";
|
|
1845
|
-
toInject[name] = zodImpl.string().optional().describe(buildDescription({ intentParamMode }));
|
|
1846
|
-
}
|
|
1863
|
+
for (const name of RETIRED_PARAM_NAMES) {
|
|
1864
|
+
if (name in existingShape) dispositions[name] = "native";
|
|
1865
|
+
}
|
|
1866
|
+
if (USER_GOAL_PARAM_NAME in existingShape) {
|
|
1867
|
+
dispositions[USER_GOAL_PARAM_NAME] = "native";
|
|
1868
|
+
} else {
|
|
1869
|
+
dispositions[USER_GOAL_PARAM_NAME] = "injected";
|
|
1870
|
+
toInject[USER_GOAL_PARAM_NAME] = zodImpl.string().optional().describe(buildUserGoalParamDescription({ intentParamMode }));
|
|
1847
1871
|
}
|
|
1848
1872
|
return { dispositions, toInject };
|
|
1849
1873
|
}
|
|
@@ -1923,12 +1947,12 @@ function assembleSurface(serverMeta, tools) {
|
|
|
1923
1947
|
function buildSeamAugmentations(options) {
|
|
1924
1948
|
return {
|
|
1925
1949
|
injected_tools: [...options.injectedToolNames].sort(),
|
|
1926
|
-
intent_param:
|
|
1950
|
+
intent_param: {
|
|
1927
1951
|
names: [...options.intentParamNames].sort(),
|
|
1928
1952
|
required_names: [...options.requiredParamNames].sort(),
|
|
1929
1953
|
mode: options.intentParamMode
|
|
1930
|
-
}
|
|
1931
|
-
instructions_suffix:
|
|
1954
|
+
},
|
|
1955
|
+
instructions_suffix: options.instructionsSuffix
|
|
1932
1956
|
};
|
|
1933
1957
|
}
|
|
1934
1958
|
|
|
@@ -1998,22 +2022,15 @@ async function resolveEnvelopeParts(ctx, extra) {
|
|
|
1998
2022
|
const session = await resolveSessionId(ctx.fallbackSessionId, extra);
|
|
1999
2023
|
return { ...session, scrubbedMeta };
|
|
2000
2024
|
}
|
|
2001
|
-
async function openCall(ctx, toolName, extra, params, registered = true) {
|
|
2025
|
+
async function openCall(ctx, toolName, extra, params, retired, registered = true) {
|
|
2002
2026
|
await maybeEmitSurfaceSnapshot(ctx);
|
|
2003
2027
|
const { sessionId, origin, scrubbedMeta } = await resolveEnvelopeParts(ctx, extra);
|
|
2004
2028
|
const dispositions = ctx.paramRegistry.get(toolName);
|
|
2005
2029
|
const rawIntent = extractGoalParam(params, USER_GOAL_PARAM_NAME, toolName, dispositions, registered);
|
|
2006
|
-
const rawExpected = extractGoalParam(
|
|
2007
|
-
params,
|
|
2008
|
-
EXPECTED_RESULT_PARAM_NAME,
|
|
2009
|
-
toolName,
|
|
2010
|
-
dispositions,
|
|
2011
|
-
registered
|
|
2012
|
-
);
|
|
2013
|
-
const rawWorkflow = extractGoalParam(params, OVERALL_TASK_PARAM_NAME, toolName, dispositions, registered);
|
|
2014
2030
|
const scrubbedIntent = scrubOrNull(ctx.scrubber, rawIntent, USER_GOAL_PARAM_NAME);
|
|
2015
|
-
const scrubbedExpected =
|
|
2016
|
-
|
|
2031
|
+
const [scrubbedExpected = null, scrubbedWorkflow = null] = RETIRED_PARAM_NAMES.map(
|
|
2032
|
+
(name) => dispositions === void 0 || dispositions[name] === "native" ? null : scrubOrNull(ctx.scrubber, retired[name] ?? popText(params, name), name)
|
|
2033
|
+
);
|
|
2017
2034
|
const principal = await resolveCallPrincipal(
|
|
2018
2035
|
ctx.resolvePrincipal,
|
|
2019
2036
|
{ extra, toolName, arguments: params }
|
|
@@ -2060,7 +2077,9 @@ function batonWrap(nameRef, original, ctx) {
|
|
|
2060
2077
|
if (slot) slot.innerFired = true;
|
|
2061
2078
|
const extra = callArgs[callArgs.length - 1];
|
|
2062
2079
|
const params = (callArgs.length > 1 ? callArgs[0] : void 0) ?? {};
|
|
2063
|
-
const
|
|
2080
|
+
const retired = slot?.retired ?? {};
|
|
2081
|
+
if (slot) slot.retired = {};
|
|
2082
|
+
const { common, sessionId } = await openCall(ctx, toolName, extra, params, retired);
|
|
2064
2083
|
const terminate = async (build, replacement) => {
|
|
2065
2084
|
if (!slot) {
|
|
2066
2085
|
await emit(ctx.sink, build);
|
|
@@ -2228,10 +2247,28 @@ function extractGoalParam(args, paramName, toolName, dispositions, registered) {
|
|
|
2228
2247
|
`
|
|
2229
2248
|
);
|
|
2230
2249
|
}
|
|
2250
|
+
return popText(args, paramName);
|
|
2251
|
+
}
|
|
2252
|
+
function popText(args, paramName) {
|
|
2253
|
+
if (!(paramName in args)) return null;
|
|
2231
2254
|
const raw = args[paramName];
|
|
2232
2255
|
delete args[paramName];
|
|
2233
2256
|
return typeof raw === "string" && raw.trim() ? raw : null;
|
|
2234
2257
|
}
|
|
2258
|
+
function stripRetiredParams(params, ctx) {
|
|
2259
|
+
const toolName = params?.name;
|
|
2260
|
+
const args = params?.arguments;
|
|
2261
|
+
if (typeof toolName !== "string") return {};
|
|
2262
|
+
if (toolName === ctx.annotationToolName) return {};
|
|
2263
|
+
if (typeof args !== "object" || args === null || Array.isArray(args)) return {};
|
|
2264
|
+
const dispositions = ctx.paramRegistry.get(toolName);
|
|
2265
|
+
const retired = {};
|
|
2266
|
+
for (const name of RETIRED_PARAM_NAMES) {
|
|
2267
|
+
if (dispositions === void 0 || dispositions[name] === "native" || !(name in args)) continue;
|
|
2268
|
+
retired[name] = popText(args, name);
|
|
2269
|
+
}
|
|
2270
|
+
return retired;
|
|
2271
|
+
}
|
|
2235
2272
|
var MAX_REMEMBERED_SURFACES = 1024;
|
|
2236
2273
|
async function maybeEmitSurfaceSnapshot(ctx) {
|
|
2237
2274
|
if (!ctx.surfaceState.dirty) return;
|
|
@@ -2251,10 +2288,11 @@ async function maybeEmitSurfaceSnapshot(ctx) {
|
|
|
2251
2288
|
if (emitted.size >= MAX_REMEMBERED_SURFACES) emitted.clear();
|
|
2252
2289
|
emitted.add(digest);
|
|
2253
2290
|
const seam = buildSeamAugmentations({
|
|
2254
|
-
injectedToolNames: [ctx.annotationToolName],
|
|
2255
|
-
intentParamNames: [USER_GOAL_PARAM_NAME
|
|
2291
|
+
injectedToolNames: ctx.annotationToolName ? [ctx.annotationToolName] : [],
|
|
2292
|
+
intentParamNames: [USER_GOAL_PARAM_NAME],
|
|
2256
2293
|
intentParamMode: ctx.intentParamMode,
|
|
2257
|
-
requiredParamNames: requiredParamNames(ctx.intentParamMode)
|
|
2294
|
+
requiredParamNames: requiredParamNames(ctx.intentParamMode),
|
|
2295
|
+
instructionsSuffix: instructionsSuffixServed(ctx.annotationToolName, ctx.servedInstructions())
|
|
2258
2296
|
});
|
|
2259
2297
|
try {
|
|
2260
2298
|
await ctx.emitSurface(ctx.fallbackSessionId, digest, { ...snapshot, seam_augmentations: seam });
|
|
@@ -2265,11 +2303,19 @@ async function maybeEmitSurfaceSnapshot(ctx) {
|
|
|
2265
2303
|
ctx.surfaceState.dirty = true;
|
|
2266
2304
|
}
|
|
2267
2305
|
}
|
|
2306
|
+
function declaredIntentParams(vendorSchema) {
|
|
2307
|
+
const declared2 = {};
|
|
2308
|
+
for (const name of [USER_GOAL_PARAM_NAME, ...RETIRED_PARAM_NAMES]) {
|
|
2309
|
+
if (declaresParam(vendorSchema, name)) declared2[name] = "native";
|
|
2310
|
+
}
|
|
2311
|
+
return declared2;
|
|
2312
|
+
}
|
|
2268
2313
|
function captureAndInject(name, entry, ctx) {
|
|
2269
2314
|
if (!entry || typeof entry !== "object") return;
|
|
2270
2315
|
const mutable = entry;
|
|
2271
|
-
|
|
2272
|
-
|
|
2316
|
+
const vendorSchema = ctx.vendorToolJsonSchema(name, mutable.inputSchema);
|
|
2317
|
+
ctx.surfaceState.noteTool(name, vendorSchema, mutable.description);
|
|
2318
|
+
if (declaresParam(vendorSchema, USER_GOAL_PARAM_NAME)) ctx.dropSubagentClause(name);
|
|
2273
2319
|
const isV2 = typeof mutable.executor === "function";
|
|
2274
2320
|
try {
|
|
2275
2321
|
const { schema, dispositions } = isV2 ? injectGoalParamsV2(mutable.inputSchema, ctx.intentParamMode) : injectGoalParams(mutable.inputSchema, ctx.intentParamMode);
|
|
@@ -2277,9 +2323,10 @@ function captureAndInject(name, entry, ctx) {
|
|
|
2277
2323
|
mutable.inputSchema = schema;
|
|
2278
2324
|
ctx.bustSchemaMemo(name);
|
|
2279
2325
|
ctx.paramRegistry.set(name, dispositions);
|
|
2280
|
-
if (dispositions[OVERALL_TASK_PARAM_NAME] === "native") ctx.dropSubagentClause(name);
|
|
2281
2326
|
} else {
|
|
2282
|
-
|
|
2327
|
+
const declared2 = declaredIntentParams(vendorSchema);
|
|
2328
|
+
if (Object.keys(declared2).length > 0) ctx.paramRegistry.set(name, declared2);
|
|
2329
|
+
else ctx.paramRegistry.delete(name);
|
|
2283
2330
|
}
|
|
2284
2331
|
} catch {
|
|
2285
2332
|
}
|
|
@@ -2521,7 +2568,12 @@ function installToolsCallSeam(lowLevel, toolEntry, ctx) {
|
|
|
2521
2568
|
TOOLS_CALL_METHOD,
|
|
2522
2569
|
TOOLS_CALL_SEAM,
|
|
2523
2570
|
(handler) => async (...args) => {
|
|
2524
|
-
const
|
|
2571
|
+
const request = args[0];
|
|
2572
|
+
const slot = {
|
|
2573
|
+
innerFired: false,
|
|
2574
|
+
flushTerminal: null,
|
|
2575
|
+
retired: stripRetiredParams(request?.params, ctx)
|
|
2576
|
+
};
|
|
2525
2577
|
let outcome = CLEAN_OUTCOME;
|
|
2526
2578
|
try {
|
|
2527
2579
|
const result = await callSlots.run(slot, () => handler(...args));
|
|
@@ -2546,13 +2598,13 @@ async function settleCall(slot, outcome, args, toolEntry, ctx) {
|
|
|
2546
2598
|
}
|
|
2547
2599
|
if (slot.innerFired) return;
|
|
2548
2600
|
if (!outcome.failed) return;
|
|
2549
|
-
await emitRequestSideFailure(outcome, args, toolEntry, ctx);
|
|
2601
|
+
await emitRequestSideFailure(outcome, args, slot.retired, toolEntry, ctx);
|
|
2550
2602
|
} catch (err) {
|
|
2551
2603
|
process.stderr.write(`baton: settling a tool call failed; events dropped: ${String(err)}
|
|
2552
2604
|
`);
|
|
2553
2605
|
}
|
|
2554
2606
|
}
|
|
2555
|
-
async function emitRequestSideFailure(outcome, args, toolEntry, ctx) {
|
|
2607
|
+
async function emitRequestSideFailure(outcome, args, retired, toolEntry, ctx) {
|
|
2556
2608
|
const request = args[0];
|
|
2557
2609
|
const toolName = request?.params?.name;
|
|
2558
2610
|
if (typeof toolName !== "string") return;
|
|
@@ -2560,13 +2612,7 @@ async function emitRequestSideFailure(outcome, args, toolEntry, ctx) {
|
|
|
2560
2612
|
const entry = toolEntry(toolName);
|
|
2561
2613
|
if (entry != null && dispatchTarget(entry)?.[BATON_WRAPPED] !== true) return;
|
|
2562
2614
|
const params = { ...request?.params?.arguments ?? {} };
|
|
2563
|
-
const { common, sessionId } = await openCall(
|
|
2564
|
-
ctx,
|
|
2565
|
-
toolName,
|
|
2566
|
-
args[1],
|
|
2567
|
-
params,
|
|
2568
|
-
entry != null
|
|
2569
|
-
);
|
|
2615
|
+
const { common, sessionId } = await openCall(ctx, toolName, args[1], params, retired, entry != null);
|
|
2570
2616
|
const failureKind = requestSideFailureKind(entry);
|
|
2571
2617
|
await emit(
|
|
2572
2618
|
ctx.sink,
|
|
@@ -2684,7 +2730,9 @@ function withBaton(server, supplied = {}) {
|
|
|
2684
2730
|
annotationToolName: ""
|
|
2685
2731
|
});
|
|
2686
2732
|
}
|
|
2687
|
-
|
|
2733
|
+
const resolved = resolveBatonConfig(supplied);
|
|
2734
|
+
warnUnusedReportSettings(resolved);
|
|
2735
|
+
return install(server, supplied, resolved, /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set());
|
|
2688
2736
|
}
|
|
2689
2737
|
function createBaton(supplied = {}) {
|
|
2690
2738
|
const disabledBy = captureDisabled();
|
|
@@ -2695,7 +2743,8 @@ function createBaton(supplied = {}) {
|
|
|
2695
2743
|
}, flush: () => unused.flush(), aclose: () => unused.aclose() };
|
|
2696
2744
|
}
|
|
2697
2745
|
const resolved = resolveBatonConfig(supplied);
|
|
2698
|
-
|
|
2746
|
+
warnUnusedReportSettings(resolved);
|
|
2747
|
+
if (resolved.reportMode === "on") deriveAnnotationToolName(resolved.vendorId, supplied.annotationToolName);
|
|
2699
2748
|
const sink = resolved.sink ?? new StdoutSink();
|
|
2700
2749
|
const config = {
|
|
2701
2750
|
...resolved,
|
|
@@ -2703,23 +2752,25 @@ function createBaton(supplied = {}) {
|
|
|
2703
2752
|
tenantId: resolveTenantId(resolved.tenantId, resolved.vendorId)
|
|
2704
2753
|
};
|
|
2705
2754
|
const emittedSurfaceHashes = /* @__PURE__ */ new Set();
|
|
2755
|
+
const warned2 = /* @__PURE__ */ new Set();
|
|
2706
2756
|
return {
|
|
2707
2757
|
wrap: (server) => {
|
|
2708
|
-
install(server, supplied, config, emittedSurfaceHashes);
|
|
2758
|
+
install(server, supplied, config, emittedSurfaceHashes, warned2);
|
|
2709
2759
|
},
|
|
2710
2760
|
flush: () => sink.flush(),
|
|
2711
2761
|
aclose: () => sink.aclose()
|
|
2712
2762
|
};
|
|
2713
2763
|
}
|
|
2714
|
-
function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
2764
|
+
function install(server, supplied, resolved, emittedSurfaceHashes, warned2) {
|
|
2715
2765
|
const internals = server;
|
|
2716
2766
|
const serverName = usableServerName(() => ({
|
|
2717
2767
|
name: internals.server._serverInfo?.name,
|
|
2718
2768
|
className: internals.constructor.name
|
|
2719
2769
|
}));
|
|
2720
|
-
const config = withServerDisplayName(resolved, supplied, serverName);
|
|
2770
|
+
const config = resolved.reportMode === "on" ? withServerDisplayName(resolved, supplied, serverName) : resolved;
|
|
2721
2771
|
const sink = config.sink ?? new StdoutSink();
|
|
2722
|
-
const
|
|
2772
|
+
const reports = reportsFor(config);
|
|
2773
|
+
const annotationToolName = reports ? resolveAnnotationToolName(serverName, { ...config, vendorDisplayName: reports.vendorDisplayName }) : "";
|
|
2723
2774
|
const intentParamMode = config.intentParamMode ?? "required";
|
|
2724
2775
|
const counter2 = new SessionCounter();
|
|
2725
2776
|
const fallbackSessionId = `sdk-${uuid7()}`;
|
|
@@ -2751,12 +2802,23 @@ function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
|
2751
2802
|
const memo = internals._toolInputSchemaJson;
|
|
2752
2803
|
if (memo && typeof memo === "object") delete memo[name];
|
|
2753
2804
|
};
|
|
2805
|
+
const warnOnce = (key, message) => {
|
|
2806
|
+
if (warned2.has(key)) return;
|
|
2807
|
+
warned2.add(key);
|
|
2808
|
+
process.stderr.write(message);
|
|
2809
|
+
};
|
|
2754
2810
|
const dropSubagentClause = (name) => {
|
|
2755
2811
|
const current2 = internals.server._instructions;
|
|
2756
|
-
if (
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2812
|
+
if (current2 === INTENT_INSTRUCTIONS) {
|
|
2813
|
+
internals.server._instructions = "";
|
|
2814
|
+
} else if (typeof current2 === "string" && current2.includes(INSTRUCTIONS_SUBAGENT_CLAUSE)) {
|
|
2815
|
+
internals.server._instructions = current2.replace(INSTRUCTIONS_SUBAGENT_CLAUSE, "");
|
|
2816
|
+
} else {
|
|
2817
|
+
return;
|
|
2818
|
+
}
|
|
2819
|
+
warnOnce(
|
|
2820
|
+
"native-user-goal",
|
|
2821
|
+
`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
|
|
2760
2822
|
`
|
|
2761
2823
|
);
|
|
2762
2824
|
};
|
|
@@ -2798,15 +2860,16 @@ function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
|
2798
2860
|
vendorToolJsonSchema,
|
|
2799
2861
|
bustSchemaMemo,
|
|
2800
2862
|
dropSubagentClause,
|
|
2863
|
+
servedInstructions: () => internals.server._instructions,
|
|
2801
2864
|
surfaceState,
|
|
2802
2865
|
emitSurface
|
|
2803
2866
|
};
|
|
2804
|
-
const
|
|
2805
|
-
|
|
2806
|
-
|
|
2807
|
-
|
|
2808
|
-
|
|
2809
|
-
|
|
2867
|
+
const ours = reports ? buildServerInstructions({ vendorDisplayName: reports.vendorDisplayName, annotationToolName }) : INTENT_INSTRUCTIONS;
|
|
2868
|
+
internals.server._instructions = withVendorInstructions(
|
|
2869
|
+
internals.server,
|
|
2870
|
+
ours,
|
|
2871
|
+
(message) => warnOnce("instructions-length", message)
|
|
2872
|
+
);
|
|
2810
2873
|
installToolsListSeam(internals.server, ctx);
|
|
2811
2874
|
installToolsCallSeam(
|
|
2812
2875
|
internals.server,
|
|
@@ -2824,12 +2887,12 @@ function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
|
2824
2887
|
ctx
|
|
2825
2888
|
);
|
|
2826
2889
|
installLifecycleSeams(internals.server, ctx);
|
|
2827
|
-
const resolvedAnnotationToolName = registerAnnotationTool(server, {
|
|
2890
|
+
const resolvedAnnotationToolName = !reports ? "" : registerAnnotationTool(server, {
|
|
2828
2891
|
sink,
|
|
2829
2892
|
counter: ctx.counter,
|
|
2830
2893
|
tenantId: ctx.tenantId,
|
|
2831
2894
|
vendorId: ctx.vendorId,
|
|
2832
|
-
vendorDisplayName:
|
|
2895
|
+
vendorDisplayName: reports.vendorDisplayName,
|
|
2833
2896
|
consentToken: ctx.consentToken,
|
|
2834
2897
|
resolvePrincipal: ctx.resolvePrincipal,
|
|
2835
2898
|
fallbackSessionId: ctx.fallbackSessionId,
|
|
@@ -2858,6 +2921,18 @@ function install(server, supplied, resolved, emittedSurfaceHashes) {
|
|
|
2858
2921
|
annotationToolName: resolvedAnnotationToolName
|
|
2859
2922
|
});
|
|
2860
2923
|
}
|
|
2924
|
+
function reportsFor(config) {
|
|
2925
|
+
if (config.reportMode !== "on") return void 0;
|
|
2926
|
+
return { vendorDisplayName: config.vendorDisplayName ?? "" };
|
|
2927
|
+
}
|
|
2928
|
+
function warnUnusedReportSettings(config) {
|
|
2929
|
+
if (config.reportMode !== "on" && config.annotationToolName !== void 0) {
|
|
2930
|
+
process.stderr.write(
|
|
2931
|
+
`baton: annotationToolName=${JSON.stringify(config.annotationToolName)} has no effect with reportMode: "off", because no annotation tool is registered
|
|
2932
|
+
`
|
|
2933
|
+
);
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2861
2936
|
|
|
2862
2937
|
// src/integrations/mcp/oauthHooks.ts
|
|
2863
2938
|
function claimsOf(context) {
|
|
@@ -3377,6 +3452,7 @@ export {
|
|
|
3377
3452
|
EventSchema,
|
|
3378
3453
|
EventTypeSchema,
|
|
3379
3454
|
HttpSink,
|
|
3455
|
+
INTENT_INSTRUCTIONS,
|
|
3380
3456
|
INTENT_PARAMS,
|
|
3381
3457
|
PRINCIPAL_FORM_HASHED,
|
|
3382
3458
|
PRINCIPAL_FORM_RAW,
|